From 22651255df2c2fafd4b31d8a54235b84d64d6947 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Leclercq?= Date: Tue, 28 Dec 2021 13:36:11 +0100 Subject: [PATCH] UnitTests/Shader: Add cond/const if tests --- tests/Engine/Shader/Const.cpp | 112 ++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 tests/Engine/Shader/Const.cpp diff --git a/tests/Engine/Shader/Const.cpp b/tests/Engine/Shader/Const.cpp new file mode 100644 index 000000000..96d72c8c7 --- /dev/null +++ b/tests/Engine/Shader/Const.cpp @@ -0,0 +1,112 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +void ExpectOutput(Nz::ShaderAst::Statement& shader, const Nz::ShaderAst::SanitizeVisitor::Options& options, std::string_view expectedOptimizedResult) +{ + Nz::ShaderAst::StatementPtr sanitizedShader; + REQUIRE_NOTHROW(sanitizedShader = Nz::ShaderAst::Sanitize(shader, options)); + + ExpectNZSL(*sanitizedShader, expectedOptimizedResult); +} + +TEST_CASE("const", "[Shader]") +{ + WHEN("using const if") + { + std::string_view sourceCode = R"( +option UseInt: bool = false; + +[cond(UseInt)] +struct inputStruct +{ + value: i32 +} + +[cond(!UseInt)] +struct inputStruct +{ + value: f32 +} + +external +{ + [set(0), binding(0)] data: uniform +} + +[entry(frag)] +fn main() +{ + let value: f32; + + const if (UseInt) + { + value = f32(data.value); + } + else + { + value = data.value; + } +} +)"; + + Nz::ShaderAst::StatementPtr shader; + REQUIRE_NOTHROW(shader = Nz::ShaderLang::Parse(sourceCode)); + + Nz::ShaderAst::SanitizeVisitor::Options options; + + WHEN("Enabling option") + { + options.optionValues[0] = true; + + ExpectOutput(*shader, options, R"( +struct inputStruct +{ + value: i32 +} + +external +{ + [set(0), binding(0)] data: uniform +} + +[entry(frag)] +fn main() +{ + let value: f32; + value = f32(data.value); +} +)"); + } + + WHEN("Disabling option") + { + options.optionValues[0] = false; + + ExpectOutput(*shader, options, R"( +struct inputStruct +{ + value: f32 +} + +external +{ + [set(0), binding(0)] data: uniform +} + +[entry(frag)] +fn main() +{ + let value: f32; + value = data.value; +} +)"); + } + } +}