1// Copyright (c) 2018-2020 NVIDIA Corporation 2// 3// SPDX-License-Identifier: CC-BY-4.0 4 5[[mesh]] 6= Mesh Shading 7 8<<shaders-task,Task>> and <<shaders-mesh,mesh shaders>> operate in 9workgroups to produce a collection of primitives that will be processed by 10subsequent stages of the graphics pipeline. 11 12Work on the mesh pipeline is initiated by the application 13<<drawing-mesh-shading,drawing>> a set of mesh tasks organized in global 14workgroups. 15If the optional task shader is active, each workgroup triggers the execution 16of task shader invocations that will create a new set of mesh workgroups 17upon completion. 18Each of these created workgroups, or each of the original workgroups if no 19task shader is present, triggers the execution of mesh shader invocations. 20 21Each mesh shader workgroup emits zero or more output primitives along with 22the group of vertices and their associated data required for each output 23primitive. 24 25 26[[mesh-task-input]] 27== Task Shader Input 28For every workgroup issued via the drawing commands a group of task shader 29invocations is executed. 30There are no inputs other than the builtin workgroup identifiers. 31 32 33[[mesh-task-output]] 34== Task Shader Output 35The task shader can emit zero or more mesh workgroups to be generated using 36the <<interfaces-builtin-variables,built-in variable>> code:TaskCountNV. 37This value must: be less than or equal to 38sname:VkPhysicalDeviceMeshShaderPropertiesNV::pname:maxTaskOutputCount. 39 40It can also output user-defined data that is passed as input to all mesh 41shader invocations that the task creates. 42These outputs are decorated as code:PerTaskNV. 43 44 45[[mesh-generation]] 46== Mesh Generation 47If a task shader exists, the mesh assembler creates a variable amount of 48mesh workgroups depending on each task's output. 49If there is no task shader, the drawing commands emit the mesh shader 50invocations directly. 51 52 53[[mesh-input]] 54== Mesh Shader Input 55The only inputs available to the mesh shader are variables identifying the 56specific workgroup and invocation and, if applicable, any outputs written as 57code:PerTaskNV by the task shader that spawned the mesh shader's workgroup. 58The mesh shader can operate without a task shader as well. 59 60 61[[mesh-output]] 62== Mesh Shader Output Primitives 63 64A mesh shader generates primitives in one of three output modes: points, 65lines, or triangles. 66The primitive mode is specified in the shader using an code:OpExecutionMode 67instruction with the code:OutputPoints, code:OutputLinesNV, or 68code:OutputTrianglesNV modes, respectively. 69Each mesh shader must: include exactly one output primitive mode. 70 71The maximum output vertex count is specified as a literal in the shader 72using an code:OpExecutionMode instruction with the mode set to 73code:OutputVertices and must: be less than or equal to 74sname:VkPhysicalDeviceMeshShaderPropertiesNV::pname:maxMeshOutputVertices. 75 76The maximum output primitive count is specified as a literal in the shader 77using an code:OpExecutionMode instruction with the mode set to 78code:OutputPrimitivesNV and must: be less than or equal to 79sname:VkPhysicalDeviceMeshShaderPropertiesNV::pname:maxMeshOutputPrimitives. 80 81The number of primitives output by the mesh shader is provided via writing 82to the <<interfaces-builtin-variables,built-in variable>> 83code:PrimitiveCountNV and must: be less than or equal to the maximum output 84primitive count specified in the shader. 85A variable decorated with code:PrimitiveIndicesNV is an output array of 86local index values into the vertex output arrays from which primitives are 87assembled according to the output primitive type. 88These resulting primitives are then further processed as described in 89<<primsrast>>. 90 91 92[[mesh-output-perview]] 93== Mesh Shader Per-View Outputs 94 95The mesh shader outputs decorated with the code:PositionPerViewNV, 96code:ClipDistancePerViewNV, code:CullDistancePerViewNV, code:LayerPerViewNV, 97and code:ViewportMaskPerViewNV built-in decorations are the per-view 98versions of the single-view variables with equivalent names (that is 99code:Position, code:ClipDistance, code:CullDistance, code:Layer, and 100code:ViewportMaskNV, respectively). 101If a shader statically assigns a value to any element of a per-view array it 102must: not statically assign a value to the equivalent single-view variable. 103 104Each of these outputs is considered arrayed, with separate values for each 105view. 106The view number is used to index the first dimension of these arrays. 107 108The second dimension of the code:ClipDistancePerViewNV, and 109code:CullDistancePerViewNV arrays have the same requirements as the 110code:ClipDistance, and code:CullDistance arrays. 111 112If a mesh shader output is _per-view_, the corresponding fragment shader 113input is taken from the element of the per-view output array that 114corresponds to the view that is currently being processed by the fragment 115shader. 116 117 118[[mesh-ordering]] 119== Mesh Shader Primitive Ordering 120 121Following guarantees are provided for the relative ordering of primitives 122produced by a mesh shader, as they pertain to <<drawing-primitive-order, 123primitive order>>. 124 125 * When a task shader is used, mesh workgroups spawned from lower tasks 126 will be ordered prior those workgroups from subsequent tasks. 127 * All output primitives generated from a given mesh workgroup are passed 128 to subsequent pipeline stages before any output primitives generated 129 from subsequent input workgroups. 130 * All output primitives within a mesh workgroup, will be generated in the 131 ordering provided by the builtin primitive indexbuffer (from low address 132 to high address). 133