Skip to content

Commit

Permalink
ResourceAllocator: several changes:
Browse files Browse the repository at this point in the history
- "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
  • Loading branch information
dzhdanNV committed Oct 7, 2024
1 parent defbe3e commit 7c1db3a
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 9 deletions.
1 change: 0 additions & 1 deletion Source/D3D12/BufferD3D12.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion Source/D3D12/QueryPoolD3D12.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions Source/D3D12/ResourceAllocatorD3D12.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion Source/D3D12/TextureD3D12.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions Source/Shared/SharedExternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions Source/VK/ResourceAllocatorVK.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 7c1db3a

Please sign in to comment.