diff --git a/src/dxvk/dxvk_buffer.cpp b/src/dxvk/dxvk_buffer.cpp index 44a5c2db9..ea77196d1 100644 --- a/src/dxvk/dxvk_buffer.cpp +++ b/src/dxvk/dxvk_buffer.cpp @@ -17,6 +17,8 @@ namespace dxvk { m_shaderStages (util::shaderStages(createInfo.stages)), m_sharingMode (device->getSharingMode()), m_info (createInfo) { + m_allocator->registerResource(this); + // Create and assign actual buffer resource assignStorage(allocateStorage()); } @@ -34,6 +36,8 @@ namespace dxvk { m_shaderStages (util::shaderStages(createInfo.stages)), m_sharingMode (device->getSharingMode()), m_info (createInfo) { + m_allocator->registerResource(this); + DxvkAllocationInfo allocationInfo = { }; allocationInfo.resourceCookie = cookie(); @@ -48,7 +52,7 @@ namespace dxvk { DxvkBuffer::~DxvkBuffer() { - + m_allocator->unregisterResource(this); } diff --git a/src/dxvk/dxvk_image.cpp b/src/dxvk/dxvk_image.cpp index c248d83f1..b97b676d1 100644 --- a/src/dxvk/dxvk_image.cpp +++ b/src/dxvk/dxvk_image.cpp @@ -14,6 +14,8 @@ namespace dxvk { m_properties (memFlags), m_shaderStages (util::shaderStages(createInfo.stages)), m_info (createInfo) { + m_allocator->registerResource(this); + copyFormatList(createInfo.viewFormatCount, createInfo.viewFormats); // Always enable depth-stencil attachment usage for depth-stencil @@ -44,6 +46,8 @@ namespace dxvk { m_properties (memFlags), m_shaderStages (util::shaderStages(createInfo.stages)), m_info (createInfo) { + m_allocator->registerResource(this); + copyFormatList(createInfo.viewFormatCount, createInfo.viewFormats); // Create backing storage for existing image resource @@ -56,7 +60,7 @@ namespace dxvk { DxvkImage::~DxvkImage() { - + m_allocator->unregisterResource(this); } diff --git a/src/dxvk/dxvk_memory.cpp b/src/dxvk/dxvk_memory.cpp index 9b6b1bcd4..c562def65 100644 --- a/src/dxvk/dxvk_memory.cpp +++ b/src/dxvk/dxvk_memory.cpp @@ -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 { auto vk = m_device->vkd(); diff --git a/src/dxvk/dxvk_memory.h b/src/dxvk/dxvk_memory.h index 469f55785..e90c6adaf 100644 --- a/src/dxvk/dxvk_memory.h +++ b/src/dxvk/dxvk_memory.h @@ -16,6 +16,7 @@ namespace dxvk { class DxvkSparsePageTable; class DxvkSharedAllocationCache; class DxvkResourceAllocation; + class DxvkPagedResource; /** * \brief Memory stats @@ -1117,6 +1118,22 @@ namespace dxvk { const VkImageCreateInfo& createInfo, 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: DxvkDevice* m_device; @@ -1144,6 +1161,10 @@ namespace dxvk { dxvk::thread m_worker; bool m_stopWorker = false; + alignas(CACHE_LINE_SIZE) + dxvk::mutex m_resourceMutex; + std::unordered_map m_resourceMap; + DxvkDeviceMemory allocateDeviceMemory( DxvkMemoryType& type, VkDeviceSize size,