Search Unity

【SRP Batcher】How to make a Default Unlit Shader compatible?

Discussion in 'Universal Render Pipeline' started by EminemJ, Apr 27, 2020.

  1. EminemJ

    EminemJ

    Joined:
    Mar 10, 2020
    Posts:
    45
    In order to use SRP Batcher, I'm trying to learn to make a shader compatible.Here is what I have done:
    1. create a default Unlit shader (created by unity asset menu)
    2.declare property in CBUFFER:
    Code (CSharp):
    1. CBUFFER_START(UnityPerMaterial)
    2.     fixed4 _Color;
    3. CBUFFER_END
    3.In the inspector ,it still reminds me that the shader is not compatible because "Material property is found in another cbuffer than "UnityPerMaterial(_Color)" "
    What Confused me is that this actually works in Windows(the shader became compatible after doing the same operations above), while in MacOS, the shader just can't make it. Then I tried to remove the property "_Color ", and I got another hint : "Builtin property found in another cbuffer than "UnityPerDraw"(unity_ObjectToWorld) ".This is wired because in the default shader ,"UnityCG.cginc" is included, and these builtin properties are already defined in a CBUFFER block in UnityShaderVariables.cginc.
    Why it acts so different between Windows and MacOS, is it because of the graphics API or some other reasons? Why the Shader is still not compatible while the properties already are declared in CBUFFER block on MacOS
     
  2. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,014
    Hi!
    What version of Unity are you using? Which graphics API?
     
  3. EminemJ

    EminemJ

    Joined:
    Mar 10, 2020
    Posts:
    45
    I've tried with 2019.3.11f1 and 2019.2.15f1,on Windows the graphic API is set to "DX11",on MacOs the graphic API is set to "auto"
     
  4. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,014
    Can you take a look at the compiled shader?
     
  5. EminemJ

    EminemJ

    Joined:
    Mar 10, 2020
    Posts:
    45
    Thanks for your reply. The shader and the complied shader is attached below. The inspector reminds me that
    unity_ObjectToWorld in another cbuffer than "UnityPerDraw"
    It seems that unity_ObjectToWorld is already in constant Buffer "VGlobals"
     

    Attached Files:

  6. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,014
    Code (CSharp):
    1. Constant Buffer "VGlobals" (144 bytes) on slot 0 {
    2.   Matrix4x4 unity_ObjectToWorld at 0
    3. ...
    And it's right - it's not in UnityPerDraw.

    The issue is that UnityCG.cginc probably doesn't declare CBUFFER_START as cbuffer.
    As you can only use SRP batcher with SRP, you shouldn't include UnityCG.cginc, but do it the same way as SRP does.
     
  7. EminemJ

    EminemJ

    Joined:
    Mar 10, 2020
    Posts:
    45
    Code (CSharp):
    1. #if defined(SHADER_API_D3D11) || defined(UNITY_ENABLE_CBUFFER) || defined(SHADER_API_PSSL)
    2. #define CBUFFER_START(name) cbuffer name {
    3. #define CBUFFER_END };
    4. #else
    5. // On specific platforms, like OpenGL, GLES3 and Metal, constant buffers may still be used for instancing
    6. #define CBUFFER_START(name)
    7. #define CBUFFER_END
    8. #end
    Oh I get it, thanks.I find it in HLSLSupport.cginc that CBUFFER_START seems not defined as cbuffer when the shader api is Metal .So that's why that shader is compatible on Windows with D3D11 but not compatible on MacOS with Metal,and it probably will not compatible on mobile platform with GLES3. By the way, what does the marco "UNITY_ENABLE_CBUFFER" stand for? Seems that with this marco defined CBUFFER_START is also defined as "cbuffer" .Is it a switch I can turn on?
     
  8. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,014
    That's correct.
    This comes from "#pragma enable_cbuffer", which forces CBUFFER to generate actual cbuffers.

    I suggest to check the shaders in Universal Render Pipeline, they frequently use newer includes and definitions than what's in UnityCG.cginc.
     
    EminemJ likes this.
  9. phil_lira

    phil_lira

    Unity Technologies

    Joined:
    Dec 17, 2014
    Posts:
    584
  10. EminemJ

    EminemJ

    Joined:
    Mar 10, 2020
    Posts:
    45
  11. EminemJ

    EminemJ

    Joined:
    Mar 10, 2020
    Posts:
    45
    Thanks for your answer! I will check the shaders in the URP later