Search code examples
c++graphicsvulkan

Vulkan vkInvalidateMappedMemoryRanges explanation


From the Vulkan doc it says "vkInvalidateMappedMemoryRanges guarantees that device writes to the memory ranges described by pMemoryRanges, which have been made available to the host memory domain using the VK_ACCESS_HOST_WRITE_BIT and VK_ACCESS_HOST_READ_BIT access types, are made visible to the host."

I wonder what command uses VK_ACCESS_HOST_WRITE_BIT and VK_ACCESS_HOST_READ_BIT to do the memory domain operation to make the write to the device domain available also available to the host domain? Or does it mean vkInvalidateMappedMemoryRanges will do the device domain to host domain operation itself? I know vkQueueSubmit will internally do a host domain to device domain operation after we do a vkflushmappedmemoryranges , but what command does the opposite?


Solution

  • As with anything else in Vulkan, you have to establish a barrier between the write and the read to make the write available to the reader. The fact that the read is on the host does not change this fact; it merely changes how you have to go about creating that barrier.

    You need to use a memory barrier after the device writes to provide host availability through the VK_ACCESS_HOST_READ_BIT. This is typically done by using a pipeline barrier with VK_PIPELINE_STAGE_HOST_BIT as the destination scope.

    However, the CPU cannot act on any of this until it has synchronized in some way with that barrier. So it needs to wait on an event set after the barrier or the fence submitted with the whole queue submit operation. Note that the event itself cannot itself contain the barrier; the barrier must happen before the event is set.