Fix compilation

This commit is contained in:
Jérôme Leclercq
2022-03-04 18:16:12 +01:00
parent 0c3607579e
commit 1919bd3302
22 changed files with 322 additions and 136 deletions

View File

@@ -11,6 +11,9 @@ TEST_CASE("structure member access", "[Shader]")
SECTION("Nested member loading")
{
std::string_view nzslSource = R"(
[nzsl_version("1.0")]
module;
struct innerStruct
{
field: vec3[f32]
@@ -27,9 +30,7 @@ external
}
)";
Nz::ShaderAst::StatementPtr shader = Nz::ShaderLang::Parse(nzslSource);
REQUIRE(shader->GetType() == Nz::ShaderAst::NodeType::MultiStatement);
Nz::ShaderAst::MultiStatement& multiStatement = static_cast<Nz::ShaderAst::MultiStatement&>(*shader);
Nz::ShaderAst::ModulePtr shaderModule = Nz::ShaderLang::Parse(nzslSource);
SECTION("Nested AccessMember")
{
@@ -40,16 +41,16 @@ external
auto swizzle = Nz::ShaderBuilder::Swizzle(std::move(secondAccess), { 2u });
auto varDecl = Nz::ShaderBuilder::DeclareVariable("result", Nz::ShaderAst::ExpressionType{ Nz::ShaderAst::PrimitiveType::Float32 }, std::move(swizzle));
multiStatement.statements.push_back(Nz::ShaderBuilder::DeclareFunction(Nz::ShaderStageType::Vertex, "main", std::move(varDecl)));
shaderModule->rootNode->statements.push_back(Nz::ShaderBuilder::DeclareFunction(Nz::ShaderStageType::Vertex, "main", std::move(varDecl)));
ExpectGLSL(*shader, R"(
ExpectGLSL(*shaderModule, R"(
void main()
{
float result = ubo.s.field.z;
}
)");
ExpectNZSL(*shader, R"(
ExpectNZSL(*shaderModule, R"(
[entry(vert)]
fn main()
{
@@ -57,7 +58,7 @@ fn main()
}
)");
ExpectSpirV(*shader, R"(
ExpectSPIRV(*shaderModule, R"(
OpFunction
OpLabel
OpVariable
@@ -77,16 +78,16 @@ OpFunctionEnd)");
auto swizzle = Nz::ShaderBuilder::Swizzle(std::move(access), { 2u });
auto varDecl = Nz::ShaderBuilder::DeclareVariable("result", Nz::ShaderAst::ExpressionType{ Nz::ShaderAst::PrimitiveType::Float32 }, std::move(swizzle));
multiStatement.statements.push_back(Nz::ShaderBuilder::DeclareFunction(Nz::ShaderStageType::Vertex, "main", std::move(varDecl)));
shaderModule->rootNode->statements.push_back(Nz::ShaderBuilder::DeclareFunction(Nz::ShaderStageType::Vertex, "main", std::move(varDecl)));
ExpectGLSL(*shader, R"(
ExpectGLSL(*shaderModule, R"(
void main()
{
float result = ubo.s.field.z;
}
)");
ExpectNZSL(*shader, R"(
ExpectNZSL(*shaderModule, R"(
[entry(vert)]
fn main()
{
@@ -94,7 +95,7 @@ fn main()
}
)");
ExpectSpirV(*shader, R"(
ExpectSPIRV(*shaderModule, R"(
OpFunction
OpLabel
OpVariable

View File

@@ -11,6 +11,9 @@ TEST_CASE("branching", "[Shader]")
WHEN("using a simple branch")
{
std::string_view nzslSource = R"(
[nzsl_version("1.0")]
module;
struct inputStruct
{
value: f32
@@ -32,9 +35,9 @@ fn main()
}
)";
Nz::ShaderAst::StatementPtr shader = Nz::ShaderLang::Parse(nzslSource);
Nz::ShaderAst::ModulePtr shaderModule = Nz::ShaderLang::Parse(nzslSource);
ExpectGLSL(*shader, R"(
ExpectGLSL(*shaderModule, R"(
void main()
{
float value;
@@ -50,7 +53,7 @@ void main()
}
)");
ExpectNZSL(*shader, R"(
ExpectNZSL(*shaderModule, R"(
[entry(frag)]
fn main()
{
@@ -67,7 +70,7 @@ fn main()
}
)");
ExpectSpirV(*shader, R"(
ExpectSPIRV(*shaderModule, R"(
OpFunction
OpLabel
OpVariable
@@ -90,6 +93,9 @@ OpFunctionEnd)");
WHEN("discarding in a branch")
{
std::string_view nzslSource = R"(
[nzsl_version("1.0")]
module;
struct inputStruct
{
value: f32
@@ -108,9 +114,9 @@ fn main()
}
)";
Nz::ShaderAst::StatementPtr shader = Nz::ShaderLang::Parse(nzslSource);
Nz::ShaderAst::ModulePtr shaderModule = Nz::ShaderLang::Parse(nzslSource);
ExpectGLSL(*shader, R"(
ExpectGLSL(*shaderModule, R"(
void main()
{
if (data.value > (0.000000))
@@ -121,7 +127,7 @@ void main()
}
)");
ExpectNZSL(*shader, R"(
ExpectNZSL(*shaderModule, R"(
[entry(frag)]
fn main()
{
@@ -133,7 +139,7 @@ fn main()
}
)");
ExpectSpirV(*shader, R"(
ExpectSPIRV(*shaderModule, R"(
OpFunction
OpLabel
OpAccessChain
@@ -154,6 +160,9 @@ OpFunctionEnd)");
WHEN("using a complex branch")
{
std::string_view nzslSource = R"(
[nzsl_version("1.0")]
module;
struct inputStruct
{
value: f32
@@ -179,9 +188,9 @@ fn main()
}
)";
Nz::ShaderAst::StatementPtr shader = Nz::ShaderLang::Parse(nzslSource);
Nz::ShaderAst::ModulePtr shaderModule = Nz::ShaderLang::Parse(nzslSource);
ExpectGLSL(*shader, R"(
ExpectGLSL(*shaderModule, R"(
void main()
{
float value;
@@ -205,7 +214,7 @@ void main()
}
)");
ExpectNZSL(*shader, R"(
ExpectNZSL(*shaderModule, R"(
[entry(frag)]
fn main()
{
@@ -230,7 +239,7 @@ fn main()
}
)");
ExpectSpirV(*shader, R"(
ExpectSPIRV(*shaderModule, R"(
OpFunction
OpLabel
OpVariable

View File

@@ -8,10 +8,10 @@
#include <catch2/catch.hpp>
#include <cctype>
void ExpectOutput(Nz::ShaderAst::Statement& shader, const Nz::ShaderAst::SanitizeVisitor::Options& options, std::string_view expectedOptimizedResult)
void ExpectOutput(Nz::ShaderAst::Module& shaderModule, const Nz::ShaderAst::SanitizeVisitor::Options& options, std::string_view expectedOptimizedResult)
{
Nz::ShaderAst::StatementPtr sanitizedShader;
REQUIRE_NOTHROW(sanitizedShader = Nz::ShaderAst::Sanitize(shader, options));
Nz::ShaderAst::ModulePtr sanitizedShader;
REQUIRE_NOTHROW(sanitizedShader = Nz::ShaderAst::Sanitize(shaderModule, options));
ExpectNZSL(*sanitizedShader, expectedOptimizedResult);
}
@@ -21,6 +21,9 @@ TEST_CASE("const", "[Shader]")
WHEN("using const if")
{
std::string_view sourceCode = R"(
[nzsl_version("1.0")]
module;
option UseInt: bool = false;
[cond(UseInt)]
@@ -56,8 +59,8 @@ fn main()
}
)";
Nz::ShaderAst::StatementPtr shader;
REQUIRE_NOTHROW(shader = Nz::ShaderLang::Parse(sourceCode));
Nz::ShaderAst::ModulePtr shaderModule;
REQUIRE_NOTHROW(shaderModule = Nz::ShaderLang::Parse(sourceCode));
Nz::ShaderAst::SanitizeVisitor::Options options;
@@ -65,7 +68,7 @@ fn main()
{
options.optionValues[0] = true;
ExpectOutput(*shader, options, R"(
ExpectOutput(*shaderModule, options, R"(
struct inputStruct
{
value: i32
@@ -89,7 +92,7 @@ fn main()
{
options.optionValues[0] = false;
ExpectOutput(*shader, options, R"(
ExpectOutput(*shaderModule, options, R"(
struct inputStruct
{
value: f32
@@ -113,6 +116,9 @@ fn main()
WHEN("using [unroll] attribute on numerical for")
{
std::string_view sourceCode = R"(
[nzsl_version("1.0")]
module;
const LightCount = 3;
[layout(std140)]
@@ -145,10 +151,10 @@ fn main()
}
)";
Nz::ShaderAst::StatementPtr shader;
REQUIRE_NOTHROW(shader = Nz::ShaderLang::Parse(sourceCode));
Nz::ShaderAst::ModulePtr shaderModule;
REQUIRE_NOTHROW(shaderModule = Nz::ShaderLang::Parse(sourceCode));
ExpectOutput(*shader, {}, R"(
ExpectOutput(*shaderModule, {}, R"(
[entry(frag)]
fn main()
{
@@ -170,6 +176,9 @@ fn main()
WHEN("using [unroll] attribute on for-each")
{
std::string_view sourceCode = R"(
[nzsl_version("1.0")]
module;
const LightCount = 3;
[layout(std140)]
@@ -202,10 +211,10 @@ fn main()
}
)";
Nz::ShaderAst::StatementPtr shader;
REQUIRE_NOTHROW(shader = Nz::ShaderLang::Parse(sourceCode));
Nz::ShaderAst::ModulePtr shaderModule;
REQUIRE_NOTHROW(shaderModule = Nz::ShaderLang::Parse(sourceCode));
ExpectOutput(*shader, {}, R"(
ExpectOutput(*shaderModule, {}, R"(
[entry(frag)]
fn main()
{

View File

@@ -11,6 +11,9 @@ TEST_CASE("loops", "[Shader]")
WHEN("using a while")
{
std::string_view nzslSource = R"(
[nzsl_version("1.0")]
module;
struct inputStruct
{
value: f32
@@ -34,9 +37,9 @@ fn main()
}
)";
Nz::ShaderAst::StatementPtr shader = Nz::ShaderLang::Parse(nzslSource);
Nz::ShaderAst::ModulePtr shaderModule = Nz::ShaderLang::Parse(nzslSource);
ExpectGLSL(*shader, R"(
ExpectGLSL(*shaderModule, R"(
void main()
{
float value = 0.000000;
@@ -50,7 +53,7 @@ void main()
}
)");
ExpectNZSL(*shader, R"(
ExpectNZSL(*shaderModule, R"(
[entry(frag)]
fn main()
{
@@ -65,7 +68,7 @@ fn main()
}
)");
ExpectSpirV(*shader, R"(
ExpectSPIRV(*shaderModule, R"(
OpFunction
OpLabel
OpVariable
@@ -94,6 +97,9 @@ OpFunctionEnd)");
WHEN("using a for range")
{
std::string_view nzslSource = R"(
[nzsl_version("1.0")]
module;
[entry(frag)]
fn main()
{
@@ -105,10 +111,10 @@ fn main()
}
)";
Nz::ShaderAst::StatementPtr shader = Nz::ShaderLang::Parse(nzslSource);
Nz::ShaderAst::ModulePtr shaderModule = Nz::ShaderLang::Parse(nzslSource);
ExpectGLSL(*shader, R"(
ExpectGLSL(*shaderModule, R"(
void main()
{
int x = 0;
@@ -123,7 +129,7 @@ void main()
}
)");
ExpectNZSL(*shader, R"(
ExpectNZSL(*shaderModule, R"(
[entry(frag)]
fn main()
{
@@ -136,7 +142,7 @@ fn main()
}
)");
ExpectSpirV(*shader, R"(
ExpectSPIRV(*shaderModule, R"(
OpFunction
OpLabel
OpVariable
@@ -169,6 +175,9 @@ OpFunctionEnd)");
WHEN("using a for range with step")
{
std::string_view nzslSource = R"(
[nzsl_version("1.0")]
module;
[entry(frag)]
fn main()
{
@@ -180,10 +189,10 @@ fn main()
}
)";
Nz::ShaderAst::StatementPtr shader = Nz::ShaderLang::Parse(nzslSource);
Nz::ShaderAst::ModulePtr shaderModule = Nz::ShaderLang::Parse(nzslSource);
ExpectGLSL(*shader, R"(
ExpectGLSL(*shaderModule, R"(
void main()
{
int x = 0;
@@ -199,7 +208,7 @@ void main()
}
)");
ExpectNZSL(*shader, R"(
ExpectNZSL(*shaderModule, R"(
[entry(frag)]
fn main()
{
@@ -212,7 +221,7 @@ fn main()
}
)");
ExpectSpirV(*shader, R"(
ExpectSPIRV(*shaderModule, R"(
OpFunction
OpLabel
OpVariable
@@ -248,6 +257,9 @@ OpFunctionEnd)");
WHEN("using a for-each")
{
std::string_view nzslSource = R"(
[nzsl_version("1.0")]
module;
struct inputStruct
{
value: array[f32, 10]
@@ -269,10 +281,10 @@ fn main()
}
)";
Nz::ShaderAst::StatementPtr shader = Nz::ShaderLang::Parse(nzslSource);
Nz::ShaderAst::ModulePtr shaderModule = Nz::ShaderLang::Parse(nzslSource);
ExpectGLSL(*shader, R"(
ExpectGLSL(*shaderModule, R"(
void main()
{
float x = 0.000000;
@@ -287,7 +299,7 @@ void main()
}
)");
ExpectNZSL(*shader, R"(
ExpectNZSL(*shaderModule, R"(
[entry(frag)]
fn main()
{
@@ -300,7 +312,7 @@ fn main()
}
)");
ExpectSpirV(*shader, R"(
ExpectSPIRV(*shaderModule, R"(
OpFunction
OpLabel
OpVariable

View File

@@ -9,24 +9,33 @@
#include <catch2/catch.hpp>
#include <cctype>
template<typename T, typename U>
std::unique_ptr<T> static_unique_pointer_cast(std::unique_ptr<U>&& ptr)
{
return std::unique_ptr<T>(Nz::SafeCast<T*>(ptr.release()));
}
void PropagateConstantAndExpect(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::PropagateConstants(*shader));
Nz::ShaderAst::ModulePtr shaderModule;
REQUIRE_NOTHROW(shaderModule = Nz::ShaderLang::Parse(sourceCode));
REQUIRE_NOTHROW(shaderModule = Nz::ShaderAst::Sanitize(*shaderModule));
REQUIRE_NOTHROW(shaderModule = Nz::ShaderAst::PropagateConstants(*shaderModule));
ExpectNZSL(*shader, expectedOptimizedResult);
ExpectNZSL(*shaderModule, expectedOptimizedResult);
}
void EliminateUnusedAndExpect(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::EliminateUnusedPass(*shader));
Nz::ShaderAst::DependencyCheckerVisitor::Config depConfig;
depConfig.usedShaderStages = Nz::ShaderStageType_All;
ExpectNZSL(*shader, expectedOptimizedResult);
Nz::ShaderAst::ModulePtr shaderModule;
REQUIRE_NOTHROW(shaderModule = Nz::ShaderLang::Parse(sourceCode));
REQUIRE_NOTHROW(shaderModule = Nz::ShaderAst::Sanitize(*shaderModule));
REQUIRE_NOTHROW(shaderModule = Nz::ShaderAst::EliminateUnusedPass(*shaderModule, depConfig));
ExpectNZSL(*shaderModule, expectedOptimizedResult);
}
TEST_CASE("optimizations", "[Shader]")
@@ -34,6 +43,9 @@ TEST_CASE("optimizations", "[Shader]")
WHEN("propagating constants")
{
PropagateConstantAndExpect(R"(
[nzsl_version("1.0")]
module;
[entry(frag)]
fn main()
{
@@ -51,6 +63,9 @@ fn main()
WHEN("propagating vector constants")
{
PropagateConstantAndExpect(R"(
[nzsl_version("1.0")]
module;
[entry(frag)]
fn main()
{
@@ -67,6 +82,9 @@ fn main()
WHEN("eliminating simple branch")
{
PropagateConstantAndExpect(R"(
[nzsl_version("1.0")]
module;
[entry(frag)]
fn main()
{
@@ -85,6 +103,9 @@ fn main()
WHEN("eliminating multiple branches")
{
PropagateConstantAndExpect(R"(
[nzsl_version("1.0")]
module;
[entry(frag)]
fn main()
{
@@ -116,6 +137,9 @@ fn main()
WHEN("eliminating multiple split branches")
{
PropagateConstantAndExpect(R"(
[nzsl_version("1.0")]
module;
[entry(frag)]
fn main()
{
@@ -158,6 +182,9 @@ fn main()
WHEN("optimizing out scalar swizzle")
{
PropagateConstantAndExpect(R"(
[nzsl_version("1.0")]
module;
[entry(frag)]
fn main()
{
@@ -175,6 +202,9 @@ fn main()
WHEN("optimizing out scalar swizzle to vector")
{
PropagateConstantAndExpect(R"(
[nzsl_version("1.0")]
module;
[entry(frag)]
fn main()
{
@@ -192,6 +222,9 @@ fn main()
WHEN("optimizing out vector swizzle")
{
PropagateConstantAndExpect(R"(
[nzsl_version("1.0")]
module;
[entry(frag)]
fn main()
{
@@ -209,6 +242,9 @@ fn main()
WHEN("optimizing out vector swizzle with repetition")
{
PropagateConstantAndExpect(R"(
[nzsl_version("1.0")]
module;
[entry(frag)]
fn main()
{
@@ -226,6 +262,9 @@ fn main()
WHEN("optimizing out complex swizzle")
{
PropagateConstantAndExpect(R"(
[nzsl_version("1.0")]
module;
[entry(frag)]
fn main()
{
@@ -243,6 +282,9 @@ fn main()
WHEN("optimizing out complex swizzle on unknown value")
{
PropagateConstantAndExpect(R"(
[nzsl_version("1.0")]
module;
struct inputStruct
{
value: vec4[f32]
@@ -270,6 +312,9 @@ fn main()
WHEN("eliminating unused code")
{
EliminateUnusedAndExpect(R"(
[nzsl_version("1.0")]
module;
struct inputStruct
{
value: vec4[f32]
@@ -305,6 +350,9 @@ fn main() -> Output
output.value = data.value;
return output;
})", R"(
[nzsl_version("1.0")]
module;
struct inputStruct
{
value: vec4[f32]

View File

@@ -12,6 +12,9 @@ TEST_CASE("sanitizing", "[Shader]")
WHEN("splitting branches")
{
std::string_view nzslSource = R"(
[nzsl_version("1.0")]
module;
struct inputStruct
{
value: f32
@@ -37,14 +40,14 @@ fn main()
}
)";
Nz::ShaderAst::StatementPtr shader = Nz::ShaderLang::Parse(nzslSource);
Nz::ShaderAst::ModulePtr shaderModule = Nz::ShaderLang::Parse(nzslSource);
Nz::ShaderAst::SanitizeVisitor::Options options;
options.splitMultipleBranches = true;
REQUIRE_NOTHROW(shader = Nz::ShaderAst::Sanitize(*shader, options));
REQUIRE_NOTHROW(shaderModule = Nz::ShaderAst::Sanitize(*shaderModule, options));
ExpectNZSL(*shader, R"(
ExpectNZSL(*shaderModule, R"(
[entry(frag)]
fn main()
{
@@ -82,6 +85,9 @@ fn main()
WHEN("reducing for-each to while")
{
std::string_view nzslSource = R"(
[nzsl_version("1.0")]
module;
struct inputStruct
{
value: array[f32, 10]
@@ -103,14 +109,14 @@ fn main()
}
)";
Nz::ShaderAst::StatementPtr shader = Nz::ShaderLang::Parse(nzslSource);
Nz::ShaderAst::ModulePtr shaderModule = Nz::ShaderLang::Parse(nzslSource);
Nz::ShaderAst::SanitizeVisitor::Options options;
options.reduceLoopsToWhile = true;
REQUIRE_NOTHROW(shader = Nz::ShaderAst::Sanitize(*shader, options));
REQUIRE_NOTHROW(shaderModule = Nz::ShaderAst::Sanitize(*shaderModule, options));
ExpectNZSL(*shader, R"(
ExpectNZSL(*shaderModule, R"(
[entry(frag)]
fn main()
{
@@ -131,6 +137,9 @@ fn main()
WHEN("removing matrix casts")
{
std::string_view nzslSource = R"(
[nzsl_version("1.0")]
module;
fn testMat2ToMat2(input: mat2[f32]) -> mat2[f32]
{
return mat2[f32](input);
@@ -177,14 +186,14 @@ fn testMat4ToMat4(input: mat4[f32]) -> mat4[f32]
}
)";
Nz::ShaderAst::StatementPtr shader = Nz::ShaderLang::Parse(nzslSource);
Nz::ShaderAst::ModulePtr shaderModule = Nz::ShaderLang::Parse(nzslSource);
Nz::ShaderAst::SanitizeVisitor::Options options;
options.removeMatrixCast = true;
REQUIRE_NOTHROW(shader = Nz::ShaderAst::Sanitize(*shader, options));
REQUIRE_NOTHROW(shaderModule = Nz::ShaderAst::Sanitize(*shaderModule, options));
ExpectNZSL(*shader, R"(
ExpectNZSL(*shaderModule, R"(
fn testMat2ToMat2(input: mat2[f32]) -> mat2[f32]
{
return input;

View File

@@ -121,7 +121,7 @@ namespace
};
}
void ExpectGLSL(Nz::ShaderAst::Statement& shader, std::string_view expectedOutput)
void ExpectGLSL(Nz::ShaderAst::Module& shader, std::string_view expectedOutput)
{
expectedOutput = Nz::Trim(expectedOutput);
@@ -140,7 +140,7 @@ void ExpectGLSL(Nz::ShaderAst::Statement& shader, std::string_view expectedOutpu
};
Nz::ShaderAst::AstReflect reflectVisitor;
reflectVisitor.Reflect(shader, callbacks);
reflectVisitor.Reflect(*shader.rootNode, callbacks);
INFO("no entry point found");
REQUIRE(entryShaderStage.has_value());
@@ -186,7 +186,7 @@ void ExpectGLSL(Nz::ShaderAst::Statement& shader, std::string_view expectedOutpu
}
}
void ExpectNZSL(Nz::ShaderAst::Statement& shader, std::string_view expectedOutput)
void ExpectNZSL(Nz::ShaderAst::Module& shader, std::string_view expectedOutput)
{
expectedOutput = Nz::Trim(expectedOutput);
@@ -209,7 +209,7 @@ void ExpectNZSL(Nz::ShaderAst::Statement& shader, std::string_view expectedOutpu
}
}
void ExpectSpirV(Nz::ShaderAst::Statement& shader, std::string_view expectedOutput, bool outputParameter)
void ExpectSPIRV(Nz::ShaderAst::Module& shader, std::string_view expectedOutput, bool outputParameter)
{
expectedOutput = Nz::Trim(expectedOutput);

View File

@@ -3,11 +3,11 @@
#ifndef NAZARA_UNITTESTS_SHADER_SHADERUTILS_HPP
#define NAZARA_UNITTESTS_SHADER_SHADERUTILS_HPP
#include <Nazara/Shader/Ast/Nodes.hpp>
#include <Nazara/Shader/Ast/Module.hpp>
#include <string>
void ExpectGLSL(Nz::ShaderAst::Statement& shader, std::string_view expectedOutput);
void ExpectNZSL(Nz::ShaderAst::Statement& shader, std::string_view expectedOutput);
void ExpectSpirV(Nz::ShaderAst::Statement& shader, std::string_view expectedOutput, bool outputParameter = false);
void ExpectGLSL(Nz::ShaderAst::Module& shader, std::string_view expectedOutput);
void ExpectNZSL(Nz::ShaderAst::Module& shader, std::string_view expectedOutput);
void ExpectSPIRV(Nz::ShaderAst::Module& shader, std::string_view expectedOutput, bool outputParameter = false);
#endif

View File

@@ -13,6 +13,9 @@ TEST_CASE("swizzle", "[Shader]")
WHEN("reading")
{
std::string_view nzslSource = R"(
[nzsl_version("1.0")]
module;
[entry(frag)]
fn main()
{
@@ -21,9 +24,9 @@ fn main()
}
)";
Nz::ShaderAst::StatementPtr shader = Nz::ShaderLang::Parse(nzslSource);
Nz::ShaderAst::ModulePtr shaderModule = Nz::ShaderLang::Parse(nzslSource);
ExpectGLSL(*shader, R"(
ExpectGLSL(*shaderModule, R"(
void main()
{
vec4 vec = vec4(0.000000, 1.000000, 2.000000, 3.000000);
@@ -31,7 +34,7 @@ void main()
}
)");
ExpectNZSL(*shader, R"(
ExpectNZSL(*shaderModule, R"(
[entry(frag)]
fn main()
{
@@ -40,7 +43,7 @@ fn main()
}
)");
ExpectSpirV(*shader, R"(
ExpectSPIRV(*shaderModule, R"(
OpFunction
OpLabel
OpVariable
@@ -57,6 +60,9 @@ OpFunctionEnd)");
WHEN("writing")
{
std::string_view nzslSource = R"(
[nzsl_version("1.0")]
module;
[entry(frag)]
fn main()
{
@@ -65,9 +71,9 @@ fn main()
}
)";
Nz::ShaderAst::StatementPtr shader = Nz::ShaderLang::Parse(nzslSource);
Nz::ShaderAst::ModulePtr shaderModule = Nz::ShaderLang::Parse(nzslSource);
ExpectGLSL(*shader, R"(
ExpectGLSL(*shaderModule, R"(
void main()
{
vec4 vec = vec4(0.000000, 0.000000, 0.000000, 0.000000);
@@ -75,7 +81,7 @@ void main()
}
)");
ExpectNZSL(*shader, R"(
ExpectNZSL(*shaderModule, R"(
[entry(frag)]
fn main()
{
@@ -84,7 +90,7 @@ fn main()
}
)");
ExpectSpirV(*shader, R"(
ExpectSPIRV(*shaderModule, R"(
OpFunction
OpLabel
OpVariable
@@ -104,6 +110,9 @@ OpFunctionEnd)");
GIVEN("a variable")
{
std::string_view nzslSource = R"(
[nzsl_version("1.0")]
module;
[entry(frag)]
fn main()
{
@@ -112,9 +121,9 @@ fn main()
}
)";
Nz::ShaderAst::StatementPtr shader = Nz::ShaderLang::Parse(nzslSource);
Nz::ShaderAst::ModulePtr shaderModule = Nz::ShaderLang::Parse(nzslSource);
ExpectGLSL(*shader, R"(
ExpectGLSL(*shaderModule, R"(
void main()
{
float value = 42.000000;
@@ -122,7 +131,7 @@ void main()
}
)");
ExpectNZSL(*shader, R"(
ExpectNZSL(*shaderModule, R"(
[entry(frag)]
fn main()
{
@@ -131,7 +140,7 @@ fn main()
}
)");
ExpectSpirV(*shader, R"(
ExpectSPIRV(*shaderModule, R"(
OpFunction
OpLabel
OpVariable
@@ -147,6 +156,9 @@ OpFunctionEnd)");
GIVEN("a function value")
{
std::string_view nzslSource = R"(
[nzsl_version("1.0")]
module;
[entry(frag)]
fn main()
{
@@ -155,9 +167,9 @@ fn main()
}
)";
Nz::ShaderAst::StatementPtr shader = Nz::ShaderLang::Parse(nzslSource);
Nz::ShaderAst::ModulePtr shaderModule = Nz::ShaderLang::Parse(nzslSource);
ExpectGLSL(*shader, R"(
ExpectGLSL(*shaderModule, R"(
void main()
{
float cachedResult = max(2.000000, 1.000000);
@@ -167,7 +179,7 @@ void main()
}
)");
ExpectNZSL(*shader, R"(
ExpectNZSL(*shaderModule, R"(
[entry(frag)]
fn main()
{
@@ -176,7 +188,7 @@ fn main()
}
)");
ExpectSpirV(*shader, R"(
ExpectSPIRV(*shaderModule, R"(
OpFunction
OpLabel
OpVariable
@@ -197,6 +209,9 @@ OpFunctionEnd)");
WHEN("reading")
{
std::string_view nzslSource = R"(
[nzsl_version("1.0")]
module;
[entry(frag)]
fn main()
{
@@ -205,9 +220,9 @@ fn main()
}
)";
Nz::ShaderAst::StatementPtr shader = Nz::ShaderLang::Parse(nzslSource);
Nz::ShaderAst::ModulePtr shaderModule = Nz::ShaderLang::Parse(nzslSource);
ExpectGLSL(*shader, R"(
ExpectGLSL(*shaderModule, R"(
void main()
{
vec4 vec = vec4(0.000000, 1.000000, 2.000000, 3.000000);
@@ -215,7 +230,7 @@ void main()
}
)");
ExpectNZSL(*shader, R"(
ExpectNZSL(*shaderModule, R"(
[entry(frag)]
fn main()
{
@@ -224,7 +239,7 @@ fn main()
}
)");
ExpectSpirV(*shader, R"(
ExpectSPIRV(*shaderModule, R"(
OpFunction
OpLabel
OpVariable
@@ -244,6 +259,9 @@ OpFunctionEnd)");
WHEN("writing")
{
std::string_view nzslSource = R"(
[nzsl_version("1.0")]
module;
[entry(frag)]
fn main()
{
@@ -253,9 +271,9 @@ fn main()
}
)";
Nz::ShaderAst::StatementPtr shader = Nz::ShaderLang::Parse(nzslSource);
Nz::ShaderAst::ModulePtr shaderModule = Nz::ShaderLang::Parse(nzslSource);
ExpectGLSL(*shader, R"(
ExpectGLSL(*shaderModule, R"(
void main()
{
vec4 vec = vec4(0.000000, 1.000000, 2.000000, 3.000000);
@@ -264,7 +282,7 @@ void main()
}
)");
ExpectNZSL(*shader, R"(
ExpectNZSL(*shaderModule, R"(
[entry(frag)]
fn main()
{
@@ -274,7 +292,7 @@ fn main()
}
)");
ExpectSpirV(*shader, R"(
ExpectSPIRV(*shaderModule, R"(
OpFunction
OpLabel
OpVariable