Shader: Add AstReflect

This commit is contained in:
Jérôme Leclercq
2021-04-17 18:11:16 +02:00
parent 500ccda85a
commit 2238bbfa0c
6 changed files with 118 additions and 14 deletions

View File

@@ -0,0 +1,25 @@
// 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/Ast/AstReflect.hpp>
#include <stdexcept>
#include <Nazara/Shader/Debug.hpp>
namespace Nz::ShaderAst
{
void AstReflect::Reflect(const StatementPtr& statement, const Callbacks& callbacks)
{
assert(statement);
m_callbacks = &callbacks;
statement->Visit(*this);
}
void AstReflect::Visit(DeclareOptionStatement& node)
{
assert(m_callbacks);
if (m_callbacks->onOptionDeclaration)
m_callbacks->onOptionDeclaration(node.optName, node.optType);
}
}

View File

@@ -113,6 +113,7 @@ 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

View File

@@ -386,7 +386,11 @@ namespace Nz
std::vector<UInt32> SpirvWriter::Generate(ShaderAst::StatementPtr& shader, const States& states)
{
ShaderAst::StatementPtr sanitizedAst = ShaderAst::Sanitize(shader);
ShaderAst::StatementPtr sanitizedAst;
if (!states.sanitized)
sanitizedAst = ShaderAst::Sanitize(shader);
ShaderAst::StatementPtr& targetAst = (states.sanitized) ? shader : sanitizedAst;
m_context.states = &states;
@@ -399,7 +403,7 @@ namespace Nz
// Register all extended instruction sets
PreVisitor preVisitor(states, state.constantTypeCache, state.funcs);
sanitizedAst->Visit(preVisitor);
targetAst->Visit(preVisitor);
m_currentState->preVisitor = &preVisitor;
@@ -407,7 +411,7 @@ namespace Nz
state.extensionInstructions[extInst] = AllocateResultId();
SpirvAstVisitor visitor(*this, state.instructions, state.funcs);
sanitizedAst->Visit(visitor);
targetAst->Visit(visitor);
AppendHeader();