typegpu 0.0.0-alpha.3 → 0.0.0-alpha.4

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "typegpu",
3
- "version": "0.0.0-alpha.3",
3
+ "private": false,
4
+ "version": "0.0.0-alpha.4",
4
5
  "description": "A thin layer between JS and WebGPU/WGSL that improves development experience and allows for faster iteration.",
5
6
  "license": "MIT",
6
7
  "type": "module",
@@ -31,11 +32,14 @@
31
32
  "module": "./macro/index.js",
32
33
  "import": "./macro/index.js",
33
34
  "default": "./macro/index.cjs"
35
+ },
36
+ "./web": {
37
+ "types": "./web/index.d.ts",
38
+ "module": "./web/index.js",
39
+ "import": "./web/index.js",
40
+ "default": "./web/index.cjs"
34
41
  }
35
42
  },
36
- "files": [
37
- "**"
38
- ],
39
43
  "sideEffects": false,
40
44
  "engines": {
41
45
  "node": ">=12.20.0"
@@ -57,20 +61,18 @@
57
61
  "bugs": {
58
62
  "url": "https://github.com/software-mansion/typegpu/issues"
59
63
  },
60
- "homepage": "https://github.com/software-mansion/typegpu",
64
+ "homepage": "https://docs.swmansion.com/typegpu",
61
65
  "devDependencies": {
66
+ "@webgpu/types": "^0.1.43",
67
+ "remeda": "^2.3.0",
62
68
  "tsup": "^8.0.2",
63
- "typescript": "^5.3.3",
69
+ "tsx": "^4.16.2",
64
70
  "typed-binary": "^4.0.0",
65
- "@webgpu/types": "^0.1.43"
71
+ "typescript": "^5.3.3"
66
72
  },
67
73
  "packageManager": "pnpm@8.15.8+sha256.691fe176eea9a8a80df20e4976f3dfb44a04841ceb885638fe2a26174f81e65e",
68
74
  "peerDependencies": {
69
75
  "typed-binary": "^4.0.1"
70
76
  },
71
- "scripts": {
72
- "dev": "tsup --watch",
73
- "build": "tsup",
74
- "test:types": "pnpm tsc --p ./tsconfig.json --noEmit && pnpm tsc --p ./tsconfig.test.json --noEmit"
75
- }
77
+ "scripts": {}
76
78
  }
@@ -0,0 +1,26 @@
1
+ import { AnySchema, Schema, Unwrap, ISerialOutput, ParseUnwrapped, ISerialInput, MaxValue, IMeasurer } from 'typed-binary';
2
+ import { M as WgslData, b as Wgsl, R as ResolutionCtx } from './types-ENDwr_p9.js';
3
+
4
+ declare class SimpleWgslData<TSchema extends AnySchema> extends Schema<Unwrap<TSchema>> implements WgslData<Unwrap<TSchema>> {
5
+ readonly size: number;
6
+ readonly byteAlignment: number;
7
+ readonly expressionCode: Wgsl;
8
+ private readonly _innerSchema;
9
+ /**
10
+ * byteAlignment has to be a power of 2
11
+ */
12
+ constructor({ schema, byteAlignment, code, }: {
13
+ schema: TSchema;
14
+ byteAlignment: number;
15
+ code: Wgsl;
16
+ });
17
+ resolveReferences(): void;
18
+ write(output: ISerialOutput, value: ParseUnwrapped<TSchema>): void;
19
+ read(input: ISerialInput): ParseUnwrapped<TSchema>;
20
+ measure(value: ParseUnwrapped<TSchema> | MaxValue, measurer?: IMeasurer): IMeasurer;
21
+ getUnderlyingTypeString(): string;
22
+ getUnderlyingType(): SimpleWgslData<AnySchema>;
23
+ resolve(ctx: ResolutionCtx): string;
24
+ }
25
+
26
+ export { SimpleWgslData as S };
@@ -0,0 +1,26 @@
1
+ import { AnySchema, Schema, Unwrap, ISerialOutput, ParseUnwrapped, ISerialInput, MaxValue, IMeasurer } from 'typed-binary';
2
+ import { M as WgslData, b as Wgsl, R as ResolutionCtx } from './types-ENDwr_p9.cjs';
3
+
4
+ declare class SimpleWgslData<TSchema extends AnySchema> extends Schema<Unwrap<TSchema>> implements WgslData<Unwrap<TSchema>> {
5
+ readonly size: number;
6
+ readonly byteAlignment: number;
7
+ readonly expressionCode: Wgsl;
8
+ private readonly _innerSchema;
9
+ /**
10
+ * byteAlignment has to be a power of 2
11
+ */
12
+ constructor({ schema, byteAlignment, code, }: {
13
+ schema: TSchema;
14
+ byteAlignment: number;
15
+ code: Wgsl;
16
+ });
17
+ resolveReferences(): void;
18
+ write(output: ISerialOutput, value: ParseUnwrapped<TSchema>): void;
19
+ read(input: ISerialInput): ParseUnwrapped<TSchema>;
20
+ measure(value: ParseUnwrapped<TSchema> | MaxValue, measurer?: IMeasurer): IMeasurer;
21
+ getUnderlyingTypeString(): string;
22
+ getUnderlyingType(): SimpleWgslData<AnySchema>;
23
+ resolve(ctx: ResolutionCtx): string;
24
+ }
25
+
26
+ export { SimpleWgslData as S };
@@ -0,0 +1,243 @@
1
+ import * as TB from 'typed-binary';
2
+ import { Parsed, ISchema } from 'typed-binary';
3
+
4
+ type Bool = WgslData<boolean>;
5
+ declare const bool: Bool;
6
+ type U32 = WgslData<number>;
7
+ declare const u32: U32;
8
+ type I32 = WgslData<number>;
9
+ declare const i32: I32;
10
+ type F32 = WgslData<number>;
11
+ declare const f32: F32;
12
+ type Vec2u = WgslData<[number, number]>;
13
+ declare const vec2u: Vec2u;
14
+ type Vec2i = WgslData<[number, number]>;
15
+ declare const vec2i: Vec2i;
16
+ type Vec2f = WgslData<[number, number]>;
17
+ declare const vec2f: Vec2f;
18
+ type Vec3u = WgslData<[number, number, number]>;
19
+ declare const vec3u: Vec3u;
20
+ type Vec3i = WgslData<[number, number, number]>;
21
+ declare const vec3i: Vec3i;
22
+ type Vec3f = WgslData<[number, number, number]>;
23
+ declare const vec3f: Vec3f;
24
+ type Vec4u = WgslData<[number, number, number, number]>;
25
+ declare const vec4u: Vec4u;
26
+ type Vec4i = WgslData<[number, number, number, number]>;
27
+ declare const vec4i: Vec4i;
28
+ type Vec4f = WgslData<[number, number, number, number]>;
29
+ declare const vec4f: Vec4f;
30
+ /**
31
+ * Array of column vectors
32
+ */
33
+ type Mat4f = WgslData<number[]>;
34
+ declare const mat4f: Mat4f;
35
+
36
+ /**
37
+ * Helpful when creating new Resolvable types. For internal use.
38
+ */
39
+ declare class WgslIdentifier implements WgslResolvable {
40
+ label?: string | undefined;
41
+ $name(label: string | undefined): this;
42
+ resolve(ctx: ResolutionCtx): string;
43
+ toString(): string;
44
+ }
45
+
46
+ type BuiltInPossibleTypes = U32 | F32 | Vec3u | Vec4f | WgslData<TB.Unwrap<U32>[]>;
47
+ declare const builtin: {
48
+ readonly vertexIndex: symbol;
49
+ readonly instanceIndex: symbol;
50
+ readonly position: symbol;
51
+ readonly clipDistances: symbol;
52
+ readonly frontFacing: symbol;
53
+ readonly fragDepth: symbol;
54
+ readonly sampleIndex: symbol;
55
+ readonly sampleMask: symbol;
56
+ readonly fragment: symbol;
57
+ readonly localInvocationId: symbol;
58
+ readonly localInvocationIndex: symbol;
59
+ readonly globalInvocationId: symbol;
60
+ readonly workgroupId: symbol;
61
+ readonly numWorkgroups: symbol;
62
+ };
63
+ interface Builtin {
64
+ name: string;
65
+ stage: 'vertex' | 'fragment' | 'compute';
66
+ direction: 'input' | 'output';
67
+ type: BuiltInPossibleTypes;
68
+ identifier: WgslIdentifier;
69
+ }
70
+ declare function getBuiltinInfo(s: symbol): Builtin;
71
+ declare function getUsedBuiltinsNamed(o: Record<symbol, string>): {
72
+ name: string;
73
+ builtin: Builtin;
74
+ }[];
75
+
76
+ declare const WgslSettableTrait: unique symbol;
77
+ interface WgslSettable {
78
+ readonly [WgslSettableTrait]: true;
79
+ }
80
+
81
+ type Getter = <T>(plum: WgslPlum<T>) => T;
82
+ type Unsubscribe = () => unknown;
83
+ type ExtractPlumValue<T> = T extends WgslPlum<infer TValue> ? TValue : never;
84
+ interface WgslPlum<TValue = unknown> {
85
+ readonly __brand: 'WgslPlum';
86
+ $name(label: string): this;
87
+ /**
88
+ * Computes the value of this plum. Circumvents the store
89
+ * memoization, so use with care.
90
+ */
91
+ compute(get: Getter): TValue;
92
+ }
93
+ declare const WgslExternalPlumTrait: unique symbol;
94
+ interface WgslExternalPlum {
95
+ readonly [WgslExternalPlumTrait]: true;
96
+ readonly version: number;
97
+ subscribe(listener: () => unknown): Unsubscribe;
98
+ }
99
+ /**
100
+ * Creates a computed plum. Its value depends on the plums read using `get`
101
+ * inside the `compute` function, so cannot be set imperatively.
102
+ *
103
+ * @param compute A pure function that describes this plum's value.
104
+ */
105
+ declare function plum<T extends Wgsl>(compute: (get: Getter) => T): WgslPlum<T> & WgslResolvable;
106
+ /**
107
+ * Creates a computed plum. Its value depends on the plums read using `get`
108
+ * inside the `compute` function, so cannot be set imperatively.
109
+ *
110
+ * @param compute A pure function that describes this plum's value.
111
+ */
112
+ declare function plum<T>(compute: (get: Getter) => T): WgslPlum<T>;
113
+ /**
114
+ * Creates a plum with an initial value of `initial`.
115
+ * Its value can be updated by calling `runtime.setPlum(thePlum, newValue)`.
116
+ *
117
+ * @param initial The initial value of this plum.
118
+ */
119
+ declare function plum<T extends Wgsl>(initial: T): WgslPlum<T> & WgslSettable & WgslResolvable;
120
+ /**
121
+ * Creates a plum with an initial value of `initial`.
122
+ * Its value can be updated by calling `runtime.setPlum(thePlum, newValue)`.
123
+ *
124
+ * @param initial The initial value of this plum.
125
+ */
126
+ declare function plum<T>(initial: T): WgslPlum<T> & WgslSettable;
127
+ declare function plumFromEvent<T>(subscribe: (listener: () => unknown) => Unsubscribe, getLatest: () => T): WgslPlum<T> & WgslExternalPlum;
128
+
129
+ type Wgsl = string | number | WgslResolvable | symbol | boolean;
130
+ /**
131
+ * Passed into each resolvable item. All sibling items share a resolution ctx,
132
+ * and a new resolution ctx is made when going down each level in the tree.
133
+ */
134
+ interface ResolutionCtx {
135
+ /**
136
+ * Slots that were used by items resolved by this context.
137
+ */
138
+ readonly usedSlots: Iterable<WgslSlot<unknown>>;
139
+ addDeclaration(item: WgslResolvable): void;
140
+ addBinding(bindable: WgslBindable, identifier: WgslIdentifier): void;
141
+ addRenderResource(resource: WgslRenderResource, identifier: WgslIdentifier): void;
142
+ addBuiltin(builtin: Builtin): void;
143
+ nameFor(token: WgslResolvable): string;
144
+ /**
145
+ * Unwraps all layers of slot indirection and returns the concrete value if available.
146
+ * @throws {MissingSlotValueError}
147
+ */
148
+ unwrap<T>(eventual: Eventual<T>): T;
149
+ resolve(item: Wgsl, slotValueOverrides?: SlotValuePair<unknown>[]): string;
150
+ }
151
+ interface WgslResolvable {
152
+ readonly label?: string | undefined;
153
+ resolve(ctx: ResolutionCtx): string;
154
+ }
155
+ declare function isResolvable(value: unknown): value is WgslResolvable;
156
+ declare function isWgsl(value: unknown): value is Wgsl;
157
+ interface WgslSlot<T> {
158
+ readonly __brand: 'WgslSlot';
159
+ readonly defaultValue: T | undefined;
160
+ readonly label?: string | undefined;
161
+ $name(label: string): WgslSlot<T>;
162
+ /**
163
+ * Used to determine if code generated using either value `a` or `b` in place
164
+ * of the slot will be equivalent. Defaults to `Object.is`.
165
+ */
166
+ areEqual(a: T, b: T): boolean;
167
+ }
168
+ declare function isSlot<T>(value: unknown | WgslSlot<T>): value is WgslSlot<T>;
169
+ /**
170
+ * Represents a value that is available at resolution time.
171
+ */
172
+ type Eventual<T> = T | WgslSlot<T>;
173
+ type EventualGetter = <T>(value: Eventual<T>) => T;
174
+ type InlineResolve = (get: EventualGetter) => Wgsl;
175
+ interface WgslResolvableSlot<T extends Wgsl> extends WgslResolvable, WgslSlot<T> {
176
+ $name(label: string): WgslResolvableSlot<T>;
177
+ }
178
+ type SlotValuePair<T> = [WgslSlot<T>, T];
179
+ interface WgslAllocatable<TData extends AnyWgslData = AnyWgslData> {
180
+ /**
181
+ * The data type this allocatable was constructed with.
182
+ * It informs the size and format of data in both JS and
183
+ * binary.
184
+ */
185
+ readonly dataType: TData;
186
+ vertexLayout: Omit<GPUVertexBufferLayout, 'attributes'> | null;
187
+ readonly initial?: Parsed<TData> | WgslPlum<Parsed<TData>> | undefined;
188
+ readonly flags: GPUBufferUsageFlags;
189
+ get label(): string | undefined;
190
+ }
191
+ interface WgslBindable<TData extends AnyWgslData = AnyWgslData, TUsage extends BufferUsage = BufferUsage> extends WgslResolvable {
192
+ readonly allocatable: WgslAllocatable<TData>;
193
+ readonly usage: TUsage;
194
+ }
195
+ type WgslSamplerType = 'sampler' | 'sampler_comparison';
196
+ type WgslTypedTextureType = 'texture_1d' | 'texture_2d' | 'texture_2d_array' | 'texture_3d' | 'texture_cube' | 'texture_cube_array' | 'texture_multisampled_2d';
197
+ type WgslDepthTextureType = 'texture_depth_2d' | 'texture_depth_2d_array' | 'texture_depth_cube' | 'texture_depth_cube_array' | 'texture_depth_multisampled_2d';
198
+ type WgslStorageTextureType = 'texture_storage_1d' | 'texture_storage_2d' | 'texture_storage_2d_array' | 'texture_storage_3d';
199
+ type WgslExternalTextureType = 'texture_external';
200
+ type WgslRenderResourceType = WgslSamplerType | WgslTypedTextureType | WgslDepthTextureType | WgslStorageTextureType | WgslExternalTextureType;
201
+ interface WgslRenderResource extends WgslResolvable {
202
+ readonly type: WgslRenderResourceType;
203
+ }
204
+ type BufferUsage = 'uniform' | 'readonly' | 'mutable' | 'vertex';
205
+ type TextureUsage = 'sampled' | 'storage';
206
+ type StorageTextureAccess = 'read' | 'write' | 'read_write';
207
+ type StorageTextureParams = {
208
+ type: WgslStorageTextureType;
209
+ access: StorageTextureAccess;
210
+ descriptor?: GPUTextureViewDescriptor;
211
+ };
212
+ type SampledTextureParams = {
213
+ type: WgslTypedTextureType;
214
+ dataType: AnyWgslPrimitive;
215
+ descriptor?: GPUTextureViewDescriptor;
216
+ };
217
+ declare function isSamplerType(type: WgslRenderResourceType): type is WgslSamplerType;
218
+ declare function isTypedTextureType(type: WgslRenderResourceType): type is WgslTypedTextureType;
219
+ declare function isDepthTextureType(type: WgslRenderResourceType): type is WgslDepthTextureType;
220
+ declare function isStorageTextureType(type: WgslRenderResourceType): type is WgslStorageTextureType;
221
+ declare function isExternalTextureType(type: WgslRenderResourceType): type is WgslExternalTextureType;
222
+ interface WgslData<TInner> extends ISchema<TInner>, WgslResolvable {
223
+ readonly byteAlignment: number;
224
+ readonly size: number;
225
+ }
226
+ type AnyWgslData = WgslData<unknown>;
227
+ type AnyWgslPrimitive = U32 | I32 | F32;
228
+ type AnyWgslTexelFormat = Vec4u | Vec4i | Vec4f;
229
+ interface WgslPointer<TScope extends 'function', TInner extends AnyWgslData> {
230
+ readonly scope: TScope;
231
+ readonly pointsTo: TInner;
232
+ }
233
+ /**
234
+ * A virtual representation of a WGSL value.
235
+ */
236
+ type WgslValue<TDataType> = {
237
+ readonly __dataType: TDataType;
238
+ };
239
+ type AnyWgslPointer = WgslPointer<'function', AnyWgslData>;
240
+ type WgslFnArgument = AnyWgslPointer | AnyWgslData;
241
+ declare function isPointer(value: AnyWgslPointer | AnyWgslData): value is AnyWgslPointer;
242
+
243
+ export { type I32 as $, type AnyWgslTexelFormat as A, type BufferUsage as B, type WgslDepthTextureType as C, type WgslStorageTextureType as D, type Eventual as E, type WgslExternalTextureType as F, isSamplerType as G, isTypedTextureType as H, type InlineResolve as I, isDepthTextureType as J, isStorageTextureType as K, isExternalTextureType as L, type WgslData as M, type WgslPointer as N, type AnyWgslPointer as O, isPointer as P, type BuiltInPossibleTypes as Q, type ResolutionCtx as R, type StorageTextureParams as S, type TextureUsage as T, type Unsubscribe as U, builtin as V, type WgslResolvable as W, type Builtin as X, getBuiltinInfo as Y, getUsedBuiltinsNamed as Z, type U32 as _, type WgslSlot as a, type Bool as a0, bool as a1, u32 as a2, i32 as a3, type F32 as a4, f32 as a5, type Vec2u as a6, vec2u as a7, type Vec2i as a8, vec2i as a9, type Vec2f as aa, vec2f as ab, type Vec3u as ac, vec3u as ad, type Vec3i as ae, vec3i as af, type Vec3f as ag, vec3f as ah, type Vec4u as ai, vec4u as aj, type Vec4i as ak, vec4i as al, type Vec4f as am, vec4f as an, type Mat4f as ao, mat4f as ap, type Wgsl as b, type WgslRenderResource as c, type SampledTextureParams as d, type AnyWgslPrimitive as e, type WgslRenderResourceType as f, type StorageTextureAccess as g, type WgslPlum as h, type ExtractPlumValue as i, type WgslSettable as j, type AnyWgslData as k, type WgslAllocatable as l, type WgslBindable as m, type WgslFnArgument as n, type WgslValue as o, WgslIdentifier as p, type WgslResolvableSlot as q, plum as r, plumFromEvent as s, isResolvable as t, isWgsl as u, isSlot as v, type EventualGetter as w, type SlotValuePair as x, type WgslSamplerType as y, type WgslTypedTextureType as z };
@@ -0,0 +1,243 @@
1
+ import * as TB from 'typed-binary';
2
+ import { Parsed, ISchema } from 'typed-binary';
3
+
4
+ type Bool = WgslData<boolean>;
5
+ declare const bool: Bool;
6
+ type U32 = WgslData<number>;
7
+ declare const u32: U32;
8
+ type I32 = WgslData<number>;
9
+ declare const i32: I32;
10
+ type F32 = WgslData<number>;
11
+ declare const f32: F32;
12
+ type Vec2u = WgslData<[number, number]>;
13
+ declare const vec2u: Vec2u;
14
+ type Vec2i = WgslData<[number, number]>;
15
+ declare const vec2i: Vec2i;
16
+ type Vec2f = WgslData<[number, number]>;
17
+ declare const vec2f: Vec2f;
18
+ type Vec3u = WgslData<[number, number, number]>;
19
+ declare const vec3u: Vec3u;
20
+ type Vec3i = WgslData<[number, number, number]>;
21
+ declare const vec3i: Vec3i;
22
+ type Vec3f = WgslData<[number, number, number]>;
23
+ declare const vec3f: Vec3f;
24
+ type Vec4u = WgslData<[number, number, number, number]>;
25
+ declare const vec4u: Vec4u;
26
+ type Vec4i = WgslData<[number, number, number, number]>;
27
+ declare const vec4i: Vec4i;
28
+ type Vec4f = WgslData<[number, number, number, number]>;
29
+ declare const vec4f: Vec4f;
30
+ /**
31
+ * Array of column vectors
32
+ */
33
+ type Mat4f = WgslData<number[]>;
34
+ declare const mat4f: Mat4f;
35
+
36
+ /**
37
+ * Helpful when creating new Resolvable types. For internal use.
38
+ */
39
+ declare class WgslIdentifier implements WgslResolvable {
40
+ label?: string | undefined;
41
+ $name(label: string | undefined): this;
42
+ resolve(ctx: ResolutionCtx): string;
43
+ toString(): string;
44
+ }
45
+
46
+ type BuiltInPossibleTypes = U32 | F32 | Vec3u | Vec4f | WgslData<TB.Unwrap<U32>[]>;
47
+ declare const builtin: {
48
+ readonly vertexIndex: symbol;
49
+ readonly instanceIndex: symbol;
50
+ readonly position: symbol;
51
+ readonly clipDistances: symbol;
52
+ readonly frontFacing: symbol;
53
+ readonly fragDepth: symbol;
54
+ readonly sampleIndex: symbol;
55
+ readonly sampleMask: symbol;
56
+ readonly fragment: symbol;
57
+ readonly localInvocationId: symbol;
58
+ readonly localInvocationIndex: symbol;
59
+ readonly globalInvocationId: symbol;
60
+ readonly workgroupId: symbol;
61
+ readonly numWorkgroups: symbol;
62
+ };
63
+ interface Builtin {
64
+ name: string;
65
+ stage: 'vertex' | 'fragment' | 'compute';
66
+ direction: 'input' | 'output';
67
+ type: BuiltInPossibleTypes;
68
+ identifier: WgslIdentifier;
69
+ }
70
+ declare function getBuiltinInfo(s: symbol): Builtin;
71
+ declare function getUsedBuiltinsNamed(o: Record<symbol, string>): {
72
+ name: string;
73
+ builtin: Builtin;
74
+ }[];
75
+
76
+ declare const WgslSettableTrait: unique symbol;
77
+ interface WgslSettable {
78
+ readonly [WgslSettableTrait]: true;
79
+ }
80
+
81
+ type Getter = <T>(plum: WgslPlum<T>) => T;
82
+ type Unsubscribe = () => unknown;
83
+ type ExtractPlumValue<T> = T extends WgslPlum<infer TValue> ? TValue : never;
84
+ interface WgslPlum<TValue = unknown> {
85
+ readonly __brand: 'WgslPlum';
86
+ $name(label: string): this;
87
+ /**
88
+ * Computes the value of this plum. Circumvents the store
89
+ * memoization, so use with care.
90
+ */
91
+ compute(get: Getter): TValue;
92
+ }
93
+ declare const WgslExternalPlumTrait: unique symbol;
94
+ interface WgslExternalPlum {
95
+ readonly [WgslExternalPlumTrait]: true;
96
+ readonly version: number;
97
+ subscribe(listener: () => unknown): Unsubscribe;
98
+ }
99
+ /**
100
+ * Creates a computed plum. Its value depends on the plums read using `get`
101
+ * inside the `compute` function, so cannot be set imperatively.
102
+ *
103
+ * @param compute A pure function that describes this plum's value.
104
+ */
105
+ declare function plum<T extends Wgsl>(compute: (get: Getter) => T): WgslPlum<T> & WgslResolvable;
106
+ /**
107
+ * Creates a computed plum. Its value depends on the plums read using `get`
108
+ * inside the `compute` function, so cannot be set imperatively.
109
+ *
110
+ * @param compute A pure function that describes this plum's value.
111
+ */
112
+ declare function plum<T>(compute: (get: Getter) => T): WgslPlum<T>;
113
+ /**
114
+ * Creates a plum with an initial value of `initial`.
115
+ * Its value can be updated by calling `runtime.setPlum(thePlum, newValue)`.
116
+ *
117
+ * @param initial The initial value of this plum.
118
+ */
119
+ declare function plum<T extends Wgsl>(initial: T): WgslPlum<T> & WgslSettable & WgslResolvable;
120
+ /**
121
+ * Creates a plum with an initial value of `initial`.
122
+ * Its value can be updated by calling `runtime.setPlum(thePlum, newValue)`.
123
+ *
124
+ * @param initial The initial value of this plum.
125
+ */
126
+ declare function plum<T>(initial: T): WgslPlum<T> & WgslSettable;
127
+ declare function plumFromEvent<T>(subscribe: (listener: () => unknown) => Unsubscribe, getLatest: () => T): WgslPlum<T> & WgslExternalPlum;
128
+
129
+ type Wgsl = string | number | WgslResolvable | symbol | boolean;
130
+ /**
131
+ * Passed into each resolvable item. All sibling items share a resolution ctx,
132
+ * and a new resolution ctx is made when going down each level in the tree.
133
+ */
134
+ interface ResolutionCtx {
135
+ /**
136
+ * Slots that were used by items resolved by this context.
137
+ */
138
+ readonly usedSlots: Iterable<WgslSlot<unknown>>;
139
+ addDeclaration(item: WgslResolvable): void;
140
+ addBinding(bindable: WgslBindable, identifier: WgslIdentifier): void;
141
+ addRenderResource(resource: WgslRenderResource, identifier: WgslIdentifier): void;
142
+ addBuiltin(builtin: Builtin): void;
143
+ nameFor(token: WgslResolvable): string;
144
+ /**
145
+ * Unwraps all layers of slot indirection and returns the concrete value if available.
146
+ * @throws {MissingSlotValueError}
147
+ */
148
+ unwrap<T>(eventual: Eventual<T>): T;
149
+ resolve(item: Wgsl, slotValueOverrides?: SlotValuePair<unknown>[]): string;
150
+ }
151
+ interface WgslResolvable {
152
+ readonly label?: string | undefined;
153
+ resolve(ctx: ResolutionCtx): string;
154
+ }
155
+ declare function isResolvable(value: unknown): value is WgslResolvable;
156
+ declare function isWgsl(value: unknown): value is Wgsl;
157
+ interface WgslSlot<T> {
158
+ readonly __brand: 'WgslSlot';
159
+ readonly defaultValue: T | undefined;
160
+ readonly label?: string | undefined;
161
+ $name(label: string): WgslSlot<T>;
162
+ /**
163
+ * Used to determine if code generated using either value `a` or `b` in place
164
+ * of the slot will be equivalent. Defaults to `Object.is`.
165
+ */
166
+ areEqual(a: T, b: T): boolean;
167
+ }
168
+ declare function isSlot<T>(value: unknown | WgslSlot<T>): value is WgslSlot<T>;
169
+ /**
170
+ * Represents a value that is available at resolution time.
171
+ */
172
+ type Eventual<T> = T | WgslSlot<T>;
173
+ type EventualGetter = <T>(value: Eventual<T>) => T;
174
+ type InlineResolve = (get: EventualGetter) => Wgsl;
175
+ interface WgslResolvableSlot<T extends Wgsl> extends WgslResolvable, WgslSlot<T> {
176
+ $name(label: string): WgslResolvableSlot<T>;
177
+ }
178
+ type SlotValuePair<T> = [WgslSlot<T>, T];
179
+ interface WgslAllocatable<TData extends AnyWgslData = AnyWgslData> {
180
+ /**
181
+ * The data type this allocatable was constructed with.
182
+ * It informs the size and format of data in both JS and
183
+ * binary.
184
+ */
185
+ readonly dataType: TData;
186
+ vertexLayout: Omit<GPUVertexBufferLayout, 'attributes'> | null;
187
+ readonly initial?: Parsed<TData> | WgslPlum<Parsed<TData>> | undefined;
188
+ readonly flags: GPUBufferUsageFlags;
189
+ get label(): string | undefined;
190
+ }
191
+ interface WgslBindable<TData extends AnyWgslData = AnyWgslData, TUsage extends BufferUsage = BufferUsage> extends WgslResolvable {
192
+ readonly allocatable: WgslAllocatable<TData>;
193
+ readonly usage: TUsage;
194
+ }
195
+ type WgslSamplerType = 'sampler' | 'sampler_comparison';
196
+ type WgslTypedTextureType = 'texture_1d' | 'texture_2d' | 'texture_2d_array' | 'texture_3d' | 'texture_cube' | 'texture_cube_array' | 'texture_multisampled_2d';
197
+ type WgslDepthTextureType = 'texture_depth_2d' | 'texture_depth_2d_array' | 'texture_depth_cube' | 'texture_depth_cube_array' | 'texture_depth_multisampled_2d';
198
+ type WgslStorageTextureType = 'texture_storage_1d' | 'texture_storage_2d' | 'texture_storage_2d_array' | 'texture_storage_3d';
199
+ type WgslExternalTextureType = 'texture_external';
200
+ type WgslRenderResourceType = WgslSamplerType | WgslTypedTextureType | WgslDepthTextureType | WgslStorageTextureType | WgslExternalTextureType;
201
+ interface WgslRenderResource extends WgslResolvable {
202
+ readonly type: WgslRenderResourceType;
203
+ }
204
+ type BufferUsage = 'uniform' | 'readonly' | 'mutable' | 'vertex';
205
+ type TextureUsage = 'sampled' | 'storage';
206
+ type StorageTextureAccess = 'read' | 'write' | 'read_write';
207
+ type StorageTextureParams = {
208
+ type: WgslStorageTextureType;
209
+ access: StorageTextureAccess;
210
+ descriptor?: GPUTextureViewDescriptor;
211
+ };
212
+ type SampledTextureParams = {
213
+ type: WgslTypedTextureType;
214
+ dataType: AnyWgslPrimitive;
215
+ descriptor?: GPUTextureViewDescriptor;
216
+ };
217
+ declare function isSamplerType(type: WgslRenderResourceType): type is WgslSamplerType;
218
+ declare function isTypedTextureType(type: WgslRenderResourceType): type is WgslTypedTextureType;
219
+ declare function isDepthTextureType(type: WgslRenderResourceType): type is WgslDepthTextureType;
220
+ declare function isStorageTextureType(type: WgslRenderResourceType): type is WgslStorageTextureType;
221
+ declare function isExternalTextureType(type: WgslRenderResourceType): type is WgslExternalTextureType;
222
+ interface WgslData<TInner> extends ISchema<TInner>, WgslResolvable {
223
+ readonly byteAlignment: number;
224
+ readonly size: number;
225
+ }
226
+ type AnyWgslData = WgslData<unknown>;
227
+ type AnyWgslPrimitive = U32 | I32 | F32;
228
+ type AnyWgslTexelFormat = Vec4u | Vec4i | Vec4f;
229
+ interface WgslPointer<TScope extends 'function', TInner extends AnyWgslData> {
230
+ readonly scope: TScope;
231
+ readonly pointsTo: TInner;
232
+ }
233
+ /**
234
+ * A virtual representation of a WGSL value.
235
+ */
236
+ type WgslValue<TDataType> = {
237
+ readonly __dataType: TDataType;
238
+ };
239
+ type AnyWgslPointer = WgslPointer<'function', AnyWgslData>;
240
+ type WgslFnArgument = AnyWgslPointer | AnyWgslData;
241
+ declare function isPointer(value: AnyWgslPointer | AnyWgslData): value is AnyWgslPointer;
242
+
243
+ export { type I32 as $, type AnyWgslTexelFormat as A, type BufferUsage as B, type WgslDepthTextureType as C, type WgslStorageTextureType as D, type Eventual as E, type WgslExternalTextureType as F, isSamplerType as G, isTypedTextureType as H, type InlineResolve as I, isDepthTextureType as J, isStorageTextureType as K, isExternalTextureType as L, type WgslData as M, type WgslPointer as N, type AnyWgslPointer as O, isPointer as P, type BuiltInPossibleTypes as Q, type ResolutionCtx as R, type StorageTextureParams as S, type TextureUsage as T, type Unsubscribe as U, builtin as V, type WgslResolvable as W, type Builtin as X, getBuiltinInfo as Y, getUsedBuiltinsNamed as Z, type U32 as _, type WgslSlot as a, type Bool as a0, bool as a1, u32 as a2, i32 as a3, type F32 as a4, f32 as a5, type Vec2u as a6, vec2u as a7, type Vec2i as a8, vec2i as a9, type Vec2f as aa, vec2f as ab, type Vec3u as ac, vec3u as ad, type Vec3i as ae, vec3i as af, type Vec3f as ag, vec3f as ah, type Vec4u as ai, vec4u as aj, type Vec4i as ak, vec4i as al, type Vec4f as am, vec4f as an, type Mat4f as ao, mat4f as ap, type Wgsl as b, type WgslRenderResource as c, type SampledTextureParams as d, type AnyWgslPrimitive as e, type WgslRenderResourceType as f, type StorageTextureAccess as g, type WgslPlum as h, type ExtractPlumValue as i, type WgslSettable as j, type AnyWgslData as k, type WgslAllocatable as l, type WgslBindable as m, type WgslFnArgument as n, type WgslValue as o, WgslIdentifier as p, type WgslResolvableSlot as q, plum as r, plumFromEvent as s, isResolvable as t, isWgsl as u, isSlot as v, type EventualGetter as w, type SlotValuePair as x, type WgslSamplerType as y, type WgslTypedTextureType as z };
package/.DS_Store DELETED
Binary file
@@ -1,4 +0,0 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});var r=class e extends Error{constructor(o,s){let t=s.map(c=>`- ${c}`);t.length>20&&(t=[...t.slice(0,11),"...",...t.slice(-10)]);super(`Resolution of the following tree failed:
2
- ${t.join(`
3
- `)}`);this.cause=o;this.trace=s;Object.setPrototypeOf(this,e.prototype)}appendToTrace(o){let s=[o,...this.trace];return new e(this.cause,s)}},l= exports.b =class e extends Error{constructor(o){super(`Missing value for '${o}'`);this.slot=o;Object.setPrototypeOf(this,e.prototype)}},n= exports.c =class e extends Error{constructor(){super("Recursive types are not supported in WGSL"),Object.setPrototypeOf(this,e.prototype)}};exports.a = r; exports.b = l; exports.c = n;
4
- //# sourceMappingURL=chunk-G3WAZR24.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/errors.ts"],"names":["ResolutionError","_ResolutionError","cause","trace","entries","ancestor","newTrace","MissingSlotValueError","_MissingSlotValueError","slot","RecursiveDataTypeError","_RecursiveDataTypeError"],"mappings":"AASO,IAAMA,EAAN,MAAMC,UAAwB,KAAM,CACzC,YACkBC,EACAC,EAChB,CACA,IAAIC,EAAUD,EAAM,IAAKE,GAAa,KAAKA,CAAQ,EAAE,EAGjDD,EAAQ,OAAS,KACnBA,EAAU,CAAC,GAAGA,EAAQ,MAAM,EAAG,EAAE,EAAG,MAAO,GAAGA,EAAQ,MAAM,GAAG,CAAC,GAGlE,MAAM;AAAA,EAA8CA,EAAQ,KAAK;AAAA,CAAI,CAAC,EAAE,EAVxD,WAAAF,EACA,WAAAC,EAYhB,OAAO,eAAe,KAAMF,EAAgB,SAAS,CACvD,CAEA,cAAcI,EAA2C,CACvD,IAAMC,EAAW,CAACD,EAAU,GAAG,KAAK,KAAK,EAEzC,OAAO,IAAIJ,EAAgB,KAAK,MAAOK,CAAQ,CACjD,CACF,EAKaC,EAAN,MAAMC,UAA8B,KAAM,CAC/C,YAA4BC,EAAyB,CACnD,MAAM,sBAAsBA,CAAI,GAAG,EADT,UAAAA,EAI1B,OAAO,eAAe,KAAMD,EAAsB,SAAS,CAC7D,CACF,EAKaE,EAAN,MAAMC,UAA+B,KAAM,CAChD,aAAc,CACZ,MAAM,2CAA2C,EAGjD,OAAO,eAAe,KAAMA,EAAuB,SAAS,CAC9D,CACF","sourcesContent":["import type { WgslResolvable, WgslSlot } from './types';\n\n/**\n * An error that happens during resolution of WGSL code.\n * Contains a trace of all ancestor resolvables in\n * which this error originated.\n *\n * @category Errors\n */\nexport class ResolutionError extends Error {\n constructor(\n public readonly cause: unknown,\n public readonly trace: WgslResolvable[],\n ) {\n let entries = trace.map((ancestor) => `- ${ancestor}`);\n\n // Showing only the root and leaf nodes.\n if (entries.length > 20) {\n entries = [...entries.slice(0, 11), '...', ...entries.slice(-10)];\n }\n\n super(`Resolution of the following tree failed: \\n${entries.join('\\n')}`);\n\n // Set the prototype explicitly.\n Object.setPrototypeOf(this, ResolutionError.prototype);\n }\n\n appendToTrace(ancestor: WgslResolvable): ResolutionError {\n const newTrace = [ancestor, ...this.trace];\n\n return new ResolutionError(this.cause, newTrace);\n }\n}\n\n/**\n * @category Errors\n */\nexport class MissingSlotValueError extends Error {\n constructor(public readonly slot: WgslSlot<unknown>) {\n super(`Missing value for '${slot}'`);\n\n // Set the prototype explicitly.\n Object.setPrototypeOf(this, MissingSlotValueError.prototype);\n }\n}\n\n/**\n * @category Errors\n */\nexport class RecursiveDataTypeError extends Error {\n constructor() {\n super('Recursive types are not supported in WGSL');\n\n // Set the prototype explicitly.\n Object.setPrototypeOf(this, RecursiveDataTypeError.prototype);\n }\n}\n"]}
@@ -1,2 +0,0 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});var f=Object.defineProperty,T=Object.defineProperties;var b=Object.getOwnPropertyDescriptors;var a=Object.getOwnPropertySymbols;var p=Object.prototype.hasOwnProperty,W=Object.prototype.propertyIsEnumerable;var i=(t,e,l)=>e in t?f(t,e,{enumerable:!0,configurable:!0,writable:!0,value:l}):t[e]=l,m= exports.a =(t,e)=>{for(var l in e||(e={}))p.call(e,l)&&i(t,l,e[l]);if(a)for(var l of a(e))W.call(e,l)&&i(t,l,e[l]);return t},v= exports.b =(t,e)=>T(t,b(e));var c=(t,e)=>{var l={};for(var n in t)p.call(t,n)&&e.indexOf(n)<0&&(l[n]=t[n]);if(t!=null&&a)for(var n of a(t))e.indexOf(n)<0&&W.call(t,n)&&(l[n]=t[n]);return l};var r=(t,e,l)=>(i(t,typeof e!="symbol"?e+"":e,l),l);function g(t){return!!t&&(typeof t=="object"||typeof t=="function")&&"resolve"in t}function S(t){return typeof t=="number"||typeof t=="boolean"||typeof t=="string"||g(t)}function R(t){return t.__brand==="WgslSlot"}function h(t){return"pointsTo"in t}function D(t,...e){let l=t.flatMap((n,s)=>{let o=e[s];return o===void 0?[n]:Array.isArray(o)?[n,...o]:[n,o]});return new u(l)}var u=class{constructor(e){this.segments=e;r(this,"_label")}get label(){return this._label}$name(e){return this._label=e,this}resolve(e){let l="";for(let n of this.segments)if(typeof n=="function"){let s=n(o=>e.unwrap(o));l+=e.resolve(s)}else g(n)?l+=e.resolve(n):l+=String(n);return l}with(e,l){return new d(this,[e,l])}toString(){var e;return`code:${(e=this._label)!=null?e:"<unnamed>"}`}},d=class t{constructor(e,l){this._innerFn=e;this._slotValuePair=l}get label(){return this._innerFn.label}with(e,l){return new t(this,[e,l])}resolve(e){return e.resolve(this._innerFn,[this._slotValuePair])}toString(){var n,s;let[e,l]=this._slotValuePair;return`code:${(n=this.label)!=null?n:"<unnamed>"}[${(s=e.label)!=null?s:"<unnamed>"}=${l}]`}};var y=class{constructor(){r(this,"label")}$name(e){return this.label=e,this}resolve(e){return e.nameFor(this)}toString(){var e;return`id:${(e=this.label)!=null?e:"<unnamed>"}`}};exports.a = m; exports.b = v; exports.c = c; exports.d = r; exports.e = g; exports.f = S; exports.g = R; exports.h = h; exports.i = D; exports.j = y;
2
- //# sourceMappingURL=chunk-JYXJ5NFX.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/types.ts","../src/wgslCode.ts","../src/wgslIdentifier.ts"],"names":["isResolvable","value","isWgsl","isSlot","isPointer","code","strings","params","segments","string","idx","param","WgslCodeImpl","__publicField","label","ctx","s","result","eventual","slot","BoundWgslCodeImpl","_a","_BoundWgslCodeImpl","_innerFn","_slotValuePair","_b","WgslIdentifier"],"mappings":"moBAiCO,SAASA,EAAaC,EAAyC,CACpE,MACE,CAAC,CAACA,IACD,OAAOA,GAAU,UAAY,OAAOA,GAAU,aAC/C,YAAaA,CAEjB,CAEO,SAASC,EAAOD,EAA+B,CACpD,OACE,OAAOA,GAAU,UACjB,OAAOA,GAAU,WACjB,OAAOA,GAAU,UACjBD,EAAaC,CAAK,CAEtB,CAkBO,SAASE,EAAUF,EAAoD,CAC5E,OAAQA,EAAsB,UAAY,UAC5C,CAkEO,SAASG,EACdH,EACyB,CACzB,MAAO,aAAcA,CACvB,CCnHO,SAASI,EACdC,KACGC,EACO,CACV,IAAMC,EAAqCF,EAAQ,QAAQ,CAACG,EAAQC,IAAQ,CAC1E,IAAMC,EAAQJ,EAAOG,CAAG,EACxB,OAAIC,IAAU,OACL,CAACF,CAAM,EAGT,MAAM,QAAQE,CAAK,EAAI,CAACF,EAAQ,GAAGE,CAAK,EAAI,CAACF,EAAQE,CAAK,CACnE,CAAC,EAED,OAAO,IAAIC,EAAaJ,CAAQ,CAClC,CAMA,IAAMI,EAAN,KAAuC,CAGrC,YAA4BJ,EAAoC,CAApC,cAAAA,EAF5BK,EAAA,KAAQ,SAEyD,CAEjE,IAAI,OAAQ,CACV,OAAO,KAAK,MACd,CAEA,MAAMC,EAA4B,CAChC,YAAK,OAASA,EACP,IACT,CAEA,QAAQC,EAAoB,CAC1B,IAAIV,EAAO,GAEX,QAAWW,KAAK,KAAK,SACnB,GAAI,OAAOA,GAAM,WAAY,CAC3B,IAAMC,EAASD,EAAGE,GAAaH,EAAI,OAAOG,CAAQ,CAAC,EACnDb,GAAQU,EAAI,QAAQE,CAAM,CAC5B,MAAWjB,EAAagB,CAAC,EACvBX,GAAQU,EAAI,QAAQC,CAAC,EAErBX,GAAQ,OAAOW,CAAC,EAIpB,OAAOX,CACT,CAEA,KAAac,EAAwBlB,EAAwC,CAC3E,OAAO,IAAImB,EAAkB,KAAM,CAACD,EAAMlB,CAAK,CAAC,CAClD,CAEA,UAAmB,CA9ErB,IAAAoB,EA+EI,MAAO,SAAQA,EAAA,KAAK,SAAL,KAAAA,EAAe,WAAW,EAC3C,CACF,EAEMD,EAAN,MAAME,CAA8C,CAClD,YACmBC,EACAC,EACjB,CAFiB,cAAAD,EACA,oBAAAC,CAChB,CAEH,IAAI,OAAQ,CACV,OAAO,KAAK,SAAS,KACvB,CAEA,KAAaL,EAAwBlB,EAAwC,CAC3E,OAAO,IAAIqB,EAAkB,KAAM,CAACH,EAAMlB,CAAK,CAAC,CAClD,CAEA,QAAQc,EAA4B,CAClC,OAAOA,EAAI,QAAQ,KAAK,SAAU,CAAC,KAAK,cAAc,CAAC,CACzD,CAEA,UAAmB,CArGrB,IAAAM,EAAAI,EAsGI,GAAM,CAACN,EAAMlB,CAAK,EAAI,KAAK,eAC3B,MAAO,SAAQoB,EAAA,KAAK,QAAL,KAAAA,EAAc,WAAW,KAAII,EAAAN,EAAK,QAAL,KAAAM,EAAc,WAAW,IAAIxB,CAAK,GAChF,CACF,ECpGO,IAAMyB,EAAN,KAA+C,CAA/C,cACLb,EAAA,cAEA,MAAMC,EAA2B,CAC/B,YAAK,MAAQA,EACN,IACT,CAEA,QAAQC,EAA4B,CAClC,OAAOA,EAAI,QAAQ,IAAI,CACzB,CAEA,UAAmB,CAjBrB,IAAAM,EAkBI,MAAO,OAAMA,EAAA,KAAK,QAAL,KAAAA,EAAc,WAAW,EACxC,CACF","sourcesContent":["import type { ISchema, Parsed } from 'typed-binary';\nimport type { WgslIdentifier } from './wgslIdentifier';\nimport type { WgslPlum } from './wgslPlum';\n\nexport type Wgsl = string | boolean | number | WgslResolvable;\n\n/**\n * Passed into each resolvable item. All sibling items share a resolution ctx,\n * and a new resolution ctx is made when going down each level in the tree.\n */\nexport interface ResolutionCtx {\n /**\n * Slots that were used by items resolved by this context.\n */\n readonly usedSlots: Iterable<WgslSlot<unknown>>;\n\n addDeclaration(item: WgslResolvable): void;\n addBinding(bindable: WgslBindable, identifier: WgslIdentifier): void;\n nameFor(token: WgslResolvable): string;\n /**\n * Unwraps all layers of slot indirection and returns the concrete value if available.\n * @throws {MissingSlotValueError}\n */\n unwrap<T>(eventual: Eventual<T>): T;\n resolve(item: Wgsl, slotValueOverrides?: SlotValuePair<unknown>[]): string;\n}\n\nexport interface WgslResolvable {\n readonly label?: string | undefined;\n\n resolve(ctx: ResolutionCtx): string;\n}\n\nexport function isResolvable(value: unknown): value is WgslResolvable {\n return (\n !!value &&\n (typeof value === 'object' || typeof value === 'function') &&\n 'resolve' in value\n );\n}\n\nexport function isWgsl(value: unknown): value is Wgsl {\n return (\n typeof value === 'number' ||\n typeof value === 'boolean' ||\n typeof value === 'string' ||\n isResolvable(value)\n );\n}\n\nexport interface WgslSlot<T> {\n readonly __brand: 'WgslSlot';\n\n readonly defaultValue: T | undefined;\n\n readonly label?: string | undefined;\n\n $name(label: string): WgslSlot<T>;\n\n /**\n * Used to determine if code generated using either value `a` or `b` in place\n * of the slot will be equivalent. Defaults to `Object.is`.\n */\n areEqual(a: T, b: T): boolean;\n}\n\nexport function isSlot<T>(value: unknown | WgslSlot<T>): value is WgslSlot<T> {\n return (value as WgslSlot<T>).__brand === 'WgslSlot';\n}\n\n/**\n * Represents a value that is available at resolution time.\n */\nexport type Eventual<T> = T | WgslSlot<T>;\n\nexport type EventualGetter = <T>(value: Eventual<T>) => T;\n\nexport type InlineResolve = (get: EventualGetter) => Wgsl;\n\nexport interface WgslResolvableSlot<T extends Wgsl>\n extends WgslResolvable,\n WgslSlot<T> {\n $name(label: string): WgslResolvableSlot<T>;\n}\n\nexport type SlotValuePair<T> = [WgslSlot<T>, T];\n\nexport interface WgslAllocatable<TData extends AnyWgslData = AnyWgslData> {\n /**\n * The data type this allocatable was constructed with.\n * It informs the size and format of data in both JS and\n * binary.\n */\n readonly dataType: TData;\n readonly initial?: Parsed<TData> | WgslPlum<Parsed<TData>> | undefined;\n readonly flags: GPUBufferUsageFlags;\n}\n\nexport interface WgslBindable<\n TData extends AnyWgslData = AnyWgslData,\n TUsage extends BufferUsage = BufferUsage,\n> extends WgslResolvable {\n readonly allocatable: WgslAllocatable<TData>;\n readonly usage: TUsage;\n}\n\nexport type BufferUsage = 'uniform' | 'readonly_storage' | 'mutable_storage';\n\nexport interface WgslData<TInner> extends ISchema<TInner>, WgslResolvable {\n readonly byteAlignment: number;\n readonly size: number;\n}\n\nexport type AnyWgslData = WgslData<unknown>;\n\nexport interface WgslPointer<\n TScope extends 'function',\n TInner extends AnyWgslData,\n> {\n readonly scope: TScope;\n readonly pointsTo: TInner;\n}\n\n/**\n * A virtual representation of a WGSL value.\n */\nexport type WgslValue<TDataType> = {\n readonly __dataType: TDataType;\n};\n\nexport type AnyWgslPointer = WgslPointer<'function', AnyWgslData>;\n\nexport type WgslFnArgument = AnyWgslPointer | AnyWgslData;\n\nexport function isPointer(\n value: AnyWgslPointer | AnyWgslData,\n): value is AnyWgslPointer {\n return 'pointsTo' in value;\n}\n","import {\n type Eventual,\n type InlineResolve,\n type ResolutionCtx,\n type SlotValuePair,\n type Wgsl,\n type WgslResolvable,\n type WgslSlot,\n isResolvable,\n} from './types';\n\n// ----------\n// Public API\n// ----------\n\nexport interface WgslCode extends WgslResolvable {\n $name(label?: string | undefined): WgslCode;\n\n with<T>(slot: WgslSlot<T>, value: Eventual<T>): BoundWgslCode;\n}\n\nexport type BoundWgslCode = Omit<WgslCode, '$name'>;\n\nexport function code(\n strings: TemplateStringsArray,\n ...params: (Wgsl | Wgsl[] | InlineResolve)[]\n): WgslCode {\n const segments: (Wgsl | InlineResolve)[] = strings.flatMap((string, idx) => {\n const param = params[idx];\n if (param === undefined) {\n return [string];\n }\n\n return Array.isArray(param) ? [string, ...param] : [string, param];\n });\n\n return new WgslCodeImpl(segments);\n}\n\n// --------------\n// Implementation\n// --------------\n\nclass WgslCodeImpl implements WgslCode {\n private _label: string | undefined;\n\n constructor(public readonly segments: (Wgsl | InlineResolve)[]) {}\n\n get label() {\n return this._label;\n }\n\n $name(label?: string | undefined) {\n this._label = label;\n return this;\n }\n\n resolve(ctx: ResolutionCtx) {\n let code = '';\n\n for (const s of this.segments) {\n if (typeof s === 'function') {\n const result = s((eventual) => ctx.unwrap(eventual));\n code += ctx.resolve(result);\n } else if (isResolvable(s)) {\n code += ctx.resolve(s);\n } else {\n code += String(s);\n }\n }\n\n return code;\n }\n\n with<TValue>(slot: WgslSlot<TValue>, value: Eventual<TValue>): BoundWgslCode {\n return new BoundWgslCodeImpl(this, [slot, value]);\n }\n\n toString(): string {\n return `code:${this._label ?? '<unnamed>'}`;\n }\n}\n\nclass BoundWgslCodeImpl<T> implements BoundWgslCode {\n constructor(\n private readonly _innerFn: BoundWgslCode,\n private readonly _slotValuePair: SlotValuePair<T>,\n ) {}\n\n get label() {\n return this._innerFn.label;\n }\n\n with<TValue>(slot: WgslSlot<TValue>, value: Eventual<TValue>): BoundWgslCode {\n return new BoundWgslCodeImpl(this, [slot, value]);\n }\n\n resolve(ctx: ResolutionCtx): string {\n return ctx.resolve(this._innerFn, [this._slotValuePair]);\n }\n\n toString(): string {\n const [slot, value] = this._slotValuePair;\n return `code:${this.label ?? '<unnamed>'}[${slot.label ?? '<unnamed>'}=${value}]`;\n }\n}\n","import type { ResolutionCtx, WgslResolvable } from './types';\n\n/**\n * Helpful when creating new Resolvable types. For internal use.\n */\nexport class WgslIdentifier implements WgslResolvable {\n label?: string | undefined;\n\n $name(label: string | undefined) {\n this.label = label;\n return this;\n }\n\n resolve(ctx: ResolutionCtx): string {\n return ctx.nameFor(this);\n }\n\n toString(): string {\n return `id:${this.label ?? '<unnamed>'}`;\n }\n}\n"]}
package/chunk-K6N2A55L.js DELETED
@@ -1,4 +0,0 @@
1
- var r=class e extends Error{constructor(o,s){let t=s.map(c=>`- ${c}`);t.length>20&&(t=[...t.slice(0,11),"...",...t.slice(-10)]);super(`Resolution of the following tree failed:
2
- ${t.join(`
3
- `)}`);this.cause=o;this.trace=s;Object.setPrototypeOf(this,e.prototype)}appendToTrace(o){let s=[o,...this.trace];return new e(this.cause,s)}},l=class e extends Error{constructor(o){super(`Missing value for '${o}'`);this.slot=o;Object.setPrototypeOf(this,e.prototype)}},n=class e extends Error{constructor(){super("Recursive types are not supported in WGSL"),Object.setPrototypeOf(this,e.prototype)}};export{r as a,l as b,n as c};
4
- //# sourceMappingURL=chunk-K6N2A55L.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/errors.ts"],"sourcesContent":["import type { WgslResolvable, WgslSlot } from './types';\n\n/**\n * An error that happens during resolution of WGSL code.\n * Contains a trace of all ancestor resolvables in\n * which this error originated.\n *\n * @category Errors\n */\nexport class ResolutionError extends Error {\n constructor(\n public readonly cause: unknown,\n public readonly trace: WgslResolvable[],\n ) {\n let entries = trace.map((ancestor) => `- ${ancestor}`);\n\n // Showing only the root and leaf nodes.\n if (entries.length > 20) {\n entries = [...entries.slice(0, 11), '...', ...entries.slice(-10)];\n }\n\n super(`Resolution of the following tree failed: \\n${entries.join('\\n')}`);\n\n // Set the prototype explicitly.\n Object.setPrototypeOf(this, ResolutionError.prototype);\n }\n\n appendToTrace(ancestor: WgslResolvable): ResolutionError {\n const newTrace = [ancestor, ...this.trace];\n\n return new ResolutionError(this.cause, newTrace);\n }\n}\n\n/**\n * @category Errors\n */\nexport class MissingSlotValueError extends Error {\n constructor(public readonly slot: WgslSlot<unknown>) {\n super(`Missing value for '${slot}'`);\n\n // Set the prototype explicitly.\n Object.setPrototypeOf(this, MissingSlotValueError.prototype);\n }\n}\n\n/**\n * @category Errors\n */\nexport class RecursiveDataTypeError extends Error {\n constructor() {\n super('Recursive types are not supported in WGSL');\n\n // Set the prototype explicitly.\n Object.setPrototypeOf(this, RecursiveDataTypeError.prototype);\n }\n}\n"],"mappings":"AASO,IAAMA,EAAN,MAAMC,UAAwB,KAAM,CACzC,YACkBC,EACAC,EAChB,CACA,IAAIC,EAAUD,EAAM,IAAKE,GAAa,KAAKA,CAAQ,EAAE,EAGjDD,EAAQ,OAAS,KACnBA,EAAU,CAAC,GAAGA,EAAQ,MAAM,EAAG,EAAE,EAAG,MAAO,GAAGA,EAAQ,MAAM,GAAG,CAAC,GAGlE,MAAM;AAAA,EAA8CA,EAAQ,KAAK;AAAA,CAAI,CAAC,EAAE,EAVxD,WAAAF,EACA,WAAAC,EAYhB,OAAO,eAAe,KAAMF,EAAgB,SAAS,CACvD,CAEA,cAAcI,EAA2C,CACvD,IAAMC,EAAW,CAACD,EAAU,GAAG,KAAK,KAAK,EAEzC,OAAO,IAAIJ,EAAgB,KAAK,MAAOK,CAAQ,CACjD,CACF,EAKaC,EAAN,MAAMC,UAA8B,KAAM,CAC/C,YAA4BC,EAAyB,CACnD,MAAM,sBAAsBA,CAAI,GAAG,EADT,UAAAA,EAI1B,OAAO,eAAe,KAAMD,EAAsB,SAAS,CAC7D,CACF,EAKaE,EAAN,MAAMC,UAA+B,KAAM,CAChD,aAAc,CACZ,MAAM,2CAA2C,EAGjD,OAAO,eAAe,KAAMA,EAAuB,SAAS,CAC9D,CACF","names":["ResolutionError","_ResolutionError","cause","trace","entries","ancestor","newTrace","MissingSlotValueError","_MissingSlotValueError","slot","RecursiveDataTypeError","_RecursiveDataTypeError"]}