Changeset View
Changeset View
Standalone View
Standalone View
source/blender/gpu/metal/mtl_shader_generator.hh
| Show First 20 Lines • Show All 349 Lines • ▼ Show 20 Lines | struct MSLFragmentOutputAttribute { | ||||
| bool operator==(const MSLFragmentOutputAttribute &right) const | bool operator==(const MSLFragmentOutputAttribute &right) const | ||||
| { | { | ||||
| return (layout_location == right.layout_location && type == right.type && name == right.name && | return (layout_location == right.layout_location && type == right.type && name == right.name && | ||||
| layout_index == right.layout_index); | layout_index == right.layout_index); | ||||
| } | } | ||||
| }; | }; | ||||
| struct MSLSharedMemoryBlock { | |||||
| /* e.g. shared vec4 color_cache[cache_size][cache_size]; */ | |||||
| std::string type_name; | |||||
| std::string varname; | |||||
| bool is_array; | |||||
| std::string array_decl; /* String containing array declaration. e.g. [cache_size][cache_size]*/ | |||||
| }; | |||||
| class MSLGeneratorInterface { | class MSLGeneratorInterface { | ||||
| static char *msl_patch_default; | static char *msl_patch_default; | ||||
| public: | public: | ||||
| /** Shader stage input/output binding information. | /** Shader stage input/output binding information. | ||||
| * Derived from shader source reflection or GPUShaderCreateInfo. */ | * Derived from shader source reflection or GPUShaderCreateInfo. */ | ||||
| blender::Vector<MSLUniformBlock> uniform_blocks; | blender::Vector<MSLUniformBlock> uniform_blocks; | ||||
| blender::Vector<MSLUniform> uniforms; | blender::Vector<MSLUniform> uniforms; | ||||
| blender::Vector<MSLTextureSampler> texture_samplers; | blender::Vector<MSLTextureSampler> texture_samplers; | ||||
| blender::Vector<MSLVertexInputAttribute> vertex_input_attributes; | blender::Vector<MSLVertexInputAttribute> vertex_input_attributes; | ||||
| blender::Vector<MSLVertexOutputAttribute> vertex_output_varyings; | blender::Vector<MSLVertexOutputAttribute> vertex_output_varyings; | ||||
| /* Should match vertex outputs, but defined separately as | /* Should match vertex outputs, but defined separately as | ||||
| * some shader permutations will not utilize all inputs/outputs. | * some shader permutations will not utilize all inputs/outputs. | ||||
| * Final shader uses the intersection between the two sets. */ | * Final shader uses the intersection between the two sets. */ | ||||
| blender::Vector<MSLVertexOutputAttribute> fragment_input_varyings; | blender::Vector<MSLVertexOutputAttribute> fragment_input_varyings; | ||||
| blender::Vector<MSLFragmentOutputAttribute> fragment_outputs; | blender::Vector<MSLFragmentOutputAttribute> fragment_outputs; | ||||
| /* Transform feedback interface. */ | /* Transform feedback interface. */ | ||||
| blender::Vector<MSLVertexOutputAttribute> vertex_output_varyings_tf; | blender::Vector<MSLVertexOutputAttribute> vertex_output_varyings_tf; | ||||
| /* Clip Distances. */ | /* Clip Distances. */ | ||||
| blender::Vector<std::string> clip_distances; | blender::Vector<std::string> clip_distances; | ||||
| /* Shared Memory Blocks. */ | |||||
| blender::Vector<MSLSharedMemoryBlock> shared_memory_blocks; | |||||
| /** GL Global usage. */ | /** GL Global usage. */ | ||||
| /* Whether GL position is used, or an alternative vertex output should be the default. */ | /* Whether GL position is used, or an alternative vertex output should be the default. */ | ||||
| bool uses_gl_Position; | bool uses_gl_Position; | ||||
| /* Whether gl_FragColor is used, or whether an alternative fragment output | /* Whether gl_FragColor is used, or whether an alternative fragment output | ||||
| * should be the default. */ | * should be the default. */ | ||||
| bool uses_gl_FragColor; | bool uses_gl_FragColor; | ||||
| /* Whether gl_PointCoord is used in the fragment shader. If so, | /* Whether gl_PointCoord is used in the fragment shader. If so, | ||||
| * we define float2 gl_PointCoord [[point_coord]]. */ | * we define float2 gl_PointCoord [[point_coord]]. */ | ||||
| bool uses_gl_PointCoord; | bool uses_gl_PointCoord; | ||||
| /* Writes out to gl_PointSize in the vertex shader output. */ | /* Writes out to gl_PointSize in the vertex shader output. */ | ||||
| bool uses_gl_PointSize; | bool uses_gl_PointSize; | ||||
| bool uses_gl_VertexID; | bool uses_gl_VertexID; | ||||
| bool uses_gl_InstanceID; | bool uses_gl_InstanceID; | ||||
| bool uses_gl_BaseInstanceARB; | bool uses_gl_BaseInstanceARB; | ||||
| bool uses_gl_FrontFacing; | bool uses_gl_FrontFacing; | ||||
| bool uses_gl_PrimitiveID; | bool uses_gl_PrimitiveID; | ||||
| /* Sets the output render target array index when using multilayered rendering. */ | /* Sets the output render target array index when using multilayered rendering. */ | ||||
| bool uses_gl_FragDepth; | bool uses_gl_FragDepth; | ||||
| bool uses_mtl_array_index_; | bool uses_mtl_array_index_; | ||||
| bool uses_transform_feedback; | bool uses_transform_feedback; | ||||
| bool uses_barycentrics; | bool uses_barycentrics; | ||||
| /* Compute shader global variables. */ | |||||
| bool uses_gl_GlobalInvocationID; | |||||
| bool uses_gl_WorkGroupSize; | |||||
| bool uses_gl_WorkGroupID; | |||||
| bool uses_gl_NumWorkGroups; | |||||
| bool uses_gl_LocalInvocationIndex; | |||||
| bool uses_gl_LocalInvocationID; | |||||
| /* Parameters. */ | /* Parameters. */ | ||||
| shader::DepthWrite depth_write; | shader::DepthWrite depth_write; | ||||
| /* Shader buffer bind indices for argument buffers. */ | /* Shader buffer bind indices for argument buffers per shader stage. | ||||
| int sampler_argument_buffer_bind_index[2] = {-1, -1}; | * NOTE: Compute stage will re-use index 0. */ | ||||
| int sampler_argument_buffer_bind_index[3] = {-1, -1, -1}; | |||||
| /*** SSBO Vertex fetch mode. ***/ | /*** SSBO Vertex fetch mode. ***/ | ||||
| /* Indicates whether to pass in Vertex Buffer's as a regular buffers instead of using vertex | /* Indicates whether to pass in Vertex Buffer's as a regular buffers instead of using vertex | ||||
| * assembly in the PSO descriptor. Enabled with special pragma. */ | * assembly in the PSO descriptor. Enabled with special pragma. */ | ||||
| bool uses_ssbo_vertex_fetch_mode; | bool uses_ssbo_vertex_fetch_mode; | ||||
| private: | private: | ||||
| /* Parent shader instance. */ | /* Parent shader instance. */ | ||||
| Show All 34 Lines | public: | ||||
| /* Code generation utility functions. */ | /* Code generation utility functions. */ | ||||
| std::string generate_msl_uniform_structs(ShaderStage shader_stage); | std::string generate_msl_uniform_structs(ShaderStage shader_stage); | ||||
| std::string generate_msl_vertex_in_struct(); | std::string generate_msl_vertex_in_struct(); | ||||
| std::string generate_msl_vertex_out_struct(ShaderStage shader_stage); | std::string generate_msl_vertex_out_struct(ShaderStage shader_stage); | ||||
| std::string generate_msl_vertex_transform_feedback_out_struct(ShaderStage shader_stage); | std::string generate_msl_vertex_transform_feedback_out_struct(ShaderStage shader_stage); | ||||
| std::string generate_msl_fragment_out_struct(); | std::string generate_msl_fragment_out_struct(); | ||||
| std::string generate_msl_vertex_inputs_string(); | std::string generate_msl_vertex_inputs_string(); | ||||
| std::string generate_msl_fragment_inputs_string(); | std::string generate_msl_fragment_inputs_string(); | ||||
| std::string generate_msl_compute_inputs_string(); | |||||
| std::string generate_msl_vertex_entry_stub(); | std::string generate_msl_vertex_entry_stub(); | ||||
| std::string generate_msl_fragment_entry_stub(); | std::string generate_msl_fragment_entry_stub(); | ||||
| std::string generate_msl_compute_entry_stub(); | |||||
| std::string generate_msl_global_uniform_population(ShaderStage stage); | std::string generate_msl_global_uniform_population(ShaderStage stage); | ||||
| std::string generate_ubo_block_macro_chain(MSLUniformBlock block); | std::string generate_ubo_block_macro_chain(MSLUniformBlock block); | ||||
| std::string generate_msl_uniform_block_population(ShaderStage stage); | std::string generate_msl_uniform_block_population(ShaderStage stage); | ||||
| std::string generate_msl_vertex_attribute_input_population(); | std::string generate_msl_vertex_attribute_input_population(); | ||||
| std::string generate_msl_vertex_output_population(); | std::string generate_msl_vertex_output_population(); | ||||
| std::string generate_msl_vertex_output_tf_population(); | std::string generate_msl_vertex_output_tf_population(); | ||||
| std::string generate_msl_fragment_input_population(); | std::string generate_msl_fragment_input_population(); | ||||
| std::string generate_msl_fragment_output_population(); | std::string generate_msl_fragment_output_population(); | ||||
| Show All 11 Lines | public: | ||||
| MTLShaderInterface *bake_shader_interface(const char *name); | MTLShaderInterface *bake_shader_interface(const char *name); | ||||
| /* Fetch combined shader source header. */ | /* Fetch combined shader source header. */ | ||||
| char *msl_patch_default_get(); | char *msl_patch_default_get(); | ||||
| MEM_CXX_CLASS_ALLOC_FUNCS("MSLGeneratorInterface"); | MEM_CXX_CLASS_ALLOC_FUNCS("MSLGeneratorInterface"); | ||||
| }; | }; | ||||
| inline std::string get_stage_class_name(ShaderStage stage) | inline const char *get_stage_class_name(ShaderStage stage) | ||||
| { | { | ||||
| switch (stage) { | switch (stage) { | ||||
| case ShaderStage::VERTEX: | case ShaderStage::VERTEX: | ||||
| return "MTLShaderVertexImpl"; | return "MTLShaderVertexImpl"; | ||||
| case ShaderStage::FRAGMENT: | case ShaderStage::FRAGMENT: | ||||
| return "MTLShaderFragmentImpl"; | return "MTLShaderFragmentImpl"; | ||||
| case ShaderStage::COMPUTE: | |||||
| return "MTLShaderComputeImpl"; | |||||
| default: | |||||
| BLI_assert_unreachable(); | |||||
| return ""; | |||||
| } | |||||
| return ""; | |||||
| } | |||||
| inline const char *get_shader_stage_instance_name(ShaderStage stage) | |||||
| { | |||||
| switch (stage) { | |||||
| case ShaderStage::VERTEX: | |||||
| return "vertex_shader_instance"; | |||||
| case ShaderStage::FRAGMENT: | |||||
| return "fragment_shader_instance"; | |||||
| case ShaderStage::COMPUTE: | |||||
| return "compute_shader_instance"; | |||||
| default: | default: | ||||
| BLI_assert_unreachable(); | BLI_assert_unreachable(); | ||||
| return ""; | return ""; | ||||
| } | } | ||||
| return ""; | return ""; | ||||
| } | } | ||||
| inline bool is_builtin_type(std::string type) | inline bool is_builtin_type(std::string type) | ||||
| ▲ Show 20 Lines • Show All 221 Lines • ▼ Show 20 Lines | switch (type) { | ||||
| case shader::Type::CHAR4: | case shader::Type::CHAR4: | ||||
| return "char4"; | return "char4"; | ||||
| default: | default: | ||||
| BLI_assert(false); | BLI_assert(false); | ||||
| return "unkown"; | return "unkown"; | ||||
| } | } | ||||
| } | } | ||||
| inline char *next_symbol_in_range(char *begin, char *end, char symbol) | |||||
| { | |||||
| for (char *a = begin; a < end; a++) { | |||||
| if (*a == symbol) { | |||||
| return a; | |||||
| } | |||||
| } | |||||
| return nullptr; | |||||
| } | |||||
| inline char *next_word_in_range(char *begin, char *end) | |||||
| { | |||||
| for (char *a = begin; a < end; a++) { | |||||
| char chr = *a; | |||||
| if ((chr >= 'a' && chr <= 'z') || (chr >= 'A' && chr <= 'Z') || (chr >= '0' && chr <= '9') || | |||||
| (chr == '_')) { | |||||
| return a; | |||||
| } | |||||
| } | |||||
| return nullptr; | |||||
| } | |||||
| } // namespace blender::gpu | } // namespace blender::gpu | ||||