Shader: Add initial support for arrays

This commit is contained in:
Jérôme Leclercq
2022-01-01 23:01:31 +01:00
parent 89c7bbf197
commit 1f15328fdd
22 changed files with 781 additions and 57 deletions

View File

@@ -79,7 +79,7 @@ namespace Nz::ShaderAst
auto clone = std::make_unique<DeclareExternalStatement>();
clone->varIndex = node.varIndex;
clone->bindingSet = CloneAttribute(node.bindingSet);
clone->bindingSet = Clone(node.bindingSet);
clone->externalVars.reserve(node.externalVars.size());
for (const auto& var : node.externalVars)
@@ -87,8 +87,8 @@ namespace Nz::ShaderAst
auto& cloneVar = clone->externalVars.emplace_back();
cloneVar.name = var.name;
cloneVar.type = var.type;
cloneVar.bindingIndex = CloneAttribute(var.bindingIndex);
cloneVar.bindingSet = CloneAttribute(var.bindingSet);
cloneVar.bindingIndex = Clone(var.bindingIndex);
cloneVar.bindingSet = Clone(var.bindingSet);
}
return clone;
@@ -97,9 +97,9 @@ namespace Nz::ShaderAst
StatementPtr AstCloner::Clone(DeclareFunctionStatement& node)
{
auto clone = std::make_unique<DeclareFunctionStatement>();
clone->depthWrite = CloneAttribute(node.depthWrite);
clone->earlyFragmentTests = CloneAttribute(node.earlyFragmentTests);
clone->entryStage = CloneAttribute(node.entryStage);
clone->depthWrite = Clone(node.depthWrite);
clone->earlyFragmentTests = Clone(node.earlyFragmentTests);
clone->entryStage = Clone(node.entryStage);
clone->funcIndex = node.funcIndex;
clone->name = node.name;
clone->parameters = node.parameters;
@@ -129,7 +129,7 @@ namespace Nz::ShaderAst
auto clone = std::make_unique<DeclareStructStatement>();
clone->structIndex = node.structIndex;
clone->description.layout = CloneAttribute(node.description.layout);
clone->description.layout = Clone(node.description.layout);
clone->description.name = node.description.name;
clone->description.members.reserve(node.description.members.size());
@@ -138,9 +138,9 @@ namespace Nz::ShaderAst
auto& cloneMember = clone->description.members.emplace_back();
cloneMember.name = member.name;
cloneMember.type = member.type;
cloneMember.builtin = CloneAttribute(member.builtin);
cloneMember.cond = CloneAttribute(member.cond);
cloneMember.locationIndex = CloneAttribute(member.locationIndex);
cloneMember.builtin = Clone(member.builtin);
cloneMember.cond = Clone(member.cond);
cloneMember.locationIndex = Clone(member.locationIndex);
}
return clone;

View File

@@ -0,0 +1,10 @@
// 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/AstCompare.hpp>
#include <Nazara/Shader/Debug.hpp>
namespace Nz::ShaderAst
{
}

View File

@@ -409,6 +409,12 @@ namespace Nz::ShaderAst
m_stream << UInt32(arg.componentCount);
m_stream << UInt32(arg.type);
}
else if constexpr (std::is_same_v<T, ArrayType>)
{
m_stream << UInt8(8);
Attribute(arg.length);
Type(arg.containedType->type);
}
else
static_assert(AlwaysFalse<T>::value, "non-exhaustive visitor");
}, type);
@@ -569,40 +575,6 @@ namespace Nz::ShaderAst
switch (typeIndex)
{
/*
if constexpr (std::is_same_v<T, NoType>)
m_stream << UInt8(0);
else if constexpr (std::is_same_v<T, PrimitiveType>)
{
m_stream << UInt8(1);
m_stream << UInt32(arg);
}
else if constexpr (std::is_same_v<T, IdentifierType>)
{
m_stream << UInt8(2);
m_stream << arg.name;
}
else if constexpr (std::is_same_v<T, MatrixType>)
{
m_stream << UInt8(3);
m_stream << UInt32(arg.columnCount);
m_stream << UInt32(arg.rowCount);
m_stream << UInt32(arg.type);
}
else if constexpr (std::is_same_v<T, SamplerType>)
{
m_stream << UInt8(4);
m_stream << UInt32(arg.dim);
m_stream << UInt32(arg.sampledType);
}
else if constexpr (std::is_same_v<T, VectorType>)
{
m_stream << UInt8(5);
m_stream << UInt32(arg.componentCount);
m_stream << UInt32(arg.type);
}
*/
case 0: //< NoType
type = NoType{};
break;
@@ -693,6 +665,22 @@ namespace Nz::ShaderAst
break;
}
case 8: //< ArrayType
{
AttributeValue<UInt32> length;
ExpressionType containedType;
Attribute(length);
Type(containedType);
ArrayType arrayType;
arrayType.length = std::move(length);
arrayType.containedType = std::make_unique<ContainedType>();
arrayType.containedType->type = std::move(containedType);
type = std::move(arrayType);
break;
}
default:
break;
}

View File

@@ -3,6 +3,7 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Shader/Ast/ConstantValue.hpp>
#include <Nazara/Shader/Ast/Nodes.hpp>
#include <Nazara/Shader/Debug.hpp>
namespace Nz::ShaderAst

View File

@@ -0,0 +1,42 @@
// 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/ExpressionType.hpp>
#include <Nazara/Shader/Ast/AstCloner.hpp>
#include <Nazara/Shader/Ast/AstCompare.hpp>
#include <Nazara/Shader/Debug.hpp>
namespace Nz::ShaderAst
{
ArrayType::ArrayType(const ArrayType& array)
{
assert(array.containedType);
containedType = std::make_unique<ContainedType>(*array.containedType);
length = Clone(length);
}
ArrayType& ArrayType::operator=(const ArrayType& array)
{
assert(array.containedType);
containedType = std::make_unique<ContainedType>(*array.containedType);
length = Clone(length);
return *this;
}
bool ArrayType::operator==(const ArrayType& rhs) const
{
assert(containedType);
assert(rhs.containedType);
if (containedType->type != rhs.containedType->type)
return false;
if (!Compare(length, rhs.length))
return false;
return true;
}
}

View File

@@ -1156,6 +1156,7 @@ namespace Nz::ShaderAst
if constexpr (std::is_same_v<T, IdentifierType> || std::is_same_v<T, StructType> || std::is_same_v<T, UniformType>)
return ResolveStruct(arg);
else if constexpr (std::is_same_v<T, NoType> ||
std::is_same_v<T, ArrayType> ||
std::is_same_v<T, PrimitiveType> ||
std::is_same_v<T, MatrixType> ||
std::is_same_v<T, SamplerType> ||
@@ -1205,6 +1206,7 @@ namespace Nz::ShaderAst
using T = std::decay_t<decltype(arg)>;
if constexpr (std::is_same_v<T, NoType> ||
std::is_same_v<T, ArrayType> ||
std::is_same_v<T, PrimitiveType> ||
std::is_same_v<T, MatrixType> ||
std::is_same_v<T, SamplerType> ||
@@ -1267,7 +1269,13 @@ namespace Nz::ShaderAst
ExpressionType exprType = GetExpressionType(*node.expr);
for (const auto& indexExpr : node.indices)
{
if (IsStructType(exprType))
if (IsArrayType(exprType))
{
const ArrayType& arrayType = std::get<ArrayType>(exprType);
exprType = arrayType.containedType->type;
}
else if (IsStructType(exprType))
{
const ShaderAst::ExpressionType& indexType = GetExpressionType(*indexExpr);
if (indexExpr->GetType() != NodeType::ConstantValueExpression || indexType != ExpressionType{ PrimitiveType::Int32 })