1
0
mirror of https://github.com/doitsujin/dxvk.git synced 2025-02-20 10:54:16 +01:00

[dxvk] Add helper class for resource relocation

This commit is contained in:
Philip Rebohle 2024-10-18 14:15:08 +02:00 committed by Philip Rebohle
parent 0723250c12
commit 9cbd45b8cf
2 changed files with 113 additions and 0 deletions

View File

@ -459,6 +459,46 @@ namespace dxvk {
DxvkRelocationList::DxvkRelocationList() {
}
DxvkRelocationList::~DxvkRelocationList() {
}
std::vector<DxvkRelocationEntry> DxvkRelocationList::poll() {
std::lock_guard lock(m_mutex);
std::vector<DxvkRelocationEntry> 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<DxvkPagedResource>&& 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();
@ -506,6 +546,10 @@ namespace dxvk {
DxvkMemoryAllocator::~DxvkMemoryAllocator() {
auto vk = m_device->vkd();
// Free all resources that are still queued up for relocation
// before destroying any allocator structures
m_relocations.clear();
// Destroy shared caches so that any allocations
// that are still alive get returned to the device
for (uint32_t i = 0; i < m_memTypeCount; i++) {

View File

@ -957,6 +957,64 @@ namespace dxvk {
};
/**
* \brief Relocation entry
*/
struct DxvkRelocationEntry {
/// Resource to relocate
Rc<DxvkPagedResource> 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<DxvkRelocationEntry> poll();
/**
* \brief Adds relocation entry to the list
*
* \param [in] resource Resource to add
* \param [in] mode Allocation mode
*/
void addResource(
Rc<DxvkPagedResource>&& resource,
DxvkAllocationModes mode);
/**
* \brief Clears list
*/
void clear();
private:
dxvk::mutex m_mutex;
std::unordered_map<Rc<DxvkPagedResource>,
DxvkAllocationModes, RcHash> m_entries;
};
/**
* \brief Memory allocator
*
@ -1162,6 +1220,14 @@ namespace dxvk {
*/
void performTimedTasks();
/**
* \brief Polls relocation list
* \returns Relocation entries
*/
auto pollRelocationList() {
return m_relocations.poll();
}
private:
DxvkDevice* m_device;
@ -1193,6 +1259,9 @@ namespace dxvk {
dxvk::mutex m_resourceMutex;
std::unordered_map<uint64_t, DxvkPagedResource*> m_resourceMap;
alignas(CACHE_LINE_SIZE)
DxvkRelocationList m_relocations;
DxvkDeviceMemory allocateDeviceMemory(
DxvkMemoryType& type,
VkDeviceSize size,