20. Drawing Commands
Drawing commands (commands with Draw
in the name) provoke work in a
graphics pipeline.
Drawing commands are recorded into a command buffer and when executed by a
queue, will produce work which executes according to the bound graphics
pipeline.
A graphics pipeline must be bound to a command buffer before any drawing
commands are recorded in that command buffer.
Each draw is made up of zero or more vertices and zero or more instances,
which are processed by the device and result in the assembly of primitives.
Primitives are assembled according to the pInputAssemblyState
member
of the VkGraphicsPipelineCreateInfo structure, which is of type
VkPipelineInputAssemblyStateCreateInfo
:
// Provided by VK_VERSION_1_0
typedef struct VkPipelineInputAssemblyStateCreateInfo {
VkStructureType sType;
const void* pNext;
VkPipelineInputAssemblyStateCreateFlags flags;
VkPrimitiveTopology topology;
VkBool32 primitiveRestartEnable;
} VkPipelineInputAssemblyStateCreateInfo;

sType
is the type of this structure. 
pNext
isNULL
or a pointer to a structure extending this structure. 
flags
is reserved for future use. 
topology
is a VkPrimitiveTopology defining the primitive topology, as described below. 
primitiveRestartEnable
controls whether a special vertex index value is treated as restarting the assembly of primitives. This enable only applies to indexed draws (vkCmdDrawIndexed, and vkCmdDrawIndexedIndirect), and the special index value is either 0xFFFFFFFF when theindexType
parameter ofvkCmdBindIndexBuffer
is equal toVK_INDEX_TYPE_UINT32
, or 0xFFFF whenindexType
is equal toVK_INDEX_TYPE_UINT16
. Primitive restart is not allowed for “list” topologies.
Restarting the assembly of primitives discards the most recent index values
if those elements formed an incomplete primitive, and restarts the primitive
assembly using the subsequent indices, but only assembling the immediately
following element through the end of the originally specified elements.
The primitive restart index value comparison is performed before adding the
vertexOffset
value to the index value.
// Provided by VK_VERSION_1_0
typedef VkFlags VkPipelineInputAssemblyStateCreateFlags;
VkPipelineInputAssemblyStateCreateFlags
is a bitmask type for setting
a mask, but is currently reserved for future use.
20.1. Primitive Topologies
Primitive topology determines how consecutive vertices are organized into primitives, and determines the type of primitive that is used at the beginning of the graphics pipeline. The effective topology for later stages of the pipeline is altered by tessellation or geometry shading (if either is in use) and depends on the execution modes of those shaders.
The primitive topologies defined by VkPrimitiveTopology are:
// Provided by VK_VERSION_1_0
typedef enum VkPrimitiveTopology {
VK_PRIMITIVE_TOPOLOGY_POINT_LIST = 0,
VK_PRIMITIVE_TOPOLOGY_LINE_LIST = 1,
VK_PRIMITIVE_TOPOLOGY_LINE_STRIP = 2,
VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST = 3,
VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP = 4,
VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN = 5,
VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY = 6,
VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY = 7,
VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY = 8,
VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY = 9,
VK_PRIMITIVE_TOPOLOGY_PATCH_LIST = 10,
} VkPrimitiveTopology;

VK_PRIMITIVE_TOPOLOGY_POINT_LIST
specifies a series of separate point primitives. 
VK_PRIMITIVE_TOPOLOGY_LINE_LIST
specifies a series of separate line primitives. 
VK_PRIMITIVE_TOPOLOGY_LINE_STRIP
specifies a series of connected line primitives with consecutive lines sharing a vertex. 
VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST
specifies a series of separate triangle primitives. 
VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP
specifies a series of connected triangle primitives with consecutive triangles sharing an edge. 
VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN
specifies a series of connected triangle primitives with all triangles sharing a common vertex. 
VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY
specifies a series of separate line primitives with adjacency. 
VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY
specifies a series of connected line primitives with adjacency, with consecutive primitives sharing three vertices. 
VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY
specifies a series of separate triangle primitives with adjacency. 
VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY
specifies connected triangle primitives with adjacency, with consecutive triangles sharing an edge. 
VK_PRIMITIVE_TOPOLOGY_PATCH_LIST
specifies separate patch primitives.
Each primitive topology, and its construction from a list of vertices, is described in detail below with a supporting diagram, according to the following key:
Vertex 
A point in 3dimensional space. Positions chosen within the diagrams are arbitrary and for illustration only. 

Vertex Number 
Sequence position of a vertex within the provided vertex data. 

Provoking Vertex 
Provoking vertex within the main primitive. The tail is angled towards the relevant primitive. Used in flat shading. 

Primitive Edge 
An edge connecting the points of a main primitive. 

Adjacency Edge 
Points connected by these lines do not contribute to a main primitive, and are only accessible in a geometry shader. 

Winding Order 
The relative order in which vertices are defined within a primitive, used in the facing determination. This ordering has no specific start or end point. 
The diagrams are supported with mathematical definitions where the vertices (v) and primitives (p) are numbered starting from 0; v_{0} is the first vertex in the provided data and p_{0} is the first primitive in the set of primitives defined by the vertices and topology.
20.1.1. Point Lists
When the topology is VK_PRIMITIVE_TOPOLOGY_POINT_LIST
, each
consecutive vertex defines a single point primitive, according to the
equation:

p_{i} = {v_{i}}
As there is only one vertex, that vertex is the provoking vertex.
The number of primitives generated is equal to vertexCount
.
20.1.2. Line Lists
When the topology
is VK_PRIMITIVE_TOPOLOGY_LINE_LIST
, each
consecutive pair of vertices defines a single line primitive, according to
the equation:

p_{i} = {v_{2i}, v_{2i+1}}
The number of primitives generated is equal to
⌊vertexCount
/2⌋.
The provoking vertex for p_{i} is v_{2i}.
20.1.3. Line Strips
When the topology
is VK_PRIMITIVE_TOPOLOGY_LINE_STRIP
, one line
primitive is defined by each vertex and the following vertex, according to
the equation:

p_{i} = {v_{i}, v_{i+1}}
The number of primitives generated is equal to
max(0,vertexCount
1).
The provoking vertex for p_{i} is v_{i}.
20.1.4. Triangle Lists
When the topology
is VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST
, each
consecutive set of three vertices defines a single triangle primitive,
according to the equation:

p_{i} = {v_{3i}, v_{3i+1}, v_{3i+2}}
The number of primitives generated is equal to
⌊vertexCount
/3⌋.
The provoking vertex for p_{i} is v_{3i}.
20.1.5. Triangle Strips
When the topology
is VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP
, one
triangle primitive is defined by each vertex and the two vertices that
follow it, according to the equation:

p_{i} = {v_{i}, v_{i+(1+i%2)}, v_{i+(2i%2)}}
The number of primitives generated is equal to
max(0,vertexCount
2).
The provoking vertex for p_{i} is v_{i}.
Note
The ordering of the vertices in each successive triangle is reversed, so that the winding order is consistent throughout the strip. 
20.1.6. Triangle Fans
When the topology
is VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN
,
triangle primitives are defined around a shared common vertex, according to
the equation:

p_{i} = {v_{i+1}, v_{i+2}, v_{0}}
The number of primitives generated is equal to
max(0,vertexCount
2).
The provoking vertex for p_{i} is v_{i+1}.
20.1.7. Line Lists With Adjacency
When the topology
is
VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY
, each consecutive set
of four vertices defines a single line primitive with adjacency, according
to the equation:

p_{i} = {v_{4i}, v_{4i+1}, v_{4i+2},v_{4i+3}}
A line primitive is described by the second and third vertices of the total primitive, with the remaining two vertices only accessible in a geometry shader.
The number of primitives generated is equal to
⌊vertexCount
/4⌋.
The provoking vertex for p_{i} is v_{4i+1}.
20.1.8. Line Strips With Adjacency
When the topology
is
VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY
, one line primitive
with adjacency is defined by each vertex and the following vertex, according
to the equation:

p_{i} = {v_{i}, v_{i+1}, v_{i+2}, v_{i+3}}
A line primitive is described by the second and third vertices of the total primitive, with the remaining two vertices only accessible in a geometry shader.
The number of primitives generated is equal to
max(0,vertexCount
3).
The provoking vertex for p_{i} is v_{i+1}.
20.1.9. Triangle Lists With Adjacency
When the topology
is
VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY
, each consecutive
set of six vertices defines a single triangle primitive with adjacency,
according to the equations:

p_{i} = {v_{6i}, v_{6i+1}, v_{6i+2}, v_{6i+3}, v_{6i+4}, v_{6i+5}}
A triangle primitive is described by the first, third, and fifth vertices of the total primitive, with the remaining three vertices only accessible in a geometry shader.
The number of primitives generated is equal to
⌊vertexCount
/6⌋.
The provoking vertex for p_{i} is v_{6i}.
20.1.10. Triangle Strips With Adjacency
When the topology
is
VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY
, one triangle
primitive with adjacency is defined by each vertex and the following 5
vertices.
The number of primitives generated, n, is equal to ⌊max(0,
vertexCount
 4)/2⌋.
If n=1, the primitive is defined as:

p = {v_{0}, v_{1}, v_{2}, v_{5}, v_{4}, v_{3}}
If n>1, the total primitive consists of different vertices according to where it is in the strip:

p_{i} = {v_{2i}, v_{2i+1}, v_{2i+2}, v_{2i+6}, v_{2i+4}, v_{2i+3}} when i=0

p_{i} = {v_{2i}, v_{2i+3}, v_{2i+4}, v_{2i+6}, v_{2i+2}, v_{2i2}} when i>0, i<n1, and i%2=1

p_{i} = {v_{2i}, v_{2i2}, v_{2i+2}, v_{2i+6}, v_{2i+4}, v_{2i+3}} when i>0, i<n1, and i%2=0

p_{i} = {v_{2i}, v_{2i+3}, v_{2i+4}, v_{2i+5}, v_{2i+2}, v_{2i2}} when i=n1 and i%2=1

p_{i} = {v_{2i}, v_{2i2}, v_{2i+2}, v_{2i+5}, v_{2i+4}, v_{2i+3}} when i=n1 and i%2=0
A triangle primitive is described by the first, third, and fifth vertices of the total primitive in all cases, with the remaining three vertices only accessible in a geometry shader.
Note
The ordering of the vertices in each successive triangle is altered so that the winding order is consistent throughout the strip. 
The provoking vertex for p_{i} is always v_{2i}.
20.1.11. Patch Lists
When the topology
is VK_PRIMITIVE_TOPOLOGY_PATCH_LIST
, each
consecutive set of m vertices defines a single patch primitive,
according to the equation:

p_{i} = {v_{mi}, v_{mi+1}, …, v_{mi+(m2)}, v_{mi+(m1)}}
where m is equal to
VkPipelineTessellationStateCreateInfo::patchControlPoints
.
Patch lists are never passed to vertex postprocessing,
and as such no provoking vertex is defined for patch primitives.
The number of primitives generated is equal to
⌊vertexCount
/m⌋.
The vertices comprising a patch have no implied geometry, and are used as inputs to tessellation shaders and the fixedfunction tessellator to generate new point, line, or triangle primitives.
20.2. Primitive Order
Primitives generated by drawing commands progress through the stages of the graphics pipeline in primitive order. Primitive order is initially determined in the following way:

Submission order determines the initial ordering

For indirect drawing commands, the order in which accessed instances of the VkDrawIndirectCommand are stored in
buffer
, from lower indirect buffer addresses to higher addresses. 
If a drawing command includes multiple instances, the order in which instances are executed, from lower numbered instances to higher.

The order in which primitives are specified by a drawing command:

For nonindexed draws, from vertices with a lower numbered
vertexIndex
to a higher numberedvertexIndex
. 
For indexed draws, vertices sourced from a lower index buffer addresses to higher addresses.

Within this order implementations further sort primitives:

If tessellation shading is active, by an implementationdependent order of new primitives generated by tessellation.

If geometry shading is active, by the order new primitives are generated by geometry shading.

If the polygon mode is not
VK_POLYGON_MODE_FILL
, by an implementationdependent ordering of the new primitives generated within the original primitive.
Primitive order is later used to define rasterization order, which determines the order in which fragments output results to a framebuffer.
20.3. Programmable Primitive Shading
Once primitives are assembled, they proceed to the vertex shading stage of the pipeline. If the draw includes multiple instances, then the set of primitives is sent to the vertex shading stage multiple times, once for each instance.
It is implementationdependent whether vertex shading occurs on vertices that are discarded as part of incomplete primitives, but if it does occur then it operates as if they were vertices in complete primitives and such invocations can have side effects.
Vertex shading receives two pervertex inputs from the primitive assembly
stage  the vertexIndex
and the instanceIndex
.
How these values are generated is defined below, with each command.
Drawing commands fall roughly into two categories:

Nonindexed drawing commands present a sequential
vertexIndex
to the vertex shader. The sequential index is generated automatically by the device (see FixedFunction Vertex Processing for details on both specifying the vertex attributes indexed byvertexIndex
, as well as binding vertex buffers containing those attributes to a command buffer). These commands are: 
Indexed drawing commands read index values from an index buffer and use this to compute the
vertexIndex
value for the vertex shader. These commands are:
To bind an index buffer to a command buffer, call:
// Provided by VK_VERSION_1_0
void vkCmdBindIndexBuffer(
VkCommandBuffer commandBuffer,
VkBuffer buffer,
VkDeviceSize offset,
VkIndexType indexType);

commandBuffer
is the command buffer into which the command is recorded. 
buffer
is the buffer being bound. 
offset
is the starting offset in bytes withinbuffer
used in index buffer address calculations. 
indexType
is a VkIndexType value specifying the size of the indices.
Possible values of vkCmdBindIndexBuffer::indexType
, specifying
the size of indices, are:
// Provided by VK_VERSION_1_0
typedef enum VkIndexType {
VK_INDEX_TYPE_UINT16 = 0,
VK_INDEX_TYPE_UINT32 = 1,
} VkIndexType;

VK_INDEX_TYPE_UINT16
specifies that indices are 16bit unsigned integer values. 
VK_INDEX_TYPE_UINT32
specifies that indices are 32bit unsigned integer values.
The parameters for each drawing command are specified directly in the command or read from buffer memory, depending on the command. Drawing commands that source their parameters from buffer memory are known as indirect drawing commands.
All drawing commands interact with the Robust Buffer Access feature.
To record a nonindexed draw, call:
// Provided by VK_VERSION_1_0
void vkCmdDraw(
VkCommandBuffer commandBuffer,
uint32_t vertexCount,
uint32_t instanceCount,
uint32_t firstVertex,
uint32_t firstInstance);

commandBuffer
is the command buffer into which the command is recorded. 
vertexCount
is the number of vertices to draw. 
instanceCount
is the number of instances to draw. 
firstVertex
is the index of the first vertex to draw. 
firstInstance
is the instance ID of the first instance to draw.
When the command is executed, primitives are assembled using the current
primitive topology and vertexCount
consecutive vertex indices with the
first vertexIndex
value equal to firstVertex
.
The primitives are drawn instanceCount
times with instanceIndex
starting with firstInstance
and increasing sequentially for each
instance.
The assembled primitives execute the bound graphics pipeline.
To record an indexed draw, call:
// Provided by VK_VERSION_1_0
void vkCmdDrawIndexed(
VkCommandBuffer commandBuffer,
uint32_t indexCount,
uint32_t instanceCount,
uint32_t firstIndex,
int32_t vertexOffset,
uint32_t firstInstance);

commandBuffer
is the command buffer into which the command is recorded. 
indexCount
is the number of vertices to draw. 
instanceCount
is the number of instances to draw. 
firstIndex
is the base index within the index buffer. 
vertexOffset
is the value added to the vertex index before indexing into the vertex buffer. 
firstInstance
is the instance ID of the first instance to draw.
When the command is executed, primitives are assembled using the current
primitive topology and indexCount
vertices whose indices are retrieved
from the index buffer.
The index buffer is treated as an array of tightly packed unsigned integers
of size defined by the vkCmdBindIndexBuffer::indexType
parameter
with which the buffer was bound.
The first vertex index is at an offset of firstIndex
×
indexSize
+ offset
within the bound index buffer, where
offset
is the offset specified by vkCmdBindIndexBuffer
and
indexSize
is the byte size of the type specified by indexType
.
Subsequent index values are retrieved from consecutive locations in the
index buffer.
Indices are first compared to the primitive restart value, then zero
extended to 32 bits (if the indexType
is
VK_INDEX_TYPE_UINT16
) and have vertexOffset
added to them,
before being supplied as the vertexIndex
value.
The primitives are drawn instanceCount
times with instanceIndex
starting with firstInstance
and increasing sequentially for each
instance.
The assembled primitives execute the bound graphics pipeline.
To record a nonindexed indirect drawing command, call:
// Provided by VK_VERSION_1_0
void vkCmdDrawIndirect(
VkCommandBuffer commandBuffer,
VkBuffer buffer,
VkDeviceSize offset,
uint32_t drawCount,
uint32_t stride);

commandBuffer
is the command buffer into which the command is recorded. 
buffer
is the buffer containing draw parameters. 
offset
is the byte offset intobuffer
where parameters begin. 
drawCount
is the number of draws to execute, and can be zero. 
stride
is the byte stride between successive sets of draw parameters.
vkCmdDrawIndirect
behaves similarly to vkCmdDraw except that the
parameters are read by the device from a buffer during execution.
drawCount
draws are executed by the command, with parameters taken
from buffer
starting at offset
and increasing by stride
bytes for each successive draw.
The parameters of each draw are encoded in an array of
VkDrawIndirectCommand structures.
If drawCount
is less than or equal to one, stride
is ignored.
The VkDrawIndirectCommand
structure is defined as:
// Provided by VK_VERSION_1_0
typedef struct VkDrawIndirectCommand {
uint32_t vertexCount;
uint32_t instanceCount;
uint32_t firstVertex;
uint32_t firstInstance;
} VkDrawIndirectCommand;

vertexCount
is the number of vertices to draw. 
instanceCount
is the number of instances to draw. 
firstVertex
is the index of the first vertex to draw. 
firstInstance
is the instance ID of the first instance to draw.
The members of VkDrawIndirectCommand
have the same meaning as the
similarly named parameters of vkCmdDraw.
To record an indexed indirect drawing command, call:
// Provided by VK_VERSION_1_0
void vkCmdDrawIndexedIndirect(
VkCommandBuffer commandBuffer,
VkBuffer buffer,
VkDeviceSize offset,
uint32_t drawCount,
uint32_t stride);

commandBuffer
is the command buffer into which the command is recorded. 
buffer
is the buffer containing draw parameters. 
offset
is the byte offset intobuffer
where parameters begin. 
drawCount
is the number of draws to execute, and can be zero. 
stride
is the byte stride between successive sets of draw parameters.
vkCmdDrawIndexedIndirect
behaves similarly to vkCmdDrawIndexed
except that the parameters are read by the device from a buffer during
execution.
drawCount
draws are executed by the command, with parameters taken
from buffer
starting at offset
and increasing by stride
bytes for each successive draw.
The parameters of each draw are encoded in an array of
VkDrawIndexedIndirectCommand structures.
If drawCount
is less than or equal to one, stride
is ignored.
The VkDrawIndexedIndirectCommand
structure is defined as:
// Provided by VK_VERSION_1_0
typedef struct VkDrawIndexedIndirectCommand {
uint32_t indexCount;
uint32_t instanceCount;
uint32_t firstIndex;
int32_t vertexOffset;
uint32_t firstInstance;
} VkDrawIndexedIndirectCommand;

indexCount
is the number of vertices to draw. 
instanceCount
is the number of instances to draw. 
firstIndex
is the base index within the index buffer. 
vertexOffset
is the value added to the vertex index before indexing into the vertex buffer. 
firstInstance
is the instance ID of the first instance to draw.
The members of VkDrawIndexedIndirectCommand
have the same meaning as
the similarly named parameters of vkCmdDrawIndexed.