|
| 1 | +// Copyright 2025 The Khronos Group, Inc. |
| 2 | +// SPDX-License-Identifier: CC-BY-4.0 |
| 3 | + |
| 4 | +ifndef::chapters[:chapters:] |
| 5 | +ifndef::images[:images: images/] |
| 6 | + |
| 7 | +[[primitive-topology]] |
| 8 | += Primitive Topology |
| 9 | + |
| 10 | +When using a graphics pipeline there are 2 types of shaders, link:https://docs.vulkan.org/spec/latest/chapters/pipelines.html#pipelines-graphics-subsets-pre-rasterization[pre-rasterization shaders] and fragment shaders. |
| 11 | + |
| 12 | +[[pre-rasterization-stages]] |
| 13 | +== Pre-Rasterization stages |
| 14 | + |
| 15 | +The following are the various shader stages that can be used in pre-rasterization. |
| 16 | + |
| 17 | +image::{images}primitive_topology_stages.svg[primitive_topology_stages.svg] |
| 18 | + |
| 19 | +The main thing to take away here is the last pre-rasterization stage might be any of the following: |
| 20 | + |
| 21 | +- VK_SHADER_STAGE_VERTEX_BIT |
| 22 | +- VK_SHADER_STAGE_MESH_BIT_EXT |
| 23 | +- VK_SHADER_STAGE_GEOMETRY_BIT |
| 24 | +- VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT (must always be with a tessellation control stage) |
| 25 | + |
| 26 | +== Types of Primitive |
| 27 | + |
| 28 | +One of the goals of pre-rasterization is to get all the `primitives` ready for rasterization. The `primitive` is the smallest organized unit of vertices forming a basic geometric shape that's processed by the rasterizer. The `topology` of these primitives can be defined with the values in link:https://docs.vulkan.org/spec/latest/chapters/drawing.html#VkPrimitiveTopology[VkPrimitiveTopology]. |
| 29 | + |
| 30 | +The following shows a basic example how 6 vertices can be connected in different `VkPrimitiveTopology` |
| 31 | + |
| 32 | +image::{images}primitive_topology_example.svg[primitive_topology_example.svg] |
| 33 | + |
| 34 | +== Various Effective Topology |
| 35 | + |
| 36 | +It is possible for the multiple link:https://docs.vulkan.org/spec/latest/chapters/drawing.html#drawing-primitive-topology-class[Topology Class] to be used during the graphics pipeline. It is important to know where in the pipeline you are when discussing "topology". |
| 37 | + |
| 38 | +=== Vertex Input Assembly |
| 39 | + |
| 40 | +`VkPipelineInputAssemblyStateCreateInfo::topology` (or set dynamically with `vkCmdSetPrimitiveTopology`) is what is provided as an input for the vertex shader. |
| 41 | + |
| 42 | +When using mesh shaders, this value is ignored. |
| 43 | + |
| 44 | +[NOTE] |
| 45 | +==== |
| 46 | +If you want to set `VK_DYNAMIC_STATE_PRIMITIVE_TOPOLOGY` in your pipeline, make sure to be aware of the `dynamicPrimitiveTopologyUnrestricted` property. Some hardware can only let you dynamically adjust the input assembly primitive topology to be in the same link:https://docs.vulkan.org/spec/latest/chapters/drawing.html#drawing-primitive-topology-class[Topology Class] |
| 47 | +==== |
| 48 | + |
| 49 | +=== Shader Execution Mode |
| 50 | + |
| 51 | +Some stages have `Execution Mode` (SPIR-V term) that are defined in the shader code itself. This allows shader authors to determine the topology the shader will output. |
| 52 | + |
| 53 | +It is possible to have multiple stages such as tessellation and geometry together. In this case the effective topology is only the `Execution Mode` set by the last shader stage in the pipeline. |
| 54 | + |
| 55 | +==== Mesh output Execution Mode |
| 56 | + |
| 57 | +The mesh stage will set either `OutputPoints`, `OutputLinesEXT`, or `OutputTrianglesEXT` |
| 58 | + |
| 59 | +link:https://godbolt.org/z/jhhsoTfnT[Try Online] |
| 60 | + |
| 61 | +[source,glsl] |
| 62 | +---- |
| 63 | +#extension GL_EXT_mesh_shader : require |
| 64 | +
|
| 65 | +// Only 1 of the 3 is allowed |
| 66 | +layout(points) out; |
| 67 | +layout(lines) out; |
| 68 | +layout(triangles) out; |
| 69 | +---- |
| 70 | + |
| 71 | +==== Tessellation output Execution Mode |
| 72 | + |
| 73 | +The tessellation evaluation stage will set either `Triangles`, `Quads`, or `Isolines` |
| 74 | + |
| 75 | +link:https://godbolt.org/z/PbPT4WWrr[Try Online] |
| 76 | + |
| 77 | +[source,glsl] |
| 78 | +---- |
| 79 | +// Only 1 of the 3 is allowed |
| 80 | +layout(quads) in; |
| 81 | +layout(isolines) in; |
| 82 | +layout(triangles) in; |
| 83 | +---- |
| 84 | + |
| 85 | +==== Geometry output Execution Mode |
| 86 | + |
| 87 | +A geometry stage will set either `OutputPoints`, `OutputLineStrip`, or `OutputTriangleStrip` |
| 88 | + |
| 89 | +link:https://godbolt.org/z/K9nn98oGv[Try Online] |
| 90 | + |
| 91 | +[source,glsl] |
| 92 | +---- |
| 93 | +// Only 1 of the 3 is allowed |
| 94 | +layout(points) out; |
| 95 | +layout(line_strip) out; |
| 96 | +layout(triangle_strip) out; |
| 97 | +---- |
| 98 | + |
| 99 | +=== Polygon Mode |
| 100 | + |
| 101 | +Once you have your primitives created you can set the link:https://docs.vulkan.org/spec/latest/chapters/primsrast.html#VkPolygonMode[VkPolygonMode]. This allows you to "fill in" the primitive. |
| 102 | + |
| 103 | +image::{images}primitive_topology_polygon_mode.svg[primitive_topology_polygon_mode.svg] |
| 104 | + |
| 105 | +If you have a vertex shader that has `VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST` input and then during rasterization uses `VK_POLYGON_MODE_LINE`, the effective topology is the Line link:https://docs.vulkan.org/spec/latest/chapters/drawing.html#drawing-primitive-topology-class[Topology Class] at time. This means something like `lineWidth` would be applied when filling in the polygon with `VK_POLYGON_MODE_LINE`. |
| 106 | + |
| 107 | +== rasterizerDiscardEnable |
| 108 | + |
| 109 | +`VkPipelineRasterizationStateCreateInfo::rasterizerDiscardEnable` (or set dynamically with `vkCmdSetRasterizerDiscardEnable`) controls whether primitives are discarded immediately before the rasterization stage. This is important because when this is set to `VK_TRUE` the rasterization hardware is not executed. There are many Validation Usage errors that will not occur if this is set to `VK_TRUE` because some topology hardware is unused and can be ignored. |
| 110 | + |
| 111 | +[NOTE] |
| 112 | +==== |
| 113 | +Enabling this state is meant for very specific use cases. Prior to compute shaders, this was a common technique for writting geometry shader output to a buffer. It can be used to debug/profile non-rasterization bottle necks. |
| 114 | +==== |
0 commit comments