Search Unity

CBuffer and Compute shader.

Discussion in 'Scripting' started by smb02dunnal, Jan 5, 2013.

  1. smb02dunnal

    smb02dunnal

    Joined:
    May 5, 2012
    Posts:
    439
    Hello there!

    Would anyone be kind enough to give me a quick snippet of code to show me how to use cbuffers in a compute shader, and how to assign values to them via scripting?

    I can create a cbuffer in a compute shader the normal way I would do it using DX11 but I'm unclear on how to assign values from scripting.

    Cheers,
    Alex
     
  2. smb02dunnal

    smb02dunnal

    Joined:
    May 5, 2012
    Posts:
    439
    Anyone?
     
  3. cecarlsen

    cecarlsen

    Joined:
    Jun 30, 2006
    Posts:
    864
    Bump! And I ran into the same question five years later. Anyone out there who knows the answer by now?
     
  4. scrawk

    scrawk

    Joined:
    Nov 22, 2012
    Posts:
    804
    Just come across this issue myself and thought I would add my 2 cents.

    As far as I can tell you declare the cbuffer the same as in hlsl/DX11 for Unity's compute shader.

    Code (CSharp):
    1. cbuffer CB
    2. {
    3.     int iLevel;
    4.     int iLevelMask;
    5.     int iWidth;
    6.     int iHeight;
    7. };
    The CBUFFER_START macro used for normal shader does not seem to work for compute.

    As for how they are set from a script it appears you just set each individual variable like normal (ie SetInt or whatever).
    Unity must know they belong to a group and uploads them together when needed. Cant find any documentation that says thats how its meant to work however and its possible Unity just ignores the cbuffer.
     
    cecarlsen likes this.
  5. cecarlsen

    cecarlsen

    Joined:
    Jun 30, 2006
    Posts:
    864
    I wonder how you managed to test whether CBUFFER_START-END works with ComputeShaders? I actually started using the macro last week, but I have no clue if makes any difference.
     
  6. scrawk

    scrawk

    Joined:
    Nov 22, 2012
    Posts:
    804
    If I use the macro I get a compile error for the computer shader but maybe Im doing it wrong.
     
  7. cecarlsen

    cecarlsen

    Joined:
    Jun 30, 2006
    Posts:
    864
    I just tried to turn on debug mode in the inspector and look at a compute buffer where I encapsulate constants in the Unity macro. They do appear, so my guess is that it is actually working. Drill down the inspector: Variants->Element 0->Constant Buffers.

    Here is an example.

    Code (CSharp):
    1. CBUFFER_START( Once )
    2.     bool _SimulateInLocalSpace;
    3.     bool _Crop;
    4.     float4 _CropMin;
    5.     float4 _CropMax;
    6. CBUFFER_END
    7.  
    8. CBUFFER_START( Often )
    9.     float _DragFactor;
    10.     float3 _GravityForce;
    11.     float _CurlForce;
    12.     float _CurlDetail;
    13.     float3 _GlobalForceMult;
    14.     float _Size;
    15.     float _SpeedColorMult;
    16.     float _AgingFactor;
    17. CBUFFER_END
    18.  
    19. CBUFFER_START( Always )
    20.     float _CurlTime;
    21.     float3 _EyePos;
    22. CBUFFER_END
     
  8. cecarlsen

    cecarlsen

    Joined:
    Jun 30, 2006
    Posts:
    864
    When I switch to the Vulcan API I get an interesting error. Not sure how to fix that.

    Code (CSharp):
    1. All kernels must use same constant buffer layouts
     
  9. Mytino

    Mytino

    Joined:
    Jan 28, 2019
    Posts:
    16
    Hey :) Anyone with an update here? I need to access the counter value of a compute buffer of type "counter" in all my kernels, and don't want to go via CPU, so my only option seems to be CopyCount (unless I'm missing something). My plan was to try to CopyCount into a constant buffer, but can't really figure out the constant buffers in the first place. I could use a structured buffer, but would like it to be as fast as possible.

    Edit: Seems like CopyCount only works for raw and indirect compute buffers, so my idea will likely fail anyway, but I'm still interested in hearing if you've got any update on constant buffers in general.
     
    Last edited: Mar 22, 2020
  10. Jens_Restemeier

    Jens_Restemeier

    Joined:
    Apr 30, 2015
    Posts:
    14
    Necro-response:
    #include "HLSLSupport.cginc"
    to get the definition for CBUFFER_START/CBUFFER_END.
    (This seems to be automatically added in some cases for "normal" shaders.)
     
    poka_ likes this.