Search Unity

Question SRP batcher with a custom CBUFFER?

Discussion in 'General Graphics' started by Bluedoom, Jun 14, 2022.

  1. Bluedoom

    Bluedoom

    Joined:
    Aug 31, 2020
    Posts:
    14
    (Unity 2019.4.31)
    I have some uniform need to update per frame.
    For example:
    Code (CSharp):
    1.             CBUFFER_START(PerFrameUpdate)// 36 bytes
    2.             half4 _OutlineColor;
    3.             .....
    4.             CBUFFER_END
    5.  
    6.             CBUFFER_START(UnityPerMaterial) // 76 bytes
    7.             float4 _MainTex_TexelSize;
    8.             ....
    9.             CBUFFER_END
    10.  
    I remove _OutlineColor from shader "Properties" in order to make the shader SRP Batcher compatible,and try update the _OutlineColor in these ways,but none of them work properly.
    Code (CSharp):
    1.  
    2. // When srp batch some objects, they share the same _OutlineColor value (first update value)
    3. void Update()
    4. {
    5.                 Visual.GetComponent<Renderer().sharedMaterial.SetColor("_OutlineColor", Outline);
    6. }
    Code (CSharp):
    1.  
    2. // Same problem they share the same _OutlineColor value.
    3. NativeArray<float> tmp = new NativeArray<float>(9, Allocator.Temp);
    4. tmp[0] = Outline.r;
    5. tmp[1] = Outline.g;
    6. tmp[2] = Outline.b;
    7. tmp[3] = Outline.a;
    8. tmp[4] = 0;
    9. tmp[5] = 0;
    10. tmp[6] = 0;
    11. tmp[7] = 0;
    12. tmp[7] = 0;
    13.  
    14. ComputeBuffer cbuffer = new ComputeBuffer(1, 36, ComputeBufferType.Constant, ComputeBufferMode.Immutable);
    15. cbuffer.SetData(tmp);
    16. Visual.GetComponent<Renderer>().sharedMaterial.SetConstantBuffer("OtherColors", cbuffer, 0, 36);
    17. tmp.Dispose();
    My Questions:
    • I have several group properties used in diffirent shader varient. they will update per frame and may exist same time. Do I need to separate them from UnityPerMaterial cbuffer?
    • If I use custom cbuffer with SRP batcher,how can i update them properly?
     
    Last edited: Jul 1, 2022
  2. Bluedoom

    Bluedoom

    Joined:
    Aug 31, 2020
    Posts:
    14
    In 2020.3.33f1 SetConstantBuffer Worked ,but break SRP Batcher. I wonder why the batcher can't just add a command like "PSSetConstantBuffers". i just change a cbuffer isn't it?
    upload_2022-6-15_13-54-9.png
     
    Last edited: Jun 15, 2022
  3. sebj

    sebj

    Joined:
    Dec 4, 2013
    Posts:
    70
    Bump - same question for 2021.3.3f1, though in my case I create a shader variable in a ShaderGraph Custom Function node cginc, and the batcher changes the values arbitrarily (possibly depending on mesh render order).