Direct3D 12 Memory Allocator
|
This is a small, standalone C++ library. It consists of a pair of 2 files: "D3D12MemAlloc.h" header file with public interface and "D3D12MemAlloc.cpp" with internal implementation. The only external dependencies are WinAPI, Direct3D 12, and parts of C/C++ standard library (but STL containers, exceptions, or RTTI are not used).
The library is developed and tested using Microsoft Visual Studio 2019, but it should work with other compilers as well. It is designed for 64-bit code.
To use the library in your project:
(1.) Copy files D3D12MemAlloc.cpp
, D3D12MemAlloc.h
to your project.
(2.) Make D3D12MemAlloc.cpp
compiling as part of the project, as C++ code.
(3.) Include library header in each CPP file that needs to use the library.
(4.) Right after you created ID3D12Device
, fill D3D12MA::ALLOCATOR_DESC structure and call function D3D12MA::CreateAllocator to create the main D3D12MA::Allocator object.
Please note that all symbols of the library are declared inside D3D12MA namespace.
(5.) Right before destroying the D3D12 device, destroy the allocator object.
Objects of this library must be destroyed by calling Release
method. They are somewhat compatible with COM: they implement IUnknown
interface with its virtual methods: AddRef
, Release
, QueryInterface
, and they are reference-counted internally. You can use smart pointers designed for COM with objects of this library - e.g. CComPtr
or Microsoft::WRL::ComPtr
. The reference counter is thread-safe. QueryInterface
method supports only IUnknown
, as classes of this library don't define their own GUIDs.
To use the library for creating resources (textures and buffers), call method D3D12MA::Allocator::CreateResource in the place where you would previously call ID3D12Device::CreateCommittedResource
.
The function has similar syntax, but it expects structure D3D12MA::ALLOCATION_DESC to be passed along with D3D12_RESOURCE_DESC
and other parameters for created resource. This structure describes parameters of the desired memory allocation, including choice of D3D12_HEAP_TYPE
.
The function also returns a new object of type D3D12MA::Allocation, created along with usual ID3D12Resource
. It represents allocated memory and can be queried for size, offset, ID3D12Resource
, and ID3D12Heap
if needed.
You need to remember both resource and allocation objects and destroy them separately when no longer needed.
The advantage of using the allocator instead of creating committed resource, and the main purpose of this library, is that it can decide to allocate bigger memory heap internally using ID3D12Device::CreateHeap
and place multiple resources in it, at different offsets, using ID3D12Device::CreatePlacedResource
. The library manages its own collection of allocated memory blocks (heaps) and remembers which parts of them are occupied and which parts are free to be used for new resources.
It is important to remember that resources created as placed don't have their memory initialized to zeros, but may contain garbage data, so they need to be fully initialized before usage, e.g. using Clear (ClearRenderTargetView
), Discard (DiscardResource
), or copy (CopyResource
).
The library also automatically handles resource heap tier. When D3D12_FEATURE_DATA_D3D12_OPTIONS::ResourceHeapTier
equals D3D12_RESOURCE_HEAP_TIER_1
, resources of 3 types: buffers, textures that are render targets or depth-stencil, and other textures must be kept in separate heaps. When D3D12_RESOURCE_HEAP_TIER_2
, they can be kept together. By using this library, you don't need to handle this manually.
The process of getting regular CPU-side pointer to the memory of a resource in Direct3D is called "mapping". There are rules and restrictions to this process, as described in D3D12 documentation of ID3D12Resource::Map
method.
Mapping happens on the level of particular resources, not entire memory heaps, and so it is out of scope of this library. Just as the documentation of the Map
function says:
When using this library, you can map and use your resources normally without considering whether they are created as committed resources or placed resources in one large heap.
Example for buffer created and filled in UPLOAD
heap type: