Lambdas, or functions as function parameters, in Sfx

Search for a command to run...

No comments yet. Be the first to comment.
Wordpress sometimes needs you to update the php version. Outside of a managed hosting, if you’re working with raw Linux, you’re pretty much stuck. Ignoring the official, unhelpful page at https://wordpress.org/support/update-php, what actually has to...
rem building v8 rem following https://medium.com/angular-in-depth/how-to-build-v8-on-windows-and-not-go-mad-6347c69aacd4 echo off set Path=%cd%\depot_tools;%Path% set DEPOT_TOOLS_WIN_TOOLCHAIN=0 set GYP_MSVS_VERSION=2022 rem curl -O https://storage...
Find your Visual Studio install directory. It is probably something like "C:\Program Files\Microsoft Visual Studio\2022\Community" Navigate to the subfolder "Common7\Packages\Debugger\Visualizers". As administrator, edit the file default.natstepfilte...
The Sfx shader language and shader variants 19 Sep Written By Roderick Kennedy For some reason there seems to be a lot of interest in custom and alternative game engine development now. Whatever the cause of this mysterious uptick, I thought I'd post...
Why the Metaverse Failed 11 Sep Written By Roderick Kennedy The research firm Gartner has a way of visualizing the development and adoption of new technologies. After an initial work and investment gold rush, there follows a "trough of disillusionmen...

Following the work in my previous post, I happened across this Mastodon post about how good it would be to use lambdas, or function objects, as inputs in shaders.
[

](https://mastodon.gamedev.place/@meuns/111119173437770101)
It occurred to me that in our Sfx framework this might not be difficult to implement.
So I’ve implemented it. Because Sfx is essentially just a big text-processing system with semantic knowledge of shader syntax, it wasn’t that difficult. You declare a function input as follows.
shader vec4 PS_Variants(posTexVertexOutput IN,variant function<vec4(posTexVertexOutput)> RenderFunction) : SV_TARGET { return RenderFunction(IN); }
For now, we actually discard the function object’s template parameters, but later on this will be used for error-checking. Then, in the pass declaration we would do, e.g.
SetPixelShaderVariants(ps_variants({PSNeon, PSWhite}));
And this would generate two variants, calling the function PSNeon(posTexVertexOutput IN) or PSWhite(posTexVertexOutput IN).
In the variant-generation stage, where we assemble the source for compilation to your specified language, variant parameters that are functions are replaced in the function content with the specific function for that variant, e.g. for GLSL this generates:
layout(location =0) in Block { posTexVertexOutput BlockData0; } ioblock; layout(location = 0) out vec4 returnObject_vec4;
void PS_Variants() { posTexVertexOutput BlockData0=ioblock.BlockData0; #line 1366 "C:/Teleport/client/Shaders/test.sfx" {returnObject_vec4=PSWhite(BlockData0);return;} }
At the moment, this only works for shader functions, we can’t pass lambdas down to the functions it calls. But I think it can easily be extended - the trick will be that when calling a function that itself has variants, we’ll need an intermediate syntax which eliminates the lambdas from the parameter list, but indicates to the shader constructor that a specialized instance of the called-function must be created.