typegpu 0.1.2 → 0.2.0-alpha.0

Sign up to get free protection for your applications and to get access to all the features.
package/index.d.cts CHANGED
@@ -1,40 +1,296 @@
1
+ import { T as TgpuRenderResource, a as TgpuNamable, b as TgpuBuffer, A as AnyTgpuData, O as OmitProps, c as TgpuBufferUsage, U as Uniform, S as Storage, d as TgpuBufferUniform, e as TgpuShaderStage, f as TgpuBufferReadonly, g as TgpuBufferMutable, h as U32, I as I32, F as F32, V as Vec4u, i as Vec4i, j as Vec4f, k as TgpuPlum, E as ExtractPlumValue, l as Unsubscribe, m as TgpuCode, B as BoundTgpuCode, n as Block, v as vecBase, o as vec3f, p as vec3i, q as vec3u } from './types-qmW7FFqN.cjs';
2
+ export { r as TgpuData, w as Vertex, s as isUsableAsStorage, t as isUsableAsUniform, u as isUsableAsVertex } from './types-qmW7FFqN.cjs';
1
3
  import { Parsed } from 'typed-binary';
2
- import { A as AnyTgpuData, T as TgpuAllocatable, a as TgpuNamable, b as TgpuPlum, v as vecBase, c as vec3f, d as vec3i, e as vec3u } from './vector-D5Vx-xhb.cjs';
3
- export { f as TgpuData } from './vector-D5Vx-xhb.cjs';
4
4
 
5
- interface Unmanaged {
5
+ declare const TgpuSettableTrait: unique symbol;
6
+ interface TgpuSettable {
7
+ readonly [TgpuSettableTrait]: true;
8
+ }
9
+
10
+ type PlumListener<T> = (newValue: T) => unknown;
11
+
12
+ interface TgpuSampler extends TgpuRenderResource, TgpuNamable {
13
+ readonly descriptor: GPUSamplerDescriptor;
14
+ }
15
+
16
+ interface Unwrapper {
6
17
  readonly device: GPUDevice;
7
- readonly buffer: GPUBuffer;
18
+ unwrap(resource: TgpuBuffer<AnyTgpuData>): GPUBuffer;
19
+ unwrap(resource: TgpuBindGroupLayout): GPUBindGroupLayout;
20
+ unwrap(resource: TgpuBindGroup): GPUBindGroup;
8
21
  }
9
- interface Uniform {
10
- usableAsUniform: true;
22
+
23
+ type TgpuLayoutEntryBase = {
24
+ /**
25
+ * Limits this resource's visibility to specific shader stages.
26
+ *
27
+ * By default, each resource is visible to all shader stage types, but
28
+ * depending on the underlying implementation, this may have performance implications.
29
+ *
30
+ * @default ['compute'] for mutable resources
31
+ * @default ['compute','vertex','fragment'] for everything else
32
+ */
33
+ visibility?: TgpuShaderStage[];
34
+ };
35
+ type TgpuLayoutUniform = TgpuLayoutEntryBase & {
36
+ uniform: AnyTgpuData | ((arrayLength: number) => AnyTgpuData);
37
+ };
38
+ type TgpuLayoutStorage = TgpuLayoutEntryBase & {
39
+ storage: AnyTgpuData | ((arrayLength: number) => AnyTgpuData);
40
+ /** @default 'readonly' */
41
+ access?: 'mutable' | 'readonly';
42
+ };
43
+ type TgpuLayoutSampler = TgpuLayoutEntryBase & {
44
+ sampler: GPUSamplerBindingType;
45
+ };
46
+ type TgpuLayoutTexture<TSampleType extends GPUTextureSampleType = GPUTextureSampleType> = TgpuLayoutEntryBase & {
47
+ /**
48
+ * - 'float' - f32
49
+ * - 'unfilterable-float' - f32, cannot be used with filtering samplers
50
+ * - 'depth' - f32
51
+ * - 'sint' - i32
52
+ * - 'uint' - u32
53
+ */
54
+ texture: TSampleType;
55
+ /**
56
+ * @default '2d'
57
+ */
58
+ viewDimension?: GPUTextureViewDimension;
59
+ /**
60
+ * @default false
61
+ */
62
+ multisampled?: boolean;
63
+ };
64
+ type TgpuLayoutStorageTexture<TFormat extends GPUTextureFormat = GPUTextureFormat> = TgpuLayoutEntryBase & {
65
+ storageTexture: TFormat;
66
+ /** @default 'writeonly' */
67
+ access?: 'readonly' | 'writeonly' | 'mutable';
68
+ /** @default '2d' */
69
+ viewDimension?: GPUTextureViewDimension;
70
+ };
71
+ type TgpuLayoutExternalTexture = TgpuLayoutEntryBase & {
72
+ externalTexture: Record<string, never>;
73
+ };
74
+ type TgpuLayoutEntry = TgpuLayoutUniform | TgpuLayoutStorage | TgpuLayoutSampler | TgpuLayoutTexture | TgpuLayoutStorageTexture | TgpuLayoutExternalTexture;
75
+ type UnwrapRuntimeConstructorInner<T extends AnyTgpuData | ((_: number) => AnyTgpuData)> = T extends AnyTgpuData ? T : T extends (_: number) => infer Return ? Return : never;
76
+ type UnwrapRuntimeConstructor<T extends AnyTgpuData | ((_: number) => AnyTgpuData)> = T extends unknown ? UnwrapRuntimeConstructorInner<T> : never;
77
+ interface TgpuBindGroupLayout<Entries extends Record<string, TgpuLayoutEntry | null> = Record<string, TgpuLayoutEntry | null>> extends TgpuNamable {
78
+ readonly resourceType: 'bind-group-layout';
79
+ readonly label: string | undefined;
80
+ readonly entries: Entries;
81
+ populate(entries: {
82
+ [K in keyof OmitProps<Entries, null>]: LayoutEntryToInput<Entries[K]>;
83
+ }): TgpuBindGroup<Entries>;
84
+ /**
85
+ * Creates a raw WebGPU resource based on the typed descriptor.
86
+ * NOTE: This creates a new resource every time, better to use `root.unwrap(...)` instead.
87
+ * @param unwrapper Used to unwrap any resources that this resource depends on.
88
+ */
89
+ unwrap(unwrapper: Unwrapper): GPUBindGroupLayout;
11
90
  }
12
- declare const Uniform: Uniform;
13
- interface Storage {
14
- usableAsStorage: true;
91
+ type StorageUsageForEntry<T extends TgpuLayoutStorage> = T extends {
92
+ access?: infer Access;
93
+ } ? 'mutable' | 'readonly' extends Access ? TgpuBufferReadonly<UnwrapRuntimeConstructor<T['storage']>> | TgpuBufferMutable<UnwrapRuntimeConstructor<T['storage']>> : 'readonly' extends Access ? TgpuBufferReadonly<UnwrapRuntimeConstructor<T['storage']>> : 'mutable' extends Access ? TgpuBufferMutable<UnwrapRuntimeConstructor<T['storage']>> : TgpuBufferReadonly<UnwrapRuntimeConstructor<T['storage']>> | TgpuBufferMutable<UnwrapRuntimeConstructor<T['storage']>> : TgpuBufferReadonly<UnwrapRuntimeConstructor<T['storage']>>;
94
+ type LayoutEntryToInput<T extends TgpuLayoutEntry | null> = T extends TgpuLayoutUniform ? TgpuBufferUsage<UnwrapRuntimeConstructor<T['uniform']>, 'uniform'> | (TgpuBuffer<UnwrapRuntimeConstructor<T['uniform']>> & Uniform) | GPUBuffer : T extends TgpuLayoutStorage ? StorageUsageForEntry<T> | (TgpuBuffer<UnwrapRuntimeConstructor<T['storage']>> & Storage) | GPUBuffer : T extends TgpuLayoutSampler ? GPUSampler : T extends TgpuLayoutTexture ? GPUTextureView : T extends TgpuLayoutStorageTexture ? GPUTextureView : T extends TgpuLayoutExternalTexture ? GPUExternalTexture : never;
95
+ type BindLayoutEntry<T extends TgpuLayoutEntry | null> = T extends TgpuLayoutUniform ? TgpuBufferUniform<UnwrapRuntimeConstructor<T['uniform']>> : T extends TgpuLayoutStorage ? StorageUsageForEntry<T> : T extends TgpuLayoutSampler ? TgpuSampler : never;
96
+ type TgpuBindGroup<Entries extends Record<string, TgpuLayoutEntry | null> = Record<string, TgpuLayoutEntry | null>> = {
97
+ readonly resourceType: 'bind-group';
98
+ readonly layout: TgpuBindGroupLayout<Entries>;
99
+ unwrap(unwrapper: Unwrapper): GPUBindGroup;
100
+ };
101
+ declare function bindGroupLayout<Entries extends Record<string, TgpuLayoutEntry | null>>(entries: Entries): TgpuBindGroupLayout<Entries>;
102
+
103
+ type StorageTextureAccess = 'read' | 'write' | 'read_write';
104
+ type TextureScalarFormat = U32 | I32 | F32;
105
+ type TexelFormat = Vec4u | Vec4i | Vec4f;
106
+
107
+ type Optional<T> = {
108
+ [P in keyof T]?: T[P] | undefined;
109
+ };
110
+ interface TgpuAnyTextureView extends TgpuRenderResource {
111
+ readonly descriptor: GPUTextureViewDescriptor;
112
+ readonly texture: TgpuAnyTexture;
113
+ readonly dataType: TextureScalarFormat | TexelFormat;
114
+ readonly access: StorageTextureAccess | undefined;
15
115
  }
16
- declare const Storage: Storage;
17
- interface Vertex {
18
- usableAsVertex: true;
116
+ interface TgpuAnyTexture {
117
+ readonly descriptor: Omit<GPUTextureDescriptor, 'usage'>;
118
+ get flags(): GPUTextureUsageFlags;
19
119
  }
20
- declare const Vertex: Vertex;
21
- type UnionToIntersection<U> = (U extends any ? (x: U) => void : never) extends (x: infer I) => void ? I : never;
22
- interface TgpuBuffer<TData extends AnyTgpuData> extends TgpuAllocatable<TData>, TgpuNamable {
23
- readonly destroyed: boolean;
24
- readonly label: string | undefined;
25
- $usage<T extends (Uniform | Storage | Vertex)[]>(...usages: T): this & UnionToIntersection<T[number]>;
26
- $addFlags(flags: GPUBufferUsageFlags): this;
27
- $device(device: GPUDevice): this & Unmanaged;
120
+ interface TgpuTextureExternal extends TgpuRenderResource, TgpuNamable {
121
+ readonly descriptor: Optional<GPUExternalTextureDescriptor>;
122
+ get source(): HTMLVideoElement | VideoFrame | undefined;
123
+ }
124
+
125
+ type SetPlumAction<T> = T | ((prev: T) => T);
126
+ interface TgpuRoot extends Unwrapper {
127
+ /**
128
+ * The GPU device associated with this root.
129
+ */
130
+ readonly device: GPUDevice;
131
+ /**
132
+ * @param typeSchema The type of data that this buffer will hold.
133
+ * @param initial The initial value of the buffer. (optional)
134
+ */
135
+ createBuffer<TData extends AnyTgpuData>(typeSchema: TData, initial?: Parsed<TData> | TgpuPlum<Parsed<TData>> | undefined): TgpuBuffer<TData>;
136
+ /**
137
+ * @param typeSchema The type of data that this buffer will hold.
138
+ * @param gpuBuffer A vanilla WebGPU buffer.
139
+ */
140
+ createBuffer<TData extends AnyTgpuData>(typeSchema: TData, gpuBuffer: GPUBuffer): TgpuBuffer<TData>;
141
+ unwrap(resource: TgpuBuffer<AnyTgpuData>): GPUBuffer;
142
+ unwrap(resource: TgpuBindGroupLayout): GPUBindGroupLayout;
143
+ unwrap(resource: TgpuBindGroup): GPUBindGroup;
28
144
  destroy(): void;
29
145
  }
146
+ interface ExperimentalTgpuRoot extends TgpuRoot {
147
+ readonly jitTranspiler?: JitTranspiler | undefined;
148
+ /**
149
+ * The current command encoder. This property will
150
+ * hold the same value until `flush()` is called.
151
+ */
152
+ readonly commandEncoder: GPUCommandEncoder;
153
+ readPlum<TPlum extends TgpuPlum>(plum: TPlum): ExtractPlumValue<TPlum>;
154
+ setPlum<TPlum extends TgpuPlum & TgpuSettable>(plum: TPlum, value: SetPlumAction<ExtractPlumValue<TPlum>>): void;
155
+ onPlumChange<TValue>(plum: TgpuPlum<TValue>, listener: PlumListener<TValue>): Unsubscribe;
156
+ setSource(texture: TgpuTextureExternal, source: HTMLVideoElement | VideoFrame): void;
157
+ isDirty(texture: TgpuTextureExternal): boolean;
158
+ markClean(texture: TgpuTextureExternal): void;
159
+ textureFor(view: TgpuAnyTexture | TgpuAnyTextureView): GPUTexture;
160
+ viewFor(view: TgpuAnyTextureView): GPUTextureView;
161
+ externalTextureFor(texture: TgpuTextureExternal): GPUExternalTexture;
162
+ samplerFor(sampler: TgpuSampler): GPUSampler;
163
+ /**
164
+ * Causes all commands enqueued by pipelines to be
165
+ * submitted to the GPU.
166
+ */
167
+ flush(): void;
168
+ makeRenderPipeline(options: RenderPipelineOptions): RenderPipelineExecutor;
169
+ makeComputePipeline(options: ComputePipelineOptions): ComputePipelineExecutor;
170
+ }
171
+ interface RenderPipelineOptions {
172
+ vertex: {
173
+ code: TgpuCode | BoundTgpuCode;
174
+ output: {
175
+ [K in symbol]: string;
176
+ } & {
177
+ [K in string]: AnyTgpuData;
178
+ };
179
+ };
180
+ fragment: {
181
+ code: TgpuCode | BoundTgpuCode;
182
+ target: Iterable<GPUColorTargetState | null>;
183
+ };
184
+ primitive: GPUPrimitiveState;
185
+ externalLayouts?: GPUBindGroupLayout[];
186
+ label?: string;
187
+ }
188
+ interface ComputePipelineOptions {
189
+ code: TgpuCode | BoundTgpuCode;
190
+ workgroupSize?: readonly [number, number?, number?];
191
+ externalLayouts?: GPUBindGroupLayout[];
192
+ label?: string;
193
+ }
194
+ type RenderPipelineExecutorOptions = GPURenderPassDescriptor & {
195
+ vertexCount: number;
196
+ instanceCount?: number;
197
+ firstVertex?: number;
198
+ firstInstance?: number;
199
+ externalBindGroups?: GPUBindGroup[];
200
+ };
201
+ interface RenderPipelineExecutor {
202
+ execute(options: RenderPipelineExecutorOptions): void;
203
+ }
204
+ type ComputePipelineExecutorOptions = {
205
+ workgroups?: readonly [number, number?, number?];
206
+ externalBindGroups?: GPUBindGroup[];
207
+ };
208
+ interface ComputePipelineExecutor {
209
+ execute(options?: ComputePipelineExecutorOptions): void;
210
+ }
211
+
212
+ /**
213
+ * Information extracted from transpiling a JS function.
214
+ */
215
+ type TranspilationResult = {
216
+ argNames: string[];
217
+ body: Block;
218
+ /**
219
+ * All identifiers found in the function code that are not declared in the
220
+ * function itself, or in the block that is accessing that identifier.
221
+ */
222
+ externalNames: string[];
223
+ };
224
+
225
+ /**
226
+ * Used to transpile JS resources into SMoL on demand.
227
+ */
228
+ interface JitTranspiler {
229
+ transpileFn(rawJs: string): TranspilationResult;
230
+ }
231
+
232
+ /**
233
+ * Options passed into {@link init}.
234
+ */
235
+ type InitOptions = {
236
+ adapter?: GPURequestAdapterOptions | undefined;
237
+ device?: GPUDeviceDescriptor | undefined;
238
+ unstable_jitTranspiler?: JitTranspiler | undefined;
239
+ };
240
+ /**
241
+ * Options passed into {@link initFromDevice}.
242
+ */
243
+ type InitFromDeviceOptions = {
244
+ device: GPUDevice;
245
+ unstable_jitTranspiler?: JitTranspiler | undefined;
246
+ };
247
+ /**
248
+ * Requests a new GPU device and creates a root around it.
249
+ * If a specific device should be used instead, use @see initFromDevice.
250
+ *
251
+ * @example
252
+ * When given no options, the function will ask the browser for a suitable GPU device.
253
+ * ```ts
254
+ * const root = await tgpu.init();
255
+ * ```
256
+ *
257
+ * @example
258
+ * If there are specific options that should be used when requesting a device, you can pass those in.
259
+ * ```ts
260
+ * const adapterOptions: GPURequestAdapterOptions = ...;
261
+ * const deviceDescriptor: GPUDeviceDescriptor = ...;
262
+ * const root = await tgpu.init({ adapter: adapterOptions, device: deviceDescriptor });
263
+ * ```
264
+ */
265
+ declare function init(options?: InitOptions): Promise<ExperimentalTgpuRoot>;
266
+ /**
267
+ * Creates a root from the given device, instead of requesting it like @see init.
268
+ *
269
+ * @example
270
+ * ```ts
271
+ * const device: GPUDevice = ...;
272
+ * const root = tgpu.initFromDevice({ device });
273
+ * ```
274
+ */
275
+ declare function initFromDevice(options: InitFromDeviceOptions): ExperimentalTgpuRoot;
276
+
277
+ /**
278
+ * @deprecated Use the `root.createBuffer` API instead, accessible through `await tgpu.init()`
279
+ *
280
+ * @param typeSchema The type of data that this buffer will hold.
281
+ * @param initial The initial value of the buffer. (optional)
282
+ */
30
283
  declare function createBuffer<TData extends AnyTgpuData>(typeSchema: TData, initial?: Parsed<TData> | TgpuPlum<Parsed<TData>> | undefined): TgpuBuffer<TData>;
284
+ /**
285
+ * @deprecated Use the `root.createBuffer` API instead, accessible through `await tgpu.init()`
286
+ *
287
+ * @param typeSchema The type of data that this buffer will hold.
288
+ * @param gpuBuffer A vanilla WebGPU buffer.
289
+ */
31
290
  declare function createBuffer<TData extends AnyTgpuData>(typeSchema: TData, gpuBuffer: GPUBuffer): TgpuBuffer<TData>;
32
- declare function isUsableAsUniform<T extends TgpuBuffer<AnyTgpuData>>(buffer: T): buffer is T & Uniform;
33
- declare function isUsableAsStorage<T extends TgpuBuffer<AnyTgpuData>>(buffer: T): buffer is T & Storage;
34
- declare function isUsableAsVertex<T extends TgpuBuffer<AnyTgpuData>>(buffer: T): buffer is T & Vertex;
35
291
 
36
- declare function write<TData extends AnyTgpuData>(buffer: TgpuBuffer<TData> & Unmanaged, data: Parsed<TData>): void;
37
- declare function read<TData extends AnyTgpuData>(buffer: TgpuBuffer<TData> & Unmanaged): Promise<Parsed<TData>>;
292
+ declare function write<TData extends AnyTgpuData>(buffer: TgpuBuffer<TData>, data: Parsed<TData>): void;
293
+ declare function read<TData extends AnyTgpuData>(buffer: TgpuBuffer<TData>): Promise<Parsed<TData>>;
38
294
 
39
295
  /**
40
296
  * @category Errors
@@ -44,16 +300,16 @@ declare class RecursiveDataTypeError extends Error {
44
300
  }
45
301
 
46
302
  declare const std: {
47
- add: <T extends vecBase>(lhs: T, rhs: T) => T;
48
- sub: <T extends vecBase>(lhs: T, rhs: T) => T;
303
+ add<T extends vecBase>(lhs: T, rhs: T): T;
304
+ sub<T extends vecBase>(lhs: T, rhs: T): T;
49
305
  mul: <T extends vecBase>(s: number, v: T) => T;
50
- dot: <T extends vecBase>(lhs: T, rhs: T) => number;
306
+ dot<T extends vecBase>(lhs: T, rhs: T): number;
51
307
  normalize: <T extends vecBase>(v: T) => T;
52
- cross: <T extends vec3f | vec3i | vec3u>(a: T, b: T) => T;
53
- fract: (a: number) => number;
54
- length: <T extends vecBase>(vector: T) => number;
55
- sin: (x: number) => number;
56
- cos: (x: number) => number;
308
+ cross<T extends vec3f | vec3i | vec3u>(a: T, b: T): T;
309
+ fract(a: number): number;
310
+ length<T extends vecBase>(vector: T): number;
311
+ sin(radians: number): number;
312
+ cos(radians: number): number;
57
313
  };
58
314
 
59
315
  /**
@@ -61,12 +317,18 @@ declare const std: {
61
317
  */
62
318
 
63
319
  declare const tgpu: {
64
- Uniform: Uniform;
65
- Storage: Storage;
66
- Vertex: Vertex;
320
+ /** @deprecated Use `'uniform'` string literal instead. */
321
+ Uniform: "uniform";
322
+ /** @deprecated Use `'storage'` string literal instead. */
323
+ Storage: "storage";
324
+ /** @deprecated Use `'vertex'` string literal instead. */
325
+ Vertex: "vertex";
326
+ bindGroupLayout: typeof bindGroupLayout;
327
+ init: typeof init;
328
+ initFromDevice: typeof initFromDevice;
67
329
  createBuffer: typeof createBuffer;
68
330
  read: typeof read;
69
331
  write: typeof write;
70
332
  };
71
333
 
72
- export { AnyTgpuData, RecursiveDataTypeError, type TgpuBuffer, type Unmanaged, tgpu as default, isUsableAsStorage, isUsableAsUniform, isUsableAsVertex, std, tgpu };
334
+ export { AnyTgpuData, type BindLayoutEntry, type LayoutEntryToInput, RecursiveDataTypeError, Storage, type TgpuBindGroup, type TgpuBindGroupLayout, TgpuBuffer, type TgpuLayoutEntry, type TgpuLayoutSampler, type TgpuLayoutUniform, Uniform, tgpu as default, std, tgpu };