NazaraEngine/tests/Engine/Shader/Loops.cpp

182 lines
2.1 KiB
C++

#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 <catch2/catch.hpp>
#include <cctype>
TEST_CASE("loops", "[Shader]")
{
WHEN("using a while")
{
std::string_view nzslSource = R"(
struct inputStruct
{
value: f32
}
external
{
[set(0), binding(0)] data: uniform<inputStruct>
}
[entry(frag)]
fn main()
{
let value = 0.0;
let i = 0;
while (i < 10)
{
value += 0.1;
i += 1;
}
}
)";
Nz::ShaderAst::StatementPtr shader = Nz::ShaderLang::Parse(nzslSource);
ExpectGLSL(*shader, R"(
void main()
{
float value = 0.000000;
int i = 0;
while (i < (10))
{
value += 0.100000;
i += 1;
}
}
)");
ExpectNZSL(*shader, R"(
[entry(frag)]
fn main()
{
let value: f32 = 0.000000;
let i: i32 = 0;
while (i < (10))
{
value += 0.100000;
i += 1;
}
}
)");
ExpectSpirV(*shader, R"(
OpFunction
OpLabel
OpVariable
OpVariable
OpStore
OpStore
OpBranch
OpLabel
OpLoad
OpSLessThan
OpLoopMerge
OpBranchConditional
OpLabel
OpLoad
OpFAdd
OpStore
OpLoad
OpIAdd
OpStore
OpBranch
OpLabel
OpReturn
OpFunctionEnd)");
}
WHEN("using a for-each")
{
std::string_view nzslSource = R"(
struct inputStruct
{
value: [f32; 10]
}
external
{
[set(0), binding(0)] data: uniform<inputStruct>
}
[entry(frag)]
fn main()
{
let x = 0.0;
for v in data.value
{
x += v;
}
}
)";
Nz::ShaderAst::StatementPtr shader = Nz::ShaderLang::Parse(nzslSource);
ExpectGLSL(*shader, R"(
void main()
{
float x = 0.000000;
uint i = 0u;
while (i < (10u))
{
float v = data.value[i];
x += v;
i += 1u;
}
}
)");
ExpectNZSL(*shader, R"(
[entry(frag)]
fn main()
{
let x: f32 = 0.000000;
for v in data.value
{
x += v;
}
}
)");
ExpectSpirV(*shader, R"(
OpFunction
OpLabel
OpVariable
OpVariable
OpVariable
OpStore
OpStore
OpBranch
OpLabel
OpLoad
OpULessThan
OpLoopMerge
OpBranchConditional
OpLabel
OpAccessChain
OpLoad
OpAccessChain
OpLoad
OpStore
OpLoad
OpLoad
OpFAdd
OpStore
OpLoad
OpIAdd
OpStore
OpBranch
OpLabel
OpReturn
OpFunctionEnd)");
}
}