Add ALLOCATOR_FLAG_ALWAYS_COMMITTED, remove debug macro D3D12MA_DEBUG_ALWAYS_COMMITTED

Also fixed nasty bug with uninitialized member of Allocation class.
This commit is contained in:
Adam Sawicki 2019-10-23 15:21:21 +02:00
parent 42693ff059
commit 6deedf96b5
5 changed files with 27 additions and 15 deletions

View File

@ -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 @@ private:
*/
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 @@ void BlockVector::WriteBlockInfoToJson(JsonWriter& json)
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 @@ HRESULT AllocatorPimpl::CreateResource(
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 @@ HRESULT AllocatorPimpl::AllocateMemory(
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 @@ void Allocation::InitHeap(AllocatorPimpl* allocator, UINT64 size, D3D12_HEAP_TYP
m_Allocator = allocator;
m_Type = TYPE_HEAP;
m_Size = size;
m_Resource = NULL;
m_Name = NULL;
m_Heap.heapType = heapType;
m_Heap.heap = heap;

View File

@ -539,6 +539,12 @@ typedef enum ALLOCATOR_FLAGS
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().

View File

@ -48,6 +48,7 @@ static const D3D_FEATURE_LEVEL MY_D3D_FEATURE_LEVEL = D3D_FEATURE_LEVEL_12_0;
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 @@ void InitD3D() // initializes direct3d 12
{
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 @@ static void ExecuteTests()
TestContext ctx = {};
ctx.device = g_Device;
ctx.allocator = g_Allocator;
ctx.allocatorFlags = g_AllocatorFlags;
Test(ctx);
}
catch(const std::exception& ex)

View File

@ -217,6 +217,8 @@ static void TestPlacedResources(const TestContext& ctx)
{
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 @@ static void TestPlacedResources(const TestContext& ctx)
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 @@ static void TestPlacedResources(const TestContext& ctx)
}
}
}
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 @@ static void TestAliasingMemory(const TestContext& ctx)
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(

View File

@ -28,6 +28,7 @@ struct TestContext
{
ID3D12Device* device;
D3D12MA::Allocator* allocator;
D3D12MA::ALLOCATOR_FLAGS allocatorFlags;
};
void Test(const TestContext& ctx);