Lecture 5:
Vulkan, Part 4 (Commands and Pipelines)
COMP5822M – High Perf. Graphics
Copyright By PowCoder代写 加微信 powcoder
– Online support today @ 15:00 – Zoom
– Linkwillfollow
COMP5822M – High Perf. Graphics
– Heaps and Types
– VkDeviceMemory – Staging & mapping
– Buffers – Images
COMP5822M – High Perf. Graphics
– Commands
– GraphicsPipeline
COMP5822M – High Perf. Graphics
– Soundseasyenough…
COMP5822M – High Perf. Graphics
Queues and queue families
– Device has one or more queue families
– Each family accepts a subset of commands
– Types:VkQueueFlags/VK_QUEUE_{type}_BIT
– GRAPHICS
– TRANSFER
– SPARSE_BINDING, VIDEO_DECODE, VIDEO_ENCODE
– Commandsareinoneofthesecategories
COMP5822M – High Perf. Graphics
Queues and queue families
– Specialrule:GRAPHICSandCOMPUTE
– Always support TRANSFER commands
– VK_QUEUE_TRANSFER_BIT not required
– But most set it anyway
– Additionally:if∃queuewithGRAPHICS
– Must have one with both GRAPHICS|COMPUTE
COMP5822M – High Perf. Graphics
Queues and queue families
– Each queue family has a max. queue count – Determines max number of VkQueue objects
– Do we need more than one?
– Submit work to VkQueue from one thread
– Multiple threads: VkQueue/thread?
– Or CPU thread synchronization
COMP5822M – High Perf. Graphics
Example: RTX 2070 (Windows)
NVIDIA GeForce RTX 2070: 3 queue families
0: 16 queues with GRAPHICS COMPUTE TRANSFER SPARSE 1: 2 queues with TRANSFER SPARSE
2: 8 queues with COMPUTE TRANSFER SPARSE
COMP5822M – High Perf. Graphics
Queues and queue families
– Might not look the same on your machine
– Family0:GRAPHICSqueue
– Accepts “all” types of commands
– GRAPHICS, COMPUTE and TRANSFER
– What about the others?
COMP5822M – High Perf. Graphics
Other queues
– NVIDIA/RTX2070specific!
– But others will have similar purposes
– Family1:TRANSFERonly
– Can only do transfers = copy
– Probably transfer hardware
– Family2:COMPUTE“only”
– Probably “async compute”
COMP5822M – High Perf. Graphics
Queue creation
– Duringdevicecreation,requestqueues
– VkQueue handles
– Cannot be created later
COMP5822M – High Perf. Graphics
– We submit commands to queues – Command buffers to be exact.
COMP5822M – High Perf. Graphics
– We submit commands to queues – Command buffers to be exact.
VkCommandBuffer
• vkCmdCopyBuffer()
• vkCmdBeginRenderpass() • …
VkCommandPool
vkAllocateCommandBuffers()
COMP5822M – High Perf. Graphics
VkCommandBuffer •…
vkQueueSubmit()
VkCommandBuffer •…
Command Pool
– VkCommandPool
– vkCreateCommandPool
– VkCommandPoolCreateInfo
– Each pool specific to one queue family!
– Controlscommandbufferbehaviour
– Allocate command buffer from pool – Buffers pull memory from pool
COMP5822M – High Perf. Graphics
Command Pool
– Lots of subtle options – Read the docs!
– Default: cannot reset individual command
– Must use vkResetCommandPool()
– Resets all command buffers from that pool
– But: VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT
COMP5822M – High Perf. Graphics
Command Buffers
– VkCommandBuffer
– vkAllocateCommandBuffers()
– vkFreeCommandBuffers()
– vkResetCommandBuffer() (optional)
– Note: do not allocate each frame
– Rather reuse command buffers
– Reset to record new commands
COMP5822M – High Perf. Graphics
Command Buffers
– Differentstates: – Initial
– Recording – Executable – Pending
– (Invalid)
COMP5822M – High Perf. Graphics
Command buffers
– Differentstates: – Initial
– Recording – Executable – Pending
– (Invalid)
COMP5822M – High Perf. Graphics
https://www.khronos.org/registry/vulkan/specs/1.3- extensions/html/vkspec.html#commandbuffers-lifecycle
Recording commands
– Begin recording … record … end recording
– vkBeginCommandBuffer()
Will implicitly reset command buffer
– vkEndCommandBuffer()
– May not be in recording or pending state
– I.e., not have started recording or have been
– Assumes CREATE_RESET_COMMAND_BUFFER_BIT
COMP5822M – High Perf. Graphics
Recording commands
– In recording state:
– Call any vkCmd*() function
– First argument is command buffer
– This is the only time you can call these!
COMP5822M – High Perf. Graphics
Submitting commands
– vkQueueSubmit()
– VkQueueSubmitInfo – VkQueue
– Submit on or more command buffers – Synchronization…
– Important:submit!=executenow – Will run at some point…
COMP5822M – High Perf. Graphics
– We’veseensome:
– vkCmdCopyBuffer()
– vkCmdCopyBufferToImage() – vkCmdUpdateBuffer()
– TheseareTRANSFERcommands – Canuse“as-is”
COMP5822M – High Perf. Graphics
– WhataboutvkCmdDraw()? – Needs a bunch of setup
– Must be in renderpass
– New: vkCmdBeginRendering() – same thing
– Must have graphics pipeline
– Must have other resources ready
COMP5822M – High Perf. Graphics
Renderpasses, Pipelines and Related
COMP5822M – High Perf. Graphics
– Soundseasyenough…
COMP5822M – High Perf. Graphics
– Soundseasyenough…
Vulkan 1.3: (optionally)
COMP5822M – High Perf. Graphics
– Soundseasyenough…
COMP5822M – High Perf. Graphics
Renderpasses, Pipelines and Related
– Pipeline: “shader program” + state – Rest:describinginputandoutput
COMP5822M – High Perf. Graphics
– I’m going to explain them in an order that makes sense (to me at least)
– This is not the order in which the objects need to be created.
COMP5822M – High Perf. Graphics
Framebuffer
– Output(*)fromrendering
– ZeroormoreVkImageViews(“attachments”)
– Color attachments
– Depth attachments
– Stencil attachments
– (FB not distinguish between these)
– (*) Transient attachments; input attachments… – Mostly for tiled architectures
COMP5822M – High Perf. Graphics
Framebuffer
– MoregeneralthanOpenGL
– E.g. multiple depth attachments
– But this is uncommon (?)
– Just a collection of images
COMP5822M – High Perf. Graphics
Framebuffer – Examples
– 2D rendering: single RGBA image
– “Normal”rendering:RGBA+depthbuffer – Shadowmaps:depthbufferonly
– Deferredrendering:
– Multiple color attachments
– Albedo, normals, specularity, …
– One depth buffer
COMP5822M – High Perf. Graphics
Position(Z)
COMP5822M – High Perf. Graphics
Src: , Many Light Management…
Renderpass
– WasspecifictoVulkan
– Direct3D now has them (but limited?)
– Zeroormoreattachments – Format
– Load & store ops
– Initial & final layout
– Oneormoresubpasses
COMP5822M – High Perf. Graphics
Renderpass attachments:
– Correspondtoframebufferimages – But only format etc, no ImageView
– LOAD_OP_LOAD
– LOAD_OP_CLEAR
– LOAD_OP_DONT_CARE
– Storeop:
– STORE_OP_STORE
– STORE_OP_DONT_CARE
COMP5822M – High Perf. Graphics
Renderpass attachments:
– Initial layout: expect image in this layout
– Final layout: transition image to this layout
– When renderpass finishes
COMP5822M – High Perf. Graphics
Renderpass subpasses:
– Renderingtakesplaceinasubpass – We have a single subpass
– Subpassspecifies
– Color attachments
– Depth/stencil attachment (single)
– Resolve attachments
– (Input attachments, “preserve attachments”)
– Layouts for each attachment!
COMP5822M – High Perf. Graphics
Renderpass subpasses:
– Subpassdependencies
– Synchronization, so get back to it later
COMP5822M – High Perf. Graphics
Renderpass & image layouts:
– Startofrenderpass:
– Images must be in the initial layout
– Eachsubpass:
– Images are transitioned to specified layout
– Endofrenderpass:
– Images are transitioned to the final layout
COMP5822M – High Perf. Graphics
Renderpasses example
clear color and depth buffer; render 3D scene;
render 2D UI;
show results;
COMP5822M – High Perf. Graphics
Renderpass 1:
– Color Buffer:
LOAD_OP_CLEAR, STORE_OP_STORE, LAYOUT_UNDEFINED -> LAYOUT_COLOR_ATTACHMENT_OPTIMAL -> LAYOUT_COLOR_ATTACHMENT_OPTIMAL
– Depth Buffer:
LOAD_OP_CLEAR, STORE_OP_DONT_CARE, LAYOUT_UNDEFINED -> LAYOUT_DEPTH_STENCIL_ATTACHMENT_O… LAYOUT_DEPTH_STENCIL_ATTACHMENT_O…
Renderpasses example
show results;
clear color and depth buffer; render 3D scene;
render 2D UI;
Renderpass 2:
– Color Buffer:
LOAD_OP_LOAD, STORE_OP_STORE, LAYOUT_COLOR_ATTACHMENT_OPTIMAL -> LAYOUT_COLOR_ATTACHMENT_OPTIMAL -> LAYOUT_COLOR_PRESENT_SRC_KHR
– Depth Buffer not used (2D)
COMP5822M – High Perf. Graphics
Renderpasses
– Mainly useful for tiled architectures
– Frequently have RP with one subpass
– In this case: dynamic rendering (1.3)
– Don’t need renderpass or framebuffer
– But specify similar things in commands
COMP5822M – High Perf. Graphics
Graphics Pipeline
COMP5822M – High Perf. Graphics
COMP5822M – High Perf. Graphics
Can access uniform data in Fragment Shader!
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
– 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
– Warning:pCodeisuint32_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
– Support improving, may not always have newest features
– Instead of going to .spv files and loading them
– Can output “.spvc” files
– Contain 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
Pipeline Layout and Descriptor Sets
– Shaders expect certain inputs
– Per-vertex data
– “Uniform data”
– Uniform data: same for all vertices in a draw call
– Uniform buffers, shader storage buffers – Textures
COMP5822M – High Perf. Graphics
Pipeline Layout and Descriptor Sets
– Descriptor: a single uniform buffer, texture, …
– VkDescriptorSet: group of descriptors
– Described by a VkDescriptorSetLayout
– Useful to group from similar scope
– E.g. per-frame data in set A, per-model data in set B, …
– VkPipelineLayout: collection of VkDescriptorSetLayouts used by a pipeline
COMP5822M – High Perf. Graphics
Pipeline Layout and Descriptor Sets
// Vertex Shader
layout( set = 0, binding = 0, std140 ) uniform UWorld {
mat4 model2Clip;
mat3 normal2World; } uWorld;
// Fragment shader
layout( set = 1, binding = 0, std140 ) uniform UModel {
vec3 ambientColor; vec3 diffuseColor; float baseAlpha;
layout( set = 1, binding = 1 ) uniform sampler2D uTexDiffuse;
COMP5822M – High Perf. Graphics
Pipeline Layout and Descriptor Sets
// Vertex Shader
layout( set = 0, binding = 0, std140 ) uniform UWorld {
mat4 model2Clip;
mat3 normal2World; } uWorld;
// Fragment shader
Uniform Block/ Buffer
layout( set = 1, binding = 0, std140 ) uniform UModel {
vec3 ambientColor; vec3 diffuseColor; float baseAlpha;
layout( set = 1, binding = 1 ) uniform sampler2D uTexDiffuse;
COMP5822M – High Perf. Graphics
Uniform Block/ Buffer
// Fragment shader
Descriptor Set 0
Pipeline Layout and Descriptor Sets
// Vertex Shader
layout( set = 0, binding = 0, std140 ) uniform UWorld {
mat4 model2Clip;
mat3 normal2World; } uWorld;
Uniform Block/ Buffer
layout( set = 1, binding = 0, std140 ) uniform UModel {
vec3 ambientColor; vec3 diffuseColor; float baseAlpha;
Descriptor Set 1
set = 1, binding = 1 ) uniform sampler2D uTexDiffuse;
Uniform Block/ Buffer
COMP5822M – High Perf. Graphics
Descriptor Set 0
Pipeline Layout and Descriptor Sets
// Vertex Shader
layout( binding = 0, std140 ) uniform UWorld {
mat4 model2Clip;
mat3 normal2World; } uWorld;
Element 0 in Descriptor Set
Element 0 in Descriptor Set
layout( binding = 0, std140 ) uniform UModel {
vec3 ambientColor; vec3 diffuseColor; float baseAlpha;
Descriptor Set 1
binding = 1
Element 1 in
) uniform sampler2D uTexDiffuse; Descriptor Set
// Fragment shader
Uniform Block/ Buffer
Uniform Block/ Buffer
COMP5822M – High Perf. Graphics
Thank you for your attention.
COMP5822M – High Perf. Graphics
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com