Skip to content
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

Merged
merged 29 commits into from
Jan 17, 2023
Merged

Shader include support #6902

merged 29 commits into from
Jan 17, 2023

Conversation

Jhonnyg
Copy link
Contributor

@Jhonnyg Jhonnyg commented Aug 22, 2022

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)

  • Prepared the PR for release notes
    • Proper description of feature or fix (see example x?)
    • Added a #fixes ### if it fixes an issue
    • Assigned issue to a project
  • [-] Added documentation for public API changes
  • Is this a new feature?

@Jhonnyg Jhonnyg added feature request A suggestion for a new feature engine Issues related to the Defold engine render labels Jan 3, 2023
// 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!
Copy link
Contributor Author

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 {
Copy link
Contributor Author

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

Comment on lines +100 to +102
// 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
Copy link
Contributor Author

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..

@Jhonnyg Jhonnyg requested review from JCash and vlaaad January 9, 2023 13:40
@Jhonnyg Jhonnyg marked this pull request as ready for review January 9, 2023 13:40
editor/src/clj/editor/workspace.clj Outdated Show resolved Hide resolved
editor/src/clj/editor/workspace.clj Outdated Show resolved Hide resolved
editor/src/clj/editor/workspace.clj Outdated Show resolved Hide resolved
Copy link
Contributor

@vlaaad vlaaad left a 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.

editor/src/clj/editor/code/shader.clj Outdated Show resolved Hide resolved
editor/src/clj/editor/workspace.clj Outdated Show resolved Hide resolved
@Jhonnyg
Copy link
Contributor Author

Jhonnyg commented Jan 10, 2023

@matgis @vlaaad fixed yer concerns

vlaaad
vlaaad previously approved these changes Jan 11, 2023
Copy link
Contributor

@vlaaad vlaaad left a 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*$")
Copy link
Contributor

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)?

Copy link
Contributor

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]+)")

https://regex101.com/r/q99Yz4/1

Copy link
Contributor Author

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?

Copy link
Contributor Author

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"

Copy link
Contributor

@JCash JCash left a 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

Comment on lines +76 to +78
for (String path : includes) {
taskBuilder.addInput(this.project.getResource(path));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

:view-opts glsl-opts}
{:ext "glsl"
:label "Shader Include"
:icon "icons/64/Icons_29-AT-Unknown.png"
Copy link
Contributor

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*$")
Copy link
Contributor

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]+)")

https://regex101.com/r/q99Yz4/1

@Jhonnyg Jhonnyg merged commit 6658043 into dev Jan 17, 2023
@Jhonnyg Jhonnyg deleted the issue-6892-shader-include-support branch January 17, 2023 00:08
@Jhonnyg Jhonnyg changed the title Issue 6892: Shader include support Shader include support Feb 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
engine Issues related to the Defold engine feature request A suggestion for a new feature render
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Include support in shader files
4 participants