34void VKShaderInterface::compute_resource_counts(InitContext &ctx)
52 switch (res.bind_type) {
77 static char PUSH_CONSTANTS_FALLBACK_NAME[] =
"push_constants_fallback";
78 static size_t PUSH_CONSTANTS_FALLBACK_NAME_LEN = strlen(PUSH_CONSTANTS_FALLBACK_NAME);
79 static char SUBPASS_FALLBACK_NAME[] =
"gpu_subpass_img_0";
80 static size_t SUBPASS_FALLBACK_NAME_LEN = strlen(SUBPASS_FALLBACK_NAME);
84 names_size += PUSH_CONSTANTS_FALLBACK_NAME_LEN + 1;
94 ctx.name_buffer_offset = 0;
97void VKShaderInterface::populate_shader_inputs(InitContext &ctx)
99 const shader::ShaderCreateInfo &info = ctx.info;
100 using namespace blender::gpu::shader;
102 Vector<ShaderCreateInfo::Resource> all_resources;
108 for (
const ShaderCreateInfo::VertIn &attr : info.
vertex_inputs_) {
110 ctx.input_ptr->location = ctx.input_ptr->binding = attr.
index;
111 if (ctx.input_ptr->location != -1) {
119 for (
const ShaderCreateInfo::Resource &res : all_resources) {
120 if (res.bind_type == ShaderCreateInfo::Resource::BindType::UNIFORM_BUFFER) {
122 ctx.input_ptr->location = ctx.input_ptr->binding = res.slot;
128 static char PUSH_CONSTANTS_FALLBACK_NAME[] =
"push_constants_fallback";
131 ctx.input_ptr, PUSH_CONSTANTS_FALLBACK_NAME,
name_buffer_, ctx.name_buffer_offset);
132 ctx.input_ptr->location = ctx.input_ptr->binding = -1;
137 static char SUBPASS_FALLBACK_NAME[] =
"gpu_subpass_img_0";
140 ctx.input_ptr->location = ctx.input_ptr->binding =
subpass_in.index;
144 for (
const ShaderCreateInfo::Resource &res : all_resources) {
145 if (res.bind_type == ShaderCreateInfo::Resource::BindType::SAMPLER) {
147 ctx.input_ptr->location = ctx.input_ptr->binding = res.slot;
150 else if (res.bind_type == ShaderCreateInfo::Resource::BindType::IMAGE) {
152 ctx.input_ptr->location = ctx.input_ptr->binding = res.slot + BIND_SPACE_IMAGE_OFFSET;
160 int32_t push_constant_location = 1024;
163 ctx.input_ptr->location = push_constant_location++;
164 ctx.input_ptr->binding = -1;
169 for (
const ShaderCreateInfo::Resource &res : all_resources) {
170 if (res.bind_type == ShaderCreateInfo::Resource::BindType::STORAGE_BUFFER) {
172 ctx.input_ptr->location = ctx.input_ptr->binding = res.slot;
178 if (res.
bind_type == ShaderCreateInfo::Resource::BindType::STORAGE_BUFFER) {
182 BLI_assert_msg(0,
"Resource type is not supported for Geometry frequency");
187 for (
const ShaderCreateInfo::Resource &res : all_resources) {
188 if (res.
bind_type == ShaderCreateInfo::Resource::BindType::ACCELERATION_STRUCTURE) {
190 ctx.input_ptr, res.acceleration_structure.name,
name_buffer_, ctx.name_buffer_offset);
191 ctx.input_ptr->location = ctx.input_ptr->binding = res.
slot;
200 ctx.input_ptr->location = constant_id++;
207void VKShaderInterface::populate_builtins()
212 builtins_[u] = (uni !=
nullptr) ? uni->location : -1;
222void VKShaderInterface::populate_resource_bindings(InitContext &ctx)
224 const shader::ShaderCreateInfo &info = ctx.info;
225 using namespace blender::gpu::shader;
227 Vector<ShaderCreateInfo::Resource> all_resources;
234 const uint32_t resources_len = input_tot_len;
236 init_descriptor_set_layout_info(
237 info, resources_len, all_resources, ctx.push_constants_storage_type);
240 resource_bindings_.fill({});
244 const ShaderInput *input = ctx.supports_local_read ?
246 shader_input_get(ShaderCreateInfo::Resource::BindType::
SAMPLER,
250 descriptor_set_location_update(input,
256 for (ShaderCreateInfo::Resource &res : all_resources) {
257 const ShaderInput *input = shader_input_get(res);
260 if (res.
bind_type == ShaderCreateInfo::Resource::BindType::IMAGE) {
261 arrayed =
ELEM(res.image.type,
262 shader::ImageType::Float1DArray,
263 shader::ImageType::Float2DArray,
264 shader::ImageType::FloatCubeArray,
265 shader::ImageType::Int1DArray,
266 shader::ImageType::Int2DArray,
267 shader::ImageType::IntCubeArray,
268 shader::ImageType::Uint1DArray,
269 shader::ImageType::Uint2DArray,
270 shader::ImageType::UintCubeArray,
271 shader::ImageType::AtomicUint2DArray,
272 shader::ImageType::AtomicInt2DArray) ?
276 else if (res.
bind_type == ShaderCreateInfo::Resource::BindType::SAMPLER) {
277 arrayed =
ELEM(res.sampler.type,
278 shader::ImageType::Float1DArray,
279 shader::ImageType::Float2DArray,
280 shader::ImageType::FloatCubeArray,
281 shader::ImageType::Int1DArray,
282 shader::ImageType::Int2DArray,
283 shader::ImageType::IntCubeArray,
284 shader::ImageType::Uint1DArray,
285 shader::ImageType::Uint2DArray,
286 shader::ImageType::UintCubeArray,
287 shader::ImageType::Shadow2DArray,
288 shader::ImageType::ShadowCubeArray,
289 shader::ImageType::Depth2DArray,
290 shader::ImageType::DepthCubeArray,
291 shader::ImageType::AtomicUint2DArray,
292 shader::ImageType::AtomicInt2DArray) ?
301 int32_t push_constant_descriptor_set_location = -1;
304 const ShaderInput *push_constant_input =
ubo_get(
"push_constants_fallback");
305 const int32_t push_constants_fallback_location = -1;
306 descriptor_set_location_update(push_constant_input,
307 push_constants_fallback_location,
312 push_constants_layout_.init(
313 info, *
this, ctx.push_constants_storage_type, push_constant_descriptor_set_location);
318 InitContext ctx{info};
322 compute_resource_counts(ctx);
323 populate_shader_inputs(ctx);
325 populate_resource_bindings(ctx);
335void VKShaderInterface::descriptor_set_location_update(
339 std::optional<const shader::ShaderCreateInfo::Resource> resource,
344 "Incorrect parameters, when no resource is given, it must be the uniform buffer "
345 "for storing push constants or input attachment for subpass inputs.");
347 "Incorrect parameter, bind types do not match.");
355 VkAccessFlags vk_access_flags = VK_ACCESS_NONE;
356 if (resource.has_value()) {
357 switch (resource->bind_type) {
359 vk_access_flags |= VK_ACCESS_UNIFORM_READ_BIT;
364 vk_access_flags |= VK_ACCESS_SHADER_READ_BIT;
367 vk_access_flags |= VK_ACCESS_SHADER_WRITE_BIT;
373 vk_access_flags |= VK_ACCESS_SHADER_READ_BIT;
376 vk_access_flags |= VK_ACCESS_SHADER_WRITE_BIT;
381 vk_access_flags |= VK_ACCESS_SHADER_READ_BIT;
385 vk_access_flags |= VK_ACCESS_ACCELERATION_STRUCTURE_READ_BIT_KHR;
390 vk_access_flags |= VK_ACCESS_UNIFORM_READ_BIT;
393 vk_access_flags |= supports_local_read ? VK_ACCESS_INPUT_ATTACHMENT_READ_BIT |
394 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT :
395 VK_ACCESS_SHADER_READ_BIT;
398 VKResourceBinding &resource_binding = resource_bindings_[
index];
401 resource_binding.
location = location;
402 resource_binding.
arrayed = arrayed;
410 return resource_bindings_[
index];
416 const ShaderInput *shader_input = shader_input_get(resource);
418 return resource_binding_info(shader_input).location;
424 const ShaderInput *shader_input = shader_input_get(bind_type, binding);
425 if (shader_input ==
nullptr) {
435const ShaderInput *VKShaderInterface::shader_input_get(
441const ShaderInput *VKShaderInterface::shader_input_get(
449 return texture_get((binding >= BIND_SPACE_IMAGE_OFFSET) ? binding :
450 binding + BIND_SPACE_IMAGE_OFFSET);
463void VKShaderInterface::init_descriptor_set_layout_info(
466 Span<shader::ShaderCreateInfo::Resource> all_resources,
469 BLI_assert(descriptor_set_layout_info_.bindings.is_empty());
473 descriptor_set_layout_info_.bindings.reserve(resources_len);
475 descriptor_set_layout_info_.vk_shader_stage_flags = VK_SHADER_STAGE_COMPUTE_BIT;
478 descriptor_set_layout_info_.vk_shader_stage_flags = VK_SHADER_STAGE_FRAGMENT_BIT;
481 descriptor_set_layout_info_.vk_shader_stage_flags = VK_SHADER_STAGE_ALL_GRAPHICS;
486 descriptor_set_layout_info_.bindings.append_n_times(
488 VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT,
491 for (
const shader::ShaderCreateInfo::Resource &res : all_resources) {
495 descriptor_set_layout_info_.bindings.append(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
#define BLI_assert_unreachable()
#define BLI_assert_msg(a, msg)
Array(Allocator allocator={}) noexcept
constexpr IndexRange()=default
void extend(Span< T > array)
constexpr bool is_empty() const
void copy_input_name(ShaderInput *input, const StringRefNull &name, char *name_buffer, uint32_t &name_buffer_offset) const
void set_image_formats_from_info(const shader::ShaderCreateInfo &info)
const ShaderInput * uniform_get(const StringRefNull name) const
const ShaderInput * texture_get(const int binding) const
const char * input_name_get(const ShaderInput *input) const
static const char * builtin_uniform_block_name(GPUUniformBlockBuiltin u)
const ShaderInput * ubo_get(const StringRefNull name) const
const ShaderInput * ssbo_get(const StringRefNull name) const
int32_t builtin_blocks_[GPU_NUM_UNIFORM_BLOCKS]
int32_t builtins_[GPU_NUM_UNIFORMS]
uint16_t enabled_attr_mask_
const ShaderInput * tlas_get(const StringRefNull name) const
uint8_t attr_types_[GPU_VERT_ATTR_MAX_LEN]
static const char * builtin_uniform_name(GPUUniformBuiltin u)
const VKExtensions & extensions_get() const
VKDescriptorSet::Location descriptor_set_location(const shader::ShaderCreateInfo::Resource &resource) const
void init(const shader::ShaderCreateInfo &info)
VkDescriptorType to_vk_descriptor_type(const shader::ShaderCreateInfo::Resource &resource)
static int32_t shader_input_index(const ShaderInput *shader_inputs, const ShaderInput *shader_input)
static VKBindType to_bind_type(shader::ShaderCreateInfo::Resource::BindType bind_type)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
bool dynamic_rendering_local_read
static StorageType determine_storage_type(const shader::ShaderCreateInfo &info, const VKDevice &device)
VKImageViewArrayed arrayed
VkAccessFlags access_mask
VKDescriptorSet::Location location
Describe inputs & outputs, stage interfaces, resources and sources of a shader. If all data is correc...
StringRefNull compute_source_
Vector< Resource, 0 > geometry_resources_
Vector< SpecializationConstant, 0 > specialization_constants_
Vector< VertIn, 0 > vertex_inputs_
size_t interface_names_size_
Vector< Resource, 0 > pass_resources_
Vector< PushConst, 0 > push_constants_
Vector< SubpassIn, 0 > subpass_inputs_
Vector< Resource, 0 > batch_resources_
std::string compute_source_generated
BuiltinBits builtins_combined() const