From 5dc3c2f57963de93ad03724a01ea48b8585dc15a Mon Sep 17 00:00:00 2001 From: Yong He Date: Wed, 25 Oct 2023 07:50:14 -0700 Subject: Add `IArray`. (#3281) * Initial support for generic interfaces. * Cleanup. * Add generic syntax for interfaces. * Add `IArray`. * Fix. * Fix. * Fix. --------- Co-authored-by: Yong He --- source/slang/core.meta.slang | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) (limited to 'source/slang/core.meta.slang') diff --git a/source/slang/core.meta.slang b/source/slang/core.meta.slang index 3e51d7028..0f600d7c6 100644 --- a/source/slang/core.meta.slang +++ b/source/slang/core.meta.slang @@ -317,10 +317,27 @@ interface __FlagsEnumType : __EnumType { }; +interface IArray +{ + int getCount(); + __subscript(int index) -> T + { + get; + } +} + __generic __magic_type(ArrayExpressionType) -struct Array +struct Array : IArray { + [ForceInline] + int getCount() { return N; } + + __subscript(int index) -> T + { + __intrinsic_op($(kIROp_GetElement)) + get; + } } // The "comma operator" is effectively just a generic function that returns its second @@ -885,7 +902,7 @@ extension int16_t /// An `N` component vector with elements of type `T`. __generic __magic_type(VectorExpressionType) -struct vector +struct vector : IArray { /// The element type of the vector typedef T Element; @@ -900,6 +917,11 @@ struct vector // TODO: we should revise semantic checking so this kind of "identity" conversion is not required __intrinsic_op(0) __init(vector value); + + [ForceInline] + int getCount() { return N; } + + __subscript(int index) -> T { __intrinsic_op($(kIROp_GetElement)) get; } } const int kRowMajorMatrixLayout = $(SLANG_MATRIX_LAYOUT_ROW_MAJOR); @@ -908,10 +930,15 @@ const int kColumnMajorMatrixLayout = $(SLANG_MATRIX_LAYOUT_COLUMN_MAJOR); /// A matrix with `R` rows and `C` columns, with elements of type `T`. __generic __magic_type(MatrixExpressionType) -struct matrix +struct matrix : IArray> { __intrinsic_op($(kIROp_MakeMatrixFromScalar)) __init(T val); + + [ForceInline] + int getCount() { return R; } + + __subscript(int index) -> vector { __intrinsic_op($(kIROp_GetElement)) get; } } ${{{{ -- cgit v1.2.3