Commit current work
This commit is contained in:
73
include/Nazara/Renderer/GlslWriter.hpp
Normal file
73
include/Nazara/Renderer/GlslWriter.hpp
Normal file
@@ -0,0 +1,73 @@
|
||||
// Copyright (C) 2016 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_GLSLWRITER_HPP
|
||||
#define NAZARA_GLSLWRITER_HPP
|
||||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <Nazara/Core/StringStream.hpp>
|
||||
#include <Nazara/Renderer/Config.hpp>
|
||||
#include <Nazara/Renderer/ShaderWriter.hpp>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class NAZARA_RENDERER_API GlslWriter : public ShaderWriter
|
||||
{
|
||||
public:
|
||||
GlslWriter();
|
||||
GlslWriter(const GlslWriter&) = delete;
|
||||
GlslWriter(GlslWriter&&) = delete;
|
||||
~GlslWriter() = default;
|
||||
|
||||
Nz::String Generate(const ShaderAst::StatementPtr& node) override;
|
||||
|
||||
void RegisterFunction(const String& name, ShaderAst::StatementPtr statement, std::initializer_list<ShaderAst::VariablePtr> parameters, ShaderAst::ExpressionType ret) override;
|
||||
void RegisterVariable(const String& name, ShaderAst::ExpressionType type) override;
|
||||
|
||||
void Write(const ShaderAst::AssignOp& node) override;
|
||||
void Write(const ShaderAst::Branch& node) override;
|
||||
void Write(const ShaderAst::BinaryOp& node) override;
|
||||
void Write(const ShaderAst::Constant& node) override;
|
||||
void Write(const ShaderAst::ExpressionStatement& node) override;
|
||||
void Write(const ShaderAst::NodePtr& node) override;
|
||||
void Write(const ShaderAst::StatementBlock& node) override;
|
||||
void Write(const ShaderAst::Variable& node) override;
|
||||
|
||||
private:
|
||||
struct Function;
|
||||
|
||||
void Append(ShaderAst::ExpressionType type);
|
||||
void Append(const String& txt);
|
||||
void AppendFunction(const Function& func);
|
||||
void AppendLine(const Nz::String& txt = Nz::String());
|
||||
|
||||
void EnterScope();
|
||||
void LeaveScope();
|
||||
|
||||
struct Function
|
||||
{
|
||||
std::vector<ShaderAst::VariablePtr> parameters;
|
||||
ShaderAst::ExpressionType retType;
|
||||
ShaderAst::StatementPtr node;
|
||||
String name;
|
||||
};
|
||||
|
||||
struct State
|
||||
{
|
||||
std::set<std::pair<ShaderAst::ExpressionType, String>> m_variables;
|
||||
StringStream stream;
|
||||
unsigned int indentLevel = 0;
|
||||
};
|
||||
|
||||
std::unordered_map<String, Function> m_functions;
|
||||
State* m_currentState;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // NAZARA_GLSLWRITER_HPP
|
||||
197
include/Nazara/Renderer/ShaderAst.hpp
Normal file
197
include/Nazara/Renderer/ShaderAst.hpp
Normal file
@@ -0,0 +1,197 @@
|
||||
// Copyright (C) 2016 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_AST_HPP
|
||||
#define NAZARA_SHADER_AST_HPP
|
||||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <Nazara/Core/String.hpp>
|
||||
#include <Nazara/Math/Vector2.hpp>
|
||||
#include <Nazara/Math/Vector3.hpp>
|
||||
#include <Nazara/Math/Vector4.hpp>
|
||||
#include <Nazara/Renderer/Config.hpp>
|
||||
#include <Nazara/Renderer/Enums.hpp>
|
||||
#include <memory>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class ShaderWriter;
|
||||
|
||||
namespace ShaderAst
|
||||
{
|
||||
enum class AssignType
|
||||
{
|
||||
Simple //< =
|
||||
};
|
||||
|
||||
enum class BinaryType
|
||||
{
|
||||
Add, //< +
|
||||
Substract, //< -
|
||||
Multiply, //< *
|
||||
Divide, //< /
|
||||
Equality //< ==
|
||||
};
|
||||
|
||||
enum class ExpressionType
|
||||
{
|
||||
Float1, // float
|
||||
Float2, // vec2
|
||||
Float3, // vec3
|
||||
Float4, // vec4
|
||||
|
||||
None // void
|
||||
};
|
||||
|
||||
enum class VariableType
|
||||
{
|
||||
Parameter,
|
||||
Variable,
|
||||
Uniform
|
||||
};
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
class Node;
|
||||
|
||||
using NodePtr = std::shared_ptr<Node>;
|
||||
|
||||
class NAZARA_RENDERER_API Node
|
||||
{
|
||||
public:
|
||||
virtual ~Node() = default;
|
||||
|
||||
virtual void Register(ShaderWriter& visitor) = 0;
|
||||
virtual void Visit(ShaderWriter& visitor) = 0;
|
||||
};
|
||||
|
||||
class Statement;
|
||||
|
||||
using StatementPtr = std::shared_ptr<Statement>;
|
||||
|
||||
class NAZARA_RENDERER_API Statement : public Node
|
||||
{
|
||||
};
|
||||
|
||||
class Expression;
|
||||
|
||||
using ExpressionPtr = std::shared_ptr<Expression>;
|
||||
|
||||
class NAZARA_RENDERER_API Expression : public Node
|
||||
{
|
||||
};
|
||||
|
||||
class NAZARA_RENDERER_API ExpressionStatement : public Statement
|
||||
{
|
||||
public:
|
||||
inline explicit ExpressionStatement(ExpressionPtr expr);
|
||||
|
||||
void Register(ShaderWriter& visitor) override;
|
||||
void Visit(ShaderWriter& visitor) override;
|
||||
|
||||
ExpressionPtr expression;
|
||||
};
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
class NAZARA_RENDERER_API StatementBlock : public Statement
|
||||
{
|
||||
public:
|
||||
template<typename... Args> explicit StatementBlock(Args&&... args);
|
||||
|
||||
void Register(ShaderWriter& visitor) override;
|
||||
void Visit(ShaderWriter& visitor) override;
|
||||
|
||||
std::vector<StatementPtr> statements;
|
||||
};
|
||||
|
||||
class Variable;
|
||||
|
||||
using VariablePtr = std::shared_ptr<Variable>;
|
||||
|
||||
class NAZARA_RENDERER_API Variable : public Expression
|
||||
{
|
||||
public:
|
||||
inline Variable(VariableType varKind, const Nz::String& varName, ExpressionType varType);
|
||||
|
||||
void Register(ShaderWriter& visitor) override;
|
||||
void Visit(ShaderWriter& visitor) override;
|
||||
|
||||
ExpressionType type;
|
||||
VariableType kind;
|
||||
Nz::String name;
|
||||
};
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
class NAZARA_RENDERER_API AssignOp : public Expression
|
||||
{
|
||||
public:
|
||||
inline AssignOp(AssignType Op, VariablePtr Var, ExpressionPtr Right);
|
||||
|
||||
void Register(ShaderWriter& visitor) override;
|
||||
void Visit(ShaderWriter& visitor) override;
|
||||
|
||||
AssignType op;
|
||||
VariablePtr variable;
|
||||
ExpressionPtr right;
|
||||
};
|
||||
|
||||
class NAZARA_RENDERER_API BinaryOp : public Expression
|
||||
{
|
||||
public:
|
||||
inline BinaryOp(BinaryType Op, ExpressionPtr Left, ExpressionPtr Right);
|
||||
|
||||
void Register(ShaderWriter& visitor) override;
|
||||
void Visit(ShaderWriter& visitor) override;
|
||||
|
||||
BinaryType op;
|
||||
ExpressionPtr left;
|
||||
ExpressionPtr right;
|
||||
};
|
||||
|
||||
class NAZARA_RENDERER_API Branch : public Statement
|
||||
{
|
||||
public:
|
||||
inline Branch(ExpressionPtr condition, StatementPtr trueStatement, StatementPtr falseStatement = nullptr);
|
||||
|
||||
void Register(ShaderWriter& visitor) override;
|
||||
void Visit(ShaderWriter& visitor) override;
|
||||
|
||||
struct ConditionalStatement
|
||||
{
|
||||
ExpressionPtr condition;
|
||||
StatementPtr statement;
|
||||
};
|
||||
|
||||
std::vector<ConditionalStatement> condStatements;
|
||||
StatementPtr elseStatement;
|
||||
};
|
||||
|
||||
class NAZARA_RENDERER_API Constant : public Expression
|
||||
{
|
||||
public:
|
||||
inline explicit Constant(float value);
|
||||
|
||||
void Register(ShaderWriter& visitor) override;
|
||||
void Visit(ShaderWriter& visitor) override;
|
||||
|
||||
ExpressionType exprType;
|
||||
|
||||
union
|
||||
{
|
||||
float vec1;
|
||||
Nz::Vector2f vec2;
|
||||
Nz::Vector3f vec3;
|
||||
Nz::Vector4f vec4;
|
||||
} values;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#include <Nazara/Renderer/ShaderAst.inl>
|
||||
|
||||
#endif // NAZARA_SHADER_AST_HPP
|
||||
58
include/Nazara/Renderer/ShaderAst.inl
Normal file
58
include/Nazara/Renderer/ShaderAst.inl
Normal file
@@ -0,0 +1,58 @@
|
||||
// Copyright (C) 2016 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/ShaderAst.hpp>
|
||||
#include <Nazara/Renderer/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
namespace ShaderAst
|
||||
{
|
||||
inline ExpressionStatement::ExpressionStatement(ExpressionPtr expr) :
|
||||
expression(std::move(expr))
|
||||
{
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
StatementBlock::StatementBlock(Args&& ...args) :
|
||||
statements({std::forward<Args>(args)...})
|
||||
{
|
||||
}
|
||||
|
||||
inline Variable::Variable(VariableType varKind, const Nz::String& varName, ExpressionType varType) :
|
||||
kind(varKind),
|
||||
name(varName),
|
||||
type(varType)
|
||||
{
|
||||
}
|
||||
|
||||
inline AssignOp::AssignOp(AssignType Op, VariablePtr Var, ExpressionPtr Right) :
|
||||
op(Op),
|
||||
variable(std::move(Var)),
|
||||
right(std::move(Right))
|
||||
{
|
||||
}
|
||||
|
||||
inline BinaryOp::BinaryOp(BinaryType Op, ExpressionPtr Left, ExpressionPtr Right) :
|
||||
op(Op),
|
||||
left(std::move(Left)),
|
||||
right(std::move(Right))
|
||||
{
|
||||
}
|
||||
|
||||
inline Branch::Branch(ExpressionPtr condition, StatementPtr trueStatement, StatementPtr falseStatement)
|
||||
{
|
||||
condStatements.emplace_back(ConditionalStatement{ std::move(condition), std::move(trueStatement) });
|
||||
elseStatement = std::move(falseStatement);
|
||||
}
|
||||
|
||||
inline Constant::Constant(float value) :
|
||||
exprType(ExpressionType::Float1)
|
||||
{
|
||||
values.vec1 = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#include <Nazara/Renderer/DebugOff.hpp>
|
||||
70
include/Nazara/Renderer/ShaderBuilder.hpp
Normal file
70
include/Nazara/Renderer/ShaderBuilder.hpp
Normal file
@@ -0,0 +1,70 @@
|
||||
// Copyright (C) 2016 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_BUILDER_HPP
|
||||
#define NAZARA_SHADER_BUILDER_HPP
|
||||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <Nazara/Renderer/ShaderAst.hpp>
|
||||
#include <memory>
|
||||
|
||||
namespace Nz { namespace ShaderBuilder
|
||||
{
|
||||
template<typename T>
|
||||
class GenBuilder
|
||||
{
|
||||
public:
|
||||
template<typename... Args>
|
||||
std::shared_ptr<T> operator()(Args&&... args) const
|
||||
{
|
||||
return std::make_shared<T>(std::forward<Args>(args)...);
|
||||
}
|
||||
};
|
||||
|
||||
template<Nz::ShaderAst::AssignType op>
|
||||
class AssignOpBuilder
|
||||
{
|
||||
public:
|
||||
std::shared_ptr<Nz::ShaderAst::AssignOp> operator()(const Nz::ShaderAst::VariablePtr& left, const Nz::ShaderAst::ExpressionPtr& right) const
|
||||
{
|
||||
return std::make_shared<Nz::ShaderAst::AssignOp>(op, left, right);
|
||||
}
|
||||
};
|
||||
|
||||
template<Nz::ShaderAst::BinaryType op>
|
||||
class BinOpBuilder
|
||||
{
|
||||
public:
|
||||
std::shared_ptr<Nz::ShaderAst::BinaryOp> operator()(const Nz::ShaderAst::ExpressionPtr& left, const Nz::ShaderAst::ExpressionPtr& right) const
|
||||
{
|
||||
return std::make_shared<Nz::ShaderAst::BinaryOp>(op, left, right);
|
||||
}
|
||||
};
|
||||
|
||||
template<Nz::ShaderAst::VariableType type>
|
||||
class VarBuilder
|
||||
{
|
||||
public:
|
||||
template<typename... Args>
|
||||
std::shared_ptr<Nz::ShaderAst::Variable> operator()(Args&&... args) const
|
||||
{
|
||||
return std::make_shared<Nz::ShaderAst::Variable>(type, std::forward<Args>(args)...);
|
||||
}
|
||||
};
|
||||
|
||||
constexpr BinOpBuilder<Nz::ShaderAst::BinaryType::Add> Add;
|
||||
constexpr AssignOpBuilder<Nz::ShaderAst::AssignType::Simple> Assign;
|
||||
constexpr BinOpBuilder<Nz::ShaderAst::BinaryType::Equality> Equal;
|
||||
constexpr GenBuilder<Nz::ShaderAst::StatementBlock> Block;
|
||||
constexpr GenBuilder<Nz::ShaderAst::Branch> Branch;
|
||||
constexpr GenBuilder<Nz::ShaderAst::Constant> Constant;
|
||||
constexpr GenBuilder<Nz::ShaderAst::ExpressionStatement> ExprStatement;
|
||||
constexpr VarBuilder<Nz::ShaderAst::VariableType::Variable> Variable;
|
||||
} }
|
||||
|
||||
#include <Nazara/Renderer/ShaderBuilder.inl>
|
||||
|
||||
#endif // NAZARA_SHADER_BUILDER_HPP
|
||||
15
include/Nazara/Renderer/ShaderBuilder.inl
Normal file
15
include/Nazara/Renderer/ShaderBuilder.inl
Normal file
@@ -0,0 +1,15 @@
|
||||
// Copyright (C) 2016 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/ShaderBuilder.hpp>
|
||||
#include <Nazara/Renderer/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
namespace ShaderAst
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
#include <Nazara/Renderer/DebugOff.hpp>
|
||||
40
include/Nazara/Renderer/ShaderWriter.hpp
Normal file
40
include/Nazara/Renderer/ShaderWriter.hpp
Normal file
@@ -0,0 +1,40 @@
|
||||
// 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_SHADERWRITER_HPP
|
||||
#define NAZARA_SHADERWRITER_HPP
|
||||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <Nazara/Renderer/Config.hpp>
|
||||
#include <Nazara/Renderer/ShaderAst.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class NAZARA_RENDERER_API ShaderWriter
|
||||
{
|
||||
public:
|
||||
ShaderWriter() = default;
|
||||
ShaderWriter(const ShaderWriter&) = delete;
|
||||
ShaderWriter(ShaderWriter&&) = delete;
|
||||
virtual ~ShaderWriter();
|
||||
|
||||
virtual Nz::String Generate(const ShaderAst::StatementPtr& node) = 0;
|
||||
|
||||
virtual void RegisterFunction(const String& name, ShaderAst::StatementPtr node, std::initializer_list<ShaderAst::VariablePtr> parameters, ShaderAst::ExpressionType ret) = 0;
|
||||
virtual void RegisterVariable(const String& name, ShaderAst::ExpressionType type) = 0;
|
||||
|
||||
virtual void Write(const ShaderAst::AssignOp& node) = 0;
|
||||
virtual void Write(const ShaderAst::Branch& node) = 0;
|
||||
virtual void Write(const ShaderAst::BinaryOp& node) = 0;
|
||||
virtual void Write(const ShaderAst::Constant& node) = 0;
|
||||
virtual void Write(const ShaderAst::ExpressionStatement& node) = 0;
|
||||
virtual void Write(const ShaderAst::NodePtr& node) = 0;
|
||||
virtual void Write(const ShaderAst::StatementBlock& node) = 0;
|
||||
virtual void Write(const ShaderAst::Variable& node) = 0;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // NAZARA_SHADERWRITER_HPP
|
||||
Reference in New Issue
Block a user