Shader: Fix support of conditional functions

This commit is contained in:
Jérôme Leclercq
2021-08-02 11:12:34 +02:00
parent c8e7fa5063
commit a5b71f33b9
5 changed files with 57 additions and 28 deletions

View File

@@ -35,6 +35,20 @@ namespace Nz
SpirvBuiltIn decoration;
};
template<typename T> T& Retrieve(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> 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;
}
std::unordered_map<ShaderAst::BuiltinEntry, Builtin> s_builtinMapping = {
{ ShaderAst::BuiltinEntry::FragCoord, { "FragmentCoordinates", ShaderStageType::Fragment, SpirvBuiltIn::FragCoord } },
{ ShaderAst::BuiltinEntry::FragDepth, { "FragmentDepth", ShaderStageType::Fragment, SpirvBuiltIn::FragDepth } },
@@ -59,7 +73,7 @@ namespace Nz
using FunctionContainer = std::vector<std::reference_wrapper<ShaderAst::DeclareFunctionStatement>>;
using StructContainer = std::vector<ShaderAst::StructDescription*>;
PreVisitor(const SpirvWriter::States& conditions, SpirvConstantCache& constantCache, std::vector<SpirvAstVisitor::FuncData>& funcs) :
PreVisitor(const SpirvWriter::States& conditions, SpirvConstantCache& constantCache, std::unordered_map<std::size_t, SpirvAstVisitor::FuncData>& funcs) :
m_states(conditions),
m_constantCache(constantCache),
m_externalBlockIndex(0),
@@ -91,7 +105,7 @@ namespace Nz
AstRecursiveVisitor::Visit(node);
assert(m_funcIndex);
auto& func = m_funcs[*m_funcIndex];
auto& func = Retrieve(m_funcs, *m_funcIndex);
auto& funcCall = func.funcCalls.emplace_back();
funcCall.firstVarIndex = func.variables.size();
@@ -152,9 +166,6 @@ namespace Nz
assert(node.funcIndex);
std::size_t funcIndex = *node.funcIndex;
if (funcIndex >= m_funcs.size())
m_funcs.resize(funcIndex + 1);
auto& funcData = m_funcs[funcIndex];
funcData.name = node.name;
funcData.funcIndex = funcIndex;
@@ -409,7 +420,7 @@ namespace Nz
SpirvConstantCache& m_constantCache;
std::optional<std::size_t> m_funcIndex;
std::size_t m_externalBlockIndex;
std::vector<SpirvAstVisitor::FuncData>& m_funcs;
std::unordered_map<std::size_t, SpirvAstVisitor::FuncData>& m_funcs;
};
}
@@ -429,7 +440,7 @@ namespace Nz
std::unordered_map<std::string, UInt32> extensionInstructionSet;
std::unordered_map<std::string, UInt32> varToResult;
std::vector<SpirvAstVisitor::FuncData> funcs;
std::unordered_map<std::size_t, SpirvAstVisitor::FuncData> funcs;
std::vector<UInt32> resultIds;
UInt32 nextVarIndex = 1;
SpirvConstantCache constantTypeCache; //< init after nextVarIndex
@@ -488,7 +499,7 @@ namespace Nz
state.extensionInstructionSet[extInst] = AllocateResultId();
// Assign function ID (required for forward declaration)
for (auto& func : state.funcs)
for (auto&& [funcIndex, func] : state.funcs)
func.funcId = AllocateResultId();
SpirvAstVisitor visitor(*this, state.instructions, state.funcs);
@@ -548,7 +559,7 @@ namespace Nz
m_currentState->header.Append(SpirvOp::OpMemoryModel, SpirvAddressingModel::Logical, SpirvMemoryModel::GLSL450);
for (auto& func : m_currentState->funcs)
for (auto&& [funcIndex, func] : m_currentState->funcs)
{
m_currentState->debugInfo.Append(SpirvOp::OpName, func.funcId, func.name);
@@ -588,7 +599,7 @@ namespace Nz
}
// Write execution modes
for (auto& func : m_currentState->funcs)
for (auto&& [funcIndex, func] : m_currentState->funcs)
{
if (func.entryPointData)
{