Blender V5.0
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
31
33{
34 static char PUSH_CONSTANTS_FALLBACK_NAME[] = "push_constants_fallback";
35 static size_t PUSH_CONSTANTS_FALLBACK_NAME_LEN = strlen(PUSH_CONSTANTS_FALLBACK_NAME);
36 static char SUBPASS_FALLBACK_NAME[] = "gpu_subpass_img_0";
37 static size_t SUBPASS_FALLBACK_NAME_LEN = strlen(SUBPASS_FALLBACK_NAME);
38
39 using namespace blender::gpu::shader;
40 shader_builtins_ = info.builtins_;
41
42 attr_len_ = info.vertex_inputs_.size();
43 uniform_len_ = info.push_constants_.size();
45 ssbo_len_ = 0;
46 ubo_len_ = 0;
48 all_resources.extend(info.pass_resources_);
49 all_resources.extend(info.batch_resources_);
50 all_resources.extend(info.geometry_resources_);
51
52 for (ShaderCreateInfo::Resource &res : all_resources) {
53 switch (res.bind_type) {
57 break;
59 ubo_len_++;
60 break;
62 ssbo_len_++;
63 break;
64 }
65 }
66 const VKDevice &device = VKBackend::get().device;
67 const bool supports_local_read = device.extensions_get().dynamic_rendering_local_read;
68 uniform_len_ += info.subpass_inputs_.size();
69
70 /* Reserve 1 uniform buffer for push constants fallback. */
71 size_t names_size = info.interface_names_size_;
72 const VKPushConstants::StorageType push_constants_storage_type =
74 if (push_constants_storage_type == VKPushConstants::StorageType::UNIFORM_BUFFER) {
75 ubo_len_++;
76 names_size += PUSH_CONSTANTS_FALLBACK_NAME_LEN + 1;
77 }
78 names_size += info.subpass_inputs_.size() * SUBPASS_FALLBACK_NAME_LEN;
79
81 inputs_ = MEM_calloc_arrayN<ShaderInput>(input_tot_len, __func__);
83
84 name_buffer_ = (char *)MEM_mallocN(names_size, "name_buffer");
85 uint32_t name_buffer_offset = 0;
86
87 /* Attributes */
88 for (const ShaderCreateInfo::VertIn &attr : info.vertex_inputs_) {
89 copy_input_name(input, attr.name, name_buffer_, name_buffer_offset);
90 input->location = input->binding = attr.index;
91 if (input->location != -1) {
92 enabled_attr_mask_ |= (1 << input->location);
93
94 /* Used in `GPU_shader_get_attribute_info`. */
95 attr_types_[input->location] = uint8_t(attr.type);
96 }
97
98 input++;
99 }
100
101 /* Uniform blocks */
102 for (const ShaderCreateInfo::Resource &res : all_resources) {
104 copy_input_name(input, res.uniformbuf.name, name_buffer_, name_buffer_offset);
105 input->location = input->binding = res.slot;
106 input++;
107 }
108 }
109 /* Add push constant when using uniform buffer as a fallback. */
110 int32_t push_constants_fallback_location = -1;
111 if (push_constants_storage_type == VKPushConstants::StorageType::UNIFORM_BUFFER) {
112 copy_input_name(input, PUSH_CONSTANTS_FALLBACK_NAME, name_buffer_, name_buffer_offset);
113 input->location = input->binding = -1;
114 input++;
115 }
116
117 /* Images, Samplers and buffers. */
118 for (const ShaderCreateInfo::SubpassIn &subpass_in : info.subpass_inputs_) {
119 copy_input_name(input, SUBPASS_FALLBACK_NAME, name_buffer_, name_buffer_offset);
120 input->location = input->binding = subpass_in.index;
121 input++;
122 }
123 for (const ShaderCreateInfo::Resource &res : all_resources) {
125 copy_input_name(input, res.sampler.name, name_buffer_, name_buffer_offset);
126 input->location = input->binding = res.slot;
127 input++;
128 }
129 else if (res.bind_type == ShaderCreateInfo::Resource::BindType::IMAGE) {
130 copy_input_name(input, res.image.name, name_buffer_, name_buffer_offset);
131 input->location = input->binding = res.slot + BIND_SPACE_IMAGE_OFFSET;
132 input++;
133 }
134 }
136
137 /* Push constants. */
138 int32_t push_constant_location = 1024;
139 for (const ShaderCreateInfo::PushConst &push_constant : info.push_constants_) {
140 copy_input_name(input, push_constant.name, name_buffer_, name_buffer_offset);
141 input->location = push_constant_location++;
142 input->binding = -1;
143 input++;
144 }
145
146 /* Storage buffers */
147 for (const ShaderCreateInfo::Resource &res : all_resources) {
149 copy_input_name(input, res.storagebuf.name, name_buffer_, name_buffer_offset);
150 input->location = input->binding = res.slot;
151 input++;
152 }
153 }
154
155 for (const ShaderCreateInfo::Resource &res : info.geometry_resources_) {
157 ssbo_attr_mask_ |= (1 << res.slot);
158 }
159 else {
160 BLI_assert_msg(0, "Resource type is not supported for Geometry frequency");
161 }
162 }
163
164 /* Constants */
165 int constant_id = 0;
166 for (const SpecializationConstant &constant : info.specialization_constants_) {
167 copy_input_name(input, constant.name, name_buffer_, name_buffer_offset);
168 input->location = constant_id++;
169 input++;
170 }
171
172 sort_inputs();
173
174 /* Builtin Uniforms */
175 for (int32_t u_int = 0; u_int < GPU_NUM_UNIFORMS; u_int++) {
176 GPUUniformBuiltin u = static_cast<GPUUniformBuiltin>(u_int);
177 const ShaderInput *uni = this->uniform_get(builtin_uniform_name(u));
178 builtins_[u] = (uni != nullptr) ? uni->location : -1;
179 }
180
181 /* Builtin Uniforms Blocks */
182 for (int32_t u_int = 0; u_int < GPU_NUM_UNIFORM_BLOCKS; u_int++) {
183 GPUUniformBlockBuiltin u = static_cast<GPUUniformBlockBuiltin>(u_int);
184 const ShaderInput *block = this->ubo_get(builtin_uniform_block_name(u));
185 builtin_blocks_[u] = (block != nullptr) ? block->binding : -1;
186 }
187
188 /* Determine the descriptor set locations after the inputs have been sorted. */
189 /* NOTE: input_tot_len is sometimes more than we need. */
190 const uint32_t resources_len = input_tot_len;
191
192 /* Initialize the descriptor set layout. */
193 init_descriptor_set_layout_info(info, resources_len, all_resources, push_constants_storage_type);
194
195 /* Update the descriptor set locations, bind types and access masks. */
196 resource_bindings_ = Array<VKResourceBinding>(resources_len);
197 resource_bindings_.fill({});
198
199 uint32_t descriptor_set_location = 0;
200 for (const ShaderCreateInfo::SubpassIn &subpass_in : info.subpass_inputs_) {
201 const ShaderInput *input = supports_local_read ?
202 texture_get(subpass_in.index) :
203 shader_input_get(
205 subpass_in.index);
207 BLI_assert(STREQ(input_name_get(input), SUBPASS_FALLBACK_NAME));
208 descriptor_set_location_update(input,
211 std::nullopt,
213 }
214 for (ShaderCreateInfo::Resource &res : all_resources) {
215 const ShaderInput *input = shader_input_get(res);
219 arrayed = ELEM(res.image.type,
220 shader::ImageType::Float1DArray,
221 shader::ImageType::Float2DArray,
222 shader::ImageType::FloatCubeArray,
223 shader::ImageType::Int1DArray,
224 shader::ImageType::Int2DArray,
225 shader::ImageType::IntCubeArray,
226 shader::ImageType::Uint1DArray,
227 shader::ImageType::Uint2DArray,
228 shader::ImageType::UintCubeArray,
229 shader::ImageType::AtomicUint2DArray,
230 shader::ImageType::AtomicInt2DArray) ?
233 }
235 arrayed = ELEM(res.sampler.type,
236 shader::ImageType::Float1DArray,
237 shader::ImageType::Float2DArray,
238 shader::ImageType::FloatCubeArray,
239 shader::ImageType::Int1DArray,
240 shader::ImageType::Int2DArray,
241 shader::ImageType::IntCubeArray,
242 shader::ImageType::Uint1DArray,
243 shader::ImageType::Uint2DArray,
244 shader::ImageType::UintCubeArray,
245 shader::ImageType::Shadow2DArray,
246 shader::ImageType::ShadowCubeArray,
247 shader::ImageType::Depth2DArray,
248 shader::ImageType::DepthCubeArray,
249 shader::ImageType::AtomicUint2DArray,
250 shader::ImageType::AtomicInt2DArray) ?
253 }
254
255 const VKBindType bind_type = to_bind_type(res.bind_type);
256 descriptor_set_location_update(input, descriptor_set_location++, bind_type, res, arrayed);
257 }
258
259 /* Post initializing push constants. */
260 /* Determine the binding location of push constants fallback buffer. */
261 int32_t push_constant_descriptor_set_location = -1;
262 if (push_constants_storage_type == VKPushConstants::StorageType::UNIFORM_BUFFER) {
263 push_constant_descriptor_set_location = descriptor_set_location++;
264 const ShaderInput *push_constant_input = ubo_get(PUSH_CONSTANTS_FALLBACK_NAME);
265 descriptor_set_location_update(push_constant_input,
266 push_constants_fallback_location,
268 std::nullopt,
270 }
271 push_constants_layout_.init(
272 info, *this, push_constants_storage_type, push_constant_descriptor_set_location);
273}
274
275static int32_t shader_input_index(const ShaderInput *shader_inputs,
276 const ShaderInput *shader_input)
277{
278 int32_t index = (shader_input - shader_inputs);
279 return index;
280}
281
282void VKShaderInterface::descriptor_set_location_update(
283 const ShaderInput *shader_input,
284 const VKDescriptorSet::Location location,
285 const VKBindType bind_type,
286 std::optional<const shader::ShaderCreateInfo::Resource> resource,
287 VKImageViewArrayed arrayed)
288{
289 BLI_assert_msg(resource.has_value() ||
291 "Incorrect parameters, when no resource is given, it must be the uniform buffer "
292 "for storing push constants or input attachment for subpass inputs.");
293 BLI_assert_msg(!resource.has_value() || to_bind_type(resource->bind_type) == bind_type,
294 "Incorrect parameter, bind types do not match.");
295
296 const VKDevice &device = VKBackend::get().device;
297 const bool supports_local_read = device.extensions_get().dynamic_rendering_local_read;
298
299 int32_t index = shader_input_index(inputs_, shader_input);
300 BLI_assert(resource_bindings_[index].binding == -1);
301
302 VkAccessFlags vk_access_flags = VK_ACCESS_NONE;
303 if (resource.has_value()) {
304 switch (resource->bind_type) {
306 vk_access_flags |= VK_ACCESS_UNIFORM_READ_BIT;
307 break;
308
310 if (bool(resource->storagebuf.qualifiers & shader::Qualifier::read) == true) {
311 vk_access_flags |= VK_ACCESS_SHADER_READ_BIT;
312 }
313 if (bool(resource->storagebuf.qualifiers & shader::Qualifier::write) == true) {
314 vk_access_flags |= VK_ACCESS_SHADER_WRITE_BIT;
315 }
316 break;
317
319 if (bool(resource->image.qualifiers & shader::Qualifier::read) == true) {
320 vk_access_flags |= VK_ACCESS_SHADER_READ_BIT;
321 }
322 if (bool(resource->image.qualifiers & shader::Qualifier::write) == true) {
323 vk_access_flags |= VK_ACCESS_SHADER_WRITE_BIT;
324 }
325 break;
326
328 vk_access_flags |= VK_ACCESS_SHADER_READ_BIT;
329 break;
330 };
331 }
332 else if (bind_type == VKBindType::UNIFORM_BUFFER) {
333 vk_access_flags |= VK_ACCESS_UNIFORM_READ_BIT;
334 }
335 else if (bind_type == VKBindType::INPUT_ATTACHMENT) {
336 vk_access_flags |= supports_local_read ? VK_ACCESS_INPUT_ATTACHMENT_READ_BIT |
337 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT :
338 VK_ACCESS_SHADER_READ_BIT;
339 }
340
341 VKResourceBinding &resource_binding = resource_bindings_[index];
342 resource_binding.bind_type = bind_type;
343 resource_binding.binding = shader_input->binding;
344 resource_binding.location = location;
345 resource_binding.arrayed = arrayed;
346 resource_binding.access_mask = vk_access_flags;
347}
348
349const VKResourceBinding &VKShaderInterface::resource_binding_info(
350 const ShaderInput *shader_input) const
351{
352 int32_t index = shader_input_index(inputs_, shader_input);
353 return resource_bindings_[index];
354}
355
358{
359 const ShaderInput *shader_input = shader_input_get(resource);
360 BLI_assert(shader_input);
361 return resource_binding_info(shader_input).location;
362}
363
364const std::optional<VKDescriptorSet::Location> VKShaderInterface::descriptor_set_location(
365 const shader::ShaderCreateInfo::Resource::BindType &bind_type, int binding) const
366{
367 const ShaderInput *shader_input = shader_input_get(bind_type, binding);
368 if (shader_input == nullptr) {
369 return std::nullopt;
370 }
371 const VKResourceBinding &resource_binding = resource_binding_info(shader_input);
372 if (resource_binding.bind_type != to_bind_type(bind_type)) {
373 return std::nullopt;
374 }
375 return resource_binding.location;
376}
377
378const ShaderInput *VKShaderInterface::shader_input_get(
380{
381 return shader_input_get(resource.bind_type, resource.slot);
382}
383
384const ShaderInput *VKShaderInterface::shader_input_get(
385 const shader::ShaderCreateInfo::Resource::BindType &bind_type, int binding) const
386{
387 switch (bind_type) {
389 /* Not really nice, but the binding namespace between OpenGL and Vulkan don't match. To fix
390 * this we need to check if one of both cases return a binding.
391 */
392 return texture_get((binding >= BIND_SPACE_IMAGE_OFFSET) ? binding :
393 binding + BIND_SPACE_IMAGE_OFFSET);
395 return texture_get(binding);
397 return ssbo_get(binding);
399 return ubo_get(binding);
400 }
401 return nullptr;
402}
403
404void VKShaderInterface::init_descriptor_set_layout_info(
405 const shader::ShaderCreateInfo &info,
406 int64_t resources_len,
407 Span<shader::ShaderCreateInfo::Resource> all_resources,
408 VKPushConstants::StorageType push_constants_storage)
409{
410 BLI_assert(descriptor_set_layout_info_.bindings.is_empty());
411 const VKExtensions &extensions = VKBackend::get().device.extensions_get();
412 const bool supports_local_read = extensions.dynamic_rendering_local_read;
413
414 descriptor_set_layout_info_.bindings.reserve(resources_len);
415 if (!(info.compute_source_.is_empty() && info.compute_source_generated.empty())) {
416 descriptor_set_layout_info_.vk_shader_stage_flags = VK_SHADER_STAGE_COMPUTE_BIT;
417 }
418 else if (supports_local_read && !info.subpass_inputs_.is_empty()) {
419 descriptor_set_layout_info_.vk_shader_stage_flags = VK_SHADER_STAGE_FRAGMENT_BIT;
420 }
421 else {
422 descriptor_set_layout_info_.vk_shader_stage_flags = VK_SHADER_STAGE_ALL_GRAPHICS;
423 }
424 for (int index : IndexRange(info.subpass_inputs_.size())) {
425 UNUSED_VARS(index);
426 // TODO: clean up remove negation.
427 descriptor_set_layout_info_.bindings.append_n_times(
428 !extensions.dynamic_rendering_local_read ? VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER :
429 VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT,
430 info.subpass_inputs_.size());
431 }
432 for (const shader::ShaderCreateInfo::Resource &res : all_resources) {
433 descriptor_set_layout_info_.bindings.append(to_vk_descriptor_type(res));
434 }
435 if (push_constants_storage == VKPushConstants::StorageType::UNIFORM_BUFFER) {
436 descriptor_set_layout_info_.bindings.append(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
437 }
438}
439
440} // namespace blender::gpu
#define BLI_assert_unreachable()
Definition BLI_assert.h:93
#define BLI_assert(a)
Definition BLI_assert.h:46
#define BLI_assert_msg(a, msg)
Definition BLI_assert.h:53
#define UNUSED_VARS(...)
#define ELEM(...)
#define STREQ(a, b)
#define GPU_NUM_UNIFORMS
GPUUniformBuiltin
GPUUniformBlockBuiltin
@ GPU_NUM_UNIFORM_BLOCKS
long long int int64_t
constexpr bool is_empty() const
void extend(Span< T > array)
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]
uint8_t attr_types_[GPU_VERT_ATTR_MAX_LEN]
static const char * builtin_uniform_name(GPUUniformBuiltin u)
static VKBackend & get()
Definition vk_backend.hh:91
const VKExtensions & extensions_get() const
Definition vk_device.hh:371
const VKDescriptorSet::Location descriptor_set_location(const shader::ShaderCreateInfo::Resource &resource) const
void init(const shader::ShaderCreateInfo &info)
#define resource
#define input
void * MEM_mallocN(size_t len, const char *str)
Definition mallocn.cc:128
void * MEM_calloc_arrayN(size_t len, size_t size, const char *str)
Definition mallocn.cc:123
VkDescriptorType to_vk_descriptor_type(const shader::ShaderCreateInfo::Resource &resource)
Definition vk_common.cc:804
static constexpr int BIND_SPACE_IMAGE_OFFSET
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)
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 > specialization_constants_