Search Unity

Modifying depth buffers made from light perspective

Discussion in 'Shaders' started by tomaszek, May 29, 2015.

  1. tomaszek

    tomaszek

    Joined:
    Jun 18, 2009
    Posts:
    3,862
    My question is quite unusal one I believe.

    Code (csharp):
    1.  
    2. [...]
    3.     #elif defined(SHADOWS_DEPTH)
    4.        // spot, directional depth buffer from light perspective
    5.        float3 wPos=i.posWorld.xyz+ray.xyz;
    6.        float4 clipPos = mul(UNITY_MATRIX_VP, float4(wPos,1));
    7.        clipPos = UnityApplyLinearShadowBias(clipPos);
    8.        float outDepthShadowCaster = clipPos.z/clipPos.w;
    9.        #if defined(SHADER_API_OPENGL)
    10.          outDepthShadowCaster=1; // no shadow casting for openGL dirtectional light/spot - I can't get it working and frame debugger doesnt work...
    11.        #endif
    12.  
    I'm testing the possibility to write into depth buffers. Although it works pretty nice, I've got some problems.
    Above is part of the code which works good on DX9/DX11, but not on openGL. outDepthShadowCaster seems to be incorrect there (the value I output into "native" depth buffer). The possible culprit is OGL depth buffer that's used for shadow casting has different format / values stored comparing to DirectX. UNITY_MATRIX_VP is not the suspect probably, because I tested on MVP, too which is available in vertex program to output the geometry when I don't write into depth. The result was the same. Can somebody at Unity tell me the difference ? When I output to depth buffer from camera perspective it works. Pointlight shadow casting also works (which is not depth buffer but rather float cubemap). So what's the difference between depth buffers there ? Is there any other reason I miss ? I'm on DX11 hardware (inteli7 built in HD4600) in -force-opengl, so I can't run frame debugger (multithreaded rendering not available then - even with -force-gfx-mt).

    The other problem - in forward SHADOWS_DEPTH keyword is used for both - rendering depth buffer from light perspective and depth buffer from camera perspective (to get initial depth for shadows collecting). The only method to determine the case is to use to check _MVP matrix against the _WorldSpaceCameraPos, so overcame the trouble, but above is unresolvable...

    Tom
     
    Last edited: May 29, 2015