From 5483a71781fc41ce900d08d32b1ff241e8ae6b6a Mon Sep 17 00:00:00 2001 From: Sangyub Lee Date: Mon, 1 Jan 2024 02:23:45 +0300 Subject: [PATCH 1/4] Exhaustive type definitions for swizzle --- packages/shader-ast/src/api/swizzles.ts | 72 +++++++++++++++++-------- 1 file changed, 50 insertions(+), 22 deletions(-) diff --git a/packages/shader-ast/src/api/swizzles.ts b/packages/shader-ast/src/api/swizzles.ts index 33b6da7194..9fad58b2ad 100644 --- a/packages/shader-ast/src/api/swizzles.ts +++ b/packages/shader-ast/src/api/swizzles.ts @@ -1,28 +1,56 @@ -// swizzle gen: -// console.log([...permutations("xyz","xyz")].map((x) =>`"${x.join("")}"`).join(" | ")) +type SwizzleXY_1 = "x" | "y"; +type SwizzleXY_2 = `${SwizzleXY_1}${SwizzleXY_1}`; +type SwizzleXY_3 = `${SwizzleXY_2}${SwizzleXY_1}`; +type SwizzleXY_4 = `${SwizzleXY_3}${SwizzleXY_1}`; +type SwizzleRG_1 = "r" | "g"; +type SwizzleRG_2 = `${SwizzleRG_1}${SwizzleRG_1}`; +type SwizzleRG_3 = `${SwizzleRG_2}${SwizzleRG_1}`; +type SwizzleRG_4 = `${SwizzleRG_3}${SwizzleRG_1}`; +type SwizzleST_1 = "s" | "t"; +type SwizzleST_2 = `${SwizzleST_1}${SwizzleST_1}`; +type SwizzleST_3 = `${SwizzleST_2}${SwizzleST_1}`; +type SwizzleST_4 = `${SwizzleST_3}${SwizzleST_1}`; -export type Swizzle2_1 = "x" | "y"; -export type Swizzle2_2 = "xx" | "xy" | "yx" | "yy"; -// prettier-ignore -export type Swizzle2_3 = "xxx" | "xxy" | "xyx" | "xyy" | "yxx" | "yxy" | "yyx" | "yyy"; -// prettier-ignore -export type Swizzle2_4 = "xxxx" | "xxxy" | "xxyx" | "xxyy" | "xyxx" | "xyxy" | "xyyx" | "xyyy" | "yxxx" | "yxxy" | "yxyx" | "yxyy" | "yyxx" | "yyxy" | "yyyx" | "yyyy"; +type SwizzleXYZ_1 = SwizzleXY_1 | "z"; +type SwizzleXYZ_2 = `${SwizzleXYZ_1}${SwizzleXYZ_1}`; +type SwizzleXYZ_3 = `${SwizzleXYZ_2}${SwizzleXYZ_1}`; +type SwizzleXYZ_4 = `${SwizzleXYZ_3}${SwizzleXYZ_1}`; +type SwizzleRGB_1 = SwizzleRG_1 | "b"; +type SwizzleRGB_2 = `${SwizzleRGB_1}${SwizzleRGB_1}`; +type SwizzleRGB_3 = `${SwizzleRGB_2}${SwizzleRGB_1}`; +type SwizzleRGB_4 = `${SwizzleRGB_3}${SwizzleRGB_1}`; +type SwizzleSTP_1 = SwizzleST_1 | "p"; +type SwizzleSTP_2 = `${SwizzleSTP_1}${SwizzleSTP_1}`; +type SwizzleSTP_3 = `${SwizzleSTP_2}${SwizzleSTP_1}`; +type SwizzleSTP_4 = `${SwizzleSTP_3}${SwizzleSTP_1}`; -export type Swizzle3_1 = "x" | "y" | "z"; -// prettier-ignore -export type Swizzle3_2 = "xx" | "xy" | "xz" | "yx" | "yy" | "yz" | "zx" | "zy" | "zz"; -// prettier-ignore -export type Swizzle3_3 = "xxx" | "xxy" | "xxz" | "xyx" | "xyy" | "xyz" | "xzx" | "xzy" | "xzz" | "yxx" | "yxy" | "yxz" | "yyx" | "yyy" | "yyz" | "yzx" | "yzy" | "yzz" | "zxx" | "zxy" | "zxz" | "zyx" | "zyy" | "zyz" | "zzx" | "zzy" | "zzz"; -// prettier-ignore -export type Swizzle3_4 = "xxxx" | "xxxy" | "xxxz" | "xxyx" | "xxyy" | "xxyz" | "xxzx" | "xxzy" | "xxzz" | "xyxx" | "xyxy" | "xyxz" | "xyyx" | "xyyy" | "xyyz" | "xyzx" | "xyzy" | "xyzz" | "xzxx" | "xzxy" | "xzxz" | "xzyx" | "xzyy" | "xzyz" | "xzzx" | "xzzy" | "xzzz" | "yxxx" | "yxxy" | "yxxz" | "yxyx" | "yxyy" | "yxyz" | "yxzx" | "yxzy" | "yxzz" | "yyxx" | "yyxy" | "yyxz" | "yyyx" | "yyyy" | "yyyz" | "yyzx" | "yyzy" | "yyzz" | "yzxx" | "yzxy" | "yzxz" | "yzyx" | "yzyy" | "yzyz" | "yzzx" | "yzzy" | "yzzz" | "zxxx" | "zxxy" | "zxxz" | "zxyx" | "zxyy" | "zxyz" | "zxzx" | "zxzy" | "zxzz" | "zyxx" | "zyxy" | "zyxz" | "zyyx" | "zyyy" | "zyyz" | "zyzx" | "zyzy" | "zyzz" | "zzxx" | "zzxy" | "zzxz" | "zzyx" | "zzyy" | "zzyz" | "zzzx" | "zzzy" | "zzzz"; +type SwizzleXYZW_1 = SwizzleXYZ_1 | "w"; +type SwizzleXYZW_2 = `${SwizzleXYZW_1}${SwizzleXYZW_1}`; +type SwizzleXYZW_3 = `${SwizzleXYZW_2}${SwizzleXYZW_1}`; +type SwizzleXYZW_4 = `${SwizzleXYZW_3}${SwizzleXYZW_1}`; +type SwizzleRGBA_1 = SwizzleRGB_1 | "a"; +type SwizzleRGBA_2 = `${SwizzleRGBA_1}${SwizzleRGBA_1}`; +type SwizzleRGBA_3 = `${SwizzleRGBA_2}${SwizzleRGBA_1}`; +type SwizzleRGBA_4 = `${SwizzleRGBA_3}${SwizzleRGBA_1}`; +type SwizzleSTPQ_1 = SwizzleSTP_1 | "q"; +type SwizzleSTPQ_2 = `${SwizzleSTPQ_1}${SwizzleSTPQ_1}`; +type SwizzleSTPQ_3 = `${SwizzleSTPQ_2}${SwizzleSTPQ_1}`; +type SwizzleSTPQ_4 = `${SwizzleSTPQ_3}${SwizzleSTPQ_1}`; -export type Swizzle4_1 = "x" | "y" | "z" | "w"; -// prettier-ignore -export type Swizzle4_2 = "xx" | "xy" | "xz" | "xw" | "yx" | "yy" | "yz" | "yw" | "zx" | "zy" | "zz" | "zw" | "wx" | "wy" | "wz" | "ww"; -// prettier-ignore -export type Swizzle4_3 = "xxx" | "xxy" | "xxz" | "xxw" | "xyx" | "xyy" | "xyz" | "xyw" | "xzx" | "xzy" | "xzz" | "xzw" | "xwx" | "xwy" | "xwz" | "xww" | "yxx" | "yxy" | "yxz" | "yxw" | "yyx" | "yyy" | "yyz" | "yyw" | "yzx" | "yzy" | "yzz" | "yzw" | "ywx" | "ywy" | "ywz" | "yww" | "zxx" | "zxy" | "zxz" | "zxw" | "zyx" | "zyy" | "zyz" | "zyw" | "zzx" | "zzy" | "zzz" | "zzw" | "zwx" | "zwy" | "zwz" | "zww" | "wxx" | "wxy" | "wxz" | "wxw" | "wyx" | "wyy" | "wyz" | "wyw" | "wzx" | "wzy" | "wzz" | "wzw" | "wwx" | "wwy" | "wwz" | "www"; -// prettier-ignore -export type Swizzle4_4 = "xxxx" | "xxxy" | "xxxz" | "xxxw" | "xxyx" | "xxyy" | "xxyz" | "xxyw" | "xxzx" | "xxzy" | "xxzz" | "xxzw" | "xxwx" | "xxwy" | "xxwz" | "xxww" | "xyxx" | "xyxy" | "xyxz" | "xyxw" | "xyyx" | "xyyy" | "xyyz" | "xyyw" | "xyzx" | "xyzy" | "xyzz" | "xyzw" | "xywx" | "xywy" | "xywz" | "xyww" | "xzxx" | "xzxy" | "xzxz" | "xzxw" | "xzyx" | "xzyy" | "xzyz" | "xzyw" | "xzzx" | "xzzy" | "xzzz" | "xzzw" | "xzwx" | "xzwy" | "xzwz" | "xzww" | "xwxx" | "xwxy" | "xwxz" | "xwxw" | "xwyx" | "xwyy" | "xwyz" | "xwyw" | "xwzx" | "xwzy" | "xwzz" | "xwzw" | "xwwx" | "xwwy" | "xwwz" | "xwww" | "yxxx" | "yxxy" | "yxxz" | "yxxw" | "yxyx" | "yxyy" | "yxyz" | "yxyw" | "yxzx" | "yxzy" | "yxzz" | "yxzw" | "yxwx" | "yxwy" | "yxwz" | "yxww" | "yyxx" | "yyxy" | "yyxz" | "yyxw" | "yyyx" | "yyyy" | "yyyz" | "yyyw" | "yyzx" | "yyzy" | "yyzz" | "yyzw" | "yywx" | "yywy" | "yywz" | "yyww" | "yzxx" | "yzxy" | "yzxz" | "yzxw" | "yzyx" | "yzyy" | "yzyz" | "yzyw" | "yzzx" | "yzzy" | "yzzz" | "yzzw" | "yzwx" | "yzwy" | "yzwz" | "yzww" | "ywxx" | "ywxy" | "ywxz" | "ywxw" | "ywyx" | "ywyy" | "ywyz" | "ywyw" | "ywzx" | "ywzy" | "ywzz" | "ywzw" | "ywwx" | "ywwy" | "ywwz" | "ywww" | "zxxx" | "zxxy" | "zxxz" | "zxxw" | "zxyx" | "zxyy" | "zxyz" | "zxyw" | "zxzx" | "zxzy" | "zxzz" | "zxzw" | "zxwx" | "zxwy" | "zxwz" | "zxww" | "zyxx" | "zyxy" | "zyxz" | "zyxw" | "zyyx" | "zyyy" | "zyyz" | "zyyw" | "zyzx" | "zyzy" | "zyzz" | "zyzw" | "zywx" | "zywy" | "zywz" | "zyww" | "zzxx" | "zzxy" | "zzxz" | "zzxw" | "zzyx" | "zzyy" | "zzyz" | "zzyw" | "zzzx" | "zzzy" | "zzzz" | "zzzw" | "zzwx" | "zzwy" | "zzwz" | "zzww" | "zwxx" | "zwxy" | "zwxz" | "zwxw" | "zwyx" | "zwyy" | "zwyz" | "zwyw" | "zwzx" | "zwzy" | "zwzz" | "zwzw" | "zwwx" | "zwwy" | "zwwz" | "zwww" | "wxxx" | "wxxy" | "wxxz" | "wxxw" | "wxyx" | "wxyy" | "wxyz" | "wxyw" | "wxzx" | "wxzy" | "wxzz" | "wxzw" | "wxwx" | "wxwy" | "wxwz" | "wxww" | "wyxx" | "wyxy" | "wyxz" | "wyxw" | "wyyx" | "wyyy" | "wyyz" | "wyyw" | "wyzx" | "wyzy" | "wyzz" | "wyzw" | "wywx" | "wywy" | "wywz" | "wyww" | "wzxx" | "wzxy" | "wzxz" | "wzxw" | "wzyx" | "wzyy" | "wzyz" | "wzyw" | "wzzx" | "wzzy" | "wzzz" | "wzzw" | "wzwx" | "wzwy" | "wzwz" | "wzww" | "wwxx" | "wwxy" | "wwxz" | "wwxw" | "wwyx" | "wwyy" | "wwyz" | "wwyw" | "wwzx" | "wwzy" | "wwzz" | "wwzw" | "wwwx" | "wwwy" | "wwwz" | "wwww"; +export type Swizzle2_1 = SwizzleXY_1 | SwizzleRG_1 | SwizzleST_1; +export type Swizzle2_2 = SwizzleXY_2 | SwizzleRG_2 | SwizzleST_2; +export type Swizzle2_3 = SwizzleXY_3 | SwizzleRG_3 | SwizzleST_3; +export type Swizzle2_4 = SwizzleXY_4 | SwizzleRG_4 | SwizzleST_4; + +export type Swizzle3_1 = SwizzleXYZ_1 | SwizzleRGB_1 | SwizzleSTP_1; +export type Swizzle3_2 = SwizzleXYZ_2 | SwizzleRGB_2 | SwizzleSTP_2; +export type Swizzle3_3 = SwizzleXYZ_3 | SwizzleRGB_3 | SwizzleSTP_3; +export type Swizzle3_4 = SwizzleXYZ_4 | SwizzleRGB_4 | SwizzleSTP_4; + +export type Swizzle4_1 = SwizzleXYZW_1 | SwizzleRGBA_1 | SwizzleSTPQ_1; +export type Swizzle4_2 = SwizzleXYZW_2 | SwizzleRGBA_2 | SwizzleSTPQ_2; +export type Swizzle4_3 = SwizzleXYZW_3 | SwizzleRGBA_3 | SwizzleSTPQ_3; +export type Swizzle4_4 = SwizzleXYZW_4 | SwizzleRGBA_4 | SwizzleSTPQ_4; export type Swizzle2 = Swizzle2_1 | Swizzle2_2 | Swizzle2_3 | Swizzle2_4; export type Swizzle3 = Swizzle3_1 | Swizzle3_2 | Swizzle3_3 | Swizzle3_4; From 2af9a54ff1e22c1aca30282741f8fb7dfaabfb34 Mon Sep 17 00:00:00 2001 From: Sangyub Lee Date: Wed, 3 Jan 2024 15:19:59 +0300 Subject: [PATCH 2/4] Fall back to stupid enumeration type --- packages/shader-ast/src/api/swizzles.ts | 109 +++++++++++++++-------- packages/shader-ast/test/swizzle.test.ts | 101 +++++++++++++++++++++ 2 files changed, 175 insertions(+), 35 deletions(-) create mode 100644 packages/shader-ast/test/swizzle.test.ts diff --git a/packages/shader-ast/src/api/swizzles.ts b/packages/shader-ast/src/api/swizzles.ts index 9fad58b2ad..d3264f1c3a 100644 --- a/packages/shader-ast/src/api/swizzles.ts +++ b/packages/shader-ast/src/api/swizzles.ts @@ -1,41 +1,80 @@ +// swizzle gen: +// console.log([...permutations("xyz","xyz")].map((x) =>`"${x.join("")}"`).join(" | ")) +// Although it is better to use template literals to generate this, +// we are still using this stupid enumeration, +// because of some historical issues with typescript handling large number of template literals. +// prettier-ignore type SwizzleXY_1 = "x" | "y"; -type SwizzleXY_2 = `${SwizzleXY_1}${SwizzleXY_1}`; -type SwizzleXY_3 = `${SwizzleXY_2}${SwizzleXY_1}`; -type SwizzleXY_4 = `${SwizzleXY_3}${SwizzleXY_1}`; +// prettier-ignore +type SwizzleXY_2 = "xx" | "xy" | "yx" | "yy"; +// prettier-ignore +type SwizzleXY_3 = "xxx" | "xxy" | "xyx" | "xyy" | "yxx" | "yxy" | "yyx" | "yyy"; +// prettier-ignore +type SwizzleXY_4 = "xxxx" | "xxxy" | "xxyx" | "xxyy" | "xyxx" | "xyxy" | "xyyx" | "xyyy" | "yxxx" | "yxxy" | "yxyx" | "yxyy" | "yyxx" | "yyxy" | "yyyx" | "yyyy"; +// prettier-ignore type SwizzleRG_1 = "r" | "g"; -type SwizzleRG_2 = `${SwizzleRG_1}${SwizzleRG_1}`; -type SwizzleRG_3 = `${SwizzleRG_2}${SwizzleRG_1}`; -type SwizzleRG_4 = `${SwizzleRG_3}${SwizzleRG_1}`; +// prettier-ignore +type SwizzleRG_2 = "rr" | "rg" | "gr" | "gg"; +// prettier-ignore +type SwizzleRG_3 = "rrr" | "rrg" | "rgr" | "rgg" | "grr" | "grg" | "ggr" | "ggg"; +// prettier-ignore +type SwizzleRG_4 = "rrrr" | "rrrg" | "rrgr" | "rrgg" | "rgrr" | "rgrg" | "rggr" | "rggg" | "grrr" | "grrg" | "grgr" | "grgg" | "ggrr" | "ggrg" | "gggr" | "gggg"; +// prettier-ignore type SwizzleST_1 = "s" | "t"; -type SwizzleST_2 = `${SwizzleST_1}${SwizzleST_1}`; -type SwizzleST_3 = `${SwizzleST_2}${SwizzleST_1}`; -type SwizzleST_4 = `${SwizzleST_3}${SwizzleST_1}`; - -type SwizzleXYZ_1 = SwizzleXY_1 | "z"; -type SwizzleXYZ_2 = `${SwizzleXYZ_1}${SwizzleXYZ_1}`; -type SwizzleXYZ_3 = `${SwizzleXYZ_2}${SwizzleXYZ_1}`; -type SwizzleXYZ_4 = `${SwizzleXYZ_3}${SwizzleXYZ_1}`; -type SwizzleRGB_1 = SwizzleRG_1 | "b"; -type SwizzleRGB_2 = `${SwizzleRGB_1}${SwizzleRGB_1}`; -type SwizzleRGB_3 = `${SwizzleRGB_2}${SwizzleRGB_1}`; -type SwizzleRGB_4 = `${SwizzleRGB_3}${SwizzleRGB_1}`; -type SwizzleSTP_1 = SwizzleST_1 | "p"; -type SwizzleSTP_2 = `${SwizzleSTP_1}${SwizzleSTP_1}`; -type SwizzleSTP_3 = `${SwizzleSTP_2}${SwizzleSTP_1}`; -type SwizzleSTP_4 = `${SwizzleSTP_3}${SwizzleSTP_1}`; - -type SwizzleXYZW_1 = SwizzleXYZ_1 | "w"; -type SwizzleXYZW_2 = `${SwizzleXYZW_1}${SwizzleXYZW_1}`; -type SwizzleXYZW_3 = `${SwizzleXYZW_2}${SwizzleXYZW_1}`; -type SwizzleXYZW_4 = `${SwizzleXYZW_3}${SwizzleXYZW_1}`; -type SwizzleRGBA_1 = SwizzleRGB_1 | "a"; -type SwizzleRGBA_2 = `${SwizzleRGBA_1}${SwizzleRGBA_1}`; -type SwizzleRGBA_3 = `${SwizzleRGBA_2}${SwizzleRGBA_1}`; -type SwizzleRGBA_4 = `${SwizzleRGBA_3}${SwizzleRGBA_1}`; -type SwizzleSTPQ_1 = SwizzleSTP_1 | "q"; -type SwizzleSTPQ_2 = `${SwizzleSTPQ_1}${SwizzleSTPQ_1}`; -type SwizzleSTPQ_3 = `${SwizzleSTPQ_2}${SwizzleSTPQ_1}`; -type SwizzleSTPQ_4 = `${SwizzleSTPQ_3}${SwizzleSTPQ_1}`; +// prettier-ignore +type SwizzleST_2 = "ss" | "st" | "ts" | "tt"; +// prettier-ignore +type SwizzleST_3 = "sss" | "sst" | "sts" | "stt" | "tss" | "tst" | "tts" | "ttt"; +// prettier-ignore +type SwizzleST_4 = "ssss" | "ssst" | "ssts" | "sstt" | "stss" | "stst" | "stts" | "sttt" | "tsss" | "tsst" | "tsts" | "tstt" | "ttss" | "ttst" | "ttts" | "tttt"; +// prettier-ignore +type SwizzleXYZ_1 = "x" | "y" | "z"; +// prettier-ignore +type SwizzleXYZ_2 = "xx" | "xy" | "xz" | "yx" | "yy" | "yz" | "zx" | "zy" | "zz"; +// prettier-ignore +type SwizzleXYZ_3 = "xxx" | "xxy" | "xxz" | "xyx" | "xyy" | "xyz" | "xzx" | "xzy" | "xzz" | "yxx" | "yxy" | "yxz" | "yyx" | "yyy" | "yyz" | "yzx" | "yzy" | "yzz" | "zxx" | "zxy" | "zxz" | "zyx" | "zyy" | "zyz" | "zzx" | "zzy" | "zzz"; +// prettier-ignore +type SwizzleXYZ_4 = "xxxx" | "xxxy" | "xxxz" | "xxyx" | "xxyy" | "xxyz" | "xxzx" | "xxzy" | "xxzz" | "xyxx" | "xyxy" | "xyxz" | "xyyx" | "xyyy" | "xyyz" | "xyzx" | "xyzy" | "xyzz" | "xzxx" | "xzxy" | "xzxz" | "xzyx" | "xzyy" | "xzyz" | "xzzx" | "xzzy" | "xzzz" | "yxxx" | "yxxy" | "yxxz" | "yxyx" | "yxyy" | "yxyz" | "yxzx" | "yxzy" | "yxzz" | "yyxx" | "yyxy" | "yyxz" | "yyyx" | "yyyy" | "yyyz" | "yyzx" | "yyzy" | "yyzz" | "yzxx" | "yzxy" | "yzxz" | "yzyx" | "yzyy" | "yzyz" | "yzzx" | "yzzy" | "yzzz" | "zxxx" | "zxxy" | "zxxz" | "zxyx" | "zxyy" | "zxyz" | "zxzx" | "zxzy" | "zxzz" | "zyxx" | "zyxy" | "zyxz" | "zyyx" | "zyyy" | "zyyz" | "zyzx" | "zyzy" | "zyzz" | "zzxx" | "zzxy" | "zzxz" | "zzyx" | "zzyy" | "zzyz" | "zzzx" | "zzzy" | "zzzz"; +// prettier-ignore +type SwizzleRGB_1 = "r" | "g" | "b"; +// prettier-ignore +type SwizzleRGB_2 = "rr" | "rg" | "rb" | "gr" | "gg" | "gb" | "br" | "bg" | "bb"; +// prettier-ignore +type SwizzleRGB_3 = "rrr" | "rrg" | "rrb" | "rgr" | "rgg" | "rgb" | "rbr" | "rbg" | "rbb" | "grr" | "grg" | "grb" | "ggr" | "ggg" | "ggb" | "gbr" | "gbg" | "gbb" | "brr" | "brg" | "brb" | "bgr" | "bgg" | "bgb" | "bbr" | "bbg" | "bbb"; +// prettier-ignore +type SwizzleRGB_4 = "rrrr" | "rrrg" | "rrrb" | "rrgr" | "rrgg" | "rrgb" | "rrbr" | "rrbg" | "rrbb" | "rgrr" | "rgrg" | "rgrb" | "rggr" | "rggg" | "rggb" | "rgbr" | "rgbg" | "rgbb" | "rbrr" | "rbrg" | "rbrb" | "rbgr" | "rbgg" | "rbgb" | "rbbr" | "rbbg" | "rbbb" | "grrr" | "grrg" | "grrb" | "grgr" | "grgg" | "grgb" | "grbr" | "grbg" | "grbb" | "ggrr" | "ggrg" | "ggrb" | "gggr" | "gggg" | "gggb" | "ggbr" | "ggbg" | "ggbb" | "gbrr" | "gbrg" | "gbrb" | "gbgr" | "gbgg" | "gbgb" | "gbbr" | "gbbg" | "gbbb" | "brrr" | "brrg" | "brrb" | "brgr" | "brgg" | "brgb" | "brbr" | "brbg" | "brbb" | "bgrr" | "bgrg" | "bgrb" | "bggr" | "bggg" | "bggb" | "bgbr" | "bgbg" | "bgbb" | "bbrr" | "bbrg" | "bbrb" | "bbgr" | "bbgg" | "bbgb" | "bbbr" | "bbbg" | "bbbb"; +// prettier-ignore +type SwizzleSTP_1 = "s" | "t" | "p"; +// prettier-ignore +type SwizzleSTP_2 = "ss" | "st" | "sp" | "ts" | "tt" | "tp" | "ps" | "pt" | "pp"; +// prettier-ignore +type SwizzleSTP_3 = "sss" | "sst" | "ssp" | "sts" | "stt" | "stp" | "sps" | "spt" | "spp" | "tss" | "tst" | "tsp" | "tts" | "ttt" | "ttp" | "tps" | "tpt" | "tpp" | "pss" | "pst" | "psp" | "pts" | "ptt" | "ptp" | "pps" | "ppt" | "ppp"; +// prettier-ignore +type SwizzleSTP_4 = "ssss" | "ssst" | "sssp" | "ssts" | "sstt" | "sstp" | "ssps" | "sspt" | "sspp" | "stss" | "stst" | "stsp" | "stts" | "sttt" | "sttp" | "stps" | "stpt" | "stpp" | "spss" | "spst" | "spsp" | "spts" | "sptt" | "sptp" | "spps" | "sppt" | "sppp" | "tsss" | "tsst" | "tssp" | "tsts" | "tstt" | "tstp" | "tsps" | "tspt" | "tspp" | "ttss" | "ttst" | "ttsp" | "ttts" | "tttt" | "tttp" | "ttps" | "ttpt" | "ttpp" | "tpss" | "tpst" | "tpsp" | "tpts" | "tptt" | "tptp" | "tpps" | "tppt" | "tppp" | "psss" | "psst" | "pssp" | "psts" | "pstt" | "pstp" | "psps" | "pspt" | "pspp" | "ptss" | "ptst" | "ptsp" | "ptts" | "pttt" | "pttp" | "ptps" | "ptpt" | "ptpp" | "ppss" | "ppst" | "ppsp" | "ppts" | "pptt" | "pptp" | "ppps" | "pppt" | "pppp"; +// prettier-ignore +type SwizzleXYZW_1 = "x" | "y" | "z" | "w"; +// prettier-ignore +type SwizzleXYZW_2 = "xx" | "xy" | "xz" | "xw" | "yx" | "yy" | "yz" | "yw" | "zx" | "zy" | "zz" | "zw" | "wx" | "wy" | "wz" | "ww"; +// prettier-ignore +type SwizzleXYZW_3 = "xxx" | "xxy" | "xxz" | "xxw" | "xyx" | "xyy" | "xyz" | "xyw" | "xzx" | "xzy" | "xzz" | "xzw" | "xwx" | "xwy" | "xwz" | "xww" | "yxx" | "yxy" | "yxz" | "yxw" | "yyx" | "yyy" | "yyz" | "yyw" | "yzx" | "yzy" | "yzz" | "yzw" | "ywx" | "ywy" | "ywz" | "yww" | "zxx" | "zxy" | "zxz" | "zxw" | "zyx" | "zyy" | "zyz" | "zyw" | "zzx" | "zzy" | "zzz" | "zzw" | "zwx" | "zwy" | "zwz" | "zww" | "wxx" | "wxy" | "wxz" | "wxw" | "wyx" | "wyy" | "wyz" | "wyw" | "wzx" | "wzy" | "wzz" | "wzw" | "wwx" | "wwy" | "wwz" | "www"; +// prettier-ignore +type SwizzleXYZW_4 = "xxxx" | "xxxy" | "xxxz" | "xxxw" | "xxyx" | "xxyy" | "xxyz" | "xxyw" | "xxzx" | "xxzy" | "xxzz" | "xxzw" | "xxwx" | "xxwy" | "xxwz" | "xxww" | "xyxx" | "xyxy" | "xyxz" | "xyxw" | "xyyx" | "xyyy" | "xyyz" | "xyyw" | "xyzx" | "xyzy" | "xyzz" | "xyzw" | "xywx" | "xywy" | "xywz" | "xyww" | "xzxx" | "xzxy" | "xzxz" | "xzxw" | "xzyx" | "xzyy" | "xzyz" | "xzyw" | "xzzx" | "xzzy" | "xzzz" | "xzzw" | "xzwx" | "xzwy" | "xzwz" | "xzww" | "xwxx" | "xwxy" | "xwxz" | "xwxw" | "xwyx" | "xwyy" | "xwyz" | "xwyw" | "xwzx" | "xwzy" | "xwzz" | "xwzw" | "xwwx" | "xwwy" | "xwwz" | "xwww" | "yxxx" | "yxxy" | "yxxz" | "yxxw" | "yxyx" | "yxyy" | "yxyz" | "yxyw" | "yxzx" | "yxzy" | "yxzz" | "yxzw" | "yxwx" | "yxwy" | "yxwz" | "yxww" | "yyxx" | "yyxy" | "yyxz" | "yyxw" | "yyyx" | "yyyy" | "yyyz" | "yyyw" | "yyzx" | "yyzy" | "yyzz" | "yyzw" | "yywx" | "yywy" | "yywz" | "yyww" | "yzxx" | "yzxy" | "yzxz" | "yzxw" | "yzyx" | "yzyy" | "yzyz" | "yzyw" | "yzzx" | "yzzy" | "yzzz" | "yzzw" | "yzwx" | "yzwy" | "yzwz" | "yzww" | "ywxx" | "ywxy" | "ywxz" | "ywxw" | "ywyx" | "ywyy" | "ywyz" | "ywyw" | "ywzx" | "ywzy" | "ywzz" | "ywzw" | "ywwx" | "ywwy" | "ywwz" | "ywww" | "zxxx" | "zxxy" | "zxxz" | "zxxw" | "zxyx" | "zxyy" | "zxyz" | "zxyw" | "zxzx" | "zxzy" | "zxzz" | "zxzw" | "zxwx" | "zxwy" | "zxwz" | "zxww" | "zyxx" | "zyxy" | "zyxz" | "zyxw" | "zyyx" | "zyyy" | "zyyz" | "zyyw" | "zyzx" | "zyzy" | "zyzz" | "zyzw" | "zywx" | "zywy" | "zywz" | "zyww" | "zzxx" | "zzxy" | "zzxz" | "zzxw" | "zzyx" | "zzyy" | "zzyz" | "zzyw" | "zzzx" | "zzzy" | "zzzz" | "zzzw" | "zzwx" | "zzwy" | "zzwz" | "zzww" | "zwxx" | "zwxy" | "zwxz" | "zwxw" | "zwyx" | "zwyy" | "zwyz" | "zwyw" | "zwzx" | "zwzy" | "zwzz" | "zwzw" | "zwwx" | "zwwy" | "zwwz" | "zwww" | "wxxx" | "wxxy" | "wxxz" | "wxxw" | "wxyx" | "wxyy" | "wxyz" | "wxyw" | "wxzx" | "wxzy" | "wxzz" | "wxzw" | "wxwx" | "wxwy" | "wxwz" | "wxww" | "wyxx" | "wyxy" | "wyxz" | "wyxw" | "wyyx" | "wyyy" | "wyyz" | "wyyw" | "wyzx" | "wyzy" | "wyzz" | "wyzw" | "wywx" | "wywy" | "wywz" | "wyww" | "wzxx" | "wzxy" | "wzxz" | "wzxw" | "wzyx" | "wzyy" | "wzyz" | "wzyw" | "wzzx" | "wzzy" | "wzzz" | "wzzw" | "wzwx" | "wzwy" | "wzwz" | "wzww" | "wwxx" | "wwxy" | "wwxz" | "wwxw" | "wwyx" | "wwyy" | "wwyz" | "wwyw" | "wwzx" | "wwzy" | "wwzz" | "wwzw" | "wwwx" | "wwwy" | "wwwz" | "wwww"; +// prettier-ignore +type SwizzleRGBA_1 = "r" | "g" | "b" | "a"; +// prettier-ignore +type SwizzleRGBA_2 = "rr" | "rg" | "rb" | "ra" | "gr" | "gg" | "gb" | "ga" | "br" | "bg" | "bb" | "ba" | "ar" | "ag" | "ab" | "aa"; +// prettier-ignore +type SwizzleRGBA_3 = "rrr" | "rrg" | "rrb" | "rra" | "rgr" | "rgg" | "rgb" | "rga" | "rbr" | "rbg" | "rbb" | "rba" | "rar" | "rag" | "rab" | "raa" | "grr" | "grg" | "grb" | "gra" | "ggr" | "ggg" | "ggb" | "gga" | "gbr" | "gbg" | "gbb" | "gba" | "gar" | "gag" | "gab" | "gaa" | "brr" | "brg" | "brb" | "bra" | "bgr" | "bgg" | "bgb" | "bga" | "bbr" | "bbg" | "bbb" | "bba" | "bar" | "bag" | "bab" | "baa" | "arr" | "arg" | "arb" | "ara" | "agr" | "agg" | "agb" | "aga" | "abr" | "abg" | "abb" | "aba" | "aar" | "aag" | "aab" | "aaa"; +// prettier-ignore +type SwizzleRGBA_4 = "rrrr" | "rrrg" | "rrrb" | "rrra" | "rrgr" | "rrgg" | "rrgb" | "rrga" | "rrbr" | "rrbg" | "rrbb" | "rrba" | "rrar" | "rrag" | "rrab" | "rraa" | "rgrr" | "rgrg" | "rgrb" | "rgra" | "rggr" | "rggg" | "rggb" | "rgga" | "rgbr" | "rgbg" | "rgbb" | "rgba" | "rgar" | "rgag" | "rgab" | "rgaa" | "rbrr" | "rbrg" | "rbrb" | "rbra" | "rbgr" | "rbgg" | "rbgb" | "rbga" | "rbbr" | "rbbg" | "rbbb" | "rbba" | "rbar" | "rbag" | "rbab" | "rbaa" | "rarr" | "rarg" | "rarb" | "rara" | "ragr" | "ragg" | "ragb" | "raga" | "rabr" | "rabg" | "rabb" | "raba" | "raar" | "raag" | "raab" | "raaa" | "grrr" | "grrg" | "grrb" | "grra" | "grgr" | "grgg" | "grgb" | "grga" | "grbr" | "grbg" | "grbb" | "grba" | "grar" | "grag" | "grab" | "graa" | "ggrr" | "ggrg" | "ggrb" | "ggra" | "gggr" | "gggg" | "gggb" | "ggga" | "ggbr" | "ggbg" | "ggbb" | "ggba" | "ggar" | "ggag" | "ggab" | "ggaa" | "gbrr" | "gbrg" | "gbrb" | "gbra" | "gbgr" | "gbgg" | "gbgb" | "gbga" | "gbbr" | "gbbg" | "gbbb" | "gbba" | "gbar" | "gbag" | "gbab" | "gbaa" | "garr" | "garg" | "garb" | "gara" | "gagr" | "gagg" | "gagb" | "gaga" | "gabr" | "gabg" | "gabb" | "gaba" | "gaar" | "gaag" | "gaab" | "gaaa" | "brrr" | "brrg" | "brrb" | "brra" | "brgr" | "brgg" | "brgb" | "brga" | "brbr" | "brbg" | "brbb" | "brba" | "brar" | "brag" | "brab" | "braa" | "bgrr" | "bgrg" | "bgrb" | "bgra" | "bggr" | "bggg" | "bggb" | "bgga" | "bgbr" | "bgbg" | "bgbb" | "bgba" | "bgar" | "bgag" | "bgab" | "bgaa" | "bbrr" | "bbrg" | "bbrb" | "bbra" | "bbgr" | "bbgg" | "bbgb" | "bbga" | "bbbr" | "bbbg" | "bbbb" | "bbba" | "bbar" | "bbag" | "bbab" | "bbaa" | "barr" | "barg" | "barb" | "bara" | "bagr" | "bagg" | "bagb" | "baga" | "babr" | "babg" | "babb" | "baba" | "baar" | "baag" | "baab" | "baaa" | "arrr" | "arrg" | "arrb" | "arra" | "argr" | "argg" | "argb" | "arga" | "arbr" | "arbg" | "arbb" | "arba" | "arar" | "arag" | "arab" | "araa" | "agrr" | "agrg" | "agrb" | "agra" | "aggr" | "aggg" | "aggb" | "agga" | "agbr" | "agbg" | "agbb" | "agba" | "agar" | "agag" | "agab" | "agaa" | "abrr" | "abrg" | "abrb" | "abra" | "abgr" | "abgg" | "abgb" | "abga" | "abbr" | "abbg" | "abbb" | "abba" | "abar" | "abag" | "abab" | "abaa" | "aarr" | "aarg" | "aarb" | "aara" | "aagr" | "aagg" | "aagb" | "aaga" | "aabr" | "aabg" | "aabb" | "aaba" | "aaar" | "aaag" | "aaab" | "aaaa"; +// prettier-ignore +type SwizzleSTPQ_1 = "s" | "t" | "p" | "q"; +// prettier-ignore +type SwizzleSTPQ_2 = "ss" | "st" | "sp" | "sq" | "ts" | "tt" | "tp" | "tq" | "ps" | "pt" | "pp" | "pq" | "qs" | "qt" | "qp" | "qq"; +// prettier-ignore +type SwizzleSTPQ_3 = "sss" | "sst" | "ssp" | "ssq" | "sts" | "stt" | "stp" | "stq" | "sps" | "spt" | "spp" | "spq" | "sqs" | "sqt" | "sqp" | "sqq" | "tss" | "tst" | "tsp" | "tsq" | "tts" | "ttt" | "ttp" | "ttq" | "tps" | "tpt" | "tpp" | "tpq" | "tqs" | "tqt" | "tqp" | "tqq" | "pss" | "pst" | "psp" | "psq" | "pts" | "ptt" | "ptp" | "ptq" | "pps" | "ppt" | "ppp" | "ppq" | "pqs" | "pqt" | "pqp" | "pqq" | "qss" | "qst" | "qsp" | "qsq" | "qts" | "qtt" | "qtp" | "qtq" | "qps" | "qpt" | "qpp" | "qpq" | "qqs" | "qqt" | "qqp" | "qqq"; +// prettier-ignore +type SwizzleSTPQ_4 = "ssss" | "ssst" | "sssp" | "sssq" | "ssts" | "sstt" | "sstp" | "sstq" | "ssps" | "sspt" | "sspp" | "sspq" | "ssqs" | "ssqt" | "ssqp" | "ssqq" | "stss" | "stst" | "stsp" | "stsq" | "stts" | "sttt" | "sttp" | "sttq" | "stps" | "stpt" | "stpp" | "stpq" | "stqs" | "stqt" | "stqp" | "stqq" | "spss" | "spst" | "spsp" | "spsq" | "spts" | "sptt" | "sptp" | "sptq" | "spps" | "sppt" | "sppp" | "sppq" | "spqs" | "spqt" | "spqp" | "spqq" | "sqss" | "sqst" | "sqsp" | "sqsq" | "sqts" | "sqtt" | "sqtp" | "sqtq" | "sqps" | "sqpt" | "sqpp" | "sqpq" | "sqqs" | "sqqt" | "sqqp" | "sqqq" | "tsss" | "tsst" | "tssp" | "tssq" | "tsts" | "tstt" | "tstp" | "tstq" | "tsps" | "tspt" | "tspp" | "tspq" | "tsqs" | "tsqt" | "tsqp" | "tsqq" | "ttss" | "ttst" | "ttsp" | "ttsq" | "ttts" | "tttt" | "tttp" | "tttq" | "ttps" | "ttpt" | "ttpp" | "ttpq" | "ttqs" | "ttqt" | "ttqp" | "ttqq" | "tpss" | "tpst" | "tpsp" | "tpsq" | "tpts" | "tptt" | "tptp" | "tptq" | "tpps" | "tppt" | "tppp" | "tppq" | "tpqs" | "tpqt" | "tpqp" | "tpqq" | "tqss" | "tqst" | "tqsp" | "tqsq" | "tqts" | "tqtt" | "tqtp" | "tqtq" | "tqps" | "tqpt" | "tqpp" | "tqpq" | "tqqs" | "tqqt" | "tqqp" | "tqqq" | "psss" | "psst" | "pssp" | "pssq" | "psts" | "pstt" | "pstp" | "pstq" | "psps" | "pspt" | "pspp" | "pspq" | "psqs" | "psqt" | "psqp" | "psqq" | "ptss" | "ptst" | "ptsp" | "ptsq" | "ptts" | "pttt" | "pttp" | "pttq" | "ptps" | "ptpt" | "ptpp" | "ptpq" | "ptqs" | "ptqt" | "ptqp" | "ptqq" | "ppss" | "ppst" | "ppsp" | "ppsq" | "ppts" | "pptt" | "pptp" | "pptq" | "ppps" | "pppt" | "pppp" | "pppq" | "ppqs" | "ppqt" | "ppqp" | "ppqq" | "pqss" | "pqst" | "pqsp" | "pqsq" | "pqts" | "pqtt" | "pqtp" | "pqtq" | "pqps" | "pqpt" | "pqpp" | "pqpq" | "pqqs" | "pqqt" | "pqqp" | "pqqq" | "qsss" | "qsst" | "qssp" | "qssq" | "qsts" | "qstt" | "qstp" | "qstq" | "qsps" | "qspt" | "qspp" | "qspq" | "qsqs" | "qsqt" | "qsqp" | "qsqq" | "qtss" | "qtst" | "qtsp" | "qtsq" | "qtts" | "qttt" | "qttp" | "qttq" | "qtps" | "qtpt" | "qtpp" | "qtpq" | "qtqs" | "qtqt" | "qtqp" | "qtqq" | "qpss" | "qpst" | "qpsp" | "qpsq" | "qpts" | "qptt" | "qptp" | "qptq" | "qpps" | "qppt" | "qppp" | "qppq" | "qpqs" | "qpqt" | "qpqp" | "qpqq" | "qqss" | "qqst" | "qqsp" | "qqsq" | "qqts" | "qqtt" | "qqtp" | "qqtq" | "qqps" | "qqpt" | "qqpp" | "qqpq" | "qqqs" | "qqqt" | "qqqp" | "qqqq"; export type Swizzle2_1 = SwizzleXY_1 | SwizzleRG_1 | SwizzleST_1; export type Swizzle2_2 = SwizzleXY_2 | SwizzleRG_2 | SwizzleST_2; diff --git a/packages/shader-ast/test/swizzle.test.ts b/packages/shader-ast/test/swizzle.test.ts new file mode 100644 index 0000000000..37d2d52256 --- /dev/null +++ b/packages/shader-ast/test/swizzle.test.ts @@ -0,0 +1,101 @@ +// The types here are more smart alternative of +// api/swizzles.ts, however, not shipped for the user yet. +// They are used as internal testing purposes to see if something +// gets accidently missing in the enumeration. +import type * as api from "../src/api/swizzles.ts"; +import { test } from "bun:test"; + +type SwizzleXY_1 = "x" | "y"; +type SwizzleXY_2 = `${SwizzleXY_1}${SwizzleXY_1}`; +type SwizzleXY_3 = `${SwizzleXY_2}${SwizzleXY_1}`; +type SwizzleXY_4 = `${SwizzleXY_3}${SwizzleXY_1}`; +type SwizzleRG_1 = "r" | "g"; +type SwizzleRG_2 = `${SwizzleRG_1}${SwizzleRG_1}`; +type SwizzleRG_3 = `${SwizzleRG_2}${SwizzleRG_1}`; +type SwizzleRG_4 = `${SwizzleRG_3}${SwizzleRG_1}`; +type SwizzleST_1 = "s" | "t"; +type SwizzleST_2 = `${SwizzleST_1}${SwizzleST_1}`; +type SwizzleST_3 = `${SwizzleST_2}${SwizzleST_1}`; +type SwizzleST_4 = `${SwizzleST_3}${SwizzleST_1}`; + +type SwizzleXYZ_1 = SwizzleXY_1 | "z"; +type SwizzleXYZ_2 = `${SwizzleXYZ_1}${SwizzleXYZ_1}`; +type SwizzleXYZ_3 = `${SwizzleXYZ_2}${SwizzleXYZ_1}`; +type SwizzleXYZ_4 = `${SwizzleXYZ_3}${SwizzleXYZ_1}`; +type SwizzleRGB_1 = SwizzleRG_1 | "b"; +type SwizzleRGB_2 = `${SwizzleRGB_1}${SwizzleRGB_1}`; +type SwizzleRGB_3 = `${SwizzleRGB_2}${SwizzleRGB_1}`; +type SwizzleRGB_4 = `${SwizzleRGB_3}${SwizzleRGB_1}`; +type SwizzleSTP_1 = SwizzleST_1 | "p"; +type SwizzleSTP_2 = `${SwizzleSTP_1}${SwizzleSTP_1}`; +type SwizzleSTP_3 = `${SwizzleSTP_2}${SwizzleSTP_1}`; +type SwizzleSTP_4 = `${SwizzleSTP_3}${SwizzleSTP_1}`; + +type SwizzleXYZW_1 = SwizzleXYZ_1 | "w"; +type SwizzleXYZW_2 = `${SwizzleXYZW_1}${SwizzleXYZW_1}`; +type SwizzleXYZW_3 = `${SwizzleXYZW_2}${SwizzleXYZW_1}`; +type SwizzleXYZW_4 = `${SwizzleXYZW_3}${SwizzleXYZW_1}`; +type SwizzleRGBA_1 = SwizzleRGB_1 | "a"; +type SwizzleRGBA_2 = `${SwizzleRGBA_1}${SwizzleRGBA_1}`; +type SwizzleRGBA_3 = `${SwizzleRGBA_2}${SwizzleRGBA_1}`; +type SwizzleRGBA_4 = `${SwizzleRGBA_3}${SwizzleRGBA_1}`; +type SwizzleSTPQ_1 = SwizzleSTP_1 | "q"; +type SwizzleSTPQ_2 = `${SwizzleSTPQ_1}${SwizzleSTPQ_1}`; +type SwizzleSTPQ_3 = `${SwizzleSTPQ_2}${SwizzleSTPQ_1}`; +type SwizzleSTPQ_4 = `${SwizzleSTPQ_3}${SwizzleSTPQ_1}`; + +type Swizzle2_1 = SwizzleXY_1 | SwizzleRG_1 | SwizzleST_1; +type Swizzle2_2 = SwizzleXY_2 | SwizzleRG_2 | SwizzleST_2; +type Swizzle2_3 = SwizzleXY_3 | SwizzleRG_3 | SwizzleST_3; +type Swizzle2_4 = SwizzleXY_4 | SwizzleRG_4 | SwizzleST_4; + +type Swizzle3_1 = SwizzleXYZ_1 | SwizzleRGB_1 | SwizzleSTP_1; +type Swizzle3_2 = SwizzleXYZ_2 | SwizzleRGB_2 | SwizzleSTP_2; +type Swizzle3_3 = SwizzleXYZ_3 | SwizzleRGB_3 | SwizzleSTP_3; +type Swizzle3_4 = SwizzleXYZ_4 | SwizzleRGB_4 | SwizzleSTP_4; + +type Swizzle4_1 = SwizzleXYZW_1 | SwizzleRGBA_1 | SwizzleSTPQ_1; +type Swizzle4_2 = SwizzleXYZW_2 | SwizzleRGBA_2 | SwizzleSTPQ_2; +type Swizzle4_3 = SwizzleXYZW_3 | SwizzleRGBA_3 | SwizzleSTPQ_3; +type Swizzle4_4 = SwizzleXYZW_4 | SwizzleRGBA_4 | SwizzleSTPQ_4; + +type Swizzle2 = Swizzle2_1 | Swizzle2_2 | Swizzle2_3 | Swizzle2_4; +type Swizzle3 = Swizzle3_1 | Swizzle3_2 | Swizzle3_3 | Swizzle3_4; +type Swizzle4 = Swizzle4_1 | Swizzle4_2 | Swizzle4_3 | Swizzle4_4; + +test("Test exhaustiveness of swizzle types", () => { + (_: Swizzle2_1): api.Swizzle2_1 => _; + (_: Swizzle2_2): api.Swizzle2_2 => _; + (_: Swizzle2_3): api.Swizzle2_3 => _; + (_: Swizzle2_4): api.Swizzle2_4 => _; + (_: Swizzle3_1): api.Swizzle3_1 => _; + (_: Swizzle3_2): api.Swizzle3_2 => _; + (_: Swizzle3_3): api.Swizzle3_3 => _; + (_: Swizzle3_4): api.Swizzle3_4 => _; + (_: Swizzle4_1): api.Swizzle4_1 => _; + (_: Swizzle4_2): api.Swizzle4_2 => _; + (_: Swizzle4_3): api.Swizzle4_3 => _; + (_: Swizzle4_4): api.Swizzle4_4 => _; + (_: Swizzle2): api.Swizzle2 => _; + (_: Swizzle3): api.Swizzle3 => _; + (_: Swizzle4): api.Swizzle4 => _; + // We flip the types and test exhaustiveness again + // The existance of both function + // (a: A) => B and (b: B) => A + // guarantees that A and B are the same type + (_: api.Swizzle2_1): Swizzle2_1 => _; + (_: api.Swizzle2_2): Swizzle2_2 => _; + (_: api.Swizzle2_3): Swizzle2_3 => _; + (_: api.Swizzle2_4): Swizzle2_4 => _; + (_: api.Swizzle3_1): Swizzle3_1 => _; + (_: api.Swizzle3_2): Swizzle3_2 => _; + (_: api.Swizzle3_3): Swizzle3_3 => _; + (_: api.Swizzle3_4): Swizzle3_4 => _; + (_: api.Swizzle4_1): Swizzle4_1 => _; + (_: api.Swizzle4_2): Swizzle4_2 => _; + (_: api.Swizzle4_3): Swizzle4_3 => _; + (_: api.Swizzle4_4): Swizzle4_4 => _; + (_: api.Swizzle2): Swizzle2 => _; + (_: api.Swizzle3): Swizzle3 => _; + (_: api.Swizzle4): Swizzle4 => _; +}); From c18639dde8c381fd15bef6cdcfb009641229ffee Mon Sep 17 00:00:00 2001 From: Sangyub Lee Date: Thu, 4 Jan 2024 14:05:16 +0300 Subject: [PATCH 3/4] Remove test file --- packages/shader-ast/test/swizzle.test.ts | 101 ----------------------- 1 file changed, 101 deletions(-) delete mode 100644 packages/shader-ast/test/swizzle.test.ts diff --git a/packages/shader-ast/test/swizzle.test.ts b/packages/shader-ast/test/swizzle.test.ts deleted file mode 100644 index 37d2d52256..0000000000 --- a/packages/shader-ast/test/swizzle.test.ts +++ /dev/null @@ -1,101 +0,0 @@ -// The types here are more smart alternative of -// api/swizzles.ts, however, not shipped for the user yet. -// They are used as internal testing purposes to see if something -// gets accidently missing in the enumeration. -import type * as api from "../src/api/swizzles.ts"; -import { test } from "bun:test"; - -type SwizzleXY_1 = "x" | "y"; -type SwizzleXY_2 = `${SwizzleXY_1}${SwizzleXY_1}`; -type SwizzleXY_3 = `${SwizzleXY_2}${SwizzleXY_1}`; -type SwizzleXY_4 = `${SwizzleXY_3}${SwizzleXY_1}`; -type SwizzleRG_1 = "r" | "g"; -type SwizzleRG_2 = `${SwizzleRG_1}${SwizzleRG_1}`; -type SwizzleRG_3 = `${SwizzleRG_2}${SwizzleRG_1}`; -type SwizzleRG_4 = `${SwizzleRG_3}${SwizzleRG_1}`; -type SwizzleST_1 = "s" | "t"; -type SwizzleST_2 = `${SwizzleST_1}${SwizzleST_1}`; -type SwizzleST_3 = `${SwizzleST_2}${SwizzleST_1}`; -type SwizzleST_4 = `${SwizzleST_3}${SwizzleST_1}`; - -type SwizzleXYZ_1 = SwizzleXY_1 | "z"; -type SwizzleXYZ_2 = `${SwizzleXYZ_1}${SwizzleXYZ_1}`; -type SwizzleXYZ_3 = `${SwizzleXYZ_2}${SwizzleXYZ_1}`; -type SwizzleXYZ_4 = `${SwizzleXYZ_3}${SwizzleXYZ_1}`; -type SwizzleRGB_1 = SwizzleRG_1 | "b"; -type SwizzleRGB_2 = `${SwizzleRGB_1}${SwizzleRGB_1}`; -type SwizzleRGB_3 = `${SwizzleRGB_2}${SwizzleRGB_1}`; -type SwizzleRGB_4 = `${SwizzleRGB_3}${SwizzleRGB_1}`; -type SwizzleSTP_1 = SwizzleST_1 | "p"; -type SwizzleSTP_2 = `${SwizzleSTP_1}${SwizzleSTP_1}`; -type SwizzleSTP_3 = `${SwizzleSTP_2}${SwizzleSTP_1}`; -type SwizzleSTP_4 = `${SwizzleSTP_3}${SwizzleSTP_1}`; - -type SwizzleXYZW_1 = SwizzleXYZ_1 | "w"; -type SwizzleXYZW_2 = `${SwizzleXYZW_1}${SwizzleXYZW_1}`; -type SwizzleXYZW_3 = `${SwizzleXYZW_2}${SwizzleXYZW_1}`; -type SwizzleXYZW_4 = `${SwizzleXYZW_3}${SwizzleXYZW_1}`; -type SwizzleRGBA_1 = SwizzleRGB_1 | "a"; -type SwizzleRGBA_2 = `${SwizzleRGBA_1}${SwizzleRGBA_1}`; -type SwizzleRGBA_3 = `${SwizzleRGBA_2}${SwizzleRGBA_1}`; -type SwizzleRGBA_4 = `${SwizzleRGBA_3}${SwizzleRGBA_1}`; -type SwizzleSTPQ_1 = SwizzleSTP_1 | "q"; -type SwizzleSTPQ_2 = `${SwizzleSTPQ_1}${SwizzleSTPQ_1}`; -type SwizzleSTPQ_3 = `${SwizzleSTPQ_2}${SwizzleSTPQ_1}`; -type SwizzleSTPQ_4 = `${SwizzleSTPQ_3}${SwizzleSTPQ_1}`; - -type Swizzle2_1 = SwizzleXY_1 | SwizzleRG_1 | SwizzleST_1; -type Swizzle2_2 = SwizzleXY_2 | SwizzleRG_2 | SwizzleST_2; -type Swizzle2_3 = SwizzleXY_3 | SwizzleRG_3 | SwizzleST_3; -type Swizzle2_4 = SwizzleXY_4 | SwizzleRG_4 | SwizzleST_4; - -type Swizzle3_1 = SwizzleXYZ_1 | SwizzleRGB_1 | SwizzleSTP_1; -type Swizzle3_2 = SwizzleXYZ_2 | SwizzleRGB_2 | SwizzleSTP_2; -type Swizzle3_3 = SwizzleXYZ_3 | SwizzleRGB_3 | SwizzleSTP_3; -type Swizzle3_4 = SwizzleXYZ_4 | SwizzleRGB_4 | SwizzleSTP_4; - -type Swizzle4_1 = SwizzleXYZW_1 | SwizzleRGBA_1 | SwizzleSTPQ_1; -type Swizzle4_2 = SwizzleXYZW_2 | SwizzleRGBA_2 | SwizzleSTPQ_2; -type Swizzle4_3 = SwizzleXYZW_3 | SwizzleRGBA_3 | SwizzleSTPQ_3; -type Swizzle4_4 = SwizzleXYZW_4 | SwizzleRGBA_4 | SwizzleSTPQ_4; - -type Swizzle2 = Swizzle2_1 | Swizzle2_2 | Swizzle2_3 | Swizzle2_4; -type Swizzle3 = Swizzle3_1 | Swizzle3_2 | Swizzle3_3 | Swizzle3_4; -type Swizzle4 = Swizzle4_1 | Swizzle4_2 | Swizzle4_3 | Swizzle4_4; - -test("Test exhaustiveness of swizzle types", () => { - (_: Swizzle2_1): api.Swizzle2_1 => _; - (_: Swizzle2_2): api.Swizzle2_2 => _; - (_: Swizzle2_3): api.Swizzle2_3 => _; - (_: Swizzle2_4): api.Swizzle2_4 => _; - (_: Swizzle3_1): api.Swizzle3_1 => _; - (_: Swizzle3_2): api.Swizzle3_2 => _; - (_: Swizzle3_3): api.Swizzle3_3 => _; - (_: Swizzle3_4): api.Swizzle3_4 => _; - (_: Swizzle4_1): api.Swizzle4_1 => _; - (_: Swizzle4_2): api.Swizzle4_2 => _; - (_: Swizzle4_3): api.Swizzle4_3 => _; - (_: Swizzle4_4): api.Swizzle4_4 => _; - (_: Swizzle2): api.Swizzle2 => _; - (_: Swizzle3): api.Swizzle3 => _; - (_: Swizzle4): api.Swizzle4 => _; - // We flip the types and test exhaustiveness again - // The existance of both function - // (a: A) => B and (b: B) => A - // guarantees that A and B are the same type - (_: api.Swizzle2_1): Swizzle2_1 => _; - (_: api.Swizzle2_2): Swizzle2_2 => _; - (_: api.Swizzle2_3): Swizzle2_3 => _; - (_: api.Swizzle2_4): Swizzle2_4 => _; - (_: api.Swizzle3_1): Swizzle3_1 => _; - (_: api.Swizzle3_2): Swizzle3_2 => _; - (_: api.Swizzle3_3): Swizzle3_3 => _; - (_: api.Swizzle3_4): Swizzle3_4 => _; - (_: api.Swizzle4_1): Swizzle4_1 => _; - (_: api.Swizzle4_2): Swizzle4_2 => _; - (_: api.Swizzle4_3): Swizzle4_3 => _; - (_: api.Swizzle4_4): Swizzle4_4 => _; - (_: api.Swizzle2): Swizzle2 => _; - (_: api.Swizzle3): Swizzle3 => _; - (_: api.Swizzle4): Swizzle4 => _; -}); From acda7f42528e1924e975375dafcb8a4a14e991d0 Mon Sep 17 00:00:00 2001 From: Sangyub Lee Date: Thu, 4 Jan 2024 14:13:23 +0300 Subject: [PATCH 4/4] Fill in swizzle mapping --- packages/shader-ast-js/src/target.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/packages/shader-ast-js/src/target.ts b/packages/shader-ast-js/src/target.ts index 63f755d53b..42270be637 100644 --- a/packages/shader-ast-js/src/target.ts +++ b/packages/shader-ast-js/src/target.ts @@ -87,7 +87,20 @@ const POOL_PRELUDE = VEC_TYPES.map( (x) => `const $${x} = env.pools.${x}.from;` ).join("\n"); -const COMPS: any = { x: 0, y: 1, z: 2, w: 3 }; +const COMPS: Record = { + x: 0, + y: 1, + z: 2, + w: 3, + r: 0, + g: 1, + b: 2, + a: 3, + s: 0, + t: 1, + p: 2, + q: 3, +}; const RE_SEMI = /[};]$/;