Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Does using RWStructuredBuffer using as StructuredBuffer has performance penalty?

Discussion in 'Shaders' started by genetic_priest, Feb 11, 2021.

  1. genetic_priest

    genetic_priest

    Joined:
    Jan 17, 2021
    Posts:
    5
    In many Unity project I see common pattern of forward declaring shaders and buffers and using some buffer in compute shader as UAV and then using it as SRV for read though it is declared as RWStructuredBuffer. This may affect performance due to binding as UAV may cause unnecessary cache flushes.
    Is there any way to declare shader in some region with all resources in same shader file? For example
    #pragma kernel kernel1
    StructuredBuffer<float> buffer1;
    RWStructuredBuffer<float3> buffer2;

    #pragma kernel kernel2
    StructuredBuffer<float> buffer2;
    RWStructuredBuffer<float3> buffer3;

    So that resource state would be correct for each shader. This would also greatly increased ReanderDoc readablity.
     
  2. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,034
    Right now there's no way to have the resources declared per kernel in the same shader file.
    You could use different names for them.
    Like this:
    StructuredBuffer<float> kernel1_buffer1;
    RWStructuredBuffer<float3> kernel1_buffer2;
    StructuredBuffer<float> kernel2_buffer2;
    RWStructuredBuffer<float3> kernel2_buffer3;
     
  3. genetic_priest

    genetic_priest

    Joined:
    Jan 17, 2021
    Posts:
    5
    Thanks a lot!
    But I see that there is an ability to declare define per shader kernel - is it suitable for isolating shader code and its resources?
     
  4. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,034
    Yes, but you'd need to have a define per kernel.
     
    genetic_priest likes this.