Shader/NZSL: Add support for array indexing

This commit is contained in:
Jérôme Leclercq
2021-06-01 16:22:41 +02:00
parent 0f3c0abb96
commit 4465e230af
17 changed files with 1139 additions and 976 deletions

View File

@@ -355,11 +355,15 @@ namespace Nz
AppendLine((forward) ? ");" : ")");
}
void GlslWriter::AppendField(std::size_t structIndex, const std::size_t* memberIndices, std::size_t remainingMembers)
void GlslWriter::AppendField(std::size_t structIndex, const ShaderAst::ExpressionPtr* memberIndices, std::size_t remainingMembers)
{
const auto& structDesc = Retrieve(m_currentState->structs, structIndex);
const auto& member = structDesc.members[*memberIndices];
assert((*memberIndices)->GetType() == ShaderAst::NodeType::ConstantExpression);
auto& constantValue = static_cast<ShaderAst::ConstantExpression&>(**memberIndices);
Int32 index = std::get<Int32>(constantValue.value);
const auto& member = structDesc.members[index];
Append(".");
Append(member.name);
@@ -655,9 +659,20 @@ namespace Nz
Visit(node.expr, true);
const ShaderAst::ExpressionType& exprType = GetExpressionType(*node.expr);
assert(IsStructType(exprType));
AppendField(std::get<ShaderAst::StructType>(exprType).structIndex, node.memberIndices.data(), node.memberIndices.size());
// For structs, convert indices to field names
if (IsStructType(exprType))
AppendField(std::get<ShaderAst::StructType>(exprType).structIndex, node.indices.data(), node.indices.size());
else
{
// Array access
for (ShaderAst::ExpressionPtr& expr : node.indices)
{
Append("[");
Visit(expr);
Append("]");
}
}
}
void GlslWriter::Visit(ShaderAst::AssignExpression& node)