typegpu 0.12.8 → 0.12.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/core/function/dualImpl.js +1 -0
- package/core/function/tgpuFn.d.ts +1 -0
- package/core/function/tgpuFn.js +10 -0
- package/core/pipeline/computePipeline.js +5 -2
- package/core/pipeline/renderPipeline.js +5 -2
- package/core/pipeline/webgpuLimitations.js +9 -2
- package/core/root/init.d.ts +3 -3
- package/core/root/init.js +12 -13
- package/core/root/rootTypes.d.ts +4 -1
- package/core/slot/accessor.js +3 -3
- package/core/texture/texture.d.ts +2 -1
- package/core/texture/texture.js +23 -24
- package/data/compiledIO.js +1 -1
- package/data/dataIO.js +16 -16
- package/data/index.d.ts +4 -3
- package/data/index.js +3 -2
- package/data/schemasEqual.d.ts +37 -0
- package/data/schemasEqual.js +131 -0
- package/data/struct.js +2 -0
- package/data/wgslTypes.d.ts +1 -1
- package/package.json +2 -2
- package/serial/restore.d.ts +3 -2
- package/serial/restore.js +2 -1
- package/shared/meta.js +1 -1
- package/shared/symbols.js +1 -1
- package/std/comptime.d.ts +67 -0
- package/std/comptime.js +90 -0
- package/std/index.d.ts +2 -0
- package/std/index.js +2 -0
- package/std/numeric.d.ts +1 -1
- package/std/numeric.js +12 -9
- package/std/typeOf.d.ts +31 -0
- package/std/typeOf.js +56 -0
- package/tgpuLogger.d.ts +3 -0
- package/tgpuLogger.js +3 -0
- package/tgsl/consoleLog/serializers.d.ts +1 -8
- package/tgsl/consoleLog/serializers.js +53 -37
- package/tgsl/shellless.js +3 -20
- package/tgsl/wgslGenerator.js +1 -1
- package/unwrapper.d.ts +2 -2
- package/data/deepEqual.d.ts +0 -21
- package/data/deepEqual.js +0 -93
package/data/struct.js
CHANGED
|
@@ -16,12 +16,14 @@ import { schemaCallWrapper } from "./schemaCallWrapper.js";
|
|
|
16
16
|
* @param props Record with `string` keys and `TgpuData` values,
|
|
17
17
|
* each entry describing one struct member.
|
|
18
18
|
*/
|
|
19
|
+
/*#__NO_SIDE_EFFECTS__*/
|
|
19
20
|
export function struct(props) {
|
|
20
21
|
if (Object.keys(props).length === 0) {
|
|
21
22
|
throw new Error('Cannot create struct schema with no properties.');
|
|
22
23
|
}
|
|
23
24
|
return INTERNAL_createStruct(props, false);
|
|
24
25
|
}
|
|
26
|
+
/*#__NO_SIDE_EFFECTS__*/
|
|
25
27
|
export function abstruct(props) {
|
|
26
28
|
return INTERNAL_createStruct(props, true);
|
|
27
29
|
}
|
package/data/wgslTypes.d.ts
CHANGED
|
@@ -23,7 +23,7 @@ export interface NumberArrayView {
|
|
|
23
23
|
/**
|
|
24
24
|
* Maps a scalar, vector, or matrix element schema to the corresponding TypedArray type.
|
|
25
25
|
*/
|
|
26
|
-
export type TypedArrayFor<T> = T extends F32 | Vec2f | Vec3f | Vec4f | Mat2x2f | Mat3x3f | Mat4x4f ? Float32Array : T extends F16 | Vec2h | Vec3h | Vec4h ? Float16Array : T extends I32 | Vec2i | Vec3i | Vec4i | Atomic<I32> ? Int32Array : T extends U32 | Vec2u | Vec3u | Vec4u | Atomic<U32> ? Uint32Array : T extends U16 ? Uint16Array : T extends Decorated<infer TBase> ? TypedArrayFor<TBase> : never;
|
|
26
|
+
export type TypedArrayFor<T> = T extends F32 | Vec2f | Vec3f | Vec4f | Mat2x2f | Mat3x3f | Mat4x4f ? Float32Array : T extends F16 | Vec2h | Vec3h | Vec4h ? Float16Array : T extends I32 | Vec2i | Vec3i | Vec4i | Atomic<I32> ? Int32Array : T extends U32 | Vec2u | Vec3u | Vec4u | Atomic<U32> ? Uint32Array : T extends U16 ? Uint16Array : T extends Decorated<infer TBase> ? TypedArrayFor<TBase> : T extends BaseData ? string extends T['type'] ? Float32Array | Float16Array | Int32Array | Uint32Array | Uint16Array : never : never;
|
|
27
27
|
/**
|
|
28
28
|
* Vector infix notation.
|
|
29
29
|
*
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "typegpu",
|
|
3
|
-
"version": "0.12.
|
|
3
|
+
"version": "0.12.9",
|
|
4
4
|
"description": "A thin layer between JS and WebGPU/WGSL that improves development experience and allows for faster iteration.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"compute",
|
|
@@ -49,7 +49,7 @@
|
|
|
49
49
|
},
|
|
50
50
|
"dependencies": {
|
|
51
51
|
"tsover-runtime": "^0.0.7",
|
|
52
|
-
"typed-binary": "^4.3.
|
|
52
|
+
"typed-binary": "^4.3.4",
|
|
53
53
|
"tinyest": "~0.3.4"
|
|
54
54
|
},
|
|
55
55
|
"engines": {
|
package/serial/restore.d.ts
CHANGED
|
@@ -9,7 +9,7 @@ import type { TgpuQuerySetSoul } from '../core/querySet/querySet.ts';
|
|
|
9
9
|
import type { TgpuGuardedComputePipelineSoul, TgpuRootSoul } from '../core/root/rootTypes.ts';
|
|
10
10
|
import type { TgpuSamplerSoul } from '../core/sampler/sampler.ts';
|
|
11
11
|
import type { TgpuAccessorSoul, TgpuMutableAccessor, TgpuSlotSoul } from '../core/slot/slotTypes.ts';
|
|
12
|
-
import { type TgpuTextureSoul } from '../core/texture/texture.ts';
|
|
12
|
+
import { INTERNAL_restoreTextureView, type TgpuTextureSoul, type TgpuTextureViewSoul } from '../core/texture/texture.ts';
|
|
13
13
|
import { type TgpuVertexLayoutSoul } from '../core/vertexLayout/vertexLayout.ts';
|
|
14
14
|
import type { AnyData } from '../data/dataTypes.ts';
|
|
15
15
|
import { type AnyWgslData } from '../data/wgslTypes.ts';
|
|
@@ -18,7 +18,7 @@ import { type TgpuBindGroupLayoutSoul, type TgpuBindGroupSoul } from '../tgpuBin
|
|
|
18
18
|
import type { TgpuSoul } from '../shared/soul.ts';
|
|
19
19
|
import type { RestoreContext } from './types.ts';
|
|
20
20
|
/** Every soul that can cross a device boundary, discriminated by `type` */
|
|
21
|
-
export type TgpuResourceSoul = TgpuBufferSoul | TgpuBufferBindingSoul | TgpuTextureSoul | TgpuSamplerSoul | TgpuQuerySetSoul | TgpuBindGroupLayoutSoul | TgpuBindGroupSoul | TgpuVertexLayoutSoul | TgpuConstSoul | TgpuImmediateVarSoul | TgpuSlotSoul | TgpuAccessorSoul | TgpuComputePipelineSoul | TgpuRenderPipelineSoul | TgpuGuardedComputePipelineSoul | TgpuRootSoul;
|
|
21
|
+
export type TgpuResourceSoul = TgpuBufferSoul | TgpuBufferBindingSoul | TgpuTextureSoul | TgpuTextureViewSoul | TgpuSamplerSoul | TgpuQuerySetSoul | TgpuBindGroupLayoutSoul | TgpuBindGroupSoul | TgpuVertexLayoutSoul | TgpuConstSoul | TgpuImmediateVarSoul | TgpuSlotSoul | TgpuAccessorSoul | TgpuComputePipelineSoul | TgpuRenderPipelineSoul | TgpuGuardedComputePipelineSoul | TgpuRootSoul;
|
|
22
22
|
type Restorer<TSoul extends TgpuSoul> = (soul: TSoul, ctx: RestoreContext) => unknown;
|
|
23
23
|
declare function restoreBufferBinding(soul: TgpuBufferBindingSoul): TgpuBufferBindingImpl<"uniform" | "mutable" | "readonly", BaseData>;
|
|
24
24
|
/**
|
|
@@ -31,6 +31,7 @@ export declare const soulRestorers: {
|
|
|
31
31
|
mutable: typeof restoreBufferBinding;
|
|
32
32
|
readonly: typeof restoreBufferBinding;
|
|
33
33
|
texture: (soul: TgpuTextureSoul, ctx: RestoreContext) => import("../indexNamedExports.ts").TgpuTexture<TextureProps>;
|
|
34
|
+
'texture-view': typeof INTERNAL_restoreTextureView;
|
|
34
35
|
sampler: (soul: TgpuSamplerSoul, ctx: RestoreContext) => import("../indexNamedExports.ts").TgpuFixedSampler;
|
|
35
36
|
'sampler-comparison': (soul: TgpuSamplerSoul, ctx: RestoreContext) => import("../indexNamedExports.ts").TgpuFixedComparisonSampler;
|
|
36
37
|
'query-set': (soul: TgpuQuerySetSoul, ctx: RestoreContext) => import("../indexNamedExports.ts").TgpuQuerySet<GPUQueryType>;
|
package/serial/restore.js
CHANGED
|
@@ -7,7 +7,7 @@ import { INTERNAL_restoreRenderPipeline, } from "../core/pipeline/renderPipeline
|
|
|
7
7
|
import { INTERNAL_restoreGuardedComputePipeline, INTERNAL_restoreRoot } from "../core/root/init.js";
|
|
8
8
|
import { accessor, mutableAccessor } from "../core/slot/accessor.js";
|
|
9
9
|
import { slot } from "../core/slot/slot.js";
|
|
10
|
-
import { INTERNAL_createTexture } from "../core/texture/texture.js";
|
|
10
|
+
import { INTERNAL_createTexture, INTERNAL_restoreTextureView, } from "../core/texture/texture.js";
|
|
11
11
|
import { vertexLayout } from "../core/vertexLayout/vertexLayout.js";
|
|
12
12
|
import { arrayOf } from "../data/array.js";
|
|
13
13
|
import { disarrayOf } from "../data/disarray.js";
|
|
@@ -40,6 +40,7 @@ export const soulRestorers = {
|
|
|
40
40
|
}
|
|
41
41
|
return texture;
|
|
42
42
|
},
|
|
43
|
+
'texture-view': INTERNAL_restoreTextureView,
|
|
43
44
|
sampler: (soul, ctx) => ctx.getRoot(soul.device).createSampler(soul.props),
|
|
44
45
|
'sampler-comparison': (soul, ctx) => ctx.getRoot(soul.device).createComparisonSampler(soul.props),
|
|
45
46
|
'query-set': (soul, ctx) => ctx.getRoot(soul.device).createQuerySet(soul.queryType, soul.count, soul.raw),
|
package/shared/meta.js
CHANGED
package/shared/symbols.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const version = "0.12.
|
|
1
|
+
const version = "0.12.9";
|
|
2
2
|
export const $internal = Symbol(`typegpu:${version}:$internal`);
|
|
3
3
|
/** A plain record of all definitional state of a resource, surviving transfer between runtimes */
|
|
4
4
|
export const $soul = Symbol(`typegpu:${version}:$soul`);
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { type DualFn } from '../types.ts';
|
|
2
|
+
/**
|
|
3
|
+
* Returns `true` if the value passed into it is statically known at comptime (during shader
|
|
4
|
+
* generation), and `false` if the value is determined by shader execution.
|
|
5
|
+
* During normal JavaScript execution every value is available right away, so it always
|
|
6
|
+
* returns `true`.
|
|
7
|
+
*
|
|
8
|
+
* Note that shader constants are NOT considered statically known at comptime, therefore
|
|
9
|
+
* `isKnownAtComptime()` returns `false` for them.
|
|
10
|
+
*
|
|
11
|
+
* A good example of where this is useful is opting into optimizations that are
|
|
12
|
+
* only valid when a value is statically known, like unrolling a loop whose iteration count
|
|
13
|
+
* depends on the size of an array:
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* const layout = tgpu.bindGroupLayout({
|
|
18
|
+
* // Swapping this for `d.arrayOf(d.vec2f)` (a runtime-sized array)
|
|
19
|
+
* // makes the loop below stay a loop.
|
|
20
|
+
* boids: { storage: d.arrayOf(d.vec2f, 3) },
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* function centroid() {
|
|
24
|
+
* 'use gpu';
|
|
25
|
+
* let sum = d.vec2f();
|
|
26
|
+
*
|
|
27
|
+
* for (
|
|
28
|
+
* const boid of std.isKnownAtComptime(layout.$.boids.length)
|
|
29
|
+
* ? tgpu.unroll(layout.$.boids)
|
|
30
|
+
* : layout.$.boids
|
|
31
|
+
* ) {
|
|
32
|
+
* sum += boid;
|
|
33
|
+
* }
|
|
34
|
+
*
|
|
35
|
+
* return sum / d.f32(layout.$.boids.length);
|
|
36
|
+
* }
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* Generates:
|
|
40
|
+
*
|
|
41
|
+
* ```wgsl
|
|
42
|
+
* @group(0) @binding(0) var<storage, read> boids: array<vec2f, 3>;
|
|
43
|
+
*
|
|
44
|
+
* fn centroid() -> vec2f {
|
|
45
|
+
* var sum = vec2f();
|
|
46
|
+
* // unrolled iteration #0
|
|
47
|
+
* sum += boids[0u];
|
|
48
|
+
* // unrolled iteration #1
|
|
49
|
+
* sum += boids[1u];
|
|
50
|
+
* // unrolled iteration #2
|
|
51
|
+
* sum += boids[2u];
|
|
52
|
+
* // ---
|
|
53
|
+
* return (sum / 3f);
|
|
54
|
+
* }
|
|
55
|
+
* ```
|
|
56
|
+
*
|
|
57
|
+
* @note
|
|
58
|
+
* Only the *value* of the argument is inspected, the expression itself is never emitted into
|
|
59
|
+
* the generated shader. Passing an expression with possible side effects (e.g. a call to a
|
|
60
|
+
* `tgpu.fn`) throws during shader generation, because those side effects would run in JS but
|
|
61
|
+
* silently not happen on the GPU.
|
|
62
|
+
*
|
|
63
|
+
* Such an expression is determined by shader execution, so it is never known at comptime and
|
|
64
|
+
* the check would always yield `false`. Remove the check, and if the side effect itself is
|
|
65
|
+
* needed, run it as a separate statement.
|
|
66
|
+
*/
|
|
67
|
+
export declare const isKnownAtComptime: DualFn<(value: unknown) => boolean>;
|
package/std/comptime.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { bool } from "../data/numeric.js";
|
|
2
|
+
import { snip } from "../data/snippet.js";
|
|
3
|
+
import { WgslTypeError } from "../errors.js";
|
|
4
|
+
import { setName } from "../shared/meta.js";
|
|
5
|
+
import { $gpuCallable } from "../shared/symbols.js";
|
|
6
|
+
import { asComptime } from "../types.js";
|
|
7
|
+
/**
|
|
8
|
+
* Returns `true` if the value passed into it is statically known at comptime (during shader
|
|
9
|
+
* generation), and `false` if the value is determined by shader execution.
|
|
10
|
+
* During normal JavaScript execution every value is available right away, so it always
|
|
11
|
+
* returns `true`.
|
|
12
|
+
*
|
|
13
|
+
* Note that shader constants are NOT considered statically known at comptime, therefore
|
|
14
|
+
* `isKnownAtComptime()` returns `false` for them.
|
|
15
|
+
*
|
|
16
|
+
* A good example of where this is useful is opting into optimizations that are
|
|
17
|
+
* only valid when a value is statically known, like unrolling a loop whose iteration count
|
|
18
|
+
* depends on the size of an array:
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* const layout = tgpu.bindGroupLayout({
|
|
23
|
+
* // Swapping this for `d.arrayOf(d.vec2f)` (a runtime-sized array)
|
|
24
|
+
* // makes the loop below stay a loop.
|
|
25
|
+
* boids: { storage: d.arrayOf(d.vec2f, 3) },
|
|
26
|
+
* });
|
|
27
|
+
*
|
|
28
|
+
* function centroid() {
|
|
29
|
+
* 'use gpu';
|
|
30
|
+
* let sum = d.vec2f();
|
|
31
|
+
*
|
|
32
|
+
* for (
|
|
33
|
+
* const boid of std.isKnownAtComptime(layout.$.boids.length)
|
|
34
|
+
* ? tgpu.unroll(layout.$.boids)
|
|
35
|
+
* : layout.$.boids
|
|
36
|
+
* ) {
|
|
37
|
+
* sum += boid;
|
|
38
|
+
* }
|
|
39
|
+
*
|
|
40
|
+
* return sum / d.f32(layout.$.boids.length);
|
|
41
|
+
* }
|
|
42
|
+
* ```
|
|
43
|
+
*
|
|
44
|
+
* Generates:
|
|
45
|
+
*
|
|
46
|
+
* ```wgsl
|
|
47
|
+
* @group(0) @binding(0) var<storage, read> boids: array<vec2f, 3>;
|
|
48
|
+
*
|
|
49
|
+
* fn centroid() -> vec2f {
|
|
50
|
+
* var sum = vec2f();
|
|
51
|
+
* // unrolled iteration #0
|
|
52
|
+
* sum += boids[0u];
|
|
53
|
+
* // unrolled iteration #1
|
|
54
|
+
* sum += boids[1u];
|
|
55
|
+
* // unrolled iteration #2
|
|
56
|
+
* sum += boids[2u];
|
|
57
|
+
* // ---
|
|
58
|
+
* return (sum / 3f);
|
|
59
|
+
* }
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
62
|
+
* @note
|
|
63
|
+
* Only the *value* of the argument is inspected, the expression itself is never emitted into
|
|
64
|
+
* the generated shader. Passing an expression with possible side effects (e.g. a call to a
|
|
65
|
+
* `tgpu.fn`) throws during shader generation, because those side effects would run in JS but
|
|
66
|
+
* silently not happen on the GPU.
|
|
67
|
+
*
|
|
68
|
+
* Such an expression is determined by shader execution, so it is never known at comptime and
|
|
69
|
+
* the check would always yield `false`. Remove the check, and if the side effect itself is
|
|
70
|
+
* needed, run it as a separate statement.
|
|
71
|
+
*/
|
|
72
|
+
export const isKnownAtComptime = /* @__PURE__ */ (() => {
|
|
73
|
+
const impl = ((_value) => true);
|
|
74
|
+
impl.toString = () => 'isKnownAtComptime';
|
|
75
|
+
setName(impl, 'isKnownAtComptime');
|
|
76
|
+
impl[$gpuCallable] = {
|
|
77
|
+
call(_ctx, [value]) {
|
|
78
|
+
if (!value) {
|
|
79
|
+
throw new WgslTypeError('`std.isKnownAtComptime` was called without any arguments');
|
|
80
|
+
}
|
|
81
|
+
if (value.possibleSideEffects) {
|
|
82
|
+
throw new WgslTypeError('`std.isKnownAtComptime` received an argument with possible side effects. The expression is never emitted into the shader, so its side effects would silently not happen, and a side-effectful expression is never known at comptime anyway, so the result would always be `false`. Remove the check, and run the side effect as a separate statement if it is needed.');
|
|
83
|
+
}
|
|
84
|
+
return snip(asComptime(value) !== undefined, bool,
|
|
85
|
+
/* origin */ 'constant',
|
|
86
|
+
/* possibleSideEffects */ false);
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
return impl;
|
|
90
|
+
})();
|
package/std/index.d.ts
CHANGED
|
@@ -18,3 +18,5 @@ export { extensionEnabled } from './extensions.ts';
|
|
|
18
18
|
export { bitcastU32toF32, bitcastU32toI32, bitcastF32toU32, bitcast } from './bitcast.ts';
|
|
19
19
|
export { range } from './range.ts';
|
|
20
20
|
export { isBeingTranspiled, getTargetShaderLanguage, getShaderStage } from './environment.ts';
|
|
21
|
+
export { typeOf } from './typeOf.ts';
|
|
22
|
+
export { isKnownAtComptime } from './comptime.ts';
|
package/std/index.js
CHANGED
|
@@ -30,3 +30,5 @@ export { extensionEnabled } from "./extensions.js";
|
|
|
30
30
|
export { bitcastU32toF32, bitcastU32toI32, bitcastF32toU32, bitcast } from "./bitcast.js";
|
|
31
31
|
export { range } from "./range.js";
|
|
32
32
|
export { isBeingTranspiled, getTargetShaderLanguage, getShaderStage } from "./environment.js";
|
|
33
|
+
export { typeOf } from "./typeOf.js";
|
|
34
|
+
export { isKnownAtComptime } from "./comptime.js";
|
package/std/numeric.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type AnyFloat32VecInstance, type AnyFloatVecInstance, type AnyIntegerVecInstance, type AnyMatInstance, type AnyNumericVecInstance, type AnySignedVecInstance, type BaseData, type v2f, type v2h, type v2i, type v3f, type v3h, type v3i, type v4f, type v4h, type v4i, type Vec2f } from '../data/wgslTypes.ts';
|
|
2
2
|
import type { Infer } from '../shared/repr.ts';
|
|
3
3
|
type NumVec = AnyNumericVecInstance;
|
|
4
|
-
export declare
|
|
4
|
+
export declare function unifyRestrictedSignature(restrict: BaseData[]): (...args: BaseData[]) => {
|
|
5
5
|
argTypes: BaseData[];
|
|
6
6
|
returnType: BaseData;
|
|
7
7
|
};
|
package/std/numeric.js
CHANGED
|
@@ -26,16 +26,19 @@ const variadicUnifySignature = (...args) => {
|
|
|
26
26
|
returnType: uargs[0],
|
|
27
27
|
};
|
|
28
28
|
};
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
29
|
+
/*#__NO_SIDE_EFFECTS__*/
|
|
30
|
+
export function unifyRestrictedSignature(restrict) {
|
|
31
|
+
return (...args) => {
|
|
32
|
+
const uargs = unify(args, restrict);
|
|
33
|
+
if (!uargs) {
|
|
34
|
+
throw new SignatureNotSupportedError(args, restrict);
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
argTypes: uargs,
|
|
38
|
+
returnType: uargs[0],
|
|
39
|
+
};
|
|
37
40
|
};
|
|
38
|
-
}
|
|
41
|
+
}
|
|
39
42
|
function variadicReduce(fn) {
|
|
40
43
|
return (fst, ...rest) => {
|
|
41
44
|
let acc = fst;
|
package/std/typeOf.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { AnyData } from '../data/dataTypes.ts';
|
|
2
|
+
import type { GPUCallable } from '../types.ts';
|
|
3
|
+
interface TypeOf extends GPUCallable {
|
|
4
|
+
(arg: unknown): AnyData | undefined;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Returns the type of an expression, the one it will have during shader execution. That information can
|
|
8
|
+
* be used to introspect or specialize the shader. It will return `undefined` if the expression cannot
|
|
9
|
+
* be represented in shader code, or if the type is unknown at the point where the query is made.
|
|
10
|
+
*
|
|
11
|
+
* The returned value is only known at shader-generation time and has no WGSL representation, so it
|
|
12
|
+
* cannot be stored in a variable (e.g. `const t = std.typeOf(a)` throws). Consume it immediately,
|
|
13
|
+
* e.g. by comparing it (`std.typeOf(a) === d.f32`) or passing it to a comptime function.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* function double(value: number) {
|
|
17
|
+
* 'use gpu';
|
|
18
|
+
* if (std.typeOf(value) === d.f32) {
|
|
19
|
+
* return value * 2;
|
|
20
|
+
* }
|
|
21
|
+
* return value << 1;
|
|
22
|
+
* }
|
|
23
|
+
*
|
|
24
|
+
* function main() {
|
|
25
|
+
* 'use gpu';
|
|
26
|
+
* double(1.5); // generates double(f32), which multiplies by 2
|
|
27
|
+
* double(1); // generates double(i32), which shifts left by 1
|
|
28
|
+
* }
|
|
29
|
+
*/
|
|
30
|
+
export declare const typeOf: TypeOf;
|
|
31
|
+
export {};
|
package/std/typeOf.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { isWgslData } from "../data/wgslTypes.js";
|
|
2
|
+
import { setName } from "../shared/meta.js";
|
|
3
|
+
import { $gpuCallable, $internal } from "../shared/symbols.js";
|
|
4
|
+
import { coerceToSnippet } from "../tgsl/generationHelpers.js";
|
|
5
|
+
/**
|
|
6
|
+
* Returns the type of an expression, the one it will have during shader execution. That information can
|
|
7
|
+
* be used to introspect or specialize the shader. It will return `undefined` if the expression cannot
|
|
8
|
+
* be represented in shader code, or if the type is unknown at the point where the query is made.
|
|
9
|
+
*
|
|
10
|
+
* The returned value is only known at shader-generation time and has no WGSL representation, so it
|
|
11
|
+
* cannot be stored in a variable (e.g. `const t = std.typeOf(a)` throws). Consume it immediately,
|
|
12
|
+
* e.g. by comparing it (`std.typeOf(a) === d.f32`) or passing it to a comptime function.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* function double(value: number) {
|
|
16
|
+
* 'use gpu';
|
|
17
|
+
* if (std.typeOf(value) === d.f32) {
|
|
18
|
+
* return value * 2;
|
|
19
|
+
* }
|
|
20
|
+
* return value << 1;
|
|
21
|
+
* }
|
|
22
|
+
*
|
|
23
|
+
* function main() {
|
|
24
|
+
* 'use gpu';
|
|
25
|
+
* double(1.5); // generates double(f32), which multiplies by 2
|
|
26
|
+
* double(1); // generates double(i32), which shifts left by 1
|
|
27
|
+
* }
|
|
28
|
+
*/
|
|
29
|
+
export const typeOf = (() => {
|
|
30
|
+
const impl = (() => {
|
|
31
|
+
// Deducing the WGSL type of a value at comptime is near-impossible with
|
|
32
|
+
// the current comptime type flowing. We can lift this restriction once it
|
|
33
|
+
// becomes more capable.
|
|
34
|
+
throw new Error('std.typeOf() cannot be called in JS, it can only be used in the GPU context. You can use std.isBeingTranspiled() to branch off and provide a fallback.');
|
|
35
|
+
});
|
|
36
|
+
setName(impl, 'typeOf');
|
|
37
|
+
impl.toString = () => 'typeOf';
|
|
38
|
+
impl[$gpuCallable] = {
|
|
39
|
+
call(_ctx, args) {
|
|
40
|
+
const [arg] = args;
|
|
41
|
+
if (!arg || args.length > 1) {
|
|
42
|
+
throw new Error(`std.typeOf() expects exactly one argument, got ${args.length}`);
|
|
43
|
+
}
|
|
44
|
+
const type = arg.dataType;
|
|
45
|
+
// Only public data types are exposed. Unknown types and internal types
|
|
46
|
+
// (e.g. AutoStruct, EntryInputRouter) must not leak to the user.
|
|
47
|
+
if (!isWgslData(type)) {
|
|
48
|
+
return coerceToSnippet(undefined);
|
|
49
|
+
}
|
|
50
|
+
return coerceToSnippet(type);
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
// Mark as internal
|
|
54
|
+
Object.defineProperty(impl, $internal, { value: true });
|
|
55
|
+
return impl;
|
|
56
|
+
})();
|
package/tgpuLogger.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
declare const warningTypes: readonly ["deprecated", "suspicious", "fallback", "precision-loss", "implicit-conversion", "webgpu-feature-missing", "webgpu-limits-exceeded", "log-limit-exceeded", "external-omitted", "uniform-schema-misaligned"];
|
|
2
2
|
type WarningType = (typeof warningTypes)[number];
|
|
3
3
|
interface Logger {
|
|
4
|
+
/** Lets callers skip computing what a warning needs when it wouldn't be shown anyway. */
|
|
5
|
+
isEnabled(type: WarningType): boolean;
|
|
4
6
|
warn(type: WarningType, ...args: unknown[]): void;
|
|
5
7
|
warnOnce(type: WarningType, key: object, tag: string, ...args: unknown[]): void;
|
|
6
8
|
}
|
|
@@ -26,6 +28,7 @@ export declare class TgpuLogger implements Logger, Warn {
|
|
|
26
28
|
constructor(prod: boolean);
|
|
27
29
|
disable(type: WarningType): void;
|
|
28
30
|
reset(): void;
|
|
31
|
+
isEnabled(type: WarningType): boolean;
|
|
29
32
|
warn(type: WarningType, ...args: unknown[]): void;
|
|
30
33
|
warnOnce(type: WarningType, key: object, tag: string, ...args: unknown[]): void;
|
|
31
34
|
}
|
package/tgpuLogger.js
CHANGED
|
@@ -35,6 +35,9 @@ export class TgpuLogger {
|
|
|
35
35
|
reset() {
|
|
36
36
|
this.#enabledWarnings = new Set(this.#initialEnabledWarnings);
|
|
37
37
|
}
|
|
38
|
+
isEnabled(type) {
|
|
39
|
+
return this.#enabledWarnings.has(type);
|
|
40
|
+
}
|
|
38
41
|
warn(type, ...args) {
|
|
39
42
|
if (this.#enabledWarnings.has(type)) {
|
|
40
43
|
console.warn(`⚠️ [${type}] `, ...args);
|
|
@@ -1,13 +1,7 @@
|
|
|
1
1
|
import type { TgpuMutable } from '../../core/buffer/bufferBinding.ts';
|
|
2
2
|
import { type TgpuFn } from '../../core/function/tgpuFn.ts';
|
|
3
|
-
import { type AnyWgslData, type Atomic, type U32, type
|
|
3
|
+
import { type AnyWgslData, type Atomic, type U32, type WgslArray } from '../../data/wgslTypes.ts';
|
|
4
4
|
import type { LogGeneratorOptions, SerializedLogCallData } from './types.ts';
|
|
5
|
-
type SerializerMap = {
|
|
6
|
-
[K in AnyWgslData['type']]?: TgpuFn<(args_0: Extract<AnyWgslData, {
|
|
7
|
-
type: K;
|
|
8
|
-
}>) => Void>;
|
|
9
|
-
};
|
|
10
|
-
export declare const serializerMap: SerializerMap;
|
|
11
5
|
/**
|
|
12
6
|
* Creates a TGPU function that serializes data to the log buffer.
|
|
13
7
|
*
|
|
@@ -18,4 +12,3 @@ export declare const serializerMap: SerializerMap;
|
|
|
18
12
|
* @param logOptions - Configuration options
|
|
19
13
|
*/
|
|
20
14
|
export declare function createLoggingFunction(id: number, dataTypes: AnyWgslData[], dataBuffer: TgpuMutable<WgslArray<SerializedLogCallData>>, indexBuffer: TgpuMutable<Atomic<U32>>, logOptions: Required<LogGeneratorOptions>): TgpuFn;
|
|
21
|
-
export {};
|
|
@@ -7,111 +7,116 @@ import { sizeOf } from "../../data/sizeOf.js";
|
|
|
7
7
|
import { vec2b, vec2f, vec2h, vec2i, vec2u, vec3b, vec3f, vec3h, vec3i, vec3u, vec4b, vec4f, vec4h, vec4i, vec4u, } from "../../data/vector.js";
|
|
8
8
|
import { isWgslArray, isWgslStruct, } from "../../data/wgslTypes.js";
|
|
9
9
|
import { getName } from "../../shared/meta.js";
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
/**
|
|
11
|
+
* The serializers are only needed when shaders log something, so they're created on first use
|
|
12
|
+
* instead of on import (creating them is not free, and most apps never log from shaders).
|
|
13
|
+
*/
|
|
14
|
+
function createSerializerState() {
|
|
15
|
+
const dataBlockIndex = privateVar(u32, 0).$name('dataBlockIndex');
|
|
16
|
+
const dataByteIndex = privateVar(u32, 0).$name('dataByteIndex');
|
|
17
|
+
const dataBufferSlot = slot().$name('dataBuffer');
|
|
18
|
+
const nextByteIndex = fn([], u32) `() {
|
|
14
19
|
let i = dataByteIndex;
|
|
15
20
|
dataByteIndex = dataByteIndex + 1u;
|
|
16
21
|
return i;
|
|
17
22
|
}`
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
const nextU32 = 'dataBuffer[dataBlockIndex].serializedData[nextByteIndex()]';
|
|
21
|
-
|
|
22
|
-
|
|
23
|
+
.$uses({ dataByteIndex })
|
|
24
|
+
.$name('nextByteIndex');
|
|
25
|
+
const nextU32 = 'dataBuffer[dataBlockIndex].serializedData[nextByteIndex()]';
|
|
26
|
+
const serializerMap = {
|
|
27
|
+
f32: fn([f32]) `(n) {
|
|
23
28
|
${nextU32} = bitcast<u32>(n);
|
|
24
29
|
}`,
|
|
25
|
-
|
|
30
|
+
f16: fn([f16]) `(n) {
|
|
26
31
|
${nextU32} = pack2x16float(vec2f(f32(n), 0.0));
|
|
27
32
|
}`,
|
|
28
|
-
|
|
33
|
+
i32: fn([i32]) `(n) {
|
|
29
34
|
${nextU32} = bitcast<u32>(n);
|
|
30
35
|
}`,
|
|
31
|
-
|
|
36
|
+
u32: fn([u32]) `(n) {
|
|
32
37
|
${nextU32} = n;
|
|
33
38
|
}`,
|
|
34
|
-
|
|
39
|
+
bool: fn([bool]) `(b) {
|
|
35
40
|
${nextU32} = u32(b);
|
|
36
41
|
}`,
|
|
37
|
-
|
|
42
|
+
vec2f: fn([vec2f]) `(v) {
|
|
38
43
|
${nextU32} = bitcast<u32>(v.x);
|
|
39
44
|
${nextU32} = bitcast<u32>(v.y);
|
|
40
45
|
}`,
|
|
41
|
-
|
|
46
|
+
vec3f: fn([vec3f]) `(v) {
|
|
42
47
|
${nextU32} = bitcast<u32>(v.x);
|
|
43
48
|
${nextU32} = bitcast<u32>(v.y);
|
|
44
49
|
${nextU32} = bitcast<u32>(v.z);
|
|
45
50
|
}`,
|
|
46
|
-
|
|
51
|
+
vec4f: fn([vec4f]) `(v) {
|
|
47
52
|
${nextU32} = bitcast<u32>(v.x);
|
|
48
53
|
${nextU32} = bitcast<u32>(v.y);
|
|
49
54
|
${nextU32} = bitcast<u32>(v.z);
|
|
50
55
|
${nextU32} = bitcast<u32>(v.w);
|
|
51
56
|
}`,
|
|
52
|
-
|
|
57
|
+
vec2h: fn([vec2h]) `(v) {
|
|
53
58
|
${nextU32} = pack2x16float(vec2f(f32(v.x), f32(v.y)));
|
|
54
59
|
}`,
|
|
55
|
-
|
|
60
|
+
vec3h: fn([vec3h]) `(v) {
|
|
56
61
|
${nextU32} = pack2x16float(vec2f(f32(v.x), f32(v.y)));
|
|
57
62
|
${nextU32} = pack2x16float(vec2f(f32(v.z), 0.0));
|
|
58
63
|
}`,
|
|
59
|
-
|
|
64
|
+
vec4h: fn([vec4h]) `(v) {
|
|
60
65
|
${nextU32} = pack2x16float(vec2f(f32(v.x), f32(v.y)));
|
|
61
66
|
${nextU32} = pack2x16float(vec2f(f32(v.z), f32(v.w)));
|
|
62
67
|
}`,
|
|
63
|
-
|
|
68
|
+
vec2i: fn([vec2i]) `(v) {
|
|
64
69
|
${nextU32} = bitcast<u32>(v.x);
|
|
65
70
|
${nextU32} = bitcast<u32>(v.y);
|
|
66
71
|
}`,
|
|
67
|
-
|
|
72
|
+
vec3i: fn([vec3i]) `(v) {
|
|
68
73
|
${nextU32} = bitcast<u32>(v.x);
|
|
69
74
|
${nextU32} = bitcast<u32>(v.y);
|
|
70
75
|
${nextU32} = bitcast<u32>(v.z);
|
|
71
76
|
}`,
|
|
72
|
-
|
|
77
|
+
vec4i: fn([vec4i]) `(v) {
|
|
73
78
|
${nextU32} = bitcast<u32>(v.x);
|
|
74
79
|
${nextU32} = bitcast<u32>(v.y);
|
|
75
80
|
${nextU32} = bitcast<u32>(v.z);
|
|
76
81
|
${nextU32} = bitcast<u32>(v.w);
|
|
77
82
|
}`,
|
|
78
|
-
|
|
83
|
+
vec2u: fn([vec2u]) `(v) {
|
|
79
84
|
${nextU32} = v.x;
|
|
80
85
|
${nextU32} = v.y;
|
|
81
86
|
}`,
|
|
82
|
-
|
|
87
|
+
vec3u: fn([vec3u]) `(v) {
|
|
83
88
|
${nextU32} = v.x;
|
|
84
89
|
${nextU32} = v.y;
|
|
85
90
|
${nextU32} = v.z;
|
|
86
91
|
}`,
|
|
87
|
-
|
|
92
|
+
vec4u: fn([vec4u]) `(v) {
|
|
88
93
|
${nextU32} = v.x;
|
|
89
94
|
${nextU32} = v.y;
|
|
90
95
|
${nextU32} = v.z;
|
|
91
96
|
${nextU32} = v.w;
|
|
92
97
|
}`,
|
|
93
|
-
|
|
98
|
+
'vec2<bool>': fn([vec2b]) `(v) {
|
|
94
99
|
${nextU32} = u32(v.x);
|
|
95
100
|
${nextU32} = u32(v.y);
|
|
96
101
|
}`,
|
|
97
|
-
|
|
102
|
+
'vec3<bool>': fn([vec3b]) `(v) {
|
|
98
103
|
${nextU32} = u32(v.x);
|
|
99
104
|
${nextU32} = u32(v.y);
|
|
100
105
|
${nextU32} = u32(v.z);
|
|
101
106
|
}`,
|
|
102
|
-
|
|
107
|
+
'vec4<bool>': fn([vec4b]) `(v) {
|
|
103
108
|
${nextU32} = u32(v.x);
|
|
104
109
|
${nextU32} = u32(v.y);
|
|
105
110
|
${nextU32} = u32(v.z);
|
|
106
111
|
${nextU32} = u32(v.w);
|
|
107
112
|
}`,
|
|
108
|
-
|
|
113
|
+
mat2x2f: fn([mat2x2f]) `(m) {
|
|
109
114
|
${nextU32} = bitcast<u32>(m[0][0]);
|
|
110
115
|
${nextU32} = bitcast<u32>(m[0][1]);
|
|
111
116
|
${nextU32} = bitcast<u32>(m[1][0]);
|
|
112
117
|
${nextU32} = bitcast<u32>(m[1][1]);
|
|
113
118
|
}`,
|
|
114
|
-
|
|
119
|
+
mat3x3f: fn([mat3x3f]) `(m) {
|
|
115
120
|
${nextU32} = bitcast<u32>(m[0][0]);
|
|
116
121
|
${nextU32} = bitcast<u32>(m[0][1]);
|
|
117
122
|
${nextU32} = bitcast<u32>(m[0][2]);
|
|
@@ -125,7 +130,7 @@ export const serializerMap = {
|
|
|
125
130
|
${nextU32} = bitcast<u32>(m[2][2]);
|
|
126
131
|
${nextU32} = 0u;
|
|
127
132
|
}`,
|
|
128
|
-
|
|
133
|
+
mat4x4f: fn([mat4x4f]) `(m) {
|
|
129
134
|
${nextU32} = bitcast<u32>(m[0][0]);
|
|
130
135
|
${nextU32} = bitcast<u32>(m[0][1]);
|
|
131
136
|
${nextU32} = bitcast<u32>(m[0][2]);
|
|
@@ -143,12 +148,21 @@ export const serializerMap = {
|
|
|
143
148
|
${nextU32} = bitcast<u32>(m[3][2]);
|
|
144
149
|
${nextU32} = bitcast<u32>(m[3][3]);
|
|
145
150
|
}`,
|
|
146
|
-
};
|
|
147
|
-
// rename the functions and add externals
|
|
148
|
-
for (const [name, serializer] of Object.entries(serializerMap)) {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
151
|
+
};
|
|
152
|
+
// rename the functions and add externals
|
|
153
|
+
for (const [name, serializer] of Object.entries(serializerMap)) {
|
|
154
|
+
serializer
|
|
155
|
+
.$name(`serialize${name[0].toLocaleUpperCase()}${name.slice(1)}`)
|
|
156
|
+
.$uses({ dataBlockIndex, nextByteIndex, dataBuffer: dataBufferSlot });
|
|
157
|
+
}
|
|
158
|
+
return { dataBlockIndex, dataByteIndex, dataBufferSlot, serializerMap };
|
|
159
|
+
}
|
|
160
|
+
let serializerState;
|
|
161
|
+
function getSerializerState() {
|
|
162
|
+
if (!serializerState) {
|
|
163
|
+
serializerState = createSerializerState();
|
|
164
|
+
}
|
|
165
|
+
return serializerState;
|
|
152
166
|
}
|
|
153
167
|
// -------
|
|
154
168
|
// Helpers
|
|
@@ -165,6 +179,7 @@ function generateHeader(argTypes) {
|
|
|
165
179
|
* @param dataBuffer - A buffer to store serialized log call data (a necessary external for the returned function)
|
|
166
180
|
*/
|
|
167
181
|
function getSerializer(dataType, dataBuffer) {
|
|
182
|
+
const { serializerMap, dataBufferSlot } = getSerializerState();
|
|
168
183
|
const maybeSerializer = serializerMap[dataType.type];
|
|
169
184
|
if (maybeSerializer) {
|
|
170
185
|
return maybeSerializer.with(dataBufferSlot, dataBuffer);
|
|
@@ -221,6 +236,7 @@ export function createLoggingFunction(id, dataTypes, dataBuffer, indexBuffer, lo
|
|
|
221
236
|
if (serializedSize > logOptions.logSizeLimit) {
|
|
222
237
|
throw new Error(`Logged data needs to fit in ${logOptions.logSizeLimit} bytes (one of the logs requires ${serializedSize} bytes). Consider increasing the limit by passing appropriate options to tgpu.init().`);
|
|
223
238
|
}
|
|
239
|
+
const { dataBlockIndex, dataByteIndex } = getSerializerState();
|
|
224
240
|
const compoundSerializer = createCompoundSerializer(dataTypes, dataBuffer).$name(`log${id}serializer`);
|
|
225
241
|
const header = generateHeader(dataTypes);
|
|
226
242
|
return fn(dataTypes) `${header} {
|