DrawMeshInstancedIndirect Compute Shader shadhow culling

Hi,

The DrawMeshInstancedIndirect API in 5.6 is really awesome! I’m building a system now which has to take an object, and then draw it in a NxNxN grid (so N cubed) times.

To do so I fire of a compute shader that checks each NxNxN positions and frustum culls then. if the position is visible it’s added to an Append Buffer. Then I use DrawMeshInstancedIndirect to draw all positions in the append buffer.

This all works great but has a major caveat. When renderering shadows objects that are frustum culled don’t cast shadows either of course. Really what needs to happen is that the culling compute shader needs to run before each rendering pass - not just before the main rendering pass.

What I maybe could do is rerun the compute shader just before shadow rendering assuming unity reads an up to date representation of the buffer. However - in a compute shader there is no way for me to know the unity_WorldToShadow matrices or the total clip planes of the shadow rendering camera.

Any advice on how to achieve compute shader based culling with DrawMeshInstancedIndirect would be greatly appreciated!

Best regards,
Arthur Brussee

1 Like

Theoretically you could use CommandBuffer.DrawMeshInstancedIndirect to render objects instead of Graphics.DrawMeshInstancedIndirect and be able to control the culling for each camera and light … but as far as I know it’s been an unsolved issue for handling directional lights, especially when using cascades.

Unfortunately the “best” option I know of would be to pass the directional light direction to your compute shader and cull based on both the camera frustum and any object with a shadow that goes through the frustum. There are certainly plenty of shadow cascade frustum culling techniques, you may just have to pick one and cross your fingers that it’s cheap enough / close enough to what Unity uses.

For spotlights / point lights you could use a command buffer to render just the missing cubes.