9.2. 图形管线
图形管线由多个着色器阶段、多个固定功能管线阶段和管线布局组成。
想创建图形管线,可调用:
VkResult vkCreateGraphicsPipelines(
VkDevice device,
VkPipelineCache pipelineCache,
uint32_t createInfoCount,
const VkGraphicsPipelineCreateInfo* pCreateInfos,
const VkAllocationCallbacks* pAllocator,
VkPipeline* pPipelines);
-
device
是创建图形管线的逻辑设备。 -
pipelineCache
或者是VK_NULL_HANDLE
,表示禁用管线缓存;或者是一个pipeline cache 对象有效的handle,此时 该命令运行期间将启用该缓存。 -
createInfoCount
是pCreateInfos
和pPipelines
数组的长度。 -
pCreateInfos
是一个VkGraphicsPipelineCreateInfo
数组。 -
pAllocator
控制了CPU端内存分配,如 Memory Allocation 一章所描述。 -
pPipelines
是一个指针,指向了存储被返回的图形管线对象的数组。
VkGraphicsPipelineCreateInfo
数据结构包含一个数组的着色器创建信息, 包括了所有想要的活跃的着色器阶段, 也包含了等译所有相关的固定管线阶段和管线布局。
VkGraphicsPipelineCreateInfo
数据结构定义如下:
typedef struct VkGraphicsPipelineCreateInfo {
VkStructureType sType;
const void* pNext;
VkPipelineCreateFlags flags;
uint32_t stageCount;
const VkPipelineShaderStageCreateInfo* pStages;
const VkPipelineVertexInputStateCreateInfo* pVertexInputState;
const VkPipelineInputAssemblyStateCreateInfo* pInputAssemblyState;
const VkPipelineTessellationStateCreateInfo* pTessellationState;
const VkPipelineViewportStateCreateInfo* pViewportState;
const VkPipelineRasterizationStateCreateInfo* pRasterizationState;
const VkPipelineMultisampleStateCreateInfo* pMultisampleState;
const VkPipelineDepthStencilStateCreateInfo* pDepthStencilState;
const VkPipelineColorBlendStateCreateInfo* pColorBlendState;
const VkPipelineDynamicStateCreateInfo* pDynamicState;
VkPipelineLayout layout;
VkRenderPass renderPass;
uint32_t subpass;
VkPipeline basePipelineHandle;
int32_t basePipelineIndex;
} VkGraphicsPipelineCreateInfo;
-
sType
是数据结构的类型。 -
pNext
是NULL
或者一个指向拓展特定的数据结构的指针。 -
flags
is a bitmask ofVkPipelineCreateFlagBits
controlling how the pipeline will be generated, as described below. -
stageCount
is the number of entries in thepStages
array. -
pStages
is an array of sizestageCount
structures of typeVkPipelineShaderStageCreateInfo
describing the set of the shader stages to be included in the graphics pipeline. -
pVertexInputState
is a pointer to an instance of theVkPipelineVertexInputStateCreateInfo
structure. -
pInputAssemblyState
is a pointer to an instance of theVkPipelineInputAssemblyStateCreateInfo
structure which determines input assembly behavior, as described in Drawing Commands. -
pTessellationState
is a pointer to an instance of theVkPipelineTessellationStateCreateInfo
structure, orNULL
if the pipeline does not include a tessellation control shader stage and tessellation evaluation shader stage. -
pViewportState
is a pointer to an instance of theVkPipelineViewportStateCreateInfo
structure, orNULL
if the pipeline has rasterization disabled. -
pRasterizationState
is a pointer to an instance of theVkPipelineRasterizationStateCreateInfo
structure. -
pMultisampleState
is a pointer to an instance of theVkPipelineMultisampleStateCreateInfo
, orNULL
if the pipeline has rasterization disabled. -
pDepthStencilState
is a pointer to an instance of theVkPipelineDepthStencilStateCreateInfo
structure, orNULL
if the pipeline has rasterization disabled or if the subpass of the render pass the pipeline is created against does not use a depth/stencil attachment. -
pColorBlendState
is a pointer to an instance of theVkPipelineColorBlendStateCreateInfo
structure, orNULL
if the pipeline has rasterization disabled or if the subpass of the render pass the pipeline is created against does not use any color attachments. -
pDynamicState
is a pointer toVkPipelineDynamicStateCreateInfo
and is used to indicate which properties of the pipeline state object are dynamic and can be changed independently of the pipeline state. This can beNULL
, which means no state in the pipeline is considered dynamic. -
layout
is the description of binding locations used by both the pipeline and descriptor sets used with the pipeline. -
renderPass
is a handle to a render pass object describing the environment in which the pipeline will be used; the pipeline must only be used with an instance of any render pass compatible with the one provided. See Render Pass Compatibility for more information. -
subpass
is the index of the subpass in the render pass where this pipeline will be used. -
basePipelineHandle
is a pipeline to derive from. -
basePipelineIndex
is an index into thepCreateInfos
parameter to use as a pipeline to derive from.
basePipelineHandle
和 basePipelineIndex
参数在 Pipeline Derivatives 中有详细的描述。
pStages
指向了一个 VkPipelineShaderStageCreateInfo
数组,此类型在之前的 Compute Pipelines中讲解过。
flags
有如下可选枚举值:
typedef enum VkPipelineCreateFlagBits {
VK_PIPELINE_CREATE_DISABLE_OPTIMIZATION_BIT = 0x00000001,
VK_PIPELINE_CREATE_ALLOW_DERIVATIVES_BIT = 0x00000002,
VK_PIPELINE_CREATE_DERIVATIVE_BIT = 0x00000004,
} VkPipelineCreateFlagBits;
-
VK_PIPELINE_CREATE_DISABLE_OPTIMIZATION_BIT
指定了创建的管线将不会被优化。使用这个标志位将减少创建管线的时间。 -
VK_PIPELINE_CREATE_ALLOW_DERIVATIVES_BIT
指定了将要创建的管线可以是另外一个管线的父,子管线可以在接下来的一次vkCreateGraphicsPipelines
调用中创建。 -
VK_PIPELINE_CREATE_DERIVATIVE_BIT
指定了当前被创建的管线是之前被创建的管线的子。
同时设置VK_PIPELINE_CREATE_ALLOW_DERIVATIVES_BIT
和 VK_PIPELINE_CREATE_DERIVATIVE_BIT
也是合法的。这将允许管线即是父,也是子。 参考Pipeline Derivatives 获取更多详细信息。
pDynamicState
指向一个类型为VkPipelineDynamicStateCreateInfo
的数据结构。
VkPipelineDynamicStateCreateInfo
类型数据结构定义如下:
typedef struct VkPipelineDynamicStateCreateInfo {
VkStructureType sType;
const void* pNext;
VkPipelineDynamicStateCreateFlags flags;
uint32_t dynamicStateCount;
const VkDynamicState* pDynamicStates;
} VkPipelineDynamicStateCreateInfo;
-
sType
是数据结构的类型。 -
pNext
是NULL
或者一个指向拓展特定的数据结构的指针。 -
flags
被保留。 -
dynamicStateCount
是pDynamicStates
数组的元素个数。 -
pDynamicStates
是一个VkDynamicState
枚举类型的数组,管线阶段的哪一部分将从动态命令中而不是从管线创建信息中使用值。
The source of difference pieces of dynamic state is determined by theVkPipelineDynamicStateCreateInfo
::pDynamicStates
property of the currently active pipeline, which takes the following values:
typedef enum VkDynamicState {
VK_DYNAMIC_STATE_VIEWPORT = 0,
VK_DYNAMIC_STATE_SCISSOR = 1,
VK_DYNAMIC_STATE_LINE_WIDTH = 2,
VK_DYNAMIC_STATE_DEPTH_BIAS = 3,
VK_DYNAMIC_STATE_BLEND_CONSTANTS = 4,
VK_DYNAMIC_STATE_DEPTH_BOUNDS = 5,
VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK = 6,
VK_DYNAMIC_STATE_STENCIL_WRITE_MASK = 7,
VK_DYNAMIC_STATE_STENCIL_REFERENCE = 8,
} VkDynamicState;
-
VK_DYNAMIC_STATE_VIEWPORT
indicates that thepViewports
state inVkPipelineViewportStateCreateInfo
will be ignored and must be set dynamically withvkCmdSetViewport
before any draw commands. The number of viewports used by a pipeline is still specified by theviewportCount
member ofVkPipelineViewportStateCreateInfo
. -
VK_DYNAMIC_STATE_SCISSOR
indicates that thepScissors
state inVkPipelineViewportStateCreateInfo
will be ignored and must be set dynamically withvkCmdSetScissor
before any draw commands. The number of scissor rectangles used by a pipeline is still specified by thescissorCount
member ofVkPipelineViewportStateCreateInfo
. -
VK_DYNAMIC_STATE_LINE_WIDTH
indicates that thelineWidth
state inVkPipelineRasterizationStateCreateInfo
will be ignored and must be set dynamically withvkCmdSetLineWidth
before any draw commands that generate line primitives for the rasterizer. -
VK_DYNAMIC_STATE_DEPTH_BIAS
indicates that thedepthBiasConstantFactor
,depthBiasClamp
anddepthBiasSlopeFactor
states inVkPipelineRasterizationStateCreateInfo
will be ignored and must be set dynamically withvkCmdSetDepthBias
before any draws are performed withdepthBiasEnable
inVkPipelineRasterizationStateCreateInfo
set toVK_TRUE
. -
VK_DYNAMIC_STATE_BLEND_CONSTANTS
indicates that theblendConstants
state inVkPipelineColorBlendStateCreateInfo
will be ignored and must be set dynamically withvkCmdSetBlendConstants
before any draws are performed with a pipeline state withVkPipelineColorBlendAttachmentState
memberblendEnable
set toVK_TRUE
and any of the blend functions using a constant blend color. -
VK_DYNAMIC_STATE_DEPTH_BOUNDS
indicates that theminDepthBounds
andmaxDepthBounds
states ofVkPipelineDepthStencilStateCreateInfo
will be ignored and must be set dynamically withvkCmdSetDepthBounds
before any draws are performed with a pipeline state withVkPipelineDepthStencilStateCreateInfo
memberdepthBoundsTestEnable
set toVK_TRUE
. -
VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK
indicates that thecompareMask
state inVkPipelineDepthStencilStateCreateInfo
for bothfront
andback
will be ignored and must be set dynamically withvkCmdSetStencilCompareMask
before any draws are performed with a pipeline state withVkPipelineDepthStencilStateCreateInfo
memberstencilTestEnable
set toVK_TRUE
-
VK_DYNAMIC_STATE_STENCIL_WRITE_MASK
indicates that thewriteMask
state inVkPipelineDepthStencilStateCreateInfo
for bothfront
andback
will be ignored and must be set dynamically withvkCmdSetStencilWriteMask
before any draws are performed with a pipeline state withVkPipelineDepthStencilStateCreateInfo
memberstencilTestEnable
set toVK_TRUE
-
VK_DYNAMIC_STATE_STENCIL_REFERENCE
indicates that thereference
state inVkPipelineDepthStencilStateCreateInfo
for bothfront
andback
will be ignored and must be set dynamically withvkCmdSetStencilReference
before any draws are performed with a pipeline state withVkPipelineDepthStencilStateCreateInfo
memberstencilTestEnable
set toVK_TRUE
9.2.1. Valid Combinations of Stages for Graphics Pipelines
如果省略了细分着色器阶段,那么细分着色和这个管线的固定功能就被跳过了。
若省略了几何着色器,几何着色阶段就被跳过。
若省略了片元着色器,片元处理的结果就是未定义的。 Specifically, any fragment color outputs are considered to have undefined values, and the fragment depth is considered to be unmodified. This can be useful for depth-only rendering.
Presence of a shader stage in a pipeline is indicated by including a valid VkPipelineShaderStageCreateInfo
with module
and pName
selecting an entry point from a shader module, where that entry point is valid for the stage specified by stage
.
Presence of some of the fixed-function stages in the pipeline is implicitly derived from enabled shaders and provided state. For example, the fixed-function tessellator is always present when the pipeline has valid Tessellation Control and Tessellation Evaluation shaders.
-
Depth/stencil-only rendering in a subpass with no color attachments
-
Active Pipeline Shader Stages
-
Vertex Shader
-
-
Required: Fixed-Function Pipeline Stages
-
-
Color-only rendering in a subpass with no depth/stencil attachment
-
Active Pipeline Shader Stages
-
Vertex Shader
-
Fragment Shader
-
-
Required: Fixed-Function Pipeline Stages
-
-
Rendering pipeline with tessellation and geometry shaders
-
Active Pipeline Shader Stages
-
Vertex Shader
-
Tessellation Control Shader
-
Tessellation Evaluation Shader
-
Geometry Shader
-
Fragment Shader
-
-
Required: Fixed-Function Pipeline Stages
-