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

[dxvk] Add global resource map to memory allocator

This commit is contained in:
Philip Rebohle 2024-10-18 00:32:22 +02:00
parent 5bff4c97f2
commit 1525fbef03
4 changed files with 45 additions and 2 deletions

View File

@ -17,6 +17,8 @@ namespace dxvk {
m_shaderStages (util::shaderStages(createInfo.stages)), m_shaderStages (util::shaderStages(createInfo.stages)),
m_sharingMode (device->getSharingMode()), m_sharingMode (device->getSharingMode()),
m_info (createInfo) { m_info (createInfo) {
m_allocator->registerResource(this);
// Create and assign actual buffer resource // Create and assign actual buffer resource
assignStorage(allocateStorage()); assignStorage(allocateStorage());
} }
@ -34,6 +36,8 @@ namespace dxvk {
m_shaderStages (util::shaderStages(createInfo.stages)), m_shaderStages (util::shaderStages(createInfo.stages)),
m_sharingMode (device->getSharingMode()), m_sharingMode (device->getSharingMode()),
m_info (createInfo) { m_info (createInfo) {
m_allocator->registerResource(this);
DxvkAllocationInfo allocationInfo = { }; DxvkAllocationInfo allocationInfo = { };
allocationInfo.resourceCookie = cookie(); allocationInfo.resourceCookie = cookie();
@ -48,7 +52,7 @@ namespace dxvk {
DxvkBuffer::~DxvkBuffer() { DxvkBuffer::~DxvkBuffer() {
m_allocator->unregisterResource(this);
} }

View File

@ -14,6 +14,8 @@ namespace dxvk {
m_properties (memFlags), m_properties (memFlags),
m_shaderStages (util::shaderStages(createInfo.stages)), m_shaderStages (util::shaderStages(createInfo.stages)),
m_info (createInfo) { m_info (createInfo) {
m_allocator->registerResource(this);
copyFormatList(createInfo.viewFormatCount, createInfo.viewFormats); copyFormatList(createInfo.viewFormatCount, createInfo.viewFormats);
// Always enable depth-stencil attachment usage for depth-stencil // Always enable depth-stencil attachment usage for depth-stencil
@ -44,6 +46,8 @@ namespace dxvk {
m_properties (memFlags), m_properties (memFlags),
m_shaderStages (util::shaderStages(createInfo.stages)), m_shaderStages (util::shaderStages(createInfo.stages)),
m_info (createInfo) { m_info (createInfo) {
m_allocator->registerResource(this);
copyFormatList(createInfo.viewFormatCount, createInfo.viewFormats); copyFormatList(createInfo.viewFormatCount, createInfo.viewFormats);
// Create backing storage for existing image resource // Create backing storage for existing image resource
@ -56,7 +60,7 @@ namespace dxvk {
DxvkImage::~DxvkImage() { DxvkImage::~DxvkImage() {
m_allocator->unregisterResource(this);
} }

View File

@ -1839,6 +1839,20 @@ namespace dxvk {
} }
void DxvkMemoryAllocator::registerResource(
DxvkPagedResource* resource) {
std::lock_guard lock(m_resourceMutex);
m_resourceMap.emplace(resource->cookie(), resource);
}
void DxvkMemoryAllocator::unregisterResource(
DxvkPagedResource* resource) {
std::lock_guard lock(m_resourceMutex);
m_resourceMap.erase(resource->cookie());
}
VkDeviceAddress DxvkMemoryAllocator::getBufferDeviceAddress(VkBuffer buffer) const { VkDeviceAddress DxvkMemoryAllocator::getBufferDeviceAddress(VkBuffer buffer) const {
auto vk = m_device->vkd(); auto vk = m_device->vkd();

View File

@ -16,6 +16,7 @@ namespace dxvk {
class DxvkSparsePageTable; class DxvkSparsePageTable;
class DxvkSharedAllocationCache; class DxvkSharedAllocationCache;
class DxvkResourceAllocation; class DxvkResourceAllocation;
class DxvkPagedResource;
/** /**
* \brief Memory stats * \brief Memory stats
@ -1117,6 +1118,22 @@ namespace dxvk {
const VkImageCreateInfo& createInfo, const VkImageCreateInfo& createInfo,
VkMemoryRequirements2& memoryRequirements) const; VkMemoryRequirements2& memoryRequirements) const;
/**
* \brief Registers a paged resource with cookie
*
* Useful when the allocator needs to track resources.
* \param [in] resource Resource to add
*/
void registerResource(
DxvkPagedResource* resource);
/**
* \brief Unregisters a paged resource
* \param [in] resource Resource to remove
*/
void unregisterResource(
DxvkPagedResource* resource);
private: private:
DxvkDevice* m_device; DxvkDevice* m_device;
@ -1144,6 +1161,10 @@ namespace dxvk {
dxvk::thread m_worker; dxvk::thread m_worker;
bool m_stopWorker = false; bool m_stopWorker = false;
alignas(CACHE_LINE_SIZE)
dxvk::mutex m_resourceMutex;
std::unordered_map<uint64_t, DxvkPagedResource*> m_resourceMap;
DxvkDeviceMemory allocateDeviceMemory( DxvkDeviceMemory allocateDeviceMemory(
DxvkMemoryType& type, DxvkMemoryType& type,
VkDeviceSize size, VkDeviceSize size,