From 7c1db3a58fef18ce77b37316dbb6a16ea4e70a5b Mon Sep 17 00:00:00 2001 From: dzhdan Date: Mon, 7 Oct 2024 12:40:35 +0800 Subject: [PATCH] ResourceAllocator: several changes: - "D3D12_RESOURCE_DESC::Alignment" set to 0 to unlock optimizations - Preferred block size explicitly set to 64 Mb (currently) to avoid divergence, because it's 64 Mb in D3D12MA and 256 Mb in VMA - added "STRATEGY_MIN_MEMORY" flag --- Source/D3D12/BufferD3D12.hpp | 1 - Source/D3D12/QueryPoolD3D12.hpp | 1 - Source/D3D12/ResourceAllocatorD3D12.hpp | 8 ++++---- Source/D3D12/TextureD3D12.hpp | 2 +- Source/Shared/SharedExternal.h | 1 + Source/VK/ResourceAllocatorVK.hpp | 5 +++-- 6 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Source/D3D12/BufferD3D12.hpp b/Source/D3D12/BufferD3D12.hpp index 31f4e49e..bd41b894 100644 --- a/Source/D3D12/BufferD3D12.hpp +++ b/Source/D3D12/BufferD3D12.hpp @@ -3,7 +3,6 @@ void nri::GetResourceDesc(D3D12_RESOURCE_DESC* desc, const BufferDesc& bufferDesc) { *desc = {}; desc->Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; - desc->Alignment = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT; // 64KB desc->Width = bufferDesc.size; desc->Height = 1; desc->DepthOrArraySize = 1; diff --git a/Source/D3D12/QueryPoolD3D12.hpp b/Source/D3D12/QueryPoolD3D12.hpp index 9347cb10..e9f9f40d 100644 --- a/Source/D3D12/QueryPoolD3D12.hpp +++ b/Source/D3D12/QueryPoolD3D12.hpp @@ -45,7 +45,6 @@ Result QueryPoolD3D12::Create(const QueryPoolDesc& queryPoolDesc) { Result QueryPoolD3D12::CreateReadbackBuffer(const QueryPoolDesc& queryPoolDesc) { D3D12_RESOURCE_DESC resourceDesc = {}; resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; - resourceDesc.Alignment = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT; resourceDesc.Width = (uint64_t)queryPoolDesc.capacity * m_QuerySize; resourceDesc.Height = 1; resourceDesc.DepthOrArraySize = 1; diff --git a/Source/D3D12/ResourceAllocatorD3D12.hpp b/Source/D3D12/ResourceAllocatorD3D12.hpp index fdf88389..893cb549 100644 --- a/Source/D3D12/ResourceAllocatorD3D12.hpp +++ b/Source/D3D12/ResourceAllocatorD3D12.hpp @@ -8,8 +8,8 @@ Result DeviceD3D12::CreateVma() { allocatorDesc.pDevice = m_Device; allocatorDesc.pAdapter = m_Adapter; allocatorDesc.pAllocationCallbacks = m_AllocationCallbackPtr; - allocatorDesc.Flags = (D3D12MA::ALLOCATOR_FLAGS)( - D3D12MA::ALLOCATOR_FLAG_DEFAULT_POOLS_NOT_ZEROED | D3D12MA::ALLOCATOR_FLAG_MSAA_TEXTURES_ALWAYS_COMMITTED | D3D12MA::ALLOCATOR_FLAG_DONT_PREFER_SMALL_BUFFERS_COMMITTED); + allocatorDesc.Flags = (D3D12MA::ALLOCATOR_FLAGS)(D3D12MA::ALLOCATOR_FLAG_DEFAULT_POOLS_NOT_ZEROED | D3D12MA::ALLOCATOR_FLAG_MSAA_TEXTURES_ALWAYS_COMMITTED | D3D12MA::ALLOCATOR_FLAG_DONT_PREFER_SMALL_BUFFERS_COMMITTED); + allocatorDesc.PreferredBlockSize = VMA_PREFERRED_BLOCK_SIZE; HRESULT hr = D3D12MA::CreateAllocator(&allocatorDesc, &m_Vma); RETURN_ON_BAD_HRESULT(this, hr, "D3D12MA::CreateAllocator"); @@ -24,7 +24,7 @@ Result BufferD3D12::Create(const AllocateBufferDesc& bufferDesc) { D3D12MA::ALLOCATION_DESC allocationDesc = {}; allocationDesc.HeapType = GetHeapType(bufferDesc.memoryLocation); - allocationDesc.Flags = D3D12MA::ALLOCATION_FLAG_CAN_ALIAS; + allocationDesc.Flags = (D3D12MA::ALLOCATION_FLAGS)(D3D12MA::ALLOCATION_FLAG_CAN_ALIAS | D3D12MA::ALLOCATION_FLAG_STRATEGY_MIN_MEMORY); #ifdef NRI_USE_AGILITY_SDK if (m_Device.GetVersion() >= 10) { @@ -80,7 +80,7 @@ Result TextureD3D12::Create(const AllocateTextureDesc& textureDesc) { D3D12MA::ALLOCATION_DESC allocationDesc = {}; allocationDesc.HeapType = GetHeapType(textureDesc.memoryLocation); - allocationDesc.Flags = D3D12MA::ALLOCATION_FLAG_CAN_ALIAS; + allocationDesc.Flags = (D3D12MA::ALLOCATION_FLAGS)(D3D12MA::ALLOCATION_FLAG_CAN_ALIAS | D3D12MA::ALLOCATION_FLAG_STRATEGY_MIN_MEMORY); #ifdef NRI_USE_AGILITY_SDK if (m_Device.GetVersion() >= 10) { diff --git a/Source/D3D12/TextureD3D12.hpp b/Source/D3D12/TextureD3D12.hpp index 9bfb2068..f876c7db 100644 --- a/Source/D3D12/TextureD3D12.hpp +++ b/Source/D3D12/TextureD3D12.hpp @@ -4,8 +4,8 @@ void nri::GetResourceDesc(D3D12_RESOURCE_DESC* desc, const TextureDesc& textureD uint16_t blockWidth = (uint16_t)GetFormatProps(textureDesc.format).blockWidth; const DxgiFormat& dxgiFormat = GetDxgiFormat(textureDesc.format); + *desc = {}; desc->Dimension = GetResourceDimension(textureDesc.type); - desc->Alignment = textureDesc.sampleNum > 1 ? 0 : D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT; desc->Width = Align(textureDesc.width, blockWidth); desc->Height = Align(std::max(textureDesc.height, (Dim_t)1), blockWidth); desc->DepthOrArraySize = std::max(textureDesc.type == TextureType::TEXTURE_3D ? textureDesc.depth : textureDesc.layerNum, (Dim_t)1); diff --git a/Source/Shared/SharedExternal.h b/Source/Shared/SharedExternal.h index 151a08ab..6d0bc4d2 100644 --- a/Source/Shared/SharedExternal.h +++ b/Source/Shared/SharedExternal.h @@ -44,6 +44,7 @@ constexpr uint32_t TIMEOUT_PRESENT = 1000; // 1 sec constexpr uint32_t TIMEOUT_FENCE = 5000; // 5 sec constexpr uint64_t PRESENT_INDEX_BIT_NUM = 56ull; constexpr uint32_t MAX_MESSAGE_LENGTH = 2048; +constexpr uint64_t VMA_PREFERRED_BLOCK_SIZE = 64 * 1024 * 1024; // https://learn.microsoft.com/en-us/windows/win32/direct3d12/root-signature-limits constexpr uint32_t ROOT_SIGNATURE_DWORD_NUM = 64; diff --git a/Source/VK/ResourceAllocatorVK.hpp b/Source/VK/ResourceAllocatorVK.hpp index 237a427c..13a68191 100644 --- a/Source/VK/ResourceAllocatorVK.hpp +++ b/Source/VK/ResourceAllocatorVK.hpp @@ -20,6 +20,7 @@ Result DeviceVK::CreateVma() { allocatorCreateInfo.instance = m_Instance; allocatorCreateInfo.pVulkanFunctions = &vulkanFunctions; allocatorCreateInfo.pAllocationCallbacks = m_AllocationCallbackPtr; + allocatorCreateInfo.preferredLargeHeapBlockSize = VMA_PREFERRED_BLOCK_SIZE; if (m_IsSupported.memoryBudget) allocatorCreateInfo.flags |= VMA_ALLOCATOR_CREATE_EXT_MEMORY_BUDGET_BIT; @@ -49,7 +50,7 @@ Result BufferVK::Create(const AllocateBufferDesc& bufferDesc) { // Create VmaAllocationCreateInfo allocationCreateInfo = {}; - allocationCreateInfo.flags = VMA_ALLOCATION_CREATE_CAN_ALIAS_BIT; + allocationCreateInfo.flags = VMA_ALLOCATION_CREATE_CAN_ALIAS_BIT | VMA_ALLOCATION_CREATE_STRATEGY_MIN_MEMORY_BIT; allocationCreateInfo.priority = bufferDesc.memoryPriority * 0.5f + 0.5f; allocationCreateInfo.usage = IsHostMemory(bufferDesc.memoryLocation) ? VMA_MEMORY_USAGE_AUTO_PREFER_HOST : VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE; @@ -115,7 +116,7 @@ Result TextureVK::Create(const AllocateTextureDesc& textureDesc) { // Create VmaAllocationCreateInfo allocationCreateInfo = {}; - allocationCreateInfo.flags = VMA_ALLOCATION_CREATE_CAN_ALIAS_BIT; + allocationCreateInfo.flags = VMA_ALLOCATION_CREATE_CAN_ALIAS_BIT | VMA_ALLOCATION_CREATE_STRATEGY_MIN_MEMORY_BIT; allocationCreateInfo.priority = textureDesc.memoryPriority * 0.5f + 0.5f; allocationCreateInfo.usage = IsHostMemory(textureDesc.memoryLocation) ? VMA_MEMORY_USAGE_AUTO_PREFER_HOST : VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE;