Shader: Add proper support for alias
This commit is contained in:
97
tests/Engine/Shader/Alias.cpp
Normal file
97
tests/Engine/Shader/Alias.cpp
Normal file
@@ -0,0 +1,97 @@
|
||||
#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("aliases", "[Shader]")
|
||||
{
|
||||
SECTION("Alias of structs")
|
||||
{
|
||||
std::string_view nzslSource = R"(
|
||||
[nzsl_version("1.0")]
|
||||
module;
|
||||
|
||||
struct Data
|
||||
{
|
||||
value: f32
|
||||
}
|
||||
|
||||
alias ExtData = Data;
|
||||
|
||||
external
|
||||
{
|
||||
[binding(0)] extData: uniform[ExtData]
|
||||
}
|
||||
|
||||
struct Input
|
||||
{
|
||||
value: f32
|
||||
}
|
||||
|
||||
alias In = Input;
|
||||
|
||||
struct Output
|
||||
{
|
||||
[location(0)] value: f32
|
||||
}
|
||||
|
||||
alias Out = Output;
|
||||
alias FragOut = Out;
|
||||
|
||||
[entry(frag)]
|
||||
fn main(input: In) -> FragOut
|
||||
{
|
||||
let output: Out;
|
||||
output.value = extData.value * input.value;
|
||||
return output;
|
||||
}
|
||||
)";
|
||||
|
||||
Nz::ShaderAst::ModulePtr shaderModule = Nz::ShaderLang::Parse(nzslSource);
|
||||
|
||||
ExpectGLSL(*shaderModule, R"(
|
||||
void main()
|
||||
{
|
||||
Input input_;
|
||||
input_.value = _NzIn_value;
|
||||
|
||||
Output output_;
|
||||
output_.value = extData.value * input_.value;
|
||||
|
||||
_NzOut_value = output_.value;
|
||||
return;
|
||||
}
|
||||
)");
|
||||
|
||||
ExpectNZSL(*shaderModule, R"(
|
||||
[entry(frag)]
|
||||
fn main(input: In) -> FragOut
|
||||
{
|
||||
let output: Out;
|
||||
output.value = extData.value * input.value;
|
||||
return output;
|
||||
}
|
||||
)");
|
||||
|
||||
ExpectSPIRV(*shaderModule, R"(
|
||||
OpFunction
|
||||
OpLabel
|
||||
OpVariable
|
||||
OpVariable
|
||||
OpAccessChain
|
||||
OpLoad
|
||||
OpAccessChain
|
||||
OpLoad
|
||||
OpFMul
|
||||
OpAccessChain
|
||||
OpStore
|
||||
OpLoad
|
||||
OpCompositeExtract
|
||||
OpStore
|
||||
OpReturn
|
||||
OpFunctionEnd)");
|
||||
}
|
||||
}
|
||||
@@ -262,6 +262,47 @@ fn testMat4ToMat4(input: mat4[f32]) -> mat4[f32]
|
||||
{
|
||||
return input;
|
||||
}
|
||||
)");
|
||||
|
||||
}
|
||||
|
||||
WHEN("removing aliases")
|
||||
{
|
||||
std::string_view nzslSource = R"(
|
||||
[nzsl_version("1.0")]
|
||||
module;
|
||||
|
||||
struct inputStruct
|
||||
{
|
||||
value: f32
|
||||
}
|
||||
|
||||
alias Input = inputStruct;
|
||||
alias In = Input;
|
||||
|
||||
external
|
||||
{
|
||||
[set(0), binding(0)] data: uniform[In]
|
||||
}
|
||||
)";
|
||||
|
||||
Nz::ShaderAst::ModulePtr shaderModule = Nz::ShaderLang::Parse(nzslSource);
|
||||
|
||||
Nz::ShaderAst::SanitizeVisitor::Options options;
|
||||
options.removeAliases = true;
|
||||
|
||||
REQUIRE_NOTHROW(shaderModule = Nz::ShaderAst::Sanitize(*shaderModule, options));
|
||||
|
||||
ExpectNZSL(*shaderModule, R"(
|
||||
struct inputStruct
|
||||
{
|
||||
value: f32
|
||||
}
|
||||
|
||||
external
|
||||
{
|
||||
[set(0), binding(0)] data: uniform[inputStruct]
|
||||
}
|
||||
)");
|
||||
|
||||
}
|
||||
|
||||
@@ -142,8 +142,10 @@ void ExpectGLSL(Nz::ShaderAst::Module& shader, std::string_view expectedOutput)
|
||||
Nz::ShaderAst::AstReflect reflectVisitor;
|
||||
reflectVisitor.Reflect(*shader.rootNode, callbacks);
|
||||
|
||||
INFO("no entry point found");
|
||||
REQUIRE(entryShaderStage.has_value());
|
||||
{
|
||||
INFO("no entry point found");
|
||||
REQUIRE(entryShaderStage.has_value());
|
||||
}
|
||||
|
||||
Nz::GlslWriter writer;
|
||||
std::string output = writer.Generate(entryShaderStage, shader);
|
||||
|
||||
Reference in New Issue
Block a user