-
-
Notifications
You must be signed in to change notification settings - Fork 292
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Shader include support #6902
Shader include support #6902
Conversation
…/defold into issue-6892-shader-include-support
// To get access to the functions, you need to put: | ||
// #include "/my-folder/my-file.glsl" | ||
// in any script using the functions. | ||
// Please consult the manual on how to use this feature! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could include a link to the manual here but the entry doesn't exist yet since this hasn't been merged yet..
|
||
public class ShaderUtil { | ||
public static class Common { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A lot of the shader transpilation uses these functions so just moved them into a separate namespace
com.dynamo.cr/com.dynamo.cr.bob/src/com/dynamo/bob/pipeline/ShaderProgramBuilder.java
Outdated
Show resolved
Hide resolved
// Iterate the source lines to find the first occurance of a 'valid' line | ||
// i.e a line that doesn't already have a preprocessor directive, or is empty | ||
// This is needed to get a valid line number when shader compilation has failed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This (and the lines after) is used for getting debug output if the shader fails to compile in runtime and won't work 100% with pasting the include source since most includes will use #ifdef header guards and the lines won't correspond to the actual lines from the includes.
But on the other hand.. it already produces wrong #line pragmas right now anyway if you have block comments for instance, so it's already incorrect behaviour anyway and I don't know what the alternative is :(
One really roundabout way of doing it would be to split the source by each include since OpenGL supports uploading shader source by a list of strings (https://registry.khronos.org/OpenGL-Refpages/gl4/html/glShaderSource.xhtml) and then using this information to output which include the error came from. I don't think it should be a showstopper for this feature, but perhaps something that will come up eventually..
com.dynamo.cr/com.dynamo.cr.bob/src/com/dynamo/bob/pipeline/ShaderIncludeCompiler.java
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, but I agree with Mats we should use built-in features for the relativization of paths.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
It would be nice if we could create .glsl
files in the editor using File -> New menu.
:resource-ext (resource/type-ext resource)}})]) | ||
|
||
(def ^:private include-pattern #"^\s*#include \s*\"([^\"]+?)\"\s*$") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
\s*
(space before slash) -> \s*
(no spaces before slash)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or rather \s+
? +
being "1 or more" ?
Also, this doesn't support comments after the include (something I do a lot), since the $
matches the end.
Try something like this:
^\s*\#include\s*(<([^"<>|\b]+)>|"([^"<>|\b]+)")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, that regex will still allow this:
#include "yada" jiasjdsjiasdidas
Maybe:
^\s*\#include\s*(<([^"<>|\b]+)>|"([^"<>|\b]+)")\s*(?:\/\/.*)?$
https://regex101.com/r/xvR1hT/1
It requires the $, but we look at includes line-by-line and it won't work if you use block comments, but not sure we want to fix everything right now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
It would be nice if we could create
.glsl
files in the editor using File -> New menu.
It does, it's called "shader include"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Look good.
I think the include regex can be improved upon to support comments at the end of the line.
There's also a question about preserving the order-of-appearance
for (String path : includes) { | ||
taskBuilder.addInput(this.project.getResource(path)); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
com.dynamo.cr/com.dynamo.cr.bob/src/com/dynamo/bob/pipeline/ShaderProgramBuilder.java
Outdated
Show resolved
Hide resolved
:view-opts glsl-opts} | ||
{:ext "glsl" | ||
:label "Shader Include" | ||
:icon "icons/64/Icons_29-AT-Unknown.png" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps we can add a glsl icon?
:resource-ext (resource/type-ext resource)}})]) | ||
|
||
(def ^:private include-pattern #"^\s*#include \s*\"([^\"]+?)\"\s*$") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or rather \s+
? +
being "1 or more" ?
Also, this doesn't support comments after the include (something I do a lot), since the $
matches the end.
Try something like this:
^\s*\#include\s*(<([^"<>|\b]+)>|"([^"<>|\b]+)")
Added support for #include pragmas in shader files. This feature can be used to separate shader functions into modules that can be used across several shader files, which should help out with reusing code from your own project, or from asset extensions.
Fixes #6892
PR checklist (remove before merging)