Search code examples
cachingmemory-managementvulkaninvalidation

VkInvalidateMappedMemoryRanges Usage


I’m having trouble to understand the usage of vkInvalidateMappedMemoryRanges.

The documentation says it invalidates caches lines of host to read data from main memory. What I understand is if I have mapped memory which is updated by GPU, I don’t have to call vkInvalidateMappedMemoryRanges at my first access to read from CPU side because my cache won’t has any information about data, but at my second time access I have to call it to clear old datas from my cache?

In addition, what happens if my memory is just HOST_VISIBLE not HOST_CACHED? Which cache will vkInvalidateMappedMemoryRanges try to invalidate? (Also same question for vkFlushMappedMemoryRange)


Solution

  • I don’t have to call vkInvalidateMappedMemoryRanges at my first access to read from CPU side because my cache won’t has any information about data

    Incorrect. The Vulkan memory model doesn't care about "caches" and the like. Invalidating mapped ranges makes device writes visible to the host. Without this, or coherent memory, those writes aren't visible to the host. It doesn't matter whether it's your first access or your 20th; if it's not visible, the host can't see them.

    "Documentation" may refer to caches and the like, but defined behavior is based on the Vulkan memory model and availability/visibility operations. And there's no special case for these operations involving the first time the device writes to memory.