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或者一个指向拓展特定的数据结构的指针。 -
flagsis a bitmask ofVkPipelineCreateFlagBitscontrolling how the pipeline will be generated, as described below. -
stageCountis the number of entries in thepStagesarray. -
pStagesis an array of sizestageCountstructures of typeVkPipelineShaderStageCreateInfodescribing the set of the shader stages to be included in the graphics pipeline. -
pVertexInputStateis a pointer to an instance of theVkPipelineVertexInputStateCreateInfostructure. -
pInputAssemblyStateis a pointer to an instance of theVkPipelineInputAssemblyStateCreateInfostructure which determines input assembly behavior, as described in Drawing Commands. -
pTessellationStateis a pointer to an instance of theVkPipelineTessellationStateCreateInfostructure, orNULLif the pipeline does not include a tessellation control shader stage and tessellation evaluation shader stage. -
pViewportStateis a pointer to an instance of theVkPipelineViewportStateCreateInfostructure, orNULLif the pipeline has rasterization disabled. -
pRasterizationStateis a pointer to an instance of theVkPipelineRasterizationStateCreateInfostructure. -
pMultisampleStateis a pointer to an instance of theVkPipelineMultisampleStateCreateInfo, orNULLif the pipeline has rasterization disabled. -
pDepthStencilStateis a pointer to an instance of theVkPipelineDepthStencilStateCreateInfostructure, orNULLif 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. -
pColorBlendStateis a pointer to an instance of theVkPipelineColorBlendStateCreateInfostructure, orNULLif the pipeline has rasterization disabled or if the subpass of the render pass the pipeline is created against does not use any color attachments. -
pDynamicStateis a pointer toVkPipelineDynamicStateCreateInfoand 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. -
layoutis the description of binding locations used by both the pipeline and descriptor sets used with the pipeline. -
renderPassis 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. -
subpassis the index of the subpass in the render pass where this pipeline will be used. -
basePipelineHandleis a pipeline to derive from. -
basePipelineIndexis an index into thepCreateInfosparameter 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_VIEWPORTindicates that thepViewportsstate inVkPipelineViewportStateCreateInfowill be ignored and must be set dynamically withvkCmdSetViewportbefore any draw commands. The number of viewports used by a pipeline is still specified by theviewportCountmember ofVkPipelineViewportStateCreateInfo. -
VK_DYNAMIC_STATE_SCISSORindicates that thepScissorsstate inVkPipelineViewportStateCreateInfowill be ignored and must be set dynamically withvkCmdSetScissorbefore any draw commands. The number of scissor rectangles used by a pipeline is still specified by thescissorCountmember ofVkPipelineViewportStateCreateInfo. -
VK_DYNAMIC_STATE_LINE_WIDTHindicates that thelineWidthstate inVkPipelineRasterizationStateCreateInfowill be ignored and must be set dynamically withvkCmdSetLineWidthbefore any draw commands that generate line primitives for the rasterizer. -
VK_DYNAMIC_STATE_DEPTH_BIASindicates that thedepthBiasConstantFactor,depthBiasClampanddepthBiasSlopeFactorstates inVkPipelineRasterizationStateCreateInfowill be ignored and must be set dynamically withvkCmdSetDepthBiasbefore any draws are performed withdepthBiasEnableinVkPipelineRasterizationStateCreateInfoset toVK_TRUE. -
VK_DYNAMIC_STATE_BLEND_CONSTANTSindicates that theblendConstantsstate inVkPipelineColorBlendStateCreateInfowill be ignored and must be set dynamically withvkCmdSetBlendConstantsbefore any draws are performed with a pipeline state withVkPipelineColorBlendAttachmentStatememberblendEnableset toVK_TRUEand any of the blend functions using a constant blend color. -
VK_DYNAMIC_STATE_DEPTH_BOUNDSindicates that theminDepthBoundsandmaxDepthBoundsstates ofVkPipelineDepthStencilStateCreateInfowill be ignored and must be set dynamically withvkCmdSetDepthBoundsbefore any draws are performed with a pipeline state withVkPipelineDepthStencilStateCreateInfomemberdepthBoundsTestEnableset toVK_TRUE. -
VK_DYNAMIC_STATE_STENCIL_COMPARE_MASKindicates that thecompareMaskstate inVkPipelineDepthStencilStateCreateInfofor bothfrontandbackwill be ignored and must be set dynamically withvkCmdSetStencilCompareMaskbefore any draws are performed with a pipeline state withVkPipelineDepthStencilStateCreateInfomemberstencilTestEnableset toVK_TRUE -
VK_DYNAMIC_STATE_STENCIL_WRITE_MASKindicates that thewriteMaskstate inVkPipelineDepthStencilStateCreateInfofor bothfrontandbackwill be ignored and must be set dynamically withvkCmdSetStencilWriteMaskbefore any draws are performed with a pipeline state withVkPipelineDepthStencilStateCreateInfomemberstencilTestEnableset toVK_TRUE -
VK_DYNAMIC_STATE_STENCIL_REFERENCEindicates that thereferencestate inVkPipelineDepthStencilStateCreateInfofor bothfrontandbackwill be ignored and must be set dynamically withvkCmdSetStencilReferencebefore any draws are performed with a pipeline state withVkPipelineDepthStencilStateCreateInfomemberstencilTestEnableset 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
-
本文详细介绍如何使用 Vulkan API 创建图形管线,包括必要的 VkGraphicsPipelineCreateInfo 结构体配置、着色器阶段定义、固定功能阶段配置等内容。
188

被折叠的 条评论
为什么被折叠?



