GPU-Driven Cascaded Shadows: ExecuteIndirect and Persistent GPU Data
As our custom DirectX 12 engine grew, shadow rendering became a significant CPU bottleneck. We already had directional shadows, depth-buffer fitting, and Cascaded Shadow Maps (CSM), but generating a shadow map for each cascade still involved substantial work on the CPU.

I tackled the problem in two stages. First, I moved shadow-caster selection and draw-command generation to the GPU using a compute shader and ExecuteIndirect. Then I removed another bottleneck: rebuilding and uploading the entire list of potential shadow casters every frame, even when almost none of them had changed.
The interesting part was that moving the draws to the GPU was only half the solution. The data feeding that GPU pipeline needed a better lifetime, too.
1. The problem: too many potential shadow draws
The test level contained roughly 7,500 mesh renderers. Because a renderer could contribute multiple submeshes, one recorded frame contained 7,597 potential shadow-draw candidates. With four cascades, submitting every candidate to every cascade meant up to 30,388 potential draws per frame.

Not every object intersects every cascade. More importantly, a shadow caster cannot be selected solely by whether it is visible to the game camera: an off-screen object may still cast a shadow onto a visible surface. I needed to test candidates against the light-space volume of each cascade.
2. GPU-driven culling with ExecuteIndirect
I introduced ShadowCasterCullingPass, a compute pass that evaluates shadow-draw candidates against the projection volume of each cascade.
Each submesh is represented by a 256-byte ShadowCasterCandidateGPU record containing its bounding-box corners, model matrix, vertex and index buffer information, draw arguments, and flags. This describes a draw without duplicating mesh geometry.
For each cascade, the compute shader transforms the candidate's bounds into the cascade's clip space. Candidates completely outside the volume are rejected; accepted candidates reserve an output slot through an atomic counter and write a compact 116-byte indirect draw command.
ShadowMapPass then uses DirectX 12 ExecuteIndirect to consume those command buffers. The CPU still starts the passes, but it no longer records an individual indexed draw for every candidate in every cascade. The cascade matrices, visibility test, and generated commands remain on the GPU, with the required resource barriers between compute and drawing.

In one development capture, the selected draw counts were:

That is 2,220 selected commands instead of 30,388 potential submissions in that particular scene and camera view, approximately 93% fewer candidate draws reaching the shadow rasterization stage. This is conservative frustum/volume culling, not occlusion culling. I also kept a conservative path for skinned meshes when their static bounds could not reliably describe the animated pose.
The culling itself was not free: one recorded profile showed approximately 0.5 ms of GPU work, alongside roughly 0.116 ms of CPU command-recording overhead. The benefit was avoiding far larger amounts of repeated CPU draw submission and unnecessary shadow rasterization.
3. The bottleneck that remained
After moving selection and drawing to the GPU, profiling exposed a second cost: every frame, the CPU still walked thousands of MeshRenderer objects, rebuilt their candidate records, and uploaded the entire list.

That meant spending roughly 2.5–3.0 ms of CPU preparation time and transferring about 1.85 MiB per frame just to describe geometry that was largely static.
I first tried caching candidate data per renderer. However, that approach still required a global scan, list reconstruction, and a full upload. Profiling showed it was slower, so I reverted it. The real fix required changing the ownership and lifetime of the data, not simply caching the result of an expensive loop.
4. Persistent GPU candidates and dirty updates
I replaced the temporary per-frame list with a persistent candidate buffer in a DirectX 12 DEFAULT heap.
A CPU-side registry assigns stable buffer slots to each renderer's submeshes. Each renderer holds a ShadowCasterHandle with generation information, allowing the system to reject stale references when objects are destroyed or slots are reused.
Rather than scanning the whole scene to discover changes, renderers notify a dirty queue when relevant data changes: transforms, bounds, meshes, skinning configuration, shadow-casting flags, or enabled state. Changes to parent objects also propagate to affected renderers. Duplicate notifications are coalesced.
At frame preparation time, the CPU rebuilds only the affected records and stages them in an upload ring buffer. CopyBufferRegion updates the corresponding slots of the persistent GPU buffer. Resource-state transitions and fences keep uploads, compute reads, and buffer lifetimes correctly synchronized.

Animated geometry needed special treatment. A character's skinned vertex buffer can change even when its GameObject transform does not, so I maintained a separate list of skinned renderers whose candidate bindings are refreshed each frame. The actual cascade matrices, culling, and indirect commands are still recomputed for the current camera view. Only the reusable candidate descriptions persist.
5. Results
The two optimizations addressed different parts of the same pipeline:
Metric | Previous approach | Optimized approach |
Shadow draws in one recorded four-cascade test | 30,388 potential draws | 2,220 selected indirect commands |
CPU candidate preparation | ~2.5–3.0 ms | ~0.04–0.08 ms |
Candidate upload during ordinary frames | ~1.85 MiB/frame | ~10–13 KiB/frame in recorded samples |
The candidate-preparation samples indicate a reduction of approximately 97–98% in that specific CPU task. They do not mean the entire frame became 98% faster, and the measurements came from development captures under slightly different scene conditions rather than a controlled benchmark.
In a separate in-engine before/after capture from the presentation, the observed framerate changed from 27 FPS (37 ms/frame) to 41 FPS (24 ms/frame)—a gain of roughly 14 FPS in that test.

6. Key Takeaways
This work reinforced an important lesson about engine optimization: a GPU-driven renderer can still be CPU-bound if its input data is rebuilt every frame.
ExecuteIndirect removed repeated CPU draw submission and let each cascade use GPU-generated visibility information. Persistent candidate storage then eliminated most redundant CPU preparation and transfers for static objects. The second change introduced its own engineering challenges: stable handles, object lifetime, dirty propagation, partial copies, synchronization, and correct treatment of skinned meshes. Very necessary to make the system scalable.
There is still room to improve animated bounds and GPU culling granularity. Nevertheless, this was one of my most valuable DirectX 12 rendering tasks because it combined profiling, compute shaders, indirect drawing, resource management, and iteration based on measured bottlenecks rather than assumptions.


Comments