|
| 1 | +# Mesh Shader Extensions |
| 2 | + |
| 3 | +🧪Experimental🧪 |
| 4 | + |
| 5 | +`wgpu` supports an experimental version of mesh shading. The extensions allow for acceleration structures to be created and built (with |
| 6 | +`Features::EXPERIMENTAL_MESH_SHADER` enabled) and interacted with in shaders. Currently `naga` has no support for mesh shaders beyond recognizing the additional shader stages. |
| 7 | +For this reason, all shaders must be created with `Device::create_shader_module_passthrough`. |
| 8 | + |
| 9 | +**Note**: The features documented here may have major bugs in them and are expected to be subject |
| 10 | +to breaking changes, suggestions for the API exposed by this should be posted on [the mesh-shading issue](https://github.com/gfx-rs/wgpu/issues/7197). |
| 11 | + |
| 12 | +***This is not*** a thorough explanation of mesh shading and how it works. Those wishing to understand mesh shading more broadly should look elsewhere first. |
| 13 | + |
| 14 | +## `wgpu` API |
| 15 | + |
| 16 | +### New `wgpu` functions |
| 17 | + |
| 18 | +`Device::create_mesh_pipeline` - Creates a mesh shader pipeline. This is very similar to creating a standard render pipeline, except that it takes a mesh shader state and optional task shader state instead of a vertex state. If the task state is omitted, during rendering the number of workgroups is passed directly from the draw call to the mesh shader state, with an empty payload. |
| 19 | + |
| 20 | +`RenderPass::draw_mesh_tasks` - Dispatches the mesh shader pipeline. This ignores render pipeline specific information, such as vertex buffer bindings and index buffer bindings. The dispatch size must adhere to the limits described below. |
| 21 | + |
| 22 | +`RenderPass::draw_mesh_tasks_indirect`, `RenderPass::multi_draw_mesh_tasks_indirect` and `RenderPass::multi_draw_mesh_tasks_indirect_count` - Dispatches the mesh shader pipeline with dispatch size taken from a buffer. This ignores render pipeline specific information, such as vertex buffer bindings and index buffer bindings. The dispatch size must adhere to the limits described below. Analogous to `draw_indirect`, `multi_draw_indirect` and `multi_draw_indirect_count`. Requires the corresponding indirect feature to be enabled. |
| 23 | + |
| 24 | +An example of using mesh shaders to render a single triangle can be seen [here](../../examples/features/src/mesh_shader). |
| 25 | + |
| 26 | +### Features |
| 27 | +* Using mesh shaders requires enabling `Features::EXPERIMENTAL_MESH_SHADER`. |
| 28 | +* Using mesh shaders with multiview requires enabling `Features::EXPERIMENTAL_MESH_SHADER_MULTIVIEW`. |
| 29 | +* Currently, only triangle rendering is tested |
| 30 | +* Line rendering is supported but untested |
| 31 | +* Point rendering is supported on vulkan. It is impossible on DirectX. Metal support hasn't been checked. |
| 32 | +* Queries are unsupported |
| 33 | + |
| 34 | +### Limits |
| 35 | + |
| 36 | +> **NOTE**: More limits will be added when support is added to `naga`. |
| 37 | +
|
| 38 | +* `Limits::max_task_workgroup_total_count` - the maximum total number of workgroups from a `draw_mesh_tasks` command or similar. The dimensions passed must be less than or equal to this limit when multiplied together. |
| 39 | +* `Limits::max_task_workgroups_per_dimension` - the maximum for each of the 3 workgroup dimensions in a `draw_mesh_tasks` command. Each dimension passed must be less than or equal to this limit. |
| 40 | +* `max_mesh_multiview_count` - The maximum number of views used when multiview rendering with a mesh shader pipeline. |
| 41 | +* `max_mesh_output_layers` - the maximum number of output layers for a mesh shader pipeline. |
| 42 | + |
| 43 | +### Backend specific information |
| 44 | +* Only Vulkan is currently supported. |
| 45 | +* DirectX 12 doesn't support point rendering. |
| 46 | +* DirectX 12 support is planned. |
| 47 | +* Metal support is desired but not currently planned. |
| 48 | + |
| 49 | + |
| 50 | +## Naga implementation |
| 51 | + |
| 52 | + |
| 53 | +### Supported frontends |
| 54 | +* 🛠️ WGSL |
| 55 | +* ❌ SPIR-V |
| 56 | +* 🚫 GLSL |
| 57 | + |
| 58 | +### Supported backends |
| 59 | +* 🛠️ SPIR-V |
| 60 | +* ❌ HLSL |
| 61 | +* ❌ MSL |
| 62 | +* 🚫 GLSL |
| 63 | +* 🚫 WGSL |
| 64 | + |
| 65 | +✔️ = Complete |
| 66 | +🛠️ = In progress |
| 67 | +❌ = Planned |
| 68 | +🚫 = Unplanned/impossible |
| 69 | + |
| 70 | +## `WGSL` extension specification |
| 71 | + |
| 72 | +The majority of changes relating to mesh shaders will be in WGSL and `naga`. |
| 73 | + |
| 74 | +Using any of these features in a `wgsl` program will require adding the `enable mesh_shading` directive to the top of a program. |
| 75 | + |
| 76 | +Two new shader stages will be added to `WGSL`. Fragment shaders are also modified slightly. Both task shaders and mesh shaders are allowed to use any compute-specific functionality, such as subgroup operations. |
| 77 | + |
| 78 | +### Task shader |
| 79 | +This shader stage can be selected by marking a function with `@task`. Task shaders must return a `vec3<u32>` as their output type. Similar to compute shaders, task shaders run in a workgroup. The output must be uniform across all threads in a workgroup. |
| 80 | + |
| 81 | +The output of this determines how many workgroups of mesh shaders will be dispatched. Once dispatched, global id variables will be local to the task shader workgroup dispatch, and mesh shaders won't know the position of their dispatch among all mesh shader dispatches unless this is passed through the payload. The output may be zero to skip dispatching any mesh shader workgroups for the task shader workgroup. |
| 82 | + |
| 83 | +If task shaders are marked with `@payload(someVar)`, where `someVar` is global variable declared like `var<workgroup> someVar: <type>`, task shaders may write to `someVar`. This payload is passed to the mesh shader workgroup that is invoked. The mesh shader can skip declaring `@payload` to ignore this input. |
| 84 | + |
| 85 | +### Mesh shader |
| 86 | +This shader stage can be selected by marking a function with `@mesh`. Mesh shaders must not return anything. |
| 87 | + |
| 88 | +Mesh shaders can be marked with `@payload(someVar)` similar to task shaders. Unlike task shaders, mesh shaders cannot write to this workgroup memory. Declaring `@payload` in a pipeline with no task shader, in a pipeline with a task shader that doesn't declare `@payload`, or in a task shader with an `@payload` that is statically sized and smaller than the mesh shader payload is illegal. |
| 89 | + |
| 90 | +Mesh shaders must be marked with `@vertex_output(OutputType, numOutputs)`, where `numOutputs` is the maximum number of vertices to be output by a mesh shader, and `OutputType` is the data associated with vertices, similar to a standard vertex shader output. |
| 91 | + |
| 92 | +Mesh shaders must also be marked with `@primitive_output(OutputType, numOutputs)`, which is similar to `@vertex_output` except it describes the primitive outputs. |
| 93 | + |
| 94 | +### Mesh shader outputs |
| 95 | + |
| 96 | +Primitive outputs from mesh shaders have some additional builtins they can set. These include `@builtin(cull_primitive)`, which must be a boolean value. If this is set to true, then the primitive is skipped during rendering. |
| 97 | + |
| 98 | +Mesh shader primitive outputs must also specify exactly one of `@builtin(triangle_indices)`, `@builtin(line_indices)`, or `@builtin(point_index)`. This determines the output topology of the mesh shader, and must match the output topology of the pipeline descriptor the mesh shader is used with. These must be of type `vec3<u32>`, `vec2<u32>`, and `u32` respectively. When setting this, each of the indices must be less than the number of vertices declared in `setMeshOutputs`. |
| 99 | + |
| 100 | +Additionally, the `@location` attributes from the vertex and primitive outputs can't overlap. |
| 101 | + |
| 102 | +Before setting any vertices or indices, or exiting, the mesh shader must call `setMeshOutputs(numVertices: u32, numIndices: u32)`, which declares the number of vertices and indices that will be written to. These must be less than the corresponding maximums set in `@vertex_output` and `@primitive_output`. The mesh shader must then write to exactly these numbers of vertices and primitives. |
| 103 | + |
| 104 | +The mesh shader can write to vertices using the `setVertex(idx: u32, vertex: VertexOutput)` where `VertexOutput` is replaced with the vertex type declared in `@vertex_output`, and `idx` is the index of the vertex to write. Similarly, the mesh shader can write to vertices using `setPrimitive(idx: u32, primitive: PrimitiveOutput)`. These can be written to multiple times, however unsynchronized writes are undefined behavior. The primitives and indices are shared across the entire mesh shader workgroup. |
| 105 | + |
| 106 | +### Fragment shader |
| 107 | + |
| 108 | +Fragment shaders may now be passed the primitive info from a mesh shader the same was as they are passed vertex inputs, for example `fn fs_main(vertex: VertexOutput, primitive: PrimitiveOutput)`. The primitive state is part of the fragment input and must match the output of the mesh shader in the pipeline. |
| 109 | + |
| 110 | +### Full example |
| 111 | + |
| 112 | +The following is a full example of WGSL shaders that could be used to create a mesh shader pipeline, showing off many of the features. |
| 113 | + |
| 114 | +```wgsl |
| 115 | +enable mesh_shading; |
| 116 | +
|
| 117 | +const positions = array( |
| 118 | + vec4(0.,-1.,0.,1.), |
| 119 | + vec4(-1.,1.,0.,1.), |
| 120 | + vec4(1.,1.,0.,1.) |
| 121 | +); |
| 122 | +const colors = array( |
| 123 | + vec4(0.,1.,0.,1.), |
| 124 | + vec4(0.,0.,1.,1.), |
| 125 | + vec4(1.,0.,0.,1.) |
| 126 | +); |
| 127 | +struct TaskPayload { |
| 128 | + colorMask: vec4<f32>, |
| 129 | + visible: bool, |
| 130 | +} |
| 131 | +var<workgroup> taskPayload: TaskPayload; |
| 132 | +var<workgroup> workgroupData: f32; |
| 133 | +struct VertexOutput { |
| 134 | + @builtin(position) position: vec4<f32>, |
| 135 | + @location(0) color: vec4<f32>, |
| 136 | +} |
| 137 | +struct PrimitiveOutput { |
| 138 | + @builtin(triangle_indices) index: vec3<u32>, |
| 139 | + @builtin(cull_primitive) cull: bool, |
| 140 | + @location(1) colorMask: vec4<f32>, |
| 141 | +} |
| 142 | +struct PrimitiveInput { |
| 143 | + @location(1) colorMask: vec4<f32>, |
| 144 | +} |
| 145 | +fn test_function(input: u32) { |
| 146 | +
|
| 147 | +} |
| 148 | +@task |
| 149 | +@payload(taskPayload) |
| 150 | +@workgroup_size(1) |
| 151 | +fn ts_main() -> @builtin(mesh_task_size) vec3<u32> { |
| 152 | + workgroupData = 1.0; |
| 153 | + taskPayload.colorMask = vec4(1.0, 1.0, 0.0, 1.0); |
| 154 | + taskPayload.visible = true; |
| 155 | + return vec3(3, 1, 1); |
| 156 | +} |
| 157 | +@mesh |
| 158 | +@payload(taskPayload) |
| 159 | +@vertex_output(VertexOutput, 3) @primitive_output(PrimitiveOutput, 1) |
| 160 | +@workgroup_size(1) |
| 161 | +fn ms_main(@builtin(local_invocation_index) index: u32, @builtin(global_invocation_id) id: vec3<u32>) { |
| 162 | + setMeshOutputs(3, 1); |
| 163 | + workgroupData = 2.0; |
| 164 | + var v: VertexOutput; |
| 165 | +
|
| 166 | + test_function(1); |
| 167 | +
|
| 168 | + v.position = positions[0]; |
| 169 | + v.color = colors[0] * taskPayload.colorMask; |
| 170 | + setVertex(0, v); |
| 171 | +
|
| 172 | + v.position = positions[1]; |
| 173 | + v.color = colors[1] * taskPayload.colorMask; |
| 174 | + setVertex(1, v); |
| 175 | +
|
| 176 | + v.position = positions[2]; |
| 177 | + v.color = colors[2] * taskPayload.colorMask; |
| 178 | + setVertex(2, v); |
| 179 | +
|
| 180 | + var p: PrimitiveOutput; |
| 181 | + p.index = vec3<u32>(0, 1, 2); |
| 182 | + p.cull = !taskPayload.visible; |
| 183 | + p.colorMask = vec4<f32>(1.0, 0.0, 1.0, 1.0); |
| 184 | + setPrimitive(0, p); |
| 185 | +} |
| 186 | +@fragment |
| 187 | +fn fs_main(vertex: VertexOutput, primitive: PrimitiveInput) -> @location(0) vec4<f32> { |
| 188 | + return vertex.color * primitive.colorMask; |
| 189 | +} |
| 190 | +``` |
0 commit comments