Blender V5.3
mtl_shader_interface.mm
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include "BLI_bitmap.hh"
12
13#include "GPU_capabilities.hh"
14
15#include "mtl_common.hh"
16#include "mtl_debug.hh"
17#include "mtl_push_constant.hh"
21
22#include "BLI_math_base_c.hh"
23#include "BLI_string.hh"
24#include "BLI_utildefines.hh"
25#include "MEM_guardedalloc.h"
26
27namespace blender::gpu {
28
30 const shader::ShaderCreateInfo &info,
31 MTLPushConstantBuf *push_constant_buf)
32{
33 using namespace blender::gpu::shader;
34
35 if (name != nullptr) {
36 STRNCPY(this->name, name);
37 }
38
39 shader_builtins_ = info.builtins_combined();
40
41 attr_len_ = info.vertex_inputs_.size();
42 uniform_len_ = info.push_constants_.size();
44 ssbo_len_ = 0;
45 ubo_len_ = 0;
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
71 inputs_ = MEM_new_array_zeroed<ShaderInput>(input_tot_len, __func__);
72 ShaderInput *input = inputs_;
73
74 size_t names_size = info.interface_names_size_;
75 name_buffer_ = MEM_new_array_uninitialized<char>(names_size, "name_buffer");
76 uint32_t name_buffer_offset = 0;
77
78 /* Attributes */
79 for (const ShaderCreateInfo::VertIn &attr : info.vertex_inputs_) {
80 copy_input_name(input, attr.name, name_buffer_, name_buffer_offset);
81 input->location = input->binding = attr.index;
82 if (input->location != -1) {
83 enabled_attr_mask_ |= (1 << input->location);
84 /* Used in `GPU_shader_get_attribute_info`. */
85 attr_types_[input->location] = uint8_t(attr.type);
86 }
87 input++;
88 }
89
90 /* Uniform blocks */
91 for (const ShaderCreateInfo::Resource &res : all_resources) {
93 copy_input_name(input, res.uniformbuf.name, name_buffer_, name_buffer_offset);
94 input->location = input->binding = res.slot;
95 enabled_ubo_mask_ |= (1 << input->binding);
96 vertex_buffer_mask_ &= ~(1 << (input->binding + MTL_UBO_SLOT_OFFSET));
97 input++;
98 }
99 }
100
101 /* Images, Samplers and buffers. */
102 for (const ShaderCreateInfo::Resource &res : all_resources) {
104 sampler_names_offsets_[res.slot] = name_buffer_offset;
105 copy_input_name(input, res.sampler.name, name_buffer_, name_buffer_offset);
106 input->location = -1; /* Setting location is not possible in MSL. */
107 input->binding = res.slot;
108 enabled_tex_mask_ |= (1ull << input->binding);
109 input++;
110 }
111 else if (res.bind_type == ShaderCreateInfo::Resource::BindType::IMAGE) {
112 image_names_offsets_[res.slot] = name_buffer_offset;
113 copy_input_name(input, res.image.name, name_buffer_, name_buffer_offset);
114 input->location = -1; /* Setting location is not possible in MSL. */
115 input->binding = res.slot;
116 enabled_ima_mask_ |= (1 << input->binding);
117 input++;
118 }
119 }
121
122 /* Push constants. */
124 copy_input_name(input, push_constant.name, name_buffer_, name_buffer_offset);
125 input->location = push_constant_buf->append(push_constant);
126 input->binding = -1;
127 input++;
128 }
129
130 /* Storage buffers */
131 for (const ShaderCreateInfo::Resource &res : all_resources) {
133 copy_input_name(input, res.storagebuf.name, name_buffer_, name_buffer_offset);
134 input->location = input->binding = res.slot;
135 enabled_ssbo_mask_ |= (1 << input->binding);
136 vertex_buffer_mask_ &= ~(1 << (input->binding + MTL_SSBO_SLOT_OFFSET));
137 input++;
138 }
139 }
140
141 /* Acceleration structures (top level). */
142 for (const ShaderCreateInfo::Resource &res : all_resources) {
144 copy_input_name(input, res.acceleration_structure.name, name_buffer_, name_buffer_offset);
145 input->location = input->binding = res.slot;
146 enabled_accel_mask_ |= (1 << input->binding);
147 vertex_buffer_mask_ &= ~(1 << (MTL_ACCELERATION_STRUCTURE_SLOT + res.slot));
148 input++;
149 }
150 }
151
152 for (const ShaderCreateInfo::Resource &res : info.geometry_resources_) {
154 ssbo_attr_mask_ |= (1 << res.slot);
155 }
156 else {
157 BLI_assert_msg(0, "Resource type is not supported for Geometry frequency");
158 }
159 }
160
161 /* Constants */
162 int constant_id = 0;
163 for (const SpecializationConstant &constant : info.specialization_constants_) {
164 copy_input_name(input, constant.name, name_buffer_, name_buffer_offset);
165 input->location = constant_id++;
166 input++;
167 }
168
169 sort_inputs();
170
171 /* Builtin Uniforms */
172 for (int32_t u_int = 0; u_int < GPU_NUM_UNIFORMS; u_int++) {
173 GPUUniformBuiltin u = static_cast<GPUUniformBuiltin>(u_int);
174 const ShaderInput *uni = this->uniform_get(builtin_uniform_name(u));
175 builtins_[u] = (uni != nullptr) ? uni->location : -1;
176 }
177
178 /* Builtin Uniforms Blocks */
179 for (int32_t u_int = 0; u_int < GPU_NUM_UNIFORM_BLOCKS; u_int++) {
180 GPUUniformBlockBuiltin u = static_cast<GPUUniformBlockBuiltin>(u_int);
181 const ShaderInput *block = this->ubo_get(builtin_uniform_block_name(u));
182 builtin_blocks_[u] = (block != nullptr) ? block->binding : -1;
183 }
184}
185
187{
188 if (arg_encoder_ != nil) {
189 [arg_encoder_ release];
190 arg_encoder_ = nil;
191 }
192}
193
195{
196 return name_buffer_ + offset;
197}
198
200{
201 if (arg_encoder_ == nil) {
202 arg_encoder_ = [mtl_function
203 newArgumentEncoderWithBufferIndex:MTL_SAMPLER_ARGUMENT_BUFFER_SLOT];
204 }
205 return arg_encoder_;
206}
207
208} // namespace blender::gpu
#define GPU_NUM_UNIFORMS
Read Blender memory allocation and freeing.
#define BLI_assert_msg(a, msg)
int append(shader::ShaderCreateInfo::PushConst push_constant)
MTLShaderInterface(const char *name, const shader::ShaderCreateInfo &info, MTLPushConstantBuf *push_constant_buf=nullptr)
id< MTLArgumentEncoder > ensure_argument_encoder(id< MTLFunction > mtl_function)
const char * name_at_offset(uint32_t offset) 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
static const char * builtin_uniform_block_name(GPUUniformBlockBuiltin u)
const ShaderInput * ubo_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)
#define push_constant
#define MTL_SAMPLER_ARGUMENT_BUFFER_SLOT
#define MTL_ACCELERATION_STRUCTURE_SLOT
#define MTL_UBO_SLOT_OFFSET
#define MTL_SSBO_SLOT_OFFSET
char * STRNCPY(char(&dst)[N], const char *src)
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
Describe inputs & outputs, stage interfaces, resources and sources of a shader. If all data is correc...
Vector< SpecializationConstant, 0 > specialization_constants_