Add ShaderAstCloner

This commit is contained in:
Jérôme Leclercq
2020-08-05 15:30:23 +02:00
parent 74fb01af28
commit d6d452d43d
8 changed files with 383 additions and 8 deletions

View File

@@ -53,6 +53,7 @@
#include <Nazara/Renderer/RenderWindowImpl.hpp>
#include <Nazara/Renderer/RenderWindowParameters.hpp>
#include <Nazara/Renderer/ShaderAst.hpp>
#include <Nazara/Renderer/ShaderAstCloner.hpp>
#include <Nazara/Renderer/ShaderAstRecursiveVisitor.hpp>
#include <Nazara/Renderer/ShaderAstSerializer.hpp>
#include <Nazara/Renderer/ShaderAstValidator.hpp>

View File

@@ -0,0 +1,73 @@
// 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
#pragma once
#ifndef NAZARA_SHADERASTCLONER_HPP
#define NAZARA_SHADERASTCLONER_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Renderer/Config.hpp>
#include <Nazara/Renderer/ShaderAstVisitor.hpp>
#include <Nazara/Renderer/ShaderVarVisitor.hpp>
#include <vector>
namespace Nz
{
class NAZARA_RENDERER_API ShaderAstCloner : public ShaderAstVisitor, public ShaderVarVisitor
{
public:
ShaderAstCloner() = default;
ShaderAstCloner(const ShaderAstCloner&) = default;
ShaderAstCloner(ShaderAstCloner&&) = default;
~ShaderAstCloner() = default;
ShaderNodes::StatementPtr Clone(const ShaderNodes::StatementPtr& statement);
ShaderAstCloner& operator=(const ShaderAstCloner&) = default;
ShaderAstCloner& operator=(ShaderAstCloner&&) = default;
private:
void Visit(const ShaderNodes::ExpressionPtr& expr);
void Visit(const ShaderNodes::StatementPtr& statement);
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;
using ShaderVarVisitor::Visit;
void Visit(const ShaderNodes::BuiltinVariable& var) override;
void Visit(const ShaderNodes::InputVariable& var) override;
void Visit(const ShaderNodes::LocalVariable& var) override;
void Visit(const ShaderNodes::OutputVariable& var) override;
void Visit(const ShaderNodes::ParameterVariable& var) override;
void Visit(const ShaderNodes::UniformVariable& var) override;
void PushExpression(ShaderNodes::ExpressionPtr expression);
void PushStatement(ShaderNodes::StatementPtr statement);
void PushVariable(ShaderNodes::VariablePtr variable);
ShaderNodes::ExpressionPtr PopExpression();
ShaderNodes::StatementPtr PopStatement();
ShaderNodes::VariablePtr PopVariable();
std::vector<ShaderNodes::ExpressionPtr> m_expressionStack;
std::vector<ShaderNodes::StatementPtr> m_statementStack;
std::vector<ShaderNodes::VariablePtr> m_variableStack;
};
}
#include <Nazara/Renderer/ShaderAstCloner.inl>
#endif

View File

@@ -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/ShaderAstCloner.hpp>
#include <Nazara/Renderer/Debug.hpp>
namespace Nz
{
}
#include <Nazara/Renderer/DebugOff.hpp>

View File

@@ -106,6 +106,7 @@ namespace Nz
std::vector<StatementPtr> statements;
static inline std::shared_ptr<StatementBlock> Build(std::vector<StatementPtr> statements);
template<typename... Args> static std::shared_ptr<StatementBlock> Build(Args&&... args);
};
@@ -115,10 +116,10 @@ namespace Nz
void Visit(ShaderAstVisitor& visitor) override;
LocalVariablePtr variable;
ExpressionPtr expression;
VariablePtr variable;
static inline std::shared_ptr<DeclareVariable> Build(LocalVariablePtr variable, ExpressionPtr expression = nullptr);
static inline std::shared_ptr<DeclareVariable> Build(VariablePtr variable, ExpressionPtr expression = nullptr);
};
struct NAZARA_RENDERER_API Identifier : public Expression
@@ -196,7 +197,8 @@ namespace Nz
StatementPtr statement;
};
inline std::shared_ptr<Branch> Build(ExpressionPtr condition, StatementPtr trueStatement, StatementPtr falseStatement = nullptr);
static inline std::shared_ptr<Branch> Build(ExpressionPtr condition, StatementPtr trueStatement, StatementPtr falseStatement = nullptr);
static inline std::shared_ptr<Branch> Build(std::vector<ConditionalStatement> statements, StatementPtr elseStatement = nullptr);
};
struct NAZARA_RENDERER_API Cast : public Expression
@@ -246,6 +248,7 @@ namespace Nz
ExpressionPtr expression;
static inline std::shared_ptr<SwizzleOp> Build(ExpressionPtr expressionPtr, std::initializer_list<SwizzleComponent> swizzleComponents);
static inline std::shared_ptr<SwizzleOp> Build(ExpressionPtr expressionPtr, const SwizzleComponent* components, std::size_t componentCount);
};
//////////////////////////////////////////////////////////////////////////

View File

@@ -107,6 +107,14 @@ namespace Nz::ShaderNodes
{
}
inline std::shared_ptr<StatementBlock> StatementBlock::Build(std::vector<StatementPtr> statements)
{
auto node = std::make_shared<StatementBlock>();
node->statements = std::move(statements);
return node;
}
template<typename... Args>
std::shared_ptr<StatementBlock> StatementBlock::Build(Args&&... args)
{
@@ -122,7 +130,7 @@ namespace Nz::ShaderNodes
{
}
inline std::shared_ptr<DeclareVariable> DeclareVariable::Build(LocalVariablePtr variable, ExpressionPtr expression)
inline std::shared_ptr<DeclareVariable> DeclareVariable::Build(VariablePtr variable, ExpressionPtr expression)
{
auto node = std::make_shared<DeclareVariable>();
node->expression = std::move(expression);
@@ -208,6 +216,15 @@ namespace Nz::ShaderNodes
return node;
}
inline std::shared_ptr<Branch> Branch::Build(std::vector<ConditionalStatement> statements, StatementPtr elseStatement)
{
auto node = std::make_shared<Branch>();
node->condStatements = std::move(statements);
node->elseStatement = std::move(elseStatement);
return node;
}
inline Cast::Cast() :
Expression(NodeType::Cast)
@@ -265,6 +282,20 @@ namespace Nz::ShaderNodes
return node;
}
inline std::shared_ptr<SwizzleOp> SwizzleOp::Build(ExpressionPtr expressionPtr, const SwizzleComponent* components, std::size_t componentCount)
{
auto node = std::make_shared<SwizzleOp>();
assert(componentCount < node->components.size());
node->componentCount = componentCount;
node->expression = std::move(expressionPtr);
std::copy(components, components + componentCount, node->components.begin());
return node;
}
inline Sample2D::Sample2D() :
Expression(NodeType::Sample2D)