Search Unity

Question how to set value to the structuredBuffer defined in .cginc?

Discussion in 'Shaders' started by KiraSnow, Sep 5, 2022.

  1. KiraSnow

    KiraSnow

    Joined:
    Feb 22, 2021
    Posts:
    21
    i just split my thousand lines shader into many .cginc files,
    in this case i also moved a structuredBuffer<int> into .cginc file.
    but i don't known how to pass value to it ...
    for the other values i put in .cginc, i defined them in the shader's properties,
    however i didn't found how to define a structure buffer in the properties, so my structuredBuffer can't get my value pass from c#...
     
  2. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    StructuredBuffer is something that only exists at runtime, so it cannot be a property. Stuff in the "Properties" section is purely for editor/material value serialization, stuff you set in the material inspector.

    Use https://docs.unity3d.com/ScriptReference/Material.SetBuffer.html from a C# script to setup the buffer when you need to.
     
  3. KiraSnow

    KiraSnow

    Joined:
    Feb 22, 2021
    Posts:
    21
    yes i'm using setBuffer to set my StructureBuffer, but the problem is i want to set a StructureBuffer in the .cginc.
    if i defined StructureBuffer in the shader, my code in .cginc can't get it's value, while if i defined it in the .cginc file and include it by shader, the setBuffer would not work if i don't define it in properties.
     
  4. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    If you've
    #include "MyStuff.cginc"
    then the buffer will be accessible in the shader.
    There's no reason the SetBuffer command wouldn't work, whether it's defined right in the shader or through a CGINC. All that happens with a CGINC/HLSL include file is that the compiler literally copies the code from the include file into your shader and then compiles the shader, it's no different.
    Can you show how you're including the CGINC file?
     
  5. KiraSnow

    KiraSnow

    Joined:
    Feb 22, 2021
    Posts:
    21
    thanks for reply, it's my miss take,
    i thought it's setBuffer bug because i've seen a blog said that i need to use properties to pass value for cginc. but actually my render code is correct, the real problem is in the compute shader part for generate input data, this part of code is wrong before i split my shader, and i didn't realise it until i split shader and do a unit test