Add support for custom pools to AllocateMemory and CreateAlisingResource
diff --git a/src/D3D12MemAlloc.cpp b/src/D3D12MemAlloc.cpp
index 28724dc..60c1d22 100644
--- a/src/D3D12MemAlloc.cpp
+++ b/src/D3D12MemAlloc.cpp
@@ -3950,7 +3950,7 @@
                 ppvResource);

         }

 

-        BlockVector* blockVector = m_BlockVectors[defaultPoolIndex];

+        BlockVector* const blockVector = m_BlockVectors[defaultPoolIndex];

         D3D12MA_ASSERT(blockVector);

 

         const UINT64 preferredBlockSize = blockVector->GetPreferredBlockSize();

@@ -4014,52 +4014,66 @@
 {

     *ppAllocation = NULL;

 

-    if(!IsHeapTypeValid(pAllocDesc->HeapType))

+    if(pAllocDesc->CustomPool != NULL)

     {

-        return E_INVALIDARG;

-    }

-

-    ALLOCATION_DESC finalAllocDesc = *pAllocDesc;

-

-    const UINT defaultPoolIndex = CalcDefaultPoolIndex(*pAllocDesc);

-    bool requireCommittedMemory = (defaultPoolIndex == UINT32_MAX);

-    if(requireCommittedMemory)

-    {

-        return AllocateHeap(&finalAllocDesc, *pAllocInfo, ppAllocation);

-    }

-

-    BlockVector* blockVector = m_BlockVectors[defaultPoolIndex];

-    D3D12MA_ASSERT(blockVector);

-

-    const UINT64 preferredBlockSize = blockVector->GetPreferredBlockSize();

-    const bool preferCommittedMemory =

-        m_AlwaysCommitted ||

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

-        pAllocInfo->SizeInBytes > preferredBlockSize / 2;

-    if(preferCommittedMemory &&

-        (finalAllocDesc.Flags & ALLOCATION_FLAG_NEVER_ALLOCATE) == 0)

-    {

-        finalAllocDesc.Flags |= ALLOCATION_FLAG_COMMITTED;

-    }

-

-    if((finalAllocDesc.Flags & ALLOCATION_FLAG_COMMITTED) != 0)

-    {

-        return AllocateHeap(&finalAllocDesc, *pAllocInfo, ppAllocation);

+        BlockVector* const blockVector = pAllocDesc->CustomPool->m_Pimpl->GetBlockVector();

+        D3D12MA_ASSERT(blockVector);

+        return blockVector->Allocate(

+            pAllocInfo->SizeInBytes,

+            pAllocInfo->Alignment,

+            *pAllocDesc,

+            1,

+            (Allocation**)ppAllocation);

     }

     else

     {

-        HRESULT hr = blockVector->Allocate(

-            pAllocInfo->SizeInBytes,

-            pAllocInfo->Alignment,

-            finalAllocDesc,

-            1,

-            (Allocation**)ppAllocation);

-        if(SUCCEEDED(hr))

+        if(!IsHeapTypeValid(pAllocDesc->HeapType))

         {

-            return hr;

+            return E_INVALIDARG;

         }

 

-        return AllocateHeap(&finalAllocDesc, *pAllocInfo, ppAllocation);

+        ALLOCATION_DESC finalAllocDesc = *pAllocDesc;

+

+        const UINT defaultPoolIndex = CalcDefaultPoolIndex(*pAllocDesc);

+        bool requireCommittedMemory = (defaultPoolIndex == UINT32_MAX);

+        if(requireCommittedMemory)

+        {

+            return AllocateHeap(&finalAllocDesc, *pAllocInfo, ppAllocation);

+        }

+

+        BlockVector* blockVector = m_BlockVectors[defaultPoolIndex];

+        D3D12MA_ASSERT(blockVector);

+

+        const UINT64 preferredBlockSize = blockVector->GetPreferredBlockSize();

+        const bool preferCommittedMemory =

+            m_AlwaysCommitted ||

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

+            pAllocInfo->SizeInBytes > preferredBlockSize / 2;

+        if(preferCommittedMemory &&

+            (finalAllocDesc.Flags & ALLOCATION_FLAG_NEVER_ALLOCATE) == 0)

+        {

+            finalAllocDesc.Flags |= ALLOCATION_FLAG_COMMITTED;

+        }

+

+        if((finalAllocDesc.Flags & ALLOCATION_FLAG_COMMITTED) != 0)

+        {

+            return AllocateHeap(&finalAllocDesc, *pAllocInfo, ppAllocation);

+        }

+        else

+        {

+            HRESULT hr = blockVector->Allocate(

+                pAllocInfo->SizeInBytes,

+                pAllocInfo->Alignment,

+                finalAllocDesc,

+                1,

+                (Allocation**)ppAllocation);

+            if(SUCCEEDED(hr))

+            {

+                return hr;

+            }

+

+            return AllocateHeap(&finalAllocDesc, *pAllocInfo, ppAllocation);

+        }

     }

 }

 

diff --git a/src/Tests.cpp b/src/Tests.cpp
index 7cfda89..b2423f2 100644
--- a/src/Tests.cpp
+++ b/src/Tests.cpp
@@ -420,6 +420,8 @@
     CHECK_HR( ctx.allocator->CreatePool(&poolDesc, &poolPtr) );

     PoolUniquePtr pool{poolPtr};

 

+    D3D12MA::Allocation* allocPtr;

+

     // # Create buffers 2x 5 MB

 

     D3D12MA::ALLOCATION_DESC allocDesc = {};

@@ -433,7 +435,6 @@
     AllocationUniquePtr allocs[4];

     for(uint32_t i = 0; i < 2; ++i)

     {

-        D3D12MA::Allocation* allocPtr;

         CHECK_HR( ctx.allocator->CreateResource(&allocDesc, &resDesc,

             D3D12_RESOURCE_STATE_GENERIC_READ,

             NULL, // pOptimizedClearValue

@@ -449,7 +450,6 @@
         allocDesc.Flags = i == 0 ?

             D3D12MA::ALLOCATION_FLAG_NEVER_ALLOCATE:

             D3D12MA::ALLOCATION_FLAG_COMMITTED;

-        D3D12MA::Allocation* allocPtr;

         const HRESULT hr = ctx.allocator->CreateResource(&allocDesc, &resDesc,

             D3D12_RESOURCE_STATE_GENERIC_READ,

             NULL, // pOptimizedClearValue

@@ -463,7 +463,6 @@
     allocDesc.Flags = D3D12MA::ALLOCATION_FLAG_NONE;

     for(uint32_t i = 2; i < 5; ++i)

     {

-        D3D12MA::Allocation* allocPtr;

         HRESULT hr = ctx.allocator->CreateResource(&allocDesc, &resDesc,

             D3D12_RESOURCE_STATE_GENERIC_READ,

             NULL, // pOptimizedClearValue

@@ -479,6 +478,27 @@
             CHECK_BOOL( FAILED(hr) );

         }

     }

+

+    // # Make room, AllocateMemory, CreateAliasingResource

+

+    allocs[3].reset();

+    allocs[0].reset();

+

+    D3D12_RESOURCE_ALLOCATION_INFO resAllocInfo = {};

+    resAllocInfo.SizeInBytes = 5 * MEGABYTE;

+    resAllocInfo.Alignment = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT;

+

+    CHECK_HR( ctx.allocator->AllocateMemory(&allocDesc, &resAllocInfo, &allocPtr) );

+    allocs[0].reset(allocPtr);

+

+    resDesc.Width = 1 * MEGABYTE;

+    CComPtr<ID3D12Resource> res;

+    CHECK_HR( ctx.allocator->CreateAliasingResource(allocs[0].get(),

+        0, // AllocationLocalOffset

+        &resDesc,

+        D3D12_RESOURCE_STATE_GENERIC_READ,

+        NULL, // pOptimizedClearValue

+        IID_PPV_ARGS(&res)) );

 }

 

 static void TestAliasingMemory(const TestContext& ctx)