Search Unity

Question Weird bug of SRP Batcher

Discussion in 'Shaders' started by zhutianlun810, Aug 23, 2021.

  1. zhutianlun810

    zhutianlun810

    Joined:
    Sep 17, 2017
    Posts:
    171
    Hello,

    I've spent a week on a weird bug. When I turn on srp batcher, the _BaseColor.xyz of my shader has been changed from (1, 1, 1) to (0, 1, 0), which gives my object a green layer. The test scene is just a empty scene with a single gameObject. I guess the bug is from my shader, but now I have no idea how to trace it. The bug disappear if I turn off srp batcher. Also, I don't see this bug on windows editor<opengles3.2>. I am using 2019.4.17 and I tested it on an Android phone.

    The test scene on editor:
    upload_2021-8-23_19-34-59.png

    The test scene on Android Phone:
    upload_2021-8-23_19-39-41.jpeg

    Everything seems correct in FrameDebugger, the _BaseColor is white:
    upload_2021-8-23_19-39-28.png

    In RenderDoc, I edit the shader to output the _BaseColor. It shows the _BaseColor is green:
    upload_2021-8-23_19-12-3.png
    upload_2021-8-23_19-34-16.png
     
  2. Wvg

    Wvg

    Joined:
    Jul 10, 2017
    Posts:
    1
    have you solved this issue yet?
     
  3. zhutianlun810

    zhutianlun810

    Joined:
    Sep 17, 2017
    Posts:
    171
    I've solved it. The CBuffer(UnityPerMaterial) should be layouted same as shader properties for each shader. I rewrited my CBuffer(UnityPerMaterial) declaration to solve this issue.

    The point is that, if you don't use SRP Batcher, you may construct your shader like:

    A.shader
    B.shader
    C.shader
    ...
    ShaderPropertyDeclaration.hlsl

    You write all properties' declaration in a single ShaderPropertyDeclaration.hlsl. Then let all shaders include ShaderPropertyDeclaration.hlsl. Then in some day you want to enable SRP Batcher. You may just put all declaration in a CBuffer(UnityPerMaterial). However, this is wrong! The shader properties of shaders are different from each other!

    Rewriting all shaders are really painful. In my project ShaderPropertyDeclaration.hlsl includes other files and these files use variables in ShaderPropertyDeclaration.hlsl. I have to rewrite them all.