DXR Geometry Compaction: Reducing Memory Footprint

In DirectX Raytracing (DXR), geometry compaction is a post-build optimization process designed to reclaim unused video memory allocated to Bottom-Level Acceleration Structures (BLAS). During the initial construction of an acceleration structure, the graphics driver must allocate a conservative worst-case memory buffer to guarantee the build succeeds. Geometry compaction measures the true, finalized size of the acceleration structure on the GPU and copies it into a precisely sized destination buffer, typically cutting BLAS memory footprints by 30 to 50 percent and improving hardware cache locality during ray traversal.

The Problem of Over-Allocation

When building a BLAS, the DXR API queries the driver for memory requirements via GetRaytracingAccelerationStructurePrebuildInfo. Because the underlying bounding volume hierarchy (BVH) generation relies on complex spatial partitioning algorithms, triangle sorting, and potential primitive splitting, the driver cannot know the exact topology or size of the tree ahead of time. Consequently, the returned ResultDataMaxSizeInBytes is an overestimation to accommodate the worst-case scenario. If left uncompacted, this unused space remains resident in VRAM across hundreds or thousands of scene meshes.

The Compaction Workflow

To reclaim this excess space, developers implement a multi-step GPU workflow:

  1. Enable Compaction: The initial BLAS is built with the D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_ALLOW_COMPACTION flag. This instructs the driver to generate internal metadata necessary for sizing and copying.
  2. Query Compacted Size: The application executes EmitRaytracingAccelerationStructurePostbuildInfo, requesting the D3D12_RAYTRACING_ACCELERATION_STRUCTURE_POSTBUILD_INFO_COMPACTED_SIZE metric. The GPU writes the exact byte requirement of the completed structure into a designated buffer.
  3. Allocate Tight Buffers: The application allocates a new ID3D12Resource buffer matching the exact reported compacted size, either per mesh or out of a shared sub-allocated pool.
  4. Execute Compaction Copy: The application calls CopyRaytracingAccelerationStructure using the D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE_COMPACT mode, directing the GPU to copy the data from the oversized source buffer to the newly allocated, tightly fitted target buffer.
  5. Reclaim Memory: Once the copy completes and the Top-Level Acceleration Structure (TLAS) updates its instance pointers to the new BLAS addresses, the original, oversized build buffers are deallocated.

Performance and Architectural Benefits

Geometry compaction delivers two distinct hardware benefits:

  • Drastic VRAM Reduction: Compaction removes spatial padding and unused node slots. For modern games with dense geometric detail, this frees hundreds of megabytes or even gigabytes of VRAM.
  • Improved Cache Efficiency: Ray traversal is memory bandwidth-intensive. A compacted acceleration structure has higher data density, meaning fewer cache lines must be fetched from VRAM when streaming BVH nodes and primitive data into the GPU’s L1 and L2 caches. This leads to higher cache hit rates and lower latency during intersection tests.