Page 1 of 1

Using #include inside shaders?

Posted: Sat Apr 30, 2022 11:50 pm
by idbrii
How can I #include or require() one glsl shader inside another in love2d so I don't need to copypaste common functions between shaders?

I use:

Code: Select all

#include "ladybug.glsl"
and I get:

> ERROR: '#include' : required extension not requested: GL_GOOGLE_include_directive

This answer hints that I need to set an include directive to enable the feature. I tried this and it seems to accept it:

Code: Select all

#extension GL_GOOGLE_include_directive : require
#include "ladybug.glsl"
But gives a new error:

> '#include' : Could not process include directive for header name: ladybug.glsl

I'm not sure what to do with that error message. ladybug.glsl is in the same directory as the current shader. I also tried the path from the folder containing main.lua.

This thread implies that I need to use a shader compiler, but what's the point of the include directive if that's the case?

Re: Using #include inside shaders?

Posted: Sun May 01, 2022 12:28 am
by slime
#include is not directly supported by love at the moment. It's not a native feature of GLSL either.

One option for you is to write a parser for your shaders to deal with #includes yourself, or a simpler (but less generic) option could be to manually concatenate the file contents you want together.

Re: Using #include inside shaders?

Posted: Mon Aug 29, 2022 4:13 am
by idbrii
slime wrote: Sun May 01, 2022 12:28 am One option for you is to write a parser for your shaders to deal with #includes yourself
Thanks for the suggestion! It worked great and I've finally extracted my solution and posted it online: shaderscan. In addition to handling includes, it outputs filenames in errors and auto reloads shaders on save.