libsdl-org / SDL_shadercross

Shader translation library for SDL's GPU API.
zlib License
57 stars 18 forks source link

Compile-time define support #62

Open kg opened 5 hours ago

kg commented 5 hours ago

When using fxc I can pass a bunch of command-line switches to #define various things to build a given shader in multiple ways. One use case for this is if you have an ubershader you want to specialize for a given permutation, but there are simpler use cases too.

The permutation thing would look like i.e.

#ifndef GET_TYPE
#define GET_TYPE vertexAttributes.type
#endif

// ...
switch (GET_TYPE) {
  case TYPE_LineSegment:
    // ...
  case TYPE_Ellipse:
    // ...
}
// ...

#ifdef OUTLINED
result = outline(result, distance);
#endif
// ...

And then at build time you would do something like

shadercross sdf.hlsl -DGET_TYPE=TYPE_LineSegment -DOUTLINED ...

A trivial example would be doing -DDEBUG when building your shaders for debug builds of your game and -DRELEASE for release builds, to include diagnostic stuff in your shaders. For example my sprite shader has diagnostic markers that appear if a uniform is set, and I only need to include that in the shader for debug builds.

thatcosmonaut commented 4 hours ago

I guess this means we'll need to add a defines array to the HLSL compilation functions. It's either that or a function like SDL_ShaderCross_SetHLSLDefines.