UnitTests/Shader: Add cond/const if tests

This commit is contained in:
Jérôme Leclercq 2021-12-28 13:36:11 +01:00
parent 343eac6616
commit 22651255df
1 changed files with 112 additions and 0 deletions

View File

@ -0,0 +1,112 @@
#include <Engine/Shader/ShaderUtils.hpp>
#include <Nazara/Core/File.hpp>
#include <Nazara/Core/StringExt.hpp>
#include <Nazara/Shader/ShaderBuilder.hpp>
#include <Nazara/Shader/ShaderLangParser.hpp>
#include <Nazara/Shader/Ast/AstOptimizer.hpp>
#include <Nazara/Shader/Ast/SanitizeVisitor.hpp>
#include <catch2/catch.hpp>
#include <cctype>
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<inputStruct>
}
[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<inputStruct>
}
[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<inputStruct>
}
[entry(frag)]
fn main()
{
let value: f32;
value = data.value;
}
)");
}
}
}