Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

How to set my constant buffer to compute shader

Discussion in 'General Graphics' started by TheCelt, Aug 17, 2020.

  1. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    723
    Hello

    I have a buffer which i want to declare as constant since i only need to read from it, but i get an error when i attempt to set it.

    In the compute shader i have:


    Code (csharp):
    1. #pragma kernel CSMain
    2.  
    3. RWBuffer<float2> Result;
    4. const Buffer<float2> Twiddles;
    Then in C# i have:


    Code (CSharp):
    1. _twiddleBuffer = new ComputeBuffer(_size,sizeof(float)*2,ComputeBufferType.Constant);
    2. _twiddleBuffer.SetData(_twiddleData);
    3.  
    4. int tID = Shader.PropertyToID("Twiddles");
    5. _computeShader.SetConstantBuffer(tID, _twiddleBuffer, 0, sizeof(float) * _size);
    But when i run my code i get this error every time:

    Code (csharp):
    1. Compute shader (CSMain): Property (Twiddles) at kernel index (7) is not set
    What am i doing wrong here ?
     
  2. AleksiUnity

    AleksiUnity

    Unity Technologies

    Joined:
    Mar 31, 2020
    Posts:
    6
    The reason it doesn't work is that you haven't defined a constant buffer in the shader.
    In HLSL syntax you define constant buffers with cbuffer. (ie. not const Buffer<>).
    How ever, that would not work correctly on all API:s.

    So instead you should use macros to do define it.

    https://docs.unity3d.com/Manual/SL-BuiltinMacros.html

    CBUFFER_START(MyRarelyUpdatedVariables)
    float4 _SomeGlobalValue;
    CBUFFER_END
     
  3. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,240
    Alternatively, if you decide you want to keep using Buffer<float2>, the syntax for read-only vs writeable is:

    Buffer<float2> mybuffer; // read-only buffer
    RWBuffer<float2> mybuffer; // writeable buffer

    For both these cases, use _computeShader.SetBuffer() from script.
     
  4. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    723
    So what is the difference to a cbuffer and a Buffer which is read only....aren't they technically both constant since you cannot write to them?

    I think the Unity docs could do with some clarification here because it only really mentions Structured Buffers in 2 lines, so i am kinda guessing a lot here.

    Most tutorials out there seem to all use Structured Buffer - unaware that you can just use Buffer for built in types.
     
    Last edited: Aug 20, 2020
  5. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,240
    The first answer to this post looks to give a decent summary:
    https://www.gamedev.net/forums/topic/624529-structured-buffers-vs-constant-buffers/

    Although I think some of that post is only relevant to NVidia GPUs, whereas AMD GPUs don't consider them as different at all, from a performance perspective.

    The very short version is that a "ConstantBuffer" is a special term for a small table of assorted values, whereas Buffer and StructuredBuffer are for arrays of the same type.

    Eg you might use a ConstantBuffer for camera params, like the FOV, Aspect Ratio, View Matrix, Near Plane, etc.
    Whereas a StructureBuffer is more likely used for "1,000 float3's to define the positions of my asteroids." Even though my second example might be constant data too.

    I agree the terminology is not ideal - it comes from the graphics vendors or microsoft or both, i think. :)
     
  6. Dinamytes

    Dinamytes

    Joined:
    Nov 3, 2016
    Posts:
    51
    ComputeShader.SetConstantBuffer is giving stackoverflow on 2021.1

    Code (CSharp):
    1. StackOverflowException: The requested operation caused a stack overflow.
    2. UnityEngine.ComputeShader.SetConstantBuffer (System.Int32 nameID, UnityEngine.ComputeBuffer buffer, System.Int32 offset, System.Int32 size) (at <21b5136302b44b7c950da7557d38186f>:0)
    3. UnityEngine.ComputeShader.SetConstantBuffer (System.Int32 nameID, UnityEngine.ComputeBuffer buffer, System.Int32 offset, System.Int32 size) (at <21b5136302b44b7c950da7557d38186f>:0)
    4. UnityEngine.ComputeShader.SetConstantBuffer (System.Int32 nameID, UnityEngine.ComputeBuffer buffer, System.Int32 offset, System.Int32 size) (at <21b5136302b44b7c950da7557d38186f>:0)
    I never managed to make it work in any version anyway.
     
    Last edited: Dec 29, 2020
  7. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,240
    please report it as a bug and we will fix :)
     
    Dinamytes likes this.
  8. INedelcu

    INedelcu

    Unity Technologies

    Joined:
    Jul 14, 2015
    Posts:
    158
    Stack overflow fixed in 2021.1.0b5 (db3e938e6c56).
     
    Dinamytes likes this.
  9. cyberjoys

    cyberjoys

    Joined:
    Dec 1, 2020
    Posts:
    64
    i was just playing around ComputeShader in 2019.4.20, where a constant buffer was declared like :
    CBUFFER_START(GlobalDispatchSize)
    uint _DispatchSize[4]; // actually intend to use this
    uint _DispatchSize_x, _DispatchSize_y, _DispatchSize_z; // not intended to use this, that need padding
    CBUFFER_END

    then in the csharp awake/start, i computeShader.SetInts(_DispatchSize, intArry[4]), where intArry[4] was filled with values. then i output the data from the ComputeShader side, confirm that "_DispatchSize" only got x comp has data, all other comp is 0.
    if i use computeShader.SetInt(_DispatchSize_x, intX) respectively,
    _DispatchSize_x, _DispatchSize_y, _DispatchSize_z all got correct value.

    was wondering if the api is bugged in 2019.4.20 ?
     
    jolix likes this.