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); }
GLSL . SPIR-V . . . .
1shader GLSL 4.00, , . , <something calculated during runtime> , , .
<something calculated during runtime>
, , - switch. , , .
, , , . , , , .
, switch , . , . , ( , ).
, Colonel Thirty Two, . , , . , .
1 , , SPIR-V GLSL, , , ... .
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; }
Source: https://habr.com/ru/post/1621747/More articles:Questions about classes extending from a base class (Java) - javaФайлы заголовков не найдены GCC - gccT-SQL table and other variables in one DECLARE block - syntaxMySQL utf8mb4 на Amazon RDS: глобальные переменные заданы правильно, но переменные не установлены - mysqlEspresso Test for Notification of Appearance - androidHow can I make it more efficient? (Merging Arrays in C) - performanceExtracting values from federated RDDs - pythonBootstrap Modal not working fully on CodePen - javascriptWhy is there a difference in how css is applied to a modal window in Chrome and FF? - cssIs it possible to indicate whether an HTTP request arises from the "save link as" action? - httpAll Articles