Shader/Modules: proof of concept

This commit is contained in:
Jérôme Leclercq
2022-03-05 04:07:18 +01:00
parent 7dab1d735f
commit 43ac86e85c
29 changed files with 667 additions and 192 deletions

View File

@@ -144,7 +144,7 @@ namespace Nz
return std::make_shared<OpenGLRenderPipelineLayout>(std::move(pipelineLayoutInfo));
}
std::shared_ptr<ShaderModule> OpenGLDevice::InstantiateShaderModule(ShaderStageTypeFlags shaderStages, ShaderAst::Module& shaderModule, const ShaderWriter::States& states)
std::shared_ptr<ShaderModule> OpenGLDevice::InstantiateShaderModule(ShaderStageTypeFlags shaderStages, const ShaderAst::Module& shaderModule, const ShaderWriter::States& states)
{
return std::make_shared<OpenGLShaderModule>(*this, shaderStages, shaderModule, states);
}

View File

@@ -13,7 +13,7 @@
namespace Nz
{
OpenGLShaderModule::OpenGLShaderModule(OpenGLDevice& device, ShaderStageTypeFlags shaderStages, ShaderAst::Module& shaderModule, const ShaderWriter::States& states) :
OpenGLShaderModule::OpenGLShaderModule(OpenGLDevice& device, ShaderStageTypeFlags shaderStages, const ShaderAst::Module& shaderModule, const ShaderWriter::States& states) :
m_device(device)
{
NazaraAssert(shaderStages != 0, "at least one shader stage must be specified");
@@ -137,7 +137,7 @@ namespace Nz
return stageFlags;
}
void OpenGLShaderModule::Create(OpenGLDevice& /*device*/, ShaderStageTypeFlags shaderStages, ShaderAst::Module& shaderModule, const ShaderWriter::States& states)
void OpenGLShaderModule::Create(OpenGLDevice& /*device*/, ShaderStageTypeFlags shaderStages, const ShaderAst::Module& shaderModule, const ShaderWriter::States& states)
{
m_states = states;
m_states.sanitized = true; //< Shader is always sanitized (because of keywords)

View File

@@ -81,6 +81,7 @@ namespace Nz::ShaderAst
{
auto clone = std::make_unique<DeclareConstStatement>();
clone->constIndex = node.constIndex;
clone->hidden = node.hidden;
clone->name = node.name;
clone->type = Clone(node.type);
clone->expression = CloneExpression(node.expression);
@@ -92,6 +93,7 @@ namespace Nz::ShaderAst
{
auto clone = std::make_unique<DeclareExternalStatement>();
clone->bindingSet = Clone(node.bindingSet);
clone->hidden = node.hidden;
clone->externalVars.reserve(node.externalVars.size());
for (const auto& var : node.externalVars)
@@ -114,9 +116,9 @@ namespace Nz::ShaderAst
clone->earlyFragmentTests = Clone(node.earlyFragmentTests);
clone->entryStage = Clone(node.entryStage);
clone->funcIndex = node.funcIndex;
clone->hidden = node.hidden;
clone->name = node.name;
clone->returnType = Clone(node.returnType);
clone->varIndex = node.varIndex;
clone->parameters.reserve(node.parameters.size());
for (auto& parameter : node.parameters)
@@ -124,6 +126,7 @@ namespace Nz::ShaderAst
auto& cloneParam = clone->parameters.emplace_back();
cloneParam.name = parameter.name;
cloneParam.type = Clone(parameter.type);
cloneParam.varIndex = parameter.varIndex;
}
clone->statements.reserve(node.statements.size());
@@ -137,6 +140,7 @@ namespace Nz::ShaderAst
{
auto clone = std::make_unique<DeclareOptionStatement>();
clone->defaultValue = CloneExpression(node.defaultValue);
clone->hidden = node.hidden;
clone->optIndex = node.optIndex;
clone->optName = node.optName;
clone->optType = Clone(node.optType);
@@ -147,8 +151,9 @@ namespace Nz::ShaderAst
StatementPtr AstCloner::Clone(DeclareStructStatement& node)
{
auto clone = std::make_unique<DeclareStructStatement>();
clone->structIndex = node.structIndex;
clone->hidden = node.hidden;
clone->isExported = Clone(node.isExported);
clone->structIndex = node.structIndex;
clone->description.layout = Clone(node.description.layout);
clone->description.name = node.description.name;
@@ -170,10 +175,10 @@ namespace Nz::ShaderAst
StatementPtr AstCloner::Clone(DeclareVariableStatement& node)
{
auto clone = std::make_unique<DeclareVariableStatement>();
clone->initialExpression = CloneExpression(node.initialExpression);
clone->varIndex = node.varIndex;
clone->varName = node.varName;
clone->varType = Clone(node.varType);
clone->initialExpression = CloneExpression(node.initialExpression);
return clone;
}

View File

@@ -195,6 +195,7 @@ namespace Nz::ShaderAst
void AstSerializerBase::Serialize(DeclareExternalStatement& node)
{
OptVal(node.hidden);
ExprValue(node.bindingSet);
Container(node.externalVars);
@@ -211,6 +212,7 @@ namespace Nz::ShaderAst
void AstSerializerBase::Serialize(DeclareConstStatement& node)
{
OptVal(node.constIndex);
OptVal(node.hidden);
Value(node.name);
ExprValue(node.type);
Node(node.expression);
@@ -224,13 +226,14 @@ namespace Nz::ShaderAst
ExprValue(node.earlyFragmentTests);
ExprValue(node.entryStage);
OptVal(node.funcIndex);
OptVal(node.varIndex);
OptVal(node.hidden);
Container(node.parameters);
for (auto& parameter : node.parameters)
{
Value(parameter.name);
ExprValue(parameter.type);
OptVal(parameter.varIndex);
}
Container(node.statements);
@@ -241,6 +244,7 @@ namespace Nz::ShaderAst
void AstSerializerBase::Serialize(DeclareOptionStatement& node)
{
OptVal(node.optIndex);
OptVal(node.hidden);
Value(node.optName);
ExprValue(node.optType);
Node(node.defaultValue);
@@ -249,6 +253,7 @@ namespace Nz::ShaderAst
void AstSerializerBase::Serialize(DeclareStructStatement& node)
{
OptVal(node.structIndex);
OptVal(node.hidden);
ExprValue(node.isExported);
Value(node.description.name);

View File

@@ -85,13 +85,13 @@ namespace Nz::ShaderAst
// Register struct used in parameters or return type
if (!node.parameters.empty())
{
assert(node.varIndex);
std::size_t parameterVarIndex = *node.varIndex;
for (auto& parameter : node.parameters)
{
assert(parameter.varIndex);
// Since parameters must always be defined, their type isn't a dependency of parameter variables
assert(m_variableUsages.find(parameterVarIndex) == m_variableUsages.end());
m_variableUsages.emplace(parameterVarIndex, UsageSet{});
assert(m_variableUsages.find(*parameter.varIndex) == m_variableUsages.end());
m_variableUsages.emplace(*parameter.varIndex, UsageSet{});
const auto& exprType = parameter.type.GetResultingValue();
if (IsStructType(exprType))
@@ -99,8 +99,6 @@ namespace Nz::ShaderAst
std::size_t structIndex = std::get<ShaderAst::StructType>(exprType).structIndex;
usageSet.usedStructs.UnboundedSet(structIndex);
}
++parameterVarIndex;
}
}

View File

@@ -0,0 +1,177 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Shader module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Shader/Ast/IndexRemapperVisitor.hpp>
#include <Nazara/Core/Algorithm.hpp>
#include <unordered_map>
#include <Nazara/Shader/Debug.hpp>
namespace Nz::ShaderAst
{
namespace
{
template<typename T> const T& Retrieve(const std::unordered_map<std::size_t, T>& map, std::size_t id)
{
auto it = map.find(id);
assert(it != map.end());
return it->second;
}
template<typename T, typename U> void UniqueInsert(std::unordered_map<T, U>& map, T key, U value)
{
assert(map.find(key) == map.end());
map.emplace(std::move(key), std::move(value));
}
template<typename T, typename U>
std::unique_ptr<T> static_unique_pointer_cast(std::unique_ptr<U>&& ptr)
{
return std::unique_ptr<T>(SafeCast<T*>(ptr.release()));
}
}
struct IndexRemapperVisitor::Context
{
const IndexRemapperVisitor::Callbacks* callbacks;
std::unordered_map<std::size_t, std::size_t> newConstIndices;
std::unordered_map<std::size_t, std::size_t> newFuncIndices;
std::unordered_map<std::size_t, std::size_t> newStructIndices;
std::unordered_map<std::size_t, std::size_t> newVarIndices;
};
StatementPtr IndexRemapperVisitor::Clone(Statement& statement, const Callbacks& callbacks)
{
assert(callbacks.constIndexGenerator);
assert(callbacks.funcIndexGenerator);
assert(callbacks.structIndexGenerator);
//assert(callbacks.typeIndexGenerator);
assert(callbacks.varIndexGenerator);
Context context;
context.callbacks = &callbacks;
m_context = &context;
return AstCloner::Clone(statement);
}
StatementPtr IndexRemapperVisitor::Clone(DeclareConstStatement& node)
{
DeclareConstStatementPtr clone = static_unique_pointer_cast<DeclareConstStatement>(AstCloner::Clone(node));
assert(clone->constIndex);
std::size_t newConstIndex = m_context->callbacks->constIndexGenerator(*clone->constIndex);
UniqueInsert(m_context->newConstIndices, *clone->constIndex, newConstIndex);
clone->constIndex = newConstIndex;
return clone;
}
StatementPtr IndexRemapperVisitor::Clone(DeclareExternalStatement& node)
{
DeclareExternalStatementPtr clone = static_unique_pointer_cast<DeclareExternalStatement>(AstCloner::Clone(node));
for (auto& extVar : clone->externalVars)
{
assert(extVar.varIndex);
std::size_t newVarIndex = m_context->callbacks->varIndexGenerator(*extVar.varIndex);
UniqueInsert(m_context->newVarIndices, *extVar.varIndex, newVarIndex);
extVar.varIndex = newVarIndex;
}
return clone;
}
StatementPtr IndexRemapperVisitor::Clone(DeclareFunctionStatement& node)
{
DeclareFunctionStatementPtr clone = static_unique_pointer_cast<DeclareFunctionStatement>(AstCloner::Clone(node));
assert(clone->funcIndex);
std::size_t newFuncIndex = m_context->callbacks->funcIndexGenerator(*clone->funcIndex);
UniqueInsert(m_context->newFuncIndices, *clone->funcIndex, newFuncIndex);
clone->funcIndex = newFuncIndex;
if (!clone->parameters.empty())
{
for (auto& parameter : node.parameters)
{
assert(parameter.varIndex);
parameter.varIndex = Retrieve(m_context->newVarIndices, *parameter.varIndex);
HandleType(parameter.type);
}
}
if (node.returnType.HasValue())
HandleType(node.returnType);
return clone;
}
StatementPtr IndexRemapperVisitor::Clone(DeclareStructStatement& node)
{
DeclareStructStatementPtr clone = static_unique_pointer_cast<DeclareStructStatement>(AstCloner::Clone(node));
assert(clone->structIndex);
std::size_t newStructIndex = m_context->callbacks->structIndexGenerator(*clone->structIndex);
UniqueInsert(m_context->newStructIndices, *clone->structIndex, newStructIndex);
clone->structIndex = newStructIndex;
for (auto& structMember : clone->description.members)
HandleType(structMember.type);
return clone;
}
StatementPtr IndexRemapperVisitor::Clone(DeclareVariableStatement& node)
{
DeclareVariableStatementPtr clone = static_unique_pointer_cast<DeclareVariableStatement>(AstCloner::Clone(node));
assert(clone->varIndex);
std::size_t newVarIndex = m_context->callbacks->varIndexGenerator(*clone->varIndex);
UniqueInsert(m_context->newConstIndices, *clone->varIndex, newVarIndex);
clone->varIndex = newVarIndex;
HandleType(node.varType);
return clone;
}
ExpressionPtr IndexRemapperVisitor::Clone(CallFunctionExpression& node)
{
CallFunctionExpressionPtr clone = static_unique_pointer_cast<CallFunctionExpression>(AstCloner::Clone(node));
const auto& targetFuncType = GetExpressionType(*node.targetFunction);
if (std::holds_alternative<FunctionType>(targetFuncType))
{
const auto& funcType = std::get<FunctionType>(targetFuncType);
FunctionType newFunc;
newFunc.funcIndex = Retrieve(m_context->newFuncIndices, funcType.funcIndex);
clone->cachedExpressionType = ExpressionType{ newFunc }; //< FIXME We should add FunctionExpression like VariableExpression to handle this
}
return clone;
}
ExpressionPtr IndexRemapperVisitor::Clone(VariableExpression& node)
{
VariableExpressionPtr clone = static_unique_pointer_cast<VariableExpression>(AstCloner::Clone(node));
assert(clone->variableId);
clone->variableId = Retrieve(m_context->newVarIndices, clone->variableId);
return clone;
}
void IndexRemapperVisitor::HandleType(ExpressionValue<ExpressionType>& exprType)
{
const auto& resultingType = exprType.GetResultingValue();
if (IsStructType(resultingType))
{
std::size_t newStructIndex = Retrieve(m_context->newStructIndices, std::get<StructType>(resultingType).structIndex);
exprType = ExpressionType{ StructType{ newStructIndex } };
}
}
}

View File

@@ -9,9 +9,14 @@
#include <Nazara/Core/StackVector.hpp>
#include <Nazara/Shader/ShaderBuilder.hpp>
#include <Nazara/Shader/Ast/AstConstantPropagationVisitor.hpp>
#include <Nazara/Shader/Ast/AstExportVisitor.hpp>
#include <Nazara/Shader/Ast/AstRecursiveVisitor.hpp>
#include <Nazara/Shader/Ast/AstUtils.hpp>
#include <Nazara/Shader/Ast/DependencyCheckerVisitor.hpp>
#include <Nazara/Shader/Ast/EliminateUnusedPassVisitor.hpp>
#include <Nazara/Shader/Ast/IndexRemapperVisitor.hpp>
#include <numeric>
#include <sstream>
#include <stdexcept>
#include <unordered_set>
#include <Nazara/Shader/Debug.hpp>
@@ -46,6 +51,7 @@ namespace Nz::ShaderAst
struct IdentifierData
{
Bitset<UInt64> availableIndices;
Bitset<UInt64> preregisteredIndices;
std::unordered_map<std::size_t, T> values;
template<typename U>
@@ -53,18 +59,21 @@ namespace Nz::ShaderAst
{
std::size_t dataIndex;
if (index.has_value())
dataIndex = *index;
else
{
dataIndex = availableIndices.FindFirst();
if (dataIndex == availableIndices.npos)
dataIndex = availableIndices.GetSize();
}
dataIndex = *index;
if (dataIndex >= availableIndices.GetSize())
availableIndices.Resize(dataIndex + 1, true);
else if (!availableIndices.Test(dataIndex))
throw AstError{ "index " + std::to_string(dataIndex) + " is already used" };
if (dataIndex >= availableIndices.GetSize())
availableIndices.Resize(dataIndex + 1, true);
else if (!availableIndices.Test(dataIndex))
{
if (preregisteredIndices.UnboundedTest(dataIndex))
preregisteredIndices.Reset(dataIndex);
else
throw AstError{ "index " + std::to_string(dataIndex) + " is already used" };
}
}
else
dataIndex = RegisterNewIndex();
assert(values.find(dataIndex) == values.end());
@@ -73,6 +82,23 @@ namespace Nz::ShaderAst
return dataIndex;
}
std::size_t RegisterNewIndex(bool preregister = false)
{
std::size_t index = availableIndices.FindFirst();
if (index == availableIndices.npos)
{
index = availableIndices.GetSize();
availableIndices.Resize(index + 1, true);
}
availableIndices.Set(index, false);
if (preregister)
preregisteredIndices.UnboundedSet(index);
return index;
}
T& Retrieve(std::size_t index)
{
auto it = values.find(index);
@@ -94,11 +120,13 @@ namespace Nz::ShaderAst
std::size_t previousSize;
};
std::size_t nextOptionIndex = 0;
Options options;
std::array<DeclareFunctionStatement*, ShaderStageTypeCount> entryFunctions = {};
std::optional<DependencyCheckerVisitor::UsageSet> importUsage;
std::size_t nextOptionIndex = 0;
std::vector<Identifier> identifiersInScope;
std::vector<PendingFunction> pendingFunctions;
std::vector<Scope> scopes;
std::vector<StatementPtr>* currentStatementList = nullptr;
std::unordered_set<std::string> declaredExternalVar;
std::unordered_set<UInt64> usedBindingIndexes;
IdentifierData<ConstantValue> constantValues;
@@ -107,9 +135,8 @@ namespace Nz::ShaderAst
IdentifierData<StructDescription*> structs;
IdentifierData<std::variant<ExpressionType, PartialType>> types;
IdentifierData<ExpressionType> variableTypes;
std::vector<Scope> scopes;
Options options;
CurrentFunctionData* currentFunction = nullptr;
std::vector<StatementPtr>* currentStatementList = nullptr;
};
ModulePtr SanitizeVisitor::Sanitize(const Module& module, const Options& options, std::string* error)
@@ -769,7 +796,10 @@ namespace Nz::ShaderAst
clone->type = expressionType;
clone->constIndex = RegisterConstant(clone->name, value, clone->constIndex);
if (m_context->importUsage.has_value())
clone->hidden = true;
clone->constIndex = RegisterConstant(clone->name, value, clone->hidden.value_or(false), clone->constIndex);
if (m_context->options.removeConstDeclaration)
return ShaderBuilder::NoOp();
@@ -783,6 +813,13 @@ namespace Nz::ShaderAst
auto clone = static_unique_pointer_cast<DeclareExternalStatement>(AstCloner::Clone(node));
if (m_context->importUsage.has_value())
{
// Since unused variables have been removed when importing a module, every variable should be used
assert(!clone->externalVars.empty());
clone->hidden = !m_context->importUsage->usedVariables.UnboundedTest(*clone->externalVars.front().varIndex);
}
UInt32 defaultBlockSet = 0;
if (clone->bindingSet.HasValue())
defaultBlockSet = ComputeExprValue(clone->bindingSet);
@@ -823,7 +860,7 @@ namespace Nz::ShaderAst
throw AstError{ "external variable " + extVar.name + " is of wrong type: only uniform and sampler are allowed in external blocks" };
extVar.type = std::move(resolvedType);
extVar.varIndex = RegisterVariable(extVar.name, std::move(varType), extVar.varIndex);
extVar.varIndex = RegisterVariable(extVar.name, std::move(varType), clone->hidden.value_or(false), extVar.varIndex);
SanitizeIdentifier(extVar.name);
}
@@ -881,6 +918,12 @@ namespace Nz::ShaderAst
}
}
if (m_context->importUsage.has_value())
{
assert(clone->funcIndex);
clone->hidden = !m_context->importUsage->usedStructs.UnboundedTest(*clone->funcIndex);
}
// Function content is resolved in a second pass
auto& pendingFunc = m_context->pendingFunctions.emplace_back();
pendingFunc.cloneNode = clone.get();
@@ -895,7 +938,7 @@ namespace Nz::ShaderAst
FunctionData funcData;
funcData.node = clone.get(); //< update function node
std::size_t funcIndex = RegisterFunction(clone->name, std::move(funcData), node.funcIndex);
std::size_t funcIndex = RegisterFunction(clone->name, std::move(funcData), clone->hidden.value_or(false), node.funcIndex);
clone->funcIndex = funcIndex;
SanitizeIdentifier(clone->name);
@@ -919,10 +962,13 @@ namespace Nz::ShaderAst
std::size_t optionIndex = m_context->nextOptionIndex++;
if (m_context->importUsage.has_value())
clone->hidden = true;
if (auto optionValueIt = m_context->options.optionValues.find(optionIndex); optionValueIt != m_context->options.optionValues.end())
clone->optIndex = RegisterConstant(clone->optName, optionValueIt->second, clone->optIndex);
clone->optIndex = RegisterConstant(clone->optName, optionValueIt->second, clone->hidden.value_or(false), clone->optIndex);
else if (clone->defaultValue)
clone->optIndex = RegisterConstant(clone->optName, ComputeConstantValue(*clone->defaultValue), clone->optIndex);
clone->optIndex = RegisterConstant(clone->optName, ComputeConstantValue(*clone->defaultValue), clone->hidden.value_or(false), clone->optIndex);
else
throw AstError{ "missing option " + clone->optName + " value (has no default value)" };
@@ -942,6 +988,12 @@ namespace Nz::ShaderAst
if (clone->isExported.HasValue())
clone->isExported = ComputeExprValue(clone->isExported);
if (m_context->importUsage.has_value())
{
assert(clone->structIndex);
clone->hidden = !m_context->importUsage->usedStructs.UnboundedTest(*clone->structIndex);
}
std::unordered_set<std::string> declaredMembers;
for (auto& member : clone->description.members)
{
@@ -980,7 +1032,7 @@ namespace Nz::ShaderAst
member.type = std::move(resolvedType);
}
clone->structIndex = RegisterStruct(clone->description.name, &clone->description, clone->structIndex);
clone->structIndex = RegisterStruct(clone->description.name, &clone->description, clone->hidden.value_or(false), clone->structIndex);
SanitizeIdentifier(clone->description.name);
@@ -1310,7 +1362,102 @@ namespace Nz::ShaderAst
StatementPtr SanitizeVisitor::Clone(ImportStatement& node)
{
// Nested import is handled separately
assert(!m_context->importUsage);
if (!m_context->options.moduleCallback)
return static_unique_pointer_cast<ImportStatement>(AstCloner::Clone(node));
auto ModulePathAsString = [&]() -> std::string
{
std::ostringstream ss;
bool first = true;
for (const std::string& part : node.modulePath)
{
if (!first)
ss << "/";
ss << part;
first = false;
}
return ss.str();
};
ModulePtr targetModule = m_context->options.moduleCallback(node.modulePath);
if (!targetModule)
throw AstError{ "module " + ModulePathAsString() + " not found" };
std::string error;
ModulePtr sanitizedModule = ShaderAst::Sanitize(*targetModule, m_context->options, &error);
if (!sanitizedModule)
throw AstError{ "module " + ModulePathAsString() + " compilation failed: " + error };
// Extract exported nodes and their dependencies
DependencyCheckerVisitor::Config depConfig;
depConfig.usedShaderStages.Clear();
DependencyCheckerVisitor moduleDependencies;
moduleDependencies.Process(*sanitizedModule->rootNode, depConfig);
DependencyCheckerVisitor::UsageSet exportedSet;
AstExportVisitor::Callbacks callbacks;
callbacks.onExportedStruct = [&](DeclareStructStatement& node)
{
assert(node.structIndex);
moduleDependencies.MarkStructAsUsed(*node.structIndex);
exportedSet.usedStructs.UnboundedSet(*node.structIndex);
};
AstExportVisitor exportVisitor;
exportVisitor.Visit(*sanitizedModule->rootNode, callbacks);
moduleDependencies.Resolve();
auto statementPtr = EliminateUnusedPass(*sanitizedModule->rootNode, moduleDependencies.GetUsage());
DependencyCheckerVisitor::UsageSet remappedExportedSet;
IndexRemapperVisitor::Callbacks remapCallbacks;
remapCallbacks.constIndexGenerator = [this](std::size_t previousIndex) { return m_context->constantValues.RegisterNewIndex(true); };
remapCallbacks.funcIndexGenerator = [&](std::size_t previousIndex)
{
std::size_t newIndex = m_context->functions.RegisterNewIndex(true);
if (exportedSet.usedFunctions.Test(previousIndex))
remappedExportedSet.usedFunctions.UnboundedSet(newIndex);
return newIndex;
};
remapCallbacks.structIndexGenerator = [&](std::size_t previousIndex)
{
std::size_t newIndex = m_context->structs.RegisterNewIndex(true);
if (exportedSet.usedStructs.Test(previousIndex))
remappedExportedSet.usedStructs.UnboundedSet(newIndex);
return newIndex;
};
remapCallbacks.varIndexGenerator = [&](std::size_t previousIndex)
{
std::size_t newIndex = m_context->variableTypes.RegisterNewIndex(true);
if (exportedSet.usedVariables.Test(previousIndex))
remappedExportedSet.usedVariables.UnboundedSet(newIndex);
return newIndex;
};
statementPtr = RemapIndices(*statementPtr, remapCallbacks);
// Register exported variables (FIXME: This shouldn't be necessary and could be handled by the IndexRemapperVisitor)
m_context->importUsage = remappedExportedSet;
CallOnExit restoreImportOnExit([&] { m_context->importUsage.reset(); });
return AstCloner::Clone(*statementPtr);
}
StatementPtr SanitizeVisitor::Clone(MultiStatement& node)
@@ -1694,131 +1841,151 @@ namespace Nz::ShaderAst
RegisterIntrinsic("reflect", IntrinsicType::Reflect);
}
std::size_t SanitizeVisitor::RegisterConstant(std::string name, ConstantValue value, std::optional<std::size_t> index)
std::size_t SanitizeVisitor::RegisterConstant(std::string name, ConstantValue value, bool hidden, std::optional<std::size_t> index)
{
if (FindIdentifier(name))
throw AstError{ name + " is already used" };
std::size_t constantIndex = m_context->constantValues.Register(std::move(value), index);
if (!hidden)
{
if (FindIdentifier(name))
throw AstError{ name + " is already used" };
m_context->identifiersInScope.push_back({
std::move(name),
constantIndex,
Identifier::Type::Constant
});
m_context->identifiersInScope.push_back({
std::move(name),
constantIndex,
Identifier::Type::Constant
});
}
return constantIndex;
}
std::size_t SanitizeVisitor::RegisterFunction(std::string name, FunctionData funcData, std::optional<std::size_t> index)
std::size_t SanitizeVisitor::RegisterFunction(std::string name, FunctionData funcData, bool hidden, std::optional<std::size_t> index)
{
if (auto* identifier = FindIdentifier(name))
{
bool duplicate = true;
// Functions cannot be declared twice, except for entry ones if their stages are different
if (funcData.node->entryStage.HasValue() && identifier->type == Identifier::Type::Function)
{
auto& otherFunction = m_context->functions.Retrieve(identifier->index);
if (funcData.node->entryStage.GetResultingValue() != otherFunction.node->entryStage.GetResultingValue())
duplicate = false;
}
if (duplicate)
throw AstError{ funcData.node->name + " is already used" };
}
std::size_t functionIndex = m_context->functions.Register(std::move(funcData), index);
m_context->identifiersInScope.push_back({
std::move(name),
functionIndex,
Identifier::Type::Function
});
if (!hidden)
{
if (auto* identifier = FindIdentifier(name))
{
bool duplicate = true;
// Functions cannot be declared twice, except for entry ones if their stages are different
if (funcData.node->entryStage.HasValue() && identifier->type == Identifier::Type::Function)
{
auto& otherFunction = m_context->functions.Retrieve(identifier->index);
if (funcData.node->entryStage.GetResultingValue() != otherFunction.node->entryStage.GetResultingValue())
duplicate = false;
}
if (duplicate)
throw AstError{ funcData.node->name + " is already used" };
}
m_context->identifiersInScope.push_back({
std::move(name),
functionIndex,
Identifier::Type::Function
});
}
return functionIndex;
}
std::size_t SanitizeVisitor::RegisterIntrinsic(std::string name, IntrinsicType type, std::optional<std::size_t> index)
std::size_t SanitizeVisitor::RegisterIntrinsic(std::string name, IntrinsicType type, bool hidden, std::optional<std::size_t> index)
{
if (FindIdentifier(name))
throw AstError{ name + " is already used" };
std::size_t intrinsicIndex = m_context->intrinsics.Register(std::move(type), index);
m_context->identifiersInScope.push_back({
std::move(name),
intrinsicIndex,
Identifier::Type::Intrinsic
});
if (!hidden)
{
if (FindIdentifier(name))
throw AstError{ name + " is already used" };
m_context->identifiersInScope.push_back({
std::move(name),
intrinsicIndex,
Identifier::Type::Intrinsic
});
}
return intrinsicIndex;
}
std::size_t SanitizeVisitor::RegisterStruct(std::string name, StructDescription* description, std::optional<std::size_t> index)
std::size_t SanitizeVisitor::RegisterStruct(std::string name, StructDescription* description, bool hidden, std::optional<std::size_t> index)
{
if (FindIdentifier(name))
throw AstError{ name + " is already used" };
std::size_t structIndex = m_context->structs.Register(description, index);
m_context->identifiersInScope.push_back({
std::move(name),
structIndex,
Identifier::Type::Struct
});
if (!hidden)
{
if (FindIdentifier(name))
throw AstError{ name + " is already used" };
m_context->identifiersInScope.push_back({
std::move(name),
structIndex,
Identifier::Type::Struct
});
}
return structIndex;
}
std::size_t SanitizeVisitor::RegisterType(std::string name, ExpressionType expressionType, std::optional<std::size_t> index)
std::size_t SanitizeVisitor::RegisterType(std::string name, ExpressionType expressionType, bool hidden, std::optional<std::size_t> index)
{
if (FindIdentifier(name))
throw AstError{ name + " is already used" };
std::size_t typeIndex = m_context->types.Register(std::move(expressionType), index);
m_context->identifiersInScope.push_back({
std::move(name),
typeIndex,
Identifier::Type::Type
});
return typeIndex;
}
std::size_t SanitizeVisitor::RegisterType(std::string name, PartialType partialType, std::optional<std::size_t> index)
{
if (FindIdentifier(name))
throw AstError{ name + " is already used" };
std::size_t typeIndex = m_context->types.Register(std::move(partialType), index);
m_context->identifiersInScope.push_back({
std::move(name),
typeIndex,
Identifier::Type::Type
});
return typeIndex;
}
std::size_t SanitizeVisitor::RegisterVariable(std::string name, ExpressionType type, std::optional<std::size_t> index)
{
if (auto* identifier = FindIdentifier(name))
if (!hidden)
{
// Allow variable shadowing
if (identifier->type != Identifier::Type::Variable)
if (FindIdentifier(name))
throw AstError{ name + " is already used" };
m_context->identifiersInScope.push_back({
std::move(name),
typeIndex,
Identifier::Type::Type
});
}
return typeIndex;
}
std::size_t SanitizeVisitor::RegisterType(std::string name, PartialType partialType, bool hidden, std::optional<std::size_t> index)
{
std::size_t typeIndex = m_context->types.Register(std::move(partialType), index);
if (!hidden)
{
if (FindIdentifier(name))
throw AstError{ name + " is already used" };
m_context->identifiersInScope.push_back({
std::move(name),
typeIndex,
Identifier::Type::Type
});
}
return typeIndex;
}
std::size_t SanitizeVisitor::RegisterVariable(std::string name, ExpressionType type, bool hidden, std::optional<std::size_t> index)
{
std::size_t varIndex = m_context->variableTypes.Register(std::move(type), index);
m_context->identifiersInScope.push_back({
std::move(name),
varIndex,
Identifier::Type::Variable
});
if (!hidden)
{
if (auto* identifier = FindIdentifier(name))
{
// Allow variable shadowing
if (identifier->type != Identifier::Type::Variable)
throw AstError{ name + " is already used" };
}
m_context->identifiersInScope.push_back({
std::move(name),
varIndex,
Identifier::Type::Variable
});
}
return varIndex;
}
@@ -1830,16 +1997,10 @@ namespace Nz::ShaderAst
{
PushScope();
std::optional<std::size_t> varIndex = pendingFunc.cloneNode->varIndex;
for (auto& parameter : pendingFunc.cloneNode->parameters)
{
std::size_t index = RegisterVariable(parameter.name, parameter.type.GetResultingValue(), varIndex);
if (!pendingFunc.cloneNode->varIndex)
pendingFunc.cloneNode->varIndex = index; //< First parameter variable index is node variable index
parameter.varIndex = RegisterVariable(parameter.name, parameter.type.GetResultingValue(), false, parameter.varIndex);
SanitizeIdentifier(parameter.name);
if (varIndex)
(*varIndex)++;
}
Context::CurrentFunctionData tempFuncData;
@@ -2304,7 +2465,7 @@ namespace Nz::ShaderAst
TypeMustMatch(resolvedType, GetExpressionType(*node.initialExpression));
}
node.varIndex = RegisterVariable(node.varName, resolvedType, node.varIndex);
node.varIndex = RegisterVariable(node.varName, resolvedType, false, node.varIndex);
node.varType = std::move(resolvedType);
if (m_context->options.makeVariableNameUnique)

View File

@@ -152,7 +152,7 @@ namespace Nz
unsigned int indentLevel = 0;
};
std::string GlslWriter::Generate(std::optional<ShaderStageType> shaderStage, ShaderAst::Module& module, const BindingMapping& bindingMapping, const States& states)
std::string GlslWriter::Generate(std::optional<ShaderStageType> shaderStage, const ShaderAst::Module& module, const BindingMapping& bindingMapping, const States& states)
{
State state(bindingMapping);
state.optionValues = states.optionValues;
@@ -210,7 +210,7 @@ namespace Nz
return s_flipYUniformName;
}
ShaderAst::ModulePtr GlslWriter::Sanitize(ShaderAst::Module& module, std::unordered_map<std::size_t, ShaderAst::ConstantValue> optionValues, std::string* error)
ShaderAst::ModulePtr GlslWriter::Sanitize(const ShaderAst::Module& module, std::unordered_map<std::size_t, ShaderAst::ConstantValue> optionValues, std::string* error)
{
// Always sanitize for reserved identifiers
ShaderAst::SanitizeVisitor::Options options;
@@ -604,12 +604,11 @@ namespace Nz
{
if (!m_currentState->inputFields.empty())
{
assert(node.varIndex);
assert(!node.parameters.empty());
auto& parameter = node.parameters.front();
const std::string& varName = parameter.name;
RegisterVariable(*node.varIndex, varName);
RegisterVariable(*parameter.varIndex, varName);
assert(IsStructType(parameter.type.GetResultingValue()));
std::size_t structIndex = std::get<ShaderAst::StructType>(parameter.type.GetResultingValue()).structIndex;
@@ -1123,13 +1122,10 @@ namespace Nz
if (node.entryStage.HasValue())
return HandleEntryPoint(node);
std::optional<std::size_t> varIndexOpt = node.varIndex;
for (const auto& parameter : node.parameters)
{
assert(varIndexOpt);
std::size_t& varIndex = *varIndexOpt;
RegisterVariable(varIndex++, parameter.name);
assert(parameter.varIndex);
RegisterVariable(*parameter.varIndex, parameter.name);
}
AppendFunctionDeclaration(node);

View File

@@ -102,7 +102,7 @@ namespace Nz
unsigned int indentLevel = 0;
};
std::string LangWriter::Generate(ShaderAst::Module& module, const States& /*states*/)
std::string LangWriter::Generate(const ShaderAst::Module& module, const States& /*states*/)
{
State state;
m_currentState = &state;
@@ -820,22 +820,21 @@ namespace Nz
{
NazaraAssert(m_currentState, "This function should only be called while processing an AST");
std::optional<std::size_t> varIndexOpt = node.varIndex;
AppendAttributes(true, EntryAttribute{ node.entryStage }, EarlyFragmentTestsAttribute{ node.earlyFragmentTests }, DepthWriteAttribute{ node.depthWrite });
Append("fn ", node.name, "(");
for (std::size_t i = 0; i < node.parameters.size(); ++i)
{
const auto& parameter = node.parameters[i];
if (i != 0)
Append(", ");
Append(node.parameters[i].name);
Append(parameter.name);
Append(": ");
Append(node.parameters[i].type);
Append(parameter.type);
assert(varIndexOpt);
std::size_t& varIndex = *varIndexOpt;
RegisterVariable(varIndex++, node.parameters[i].name);
assert(parameter.varIndex);
RegisterVariable(*parameter.varIndex, parameter.name);
}
Append(")");
if (node.returnType.HasValue())

View File

@@ -640,13 +640,15 @@ namespace Nz::ShaderLang
ShaderAst::DeclareFunctionStatement::Parameter Parser::ParseFunctionParameter()
{
std::string parameterName = ParseIdentifierAsName();
ShaderAst::DeclareFunctionStatement::Parameter parameter;
parameter.name = ParseIdentifierAsName();
Expect(Advance(), TokenType::Colon);
ShaderAst::ExpressionPtr parameterType = ParseType();
parameter.type = ParseType();
return { parameterName, std::move(parameterType) };
return parameter;
}
ShaderAst::StatementPtr Parser::ParseImportStatement()

View File

@@ -624,13 +624,13 @@ namespace Nz
if (!func.parameters.empty())
{
std::size_t varIndex = *node.varIndex;
for (const auto& param : func.parameters)
assert(node.parameters.size() == func.parameters.size());
for (std::size_t i = 0; i < func.parameters.size(); ++i)
{
UInt32 paramResultId = m_writer.AllocateResultId();
m_instructions.Append(SpirvOp::OpFunctionParameter, param.pointerTypeId, paramResultId);
m_instructions.Append(SpirvOp::OpFunctionParameter, func.parameters[i].pointerTypeId, paramResultId);
RegisterVariable(varIndex++, param.typeId, paramResultId, SpirvStorageClass::Function);
RegisterVariable(*node.parameters[i].varIndex, func.parameters[i].typeId, paramResultId, SpirvStorageClass::Function);
}
}
@@ -655,8 +655,6 @@ namespace Nz
{
auto& inputStruct = *entryPointData.inputStruct;
std::size_t varIndex = *node.varIndex;
UInt32 paramId = m_writer.AllocateResultId();
m_currentBlock->Append(SpirvOp::OpVariable, inputStruct.pointerId, paramId, SpirvStorageClass::Function);
@@ -667,7 +665,7 @@ namespace Nz
m_currentBlock->Append(SpirvOp::OpCopyMemory, resultId, input.varId);
}
RegisterVariable(varIndex, inputStruct.typeId, paramId, SpirvStorageClass::Function);
RegisterVariable(*node.parameters.front().varIndex, inputStruct.typeId, paramId, SpirvStorageClass::Function);
}
}

View File

@@ -496,7 +496,7 @@ namespace Nz
{
}
std::vector<UInt32> SpirvWriter::Generate(ShaderAst::Module& module, const States& states)
std::vector<UInt32> SpirvWriter::Generate(const ShaderAst::Module& module, const States& states)
{
ShaderAst::ModulePtr sanitizedModule;
ShaderAst::Statement* targetAst;

View File

@@ -61,7 +61,7 @@ namespace Nz
return pipelineLayout;
}
std::shared_ptr<ShaderModule> VulkanDevice::InstantiateShaderModule(ShaderStageTypeFlags stages, ShaderAst::Module& shaderModule, const ShaderWriter::States& states)
std::shared_ptr<ShaderModule> VulkanDevice::InstantiateShaderModule(ShaderStageTypeFlags stages, const ShaderAst::Module& shaderModule, const ShaderWriter::States& states)
{
auto stage = std::make_shared<VulkanShaderModule>();
if (!stage->Create(*this, stages, shaderModule, states))

View File

@@ -57,7 +57,7 @@ namespace Nz
};
}
bool VulkanShaderModule::Create(Vk::Device& device, ShaderStageTypeFlags shaderStages, ShaderAst::Module& shaderModule, const ShaderWriter::States& states)
bool VulkanShaderModule::Create(Vk::Device& device, ShaderStageTypeFlags shaderStages, const ShaderAst::Module& shaderModule, const ShaderWriter::States& states)
{
SpirvWriter::Environment env;

View File

@@ -819,10 +819,9 @@ std::unique_ptr<Nz::ShaderAst::DeclareFunctionStatement> ShaderGraph::ToFunction
std::vector<Nz::ShaderAst::DeclareFunctionStatement::Parameter> parameters;
if (!m_inputs.empty())
{
parameters.push_back({
"input",
Nz::ShaderAst::ExpressionPtr{ Nz::ShaderBuilder::Identifier("InputData") }
});
auto& parameter = parameters.emplace_back();
parameter.name = "input";
parameter.type = Nz::ShaderAst::ExpressionPtr{ Nz::ShaderBuilder::Identifier("InputData") };
}
Nz::ShaderAst::ExpressionPtr returnType;