Blender V5.3
vk_shader_interface.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10#include "vk_backend.hh"
11#include "vk_context.hh"
12#include "vk_state_manager.hh"
13
14namespace blender::gpu {
15
33
34void VKShaderInterface::compute_resource_counts(InitContext &ctx)
35{
36 const shader::ShaderCreateInfo &info = ctx.info;
37 using namespace blender::gpu::shader;
38
39 attr_len_ = info.vertex_inputs_.size();
40 uniform_len_ = info.push_constants_.size();
42 ssbo_len_ = 0;
43 ubo_len_ = 0;
44 tlas_len_ = 0;
45
47 all_resources.extend(info.pass_resources_);
48 all_resources.extend(info.batch_resources_);
49 all_resources.extend(info.geometry_resources_);
50
51 for (ShaderCreateInfo::Resource &res : all_resources) {
52 switch (res.bind_type) {
56 break;
58 ubo_len_++;
59 break;
61 ssbo_len_++;
62 break;
64 tlas_len_++;
65 break;
66 }
67 }
68
69 const VKDevice &device = VKBackend::get().device;
70 ctx.supports_local_read = device.extensions_get().dynamic_rendering_local_read;
71 uniform_len_ += info.subpass_inputs_.size();
72
73 /* Reserve 1 uniform buffer for push constants fallback. */
74 size_t names_size = info.interface_names_size_;
75 ctx.push_constants_storage_type = VKPushConstants::Layout::determine_storage_type(info, device);
76
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);
81
82 if (ctx.push_constants_storage_type == VKPushConstants::StorageType::BUFFER) {
83 ubo_len_++;
84 names_size += PUSH_CONSTANTS_FALLBACK_NAME_LEN + 1;
85 }
86 names_size += info.subpass_inputs_.size() * SUBPASS_FALLBACK_NAME_LEN;
87
90 inputs_ = MEM_new_array_zeroed<ShaderInput>(input_tot_len, __func__);
91 ctx.input_ptr = inputs_;
92
93 name_buffer_ = MEM_new_array_uninitialized<char>(names_size, "name_buffer");
94 ctx.name_buffer_offset = 0;
95}
96
97void VKShaderInterface::populate_shader_inputs(InitContext &ctx)
98{
99 const shader::ShaderCreateInfo &info = ctx.info;
100 using namespace blender::gpu::shader;
101
102 Vector<ShaderCreateInfo::Resource> all_resources;
103 all_resources.extend(info.pass_resources_);
104 all_resources.extend(info.batch_resources_);
105 all_resources.extend(info.geometry_resources_);
106
107 /* Attributes */
108 for (const ShaderCreateInfo::VertIn &attr : info.vertex_inputs_) {
109 copy_input_name(ctx.input_ptr, attr.name, name_buffer_, ctx.name_buffer_offset);
110 ctx.input_ptr->location = ctx.input_ptr->binding = attr.index;
111 if (ctx.input_ptr->location != -1) {
112 enabled_attr_mask_ |= (1 << ctx.input_ptr->location);
113 attr_types_[ctx.input_ptr->location] = uint8_t(attr.type);
114 }
115 ctx.input_ptr++;
116 }
117
118 /* Uniform blocks */
119 for (const ShaderCreateInfo::Resource &res : all_resources) {
120 if (res.bind_type == ShaderCreateInfo::Resource::BindType::UNIFORM_BUFFER) {
121 copy_input_name(ctx.input_ptr, res.uniformbuf.name, name_buffer_, ctx.name_buffer_offset);
122 ctx.input_ptr->location = ctx.input_ptr->binding = res.slot;
123 ctx.input_ptr++;
124 }
125 }
126
127 /* Add push constant when using uniform buffer as a fallback. */
128 static char PUSH_CONSTANTS_FALLBACK_NAME[] = "push_constants_fallback";
129 if (ctx.push_constants_storage_type == VKPushConstants::StorageType::BUFFER) {
131 ctx.input_ptr, PUSH_CONSTANTS_FALLBACK_NAME, name_buffer_, ctx.name_buffer_offset);
132 ctx.input_ptr->location = ctx.input_ptr->binding = -1;
133 ctx.input_ptr++;
134 }
135
136 /* Images, Samplers and buffers. */
137 static char SUBPASS_FALLBACK_NAME[] = "gpu_subpass_img_0";
138 for (const ShaderCreateInfo::SubpassIn &subpass_in : info.subpass_inputs_) {
139 copy_input_name(ctx.input_ptr, SUBPASS_FALLBACK_NAME, name_buffer_, ctx.name_buffer_offset);
140 ctx.input_ptr->location = ctx.input_ptr->binding = subpass_in.index;
141 ctx.input_ptr++;
142 }
143
144 for (const ShaderCreateInfo::Resource &res : all_resources) {
145 if (res.bind_type == ShaderCreateInfo::Resource::BindType::SAMPLER) {
146 copy_input_name(ctx.input_ptr, res.sampler.name, name_buffer_, ctx.name_buffer_offset);
147 ctx.input_ptr->location = ctx.input_ptr->binding = res.slot;
148 ctx.input_ptr++;
149 }
150 else if (res.bind_type == ShaderCreateInfo::Resource::BindType::IMAGE) {
151 copy_input_name(ctx.input_ptr, res.image.name, name_buffer_, ctx.name_buffer_offset);
152 ctx.input_ptr->location = ctx.input_ptr->binding = res.slot + BIND_SPACE_IMAGE_OFFSET;
153 ctx.input_ptr++;
154 }
155 }
156
158
159 /* Push constants. */
160 int32_t push_constant_location = 1024;
161 for (const ShaderCreateInfo::PushConst &push_constant : info.push_constants_) {
162 copy_input_name(ctx.input_ptr, push_constant.name, name_buffer_, ctx.name_buffer_offset);
163 ctx.input_ptr->location = push_constant_location++;
164 ctx.input_ptr->binding = -1;
165 ctx.input_ptr++;
166 }
167
168 /* Storage buffers */
169 for (const ShaderCreateInfo::Resource &res : all_resources) {
170 if (res.bind_type == ShaderCreateInfo::Resource::BindType::STORAGE_BUFFER) {
171 copy_input_name(ctx.input_ptr, res.storagebuf.name, name_buffer_, ctx.name_buffer_offset);
172 ctx.input_ptr->location = ctx.input_ptr->binding = res.slot;
173 ctx.input_ptr++;
174 }
175 }
176
177 for (const ShaderCreateInfo::Resource &res : info.geometry_resources_) {
178 if (res.bind_type == ShaderCreateInfo::Resource::BindType::STORAGE_BUFFER) {
179 ssbo_attr_mask_ |= (1 << res.slot);
180 }
181 else {
182 BLI_assert_msg(0, "Resource type is not supported for Geometry frequency");
183 }
184 }
185
186 /* Acceleration structures */
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;
192 ctx.input_ptr++;
193 }
194 }
195
196 /* Constants */
197 int constant_id = 0;
198 for (const SpecializationConstant &constant : info.specialization_constants_) {
199 copy_input_name(ctx.input_ptr, constant.name, name_buffer_, ctx.name_buffer_offset);
200 ctx.input_ptr->location = constant_id++;
201 ctx.input_ptr++;
202 }
203
204 sort_inputs();
205}
206
207void VKShaderInterface::populate_builtins()
208{
209 for (int32_t u_int = 0; u_int < GPU_NUM_UNIFORMS; u_int++) {
210 GPUUniformBuiltin u = static_cast<GPUUniformBuiltin>(u_int);
211 const ShaderInput *uni = this->uniform_get(builtin_uniform_name(u));
212 builtins_[u] = (uni != nullptr) ? uni->location : -1;
213 }
214
215 for (int32_t u_int = 0; u_int < GPU_NUM_UNIFORM_BLOCKS; u_int++) {
216 GPUUniformBlockBuiltin u = static_cast<GPUUniformBlockBuiltin>(u_int);
217 const ShaderInput *block = this->ubo_get(builtin_uniform_block_name(u));
218 builtin_blocks_[u] = (block != nullptr) ? block->binding : -1;
219 }
220}
221
222void VKShaderInterface::populate_resource_bindings(InitContext &ctx)
223{
224 const shader::ShaderCreateInfo &info = ctx.info;
225 using namespace blender::gpu::shader;
226
227 Vector<ShaderCreateInfo::Resource> all_resources;
228 all_resources.extend(info.pass_resources_);
229 all_resources.extend(info.batch_resources_);
230 all_resources.extend(info.geometry_resources_);
231
233 tlas_len_;
234 const uint32_t resources_len = input_tot_len;
235
236 init_descriptor_set_layout_info(
237 info, resources_len, all_resources, ctx.push_constants_storage_type);
238
239 resource_bindings_ = Array<VKResourceBinding>(resources_len);
240 resource_bindings_.fill({});
241
243 for (const ShaderCreateInfo::SubpassIn &subpass_in : info.subpass_inputs_) {
244 const ShaderInput *input = ctx.supports_local_read ?
245 texture_get(subpass_in.index) :
246 shader_input_get(ShaderCreateInfo::Resource::BindType::SAMPLER,
248 BLI_assert(input);
249 BLI_assert(STREQ(input_name_get(input), "gpu_subpass_img_0"));
250 descriptor_set_location_update(input,
253 std::nullopt,
255 }
256 for (ShaderCreateInfo::Resource &res : all_resources) {
257 const ShaderInput *input = shader_input_get(res);
258 BLI_assert(input);
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) ?
275 }
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) ?
295 }
296
297 const VKBindType bind_type = to_bind_type(res.bind_type);
298 descriptor_set_location_update(input, descriptor_set_location++, bind_type, res, arrayed);
299 }
300
301 int32_t push_constant_descriptor_set_location = -1;
302 if (ctx.push_constants_storage_type == VKPushConstants::StorageType::BUFFER) {
303 push_constant_descriptor_set_location = descriptor_set_location++;
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,
309 std::nullopt,
311 }
312 push_constants_layout_.init(
313 info, *this, ctx.push_constants_storage_type, push_constant_descriptor_set_location);
314}
315
317{
318 InitContext ctx{info};
319
320 shader_builtins_ = info.builtins_combined();
321
322 compute_resource_counts(ctx);
323 populate_shader_inputs(ctx);
324 populate_builtins();
325 populate_resource_bindings(ctx);
326}
327
328static int32_t shader_input_index(const ShaderInput *shader_inputs,
329 const ShaderInput *shader_input)
330{
331 int32_t index = (shader_input - shader_inputs);
332 return index;
333}
334
335void VKShaderInterface::descriptor_set_location_update(
336 const ShaderInput *shader_input,
337 const VKDescriptorSet::Location location,
338 const VKBindType bind_type,
339 std::optional<const shader::ShaderCreateInfo::Resource> resource,
340 VKImageViewArrayed arrayed)
341{
342 BLI_assert_msg(resource.has_value() ||
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.");
346 BLI_assert_msg(!resource.has_value() || to_bind_type(resource->bind_type) == bind_type,
347 "Incorrect parameter, bind types do not match.");
348
349 const VKDevice &device = VKBackend::get().device;
350 const bool supports_local_read = device.extensions_get().dynamic_rendering_local_read;
351
352 int32_t index = shader_input_index(inputs_, shader_input);
353 BLI_assert(resource_bindings_[index].binding == -1);
354
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;
360 break;
361
363 if (flag_is_set(resource->storagebuf.qualifiers, shader::Qualifier::read)) {
364 vk_access_flags |= VK_ACCESS_SHADER_READ_BIT;
365 }
366 if (flag_is_set(resource->storagebuf.qualifiers, shader::Qualifier::write)) {
367 vk_access_flags |= VK_ACCESS_SHADER_WRITE_BIT;
368 }
369 break;
370
372 if (flag_is_set(resource->image.qualifiers, shader::Qualifier::read)) {
373 vk_access_flags |= VK_ACCESS_SHADER_READ_BIT;
374 }
375 if (flag_is_set(resource->image.qualifiers, shader::Qualifier::write)) {
376 vk_access_flags |= VK_ACCESS_SHADER_WRITE_BIT;
377 }
378 break;
379
381 vk_access_flags |= VK_ACCESS_SHADER_READ_BIT;
382 break;
383
385 vk_access_flags |= VK_ACCESS_ACCELERATION_STRUCTURE_READ_BIT_KHR;
386 break;
387 };
388 }
389 else if (bind_type == VKBindType::UNIFORM_BUFFER) {
390 vk_access_flags |= VK_ACCESS_UNIFORM_READ_BIT;
391 }
392 else if (bind_type == VKBindType::INPUT_ATTACHMENT) {
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;
396 }
397
398 VKResourceBinding &resource_binding = resource_bindings_[index];
399 resource_binding.bind_type = bind_type;
400 resource_binding.binding = shader_input->binding;
401 resource_binding.location = location;
402 resource_binding.arrayed = arrayed;
403 resource_binding.access_mask = vk_access_flags;
404}
405
406const VKResourceBinding &VKShaderInterface::resource_binding_info(
407 const ShaderInput *shader_input) const
408{
409 int32_t index = shader_input_index(inputs_, shader_input);
410 return resource_bindings_[index];
411}
412
414 const shader::ShaderCreateInfo::Resource &resource) const
415{
416 const ShaderInput *shader_input = shader_input_get(resource);
417 BLI_assert(shader_input);
418 return resource_binding_info(shader_input).location;
419}
420
421std::optional<VKDescriptorSet::Location> VKShaderInterface::descriptor_set_location(
422 const shader::ShaderCreateInfo::Resource::BindType &bind_type, int binding) const
423{
424 const ShaderInput *shader_input = shader_input_get(bind_type, binding);
425 if (shader_input == nullptr) {
426 return std::nullopt;
427 }
428 const VKResourceBinding &resource_binding = resource_binding_info(shader_input);
429 if (resource_binding.bind_type != to_bind_type(bind_type)) {
430 return std::nullopt;
431 }
432 return resource_binding.location;
433}
434
435const ShaderInput *VKShaderInterface::shader_input_get(
436 const shader::ShaderCreateInfo::Resource &resource) const
437{
438 return shader_input_get(resource.bind_type, resource.slot);
439}
440
441const ShaderInput *VKShaderInterface::shader_input_get(
442 const shader::ShaderCreateInfo::Resource::BindType &bind_type, int binding) const
443{
444 switch (bind_type) {
446 /* Not really nice, but the binding namespace between OpenGL and Vulkan don't match. To fix
447 * this we need to check if one of both cases return a binding.
448 */
449 return texture_get((binding >= BIND_SPACE_IMAGE_OFFSET) ? binding :
450 binding + BIND_SPACE_IMAGE_OFFSET);
452 return texture_get(binding);
454 return ssbo_get(binding);
456 return tlas_get(binding);
458 return ubo_get(binding);
459 }
460 return nullptr;
461}
462
463void VKShaderInterface::init_descriptor_set_layout_info(
464 const shader::ShaderCreateInfo &info,
465 int64_t resources_len,
466 Span<shader::ShaderCreateInfo::Resource> all_resources,
467 VKPushConstants::StorageType push_constants_storage)
468{
469 BLI_assert(descriptor_set_layout_info_.bindings.is_empty());
470 const VKExtensions &extensions = VKBackend::get().device.extensions_get();
471 const bool supports_local_read = extensions.dynamic_rendering_local_read;
472
473 descriptor_set_layout_info_.bindings.reserve(resources_len);
474 if (!(info.compute_source_.is_empty() && info.compute_source_generated.empty())) {
475 descriptor_set_layout_info_.vk_shader_stage_flags = VK_SHADER_STAGE_COMPUTE_BIT;
476 }
477 else if (supports_local_read && !info.subpass_inputs_.is_empty()) {
478 descriptor_set_layout_info_.vk_shader_stage_flags = VK_SHADER_STAGE_FRAGMENT_BIT;
479 }
480 else {
481 descriptor_set_layout_info_.vk_shader_stage_flags = VK_SHADER_STAGE_ALL_GRAPHICS;
482 }
483 for (int index : IndexRange(info.subpass_inputs_.size())) {
484 UNUSED_VARS(index);
485 // TODO: clean up remove negation.
486 descriptor_set_layout_info_.bindings.append_n_times(
487 !extensions.dynamic_rendering_local_read ? VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER :
488 VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT,
489 info.subpass_inputs_.size());
490 }
491 for (const shader::ShaderCreateInfo::Resource &res : all_resources) {
492 descriptor_set_layout_info_.bindings.append(to_vk_descriptor_type(res));
493 }
494 if (push_constants_storage == VKPushConstants::StorageType::BUFFER) {
495 descriptor_set_layout_info_.bindings.append(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
496 }
497}
498
499} // namespace blender::gpu
#define GPU_NUM_UNIFORMS
#define BLI_assert_unreachable()
#define UNUSED_VARS(...)
#define BLI_assert(a)
#define ELEM(...)
#define BLI_assert_msg(a, msg)
#define STREQ(a, b)
Array(Allocator allocator={}) noexcept
Definition BLI_array.hh:78
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]
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)
static VKBackend & get()
const VKExtensions & extensions_get() const
Definition vk_device.hh:398
VKDescriptorSet::Location descriptor_set_location(const shader::ShaderCreateInfo::Resource &resource) const
void init(const shader::ShaderCreateInfo &info)
#define index(i)
#define push_constant
#define subpass_in
VkDescriptorType to_vk_descriptor_type(const shader::ShaderCreateInfo::Resource &resource)
Definition vk_common.cc:823
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)
__int64 int64_t
Definition BKE_text.h:90
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
unsigned int uint32_t
Definition BKE_text.h:81
unsigned char uint8_t
Definition BKE_text.h:79
signed int int32_t
Definition BKE_text.h:78
GPUUniformBlockBuiltin
@ GPU_NUM_UNIFORM_BLOCKS
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
static StorageType determine_storage_type(const shader::ShaderCreateInfo &info, const VKDevice &device)
VKDescriptorSet::Location location
Describe inputs & outputs, stage interfaces, resources and sources of a shader. If all data is correc...
Vector< SpecializationConstant, 0 > specialization_constants_