lavapipe: EXT_memory_priority
authorMike Blumenkrantz <michael.blumenkrantz@gmail.com>
Wed, 17 May 2023 12:26:24 +0000 (08:26 -0400)
committerMarge Bot <emma+marge@anholt.net>
Tue, 23 May 2023 21:09:28 +0000 (21:09 +0000)
Reviewed-by: Dave Airlie <airlied@redhat.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/23077>

docs/features.txt
src/gallium/frontends/lavapipe/lvp_device.c

index a60e406..fb7802c 100644 (file)
@@ -575,7 +575,7 @@ Khronos extensions that are not part of any Vulkan version:
   VK_EXT_line_rasterization                             DONE (anv, lvp, radv, tu, v3dv, vn)
   VK_EXT_load_store_op_none                             DONE (anv, radv, tu, v3dv, vn)
   VK_EXT_memory_budget                                  DONE (anv, lvp, radv, tu, v3dv, vn)
-  VK_EXT_memory_priority                                DONE (radv)
+  VK_EXT_memory_priority                                DONE (lvp, radv)
   VK_EXT_mesh_shader                                    DONE (anv/gfx12.5+, radv)
   VK_EXT_multi_draw                                     DONE (anv, lvp, radv, tu, vn)
   VK_EXT_multisampled_render_to_single_sampled          DONE (lvp)
index 98b1787..3828aff 100644 (file)
 #include "nir.h"
 #include "nir_builder.h"
 
+#if DETECT_OS_LINUX
+#include <sys/mman.h>
+#endif
+
 #if defined(VK_USE_PLATFORM_WAYLAND_KHR) || \
     defined(VK_USE_PLATFORM_WIN32_KHR) || \
     defined(VK_USE_PLATFORM_XCB_KHR) || \
@@ -167,6 +171,9 @@ static const struct vk_device_extension_table lvp_device_extensions_supported =
    .EXT_index_type_uint8                  = true,
    .EXT_inline_uniform_block              = true,
    .EXT_memory_budget                     = true,
+#if DETECT_OS_LINUX
+   .EXT_memory_priority                   = true,
+#endif
    .EXT_multisampled_render_to_single_sampled = true,
    .EXT_multi_draw                        = true,
    .EXT_non_seamless_cube_map             = true,
@@ -533,6 +540,9 @@ lvp_get_features(const struct lvp_physical_device *pdevice,
       .shaderSharedFloat64AtomicMinMax = false,
       .shaderImageFloat32AtomicMinMax  = LLVM_VERSION_MAJOR >= 15,
       .sparseImageFloat32AtomicMinMax  = false,
+
+      /* VK_EXT_memory_priority */
+      .memoryPriority = true,
    };
 }
 
@@ -1527,6 +1537,7 @@ VKAPI_ATTR VkResult VKAPI_CALL lvp_AllocateMemory(
    const VkImportMemoryHostPointerInfoEXT *host_ptr_info = NULL;
    VkResult error = VK_ERROR_OUT_OF_DEVICE_MEMORY;
    assert(pAllocateInfo->sType == VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO);
+   int priority = 0;
 
    if (pAllocateInfo->allocationSize == 0) {
       /* Apparently, this is allowed */
@@ -1548,6 +1559,16 @@ VKAPI_ATTR VkResult VKAPI_CALL lvp_AllocateMemory(
          import_info = (VkImportMemoryFdInfoKHR*)ext;
          assert(import_info->handleType == VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT);
          break;
+      case VK_STRUCTURE_TYPE_MEMORY_PRIORITY_ALLOCATE_INFO_EXT: {
+         VkMemoryPriorityAllocateInfoEXT *prio = (VkMemoryPriorityAllocateInfoEXT*)ext;
+         if (prio->priority < 0.3)
+            priority = -1;
+         else if (prio->priority < 0.6)
+            priority = 0;
+         else
+            priority = 1;
+         break;
+      }
       default:
          break;
       }
@@ -1611,6 +1632,19 @@ VKAPI_ATTR VkResult VKAPI_CALL lvp_AllocateMemory(
       if (device->poison_mem)
          /* this is a value that will definitely break things */
          memset(mem->pmem, UINT8_MAX / 2 + 1, pAllocateInfo->allocationSize);
+#if DETECT_OS_LINUX
+      if (priority) {
+         int advice = 0;
+#ifdef MADV_COLD
+         if (priority < 0)
+            advice |= MADV_COLD;
+#endif
+         if (priority > 0)
+            advice |= MADV_WILLNEED;
+         if (advice)
+            madvise(mem->pmem, pAllocateInfo->allocationSize, advice);
+      }
+#endif
    }
 
    mem->type_index = pAllocateInfo->memoryTypeIndex;