您的位置:首页 > 编程语言

Vulkan编程指南翻译 第七章 图形管线 第4节 创建简单的图形管线(下)

2017-03-22 00:51 507 查看
这一节实在太长了,拖了好久。还是分开发吧。

7.4.3  输入组装

图形管线的输入组装阶段接受顶点数据,并把它们分组,组成图元,以供管线接下来的部分处理。它是通过一个VkPipelineInputAssemblyStateCreateInfo类型的数据描述的,通过VkGraphicsPipelineCreateInfo类型数据的pInputAssemblyState成员传递。VkPipelineInputAssemblyStateCreateInfo的定义是:

typedef struct VkPipelineInputAssemblyStateCreateInfo {

VkStructureType sType;

const void* pNext;

VkPipelineInputAssemblyStateCreateFlags flags;

VkPrimitiveTopology topology;

VkBool32 primitiveRestartEnable;

} VkPipelineInputAssemblyStateCreateInfo;

 

sType域应置为VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,pNext应置为nullptr。flags域被保留使用,应置为0。图元拓扑类型由topology指定,应该是受Vulkan支持的图元拓扑类型之一。它们是VkPrimitiveTopology类型的枚举。枚举中最简单的成员是list拓扑,如下所示:

• VK_PRIMITIVE_TOPOLOGY_POINT_LIST: Each vertex is used to construct an

independent point.

• VK_PRIMITIVE_TOPOLOGY_LINE_LIST: Vertices are grouped into pairs, each pair

forming a line segment from the first to the second vertex.

• VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST: Vertices are grouped into triplets forming

Triangles.

 

接下来是strip和fan图元。这些是顶点组成的图元(线段或者三角形)再组成的,每一个线段或者三角形都与上一个共享一个或者两个顶点。strip和fan图元如下所示:

• VK_PRIMITIVE_TOPOLOGY_LINE_STRIP: The first two vertices in a draw form a single

line segment. Each new vertex after them forms a new line segment from the last processed

vertex. The result is a connected sequence of lines.

• VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP: The first three vertices in a draw form a

single triangle. Each subsequent vertex forms a new triangle along with the last two vertices.

The result is a connected row of triangles, each sharing an edge with the last.

• VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN: The first three vertices in a draw form a

single triangle. Each subsequent vertex forms a new triangle along with the last vertex and the

first vertex in the draw.

Strip and fan拓扑并不复杂,但是如果对它们不熟悉可能不容易绘制出来。Figure 7.2展示了它们图形化的布局。

 

Figure 7.2: Strip (Left) and Fan (Right) Topologies
 

接下来是邻接图元,通常是几何着色器启用了才被使用,可以携带有关于在同一个mesh里邻近图元的附加信息。邻接图元拓扑有:

• VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY: Every four vertices in the

draw form a single primitive, with the center two vertices forming a line and the first and last

vertex in each group of four being presented to the geometry shader, when present.

• VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY: The first four vertices in

the draw form a single primitive, with the center two vertices forming a line segment and the

first and last being presented to the geometry shader as adjacency information. Each subsequent

vertex essentially slides this window of four vertices along by one, forming a new line segment

and presenting the new vertex as adjacency information.

• VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY: Similar to lines with

adjacency, each group of six vertices is formed into a single primitive, with the first, third, and

fifth in each group constructing a triangle and the second, fourth, and sixth being presented to

the geometry shader as adjacency information.

• VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY: This is perhaps the

most confusing primitive topology and certainly needs a diagram to visualize. Essentially, the

strip begins with the first six vertices forming a triangle with adjacency information as in the list

case. For every two new vertices, a new triangle is formed, with the odd-numbered vertices

forming the triangle and the even-numbered vertices providing adjacency information.

 

邻接图元很难被视觉化出来--特别是VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY拓扑。Figure
7.3示例了在VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY拓扑中顶点的布局。在这张图中你可以看到从12个顶点中形成了两个三角形。顶点由外层包裹一个三角形,奇数序号的顶点形成了中心三角形(A和B),偶数序号的顶点形成了并不会被渲染的虚拟的三角形,但是,携带有邻接的信息。这个概念也同样在三角形strip图元中适用,Figure
7.4展示了如何应用到VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY。

 
Figure 7.3: Triangles with Adjacency Topology
 
 
Figure 7.4: Triangle Strip with Adjacency Topology
邻接拓扑通常在几何着色器存在时才被使用,因为几何着色器是唯一能够看到邻接顶点的阶段。然而,也可能没有几何着色器的情况下使用邻接图元,邻接的顶点直接被扔掉了而已。
 

 

 

 

7.4.4  细分状态

 

7.4.5  视口状态

 

7.4.6  栅格化状态

 

7.4.7  多采样状态

多采样是为图像内每一个像素生成多个样本的过程。用来对抗走样,在图像直接使用时能很大程度上提升图像质量。当你进行多采样时,颜色和深度-stencil附件必须是多采样图像,并且管线的多采样状态应该通过VkGraphicsPipelineCreateInfo的pMultisampleState域合理设置。这个一个指向VkPipelineMultisampleStateCreateInfo类型数据的指针,该类型定义为:

typedef struct VkPipelineMultisampleStateCreateInfo {

VkStructureType sType;

const void* pNext;

VkPipelineMultisampleStateCreateFlags flags;

VkSampleCountFlagBits rasterizationSamples;

VkBool32 sampleShadingEnable;

float minSampleShading;

const VkSampleMask* pSampleMask;

VkBool32 alphaToCoverageEnable;

VkBool32 alphaToOneEnable;

} VkPipelineMultisampleStateCreateInfo;

 

VkPipelineMultisampleStateCreateInfo的sType域应置为VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,pNext应置为nullptr。flags域被保留使用,应置为0。

 

 

7.4.8  深度和stencil测试状态

深度-stencil状态控制了如何进行深度和stencil测试和如何决定片元如何通过这些测试。深度和stencil测试可以在片元着色器运行之前或之后进行。默认情况下,深度测试在片元着色器运行之后发生。1

1. Most implementations will only keep up the appearance that the depth and stencil tests are running after the fragment shader and, if possible, run the tests before running the shader to avoid running shader code when the test would fail.

 
要在深度测试之前运行片元着色器,我们可以设置我们的片元着色器入口为SPIR-V EarlyFragmentTests执行模式。
Depth-stencil状态通过VkGraphicsPipelineCreateInfo类型的成员pDepthStencilState,一个指向VkPipelineDepthStencilStateCreateInfo类型数据的指针来配置。VkPipelineDepthStencilStateCreateInfo的定义是:
typedef struct VkPipelineDepthStencilStateCreateInfo {
VkStructureType sType;
const void* pNext;
VkPipelineDepthStencilStateCreateFlags flags;
VkBool32 depthTestEnable;
VkBool32 depthWriteEnable;
VkCompareOp depthCompareOp;
VkBool32 depthBoundsTestEnable;
VkBool32 stencilTestEnable;
VkStencilOpState front;
VkStencilOpState back;
float minDepthBounds;
float maxDepthBounds;
} VkPipelineDepthStencilStateCreateInfo;
VkPipelineDepthStencilStateCreateInfo的sType域应置为VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_CREATE_INFO,pNext应置为nullptr。flags域被保留使用,应置为0。
如果depthTestEnable被设置为VK_TRUE,那么深度测试被开启。如果深度测试被启用,那么测试算法可通过depthCompareOp选择,它是VkCompareOp枚举值之一。可用的深度测试操作将在第十章“片元处理”中深入的讲解。如果depthTestEnable被设置为VK_FALSE,那么深度测试被关闭。depthCompareOp被启用,所有的片元被认为已经通过了深度测试。需要注意,当深度测试被关闭是,将不会有深度缓冲区写入操作。
如果通过了深度测试(或者深度测试被关闭),那么片元测试继续到stencil测试。如果VkPipelineDepthStencilCreateInfo的stencilTestEnable被设置为VK_TRUE,那么stencil测试被开启。当stencil测试被开启时,front
and back成员给正面和背面图元提供了单独的状态。如果stencil测试被禁用了,所有的图元被认为通过了stencil测试。
关于深度和stencil测试,将会在第十章“片元处理”中深入讲解。
 

7.4.9  颜色混合状态

Vulkan图形管线的最后一个阶段是颜色混合阶段。这个阶段负责把片元写入颜色附件。在很多情况下,这是一个非常简单的哦操作,仅仅是把附件的数据覆盖为片元着色器的输出即可。然而,颜色混合可以把帧缓冲区中已存在的值做混合,在片元着色器的输出和当前的帧缓存区的内容之间进行简单的逻辑操作。

颜色混合状态通过VkGraphicsPipelineCreateInfo的pColorBlendState成员指定。这是一个指向VkPipelineColorBlendStateCreateInfo类型实例的指针,该类型定义是:

typedef struct VkPipelineColorBlendStateCreateInfo {

VkStructureType sType;

const void* pNext;

VkPipelineColorBlendStateCreateFlags flags;

VkBool32 logicOpEnable;

VkLogicOp logicOp;

uint32_t attachmentCount;

const VkPipelineColorBlendAttachmentState* pAttachments;

float blendConstants[4];

} VkPipelineColorBlendStateCreateInfo;

VkPipelineColorBlendStateCreateInfo的sType域应置为VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,pNext应置为nullptr。flags域被保留使用,应置为0。

logicOpEnable域指定了在片元着色器的输出和颜色附件的内容之间是否进行逻辑操作。当logicOpEnable是VK_FALSE时,逻辑操作被禁用,片元着色器的输出被未经改变的写入到颜色附件。当logicOpEnable为VK_TRUE时,逻辑操作被支持他的附件启用。采用的逻辑操作对于每个附件来说都是一样的,它是VkLogicOp枚举类型的一个成员。每一个枚举的意义和关于逻辑操作的更多信息在第十章“片元处理”中给出。

每一个附件可以有不同的格式,可以支持不同混合操作。这些是通过一个VkPipelineColorBlendAttachmentState类型数组来指定的,数组的地址通过VkPipelineColorBlendStateCreateInfo.类型数据的成员pAttachments来传递。附件个数通过attachmentCount设置。VkPipelineColorBlendAttachmentState的定义是:

typedef struct VkPipelineColorBlendAttachmentState {

VkBool32 blendEnable;

VkBlendFactor srcColorBlendFactor;

VkBlendFactor dstColorBlendFactor;

VkBlendOp colorBlendOp;

VkBlendFactor srcAlphaBlendFactor;

VkBlendFactor dstAlphaBlendFactor;

VkBlendOp alphaBlendOp;

VkColorComponentFlags colorWriteMask;

} VkPipelineColorBlendAttachmentState;

 

对已每一个颜色附件,VkPipelineColorBlendAttachmentState的成员控制了是否开启混合,源和目标的比例因子,何种混合操作(),需要更新输出图像的哪个通道。

如果VkPipelineColorBlendAttachmentState的colorBlendEnable域为VK_TRUE,那么剩余的参数控制了混合的状态。混合将会在第十章讲到。当colorBlendEnable是VK_FALSE是,VkPipelineColorBlendAttachmentState中的混合相关的参数被忽略,该附件的混合被禁用。

不管colorBlendEnable的状态如何,最后一个域:colorWriteMask,控制了输出图像的哪个通道写入到福建。这是一个位域,由VkColorComponentFlagBits枚举的一个或多个值组横。四个通道,由VK_COLOR_COMPONENT_R_BIT,
VK_COLOR_COMPONENT_G_BIT,

VK_COLOR_COMPONENT_B_BIT, and VK_COLOR_COMPONENT_A_BIT表示,可以被单独的设置。如果对应某个通道的标志位没有被包含在colorWriteMask,那么这个通道将不会被修改。只有被colorWriteMask包含的通道将通过渲染到附件来更新。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐