Search Unity

How computebuffer works?

Discussion in 'Shaders' started by imaewyn, May 1, 2020.

  1. imaewyn

    imaewyn

    Joined:
    Apr 23, 2016
    Posts:
    211
    Hello, maybe someone can explain how works
    Code (CSharp):
    1. Shader.SetGlobalBuffer("_myBuffer", myBuffer);
    I set data into the buffer (like 1000 int array) and initialize it in shader
    Code (CSharp):
    1. uniform StructuredBuffer<min12int> _myBuffer : register(t1)
    After that i see some deacrease in my fps. And if I have another shader with registration the same buffer
    Code (CSharp):
    1. uniform StructuredBuffer<min12int> _myBuffer : register(t1)
    this also affects performance. So, I have question, why the same data in memory wchich dedicated for buffer are duplicating if I register it in several shaders?

    Approch with buffer not the only for my needs (I just want to transfer the mesh data to the shader for each custom landscape and water chunk. It's on picture). Another way pack data into textures, but it will break batching beacuse each chunk will have his own material data.




    However, seems buffer has less perfomans result despite of one it has ont material vs many materials....
    it seems strange to me
     
    Last edited: May 1, 2020
  2. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,549
    Are you doing the SetGlobalBuffer each frame? There is a cost to sending data to the GPU.
    But also it's hard to say without seeing what your shader is doing. If it's a "1000 int array" like you mention and you're looping over this array in your shader, that would cause an understandable performance hit depending on the math done.
    Are you using branching to differentiate the data in the buffer between tiles? That could have a fairly big performance hit too depending on how the branch condition is fed.
    Also, how much is it affecting performance?
    On what kind of hardware?

    You're also binding a
    StructuredBuffer
    to a Texture resource register
    register(t1)
    . The type of register you bind to has an effect on the performance in different usage scenarios. You likely want a UAV (unordered access view) register, so
    register(u1)
    for example. Or perhaps consider moving to using a Constant Buffer with
    (b1)
    , though that has it's own pros/cons depending on your use case and slightly different code for assignment.
     
    Last edited: May 2, 2020