返回排行榜

GPUOpen-LibrariesAndSDKs/D3D12MemoryAllocator

C++

Easy to integrate memory allocation library for Direct3D 12

memory-managementdirect3d12d3d12directx12directxdirectx-12
Star 增长趋势
Star
1k
Forks
115
周增长
Issues
2
5001k
2019年9月2021年12月2024年4月2026年7月
README

D3D12 Memory Allocator

Easy to integrate memory allocation library for Direct3D 12.

Documentation: Browse online: D3D12 Memory Allocator (generated from Doxygen-style comments in include/D3D12MemAlloc.h)

License: MIT. See LICENSE.txt

Changelog: See CHANGELOG.md

Product page: D3D12 Memory Allocator on GPUOpen

Average time to resolve an issue

Problem

Memory 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 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.

Features

This library can help developers to manage memory allocations and resource creation by offering function Allocator::CreateResource similar to the standard ID3D12Device::CreateCommittedResource. It internally:

  • 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.
  • Automatically respects size and alignment requirements for created resources.
  • 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.

Additional features:

  • Well-documented - description of all classes and functions provided, along with chapters that contain general description and example code.
  • Thread-safety: Library is designed to be used in multithreaded code.
  • Configuration: Fill optional members of ALLOCATOR_DESC structure to provide custom CPU memory allocator and other parameters.
  • Customization and integration with custom engines: Predefine appropriate macros to provide your own implementation of external facilities used by the library, like assert, mutex, and atomic.
  • Support for resource aliasing (overlap).
  • Custom memory pools: Create a pool with desired parameters (e.g. fixed or limited maximum size, custom D3D12_HEAP_PROPERTIES and D3D12_HEAP_FLAGS) and allocate memory out of it.
  • Support for GPU Upload Heaps from preview Agility SDK (needs compilation with D3D12MA_OPTIONS16_SUPPORTED macro).
  • Linear allocator: Create a pool with linear algorithm and use it for much faster allocations and deallocations in free-at-once, stack, double stack, or ring buffer fashion.
  • Defragmentation: Let the library move data around to free some memory blocks and make your allocations better compacted.
  • Statistics: Obtain brief or detailed statistics about the amount of memory used, unused, number of allocated heaps, number of allocations etc. - globally and per memory heap type. Current memory usage and budget as reported by the system can also be queried.
  • Debug annotations: Associate custom void* pPrivateData and debug LPCWSTR pName with each allocation.
  • JSON dump: Obtain a string in JSON format with detailed map of internal state, including list of allocations, their string names, and gaps between them.
  • Convert this JSON dump into a picture to visualize your memory. See tools/GpuMemDumpVis.
  • Virtual allocator - an API that exposes the core allocation algorithm to be used without allocating real GPU memory, to allocate your own stuff, e.g. sub-allocate pieces of one large buffer.

Prerequisites

  • Self-contained C++ library in single pair of H + CPP files. No external dependencies other than standard C, C++ library and Windows SDK. Some features of C++14 used. STL containers, C++ exceptions, and RTTI are not used.
  • Object-oriented interface in a convention similar to D3D12.
  • Error handling implemented by returning HRESULT error codes - same way as in D3D12.
  • Interface documented using Doxygen-style comments.

Example

Basic 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:

D3D12_RESOURCE_DESC resourceDesc = {};
resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
resourceDesc.Alignment = 0;
resourceDesc.Width = 1024;
resourceDesc.Height = 1024;
resourceDesc.DepthOrArraySize = 1;
resourceDesc.MipLevels = 1;
resourceDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
resourceDesc.SampleDesc.Count = 1;
resourceDesc.SampleDesc.Quality = 0;
resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
resourceDesc.Flags = D3D12_RESOURCE_FLAG_NONE;

D3D12MA::ALLOCATION_DESC allocDesc = {};
allocDesc.HeapType = D3D12_HEAP_TYPE_DEFAULT;

D3D12Resource* resource;
D3D12MA::Allocation* allocation;
HRESULT hr = allocator->CreateResource(
    &allocDesc, &resourceDesc,
    D3D12_RESOURCE_STATE_COPY_DEST, NULL,
    &allocation, IID_PPV_ARGS(&resource));

With this one function call:

  1. ID3D12Heap memory block is allocated if needed.
  2. An unused region of the memory block is reserved for the allocation.
  3. ID3D12Resource is created as placed resource, bound to this region.

Allocation is an object that represents memory assigned to this texture. It can be queried for parameters like offset and size.

Binaries

The release comes with precompiled binary executable for "D3D12Sample" application which contains test suite. It is compiled using Visual Studio 2022, so it requires appropriate libraries to work, including "MSVCP140.dll", "VCRUNTIME140.dll", "VCRUNTIME140_1.dll". If its launch fails with error message telling about those files missing, please download and install Microsoft Visual C++ Redistributable, "X64" version.

Copyright notice

This software package uses third party software:

For more information see NOTICES.txt.

See also

Software using this library

Some other projects on GitHub and some game development studios that use DX12 in their games.

相关仓库
mem0ai/mem0

Universal memory layer for AI Agents

TypeScriptnpmApache License 2.0aichatgpt
mem0.ai
61.4k7.1k
topoteretes/cognee

Cognee is the open-source AI memory platform for agents. Give your AI agents persistent long-term memory across sessions with a self-hosted knowledge graph engine.

PythonPyPIApache License 2.0aicognitive-architecture
cognee.ai
29k2.8k
MemoriLabs/Memori

Memori is agent-native memory infrastructure. A LLM-agnostic layer that turns agent execution and conversation into structured, persistent state for production systems. Built for enterprise, Memori works with the data infrastructure you already run, no rip-and-replace, and deploys across managed cloud, single-tenant cloud, VPC, and on-premises.

PythonPyPIOtheragentai
memorilabs.ai
15.6k2.9k
EverMind-AI/EverOS

One portable memory layer for every AI agent: local-first, Markdown-native, user-owned, and self-evolving across apps, tools, and workflows.

PythonPyPIApache License 2.0agentic-aiai
evermind.ai/everos
11.4k864
MemTensor/MemOS

Self-evolving memory OS for LLM & AI Agents: ultra-persistent memory, hybrid-retrieval, and cross-task skill reuse, with 35.24% token savings

TypeScriptnpmApache License 2.0agentllm
memos.openmem.net
10.3k942
henrypp/memreduct

Lightweight real-time memory management application to monitor and clean system memory on your computer.

CGNU General Public License v3.0memory-managementmemory-monitoring
9.9k577
IgorMundstein/WinMemoryCleaner

This free RAM cleaner uses native Windows features to optimize memory areas. It's a compact, portable, and smart application.

C#GNU General Public License v3.0memory-cleanermemory
4.8k297
bdwgc/bdwgc

The Boehm-Demers-Weiser conservative C/C++ Garbage Collector (bdwgc, also known as bdw-gc, boehm-gc, libgc)

COthergarbage-collectionmemory-management
hboehm.info/gc/
3.5k441
GPUOpen-LibrariesAndSDKs/VulkanMemoryAllocator

Easy to integrate Vulkan memory allocation library

CMIT Licensevulkanmemory-management
3.4k447
MemMachine/MemMachine

Universal memory layer for AI Agents. It provides scalable, extensible, and interoperable memory storage and retrieval to streamline AI agent state management for next-generation autonomous systems.

PythonPyPIApache License 2.0aimemory
memmachine.ai
3.3k197
awslabs/agentcore-samples

Amazon Bedrock Agentcore accelerates AI agents into production with the scale, reliability, and security, critical to real-world deployment.

PythonPyPIApache License 2.0agentagentic-ai
aws.amazon.com/bedrock/agentcore/
3.2k1.2k
ivmai/bdwgc

The Boehm-Demers-Weiser conservative C/C++ Garbage Collector (bdwgc, also known as bdw-gc, boehm-gc, libgc)

COthergarbage-collectionmemory-management
hboehm.info/gc/
3.1k413