Skip to content

Add validation for memory coherence in Vulkan flush/invalidate operations #412

Open
@Pikachuxxxx

Description

@Pikachuxxxx

Problem

We currently allow vkFlushMappedMemoryRanges and vkInvalidateMappedMemoryRanges to be called unconditionally. This is incorrect when the memory is HOST_COHERENT, as such operations are unnecessary and can introduce redundant overhead. (Thanks to protocols like MESI!)

Coherent Memory

  • Coherent memory automatically keeps CPU and GPU memory views synchronized.
  • No need to flush or invalidate manually.
  • When the CPU writes to mapped memory, the GPU can immediately see the changes — and vice versa.
  • Ideal for frequent, small updates (e.g., uniform buffers).
  • Identified via VK_MEMORY_PROPERTY_HOST_COHERENT_BIT.

Non-coherent Memory

  • Non-coherent memory requires explicit synchronization between the host and the device.
  • We must manually flush CPU writes using vkFlushMappedMemoryRanges.
  • We must manually invalidate CPU-side caches before reading GPU-written data using vkInvalidateMappedMemoryRanges.

Solution

// TODO:

  • Add logic to track VkMemoryPropertyFlags per allocation.
  • Add Asserts and Logging

Before any flush/invalidate operation:

  • Check whether VK_MEMORY_PROPERTY_HOST_COHERENT_BIT is set.
  • If so, emit a RZ_LOG_WARN and optionally RAZIX_CORE_ASSERT to catch incorrect usage.

Example:

if (isMemoryCoherent(memoryProperties)) {
    RZ_LOG_WARN("vkFlushMappedMemoryRanges called on coherent memory — unnecessary.");
    RAZIX_CORE_ASSERT(false && "Avoid flushing/invalidation on HOST_COHERENT memory.");
}

Metadata

Metadata

Assignees

Labels

ArchitectureIssues related to API/ Engine design and architecture.bugSomething isn't workinggood first issueGood for newcomersgraphics:VulkanIssue related to Vulkan API implementation

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions