CS代考 COMP5822M – High Perf. Graphics

Lecture 7:
Vulkan, Part 6 (Synchronization)
COMP5822M – High Perf. Graphics

Copyright By PowCoder代写 加微信 powcoder

– Vulkanresources
– Vulkancommands
– Graphicspipeline – Render pass
– Descriptors
COMP5822M – High Perf. Graphics

– Basicresources:VkBuffer,VkImage – VkDeviceMemory
– VkPipeline: “how to draw”
– Inputs (uniform/descriptor, per-vertex)
– VkRenderPass: “where to draw” – VkFramebuffer
– Multiple pipelines
COMP5822M – High Perf. Graphics

– Synchronization
– Barriers (&Events, endencies) – Fences
– Semaphores
– (Timeline semaphores, >= Vulkan 1.2)
COMP5822M – High Perf. Graphics

– Soundseasyenough…
COMP5822M – High Perf. Graphics

Synchronization2
– Vulkan 1.3 introduces a new set of synchronization functions.
https://github.com/KhronosGroup/Vulkan- Guide/blob/master/chapters/extensions/VK_KHR_synchronization2. adoc
– Do not have personal experience with
these yet, plus we have people stuck on
= Vulkan 1.2)
COMP5822M – High Perf. Graphics

– Split command buffer into “before” and “after”
– vkCmdPipelineBarrier()
– Also used to
– Transition image layouts
– Transfer ownership between queues
COMP5822M – High Perf. Graphics

– Restricted to one queue – Not across queues
COMP5822M – High Perf. Graphics

– ExecutionbarriervsMemorybarrier – Execution:ordercomputations
– Memory:ensurethatresultsare“visible”
– Pureexecutionbarrier
– Execution+memorybarrier(s)
– Pure execution less useful/common of the two
COMP5822M – High Perf. Graphics

Execution barriers
– Barrierbetweentwocommands
– CommandA, {barrier}, CommandB
– Applies to all commands before & after!
– Commands execute in a pipeline
– Barrier:
– Ensure that StageX of CommandA has
finished before starting StageY of CommandB
COMP5822M – High Perf. Graphics

– Different processing stages in the processing pipeline
COMP5822M – High Perf. Graphics

VERTEX_SHADER
FRAGMENT_SHADER
COMP5822M – High Perf. Graphics

VERTEX_INPUT VERTEX_SHADER
FRAGMENT_SHADER
COLOR_ATTACH._OUTPUT
COMP5822M – High Perf. Graphics

TOP_OF_PIPE VERTEX_INPUT
VERTEX_SHADER …
FRAGMENT_SHADER
COLOR_ATTACH._OUTPUT
BOTTOM_OF_PIPE
COMP5822M – High Perf. Graphics

TOP_OF_PIPE VERTEX_INPUT
VERTEX_SHADER …
FRAGMENT_SHADER
COMPUTE_SHADER TRANSFER
COLOR_ATTACH._OUTPUT
BOTTOM_OF_PIPE
COMP5822M – High Perf. Graphics

– srcStageMask: wait until this stage has finished
– dstStageMask: do not begin this stage until that point
COMP5822M – High Perf. Graphics

– srcStageMask: wait until this stage has finished
– dstStageMask: do not begin this stage until that point
– Example:
VK_PIPELINE_STAGE_TRANSFER_BIT, // srcStageMask VK_PIPELINE_STAGE_VERTEX_INPUT_BIT, // dstStageMask
– “Do not begin next VERTEX_INPUT until previous TRANSFER has finished.”
COMP5822M – High Perf. Graphics

Memory barriers
– Ensureresultsfromonecommandare visible to following commands
– Incoherentmemorymodel – Contrast to x86 & x86_64
COMP5822M – High Perf. Graphics

Memory barriers
– GPU:manyexecutionunits(EUs)
– Each EU has private L1$ (and perhaps L2$)
– EU1’s L1$ is not visible to EU2 and vice versa
– Must flush EU1’s L1$ to RAM for EU2 to see it
– Mentalmodel:manydifferentmemories
– Each holds different data: no problem
– Shared data: need to “synch up”
COMP5822M – High Perf. Graphics

Coherent vs Incoherent Memory
Cache Cache
COMP5822M – High Perf. Graphics

Coherent vs Incoherent Memory
Cache Cache
COMP5822M – High Perf. Graphics

Coherent vs Incoherent Memory
…✓ COMP5822M – High Perf. Graphics

Coherent vs Incoherent Memory
COMP5822M – High Perf. Graphics

COMP5822M – High Perf. Graphics

COMP5822M – High Perf. Graphics

COMP5822M – High Perf. Graphics

COMP5822M – High Perf. Graphics
Overheads keeping caches in synch.

Incoherent
COMP5822M – High Perf. Graphics

Incoherent
COMP5822M – High Perf. Graphics

Incoherent
COMP5822M – High Perf. Graphics
(Unless you really really really know what you’re doing. )

Memory barriers
– Specifyaccessmask
– Source access: how memory was used before
– Destination access: how memory will be
accessed after
– Memorybarriertypes
– Global: All memory objects
– Buffer: memory bound to VkBuffer object
– Image: memory bound to VkImage object
COMP5822M – High Perf. Graphics

Barriers Example
// Upload data to GPU vkCmdCopyToBuffer()
// Use data on GPU vkCmdDraw()
COMP5822M – High Perf. Graphics

Barriers Example
// Upload data to GPU vkCmdCopyToBuffer()
VkBufferMemoryBarrier barrier;
barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; barrier.dstAccessMask = VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT; //…
vkCmdPipelineBarrier(
VK_PIPELINE_STAGE_TRANSFER_BIT, // srcStageMask VK_PIPELINE_STAGE_VERTEX_INPUT_BIT, // dstStageMask 1, &barrier, …
// Use data on GPU vkCmdDraw()
COMP5822M – High Perf. Graphics

“How and where (stage) we changed the memory.”
Barriers Example
// Upload data to GPU vkCmdCopyToBuffer()
VkBufferMemoryBarrier barrier;
barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; barrier.dstAccessMask = VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT; //…
vkCmdPipelineBarrier(
VK_PIPELINE_STAGE_TRANSFER_BIT, // srcStageMask VK_PIPELINE_STAGE_VERTEX_INPUT_BIT, // dstStageMask 1, &barrier, …
// Use data on GPU vkCmdDraw()
COMP5822M – High Perf. Graphics
“How and where we intend to use the changed memory.”

“How and where (stage) we changed the memory.”
Barriers Example
// Upload data to GPU vkCmdCopyToBuffer()
VkBufferMemoryBarrier barrier;
barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; barrier.dstAccessMask = VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT; //…
vkCmdPipelineBarrier(
VK_PIPELINE_STAGE_TRANSFER_BIT, // srcStageMask VK_PIPELINE_STAGE_VERTEX_INPUT_BIT, // dstStageMask 1, &barrier, …
// Use data on GPU vkCmdDraw()
COMP5822M – High Perf. Graphics
Changed by transfer (copy). Will be used as vertex attributes (per- vertex data).
“How and where we intend to use the changed memory.”

Barrier Extras
– Imagememorybarriersalso
transition image layouts
– oldLayout: current layout of image – newLayout: desired layout
– Image/BufferBarrierscanbe
applied to only a part
– E.g., buffer range or “sub image” (mip level)
COMP5822M – High Perf. Graphics

Barrier Extras
– Buffer/Imagememorybarriers
– Transfer ownership between queues
– I.e., for SHARING_MODE_EXCLUSIVE
– Twobarriers
– Barrier 1: release from queue A
– Barrier 2: acquire in queue B
– Note: needs additional synchronization!
COMP5822M – High Perf. Graphics

– Barrier: Split commands into two sets – Before and after
– Events: Split into three sets – Before
– {Unaffected commands} – After
– Have not used events much…
– Can’t be used across queues…
COMP5822M – High Perf. Graphics

– VkEvent object
– vkCmdSetEvent(), vkCmdResetEvent()
– vkCmdWaitEvents()
COMP5822M – High Perf. Graphics

Barriers & Events
– Orderstuff
– Inside a command buffer
– Across command buffers submitted
to the same queue
COMP5822M – High Perf. Graphics

Barriers & Events
– Orderstuff
– Inside a command buffer
– Across command buffers submitted
to the same queue
– Renderpass:Subpassdependency
– Like an image memory barrier
– For the subpass’s attachments
– SUBPASS_EXTERNAL for barrier before
or after render pass.
COMP5822M – High Perf. Graphics

– Synchronize from device/GPU to host/CPU
– vkWaitForFences: wait for fence to be signaled
– E.g. set of commands completed
– Exercise 2
– vkQueueSubmit() – all submitted commands
– Not for individual commands
– vkResetFences() – reset fences from signaled state
COMP5822M – High Perf. Graphics

Semaphores
– Synchronize device/GPU to device/GPU
– VkSemaphore
– VkSubmitInfo / vkQueueSubmit
– Implicit memory barrier
– VkSubmitInfo:
– Set of semaphores to wait on before execution
– Set of semaphores to signal after execution
– Specify stage that should wait
– Waiting on a semaphore resets it automatically
COMP5822M – High Perf. Graphics

Semaphores
– Example use case (Semaphore)
vkBeginCommandBuffer()
// record rendering commands (…, vkDraw*(), …)
vkEndCommandBuffer()
vkQueueSubmit(); // with: signal semaphore
vkQueuePresentKHR(); // with: wait for semaphore
COMP5822M – High Perf. Graphics

Semaphores
– Example use case (Semaphore)
vkBeginCommandBuffer()
// record rendering commands (…, vkDraw*(), …)
vkEndCommandBuffer()
vkQueueSubmit(); // with: signal semaphore
vkQueuePresentKHR(); // with: wait for semaphore
COMP5822M – High Perf. Graphics
Don’t show image before render is complete!

Timeline Semaphores
– >=Vulkan1.2
– Combine aspects of fence and semaphore
– Won’t go into details
– Read up in the docs – should be somewhat
understandable now
– Doesn’t interact well with WSI
COMP5822M – High Perf. Graphics

Synchronization summary
– Barriers/Events/Subpass dependency – Dependencies between commands
– Device to host: E.g. wait for device to finish
– Semaphore
– Between command buffers
– Across queues, presentation
COMP5822M – High Perf. Graphics

Synchronization summary
– Understood to be one of the most confusing aspects in Vulkan
– Beverycarefulwhenreadingaboutit
– Make sure you rely on good sources
– Various places on the internet get this wrong
– (Verify what I’ve said too!)
COMP5822M – High Perf. Graphics

Synchronization summary
– See links in Minerva.
COMP5822M – High Perf. Graphics

“ ” / vkconfig.exe (Vulkan SDK)
COMP5822M – High Perf. Graphics

Thank you for your attention.
COMP5822M – High Perf. Graphics

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com