Volume rendering, shadows in Forward mode, not working for me...

I wrote a volume renderer and its working really well in deferred mode, but I can get it to work 100% in forward mode because the shadows are often not rendering correctly.

Before I go into details, is there know issues with shaders that output depth in forward mode with shadows?
(If not, I will document my problem in detail in a followup.)

Do you also have a shadowcaster pass that does your volume rendering? In deferred the depth output by the shader is used for shadow receiving, but in forward it actually uses a pre-pass to render the depth and directional shadows are cast on that. The same shadowcaster pass is used both for generating the depth for shadow maps and for that depth texture.

1 Like

Thank you! I think you have 100% isolated my problem.

Yes, I do have a shadowcaster (the same one used in forward/deferred)
and I never thought this was also used to generate the depth buffer in forward mode.

I will look at the shadowcaster setup and see what might be going wrong.

Is there any reference I can follow ?

There shouldn’t be anything particularly special about the shadow caster pass used for the camera depth vs the shadow maps, Unity does not even differentiate between the two. The shadow caster shader variant used for camera depth, directional shadows, and spot lights are the same, only point lights are different because Unity doesn’t use a shadow texture format.

My assumptions would be that your existing shadow caster pass isn’t working properly and I don’t know how you have your main forward pass calculating the depth output, but the calculations should be the same.

My shadowcaster seem to work well in all cases, to cast shadows. (point , spot, directional)

But I do see issues even with shadows completely disabled,
and those artifacts only go away if I comment out the shadow pass.

So the shadow caster is indeed used even if no shadow are enabled / computed.
And my shadowcaster is not compatible with how unity uses it for the forward pre-pass.

FYI: I’m trying to solve this for this project

(Its a raymarcher into volumetric data, the “geometry” is generated in the fragment shader.)

Exactly what issues are you seeing? If you have shadows disabled the shadowcaster pass is no longer used for anything, unless you’re also doing post process effects that use the depth, or depth normals which use a completely different method.

Here is what I noticed… (humm, let me see, it seem that I can really break the rendering by simply move the object)2850359--208278--forward1.jpg

Have you tried using the frame debugger? That’ll give you a better view of the depth buffer that might help.

Alright. HUGE clue. “Dynamic batching”

It seem Unity modify the vertex data in the forward pre-pass even so my shader is tagged
“DisableBatching” = “True”

Curious, how did you know Unity uses the shadowcaster in a forward pre-pass ?
Is this documented or you figured it out via experiments?

Is DisableBatching on the pass or the subshader level? I believe it’s ignored if it’s only on the pass.

As for the pre-pass it was originally by “brute force” dissecting of the shaders, and eventually the frame debugger. Aras also wrote about it here: Deferred Cascaded Shadow Maps · Aras' website

It might be in the documentation someplace as an offhanded line, but I don’t remember.

Thank you for sharing your research.

I never used the frame debugger, so I will give this a try.
But my guess at this time is that Unity modify my vertex positions. (even with dynamic batching disabled)

This is what I have for the shadowcaster

    Pass{ // ShadowCaster
        Tags {
            "LightMode" = "ShadowCaster"
            "DisableBatching" = "True"
        }

        CGPROGRAM
        #pragma vertex VertShader
        #pragma fragment PixShader
        #pragma multi_compile_shadowcaster

       // VertShader & PixShader code

        ENDCG
    }

Ok, I have the problem identified. (The frame debugger helped big time)

But I can only code a solution if I can identity if the shadowcaster is used to for the depth pass.
Because the view direction is not the same…

Is the view direction flipped?

See _ProjectionParams.x

The frame debugger seem to indicate (at least in unity 5.4.0f3) that unity_LightShadowBias is zeroed in the depth pass.

In the shadowcaster I’m now using this

if(any(unity_LightShadowBias)==false) // Depth pass

I’m isolating this issue to this thread

So now my shader only breaks when “dynamic batching” is enabled.

Since “DisableBatching”=“True” is ignored, is there a hack to break batching for a Depth pass shadowcaster ?

OR even better… does unity offer a hook for us to be part of the batching process?
(not custom batching, but have a chance to massage the vertex data after it appended)

There’s not a way I know of to have much control over batching beyond turning it off project wide. There’s some weirdness with any depth pass (camera depth, shadows, or forward depth injection into deferred) where Unity will ignore all sorts of settings, but I’m a little surprised DisableBatching isn’t working though, as long as that’s set in the tags under Category or SubShader and not under each individual Pass I’ve seen that be effective and disabling batching.

The alternative is it seems like you’re generating the meshes? You could store the data you need in the additional UVs using SetUV() which lets you store up to a Vector4 per UV. Unity will stomp on the second and third UVs if you set the renderer to static for use with static lightmaps and dynamic lightmap, but the first and fourth shouldn’t ever be touched.

edit: You don’t need to recreate the mesh every time you need the data to update either, you can use Unity - Scripting API: MeshRenderer.additionalVertexStreams

DisableBatching works for me in all cases, expect for the depth pass so I think its a “bug” with unity not honoring the tag in this situation.

And the issue I have with batching is that Unity modify the position (by applying the model matrix),
but I have other value that are in local space… so I end-up with a miss-match.

So I think I can enable unity batching if I can make my data match what unity did to my vertex position…
But for now, I just want Unity to never, ever batch my vertex shaders :slight_smile: