vulkan: memory: Flush non coherent memory after write.
authorVíctor Manuel Jáquez Leal <vjaquez@igalia.com>
Wed, 4 Jan 2023 16:30:47 +0000 (17:30 +0100)
committerGStreamer Marge Bot <gitlab-merge-bot@gstreamer-foundation.org>
Thu, 26 Jan 2023 23:29:06 +0000 (23:29 +0000)
Spec 7.1.3:

If a memory object does not have the VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
property, then vkFlushMappedMemoryRanges must be called in order to guarantee
that writes to the memory object from the host are made available to the host
domain, where they can be further made available to the device domain via a
domain operation. Similarly, vkInvalidateMappedMemoryRanges must be called to
guarantee that writes which are available to the host domain are made visible to
host operations.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/3817>

subprojects/gst-plugins-bad/gst-libs/gst/vulkan/gstvkmemory.c

index 40ed31e..5bec921 100644 (file)
@@ -142,6 +142,27 @@ _vk_mem_map_full (GstVulkanMemory * mem, GstMapInfo * info, gsize size)
 static void
 _vk_mem_unmap_full (GstVulkanMemory * mem, GstMapInfo * info)
 {
+  if ((info->flags & GST_MAP_WRITE)
+      && !(mem->properties & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)) {
+    GError *error = NULL;
+    VkResult err;
+    VkMappedMemoryRange range = {
+      .sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,
+      /* .pNext = */
+      .memory = mem->mem_ptr,
+      .offset = mem->vk_offset,
+      .size = mem->mem.size,
+    };
+
+    err = vkFlushMappedMemoryRanges (mem->device->device, 1u, &range);
+    if (gst_vulkan_error_to_g_error (err, &error,
+            "vkFlushMappedMemoryRanges") < 0) {
+      GST_CAT_WARNING (GST_CAT_VULKAN_MEMORY, "Failed to flush memory: %s",
+          error->message);
+      g_clear_error (&error);
+    }
+  }
+
   vkUnmapMemory (mem->device->device, mem->mem_ptr);
 }