19#include <fmt/format.h>
56 return "Vertex Shader";
58 return "Fragment Shader";
60 return "Compute Shader";
64 return "Unknown Shader Stage";
69 std::string define =
"#define ";
72 define +=
"GPU_VERTEX_SHADER";
75 define +=
"GPU_FRAGMENT_SHADER";
78 define +=
"GPU_COMPUTE_SHADER";
100 MEM_SAFE_DELETE(push_constant_buf_);
108 if (pso_descriptor_ != nil) {
109 [pso_descriptor_ release];
110 pso_descriptor_ = nil;
114 pso_cache_lock_.lock();
118 [pso_inst->pso release];
121 if (pso_inst->vert) {
122 [pso_inst->vert release];
125 if (pso_inst->frag) {
126 [pso_inst->frag release];
136 [pso_inst->pso release];
139 if (pso_inst->compute) {
140 [pso_inst->compute release];
143 compute_pso_cache_.clear();
144 pso_cache_lock_.unlock();
147 if (shader_library_vert_ != nil) {
148 [shader_library_vert_ release];
149 shader_library_vert_ = nil;
151 if (shader_library_frag_ != nil) {
152 [shader_library_frag_ release];
153 shader_library_frag_ = nil;
155 if (shader_library_comp_ != nil) {
156 [shader_library_comp_ release];
157 shader_library_comp_ = nil;
175 patched_info_ = std::make_unique<PatchedShaderCreateInfo>(original_info);
189std::string MTLShader::entry_point_name_get(
const ShaderStage stage)
193 for (
char &c :
name) {
194 if (!std::isalnum(c)) {
201 return "_" +
name +
"_vert";
203 return "_" +
name +
"_frag";
205 return "_" +
name +
"_comp";
214 const bool use_texture_atomic,
215 const bool use_ray_query)
217 ::MTLCompileOptions *
options = [[MTLCompileOptions alloc]
init];
218 options.languageVersion = MTLLanguageVersion2_2;
221 options.preserveInvariance = YES;
225 if (use_subpass_input) {
226 options.languageVersion = MTLLanguageVersion2_3;
230 options.languageVersion = MTLLanguageVersion3_0;
232#if defined(MAC_OS_VERSION_14_0)
233 if (@available(macOS 14.0, *)) {
235 if (use_texture_atomic) {
236 options.languageVersion = MTLLanguageVersion3_1;
248 info, stage, entry_point_name_get(stage));
250 std::string shader_compat;
252 std::stringstream ss;
258 ss <<
"#define GPU_PROVOKING_VERTEX_LAST\n";
260 ss <<
"#define MTL_USE_SAMPLER_ARGUMENT_BUFFER\n";
266 ss <<
"#define MTL_SUPPORTS_TEXTURE_ATOMICS 1\n";
270 ss <<
"#define MTL_USE_RAY_QUERY\n";
273 shader::GeneratedSource defines_src{
"gpu_shader_msl_defines.msl", {}, ss.str()};
274 shader::GeneratedSource wrapper_src{
275 "gpu_shader_msl_wrapper.msl", {
"gpu_shader_msl_types.msl"}, wrapper.first};
280 "gpu_shader_compat_msl.msl", generated_sources);
281 shader_compat = fmt::to_string(fmt::join(compatibility_src,
""));
286 const std::string original_source = fmt::to_string(fmt::join(sources,
"")) + wrapper.second;
289 this->
name_get(), this->entry_point_name_get(stage),
".msl", original_source);
291 std::string processed_source;
296 this->entry_point_name_get(stage) +
".expanded",
301 processed_source = original_source;
306 processed_source =
"#include <metal_raytracing>\nusing namespace metal::raytracing;\n" +
316 NSError *error =
nullptr;
317 id<MTLLibrary> library = [context_->device
318 newLibraryWithSource:[NSString stringWithUTF8String:processed_source.c_str()]
321 library.label = [NSString stringWithUTF8String:this->
name];
325 if (error ==
nullptr) {
329 NSString *error_localized = [error localizedDescription];
332 if ([error_localized rangeOfString:
@"Compilation succeeded"].
length > 0) {
340 print_log({original_source}, [error_localized UTF8String],
to_string(stage),
true, &parser);
354 MTL_LOG_ERROR(
"MTLShader::geometry_shader_from_glsl - Geometry shaders unsupported!");
377 if (this->shader_library_vert_ == nil && this->shader_library_frag_ == nil &&
378 this->shader_library_comp_ == nil)
384 const bool is_compute = (this->shader_library_vert_ == nil && this->shader_library_frag_ == nil);
386 if (!is_compute && (this->shader_library_vert_ == nil || this->shader_library_frag_ == nil)) {
393 push_constant_buf_ = MEM_new<MTLPushConstantBuf>(
"MTLPushConstantBuf", *info);
417 pso_descriptor_ = [[MTLRenderPipelineDescriptor alloc]
init];
418 pso_descriptor_.label = [NSString stringWithUTF8String:this->name];
438 "MTLShader::bind - Shader '%s' has no valid implementation in Metal, draw calls will be "
458 if (push_constant_buf_ ==
nullptr || location < 0 || location >= push_constant_buf_->size()) {
459 MTL_LOG_WARNING(
"Shader %s: Invalid uniform location %d", this->name, location);
468 else if (comp_len == 16) {
475 comp_len *= array_size;
487 constexpr int size_padded = 16;
488 const size_t data_size = comp_len *
sizeof(
float);
490 const void *src =
data + i * comp_len;
491 void *dst = push_constant_buf_->data() + (location + i * size_padded);
492 if (update || memcmp(dst, src, data_size) != 0) {
493 memcpy(dst, src, data_size);
499 push_constant_buf_->tag_dirty();
505 static_assert(
sizeof(int) ==
sizeof(
float),
"int to float reinterpret expect matching size");
506 uniform_float(location, comp_len, array_size,
reinterpret_cast<const float *
>(
data));
521 parent_mtl->pso_cache_lock_.lock();
522 for (
const auto &pso_entry : parent_mtl->pso_cache_.items()) {
525 descriptors.append(pso_descriptor);
526 prim_classes.append(pso_inst->
prim_type);
528 parent_mtl->pso_cache_lock_.unlock();
532 limit = (limit > 0) ? limit : descriptors.
size();
535 const MTLPrimitiveTopologyClass &prim_class = prim_classes[i];
558 ::MTLFunctionConstantValues *values = [MTLFunctionConstantValues
new];
560 for (
auto i : shader_constants.
types.index_range()) {
564 switch (shader_constants.
types[i]) {
566 [values setConstantValue:&value.i type:MTLDataTypeInt atIndex:
index];
569 [values setConstantValue:&value.u type:MTLDataTypeUInt atIndex:
index];
572 [values setConstantValue:&value.u type:MTLDataTypeBool atIndex:
index];
575 [values setConstantValue:&value.f type:MTLDataTypeFloat atIndex:
index];
593 for (
int i = 0; i < [args count]; i++) {
594 MTLArgument *arg = [args objectAtIndex:i];
595 if ([arg type] == MTLArgumentTypeBuffer && [arg
isActive] == TRUE) {
608 for (
int i = 0; i < [args count]; i++) {
609 MTLArgument *arg = [args objectAtIndex:i];
610 if ([arg type] == MTLArgumentTypeTexture && [arg
isActive] == TRUE) {
623 for (
int i = 0; i < [args count]; i++) {
624 MTLArgument *arg = [args objectAtIndex:i];
625 if ([arg type] == MTLArgumentTypeTexture && [arg
isActive] == TRUE) {
636 MTLRenderPipelineReflection *reflection_data)
668 MTLContext *ctx, MTLPrimitiveTopologyClass prim_type)
692 if (color_attachment.
used) {
698 mtl_format = MTLPixelFormatRGBA8Unorm;
708 if (color_attachment.
ignored) {
717 MTLPixelFormatInvalid;
719 (stencil_attachment.
used) ?
721 MTLPixelFormatInvalid;
743 bool requires_specific_topology_class =
interface.use_layer() ||
745 prim_type == MTLPrimitiveTopologyClassPoint;
747 (requires_specific_topology_class) ? prim_type : MTLPrimitiveTopologyClassUnspecified;
760 MTLPrimitiveTopologyClass prim_type,
766 pso_cache_lock_.lock();
769 pso_cache_lock_.unlock();
771 if (pipeline_state !=
nullptr) {
772 return pipeline_state;
788 ::MTLRenderPipelineDescriptor *desc = pso_descriptor_;
790 desc.label = [NSString stringWithUTF8String:this->name];
802 ::MTLVertexAttributeDescriptor *mtl_attribute = mtl_vert_desc.
attributes[i];
804 mtl_attribute.offset = attribute_desc.
offset;
805 mtl_attribute.bufferIndex = attribute_desc.
buffer_index;
813 MTLVertexBufferLayoutDescriptor *mtl_layout = mtl_vert_desc.layouts[buf_layout.
buffer_slot];
815 mtl_layout.stepRate = buf_layout.
step_rate;
816 mtl_layout.stride = buf_layout.
stride;
824 bool using_null_buffer =
false;
827 ::MTLVertexAttributeDescriptor *mtl_attribute = mtl_vert_desc.
attributes[attr.binding];
828 if (mtl_attribute.format == MTLVertexFormatInvalid) {
833 mtl_attribute.offset = 0;
834 mtl_attribute.bufferIndex = null_buffer_index;
835 using_null_buffer =
true;
839 if (using_null_buffer) {
840 MTLVertexBufferLayoutDescriptor *mtl_layout = mtl_vert_desc.layouts[null_buffer_index];
843 mtl_layout.stepFunction = MTLVertexStepFunctionConstant;
844 mtl_layout.stepRate = 0;
845 mtl_layout.stride = 16;
846 vbo_bind_mask |= 1 << null_buffer_index;
853 bool null_pointsize =
true;
854 float MTL_pointsize = pipeline_descriptor.
point_size;
859 if (MTL_pointsize < 0.0) {
860 MTL_pointsize =
fabsf(MTL_pointsize);
861 [values setConstantValue:&MTL_pointsize
862 type:MTLDataTypeFloat
863 withName:
@"MTL_global_pointsize"];
864 null_pointsize =
false;
868 if (null_pointsize) {
869 MTL_pointsize = 0.0f;
870 [values setConstantValue:&MTL_pointsize
871 type:MTLDataTypeFloat
872 withName:
@"MTL_global_pointsize"];
875 bool has_error =
false;
878 NSError *error =
nullptr;
880 desc.vertexFunction = [shader_library_vert_
881 newFunctionWithName:[NSString stringWithUTF8String:function_name.c_str()]
882 constantValues:values
886 [[error localizedDescription] rangeOfString:
@"Compilation succeeded"].location ==
889 const char *errors_c_str = [[error localizedDescription] UTF8String];
890 MTL_LOG_ERROR(
"%s : %s", function_name.c_str(), errors_c_str);
896 NSError *error =
nullptr;
898 desc.fragmentFunction = [shader_library_frag_
899 newFunctionWithName:[NSString stringWithUTF8String:function_name.c_str()]
900 constantValues:values
904 [[error localizedDescription] rangeOfString:
@"Compilation succeeded"].location ==
907 const char *errors_c_str = [[error localizedDescription] UTF8String];
908 MTL_LOG_ERROR(
"%s : %s", function_name.c_str(), errors_c_str);
926 MTLRenderPipelineColorAttachmentDescriptor *col_attachment =
927 desc.colorAttachments[color_attachment];
929 col_attachment.pixelFormat = pixel_format;
930 if (pixel_format != MTLPixelFormatInvalid) {
938 col_attachment.writeMask = MTLColorWriteMaskNone;
941 format_supports_blending;
943 col_attachment.alphaBlendOperation = pipeline_descriptor.
alpha_blend_op;
944 col_attachment.rgbBlendOperation = pipeline_descriptor.
rgb_blend_op;
953 "Attempting to Bake PSO, but MTLPixelFormat %d does not support blending",
967 BLI_assert_msg((MTL_uniform_buffer_base_index + get_max_ubo_index() + 2) <
969 "UBO and SSBO bindings exceed the fragment bind table limit.");
973 MTLAutoreleasedRenderPipelineReflection reflection_data;
974 NSError *error =
nullptr;
978 newRenderPipelineStateWithDescriptor:desc
979 options:MTLPipelineOptionBufferTypeInfo
980 reflection:&reflection_data
984 "Failed to create PSO for shader: %s error %s",
986 [[error localizedDescription] UTF8String]);
992 &
LOG,
"Failed to create PSO for shader: %s, but no error was provided!", this->name);
999 pso_inst->
vert = desc.vertexFunction;
1000 pso_inst->
frag = desc.fragmentFunction;
1001 pso_inst->
pso = pso;
1010 pso_cache_lock_.lock();
1012 pso_cache_.add(pipeline_descriptor, pso_inst);
1013 pso_cache_lock_.unlock();
1015 "PSO CACHE: Stored new variant in PSO cache for shader '%s' Hash: '%llu'",
1017 (
unsigned long long)pipeline_descriptor.
hash());
1029 pso_cache_lock_.lock();
1031 compute_pipeline_descriptor);
1033 pso_cache_lock_.unlock();
1035 if (pipeline_state !=
nullptr) {
1038 return pipeline_state;
1050 NSError *error =
nullptr;
1054 newFunctionWithName:[NSString stringWithUTF8String:function_name.c_str()]
1055 constantValues:values
1057 compute_function.label = [NSString stringWithUTF8String:this->name];
1063 "Metal Shader compute function, error %s",
1064 [[error localizedDescription] UTF8String]);
1067 if ([[error localizedDescription] rangeOfString:
@"Compilation succeeded"].location ==
1075 ::MTLComputePipelineDescriptor *desc = [[MTLComputePipelineDescriptor alloc]
init];
1076 desc.label = [NSString stringWithUTF8String:this->name];
1077 desc.computeFunction = compute_function;
1080 newComputePipelineStateWithDescriptor:desc
1092 uint num_required_threads_per_threadgroup = compute_pso_common_state_.threadgroup_x_len *
1093 compute_pso_common_state_.threadgroup_y_len *
1094 compute_pso_common_state_.threadgroup_z_len;
1095 if (pso.maxTotalThreadsPerThreadgroup < num_required_threads_per_threadgroup) {
1097 "Shader '%s' requires %u threads per threadgroup, but PSO limit is: %lu. Recompiling "
1098 "with increased limit on descriptor.\n",
1100 num_required_threads_per_threadgroup,
1101 (
unsigned long)pso.maxTotalThreadsPerThreadgroup);
1104 desc.maxTotalThreadsPerThreadgroup = 1024;
1105 pso = [ctx->
device newComputePipelineStateWithDescriptor:desc
1116 "Failed to create PSO for compute shader: %s error %s",
1118 [[error localizedDescription] UTF8String]);
1123 "Failed to create PSO for compute shader: %s, but no error was provided!",
1130 compute_pso_instance->
compute = compute_function;
1131 compute_pso_instance->
pso = pso;
1132 pso_cache_lock_.lock();
1134 compute_pso_cache_.add(compute_pipeline_descriptor, compute_pso_instance);
1135 pso_cache_lock_.unlock();
1137 return compute_pso_instance;
1168 "Shader must be finalized before precompiling specializations");
1170 if (!
shader->has_compute_shader_lib()) {
1179 shader->bake_compute_pipeline_state(metal_context, compute_pipeline_descriptor);
Platform independent time functions.
#define CLOG_ERROR(clg_ref,...)
#define CLOG_DEBUG(clg_ref,...)
#define CLOG_WARN(clg_ref,...)
#define BLI_assert_unreachable()
#define BLI_assert_msg(a, msg)
SIMD_FORCE_INLINE bool isActive() const
SIMD_FORCE_INLINE btScalar length() const
Return the length of the vector.
constexpr IndexRange()=default
StateManager * state_manager
static MTLCapabilities & get_capabilities()
MTLFrameBuffer * get_current_framebuffer()
static MTLContext * get()
MTLContextGlobalShaderPipelineState pipeline_state
void specialization_constants_set(const shader::SpecializationConstants *constants_state)
MTLAttachment get_color_attachment(uint slot)
MTLAttachment get_stencil_attachment()
MTLAttachment get_depth_attachment()
Shader * compile_shader(const shader::ShaderCreateInfo &info) override
void specialize_shader(const ShaderSpecialization &specialization) 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
MTLComputePipelineStateInstance * bake_compute_pipeline_state(MTLContext *ctx, MTLComputePipelineStateDescriptor &compute_pipeline_descriptor)
const shader::ShaderCreateInfo & patch_create_info(const shader::ShaderCreateInfo &original_info) override
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()
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)
void init(const shader::ShaderCreateInfo &, bool) override
void fragment_shader_from_glsl(const shader::ShaderCreateInfo &info, MutableSpan< StringRefNull > sources) override
MTLRenderPipelineStateDescriptor & get_pipeline_descriptor()
Shader * compile(const shader::ShaderCreateInfo &info, bool is_codegen_only)
ShaderCompiler(uint32_t threads_count=1, GPUWorker::ContextType context_type=GPUWorker::ContextType::PerThread, bool support_specializations=false)
std::unique_ptr< PatchedShaderCreateInfo > patched_info_
static void dump_source_to_disk(StringRef shader_name, StringRef shader_name_with_stage_name, StringRef extension, StringRef source)
std::unique_ptr< const shader::SpecializationConstants > constants
ShaderInterface * interface
StringRefNull name_get() const
static std::string run_preprocessor(StringRef source, bool no_dead_code_elimination)
void print_log(Span< StringRefNull > sources, const char *log, const char *stage, bool error, GPULogParser *parser)
TextureFormat format_get() const
CCL_NAMESPACE_BEGIN struct Options options
#define GPU_FB_MAX_COLOR_ATTACHMENT
#define SOURCES_INDEX_VERSION
#define MTL_MAX_BUFFER_BINDINGS
#define MTL_MAX_TEXTURE_SLOTS
#define MTL_MAX_IMAGE_SLOTS
#define MTL_LOG_WARNING(info,...)
#define MTL_LOG_ERROR(info,...)
#define MTL_SPECIALIZATION_CONSTANT_OFFSET
Vector< shader::GeneratedSource, 0 > GeneratedSourceList
Vector< StringRefNull > gpu_shader_dependency_get_resolved_source(const StringRefNull shader_source_name, const shader::GeneratedSourceList &generated_sources, const StringRefNull shader_name)
MTLPixelFormat gpu_texture_format_to_metal(TextureFormat tex_format)
const char * to_string(ShaderStage stage)
std::pair< std::string, std::string > generate_entry_point(const ShaderCreateInfo &info, const ShaderStage stage, const StringRefNull entry_point_name)
static uint16_t get_images_binding_mask(NSArray< MTLArgument * > *args)
::MTLCompileOptions * get_compile_options(const bool use_subpass_input, const bool use_texture_atomic, const bool use_ray_query)
std::string shader_stage_define(const ShaderStage stage)
MTLVertexFormat gpu_type_to_metal_vertex_format(shader::Type type)
::MTLFunctionConstantValues * populate_specialization_constant_values(const shader::SpecializationConstants &shader_constants, const SpecializationStateDescriptor &specialization_descriptor)
bool mtl_format_supports_blending(MTLPixelFormat format)
static uint64_t get_samplers_binding_mask(NSArray< MTLArgument * > *args)
void patch_create_info_atomic_workaround(std::unique_ptr< PatchedShaderCreateInfo > &patched_info, const shader::ShaderCreateInfo &original_info)
static uint32_t get_buffers_binding_mask(NSArray< MTLArgument * > *args)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned __int64 uint64_t
@ G_DEBUG_GPU_SHADER_NO_DCE
BMesh const char void * data
MINLINE unsigned int bitscan_reverse_uint(unsigned int a)
int GPU_max_parallel_compilations()
MINLINE int min_ii(int a, int b)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
gpu::shader::SpecializationConstants constants
gpu::MTLTexture * texture
SpecializationStateDescriptor specialization_state
id< MTLFunction > compute
id< MTLComputePipelineState > pso
uint32_t shader_pso_index
MTLColorWriteMask color_write_mask
MTLBlendFactor src_rgb_blend_factor
MTLBlendOperation alpha_blend_op
MTLBlendFactor src_alpha_blend_factor
MTLShader * active_shader
MTLBlendFactor dest_alpha_blend_factor
MTLBlendOperation rgb_blend_op
MTLBlendFactor dest_rgb_blend_factor
bool clip_distance_enabled[6]
uchar clipping_plane_enable_mask
MTLBlendFactor dest_alpha_blend_factor
MTLBlendOperation alpha_blend_op
MTLPixelFormat color_attachment_format[GPU_FB_MAX_COLOR_ATTACHMENT]
MTLPixelFormat depth_attachment_format
MTLVertexDescriptor vertex_descriptor
int num_color_attachments
MTLColorWriteMask color_write_mask
MTLBlendFactor dest_rgb_blend_factor
MTLBlendFactor src_alpha_blend_factor
MTLBlendFactor src_rgb_blend_factor
MTLPixelFormat stencil_attachment_format
MTLBlendOperation rgb_blend_op
uint8_t color_attachment_mask
SpecializationStateDescriptor specialization_state
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
::MTLVertexStepFunction step_function
::MTLPrimitiveTopologyClass prim_topology_class
MTLVertexAttributeDescriptorPSO attributes[GPU_VERT_ATTR_MAX_LEN]
MTLVertexBufferLayoutDescriptorPSO buffer_layouts[GPU_BATCH_VBO_MAX_LEN]
Vector< shader::SpecializationConstant::Value > values
Describe inputs & outputs, stage interfaces, resources and sources of a shader. If all data is correc...
int max_sampler_slot() const
Vector< PushConst, 0 > push_constants_
Vector< SubpassIn, 0 > subpass_inputs_
ComputeStageLayout compute_layout_
BuiltinBits builtins_combined() const
Vector< SpecializationConstant::Value, 8 > values
Vector< gpu::shader::Type, 8 > types