SRP Batcher not working on Android for custom shader with URP

Hi guys,

I have a very very basic custom shader working fine with URP. I want to make it work with the SRP Batcher.

Shader "Hidden/SimpleSRPTest"
{
    Properties
    {
    }

    SubShader
    {
        Pass
        {
            Cull Front

            CGPROGRAM

            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
            };

            struct v2f
            {
                float4 posClipSpace : SV_POSITION;
            };

            v2f vert(appdata v)
            {
                v2f o;
                o.posClipSpace = UnityObjectToClipPos(v.vertex);
                return o;
            }

            half4 frag(v2f i) : SV_Target   { return 1; }

            ENDCG
        }
    }
}

The shader is compatible with SRP Batcher:
6029063--650507--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:
6029063--650513--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:
    6029063--650516--Screenshot_14.png
  • when the 2 objects use the same material, it says:
    6029063--650519--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

Hi!
What Android device do you use?

Hi :slight_smile:
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

Ah, ok :slight_smile:
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.

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 :slight_smile:

1 Like

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?

The major difference between the 2 custom shaders is that one utilizes global variables set by script and one does not.

If you’re on Windows, you could switch the Editor to OpenGL and see if it batches there or not.

It breaks batching!
But it also helped me find the reason that it broke!.
Thanks

Oh wait, I retract my last post. Not enough information to tell me why it broke

It should say something in the shader inspector about this.

Oh wait, does SRP batcher work on OpenGL4.1 as I am working on a Mac?

I figured out that to debug for GLES, I probably need a Windows Machine

No, it requires OpenGL 4.3 on desktop.
As I said in the first reply, “if you’re on Windows” :slight_smile:

Can you paste the source of the shader that isn’t SRP batcher compatible?

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.

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.

Well, you could file a bug report - it would get investigated then :slight_smile:

Hello Aleksandrk,

I had post here too: https://discussions.unity.com/t/837563 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.

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.

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.