Add internal structure Allocation::PackedData with bit fields to optimize memory size of Allocation class.

This commit is contained in:
Adam Sawicki 2020-03-16 16:36:37 +01:00
parent 4d27d19b4c
commit 38f53e8e4f
2 changed files with 47 additions and 27 deletions

View File

@ -1316,7 +1316,7 @@ void JsonWriter::WriteIndent(bool oneLess)
void JsonWriter::AddAllocationToObject(const Allocation& alloc)
{
WriteString(L"Type");
switch (alloc.m_ResourceDimension) {
switch (alloc.m_PackedData.GetResourceDimension()) {
case D3D12_RESOURCE_DIMENSION_UNKNOWN:
WriteString(L"UNKNOWN");
break;
@ -1342,15 +1342,15 @@ void JsonWriter::AddAllocationToObject(const Allocation& alloc)
WriteString(L"Name");
WriteString(name);
}
if(alloc.m_ResourceFlags)
if(alloc.m_PackedData.GetResourceFlags())
{
WriteString(L"Flags");
WriteNumber((UINT)alloc.m_ResourceFlags);
WriteNumber((UINT)alloc.m_PackedData.GetResourceFlags());
}
if(alloc.m_TextureLayout)
if(alloc.m_PackedData.GetTextureLayout())
{
WriteString(L"Layout");
WriteNumber((UINT)alloc.m_TextureLayout);
WriteNumber((UINT)alloc.m_PackedData.GetTextureLayout());
}
if(alloc.m_CreationFrameIndex)
{
@ -4074,7 +4074,7 @@ void AllocatorPimpl::UnregisterCommittedAllocation(Allocation* alloc, D3D12_HEAP
void AllocatorPimpl::FreeCommittedMemory(Allocation* allocation)
{
D3D12MA_ASSERT(allocation && allocation->m_Type == Allocation::TYPE_COMMITTED);
D3D12MA_ASSERT(allocation && allocation->m_PackedData.GetType() == Allocation::TYPE_COMMITTED);
UnregisterCommittedAllocation(allocation, allocation->m_Committed.heapType);
const UINT64 allocationSize = allocation->GetSize();
@ -4085,7 +4085,7 @@ void AllocatorPimpl::FreeCommittedMemory(Allocation* allocation)
void AllocatorPimpl::FreePlacedMemory(Allocation* allocation)
{
D3D12MA_ASSERT(allocation && allocation->m_Type == Allocation::TYPE_PLACED);
D3D12MA_ASSERT(allocation && allocation->m_PackedData.GetType() == Allocation::TYPE_PLACED);
NormalBlock* const block = allocation->m_Placed.block;
D3D12MA_ASSERT(block);
@ -4097,7 +4097,7 @@ void AllocatorPimpl::FreePlacedMemory(Allocation* allocation)
void AllocatorPimpl::FreeHeapMemory(Allocation* allocation)
{
D3D12MA_ASSERT(allocation && allocation->m_Type == Allocation::TYPE_HEAP);
D3D12MA_ASSERT(allocation && allocation->m_PackedData.GetType() == Allocation::TYPE_HEAP);
UnregisterCommittedAllocation(allocation, allocation->m_Heap.heapType);
SAFE_RELEASE(allocation->m_Heap.heap);
@ -4525,7 +4525,7 @@ void Allocation::Release()
SAFE_RELEASE(m_Resource);
switch(m_Type)
switch(m_PackedData.GetType())
{
case TYPE_COMMITTED:
m_Allocator->FreeCommittedMemory(this);
@ -4545,7 +4545,7 @@ void Allocation::Release()
UINT64 Allocation::GetOffset() const
{
switch(m_Type)
switch(m_PackedData.GetType())
{
case TYPE_COMMITTED:
case TYPE_HEAP:
@ -4560,7 +4560,7 @@ UINT64 Allocation::GetOffset() const
ID3D12Heap* Allocation::GetHeap() const
{
switch(m_Type)
switch(m_PackedData.GetType())
{
case TYPE_COMMITTED:
return NULL;
@ -4588,14 +4588,11 @@ void Allocation::SetName(LPCWSTR Name)
Allocation::Allocation(AllocatorPimpl* allocator, UINT64 size) :
m_Allocator{allocator},
m_Type{TYPE_COUNT},
m_Size{size},
m_Resource{NULL},
m_ResourceDimension{D3D12_RESOURCE_DIMENSION_UNKNOWN},
m_ResourceFlags{D3D12_RESOURCE_FLAG_NONE},
m_TextureLayout{D3D12_TEXTURE_LAYOUT_UNKNOWN},
m_CreationFrameIndex{allocator->GetCurrentFrameIndex()},
m_Name{NULL}
m_Name{NULL},
m_PackedData{TYPE_COUNT, D3D12_RESOURCE_DIMENSION_UNKNOWN, D3D12_RESOURCE_FLAG_NONE, D3D12_TEXTURE_LAYOUT_UNKNOWN}
{
D3D12MA_ASSERT(allocator);
}
@ -4607,20 +4604,20 @@ Allocation::~Allocation()
void Allocation::InitCommitted(D3D12_HEAP_TYPE heapType)
{
m_Type = TYPE_COMMITTED;
m_PackedData.SetType(TYPE_COMMITTED);
m_Committed.heapType = heapType;
}
void Allocation::InitPlaced(UINT64 offset, UINT64 alignment, NormalBlock* block)
{
m_Type = TYPE_PLACED;
m_PackedData.SetType(TYPE_PLACED);
m_Placed.offset = offset;
m_Placed.block = block;
}
void Allocation::InitHeap(D3D12_HEAP_TYPE heapType, ID3D12Heap* heap)
{
m_Type = TYPE_HEAP;
m_PackedData.SetType(TYPE_HEAP);
m_Heap.heapType = heapType;
m_Heap.heap = heap;
}
@ -4630,9 +4627,9 @@ void Allocation::SetResource(ID3D12Resource* resource, const D3D12_RESOURCE_DESC
D3D12MA_ASSERT(m_Resource == NULL);
D3D12MA_ASSERT(pResourceDesc);
m_Resource = resource;
m_ResourceDimension = pResourceDesc->Dimension;
m_ResourceFlags = pResourceDesc->Flags;
m_TextureLayout = pResourceDesc->Layout;
m_PackedData.SetResourceDimension(pResourceDesc->Dimension);
m_PackedData.SetResourceFlags(pResourceDesc->Flags);
m_PackedData.SetTextureLayout(pResourceDesc->Layout);
}
void Allocation::FreeName()

View File

@ -514,19 +514,17 @@ private:
template<typename T> friend void D3D12MA_DELETE(const ALLOCATION_CALLBACKS&, T*);
template<typename T> friend class PoolAllocator;
AllocatorPimpl* m_Allocator;
enum Type
{
TYPE_COMMITTED,
TYPE_PLACED,
TYPE_HEAP,
TYPE_COUNT
} m_Type;
};
AllocatorPimpl* m_Allocator;
UINT64 m_Size;
ID3D12Resource* m_Resource;
D3D12_RESOURCE_DIMENSION m_ResourceDimension;
D3D12_RESOURCE_FLAGS m_ResourceFlags;
D3D12_TEXTURE_LAYOUT m_TextureLayout;
UINT m_CreationFrameIndex;
wchar_t* m_Name;
@ -550,6 +548,31 @@ private:
} m_Heap;
};
struct PackedData
{
public:
PackedData() :
m_Type(0), m_ResourceDimension(0), m_ResourceFlags(0), m_TextureLayout(0) { }
PackedData(Type type, D3D12_RESOURCE_DIMENSION resourceDimension, D3D12_RESOURCE_FLAGS resourceFlags, D3D12_TEXTURE_LAYOUT textureLayout) :
m_Type(type), m_ResourceDimension(resourceDimension), m_ResourceFlags(resourceFlags), m_TextureLayout(textureLayout) { }
Type GetType() const { return (Type)m_Type; }
D3D12_RESOURCE_DIMENSION GetResourceDimension() const { return (D3D12_RESOURCE_DIMENSION)m_ResourceDimension; }
D3D12_RESOURCE_FLAGS GetResourceFlags() const { return (D3D12_RESOURCE_FLAGS)m_ResourceFlags; }
D3D12_TEXTURE_LAYOUT GetTextureLayout() const { return (D3D12_TEXTURE_LAYOUT)m_TextureLayout; }
void SetType(Type type) { m_Type = (UINT)type; }
void SetResourceDimension(D3D12_RESOURCE_DIMENSION resourceDimension) { m_ResourceDimension = (UINT)resourceDimension; }
void SetResourceFlags(D3D12_RESOURCE_FLAGS resourceFlags) { m_ResourceFlags = (UINT)resourceFlags; }
void SetTextureLayout(D3D12_TEXTURE_LAYOUT textureLayout) { m_TextureLayout = (UINT)textureLayout; }
private:
UINT m_Type : 2; // enum Type
UINT m_ResourceDimension : 3; // enum D3D12_RESOURCE_DIMENSION
UINT m_ResourceFlags : 7; // flags D3D12_RESOURCE_FLAGS
UINT m_TextureLayout : 2; // enum D3D12_TEXTURE_LAYOUT
} m_PackedData;
Allocation(AllocatorPimpl* allocator, UINT64 size);
~Allocation();
void InitCommitted(D3D12_HEAP_TYPE heapType);