Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Second order functions in GLSL?

I'm looking for a way to use a function as an argument to another function in GLSL. In regular C, it can be simulated by passing a function pointer as a function argument. It also seems that other languages (like HLSL) now provide ways to deal with high-level constructs like higher-order functions, or can simulate them with clever use of HLSL structures. unfortunately I'm stuck with GLSL for now, and I can't find any way to simulate higher-order functions. Is it really impossible in current (4.2) GLSL ? Or am I missing some clever trick ?

common example of what I'm trying to achieve :

int f(someType f2, int i) {
    return f2(i);
}
like image 510
Alex Repain Avatar asked Feb 29 '12 14:02

Alex Repain


People also ask

How do you create a function in GLSL?

A function in GLSL must be declared before it can be used, a bit like defining a variable. This feature is the same as in C and was deemed unnecessary with the development of Java. Note unlike C or Java a function in GLSL cannot be called recursively. This is the code within a function cannot call the same function.

Does OpenGL support HLSL?

The GLSL that is referred to herein is compatible with OpenGL ES 2.0; the HLSL is compatible with Direct3D 11.

Does GLSL have loops?

The while and do-while loops are optional. The only loop construct you are guaranteed to have is the for loop. In addition, there are many restrictions on the looping constructs. In general “control flow is limited to loops where the maximum number of iterations can easily be determined at compile time.”


1 Answers

I'm looking for a way to use a function as an argument to another function in GLSL.

Short answer: you can't.

The closest thing to this kind of functionality you'll get in GLSL is shader subroutines. And that only allows the external OpenGL API to select which subroutine to use, not the shader itself.

So just do the switch/case statement and get it over with.

like image 173
Nicol Bolas Avatar answered Sep 23 '22 01:09

Nicol Bolas