DrawMeshInstancedIndirect shader not writing to CameraDepthTexture

I’m using drawmeshinstancedindirect to draw hundreds of thousands of instances of a particular mesh. Now I’ve written a post processing depth of field shader that samples the cameraDepthTexture. The problem is, my simple shader doesn’t seem to write to this texture and most resources I’ve found so far say I have to add a shadow caster pass for it to do so; however, having a shadow caster pass on hundreds of thousands of instances of this mesh brings it to a screeching halt. I don’t care about shadows, so is there a way to write to the camera depth texture without adding the unnecessary shadow caster pass?

Is this simply not possible without a shadow caster pass? Do I have to write depth to a different texture and then merge with the cameradepthtexture at the depth sample pass of my dof shader?

The regular ShadowCaster pass isn’t actually casting shadows. The naming is confusing and in the new pipelines there’s now also just a “DepthOnly” pass. Shadows need the depth information, so the pass only outputs the depth information for the meshes, which then also allows future rendering to early-z cull and avoid the cost of what is usually a much more expensive shader fragment pass.

The depth buffer needs to get filled out so that it can then be then copied and bound to future render passes for sampling (this is what becomes the cameraDepthTexture).

If you’d like to avoid 2 passes happening, which could indeed be the limiting factor here of having to push all the geometry through the raster pipeline twice… then I’m not sure. I think you’d have to inject your rendering during that early phase, “BeforeOpaque”, so that when the camera copies the buffer before rendering opaques and transparents your information is there.

Or you could try binding that resulting texture and writing your depth values to it during your normal render pass… But it might not be set to writeable.

Thank you tremendously for this. I think what may be happening then, as I’ve tried with simple shadowcaster passes, but my instancing throughput gets dropped to %10, is that my shadowcaster pass may be breaking instancing. Other than performance I don’t really know of a way to test this. I have yet to find an example out there of a simple unlit instanced shader that has an instanced shadowcaster pass (I guess I mean a shadowcaster pass that is anything more than a simple shadowcaster pass and has any special methods for instancing). Is this a thing, or am I barking up the wrong tree?

I don’t personally have experience making something like that so I can’t say for sure. I’d imagine the per-light information Unity binds to the ShadowCaster passes so that it can adjust the output depths for a given light’s shadow bias values would be what’s breaking it. If you modified the pass to only output depth and not use any of the include files, nor the custom functions, mayyybe it would stop breaking instancing.
But otherwise you might just have to use a CommandBuffer to inject your rendering before Opaques, using a differently named Pass and thus outputing depth information during that time to have it then be captured in the cameraDepthTexture