Replace const ShaderAst::StatementPtr& by ShaderAst::Statement& in input
This commit is contained in:
parent
54d56abc56
commit
815a7b0c62
|
|
@ -43,7 +43,7 @@ namespace Nz
|
|||
virtual std::shared_ptr<RenderPass> InstantiateRenderPass(std::vector<RenderPass::Attachment> attachments, std::vector<RenderPass::SubpassDescription> subpassDescriptions, std::vector<RenderPass::SubpassDependency> subpassDependencies) = 0;
|
||||
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, ShaderAst::Statement& shaderAst, const ShaderWriter::States& states) = 0;
|
||||
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;
|
||||
|
|
|
|||
|
|
@ -23,8 +23,8 @@ namespace Nz::ShaderAst
|
|||
AstCloner(AstCloner&&) = delete;
|
||||
~AstCloner() = default;
|
||||
|
||||
ExpressionPtr Clone(const ExpressionPtr& statement);
|
||||
StatementPtr Clone(const StatementPtr& statement);
|
||||
ExpressionPtr Clone(Expression& statement);
|
||||
StatementPtr Clone(Statement& statement);
|
||||
|
||||
AstCloner& operator=(const AstCloner&) = delete;
|
||||
AstCloner& operator=(AstCloner&&) = delete;
|
||||
|
|
@ -79,8 +79,8 @@ namespace Nz::ShaderAst
|
|||
std::vector<StatementPtr> m_statementStack;
|
||||
};
|
||||
|
||||
inline ExpressionPtr Clone(ExpressionPtr& node);
|
||||
inline StatementPtr Clone(StatementPtr& node);
|
||||
inline ExpressionPtr Clone(Expression& node);
|
||||
inline StatementPtr Clone(Statement& node);
|
||||
}
|
||||
|
||||
#include <Nazara/Shader/Ast/AstCloner.inl>
|
||||
|
|
|
|||
|
|
@ -23,13 +23,13 @@ namespace Nz::ShaderAst
|
|||
return CloneStatement(*statement);
|
||||
}
|
||||
|
||||
inline ExpressionPtr Clone(ExpressionPtr& node)
|
||||
inline ExpressionPtr Clone(Expression& node)
|
||||
{
|
||||
AstCloner cloner;
|
||||
return cloner.Clone(node);
|
||||
}
|
||||
|
||||
inline StatementPtr Clone(StatementPtr& node)
|
||||
inline StatementPtr Clone(Statement& node)
|
||||
{
|
||||
AstCloner cloner;
|
||||
return cloner.Clone(node);
|
||||
|
|
|
|||
|
|
@ -23,8 +23,8 @@ namespace Nz::ShaderAst
|
|||
AstOptimizer(AstOptimizer&&) = delete;
|
||||
~AstOptimizer() = default;
|
||||
|
||||
StatementPtr Optimise(const StatementPtr& statement);
|
||||
StatementPtr Optimise(const StatementPtr& statement, UInt64 enabledConditions);
|
||||
StatementPtr Optimise(Statement& statement);
|
||||
StatementPtr Optimise(Statement& statement, UInt64 enabledConditions);
|
||||
|
||||
AstOptimizer& operator=(const AstOptimizer&) = delete;
|
||||
AstOptimizer& operator=(AstOptimizer&&) = delete;
|
||||
|
|
@ -48,8 +48,8 @@ namespace Nz::ShaderAst
|
|||
std::optional<UInt64> m_enabledOptions;
|
||||
};
|
||||
|
||||
inline StatementPtr Optimize(const StatementPtr& ast);
|
||||
inline StatementPtr Optimize(const StatementPtr& ast, UInt64 enabledConditions);
|
||||
inline StatementPtr Optimize(Statement& ast);
|
||||
inline StatementPtr Optimize(Statement& ast, UInt64 enabledConditions);
|
||||
}
|
||||
|
||||
#include <Nazara/Shader/Ast/AstOptimizer.inl>
|
||||
|
|
|
|||
|
|
@ -7,13 +7,13 @@
|
|||
|
||||
namespace Nz::ShaderAst
|
||||
{
|
||||
inline StatementPtr Optimize(const StatementPtr& ast)
|
||||
inline StatementPtr Optimize(Statement& ast)
|
||||
{
|
||||
AstOptimizer optimize;
|
||||
return optimize.Optimise(ast);
|
||||
}
|
||||
|
||||
inline StatementPtr Optimize(const StatementPtr& ast, UInt64 enabledConditions)
|
||||
inline StatementPtr Optimize(Statement& ast, UInt64 enabledConditions)
|
||||
{
|
||||
AstOptimizer optimize;
|
||||
return optimize.Optimise(ast, enabledConditions);
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ namespace Nz::ShaderAst
|
|||
AstReflect(AstReflect&&) = delete;
|
||||
~AstReflect() = default;
|
||||
|
||||
void Reflect(const StatementPtr& statement, const Callbacks& callbacks);
|
||||
void Reflect(Statement& statement, const Callbacks& callbacks);
|
||||
|
||||
AstReflect& operator=(const AstReflect&) = delete;
|
||||
AstReflect& operator=(AstReflect&&) = delete;
|
||||
|
|
|
|||
|
|
@ -27,8 +27,8 @@ namespace Nz::ShaderAst
|
|||
SanitizeVisitor(SanitizeVisitor&&) = delete;
|
||||
~SanitizeVisitor() = default;
|
||||
|
||||
inline StatementPtr Sanitize(const StatementPtr& statement, std::string* error = nullptr);
|
||||
StatementPtr Sanitize(const StatementPtr& statement, const Options& options, std::string* error = nullptr);
|
||||
inline StatementPtr Sanitize(Statement& statement, std::string* error = nullptr);
|
||||
StatementPtr Sanitize(Statement& statement, const Options& options, std::string* error = nullptr);
|
||||
|
||||
SanitizeVisitor& operator=(const SanitizeVisitor&) = delete;
|
||||
SanitizeVisitor& operator=(SanitizeVisitor&&) = delete;
|
||||
|
|
@ -80,7 +80,7 @@ namespace Nz::ShaderAst
|
|||
void PushScope();
|
||||
void PopScope();
|
||||
|
||||
std::size_t DeclareFunction(DeclareFunctionStatement* funcDecl);
|
||||
std::size_t DeclareFunction(DeclareFunctionStatement& funcDecl);
|
||||
|
||||
void PropagateFunctionFlags(std::size_t funcIndex, FunctionFlags flags, Bitset<>& seen);
|
||||
|
||||
|
|
@ -141,8 +141,8 @@ namespace Nz::ShaderAst
|
|||
Context* m_context;
|
||||
};
|
||||
|
||||
inline StatementPtr Sanitize(const StatementPtr& ast, std::string* error = nullptr);
|
||||
inline StatementPtr Sanitize(const StatementPtr& ast, const SanitizeVisitor::Options& options, std::string* error = nullptr);
|
||||
inline StatementPtr Sanitize(Statement& ast, std::string* error = nullptr);
|
||||
inline StatementPtr Sanitize(Statement& ast, const SanitizeVisitor::Options& options, std::string* error = nullptr);
|
||||
}
|
||||
|
||||
#include <Nazara/Shader/Ast/SanitizeVisitor.inl>
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
namespace Nz::ShaderAst
|
||||
{
|
||||
inline StatementPtr SanitizeVisitor::Sanitize(const StatementPtr& statement, std::string* error)
|
||||
inline StatementPtr SanitizeVisitor::Sanitize(Statement& statement, std::string* error)
|
||||
{
|
||||
return Sanitize(statement, {}, error);
|
||||
}
|
||||
|
|
@ -21,13 +21,13 @@ namespace Nz::ShaderAst
|
|||
return &*it;
|
||||
}
|
||||
|
||||
inline StatementPtr Sanitize(const StatementPtr& ast, std::string* error)
|
||||
inline StatementPtr Sanitize(Statement& ast, std::string* error)
|
||||
{
|
||||
SanitizeVisitor sanitizer;
|
||||
return sanitizer.Sanitize(ast, error);
|
||||
}
|
||||
|
||||
inline StatementPtr Sanitize(const StatementPtr& ast, const SanitizeVisitor::Options& options, std::string* error)
|
||||
inline StatementPtr Sanitize(Statement& ast, const SanitizeVisitor::Options& options, std::string* error)
|
||||
{
|
||||
SanitizeVisitor sanitizer;
|
||||
return sanitizer.Sanitize(ast, options, error);
|
||||
|
|
|
|||
|
|
@ -30,8 +30,8 @@ namespace Nz
|
|||
GlslWriter(GlslWriter&&) = delete;
|
||||
~GlslWriter() = default;
|
||||
|
||||
inline std::string Generate(ShaderAst::StatementPtr& shader, const States& states = {});
|
||||
std::string Generate(std::optional<ShaderStageType> shaderStage, ShaderAst::StatementPtr& shader, const States& states = {});
|
||||
inline std::string Generate(ShaderAst::Statement& shader, const States& states = {});
|
||||
std::string Generate(std::optional<ShaderStageType> shaderStage, ShaderAst::Statement& shader, const States& states = {});
|
||||
|
||||
void SetEnv(Environment environment);
|
||||
|
||||
|
|
@ -45,6 +45,7 @@ namespace Nz
|
|||
};
|
||||
|
||||
static const char* GetFlipYUniformName();
|
||||
static ShaderAst::StatementPtr Sanitize(ShaderAst::Statement& ast, std::string* error = nullptr);
|
||||
|
||||
private:
|
||||
void Append(const ShaderAst::ExpressionType& type);
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ namespace Nz
|
|||
{
|
||||
}
|
||||
|
||||
inline std::string GlslWriter::Generate(ShaderAst::StatementPtr& shader, const States& states)
|
||||
inline std::string GlslWriter::Generate(ShaderAst::Statement& shader, const States& states)
|
||||
{
|
||||
return Generate(std::nullopt, shader, states);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ namespace Nz
|
|||
LangWriter(LangWriter&&) = delete;
|
||||
~LangWriter() = default;
|
||||
|
||||
std::string Generate(ShaderAst::StatementPtr& shader, const States& conditions = {});
|
||||
std::string Generate(ShaderAst::Statement& shader, const States& conditions = {});
|
||||
|
||||
void SetEnv(Environment environment);
|
||||
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ namespace Nz
|
|||
SpirvWriter(SpirvWriter&&) = delete;
|
||||
~SpirvWriter() = default;
|
||||
|
||||
std::vector<UInt32> Generate(ShaderAst::StatementPtr& shader, const States& states = {});
|
||||
std::vector<UInt32> Generate(ShaderAst::Statement& shader, const States& states = {});
|
||||
|
||||
void SetEnv(Environment environment);
|
||||
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ namespace Nz
|
|||
std::shared_ptr<RenderPass> InstantiateRenderPass(std::vector<RenderPass::Attachment> attachments, std::vector<RenderPass::SubpassDescription> subpassDescriptions, std::vector<RenderPass::SubpassDependency> subpassDependencies) override;
|
||||
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, ShaderAst::Statement& shaderAst, const ShaderWriter::States& states) 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;
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ namespace Nz
|
|||
VulkanShaderModule(VulkanShaderModule&&) = delete;
|
||||
~VulkanShaderModule() = default;
|
||||
|
||||
bool Create(Vk::Device& device, ShaderStageTypeFlags shaderStages, ShaderAst::StatementPtr& shaderAst, const ShaderWriter::States& states);
|
||||
bool Create(Vk::Device& device, ShaderStageTypeFlags shaderStages, ShaderAst::Statement& shaderAst, const ShaderWriter::States& states);
|
||||
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;
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ namespace Nz
|
|||
ShaderAst::SanitizeVisitor::Options options;
|
||||
options.removeOptionDeclaration = false;
|
||||
|
||||
m_shaderAst = ShaderAst::Sanitize(shaderAst, options);
|
||||
m_shaderAst = ShaderAst::Sanitize(*shaderAst, options);
|
||||
|
||||
std::size_t optionCount = 0;
|
||||
|
||||
|
|
@ -31,7 +31,7 @@ namespace Nz
|
|||
};
|
||||
|
||||
ShaderAst::AstReflect reflect;
|
||||
reflect.Reflect(m_shaderAst, callbacks);
|
||||
reflect.Reflect(*m_shaderAst, callbacks);
|
||||
|
||||
if (optionCount >= 64)
|
||||
throw std::runtime_error("Too many conditions");
|
||||
|
|
@ -61,7 +61,7 @@ namespace Nz
|
|||
states.enabledOptions = combination;
|
||||
states.sanitized = true;
|
||||
|
||||
std::shared_ptr<ShaderModule> stage = Graphics::Instance()->GetRenderDevice()->InstantiateShaderModule(m_shaderStage, m_shaderAst, std::move(states));
|
||||
std::shared_ptr<ShaderModule> stage = Graphics::Instance()->GetRenderDevice()->InstantiateShaderModule(m_shaderStage, *m_shaderAst, std::move(states));
|
||||
|
||||
it = m_combinations.emplace(combination, std::move(stage)).first;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -122,7 +122,7 @@ namespace Nz
|
|||
return std::make_shared<OpenGLRenderPipelineLayout>(std::move(pipelineLayoutInfo));
|
||||
}
|
||||
|
||||
std::shared_ptr<ShaderModule> OpenGLDevice::InstantiateShaderModule(ShaderStageTypeFlags shaderStages, ShaderAst::StatementPtr& shaderAst, const ShaderWriter::States& states)
|
||||
std::shared_ptr<ShaderModule> OpenGLDevice::InstantiateShaderModule(ShaderStageTypeFlags shaderStages, ShaderAst::Statement& shaderAst, const ShaderWriter::States& states)
|
||||
{
|
||||
return std::make_shared<OpenGLShaderModule>(*this, shaderStages, shaderAst, states);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@
|
|||
|
||||
namespace Nz
|
||||
{
|
||||
OpenGLShaderModule::OpenGLShaderModule(OpenGLDevice& device, ShaderStageTypeFlags shaderStages, ShaderAst::StatementPtr& shaderAst, const ShaderWriter::States& states)
|
||||
OpenGLShaderModule::OpenGLShaderModule(OpenGLDevice& device, ShaderStageTypeFlags shaderStages, ShaderAst::Statement& shaderAst, const ShaderWriter::States& states) :
|
||||
{
|
||||
NazaraAssert(shaderStages != 0, "at least one shader stage must be specified");
|
||||
Create(device, shaderStages, shaderAst, states);
|
||||
|
|
@ -57,7 +57,7 @@ namespace Nz
|
|||
case ShaderLanguage::NazaraBinary:
|
||||
{
|
||||
auto shader = ShaderAst::UnserializeShader(source, sourceSize);
|
||||
Create(device, shaderStages, shader, {});
|
||||
Create(device, shaderStages, *shader, states);
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
@ -67,7 +67,7 @@ namespace Nz
|
|||
|
||||
Nz::ShaderLang::Parser parser;
|
||||
Nz::ShaderAst::StatementPtr shaderAst = parser.Parse(tokens);
|
||||
Create(device, shaderStages, shaderAst, states);
|
||||
Create(device, shaderStages, *shaderAst, states);
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -8,17 +8,17 @@
|
|||
|
||||
namespace Nz::ShaderAst
|
||||
{
|
||||
ExpressionPtr AstCloner::Clone(const ExpressionPtr& expr)
|
||||
ExpressionPtr AstCloner::Clone(Expression& expr)
|
||||
{
|
||||
expr->Visit(*this);
|
||||
expr.Visit(*this);
|
||||
|
||||
assert(m_statementStack.empty() && m_expressionStack.size() == 1);
|
||||
return PopExpression();
|
||||
}
|
||||
|
||||
StatementPtr AstCloner::Clone(const StatementPtr& statement)
|
||||
StatementPtr AstCloner::Clone(Statement& statement)
|
||||
{
|
||||
statement->Visit(*this);
|
||||
statement.Visit(*this);
|
||||
|
||||
assert(m_expressionStack.empty() && m_statementStack.size() == 1);
|
||||
return PopStatement();
|
||||
|
|
|
|||
|
|
@ -531,13 +531,13 @@ namespace Nz::ShaderAst
|
|||
#undef EnableOptimisation
|
||||
}
|
||||
|
||||
StatementPtr AstOptimizer::Optimise(const StatementPtr& statement)
|
||||
StatementPtr AstOptimizer::Optimise(Statement& statement)
|
||||
{
|
||||
m_enabledOptions.reset();
|
||||
return CloneStatement(statement);
|
||||
}
|
||||
|
||||
StatementPtr AstOptimizer::Optimise(const StatementPtr& statement, UInt64 enabledConditions)
|
||||
StatementPtr AstOptimizer::Optimise(Statement& statement, UInt64 enabledConditions)
|
||||
{
|
||||
m_enabledOptions = enabledConditions;
|
||||
|
||||
|
|
@ -751,7 +751,7 @@ namespace Nz::ShaderAst
|
|||
if (statements.empty())
|
||||
{
|
||||
// First condition is true, dismiss the branch
|
||||
return AstCloner::Clone(condStatement.statement);
|
||||
return AstCloner::Clone(*condStatement.statement);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -772,7 +772,7 @@ namespace Nz::ShaderAst
|
|||
{
|
||||
// All conditions have been removed, replace by else statement or no-op
|
||||
if (node.elseStatement)
|
||||
return AstCloner::Clone(node.elseStatement);
|
||||
return AstCloner::Clone(*node.elseStatement);
|
||||
else
|
||||
return ShaderBuilder::NoOp();
|
||||
}
|
||||
|
|
@ -789,9 +789,9 @@ namespace Nz::ShaderAst
|
|||
return AstCloner::Clone(node);
|
||||
|
||||
if (TestBit<UInt64>(*m_enabledOptions, node.optionIndex))
|
||||
return AstCloner::Clone(node.truePath);
|
||||
return AstCloner::Clone(*node.truePath);
|
||||
else
|
||||
return AstCloner::Clone(node.falsePath);
|
||||
return AstCloner::Clone(*node.falsePath);
|
||||
}
|
||||
|
||||
ExpressionPtr AstOptimizer::Clone(UnaryExpression& node)
|
||||
|
|
|
|||
|
|
@ -8,12 +8,10 @@
|
|||
|
||||
namespace Nz::ShaderAst
|
||||
{
|
||||
void AstReflect::Reflect(const StatementPtr& statement, const Callbacks& callbacks)
|
||||
void AstReflect::Reflect(Statement& statement, const Callbacks& callbacks)
|
||||
{
|
||||
assert(statement);
|
||||
|
||||
m_callbacks = &callbacks;
|
||||
statement->Visit(*this);
|
||||
statement.Visit(*this);
|
||||
}
|
||||
|
||||
void AstReflect::Visit(DeclareOptionStatement& node)
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ namespace Nz::ShaderAst
|
|||
FunctionData* currentFunction = nullptr;
|
||||
};
|
||||
|
||||
StatementPtr SanitizeVisitor::Sanitize(const StatementPtr& nodePtr, const Options& options, std::string* error)
|
||||
StatementPtr SanitizeVisitor::Sanitize(Statement& statement, const Options& options, std::string* error)
|
||||
{
|
||||
StatementPtr clone;
|
||||
|
||||
|
|
@ -65,21 +65,21 @@ namespace Nz::ShaderAst
|
|||
RegisterIntrinsic("pow", IntrinsicType::Pow);
|
||||
|
||||
// Collect function name and their types
|
||||
if (nodePtr->GetType() == NodeType::MultiStatement)
|
||||
if (statement.GetType() == NodeType::MultiStatement)
|
||||
{
|
||||
const MultiStatement& multiStatement = static_cast<const MultiStatement&>(*nodePtr);
|
||||
const MultiStatement& multiStatement = static_cast<const MultiStatement&>(statement);
|
||||
for (auto& statementPtr : multiStatement.statements)
|
||||
{
|
||||
if (statementPtr->GetType() == NodeType::DeclareFunctionStatement)
|
||||
DeclareFunction(static_cast<DeclareFunctionStatement*>(statementPtr.get()));
|
||||
DeclareFunction(static_cast<DeclareFunctionStatement&>(*statementPtr));
|
||||
}
|
||||
}
|
||||
else if (nodePtr->GetType() == NodeType::DeclareFunctionStatement)
|
||||
DeclareFunction(static_cast<DeclareFunctionStatement*>(nodePtr.get()));
|
||||
else if (statement.GetType() == NodeType::DeclareFunctionStatement)
|
||||
DeclareFunction(static_cast<DeclareFunctionStatement&>(statement));
|
||||
|
||||
try
|
||||
{
|
||||
clone = AstCloner::Clone(nodePtr);
|
||||
clone = AstCloner::Clone(statement);
|
||||
}
|
||||
catch (const AstError& err)
|
||||
{
|
||||
|
|
@ -962,11 +962,11 @@ namespace Nz::ShaderAst
|
|||
m_scopeSizes.pop_back();
|
||||
}
|
||||
|
||||
std::size_t SanitizeVisitor::DeclareFunction(DeclareFunctionStatement* funcDecl)
|
||||
std::size_t SanitizeVisitor::DeclareFunction(DeclareFunctionStatement& funcDecl)
|
||||
{
|
||||
std::size_t functionIndex = m_functions.size();
|
||||
auto& funcData = m_functions.emplace_back();
|
||||
funcData.node = funcDecl;
|
||||
funcData.node = &funcDecl;
|
||||
|
||||
return functionIndex;
|
||||
}
|
||||
|
|
@ -1027,7 +1027,7 @@ namespace Nz::ShaderAst
|
|||
std::move(name),
|
||||
intrinsicIndex,
|
||||
Identifier::Type::Intrinsic
|
||||
});
|
||||
});
|
||||
|
||||
return intrinsicIndex;
|
||||
}
|
||||
|
|
@ -1044,7 +1044,7 @@ namespace Nz::ShaderAst
|
|||
std::move(name),
|
||||
optionIndex,
|
||||
Identifier::Type::Option
|
||||
});
|
||||
});
|
||||
|
||||
return optionIndex;
|
||||
}
|
||||
|
|
@ -1079,7 +1079,7 @@ namespace Nz::ShaderAst
|
|||
std::move(name),
|
||||
varIndex,
|
||||
Identifier::Type::Variable
|
||||
});
|
||||
});
|
||||
|
||||
return varIndex;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -138,7 +138,7 @@ namespace Nz
|
|||
unsigned int indentLevel = 0;
|
||||
};
|
||||
|
||||
std::string GlslWriter::Generate(std::optional<ShaderStageType> shaderStage, ShaderAst::StatementPtr& shader, const States& states)
|
||||
std::string GlslWriter::Generate(std::optional<ShaderStageType> shaderStage, ShaderAst::Statement& shader, const States& states)
|
||||
{
|
||||
State state;
|
||||
state.enabledOptions = states.enabledOptions;
|
||||
|
|
@ -150,26 +150,24 @@ namespace Nz
|
|||
m_currentState = nullptr;
|
||||
});
|
||||
|
||||
// Always sanitize for reserved identifiers
|
||||
ShaderAst::SanitizeVisitor::Options options;
|
||||
options.reservedIdentifiers = {
|
||||
// All reserved GLSL keywords as of GLSL ES 3.2
|
||||
"active", "asm", "atomic_uint", "attribute", "bool", "break", "buffer", "bvec2", "bvec3", "bvec4", "case", "cast", "centroid", "class", "coherent", "common", "const", "continue", "default", "discard", "dmat2", "dmat2x2", "dmat2x3", "dmat2x4", "dmat3", "dmat3x2", "dmat3x3", "dmat3x4", "dmat4", "dmat4x2", "dmat4x3", "dmat4x4", "do", "double", "dvec2", "dvec3", "dvec4", "else", "enum", "extern", "external", "false", "filter", "fixed", "flat", "float", "for", "fvec2", "fvec3", "fvec4", "goto", "half", "highp", "hvec2", "hvec3", "hvec4", "if", "iimage1D", "iimage1DArray", "iimage2D", "iimage2DArray", "iimage2DMS", "iimage2DMSArray", "iimage2DRect", "iimage3D", "iimageBuffer", "iimageCube", "iimageCubeArray", "image1D", "image1DArray", "image2D", "image2DArray", "image2DMS", "image2DMSArray", "image2DRect", "image3D", "imageBuffer", "imageCube", "imageCubeArray", "in", "inline", "inout", "input", "int", "interface", "invariant", "isampler1D", "isampler1DArray", "isampler2D", "isampler2DArray", "isampler2DMS", "isampler2DMSArray", "isampler2DRect", "isampler3D", "isamplerBuffer", "isamplerCube", "isamplerCubeArray", "isubpassInput", "isubpassInputMS", "itexture2D", "itexture2DArray", "itexture2DMS", "itexture2DMSArray", "itexture3D", "itextureBuffer", "itextureCube", "itextureCubeArray", "ivec2", "ivec3", "ivec4", "layout", "long", "lowp", "mat2", "mat2x2", "mat2x3", "mat2x4", "mat3", "mat3x2", "mat3x3", "mat3x4", "mat4", "mat4x2", "mat4x3", "mat4x4", "mediump", "namespace", "noinline", "noperspective", "out", "output", "partition", "patch", "precise", "precision", "public", "readonly", "resource", "restrict", "return", "sample", "sampler", "sampler1D", "sampler1DArray", "sampler1DArrayShadow", "sampler1DShadow", "sampler2D", "sampler2DArray", "sampler2DArrayShadow", "sampler2DMS", "sampler2DMSArray", "sampler2DRect", "sampler2DRectShadow", "sampler2DShadow", "sampler3D", "sampler3DRect", "samplerBuffer", "samplerCube", "samplerCubeArray", "samplerCubeArrayShadow", "samplerCubeShadow", "samplerShadow", "shared", "short", "sizeof", "smooth", "static", "struct", "subpassInput", "subpassInputMS", "subroutine", "superp", "switch", "template", "texture2D", "texture2DArray", "texture2DMS", "texture2DMSArray", "texture3D", "textureBuffer", "textureCube", "textureCubeArray", "this", "true", "typedef", "uimage1D", "uimage1DArray", "uimage2D", "uimage2DArray", "uimage2DMS", "uimage2DMSArray", "uimage2DRect", "uimage3D", "uimageBuffer", "uimageCube", "uimageCubeArray", "uint", "uniform", "union", "unsigned", "usampler1D", "usampler1DArray", "usampler2D", "usampler2DArray", "usampler2DMS", "usampler2DMSArray", "usampler2DRect", "usampler3D", "usamplerBuffer", "usamplerCube", "usamplerCubeArray", "using", "usubpassInput", "usubpassInputMS", "utexture2D", "utexture2DArray", "utexture2DMS", "utexture2DMSArray", "utexture3D", "utextureBuffer", "utextureCube", "utextureCubeArray", "uvec2", "uvec3", "uvec4", "varying", "vec2", "vec3", "vec4", "void", "volatile", "while", "writeonly"
|
||||
};
|
||||
ShaderAst::StatementPtr sanitizedAst;
|
||||
ShaderAst::Statement* targetAst;
|
||||
if (!states.sanitized)
|
||||
{
|
||||
sanitizedAst = Sanitize(shader);
|
||||
targetAst = sanitizedAst.get();
|
||||
}
|
||||
else
|
||||
targetAst = &shader;
|
||||
|
||||
ShaderAst::StatementPtr sanitizedAst = ShaderAst::Sanitize(shader, options);
|
||||
|
||||
ShaderAst::StatementPtr* targetAstPtr = &sanitizedAst;
|
||||
|
||||
ShaderAst::StatementPtr optimizedAst;
|
||||
if (states.optimize)
|
||||
{
|
||||
optimizedAst = ShaderAst::Optimize(*targetAstPtr);
|
||||
targetAstPtr = &optimizedAst;
|
||||
optimizedAst = ShaderAst::Optimize(*targetAst);
|
||||
targetAst = optimizedAst.get();
|
||||
}
|
||||
|
||||
ShaderAst::StatementPtr& targetAst = *targetAstPtr;
|
||||
|
||||
state.previsitor.enabledOptions = states.enabledOptions;
|
||||
state.previsitor.selectedStage = shaderStage;
|
||||
targetAst->Visit(state.previsitor);
|
||||
|
|
@ -191,6 +189,18 @@ namespace Nz
|
|||
return s_flipYUniformName;
|
||||
}
|
||||
|
||||
ShaderAst::StatementPtr GlslWriter::Sanitize(ShaderAst::Statement& ast, std::string* error)
|
||||
{
|
||||
// Always sanitize for reserved identifiers
|
||||
ShaderAst::SanitizeVisitor::Options options;
|
||||
options.reservedIdentifiers = {
|
||||
// All reserved GLSL keywords as of GLSL ES 3.2
|
||||
"active", "asm", "atomic_uint", "attribute", "bool", "break", "buffer", "bvec2", "bvec3", "bvec4", "case", "cast", "centroid", "class", "coherent", "common", "const", "continue", "default", "discard", "dmat2", "dmat2x2", "dmat2x3", "dmat2x4", "dmat3", "dmat3x2", "dmat3x3", "dmat3x4", "dmat4", "dmat4x2", "dmat4x3", "dmat4x4", "do", "double", "dvec2", "dvec3", "dvec4", "else", "enum", "extern", "external", "false", "filter", "fixed", "flat", "float", "for", "fvec2", "fvec3", "fvec4", "goto", "half", "highp", "hvec2", "hvec3", "hvec4", "if", "iimage1D", "iimage1DArray", "iimage2D", "iimage2DArray", "iimage2DMS", "iimage2DMSArray", "iimage2DRect", "iimage3D", "iimageBuffer", "iimageCube", "iimageCubeArray", "image1D", "image1DArray", "image2D", "image2DArray", "image2DMS", "image2DMSArray", "image2DRect", "image3D", "imageBuffer", "imageCube", "imageCubeArray", "in", "inline", "inout", "input", "int", "interface", "invariant", "isampler1D", "isampler1DArray", "isampler2D", "isampler2DArray", "isampler2DMS", "isampler2DMSArray", "isampler2DRect", "isampler3D", "isamplerBuffer", "isamplerCube", "isamplerCubeArray", "isubpassInput", "isubpassInputMS", "itexture2D", "itexture2DArray", "itexture2DMS", "itexture2DMSArray", "itexture3D", "itextureBuffer", "itextureCube", "itextureCubeArray", "ivec2", "ivec3", "ivec4", "layout", "long", "lowp", "mat2", "mat2x2", "mat2x3", "mat2x4", "mat3", "mat3x2", "mat3x3", "mat3x4", "mat4", "mat4x2", "mat4x3", "mat4x4", "mediump", "namespace", "noinline", "noperspective", "out", "output", "partition", "patch", "precise", "precision", "public", "readonly", "resource", "restrict", "return", "sample", "sampler", "sampler1D", "sampler1DArray", "sampler1DArrayShadow", "sampler1DShadow", "sampler2D", "sampler2DArray", "sampler2DArrayShadow", "sampler2DMS", "sampler2DMSArray", "sampler2DRect", "sampler2DRectShadow", "sampler2DShadow", "sampler3D", "sampler3DRect", "samplerBuffer", "samplerCube", "samplerCubeArray", "samplerCubeArrayShadow", "samplerCubeShadow", "samplerShadow", "shared", "short", "sizeof", "smooth", "static", "struct", "subpassInput", "subpassInputMS", "subroutine", "superp", "switch", "template", "texture2D", "texture2DArray", "texture2DMS", "texture2DMSArray", "texture3D", "textureBuffer", "textureCube", "textureCubeArray", "this", "true", "typedef", "uimage1D", "uimage1DArray", "uimage2D", "uimage2DArray", "uimage2DMS", "uimage2DMSArray", "uimage2DRect", "uimage3D", "uimageBuffer", "uimageCube", "uimageCubeArray", "uint", "uniform", "union", "unsigned", "usampler1D", "usampler1DArray", "usampler2D", "usampler2DArray", "usampler2DMS", "usampler2DMSArray", "usampler2DRect", "usampler3D", "usamplerBuffer", "usamplerCube", "usamplerCubeArray", "using", "usubpassInput", "usubpassInputMS", "utexture2D", "utexture2DArray", "utexture2DMS", "utexture2DMSArray", "utexture3D", "utextureBuffer", "utextureCube", "utextureCubeArray", "uvec2", "uvec3", "uvec4", "varying", "vec2", "vec3", "vec4", "void", "volatile", "while", "writeonly"
|
||||
};
|
||||
|
||||
return ShaderAst::Sanitize(ast, options, error);
|
||||
}
|
||||
|
||||
void GlslWriter::Append(const ShaderAst::ExpressionType& type)
|
||||
{
|
||||
std::visit([&](auto&& arg)
|
||||
|
|
|
|||
|
|
@ -442,26 +442,24 @@ namespace Nz
|
|||
{
|
||||
}
|
||||
|
||||
std::vector<UInt32> SpirvWriter::Generate(ShaderAst::StatementPtr& shader, const States& states)
|
||||
std::vector<UInt32> SpirvWriter::Generate(ShaderAst::Statement& shader, const States& states)
|
||||
{
|
||||
ShaderAst::StatementPtr* targetAstPtr = &shader;
|
||||
ShaderAst::Statement* targetAst = &shader;
|
||||
|
||||
ShaderAst::StatementPtr sanitizedAst;
|
||||
if (!states.sanitized)
|
||||
{
|
||||
sanitizedAst = ShaderAst::Sanitize(shader);
|
||||
targetAstPtr = &sanitizedAst;
|
||||
targetAst = sanitizedAst.get();
|
||||
}
|
||||
|
||||
ShaderAst::StatementPtr optimizedAst;
|
||||
if (states.optimize)
|
||||
{
|
||||
optimizedAst = ShaderAst::Optimize(*targetAstPtr);
|
||||
targetAstPtr = &optimizedAst;
|
||||
optimizedAst = ShaderAst::Optimize(*targetAst);
|
||||
targetAst = optimizedAst.get();
|
||||
}
|
||||
|
||||
ShaderAst::StatementPtr& targetAst = *targetAstPtr;
|
||||
|
||||
m_context.states = &states;
|
||||
|
||||
State state;
|
||||
|
|
|
|||
|
|
@ -56,7 +56,7 @@ namespace Nz
|
|||
return pipelineLayout;
|
||||
}
|
||||
|
||||
std::shared_ptr<ShaderModule> VulkanDevice::InstantiateShaderModule(ShaderStageTypeFlags stages, ShaderAst::StatementPtr& shaderAst, const ShaderWriter::States& states)
|
||||
std::shared_ptr<ShaderModule> VulkanDevice::InstantiateShaderModule(ShaderStageTypeFlags stages, ShaderAst::Statement& shaderAst, const ShaderWriter::States& states)
|
||||
{
|
||||
auto stage = std::make_shared<VulkanShaderModule>();
|
||||
if (!stage->Create(*this, stages, shaderAst, states))
|
||||
|
|
|
|||
|
|
@ -57,7 +57,7 @@ namespace Nz
|
|||
};
|
||||
}
|
||||
|
||||
bool VulkanShaderModule::Create(Vk::Device& device, ShaderStageTypeFlags shaderStages, ShaderAst::StatementPtr& shaderAst, const ShaderWriter::States& states)
|
||||
bool VulkanShaderModule::Create(Vk::Device& device, ShaderStageTypeFlags shaderStages, ShaderAst::Statement& shaderAst, const ShaderWriter::States& states)
|
||||
{
|
||||
SpirvWriter::Environment env;
|
||||
|
||||
|
|
@ -80,7 +80,7 @@ namespace Nz
|
|||
case ShaderLanguage::NazaraBinary:
|
||||
{
|
||||
auto shader = ShaderAst::UnserializeShader(source, sourceSize);
|
||||
return Create(device, shaderStages, shader, {});
|
||||
return Create(device, shaderStages, *shader, {});
|
||||
}
|
||||
|
||||
case ShaderLanguage::NazaraShader:
|
||||
|
|
@ -89,7 +89,7 @@ namespace Nz
|
|||
|
||||
Nz::ShaderLang::Parser parser;
|
||||
Nz::ShaderAst::StatementPtr shaderAst = parser.Parse(tokens);
|
||||
return Create(device, shaderStages, shaderAst, states);
|
||||
return Create(device, shaderStages, *shaderAst, states);
|
||||
}
|
||||
|
||||
case ShaderLanguage::SpirV:
|
||||
|
|
|
|||
|
|
@ -872,7 +872,7 @@ std::unique_ptr<Nz::ShaderAst::DeclareFunctionStatement> ShaderGraph::ToFunction
|
|||
|
||||
qDebug() << shaderNode->name() << node->id();
|
||||
if (auto it = variableExpressions.find(BuildKey(node->id(), portIndex)); it != variableExpressions.end())
|
||||
return Nz::ShaderAst::Clone(it->second);
|
||||
return Nz::ShaderAst::Clone(*it->second);
|
||||
|
||||
auto it = usageCount.find(BuildKey(node->id(), portIndex));
|
||||
assert(it != usageCount.end());
|
||||
|
|
@ -925,7 +925,7 @@ std::unique_ptr<Nz::ShaderAst::DeclareFunctionStatement> ShaderGraph::ToFunction
|
|||
else
|
||||
varExpression = std::move(expression);
|
||||
|
||||
variableExpressions[BuildKey(node->id(), portIndex)] = Nz::ShaderAst::Clone(varExpression);
|
||||
variableExpressions[BuildKey(node->id(), portIndex)] = Nz::ShaderAst::Clone(*varExpression);
|
||||
|
||||
return varExpression;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -66,10 +66,10 @@ void CodeOutputWidget::Refresh()
|
|||
|
||||
if (m_optimisationCheckbox->isChecked())
|
||||
{
|
||||
shaderAst = Nz::ShaderAst::Sanitize(shaderAst);
|
||||
shaderAst = Nz::ShaderAst::Sanitize(*shaderAst);
|
||||
|
||||
Nz::ShaderAst::AstOptimizer optimiser;
|
||||
shaderAst = optimiser.Optimise(shaderAst, enabledConditions);
|
||||
shaderAst = optimiser.Optimise(*shaderAst, enabledConditions);
|
||||
}
|
||||
|
||||
Nz::ShaderWriter::States states;
|
||||
|
|
@ -82,21 +82,21 @@ void CodeOutputWidget::Refresh()
|
|||
case OutputLanguage::GLSL:
|
||||
{
|
||||
Nz::GlslWriter writer;
|
||||
output = writer.Generate(ShaderGraph::ToShaderStageType(m_shaderGraph.GetType()), shaderAst, states);
|
||||
output = writer.Generate(ShaderGraph::ToShaderStageType(m_shaderGraph.GetType()), *shaderAst, bindingMapping, states);
|
||||
break;
|
||||
}
|
||||
|
||||
case OutputLanguage::Nazalang:
|
||||
{
|
||||
Nz::LangWriter writer;
|
||||
output = writer.Generate(shaderAst, states);
|
||||
output = writer.Generate(*shaderAst, states);
|
||||
break;
|
||||
}
|
||||
|
||||
case OutputLanguage::SpirV:
|
||||
{
|
||||
Nz::SpirvWriter writer;
|
||||
std::vector<std::uint32_t> spirv = writer.Generate(shaderAst, states);
|
||||
std::vector<std::uint32_t> spirv = writer.Generate(*shaderAst, states);
|
||||
|
||||
Nz::SpirvPrinter printer;
|
||||
output = printer.Print(spirv.data(), spirv.size());
|
||||
|
|
|
|||
Loading…
Reference in New Issue