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
Bump! And I ran into the same question five years later. Anyone out there who knows the answer by now?
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): cbuffer CB { int iLevel; int iLevelMask; int iWidth; int iHeight; }; 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.
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.
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): CBUFFER_START( Once ) bool _SimulateInLocalSpace; bool _Crop; float4 _CropMin; float4 _CropMax; CBUFFER_END CBUFFER_START( Often ) float _DragFactor; float3 _GravityForce; float _CurlForce; float _CurlDetail; float3 _GlobalForceMult; float _Size; float _SpeedColorMult; float _AgingFactor; CBUFFER_END CBUFFER_START( Always ) float _CurlTime; float3 _EyePos; CBUFFER_END
When I switch to the Vulcan API I get an interesting error. Not sure how to fix that. Code (CSharp): All kernels must use same constant buffer layouts
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.
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.)