Shader: Fix function calls with OpenGL
This commit is contained in:
@@ -102,6 +102,7 @@ namespace Nz
|
|||||||
void Visit(ShaderAst::CallFunctionExpression& node) override;
|
void Visit(ShaderAst::CallFunctionExpression& node) override;
|
||||||
void Visit(ShaderAst::CastExpression& node) override;
|
void Visit(ShaderAst::CastExpression& node) override;
|
||||||
void Visit(ShaderAst::ConstantValueExpression& node) override;
|
void Visit(ShaderAst::ConstantValueExpression& node) override;
|
||||||
|
void Visit(ShaderAst::FunctionExpression& node) override;
|
||||||
void Visit(ShaderAst::IntrinsicExpression& node) override;
|
void Visit(ShaderAst::IntrinsicExpression& node) override;
|
||||||
void Visit(ShaderAst::SwizzleExpression& node) override;
|
void Visit(ShaderAst::SwizzleExpression& node) override;
|
||||||
void Visit(ShaderAst::VariableValueExpression& node) override;
|
void Visit(ShaderAst::VariableValueExpression& node) override;
|
||||||
|
|||||||
@@ -113,6 +113,7 @@ namespace Nz
|
|||||||
void Visit(ShaderAst::ConditionalExpression& node) override;
|
void Visit(ShaderAst::ConditionalExpression& node) override;
|
||||||
void Visit(ShaderAst::ConstantValueExpression& node) override;
|
void Visit(ShaderAst::ConstantValueExpression& node) override;
|
||||||
void Visit(ShaderAst::ConstantExpression& node) override;
|
void Visit(ShaderAst::ConstantExpression& node) override;
|
||||||
|
void Visit(ShaderAst::FunctionExpression& node) override;
|
||||||
void Visit(ShaderAst::IdentifierExpression& node) override;
|
void Visit(ShaderAst::IdentifierExpression& node) override;
|
||||||
void Visit(ShaderAst::IntrinsicExpression& node) override;
|
void Visit(ShaderAst::IntrinsicExpression& node) override;
|
||||||
void Visit(ShaderAst::StructTypeExpression& node) override;
|
void Visit(ShaderAst::StructTypeExpression& node) override;
|
||||||
|
|||||||
@@ -967,7 +967,12 @@ namespace Nz
|
|||||||
static_assert(AlwaysFalse<T>::value, "non-exhaustive visitor");
|
static_assert(AlwaysFalse<T>::value, "non-exhaustive visitor");
|
||||||
}, node.value);
|
}, node.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void GlslWriter::Visit(ShaderAst::FunctionExpression& node)
|
||||||
|
{
|
||||||
|
const std::string& targetName = Retrieve(m_currentState->previsitor.functions, node.funcId).name;
|
||||||
|
Append(targetName);
|
||||||
|
}
|
||||||
|
|
||||||
void GlslWriter::Visit(ShaderAst::IntrinsicExpression& node)
|
void GlslWriter::Visit(ShaderAst::IntrinsicExpression& node)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -969,6 +969,11 @@ namespace Nz
|
|||||||
AppendIdentifier(m_currentState->constants, node.constantId);
|
AppendIdentifier(m_currentState->constants, node.constantId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void LangWriter::Visit(ShaderAst::FunctionExpression& node)
|
||||||
|
{
|
||||||
|
Append(Retrieve(m_currentState->functions, node.funcId).name);
|
||||||
|
}
|
||||||
|
|
||||||
void LangWriter::Visit(ShaderAst::IdentifierExpression& node)
|
void LangWriter::Visit(ShaderAst::IdentifierExpression& node)
|
||||||
{
|
{
|
||||||
Append(node.identifier);
|
Append(node.identifier);
|
||||||
|
|||||||
91
tests/Engine/Shader/FunctionsTests.cpp
Normal file
91
tests/Engine/Shader/FunctionsTests.cpp
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
#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("functions", "[Shader]")
|
||||||
|
{
|
||||||
|
SECTION("Simple function call")
|
||||||
|
{
|
||||||
|
std::string_view nzslSource = R"(
|
||||||
|
[nzsl_version("1.0")]
|
||||||
|
module;
|
||||||
|
|
||||||
|
struct FragOut
|
||||||
|
{
|
||||||
|
[location(0)] value: f32
|
||||||
|
}
|
||||||
|
|
||||||
|
fn GetValue() -> f32
|
||||||
|
{
|
||||||
|
return 42.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
[entry(frag)]
|
||||||
|
fn main() -> FragOut
|
||||||
|
{
|
||||||
|
let output: FragOut;
|
||||||
|
output.value = GetValue();
|
||||||
|
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
)";
|
||||||
|
|
||||||
|
Nz::ShaderAst::ModulePtr shaderModule = Nz::ShaderLang::Parse(nzslSource);
|
||||||
|
shaderModule = SanitizeModule(*shaderModule);
|
||||||
|
|
||||||
|
ExpectGLSL(*shaderModule, R"(
|
||||||
|
float GetValue()
|
||||||
|
{
|
||||||
|
return 42.000000;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*************** Outputs ***************/
|
||||||
|
layout(location = 0) out float _NzOut_value;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
FragOut output_;
|
||||||
|
output_.value = GetValue();
|
||||||
|
|
||||||
|
_NzOut_value = output_.value;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
)");
|
||||||
|
|
||||||
|
ExpectNZSL(*shaderModule, R"(
|
||||||
|
fn GetValue() -> f32
|
||||||
|
{
|
||||||
|
return 42.000000;
|
||||||
|
}
|
||||||
|
|
||||||
|
[entry(frag)]
|
||||||
|
fn main() -> FragOut
|
||||||
|
{
|
||||||
|
let output: FragOut;
|
||||||
|
output.value = GetValue();
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
)");
|
||||||
|
|
||||||
|
ExpectSPIRV(*shaderModule, R"(
|
||||||
|
OpFunction
|
||||||
|
OpLabel
|
||||||
|
OpReturnValue
|
||||||
|
OpFunctionEnd
|
||||||
|
OpFunction
|
||||||
|
OpLabel
|
||||||
|
OpVariable
|
||||||
|
OpFunctionCall
|
||||||
|
OpAccessChain
|
||||||
|
OpStore
|
||||||
|
OpLoad
|
||||||
|
OpCompositeExtract
|
||||||
|
OpStore
|
||||||
|
OpReturn
|
||||||
|
OpFunctionEnd)");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user