Blender V5.3
mtl_shader.hh
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
9#pragma once
10
11#include "MEM_guardedalloc.h"
12
13#include "GPU_batch.hh"
14#include "GPU_capabilities.hh"
15#include "GPU_shader.hh"
16#include "GPU_vertex_format.hh"
17
18#include <Metal/Metal.h>
19#include <QuartzCore/QuartzCore.h>
20#include <functional>
21#include <unordered_map>
22
23#include <deque>
24#include <mutex>
25#include <thread>
26
27#include "mtl_framebuffer.hh"
29#include "mtl_shader_shared.hh"
30#include "mtl_state.hh"
31#include "mtl_texture.hh"
32
34#include "gpu_shader_private.hh"
35
36namespace blender::gpu {
37
39class MTLContext;
40
41/* Debug control. */
42#define MTL_SHADER_DEBUG_EXPORT_SOURCE 0
43#define MTL_SHADER_TRANSLATION_DEBUG_OUTPUT 0
44
45/* Maximum threshold for specialized shader variant count.
46 * This is a catch-all to prevent excessive PSO permutations from being created and also catch
47 * parameters which should ideally not be used for specialization. */
48#define MTL_SHADER_MAX_SPECIALIZED_PSOS 5
49
51 /* Function instances with specialization.
52 * Required for argument encoder construction. */
55
56 /* PSO handle. */
58
60 /* Unique index for PSO variant. */
62 /* buffer bind slot used for null attributes (-1 if not needed). */
64 /* Topology class. */
65 MTLPrimitiveTopologyClass prim_type;
66
74 /* Same thing for images. */
77 /* Same thing for samplers. */
80
81 void parse_reflection_data(::MTLRenderPipelineReflection *reflection_data);
82};
83
85 /* Thread-group information is common for all PSO variants. */
89
90 inline void set_compute_workgroup_size(int workgroup_size_x,
91 int workgroup_size_y,
92 int workgroup_size_z)
93 {
94 this->threadgroup_x_len = workgroup_size_x;
95 this->threadgroup_y_len = workgroup_size_y;
96 this->threadgroup_z_len = workgroup_size_z;
97 }
98};
99
102 /* Unique index for PSO variant. */
104
105 /* Function instances with specialization.
106 * Required for argument encoder construction. */
108 /* PSO handle. */
110};
111
125class MTLShader : public Shader {
128
129 private:
130 /* Context Handle. */
131 MTLContext *context_ = nullptr;
132
133 /* Can be nullptr if no uniform is present inside the shader. */
134 MTLPushConstantBuf *push_constant_buf_ = nullptr;
135
137 id<MTLLibrary> shader_library_vert_ = nil;
138 id<MTLLibrary> shader_library_frag_ = nil;
139 id<MTLLibrary> shader_library_comp_ = nil;
140 bool valid_ = false;
141
143 /* Metal API Descriptor used for creation of unique PSOs based on rendering state. */
144 ::MTLRenderPipelineDescriptor *pso_descriptor_ = nil;
145 /* Metal backend struct containing all high-level pipeline state parameters
146 * which contribute to instantiation of a unique PSO. */
147 MTLRenderPipelineStateDescriptor current_pipeline_state_;
148 /* Cache of compiled PipelineStateObjects. */
150 std::mutex pso_cache_lock_;
151
153 MTLComputePipelineStateCommon compute_pso_common_state_;
155
156 public:
157 MTLShader(MTLContext *ctx, const char *name);
158 ~MTLShader();
159
160 void init(const shader::ShaderCreateInfo & /*info*/, bool /*is_codegen_only*/) override {};
161
162 /* Patch create infos for any additional resources that could be needed. */
164 const shader::ShaderCreateInfo &original_info) override;
165
166 /* Assign GLSL source. */
168 MutableSpan<StringRefNull> sources) override;
170 MutableSpan<StringRefNull> sources) override;
172 MutableSpan<StringRefNull> sources) override;
174 MutableSpan<StringRefNull> sources) override;
175
176 /* Compile and build - Return true if successful. */
177 bool finalize(const shader::ShaderCreateInfo *info = nullptr) override;
179 void warm_cache(int limit) override;
180
181 /* Utility. */
182 bool is_valid()
183 {
184 return valid_;
185 }
187 {
188 return (shader_library_comp_ != nil);
189 }
191 {
192 return (parent_shader_ != nil);
193 }
195 {
196 return current_pipeline_state_;
197 }
198
200 {
201 return *static_cast<MTLShaderInterface *>(this->interface);
202 }
203
204 /* Might return nullptr if no push constants are present in the interface. */
206 {
207 return push_constant_buf_;
208 }
209
210 /* Shader source generators from create-info.
211 * These aren't all used by Metal, as certain parts of source code generation
212 * for shader entry-points and resource mapping occur during `finalize`. */
213 std::string resources_declare(const shader::ShaderCreateInfo & /*info*/) const override
214 {
215 return "";
216 }
217 std::string vertex_interface_declare(const shader::ShaderCreateInfo & /*info*/) const override
218 {
219 return "";
220 }
221 std::string fragment_interface_declare(const shader::ShaderCreateInfo & /*info*/) const override
222 {
223 return "";
224 }
225 std::string geometry_interface_declare(const shader::ShaderCreateInfo & /*info*/) const override
226 {
227 return "";
228 }
229 std::string geometry_layout_declare(const shader::ShaderCreateInfo & /*info*/) const override
230 {
231 return "";
232 }
233 std::string compute_layout_declare(const shader::ShaderCreateInfo & /*info*/) const override
234 {
235 return "";
236 }
237
238 void bind(const shader::SpecializationConstants *constants_state) override;
239 void unbind() override;
240
241 void uniform_float(int location, int comp_len, int array_size, const float *data) override;
242 void uniform_int(int location, int comp_len, int array_size, const int *data) override;
243
245 MTLPrimitiveTopologyClass prim_type);
246 /* Bakes and caches a PSO for graphic. */
248 MTLContext *ctx,
249 MTLPrimitiveTopologyClass prim_type,
250 const MTLRenderPipelineStateDescriptor &pipeline_descriptor);
251
252 /* Bakes and caches a PSO for compute. */
254 MTLContext *ctx, MTLComputePipelineStateDescriptor &compute_pipeline_descriptor);
255
257 {
258 return compute_pso_common_state_;
259 }
260
261 private:
263 id<MTLLibrary> create_shader_library(const shader::ShaderCreateInfo &info,
264 ShaderStage stage,
266
267 std::string entry_point_name_get(const ShaderStage stage);
268
269 MEM_CXX_CLASS_ALLOC_FUNCS("MTLShader");
270};
271
273 public:
275
276 Shader *compile_shader(const shader::ShaderCreateInfo &info) override;
277 void specialize_shader(const ShaderSpecialization &specialization) override;
278};
279
280/* Vertex format conversion.
281 * Determines whether it is possible to resize a vertex attribute type
282 * during input assembly. A conversion is implied by the difference
283 * between the input vertex descriptor (from MTLBatch/MTLImmediate)
284 * and the type specified in the shader source.
285 *
286 * e.g. vec3 to vec4 expansion, or vec4 to vec2 truncation.
287 * NOTE: Vector expansion will replace empty elements with the values
288 * (0,0,0,1).
289 *
290 * If implicit format resize is not possible, this function
291 * returns false.
292 *
293 * Implicitly supported conversions in Metal are described here:
294 * https://developer.apple.com/documentation/metal/mtlvertexattributedescriptor/1516081-format?language=objc
295 */
296inline MTLVertexFormat format_resize_comp(MTLVertexFormat mtl_format, uint32_t components)
297{
298#define RESIZE_TYPE(_type, _suffix) \
299 case MTLVertexFormat##_type##_suffix: \
300 case MTLVertexFormat##_type##2##_suffix: \
301 case MTLVertexFormat##_type##3##_suffix: \
302 case MTLVertexFormat##_type##4##_suffix: \
303 switch (components) { \
304 case 1: \
305 return MTLVertexFormat##_type##_suffix; \
306 case 2: \
307 return MTLVertexFormat##_type##2##_suffix; \
308 case 3: \
309 return MTLVertexFormat##_type##3##_suffix; \
310 case 4: \
311 return MTLVertexFormat##_type##4##_suffix; \
312 } \
313 break;
314
315 switch (mtl_format) {
316 RESIZE_TYPE(Char, )
317 RESIZE_TYPE(Char, Normalized)
318 RESIZE_TYPE(UChar, )
319 RESIZE_TYPE(UChar, Normalized)
321 RESIZE_TYPE(Short, Normalized)
322 RESIZE_TYPE(UShort, )
323 RESIZE_TYPE(UShort, Normalized)
324 RESIZE_TYPE(Int, )
325 RESIZE_TYPE(UInt, )
326 RESIZE_TYPE(Half, )
328 default:
329 /* Can only call this function on format that can be resized. */
331 break;
332 }
333
334#undef RESIZE_TYPE
335 return MTLVertexFormatInvalid;
336}
337
338inline MTLVertexFormat format_get_component_type(MTLVertexFormat mtl_format)
339{
340 return format_resize_comp(mtl_format, 1);
341}
342
343inline MTLVertexFormat to_mtl(GPUVertCompType component_type,
344 GPUVertFetchMode fetch_mode,
345 uint32_t component_len)
346{
347#define FORMAT_PER_COMP(_type, _suffix) \
348 switch (component_len) { \
349 case 1: \
350 return MTLVertexFormat##_type##_suffix; \
351 case 2: \
352 return MTLVertexFormat##_type##2##_suffix; \
353 case 3: \
354 return MTLVertexFormat##_type##3##_suffix; \
355 case 4: \
356 return MTLVertexFormat##_type##4##_suffix; \
357 default: \
358 BLI_assert_msg(0, "Invalid attribute component count"); \
359 break; \
360 } \
361 break;
362
363#define FORMAT_PER_COMP_SMALL_INT(_type) \
364 switch (fetch_mode) { \
365 case GPU_FETCH_INT: \
366 FORMAT_PER_COMP(_type, ) \
367 case GPU_FETCH_INT_TO_FLOAT_UNIT: \
368 FORMAT_PER_COMP(_type, Normalized) \
369 case GPU_FETCH_FLOAT: \
370 BLI_assert_msg(0, "Invalid fetch mode for integer attribute"); \
371 break; \
372 } \
373 break;
374
375#define FORMAT_PER_COMP_INT(_type) \
376 switch (fetch_mode) { \
377 case GPU_FETCH_INT: \
378 FORMAT_PER_COMP(_type, ) \
379 case GPU_FETCH_INT_TO_FLOAT_UNIT: \
380 case GPU_FETCH_FLOAT: \
381 BLI_assert_msg(0, "Invalid fetch mode for integer attribute"); \
382 break; \
383 } \
384 break;
385
386 switch (component_type) {
387 case GPU_COMP_I8:
389 case GPU_COMP_U8:
391 case GPU_COMP_I16:
393 case GPU_COMP_U16:
395 case GPU_COMP_I32:
397 case GPU_COMP_U32:
399 case GPU_COMP_F32:
400 switch (fetch_mode) {
401 case GPU_FETCH_FLOAT:
403 break;
404 case GPU_FETCH_INT:
406 BLI_assert_msg(0, "Invalid fetch mode for float attribute");
407 break;
408 }
409 case GPU_COMP_I10:
410 switch (fetch_mode) {
412 return MTLVertexFormatInt1010102Normalized;
413 case GPU_FETCH_FLOAT:
414 case GPU_FETCH_INT:
415 BLI_assert_msg(0, "Invalid fetch mode for compressed attribute");
416 break;
417 }
418 case GPU_COMP_MAX:
420 break;
421 }
422#undef FORMAT_PER_COMP
423 /* Loading mode not natively supported. */
424 return MTLVertexFormatInvalid;
425}
426
427inline int mtl_format_component_len(MTLVertexFormat format)
428{
429#define FORMAT_PER_TYPE(_comp, _value) \
430 case MTLVertexFormatChar##_comp: \
431 case MTLVertexFormatChar##_comp##Normalized: \
432 case MTLVertexFormatUChar##_comp: \
433 case MTLVertexFormatUChar##_comp##Normalized: \
434 case MTLVertexFormatShort##_comp: \
435 case MTLVertexFormatShort##_comp##Normalized: \
436 case MTLVertexFormatUShort##_comp: \
437 case MTLVertexFormatUShort##_comp##Normalized: \
438 case MTLVertexFormatInt##_comp: \
439 case MTLVertexFormatUInt##_comp: \
440 case MTLVertexFormatHalf##_comp: \
441 case MTLVertexFormatFloat##_comp: \
442 return _value;
443
444 switch (format) {
445 FORMAT_PER_TYPE(, 1)
446 FORMAT_PER_TYPE(2, 2)
447 FORMAT_PER_TYPE(3, 3)
448 FORMAT_PER_TYPE(4, 4)
449 case MTLVertexFormatUInt1010102Normalized:
450 case MTLVertexFormatInt1010102Normalized:
451 case MTLVertexFormatUChar4Normalized_BGRA:
452 return 4;
453#if defined(MAC_OS_VERSION_14_0)
454 case MTLVertexFormatFloatRG11B10:
455 return 3;
456 case MTLVertexFormatFloatRGB9E5:
457 return 3;
458#endif
459 case MTLVertexFormatInvalid:
460 return -1;
461 }
462
463#undef FORMAT_PER_TYPE
464 return -1;
465}
466
467inline bool mtl_format_is_normalized(MTLVertexFormat format)
468{
469#define FORMAT_PER_TYPE(_comp) \
470 case MTLVertexFormatChar##_comp##Normalized: \
471 case MTLVertexFormatUChar##_comp##Normalized: \
472 case MTLVertexFormatShort##_comp##Normalized: \
473 case MTLVertexFormatUShort##_comp##Normalized: \
474 return true;
475
476 switch (format) {
481 default:
482 break;
483 }
484
485#undef FORMAT_PER_TYPE
486 return false;
487}
488
489} // namespace blender::gpu
Read Blender memory allocation and freeing.
#define BLI_assert_unreachable()
#define BLI_assert_msg(a, msg)
Shader * compile_shader(const shader::ShaderCreateInfo &info) override
void specialize_shader(const ShaderSpecialization &specialization) override
std::string resources_declare(const shader::ShaderCreateInfo &) const override
std::string fragment_interface_declare(const shader::ShaderCreateInfo &) const override
void warm_cache(int limit) override
void vertex_shader_from_glsl(const shader::ShaderCreateInfo &info, MutableSpan< StringRefNull > sources) override
void compute_shader_from_glsl(const shader::ShaderCreateInfo &info, MutableSpan< StringRefNull > sources) override
const MTLComputePipelineStateCommon & get_compute_common_state()
MTLComputePipelineStateInstance * bake_compute_pipeline_state(MTLContext *ctx, MTLComputePipelineStateDescriptor &compute_pipeline_descriptor)
const shader::ShaderCreateInfo & patch_create_info(const shader::ShaderCreateInfo &original_info) override
bool finalize_compute(const shader::ShaderCreateInfo *info)
void uniform_int(int location, int comp_len, int array_size, const int *data) override
void bind(const shader::SpecializationConstants *constants_state) override
bool finalize(const shader::ShaderCreateInfo *info=nullptr) override
MTLShaderInterface & get_interface()
std::string vertex_interface_declare(const shader::ShaderCreateInfo &) const override
std::string compute_layout_declare(const shader::ShaderCreateInfo &) const override
void geometry_shader_from_glsl(const shader::ShaderCreateInfo &info, MutableSpan< StringRefNull > sources) override
void unbind() override
void uniform_float(int location, int comp_len, int array_size, const float *data) override
MTLRenderPipelineStateInstance * bake_graphic_pipeline_state(MTLContext *ctx, MTLPrimitiveTopologyClass prim_type, const MTLRenderPipelineStateDescriptor &pipeline_descriptor)
MTLRenderPipelineStateInstance * bake_current_pipeline_state(MTLContext *ctx, MTLPrimitiveTopologyClass prim_type)
MTLShader(MTLContext *ctx, const char *name)
Definition mtl_shader.mm:91
std::string geometry_layout_declare(const shader::ShaderCreateInfo &) const override
MTLRenderPipelineStateDescriptor & get_current_pipeline_state()
void init(const shader::ShaderCreateInfo &, bool) override
void fragment_shader_from_glsl(const shader::ShaderCreateInfo &info, MutableSpan< StringRefNull > sources) override
std::string geometry_interface_declare(const shader::ShaderCreateInfo &) const override
MTLPushConstantBuf * get_push_constant_buf()
ShaderCompiler(uint32_t threads_count=1, GPUWorker::ContextType context_type=GPUWorker::ContextType::PerThread, bool support_specializations=false)
ShaderInterface * interface
Shader(const char *name)
#define FORMAT_PER_COMP_INT(_type)
#define RESIZE_TYPE(_type, _suffix)
#define FORMAT_PER_TYPE(_comp, _value)
#define FORMAT_PER_COMP_SMALL_INT(_type)
#define FORMAT_PER_COMP(_type, _suffix)
MTLVertexFormat format_resize_comp(MTLVertexFormat mtl_format, uint32_t components)
MTLVertexFormat format_get_component_type(MTLVertexFormat mtl_format)
MTLVertexFormat to_mtl(GPUVertCompType component_type, GPUVertFetchMode fetch_mode, uint32_t component_len)
bool mtl_format_is_normalized(MTLVertexFormat format)
int mtl_format_component_len(MTLVertexFormat format)
unsigned __int64 uint64_t
Definition BKE_text.h:91
@ GPU_FETCH_INT_TO_FLOAT_UNIT
unsigned short uint16_t
Definition BKE_text.h:80
unsigned int uint32_t
Definition BKE_text.h:81
BMesh const char void * data
void set_compute_workgroup_size(int workgroup_size_x, int workgroup_size_y, int workgroup_size_z)
Definition mtl_shader.hh:90
void parse_reflection_data(::MTLRenderPipelineReflection *reflection_data)
Describe inputs & outputs, stage interfaces, resources and sources of a shader. If all data is correc...