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:
- Enable Compaction: The initial BLAS is built with
the
D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_ALLOW_COMPACTIONflag. This instructs the driver to generate internal metadata necessary for sizing and copying. - Query Compacted Size: The application executes
EmitRaytracingAccelerationStructurePostbuildInfo, requesting theD3D12_RAYTRACING_ACCELERATION_STRUCTURE_POSTBUILD_INFO_COMPACTED_SIZEmetric. The GPU writes the exact byte requirement of the completed structure into a designated buffer. - Allocate Tight Buffers: The application allocates a
new
ID3D12Resourcebuffer matching the exact reported compacted size, either per mesh or out of a shared sub-allocated pool. - Execute Compaction Copy: The application calls
CopyRaytracingAccelerationStructureusing theD3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE_COMPACTmode, directing the GPU to copy the data from the oversized source buffer to the newly allocated, tightly fitted target buffer. - 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.