Shader: Add support for custom functions calls (and better handle intrinsics)

This commit is contained in:
Jérôme Leclercq
2021-05-22 13:37:54 +02:00
parent 8a6f0db034
commit f6fd996bf1
24 changed files with 777 additions and 356 deletions

View File

@@ -141,6 +141,7 @@ namespace Nz
auto& funcData = m_funcs[funcIndex];
funcData.name = node.name;
funcData.funcIndex = funcIndex;
if (!entryPointType)
{
@@ -228,7 +229,6 @@ namespace Nz
*entryPointType,
inputStruct,
outputStructId,
funcIndex,
std::move(inputs),
std::move(outputs)
};
@@ -253,6 +253,23 @@ namespace Nz
m_constantCache.Register(*m_constantCache.BuildType(node.description));
}
void Visit(ShaderAst::CallFunctionExpression& node) override
{
AstRecursiveVisitor::Visit(node);
assert(m_funcIndex);
auto& func = m_funcs[*m_funcIndex];
auto& funcCall = func.funcCalls.emplace_back();
funcCall.firstVarIndex = func.variables.size();
for (const auto& parameter : node.parameters)
{
auto& var = func.variables.emplace_back();
var.typeId = m_constantCache.Register(*m_constantCache.BuildPointerType(GetExpressionType(*parameter), SpirvStorageClass::Function));
}
}
void Visit(ShaderAst::DeclareVariableStatement& node) override
{
AstRecursiveVisitor::Visit(node);
@@ -440,6 +457,10 @@ namespace Nz
for (const std::string& extInst : preVisitor.extInsts)
state.extensionInstructionSet[extInst] = AllocateResultId();
// Assign function ID (required for forward declaration)
for (auto& func : state.funcs)
func.funcId = AllocateResultId();
SpirvAstVisitor visitor(*this, state.instructions, state.funcs);
targetAst->Visit(visitor);