Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

SPR batcher on hand-written shader / URP

Discussion in 'Universal Render Pipeline' started by Chmyke, Mar 9, 2020.

  1. Chmyke

    Chmyke

    Joined:
    Aug 1, 2012
    Posts:
    34
    Hello,
    I have written this very simple unlit shader because I think the default UniversealRenderPipeline/Unit is not fast enough (maybe I'm wrong and my shader is probably slower)

    Code (CSharp):
    1. Shader "Cecly/Simple Color"
    2. {
    3.     Properties
    4.     {
    5.         _Color("Color", Color) = (1,1,1)
    6.     }
    7.  
    8.     SubShader
    9.     {
    10.         Tags {"Queue" = "Overlay"}
    11.  
    12.         Color[_Color]
    13.         Pass{}
    14.     }
    15. }
    Sadly, my shader is not compatible with the SRP bacther.


    Is someone there know how to achieve this? Thanks!
     
  2. GunLengend

    GunLengend

    Joined:
    Sep 24, 2014
    Posts:
    54
    Use Shader Graph is a choice
     
    Chmyke likes this.
  3. StaggartCreations

    StaggartCreations

    Joined:
    Feb 18, 2015
    Posts:
    2,141
    Chmyke likes this.
  4. Andre_Mcgrail

    Andre_Mcgrail

    Unity Technologies

    Joined:
    Dec 9, 2016
    Posts:
    244
    This is a fixed funtion shader, which currently will generate a Builtin shader rather than a URP shader (if you select the shader and look at the inspector for it, you can compile the 'Fixed Function' code, and you will see why it will not work with URP).
    The reason it will render is because URP was designed so that it could render built in shaders that were unlit. But this is not the way to go forward, and you will need to write a shader using the new SRP shaderlibrary, unfortunately we are super slim when it comes to learning resources here.

    If you want to make a custom URP shader take a good look at the provided ones in URP, they are more complicated than they need to be due to the 'uber' nature of them and also platform support, XR, instancing, etc.

    Below you can see a very basic Unlit shader in URP, this is what I would say is probably the bare minimum.

    Code (CSharp):
    1. Shader "Unlit/NewUnlitShader"
    2. {
    3.     Properties
    4.     {
    5.         _Color("Color", Color) = (1,1,1,1)
    6.     }
    7.     SubShader
    8.     {
    9.         Tags { "RenderType"="Opaque" }
    10.      
    11.         Pass
    12.         {
    13.             HLSLPROGRAM
    14.             // Required to compile gles 2.0 with standard srp library
    15.             #pragma prefer_hlslcc gles
    16.             #pragma exclude_renderers d3d11_9x
    17.             // Will make life easier to have at least URP core.hlsl
    18.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
    19.  
    20.             #pragma vertex vert
    21.             #pragma fragment frag
    22.  
    23.             // For SRP Batcher to work you need to put things in the right Cbuffer
    24.             CBUFFER_START(UnityPerMaterial)
    25.             half4 _Color;
    26.             CBUFFER_END
    27.  
    28.             float4 vert(float4 positionOS : POSITION) : SV_POSITION
    29.             {
    30.                 // This struct contains commonly used transformations such as Clip, World, etc
    31.                 VertexPositionInputs vertexInput = GetVertexPositionInputs(positionOS.xyz);
    32.                 return vertexInput.positionCS;
    33.             }
    34.  
    35.             half4 frag() : SV_Target
    36.             {
    37.                 return _Color;
    38.             }
    39.             ENDHLSL
    40.         }
    41.     }
    42. }
    43.  
    To that note, you will notice this is a vert/frag shader and these are the only shaders fully supoported under SRP, the other optiuon is shadergraph, these will also generate shader that have full platform support, XR, instancing etc.

    This shader should work on all platforms but will not work with intancing or stereo rendering.

    more information about the SRP batcher(including custom shaders):https://blogs.unity3d.com/2019/02/28/srp-batcher-speed-up-your-rendering/
     
    Last edited: Mar 10, 2020
    chrismarch and Chmyke like this.
  5. Chmyke

    Chmyke

    Joined:
    Aug 1, 2012
    Posts:
    34
    Thank you all, I am very satisfied with your answers! :)
     
  6. FranFndz

    FranFndz

    Joined:
    Sep 20, 2018
    Posts:
    178
    question,

    if i inlcude my own HLSL shader, should i also add CBUFFER_START(UnityPerMaterial) inside the included HLSL file properties?

    Right now i have a hand made shader compatible with SRP Batcher, but my hlsl include property are not inside the CBUFFER_START. (only the properties inside the main shaders are)
     
  7. Andre_Mcgrail

    Andre_Mcgrail

    Unity Technologies

    Joined:
    Dec 9, 2016
    Posts:
    244
    If they are properties you plan to have different on different materials then yes, if it's properties that will be different per renderer then you need UnityPerDraw cbuffer, if it is a constant amongst all things that use this shader then they can live ouside of a cbuffer. Again check this blogpost out as it has a section about writing your own shaders: https://blogs.unity3d.com/2019/02/2...4.1974275149.1584002334-1241191896.1583834030
     
  8. studentutu

    studentutu

    Joined:
    Oct 22, 2017
    Posts:
    105
    @Andre_Mcgrail could you give a simple sample how to setup instanced color in UnityPerDraw cbuffer without braking the full shader SRP compatibility?
     
  9. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,511
    Can we have bare minimum sprite shader that works with urp and srp batching please?
     
  10. FranFndz

    FranFndz

    Joined:
    Sep 20, 2018
    Posts:
    178

    just to clarify, per Renderer = “Per Object” variables