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. Dismiss Notice

Feature Request Why are int and uint properties/nodes absent from Shadergraph?

Discussion in 'Shader Graph' started by daneobyrd, Jul 18, 2022.

  1. daneobyrd

    daneobyrd

    Joined:
    Mar 29, 2018
    Posts:
    101
    Precision is always an important factor to keep in mind when creating shaders, and Shadergraph has provided access to float and half precision for a long time.

    However, int and uint property types are still absent despite them being natively supported by HLSL (and therefore by the custom function node). VFX Graph uses both ints and uints.

    I think (and hope) that adding int/uint blackboard properties and nodes is simpler than some other feature requests.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,236
    Unity materials don’t support integers, only floats. Though Unity’s Shader Lab lets you define properties as “integers”, the material inspector and internal data stores that value as a float that gets transformed into an integer when passed to the GPU, so there’s no precision benefit to be gained.

    If you want real integers, you need to use a structure buffer to pass the data to the shader. Structured buffers are only supported when using a custom function node using an external file, so there’s not a reason to add support for it in the graph.
     
    daneobyrd likes this.
  3. daneobyrd

    daneobyrd

    Joined:
    Mar 29, 2018
    Posts:
    101
    Thanks for the insight! I've been writing a lot of HLSL for custom function nodes lately and I kept thinking that it would be nice to use ints and uints like many builtin functions do. I had no idea that integers are stored as floats and then transformed to ints later.

    It would be nice to have int nodes and have it as a possible input/output type for custom function nodes; even if adding support for int properties wouldn't make sense.
     
  4. daneobyrd

    daneobyrd

    Joined:
    Mar 29, 2018
    Posts:
    101
    Somehow in all my time using shader graph I didn't know there was an int node. It will turn display as a float when in the blackboard and in the material inspector, as bgolus said.
     
  5. StuwuStudio

    StuwuStudio

    Joined:
    Feb 4, 2015
    Posts:
    165
    I'm using Graphics.DrawProceduralIndirect, I have this "IndexBuffer" and "VertBuffer". I need to take in vertex id, an integer, and use it to find the index, then the vertex data so I can extract the attributes.

    What can I do in this case? My custom function has an int parameter, but the vertex id node probably returns float that'll then get casted back as a int, that sounds like an issue.
     
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,236
    Not a problem at all.

    Material properties can't be true integer values, but shader values can be. Specifically, you can pass integers, or several other numerical types not supported by materials, to shaders via structured buffers as mentioned previously, or constant buffers. Unity has also recently added a
    Material.SetInteger()
    that I believe actually keep the values as signed 32 bit integers at all times rather than converting to floats, though I've not experimented with it to know for sure.

    But all of that is moot, because you don't need to set anything to your shader and indeed shouldn't because the GPU already sets that data via
    SV_VertexID
    .
    Code (csharp):
    1. v2f vert(uint vertexID : SV_VertexID)
    2. {
    3.   uint vertexIndex = indexBuffer[vertexID];
    4.   float3 vertexPosition = vertexBuffer[vertexIndex];
    5.   // etc
    6. }