Blender V5.0
blender::gpu::MTLShader Class Reference

#include <mtl_shader.hh>

Inherits blender::gpu::Shader.

Public Member Functions

bool finalize_compute (const shader::ShaderCreateInfo *info)
bool is_valid ()
bool has_compute_shader_lib ()
bool has_parent_shader ()
MTLRenderPipelineStateDescriptorget_current_pipeline_state ()
MTLShaderInterfaceget_interface ()
void * get_push_constant_data ()
std::string geometry_interface_declare (const shader::ShaderCreateInfo &info) const override
const MTLComputePipelineStateCommonget_compute_common_state ()
Creation / Destruction.
 MTLShader (MTLContext *ctx, const char *name)
 MTLShader (MTLContext *ctx, MTLShaderInterface *interface, const char *name, NSString *input_vertex_source, NSString *input_fragment_source, NSString *vertex_function_name_, NSString *fragment_function_name_)
 ~MTLShader ()
void init (const shader::ShaderCreateInfo &, bool is_batch_compilation) override
Shader stage creation.
void vertex_shader_from_glsl (MutableSpan< StringRefNull > sources) override
void geometry_shader_from_glsl (MutableSpan< StringRefNull > sources) override
void fragment_shader_from_glsl (MutableSpan< StringRefNull > sources) override
void compute_shader_from_glsl (MutableSpan< StringRefNull > sources) override
bool finalize (const shader::ShaderCreateInfo *info=nullptr) override
Shader Binding.
void warm_cache (int limit) override
void bind (const shader::SpecializationConstants *constants_state) override
void unbind () override
void uniform_float (int location, int comp_len, int array_size, const float *data) override
void uniform_int (int location, int comp_len, int array_size, const int *data) override
bool get_push_constant_is_dirty ()
void push_constant_bindstate_mark_dirty (bool is_dirty)
MTLShader builtin shader generation utilities.
std::string resources_declare (const shader::ShaderCreateInfo &info) const override
std::string vertex_interface_declare (const shader::ShaderCreateInfo &info) const override
std::string fragment_interface_declare (const shader::ShaderCreateInfo &info) const override
std::string geometry_layout_declare (const shader::ShaderCreateInfo &info) const override
std::string compute_layout_declare (const shader::ShaderCreateInfo &info) const override
METAL Custom Behavior
void set_vertex_function_name (NSString *vetex_function_name)
void set_fragment_function_name (NSString *fragment_function_name)
void set_compute_function_name (NSString *compute_function_name)
void shader_source_from_msl (NSString *input_vertex_source, NSString *input_fragment_source)
void shader_compute_source_from_msl (NSString *input_compute_source)
void set_interface (MTLShaderInterface *interface)
Bake Pipeline State Objects
MTLRenderPipelineStateInstancebake_current_pipeline_state (MTLContext *ctx, MTLPrimitiveTopologyClass prim_type)
MTLRenderPipelineStateInstancebake_pipeline_state (MTLContext *ctx, MTLPrimitiveTopologyClass prim_type, const MTLRenderPipelineStateDescriptor &pipeline_descriptor)
MTLComputePipelineStateInstancebake_compute_pipeline_state (MTLContext *ctx, MTLComputePipelineStateDescriptor &compute_pipeline_descriptor)
Public Member Functions inherited from blender::gpu::Shader
std::string defines_declare (const shader::ShaderCreateInfo &info) const
StringRefNull name_get () const
void parent_set (Shader *parent)
Shaderparent_get () const
 Shader (const char *name)
virtual ~Shader ()
void specialization_constants_init (const shader::ShaderCreateInfo &info)

Additional Inherited Members

Static Public Member Functions inherited from blender::gpu::Shader
static void set_scene_linear_to_xyz_uniform (gpu::Shader *shader)
static void set_srgb_uniform (Context *ctx, gpu::Shader *shader)
static void set_framebuffer_srgb_target (int use_srgb_to_linear)
Public Attributes inherited from blender::gpu::Shader
ShaderInterfaceinterface = nullptr
uint16_t fragment_output_bits = 0
std::unique_ptr< const shader::SpecializationConstantsconstants
bool is_polyline = false
Protected Member Functions inherited from blender::gpu::Shader
void print_log (Span< StringRefNull > sources, const char *log, const char *stage, bool error, GPULogParser *parser)
Protected Attributes inherited from blender::gpu::Shader
char name [64]
Shaderparent_shader_ = nullptr

Detailed Description

MTLShader implements shader compilation, Pipeline State Object (PSO) creation for rendering and uniform data binding. Shaders can either be created from native MSL, or generated from a GLSL source shader using #GPUShaderCreateInfo.

Shader creation process:

  • Create MTLShader:
    • Convert GLSL to MSL source if required.
  • set MSL source.
  • set Vertex/Fragment function names.
  • Create and populate MTLShaderInterface.

Definition at line 166 of file mtl_shader.hh.

Constructor & Destructor Documentation

◆ MTLShader() [1/2]

blender::gpu::MTLShader::MTLShader ( MTLContext * ctx,
const char * name )

Definition at line 71 of file mtl_shader.mm.

References i, blender::gpu::Shader::name, and blender::gpu::Shader::Shader().

Referenced by MTLShader(), and warm_cache().

◆ MTLShader() [2/2]

blender::gpu::MTLShader::MTLShader ( MTLContext * ctx,
MTLShaderInterface * interface,
const char * name,
NSString * input_vertex_source,
NSString * input_fragment_source,
NSString * vertex_function_name_,
NSString * fragment_function_name_ )

◆ ~MTLShader()

blender::gpu::MTLShader::~MTLShader ( )

Definition at line 111 of file mtl_shader.mm.

References is_valid(), and MEM_freeN().

Member Function Documentation

◆ bake_compute_pipeline_state()

MTLComputePipelineStateInstance * blender::gpu::MTLShader::bake_compute_pipeline_state ( MTLContext * ctx,
MTLComputePipelineStateDescriptor & compute_pipeline_descriptor )

If Max Total threads per threadgroup tuning parameters are specified, compile with these. This enables the compiler to make informed decisions based on the upper bound of threads issued for a given compute call. This per-shader tuning can reduce the static register memory allocation by reducing the worst-case allocation and increasing thread occupancy.

NOTE: This is only enabled on Apple M1 and M2 GPUs. Apple M3 GPUs feature dynamic caching which controls register allocation dynamically based on the runtime state.

Definition at line 1402 of file mtl_shader.mm.

References blender::gpu::APPLE_GPU_M1, blender::gpu::APPLE_GPU_M2, blender::gpu::MTLComputePipelineStateInstance::base_storage_buffer_index, blender::gpu::MTLComputePipelineStateInstance::base_uniform_buffer_index, BLI_assert, blender::gpu::MTLComputePipelineStateInstance::compute, blender::gpu::Shader::constants, blender::gpu::MTLContext::device, ELEM, error(), blender::gpu::MTLBackend::get_capabilities(), get_interface(), blender::gpu::MTLShaderInterface::get_total_uniform_blocks(), blender::gpu::MTLCapabilities::gpu, init(), is_valid(), MTL_LOG_DEBUG, MTL_LOG_WARNING, blender::gpu::Shader::name, options, blender::gpu::populate_specialization_constant_values(), blender::gpu::MTLComputePipelineStateInstance::pso, blender::gpu::MTLComputePipelineStateInstance::shader_pso_index, and blender::gpu::MTLComputePipelineStateDescriptor::specialization_state.

Referenced by blender::gpu::MTLContext::ensure_compute_pipeline_state(), and finalize().

◆ bake_current_pipeline_state()

MTLRenderPipelineStateInstance * blender::gpu::MTLShader::bake_current_pipeline_state ( MTLContext * ctx,
MTLPrimitiveTopologyClass prim_type )

Bakes or fetches a pipeline state using the current MTLRenderPipelineStateDescriptor state.

This state contains information on shader inputs/outputs, such as the vertex descriptor, used to control vertex assembly for current vertex data, and active render target information, describing the output attachment pixel formats.

Other rendering parameters such as global point-size, blend state, color mask etc; are also used. See mtl_shader.h for full #MLRenderPipelineStateDescriptor.

Populate global pipeline descriptor and use this to prepare new PSO.

Definition at line 841 of file mtl_shader.mm.

References blender::gpu::MTLContextGlobalShaderPipelineState::alpha_blend_op, blender::gpu::MTLRenderPipelineStateDescriptor::alpha_blend_op, bake_pipeline_state(), blender::gpu::MTLContextGlobalShaderPipelineState::blending_enabled, blender::gpu::MTLRenderPipelineStateDescriptor::blending_enabled, BLI_assert, blender::gpu::MTLContextGlobalShaderPipelineState::clip_distance_enabled, blender::gpu::MTLRenderPipelineStateDescriptor::clipping_plane_enable_mask, blender::gpu::MTLRenderPipelineStateDescriptor::color_attachment_format, blender::gpu::MTLContextGlobalShaderPipelineState::color_write_mask, blender::gpu::MTLRenderPipelineStateDescriptor::color_write_mask, blender::gpu::MTLContext::constants_state, blender::gpu::MTLRenderPipelineStateDescriptor::depth_attachment_format, blender::gpu::MTLContextGlobalShaderPipelineState::dest_alpha_blend_factor, blender::gpu::MTLRenderPipelineStateDescriptor::dest_alpha_blend_factor, blender::gpu::MTLContextGlobalShaderPipelineState::dest_rgb_blend_factor, blender::gpu::MTLRenderPipelineStateDescriptor::dest_rgb_blend_factor, blender::gpu::Texture::format_get(), blender::gpu::MTLContext::get(), blender::gpu::MTLFrameBuffer::get_color_attachment(), blender::gpu::MTLContext::get_current_framebuffer(), blender::gpu::MTLFrameBuffer::get_depth_attachment(), blender::gpu::MTLFrameBuffer::get_is_srgb(), blender::gpu::MTLStateManager::get_pipeline_descriptor(), blender::gpu::MTLFrameBuffer::get_srgb_enabled(), blender::gpu::MTLFrameBuffer::get_stencil_attachment(), GPU_FB_MAX_COLOR_ATTACHMENT, blender::gpu::gpu_texture_format_to_metal(), is_valid(), blender::gpu::MTLRenderPipelineStateDescriptor::num_color_attachments, blender::gpu::MTLContext::pipeline_state, blender::gpu::MTLContextGlobalShaderPipelineState::point_size, blender::gpu::MTLRenderPipelineStateDescriptor::point_size, blender::gpu::MTLVertexDescriptor::prim_topology_class, blender::gpu::MTLContextGlobalShaderPipelineState::rgb_blend_op, blender::gpu::MTLRenderPipelineStateDescriptor::rgb_blend_op, blender::gpu::MTLRenderPipelineStateDescriptor::specialization_state, blender::gpu::MTLContextGlobalShaderPipelineState::src_alpha_blend_factor, blender::gpu::MTLRenderPipelineStateDescriptor::src_alpha_blend_factor, blender::gpu::MTLContextGlobalShaderPipelineState::src_rgb_blend_factor, blender::gpu::MTLRenderPipelineStateDescriptor::src_rgb_blend_factor, blender::gpu::Context::state_manager, blender::gpu::MTLRenderPipelineStateDescriptor::stencil_attachment_format, blender::gpu::MTLAttachment::texture, blender::gpu::MTLAttachment::used, blender::gpu::shader::SpecializationConstants::values, and blender::gpu::MTLRenderPipelineStateDescriptor::vertex_descriptor.

◆ bake_pipeline_state()

MTLRenderPipelineStateInstance * blender::gpu::MTLShader::bake_pipeline_state ( MTLContext * ctx,
MTLPrimitiveTopologyClass prim_type,
const MTLRenderPipelineStateDescriptor & pipeline_descriptor )

Definition at line 926 of file mtl_shader.mm.

References blender::gpu::MTLRenderPipelineStateDescriptor::alpha_blend_op, blender::gpu::MTLVertexDescriptor::attributes, blender::gpu::MTLRenderPipelineStateInstance::base_storage_buffer_index, blender::gpu::MTLRenderPipelineStateInstance::base_uniform_buffer_index, blender::gpu::MTLRenderPipelineStateDescriptor::blending_enabled, BLI_assert, BLI_assert_msg, blender::gpu::MTLRenderPipelineStateInstance::buffer_bindings_reflection_data_frag, blender::gpu::MTLRenderPipelineStateInstance::buffer_bindings_reflection_data_vert, blender::gpu::MTLVertexAttributeDescriptorPSO::buffer_index, blender::gpu::MTLVertexDescriptor::buffer_layouts, blender::StringRefNull::c_str(), blender::gpu::MTLContextGlobalShaderPipelineState::clip_distance_enabled, blender::gpu::MTLRenderPipelineStateDescriptor::clipping_plane_enable_mask, blender::gpu::MTLRenderPipelineStateDescriptor::color_attachment_format, blender::gpu::MTLRenderPipelineStateDescriptor::color_write_mask, blender::gpu::Shader::constants, count, blender::gpu::MTLRenderPipelineStateDescriptor::depth_attachment_format, blender::gpu::MTLRenderPipelineStateDescriptor::dest_alpha_blend_factor, blender::gpu::MTLRenderPipelineStateDescriptor::dest_rgb_blend_factor, blender::gpu::MTLContext::device, error(), fabsf, blender::gpu::MTLShaderInputAttribute::format, blender::gpu::MTLVertexAttributeDescriptorPSO::format, blender::gpu::MTLVertexAttributeDescriptorPSO::format_conversion_mode, blender::gpu::MTLRenderPipelineStateInstance::frag, blender::gpu::MTLShaderInterface::get_argument_buffer_bind_index(), blender::gpu::MTLShaderInterface::get_attribute(), get_interface(), blender::gpu::MTLShaderInterface::get_name(), blender::gpu::MTLShaderInterface::get_name_at_offset(), blender::gpu::MTLShaderInterface::get_total_attributes(), blender::gpu::MTLShaderInterface::get_total_uniform_blocks(), GPU_FB_MAX_COLOR_ATTACHMENT, GPU_FETCH_INT_TO_FLOAT_UNIT, GPU_VERT_ATTR_MAX_LEN, blender::gpu::MTLRenderPipelineStateDescriptor::hash(), i, is_valid(), isActive(), blender::gpu::MTLShaderInputAttribute::location, blender::gpu::MTLVertexDescriptor::max_attribute_value, max_ii(), blender::gpu::mtl_format_supports_blending(), MTL_LOG_DEBUG, MTL_LOG_WARNING, MTL_MAX_BUFFER_BINDINGS, blender::gpu::Shader::name, blender::gpu::MTLShaderInputAttribute::name_offset, blender::gpu::MTLRenderPipelineStateInstance::null_attribute_buffer_index, blender::gpu::MTLVertexDescriptor::num_vert_buffers, blender::gpu::MTLVertexAttributeDescriptorPSO::offset, options, blender::gpu::MTLContext::pipeline_state, blender::gpu::MTLRenderPipelineStateDescriptor::point_size, blender::gpu::populate_specialization_constant_values(), blender::gpu::MTLVertexDescriptor::prim_topology_class, blender::gpu::MTLRenderPipelineStateInstance::prim_type, blender::gpu::Shader::print_log(), printf, blender::gpu::MTLRenderPipelineStateInstance::pso, blender::gpu::MTLRenderPipelineStateInstance::reflection_data_available, reset(), blender::gpu::MTLRenderPipelineStateDescriptor::rgb_blend_op, shader_debug_printf, blender::gpu::MTLRenderPipelineStateInstance::shader_pso_index, blender::gpu::MTLShaderInputAttribute::size, blender::gpu::MTLRenderPipelineStateDescriptor::specialization_state, blender::gpu::MTLRenderPipelineStateDescriptor::src_alpha_blend_factor, blender::gpu::MTLRenderPipelineStateDescriptor::src_rgb_blend_factor, blender::gpu::MTLRenderPipelineStateDescriptor::stencil_attachment_format, blender::gpu::MTLVertexBufferLayoutDescriptorPSO::step_function, blender::gpu::MTLVertexBufferLayoutDescriptorPSO::step_rate, blender::gpu::MTLVertexBufferLayoutDescriptorPSO::stride, blender::gpu::MTLShaderInterface::uses_argument_buffer_for_samplers(), blender::gpu::MTLRenderPipelineStateInstance::vert, and blender::gpu::MTLRenderPipelineStateDescriptor::vertex_descriptor.

Referenced by bake_current_pipeline_state(), and warm_cache().

◆ bind()

◆ compute_layout_declare()

std::string blender::gpu::MTLShader::compute_layout_declare ( const shader::ShaderCreateInfo & info) const
overridevirtual

Implements blender::gpu::Shader.

Definition at line 334 of file mtl_shader_generator.mm.

◆ compute_shader_from_glsl()

void blender::gpu::MTLShader::compute_shader_from_glsl ( MutableSpan< StringRefNull > sources)
overridevirtual

◆ finalize()

◆ finalize_compute()

bool blender::gpu::MTLShader::finalize_compute ( const shader::ShaderCreateInfo * info)

◆ fragment_interface_declare()

std::string blender::gpu::MTLShader::fragment_interface_declare ( const shader::ShaderCreateInfo & info) const
overridevirtual

◆ fragment_shader_from_glsl()

void blender::gpu::MTLShader::fragment_shader_from_glsl ( MutableSpan< StringRefNull > sources)
overridevirtual

◆ geometry_interface_declare()

std::string blender::gpu::MTLShader::geometry_interface_declare ( const shader::ShaderCreateInfo & info) const
overridevirtual

◆ geometry_layout_declare()

std::string blender::gpu::MTLShader::geometry_layout_declare ( const shader::ShaderCreateInfo & info) const
overridevirtual

Implements blender::gpu::Shader.

Definition at line 328 of file mtl_shader_generator.mm.

References BLI_assert_msg.

◆ geometry_shader_from_glsl()

void blender::gpu::MTLShader::geometry_shader_from_glsl ( MutableSpan< StringRefNull > sources)
overridevirtual

Implements blender::gpu::Shader.

Definition at line 217 of file mtl_shader.mm.

References MTL_LOG_ERROR.

◆ get_compute_common_state()

const MTLComputePipelineStateCommon & blender::gpu::MTLShader::get_compute_common_state ( )
inline

Definition at line 316 of file mtl_shader.hh.

◆ get_current_pipeline_state()

MTLRenderPipelineStateDescriptor & blender::gpu::MTLShader::get_current_pipeline_state ( )
inline

Definition at line 267 of file mtl_shader.hh.

◆ get_interface()

MTLShaderInterface * blender::gpu::MTLShader::get_interface ( )
inline

◆ get_push_constant_data()

void * blender::gpu::MTLShader::get_push_constant_data ( )
inline

Definition at line 275 of file mtl_shader.hh.

◆ get_push_constant_is_dirty()

bool blender::gpu::MTLShader::get_push_constant_is_dirty ( )

Definition at line 699 of file mtl_shader.mm.

◆ has_compute_shader_lib()

bool blender::gpu::MTLShader::has_compute_shader_lib ( )
inline

Definition at line 259 of file mtl_shader.hh.

◆ has_parent_shader()

bool blender::gpu::MTLShader::has_parent_shader ( )
inline

Definition at line 263 of file mtl_shader.hh.

References blender::gpu::Shader::parent_shader_.

◆ init()

void blender::gpu::MTLShader::init ( const shader::ShaderCreateInfo & ,
bool is_batch_compilation )
overridevirtual

Implements blender::gpu::Shader.

Definition at line 189 of file mtl_shader.mm.

Referenced by bake_compute_pipeline_state(), and finalize().

◆ is_valid()

◆ push_constant_bindstate_mark_dirty()

void blender::gpu::MTLShader::push_constant_bindstate_mark_dirty ( bool is_dirty)

Definition at line 704 of file mtl_shader.mm.

Referenced by finalize(), uniform_float(), and uniform_int().

◆ resources_declare()

◆ set_compute_function_name()

void blender::gpu::MTLShader::set_compute_function_name ( NSString * compute_function_name)

Definition at line 757 of file mtl_shader.mm.

◆ set_fragment_function_name()

void blender::gpu::MTLShader::set_fragment_function_name ( NSString * fragment_function_name)

Definition at line 752 of file mtl_shader.mm.

Referenced by MTLShader().

◆ set_interface()

void blender::gpu::MTLShader::set_interface ( MTLShaderInterface * interface)

Definition at line 778 of file mtl_shader.mm.

References BLI_assert, and blender::gpu::Shader::interface.

Referenced by finalize(), and MTLShader().

◆ set_vertex_function_name()

void blender::gpu::MTLShader::set_vertex_function_name ( NSString * vetex_function_name)

Definition at line 747 of file mtl_shader.mm.

Referenced by MTLShader().

◆ shader_compute_source_from_msl()

void blender::gpu::MTLShader::shader_compute_source_from_msl ( NSString * input_compute_source)

Definition at line 771 of file mtl_shader.mm.

References BLI_assert.

◆ shader_source_from_msl()

void blender::gpu::MTLShader::shader_source_from_msl ( NSString * input_vertex_source,
NSString * input_fragment_source )

Definition at line 762 of file mtl_shader.mm.

References BLI_assert.

Referenced by MTLShader().

◆ unbind()

void blender::gpu::MTLShader::unbind ( )
overridevirtual

◆ uniform_float()

◆ uniform_int()

◆ vertex_interface_declare()

std::string blender::gpu::MTLShader::vertex_interface_declare ( const shader::ShaderCreateInfo & info) const
overridevirtual

◆ vertex_shader_from_glsl()

void blender::gpu::MTLShader::vertex_shader_from_glsl ( MutableSpan< StringRefNull > sources)
overridevirtual

◆ warm_cache()


The documentation for this class was generated from the following files: