Renderer: Add ShaderRecursiveVisitor
This commit is contained in:
parent
251810ca99
commit
a02dd3bf05
|
|
@ -0,0 +1,42 @@
|
||||||
|
// Copyright (C) 2020 Jérôme Leclercq
|
||||||
|
// This file is part of the "Nazara Engine - Renderer module"
|
||||||
|
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#ifndef NAZARA_SHADER_RECURSIVE_VISITOR_HPP
|
||||||
|
#define NAZARA_SHADER_RECURSIVE_VISITOR_HPP
|
||||||
|
|
||||||
|
#include <Nazara/Prerequisites.hpp>
|
||||||
|
#include <Nazara/Renderer/Config.hpp>
|
||||||
|
#include <Nazara/Renderer/ShaderVisitor.hpp>
|
||||||
|
|
||||||
|
namespace Nz
|
||||||
|
{
|
||||||
|
class NAZARA_RENDERER_API ShaderRecursiveVisitor : public ShaderVisitor
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
ShaderRecursiveVisitor() = default;
|
||||||
|
~ShaderRecursiveVisitor() = default;
|
||||||
|
|
||||||
|
using ShaderVisitor::Visit;
|
||||||
|
|
||||||
|
void Visit(const ShaderNodes::AccessMember& node) override;
|
||||||
|
void Visit(const ShaderNodes::AssignOp& node) override;
|
||||||
|
void Visit(const ShaderNodes::BinaryOp& node) override;
|
||||||
|
void Visit(const ShaderNodes::Branch& node) override;
|
||||||
|
void Visit(const ShaderNodes::Cast& node) override;
|
||||||
|
void Visit(const ShaderNodes::Constant& node) override;
|
||||||
|
void Visit(const ShaderNodes::DeclareVariable& node) override;
|
||||||
|
void Visit(const ShaderNodes::ExpressionStatement& node) override;
|
||||||
|
void Visit(const ShaderNodes::Identifier& node) override;
|
||||||
|
void Visit(const ShaderNodes::IntrinsicCall& node) override;
|
||||||
|
void Visit(const ShaderNodes::Sample2D& node) override;
|
||||||
|
void Visit(const ShaderNodes::StatementBlock& node) override;
|
||||||
|
void Visit(const ShaderNodes::SwizzleOp& node) override;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#include <Nazara/Renderer/ShaderRecursiveVisitor.inl>
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
// Copyright (C) 2020 Jérôme Leclercq
|
||||||
|
// This file is part of the "Nazara Engine - Renderer module"
|
||||||
|
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||||
|
|
||||||
|
#include <Nazara/Renderer/ShaderRecursiveVisitor.hpp>
|
||||||
|
#include <Nazara/Renderer/Debug.hpp>
|
||||||
|
|
||||||
|
namespace Nz
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
#include <Nazara/Renderer/DebugOff.hpp>
|
||||||
|
|
@ -12,11 +12,11 @@
|
||||||
#include <Nazara/Core/ByteStream.hpp>
|
#include <Nazara/Core/ByteStream.hpp>
|
||||||
#include <Nazara/Renderer/Config.hpp>
|
#include <Nazara/Renderer/Config.hpp>
|
||||||
#include <Nazara/Renderer/ShaderAst.hpp>
|
#include <Nazara/Renderer/ShaderAst.hpp>
|
||||||
#include <Nazara/Renderer/ShaderVisitor.hpp>
|
#include <Nazara/Renderer/ShaderRecursiveVisitor.hpp>
|
||||||
|
|
||||||
namespace Nz
|
namespace Nz
|
||||||
{
|
{
|
||||||
class NAZARA_RENDERER_API ShaderValidator : public ShaderVisitor
|
class NAZARA_RENDERER_API ShaderValidator : public ShaderRecursiveVisitor
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
inline ShaderValidator(const ShaderAst& shader);
|
inline ShaderValidator(const ShaderAst& shader);
|
||||||
|
|
@ -32,7 +32,7 @@ namespace Nz
|
||||||
void TypeMustMatch(const ShaderNodes::ExpressionPtr& left, const ShaderNodes::ExpressionPtr& right);
|
void TypeMustMatch(const ShaderNodes::ExpressionPtr& left, const ShaderNodes::ExpressionPtr& right);
|
||||||
void TypeMustMatch(const ShaderExpressionType& left, const ShaderExpressionType& right);
|
void TypeMustMatch(const ShaderExpressionType& left, const ShaderExpressionType& right);
|
||||||
|
|
||||||
using ShaderVisitor::Visit;
|
using ShaderRecursiveVisitor::Visit;
|
||||||
void Visit(const ShaderNodes::AccessMember& node) override;
|
void Visit(const ShaderNodes::AccessMember& node) override;
|
||||||
void Visit(const ShaderNodes::AssignOp& node) override;
|
void Visit(const ShaderNodes::AssignOp& node) override;
|
||||||
void Visit(const ShaderNodes::BinaryOp& node) override;
|
void Visit(const ShaderNodes::BinaryOp& node) override;
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,93 @@
|
||||||
|
// Copyright (C) 2015 Jérôme Leclercq
|
||||||
|
// This file is part of the "Nazara Engine - Renderer module"
|
||||||
|
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||||
|
|
||||||
|
#include <Nazara/Renderer/ShaderRecursiveVisitor.hpp>
|
||||||
|
#include <Nazara/Renderer/Debug.hpp>
|
||||||
|
|
||||||
|
namespace Nz
|
||||||
|
{
|
||||||
|
void ShaderRecursiveVisitor::Visit(const ShaderNodes::AccessMember& node)
|
||||||
|
{
|
||||||
|
Visit(node.structExpr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ShaderRecursiveVisitor::Visit(const ShaderNodes::AssignOp& node)
|
||||||
|
{
|
||||||
|
Visit(node.left);
|
||||||
|
Visit(node.right);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ShaderRecursiveVisitor::Visit(const ShaderNodes::BinaryOp& node)
|
||||||
|
{
|
||||||
|
Visit(node.left);
|
||||||
|
Visit(node.right);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ShaderRecursiveVisitor::Visit(const ShaderNodes::Branch& node)
|
||||||
|
{
|
||||||
|
for (auto& cond : node.condStatements)
|
||||||
|
{
|
||||||
|
Visit(cond.condition);
|
||||||
|
Visit(cond.statement);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (node.elseStatement)
|
||||||
|
Visit(node.elseStatement);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ShaderRecursiveVisitor::Visit(const ShaderNodes::Cast& node)
|
||||||
|
{
|
||||||
|
for (auto& expr : node.expressions)
|
||||||
|
{
|
||||||
|
if (!expr)
|
||||||
|
break;
|
||||||
|
|
||||||
|
Visit(expr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ShaderRecursiveVisitor::Visit(const ShaderNodes::Constant& /*node*/)
|
||||||
|
{
|
||||||
|
/* Nothing to do */
|
||||||
|
}
|
||||||
|
|
||||||
|
void ShaderRecursiveVisitor::Visit(const ShaderNodes::DeclareVariable& node)
|
||||||
|
{
|
||||||
|
if (node.expression)
|
||||||
|
Visit(node.expression);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ShaderRecursiveVisitor::Visit(const ShaderNodes::ExpressionStatement& node)
|
||||||
|
{
|
||||||
|
Visit(node.expression);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ShaderRecursiveVisitor::Visit(const ShaderNodes::Identifier& /*node*/)
|
||||||
|
{
|
||||||
|
/* Nothing to do */
|
||||||
|
}
|
||||||
|
|
||||||
|
void ShaderRecursiveVisitor::Visit(const ShaderNodes::IntrinsicCall& node)
|
||||||
|
{
|
||||||
|
for (auto& param : node.parameters)
|
||||||
|
Visit(param);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ShaderRecursiveVisitor::Visit(const ShaderNodes::Sample2D& node)
|
||||||
|
{
|
||||||
|
Visit(node.sampler);
|
||||||
|
Visit(node.coordinates);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ShaderRecursiveVisitor::Visit(const ShaderNodes::StatementBlock& node)
|
||||||
|
{
|
||||||
|
for (auto& statement : node.statements)
|
||||||
|
Visit(statement);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ShaderRecursiveVisitor::Visit(const ShaderNodes::SwizzleOp& node)
|
||||||
|
{
|
||||||
|
Visit(node.expression);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -114,8 +114,7 @@ namespace Nz
|
||||||
if (node.left->GetExpressionCategory() != ShaderNodes::ExpressionCategory::LValue)
|
if (node.left->GetExpressionCategory() != ShaderNodes::ExpressionCategory::LValue)
|
||||||
throw AstError { "Assignation is only possible with a l-value" };
|
throw AstError { "Assignation is only possible with a l-value" };
|
||||||
|
|
||||||
Visit(node.left);
|
ShaderRecursiveVisitor::Visit(node);
|
||||||
Visit(node.right);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ShaderValidator::Visit(const ShaderNodes::BinaryOp& node)
|
void ShaderValidator::Visit(const ShaderNodes::BinaryOp& node)
|
||||||
|
|
@ -187,17 +186,18 @@ namespace Nz
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Visit(node.left);
|
ShaderRecursiveVisitor::Visit(node);
|
||||||
Visit(node.right);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ShaderValidator::Visit(const ShaderNodes::Branch& node)
|
void ShaderValidator::Visit(const ShaderNodes::Branch& node)
|
||||||
{
|
{
|
||||||
for (const auto& condStatement : node.condStatements)
|
for (const auto& condStatement : node.condStatements)
|
||||||
{
|
{
|
||||||
Visit(MandatoryNode(condStatement.condition));
|
MandatoryNode(condStatement.condition);
|
||||||
Visit(MandatoryNode(condStatement.statement));
|
MandatoryNode(condStatement.statement);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ShaderRecursiveVisitor::Visit(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ShaderValidator::Visit(const ShaderNodes::Cast& node)
|
void ShaderValidator::Visit(const ShaderNodes::Cast& node)
|
||||||
|
|
@ -214,11 +214,12 @@ namespace Nz
|
||||||
throw AstError{ "incompatible type" };
|
throw AstError{ "incompatible type" };
|
||||||
|
|
||||||
componentCount += node.GetComponentCount(std::get<ShaderNodes::BasicType>(exprType));
|
componentCount += node.GetComponentCount(std::get<ShaderNodes::BasicType>(exprType));
|
||||||
Visit(exprPtr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (componentCount != requiredComponents)
|
if (componentCount != requiredComponents)
|
||||||
throw AstError{ "Component count doesn't match required component count" };
|
throw AstError{ "Component count doesn't match required component count" };
|
||||||
|
|
||||||
|
ShaderRecursiveVisitor::Visit(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ShaderValidator::Visit(const ShaderNodes::Constant& /*node*/)
|
void ShaderValidator::Visit(const ShaderNodes::Constant& /*node*/)
|
||||||
|
|
@ -229,17 +230,18 @@ namespace Nz
|
||||||
{
|
{
|
||||||
assert(m_context);
|
assert(m_context);
|
||||||
|
|
||||||
if (node.expression)
|
|
||||||
Visit(node.expression);
|
|
||||||
|
|
||||||
auto& local = m_context->declaredLocals.emplace_back();
|
auto& local = m_context->declaredLocals.emplace_back();
|
||||||
local.name = node.variable->name;
|
local.name = node.variable->name;
|
||||||
local.type = node.variable->type;
|
local.type = node.variable->type;
|
||||||
|
|
||||||
|
ShaderRecursiveVisitor::Visit(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ShaderValidator::Visit(const ShaderNodes::ExpressionStatement& node)
|
void ShaderValidator::Visit(const ShaderNodes::ExpressionStatement& node)
|
||||||
{
|
{
|
||||||
Visit(MandatoryNode(node.expression));
|
MandatoryNode(node.expression);
|
||||||
|
|
||||||
|
ShaderRecursiveVisitor::Visit(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ShaderValidator::Visit(const ShaderNodes::Identifier& node)
|
void ShaderValidator::Visit(const ShaderNodes::Identifier& node)
|
||||||
|
|
@ -377,8 +379,7 @@ namespace Nz
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto& param : node.parameters)
|
ShaderRecursiveVisitor::Visit(node);
|
||||||
Visit(param);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ShaderValidator::Visit(const ShaderNodes::Sample2D& node)
|
void ShaderValidator::Visit(const ShaderNodes::Sample2D& node)
|
||||||
|
|
@ -389,8 +390,7 @@ namespace Nz
|
||||||
if (MandatoryExpr(node.coordinates)->GetExpressionType() != ShaderExpressionType{ ShaderNodes::BasicType::Float2 })
|
if (MandatoryExpr(node.coordinates)->GetExpressionType() != ShaderExpressionType{ ShaderNodes::BasicType::Float2 })
|
||||||
throw AstError{ "Coordinates must be a Float2" };
|
throw AstError{ "Coordinates must be a Float2" };
|
||||||
|
|
||||||
Visit(node.sampler);
|
ShaderRecursiveVisitor::Visit(node);
|
||||||
Visit(node.coordinates);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ShaderValidator::Visit(const ShaderNodes::StatementBlock& node)
|
void ShaderValidator::Visit(const ShaderNodes::StatementBlock& node)
|
||||||
|
|
@ -400,11 +400,13 @@ namespace Nz
|
||||||
m_context->blockLocalIndex.push_back(m_context->declaredLocals.size());
|
m_context->blockLocalIndex.push_back(m_context->declaredLocals.size());
|
||||||
|
|
||||||
for (const auto& statement : node.statements)
|
for (const auto& statement : node.statements)
|
||||||
Visit(MandatoryNode(statement));
|
MandatoryNode(statement);
|
||||||
|
|
||||||
assert(m_context->declaredLocals.size() >= m_context->blockLocalIndex.back());
|
assert(m_context->declaredLocals.size() >= m_context->blockLocalIndex.back());
|
||||||
m_context->declaredLocals.resize(m_context->blockLocalIndex.back());
|
m_context->declaredLocals.resize(m_context->blockLocalIndex.back());
|
||||||
m_context->blockLocalIndex.pop_back();
|
m_context->blockLocalIndex.pop_back();
|
||||||
|
|
||||||
|
ShaderRecursiveVisitor::Visit(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ShaderValidator::Visit(const ShaderNodes::SwizzleOp& node)
|
void ShaderValidator::Visit(const ShaderNodes::SwizzleOp& node)
|
||||||
|
|
@ -428,7 +430,7 @@ namespace Nz
|
||||||
throw AstError{ "Cannot swizzle this type" };
|
throw AstError{ "Cannot swizzle this type" };
|
||||||
}
|
}
|
||||||
|
|
||||||
Visit(node.expression);
|
ShaderRecursiveVisitor::Visit(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ValidateShader(const ShaderAst& shader, std::string* error)
|
bool ValidateShader(const ShaderAst& shader, std::string* error)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue