Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

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:
    527
    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:
    765
    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:
    527
    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:
    765
    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:
    527
    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:
    527
    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