24. FixedFunction Vertex PostProcessing
After programmable vertex processing, the following fixedfunction operations are applied to vertices of the resulting primitives:

Viewport swizzle (see Viewport Swizzle)

Flat shading (see Flat Shading).

Primitive clipping, including clientdefined halfspaces (see Primitive Clipping).

Shader output attribute clipping (see Clipping Shader Outputs).

Clip space W scaling (see Controlling Viewport W Scaling).

Perspective division on clip coordinates (see Coordinate Transformations).

Viewport mapping, including depth range scaling (see Controlling the Viewport).

Front face determination for polygon primitives (see Basic Polygon Rasterization).
Next, rasterization is performed on primitives as described in chapter Rasterization.
24.1. Viewport Swizzle
Each primitive sent to a given viewport has a swizzle and optional negation
applied to its clip coordinates.
The swizzle that is applied depends on the viewport index, and is controlled
by the VkPipelineViewportSwizzleStateCreateInfoNV
pipeline state:
typedef struct VkPipelineViewportSwizzleStateCreateInfoNV {
VkStructureType sType;
const void* pNext;
VkPipelineViewportSwizzleStateCreateFlagsNV flags;
uint32_t viewportCount;
const VkViewportSwizzleNV* pViewportSwizzles;
} VkPipelineViewportSwizzleStateCreateInfoNV;

sType
is the type of this structure. 
pNext
isNULL
or a pointer to an extensionspecific structure. 
flags
is reserved for future use. 
viewportCount
is the number of viewport swizzles used by the pipeline. 
pViewportSwizzles
is a pointer to an array of VkViewportSwizzleNV structures, defining the viewport swizzles.
typedef VkFlags VkPipelineViewportSwizzleStateCreateFlagsNV;
VkPipelineViewportSwizzleStateCreateFlagsNV
is a bitmask type for
setting a mask, but is currently reserved for future use.
The VkPipelineViewportSwizzleStateCreateInfoNV
state is set by adding
an instance of this structure to the pNext
chain of an instance of the
VkPipelineViewportStateCreateInfo
structure and setting the graphics
pipeline state with vkCreateGraphicsPipelines.
Each viewport specified from 0 to viewportCount
 1 has its x,y,z,w
swizzle state set to the corresponding x
, y
, z
and w
in the VkViewportSwizzleNV structure.
Each component is of type VkViewportCoordinateSwizzleNV, which
determines the type of swizzle for that component.
The value of x
computes the new x component of the position as:
if (x == VK_VIEWPORT_COORDINATE_SWIZZLE_POSITIVE_X_NV) x' = x;
if (x == VK_VIEWPORT_COORDINATE_SWIZZLE_NEGATIVE_X_NV) x' = x;
if (x == VK_VIEWPORT_COORDINATE_SWIZZLE_POSITIVE_Y_NV) x' = y;
if (x == VK_VIEWPORT_COORDINATE_SWIZZLE_NEGATIVE_Y_NV) x' = y;
if (x == VK_VIEWPORT_COORDINATE_SWIZZLE_POSITIVE_Z_NV) x' = z;
if (x == VK_VIEWPORT_COORDINATE_SWIZZLE_NEGATIVE_Z_NV) x' = z;
if (x == VK_VIEWPORT_COORDINATE_SWIZZLE_POSITIVE_W_NV) x' = w;
if (x == VK_VIEWPORT_COORDINATE_SWIZZLE_NEGATIVE_W_NV) x' = w;
Similar selections are performed for the y
, z
, and w
coordinates.
This swizzling is applied before clipping and perspective divide.
If the swizzle for an active viewport index is not specified, the swizzle
for x
is VK_VIEWPORT_COORDINATE_SWIZZLE_POSITIVE_X_NV
, y
is VK_VIEWPORT_COORDINATE_SWIZZLE_POSITIVE_Y_NV
, z
is
VK_VIEWPORT_COORDINATE_SWIZZLE_POSITIVE_Z_NV
and w
is
VK_VIEWPORT_COORDINATE_SWIZZLE_POSITIVE_W_NV
.
Viewport swizzle parameters are specified by setting the pNext
pointer
of VkGraphicsPipelineCreateInfo
to point to an instance of
VkPipelineViewportSwizzleStateCreateInfoNV
.
VkPipelineViewportSwizzleStateCreateInfoNV uses
VkViewportSwizzleNV
to set the viewport swizzle parameters.
The VkViewportSwizzleNV
structure is defined as:
typedef struct VkViewportSwizzleNV {
VkViewportCoordinateSwizzleNV x;
VkViewportCoordinateSwizzleNV y;
VkViewportCoordinateSwizzleNV z;
VkViewportCoordinateSwizzleNV w;
} VkViewportSwizzleNV;

x
is a VkViewportCoordinateSwizzleNV value specifying the swizzle operation to apply to the x component of the primitive 
y
is a VkViewportCoordinateSwizzleNV value specifying the swizzle operation to apply to the y component of the primitive 
z
is a VkViewportCoordinateSwizzleNV value specifying the swizzle operation to apply to the z component of the primitive 
w
is a VkViewportCoordinateSwizzleNV value specifying the swizzle operation to apply to the w component of the primitive
Possible values of the VkViewportSwizzleNV::x
, y
, z
,
and w
members, specifying swizzling of the corresponding components of
primitives, are:
typedef enum VkViewportCoordinateSwizzleNV {
VK_VIEWPORT_COORDINATE_SWIZZLE_POSITIVE_X_NV = 0,
VK_VIEWPORT_COORDINATE_SWIZZLE_NEGATIVE_X_NV = 1,
VK_VIEWPORT_COORDINATE_SWIZZLE_POSITIVE_Y_NV = 2,
VK_VIEWPORT_COORDINATE_SWIZZLE_NEGATIVE_Y_NV = 3,
VK_VIEWPORT_COORDINATE_SWIZZLE_POSITIVE_Z_NV = 4,
VK_VIEWPORT_COORDINATE_SWIZZLE_NEGATIVE_Z_NV = 5,
VK_VIEWPORT_COORDINATE_SWIZZLE_POSITIVE_W_NV = 6,
VK_VIEWPORT_COORDINATE_SWIZZLE_NEGATIVE_W_NV = 7,
} VkViewportCoordinateSwizzleNV;
These values are described in detail in Viewport Swizzle.
24.2. Flat Shading
Flat shading a vertex output attribute means to assign all vertices of the primitive the same value for that output.
The output values assigned are those of the provoking vertex of the primitive. The provoking vertex depends on the primitive topology, and is generally the “first” vertex of the primitive. For primitives not processed by tessellation or geometry shaders, the provoking vertex is selected from the input vertices according to the following table.
Primitive type of primitive i 
Provoking vertex number 

i 

2 i 

i 

3 i 

i 

i + 1 

4 i + 1 

i + 1 

6 i 

2 i 
Flat shading is applied to those vertex attributes that
match fragment input attributes which
are decorated as Flat
.
If a geometry shader is active, the output primitive topology is either points, line strips, or triangle strips, and the selection of the provoking vertex behaves according to the corresponding row of the table. If a tessellation evaluation shader is active and a geometry shader is not active, the provoking vertex is undefined but must be one of the vertices of the primitive.
24.3. Primitive Clipping
Primitives are culled against the cull volume and then clipped to the clip volume. In clip coordinates, the view volume is defined by:
This view volume can be further restricted by as many as
VkPhysicalDeviceLimits
::maxClipDistances
clientdefined
halfspaces.
The cull volume is the intersection of up to
VkPhysicalDeviceLimits
::maxCullDistances
clientdefined
halfspaces (if no clientdefined cull halfspaces are enabled, culling
against the cull volume is skipped).
A shader must write a single cull distance for each enabled cull halfspace
to elements of the CullDistance
array.
If the cull distance for any enabled cull halfspace is negative for all of
the vertices of the primitive under consideration, the primitive is
discarded.
Otherwise the primitive is clipped against the clip volume as defined below.
The clip volume is the intersection of up to
VkPhysicalDeviceLimits
::maxClipDistances
clientdefined
halfspaces with the view volume (if no clientdefined clip halfspaces are
enabled, the clip volume is the view volume).
A shader must write a single clip distance for each enabled clip halfspace
to elements of the ClipDistance
array.
Clip halfspace i is then given by the set of points satisfying the
inequality

c_{i}(P) ≥ 0
where c_{i}(P) is the clip distance i at point P. For point primitives, c_{i}(P) is simply the clip distance for the vertex in question. For line and triangle primitives, pervertex clip distances are interpolated using a weighted mean, with weights derived according to the algorithms described in sections Basic Line Segment Rasterization and Basic Polygon Rasterization, using the perspective interpolation equations.
The number of clientdefined clip and cull halfspaces that are enabled is
determined by the explicit size of the builtin arrays ClipDistance
and
CullDistance
, respectively, declared as an output in the interface of
the entry point of the final shader stage before clipping.
Depth clamping is enabled or disabled via the depthClampEnable
enable
of the VkPipelineRasterizationStateCreateInfo
structure.
If depth clamping is enabled, the plane equation

0 ≤ z_{c} ≤ w_{c}
(see the clip volume definition above) is ignored by view volume clipping (effectively, there is no near or far plane clipping).
If the primitive under consideration is a point or line segment, then clipping passes it unchanged if its vertices lie entirely within the clip volume.
Possible values of
VkPhysicalDevicePointClippingProperties::pointClippingBehavior
,
specifying clipping behavior of a point primitive whose vertex lies outside
the clip volume, are:
typedef enum VkPointClippingBehavior {
VK_POINT_CLIPPING_BEHAVIOR_ALL_CLIP_PLANES = 0,
VK_POINT_CLIPPING_BEHAVIOR_USER_CLIP_PLANES_ONLY = 1,
VK_POINT_CLIPPING_BEHAVIOR_ALL_CLIP_PLANES_KHR = VK_POINT_CLIPPING_BEHAVIOR_ALL_CLIP_PLANES,
VK_POINT_CLIPPING_BEHAVIOR_USER_CLIP_PLANES_ONLY_KHR = VK_POINT_CLIPPING_BEHAVIOR_USER_CLIP_PLANES_ONLY,
} VkPointClippingBehavior;
or the equivalent
typedef VkPointClippingBehavior VkPointClippingBehaviorKHR;

VK_POINT_CLIPPING_BEHAVIOR_ALL_CLIP_PLANES
specifies that the primitive is discarded if the vertex lies outside any clip plane, including the planes bounding the view volume. 
VK_POINT_CLIPPING_BEHAVIOR_USER_CLIP_PLANES_ONLY
specifies that the primitive is discarded only if the vertex lies outside any user clip plane.
If either of a line segment’s vertices lie outside of the clip volume, the line segment may be clipped, with new vertex coordinates computed for each vertex that lies outside the clip volume. A clipped line segment endpoint lies on both the original line segment and the boundary of the clip volume.
This clipping produces a value, 0 ≤ t ≤ 1, for each clipped vertex. If the coordinates of a clipped vertex are P and the original vertices’ coordinates are P_{1} and P_{2}, then t is given by

P = t P_{1} + (1t) P_{2}.
t is used to clip vertex output attributes as described in Clipping Shader Outputs.
If the primitive is a polygon, it passes unchanged if every one of its edges lie entirely inside the clip volume, and it is discarded if every one of its edges lie entirely outside the clip volume. If the edges of the polygon intersect the boundary of the clip volume, the intersecting edges are reconnected by new edges that lie along the boundary of the clip volume  in some cases requiring the introduction of new vertices into a polygon.
If a polygon intersects an edge of the clip volume’s boundary, the clipped polygon must include a point on this boundary edge.
Primitives rendered with userdefined halfspaces must satisfy a complementarity criterion. Suppose a series of primitives is drawn where each vertex i has a single specified clip distance d_{i} (or a number of similarly specified clip distances, if multiple halfspaces are enabled). Next, suppose that the same series of primitives are drawn again with each such clip distance replaced by d_{i} (and the graphics pipeline is otherwise the same). In this case, primitives must not be missing any pixels, and pixels must not be drawn twice in regions where those primitives are cut by the clip planes.
24.4. Clipping Shader Outputs
Next, vertex output attributes are clipped. The output values associated with a vertex that lies within the clip volume are unaffected by clipping. If a primitive is clipped, however, the output values assigned to vertices produced by clipping are clipped.
Let the output values assigned to the two vertices P_{1} and P_{2} of an unclipped edge be c_{1} and c_{2}. The value of t (see Primitive Clipping) for a clipped point P is used to obtain the output value associated with P as

c = t c_{1} + (1t) c_{2}.
(Multiplying an output value by a scalar means multiplying each of x, y, z, and w by the scalar.)
Since this computation is performed in clip space before division by w_{c}, clipped output values are perspectivecorrect.
Polygon clipping creates a clipped vertex along an edge of the clip volume’s boundary. This situation is handled by noting that polygon clipping proceeds by clipping against one halfspace at a time. Output value clipping is done in the same way, so that clipped points always occur at the intersection of polygon edges (possibly already clipped) with the clip volume’s boundary.
For vertex output attributes whose matching fragment input attributes are
decorated with NoPerspective
, the value of t used to obtain the
output value associated with P will be adjusted to produce results
that vary linearly in framebuffer space.
Output attributes of integer or unsigned integer type must always be flat shaded. Flat shaded attributes are constant over the primitive being rasterized (see Basic Line Segment Rasterization and Basic Polygon Rasterization), and no interpolation is performed. The output value c is taken from either c_{1} or c_{2}, since flat shading has already occurred and the two values are identical.
24.5. Controlling Viewport W Scaling
If viewport W scaling is enabled, the W component of the clip coordinate is modified by the provided coefficients from the corresponding viewport as follows.

w_{c}' = x_{coeff} x_{c} + y_{coeff} y_{c} + w_{c}
The VkPipelineViewportWScalingStateCreateInfoNV
structure is defined
as:
typedef struct VkPipelineViewportWScalingStateCreateInfoNV {
VkStructureType sType;
const void* pNext;
VkBool32 viewportWScalingEnable;
uint32_t viewportCount;
const VkViewportWScalingNV* pViewportWScalings;
} VkPipelineViewportWScalingStateCreateInfoNV;

sType
is the type of this structure. 
pNext
isNULL
or a pointer to an extensionspecific structure. 
viewportWScalingEnable
controls whether viewport W scaling is enabled. 
viewportCount
is the number of viewports used by W scaling, and must match the number of viewports in the pipeline if viewport W scaling is enabled. 
pViewportWScalings
is a pointer to an array ofVkViewportWScalingNV
structures, which define the W scaling parameters for the corresponding viewport. If the viewport W scaling state is dynamic, this member is ignored.
The VkPipelineViewportWScalingStateCreateInfoNV
state is set by adding
an instance of this structure to the pNext
chain of an instance of the
VkPipelineViewportStateCreateInfo
structure and setting the graphics
pipeline state with vkCreateGraphicsPipelines.
If the bound pipeline state object was not created with the
VK_DYNAMIC_STATE_VIEWPORT_W_SCALING_NV
dynamic state enabled, viewport
W scaling parameters are specified using the pViewportWScalings
member of VkPipelineViewportWScalingStateCreateInfoNV in the pipeline
state object.
If the pipeline state object was created with the
VK_DYNAMIC_STATE_VIEWPORT_W_SCALING_NV
dynamic state enabled, the
viewport transformation parameters are dynamically set and changed with the
command:
void vkCmdSetViewportWScalingNV(
VkCommandBuffer commandBuffer,
uint32_t firstViewport,
uint32_t viewportCount,
const VkViewportWScalingNV* pViewportWScalings);

commandBuffer
is the command buffer into which the command will be recorded. 
firstViewport
is the index of the first viewport whose parameters are updated by the command. 
viewportCount
is the number of viewports whose parameters are updated by the command. 
pViewportWScalings
is a pointer to an array of VkViewportWScalingNV structures specifying viewport parameters.
The viewport parameters taken from element i of
pViewportWScalings
replace the current state for the viewport index
firstViewport
+ i, for i in [0,
viewportCount
).
Both VkPipelineViewportWScalingStateCreateInfoNV and
vkCmdSetViewportWScalingNV use VkViewportWScalingNV
to set the
viewport transformation parameters.
The VkViewportWScalingNV
structure is defined as:
typedef struct VkViewportWScalingNV {
float xcoeff;
float ycoeff;
} VkViewportWScalingNV;

xcoeff
andycoeff
are the viewport’s W scaling factor for x and y respectively.
24.6. Coordinate Transformations
Clip coordinates for a vertex result from shader execution, which yields a
vertex coordinate Position
.
Perspective division on clip coordinates yields normalized device coordinates, followed by a viewport transformation (see Controlling the Viewport) to convert these coordinates into framebuffer coordinates.
If a vertex in clip coordinates has a position given by
then the vertex’s normalized device coordinates are
24.7. Controlling the Viewport
The viewport transformation is determined by the selected viewport’s width and height in pixels, p_{x} and p_{y}, respectively, and its center (o_{x}, o_{y}) (also in pixels), as well as its depth range min and max determining a depth range scale value p_{z} and a depth range bias value o_{z} (defined below). The vertex’s framebuffer coordinates (x_{f}, y_{f}, z_{f}) are given by

x_{f} = (p_{x} / 2) x_{d} + o_{x}

y_{f} = (p_{y} / 2) y_{d} + o_{y}

z_{f} = p_{z} × z_{d} + o_{z}
Multiple viewports are available, numbered zero up to
VkPhysicalDeviceLimits
::maxViewports
minus one.
The number of viewports used by a pipeline is controlled by the
viewportCount
member of the VkPipelineViewportStateCreateInfo
structure used in pipeline creation.
The VkPipelineViewportStateCreateInfo
structure is defined as:
typedef struct VkPipelineViewportStateCreateInfo {
VkStructureType sType;
const void* pNext;
VkPipelineViewportStateCreateFlags flags;
uint32_t viewportCount;
const VkViewport* pViewports;
uint32_t scissorCount;
const VkRect2D* pScissors;
} VkPipelineViewportStateCreateInfo;

sType
is the type of this structure. 
pNext
isNULL
or a pointer to an extensionspecific structure. 
flags
is reserved for future use. 
viewportCount
is the number of viewports used by the pipeline. 
pViewports
is a pointer to an array of VkViewport structures, defining the viewport transforms. If the viewport state is dynamic, this member is ignored. 
scissorCount
is the number of scissors and must match the number of viewports. 
pScissors
is a pointer to an array of VkRect2D structures which define the rectangular bounds of the scissor for the corresponding viewport. If the scissor state is dynamic, this member is ignored.
typedef VkFlags VkPipelineViewportStateCreateFlags;
VkPipelineViewportStateCreateFlags
is a bitmask type for setting a
mask, but is currently reserved for future use.
A vertex processing stage may direct each primitive to zero or more
viewports.
The destination viewports for a primitive are selected by the last active
vertex processing stage that has an output variable decorated with
ViewportIndex
(selecting a single viewport) or ViewportMaskNV
(selecting multiple viewports).
The viewport transform uses the viewport corresponding to either the value
assigned to ViewportIndex
or one of the bits set in
ViewportMaskNV
, and taken from an implementationdependent vertex of
each primitive.
If ViewportIndex
or any of the bits in ViewportMaskNV
are outside
the range zero to viewportCount
minus one for a primitive, or if the
last active vertex processing stage did not assign a value to either
ViewportIndex
or ViewportMaskNV
for all vertices of a primitive
due to flow control, the results of the viewport transformation of the
vertices of such primitives are undefined.
If the last vertex processing stage does not have an output decorated with
ViewportIndex
or ViewportMaskNV
, the viewport numbered zero is
used by the viewport transformation.
A single vertex can be used in more than one individual primitive, in
primitives such as VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP
.
In this case, the viewport transformation is applied separately for each
primitive.
If the bound pipeline state object was not created with the
VK_DYNAMIC_STATE_VIEWPORT
dynamic state enabled, viewport
transformation parameters are specified using the pViewports
member of
VkPipelineViewportStateCreateInfo
in the pipeline state object.
If the pipeline state object was created with the
VK_DYNAMIC_STATE_VIEWPORT
dynamic state enabled, the viewport
transformation parameters are dynamically set and changed with the command:
void vkCmdSetViewport(
VkCommandBuffer commandBuffer,
uint32_t firstViewport,
uint32_t viewportCount,
const VkViewport* pViewports);

commandBuffer
is the command buffer into which the command will be recorded. 
firstViewport
is the index of the first viewport whose parameters are updated by the command. 
viewportCount
is the number of viewports whose parameters are updated by the command. 
pViewports
is a pointer to an array of VkViewport structures specifying viewport parameters.
The viewport parameters taken from element i of pViewports
replace the current state for the viewport index firstViewport
+ i, for i in [0, viewportCount
).
Both VkPipelineViewportStateCreateInfo and vkCmdSetViewport use
VkViewport
to set the viewport transformation parameters.
The VkViewport
structure is defined as:
typedef struct VkViewport {
float x;
float y;
float width;
float height;
float minDepth;
float maxDepth;
} VkViewport;

x
andy
are the viewport’s upper left corner (x,y). 
width
andheight
are the viewport’s width and height, respectively. 
minDepth
andmaxDepth
are the depth range for the viewport. It is valid forminDepth
to be greater than or equal tomaxDepth
.
The framebuffer depth coordinate z
_{f} may be represented using
either a fixedpoint or floatingpoint representation.
However, a floatingpoint representation must be used if the depth/stencil
attachment has a floatingpoint depth component.
If an mbit fixedpoint representation is used, we assume that it
represents each value \(\frac{k}{2^m  1}\), where k ∈ {
0, 1, …, 2^{m}1 }, as k (e.g. 1.0 is represented in binary as a
string of all ones).
The viewport parameters shown in the above equations are found from these values as

o_{x} =
x
+width
/ 2 
o_{y} =
y
+height
/ 2 
o_{z} =
minDepth

p_{x} =
width

p_{y} =
height

p_{z} =
maxDepth
minDepth
.
The application can specify a negative term for height
, which has the
effect of negating the y coordinate in clip space before performing the
transform.
When using a negative height
, the application should also adjust the
y
value to point to the lower left corner of the viewport instead of
the upper left corner.
Using the negative height
allows the application to avoid having to
negate the y component of the Position
output from the last vertex
processing stage in shaders that also target other graphics APIs.
The width and height of the implementationdependent maximum viewport dimensions must be greater than or equal to the width and height of the largest image which can be created and attached to a framebuffer.
The floatingpoint viewport bounds are represented with an implementationdependent precision.