GPU: Shader Preprocess: Add support for BLI C++ vector functional swizzle

This allow to share more code between C++ and the shader
code base.

Pull Request: https://projects.blender.org/blender/blender/pulls/138820
This commit is contained in:
Clément Foucault
2025-05-13 16:28:55 +02:00
committed by Clément Foucault
parent 90be031d6b
commit 83a40ceae2
2 changed files with 25 additions and 0 deletions

View File

@@ -209,6 +209,7 @@ class Preprocessor {
include_parse(str);
}
str = preprocessor_directive_mutation(str);
str = swizzle_function_mutation(str);
if (language == BLENDER_GLSL) {
str = loop_unroll(str, report_error);
str = assert_processing(str, filename);
@@ -813,6 +814,14 @@ class Preprocessor {
return std::regex_replace(str, regex, "");
}
std::string swizzle_function_mutation(const std::string &str)
{
/* Change C++ swizzle functions into plain swizzle. */
std::regex regex(R"((\.[rgbaxyzw]{2,4})\(\))");
/* Keep character count the same. Replace parenthesis by spaces. */
return std::regex_replace(str, regex, "$1 ");
}
void threadgroup_variables_parsing(const std::string &str)
{
std::regex regex(R"(shared\s+(\w+)\s+(\w+)([^;]*);)");

View File

@@ -685,4 +685,20 @@ using C = B::func;
}
GPU_TEST(preprocess_namespace);
static void test_preprocess_swizzle()
{
using namespace shader;
using namespace std;
{
string input = R"(a.xyzw().aaa().xxx().grba().yzww; aaaa();)";
string expect = R"(a.xyzw .aaa .xxx .grba .yzww; aaaa();)";
string error;
string output = process_test_string(input, error);
EXPECT_EQ(output, expect);
EXPECT_EQ(error, "");
}
}
GPU_TEST(preprocess_swizzle);
} // namespace blender::gpu::tests