Shader: Fix function calls with OpenGL

This commit is contained in:
Jérôme Leclercq 2022-03-13 15:07:56 +01:00
parent e40e8eb204
commit e9543b20a2
5 changed files with 104 additions and 1 deletions

View File

@ -102,6 +102,7 @@ namespace Nz
void Visit(ShaderAst::CallFunctionExpression& node) override;
void Visit(ShaderAst::CastExpression& node) override;
void Visit(ShaderAst::ConstantValueExpression& node) override;
void Visit(ShaderAst::FunctionExpression& node) override;
void Visit(ShaderAst::IntrinsicExpression& node) override;
void Visit(ShaderAst::SwizzleExpression& node) override;
void Visit(ShaderAst::VariableValueExpression& node) override;

View File

@ -113,6 +113,7 @@ namespace Nz
void Visit(ShaderAst::ConditionalExpression& node) override;
void Visit(ShaderAst::ConstantValueExpression& node) override;
void Visit(ShaderAst::ConstantExpression& node) override;
void Visit(ShaderAst::FunctionExpression& node) override;
void Visit(ShaderAst::IdentifierExpression& node) override;
void Visit(ShaderAst::IntrinsicExpression& node) override;
void Visit(ShaderAst::StructTypeExpression& node) override;

View File

@ -967,7 +967,12 @@ namespace Nz
static_assert(AlwaysFalse<T>::value, "non-exhaustive visitor");
}, 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)
{

View File

@ -969,6 +969,11 @@ namespace Nz
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)
{
Append(node.identifier);

View 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)");
}
}