![]() |
VPP
0.7
A high-level modern C++ API for Vulkan
|
We have already shown in How to write shader and computation code how to create functions in GPU code. But those were only local to the shader. What to do if you want a reusable function across multiple shaders, or even a function library?
There are two approaches to this task. The first one is to create a macro function, while the second one results in a fully reusable GPU-level function.
As we alreaty know, using regular C++ constructs inside GPU code works like they were macro constructs. The code is unfolded and inlined inside generated SPIR-V. Therefore we can define a function like this:
What will happen if we call such Gcd
function from GPU code, like this?
The entire Gcd
code will be inlined into the caller's code and the program will behave as expected - i.e. as if the Gcd
function were called.
This way is very useful for small functions. The Gcd
function is not a very good candidate, though. It defines several mutable variables and if being called multiple times, these variables will be declared each time. This produces excessive number of mutable variables and can hurt performance (immutable variables do not have this problem). C++ optimizer will not be able to help here. Code optimizers in drivers theoretically could, but we should not rely on this.
For such complex functions with local mutable variables, a full-fledged GPU function is better.
So we are falling back to vpp::Function() syntax. This time however, we will wrap it into a C++ class:
Then, in the client code we use it like this:
Variables will not be declared multiple times, because they are hidden inside SPIR-V level function definition. The definition itself is generated when GFunGCD
object initializes. The calls which occur below, just generate a SPIR-V call instruction.
As you can see, creating reusable GPU funtions is simple - just make C++ functors of them, using the syntax presented above.
In case of small functions that do not create mutable variables, consider using macro functions as they have a chance to be faster.