From a84391cf086e02937ba9fc314af107cd3900acbf Mon Sep 17 00:00:00 2001 From: Lynix Date: Thu, 5 Jan 2017 16:41:48 +0100 Subject: [PATCH] Renderer/ShaderAst: Add support for expression type --- include/Nazara/Renderer/ShaderAst.hpp | 20 ++++++++++----- include/Nazara/Renderer/ShaderAst.inl | 4 +++ src/Nazara/Renderer/ShaderAst.cpp | 37 +++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 6 deletions(-) diff --git a/include/Nazara/Renderer/ShaderAst.hpp b/include/Nazara/Renderer/ShaderAst.hpp index 87c00548e..d0d502959 100644 --- a/include/Nazara/Renderer/ShaderAst.hpp +++ b/include/Nazara/Renderer/ShaderAst.hpp @@ -43,13 +43,14 @@ namespace Nz enum class ExpressionType { - Float1, // float - Float2, // vec2 - Float3, // vec3 - Float4, // vec4 - Mat4x4, // mat4 + Boolean, // bool + Float1, // float + Float2, // vec2 + Float3, // vec3 + Float4, // vec4 + Mat4x4, // mat4 - None // void + None // void }; enum class VariableType @@ -91,6 +92,8 @@ namespace Nz class NAZARA_RENDERER_API Expression : public Node { + public: + virtual ExpressionType GetExpressionType() const = 0; }; class NAZARA_RENDERER_API ExpressionStatement : public Statement @@ -126,6 +129,8 @@ namespace Nz public: inline Variable(VariableType varKind, ExpressionType varType); + ExpressionType GetExpressionType() const override; + ExpressionType type; VariableType kind; }; @@ -163,6 +168,7 @@ namespace Nz public: inline AssignOp(AssignType Op, VariablePtr Var, ExpressionPtr Right); + ExpressionType GetExpressionType() const override; void Register(ShaderWriter& visitor) override; void Visit(ShaderWriter& visitor) override; @@ -176,6 +182,7 @@ namespace Nz public: inline BinaryOp(BinaryType Op, ExpressionPtr Left, ExpressionPtr Right); + ExpressionType GetExpressionType() const override; void Register(ShaderWriter& visitor) override; void Visit(ShaderWriter& visitor) override; @@ -207,6 +214,7 @@ namespace Nz public: inline explicit Constant(float value); + ExpressionType GetExpressionType() const override; void Register(ShaderWriter& visitor) override; void Visit(ShaderWriter& visitor) override; diff --git a/include/Nazara/Renderer/ShaderAst.inl b/include/Nazara/Renderer/ShaderAst.inl index b2b5a1f56..3fe505726 100644 --- a/include/Nazara/Renderer/ShaderAst.inl +++ b/include/Nazara/Renderer/ShaderAst.inl @@ -3,6 +3,7 @@ // For conditions of distribution and use, see copyright notice in Config.hpp #include +#include #include namespace Nz @@ -50,6 +51,9 @@ namespace Nz left(std::move(Left)), right(std::move(Right)) { + //TODO: AstParseError + if (left->GetExpressionType() != right->GetExpressionType()) + throw std::runtime_error("Left expression type must match right expression type"); } inline Branch::Branch(ExpressionPtr condition, StatementPtr trueStatement, StatementPtr falseStatement) diff --git a/src/Nazara/Renderer/ShaderAst.cpp b/src/Nazara/Renderer/ShaderAst.cpp index 8fc554623..7cc3b6313 100644 --- a/src/Nazara/Renderer/ShaderAst.cpp +++ b/src/Nazara/Renderer/ShaderAst.cpp @@ -31,6 +31,11 @@ namespace Nz { namespace ShaderAst } + ExpressionType Variable::GetExpressionType() const + { + return type; + } + void NamedVariable::Register(ShaderWriter& visitor) { visitor.RegisterVariable(kind, name, type); @@ -52,6 +57,11 @@ namespace Nz { namespace ShaderAst } + ExpressionType AssignOp::GetExpressionType() const + { + return variable->GetExpressionType(); + } + void AssignOp::Register(ShaderWriter& visitor) { variable->Register(visitor); @@ -64,6 +74,28 @@ namespace Nz { namespace ShaderAst } + ExpressionType BinaryOp::GetExpressionType() const + { + ShaderAst::ExpressionType exprType = ShaderAst::ExpressionType::None; + + switch (op) + { + case ShaderAst::BinaryType::Add: + case ShaderAst::BinaryType::Divide: + case ShaderAst::BinaryType::Multiply: + case ShaderAst::BinaryType::Substract: + exprType = left->GetExpressionType(); + break; + + case ShaderAst::BinaryType::Equality: + exprType = ExpressionType::Boolean; + } + + NazaraAssert(exprType != ShaderAst::ExpressionType::None, "Unhandled builtin"); + + return exprType; + } + void BinaryOp::Register(ShaderWriter& visitor) { left->Register(visitor); @@ -94,6 +126,11 @@ namespace Nz { namespace ShaderAst } + ExpressionType Constant::GetExpressionType() const + { + return exprType; + } + void Constant::Register(ShaderWriter&) { }