Changeset View
Changeset View
Standalone View
Standalone View
source/blender/gpu/metal/mtl_shader.hh
| Show First 20 Lines • Show All 64 Lines • ▼ Show 20 Lines | struct MTLRenderPipelineStateInstance { | ||||
| id<MTLRenderPipelineState> pso; | id<MTLRenderPipelineState> pso; | ||||
| /** Derived information. */ | /** Derived information. */ | ||||
| /* Unique index for PSO variant. */ | /* Unique index for PSO variant. */ | ||||
| uint32_t shader_pso_index; | uint32_t shader_pso_index; | ||||
| /* Base bind index for binding uniform buffers, offset based on other | /* Base bind index for binding uniform buffers, offset based on other | ||||
| * bound buffers such as vertex buffers, as the count can vary. */ | * bound buffers such as vertex buffers, as the count can vary. */ | ||||
| int base_uniform_buffer_index; | int base_uniform_buffer_index; | ||||
| /* Base bind index for binding storage buffers. */ | |||||
| int base_ssbo_buffer_index; | |||||
| /* buffer bind slot used for null attributes (-1 if not needed). */ | /* buffer bind slot used for null attributes (-1 if not needed). */ | ||||
| int null_attribute_buffer_index; | int null_attribute_buffer_index; | ||||
| /* buffer bind used for transform feedback output buffer. */ | /* buffer bind used for transform feedback output buffer. */ | ||||
| int transform_feedback_buffer_index; | int transform_feedback_buffer_index; | ||||
| /** Reflection Data. | /** Reflection Data. | ||||
| * Currently used to verify whether uniform buffers of incorrect sizes being bound, due to left | * Currently used to verify whether uniform buffers of incorrect sizes being bound, due to left | ||||
| * over bindings being used for slots that did not need updating for a particular draw. Metal | * over bindings being used for slots that did not need updating for a particular draw. Metal | ||||
| * Back-end over-generates bindings due to detecting their presence, though in many cases, the | * Back-end over-generates bindings due to detecting their presence, though in many cases, the | ||||
| * bindings in the source are not all used for a given shader. | * bindings in the source are not all used for a given shader. | ||||
| * This information can also be used to eliminate redundant/unused bindings. */ | * This information can also be used to eliminate redundant/unused bindings. */ | ||||
| bool reflection_data_available; | bool reflection_data_available; | ||||
| blender::Vector<MTLBufferArgumentData> buffer_bindings_reflection_data_vert; | blender::Vector<MTLBufferArgumentData> buffer_bindings_reflection_data_vert; | ||||
| blender::Vector<MTLBufferArgumentData> buffer_bindings_reflection_data_frag; | blender::Vector<MTLBufferArgumentData> buffer_bindings_reflection_data_frag; | ||||
| }; | }; | ||||
| /* Metal COmpute Pipeline State instance. */ | |||||
| struct MTLComputePipelineStateInstance { | |||||
| /* Function instances with specialization. | |||||
| * Required for argument encoder construction. */ | |||||
| id<MTLFunction> compute = nil; | |||||
| /* PSO handle. */ | |||||
| id<MTLComputePipelineState> pso = nil; | |||||
| /* Base bind index for binding uniform buffers, offset based on other | |||||
| * bound buffers such as vertex buffers, as the count can vary. */ | |||||
| int base_uniform_buffer_index = -1; | |||||
| /* Base bind index for binding storage buffers. */ | |||||
| int base_ssbo_buffer_index = -1; | |||||
| int threadgroup_x_len = 1; | |||||
| int threadgroup_y_len = 1; | |||||
| int threadgroup_z_len = 1; | |||||
| inline void set_compute_workgroup_size(int workgroup_size_x, | |||||
| int workgroup_size_y, | |||||
| int workgroup_size_z) | |||||
| { | |||||
| this->threadgroup_x_len = workgroup_size_x; | |||||
| this->threadgroup_y_len = workgroup_size_y; | |||||
| this->threadgroup_z_len = workgroup_size_z; | |||||
| } | |||||
| }; | |||||
| /* #MTLShaderBuilder source wrapper used during initial compilation. */ | /* #MTLShaderBuilder source wrapper used during initial compilation. */ | ||||
| struct MTLShaderBuilder { | struct MTLShaderBuilder { | ||||
| NSString *msl_source_vert_ = @""; | NSString *msl_source_vert_ = @""; | ||||
| NSString *msl_source_frag_ = @""; | NSString *msl_source_frag_ = @""; | ||||
| NSString *msl_source_compute_ = @""; | |||||
| /* Generated GLSL source used during compilation. */ | /* Generated GLSL source used during compilation. */ | ||||
| std::string glsl_vertex_source_ = ""; | std::string glsl_vertex_source_ = ""; | ||||
| std::string glsl_fragment_source_ = ""; | std::string glsl_fragment_source_ = ""; | ||||
| std::string glsl_compute_source_ = ""; | |||||
| /* Indicates whether source code has been provided via MSL directly. */ | /* Indicates whether source code has been provided via MSL directly. */ | ||||
| bool source_from_msl_ = false; | bool source_from_msl_ = false; | ||||
| }; | }; | ||||
| /** | /** | ||||
| * #MTLShader implements shader compilation, Pipeline State Object (PSO) | * #MTLShader implements shader compilation, Pipeline State Object (PSO) | ||||
| * creation for rendering and uniform data binding. | * creation for rendering and uniform data binding. | ||||
| Show All 31 Lines | private: | ||||
| bool transform_feedback_active_ = false; | bool transform_feedback_active_ = false; | ||||
| /* Vertex buffer to write transform feedback data into. */ | /* Vertex buffer to write transform feedback data into. */ | ||||
| GPUVertBuf *transform_feedback_vertbuf_ = nullptr; | GPUVertBuf *transform_feedback_vertbuf_ = nullptr; | ||||
| /** Shader source code. */ | /** Shader source code. */ | ||||
| MTLShaderBuilder *shd_builder_ = nullptr; | MTLShaderBuilder *shd_builder_ = nullptr; | ||||
| NSString *vertex_function_name_ = @""; | NSString *vertex_function_name_ = @""; | ||||
| NSString *fragment_function_name_ = @""; | NSString *fragment_function_name_ = @""; | ||||
| NSString *compute_function_name_ = @""; | |||||
| /** Compiled shader resources. */ | /** Compiled shader resources. */ | ||||
| id<MTLLibrary> shader_library_vert_ = nil; | id<MTLLibrary> shader_library_vert_ = nil; | ||||
| id<MTLLibrary> shader_library_frag_ = nil; | id<MTLLibrary> shader_library_frag_ = nil; | ||||
| id<MTLLibrary> shader_library_compute_ = nil; | |||||
| bool valid_ = false; | bool valid_ = false; | ||||
| /** Render pipeline state and PSO caching. */ | /** Render pipeline state and PSO caching. */ | ||||
| /* Metal API Descriptor used for creation of unique PSOs based on rendering state. */ | /* Metal API Descriptor used for creation of unique PSOs based on rendering state. */ | ||||
| MTLRenderPipelineDescriptor *pso_descriptor_ = nil; | MTLRenderPipelineDescriptor *pso_descriptor_ = nil; | ||||
| /* Metal backend struct containing all high-level pipeline state parameters | /* Metal backend struct containing all high-level pipeline state parameters | ||||
| * which contribute to instantiation of a unique PSO. */ | * which contribute to instantiation of a unique PSO. */ | ||||
| MTLRenderPipelineStateDescriptor current_pipeline_state_; | MTLRenderPipelineStateDescriptor current_pipeline_state_; | ||||
| /* Cache of compiled PipelineStateObjects. */ | /* Cache of compiled PipelineStateObjects. */ | ||||
| blender::Map<MTLRenderPipelineStateDescriptor, MTLRenderPipelineStateInstance *> pso_cache_; | blender::Map<MTLRenderPipelineStateDescriptor, MTLRenderPipelineStateInstance *> pso_cache_; | ||||
| /** Compute pipeline state and Compute PSO caching. */ | |||||
| MTLComputePipelineStateInstance compute_pso_instance_; | |||||
| /* True to enable multi-layered rendering support. */ | /* True to enable multi-layered rendering support. */ | ||||
| bool uses_mtl_array_index_ = false; | bool uses_mtl_array_index_ = false; | ||||
| /** SSBO Vertex fetch pragma options. */ | /** SSBO Vertex fetch pragma options. */ | ||||
| /* Indicates whether to pass in VertexBuffer's as regular buffer bindings | /* Indicates whether to pass in VertexBuffer's as regular buffer bindings | ||||
| * and perform vertex assembly manually, rather than using Stage-in. | * and perform vertex assembly manually, rather than using Stage-in. | ||||
| * This is used to give a vertex shader full access to all of the | * This is used to give a vertex shader full access to all of the | ||||
| * vertex data. | * vertex data. | ||||
| ▲ Show 20 Lines • Show All 47 Lines • ▼ Show 20 Lines | public: | ||||
| /* Assign GLSL source. */ | /* Assign GLSL source. */ | ||||
| void vertex_shader_from_glsl(MutableSpan<const char *> sources) override; | void vertex_shader_from_glsl(MutableSpan<const char *> sources) override; | ||||
| void geometry_shader_from_glsl(MutableSpan<const char *> sources) override; | void geometry_shader_from_glsl(MutableSpan<const char *> sources) override; | ||||
| void fragment_shader_from_glsl(MutableSpan<const char *> sources) override; | void fragment_shader_from_glsl(MutableSpan<const char *> sources) override; | ||||
| void compute_shader_from_glsl(MutableSpan<const char *> sources) override; | void compute_shader_from_glsl(MutableSpan<const char *> sources) override; | ||||
| /* Compile and build - Return true if successful. */ | /* Compile and build - Return true if successful. */ | ||||
| bool finalize(const shader::ShaderCreateInfo *info = nullptr) override; | bool finalize(const shader::ShaderCreateInfo *info = nullptr) override; | ||||
| bool finalize_compute(const shader::ShaderCreateInfo *info); | |||||
| /* Utility. */ | /* Utility. */ | ||||
| bool is_valid() | bool is_valid() | ||||
| { | { | ||||
| return valid_; | return valid_; | ||||
| } | } | ||||
| MTLRenderPipelineStateDescriptor &get_current_pipeline_state() | MTLRenderPipelineStateDescriptor &get_current_pipeline_state() | ||||
| { | { | ||||
| ▲ Show 20 Lines • Show All 54 Lines • ▼ Show 20 Lines | public: | ||||
| /* SSBO Vertex Bindings Utility functions. */ | /* SSBO Vertex Bindings Utility functions. */ | ||||
| void ssbo_vertex_fetch_bind_attributes_begin(); | void ssbo_vertex_fetch_bind_attributes_begin(); | ||||
| void ssbo_vertex_fetch_bind_attribute(const MTLSSBOAttribute &ssbo_attr); | void ssbo_vertex_fetch_bind_attribute(const MTLSSBOAttribute &ssbo_attr); | ||||
| void ssbo_vertex_fetch_bind_attributes_end(id<MTLRenderCommandEncoder> active_encoder); | void ssbo_vertex_fetch_bind_attributes_end(id<MTLRenderCommandEncoder> active_encoder); | ||||
| /* Metal shader properties and source mapping. */ | /* Metal shader properties and source mapping. */ | ||||
| void set_vertex_function_name(NSString *vetex_function_name); | void set_vertex_function_name(NSString *vetex_function_name); | ||||
| void set_fragment_function_name(NSString *fragment_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_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); | void set_interface(MTLShaderInterface *interface); | ||||
| MTLRenderPipelineStateInstance *bake_current_pipeline_state(MTLContext *ctx, | MTLRenderPipelineStateInstance *bake_current_pipeline_state(MTLContext *ctx, | ||||
| MTLPrimitiveTopologyClass prim_type); | MTLPrimitiveTopologyClass prim_type); | ||||
| bool bake_compute_pipeline_state(MTLContext *ctx); | |||||
| const MTLComputePipelineStateInstance &get_compute_pipeline_state(); | |||||
| /* Transform Feedback. */ | /* Transform Feedback. */ | ||||
| GPUVertBuf *get_transform_feedback_active_buffer(); | GPUVertBuf *get_transform_feedback_active_buffer(); | ||||
| bool has_transform_feedback_varying(std::string str); | bool has_transform_feedback_varying(std::string str); | ||||
| private: | private: | ||||
| /* Generate MSL shader from GLSL source. */ | /* Generate MSL shader from GLSL source. */ | ||||
| bool generate_msl_from_glsl(const shader::ShaderCreateInfo *info); | bool generate_msl_from_glsl(const shader::ShaderCreateInfo *info); | ||||
| bool generate_msl_from_glsl_compute(const shader::ShaderCreateInfo *info); | |||||
| MEM_CXX_CLASS_ALLOC_FUNCS("MTLShader"); | MEM_CXX_CLASS_ALLOC_FUNCS("MTLShader"); | ||||
| }; | }; | ||||
| /* Vertex format conversion. | /* Vertex format conversion. | ||||
| * Determines whether it is possible to resize a vertex attribute type | * Determines whether it is possible to resize a vertex attribute type | ||||
| * during input assembly. A conversion is implied by the difference | * during input assembly. A conversion is implied by the difference | ||||
| * between the input vertex descriptor (from MTLBatch/MTLImmediate) | * between the input vertex descriptor (from MTLBatch/MTLImmediate) | ||||
| ▲ Show 20 Lines • Show All 854 Lines • Show Last 20 Lines | |||||