Shader: Add initial support for options (WIP)
This commit is contained in:
@@ -39,7 +39,7 @@ namespace Nz
|
||||
std::shared_ptr<RenderPipeline> InstantiateRenderPipeline(RenderPipelineInfo pipelineInfo) override;
|
||||
std::shared_ptr<RenderPipelineLayout> InstantiateRenderPipelineLayout(RenderPipelineLayoutInfo pipelineLayoutInfo) override;
|
||||
std::shared_ptr<ShaderModule> InstantiateShaderModule(ShaderStageTypeFlags shaderStages, ShaderAst::StatementPtr& shaderAst, const ShaderWriter::States& states) override;
|
||||
std::shared_ptr<ShaderModule> InstantiateShaderModule(ShaderStageTypeFlags shaderStages, ShaderLanguage lang, const void* source, std::size_t sourceSize) override;
|
||||
std::shared_ptr<ShaderModule> InstantiateShaderModule(ShaderStageTypeFlags shaderStages, ShaderLanguage lang, const void* source, std::size_t sourceSize, const ShaderWriter::States& states) override;
|
||||
std::shared_ptr<Texture> InstantiateTexture(const TextureInfo& params) override;
|
||||
std::shared_ptr<TextureSampler> InstantiateTextureSampler(const TextureSamplerInfo& params) override;
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ namespace Nz
|
||||
{
|
||||
public:
|
||||
OpenGLShaderModule(OpenGLDevice& device, ShaderStageTypeFlags shaderStages, ShaderAst::StatementPtr& shaderAst, const ShaderWriter::States& states);
|
||||
OpenGLShaderModule(OpenGLDevice& device, ShaderStageTypeFlags shaderStages, ShaderLanguage lang, const void* source, std::size_t sourceSize);
|
||||
OpenGLShaderModule(OpenGLDevice& device, ShaderStageTypeFlags shaderStages, ShaderLanguage lang, const void* source, std::size_t sourceSize, const ShaderWriter::States& states);
|
||||
OpenGLShaderModule(const OpenGLShaderModule&) = delete;
|
||||
OpenGLShaderModule(OpenGLShaderModule&&) noexcept = default;
|
||||
~OpenGLShaderModule() = default;
|
||||
|
||||
@@ -40,8 +40,8 @@ namespace Nz
|
||||
virtual std::shared_ptr<RenderPipeline> InstantiateRenderPipeline(RenderPipelineInfo pipelineInfo) = 0;
|
||||
virtual std::shared_ptr<RenderPipelineLayout> InstantiateRenderPipelineLayout(RenderPipelineLayoutInfo pipelineLayoutInfo) = 0;
|
||||
virtual std::shared_ptr<ShaderModule> InstantiateShaderModule(ShaderStageTypeFlags shaderStages, ShaderAst::StatementPtr& shaderAst, const ShaderWriter::States& states) = 0;
|
||||
virtual std::shared_ptr<ShaderModule> InstantiateShaderModule(ShaderStageTypeFlags shaderStages, ShaderLanguage lang, const void* source, std::size_t sourceSize) = 0;
|
||||
std::shared_ptr<ShaderModule> InstantiateShaderModule(ShaderStageTypeFlags shaderStages, ShaderLanguage lang, const std::filesystem::path& sourcePath);
|
||||
virtual std::shared_ptr<ShaderModule> InstantiateShaderModule(ShaderStageTypeFlags shaderStages, ShaderLanguage lang, const void* source, std::size_t sourceSize, const ShaderWriter::States& states) = 0;
|
||||
std::shared_ptr<ShaderModule> InstantiateShaderModule(ShaderStageTypeFlags shaderStages, ShaderLanguage lang, const std::filesystem::path& sourcePath, const ShaderWriter::States& states);
|
||||
virtual std::shared_ptr<Texture> InstantiateTexture(const TextureInfo& params) = 0;
|
||||
virtual std::shared_ptr<TextureSampler> InstantiateTextureSampler(const TextureSamplerInfo& params) = 0;
|
||||
};
|
||||
|
||||
@@ -45,6 +45,7 @@ namespace Nz::ShaderAst
|
||||
virtual ExpressionPtr Clone(ConstantExpression& node);
|
||||
virtual ExpressionPtr Clone(IdentifierExpression& node);
|
||||
virtual ExpressionPtr Clone(IntrinsicExpression& node);
|
||||
virtual ExpressionPtr Clone(SelectOptionExpression& node);
|
||||
virtual ExpressionPtr Clone(SwizzleExpression& node);
|
||||
virtual ExpressionPtr Clone(VariableExpression& node);
|
||||
|
||||
@@ -52,6 +53,7 @@ namespace Nz::ShaderAst
|
||||
virtual StatementPtr Clone(ConditionalStatement& node);
|
||||
virtual StatementPtr Clone(DeclareExternalStatement& node);
|
||||
virtual StatementPtr Clone(DeclareFunctionStatement& node);
|
||||
virtual StatementPtr Clone(DeclareOptionStatement& node);
|
||||
virtual StatementPtr Clone(DeclareStructStatement& node);
|
||||
virtual StatementPtr Clone(DeclareVariableStatement& node);
|
||||
virtual StatementPtr Clone(DiscardStatement& node);
|
||||
|
||||
@@ -35,12 +35,14 @@ NAZARA_SHADERAST_EXPRESSION(ConditionalExpression)
|
||||
NAZARA_SHADERAST_EXPRESSION(ConstantExpression)
|
||||
NAZARA_SHADERAST_EXPRESSION(IdentifierExpression)
|
||||
NAZARA_SHADERAST_EXPRESSION(IntrinsicExpression)
|
||||
NAZARA_SHADERAST_EXPRESSION(SelectOptionExpression)
|
||||
NAZARA_SHADERAST_EXPRESSION(SwizzleExpression)
|
||||
NAZARA_SHADERAST_EXPRESSION(VariableExpression)
|
||||
NAZARA_SHADERAST_STATEMENT(BranchStatement)
|
||||
NAZARA_SHADERAST_STATEMENT(ConditionalStatement)
|
||||
NAZARA_SHADERAST_STATEMENT(DeclareExternalStatement)
|
||||
NAZARA_SHADERAST_STATEMENT(DeclareFunctionStatement)
|
||||
NAZARA_SHADERAST_STATEMENT(DeclareOptionStatement)
|
||||
NAZARA_SHADERAST_STATEMENT(DeclareStructStatement)
|
||||
NAZARA_SHADERAST_STATEMENT(DeclareVariableStatement)
|
||||
NAZARA_SHADERAST_STATEMENT(DiscardStatement)
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include <Nazara/Prerequisites.hpp>
|
||||
#include <Nazara/Shader/Config.hpp>
|
||||
#include <Nazara/Shader/Ast/AstCloner.hpp>
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
|
||||
namespace Nz::ShaderAst
|
||||
@@ -29,16 +30,15 @@ namespace Nz::ShaderAst
|
||||
AstOptimizer& operator=(AstOptimizer&&) = delete;
|
||||
|
||||
protected:
|
||||
using AstCloner::Visit;
|
||||
void Visit(BinaryExpression& node) override;
|
||||
void Visit(ConditionalExpression& node) override;
|
||||
void Visit(BranchStatement& node) override;
|
||||
void Visit(ConditionalStatement& node) override;
|
||||
ExpressionPtr Clone(BinaryExpression& node) override;
|
||||
ExpressionPtr Clone(ConditionalExpression& node) override;
|
||||
StatementPtr Clone(BranchStatement& node) override;
|
||||
StatementPtr Clone(ConditionalStatement& node) override;
|
||||
|
||||
template<BinaryType Type> void PropagateConstant(std::unique_ptr<ConstantExpression>&& lhs, std::unique_ptr<ConstantExpression>&& rhs);
|
||||
template<BinaryType Type> ExpressionPtr PropagateConstant(std::unique_ptr<ConstantExpression>&& lhs, std::unique_ptr<ConstantExpression>&& rhs);
|
||||
|
||||
private:
|
||||
UInt64 m_enabledConditions;
|
||||
std::optional<UInt64> m_enabledOptions;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -29,6 +29,7 @@ namespace Nz::ShaderAst
|
||||
void Visit(ConstantExpression& node) override;
|
||||
void Visit(IdentifierExpression& node) override;
|
||||
void Visit(IntrinsicExpression& node) override;
|
||||
void Visit(SelectOptionExpression& node) override;
|
||||
void Visit(SwizzleExpression& node) override;
|
||||
void Visit(VariableExpression& node) override;
|
||||
|
||||
@@ -36,6 +37,7 @@ namespace Nz::ShaderAst
|
||||
void Visit(ConditionalStatement& node) override;
|
||||
void Visit(DeclareExternalStatement& node) override;
|
||||
void Visit(DeclareFunctionStatement& node) override;
|
||||
void Visit(DeclareOptionStatement& node) override;
|
||||
void Visit(DeclareStructStatement& node) override;
|
||||
void Visit(DeclareVariableStatement& node) override;
|
||||
void Visit(DiscardStatement& node) override;
|
||||
|
||||
@@ -32,6 +32,7 @@ namespace Nz::ShaderAst
|
||||
void Serialize(ConstantExpression& node);
|
||||
void Serialize(IdentifierExpression& node);
|
||||
void Serialize(IntrinsicExpression& node);
|
||||
void Serialize(SelectOptionExpression& node);
|
||||
void Serialize(SwizzleExpression& node);
|
||||
void Serialize(VariableExpression& node);
|
||||
|
||||
@@ -39,6 +40,7 @@ namespace Nz::ShaderAst
|
||||
void Serialize(ConditionalStatement& node);
|
||||
void Serialize(DeclareExternalStatement& node);
|
||||
void Serialize(DeclareFunctionStatement& node);
|
||||
void Serialize(DeclareOptionStatement& node);
|
||||
void Serialize(DeclareStructStatement& node);
|
||||
void Serialize(DeclareVariableStatement& node);
|
||||
void Serialize(DiscardStatement& node);
|
||||
|
||||
@@ -40,6 +40,7 @@ namespace Nz::ShaderAst
|
||||
void Visit(ConstantExpression& node) override;
|
||||
void Visit(IdentifierExpression& node) override;
|
||||
void Visit(IntrinsicExpression& node) override;
|
||||
void Visit(SelectOptionExpression& node) override;
|
||||
void Visit(SwizzleExpression& node) override;
|
||||
void Visit(VariableExpression& node) override;
|
||||
|
||||
|
||||
@@ -22,7 +22,8 @@ namespace Nz::ShaderAst
|
||||
Builtin, //< Builtin (struct member only) - has argument type
|
||||
Entry, //< Entry point (function only) - has argument type
|
||||
Layout, //< Struct layout (struct only) - has argument style
|
||||
Location //< Location (struct member only) - has argument index
|
||||
Location, //< Location (struct member only) - has argument index
|
||||
Option, //< Conditional compilation option - has argument expr
|
||||
};
|
||||
|
||||
enum class BinaryType
|
||||
|
||||
@@ -116,7 +116,7 @@ namespace Nz::ShaderAst
|
||||
NodeType GetType() const override;
|
||||
void Visit(AstExpressionVisitor& visitor) override;
|
||||
|
||||
std::string conditionName;
|
||||
std::size_t optionIndex;
|
||||
ExpressionPtr falsePath;
|
||||
ExpressionPtr truePath;
|
||||
};
|
||||
@@ -146,6 +146,16 @@ namespace Nz::ShaderAst
|
||||
std::vector<ExpressionPtr> parameters;
|
||||
};
|
||||
|
||||
struct NAZARA_SHADER_API SelectOptionExpression : public Expression
|
||||
{
|
||||
NodeType GetType() const override;
|
||||
void Visit(AstExpressionVisitor& visitor) override;
|
||||
|
||||
std::string optionName;
|
||||
ExpressionPtr falsePath;
|
||||
ExpressionPtr truePath;
|
||||
};
|
||||
|
||||
struct NAZARA_SHADER_API SwizzleExpression : public Expression
|
||||
{
|
||||
NodeType GetType() const override;
|
||||
@@ -203,7 +213,7 @@ namespace Nz::ShaderAst
|
||||
NodeType GetType() const override;
|
||||
void Visit(AstStatementVisitor& visitor) override;
|
||||
|
||||
std::string conditionName;
|
||||
std::size_t optionIndex;
|
||||
StatementPtr statement;
|
||||
};
|
||||
|
||||
@@ -237,12 +247,24 @@ namespace Nz::ShaderAst
|
||||
std::optional<ShaderStageType> entryStage;
|
||||
std::optional<std::size_t> funcIndex;
|
||||
std::optional<std::size_t> varIndex;
|
||||
std::string optionName;
|
||||
std::string name;
|
||||
std::vector<Parameter> parameters;
|
||||
std::vector<StatementPtr> statements;
|
||||
ExpressionType returnType;
|
||||
};
|
||||
|
||||
struct NAZARA_SHADER_API DeclareOptionStatement : Statement
|
||||
{
|
||||
NodeType GetType() const override;
|
||||
void Visit(AstStatementVisitor& visitor) override;
|
||||
|
||||
std::optional<std::size_t> optIndex;
|
||||
std::string optName;
|
||||
ExpressionPtr initialValue;
|
||||
ExpressionType optType;
|
||||
};
|
||||
|
||||
struct NAZARA_SHADER_API DeclareStructStatement : Statement
|
||||
{
|
||||
NodeType GetType() const override;
|
||||
|
||||
@@ -17,16 +17,24 @@ namespace Nz::ShaderAst
|
||||
class NAZARA_SHADER_API SanitizeVisitor final : AstCloner
|
||||
{
|
||||
public:
|
||||
struct Options;
|
||||
|
||||
inline SanitizeVisitor();
|
||||
SanitizeVisitor(const SanitizeVisitor&) = delete;
|
||||
SanitizeVisitor(SanitizeVisitor&&) = delete;
|
||||
~SanitizeVisitor() = default;
|
||||
|
||||
StatementPtr Sanitize(StatementPtr& statement, std::string* error = nullptr);
|
||||
inline StatementPtr Sanitize(StatementPtr& statement, std::string* error = nullptr);
|
||||
StatementPtr Sanitize(StatementPtr& statement, const Options& options, std::string* error = nullptr);
|
||||
|
||||
SanitizeVisitor& operator=(const SanitizeVisitor&) = delete;
|
||||
SanitizeVisitor& operator=(SanitizeVisitor&&) = delete;
|
||||
|
||||
struct Options
|
||||
{
|
||||
bool removeOptionDeclaration = true;
|
||||
};
|
||||
|
||||
private:
|
||||
struct Identifier;
|
||||
|
||||
@@ -42,12 +50,14 @@ namespace Nz::ShaderAst
|
||||
ExpressionPtr Clone(ConstantExpression& node) override;
|
||||
ExpressionPtr Clone(IdentifierExpression& node) override;
|
||||
ExpressionPtr Clone(IntrinsicExpression& node) override;
|
||||
ExpressionPtr Clone(SelectOptionExpression& node) override;
|
||||
ExpressionPtr Clone(SwizzleExpression& node) override;
|
||||
|
||||
StatementPtr Clone(BranchStatement& node) override;
|
||||
StatementPtr Clone(ConditionalStatement& node) override;
|
||||
StatementPtr Clone(DeclareExternalStatement& node) override;
|
||||
StatementPtr Clone(DeclareFunctionStatement& node) override;
|
||||
StatementPtr Clone(DeclareOptionStatement& node) override;
|
||||
StatementPtr Clone(DeclareStructStatement& node) override;
|
||||
StatementPtr Clone(DeclareVariableStatement& node) override;
|
||||
StatementPtr Clone(ExpressionStatement& node) override;
|
||||
@@ -64,6 +74,7 @@ namespace Nz::ShaderAst
|
||||
void PopScope();
|
||||
|
||||
inline std::size_t RegisterFunction(std::string name);
|
||||
inline std::size_t RegisterOption(std::string name, ExpressionType type);
|
||||
inline std::size_t RegisterStruct(std::string name, StructDescription description);
|
||||
inline std::size_t RegisterVariable(std::string name, ExpressionType type);
|
||||
|
||||
@@ -78,6 +89,11 @@ namespace Nz::ShaderAst
|
||||
std::variant<ExpressionType> value;
|
||||
};
|
||||
|
||||
struct Option
|
||||
{
|
||||
std::size_t optionIndex;
|
||||
};
|
||||
|
||||
struct Struct
|
||||
{
|
||||
std::size_t structIndex;
|
||||
@@ -91,11 +107,12 @@ namespace Nz::ShaderAst
|
||||
struct Identifier
|
||||
{
|
||||
std::string name;
|
||||
std::variant<Alias, Struct, Variable> value;
|
||||
std::variant<Alias, Option, Struct, Variable> value;
|
||||
};
|
||||
|
||||
std::size_t m_nextFuncIndex;
|
||||
std::vector<Identifier> m_identifiersInScope;
|
||||
std::vector<ExpressionType> m_options;
|
||||
std::vector<StructDescription> m_structs;
|
||||
std::vector<ExpressionType> m_variables;
|
||||
std::vector<std::size_t> m_scopeSizes;
|
||||
@@ -105,6 +122,7 @@ namespace Nz::ShaderAst
|
||||
};
|
||||
|
||||
inline StatementPtr Sanitize(StatementPtr& ast, std::string* error = nullptr);
|
||||
inline StatementPtr Sanitize(StatementPtr& ast, const SanitizeVisitor::Options& options, std::string* error = nullptr);
|
||||
}
|
||||
|
||||
#include <Nazara/Shader/Ast/SanitizeVisitor.inl>
|
||||
|
||||
@@ -12,6 +12,11 @@ namespace Nz::ShaderAst
|
||||
{
|
||||
}
|
||||
|
||||
inline StatementPtr SanitizeVisitor::Sanitize(StatementPtr& statement, std::string* error)
|
||||
{
|
||||
return Sanitize(statement, {}, error);
|
||||
}
|
||||
|
||||
inline auto SanitizeVisitor::FindIdentifier(const std::string_view& identifierName) const -> const Identifier*
|
||||
{
|
||||
auto it = std::find_if(m_identifiersInScope.rbegin(), m_identifiersInScope.rend(), [&](const Identifier& identifier) { return identifier.name == identifierName; });
|
||||
@@ -23,10 +28,23 @@ namespace Nz::ShaderAst
|
||||
|
||||
inline std::size_t SanitizeVisitor::RegisterFunction(std::string name)
|
||||
{
|
||||
std::size_t funcIndex = m_nextFuncIndex++;
|
||||
return funcIndex;
|
||||
return m_nextFuncIndex++;
|
||||
}
|
||||
|
||||
inline std::size_t SanitizeVisitor::RegisterOption(std::string name, ExpressionType type)
|
||||
{
|
||||
std::size_t optionIndex = m_options.size();
|
||||
m_options.emplace_back(std::move(type));
|
||||
|
||||
m_identifiersInScope.push_back({
|
||||
std::move(name),
|
||||
Option {
|
||||
optionIndex
|
||||
}
|
||||
});
|
||||
|
||||
return optionIndex;
|
||||
}
|
||||
|
||||
inline std::size_t SanitizeVisitor::RegisterStruct(std::string name, StructDescription description)
|
||||
{
|
||||
@@ -63,6 +81,12 @@ namespace Nz::ShaderAst
|
||||
SanitizeVisitor sanitizer;
|
||||
return sanitizer.Sanitize(ast, error);
|
||||
}
|
||||
|
||||
StatementPtr Sanitize(StatementPtr& ast, const SanitizeVisitor::Options& options, std::string* error)
|
||||
{
|
||||
SanitizeVisitor sanitizer;
|
||||
return sanitizer.Sanitize(ast, options, error);
|
||||
}
|
||||
}
|
||||
|
||||
#include <Nazara/Shader/DebugOff.hpp>
|
||||
|
||||
@@ -29,8 +29,8 @@ namespace Nz
|
||||
GlslWriter(GlslWriter&&) = delete;
|
||||
~GlslWriter() = default;
|
||||
|
||||
inline std::string Generate(ShaderAst::StatementPtr& shader, const States& conditions = {});
|
||||
std::string Generate(std::optional<ShaderStageType> shaderStage, ShaderAst::StatementPtr& shader, const States& conditions = {});
|
||||
inline std::string Generate(ShaderAst::StatementPtr& shader, const States& states = {});
|
||||
std::string Generate(std::optional<ShaderStageType> shaderStage, ShaderAst::StatementPtr& shader, const States& states = {});
|
||||
|
||||
void SetEnv(Environment environment);
|
||||
|
||||
@@ -60,7 +60,6 @@ namespace Nz
|
||||
template<typename T> void Append(const T& param);
|
||||
template<typename T1, typename T2, typename... Args> void Append(const T1& firstParam, const T2& secondParam, Args&&... params);
|
||||
void AppendCommentSection(const std::string& section);
|
||||
void AppendEntryPoint(ShaderStageType shaderStage, ShaderAst::StatementPtr& shader);
|
||||
void AppendField(std::size_t structIndex, const std::size_t* memberIndices, std::size_t remainingMembers);
|
||||
void AppendHeader();
|
||||
void AppendLine(const std::string& txt = {});
|
||||
|
||||
@@ -12,9 +12,9 @@ namespace Nz
|
||||
{
|
||||
}
|
||||
|
||||
inline std::string GlslWriter::Generate(ShaderAst::StatementPtr& shader, const States& conditions)
|
||||
inline std::string GlslWriter::Generate(ShaderAst::StatementPtr& shader, const States& states)
|
||||
{
|
||||
return Generate(std::nullopt, shader, conditions);
|
||||
return Generate(std::nullopt, shader, states);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
111
include/Nazara/Shader/LangWriter.hpp
Normal file
111
include/Nazara/Shader/LangWriter.hpp
Normal file
@@ -0,0 +1,111 @@
|
||||
// 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_LANGWRITER_HPP
|
||||
#define NAZARA_LANGWRITER_HPP
|
||||
|
||||
#include <Nazara/Prerequisites.hpp>
|
||||
#include <Nazara/Shader/Config.hpp>
|
||||
#include <Nazara/Shader/ShaderWriter.hpp>
|
||||
#include <Nazara/Shader/Ast/AstExpressionVisitorExcept.hpp>
|
||||
#include <Nazara/Shader/Ast/AstStatementVisitorExcept.hpp>
|
||||
#include <set>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class NAZARA_SHADER_API LangWriter : public ShaderWriter, public ShaderAst::ExpressionVisitorExcept, public ShaderAst::StatementVisitorExcept
|
||||
{
|
||||
public:
|
||||
struct Environment;
|
||||
|
||||
inline LangWriter();
|
||||
LangWriter(const LangWriter&) = delete;
|
||||
LangWriter(LangWriter&&) = delete;
|
||||
~LangWriter() = default;
|
||||
|
||||
std::string Generate(ShaderAst::StatementPtr& shader, const States& conditions = {});
|
||||
|
||||
void SetEnv(Environment environment);
|
||||
|
||||
struct Environment
|
||||
{
|
||||
};
|
||||
|
||||
private:
|
||||
struct BindingAttribute;
|
||||
struct BuiltinAttribute;
|
||||
struct EntryAttribute;
|
||||
struct LayoutAttribute;
|
||||
struct LocationAttribute;
|
||||
|
||||
void Append(const ShaderAst::ExpressionType& type);
|
||||
void Append(const ShaderAst::IdentifierType& identifierType);
|
||||
void Append(const ShaderAst::MatrixType& matrixType);
|
||||
void Append(ShaderAst::NoType);
|
||||
void Append(ShaderAst::PrimitiveType type);
|
||||
void Append(const ShaderAst::SamplerType& samplerType);
|
||||
void Append(const ShaderAst::StructType& structType);
|
||||
void Append(const ShaderAst::UniformType& uniformType);
|
||||
void Append(const ShaderAst::VectorType& vecType);
|
||||
template<typename T> void Append(const T& param);
|
||||
template<typename T1, typename T2, typename... Args> void Append(const T1& firstParam, const T2& secondParam, Args&&... params);
|
||||
template<typename... Args> void AppendAttributes(bool appendLine, Args&&... params);
|
||||
void AppendAttribute(BindingAttribute builtin);
|
||||
void AppendAttribute(BuiltinAttribute builtin);
|
||||
void AppendAttribute(EntryAttribute entry);
|
||||
void AppendAttribute(LayoutAttribute layout);
|
||||
void AppendAttribute(LocationAttribute location);
|
||||
void AppendCommentSection(const std::string& section);
|
||||
void AppendField(std::size_t structIndex, const std::size_t* memberIndices, std::size_t remainingMembers);
|
||||
void AppendHeader();
|
||||
void AppendLine(const std::string& txt = {});
|
||||
template<typename... Args> void AppendLine(Args&&... params);
|
||||
void AppendStatementList(std::vector<ShaderAst::StatementPtr>& statements);
|
||||
|
||||
void EnterScope();
|
||||
void LeaveScope(bool skipLine = true);
|
||||
|
||||
void RegisterOption(std::size_t optionIndex, std::string optionName);
|
||||
void RegisterStruct(std::size_t structIndex, ShaderAst::StructDescription desc);
|
||||
void RegisterVariable(std::size_t varIndex, std::string varName);
|
||||
|
||||
void Visit(ShaderAst::ExpressionPtr& expr, bool encloseIfRequired = false);
|
||||
|
||||
void Visit(ShaderAst::AccessMemberIndexExpression& node) override;
|
||||
void Visit(ShaderAst::AssignExpression& node) override;
|
||||
void Visit(ShaderAst::BinaryExpression& node) override;
|
||||
void Visit(ShaderAst::CastExpression& node) override;
|
||||
void Visit(ShaderAst::ConditionalExpression& node) override;
|
||||
void Visit(ShaderAst::ConstantExpression& node) override;
|
||||
void Visit(ShaderAst::IntrinsicExpression& node) override;
|
||||
void Visit(ShaderAst::SwizzleExpression& node) override;
|
||||
void Visit(ShaderAst::VariableExpression& node) override;
|
||||
|
||||
void Visit(ShaderAst::BranchStatement& node) override;
|
||||
void Visit(ShaderAst::ConditionalStatement& node) override;
|
||||
void Visit(ShaderAst::DeclareExternalStatement& node) override;
|
||||
void Visit(ShaderAst::DeclareFunctionStatement& node) override;
|
||||
void Visit(ShaderAst::DeclareOptionStatement& node) override;
|
||||
void Visit(ShaderAst::DeclareStructStatement& node) override;
|
||||
void Visit(ShaderAst::DeclareVariableStatement& node) override;
|
||||
void Visit(ShaderAst::DiscardStatement& node) override;
|
||||
void Visit(ShaderAst::ExpressionStatement& node) override;
|
||||
void Visit(ShaderAst::MultiStatement& node) override;
|
||||
void Visit(ShaderAst::NoOpStatement& node) override;
|
||||
void Visit(ShaderAst::ReturnStatement& node) override;
|
||||
|
||||
struct State;
|
||||
|
||||
Environment m_environment;
|
||||
State* m_currentState;
|
||||
};
|
||||
}
|
||||
|
||||
#include <Nazara/Shader/LangWriter.inl>
|
||||
|
||||
#endif
|
||||
16
include/Nazara/Shader/LangWriter.inl
Normal file
16
include/Nazara/Shader/LangWriter.inl
Normal file
@@ -0,0 +1,16 @@
|
||||
// Copyright (C) 2020 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Shader generator"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Shader/LangWriter.hpp>
|
||||
#include <Nazara/Shader/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
inline LangWriter::LangWriter() :
|
||||
m_currentState(nullptr)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
#include <Nazara/Shader/DebugOff.hpp>
|
||||
@@ -44,12 +44,12 @@ namespace Nz::ShaderBuilder
|
||||
|
||||
struct ConditionalExpression
|
||||
{
|
||||
inline std::unique_ptr<ShaderAst::ConditionalExpression> operator()(std::string conditionName, ShaderAst::ExpressionPtr truePath, ShaderAst::ExpressionPtr falsePath) const;
|
||||
inline std::unique_ptr<ShaderAst::ConditionalExpression> operator()(std::size_t optionIndex, ShaderAst::ExpressionPtr truePath, ShaderAst::ExpressionPtr falsePath) const;
|
||||
};
|
||||
|
||||
struct ConditionalStatement
|
||||
{
|
||||
inline std::unique_ptr<ShaderAst::ConditionalStatement> operator()(std::string conditionName, ShaderAst::StatementPtr statement) const;
|
||||
inline std::unique_ptr<ShaderAst::ConditionalStatement> operator()(std::size_t optionIndex, ShaderAst::StatementPtr statement) const;
|
||||
};
|
||||
|
||||
struct Constant
|
||||
@@ -64,6 +64,11 @@ namespace Nz::ShaderBuilder
|
||||
inline std::unique_ptr<ShaderAst::DeclareFunctionStatement> operator()(std::optional<ShaderStageType> entryStage, std::string name, std::vector<ShaderAst::DeclareFunctionStatement::Parameter> parameters, std::vector<ShaderAst::StatementPtr> statements, ShaderAst::ExpressionType returnType = ShaderAst::NoType{}) const;
|
||||
};
|
||||
|
||||
struct DeclareOption
|
||||
{
|
||||
inline std::unique_ptr<ShaderAst::DeclareOptionStatement> operator()(std::string name, ShaderAst::ExpressionType type, ShaderAst::ExpressionPtr initialValue = nullptr) const;
|
||||
};
|
||||
|
||||
struct DeclareStruct
|
||||
{
|
||||
inline std::unique_ptr<ShaderAst::DeclareStructStatement> operator()(ShaderAst::StructDescription description) const;
|
||||
@@ -106,6 +111,11 @@ namespace Nz::ShaderBuilder
|
||||
inline std::unique_ptr<ShaderAst::ReturnStatement> operator()(ShaderAst::ExpressionPtr expr = nullptr) const;
|
||||
};
|
||||
|
||||
struct SelectOption
|
||||
{
|
||||
inline std::unique_ptr<ShaderAst::SelectOptionExpression> operator()(std::string optionName, ShaderAst::ExpressionPtr truePath, ShaderAst::ExpressionPtr falsePath) const;
|
||||
};
|
||||
|
||||
struct Swizzle
|
||||
{
|
||||
inline std::unique_ptr<ShaderAst::SwizzleExpression> operator()(ShaderAst::ExpressionPtr expression, std::vector<ShaderAst::SwizzleComponent> swizzleComponents) const;
|
||||
@@ -121,6 +131,7 @@ namespace Nz::ShaderBuilder
|
||||
constexpr Impl::ConditionalStatement ConditionalStatement;
|
||||
constexpr Impl::Constant Constant;
|
||||
constexpr Impl::DeclareFunction DeclareFunction;
|
||||
constexpr Impl::DeclareOption DeclareOption;
|
||||
constexpr Impl::DeclareStruct DeclareStruct;
|
||||
constexpr Impl::DeclareVariable DeclareVariable;
|
||||
constexpr Impl::ExpressionStatement ExpressionStatement;
|
||||
@@ -130,6 +141,7 @@ namespace Nz::ShaderBuilder
|
||||
constexpr Impl::Multi MultiStatement;
|
||||
constexpr Impl::NoParam<ShaderAst::NoOpStatement> NoOp;
|
||||
constexpr Impl::Return Return;
|
||||
constexpr Impl::SelectOption SelectOption;
|
||||
constexpr Impl::Swizzle Swizzle;
|
||||
}
|
||||
|
||||
|
||||
@@ -70,20 +70,20 @@ namespace Nz::ShaderBuilder
|
||||
return castNode;
|
||||
}
|
||||
|
||||
inline std::unique_ptr<ShaderAst::ConditionalExpression> Impl::ConditionalExpression::operator()(std::string conditionName, ShaderAst::ExpressionPtr truePath, ShaderAst::ExpressionPtr falsePath) const
|
||||
inline std::unique_ptr<ShaderAst::ConditionalExpression> Impl::ConditionalExpression::operator()(std::size_t optionIndex, ShaderAst::ExpressionPtr truePath, ShaderAst::ExpressionPtr falsePath) const
|
||||
{
|
||||
auto condExprNode = std::make_unique<ShaderAst::ConditionalExpression>();
|
||||
condExprNode->conditionName = std::move(conditionName);
|
||||
condExprNode->optionIndex = optionIndex;
|
||||
condExprNode->falsePath = std::move(falsePath);
|
||||
condExprNode->truePath = std::move(truePath);
|
||||
|
||||
return condExprNode;
|
||||
}
|
||||
|
||||
inline std::unique_ptr<ShaderAst::ConditionalStatement> Impl::ConditionalStatement::operator()(std::string conditionName, ShaderAst::StatementPtr statement) const
|
||||
inline std::unique_ptr<ShaderAst::ConditionalStatement> Impl::ConditionalStatement::operator()(std::size_t optionIndex, ShaderAst::StatementPtr statement) const
|
||||
{
|
||||
auto condStatementNode = std::make_unique<ShaderAst::ConditionalStatement>();
|
||||
condStatementNode->conditionName = std::move(conditionName);
|
||||
condStatementNode->optionIndex = optionIndex;
|
||||
condStatementNode->statement = std::move(statement);
|
||||
|
||||
return condStatementNode;
|
||||
@@ -129,6 +129,16 @@ namespace Nz::ShaderBuilder
|
||||
return declareFunctionNode;
|
||||
}
|
||||
|
||||
inline std::unique_ptr<ShaderAst::DeclareOptionStatement> Nz::ShaderBuilder::Impl::DeclareOption::operator()(std::string name, ShaderAst::ExpressionType type, ShaderAst::ExpressionPtr initialValue) const
|
||||
{
|
||||
auto declareOptionNode = std::make_unique<ShaderAst::DeclareOptionStatement>();
|
||||
declareOptionNode->optName = std::move(name);
|
||||
declareOptionNode->optType = std::move(type);
|
||||
declareOptionNode->initialValue = std::move(initialValue);
|
||||
|
||||
return declareOptionNode;
|
||||
}
|
||||
|
||||
inline std::unique_ptr<ShaderAst::DeclareStructStatement> Impl::DeclareStruct::operator()(ShaderAst::StructDescription description) const
|
||||
{
|
||||
auto declareStructNode = std::make_unique<ShaderAst::DeclareStructStatement>();
|
||||
@@ -203,6 +213,16 @@ namespace Nz::ShaderBuilder
|
||||
return returnNode;
|
||||
}
|
||||
|
||||
inline std::unique_ptr<ShaderAst::SelectOptionExpression> Impl::SelectOption::operator()(std::string optionName, ShaderAst::ExpressionPtr truePath, ShaderAst::ExpressionPtr falsePath) const
|
||||
{
|
||||
auto selectOptNode = std::make_unique<ShaderAst::SelectOptionExpression>();
|
||||
selectOptNode->optionName = std::move(optionName);
|
||||
selectOptNode->falsePath = std::move(falsePath);
|
||||
selectOptNode->truePath = std::move(truePath);
|
||||
|
||||
return selectOptNode;
|
||||
}
|
||||
|
||||
inline std::unique_ptr<ShaderAst::SwizzleExpression> Impl::Swizzle::operator()(ShaderAst::ExpressionPtr expression, std::vector<ShaderAst::SwizzleComponent> swizzleComponents) const
|
||||
{
|
||||
auto swizzleNode = std::make_unique<ShaderAst::SwizzleExpression>();
|
||||
|
||||
@@ -85,6 +85,7 @@ namespace Nz::ShaderLang
|
||||
std::vector<ShaderAst::StatementPtr> ParseFunctionBody();
|
||||
ShaderAst::StatementPtr ParseFunctionDeclaration(std::vector<ShaderAst::Attribute> attributes = {});
|
||||
ShaderAst::DeclareFunctionStatement::Parameter ParseFunctionParameter();
|
||||
ShaderAst::StatementPtr ParseOptionDeclaration();
|
||||
ShaderAst::StatementPtr ParseStructDeclaration(std::vector<ShaderAst::Attribute> attributes = {});
|
||||
ShaderAst::StatementPtr ParseReturnStatement();
|
||||
ShaderAst::StatementPtr ParseStatement();
|
||||
@@ -100,6 +101,7 @@ namespace Nz::ShaderLang
|
||||
std::vector<ShaderAst::ExpressionPtr> ParseParameters();
|
||||
ShaderAst::ExpressionPtr ParseParenthesisExpression();
|
||||
ShaderAst::ExpressionPtr ParsePrimaryExpression();
|
||||
ShaderAst::ExpressionPtr ParseSelectOptExpression();
|
||||
ShaderAst::ExpressionPtr ParseVariableAssignation();
|
||||
|
||||
ShaderAst::AttributeType ParseIdentifierAsAttributeType();
|
||||
|
||||
@@ -40,8 +40,10 @@ NAZARA_SHADERLANG_TOKEN(Plus)
|
||||
NAZARA_SHADERLANG_TOKEN(OpenCurlyBracket)
|
||||
NAZARA_SHADERLANG_TOKEN(OpenSquareBracket)
|
||||
NAZARA_SHADERLANG_TOKEN(OpenParenthesis)
|
||||
NAZARA_SHADERLANG_TOKEN(Option)
|
||||
NAZARA_SHADERLANG_TOKEN(Semicolon)
|
||||
NAZARA_SHADERLANG_TOKEN(Return)
|
||||
NAZARA_SHADERLANG_TOKEN(SelectOpt)
|
||||
NAZARA_SHADERLANG_TOKEN(Struct)
|
||||
|
||||
#undef NAZARA_SHADERLANG_TOKEN
|
||||
|
||||
@@ -26,7 +26,7 @@ namespace Nz
|
||||
|
||||
struct States
|
||||
{
|
||||
Nz::UInt64 enabledConditions = 0;
|
||||
Nz::UInt64 enabledOptions = 0;
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ namespace Nz
|
||||
SpirvWriter(SpirvWriter&&) = delete;
|
||||
~SpirvWriter() = default;
|
||||
|
||||
std::vector<UInt32> Generate(ShaderAst::StatementPtr& shader, const States& conditions = {});
|
||||
std::vector<UInt32> Generate(ShaderAst::StatementPtr& shader, const States& states = {});
|
||||
|
||||
void SetEnv(Environment environment);
|
||||
|
||||
@@ -62,7 +62,7 @@ namespace Nz
|
||||
UInt32 GetPointerTypeId(const ShaderAst::ExpressionType& type, SpirvStorageClass storageClass) const;
|
||||
UInt32 GetTypeId(const ShaderAst::ExpressionType& type) const;
|
||||
|
||||
inline bool IsConditionEnabled(const std::string& condition) const;
|
||||
bool IsOptionEnabled(std::size_t optionIndex) const;
|
||||
|
||||
UInt32 RegisterConstant(const ShaderAst::ConstantValue& value);
|
||||
UInt32 RegisterFunctionType(const ShaderAst::DeclareFunctionStatement& functionNode);
|
||||
|
||||
@@ -8,14 +8,6 @@
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
inline bool SpirvWriter::IsConditionEnabled(const std::string& condition) const
|
||||
{
|
||||
/*std::size_t conditionIndex = m_context.shader->FindConditionByName(condition);
|
||||
assert(conditionIndex != ShaderAst::InvalidCondition);
|
||||
|
||||
return TestBit<Nz::UInt64>(m_context.states->enabledConditions, conditionIndex);*/
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
#include <Nazara/Shader/DebugOff.hpp>
|
||||
|
||||
@@ -30,7 +30,7 @@ namespace Nz
|
||||
std::shared_ptr<RenderPipeline> InstantiateRenderPipeline(RenderPipelineInfo pipelineInfo) override;
|
||||
std::shared_ptr<RenderPipelineLayout> InstantiateRenderPipelineLayout(RenderPipelineLayoutInfo pipelineLayoutInfo) override;
|
||||
std::shared_ptr<ShaderModule> InstantiateShaderModule(ShaderStageTypeFlags stages, ShaderAst::StatementPtr& shaderAst, const ShaderWriter::States& states) override;
|
||||
std::shared_ptr<ShaderModule> InstantiateShaderModule(ShaderStageTypeFlags stages, ShaderLanguage lang, const void* source, std::size_t sourceSize) override;
|
||||
std::shared_ptr<ShaderModule> InstantiateShaderModule(ShaderStageTypeFlags stages, ShaderLanguage lang, const void* source, std::size_t sourceSize, const ShaderWriter::States& states) override;
|
||||
std::shared_ptr<Texture> InstantiateTexture(const TextureInfo& params) override;
|
||||
std::shared_ptr<TextureSampler> InstantiateTextureSampler(const TextureSamplerInfo& params) override;
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ namespace Nz
|
||||
~VulkanShaderModule() = default;
|
||||
|
||||
bool Create(Vk::Device& device, ShaderStageTypeFlags shaderStages, ShaderAst::StatementPtr& shaderAst, const ShaderWriter::States& states);
|
||||
bool Create(Vk::Device& device, ShaderStageTypeFlags shaderStages, ShaderLanguage lang, const void* source, std::size_t sourceSize);
|
||||
bool Create(Vk::Device& device, ShaderStageTypeFlags shaderStages, ShaderLanguage lang, const void* source, std::size_t sourceSize, const ShaderWriter::States& states);
|
||||
|
||||
inline const Vk::ShaderModule& GetHandle() const;
|
||||
inline const std::vector<Stage>& GetStages() const;
|
||||
|
||||
Reference in New Issue
Block a user