【发布时间】:2021-01-26 02:46:53
【问题描述】:
我试图根据变量的值调用许多函数之一。该变量是在运行时设置的,因此 CPU 上的代码将不起作用。由于存在大量可能性,使用 if/switch 语句会很慢。[可能不正确]
我正在寻找一种将函数存储在数组中的方法,方法是使用函数指针,或者将实际方程(例如 texcoord.x*2)存储在数组中。
示例伪代码:
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);
}
【问题讨论】:
-
“由于可能性很大,使用 if/switch 语句会很慢。”这是一个非常大胆的假设。
-
如果你所有的函数都是线性方程,你可以创建一个向量数组,得到tex坐标和向量的点积。 (例如
func1(texcoord) == dot(vec3(texcoord, 1), vec3(1,1,0))、func299(texcoord) == dot(vec3(texcoord, 1), vec3(0, 1, -7))
标签: glsl function-pointers raytracing