Skip to content

Commit c2ce266

Browse files
authored
feat: vecBase and matBase (#2206)
1 parent 89c1425 commit c2ce266

2 files changed

Lines changed: 51 additions & 6 deletions

File tree

packages/typegpu/src/data/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export type {
7676
Mat2x2f,
7777
Mat3x3f,
7878
Mat4x4f,
79+
matBase,
7980
Ptr,
8081
Size,
8182
StorableData,
@@ -108,6 +109,7 @@ export type {
108109
Vec4h,
109110
Vec4i,
110111
Vec4u,
112+
vecBase,
111113
WgslArray,
112114
WgslStruct,
113115
} from './wgslTypes.ts';

packages/typegpu/src/data/wgslTypes.ts

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export interface NumberArrayView {
5858
* These functions are not defined on vectors,
5959
* but are instead assigned to `VecBase` after both `data` and `std` are initialized.
6060
*/
61-
export interface vecInfixNotation<T extends AnyNumericVecInstance> {
61+
export interface vecInfixNotation<T extends vecBase> {
6262
add(other: T | number): T;
6363
sub(other: T | number): T;
6464
mul(other: mBaseForVec<T> | T | number): T;
@@ -82,7 +82,7 @@ export interface vecInfixNotation<T extends AnyNumericVecInstance> {
8282
* These functions are not defined on matrices,
8383
* but are instead assigned to `MatBase` after both `data` and `std` are initialized.
8484
*/
85-
export interface matInfixNotation<T extends AnyMatInstance> {
85+
export interface matInfixNotation<T extends matBase> {
8686
add(other: T): T;
8787
sub(other: T): T;
8888
mul(other: T | number): T;
@@ -184,6 +184,38 @@ type Tuple2<S> = [S, S];
184184
type Tuple3<S> = [S, S, S];
185185
type Tuple4<S> = [S, S, S, S];
186186

187+
/**
188+
* A type which every numeric vector is assignable to. In most cases the union v2f | v3f | v4f | v2h | v3h | v4h | v2i | v3i | v4i | v2u | v3u | v4u
189+
* is preferred, but when an implementation uses overloaded operators and is generic on the type,
190+
* this makes the type checking much more laid back.
191+
*
192+
* @example
193+
* ```ts
194+
* export function quinticInterpolation(t: d.v2f): d.v2f;
195+
* export function quinticInterpolation(t: d.v3f): d.v3f;
196+
* export function quinticInterpolation(t: d.vecBase): d.vecBase {
197+
* 'use gpu';
198+
* return t * t * t * (t * (t * 6 - 15) + 10);
199+
* }
200+
* ```
201+
*/
202+
export interface vecBase extends vecInfixNotation<vecBase> {
203+
readonly [$internal]: true;
204+
readonly kind:
205+
| 'vec2f'
206+
| 'vec3f'
207+
| 'vec4f'
208+
| 'vec2h'
209+
| 'vec3h'
210+
| 'vec4h'
211+
| 'vec2i'
212+
| 'vec3i'
213+
| 'vec4i'
214+
| 'vec2u'
215+
| 'vec3u'
216+
| 'vec4u';
217+
}
218+
187219
/**
188220
* Interface representing its WGSL vector type counterpart: vec2f or vec2<f32>.
189221
* A vector with 2 elements of type f32
@@ -479,6 +511,16 @@ export type AnyVecInstance =
479511

480512
export type VecKind = AnyVecInstance['kind'];
481513

514+
/**
515+
* A type which every matrix is assignable to. In most cases the union m2x2f | m3x3f | m4x4f
516+
* is preferred, but when an implementation uses overloaded operators and is generic on the type,
517+
* this makes the type checking much more laid back.
518+
*/
519+
export interface matBase extends matInfixNotation<matBase> {
520+
readonly [$internal]: true;
521+
readonly kind: 'mat2x2f' | 'mat3x3f' | 'mat4x4f';
522+
}
523+
482524
/**
483525
* Interface representing its WGSL matrix type counterpart: mat2x2
484526
* A matrix with 2 rows and 2 columns, with elements of type `TColumn`
@@ -546,14 +588,15 @@ export interface m4x4f extends mat4x4<v4f>, matInfixNotation<m4x4f> {
546588

547589
export type AnyMatInstance = m2x2f | m3x3f | m4x4f;
548590

549-
export type vBaseForMat<T extends AnyMatInstance> = T extends m2x2f ? v2f
591+
export type vBaseForMat<T extends matBase> = T extends m2x2f ? v2f
550592
: T extends m3x3f ? v3f
551-
: v4f;
593+
: T extends m4x4f ? v4f
594+
: vecBase;
552595

553-
export type mBaseForVec<T extends AnyVecInstance> = T extends v2f ? m2x2f
596+
export type mBaseForVec<T extends vecBase> = T extends v2f ? m2x2f
554597
: T extends v3f ? m3x3f
555598
: T extends v4f ? m4x4f
556-
: never;
599+
: matBase;
557600

558601
// #endregion
559602

0 commit comments

Comments
 (0)