The Graphics Pipeline - College of...

27
mjb – September 4, 2019 Computer Graphics 1 GraphicsPipeline.pptx Mike Bailey [email protected] This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License The Graphics Pipeline

Transcript of The Graphics Pipeline - College of...

Page 1: The Graphics Pipeline - College of Engineeringweb.engr.oregonstate.edu/.../GraphicsPipeline.1pp.pdf · 2019-09-17 · mjb –September 4, 2019 Computer Graphics 1 GraphicsPipeline.pptx

mjb – September 4, 2019Computer Graphics

1

GraphicsPipeline.pptx

Mike [email protected]

This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License

The Graphics Pipeline

Page 2: The Graphics Pipeline - College of Engineeringweb.engr.oregonstate.edu/.../GraphicsPipeline.1pp.pdf · 2019-09-17 · mjb –September 4, 2019 Computer Graphics 1 GraphicsPipeline.pptx

mjb – September 4, 2019Computer Graphics

2What is the Vulkan Graphics Pipeline?

Here’s what you need to know:

1. The Vulkan Graphics Pipeline is like what OpenGL would call “The State”, or “The Context”. It is a data structure.

2. The Vulkan Graphics Pipeline is not the processes that OpenGLwould call “the graphics pipeline”.

3. For the most part, the Vulkan Graphics Pipeline is meant to be immutable – that is, once this combination of state variables is combined into a Pipeline, that Pipeline never gets changed. To make new combinations of state variables, create a new Graphics Pipelines.

4. The shaders get compiled the rest of the way when their Graphics Pipeline gets created.

Don’t worry if this is too small to read –a larger version is coming up.

There is also a Vulkan Compute Pipeline – we will get to that later.

Page 3: The Graphics Pipeline - College of Engineeringweb.engr.oregonstate.edu/.../GraphicsPipeline.1pp.pdf · 2019-09-17 · mjb –September 4, 2019 Computer Graphics 1 GraphicsPipeline.pptx

mjb – September 4, 2019Computer Graphics

3

Input Assembly

Vertex Input Stage

Viewport

Vertex Shader moduleSpecialization infoVertex Input bindingVertex Input attributes

Fragment Shader moduleSpecialization info

Fragment Shader Stage

Topology

ViewportScissoring

Depth ClampingDiscardEnablePolygonModeCullModeFrontFaceLineWidth

Rasterization

Dynamic StateWhich states are dynamic

Depth/StencilDepthTestEnableDepthWriteEnableDepthCompareOpStencilTestEnable

Tesselation, Geometry ShadersTessellation Shaders, Geometry Shader

Color Blending parameters Color Blending Stage

Graphics Pipeline Stages and what goes into ThemThe GPU and Driver specify the Pipeline Stages – the Vulkan Graphics Pipeline declares what goes in them

Page 4: The Graphics Pipeline - College of Engineeringweb.engr.oregonstate.edu/.../GraphicsPipeline.1pp.pdf · 2019-09-17 · mjb –September 4, 2019 Computer Graphics 1 GraphicsPipeline.pptx

mjb – September 4, 2019Computer Graphics

4The First Step: Create the Graphics Pipeline Layout

VkResultInit14GraphicsPipelineLayout( ){

VkResult result;

VkPipelineLayoutCreateInfo vplci;vplci.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;vplci.pNext = nullptr;vplci.flags = 0;vplci.setLayoutCount = 4;vplci.pSetLayouts = &DescriptorSetLayouts[0];vplci.pushConstantRangeCount = 0;vplci.pPushConstantRanges = (VkPushConstantRange *)nullptr;

result = vkCreatePipelineLayout( LogicalDevice, IN &vplci, PALLOCATOR, OUT &GraphicsPipelineLayout );

return result;}

The Graphics Pipeline Layout is fairly static. Only the layout of the Descriptor Sets and information on the Push Constants need to be supplied.

Let the Pipeline Layout know about the Descriptor Set and Push Constant layouts.

Page 5: The Graphics Pipeline - College of Engineeringweb.engr.oregonstate.edu/.../GraphicsPipeline.1pp.pdf · 2019-09-17 · mjb –September 4, 2019 Computer Graphics 1 GraphicsPipeline.pptx

mjb – September 4, 2019Computer Graphics

5Vulkan: A Pipeline Records the Following Items:

• Pipeline Layout: DescriptorSets, PushConstants• Which Shaders are going to be used• Per-vertex input attributes: location, binding, format, offset• Per-vertex input bindings: binding, stride, inputRate• Assembly: topology• Viewport: x, y, w, h, minDepth, maxDepth• Scissoring: x, y, w, h• Rasterization: cullMode, polygonMode, frontFace, lineWidth• Depth: depthTestEnable, depthWriteEnable, depthCompareOp• Stencil: stencilTestEnable, stencilOpStateFront, stencilOpStateBack• Blending: blendEnable, srcColorBlendFactor, dstColorBlendFactor, colorBlendOp,

srcAlphaBlendFactor, dstAlphaBlendFactor, alphaBlendOp, colorWriteMask• DynamicState: which states can be set dynamically (bound to the command buffer,

outside the Pipeline)

Bold/Italics indicates that this state item can also be set with Dynamic Variables

Page 6: The Graphics Pipeline - College of Engineeringweb.engr.oregonstate.edu/.../GraphicsPipeline.1pp.pdf · 2019-09-17 · mjb –September 4, 2019 Computer Graphics 1 GraphicsPipeline.pptx

mjb – September 4, 2019Computer Graphics

6

VkGraphicsPipelineCreateInfo

ShadersVertexInput State

InputAssembly StateTesselation State

Viewport StateRasterization StateMultiSample StateDepthStencil StateColorBlend StateDynamic StatePipeline layoutRenderPass

basePipelineHandlebasePipelineIndex

VkPipelineShaderStageCreateInfo

VkPipelineVertexInputStateCreateInfo

VkVertexInputBindingDescription

VkViewportStateCreateInfo Viewportx, y, w, h, minDepth, maxDepth

offsetextent

ScissorVkPipelineRasterizationStateCreateInfo

cullModepolygonMode

frontFacelineWidth

VkSpecializationInfo

which stage (VERTEX, etc.)

VkShaderModule

VkPipelineInputAssemblyStateCreateInfo

Topology

VkVertexInputAttributeDescription

bindingstride

inputRate locationbindingformatoffset

VkPipelineDepthStencilStateCreateInfo

VkPipelineColorBlendStateCreateInfodepthTestEnabledepthWriteEnabledepthCompareOpstencilTestEnable

stencilOpStateFrontstencilOpStateBack

blendEnablesrcColorBlendFactordstColorBlendFactor

colorBlendOpsrcAlphaBlendFactordstAlphaBlendFactor

alphaBlendOpcolorWriteMask

VkPipelineDynamicStateCreateInfo

vkCreateGraphicsPipeline( )

Array naming the states that can be set dynamically

vkCreatePipelineLayout( )

Descriptor SetLayouts Push Constants

Graphics Pipeline

VkPipelineColorBlendAttachmentState

VkPipelineLayoutCreateInfo

Creating a Graphics Pipeline from a lot of Pieces

Page 7: The Graphics Pipeline - College of Engineeringweb.engr.oregonstate.edu/.../GraphicsPipeline.1pp.pdf · 2019-09-17 · mjb –September 4, 2019 Computer Graphics 1 GraphicsPipeline.pptx

mjb – September 4, 2019Computer Graphics

7

VkResultInit14GraphicsVertexFragmentPipeline( VkShaderModule vertexShader, VkShaderModule fragmentShader,

VkPrimitiveTopology topology, OUT VkPipeline *pGraphicsPipeline ){#ifdef ASSUMPTIONS

vvibd[0].inputRate = VK_VERTEX_INPUT_RATE_VERTEX;vprsci.depthClampEnable = VK_FALSE;vprsci.rasterizerDiscardEnable = VK_FALSE;vprsci.polygonMode = VK_POLYGON_MODE_FILL;vprsci.cullMode = VK_CULL_MODE_NONE; // best to do this because of the projectionMatrix[1][1] *= -1.;vprsci.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;vpmsci.rasterizationSamples = VK_SAMPLE_COUNT_ONE_BIT;vpcbas.blendEnable = VK_FALSE;vpcbsci.logicOpEnable = VK_FALSE;vpdssci.depthTestEnable = VK_TRUE;vpdssci.depthWriteEnable = VK_TRUE;vpdssci.depthCompareOp = VK_COMPARE_OP_LESS;

#endif

. . .

Creating a Typical Graphics Pipeline

These settings seem pretty typical to me. Let’s write a simplified Pipeline-creator that accepts Vertex and Fragment shader modules and the topology, and always uses the settings in red above.

Page 8: The Graphics Pipeline - College of Engineeringweb.engr.oregonstate.edu/.../GraphicsPipeline.1pp.pdf · 2019-09-17 · mjb –September 4, 2019 Computer Graphics 1 GraphicsPipeline.pptx

mjb – September 4, 2019Computer Graphics

8

VkPipelineShaderStageCreateInfo vpssci[2];vpssci[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;vpssci[0].pNext = nullptr;vpssci[0].flags = 0;vpssci[0].stage = VK_SHADER_STAGE_VERTEX_BIT;vpssci[0].module = vertexShader;vpssci[0].pName = "main";vpssci[0].pSpecializationInfo = (VkSpecializationInfo *)nullptr;

#ifdef BITSVK_SHADER_STAGE_VERTEX_BITVK_SHADER_STAGE_TESSELLATION_CONTROL_BITVK_SHADER_STAGE_TESSELLATION_EVALUATION_BITVK_SHADER_STAGE_GEOMETRY_BITVK_SHADER_STAGE_FRAGMENT_BITVK_SHADER_STAGE_COMPUTE_BITVK_SHADER_STAGE_ALL_GRAPHICSVK_SHADER_STAGE_ALL#endif

vpssci[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;vpssci[1].pNext = nullptr;vpssci[1].flags = 0;vpssci[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;vpssci[1].module = fragmentShader;vpssci[1].pName = "main";vpssci[1].pSpecializationInfo = (VkSpecializationInfo *)nullptr;

VkVertexInputBindingDescription vvibd[1]; // an array containing one of these per buffer being usedvvibd[0].binding = 0; // which binding # this isvvibd[0].stride = sizeof( struct vertex ); // bytes between successivevvibd[0].inputRate = VK_VERTEX_INPUT_RATE_VERTEX;

#ifdef CHOICESVK_VERTEX_INPUT_RATE_VERTEXVK_VERTEX_INPUT_RATE_INSTANCE#endif

Link in the Shaders

Use one vpssci array member per shader module you are using

Use one vvibd array member per vertex input array-of-structures you are using

Page 9: The Graphics Pipeline - College of Engineeringweb.engr.oregonstate.edu/.../GraphicsPipeline.1pp.pdf · 2019-09-17 · mjb –September 4, 2019 Computer Graphics 1 GraphicsPipeline.pptx

mjb – September 4, 2019Computer Graphics

9

VkVertexInputAttributeDescription vviad[4]; // an array containing one of these per vertex attribute in all bindings// 4 = vertex, normal, color, texture coordvviad[0].location = 0; // location in the layoutvviad[0].binding = 0; // which binding description this is part ofvviad[0].format = VK_FORMAT_VEC3; // x, y, zvviad[0].offset = offsetof( struct vertex, position ); // 0

#ifdef EXTRAS_DEFINED_AT_THE_TOP// these are here for convenience and readability:#define VK_FORMAT_VEC4 VK_FORMAT_R32G32B32A32_SFLOAT#define VK_FORMAT_XYZW VK_FORMAT_R32G32B32A32_SFLOAT#define VK_FORMAT_VEC3 VK_FORMAT_R32G32B32_SFLOAT#define VK_FORMAT_STP VK_FORMAT_R32G32B32_SFLOAT#define VK_FORMAT_XYZ VK_FORMAT_R32G32B32_SFLOAT#define VK_FORMAT_VEC2 VK_FORMAT_R32G32_SFLOAT#define VK_FORMAT_ST VK_FORMAT_R32G32_SFLOAT#define VK_FORMAT_XY VK_FORMAT_R32G32_SFLOAT#define VK_FORMAT_FLOAT VK_FORMAT_R32_SFLOAT#define VK_FORMAT_S VK_FORMAT_R32_SFLOAT#define VK_FORMAT_X VK_FORMAT_R32_SFLOAT#endif

vviad[1].location = 1;vviad[1].binding = 0;vviad[1].format = VK_FORMAT_VEC3; // nx, ny, nzvviad[1].offset = offsetof( struct vertex, normal ); // 12

vviad[2].location = 2;vviad[2].binding = 0;vviad[2].format = VK_FORMAT_VEC3; // r, g, bvviad[2].offset = offsetof( struct vertex, color ); // 24

vviad[3].location = 3;vviad[3].binding = 0;vviad[3].format = VK_FORMAT_VEC2; // s, tvviad[3].offset = offsetof( struct vertex, texCoord ); // 36

These are defined at the top of the sample code so that you don’t need to use confusing image-looking formats for positions, normals, and tex coords

Use one vviad array member per element in the struct for the array-of-structures element you are using as vertex input

Link in the Per-Vertex Attributes

Page 10: The Graphics Pipeline - College of Engineeringweb.engr.oregonstate.edu/.../GraphicsPipeline.1pp.pdf · 2019-09-17 · mjb –September 4, 2019 Computer Graphics 1 GraphicsPipeline.pptx

mjb – September 4, 2019Computer Graphics

10VkPipelineVertexInputStateCreateInfo vpvisci; // used to describe the input vertex attributes

vpvisci.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;vpvisci.pNext = nullptr;vpvisci.flags = 0;vpvisci.vertexBindingDescriptionCount = 1;vpvisci.pVertexBindingDescriptions = vvibd;vpvisci.vertexAttributeDescriptionCount = 4;vpvisci.pVertexAttributeDescriptions = vviad;

VkPipelineInputAssemblyStateCreateInfo vpiasci;vpiasci.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;vpiasci.pNext = nullptr;vpiasci.flags = 0;vpiasci.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;;

#ifdef CHOICESVK_PRIMITIVE_TOPOLOGY_POINT_LISTVK_PRIMITIVE_TOPOLOGY_LINE_LISTVK_PRIMITIVE_TOPOLOGY_TRIANGLE_LISTVK_PRIMITIVE_TOPOLOGY_LINE_STRIPVK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIPVK_PRIMITIVE_TOPOLOGY_TRIANGLE_FANVK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCYVK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCYVK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCYVK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY#endif

vpiasci.primitiveRestartEnable = VK_FALSE;

VkPipelineTessellationStateCreateInfo vptsci;vptsci.sType = VK_STRUCTURE_TYPE_PIPELINE_TESSELLATION_STATE_CREATE_INFO;vptsci.pNext = nullptr;vptsci.flags = 0;vptsci.patchControlPoints = 0; // number of patch control points

// VkPipelineGeometryStateCreateInfo vpgsci;// vptsci.sType = VK_STRUCTURE_TYPE_PIPELINE_TESSELLATION_STATE_CREATE_INFO;// vptsci.pNext = nullptr;// vptsci.flags = 0;

Declare the binding descriptions and attribute descriptions

Declare the vertex topology

Tessellation Shader info

Geometry Shader info

Page 11: The Graphics Pipeline - College of Engineeringweb.engr.oregonstate.edu/.../GraphicsPipeline.1pp.pdf · 2019-09-17 · mjb –September 4, 2019 Computer Graphics 1 GraphicsPipeline.pptx

mjb – September 4, 2019Computer Graphics

11Options for vpiasci.topology

VK_PRIMITIVE_TOPOLOGY_POINT_LIST

V0

V3

V1

V2

VK_PRIMITIVE_TOPOLOGY_LINE_LIST

V0

V3

V1

V2

VK_PRIMITIVE_TOPOLOGY_LINE_STRIP

V0

V3

V1

V2

V0

V2

V1

V3

V5

VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST

V4

V7

VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP

V0

V2

V1

V3

V6

V5

V4

VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN

V0

V1

V2

V3

V4 V5

V0

Page 12: The Graphics Pipeline - College of Engineeringweb.engr.oregonstate.edu/.../GraphicsPipeline.1pp.pdf · 2019-09-17 · mjb –September 4, 2019 Computer Graphics 1 GraphicsPipeline.pptx

mjb – September 4, 2019Computer Graphics

12

vpiasci.primitiveRestartEnable = VK_FALSE;

What is “Primitive Restart Enable”?

“Restart Enable” is used with:• Indexed drawing.• Triangle Fan and *Strip topologies

If vpiasci.primitiveRestartEnable is VK_TRUE, then a special “index” indicates that the primitive should start over. This is more efficient than explicitly ending the current primitive and explicitly starting a new primitive of the same type.

typedef enum VkIndexType{

VK_INDEX_TYPE_UINT16 = 0, // 0 – 65,535VK_INDEX_TYPE_UINT32 = 1, // 0 – 4,294,967,295

} VkIndexType;

If your VkIndexType is VK_INDEX_TYPE_UINT16, then the special index is 0xffff.If your VkIndexType is VK_INDEX_TYPE_UINT32, it is 0xffffffff.

Page 13: The Graphics Pipeline - College of Engineeringweb.engr.oregonstate.edu/.../GraphicsPipeline.1pp.pdf · 2019-09-17 · mjb –September 4, 2019 Computer Graphics 1 GraphicsPipeline.pptx

mjb – September 4, 2019Computer Graphics

13One Really Good use of Restart Enable is in Drawing Terrain Surfaces with Triangle Strips

Triangle Strip #0:

Triangle Strip #1:

Triangle Strip #2:. . .

Page 14: The Graphics Pipeline - College of Engineeringweb.engr.oregonstate.edu/.../GraphicsPipeline.1pp.pdf · 2019-09-17 · mjb –September 4, 2019 Computer Graphics 1 GraphicsPipeline.pptx

mjb – September 4, 2019Computer Graphics

14

VkViewport vv;vv.x = 0;vv.y = 0;vv.width = (float)Width;vv.height = (float)Height;vv.minDepth = 0.0f;vv.maxDepth = 1.0f;

VkRect2D vr;vr.offset.x = 0;vr.offset.y = 0;vr.extent.width = Width;vr.extent.height = Height;

VkPipelineViewportStateCreateInfo vpvsci;vpvsci.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;vpvsci.pNext = nullptr;vpvsci.flags = 0;vpvsci.viewportCount = 1;vpvsci.pViewports = &vv;vpvsci.scissorCount = 1;vpvsci.pScissors = &vr;

Declare the viewport information

Declare the scissoring information

Group the viewport and scissor information together

Page 15: The Graphics Pipeline - College of Engineeringweb.engr.oregonstate.edu/.../GraphicsPipeline.1pp.pdf · 2019-09-17 · mjb –September 4, 2019 Computer Graphics 1 GraphicsPipeline.pptx

mjb – September 4, 2019Computer Graphics

15What is the Difference Between Changing the Viewport and Changing the Scissoring?

Original Image

Viewporting operates on vertices and takes place right before the rasterizer. Changing the vertical part of the viewport causes the entire scene to get scaled (scrunched) into the viewport area.

Scissoring operates on fragmentsand takes place right after the rasterizer. Changing the vertical part of the scissor causes the entire scene to get clipped where it falls outside the scissor area.

Viewport:

Scissoring:

Page 16: The Graphics Pipeline - College of Engineeringweb.engr.oregonstate.edu/.../GraphicsPipeline.1pp.pdf · 2019-09-17 · mjb –September 4, 2019 Computer Graphics 1 GraphicsPipeline.pptx

mjb – September 4, 2019Computer Graphics

16

VkPipelineRasterizationStateCreateInfo vprsci;vprsci.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;vprsci.pNext = nullptr;vprsci.flags = 0;vprsci.depthClampEnable = VK_FALSE;vprsci.rasterizerDiscardEnable = VK_FALSE;vprsci.polygonMode = VK_POLYGON_MODE_FILL;

#ifdef CHOICESVK_POLYGON_MODE_FILLVK_POLYGON_MODE_LINEVK_POLYGON_MODE_POINT#endif

vprsci.cullMode = VK_CULL_MODE_NONE; // recommend this because of the projMatrix[1][1] *= -1.;#ifdef CHOICESVK_CULL_MODE_NONEVK_CULL_MODE_FRONT_BITVK_CULL_MODE_BACK_BITVK_CULL_MODE_FRONT_AND_BACK_BIT#endif

vprsci.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;#ifdef CHOICESVK_FRONT_FACE_COUNTER_CLOCKWISEVK_FRONT_FACE_CLOCKWISE#endif

vprsci.depthBiasEnable = VK_FALSE;vprsci.depthBiasConstantFactor = 0.f;vprsci.depthBiasClamp = 0.f;vprsci.depthBiasSlopeFactor = 0.f;vprsci.lineWidth = 1.f;

Declare information about how the rasterization will take place

Setting the Rasterizer State

Page 17: The Graphics Pipeline - College of Engineeringweb.engr.oregonstate.edu/.../GraphicsPipeline.1pp.pdf · 2019-09-17 · mjb –September 4, 2019 Computer Graphics 1 GraphicsPipeline.pptx

mjb – September 4, 2019Computer Graphics

17

vprsci.depthClampEnable = VK_FALSE;

What is “Depth Clamp Enable”?

Depth Clamp Enable causes the fragments that would normally have been discarded because they are closer to the viewer than the near clipping plane to instead get projected to the near clipping plane and displayed.

A good use for this is Polygon Capping:

The front of the polygon is clipped, revealing to the viewer that this is really a shell, not a solid

The gray area shows what would happen with depthClampEnable (except it would have been red).

Page 18: The Graphics Pipeline - College of Engineeringweb.engr.oregonstate.edu/.../GraphicsPipeline.1pp.pdf · 2019-09-17 · mjb –September 4, 2019 Computer Graphics 1 GraphicsPipeline.pptx

mjb – September 4, 2019Computer Graphics

18

vprsci.depthBiasEnable = VK_FALSE;vprsci.depthBiasConstantFactor = 0.f;vprsci.depthBiasClamp = 0.f;vprsci.depthBiasSlopeFactor = 0.f;

What is “Depth Bias Enable”?

Depth Bias Enable allows scaling and translation of the Z-depth values as they come through the rasterizer to avoid Z-fighting.

Z-fighting

Page 19: The Graphics Pipeline - College of Engineeringweb.engr.oregonstate.edu/.../GraphicsPipeline.1pp.pdf · 2019-09-17 · mjb –September 4, 2019 Computer Graphics 1 GraphicsPipeline.pptx

mjb – September 4, 2019Computer Graphics

19

VkPipelineMultisampleStateCreateInfo vpmsci;vpmsci.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;vpmsci.pNext = nullptr;vpmsci.flags = 0;vpmsci.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;vpmsci.sampleShadingEnable = VK_FALSE;vpmsci.minSampleShading = 0;vpmsci.pSampleMask = (VkSampleMask *)nullptr;vpmsci.alphaToCoverageEnable = VK_FALSE;vpmsci.alphaToOneEnable = VK_FALSE;

Declare information about how the multisampling will take place

MultiSampling State

Page 20: The Graphics Pipeline - College of Engineeringweb.engr.oregonstate.edu/.../GraphicsPipeline.1pp.pdf · 2019-09-17 · mjb –September 4, 2019 Computer Graphics 1 GraphicsPipeline.pptx

mjb – September 4, 2019Computer Graphics

20

VkPipelineColorBlendAttachmentState vpcbas;vpcbas.blendEnable = VK_FALSE;vpcbas.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_COLOR;vpcbas.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR;vpcbas.colorBlendOp = VK_BLEND_OP_ADD;vpcbas.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONEvpcbas.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;vpcbas.alphaBlendOp = VK_BLEND_OP_ADD;vpcbas.colorWriteMask = VK_COLOR_COMPONENT_R_BIT

| VK_COLOR_COMPONENT_G_BIT| VK_COLOR_COMPONENT_B_BIT| VK_COLOR_COMPONENT_A_BIT;

Color Blending State for each Color Attachment

Create an array with one of these for each color buffer attachment. Each color buffer attachment can use different blending operations.

This controls blending between the output of each color attachment and its image memory.

Page 21: The Graphics Pipeline - College of Engineeringweb.engr.oregonstate.edu/.../GraphicsPipeline.1pp.pdf · 2019-09-17 · mjb –September 4, 2019 Computer Graphics 1 GraphicsPipeline.pptx

mjb – September 4, 2019Computer Graphics

21

VkPipelineColorBlendStateCreateInfo vpcbsci;vpcbsci.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;vpcbsci.pNext = nullptr;vpcbsci.flags = 0;vpcbsci.logicOpEnable = VK_FALSE;vpcbsci.logicOp = VK_LOGIC_OP_COPY;

#ifdef CHOICESVK_LOGIC_OP_CLEARVK_LOGIC_OP_ANDVK_LOGIC_OP_AND_REVERSEVK_LOGIC_OP_COPYVK_LOGIC_OP_AND_INVERTEDVK_LOGIC_OP_NO_OPVK_LOGIC_OP_XORVK_LOGIC_OP_ORVK_LOGIC_OP_NORVK_LOGIC_OP_EQUIVALENTVK_LOGIC_OP_INVERTVK_LOGIC_OP_OR_REVERSEVK_LOGIC_OP_COPY_INVERTEDVK_LOGIC_OP_OR_INVERTEDVK_LOGIC_OP_NANDVK_LOGIC_OP_SET#endif

vpcbsci.attachmentCount = 1;vpcbsci.pAttachments = &vpcbas;vpcbsci.blendConstants[0] = 0;vpcbsci.blendConstants[1] = 0;vpcbsci.blendConstants[2] = 0;vpcbsci.blendConstants[3] = 0;

This controls blending between the output of the fragment shader and the input to the color attachments.

Color Blending State for each Color Attachment

Page 22: The Graphics Pipeline - College of Engineeringweb.engr.oregonstate.edu/.../GraphicsPipeline.1pp.pdf · 2019-09-17 · mjb –September 4, 2019 Computer Graphics 1 GraphicsPipeline.pptx

mjb – September 4, 2019Computer Graphics

22

VkDynamicState vds[ ] = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR };#ifdef CHOICESVK_DYNAMIC_STATE_VIEWPORT -- vkCmdSetViewport( )VK_DYNAMIC_STATE_SCISSOR -- vkCmdSetScissor( )VK_DYNAMIC_STATE_LINE_WIDTH -- vkCmdSetLineWidth( )VK_DYNAMIC_STATE_DEPTH_BIAS -- vkCmdSetDepthBias( )VK_DYNAMIC_STATE_BLEND_CONSTANTS -- vkCmdSetBendConstants( )VK_DYNAMIC_STATE_DEPTH_BOUNDS -- vkCmdSetDepthZBounds( )VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK -- vkCmdSetStencilCompareMask( )VK_DYNAMIC_STATE_STENCIL_WRITE_MASK -- vkCmdSetStencilWriteMask( )VK_DYNAMIC_STATE_STENCIL_REFERENCE -- vkCmdSetStencilReferences( )#endif

VkPipelineDynamicStateCreateInfo vpdsci;vpdsci.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;vpdsci.pNext = nullptr;vpdsci.flags = 0;vpdsci.dynamicStateCount = 0; // leave turned off for nowvpdsci.pDynamicStates = vds;

Which Pipeline Variables can be Set Dynamically

Page 23: The Graphics Pipeline - College of Engineeringweb.engr.oregonstate.edu/.../GraphicsPipeline.1pp.pdf · 2019-09-17 · mjb –September 4, 2019 Computer Graphics 1 GraphicsPipeline.pptx

mjb – September 4, 2019Computer Graphics

23VkStencilOpState vsosf; // front

vsosf.depthFailOp = VK_STENCIL_OP_KEEP; // what to do if depth operation failsvsosf.failOp = VK_STENCIL_OP_KEEP; // what to do if stencil operation failsvsosf.passOp = VK_STENCIL_OP_KEEP; // what to do if stencil operation succeeds

#ifdef CHOICESVK_STENCIL_OP_KEEP -- keep the stencil value as it isVK_STENCIL_OP_ZERO -- set stencil value to 0VK_STENCIL_OP_REPLACE -- replace stencil value with the reference valueVK_STENCIL_OP_INCREMENT_AND_CLAMP -- increment stencil valueVK_STENCIL_OP_DECREMENT_AND_CLAMP -- decrement stencil valueVK_STENCIL_OP_INVERT -- bit-invert stencil valueVK_STENCIL_OP_INCREMENT_AND_WRAP -- increment stencil valueVK_STENCIL_OP_DECREMENT_AND_WRAP -- decrement stencil value#endif

vsosf.compareOp = VK_COMPARE_OP_NEVER;#ifdef CHOICESVK_COMPARE_OP_NEVER -- never succeedsVK_COMPARE_OP_LESS -- succeeds if stencil value is < the reference valueVK_COMPARE_OP_EQUAL -- succeeds if stencil value is == the reference valueVK_COMPARE_OP_LESS_OR_EQUAL -- succeeds if stencil value is <= the reference valueVK_COMPARE_OP_GREATER -- succeeds if stencil value is > the reference valueVK_COMPARE_OP_NOT_EQUAL -- succeeds if stencil value is != the reference valueVK_COMPARE_OP_GREATER_OR_EQUAL -- succeeds if stencil value is >= the reference valueVK_COMPARE_OP_ALWAYS -- always succeeds#endif

vsosf.compareMask = ~0;vsosf.writeMask = ~0;vsosf.reference = 0;

VkStencilOpState vsosb; // backvsosb.depthFailOp = VK_STENCIL_OP_KEEP;vsosb.failOp = VK_STENCIL_OP_KEEP;vsosb.passOp = VK_STENCIL_OP_KEEP;vsosb.compareOp = VK_COMPARE_OP_NEVER;vsosb.compareMask = ~0;vsosb.writeMask = ~0;vsosb.reference = 0;

Stencil Operations for Front and Back Faces

Page 24: The Graphics Pipeline - College of Engineeringweb.engr.oregonstate.edu/.../GraphicsPipeline.1pp.pdf · 2019-09-17 · mjb –September 4, 2019 Computer Graphics 1 GraphicsPipeline.pptx

mjb – September 4, 2019Computer Graphics

24Uses for Stencil Operations

Magic Lenses

Polygon edges without Z-fighting

Page 25: The Graphics Pipeline - College of Engineeringweb.engr.oregonstate.edu/.../GraphicsPipeline.1pp.pdf · 2019-09-17 · mjb –September 4, 2019 Computer Graphics 1 GraphicsPipeline.pptx

mjb – September 4, 2019Computer Graphics

25

VkPipelineDepthStencilStateCreateInfo vpdssci;vpdssci.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;vpdssci.pNext = nullptr;vpdssci.flags = 0;vpdssci.depthTestEnable = VK_TRUE;vpdssci.depthWriteEnable = VK_TRUE;vpdssci.depthCompareOp = VK_COMPARE_OP_LESS;

VK_COMPARE_OP_NEVER -- never succeedsVK_COMPARE_OP_LESS -- succeeds if new depth value is < the existing valueVK_COMPARE_OP_EQUAL -- succeeds if new depth value is == the existing valueVK_COMPARE_OP_LESS_OR_EQUAL -- succeeds if new depth value is <= the existing valueVK_COMPARE_OP_GREATER -- succeeds if new depth value is > the existing valueVK_COMPARE_OP_NOT_EQUAL -- succeeds if new depth value is != the existing valueVK_COMPARE_OP_GREATER_OR_EQUAL -- succeeds if new depth value is >= the existing valueVK_COMPARE_OP_ALWAYS -- always succeeds#endif

vpdssci.depthBoundsTestEnable = VK_FALSE;vpdssci.front = vsosf;vpdssci.back = vsosb;vpdssci.minDepthBounds = 0.;vpdssci.maxDepthBounds = 1.;vpdssci.stencilTestEnable = VK_FALSE;

Operations for Depth Values

Page 26: The Graphics Pipeline - College of Engineeringweb.engr.oregonstate.edu/.../GraphicsPipeline.1pp.pdf · 2019-09-17 · mjb –September 4, 2019 Computer Graphics 1 GraphicsPipeline.pptx

mjb – September 4, 2019Computer Graphics

26

VkGraphicsPipelineCreateInfo vgpci;vgpci.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;vgpci.pNext = nullptr;vgpci.flags = 0;

#ifdef CHOICESVK_PIPELINE_CREATE_DISABLE_OPTIMIZATION_BITVK_PIPELINE_CREATE_ALLOW_DERIVATIVES_BITVK_PIPELINE_CREATE_DERIVATIVE_BIT#endif

vgpci.stageCount = 2; // number of stages in this pipelinevgpci.pStages = vpssci;vgpci.pVertexInputState = &vpvisci;vgpci.pInputAssemblyState = &vpiasci;vgpci.pTessellationState = (VkPipelineTessellationStateCreateInfo *)nullptr;vgpci.pViewportState = &vpvsci;vgpci.pRasterizationState = &vprsci;vgpci.pMultisampleState = &vpmsci;vgpci.pDepthStencilState = &vpdssci;vgpci.pColorBlendState = &vpcbsci;vgpci.pDynamicState = &vpdsci;vgpci.layout = IN GraphicsPipelineLayout;vgpci.renderPass = IN RenderPass;vgpci.subpass = 0; // subpass numbervgpci.basePipelineHandle = (VkPipeline) VK_NULL_HANDLE;vgpci.basePipelineIndex = 0;

result = vkCreateGraphicsPipelines( LogicalDevice, VK_NULL_HANDLE, 1, IN &vgpci,PALLOCATOR, OUT pGraphicsPipeline );

return result;}

Group all of the individual state information and create the pipeline

Putting it all Together! (finally…)

Page 27: The Graphics Pipeline - College of Engineeringweb.engr.oregonstate.edu/.../GraphicsPipeline.1pp.pdf · 2019-09-17 · mjb –September 4, 2019 Computer Graphics 1 GraphicsPipeline.pptx

mjb – September 4, 2019Computer Graphics

27Later on, we will Bind the Graphics Pipeline to theCommand Buffer when Drawing

vkCmdBindPipeline( CommandBuffers[nextImageIndex],VK_PIPELINE_BIND_POINT_GRAPHICS, GraphicsPipeline );