• Home
Name Date Size #Lines LOC

..--

bin/12-May-2024-

docs/gfx/12-May-2024-

premake/12-May-2024-8466

src/12-May-2024-13,91410,048

tools/D3d12maDumpVis/12-May-2024-285228

.gitignoreD12-May-202420 32

CHANGELOG.mdD12-May-202454 42

LICENSE.txtD12-May-20241.1 KiB2016

NOTICES.txtD12-May-20244.3 KiB8262

README.mdD12-May-20246.5 KiB11880

README.md

1# D3D12 Memory Allocator
2
3Easy to integrate memory allocation library for Direct3D 12.
4
5**License:** MIT. See [LICENSE.txt](LICENSE.txt)
6
7**Changelog:** See [CHANGELOG.md](CHANGELOG.md)
8
9**Product page:** [D3D12 Memory Allocator on GPUOpen](https://gpuopen.com/gaming-product/d3d12-memory-allocator/)
10
11**Build status:**
12
13Windows: [![Build status](https://ci.appveyor.com/api/projects/status/860i07bxv55ydgvg?svg=true)](https://ci.appveyor.com/project/adam-sawicki-amd/d3d12memoryallocator)
14
15# Problem
16
17Memory allocation and resource (buffer and texture) creation in new, explicit graphics APIs (Vulkan® and Direct3D 12) is difficult comparing to older graphics APIs like Direct3D 11 or OpenGL® because it is recommended to allocate bigger blocks of memory and assign parts of them to resources. [Vulkan Memory Allocator](https://github.com/GPUOpen-LibrariesAndSDKs/VulkanMemoryAllocator/) is a library that implements this functionality for Vulkan. It is available online since 2017 and it is successfully used in many software projects, including some AAA game studios. This is an equivalent library for D3D12.
18
19# Features
20
21This library can help developers to manage memory allocations and resource creation by offering function `Allocator::CreateResource` similar to the standard `ID3D12Device::CreateCommittedResource`. It internally:
22
23- Allocates and keeps track of bigger memory heaps, used and unused ranges inside them, finds best matching unused ranges to create new resources there as placed resources.
24- Automatically respects size and alignment requirements for created resources.
25- Automatically handles resource heap tier - whether it's `D3D12_RESOURCE_HEAP_TIER_1` that requires to keep certain classes of resources separate or `D3D12_RESOURCE_HEAP_TIER_2` that allows to keep them all together.
26
27Additional features:
28
29- Support for resource aliasing (overlap).
30- Virtual allocator - possibility to use core allocation algorithm without using real GPU memory, to allocate your own stuff, e.g. sub-allocate pieces of one large buffer.
31- Well-documented - description of all classes and functions provided, along with chapters that contain general description and example code.
32- Thread-safety: Library is designed to be used in multithreaded code.
33- Configuration: Fill optional members of `ALLOCATOR_DESC` structure to provide custom CPU memory allocator and other parameters.
34- Customization: Predefine appropriate macros to provide your own implementation of external facilities used by the library, like assert, mutex, and atomic.
35- Statistics: Obtain detailed statistics about the amount of memory used, unused, number of allocated blocks, number of allocations etc. - globally and per memory heap type.
36- Debug annotations: Associate string name with every allocation.
37- JSON dump: Obtain a string in JSON format with detailed map of internal state, including list of allocations and gaps between them.
38
39# Prerequisites
40
41- Self-contained C++ library in single pair of H + CPP files. No external dependencies other than standard C, C++ library and Windows SDK. STL containers, C++ exceptions, and RTTI are not used.
42- Object-oriented interface in a convention similar to D3D12.
43- Error handling implemented by returning `HRESULT` error codes - same way as in D3D12.
44- Interface documented using Doxygen-style comments.
45
46# Example
47
48Basic usage of this library is very simple. Advanced features are optional. After you created global `Allocator` object, a complete code needed to create a texture may look like this:
49
50```cpp
51D3D12_RESOURCE_DESC resourceDesc = {};
52resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
53resourceDesc.Alignment = 0;
54resourceDesc.Width = 1024;
55resourceDesc.Height = 1024;
56resourceDesc.DepthOrArraySize = 1;
57resourceDesc.MipLevels = 1;
58resourceDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
59resourceDesc.SampleDesc.Count = 1;
60resourceDesc.SampleDesc.Quality = 0;
61resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
62resourceDesc.Flags = D3D12_RESOURCE_FLAG_NONE;
63
64D3D12MA::ALLOCATION_DESC allocationDesc = {};
65allocDesc.HeapType = D3D12_HEAP_TYPE_DEFAULT;
66
67D3D12Resource* resource;
68D3D12MA::Allocation* allocation;
69HRESULT hr = allocator->CreateResource(
70    &allocationDesc,
71    &resourceDesc,
72    D3D12_RESOURCE_STATE_COPY_DEST,
73    NULL,
74    &allocation,
75    IID_PPV_ARGS(&resource));
76```
77
78With this one function call:
79
801. `ID3D12Heap` memory block is allocated if needed.
812. An unused region of the memory block assigned.
823. `ID3D12Resource` is created as placed resource, bound to this region.
83
84`Allocation` is an object that represents memory assigned to this texture. It can be queried for parameters like offset and size.
85
86# Binaries
87
88The release comes with precompiled binary executable for "D3D12Sample" application which contains test suite. It is compiled using Visual Studio 2017, so it requires appropriate libraries to work, including "vcruntime140.dll" and "msvcp140.dll". If its launch fails with error message telling about those files missing, please download and install [Microsoft Visual C++ Redistributable for Visual Studio 2015, 2017 and 2019](https://support.microsoft.com/en-us/help/2977003/the-latest-supported-visual-c-downloads), "x64" version.
89
90# Documentation
91
92Documentation is provided together with the library in form of Doxygen-style comments inside the code, in file "src/D3D12MemAlloc.h". They can be read directly or turned into HTML and other convenient to read formats. Unfortunately we can't provide pregenerated version browseable online. You can generate it on your own by following these steps:
93
941. Install [Doxygen](http://www.doxygen.nl/).
952. Open command line and enter "src" directory.
963. Type command: `doxygen`
974. Open following file in a web browser: "docs/html/index.html".
98
99# Copyright notice
100
101This software package uses third party software:
102
103- Parts of the code of [Vulkan Memory Allocator](https://github.com/GPUOpen-LibrariesAndSDKs/VulkanMemoryAllocator/) by AMD, license: MIT
104- Parts of the code of [DirectX-Graphics-Samples](https://github.com/microsoft/DirectX-Graphics-Samples) by Microsoft, license: MIT
105- [Premake 5](https://premake.github.io/) binary, license: BSD
106
107For more information see [NOTICES.txt](NOTICES.txt).
108
109# Software using this library
110
111- **[The Forge](https://github.com/ConfettiFX/The-Forge)** - cross-platform rendering framework. Apache License 2.0.
112
113[Some other projects on GitHub](https://github.com/search?q=D3D12MemAlloc.h&type=Code) and some game development studios that use DX12 in their games.
114
115# See also
116
117- **[Vulkan Memory Allocator](https://github.com/GPUOpen-LibrariesAndSDKs/VulkanMemoryAllocator/)** - equivalent library for Vulkan. License: MIT.
118