18#include <Metal/Metal.h>
19#include <QuartzCore/QuartzCore.h>
21#include <unordered_map>
42#define MTL_SHADER_DEBUG_EXPORT_SOURCE 0
43#define MTL_SHADER_TRANSLATION_DEBUG_OUTPUT 0
48#define MTL_SHADER_MAX_SPECIALIZED_PSOS 5
94 this->threadgroup_x_len = workgroup_size_x;
95 this->threadgroup_y_len = workgroup_size_y;
96 this->threadgroup_z_len = workgroup_size_z;
144 ::MTLRenderPipelineDescriptor *pso_descriptor_ = nil;
150 std::mutex pso_cache_lock_;
188 return (shader_library_comp_ != nil);
196 return current_pipeline_state_;
207 return push_constant_buf_;
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;
245 MTLPrimitiveTopologyClass prim_type);
249 MTLPrimitiveTopologyClass prim_type,
258 return compute_pso_common_state_;
267 std::string entry_point_name_get(
const ShaderStage stage);
269 MEM_CXX_CLASS_ALLOC_FUNCS(
"MTLShader");
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) { \
305 return MTLVertexFormat##_type##_suffix; \
307 return MTLVertexFormat##_type##2##_suffix; \
309 return MTLVertexFormat##_type##3##_suffix; \
311 return MTLVertexFormat##_type##4##_suffix; \
315 switch (mtl_format) {
335 return MTLVertexFormatInvalid;
347#define FORMAT_PER_COMP(_type, _suffix) \
348 switch (component_len) { \
350 return MTLVertexFormat##_type##_suffix; \
352 return MTLVertexFormat##_type##2##_suffix; \
354 return MTLVertexFormat##_type##3##_suffix; \
356 return MTLVertexFormat##_type##4##_suffix; \
358 BLI_assert_msg(0, "Invalid attribute component count"); \
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"); \
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"); \
386 switch (component_type) {
400 switch (fetch_mode) {
410 switch (fetch_mode) {
412 return MTLVertexFormatInt1010102Normalized;
422#undef FORMAT_PER_COMP
424 return MTLVertexFormatInvalid;
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: \
449 case MTLVertexFormatUInt1010102Normalized:
450 case MTLVertexFormatInt1010102Normalized:
451 case MTLVertexFormatUChar4Normalized_BGRA:
453#if defined(MAC_OS_VERSION_14_0)
454 case MTLVertexFormatFloatRG11B10:
456 case MTLVertexFormatFloatRGB9E5:
459 case MTLVertexFormatInvalid:
463#undef FORMAT_PER_TYPE
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: \
485#undef FORMAT_PER_TYPE
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
bool has_compute_shader_lib()
std::string compute_layout_declare(const shader::ShaderCreateInfo &) const override
void geometry_shader_from_glsl(const shader::ShaderCreateInfo &info, MutableSpan< StringRefNull > sources) 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)
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
#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
@ GPU_FETCH_INT_TO_FLOAT_UNIT
BMesh const char void * data
void set_compute_workgroup_size(int workgroup_size_x, int workgroup_size_y, int workgroup_size_z)
id< MTLFunction > compute
id< MTLComputePipelineState > pso
uint32_t shader_pso_index
int null_attribute_buffer_index
id< MTLRenderPipelineState > pso
uint16_t used_ima_vert_mask
uint64_t used_tex_vert_mask
uint16_t used_ima_frag_mask
uint64_t used_tex_frag_mask
uint32_t shader_pso_index
uint32_t used_buf_frag_mask
uint32_t used_buf_vert_mask
void parse_reflection_data(::MTLRenderPipelineReflection *reflection_data)
MTLPrimitiveTopologyClass prim_type
Describe inputs & outputs, stage interfaces, resources and sources of a shader. If all data is correc...