Search Unity

How to have surface shader logic that only runs in deferred mode?

Discussion in 'Shaders' started by Deleted User, May 11, 2017.

  1. Deleted User

    Deleted User

    Guest

    I have a surface shader that has a feature that will only work in forward rendering mode. So I need to change my shader so that in the deferred variant of my compiled shader, a certain line sets a float to 0, effectively disabling the feature.

    I tried this in my surf function but it doesn't work:

    Code (CSharp):
    1. #ifdef UNITY_PASS_DEFERRED
    2.     depthBlend = 0;
    3. #endif
    Is this keyword not what I'm supposed to use? Thanks!
     
  2. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    Don't use ifdef in the case of Unity's built-in preprocessor macros. Just do:
    Code (CSharp):
    1. #if UNITY_PASS_DEFERRED
    2.     depthBlend = 0;
    3. #endif
     
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    Note, that won't make something not render when the camera or project is rendering using deferred, only if that particular shader is using an opaque queue (background, geometry, or alphatest). If you're using a transparent queue those will always use forward rendering, so that will never be true.
     
  4. Deleted User

    Deleted User

    Guest

    @bgolus This is surprising news to me since my shader IS actually in the "Overlay" render queue but I'm having trouble with depth as you can see in my other thread:

    https://forum.unity3d.com/threads/i...are-breaks-in-deferred-rendering-mode.470413/

    If my shader is being rendered in forward mode then why am I having this depth sample issue when the main camera is rendering in deferred mode? Shouldn't it not make any difference?

    @Invertex This is giving me a compile error:

    Code (CSharp):
    1. Shader error in 'MyShader/MyShader': invalid or unsupported integer constant expression at line 125 (on d3d9)
    2.  
    3. Compiling Vertex program
    4. Platform defines: UNITY_ENABLE_REFLECTION_BUFFERS UNITY_USE_DITHER_MASK_FOR_ALPHABLENDED_SHADOWS UNITY_PBS_USE_BRDF1 UNITY_SPECCUBE_BOX_PROJECTION UNITY_SPECCUBE_BLENDING UNITY_ENABLE_DETAIL_NORMALMAP SHADER_API_DESKTOP
    5.  
     
    Last edited by a moderator: May 15, 2017
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    Because surface shaders are funny things, and just because you set a queue doesn't mean it'll respect that, especially with deferred. This should probably be in the other thread, but you should try including exclude_path:deferred on the #pragma surface line. If there's a deferred pass at all I believe Unity tries to use it when running deferred, and just changing the queue doesn't disable its generation. You either need that option, or have alpha in that line.
     
  6. Deleted User

    Deleted User

    Guest

    Wow, the render queues are all kinds of messed up huh?

    I had success only after adding both exclude_path:deferred and alpha but it works now in both rendering modes with no issues. Thanks!
     
  7. Deleted User

    Deleted User

    Guest

    @bgolus Unfortunately it looks like adding alpha has disabled ZWrite on my shader - which I really need. Is there another way to disable the deferred pass and keep ZWrite On?
     
  8. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    You can try using keepalpha instead of alpha.
     
  9. Deleted User

    Deleted User

    Guest

    @bgolus Unfortunately keepalpha seems to have re-enabled the deferred pass. :/
     
  10. Deleted User

    Deleted User

    Guest

    Bump. Still haven't cracked this part. :/
     
  11. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    Strange. I tried this myself and it worked for me.
     
  12. Deleted User

    Deleted User

    Guest

    This is what I got:
    Code (csharp):
    1. #pragma surface surf Standard vertex:vert exclude_path:deferred keepalpha
    The result is that ZWrite is now on but I've lost depth data again so I assumed the deferred shader started compiling as a result of the keepalpha.