1e5c31af7Sopenharmony_ci// Copyright 2015-2024 The Khronos Group Inc.
2e5c31af7Sopenharmony_ci//
3e5c31af7Sopenharmony_ci// SPDX-License-Identifier: CC-BY-4.0
4e5c31af7Sopenharmony_ci
5e5c31af7Sopenharmony_ci[[geometry]]
6e5c31af7Sopenharmony_ci= Geometry Shading
7e5c31af7Sopenharmony_ci
8e5c31af7Sopenharmony_ciThe geometry shader operates on a group of vertices and their associated
9e5c31af7Sopenharmony_cidata assembled from a single input primitive, and emits zero or more output
10e5c31af7Sopenharmony_ciprimitives and the group of vertices and their associated data required for
11e5c31af7Sopenharmony_cieach output primitive.
12e5c31af7Sopenharmony_ciGeometry shading is enabled when a geometry shader is included in the
13e5c31af7Sopenharmony_cipipeline.
14e5c31af7Sopenharmony_ci
15e5c31af7Sopenharmony_ci
16e5c31af7Sopenharmony_ci[[geometry-input]]
17e5c31af7Sopenharmony_ci== Geometry Shader Input Primitives
18e5c31af7Sopenharmony_ci
19e5c31af7Sopenharmony_ciEach geometry shader invocation has access to all vertices in the primitive
20e5c31af7Sopenharmony_ci(and their associated data), which are presented to the shader as an array
21e5c31af7Sopenharmony_ciof inputs.
22e5c31af7Sopenharmony_ci
23e5c31af7Sopenharmony_ciThe input primitive type expected by the geometry shader is specified with
24e5c31af7Sopenharmony_cian code:OpExecutionMode instruction in the geometry shader, and must: match
25e5c31af7Sopenharmony_cithe incoming primitive type specified by either the pipeline's
26e5c31af7Sopenharmony_ci<<drawing-primitive-topologies, primitive topology>> if tessellation is
27e5c31af7Sopenharmony_ciinactive, or the <<tessellation, tessellation mode>> if tessellation is
28e5c31af7Sopenharmony_ciactive, as follows:
29e5c31af7Sopenharmony_ci
30e5c31af7Sopenharmony_ci  * An input primitive type of code:InputPoints must: only be used with a
31e5c31af7Sopenharmony_ci    pipeline topology of ename:VK_PRIMITIVE_TOPOLOGY_POINT_LIST, or with a
32e5c31af7Sopenharmony_ci    tessellation shader specifying code:PointMode.
33e5c31af7Sopenharmony_ci    The input arrays always contain one element, as described by the
34e5c31af7Sopenharmony_ci    <<drawing-point-lists, point list topology>> or
35e5c31af7Sopenharmony_ci    <<tessellation-point-mode, tessellation in point mode>>.
36e5c31af7Sopenharmony_ci  * An input primitive type of code:InputLines must: only be used with a
37e5c31af7Sopenharmony_ci    pipeline topology of ename:VK_PRIMITIVE_TOPOLOGY_LINE_LIST or
38e5c31af7Sopenharmony_ci    ename:VK_PRIMITIVE_TOPOLOGY_LINE_STRIP, or with a tessellation shader
39e5c31af7Sopenharmony_ci    specifying code:IsoLines that does not specify code:PointMode.
40e5c31af7Sopenharmony_ci    The input arrays always contain two elements, as described by the
41e5c31af7Sopenharmony_ci    <<drawing-line-lists, line list topology>> or <<drawing-line-strips,
42e5c31af7Sopenharmony_ci    line strip topology>>, or by <<tessellation-isoline-tessellation,
43e5c31af7Sopenharmony_ci    isoline tessellation>>.
44e5c31af7Sopenharmony_ci  * An input primitive type of code:InputLinesAdjacency must: only be used
45e5c31af7Sopenharmony_ci    when tessellation is inactive, with a pipeline topology of
46e5c31af7Sopenharmony_ci    ename:VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY or
47e5c31af7Sopenharmony_ci    ename:VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY.
48e5c31af7Sopenharmony_ci    The input arrays always contain four elements, as described by the
49e5c31af7Sopenharmony_ci    <<drawing-line-lists-with-adjacency, line list with adjacency topology>>
50e5c31af7Sopenharmony_ci    or <<drawing-line-strips-with-adjacency, line strip with adjacency
51e5c31af7Sopenharmony_ci    topology>>.
52e5c31af7Sopenharmony_ci  * An input primitive type of code:Triangles must: only be used with a
53e5c31af7Sopenharmony_ci    pipeline topology of ename:VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
54e5c31af7Sopenharmony_ci    ename:VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP, or
55e5c31af7Sopenharmony_ci    ename:VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN; or with a tessellation shader
56e5c31af7Sopenharmony_ci    specifying code:Quads or code:Triangles that does not specify
57e5c31af7Sopenharmony_ci    code:PointMode.
58e5c31af7Sopenharmony_ci    The input arrays always contain three elements, as described by the
59e5c31af7Sopenharmony_ci    <<drawing-triangle-lists, triangle list topology>>,
60e5c31af7Sopenharmony_ci    <<drawing-triangle-strips, triangle strip topology>>, or
61e5c31af7Sopenharmony_ci    <<drawing-triangle-fans, triangle fan topology>>, or by
62e5c31af7Sopenharmony_ci    <<tessellation-triangle-tessellation, triangle>> or
63e5c31af7Sopenharmony_ci    <<tessellation-quad-tessellation, quad tessellation>>.
64e5c31af7Sopenharmony_ci    Vertices may: be in a different absolute order than specified by the
65e5c31af7Sopenharmony_ci    topology, but must: adhere to the specified winding order.
66e5c31af7Sopenharmony_ci  * An input primitive type of code:InputTrianglesAdjacency must: only be
67e5c31af7Sopenharmony_ci    used when tessellation is inactive, with a pipeline topology of
68e5c31af7Sopenharmony_ci    ename:VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY or
69e5c31af7Sopenharmony_ci    ename:VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY.
70e5c31af7Sopenharmony_ci    The input arrays always contain six elements, as described by the
71e5c31af7Sopenharmony_ci    <<drawing-triangle-lists-with-adjacency, triangle list with adjacency
72e5c31af7Sopenharmony_ci    topology>> or <<drawing-triangle-strips-with-adjacency, triangle strip
73e5c31af7Sopenharmony_ci    with adjacency topology>>.
74e5c31af7Sopenharmony_ci    Vertices may: be in a different absolute order than specified by the
75e5c31af7Sopenharmony_ci    topology, but must: adhere to the specified winding order, and the
76e5c31af7Sopenharmony_ci    vertices making up the main primitive must: still occur at the first,
77e5c31af7Sopenharmony_ci    third, and fifth index.
78e5c31af7Sopenharmony_ci
79e5c31af7Sopenharmony_ci
80e5c31af7Sopenharmony_ci[[geometry-output]]
81e5c31af7Sopenharmony_ci== Geometry Shader Output Primitives
82e5c31af7Sopenharmony_ci
83e5c31af7Sopenharmony_ciA geometry shader generates primitives in one of three output modes: points,
84e5c31af7Sopenharmony_ciline strips, or triangle strips.
85e5c31af7Sopenharmony_ciThe primitive mode is specified in the shader using an code:OpExecutionMode
86e5c31af7Sopenharmony_ciinstruction with the code:OutputPoints, code:OutputLineStrip or
87e5c31af7Sopenharmony_cicode:OutputTriangleStrip modes, respectively.
88e5c31af7Sopenharmony_ciEach geometry shader must: include exactly one output primitive mode.
89e5c31af7Sopenharmony_ci
90e5c31af7Sopenharmony_ciThe vertices output by the geometry shader are assembled into points, lines,
91e5c31af7Sopenharmony_cior triangles based on the output primitive type and the resulting primitives
92e5c31af7Sopenharmony_ciare then further processed as described in <<primsrast>>.
93e5c31af7Sopenharmony_ciIf the number of vertices emitted by the geometry shader is not sufficient
94e5c31af7Sopenharmony_cito produce a single primitive, vertices corresponding to incomplete
95e5c31af7Sopenharmony_ciprimitives are not processed by subsequent pipeline stages.
96e5c31af7Sopenharmony_ciThe number of vertices output by the geometry shader is limited to a maximum
97e5c31af7Sopenharmony_cicount specified in the shader.
98e5c31af7Sopenharmony_ci
99e5c31af7Sopenharmony_ciThe maximum output vertex count is specified in the shader using an
100e5c31af7Sopenharmony_cicode:OpExecutionMode instruction with the mode set to code:OutputVertices
101e5c31af7Sopenharmony_ciand the maximum number of vertices that will be produced by the geometry
102e5c31af7Sopenharmony_cishader specified as a literal.
103e5c31af7Sopenharmony_ciEach geometry shader must: specify a maximum output vertex count.
104e5c31af7Sopenharmony_ci
105e5c31af7Sopenharmony_ci
106e5c31af7Sopenharmony_ci[[geometry-invocations]]
107e5c31af7Sopenharmony_ci== Multiple Invocations of Geometry Shaders
108e5c31af7Sopenharmony_ci
109e5c31af7Sopenharmony_ciGeometry shaders can: be invoked more than one time for each input
110e5c31af7Sopenharmony_ciprimitive.
111e5c31af7Sopenharmony_ciThis is known as _geometry shader instancing_ and is requested by including
112e5c31af7Sopenharmony_cian code:OpExecutionMode instruction with code:mode specified as
113e5c31af7Sopenharmony_cicode:Invocations and the number of invocations specified as an integer
114e5c31af7Sopenharmony_ciliteral.
115e5c31af7Sopenharmony_ci
116e5c31af7Sopenharmony_ciIn this mode, the geometry shader will execute at least [eq]#n# times for
117e5c31af7Sopenharmony_cieach input primitive, where [eq]#n# is the number of invocations specified
118e5c31af7Sopenharmony_ciin the code:OpExecutionMode instruction.
119e5c31af7Sopenharmony_ciThe instance number is available to each invocation as a built-in input
120e5c31af7Sopenharmony_ciusing code:InvocationId.
121e5c31af7Sopenharmony_ci
122e5c31af7Sopenharmony_ci
123e5c31af7Sopenharmony_ci[[geometry-ordering]]
124e5c31af7Sopenharmony_ci== Geometry Shader Primitive Ordering
125e5c31af7Sopenharmony_ci
126e5c31af7Sopenharmony_ciLimited guarantees are provided for the relative ordering of primitives
127e5c31af7Sopenharmony_ciproduced by a geometry shader, as they pertain to <<drawing-primitive-order,
128e5c31af7Sopenharmony_ciprimitive order>>.
129e5c31af7Sopenharmony_ci
130e5c31af7Sopenharmony_ci  * For instanced geometry shaders, the output primitives generated from
131e5c31af7Sopenharmony_ci    each input primitive are passed to subsequent pipeline stages using the
132e5c31af7Sopenharmony_ci    invocation number to order the primitives, from least to greatest.
133e5c31af7Sopenharmony_ci  * All output primitives generated from a given input primitive are passed
134e5c31af7Sopenharmony_ci    to subsequent pipeline stages before any output primitives generated
135e5c31af7Sopenharmony_ci    from subsequent input primitives.
136e5c31af7Sopenharmony_ci
137e5c31af7Sopenharmony_ci
138e5c31af7Sopenharmony_ciifdef::VK_NV_geometry_shader_passthrough[]
139e5c31af7Sopenharmony_ci[[geometry-passthrough]]
140e5c31af7Sopenharmony_ci== Geometry Shader Passthrough
141e5c31af7Sopenharmony_ci
142e5c31af7Sopenharmony_ciA geometry shader that uses the code:PassthroughNV decoration on a variable
143e5c31af7Sopenharmony_ciin its input interface is considered a _passthrough geometry shader_.
144e5c31af7Sopenharmony_ciOutput primitives in a passthrough geometry shader must: have the same
145e5c31af7Sopenharmony_citopology as the input primitive and are not produced by emitting vertices.
146e5c31af7Sopenharmony_ciThe vertices of the output primitive have two different types of attributes,
147e5c31af7Sopenharmony_ciper-vertex and per-primitive.
148e5c31af7Sopenharmony_ciGeometry shader input variables with code:PassthroughNV decoration are
149e5c31af7Sopenharmony_ciconsidered to produce per-vertex outputs, where values for each output
150e5c31af7Sopenharmony_civertex are copied from the corresponding input vertex.
151e5c31af7Sopenharmony_ciAny built-in or user-defined geometry shader outputs are considered
152e5c31af7Sopenharmony_ciper-primitive in a passthrough geometry shader, where a single output value
153e5c31af7Sopenharmony_ciis copied to all output vertices.
154e5c31af7Sopenharmony_ci
155e5c31af7Sopenharmony_ciThe remainder of this section details the usage of the code:PassthroughNV
156e5c31af7Sopenharmony_cidecoration and modifications to the interface matching rules when using
157e5c31af7Sopenharmony_cipassthrough geometry shaders.
158e5c31af7Sopenharmony_ci
159e5c31af7Sopenharmony_ci
160e5c31af7Sopenharmony_ci[[geometry-passthrough-passthrough]]
161e5c31af7Sopenharmony_ci=== code:PassthroughNV Decoration
162e5c31af7Sopenharmony_ci
163e5c31af7Sopenharmony_ciDecorating a geometry shader input variable with the code:PassthroughNV
164e5c31af7Sopenharmony_cidecoration indicates that values of this input are copied through to the
165e5c31af7Sopenharmony_cicorresponding vertex of the output primitive.
166e5c31af7Sopenharmony_ciInput variables and block members which do not have the code:PassthroughNV
167e5c31af7Sopenharmony_cidecoration are consumed by the geometry shader without being passed through
168e5c31af7Sopenharmony_cito subsequent stages.
169e5c31af7Sopenharmony_ci
170e5c31af7Sopenharmony_ciThe code:PassthroughNV decoration must: only be used within a geometry
171e5c31af7Sopenharmony_cishader.
172e5c31af7Sopenharmony_ci
173e5c31af7Sopenharmony_ciAny variable decorated with code:PassthroughNV must: be declared using the
174e5c31af7Sopenharmony_cicode:Input storage class.
175e5c31af7Sopenharmony_ci
176e5c31af7Sopenharmony_ciThe code:PassthroughNV decoration must: not be used with any of:
177e5c31af7Sopenharmony_ci
178e5c31af7Sopenharmony_ci  * an input primitive type other than code:InputPoints, code:InputLines, or
179e5c31af7Sopenharmony_ci    code:Triangles, as specified by the mode for code:OpExecutionMode.
180e5c31af7Sopenharmony_ci  * an invocation count other than one, as specified by the code:Invocations
181e5c31af7Sopenharmony_ci    mode for code:OpExecutionMode.
182e5c31af7Sopenharmony_ci  * an code:OpEntryPoint which statically uses the code:OpEmitVertex or
183e5c31af7Sopenharmony_ci    code:OpEndPrimitive instructions.
184e5c31af7Sopenharmony_ci  * a variable decorated with the code:InvocationId built-in decoration.
185e5c31af7Sopenharmony_ci  * a variable decorated with the code:PrimitiveId built-in decoration that
186e5c31af7Sopenharmony_ci    is declared using the code:Input storage class.
187e5c31af7Sopenharmony_ci
188e5c31af7Sopenharmony_ci
189e5c31af7Sopenharmony_ci[[geometry-passthrough-interface]]
190e5c31af7Sopenharmony_ci=== Passthrough Interface Matching
191e5c31af7Sopenharmony_ci
192e5c31af7Sopenharmony_ciWhen a passthrough geometry shader is in use, the
193e5c31af7Sopenharmony_ci<<interfaces-iointerfaces-matching,Interface Matching>> rules involving the
194e5c31af7Sopenharmony_cigeometry shader input and output interfaces operate as described in this
195e5c31af7Sopenharmony_cisection.
196e5c31af7Sopenharmony_ci
197e5c31af7Sopenharmony_ciFor the purposes of matching passthrough geometry shader inputs with outputs
198e5c31af7Sopenharmony_ciof the previous pipeline stages, the code:PassthroughNV decoration is
199e5c31af7Sopenharmony_ciignored.
200e5c31af7Sopenharmony_ci
201e5c31af7Sopenharmony_ciFor the purposes of matching the outputs of the geometry shader with
202e5c31af7Sopenharmony_cisubsequent pipeline stages, each input variable with the code:PassthroughNV
203e5c31af7Sopenharmony_cidecoration is considered to add an equivalent output variable with the same
204e5c31af7Sopenharmony_citype, decoration (other than code:PassthroughNV), number, and declaration
205e5c31af7Sopenharmony_ciorder on the output interface.
206e5c31af7Sopenharmony_ciThe output variable declaration corresponding to an input variable decorated
207e5c31af7Sopenharmony_ciwith code:PassthroughNV will be identical to the input declaration, except
208e5c31af7Sopenharmony_cithat the outermost array dimension of such variables is removed.
209e5c31af7Sopenharmony_ciThe output block declaration corresponding to an input block decorated with
210e5c31af7Sopenharmony_cicode:PassthroughNV or having members decorated with code:PassthroughNV will
211e5c31af7Sopenharmony_cibe identical to the input declaration, except that the outermost array
212e5c31af7Sopenharmony_cidimension of such declaration is removed.
213e5c31af7Sopenharmony_ci
214e5c31af7Sopenharmony_ciIf an input block is decorated with code:PassthroughNV, the equivalent
215e5c31af7Sopenharmony_cioutput block contains all the members of the input block.
216e5c31af7Sopenharmony_ciOtherwise, the equivalent output block contains only those input block
217e5c31af7Sopenharmony_cimembers decorated with code:PassthroughNV.
218e5c31af7Sopenharmony_ciAll members of the corresponding output block are assigned code:Location and
219e5c31af7Sopenharmony_cicode:Component decorations identical to those assigned to the corresponding
220e5c31af7Sopenharmony_ciinput block members.
221e5c31af7Sopenharmony_ci
222e5c31af7Sopenharmony_ciOutput variables and blocks generated from inputs decorated with
223e5c31af7Sopenharmony_cicode:PassthroughNV will only exist for the purposes of interface matching;
224e5c31af7Sopenharmony_cithese declarations are not available to geometry shader code or listed in
225e5c31af7Sopenharmony_cithe module interface.
226e5c31af7Sopenharmony_ci
227e5c31af7Sopenharmony_ciFor the purposes of component counting, passthrough geometry shaders count
228e5c31af7Sopenharmony_ciall statically used input variable components declared with the
229e5c31af7Sopenharmony_cicode:PassthroughNV decoration as output components as well, since their
230e5c31af7Sopenharmony_civalues will be copied to the output primitive produced by the geometry
231e5c31af7Sopenharmony_cishader.
232e5c31af7Sopenharmony_ci
233e5c31af7Sopenharmony_ciendif::VK_NV_geometry_shader_passthrough[]
234e5c31af7Sopenharmony_ci
235e5c31af7Sopenharmony_ci
236