From a858305900b7c1aebd6862397d113b184b62b678 Mon Sep 17 00:00:00 2001 From: Philip Rebohle Date: Fri, 18 Oct 2024 14:15:08 +0200 Subject: [PATCH] [dxvk] Add helper class for resource relocation --- src/dxvk/dxvk_memory.cpp | 42 ++++++++++++++++++++++++ src/dxvk/dxvk_memory.h | 69 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) diff --git a/src/dxvk/dxvk_memory.cpp b/src/dxvk/dxvk_memory.cpp index c2cf3dbad..40d089cf9 100644 --- a/src/dxvk/dxvk_memory.cpp +++ b/src/dxvk/dxvk_memory.cpp @@ -459,6 +459,46 @@ namespace dxvk { + DxvkRelocationList::DxvkRelocationList() { + + } + + + DxvkRelocationList::~DxvkRelocationList() { + + } + + + std::vector DxvkRelocationList::poll() { + std::lock_guard lock(m_mutex); + + std::vector result; + result.reserve(m_entries.size()); + + for (const auto& p : m_entries) + result.push_back({ p.first, p.second }); + + m_entries.clear(); + return result; + } + + + void DxvkRelocationList::addResource( + Rc&& resource, + DxvkAllocationModes mode) { + std::lock_guard lock(m_mutex); + m_entries.emplace(std::move(resource), mode); + } + + + void DxvkRelocationList::clear() { + std::lock_guard lock(m_mutex); + m_entries.clear(); + } + + + + DxvkMemoryAllocator::DxvkMemoryAllocator(DxvkDevice* device) : m_device(device), m_sharingModeInfo(m_device->getSharingMode()) { VkPhysicalDeviceMemoryProperties memInfo = m_device->adapter()->memoryProperties(); @@ -509,6 +549,8 @@ namespace dxvk { DxvkMemoryAllocator::~DxvkMemoryAllocator() { auto vk = m_device->vkd(); + m_relocations.clear(); + { std::unique_lock lock(m_mutex); m_stopWorker = true; m_cond.notify_one(); diff --git a/src/dxvk/dxvk_memory.h b/src/dxvk/dxvk_memory.h index 397146b4f..2c0e9c3fc 100644 --- a/src/dxvk/dxvk_memory.h +++ b/src/dxvk/dxvk_memory.h @@ -957,6 +957,64 @@ namespace dxvk { }; + /** + * \brief Relocation entry + */ + struct DxvkRelocationEntry { + /// Resource to relocate + Rc resource; + /// Resource to relocate + DxvkAllocationModes mode = 0u; + }; + + + /** + * \brief Resource relocation helper + * + * Simple thread-safe data structure used to pass a list of + * resources to move from the allocator to the CS thread. + */ + class DxvkRelocationList { + + public: + + DxvkRelocationList(); + + ~DxvkRelocationList(); + + /** + * \brief Retrieves list of resources to move + * + * Clears the internally stored list. Any + * duplicate entries will be removed. + * \returns List of resources to move + */ + std::vector poll(); + + /** + * \brief Adds relocation entry to the list + * + * \param [in] resource Resource to add + * \param [in] mode Allocation mode + */ + void addResource( + Rc&& resource, + DxvkAllocationModes mode); + + /** + * \brief Clears list + */ + void clear(); + + private: + + dxvk::mutex m_mutex; + std::unordered_map, + DxvkAllocationModes, RcHash> m_entries; + + }; + + /** * \brief Memory allocator * @@ -1153,6 +1211,14 @@ namespace dxvk { void unregisterResource( DxvkPagedResource* resource); + /** + * \brief Polls relocation list + * \returns Relocation entries + */ + auto pollRelocationList() { + return m_relocations.poll(); + } + private: DxvkDevice* m_device; @@ -1184,6 +1250,9 @@ namespace dxvk { dxvk::mutex m_resourceMutex; std::unordered_map m_resourceMap; + alignas(CACHE_LINE_SIZE) + DxvkRelocationList m_relocations; + DxvkDeviceMemory allocateDeviceMemory( DxvkMemoryType& type, VkDeviceSize size,