Passing functions to shaders?

30 Views Asked by At

I am building a small machine learning implementation using compute shaders and I want to pass an array of activation functions indexed to each workgroup/invocation through an SSBO because writing an if-else/switch statement for each invocation would become problematic very quickly.

C/C++ allows functions to be passed into others by passing a pointer to the function and then dereferencing it:

void foo(int input);

void bar(int input, void(*foo)(int)) {
    *foo(input);
}
//Side note, it is not necessary for me that the function is a void function,
// it can return an int, double, float, etc.

However, I don’t think this exact implementation can be done with shaders since the data "lives" on the GPU, so pointers to memory on the CPU might not be a valid method.

So, is it possible to pass functions to a compute shader (GLSL, HLSL, or SPIR-V) in a storage buffer or via some other method? I am currently using the Vulkan framework, but I am open to implementations using other computer graphics frameworks.

0

There are 0 best solutions below