1
0
mirror of https://github.com/doitsujin/dxvk.git synced 2025-02-24 04:54:14 +01:00

[dxvk] Add helper class for resource relocation

This commit is contained in:
Philip Rebohle 2024-10-18 14:15:08 +02:00
parent 2d1f785bef
commit a858305900
2 changed files with 111 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();
@ -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();

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
*
@ -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<uint64_t, DxvkPagedResource*> m_resourceMap;
alignas(CACHE_LINE_SIZE)
DxvkRelocationList m_relocations;
DxvkDeviceMemory allocateDeviceMemory(
DxvkMemoryType& type,
VkDeviceSize size,