Shader: Add support for numerical fors
This commit is contained in:
@@ -170,12 +170,26 @@ namespace Nz::ShaderAst
|
||||
return clone;
|
||||
}
|
||||
|
||||
StatementPtr AstCloner::Clone(ForStatement& node)
|
||||
{
|
||||
auto clone = std::make_unique<ForStatement>();
|
||||
clone->fromExpr = CloneExpression(node.fromExpr);
|
||||
clone->stepExpr = CloneExpression(node.stepExpr);
|
||||
clone->toExpr = CloneExpression(node.toExpr);
|
||||
clone->statement = CloneStatement(node.statement);
|
||||
clone->unroll = Clone(node.unroll);
|
||||
clone->varName = node.varName;
|
||||
|
||||
return clone;
|
||||
}
|
||||
|
||||
StatementPtr AstCloner::Clone(ForEachStatement& node)
|
||||
{
|
||||
auto clone = std::make_unique<ForEachStatement>();
|
||||
clone->expression = CloneExpression(node.expression);
|
||||
clone->statement = CloneStatement(node.statement);
|
||||
clone->unroll = Clone(node.unroll);
|
||||
clone->varName = node.varName;
|
||||
|
||||
return clone;
|
||||
}
|
||||
|
||||
@@ -161,6 +161,21 @@ namespace Nz::ShaderAst
|
||||
node.expression->Visit(*this);
|
||||
}
|
||||
|
||||
void AstRecursiveVisitor::Visit(ForStatement& node)
|
||||
{
|
||||
if (node.fromExpr)
|
||||
node.fromExpr->Visit(*this);
|
||||
|
||||
if (node.toExpr)
|
||||
node.toExpr->Visit(*this);
|
||||
|
||||
if (node.stepExpr)
|
||||
node.stepExpr->Visit(*this);
|
||||
|
||||
if (node.statement)
|
||||
node.statement->Visit(*this);
|
||||
}
|
||||
|
||||
void AstRecursiveVisitor::Visit(ForEachStatement& node)
|
||||
{
|
||||
if (node.expression)
|
||||
|
||||
@@ -301,6 +301,16 @@ namespace Nz::ShaderAst
|
||||
Node(node.expression);
|
||||
}
|
||||
|
||||
void AstSerializerBase::Serialize(ForStatement& node)
|
||||
{
|
||||
Attribute(node.unroll);
|
||||
Value(node.varName);
|
||||
Node(node.fromExpr);
|
||||
Node(node.toExpr);
|
||||
Node(node.stepExpr);
|
||||
Node(node.statement);
|
||||
}
|
||||
|
||||
void AstSerializerBase::Serialize(ForEachStatement& node)
|
||||
{
|
||||
Attribute(node.unroll);
|
||||
|
||||
@@ -163,19 +163,6 @@ namespace Nz::ShaderAst
|
||||
const ExpressionType& exprType = GetExpressionType(*indexedExpr);
|
||||
if (IsStructType(exprType))
|
||||
{
|
||||
// Transform to AccessIndexExpression
|
||||
AccessIndexExpression* accessIndexPtr;
|
||||
if (indexedExpr->GetType() != NodeType::AccessIndexExpression)
|
||||
{
|
||||
std::unique_ptr<AccessIndexExpression> accessIndex = std::make_unique<AccessIndexExpression>();
|
||||
accessIndex->expr = std::move(indexedExpr);
|
||||
|
||||
accessIndexPtr = accessIndex.get();
|
||||
indexedExpr = std::move(accessIndex);
|
||||
}
|
||||
else
|
||||
accessIndexPtr = static_cast<AccessIndexExpression*>(indexedExpr.get());
|
||||
|
||||
std::size_t structIndex = ResolveStruct(exprType);
|
||||
assert(structIndex < m_context->structs.size());
|
||||
const StructDescription* s = m_context->structs[structIndex];
|
||||
@@ -200,8 +187,42 @@ namespace Nz::ShaderAst
|
||||
if (!fieldPtr)
|
||||
throw AstError{ "unknown field " + identifier };
|
||||
|
||||
accessIndexPtr->indices.push_back(ShaderBuilder::Constant(fieldIndex));
|
||||
accessIndexPtr->cachedExpressionType = ResolveType(fieldPtr->type);
|
||||
if (m_context->options.useIdentifierAccessesForStructs)
|
||||
{
|
||||
// Use a AccessIdentifierExpression
|
||||
AccessIdentifierExpression* accessIdentifierPtr;
|
||||
if (indexedExpr->GetType() != NodeType::AccessIdentifierExpression)
|
||||
{
|
||||
std::unique_ptr<AccessIdentifierExpression> accessIndex = std::make_unique<AccessIdentifierExpression>();
|
||||
accessIndex->expr = std::move(indexedExpr);
|
||||
|
||||
accessIdentifierPtr = accessIndex.get();
|
||||
indexedExpr = std::move(accessIndex);
|
||||
}
|
||||
else
|
||||
accessIdentifierPtr = static_cast<AccessIdentifierExpression*>(indexedExpr.get());
|
||||
|
||||
accessIdentifierPtr->identifiers.push_back(s->members[fieldIndex].name);
|
||||
accessIdentifierPtr->cachedExpressionType = ResolveType(fieldPtr->type);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Transform to AccessIndexExpression
|
||||
AccessIndexExpression* accessIndexPtr;
|
||||
if (indexedExpr->GetType() != NodeType::AccessIndexExpression)
|
||||
{
|
||||
std::unique_ptr<AccessIndexExpression> accessIndex = std::make_unique<AccessIndexExpression>();
|
||||
accessIndex->expr = std::move(indexedExpr);
|
||||
|
||||
accessIndexPtr = accessIndex.get();
|
||||
indexedExpr = std::move(accessIndex);
|
||||
}
|
||||
else
|
||||
accessIndexPtr = static_cast<AccessIndexExpression*>(indexedExpr.get());
|
||||
|
||||
accessIndexPtr->indices.push_back(ShaderBuilder::Constant(fieldIndex));
|
||||
accessIndexPtr->cachedExpressionType = ResolveType(fieldPtr->type);
|
||||
}
|
||||
}
|
||||
else if (IsPrimitiveType(exprType) || IsVectorType(exprType))
|
||||
{
|
||||
@@ -269,6 +290,8 @@ namespace Nz::ShaderAst
|
||||
auto clone = static_unique_pointer_cast<AccessIndexExpression>(AstCloner::Clone(node));
|
||||
Validate(*clone);
|
||||
|
||||
// TODO: Handle AccessIndex on structs with m_context->options.useIdentifierAccessesForStructs
|
||||
|
||||
return clone;
|
||||
}
|
||||
|
||||
@@ -829,9 +852,180 @@ namespace Nz::ShaderAst
|
||||
return AstCloner::Clone(node);
|
||||
}
|
||||
|
||||
StatementPtr SanitizeVisitor::Clone(ForStatement& node)
|
||||
{
|
||||
if (node.varName.empty())
|
||||
throw AstError{ "numerical for variable name cannot be empty" };
|
||||
|
||||
auto fromExpr = CloneExpression(MandatoryExpr(node.fromExpr));
|
||||
auto stepExpr = CloneExpression(node.stepExpr);
|
||||
auto toExpr = CloneExpression(MandatoryExpr(node.toExpr));
|
||||
|
||||
MandatoryStatement(node.statement);
|
||||
|
||||
const ExpressionType& fromExprType = GetExpressionType(*fromExpr);
|
||||
if (!IsPrimitiveType(fromExprType))
|
||||
throw AstError{ "numerical for from expression must be an integer or unsigned integer" };
|
||||
|
||||
PrimitiveType fromType = std::get<PrimitiveType>(fromExprType);
|
||||
if (fromType != PrimitiveType::Int32 && fromType != PrimitiveType::UInt32)
|
||||
throw AstError{ "numerical for from expression must be an integer or unsigned integer" };
|
||||
|
||||
const ExpressionType& toExprType = GetExpressionType(*fromExpr);
|
||||
if (toExprType != fromExprType)
|
||||
throw AstError{ "numerical for to expression type must match from expression type" };
|
||||
|
||||
if (stepExpr)
|
||||
{
|
||||
const ExpressionType& stepExprType = GetExpressionType(*fromExpr);
|
||||
if (stepExprType != fromExprType)
|
||||
throw AstError{ "numerical for step expression type must match from expression type" };
|
||||
}
|
||||
|
||||
|
||||
AttributeValue<LoopUnroll> unrollValue;
|
||||
if (node.unroll.HasValue())
|
||||
{
|
||||
unrollValue = ComputeAttributeValue(node.unroll);
|
||||
if (unrollValue.GetResultingValue() == LoopUnroll::Always)
|
||||
{
|
||||
PushScope();
|
||||
|
||||
auto multi = std::make_unique<MultiStatement>();
|
||||
|
||||
auto Unroll = [&](auto dummy)
|
||||
{
|
||||
using T = std::decay_t<decltype(dummy)>;
|
||||
|
||||
T counter = std::get<T>(ComputeConstantValue(*fromExpr));
|
||||
T to = std::get<T>(ComputeConstantValue(*toExpr));
|
||||
T step = (stepExpr) ? std::get<T>(ComputeConstantValue(*stepExpr)) : T(1);
|
||||
|
||||
for (; counter < to; counter += step)
|
||||
{
|
||||
auto var = ShaderBuilder::DeclareVariable(node.varName, ShaderBuilder::Constant(counter));
|
||||
Validate(*var);
|
||||
multi->statements.emplace_back(std::move(var));
|
||||
|
||||
multi->statements.emplace_back(CloneStatement(node.statement));
|
||||
}
|
||||
};
|
||||
|
||||
switch (fromType)
|
||||
{
|
||||
case PrimitiveType::Int32:
|
||||
Unroll(Int32{});
|
||||
break;
|
||||
|
||||
case PrimitiveType::UInt32:
|
||||
Unroll(UInt32{});
|
||||
break;
|
||||
|
||||
default:
|
||||
throw AstError{ "internal error" };
|
||||
}
|
||||
|
||||
PopScope();
|
||||
|
||||
return multi;
|
||||
}
|
||||
}
|
||||
|
||||
if (m_context->options.reduceLoopsToWhile)
|
||||
{
|
||||
PushScope();
|
||||
|
||||
auto multi = std::make_unique<MultiStatement>();
|
||||
|
||||
// Counter variable
|
||||
auto counterVariable = ShaderBuilder::DeclareVariable(node.varName, std::move(fromExpr));
|
||||
Validate(*counterVariable);
|
||||
|
||||
std::size_t counterVarIndex = counterVariable->varIndex.value();
|
||||
multi->statements.emplace_back(std::move(counterVariable));
|
||||
|
||||
// Target variable
|
||||
auto targetVariable = ShaderBuilder::DeclareVariable("to", std::move(toExpr));
|
||||
Validate(*targetVariable);
|
||||
|
||||
std::size_t targetVarIndex = targetVariable->varIndex.value();
|
||||
multi->statements.emplace_back(std::move(targetVariable));
|
||||
|
||||
// Step variable
|
||||
std::optional<std::size_t> stepVarIndex;
|
||||
|
||||
if (stepExpr)
|
||||
{
|
||||
auto stepVariable = ShaderBuilder::DeclareVariable("step", std::move(stepExpr));
|
||||
Validate(*stepVariable);
|
||||
|
||||
stepVarIndex = stepVariable->varIndex;
|
||||
multi->statements.emplace_back(std::move(stepVariable));
|
||||
}
|
||||
|
||||
// While
|
||||
auto whileStatement = std::make_unique<WhileStatement>();
|
||||
whileStatement->unroll = std::move(unrollValue);
|
||||
|
||||
// While condition
|
||||
auto condition = ShaderBuilder::Binary(BinaryType::CompLt, ShaderBuilder::Variable(counterVarIndex, fromType), ShaderBuilder::Variable(targetVarIndex, fromType));
|
||||
Validate(*condition);
|
||||
|
||||
whileStatement->condition = std::move(condition);
|
||||
|
||||
// While body
|
||||
auto body = std::make_unique<MultiStatement>();
|
||||
body->statements.reserve(2);
|
||||
|
||||
body->statements.emplace_back(CloneStatement(node.statement));
|
||||
|
||||
ExpressionPtr incrExpr;
|
||||
if (stepVarIndex)
|
||||
incrExpr = ShaderBuilder::Variable(*stepVarIndex, fromType);
|
||||
else
|
||||
incrExpr = (fromType == PrimitiveType::Int32) ? ShaderBuilder::Constant(1) : ShaderBuilder::Constant(1u);
|
||||
|
||||
auto incrCounter = ShaderBuilder::Assign(AssignType::CompoundAdd, ShaderBuilder::Variable(counterVarIndex, fromType), std::move(incrExpr));
|
||||
Validate(*incrCounter);
|
||||
|
||||
body->statements.emplace_back(ShaderBuilder::ExpressionStatement(std::move(incrCounter)));
|
||||
|
||||
whileStatement->body = std::move(body);
|
||||
|
||||
multi->statements.emplace_back(std::move(whileStatement));
|
||||
|
||||
PopScope();
|
||||
|
||||
return multi;
|
||||
}
|
||||
else
|
||||
{
|
||||
auto clone = std::make_unique<ForStatement>();
|
||||
clone->fromExpr = std::move(fromExpr);
|
||||
clone->stepExpr = std::move(stepExpr);
|
||||
clone->toExpr = std::move(toExpr);
|
||||
clone->varName = node.varName;
|
||||
clone->unroll = std::move(unrollValue);
|
||||
|
||||
PushScope();
|
||||
{
|
||||
clone->varIndex = RegisterVariable(node.varName, fromExprType);
|
||||
clone->statement = CloneStatement(node.statement);
|
||||
}
|
||||
PopScope();
|
||||
|
||||
SanitizeIdentifier(clone->varName);
|
||||
|
||||
return clone;
|
||||
}
|
||||
}
|
||||
|
||||
StatementPtr SanitizeVisitor::Clone(ForEachStatement& node)
|
||||
{
|
||||
auto expr = CloneExpression(node.expression);
|
||||
auto expr = CloneExpression(MandatoryExpr(node.expression));
|
||||
|
||||
if (node.varName.empty())
|
||||
throw AstError{ "for-each variable name cannot be empty"};
|
||||
|
||||
const ExpressionType& exprType = GetExpressionType(*expr);
|
||||
ExpressionType innerType;
|
||||
@@ -849,6 +1043,8 @@ namespace Nz::ShaderAst
|
||||
unrollValue = ComputeAttributeValue(node.unroll);
|
||||
if (unrollValue.GetResultingValue() == LoopUnroll::Always)
|
||||
{
|
||||
PushScope();
|
||||
|
||||
// Repeat code
|
||||
auto multi = std::make_unique<MultiStatement>();
|
||||
if (IsArrayType(exprType))
|
||||
@@ -869,6 +1065,8 @@ namespace Nz::ShaderAst
|
||||
}
|
||||
}
|
||||
|
||||
PopScope();
|
||||
|
||||
return multi;
|
||||
}
|
||||
}
|
||||
@@ -943,7 +1141,7 @@ namespace Nz::ShaderAst
|
||||
}
|
||||
PopScope();
|
||||
|
||||
SanitizeIdentifier(node.varName);
|
||||
SanitizeIdentifier(clone->varName);
|
||||
|
||||
return clone;
|
||||
}
|
||||
|
||||
@@ -390,41 +390,6 @@ namespace Nz
|
||||
AppendLine((forward) ? ");" : ")");
|
||||
}
|
||||
|
||||
void GlslWriter::AppendField(std::size_t structIndex, const ShaderAst::ExpressionPtr* memberIndices, std::size_t remainingMembers)
|
||||
{
|
||||
ShaderAst::StructDescription* structDesc = Retrieve(m_currentState->structs, structIndex);
|
||||
|
||||
assert((*memberIndices)->GetType() == ShaderAst::NodeType::ConstantValueExpression);
|
||||
auto& constantValue = static_cast<ShaderAst::ConstantValueExpression&>(**memberIndices);
|
||||
Int32 index = std::get<Int32>(constantValue.value);
|
||||
assert(index >= 0);
|
||||
|
||||
auto it = structDesc->members.begin();
|
||||
for (; it != structDesc->members.end(); ++it)
|
||||
{
|
||||
const auto& member = *it;
|
||||
if (member.cond.HasValue() && !member.cond.GetResultingValue())
|
||||
continue;
|
||||
|
||||
if (index == 0)
|
||||
break;
|
||||
|
||||
index--;
|
||||
}
|
||||
|
||||
assert(it != structDesc->members.end());
|
||||
const auto& member = *it;
|
||||
|
||||
Append(".");
|
||||
Append(member.name);
|
||||
|
||||
if (remainingMembers > 1)
|
||||
{
|
||||
assert(IsStructType(member.type));
|
||||
AppendField(std::get<ShaderAst::StructType>(member.type).structIndex, memberIndices + 1, remainingMembers - 1);
|
||||
}
|
||||
}
|
||||
|
||||
void GlslWriter::AppendHeader()
|
||||
{
|
||||
unsigned int glslVersion;
|
||||
@@ -734,24 +699,30 @@ namespace Nz
|
||||
Append(")");
|
||||
}
|
||||
|
||||
void GlslWriter::Visit(ShaderAst::AccessIdentifierExpression& node)
|
||||
{
|
||||
Visit(node.expr, true);
|
||||
|
||||
const ShaderAst::ExpressionType& exprType = GetExpressionType(*node.expr);
|
||||
assert(IsStructType(exprType));
|
||||
|
||||
for (const std::string& identifier : node.identifiers)
|
||||
Append(".", identifier);
|
||||
}
|
||||
|
||||
void GlslWriter::Visit(ShaderAst::AccessIndexExpression& node)
|
||||
{
|
||||
Visit(node.expr, true);
|
||||
|
||||
const ShaderAst::ExpressionType& exprType = GetExpressionType(*node.expr);
|
||||
assert(!IsStructType(exprType));
|
||||
|
||||
// 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)
|
||||
{
|
||||
// Array access
|
||||
for (ShaderAst::ExpressionPtr& expr : node.indices)
|
||||
{
|
||||
Append("[");
|
||||
Visit(expr);
|
||||
Append("]");
|
||||
}
|
||||
Append("[");
|
||||
Visit(expr);
|
||||
Append("]");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -83,6 +83,13 @@ namespace Nz
|
||||
inline bool HasValue() const { return setIndex.HasValue(); }
|
||||
};
|
||||
|
||||
struct LangWriter::UnrollAttribute
|
||||
{
|
||||
const ShaderAst::AttributeValue<ShaderAst::LoopUnroll>& unroll;
|
||||
|
||||
inline bool HasValue() const { return unroll.HasValue(); }
|
||||
};
|
||||
|
||||
struct LangWriter::State
|
||||
{
|
||||
const States* states = nullptr;
|
||||
@@ -103,10 +110,7 @@ namespace Nz
|
||||
m_currentState = nullptr;
|
||||
});
|
||||
|
||||
ShaderAst::SanitizeVisitor::Options options;
|
||||
options.removeOptionDeclaration = false;
|
||||
|
||||
ShaderAst::StatementPtr sanitizedAst = ShaderAst::Sanitize(shader, options);
|
||||
ShaderAst::StatementPtr sanitizedAst = ShaderAst::Sanitize(shader);
|
||||
|
||||
AppendHeader();
|
||||
|
||||
@@ -277,10 +281,14 @@ namespace Nz
|
||||
if (!binding.HasValue())
|
||||
return;
|
||||
|
||||
Append("binding(");
|
||||
|
||||
if (binding.bindingIndex.IsResultingValue())
|
||||
Append("binding(", binding.bindingIndex.GetResultingValue(), ")");
|
||||
Append(binding.bindingIndex.GetResultingValue());
|
||||
else
|
||||
binding.bindingIndex.GetExpression()->Visit(*this);
|
||||
|
||||
Append(")");
|
||||
}
|
||||
|
||||
void LangWriter::AppendAttribute(BuiltinAttribute builtin)
|
||||
@@ -288,25 +296,29 @@ namespace Nz
|
||||
if (!builtin.HasValue())
|
||||
return;
|
||||
|
||||
Append("builtin(");
|
||||
|
||||
if (builtin.builtin.IsResultingValue())
|
||||
{
|
||||
switch (builtin.builtin.GetResultingValue())
|
||||
{
|
||||
case ShaderAst::BuiltinEntry::FragCoord:
|
||||
Append("builtin(fragcoord)");
|
||||
Append("fragcoord");
|
||||
break;
|
||||
|
||||
case ShaderAst::BuiltinEntry::FragDepth:
|
||||
Append("builtin(fragdepth)");
|
||||
Append("fragdepth");
|
||||
break;
|
||||
|
||||
case ShaderAst::BuiltinEntry::VertexPosition:
|
||||
Append("builtin(position)");
|
||||
Append("position");
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
builtin.builtin.GetExpression()->Visit(*this);
|
||||
|
||||
Append(")");
|
||||
}
|
||||
|
||||
void LangWriter::AppendAttribute(DepthWriteAttribute depthWrite)
|
||||
@@ -314,29 +326,33 @@ namespace Nz
|
||||
if (!depthWrite.HasValue())
|
||||
return;
|
||||
|
||||
Append("depth_write(");
|
||||
|
||||
if (depthWrite.writeMode.IsResultingValue())
|
||||
{
|
||||
switch (depthWrite.writeMode.GetResultingValue())
|
||||
{
|
||||
case ShaderAst::DepthWriteMode::Greater:
|
||||
Append("depth_write(greater)");
|
||||
Append("greater");
|
||||
break;
|
||||
|
||||
case ShaderAst::DepthWriteMode::Less:
|
||||
Append("depth_write(less)");
|
||||
Append("less");
|
||||
break;
|
||||
|
||||
case ShaderAst::DepthWriteMode::Replace:
|
||||
Append("depth_write(replace)");
|
||||
Append("replace");
|
||||
break;
|
||||
|
||||
case ShaderAst::DepthWriteMode::Unchanged:
|
||||
Append("depth_write(unchanged)");
|
||||
Append("unchanged");
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
depthWrite.writeMode.GetExpression()->Visit(*this);
|
||||
|
||||
Append(")");
|
||||
}
|
||||
|
||||
void LangWriter::AppendAttribute(EarlyFragmentTestsAttribute earlyFragmentTests)
|
||||
@@ -344,15 +360,19 @@ namespace Nz
|
||||
if (!earlyFragmentTests.HasValue())
|
||||
return;
|
||||
|
||||
Append("early_fragment_tests(");
|
||||
|
||||
if (earlyFragmentTests.earlyFragmentTests.IsResultingValue())
|
||||
{
|
||||
if (earlyFragmentTests.earlyFragmentTests.GetResultingValue())
|
||||
Append("early_fragment_tests(true)");
|
||||
Append("true");
|
||||
else
|
||||
Append("early_fragment_tests(false)");
|
||||
Append("false");
|
||||
}
|
||||
else
|
||||
earlyFragmentTests.earlyFragmentTests.GetExpression()->Visit(*this);
|
||||
|
||||
Append(")");
|
||||
}
|
||||
|
||||
void LangWriter::AppendAttribute(EntryAttribute entry)
|
||||
@@ -360,21 +380,25 @@ namespace Nz
|
||||
if (!entry.HasValue())
|
||||
return;
|
||||
|
||||
Append("entry(");
|
||||
|
||||
if (entry.stageType.IsResultingValue())
|
||||
{
|
||||
switch (entry.stageType.GetResultingValue())
|
||||
{
|
||||
case ShaderStageType::Fragment:
|
||||
Append("entry(frag)");
|
||||
Append("frag");
|
||||
break;
|
||||
|
||||
case ShaderStageType::Vertex:
|
||||
Append("entry(vert)");
|
||||
Append("vert");
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
entry.stageType.GetExpression()->Visit(*this);
|
||||
|
||||
Append(")");
|
||||
}
|
||||
|
||||
void LangWriter::AppendAttribute(LayoutAttribute entry)
|
||||
@@ -382,17 +406,19 @@ namespace Nz
|
||||
if (!entry.HasValue())
|
||||
return;
|
||||
|
||||
Append("layout(");
|
||||
if (entry.layout.IsResultingValue())
|
||||
{
|
||||
switch (entry.layout.GetResultingValue())
|
||||
{
|
||||
case StructLayout::Std140:
|
||||
Append("layout(std140)");
|
||||
Append("std140");
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
entry.layout.GetExpression()->Visit(*this);
|
||||
Append(")");
|
||||
}
|
||||
|
||||
void LangWriter::AppendAttribute(LocationAttribute location)
|
||||
@@ -400,10 +426,14 @@ namespace Nz
|
||||
if (!location.HasValue())
|
||||
return;
|
||||
|
||||
Append("location(");
|
||||
|
||||
if (location.locationIndex.IsResultingValue())
|
||||
Append("location(", location.locationIndex.GetResultingValue(), ")");
|
||||
Append(location.locationIndex.GetResultingValue());
|
||||
else
|
||||
location.locationIndex.GetExpression()->Visit(*this);
|
||||
|
||||
Append(")");
|
||||
}
|
||||
|
||||
void LangWriter::AppendAttribute(SetAttribute set)
|
||||
@@ -411,10 +441,45 @@ namespace Nz
|
||||
if (!set.HasValue())
|
||||
return;
|
||||
|
||||
Append("set(");
|
||||
|
||||
if (set.setIndex.IsResultingValue())
|
||||
Append("set(", set.setIndex.GetResultingValue(), ")");
|
||||
Append(set.setIndex.GetResultingValue());
|
||||
else
|
||||
set.setIndex.GetExpression()->Visit(*this);
|
||||
|
||||
Append(")");
|
||||
}
|
||||
|
||||
void LangWriter::AppendAttribute(UnrollAttribute unroll)
|
||||
{
|
||||
if (!unroll.HasValue())
|
||||
return;
|
||||
|
||||
Append("unroll(");
|
||||
|
||||
if (unroll.unroll.IsResultingValue())
|
||||
{
|
||||
switch (unroll.unroll.GetResultingValue())
|
||||
{
|
||||
case ShaderAst::LoopUnroll::Always:
|
||||
Append("always");
|
||||
break;
|
||||
|
||||
case ShaderAst::LoopUnroll::Hint:
|
||||
Append("hint");
|
||||
break;
|
||||
|
||||
case ShaderAst::LoopUnroll::Never:
|
||||
Append("never");
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
unroll.unroll.GetExpression()->Visit(*this);
|
||||
}
|
||||
|
||||
void LangWriter::AppendCommentSection(const std::string& section)
|
||||
@@ -426,26 +491,6 @@ namespace Nz
|
||||
AppendLine();
|
||||
}
|
||||
|
||||
void LangWriter::AppendField(std::size_t structIndex, const ShaderAst::ExpressionPtr* memberIndices, std::size_t remainingMembers)
|
||||
{
|
||||
ShaderAst::StructDescription* structDesc = Retrieve(m_currentState->structs, structIndex);
|
||||
|
||||
assert((*memberIndices)->GetType() == ShaderAst::NodeType::ConstantValueExpression);
|
||||
auto& constantValue = static_cast<ShaderAst::ConstantValueExpression&>(**memberIndices);
|
||||
Int32 index = std::get<Int32>(constantValue.value);
|
||||
|
||||
const auto& member = structDesc->members[index];
|
||||
|
||||
Append(".");
|
||||
Append(member.name);
|
||||
|
||||
if (remainingMembers > 1)
|
||||
{
|
||||
assert(IsStructType(member.type));
|
||||
AppendField(std::get<ShaderAst::StructType>(member.type).structIndex, memberIndices + 1, remainingMembers - 1);
|
||||
}
|
||||
}
|
||||
|
||||
void LangWriter::AppendLine(const std::string& txt)
|
||||
{
|
||||
NazaraAssert(m_currentState, "This function should only be called while processing an AST");
|
||||
@@ -526,24 +571,30 @@ namespace Nz
|
||||
Append(")");
|
||||
}
|
||||
|
||||
void LangWriter::Visit(ShaderAst::AccessIdentifierExpression& node)
|
||||
{
|
||||
Visit(node.expr, true);
|
||||
|
||||
const ShaderAst::ExpressionType& exprType = GetExpressionType(*node.expr);
|
||||
assert(IsStructType(exprType));
|
||||
|
||||
for (const std::string& identifier : node.identifiers)
|
||||
Append(".", identifier);
|
||||
}
|
||||
|
||||
void LangWriter::Visit(ShaderAst::AccessIndexExpression& node)
|
||||
{
|
||||
Visit(node.expr, true);
|
||||
|
||||
const ShaderAst::ExpressionType& exprType = GetExpressionType(*node.expr);
|
||||
assert(!IsStructType(exprType));
|
||||
|
||||
// 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)
|
||||
{
|
||||
// Array access
|
||||
for (ShaderAst::ExpressionPtr& expr : node.indices)
|
||||
{
|
||||
Append("[");
|
||||
Visit(expr);
|
||||
Append("]");
|
||||
}
|
||||
Append("[");
|
||||
expr->Visit(*this);
|
||||
Append("]");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -826,11 +877,36 @@ namespace Nz
|
||||
Append(";");
|
||||
}
|
||||
|
||||
void LangWriter::Visit(ShaderAst::ForStatement& node)
|
||||
{
|
||||
assert(node.varIndex);
|
||||
RegisterVariable(*node.varIndex, node.varName);
|
||||
|
||||
AppendAttributes(true, UnrollAttribute{ node.unroll });
|
||||
Append("for ", node.varName, " in ");
|
||||
node.fromExpr->Visit(*this);
|
||||
Append(" -> ");
|
||||
node.toExpr->Visit(*this);
|
||||
|
||||
if (node.stepExpr)
|
||||
{
|
||||
Append(" : ");
|
||||
node.stepExpr->Visit(*this);
|
||||
}
|
||||
|
||||
AppendLine();
|
||||
|
||||
EnterScope();
|
||||
node.statement->Visit(*this);
|
||||
LeaveScope();
|
||||
}
|
||||
|
||||
void LangWriter::Visit(ShaderAst::ForEachStatement& node)
|
||||
{
|
||||
assert(node.varIndex);
|
||||
RegisterVariable(*node.varIndex, node.varName);
|
||||
|
||||
AppendAttributes(true, UnrollAttribute{ node.unroll });
|
||||
Append("for ", node.varName, " in ");
|
||||
node.expression->Visit(*this);
|
||||
AppendLine();
|
||||
|
||||
@@ -113,7 +113,7 @@ namespace Nz::ShaderLang
|
||||
if (next == '>')
|
||||
{
|
||||
currentPos++;
|
||||
tokenType = TokenType::FunctionReturn;
|
||||
tokenType = TokenType::Arrow;
|
||||
break;
|
||||
}
|
||||
else if (next == '=')
|
||||
|
||||
@@ -614,24 +614,63 @@ namespace Nz::ShaderLang
|
||||
|
||||
ShaderAst::ExpressionPtr expr = ParseExpression();
|
||||
|
||||
ShaderAst::StatementPtr statement = ParseStatement();
|
||||
|
||||
auto forEach = ShaderBuilder::ForEach(std::move(varName), std::move(expr), std::move(statement));
|
||||
|
||||
for (auto&& [attributeType, arg] : attributes)
|
||||
if (Peek().type == TokenType::Arrow)
|
||||
{
|
||||
switch (attributeType)
|
||||
// Numerical for
|
||||
Consume();
|
||||
|
||||
ShaderAst::ExpressionPtr toExpr = ParseExpression();
|
||||
|
||||
ShaderAst::ExpressionPtr stepExpr;
|
||||
if (Peek().type == TokenType::Colon)
|
||||
{
|
||||
case ShaderAst::AttributeType::Unroll:
|
||||
HandleUniqueStringAttribute("unroll", s_unrollModes, forEach->unroll, std::move(arg), std::make_optional(ShaderAst::LoopUnroll::Always));
|
||||
break;
|
||||
|
||||
default:
|
||||
throw AttributeError{ "unhandled attribute for for-each" };
|
||||
Consume();
|
||||
stepExpr = ParseExpression();
|
||||
}
|
||||
}
|
||||
|
||||
return forEach;
|
||||
ShaderAst::StatementPtr statement = ParseStatement();
|
||||
|
||||
auto forNode = ShaderBuilder::For(std::move(varName), std::move(expr), std::move(toExpr), std::move(stepExpr), std::move(statement));
|
||||
|
||||
// TODO: Deduplicate code
|
||||
for (auto&& [attributeType, arg] : attributes)
|
||||
{
|
||||
switch (attributeType)
|
||||
{
|
||||
case ShaderAst::AttributeType::Unroll:
|
||||
HandleUniqueStringAttribute("unroll", s_unrollModes, forNode->unroll, std::move(arg), std::make_optional(ShaderAst::LoopUnroll::Always));
|
||||
break;
|
||||
|
||||
default:
|
||||
throw AttributeError{ "unhandled attribute for numerical for" };
|
||||
}
|
||||
}
|
||||
|
||||
return forNode;
|
||||
}
|
||||
else
|
||||
{
|
||||
// For each
|
||||
ShaderAst::StatementPtr statement = ParseStatement();
|
||||
|
||||
auto forEachNode = ShaderBuilder::ForEach(std::move(varName), std::move(expr), std::move(statement));
|
||||
|
||||
// TODO: Deduplicate code
|
||||
for (auto&& [attributeType, arg] : attributes)
|
||||
{
|
||||
switch (attributeType)
|
||||
{
|
||||
case ShaderAst::AttributeType::Unroll:
|
||||
HandleUniqueStringAttribute("unroll", s_unrollModes, forEachNode->unroll, std::move(arg), std::make_optional(ShaderAst::LoopUnroll::Always));
|
||||
break;
|
||||
|
||||
default:
|
||||
throw AttributeError{ "unhandled attribute for for-each" };
|
||||
}
|
||||
}
|
||||
|
||||
return forEachNode;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<ShaderAst::StatementPtr> Parser::ParseFunctionBody()
|
||||
@@ -668,7 +707,7 @@ namespace Nz::ShaderLang
|
||||
Expect(Advance(), TokenType::ClosingParenthesis);
|
||||
|
||||
ShaderAst::ExpressionType returnType;
|
||||
if (Peek().type == TokenType::FunctionReturn)
|
||||
if (Peek().type == TokenType::Arrow)
|
||||
{
|
||||
Consume();
|
||||
returnType = ParseType();
|
||||
|
||||
@@ -491,6 +491,7 @@ namespace Nz
|
||||
options.removeCompoundAssignments = true;
|
||||
options.removeOptionDeclaration = true;
|
||||
options.splitMultipleBranches = true;
|
||||
options.useIdentifierAccessesForStructs = false;
|
||||
|
||||
sanitizedAst = ShaderAst::Sanitize(shader, options);
|
||||
targetAst = sanitizedAst.get();
|
||||
|
||||
Reference in New Issue
Block a user