Skip to content

Graphics Module

Artem edited this page Jan 30, 2025 · 2 revisions

Graphics Module

The Graphics Module provides a convenient wrapper around the D3D12 API. This module actively uses Graphics.Heaps and allows easy interaction with buffers and textures, creating descriptors for them. It implements code for SwapChain creation, shader compilation, and supports two graphics queues: Direct and Compute.


Resource Structure

Below is the hierarchy of classes responsible for resource management.

Resources

ResourceD3D12

The base class for all resources is ResourceD3D12. It takes a reference to the source ID3D12Resource in the constructor and enqueues this resource for deletion in the destructor. It also accepts a QueueID to determine which queue the resource was used in, ensuring that it is deleted only when the GPU no longer needs it. Essentially, ResourceD3D12 implements automatic resource management: when a resource is created, it is stored in this class. Once the class is freed, the resource is automatically placed in the deletion queue.

BufferD3D12

This class creates a resource in the DEFAULT heap, allows buffer initialization with initial data, and provides CBV, SRV, and UAV creation.

VertexBufferD3D12 & IndexBufferD3D12

Convenient wrappers for creating vertex and index buffers. These classes provide functions to obtain D3D12_VERTEX_BUFFER_VIEW and D3D12_INDEX_BUFFER_VIEW.

TextureD3D12

Creates a resource in the DEFAULT heap. It can load a texture into memory using void* data or a file path to a .dds texture. The .dds texture loading is handled using DDSTextureLoader. Additionally, it provides convenient functions for creating SRV, RTV, and DSV.

UploadBufferD3D12

A wrapper for creating resources in the UPLOAD heap. Useful, for example, for initializing a buffer with empty data using CopyBufferRegion.

DynamicUploadBuffer

Allocates memory in the DynamicUploadHeap (reference) and provides functions for data upload. Supports CBV and SRV creation. Lifetime: one frame. Useful for creating constant buffers or structured buffers that update every frame.


Command List and Command Queue Management

Below is a simplified diagram of the classes responsible for command creation and execution on the GPU.

RenderDeviceD3D12

CommandListManager

Inspired by the implementation in DiligentEngine. This class efficiently reuses ID3D12CommandAllocator. More details: Resource Management.

CommandContext

A wrapper around ID3D12GraphicsCommandList. It uses CommandListManager for handling ID3D12CommandAllocator. Additionally, it implements a queue of D3D12_RESOURCE_BARRIER and a FlushResourceBarriers function, which applies all pending barriers for optimization.

CommandQueueD3D12

Represents a command queue of a specific type: Direct or Compute. It contains a DynamicUploadHeap for creating dynamic resources. This class also manages a queue for safe GPU resource deletion and provides functionality for submitting command lists and handling Fence synchronization.

RenderDeviceD3D12

The main class encapsulating multiple subsystems. It stores:

  • Two CommandContext instances
  • Two CommandQueueD3D12 instances (Direct and Compute)
  • Four CPUDescriptorHeap
  • Two GPUDescriptorHeap
  • Two DynamicSuballocationManager

As illustrated in this diagram. It maintains its own resource release queue for cases where a resource is used in both Direct and Compute queues simultaneously. Provides functions for allocating resources in the appropriate queues and accessing CommandContext and CommandQueueD3D12.


Additional Classes

Several additional classes simplify working with DirectX 12.

SwapChain

Creates IDXGISwapChain4 in the constructor. It supports transparent window swap chains using DirectComposition. The approach described in this article was used when implementing the swap chain.

ShaderD3D12

Takes an .hlsl file path and compiles the shader into bytecode. Supports Vertex, Geometry, Pixel, and Compute shaders.

GBuffer

A wrapper for GBuffer resource creation. It generates the necessary buffers and includes an Accumulation Buffer.

RootSignatureD3D12

A convenient wrapper for ID3D12RootSignature creation. Each root signature also generates static samplers for easy shader use.

CommandSignatureD3D12

A wrapper for ID3D12CommandSignature, enabling Indirect Drawing.

PipelineStateD3D12

A wrapper for graphics ID3D12PipelineState creation.

ComputePipelineStateD3D12

A wrapper for compute ID3D12PipelineState creation.

Clone this wiki locally