Glsl function pointer (or equivalent)

I am trying to call one of many functions based on the value of a variable. The variable is set at runtime, so the code on the CPU will not work. Using the if / switch statement will be slow due to the large number of features. [possibly wrong]

I am looking for a way to store functions in an array either using function pointers or by storing the actual equations (e.g. texcoord.x * 2) in an array.

Example psuedo-code:

    in vec2 texcoord;
    out vec4 color;

    int number; //Assigned a number between 0 and 300 during runtime

    float func1(void) {
      return texcoord.x + texcoord.y;
    }

    float func2(void) {
      return texcoord.x*2;
    }

    ...

    float func299(void) {
      return texcoord.y - 7;
    }

    void main(void) {
      number = <something calculated during runtime>;
      float output = func<number>(); //              <--------------
      color = vec4(output, output, output, 1);
    }
+7
source share
2 answers

GLSL . SPIR-V . . . .

1shader GLSL 4.00, , . , <something calculated during runtime> , , .

, , - switch. , , .

, , , . , , , .

, switch , . , . , ( , ).

, Colonel Thirty Two, . , , . , .

1 , , SPIR-V GLSL, , , ... .

+9

GLSL , :

const struct functions_list {
    int sin;
    int cos;
    int tan;
    int fract;
};
functions_list functions = functions_list(1,2,3,4);

, :

float callback(int func,float arg){
    if(func == functions.sin)
        return sin(arg);
    else if(func == functions.cos)
        return cos(arg);
    else if(func == functions.tan)
        return tan(arg);
    else if (func == functions.fract)
        return fract(arg);
    else
        return 0.0;
}
0

Source: https://habr.com/ru/post/1621747/


All Articles