UnitTests/Shader: Add sanitization and optimizations unit tests
This commit is contained in:
parent
4e5def1095
commit
2f64e493de
|
|
@ -0,0 +1,145 @@
|
|||
#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 ExpectOptimization(std::string_view sourceCode, std::string_view expectedOptimizedResult)
|
||||
{
|
||||
Nz::ShaderAst::StatementPtr shader;
|
||||
REQUIRE_NOTHROW(shader = Nz::ShaderLang::Parse(sourceCode));
|
||||
REQUIRE_NOTHROW(shader = Nz::ShaderAst::Sanitize(*shader));
|
||||
REQUIRE_NOTHROW(shader = Nz::ShaderAst::Optimize(*shader));
|
||||
|
||||
ExpectNZSL(*shader, expectedOptimizedResult);
|
||||
}
|
||||
|
||||
TEST_CASE("optimizations", "[Shader]")
|
||||
{
|
||||
WHEN("propaging constants")
|
||||
{
|
||||
ExpectOptimization(R"(
|
||||
[entry(frag)]
|
||||
fn main()
|
||||
{
|
||||
let output = 8.0 * (7.0 + 5.0) * 2.0 / 4.0 - 6.0;
|
||||
}
|
||||
)", R"(
|
||||
[entry(frag)]
|
||||
fn main()
|
||||
{
|
||||
let output: f32 = 42.000000;
|
||||
)");
|
||||
}
|
||||
|
||||
WHEN("propaging vector constants")
|
||||
{
|
||||
ExpectOptimization(R"(
|
||||
[entry(frag)]
|
||||
fn main()
|
||||
{
|
||||
let output = vec4<f32>(8.0, 2.0, -7.0, 0.0) * (7.0 + 5.0) * 2.0 / 4.0;
|
||||
}
|
||||
)", R"(
|
||||
[entry(frag)]
|
||||
fn main()
|
||||
{
|
||||
let output: vec4<f32> = vec4<f32>(48.000000, 12.000000, -42.000000, 0.000000);
|
||||
)");
|
||||
}
|
||||
|
||||
WHEN("eliminating simple branch")
|
||||
{
|
||||
ExpectOptimization(R"(
|
||||
[entry(frag)]
|
||||
fn main()
|
||||
{
|
||||
if (5 + 3 < 2)
|
||||
discard;
|
||||
}
|
||||
)", R"(
|
||||
[entry(frag)]
|
||||
fn main()
|
||||
{
|
||||
|
||||
}
|
||||
)");
|
||||
}
|
||||
|
||||
WHEN("eliminating multiple branches")
|
||||
{
|
||||
ExpectOptimization(R"(
|
||||
[entry(frag)]
|
||||
fn main()
|
||||
{
|
||||
let output = 0.0;
|
||||
if (5 <= 3)
|
||||
output = 5.0;
|
||||
else if (4 <= 3)
|
||||
output = 4.0;
|
||||
else if (3 <= 3)
|
||||
output = 3.0;
|
||||
else if (2 <= 3)
|
||||
output = 2.0;
|
||||
else if (1 <= 3)
|
||||
output = 1.0;
|
||||
else
|
||||
output = 0.0;
|
||||
}
|
||||
)", R"(
|
||||
[entry(frag)]
|
||||
fn main()
|
||||
{
|
||||
let output: f32 = 0.000000;
|
||||
output = 3.000000;
|
||||
}
|
||||
)");
|
||||
}
|
||||
|
||||
|
||||
WHEN("eliminating multiple splitted branches")
|
||||
{
|
||||
ExpectOptimization(R"(
|
||||
[entry(frag)]
|
||||
fn main()
|
||||
{
|
||||
let output = 0.0;
|
||||
if (5 <= 3)
|
||||
output = 5.0;
|
||||
else
|
||||
{
|
||||
if (4 <= 3)
|
||||
output = 4.0;
|
||||
else
|
||||
{
|
||||
if (3 <= 3)
|
||||
output = 3.0;
|
||||
else
|
||||
{
|
||||
if (2 <= 3)
|
||||
output = 2.0;
|
||||
else
|
||||
{
|
||||
if (1 <= 3)
|
||||
output = 1.0;
|
||||
else
|
||||
output = 0.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
)", R"(
|
||||
[entry(frag)]
|
||||
fn main()
|
||||
{
|
||||
let output: f32 = 0.000000;
|
||||
output = 3.000000;
|
||||
}
|
||||
)");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,81 @@
|
|||
#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/SanitizeVisitor.hpp>
|
||||
#include <catch2/catch.hpp>
|
||||
#include <cctype>
|
||||
|
||||
TEST_CASE("sanitizing", "[Shader]")
|
||||
{
|
||||
WHEN("splitting branches")
|
||||
{
|
||||
std::string_view nzslSource = R"(
|
||||
struct inputStruct
|
||||
{
|
||||
value: f32
|
||||
}
|
||||
|
||||
external
|
||||
{
|
||||
[set(0), binding(0)] data: uniform<inputStruct>
|
||||
}
|
||||
|
||||
[entry(frag)]
|
||||
fn main()
|
||||
{
|
||||
let value: f32;
|
||||
if (data.value > 3.0)
|
||||
value = 3.0;
|
||||
else if (data.value > 2.0)
|
||||
value = 2.0;
|
||||
else if (data.value > 1.0)
|
||||
value = 1.0;
|
||||
else
|
||||
value = 0.0;
|
||||
}
|
||||
)";
|
||||
|
||||
Nz::ShaderAst::StatementPtr shader = Nz::ShaderLang::Parse(nzslSource);
|
||||
|
||||
Nz::ShaderAst::SanitizeVisitor::Options options;
|
||||
options.splitMultipleBranches = true;
|
||||
|
||||
REQUIRE_NOTHROW(shader = Nz::ShaderAst::Sanitize(*shader, options));
|
||||
|
||||
ExpectNZSL(*shader, R"(
|
||||
[entry(frag)]
|
||||
fn main()
|
||||
{
|
||||
let value: f32;
|
||||
if (data.value > (3.000000))
|
||||
{
|
||||
value = 3.000000;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (data.value > (2.000000))
|
||||
{
|
||||
value = 2.000000;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (data.value > (1.000000))
|
||||
{
|
||||
value = 1.000000;
|
||||
}
|
||||
else
|
||||
{
|
||||
value = 0.000000;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
)");
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue