Search code examples
opengl-es-2.0glsles

How to declare a function that returns an array in glsl es (version 100)


The following shader method:

float[1] GetArray()
{
    float array[1];
    array[0] = 1.0;
    return array;
}

Gives me: ERROR: 0:1: 'GetArray' : syntax error: Array size must appear after variable name


Solution

  • I found a way to work around this limitation. You can return an array by modifying the passed in array by reference. Here is a sample fragment shader:

    void GetArray(inout vec4 array[1])
    {
        array[0] = vec4(.5,.2,.1,1.0);
    } 
    
    void main()
    {
        vec4 test[1];
        GetArray(test);
        gl_FragColor = test[0];
    }