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

Support shader preprocessor concatenation symbol #74737

Merged
merged 1 commit into from
Aug 8, 2023

Conversation

JohanAR
Copy link
Contributor

@JohanAR JohanAR commented Mar 10, 2023

No description provided.

@@ -1074,8 +1081,12 @@ bool ShaderPreprocessor::expand_macros_once(const String &p_line, int p_line_num
}
}

result = result.substr(0, index) + " " + body + " " + result.substr(args_end + 1, result.length());
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed the padding " " because they made unit testing much more complicated. Have not found any case where this caused problems.

Copy link
Member

Choose a reason for hiding this comment

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

Should be fine, like I mentioned in RocketChat adding those spaces might have been there "just in case".

Copy link
Contributor Author

@JohanAR JohanAR Mar 14, 2023

Choose a reason for hiding this comment

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

I found one case where it changes the behaviour:

#define MOD +
float a MOD= 1.0;

Previously this would've been expanded to invalid code, while now it works. I did some tests with a C++ compiler and it doesn't work there.

Should I revert this line and update the tests to require the surrounding spaces? I think it would be difficult to relax the test to allow them without adding a full blown glsl parser/tokenizer.

On the other hand, this is probably an edge case, and I would say that (with my change) it works like I would expect it to.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Was about to revert this padding change, but then I realised that the example above would not be affected since the padding was only done for macros with arguments.

E.g. before my PR the following defines:

#define A -10
#define B(x) -x
int n = 1-A;
int m = 1-B(3);

Would be expanded to:

int n = 1--10;
int m = 1- -3 ;

I.e. the behaviour changes depending on if you have args or not, and leading to one case where the code becomes invalid.

Kind of leaning towards adding the padding for argument-less macros too now, even if it'll make my unit tests uglier.

@clayjohn clayjohn requested a review from Chaosus March 10, 2023 17:53
@clayjohn
Copy link
Member

CC @bitsawer

CHECK_SHADER_EQ(result, expected);
}

TEST_CASE("[ShaderPreprocessor] Concatenation sorting network") {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

And in case anyone was wondering, this is my actual use case that motivated this PR. I wanted to make a sorting network more easily.

Copy link
Member

@bitsawer bitsawer left a comment

Choose a reason for hiding this comment

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

Overall, looks pretty good! Some small changes needed, but nothing fundamental. Gave it a few quick tests and nothing exploded. Don't worry about making those tests look ugly and full of whitespace with weird indentation and lax comparison rules. As discussed on RocketChat, I'm working on that shader preprocessor test suite so I'll add and format your tests into it and eventually try to make a PR once I have some extra time to finish it. I can give this another test later and a core team member will also have to give their review, but I don't see any major issues.

servers/rendering/shader_preprocessor.cpp Outdated Show resolved Hide resolved
tests/servers/rendering/test_shader_preprocessor.h Outdated Show resolved Hide resolved
tests/servers/rendering/test_shader_preprocessor.h Outdated Show resolved Hide resolved
tests/servers/rendering/test_shader_preprocessor.h Outdated Show resolved Hide resolved
CHECK_SHADER_EQ(result, expected);
}

TEST_CASE_MAY_FAIL("[ShaderPreprocessor] Nested concatenation") {
Copy link
Member

Choose a reason for hiding this comment

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

This TEST_CASE_MAY_FAIL() is bit ugly in the test output, printing a big red error and the failed comparison text in the console even though the full test suite still passes. It might be better to make it a normal TEST_CASE() and "fail succesfully" to keep the "noise" in the testing results output minimal. The comment there should be enough to clarify the test case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Before I discovered the TEST_CASE_MAY_FAIL macro I had it marked both as may_fail and skip, but I'll do as you suggest.

@Calinou
Copy link
Member

Calinou commented Mar 13, 2023

Remember to document this change in https://docs.godotengine.org/en/latest/tutorials/shaders/shader_reference/shader_preprocessor.html 🙂

You can open a draft pull request on godot-docs' master branch now – no need to wait for this PR being merged.

@JohanAR
Copy link
Contributor Author

JohanAR commented Mar 14, 2023

Docs PR godotengine/godot-docs#6979

@JohanAR JohanAR force-pushed the preprocessor_concat branch 2 times, most recently from 8ce1b77 to cabdb43 Compare April 9, 2023 07:40
@bitsawer
Copy link
Member

Looks good to me after the changes. Tested with my own projects and tests, seems to work as expected. I'll add those tests to my preprocessor test suite and try to eventually make a public PR for it once I have some extra time.

@Chaosus might still want to take a look and give the final approval.

Copy link
Member

@Chaosus Chaosus left a comment

Choose a reason for hiding this comment

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

Tested and it works fine for me - just add the missing dots to the commentaries.

servers/rendering/shader_preprocessor.cpp Outdated Show resolved Hide resolved
servers/rendering/shader_preprocessor.cpp Outdated Show resolved Hide resolved
servers/rendering/shader_preprocessor.cpp Outdated Show resolved Hide resolved
tests/servers/rendering/test_shader_preprocessor.h Outdated Show resolved Hide resolved
tests/servers/rendering/test_shader_preprocessor.h Outdated Show resolved Hide resolved
tests/servers/rendering/test_shader_preprocessor.h Outdated Show resolved Hide resolved
tests/servers/rendering/test_shader_preprocessor.h Outdated Show resolved Hide resolved
@JohanAR
Copy link
Contributor Author

JohanAR commented Aug 7, 2023

Tested and it works fine for me - just add the missing dots to the commentaries.

Thanks! I'm not that used to working with github's web UI, but I think I got all the suggestions. Should I try to squash the branch or will that be done when merging?

@Chaosus
Copy link
Member

Chaosus commented Aug 7, 2023

Should I try to squash the branch or will that be done when merging?

Yes, you should.

@akien-mga akien-mga merged commit 779ca0a into godotengine:master Aug 8, 2023
15 checks passed
@akien-mga
Copy link
Member

Thanks!

AlfishSoftware added a commit to AlfishSoftware/godot-files-vscode that referenced this pull request Aug 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants