From 456485f1765340bd7d13ebaaa206fb88f2da16f6 Mon Sep 17 00:00:00 2001 From: Philip Rebohle Date: Thu, 17 Oct 2024 23:26:19 +0200 Subject: [PATCH] [dxvk] Pass resource cookie to allocation objects --- src/dxvk/dxvk_buffer.cpp | 5 ++++- src/dxvk/dxvk_buffer.h | 1 + src/dxvk/dxvk_image.cpp | 6 +++++- src/dxvk/dxvk_memory.cpp | 35 ++++++++++++++++++++++++----------- src/dxvk/dxvk_memory.h | 13 ++++++++++--- 5 files changed, 44 insertions(+), 16 deletions(-) diff --git a/src/dxvk/dxvk_buffer.cpp b/src/dxvk/dxvk_buffer.cpp index c9b9e555a..44a5c2db9 100644 --- a/src/dxvk/dxvk_buffer.cpp +++ b/src/dxvk/dxvk_buffer.cpp @@ -34,13 +34,16 @@ namespace dxvk { m_shaderStages (util::shaderStages(createInfo.stages)), m_sharingMode (device->getSharingMode()), m_info (createInfo) { + DxvkAllocationInfo allocationInfo = { }; + allocationInfo.resourceCookie = cookie(); + VkBufferCreateInfo info = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO }; info.flags = m_info.flags; info.usage = m_info.usage; info.size = m_info.size; m_sharingMode.fill(info); - assignStorage(allocator.importBufferResource(info, importInfo)); + assignStorage(allocator.importBufferResource(info, allocationInfo, importInfo)); } diff --git a/src/dxvk/dxvk_buffer.h b/src/dxvk/dxvk_buffer.h index 7b33528ce..3d7ffd479 100644 --- a/src/dxvk/dxvk_buffer.h +++ b/src/dxvk/dxvk_buffer.h @@ -313,6 +313,7 @@ namespace dxvk { */ Rc allocateStorage(DxvkLocalAllocationCache* cache) { DxvkAllocationInfo allocationInfo = { }; + allocationInfo.resourceCookie = cookie(); allocationInfo.properties = m_properties; VkBufferCreateInfo info = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO }; diff --git a/src/dxvk/dxvk_image.cpp b/src/dxvk/dxvk_image.cpp index 4fb8f2e2c..c248d83f1 100644 --- a/src/dxvk/dxvk_image.cpp +++ b/src/dxvk/dxvk_image.cpp @@ -47,8 +47,11 @@ namespace dxvk { copyFormatList(createInfo.viewFormatCount, createInfo.viewFormats); // Create backing storage for existing image resource + DxvkAllocationInfo allocationInfo = { }; + allocationInfo.resourceCookie = cookie(); + VkImageCreateInfo imageInfo = getImageCreateInfo(DxvkImageUsageInfo()); - assignStorage(m_allocator->importImageResource(imageInfo, imageHandle)); + assignStorage(m_allocator->importImageResource(imageInfo, allocationInfo, imageHandle)); } @@ -174,6 +177,7 @@ namespace dxvk { } DxvkAllocationInfo allocationInfo = { }; + allocationInfo.resourceCookie = cookie(); allocationInfo.properties = m_properties; return m_allocator->createImageResource(imageInfo, diff --git a/src/dxvk/dxvk_memory.cpp b/src/dxvk/dxvk_memory.cpp index bc99081c1..6217b8f83 100644 --- a/src/dxvk/dxvk_memory.cpp +++ b/src/dxvk/dxvk_memory.cpp @@ -526,7 +526,7 @@ namespace dxvk { int64_t address = selectedPool.alloc(size, requirements.alignment); if (likely(address >= 0)) - return createAllocation(type, selectedPool, address, size); + return createAllocation(type, selectedPool, address, size, allocationInfo); // If the memory type is host-visible, try to find an existing chunk // in the other memory pool of the memory type and move over. @@ -551,7 +551,7 @@ namespace dxvk { address = selectedPool.alloc(size, requirements.alignment); if (likely(address >= 0)) - return createAllocation(type, selectedPool, address, size); + return createAllocation(type, selectedPool, address, size, allocationInfo); } } @@ -574,7 +574,7 @@ namespace dxvk { continue; mapDeviceMemory(memory, allocationInfo.properties); - return createAllocation(type, memory); + return createAllocation(type, memory, allocationInfo); } // Try to allocate a new chunk that is large enough to hold @@ -586,7 +586,7 @@ namespace dxvk { if (allocateChunkInPool(type, selectedPool, allocationInfo.properties, size, desiredSize)) { address = selectedPool.alloc(size, requirements.alignment); - return createAllocation(type, selectedPool, address, size); + return createAllocation(type, selectedPool, address, size, allocationInfo); } } @@ -608,7 +608,7 @@ namespace dxvk { if (likely(memory.memory != VK_NULL_HANDLE)) { mapDeviceMemory(memory, allocationInfo.properties); - return createAllocation(type, memory); + return createAllocation(type, memory, allocationInfo); } } @@ -718,7 +718,9 @@ namespace dxvk { logMemoryStats(); } } else { - allocation = createAllocation(new DxvkSparsePageTable(m_device, createInfo, buffer)); + allocation = createAllocation( + new DxvkSparsePageTable(m_device, createInfo, buffer), + allocationInfo); } if (!allocation) { @@ -855,7 +857,7 @@ namespace dxvk { allocation->m_sparsePageTable = pageTable.release(); } else { // Just need a page table, but no memory - allocation = createAllocation(pageTable.release()); + allocation = createAllocation(pageTable.release(), allocationInfo); } } @@ -930,9 +932,11 @@ namespace dxvk { Rc DxvkMemoryAllocator::importBufferResource( const VkBufferCreateInfo& createInfo, + const DxvkAllocationInfo& allocationInfo, const DxvkBufferImportInfo& importInfo) { Rc allocation = m_allocationPool.create(this, nullptr); allocation->m_flags.set(DxvkAllocationFlag::Imported); + allocation->m_resourceCookie = allocation->m_resourceCookie; allocation->m_size = createInfo.size; allocation->m_mapPtr = importInfo.mapPtr; allocation->m_buffer = importInfo.buffer; @@ -947,9 +951,11 @@ namespace dxvk { Rc DxvkMemoryAllocator::importImageResource( const VkImageCreateInfo& createInfo, + const DxvkAllocationInfo& allocationInfo, VkImage imageHandle) { Rc allocation = m_allocationPool.create(this, nullptr); allocation->m_flags.set(DxvkAllocationFlag::Imported); + allocation->m_resourceCookie = allocation->m_resourceCookie; allocation->m_image = imageHandle; return allocation; @@ -1105,7 +1111,8 @@ namespace dxvk { DxvkMemoryType& type, DxvkMemoryPool& pool, VkDeviceSize address, - VkDeviceSize size) { + VkDeviceSize size, + const DxvkAllocationInfo& allocationInfo) { type.stats.memoryUsed += size; uint32_t chunkIndex = address >> DxvkPageAllocator::ChunkAddressBits; @@ -1116,6 +1123,7 @@ namespace dxvk { VkDeviceSize offset = address & DxvkPageAllocator::ChunkAddressMask; auto allocation = m_allocationPool.create(this, &type); + allocation->m_resourceCookie = allocationInfo.resourceCookie; allocation->m_memory = chunk.memory.memory; allocation->m_address = address; allocation->m_size = size; @@ -1135,8 +1143,10 @@ namespace dxvk { DxvkResourceAllocation* DxvkMemoryAllocator::createAllocation( - DxvkSparsePageTable* sparsePageTable) { + DxvkSparsePageTable* sparsePageTable, + const DxvkAllocationInfo& allocationInfo) { auto allocation = m_allocationPool.create(this, nullptr); + allocation->m_resourceCookie = allocationInfo.resourceCookie; allocation->m_sparsePageTable = sparsePageTable; return allocation; @@ -1145,7 +1155,8 @@ namespace dxvk { DxvkResourceAllocation* DxvkMemoryAllocator::createAllocation( DxvkMemoryType& type, - const DxvkDeviceMemory& memory) { + const DxvkDeviceMemory& memory, + const DxvkAllocationInfo& allocationInfo) { type.stats.memoryUsed += memory.size; auto allocation = m_allocationPool.create(this, &type); @@ -1154,6 +1165,7 @@ namespace dxvk { if (memory.buffer) allocation->m_flags.set(DxvkAllocationFlag::OwnsBuffer); + allocation->m_resourceCookie = allocationInfo.resourceCookie; allocation->m_memory = memory.memory; allocation->m_address = DedicatedChunkAddress; allocation->m_size = memory.size; @@ -1431,7 +1443,8 @@ namespace dxvk { // Add allocation to the list and mark it as cacheable, // so it will get recycled as-is after use. - allocation = createAllocation(memoryType, memoryPool, address, allocationSize); + allocation = createAllocation(memoryType, memoryPool, + address, allocationSize, DxvkAllocationInfo()); allocation->m_flags.set(DxvkAllocationFlag::Cacheable); if (tail) { diff --git a/src/dxvk/dxvk_memory.h b/src/dxvk/dxvk_memory.h index 2a00cb090..6cec3b4db 100644 --- a/src/dxvk/dxvk_memory.h +++ b/src/dxvk/dxvk_memory.h @@ -921,6 +921,8 @@ namespace dxvk { * \brief Allocation properties */ struct DxvkAllocationInfo { + /// Virtual resource cookie for the allocation + uint64_t resourceCookie = 0u; /// Desired memory property flags VkMemoryPropertyFlags properties = 0u; }; @@ -1040,6 +1042,7 @@ namespace dxvk { */ Rc importBufferResource( const VkBufferCreateInfo& createInfo, + const DxvkAllocationInfo& allocationInfo, const DxvkBufferImportInfo& importInfo); /** @@ -1051,6 +1054,7 @@ namespace dxvk { */ Rc importImageResource( const VkImageCreateInfo& createInfo, + const DxvkAllocationInfo& allocationInfo, VkImage imageHandle); /** @@ -1186,14 +1190,17 @@ namespace dxvk { DxvkMemoryType& type, DxvkMemoryPool& pool, VkDeviceSize address, - VkDeviceSize size); + VkDeviceSize size, + const DxvkAllocationInfo& allocationInfo); DxvkResourceAllocation* createAllocation( DxvkMemoryType& type, - const DxvkDeviceMemory& memory); + const DxvkDeviceMemory& memory, + const DxvkAllocationInfo& allocationInfo); DxvkResourceAllocation* createAllocation( - DxvkSparsePageTable* sparsePageTable); + DxvkSparsePageTable* sparsePageTable, + const DxvkAllocationInfo& allocationInfo); bool refillAllocationCache( DxvkLocalAllocationCache* cache,