Search Unity

SRP Batcher not working on Android for custom shader with URP

Discussion in 'Universal Render Pipeline' started by stephero, Jun 27, 2020.

  1. stephero

    stephero

    Joined:
    Feb 8, 2016
    Posts:
    123
    Hi guys,

    I have a very very basic custom shader working fine with URP. I want to make it work with the SRP Batcher.
    Code (CSharp):
    1. Shader "Hidden/SimpleSRPTest"
    2. {
    3.     Properties
    4.     {
    5.     }
    6.  
    7.     SubShader
    8.     {
    9.         Pass
    10.         {
    11.             Cull Front
    12.  
    13.             CGPROGRAM
    14.  
    15.             #pragma vertex vert
    16.             #pragma fragment frag
    17.  
    18.             #include "UnityCG.cginc"
    19.  
    20.             struct appdata
    21.             {
    22.                 float4 vertex : POSITION;
    23.             };
    24.  
    25.             struct v2f
    26.             {
    27.                 float4 posClipSpace : SV_POSITION;
    28.             };
    29.  
    30.             v2f vert(appdata v)
    31.             {
    32.                 v2f o;
    33.                 o.posClipSpace = UnityObjectToClipPos(v.vertex);
    34.                 return o;
    35.             }
    36.  
    37.             half4 frag(v2f i) : SV_Target   { return 1; }
    38.  
    39.             ENDCG
    40.         }
    41.     }
    42. }
    43.  
    The shader is compatible with SRP Batcher:
    Screenshot_12.png

    I have a scene with 2 different meshes using this material/shader.

    The batching is working fine in both the editor and standalone Windows build:
    Screenshot_13.png

    However, it doesn't when running on Android. I find the messages in the Frame Debugger specially misleading and they seem to be only about Dynamic Batching because:
    - when the 2 objects use different materials, it says:
    Screenshot_14.png
    - when the 2 objects use the same material, it says:
    Screenshot_15.png

    Is there a way to have more useful message about batching issues in the Frame Debugger?
    I would have prefer to have 1 message per batching technique (dynamic batching, gpu instancing, srp batcher) instead of only 1 message with which you have to figure out the batching technique is refers to.

    And do you have an idea what in this basic shader could break the SRP Batcher on Android?

    I am using Unity 2019.4.0f1.

    Thanks
     
  2. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,026
    Hi!
    What Android device do you use?
     
  3. stephero

    stephero

    Joined:
    Feb 8, 2016
    Posts:
    123
    Hi :)
    It's a Samsung Galaxy A5 2017 running Android 8.0.0.
    Note that if I try to use the "Universal Render Pipeline/Unlit" shader from the URP package, the SRP Batcher works fine on my Android device (with both 1 material or 2 different materials), so the issue issue seems related to my custom shader.
    Thank you
     
  4. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,026
    Ah, ok :)
    That's easy then: UnityCG.cginc (and all include files used for built-in pipeline) define CBUFFER macro to actually produce a cbuffer only for a couple of platforms (D3D11 is among them, so it works in the Editor on Windows).
    You should either include something like HLSLSupport.cginc from SRP Core or force-enable constant buffers using #pragma enable_cbuffer in your shader.
     
  5. stephero

    stephero

    Joined:
    Feb 8, 2016
    Posts:
    123
    Thank you very much for the help. The pragma seems to work just fine, I wouldn't have find it by myself. I'm happy there is a simple solution that doesn't require to remove the UnityCG.cginc!
    Thank you :)
     
    aleksandrk likes this.
  6. weiping-toh

    weiping-toh

    Joined:
    Sep 8, 2015
    Posts:
    192
    I am currently experiencing a similar phenomena on URP 7.5.1.
    I have 2 custom shaders.
    On Android Vulkan, both batched well.
    On Android GLES3 however, one successfully batched and one did not.
    I tried the #pragma_cbuffer and included the Core.hlsl to enable CBUFFERS but is still experiencing it.

    aleksandrk
    Any hints on where do I look for?
     
  7. weiping-toh

    weiping-toh

    Joined:
    Sep 8, 2015
    Posts:
    192
    The major difference between the 2 custom shaders is that one utilizes global variables set by script and one does not.
     
  8. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,026
    If you're on Windows, you could switch the Editor to OpenGL and see if it batches there or not.
     
  9. weiping-toh

    weiping-toh

    Joined:
    Sep 8, 2015
    Posts:
    192
    It breaks batching!
    But it also helped me find the reason that it broke!.
    Thanks
     
  10. weiping-toh

    weiping-toh

    Joined:
    Sep 8, 2015
    Posts:
    192
    Oh wait, I retract my last post. Not enough information to tell me why it broke
     
  11. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,026
    It should say something in the shader inspector about this.
     
  12. weiping-toh

    weiping-toh

    Joined:
    Sep 8, 2015
    Posts:
    192
    Oh wait, does SRP batcher work on OpenGL4.1 as I am working on a Mac?
     
  13. weiping-toh

    weiping-toh

    Joined:
    Sep 8, 2015
    Posts:
    192
    I figured out that to debug for GLES, I probably need a Windows Machine
     
  14. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,026
    No, it requires OpenGL 4.3 on desktop.
    As I said in the first reply, "if you're on Windows" :)

    Can you paste the source of the shader that isn't SRP batcher compatible?
     
  15. weiping-toh

    weiping-toh

    Joined:
    Sep 8, 2015
    Posts:
    192
    Actually, I have more than one shader that ain't batching under GLES3. But I managed to resolve some via guesswork. Will try to get a OpenGL4.3 machine to debug the remaining. Thanks for the help and the hint for resolution.
    It will become a clutter if I post all offending shaders here.

    Oh, the reason that broke batching, I have deleted away some unused Properties in my shader code that was included in the UnityPerMaterial CBUFFER. There are probably mismatches in the Properties field and the CBUFFER for the offending shaders.
     
  16. weiping-toh

    weiping-toh

    Joined:
    Sep 8, 2015
    Posts:
    192
    But it is still strange that it is fine for the other APIs to gracefully compile even having such mismatches and GLES3 complains over it.
     
  17. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,026
    Well, you could file a bug report - it would get investigated then :)
     
  18. Deleted User

    Deleted User

    Guest

    Hello Aleksandrk,

    I had post here too: https://forum.unity.com/threads/srp-batcher-does-not-work-with-opengles3.1095937/page-2

    But I'm facing a similar problem with Unity 2021.3.5f1 using Omnishade, this shader inspector shows that this shader is compatible with SRP Batcher. Testing on editor the SRP Batcher works well, but when I build and test on Android device the SRP Batcher don't work. If I change the shader to Unity URP Lit the SRP Batch works on Android device too.

    I tested the same case on unity 2021.3.15f1 and the problem occurs. But on unity 2022.1.23f1 the SRP Batcher works well with omnishade.
     
  19. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,026
    Hi!
    I don't know what omnishade is. If SRP batcher works with one shader, the problem is in the other shader. You need to talk to whoever made this asset.
     
  20. Deleted User

    Deleted User

    Guest

    But it work only with Unity Lit Shader, and the SRP batcher work on editor but not in Android build and Unity show that shader is compatible with SRP Batcher. It look like a Unity error too.

    When I update to Unity 2022 the problem is solved, but it is not work on 2021 LTS version.
     
  21. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,026
    The inspector shows the compatibility status for the Editor, not in general.
     
  22. Deleted User

    Deleted User

    Guest

    Ah ok. Do you know there are a way to check if the shader is compatible with SRP Batcher on build platforms?

    Do you know something that make the shader incompatible with SRP Batcher on the URP of Unity 2021.3.15f1 and don't make incompatible with URP of Unity 2022.1.23f1?
     
    Last edited by a moderator: Dec 5, 2022