typegpu 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,59 @@
1
- import { Unwrap, ISchema, Parsed } from 'typed-binary';
1
+ import { Unwrap, Parsed, ISchema } from 'typed-binary';
2
+
3
+ type Return = {
4
+ return: Expression | null;
5
+ };
6
+ type If = {
7
+ /** condition */
8
+ if: Expression;
9
+ do: Statement;
10
+ else?: Statement | undefined;
11
+ };
12
+ type Block = {
13
+ block: Statement[];
14
+ };
15
+ type Let = {
16
+ /** variable's identifier */
17
+ let: string;
18
+ eq?: Expression | undefined;
19
+ };
20
+ type Const = {
21
+ /** variable's identifier */
22
+ const: string;
23
+ eq?: Expression | undefined;
24
+ };
25
+ type Statement = Return | If | Block | Let | Const | Expression;
26
+ type BinaryOperator = '==' | '!=' | '<' | '<=' | '>' | '>=' | '<<' | '>>' | '+' | '-' | '*' | '/' | '%' | '|' | '^' | '&';
27
+ type BinaryExpression = {
28
+ x2: [Expression, BinaryOperator, Expression];
29
+ };
30
+ type AssignmentOperator = '=' | '+=' | '-=' | '*=' | '/=' | '%=' | '<<=' | '>>=' | '|=' | '^=' | '&=' | '**=' | '||=' | '&&=';
31
+ type AssignmentExpression = {
32
+ x2: [Expression, AssignmentOperator, Expression];
33
+ };
34
+ type LogicalOperator = '&&' | '||';
35
+ type LogicalExpression = {
36
+ x2: [Expression, LogicalOperator, Expression];
37
+ };
38
+ type MemberAccess = {
39
+ '.': [Expression, string];
40
+ };
41
+ type IndexAccess = {
42
+ '[]': [Expression, Expression];
43
+ };
44
+ type Call = {
45
+ /** function identifier */
46
+ call: Expression;
47
+ /** expressions passed as function arguments */
48
+ args: Expression[];
49
+ };
50
+ /** A numeric literal */
51
+ type Num = {
52
+ num: string;
53
+ };
54
+ type Literal = Num | boolean;
55
+ /** Identifiers are just strings, since string literals are rare in WGSL, and identifiers are everywhere. */
56
+ type Expression = string | BinaryExpression | AssignmentExpression | LogicalExpression | MemberAccess | IndexAccess | Call | Literal;
2
57
 
3
58
  /**
4
59
  * Can be assigned a name. Not to be confused with
@@ -9,6 +64,8 @@ interface TgpuNamable {
9
64
  }
10
65
 
11
66
  type Getter = <T>(plum: TgpuPlum<T>) => T;
67
+ type Unsubscribe = () => unknown;
68
+ type ExtractPlumValue<T> = T extends TgpuPlum<infer TValue> ? TValue : never;
12
69
  interface TgpuPlum<TValue = unknown> extends TgpuNamable {
13
70
  readonly __brand: 'TgpuPlum';
14
71
  /**
@@ -18,110 +75,85 @@ interface TgpuPlum<TValue = unknown> extends TgpuNamable {
18
75
  compute(get: Getter): TValue;
19
76
  }
20
77
 
78
+ interface TgpuBufferUniform<TData extends AnyTgpuData> extends TgpuBindable<TData, 'uniform'> {
79
+ readonly resourceType: 'buffer-usage';
80
+ readonly value: Unwrap<TData>;
81
+ }
82
+ interface TgpuBufferReadonly<TData extends AnyTgpuData> extends TgpuBindable<TData, 'readonly'> {
83
+ readonly resourceType: 'buffer-usage';
84
+ readonly value: Unwrap<TData>;
85
+ }
86
+ interface TgpuBufferMutable<TData extends AnyTgpuData> extends TgpuBindable<TData, 'mutable'> {
87
+ readonly resourceType: 'buffer-usage';
88
+ value: Unwrap<TData>;
89
+ }
21
90
  interface TgpuBufferUsage<TData extends AnyTgpuData, TUsage extends BufferUsage = BufferUsage> extends TgpuBindable<TData, TUsage> {
91
+ readonly resourceType: 'buffer-usage';
22
92
  value: Unwrap<TData>;
23
93
  }
24
94
 
25
- type AnyTgpuDataTuple = [AnyTgpuData, ...AnyTgpuData[]] | [];
26
- type UnwrapArgs<T extends AnyTgpuDataTuple> = {
27
- [Idx in keyof T]: Unwrap<T[Idx]>;
28
- };
29
- type UnwrapReturn<T extends AnyTgpuData | undefined> = T extends undefined ? void : Unwrap<T>;
30
95
  /**
31
- * Describes a function signature (its arguments and return type)
96
+ * Boolean schema representing a single WGSL bool value.
97
+ * Cannot be used inside buffers as it is not host-shareable.
32
98
  */
33
- interface TgpuFnShell<Args extends AnyTgpuDataTuple, Return extends AnyTgpuData | undefined> {
34
- readonly argTypes: Args;
35
- readonly returnType: Return | undefined;
36
- /**
37
- * Creates a type-safe implementation of this signature
38
- */
39
- implement(body: (...args: UnwrapArgs<Args>) => UnwrapReturn<Return>): TgpuFn<Args, Return>;
40
- }
41
- interface TgpuFnBase<Args extends AnyTgpuDataTuple, Return extends AnyTgpuData | undefined = undefined> extends TgpuResolvable, TgpuNamable {
42
- readonly shell: TgpuFnShell<Args, Return>;
43
- /** The JS function body passed as an implementation of a TypeGPU function. */
44
- readonly body: (...args: UnwrapArgs<Args>) => UnwrapReturn<Return>;
45
- readonly bodyResolvable: TgpuResolvable;
46
- $uses(dependencyMap: Record<string, unknown>): this;
47
- }
48
- type TgpuFn<Args extends AnyTgpuDataTuple, Return extends AnyTgpuData | undefined = undefined> = TgpuFnBase<Args, Return> & ((...args: UnwrapArgs<Args>) => UnwrapReturn<Return>);
49
-
50
- type Wgsl = string | number | TgpuResolvable | symbol | boolean;
99
+ type Bool = TgpuData<boolean>;
51
100
  /**
52
- * Passed into each resolvable item. All sibling items share a resolution ctx,
53
- * and a new resolution ctx is made when going down each level in the tree.
101
+ * A schema that represents a boolean value. (equivalent to `bool` in WGSL)
54
102
  */
55
- interface ResolutionCtx {
56
- addDeclaration(item: TgpuResolvable): void;
57
- addBinding(bindable: TgpuBindable, identifier: TgpuIdentifier): void;
58
- addRenderResource(resource: TgpuRenderResource, identifier: TgpuIdentifier): void;
59
- addBuiltin(builtin: symbol): void;
60
- nameFor(token: TgpuResolvable): string;
61
- /**
62
- * Unwraps all layers of slot indirection and returns the concrete value if available.
63
- * @throws {MissingSlotValueError}
64
- */
65
- unwrap<T>(eventual: Eventual<T>): T;
66
- resolve(item: Wgsl, slotValueOverrides?: SlotValuePair<unknown>[]): string;
67
- fnToWgsl(fn: TgpuFn<any, any>, externalMap: Record<string, unknown>): {
68
- head: Wgsl;
69
- body: Wgsl;
70
- };
71
- }
72
- interface TgpuResolvable {
73
- readonly label?: string | undefined;
74
- resolve(ctx: ResolutionCtx): string;
75
- }
76
- interface TgpuIdentifier extends TgpuNamable, TgpuResolvable {
77
- }
103
+ declare const bool: Bool;
78
104
  /**
79
- * Represents a value that is available at resolution time.
105
+ * Unsigned 32-bit integer schema representing a single WGSL u32 value.
80
106
  */
81
- type Eventual<T> = T | TgpuSlot<T>;
82
- type SlotValuePair<T> = [TgpuSlot<T>, T];
83
- interface TgpuBindable<TData extends AnyTgpuData = AnyTgpuData, TUsage extends BufferUsage = BufferUsage> extends TgpuResolvable {
84
- readonly allocatable: TgpuBuffer<TData>;
85
- readonly usage: TUsage;
86
- }
87
- type TgpuSamplerType = 'sampler' | 'sampler_comparison';
88
- type TgpuTypedTextureType = 'texture_1d' | 'texture_2d' | 'texture_2d_array' | 'texture_3d' | 'texture_cube' | 'texture_cube_array' | 'texture_multisampled_2d';
89
- type TgpuDepthTextureType = 'texture_depth_2d' | 'texture_depth_2d_array' | 'texture_depth_cube' | 'texture_depth_cube_array' | 'texture_depth_multisampled_2d';
90
- type TgpuStorageTextureType = 'texture_storage_1d' | 'texture_storage_2d' | 'texture_storage_2d_array' | 'texture_storage_3d';
91
- type TgpuExternalTextureType = 'texture_external';
92
- type TgpuRenderResourceType = TgpuSamplerType | TgpuTypedTextureType | TgpuDepthTextureType | TgpuStorageTextureType | TgpuExternalTextureType;
93
- interface TgpuRenderResource extends TgpuResolvable {
94
- readonly type: TgpuRenderResourceType;
95
- }
96
- type BufferUsage = 'uniform' | 'readonly' | 'mutable' | 'vertex';
97
- type ValueOf<T> = T extends TgpuSlot<infer I> ? ValueOf<I> : T extends TgpuBufferUsage<infer D> ? ValueOf<D> : T extends TgpuData<unknown> ? Unwrap<T> : T;
98
- interface TgpuData<TInner> extends ISchema<TInner>, TgpuResolvable {
99
- readonly isLoose: false;
100
- readonly isCustomAligned: boolean;
101
- readonly byteAlignment: number;
102
- readonly size: number;
103
- }
104
- interface TgpuLooseData<TInner> extends ISchema<TInner>, TgpuResolvable {
105
- readonly isLoose: true;
106
- readonly size: number;
107
- }
108
- type AnyTgpuData = TgpuData<unknown>;
109
- type AnyTgpuLooseData = TgpuLooseData<unknown>;
110
- interface TgpuPointer<TScope extends 'function', TInner extends AnyTgpuData> {
111
- readonly scope: TScope;
112
- readonly pointsTo: TInner;
113
- }
114
- interface TgpuSlot<T> extends TgpuNamable {
115
- readonly __brand: 'TgpuSlot';
116
- readonly defaultValue: T | undefined;
117
- readonly label?: string | undefined;
118
- /**
119
- * Used to determine if code generated using either value `a` or `b` in place
120
- * of the slot will be equivalent. Defaults to `Object.is`.
121
- */
122
- areEqual(a: T, b: T): boolean;
123
- value: ValueOf<T>;
124
- }
107
+ type U32 = TgpuData<number> & {
108
+ kind: 'u32';
109
+ } & ((v: number | boolean) => number);
110
+ /**
111
+ * A schema that represents an unsigned 32-bit integer value. (equivalent to `u32` in WGSL)
112
+ *
113
+ * Can also be called to cast a value to an u32 in accordance with WGSL casting rules.
114
+ *
115
+ * @example
116
+ * const value = u32(3.14); // 3
117
+ * @example
118
+ * const value = u32(-1); // 4294967295
119
+ * @example
120
+ * const value = u32(-3.1); // 0
121
+ */
122
+ declare const u32: U32;
123
+ /**
124
+ * Signed 32-bit integer schema representing a single WGSL i32 value.
125
+ */
126
+ type I32 = TgpuData<number> & {
127
+ kind: 'i32';
128
+ } & ((v: number | boolean) => number);
129
+ /**
130
+ * A schema that represents a signed 32-bit integer value. (equivalent to `i32` in WGSL)
131
+ *
132
+ * Can also be called to cast a value to an i32 in accordance with WGSL casting rules.
133
+ *
134
+ * @example
135
+ * const value = i32(3.14); // 3
136
+ * @example
137
+ * const value = i32(-3.9); // -3
138
+ * @example
139
+ * const value = i32(10000000000) // 1410065408
140
+ */
141
+ declare const i32: I32;
142
+ /**
143
+ * 32-bit float schema representing a single WGSL f32 value.
144
+ */
145
+ type F32 = TgpuData<number> & {
146
+ kind: 'f32';
147
+ } & ((v: number | boolean) => number);
148
+ /**
149
+ * A schema that represents a 32-bit float value. (equivalent to `f32` in WGSL)
150
+ *
151
+ * Can also be called to cast a value to an f32.
152
+ *
153
+ * @example
154
+ * const value = f32(true); // 1
155
+ */
156
+ declare const f32: F32;
125
157
 
126
158
  interface Swizzle2<T2, T3, T4> {
127
159
  readonly xx: T2;
@@ -428,82 +460,286 @@ interface Swizzle4<T2, T3, T4> extends Swizzle3<T2, T3, T4> {
428
460
  readonly wwwz: T4;
429
461
  readonly wwww: T4;
430
462
  }
431
- interface vec2 {
463
+ interface vec2 extends NumberArrayView {
432
464
  x: number;
433
465
  y: number;
434
466
  [Symbol.iterator](): Iterator<number>;
435
467
  }
436
- interface vec3 {
468
+ interface vec3 extends NumberArrayView {
437
469
  x: number;
438
470
  y: number;
439
471
  z: number;
440
472
  [Symbol.iterator](): Iterator<number>;
441
473
  }
442
- interface vec4 {
474
+ interface vec4 extends NumberArrayView {
443
475
  x: number;
444
476
  y: number;
445
477
  z: number;
446
478
  w: number;
447
479
  [Symbol.iterator](): Iterator<number>;
448
480
  }
481
+ /**
482
+ * Type encompassing all available kinds of vector.
483
+ */
449
484
  type VecKind = 'vec2f' | 'vec2i' | 'vec2u' | 'vec3f' | 'vec3i' | 'vec3u' | 'vec4f' | 'vec4i' | 'vec4u';
485
+ /**
486
+ * Common interface for all vector types, no matter their size and inner type.
487
+ */
450
488
  interface vecBase {
451
489
  kind: VecKind;
452
490
  [Symbol.iterator](): Iterator<number>;
453
491
  }
454
- type Vec2f = TgpuData<vec2f> & ((x: number, y: number) => vec2f) & ((xy: number) => vec2f) & (() => vec2f);
492
+ /**
493
+ * Type of the `d.vec2f` object/function: vector data type schema/constructor
494
+ */
495
+ type Vec2f = TgpuData<vec2f> & {
496
+ kind: 'vec2f';
497
+ } & ((x: number, y: number) => vec2f) & ((xy: number) => vec2f) & (() => vec2f);
498
+ /**
499
+ * Interface representing its WGSL vector type counterpart: vec2f or vec2<f32>.
500
+ * A vector with 2 elements of type d.f32
501
+ */
455
502
  interface vec2f extends vec2, Swizzle2<vec2f, vec3f, vec4f> {
456
503
  /** use to distinguish between vectors of the same size on the type level */
457
504
  kind: 'vec2f';
458
505
  }
506
+ /**
507
+ *
508
+ * Schema representing vec2f - a vector with 2 elements of type f32.
509
+ * Also a constructor function for this vector value.
510
+ *
511
+ * @example
512
+ * const vector = d.vec2f(); // (0.0, 0.0)
513
+ * const vector = d.vec2f(1); // (1.0, 1.0)
514
+ * const vector = d.vec2f(0.5, 0.1); // (0.5, 0.1)
515
+ *
516
+ * @example
517
+ * const buffer = root.createBuffer(d.vec2f, d.vec2f(0, 1)); // buffer holding a d.vec2f value, with an initial value of vec2f(0, 1);
518
+ */
459
519
  declare const vec2f: Vec2f;
460
- type Vec2i = TgpuData<vec2i> & ((x: number, y: number) => vec2i) & ((xy: number) => vec2i) & (() => vec2i);
520
+ /**
521
+ * Type of the `d.vec2i` object/function: vector data type schema/constructor
522
+ */
523
+ type Vec2i = TgpuData<vec2i> & {
524
+ kind: 'vec2i';
525
+ } & ((x: number, y: number) => vec2i) & ((xy: number) => vec2i) & (() => vec2i);
526
+ /**
527
+ * Interface representing its WGSL vector type counterpart: vec2i or vec2<i32>.
528
+ * A vector with 2 elements of type d.i32
529
+ */
461
530
  interface vec2i extends vec2, Swizzle2<vec2i, vec3i, vec4i> {
462
531
  /** use to distinguish between vectors of the same size on the type level */
463
532
  kind: 'vec2i';
464
533
  }
534
+ /**
535
+ *
536
+ * Schema representing vec2i - a vector with 2 elements of type i32.
537
+ * Also a constructor function for this vector value.
538
+ *
539
+ * @example
540
+ * const vector = d.vec2i(); // (0, 0)
541
+ * const vector = d.vec2i(1); // (1, 1)
542
+ * const vector = d.vec2i(-1, 1); // (-1, 1)
543
+ *
544
+ * @example
545
+ * const buffer = root.createBuffer(d.vec2i, d.vec2i(0, 1)); // buffer holding a d.vec2i value, with an initial value of vec2i(0, 1);
546
+ */
465
547
  declare const vec2i: Vec2i;
466
- type Vec2u = TgpuData<vec2u> & ((x: number, y: number) => vec2u) & ((xy: number) => vec2u) & (() => vec2u);
548
+ /**
549
+ * Type of the `d.vec2u` object/function: vector data type schema/constructor
550
+ */
551
+ type Vec2u = TgpuData<vec2u> & {
552
+ kind: 'vec2u';
553
+ } & ((x: number, y: number) => vec2u) & ((xy: number) => vec2u) & (() => vec2u);
554
+ /**
555
+ * Interface representing its WGSL vector type counterpart: vec2u or vec2<u32>.
556
+ * A vector with 2 elements of type d.u32
557
+ */
467
558
  interface vec2u extends vec2, Swizzle2<vec2u, vec3u, vec4u> {
468
559
  /** use to distinguish between vectors of the same size on the type level */
469
560
  kind: 'vec2u';
470
561
  }
562
+ /**
563
+ *
564
+ * Schema representing vec2u - a vector with 2 elements of type u32.
565
+ * Also a constructor function for this vector value.
566
+ *
567
+ * @example
568
+ * const vector = d.vec2u(); // (0, 0)
569
+ * const vector = d.vec2u(1); // (1, 1)
570
+ * const vector = d.vec2u(1, 2); // (1, 2)
571
+ *
572
+ * @example
573
+ * const buffer = root.createBuffer(d.vec2u, d.vec2u(0, 1)); // buffer holding a d.vec2u value, with an initial value of vec2u(0, 1);
574
+ */
471
575
  declare const vec2u: Vec2u;
472
- type Vec3f = TgpuData<vec3f> & ((x: number, y: number, z: number) => vec3f) & ((xyz: number) => vec3f) & (() => vec3f);
576
+ /**
577
+ * Type of the `d.vec3f` object/function: vector data type schema/constructor
578
+ */
579
+ type Vec3f = TgpuData<vec3f> & {
580
+ kind: 'vec3f';
581
+ } & ((x: number, y: number, z: number) => vec3f) & ((xyz: number) => vec3f) & (() => vec3f);
582
+ /**
583
+ * Interface representing its WGSL vector type counterpart: vec3f or vec3<f32>.
584
+ * A vector with 3 elements of type d.f32
585
+ */
473
586
  interface vec3f extends vec3, Swizzle3<vec2f, vec3f, vec4f> {
474
587
  /** use to distinguish between vectors of the same size on the type level */
475
588
  kind: 'vec3f';
476
589
  }
590
+ /**
591
+ *
592
+ * Schema representing vec3f - a vector with 3 elements of type f32.
593
+ * Also a constructor function for this vector value.
594
+ *
595
+ * @example
596
+ * const vector = d.vec3f(); // (0.0, 0.0, 0.0)
597
+ * const vector = d.vec3f(1); // (1.0, 1.0, 1.0)
598
+ * const vector = d.vec3f(1, 2, 3.5); // (1.0, 2.0, 3.5)
599
+ *
600
+ * @example
601
+ * const buffer = root.createBuffer(d.vec3f, d.vec3f(0, 1, 2)); // buffer holding a d.vec3f value, with an initial value of vec3f(0, 1, 2);
602
+ */
477
603
  declare const vec3f: Vec3f;
478
- type Vec3i = TgpuData<vec3i> & ((x: number, y: number, z: number) => vec3i) & ((xyz: number) => vec3i) & (() => vec3i);
604
+ /**
605
+ * Type of the `d.vec3i` object/function: vector data type schema/constructor
606
+ */
607
+ type Vec3i = TgpuData<vec3i> & {
608
+ kind: 'vec3i';
609
+ } & ((x: number, y: number, z: number) => vec3i) & ((xyz: number) => vec3i) & (() => vec3i);
610
+ /**
611
+ * Interface representing its WGSL vector type counterpart: vec3i or vec3<i32>.
612
+ * A vector with 3 elements of type d.i32
613
+ */
479
614
  interface vec3i extends vec3, Swizzle3<vec2i, vec3i, vec4i> {
480
615
  /** use to distinguish between vectors of the same size on the type level */
481
616
  kind: 'vec3i';
482
617
  }
618
+ /**
619
+ *
620
+ * Schema representing vec3i - a vector with 3 elements of type i32.
621
+ * Also a constructor function for this vector value.
622
+ *
623
+ * @example
624
+ * const vector = d.vec3i(); // (0, 0, 0)
625
+ * const vector = d.vec3i(1); // (1, 1, 1)
626
+ * const vector = d.vec3i(1, 2, -3); // (1, 2, -3)
627
+ *
628
+ * @example
629
+ * const buffer = root.createBuffer(d.vec3i, d.vec3i(0, 1, 2)); // buffer holding a d.vec3i value, with an initial value of vec3i(0, 1, 2);
630
+ */
483
631
  declare const vec3i: Vec3i;
484
- type Vec3u = TgpuData<vec3u> & ((x: number, y: number, z: number) => vec3u) & ((xyz: number) => vec3u) & (() => vec3u);
632
+ /**
633
+ * Type of the `d.vec3u` object/function: vector data type schema/constructor
634
+ */
635
+ type Vec3u = TgpuData<vec3u> & {
636
+ kind: 'vec3u';
637
+ } & ((x: number, y: number, z: number) => vec3u) & ((xyz: number) => vec3u) & (() => vec3u);
638
+ /**
639
+ * Interface representing its WGSL vector type counterpart: vec3u or vec3<u32>.
640
+ * A vector with 3 elements of type d.u32
641
+ */
485
642
  interface vec3u extends vec3, Swizzle3<vec2u, vec3u, vec4u> {
486
643
  /** use to distinguish between vectors of the same size on the type level */
487
644
  kind: 'vec3u';
488
645
  }
646
+ /**
647
+ *
648
+ * Schema representing vec3u - a vector with 3 elements of type u32.
649
+ * Also a constructor function for this vector value.
650
+ *
651
+ * @example
652
+ * const vector = d.vec3u(); // (0, 0, 0)
653
+ * const vector = d.vec3u(1); // (1, 1, 1)
654
+ * const vector = d.vec3u(1, 2, 3); // (1, 2, 3)
655
+ *
656
+ * @example
657
+ * const buffer = root.createBuffer(d.vec3u, d.vec3u(0, 1, 2)); // buffer holding a d.vec3u value, with an initial value of vec3u(0, 1, 2);
658
+ */
489
659
  declare const vec3u: Vec3u;
490
- type Vec4f = TgpuData<vec4f> & ((x: number, y: number, z: number, w: number) => vec4f) & ((xyzw: number) => vec4f) & (() => vec4f);
660
+ /**
661
+ * Type of the `d.vec4f` object/function: vector data type schema/constructor
662
+ */
663
+ type Vec4f = TgpuData<vec4f> & {
664
+ kind: 'vec4f';
665
+ } & ((x: number, y: number, z: number, w: number) => vec4f) & ((xyzw: number) => vec4f) & (() => vec4f);
666
+ /**
667
+ * Interface representing its WGSL vector type counterpart: vec4f or vec4<f32>.
668
+ * A vector with 4 elements of type d.f32
669
+ */
491
670
  interface vec4f extends vec4, Swizzle4<vec2f, vec3f, vec4f> {
492
671
  /** use to distinguish between vectors of the same size on the type level */
493
672
  kind: 'vec4f';
494
673
  }
674
+ /**
675
+ *
676
+ * Schema representing vec4f - a vector with 4 elements of type f32.
677
+ * Also a constructor function for this vector value.
678
+ *
679
+ * @example
680
+ * const vector = d.vec4f(); // (0.0, 0.0, 0.0, 0.0)
681
+ * const vector = d.vec4f(1); // (1.0, 1.0, 1.0, 1.0)
682
+ * const vector = d.vec4f(1, 2, 3, 4.5); // (1.0, 2.0, 3.0, 4.5)
683
+ *
684
+ * @example
685
+ * const buffer = root.createBuffer(d.vec4f, d.vec4f(0, 1, 2, 3)); // buffer holding a d.vec4f value, with an initial value of vec4f(0, 1, 2, 3);
686
+ */
495
687
  declare const vec4f: Vec4f;
496
- type Vec4i = TgpuData<vec4i> & ((x: number, y: number, z: number, w: number) => vec4i) & ((xyzw: number) => vec4i) & (() => vec4i);
688
+ /**
689
+ * Type of the `d.vec4i` object/function: vector data type schema/constructor
690
+ */
691
+ type Vec4i = TgpuData<vec4i> & {
692
+ kind: 'vec4i';
693
+ } & ((x: number, y: number, z: number, w: number) => vec4i) & ((xyzw: number) => vec4i) & (() => vec4i);
694
+ /**
695
+ * Interface representing its WGSL vector type counterpart: vec4i or vec4<i32>.
696
+ * A vector with 4 elements of type d.i32
697
+ */
497
698
  interface vec4i extends vec4, Swizzle4<vec2i, vec3i, vec4i> {
498
699
  /** use to distinguish between vectors of the same size on the type level */
499
700
  kind: 'vec4i';
500
701
  }
702
+ /**
703
+ *
704
+ * Schema representing vec4i - a vector with 4 elements of type i32.
705
+ * Also a constructor function for this vector value.
706
+ *
707
+ * @example
708
+ * const vector = d.vec4i(); // (0, 0, 0, 0)
709
+ * const vector = d.vec4i(1); // (1, 1, 1, 1)
710
+ * const vector = d.vec4i(1, 2, 3, -4); // (1, 2, 3, -4)
711
+ *
712
+ * @example
713
+ * const buffer = root.createBuffer(d.vec4i, d.vec4i(0, 1, 2, 3)); // buffer holding a d.vec4i value, with an initial value of vec4i(0, 1, 2, 3);
714
+ */
501
715
  declare const vec4i: Vec4i;
502
- type Vec4u = TgpuData<vec4u> & ((x: number, y: number, z: number, w: number) => vec4u) & ((xyzw: number) => vec4u) & (() => vec4u);
716
+ /**
717
+ * Type of the `d.vec4u` object/function: vector data type schema/constructor
718
+ */
719
+ type Vec4u = TgpuData<vec4u> & {
720
+ kind: 'vec4u';
721
+ } & ((x: number, y: number, z: number, w: number) => vec4u) & ((xyzw: number) => vec4u) & (() => vec4u);
722
+ /**
723
+ * Interface representing its WGSL vector type counterpart: vec4u or vec4<u32>.
724
+ * A vector with 4 elements of type d.u32
725
+ */
503
726
  interface vec4u extends vec4, Swizzle4<vec2u, vec3u, vec4u> {
504
727
  /** use to distinguish between vectors of the same size on the type level */
505
728
  kind: 'vec4u';
506
729
  }
730
+ /**
731
+ *
732
+ * Schema representing vec4u - a vector with 4 elements of type u32.
733
+ * Also a constructor function for this vector value.
734
+ *
735
+ * @example
736
+ * const vector = d.vec4u(); // (0, 0, 0, 0)
737
+ * const vector = d.vec4u(1); // (1, 1, 1, 1)
738
+ * const vector = d.vec4u(1, 2, 3, 4); // (1, 2, 3, 4)
739
+ *
740
+ * @example
741
+ * const buffer = root.createBuffer(d.vec4u, d.vec4u(0, 1, 2, 3)); // buffer holding a d.vec4u value, with an initial value of vec4u(0, 1, 2, 3);
742
+ */
507
743
  declare const vec4u: Vec4u;
508
744
 
509
745
  interface Uniform {
@@ -519,6 +755,7 @@ interface Vertex {
519
755
  }
520
756
  declare const Vertex: Vertex;
521
757
  type UnionToIntersection<U> = (U extends any ? (x: U) => void : never) extends (x: infer I) => void ? I : never;
758
+ type LiteralToUsageType<T extends 'uniform' | 'storage' | 'vertex'> = T extends 'uniform' ? Uniform : T extends 'storage' ? Storage : T extends 'vertex' ? Vertex : never;
522
759
  interface TgpuBuffer<TData extends AnyTgpuData> extends TgpuNamable {
523
760
  readonly resourceType: 'buffer';
524
761
  readonly dataType: TData;
@@ -527,7 +764,7 @@ interface TgpuBuffer<TData extends AnyTgpuData> extends TgpuNamable {
527
764
  readonly buffer: GPUBuffer;
528
765
  readonly device: GPUDevice;
529
766
  readonly destroyed: boolean;
530
- $usage<T extends (Uniform | Storage | Vertex)[]>(...usages: T): this & UnionToIntersection<T[number]>;
767
+ $usage<T extends ('uniform' | 'storage' | 'vertex')[]>(...usages: T): this & UnionToIntersection<LiteralToUsageType<T[number]>>;
531
768
  $addFlags(flags: GPUBufferUsageFlags): this;
532
769
  $device(device: GPUDevice): this;
533
770
  write(data: Parsed<TData> | TgpuBuffer<TData>): void;
@@ -538,4 +775,106 @@ declare function isUsableAsUniform<T extends TgpuBuffer<AnyTgpuData>>(buffer: T)
538
775
  declare function isUsableAsStorage<T extends TgpuBuffer<AnyTgpuData>>(buffer: T): buffer is T & Storage;
539
776
  declare function isUsableAsVertex<T extends TgpuBuffer<AnyTgpuData>>(buffer: T): buffer is T & Vertex;
540
777
 
541
- export { type AnyTgpuData as A, type Vec4f as B, type Vec4i as C, type Vec4u as D, type ResolutionCtx as R, Storage as S, type TgpuPlum as T, Uniform as U, Vertex as V, type TgpuBuffer as a, vec3f as b, vec3i as c, vec3u as d, type TgpuData as e, isUsableAsUniform as f, isUsableAsVertex as g, type TgpuNamable as h, isUsableAsStorage as i, vec2f as j, vec4f as k, type TgpuPointer as l, type AnyTgpuLooseData as m, type TgpuLooseData as n, type VecKind as o, vec2i as p, vec2u as q, vec4i as r, vec4u as s, type Vec2f as t, type Vec2i as u, type vecBase as v, type Vec2u as w, type Vec3f as x, type Vec3i as y, type Vec3u as z };
778
+ interface TgpuFnShellBase<Args extends AnyTgpuData[], Return extends AnyTgpuData | undefined> {
779
+ readonly argTypes: Args;
780
+ readonly returnType: Return | undefined;
781
+ }
782
+
783
+ type Wgsl = string | number | TgpuResolvable | symbol | boolean;
784
+ type TgpuShaderStage = 'compute' | 'vertex' | 'fragment';
785
+ interface NumberArrayView {
786
+ readonly length: number;
787
+ [n: number]: number;
788
+ }
789
+ /**
790
+ * Removes properties from record type that extend `Prop`
791
+ */
792
+ type OmitProps<T extends Record<string, unknown>, Prop> = Pick<T, {
793
+ [Key in keyof T]: T[Key] extends Prop ? never : Key;
794
+ }[keyof T]>;
795
+ /**
796
+ * Passed into each resolvable item. All sibling items share a resolution ctx,
797
+ * and a new resolution ctx is made when going down each level in the tree.
798
+ */
799
+ interface ResolutionCtx {
800
+ addDeclaration(item: TgpuResolvable): void;
801
+ addBinding(bindable: TgpuBindable, identifier: TgpuIdentifier): void;
802
+ addRenderResource(resource: TgpuRenderResource, identifier: TgpuIdentifier): void;
803
+ addBuiltin(builtin: symbol): void;
804
+ nameFor(token: TgpuResolvable): string;
805
+ /**
806
+ * Unwraps all layers of slot indirection and returns the concrete value if available.
807
+ * @throws {MissingSlotValueError}
808
+ */
809
+ unwrap<T>(eventual: Eventual<T>): T;
810
+ resolve(item: Wgsl, slotValueOverrides?: SlotValuePair<unknown>[]): string;
811
+ transpileFn(fn: string): {
812
+ argNames: string[];
813
+ body: Block;
814
+ externalNames: string[];
815
+ };
816
+ fnToWgsl(shell: TgpuFnShellBase<any, AnyTgpuData | undefined>, argNames: string[], body: Block, externalMap: Record<string, unknown>): {
817
+ head: Wgsl;
818
+ body: Wgsl;
819
+ };
820
+ }
821
+ interface TgpuResolvable {
822
+ readonly label?: string | undefined;
823
+ resolve(ctx: ResolutionCtx): string;
824
+ }
825
+ interface TgpuIdentifier extends TgpuNamable, TgpuResolvable {
826
+ }
827
+ /**
828
+ * Represents a value that is available at resolution time.
829
+ */
830
+ type Eventual<T> = T | TgpuSlot<T>;
831
+ type SlotValuePair<T> = [TgpuSlot<T>, T];
832
+ interface TgpuBindable<TData extends AnyTgpuData = AnyTgpuData, TUsage extends BufferUsage = BufferUsage> extends TgpuResolvable {
833
+ readonly allocatable: TgpuBuffer<TData>;
834
+ readonly usage: TUsage;
835
+ }
836
+ type TgpuSamplerType = 'sampler' | 'sampler_comparison';
837
+ type TgpuTypedTextureType = 'texture_1d' | 'texture_2d' | 'texture_2d_array' | 'texture_3d' | 'texture_cube' | 'texture_cube_array' | 'texture_multisampled_2d';
838
+ type TgpuDepthTextureType = 'texture_depth_2d' | 'texture_depth_2d_array' | 'texture_depth_cube' | 'texture_depth_cube_array' | 'texture_depth_multisampled_2d';
839
+ type TgpuStorageTextureType = 'texture_storage_1d' | 'texture_storage_2d' | 'texture_storage_2d_array' | 'texture_storage_3d';
840
+ type TgpuExternalTextureType = 'texture_external';
841
+ type TgpuRenderResourceType = TgpuSamplerType | TgpuTypedTextureType | TgpuDepthTextureType | TgpuStorageTextureType | TgpuExternalTextureType;
842
+ interface TgpuRenderResource extends TgpuResolvable {
843
+ readonly type: TgpuRenderResourceType;
844
+ }
845
+ type BufferUsage = 'uniform' | 'readonly' | 'mutable' | 'vertex';
846
+ type ValueOf<T> = T extends TgpuSlot<infer I> ? ValueOf<I> : T extends TgpuBufferUsage<infer D> ? ValueOf<D> : T extends TgpuData<unknown> ? Unwrap<T> : T;
847
+ interface TgpuData<TInner> extends ISchema<TInner>, TgpuResolvable {
848
+ readonly isLoose: false;
849
+ readonly byteAlignment: number;
850
+ readonly size: number;
851
+ }
852
+ interface TgpuLooseData<TInner> extends ISchema<TInner> {
853
+ readonly isLoose: true;
854
+ readonly byteAlignment: number;
855
+ readonly size: number;
856
+ }
857
+ type AnyTgpuData = TgpuData<unknown>;
858
+ type AnyTgpuLooseData = TgpuLooseData<unknown>;
859
+ interface TgpuPointer<TScope extends 'function', TInner extends AnyTgpuData> {
860
+ readonly scope: TScope;
861
+ readonly pointsTo: TInner;
862
+ }
863
+ interface BoundTgpuCode extends TgpuResolvable {
864
+ with<T>(slot: TgpuSlot<T>, value: Eventual<T>): BoundTgpuCode;
865
+ }
866
+ interface TgpuCode extends BoundTgpuCode, TgpuNamable {
867
+ }
868
+ interface TgpuSlot<T> extends TgpuNamable {
869
+ readonly __brand: 'TgpuSlot';
870
+ readonly defaultValue: T | undefined;
871
+ readonly label?: string | undefined;
872
+ /**
873
+ * Used to determine if code generated using either value `a` or `b` in place
874
+ * of the slot will be equivalent. Defaults to `Object.is`.
875
+ */
876
+ areEqual(a: T, b: T): boolean;
877
+ value: ValueOf<T>;
878
+ }
879
+
880
+ export { vec2u as $, type AnyTgpuData as A, type BoundTgpuCode as B, vec4f as C, type Vec2u as D, type ExtractPlumValue as E, type F32 as F, type Vec2i as G, type Vec2f as H, type I32 as I, type Vec3f as J, type Vec3u as K, type Vec3i as L, type TgpuPointer as M, type NumberArrayView as N, type OmitProps as O, type Bool as P, bool as Q, type ResolutionCtx as R, Storage as S, type TgpuRenderResource as T, Uniform as U, type Vec4u as V, u32 as W, i32 as X, f32 as Y, type VecKind as Z, vec2i as _, type TgpuNamable as a, vec4i as a0, vec4u as a1, type TgpuBuffer as b, type TgpuBufferUsage as c, type TgpuBufferUniform as d, type TgpuShaderStage as e, type TgpuBufferReadonly as f, type TgpuBufferMutable as g, type U32 as h, type Vec4i as i, type Vec4f as j, type TgpuPlum as k, type Unsubscribe as l, type TgpuCode as m, type Block as n, vec3f as o, vec3i as p, vec3u as q, type TgpuData as r, isUsableAsStorage as s, isUsableAsUniform as t, isUsableAsVertex as u, type vecBase as v, Vertex as w, type AnyTgpuLooseData as x, type TgpuLooseData as y, vec2f as z };