Skip to content

Commit 8cafa4c

Browse files
committed
Add support for implicit matrices
1 parent ea1b808 commit 8cafa4c

File tree

10 files changed

+368
-47
lines changed

10 files changed

+368
-47
lines changed

include/NZSL/Ast/ExpressionType.hpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,15 @@ namespace nzsl::Ast
116116
inline bool operator!=(const ImplicitArrayType& rhs) const;
117117
};
118118

119+
struct ImplicitMatrixType
120+
{
121+
std::size_t columnCount;
122+
std::size_t rowCount;
123+
124+
inline bool operator==(const ImplicitMatrixType& rhs) const;
125+
inline bool operator!=(const ImplicitMatrixType& rhs) const;
126+
};
127+
119128
struct ImplicitVectorType
120129
{
121130
std::size_t componentCount;
@@ -253,7 +262,7 @@ namespace nzsl::Ast
253262
inline bool operator!=(const PushConstantType& rhs) const;
254263
};
255264

256-
using ExpressionType = std::variant<NoType, AliasType, ArrayType, DynArrayType, FunctionType, ImplicitArrayType, ImplicitVectorType, IntrinsicFunctionType, MatrixType, MethodType, ModuleType, NamedExternalBlockType, PrimitiveType, PushConstantType, SamplerType, StorageType, StructType, TextureType, Type, UniformType, VectorType>;
265+
using ExpressionType = std::variant<NoType, AliasType, ArrayType, DynArrayType, FunctionType, ImplicitArrayType, ImplicitMatrixType, ImplicitVectorType, IntrinsicFunctionType, MatrixType, MethodType, ModuleType, NamedExternalBlockType, PrimitiveType, PushConstantType, SamplerType, StorageType, StructType, TextureType, Type, UniformType, VectorType>;
257266

258267
struct ContainedType
259268
{
@@ -287,6 +296,7 @@ namespace nzsl::Ast
287296
inline bool IsFunctionType(const ExpressionType& type);
288297
inline bool IsImplicitType(const ExpressionType& type);
289298
inline bool IsImplicitArrayType(const ExpressionType& type);
299+
inline bool IsImplicitMatrixType(const ExpressionType& type);
290300
inline bool IsImplicitVectorType(const ExpressionType& type);
291301
inline bool IsIntrinsicFunctionType(const ExpressionType& type);
292302
inline bool IsMatrixType(const ExpressionType& type);
@@ -347,6 +357,8 @@ namespace nzsl::Ast
347357

348358
NZSL_API std::string ToString(const AliasType& type, const Stringifier& stringifier = {});
349359
NZSL_API std::string ToString(const ArrayType& type, const Stringifier& stringifier = {});
360+
NZSL_API std::string ToString(const ImplicitArrayType& type, const Stringifier& stringifier = {});
361+
NZSL_API std::string ToString(const ImplicitMatrixType& type, const Stringifier& stringifier = {});
350362
NZSL_API std::string ToString(const ImplicitVectorType& type, const Stringifier& stringifier = {});
351363
NZSL_API std::string ToString(const DynArrayType& type, const Stringifier& stringifier = {});
352364
NZSL_API std::string ToString(const ExpressionType& type, const Stringifier& stringifier = {});

include/NZSL/Ast/ExpressionType.inl

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,17 @@ namespace nzsl::Ast
111111
}
112112

113113

114+
inline bool ImplicitMatrixType::operator==(const ImplicitMatrixType& rhs) const
115+
{
116+
return columnCount == rhs.columnCount && rowCount && rhs.rowCount;
117+
}
118+
119+
inline bool ImplicitMatrixType::operator!=(const ImplicitMatrixType& rhs) const
120+
{
121+
return !operator==(rhs);
122+
}
123+
124+
114125
inline bool ImplicitVectorType::operator==(const ImplicitVectorType& rhs) const
115126
{
116127
return componentCount == rhs.componentCount;
@@ -281,14 +292,19 @@ namespace nzsl::Ast
281292

282293
inline bool IsImplicitType(const ExpressionType& type)
283294
{
284-
return IsImplicitArrayType(type) || IsImplicitVectorType(type);
295+
return IsImplicitArrayType(type) || IsImplicitMatrixType(type) || IsImplicitVectorType(type);
285296
}
286297

287298
inline bool IsImplicitArrayType(const ExpressionType& type)
288299
{
289300
return std::holds_alternative<ImplicitArrayType>(type);
290301
}
291302

303+
inline bool IsImplicitMatrixType(const ExpressionType& type)
304+
{
305+
return std::holds_alternative<ImplicitMatrixType>(type);
306+
}
307+
292308
inline bool IsImplicitVectorType(const ExpressionType& type)
293309
{
294310
return std::holds_alternative<ImplicitVectorType>(type);
@@ -588,6 +604,15 @@ namespace std
588604
}
589605
};
590606

607+
template<>
608+
struct hash<nzsl::Ast::ImplicitMatrixType>
609+
{
610+
std::size_t operator()(const nzsl::Ast::ImplicitMatrixType& matrixType) const
611+
{
612+
return Nz::HashCombine(matrixType.columnCount, matrixType.rowCount);
613+
}
614+
};
615+
591616
template<>
592617
struct hash<nzsl::Ast::ImplicitVectorType>
593618
{

include/NZSL/GlslWriter.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ namespace nzsl
8080
void Append(const Ast::ExpressionValue<Ast::ExpressionType>& type);
8181
void Append(const Ast::FunctionType& functionType);
8282
void Append(const Ast::ImplicitArrayType& type);
83+
void Append(const Ast::ImplicitMatrixType& type);
8384
void Append(const Ast::ImplicitVectorType& type);
8485
void Append(Ast::InterpolationQualifier interpolation);
8586
void Append(const Ast::IntrinsicFunctionType& intrinsicFunctionType);

include/NZSL/LangWriter.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ namespace nzsl
6666
void Append(const Ast::ExpressionValue<Ast::ExpressionType>& type);
6767
void Append(const Ast::FunctionType& functionType);
6868
void Append(const Ast::ImplicitArrayType& type);
69+
void Append(const Ast::ImplicitMatrixType& type);
6970
void Append(const Ast::ImplicitVectorType& type);
7071
void Append(const Ast::IntrinsicFunctionType& intrinsicFunctionType);
7172
void Append(const Ast::MatrixType& matrixType);

src/NZSL/Ast/AstSerializer.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,12 @@ namespace nzsl::Ast
796796
{
797797
m_serializer.Serialize(std::uint8_t(21));
798798
}
799+
else if constexpr (std::is_same_v<T, ImplicitMatrixType>)
800+
{
801+
m_serializer.Serialize(std::uint8_t(22));
802+
SizeT(arg.columnCount);
803+
SizeT(arg.rowCount);
804+
}
799805
else
800806
static_assert(Nz::AlwaysFalse<T>(), "non-exhaustive visitor");
801807
}, type);
@@ -1261,6 +1267,19 @@ NAZARA_WARNING_GCC_DISABLE("-Wmaybe-uninitialized")
12611267
type = ImplicitArrayType{};
12621268
break;
12631269

1270+
case 22: //< ImplicitMatrixType
1271+
{
1272+
std::size_t columnCount;
1273+
std::size_t rowCount;
1274+
SizeT(columnCount);
1275+
SizeT(rowCount);
1276+
1277+
type = ImplicitMatrixType{
1278+
columnCount, rowCount
1279+
};
1280+
break;
1281+
}
1282+
12641283
default:
12651284
throw std::runtime_error("unexpected type index " + std::to_string(typeIndex));
12661285
}

src/NZSL/Ast/ExpressionType.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ namespace nzsl::Ast
293293
else if constexpr (std::is_same_v<T, NoType> ||
294294
std::is_same_v<T, ArrayType> ||
295295
std::is_same_v<T, ImplicitArrayType> ||
296+
std::is_same_v<T, ImplicitMatrixType> ||
296297
std::is_same_v<T, ImplicitVectorType> ||
297298
std::is_same_v<T, DynArrayType> ||
298299
std::is_same_v<T, FunctionType> ||
@@ -355,6 +356,14 @@ namespace nzsl::Ast
355356
return "array";
356357
}
357358

359+
std::string ToString(const ImplicitMatrixType& type, const Stringifier& /*stringifier*/)
360+
{
361+
if (type.columnCount == type.rowCount)
362+
return fmt::format("mat{}", type.columnCount);
363+
else
364+
return fmt::format("mat{}x{}", type.columnCount, type.rowCount);
365+
}
366+
358367
std::string ToString(const ImplicitVectorType& type, const Stringifier& /*stringifier*/)
359368
{
360369
return fmt::format("vec{}", type.componentCount);

src/NZSL/Ast/Transformations/ResolveTransformer.cpp

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -732,9 +732,12 @@ namespace nzsl::Ast
732732
name = fmt::format("mat{}x{}", columnCount, rowCount);
733733

734734
RegisterPartialType(std::move(name), PartialType{
735-
{ TypeParameterCategory::PrimitiveType }, {},
735+
{}, { TypeParameterCategory::PrimitiveType },
736736
[=](const TypeParameter* parameters, [[maybe_unused]] std::size_t parameterCount, const SourceLocation& sourceLocation) -> ExpressionType
737737
{
738+
if (parameterCount == 0)
739+
return ImplicitMatrixType{ columnCount, rowCount };
740+
738741
assert(parameterCount == 1);
739742
assert(std::holds_alternative<ExpressionType>(*parameters));
740743

@@ -762,18 +765,16 @@ namespace nzsl::Ast
762765
{
763766
if (parameterCount == 0)
764767
return ImplicitVectorType{ componentCount };
765-
else
766-
{
767-
assert(parameterCount == 1);
768-
assert(std::holds_alternative<ExpressionType>(*parameters));
769768

770-
const ExpressionType& exprType = std::get<ExpressionType>(*parameters);
771-
assert(IsPrimitiveType(exprType));
769+
assert(parameterCount == 1);
770+
assert(std::holds_alternative<ExpressionType>(*parameters));
772771

773-
return VectorType {
774-
componentCount, std::get<PrimitiveType>(exprType)
775-
};
776-
}
772+
const ExpressionType& exprType = std::get<ExpressionType>(*parameters);
773+
assert(IsPrimitiveType(exprType));
774+
775+
return VectorType {
776+
componentCount, std::get<PrimitiveType>(exprType)
777+
};
777778
}
778779
});
779780
}
@@ -2276,6 +2277,35 @@ namespace nzsl::Ast
22762277
targetType = std::move(targetArrayType);
22772278
}
22782279
}
2280+
else if (IsImplicitMatrixType(targetType))
2281+
{
2282+
std::optional<PrimitiveType> innerType;
2283+
for (std::size_t i = 0; i < castExpr.expressions.size(); ++i)
2284+
{
2285+
const ExpressionType* exprType = GetResolvedExpressionType(MandatoryExpr(castExpr.expressions[i], castExpr.sourceLocation), true);
2286+
if (!exprType)
2287+
break;
2288+
2289+
if (IsMatrixType(*exprType))
2290+
innerType = std::get<MatrixType>(*exprType).type;
2291+
else if (IsVectorType(*exprType))
2292+
innerType = std::get<VectorType>(*exprType).type;
2293+
else if (IsPrimitiveType(*exprType))
2294+
innerType = std::get<PrimitiveType>(*exprType);
2295+
2296+
if (innerType && !IsLiteralType(*innerType))
2297+
break; //< continue if literal type as we could encounter a non-literal type later (e.g. vec3(1, u32(2), 3))
2298+
}
2299+
2300+
if (innerType)
2301+
{
2302+
const ImplicitMatrixType& implicitMatrixType = std::get<ImplicitMatrixType>(targetType);
2303+
std::size_t columnCount = implicitMatrixType.columnCount;
2304+
std::size_t rowCount = implicitMatrixType.rowCount;
2305+
2306+
targetType = MatrixType{ columnCount, rowCount, *innerType };
2307+
}
2308+
}
22792309
else if (IsImplicitVectorType(targetType))
22802310
{
22812311
std::optional<PrimitiveType> innerType;

src/NZSL/GlslWriter.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,11 @@ namespace nzsl
723723
throw std::runtime_error("unexpected ImplicitArrayType");
724724
}
725725

726+
void GlslWriter::Append(const Ast::ImplicitMatrixType& /*type*/)
727+
{
728+
throw std::runtime_error("unexpected ImplicitMatrixType");
729+
}
730+
726731
void GlslWriter::Append(const Ast::ImplicitVectorType& /*type*/)
727732
{
728733
throw std::runtime_error("unexpected ImplicitVectorType");

src/NZSL/LangWriter.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,11 +300,16 @@ namespace nzsl
300300
throw std::runtime_error("unexpected function type");
301301
}
302302

303-
void LangWriter::Append(const Ast::ImplicitArrayType& /*vecType*/)
303+
void LangWriter::Append(const Ast::ImplicitArrayType& /*arrayType*/)
304304
{
305305
throw std::runtime_error("unexpected ImplicitVectorType");
306306
}
307307

308+
void LangWriter::Append(const Ast::ImplicitMatrixType& /*matrixType*/)
309+
{
310+
throw std::runtime_error("unexpected ImplicitMatrixType");
311+
}
312+
308313
void LangWriter::Append(const Ast::ImplicitVectorType& /*vecType*/)
309314
{
310315
throw std::runtime_error("unexpected ImplicitVectorType");

0 commit comments

Comments
 (0)