Xpanda - shader code reuse made simpler

kraifpatrik

Member
Hey everyone,

as I work quite a lot with shaders in GMS2, I've found it really a pitty that there was no easy way to reuse shader functions between projects or even multiple shaders in a single project, that would not require copy-pasting them all over the place. In HLSL, it is possible to use #include preprocessor command, but that takes files from god knows which directory (in GMS1 it was the included files) and GLSL specification does not support includes at all (except for some extension that didn't seem to work).

And so I decided to create my own tool that solves this problem. It is called Xpanda and it is available for free on the GameMakerDiscord github, including a growing library of handy functions. With this tool you can just write #pragma include("SomeCoolFunctions.sh") (almost) anywhere in your shaders (both GLSL and HLSL) and run the tool to copy the contents of the specified file to the shader. I've also made it in a way that it has a translation table (like Mat3 will translate to mat3 in GLSL and float3x3 in HLSL etc.), so you can easier make functions compatible and reusable in both GLSL and HLSL. Well, you can find more info and full how-to at the repo.

So if you are interested, you can just check it out and also I would really welcome if you let me know whether you like it or not and what you think I could improve.

Cheers,

Patrik

PS: I have a history of making threads in wrong sections, so pardon me if this is another case.
 

kburkhart84

Firehammer Games
Does it not work with GLSLES or was that just an oversight in your description?

So this copies it upon doing the run tool beforehand, not at compile time right? So if I were to make changes to those functions I added, I'd have to delete them from the shaders, then re-add them? Or if I run the tool does it somehow store what it did so if I update it you do that part automatically as well? That would be extremely handy for someone writing things for 3d, as you could easily iterate changes while testing.
 

kraifpatrik

Member
I'm not 100% sure, but I think that the function and type names are same for GLSL and GLSL ES, so it should work for both, but I will have a look at that to make sure.
The included code you don't have to delete by hand before running the tool again, it does that on it's own. I will fix the description in the repo so it's more clear :)
 

kburkhart84

Firehammer Games
WOW, if it works for GLSLES and deletes/repastes the code, then it gets around one of the biggest issues I have with GMS2's shader system. I have it understood that the regular DX/OGL APIs let you specify vertex and fragment shaders separately, so you could use the same vertex shader with multiple fragment shaders. GMS2 of course doesn't let you do that, so if you want multiple fragment shaders, you will have repeated code in the vertex shaders. So for all the different types of shaders(I'm thinking 3d usage) you end up with lots of duplicate code and shaders. If XPanda does things the way you say, it will help immensely, especially in the creation process when you are iterating over shader code.
 

kraifpatrik

Member
Just wanted to let you guys know that I've revisited the project and finally released a new update! You can now write much more complex branching directives, as well as define custom constants (previously only XGLSL, XHLSL, XHLSL9 and XHLSL11 were available). This is super handy for creating different shader permutations.

Code:
#if XGLSL
  // Code to include only in GLSL shaders
#else
  // Code to include only in HLSL shaders
#endif

#if XHLSL
  // Code common for both HLSL9 and HLSL11
  #if XHLSL9
   // HLSL9 specific code
  #else
   // HLSL11 specific code
  #endif
#endif

// Complex conditions like these are also supported
#if (X * 2 > 4) && !((A || B) && C)
  // Constants X, A, B, C can be defined through command line parameters
#endif
Check out README at https://github.com/GameMakerDiscord/Xpanda for more info :)

EDIT: Xpanda now supports minification, which removes comments and whitespace from code.
 
Last edited:
Top