Lecture 6:
Vulkan, Part 5 (Pipelines)
COMP5822M – High Perf. Graphics
Copyright By PowCoder代写 加微信 powcoder
Last time:
– Commands
– Pipeline intro – Framebuffer – Renderpass
COMP5822M – High Perf. Graphics
– Soundseasyenough…
COMP5822M – High Perf. Graphics
– Descriptors
– Graphicspipeline – Shaders
COMP5822M – High Perf. Graphics
Graphics Pipeline
COMP5822M – High Perf. Graphics
Graphics Pipeline
“Uniform” Input
– Uniform buffers
– Images / Textures
– Storage buffers
– Push constants
Vertex Processing
– Input assembly (fetch data)
– Vertex shader
– Tessellation shaders
– Geometry shader
Primitive assembly & Rasterization
Fragment Processing
– Early fragment ops
– Fragment shader
– Late fragment ops – Blending
“Varying” Inputs
– Vertex buffers
– Index buffer
Framebuffer – Images
COMP5822M – High Perf. Graphics
Graphics Pipeline
“Uniform” Input
“Varying” Inputs
Buffers and Images
– Push constants
Vertex Processing
– Input assembly (fetch data)
Shader Modules
– Vertex shader
– Tessellation shaders
– Geometry shader
Primitive assembly & Rasterization
Fragment Processing
– Early fragment ops
– Fragment shader
– Late fragment ops – Blending
Framebuffer and Images
COMP5822M – High Perf. Graphics
– Uniform buffers
– Images / Textures
– Storage buffers
Framebuffer – Images
– Vertex buffers
– Index buffer
Graphics Pipeline
Buffers and Images
“Uniform” Input
– Uniform buffers
– Images / Textures
– Storage buffers
– Push constants
Descriptors
“Varying” Inputs
– Vertex buffers
– Index buffer
Vertex Processing
– Input assembly (fetch data)
Shader Modules
– Vertex shader
– Tessellation shaders
– Geometry shader
Primitive assembly & Rasterization
Fragment Processing
– Early fragment ops
– Fragment shader
– Late fragment ops – Blending
Framebuffer – Images
Framebuffer and Images
COMP5822M – High Perf. Graphics
Graphics pipeline Renderpass (Subpass)
Vertex Processing – Mesh Shaders Vertex Processing
“Varying” Inputs
– Vertex buffers
– Index buffer
– Tessellation shaders
– Geometry shader
– Task shader (optional)
– Input assembly (fetch data)
– Mesh generation
– Vertex shader – Mesh shader
Primitive assembly & Rasterization
Fragment Processing
– Early fragment ops
– Fragment shader
– Late fragment ops – Blending
Bonus: Mesh Shaders
“Uniform” Input
– Uniform buffers
– Images / Textures
– Storage buffers
– Push constants
Framebuffer – Images
COMP5822M – High Perf. Graphics
COMP5822M – High Perf. Graphics
COMP5822M – High Perf. Graphics
Buffers and Images!
COMP5822M – High Perf. Graphics
Projective Graphics Pipeline
Buffers and Images!
COMP5822M – High Perf. Graphics
Projective Graphics Pipeline
Buffers and Images!
COMP5822M – High Perf. Graphics
Compute Pipeline
Projective Graphics Pipeline
COMP5822M – High Perf. Graphics
Buffers and Images!
Constants (~128 bytes)
Compute Pipeline
Projective Graphics Pipeline
– VkPipelineobject
– vkCreateGraphicsPipeline()
– VkGraphicsPipelineCreateInfo
– Same as always …
COMP5822M – High Perf. Graphics
Projective Graphics Pipeline
– VkPipelineobject
– vkCreateGraphicsPipeline()
– VkGraphicsPipelineCreateInfo
– Same as always … right?
COMP5822M – High Perf. Graphics
VkGraphicsPipelineCreateInfo
– “Renderpass”
– Shader stages
– Pipeline layout
– Vertex input data
– Input assembly state
– Viewport state
– Rasterization state
– Depth/Stencil state
– Color Blend state
– Dynamic states
COMP5822M – High Perf. Graphics
VkGraphicsPipelineCreateInfo
– “Renderpass”
– Shader stages
– Pipeline layout
– Vertex input data
– Input assembly state
– Viewport state
– Rasterization state
– Depth/Stencil state
– Color Blend state
– Dynamic states
COMP5822M – High Perf. Graphics
VkGraphicsPipelineCreateInfo
– “Renderpass”
– Shader stages
– Pipeline layout
– Vertex input data
– Input assembly state
– Viewport state
– Rasterization state
– Depth/Stencil state
– Color Blend state
– Dynamic states
COMP5822M – High Perf. Graphics
Shader Stages
– Codeforvertexandfragmentshaders – Other stages (optional)
– VulkanacceptsSPIR-V“bytecode” – Binary, list of 32-bit words
– We write in a higher level language
– GLSL (or HLSL)
– Compile to SPIR-V
COMP5822M – High Perf. Graphics
Shader Stages
– Compilers:
– glslc: part of Google’s shaderc
https://github.com/google/shaderc
– glslValidator: GLSL compiler by Khronos
– Both in the Vulkan SDK
– And in the exercises (except for MacOS)
– Compile GLSL code to SPIR-V (*.spv)
– Load*.spvmodulesfromfiles – Or compile into program…
COMP5822M – High Perf. Graphics
Shader Modules
– CreateaVkShaderModuleforeachshader
– Vertex shader: shader module
– Fragment shader: shader module -…
– vkCreateShaderModule(),
– VkShaderModuleCreateInfo
– Gotcha: pCode is uint32_t const*, but codeSize is code size in bytes!
COMP5822M – High Perf. Graphics
Shader Stages
– VkPipelineShaderStageCreateInfo per shader module – Two if you have vertex + fragment shader
– VkShaderStageFlagBits identifies shader stage
(VK_SHADER_STAGE_VERTEX_BIT vs FRAGMENT_BIT)
– pName defines the name of the entry point
– In theory: doesn’t have to be the default “main” (c.f. OpenGL)
– Has been buggy to various degrees, “main” is safe, though
COMP5822M – High Perf. Graphics
Shaders – Alternatives
– HLSL(DirectX’sshaderlanguage)possible
– Can be compiled to SPIR-V
– Some extra annotations? (Have not used it)
– Instead of going to .spv files and loading them
– Can output “.spvc” files
– Contains SPIR-V in a C/C++ array construct (array of std::uint32_t)
– #include to embed SPIR-V code in program
constexpr std::uint32_t spvSimpleVert[] = #include ”simple.vert.spvc”
COMP5822M – High Perf. Graphics
VkGraphicsPipelineCreateInfo
– “Renderpass”
– Shader stages
– Pipeline layout
– Vertex input data
– Input assembly state
– Viewport state
– Rasterization state
– Depth/Stencil state
– Color Blend state
– Dynamic states
COMP5822M – High Perf. Graphics
Uniform data
– Shaders expect certain inputs
– Per-vertex data
– “Uniform data”
– Uniformdata:constants
– Data that is the same for all
vertices and fragments in a draw call
– Uniform buffers, storage buffers, textures,
push constants, …
COMP5822M – High Perf. Graphics
Uniform data
– Shaders expect certain inputs
– Per-vertex data
– “Uniform data”
– Uniformdata:constants
– Data that is the same for all
vertices and fragments in a draw call
– Uniform buffers, storage buffers, textures,
push constants, …
COMP5822M – High Perf. Graphics
Descriptors
– Descriptor: reference to an input
– A texture
– A uniform buffer
– A storage buffer -…
COMP5822M – High Perf. Graphics
Descriptors
– Descriptor: reference to an input
– A texture
– A uniform buffer
– A storage buffer -…
– DescriptorSet
– Group of descriptors
COMP5822M – High Perf. Graphics
Descriptors
– Descriptorset(VkDescriptorSet)
– Refers to specific resources
– “Struct/class instance”
– Descriptor:struct/classmember
– Descriptorsetlayout(VkDescriptorSetLayout)
– Declares type of a descriptor set
– “Struct/class declaration”
COMP5822M – High Perf. Graphics
Pipeline Layout
– Pipelines take multiple descriptor sets
– Declared via a pipeline layout
– Push constants…
– VkPipelineLayout
COMP5822M – High Perf. Graphics
Pipeline Layout and Descriptor Sets
// Vertex and Fragment shader
layout( set = 0, binding = 0, std140 ) uniform UFrame {
mat4 world2Clip;
vec3 worldEyePos; } uFrame;
// Vertex shader only
layout( set = 1, binding = 0, std140 ) uniform UInstance {
mat4 model2World;
mat3 tangent2World;
} uInstance;
// Fragment shader only
layout( set = 1, binding = 1 ) uniform sampler2D uTexDiffuse;
COMP5822M – High Perf. Graphics
Pipeline Layout and Descriptor Sets
// Vertex and Fragment shader
layout( set = 0, binding = 0, std140 ) uniform UFrame {
mat4 world2Clip;
vec3 worldEyePos; } uFrame;
Uniform Block/ Buffer
// Vertex shader only
layout( set = 1, binding = 0, std140 ) uniform UInstance {
mat4 model2World;
mat3 tangent2World;
} uInstance;
Uniform Block/ Buffer
// Fragment shader only
layout( set = 1, binding = 1 ) uniform sampler2D uTexDiffuse;
COMP5822M – High Perf. Graphics
Descriptor Set 0
Pipeline Layout and Descriptor Sets
// Vertex and Fragment shader
layout( set = 0, binding = 0, std140 ) uniform UFrame {
mat4 world2Clip;
vec3 worldEyePos; } uFrame;
// Vertex shader only
layout( set = 1, binding = 0, std140 ) uniform UInstance {
mat4 model2World;
mat3 tangent2World;
} uInstance;
Descriptor Set 1
// Fragment shader only
layout( set = 1, binding = 1 ) uniform sampler2D uTexDiffuse;
Uniform Block/ Buffer
Uniform Block/ Buffer
COMP5822M – High Perf. Graphics
Descriptor Set 0
Pipeline Layout and Descriptor Sets
// Vertex and Fragment shader
layout( set = 0, binding = 0, std140 ) uniform UFrame {
mat4 world2Clip;
vec3 worldEyePos; } uFrame;
Element 0 in Descriptor Set
Element 0 in Descriptor Set
// Vertex shader only
layout( set = 1, binding = 0, std140 ) uniform UInstance {
mat4 model2World;
mat3 tangent2World;
} uInstance;
Descriptor Set 1
Element 1 in Descriptor Set
// Fragment shader only
layout( set = 1, binding = 1 ) uniform sampler2D uTexDiffuse;
Uniform Block/ Buffer
Uniform Block/ Buffer
COMP5822M – High Perf. Graphics
Pipeline Layout and Descriptor Sets
– DescriptorSetA
– Descriptor 0: Uniform block / buffer
– DescriptorSetB
– Descriptor 0: Uniform block / buffer
– Descriptor 1: Texture
– Must say which stages can access each descriptor
COMP5822M – High Perf. Graphics
Pipeline Layout and Descriptor Sets
– CreatetwoVkDescriptorSetLayouts – Describing each Descriptor Set A and B
– Pipeline Layout
– Entry 0: Layout of Descriptor Set A
– Entry 1: Layout of Descriptor Set B
– Can share descriptor set layouts
across multiple pipelines
2021/202-2 Can share pipeline layouts too COMP5822M – High Perf. Graphics
Using Descriptor Sets
– CreateaVkDescriptorSetLayout
– CreateVkDescriptorPool
– Fixed number of descriptor resources
of each type
– E.g., N buffer descriptors,
M texture descriptors, …
– Max number of sets too!
– AllocateVkDescriptorSetfrompool
– vkAllocateDescriptorSets()
COMP5822M – High Perf. Graphics
Using Descriptor Sets
– Write data to Descriptor Set
– “Bind” buffer/texture resources to slots
– E.g., bind given VkBuffer to slot 0
– vkUpdateDescriptorSets() and array of VkWriteDescriptorSet
– Writealldescriptorsinasetatonce
– Can even update multiple sets in one call
COMP5822M – High Perf. Graphics
Using Descriptor Sets
Descriptor Set 0:
• World to Clip Mat. • World camera pos •…
Same for all objects!
COMP5822M – High Perf. Graphics
Using Descriptor Sets
Descriptor Set 1: • Lion Uniforms • Lion Textures
Descriptor Set 0:
• World to Clip Mat. • World camera pos •…
Same for all objects!
Descriptor Set 1:
• Curtain Uniforms • Curtain Textures
COMP5822M – High Perf. Graphics
Using Descriptor Sets
– Example: create all Descriptor Sets upfront – Rendering:
Bind Descriptor Set 0; for each object
Bind Descriptor Set 1 based on object; Draw object;
– Otherstrategiespossible
– Avoidbindingdescriptorsperobject.
– Avoidneedingtoallocate+createalldescriptorsets.
COMP5822M – High Perf. Graphics
VkGraphicsPipelineCreateInfo
– “Renderpass”
– Shader stages
– Pipeline layout
– Vertex input data
– Input assembly state
– Viewport state
– Rasterization state
– Depth/Stencil state
– Color Blend state
– Dynamic states
COMP5822M – High Perf. Graphics
Vertex Input State
– Concept:SimilartoDescriptorSetLayout – What kind of data comes in?
– VkPipelineVertexInputStateCreateInfo
– For each input: format, location, …
– Input rate: one per vertex or one per instance
COMP5822M – High Perf. Graphics
// Vertex Shader Only!
layout( location = 0 ) in vec3 iPosition; layout( location = 1 ) in vec3 iNormal; layout( location = 2 ) in vec2 iTexCoord;
COMP5822M – High Perf. Graphics
// Vertex Shader Only!
layout( location = 0 ) in vec3 iPosition; // VK_FORMAT_R32G32B32_SFLOAT layout( location = 1 ) in vec3 iNormal; // (same)
layout( location = 2 ) in vec2 iTexCoord; // VK_FORMAT_R32G32_SFLOAT
COMP5822M – High Perf. Graphics
Vertex Input State
– A few choices:
– One attribute per VkBuffer (“planar”)
[xyz, xyz, xyz, …, xyz]; [uv, uv, uv, …, uv]; […]
– One VkBuffer for all attributes (“interleaved”) [xyz uv …, xyz uv …, xyz uv …, …, xyz uv …]
– Or a mix thereof?
– Unclear.
– Planar: rendering SM may not require normals
– Interleaved: single large fetch per vertex
– (I tend to go for planar; but interleaved popular.)
COMP5822M – High Perf. Graphics
Vertex Input State
– Exercises:Planar
– Seevulkan-tutorial.comforinterleaved.
COMP5822M – High Perf. Graphics
VkGraphicsPipelineCreateInfo
– “Renderpass”
– Shader stages
– Pipeline layout
– Vertex input data
– Input assembly state
– Viewport state
– Rasterization state
– Depth/Stencil state
– Color Blend state
– Dynamic states
COMP5822M – High Perf. Graphics
VkGraphicsPipelineCreateInfo
– “Renderpass”
– Shader stages
– Pipeline layout
– Vertex input data
– Input assembly state
– Viewport state
– Rasterization state
– Depth/Stencil state
– Color Blend state
– Dynamic states
COMP5822M – High Perf. Graphics
Primitive type (e.g. Triangles) Region of image to render to
Face culling mode, etc. Depth testing, etc.
Blending (~transparency) Runtime pipeline states
VkGraphicsPipelineCreateInfo
– “Renderpass”
– Shader stages
– Pipeline layout
– Vertex input data
– Input assembly state
– Viewport state
– Rasterization state
– Depth/Stencil state
– Color Blend state
– Dynamic states
COMP5822M – High Perf. Graphics
~ OpenGL States
Primitive type (e.g. Triangles) Region of image to render to
Face culling mode, etc. Depth testing, etc.
Blending (~transparency) Runtime pipeline states
VkGraphicsPipelineCreateInfo
– Didn’tinclude:
– Tessellation state
– Multisampling state
– Haven’t used tessellation state
– Multisamplinglaterand/orinexercise/CW.
COMP5822M – High Perf. Graphics
Drawing a Triangle!
vkBeginCommandBuffer() vkCmdBeginRenderpass()
vkCmdBindPipeline() vkCmdBindDescriptorSets(), … vkCmdBindVertexBuffers() vkCmdBindIndexBuffer()
vkCmdDrawIndexed()
vkCmdEndRenderpass() vkEndCommandBuffer() vkQueueSubmit()
COMP5822M – High Perf. Graphics
– Nexttime:Synchronization&Swapchains(?) – Last “pure” Vulkan components
– Exercise1.2
– Create a graphics pipeline
– Draw a triangle
– (Out soonTM)
COMP5822M – High Perf. Graphics
Thank you for your attention.
COMP5822M – High Perf. Graphics
Vertex Processing – Mesh Shaders Vertex Processing
“Varying” Inputs
– Vertex buffers
– Index buffer
– Tessellation shaders
– Geometry shader
– Task shader (optional)
– Input assembly (fetch data)
– Mesh generation
– Vertex shader – Mesh shader
Primitive assembly & Rasterization
Fragment Processing
– Early fragment ops
– Fragment shader
– Late fragment ops – Blending
Bonus: Mesh Shaders
“Uniform” Input
– Uniform buffers
– Images / Textures
– Storage buffers
– Push constants
Framebuffer – Images
COMP5822M – High Perf. Graphics
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com