Add ALLOCATOR_FLAG_ALWAYS_COMMITTED, remove debug macro D3D12MA_DEBUG_ALWAYS_COMMITTED

Also fixed nasty bug with uninitialized member of Allocation class.
diff --git a/src/D3D12MemAlloc.cpp b/src/D3D12MemAlloc.cpp
index 931fbb7..ff82d46 100644
--- a/src/D3D12MemAlloc.cpp
+++ b/src/D3D12MemAlloc.cpp
@@ -51,14 +51,6 @@
     #endif

 #endif

 

-#ifndef D3D12MA_DEBUG_ALWAYS_COMMITTED

-    /*

-    Every allocation will have its own memory block.

-    Define to 1 for debugging purposes only.

-    */

-    #define D3D12MA_DEBUG_ALWAYS_COMMITTED (0)

-#endif

-

 #ifndef D3D12MA_DEBUG_ALIGNMENT

     /*

     Minimum alignment of all allocations, in bytes.

@@ -2139,7 +2131,8 @@
     */

     static bool PrefersCommittedAllocation(const D3D12_RESOURCE_DESC& resourceDesc);

 

-    bool m_UseMutex;

+    const bool m_UseMutex;

+    const bool m_AlwaysCommitted;

     ID3D12Device* m_Device;

     UINT64 m_PreferredBlockSize;

     ALLOCATION_CALLBACKS m_AllocationCallbacks;

@@ -3239,6 +3232,7 @@
 

 AllocatorPimpl::AllocatorPimpl(const ALLOCATION_CALLBACKS& allocationCallbacks, const ALLOCATOR_DESC& desc) :

     m_UseMutex((desc.Flags & ALLOCATOR_FLAG_SINGLETHREADED) == 0),

+    m_AlwaysCommitted((desc.Flags & ALLOCATOR_FLAG_ALWAYS_COMMITTED) != 0),

     m_Device(desc.pDevice),

     m_PreferredBlockSize(desc.PreferredBlockSize != 0 ? desc.PreferredBlockSize : D3D12MA_DEFAULT_BLOCK_SIZE),

     m_AllocationCallbacks(allocationCallbacks),

@@ -3338,7 +3332,7 @@
 

     const UINT64 preferredBlockSize = blockVector->GetPreferredBlockSize();

     bool preferCommittedMemory =

-        D3D12MA_DEBUG_ALWAYS_COMMITTED ||

+        m_AlwaysCommitted ||

         PrefersCommittedAllocation(*pResourceDesc) ||

         // Heuristics: Allocate committed memory if requested size if greater than half of preferred block size.

         resAllocInfo.SizeInBytes > preferredBlockSize / 2;

@@ -3433,7 +3427,7 @@
 

     const UINT64 preferredBlockSize = blockVector->GetPreferredBlockSize();

     bool preferCommittedMemory =

-        D3D12MA_DEBUG_ALWAYS_COMMITTED ||

+        m_AlwaysCommitted ||

         // Heuristics: Allocate committed memory if requested size if greater than half of preferred block size.

         pAllocInfo->SizeInBytes > preferredBlockSize / 2;

     if(preferCommittedMemory &&

@@ -4039,6 +4033,7 @@
     m_Allocator = allocator;

     m_Type = TYPE_HEAP;

     m_Size = size;

+    m_Resource = NULL;

     m_Name = NULL;

     m_Heap.heapType = heapType;

     m_Heap.heap = heap;

diff --git a/src/D3D12MemAlloc.h b/src/D3D12MemAlloc.h
index c78a900..c7e91d2 100644
--- a/src/D3D12MemAlloc.h
+++ b/src/D3D12MemAlloc.h
@@ -539,6 +539,12 @@
     Using this flag may increase performance because internal mutexes are not used.

     */

     ALLOCATOR_FLAG_SINGLETHREADED = 0x1,

+

+    /**

+    Every allocation will have its own memory block.

+    To be used for debugging purposes.

+   */

+    ALLOCATOR_FLAG_ALWAYS_COMMITTED = 0x2,

 } ALLOCATOR_FLAGS;

 

 /// \brief Parameters of created Allocator object. To be used with CreateAllocator().

diff --git a/src/D3D12Sample.cpp b/src/D3D12Sample.cpp
index 9d8032d..aa7bfa0 100644
--- a/src/D3D12Sample.cpp
+++ b/src/D3D12Sample.cpp
@@ -48,6 +48,7 @@
 static const bool ENABLE_DEBUG_LAYER = true;

 static const bool ENABLE_CPU_ALLOCATION_CALLBACKS = true;

 static const bool ENABLE_CPU_ALLOCATION_CALLBACKS_PRINT = false;

+static constexpr D3D12MA::ALLOCATOR_FLAGS g_AllocatorFlags = D3D12MA::ALLOCATOR_FLAG_NONE;

 

 static HINSTANCE g_Instance;

 static HWND g_Wnd;

@@ -417,7 +418,7 @@
 

     {

         D3D12MA::ALLOCATOR_DESC desc = {};

-        desc.Flags = D3D12MA::ALLOCATOR_FLAG_NONE;

+        desc.Flags = g_AllocatorFlags;

         desc.pDevice = device;

 

         D3D12MA::ALLOCATION_CALLBACKS allocationCallbacks = {};

@@ -1376,6 +1377,7 @@
         TestContext ctx = {};

         ctx.device = g_Device;

         ctx.allocator = g_Allocator;

+        ctx.allocatorFlags = g_AllocatorFlags;

         Test(ctx);

     }

     catch(const std::exception& ex)

diff --git a/src/Tests.cpp b/src/Tests.cpp
index 2e69e95..19e11c7 100644
--- a/src/Tests.cpp
+++ b/src/Tests.cpp
@@ -217,6 +217,8 @@
 {

     wprintf(L"Test placed resources\n");

 

+    const bool alwaysCommitted = (ctx.allocatorFlags & D3D12MA::ALLOCATOR_FLAG_ALWAYS_COMMITTED) != 0;

+

     const UINT count = 4;

     const UINT64 bufSize = 32ull * 1024;

     ResourceWithAllocation resources[count];

@@ -240,7 +242,10 @@
         resources[i].allocation.reset(alloc);

 

         // Make sure it doesn't have implicit heap.

-        CHECK_BOOL( resources[i].allocation->GetHeap() != NULL );

+        if(!alwaysCommitted)

+        {

+            CHECK_BOOL( resources[i].allocation->GetHeap() != NULL );

+        }

     }

 

     // Make sure at least some of the resources belong to the same heap, but their memory ranges don't overlap.

@@ -260,7 +265,10 @@
             }

         }

     }

-    CHECK_BOOL(sameHeapFound);

+    if(!alwaysCommitted)

+    {

+        CHECK_BOOL(sameHeapFound);

+    }

 

     // Additionally create a texture to see if no error occurs due to bad handling of Resource Tier.

     resourceDesc = {};

@@ -347,7 +355,7 @@
 

     D3D12_RESOURCE_ALLOCATION_INFO allocInfo = {};

     allocInfo.Alignment = std::max(allocInfo1.Alignment, allocInfo2.Alignment);

-    allocInfo.SizeInBytes = std::max(allocInfo1.SizeInBytes, allocInfo2.SizeInBytes);

+    allocInfo.SizeInBytes = AlignUp(std::max(allocInfo1.SizeInBytes, allocInfo2.SizeInBytes), 64ull * 1024);

 

     D3D12MA::Allocation* allocPtr = NULL;

     CHECK_HR( ctx.allocator->AllocateMemory(

diff --git a/src/Tests.h b/src/Tests.h
index e67c5ee..8bbe247 100644
--- a/src/Tests.h
+++ b/src/Tests.h
@@ -28,6 +28,7 @@
 {

     ID3D12Device* device;

     D3D12MA::Allocator* allocator;

+    D3D12MA::ALLOCATOR_FLAGS allocatorFlags;

 };

 

 void Test(const TestContext& ctx);