diff --git a/include/D3D12MemAlloc.h b/include/D3D12MemAlloc.h index 4bf64ee..dba3c81 100644 --- a/include/D3D12MemAlloc.h +++ b/include/D3D12MemAlloc.h @@ -24,9 +24,9 @@ /** \mainpage D3D12 Memory Allocator -Version 2.1.0-development (2022-12-15) +Version 2.1.0-development (2023-07-05) -Copyright (c) 2019-2022 Advanced Micro Devices, Inc. All rights reserved. \n +Copyright (c) 2019-2023 Advanced Micro Devices, Inc. All rights reserved. \n License: MIT Documentation of all members: D3D12MemAlloc.h @@ -1333,7 +1333,7 @@ public: - `pNonLocalBudget` returns the budget of the system memory available for D3D12 resources. - When IsUMA() `== TRUE` (integrated graphics chip): - `pLocalBudget` returns the budget of the shared memory available for all D3D12 resources. - All memory is considered "local". + All memory is considered "local". - `pNonLocalBudget` is not applicable and returns zeros. This function is called "get" not "calculate" because it is very fast, suitable to be called diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 10ac885..668cb64 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -126,3 +126,27 @@ if(D3D12MA_BUILD_SAMPLE) message(STATUS "D3D12Sample application is not supported to Linux") endif() endif() + +set(D3D12MA_AGILITY_SDK_DIRECTORY "" CACHE STRING "Path to unpacked DX12 Agility SDK. Leave empty to compile without it.") +option(D3D12MA_AGILITY_SDK_PREVIEW "Set if DX12 Agility SDK is preview version." OFF) +if(D3D12MA_AGILITY_SDK_DIRECTORY) + if(EXISTS "${D3D12MA_AGILITY_SDK_DIRECTORY}/build/native/include/d3d12.h") + message(STATUS "DX12 Agility SDK used from \"${D3D12MA_AGILITY_SDK_DIRECTORY}\".") + target_compile_definitions(D3D12MemoryAllocator PRIVATE D3D12MA_USE_AGILITY_SDK=1) + target_include_directories(D3D12MemoryAllocator BEFORE PRIVATE "${D3D12MA_AGILITY_SDK_DIRECTORY}/build/native/include") + if(D3D12MA_AGILITY_SDK_PREVIEW) + target_compile_definitions(D3D12MemoryAllocator PRIVATE D3D12MA_USE_AGILITY_SDK_PREVIEW=1) + endif() + if(${D3D12MA_BUILD_SAMPLE} AND ${WIN32}) + target_compile_definitions(D3D12Sample PRIVATE D3D12MA_USE_AGILITY_SDK=1) + target_include_directories(D3D12Sample BEFORE PRIVATE "${D3D12MA_AGILITY_SDK_DIRECTORY}/build/native/include") + if(D3D12MA_AGILITY_SDK_PREVIEW) + target_compile_definitions(D3D12Sample PRIVATE D3D12MA_USE_AGILITY_SDK_PREVIEW=1) + endif() + endif() + else() + message(FATAL_ERROR "DX12 Agility SDK not found - cannot find file \"${D3D12MA_AGILITY_SDK_DIRECTORY}/build/native/include/d3d12.h\".") + endif() +else() + message(STATUS "DX12 Agility SDK not used.") +endif() diff --git a/src/D3D12MemAlloc.cpp b/src/D3D12MemAlloc.cpp index ed88072..da70837 100644 --- a/src/D3D12MemAlloc.cpp +++ b/src/D3D12MemAlloc.cpp @@ -148,6 +148,12 @@ static const WCHAR* const HeapTypeNames[] = L"READBACK", L"CUSTOM", }; +static const WCHAR* const StandardHeapTypeNames[] = +{ + L"DEFAULT", + L"UPLOAD", + L"READBACK", +}; static const D3D12_HEAP_FLAGS RESOURCE_CLASS_HEAP_FLAGS = D3D12_HEAP_FLAG_DENY_BUFFERS | D3D12_HEAP_FLAG_DENY_RT_DS_TEXTURES | D3D12_HEAP_FLAG_DENY_NON_RT_DS_TEXTURES; @@ -452,23 +458,26 @@ static IterT BinaryFindSorted(const IterT& beg, const IterT& end, const KeyT& va return end; } -static UINT HeapTypeToIndex(D3D12_HEAP_TYPE type) +static UINT StandardHeapTypeToIndex(D3D12_HEAP_TYPE type) { switch (type) { case D3D12_HEAP_TYPE_DEFAULT: return 0; case D3D12_HEAP_TYPE_UPLOAD: return 1; case D3D12_HEAP_TYPE_READBACK: return 2; - case D3D12_HEAP_TYPE_CUSTOM: return 3; default: D3D12MA_ASSERT(0); return UINT_MAX; } } -static D3D12_HEAP_TYPE IndexToHeapType(UINT heapTypeIndex) +static D3D12_HEAP_TYPE IndexToStandardHeapType(UINT heapTypeIndex) { - D3D12MA_ASSERT(heapTypeIndex < 4); - // D3D12_HEAP_TYPE_DEFAULT starts at 1. - return (D3D12_HEAP_TYPE)(heapTypeIndex + 1); + switch(heapTypeIndex) + { + case 0: return D3D12_HEAP_TYPE_DEFAULT; + case 1: return D3D12_HEAP_TYPE_UPLOAD; + case 2: return D3D12_HEAP_TYPE_READBACK; + default: D3D12MA_ASSERT(0); return D3D12_HEAP_TYPE_CUSTOM; + } } static UINT64 HeapFlagsToAlignment(D3D12_HEAP_FLAGS flags, bool denyMsaaTextures) @@ -6625,8 +6634,8 @@ public: void SetResidencyPriority(ID3D12Pageable* obj, D3D12_RESIDENCY_PRIORITY priority) const; void SetCurrentFrameIndex(UINT frameIndex); - // For more deailed stats use outCutomHeaps to access statistics divided into L0 and L1 group - void CalculateStatistics(TotalStatistics& outStats, DetailedStatistics outCutomHeaps[2] = NULL); + // For more deailed stats use outCustomHeaps to access statistics divided into L0 and L1 group + void CalculateStatistics(TotalStatistics& outStats, DetailedStatistics outCustomHeaps[2] = NULL); void GetBudget(Budget* outLocalBudget, Budget* outNonLocalBudget); void GetBudgetForHeapType(Budget& outBudget, D3D12_HEAP_TYPE heapType); @@ -6747,7 +6756,7 @@ AllocatorPimpl::AllocatorPimpl(const ALLOCATION_CALLBACKS& allocationCallbacks, { m_CommittedAllocations[i].Init( m_UseMutex, - (D3D12_HEAP_TYPE)(D3D12_HEAP_TYPE_DEFAULT + i), + IndexToStandardHeapType(i), NULL); // pool } @@ -7253,7 +7262,7 @@ void AllocatorPimpl::SetCurrentFrameIndex(UINT frameIndex) #endif } -void AllocatorPimpl::CalculateStatistics(TotalStatistics& outStats, DetailedStatistics outCutomHeaps[2]) +void AllocatorPimpl::CalculateStatistics(TotalStatistics& outStats, DetailedStatistics outCustomHeaps[2]) { // Init stats for (size_t i = 0; i < HEAP_TYPE_COUNT; i++) @@ -7261,10 +7270,10 @@ void AllocatorPimpl::CalculateStatistics(TotalStatistics& outStats, DetailedStat for (size_t i = 0; i < DXGI_MEMORY_SEGMENT_GROUP_COUNT; i++) ClearDetailedStatistics(outStats.MemorySegmentGroup[i]); ClearDetailedStatistics(outStats.Total); - if (outCutomHeaps) + if (outCustomHeaps) { - ClearDetailedStatistics(outCutomHeaps[0]); - ClearDetailedStatistics(outCutomHeaps[1]); + ClearDetailedStatistics(outCustomHeaps[0]); + ClearDetailedStatistics(outCustomHeaps[1]); } // Process default pools. 3 standard heap types only. Add them to outStats.HeapType[i]. @@ -7321,8 +7330,8 @@ void AllocatorPimpl::CalculateStatistics(TotalStatistics& outStats, DetailedStat AddDetailedStatistics( outStats.MemorySegmentGroup[memorySegment], tmpStats); - if (outCutomHeaps) - AddDetailedStatistics(outCutomHeaps[memorySegment], tmpStats); + if (outCustomHeaps) + AddDetailedStatistics(outCustomHeaps[memorySegment], tmpStats); } } @@ -7334,7 +7343,7 @@ void AllocatorPimpl::CalculateStatistics(TotalStatistics& outStats, DetailedStat AddDetailedStatistics( outStats.HeapType[heapTypeIndex], tmpStats); AddDetailedStatistics( - outStats.MemorySegmentGroup[StandardHeapTypeToMemorySegmentGroup(IndexToHeapType(heapTypeIndex))], tmpStats); + outStats.MemorySegmentGroup[StandardHeapTypeToMemorySegmentGroup(IndexToStandardHeapType(heapTypeIndex))], tmpStats); } // Sum up memory segment groups to totals. @@ -7678,7 +7687,7 @@ void AllocatorPimpl::BuildStatsString(WCHAR** ppStatsString, BOOL detailedMap) { for (uint8_t heapType = 0; heapType < STANDARD_HEAP_TYPE_COUNT; ++heapType) { - json.WriteString(HeapTypeNames[heapType]); + json.WriteString(StandardHeapTypeNames[heapType]); json.BeginObject(); writeHeapInfo(m_BlockVectors[heapType], m_CommittedAllocations + heapType, false); json.EndObject(); @@ -7695,11 +7704,11 @@ void AllocatorPimpl::BuildStatsString(WCHAR** ppStatsString, BOOL detailedMap) L" - Textures", L" - Textures RT/DS", }; - json.BeginString(HeapTypeNames[heapType]); + json.BeginString(StandardHeapTypeNames[heapType]); json.EndString(heapSubTypeName[heapSubType]); json.BeginObject(); - writeHeapInfo(m_BlockVectors[heapType + heapSubType], m_CommittedAllocations + heapType, false); + writeHeapInfo(m_BlockVectors[heapType * 3 + heapSubType], m_CommittedAllocations + heapType, false); json.EndObject(); } } @@ -8014,7 +8023,7 @@ HRESULT AllocatorPimpl::CalcAllocationParams(const ALLOCATION_DESC& allocDesc, U outCommittedAllocationParams.m_HeapProperties = StandardHeapTypeToHeapProperties(allocDesc.HeapType); outCommittedAllocationParams.m_HeapFlags = allocDesc.ExtraHeapFlags; - outCommittedAllocationParams.m_List = &m_CommittedAllocations[HeapTypeToIndex(allocDesc.HeapType)]; + outCommittedAllocationParams.m_List = &m_CommittedAllocations[StandardHeapTypeToIndex(allocDesc.HeapType)]; // outCommittedAllocationParams.m_ResidencyPriority intentionally left with default value. const ResourceClass resourceClass = (resDesc != NULL) ? @@ -8150,7 +8159,7 @@ void AllocatorPimpl::CalcDefaultPoolParams(D3D12_HEAP_TYPE& outHeapType, D3D12_H void AllocatorPimpl::RegisterPool(Pool* pool, D3D12_HEAP_TYPE heapType) { - const UINT heapTypeIndex = HeapTypeToIndex(heapType); + const UINT heapTypeIndex = (UINT)heapType - 1; MutexLockWrite lock(m_PoolsMutex[heapTypeIndex], m_UseMutex); m_Pools[heapTypeIndex].PushBack(pool->m_Pimpl); @@ -8158,7 +8167,7 @@ void AllocatorPimpl::RegisterPool(Pool* pool, D3D12_HEAP_TYPE heapType) void AllocatorPimpl::UnregisterPool(Pool* pool, D3D12_HEAP_TYPE heapType) { - const UINT heapTypeIndex = HeapTypeToIndex(heapType); + const UINT heapTypeIndex = (UINT)heapType - 1; MutexLockWrite lock(m_PoolsMutex[heapTypeIndex], m_UseMutex); m_Pools[heapTypeIndex].Remove(pool->m_Pimpl); diff --git a/src/D3D12Sample.cpp b/src/D3D12Sample.cpp index 31e2451..b5ce491 100644 --- a/src/D3D12Sample.cpp +++ b/src/D3D12Sample.cpp @@ -35,6 +35,15 @@ namespace PS #include "Shaders\PS_Compiled.h" } +#if D3D12MA_USE_AGILITY_SDK + #if D3D12MA_USE_AGILITY_SDK_PREVIEW + extern "C" { __declspec(dllexport) extern const UINT D3D12SDKVersion = D3D12_PREVIEW_SDK_VERSION; } + #else + extern "C" { __declspec(dllexport) extern const UINT D3D12SDKVersion = D3D12_SDK_VERSION; } + #endif + extern "C" { __declspec(dllexport) extern const char* D3D12SDKPath = u8".\\D3D12\\"; } +#endif + enum class ExitCode : int { GPUList = 2,