Skip to content

Memory Management

Chuck Walbourn edited this page Apr 26, 2022 · 9 revisions
DirectXTK

In DirectX 12, the application is responsible for much of the graphics memory management much as it is for system memory. This includes managing CPU/GPU synchronization and ensuring that resources are not modified by the CPU while the GPU is using them or vice-versa.

For the DirectX Tool Kit for DirectX 12 this is primarily handled through two classes: GraphicsMemory and ResourceUploadBatch.

Constant buffers

The primary use of the GraphicsMemory class is to allocate constant buffers from a D3D12_HEAP_TYPE_UPLOAD heap. CBs can persist for many frames, or be used for a single frame, but they must remain unchanged until their usage is completed. GraphicsMemory tracks this using a per-frame fence which is reclaimed each frame as they complete during the Present loop. CBs are in memory shared between the CPU and GPU as they are used for communication between CPU code and GPU shaders.

Index/Vertex buffers

Vertex and index buffers have two general usage cases: 'dynamic' or 'static'.

In the 'dynamic' case, the contents are generated by the CPU on a per-frame (or every few frames), and is implemented in the DirectX Tool Kit by using the GraphicsMemory class. There is a performance impact to reading VB/IB data from D3D12_HEAP_TYPE_UPLOAD heap, but this is also the simplest case to manage. This is used for VB/IB data for GeometricPrimitive and PrimitiveBatch, and the VB data for SpriteBatch.

In the 'static' case, the content is uploaded to the GPU once to be used over and over again, and is created as a ID3D12Resource interface with committed memory in the D3D12_HEAP_TYPE_DEFAULT heap. The data has to be uploaded via a D3D12_HEAP_TYPE_UPLOAD heap such as that used by GraphicsMemory or ResourceUploadBatch since D3D12_HEAP_TYPE_DEFAULT heap memory is not necessarily accessible by the CPU. A static IB is used by SpriteBatch.

The ModelMeshPart class makes use of both modes. When the model data is first loaded, it is placed into the GraphicsMemory class' D3D12_HEAP_TYPE_UPLOAD heap and can be used to render "as is". Optionally, you can upload this data to a 'static' VB/IB by using the LoadStaticBuffers method on Model which provides better performance.

Textures

Typically textures are created as ID3D12Resource interfaces as committed memory in the D3D12_HEAP_TYPE_DEFAULT heap, requiring a D3D12_HEAP_TYPE_UPLOAD heap to transfer data from the CPU to the GPU memory which is often managed by the ResourceUploadBatch class. For more information see Textures.

Direct3D 12 does support 'sparse textures'. For more information see the D3D12ReservedResources sample for PC / UWP

Further reading

Memory Management in Direct3D 12

For Use

  • Universal Windows Platform apps
  • Windows desktop apps
  • Windows 11
  • Windows 10
  • Xbox One
  • Xbox Series X|S

Architecture

  • x86
  • x64
  • ARM64

For Development

  • Visual Studio 2022
  • Visual Studio 2019 (16.11)
  • clang/LLVM v12 - v18
  • MinGW 12.2, 13.2
  • CMake 3.20

Related Projects

DirectX Tool Kit for DirectX 11

DirectXMesh

DirectXTex

DirectXMath

Tools

Test Suite

Model Viewer

Content Exporter

DxCapsViewer

See also

DirectX Landing Page

Clone this wiki locally