Skip to content

Variables

Youness KAFIA edited this page Sep 17, 2023 · 2 revisions

Variables

Variables can be defined either globally through uniform buffer or localy. They are discriminated based on where/how they are stored (typically known thanks to the StorageClass attribute), giving information on its lifetime.

Declaration

Variables are always defined inside a shader mixin.

// this is an error
int outOfScope;

shader MyShader
{
    float a;
    int b;

    void HelloWorld()
    {
        var x = 0;
    }
}

Inside functions, rules on variable declarations are the same as in C#.

Variables are considered global when they are out of the scope.

The mixin system will make every variables available through the inheritance/composition system.

Global constants

Global constants are values defined without any staging/streaming attributes. They function as constant values usable through the shader you're compiling.

shader ParentMixin
{
    // Here we declare variables
    float4 color = float4(1);
    int initialValue;
}

shader ChildMixin : ParentMixin
{
    // This won't compile as color is already declared in the ParentMixin
    float4 color = float4(2);

    void MyFunction()
    {
        // We can access all global constants in functions
        var c = initialValue;
        c += 3;
        color *= c;
    }
}

Constant generics/Specialized constants

You can define constants through generics in the mixin system.

shader ParentMixin<int initialValue, float4 color>
{
    void ParentFunction()
    {

    }
}


// We can use constant generics to declare variables with their values.
shader ChildMixin : ParentMixin<1,float4(1)>
{
    void MyFunction()
    {
        // We can access all global constants in functions
        var c = initialValue;
        c += 3;
        color *= c;
    }
}

Those constants must have default values in every cases. If a mixin containing constant generics is used as a shader module the user will have to specify specialized constants through the C# api.

Clone this wiki locally