typegpu 0.12.8 → 0.12.10
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/common/attachAutoResizer.d.ts +33 -0
- package/common/attachAutoResizer.js +83 -0
- package/common/index.d.ts +1 -0
- package/common/index.js +1 -0
- package/common/writeSoA.js +4 -3
- package/core/commandEncoder/commandEncoder.d.ts +1 -1
- package/core/declare/tgpuDeclare.d.ts +10 -7
- package/core/declare/tgpuDeclare.js +25 -10
- package/core/function/comptime.d.ts +2 -0
- package/core/function/comptime.js +29 -24
- package/core/function/createCallableSchema.js +32 -37
- package/core/function/dualImpl.js +45 -44
- package/core/function/tgpuComputeFn.d.ts +0 -3
- package/core/function/tgpuFn.d.ts +1 -0
- package/core/function/tgpuFn.js +14 -4
- package/core/function/tgpuFragmentFn.d.ts +1 -1
- package/core/function/tgpuVertexFn.d.ts +1 -1
- package/core/pipeline/computePipeline.js +5 -2
- package/core/pipeline/drawState.js +1 -1
- package/core/pipeline/renderPipeline.js +5 -2
- package/core/pipeline/webgpuLimitations.js +11 -3
- package/core/resolve/externals.js +4 -0
- package/core/resolve/namespace.d.ts +4 -0
- package/core/resolve/namespace.js +1 -2
- package/core/resolve/stitch.js +13 -8
- package/core/root/init.d.ts +3 -3
- package/core/root/init.js +12 -13
- package/core/root/rootTypes.d.ts +44 -37
- package/core/slot/accessor.js +4 -4
- package/core/texture/texture.d.ts +3 -3
- package/core/texture/texture.js +60 -44
- package/core/texture/textureFormats.d.ts +4 -3
- package/core/texture/textureFormats.js +17 -6
- package/core/texture/textureWrite.d.ts +9 -0
- package/core/texture/textureWrite.js +21 -0
- package/core/unroll/tgpuUnroll.js +2 -4
- package/core/vertexLayout/vertexAttribute.d.ts +1 -4
- package/core/vertexLayout/vertexLayout.js +11 -30
- package/data/alignmentOf.d.ts +1 -1
- package/data/alignmentOf.js +5 -12
- package/data/array.js +34 -2
- package/data/attributes.d.ts +16 -3
- package/data/attributes.js +11 -2
- package/data/compiledIO.js +3 -3
- package/data/dataIO.js +77 -111
- package/data/dataTypes.d.ts +0 -2
- package/data/dataTypes.js +3 -3
- package/data/disarray.js +2 -0
- package/data/index.d.ts +4 -3
- package/data/index.js +3 -2
- package/data/offsetUtils.js +23 -13
- package/data/offsets.js +1 -2
- package/data/partialIO.js +2 -3
- package/data/ref.js +35 -37
- package/data/schemaCallWrapper.js +2 -2
- package/data/schemaMemoryLayout.d.ts +6 -1
- package/data/schemaMemoryLayout.js +23 -14
- package/data/schemasEqual.d.ts +37 -0
- package/data/schemasEqual.js +122 -0
- package/data/sizeOf.d.ts +1 -1
- package/data/sizeOf.js +2 -3
- package/data/snippet.d.ts +1 -1
- package/data/struct.js +27 -1
- package/data/unstruct.js +2 -0
- package/data/vectorImpl.js +45 -21
- package/data/vertexFormatData.js +7 -9
- package/data/wgslTypes.d.ts +28 -13
- package/data/wgslTypes.js +33 -39
- package/extension.d.ts +0 -4
- package/package.json +2 -2
- package/resolutionCtx.d.ts +1 -1
- package/resolutionCtx.js +26 -7
- package/serial/restore.d.ts +4 -3
- package/serial/restore.js +2 -1
- package/shared/meta.js +1 -1
- package/shared/symbols.d.ts +5 -0
- package/shared/symbols.js +6 -1
- package/shared/utilityTypes.d.ts +0 -6
- package/std/bitcast.d.ts +1 -1
- package/std/comptime.d.ts +67 -0
- package/std/comptime.js +88 -0
- package/std/environment.js +2 -4
- package/std/index.d.ts +2 -0
- package/std/index.js +2 -0
- package/std/matrix.js +5 -5
- package/std/numeric.d.ts +2 -1
- package/std/numeric.js +47 -10
- package/std/typeOf.d.ts +31 -0
- package/std/typeOf.js +54 -0
- package/tgpu.d.ts +1 -1
- package/tgpu.js +1 -1
- package/tgpuBindGroupLayout.d.ts +6 -3
- package/tgpuBindGroupLayout.js +15 -2
- package/tgpuLogger.d.ts +3 -0
- package/tgpuLogger.js +3 -0
- package/tgpuUnstableNamespace.d.ts +1 -0
- package/tgpuUnstableNamespace.js +5 -0
- package/tgsl/accessIndex.js +10 -7
- package/tgsl/accessProp.js +5 -57
- package/tgsl/consoleLog/serializers.d.ts +1 -8
- package/tgsl/consoleLog/serializers.js +53 -37
- package/tgsl/consoleLog/types.d.ts +1 -1
- package/tgsl/conversion.js +21 -0
- package/tgsl/forOfUtils.js +1 -1
- package/tgsl/generationHelpers.d.ts +0 -1
- package/tgsl/generationHelpers.js +1 -4
- package/tgsl/shaderGenerator.d.ts +5 -0
- package/tgsl/shellless.js +3 -20
- package/tgsl/swizzle.d.ts +24 -0
- package/tgsl/swizzle.js +112 -0
- package/tgsl/wgslGenerator.d.ts +13 -3
- package/tgsl/wgslGenerator.js +150 -152
- package/types.d.ts +10 -10
- package/types.js +1 -1
- package/unwrapper.d.ts +2 -2
- package/data/deepEqual.d.ts +0 -21
- package/data/deepEqual.js +0 -93
- package/taskQueue.d.ts +0 -6
- package/taskQueue.js +0 -30
|
@@ -82,7 +82,7 @@ function computeMLOfStruct(struct) {
|
|
|
82
82
|
const prevSize = size;
|
|
83
83
|
size = roundUp(size, alignmentOf(property));
|
|
84
84
|
const hasPadding = prevSize !== size;
|
|
85
|
-
const propLayout =
|
|
85
|
+
const propLayout = getMemoryLayout(property);
|
|
86
86
|
size += propLayout.size;
|
|
87
87
|
if (Number.isNaN(size) && property.type !== 'array') {
|
|
88
88
|
throw new Error('Cannot nest unbounded struct within another struct');
|
|
@@ -121,7 +121,7 @@ function computeMLOfUnstruct(data) {
|
|
|
121
121
|
if (hasPadding) {
|
|
122
122
|
isContiguous = false;
|
|
123
123
|
}
|
|
124
|
-
const propLayout =
|
|
124
|
+
const propLayout = getMemoryLayout(property);
|
|
125
125
|
size += propLayout.size;
|
|
126
126
|
if (prefixEnd) {
|
|
127
127
|
continue;
|
|
@@ -145,9 +145,9 @@ function computeMLOfUnstruct(data) {
|
|
|
145
145
|
}
|
|
146
146
|
function computeMLOfWgslArray(data) {
|
|
147
147
|
const elementType = data.elementType;
|
|
148
|
-
const elementMemoryLayout =
|
|
148
|
+
const elementMemoryLayout = getMemoryLayout(elementType);
|
|
149
149
|
const elementSize = elementMemoryLayout.size;
|
|
150
|
-
const stride =
|
|
150
|
+
const stride = strideOf(data);
|
|
151
151
|
const hasPadding = stride > elementSize;
|
|
152
152
|
const isContiguous = !hasPadding && elementMemoryLayout.isContiguous;
|
|
153
153
|
const size = data.elementCount === 0 ? Number.NaN : data.elementCount * stride;
|
|
@@ -162,9 +162,9 @@ function computeMLOfWgslArray(data) {
|
|
|
162
162
|
}
|
|
163
163
|
function computeMLOfDisarray(data) {
|
|
164
164
|
const elementType = data.elementType;
|
|
165
|
-
const elementMemoryLayout =
|
|
165
|
+
const elementMemoryLayout = getMemoryLayout(elementType);
|
|
166
166
|
const elementSize = elementMemoryLayout.size;
|
|
167
|
-
const stride =
|
|
167
|
+
const stride = strideOf(data);
|
|
168
168
|
const hasPadding = stride > elementSize;
|
|
169
169
|
const isContiguous = !hasPadding && elementMemoryLayout.isContiguous;
|
|
170
170
|
const size = data.elementCount * stride;
|
|
@@ -177,6 +177,14 @@ function computeMLOfDisarray(data) {
|
|
|
177
177
|
}
|
|
178
178
|
return { size, isContiguous, longestContiguousPrefix };
|
|
179
179
|
}
|
|
180
|
+
function getMemoryLayout(data) {
|
|
181
|
+
let layout = cachedLayouts.get(data);
|
|
182
|
+
if (layout === undefined) {
|
|
183
|
+
layout = computeMemoryLayout(data);
|
|
184
|
+
cachedLayouts.set(data, layout);
|
|
185
|
+
}
|
|
186
|
+
return layout;
|
|
187
|
+
}
|
|
180
188
|
function computeMemoryLayout(data) {
|
|
181
189
|
const knownSize = knownSizesMap[data.type];
|
|
182
190
|
if (knownSize !== undefined) {
|
|
@@ -200,7 +208,7 @@ function computeMemoryLayout(data) {
|
|
|
200
208
|
}
|
|
201
209
|
if (isDecorated(data) || isLooseDecorated(data)) {
|
|
202
210
|
const size = getCustomSize(data);
|
|
203
|
-
const undecoratedLayout =
|
|
211
|
+
const undecoratedLayout = getMemoryLayout(undecorate(data));
|
|
204
212
|
if (size) {
|
|
205
213
|
const isContiguous = size === undecoratedLayout.size && undecoratedLayout.isContiguous;
|
|
206
214
|
return {
|
|
@@ -209,7 +217,7 @@ function computeMemoryLayout(data) {
|
|
|
209
217
|
longestContiguousPrefix: undecoratedLayout.longestContiguousPrefix,
|
|
210
218
|
};
|
|
211
219
|
}
|
|
212
|
-
return
|
|
220
|
+
return getMemoryLayout(data.inner);
|
|
213
221
|
}
|
|
214
222
|
throw new Error(`Cannot determine memory layout of data: ${data}`);
|
|
215
223
|
}
|
|
@@ -218,11 +226,12 @@ function computeMemoryLayout(data) {
|
|
|
218
226
|
* Instead, this weak map acts as an extended property of those data types.
|
|
219
227
|
*/
|
|
220
228
|
const cachedLayouts = new WeakMap();
|
|
229
|
+
/**
|
|
230
|
+
* Returns the distance (in bytes) between the starts of consecutive elements of an array.
|
|
231
|
+
*/
|
|
232
|
+
export function strideOf(schema) {
|
|
233
|
+
return roundUp(getMemoryLayout(schema.elementType).size, alignmentOf(schema));
|
|
234
|
+
}
|
|
221
235
|
export function getLayoutInfo(schema, key) {
|
|
222
|
-
|
|
223
|
-
if (layout === undefined) {
|
|
224
|
-
layout = computeMemoryLayout(schema);
|
|
225
|
-
cachedLayouts.set(schema, layout);
|
|
226
|
-
}
|
|
227
|
-
return layout[key];
|
|
236
|
+
return getMemoryLayout(schema)[key];
|
|
228
237
|
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { AnyData } from './dataTypes.ts';
|
|
2
|
+
/**
|
|
3
|
+
* Performs a structural comparison of two TypeGPU data schemas. This
|
|
4
|
+
* means two struct definitions are considered equal when they have the
|
|
5
|
+
* same properties. Use `schemasEqualNominally` for a nominal comparison.
|
|
6
|
+
*
|
|
7
|
+
* @param a The first data schema to compare.
|
|
8
|
+
* @param b The second data schema to compare.
|
|
9
|
+
* @returns `true` if the schemas are structurally equal, `false` otherwise.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* import { vec3f, struct, schemasEqualStructurally } from 'typegpu/data';
|
|
13
|
+
*
|
|
14
|
+
* console.log(schemasEqualStructurally(struct({ a: vec3f }), struct({ a: vec3f }))); // true
|
|
15
|
+
* console.log(schemasEqualStructurally(struct({ a: vec3f }), struct({ b: vec3f }))); // false
|
|
16
|
+
*/
|
|
17
|
+
export declare const schemasEqualStructurally: import("../indexNamedExports.ts").TgpuComptime<(a: AnyData, b: AnyData) => boolean>;
|
|
18
|
+
/**
|
|
19
|
+
* Performs a nominal comparison of two TypeGPU data schemas. This
|
|
20
|
+
* means two struct definitions are only considered equal when they
|
|
21
|
+
* have the same object identity. Use `schemasEqualStructurally` for
|
|
22
|
+
* a structural comparison.
|
|
23
|
+
*
|
|
24
|
+
* @param a The first data schema to compare.
|
|
25
|
+
* @param b The second data schema to compare.
|
|
26
|
+
* @returns `true` if the schemas are nominally equal, `false` otherwise.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* import { vec3f, struct, schemasEqualNominally } from 'typegpu/data';
|
|
30
|
+
*
|
|
31
|
+
* const struct1 = struct({ a: vec3f });
|
|
32
|
+
* const struct2 = struct({ a: vec3f });
|
|
33
|
+
*
|
|
34
|
+
* console.log(schemasEqualNominally(d.arrayOf(struct1, 2), d.arrayOf(struct1, 2))); // true
|
|
35
|
+
* console.log(schemasEqualNominally(struct1, struct2)); // false
|
|
36
|
+
*/
|
|
37
|
+
export declare const schemasEqualNominally: import("../indexNamedExports.ts").TgpuComptime<(a: AnyData, b: AnyData) => boolean>;
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { comptime } from "../core/function/comptime.js";
|
|
2
|
+
import { isDisarray, isLooseDecorated, isUnstruct } from "./dataTypes.js";
|
|
3
|
+
import { isWgslStorageTexture, isWgslTexture } from "./texture.js";
|
|
4
|
+
import { isAtomic, isDecorated, isPtr, isWgslArray, isWgslStruct } from "./wgslTypes.js";
|
|
5
|
+
/**
|
|
6
|
+
* Performs a structural comparison of two TypeGPU data schemas. This
|
|
7
|
+
* means two struct definitions are considered equal when they have the
|
|
8
|
+
* same properties. Use `schemasEqualNominally` for a nominal comparison.
|
|
9
|
+
*
|
|
10
|
+
* @param a The first data schema to compare.
|
|
11
|
+
* @param b The second data schema to compare.
|
|
12
|
+
* @returns `true` if the schemas are structurally equal, `false` otherwise.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* import { vec3f, struct, schemasEqualStructurally } from 'typegpu/data';
|
|
16
|
+
*
|
|
17
|
+
* console.log(schemasEqualStructurally(struct({ a: vec3f }), struct({ a: vec3f }))); // true
|
|
18
|
+
* console.log(schemasEqualStructurally(struct({ a: vec3f }), struct({ b: vec3f }))); // false
|
|
19
|
+
*/
|
|
20
|
+
export const schemasEqualStructurally = comptime((a, b) => schemasEqual(a, b, true)).$name('schemasEqualStructurally');
|
|
21
|
+
/**
|
|
22
|
+
* Performs a nominal comparison of two TypeGPU data schemas. This
|
|
23
|
+
* means two struct definitions are only considered equal when they
|
|
24
|
+
* have the same object identity. Use `schemasEqualStructurally` for
|
|
25
|
+
* a structural comparison.
|
|
26
|
+
*
|
|
27
|
+
* @param a The first data schema to compare.
|
|
28
|
+
* @param b The second data schema to compare.
|
|
29
|
+
* @returns `true` if the schemas are nominally equal, `false` otherwise.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* import { vec3f, struct, schemasEqualNominally } from 'typegpu/data';
|
|
33
|
+
*
|
|
34
|
+
* const struct1 = struct({ a: vec3f });
|
|
35
|
+
* const struct2 = struct({ a: vec3f });
|
|
36
|
+
*
|
|
37
|
+
* console.log(schemasEqualNominally(d.arrayOf(struct1, 2), d.arrayOf(struct1, 2))); // true
|
|
38
|
+
* console.log(schemasEqualNominally(struct1, struct2)); // false
|
|
39
|
+
*/
|
|
40
|
+
export const schemasEqualNominally = comptime((a, b) => schemasEqual(a, b, false)).$name('schemasEqualNominally');
|
|
41
|
+
function schemasEqual(a, b, structural) {
|
|
42
|
+
if (a === b) {
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
if (a.type !== b.type) {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
if ((isWgslStruct(a) && isWgslStruct(b)) || (isUnstruct(a) && isUnstruct(b))) {
|
|
49
|
+
if (!structural) {
|
|
50
|
+
// With nominal typing, structs are only considered equal when they
|
|
51
|
+
// generate the same declaration, i.e. have the same object identity.
|
|
52
|
+
// If they didn't pass the previous condition, then they're not equal
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
const aProps = a.propTypes;
|
|
56
|
+
const bProps = b.propTypes;
|
|
57
|
+
const aKeys = Object.keys(aProps);
|
|
58
|
+
const bKeys = Object.keys(bProps);
|
|
59
|
+
if (aKeys.length !== bKeys.length) {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
for (let i = 0; i < aKeys.length; i++) {
|
|
63
|
+
const keyA = aKeys[i];
|
|
64
|
+
const keyB = bKeys[i];
|
|
65
|
+
if (keyA !== keyB ||
|
|
66
|
+
!keyA ||
|
|
67
|
+
!keyB ||
|
|
68
|
+
!schemasEqual(aProps[keyA], bProps[keyB], structural)) {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
if ((isWgslArray(a) && isWgslArray(b)) || (isDisarray(a) && isDisarray(b))) {
|
|
75
|
+
return (a.elementCount === b.elementCount &&
|
|
76
|
+
schemasEqual(a.elementType, b.elementType, structural));
|
|
77
|
+
}
|
|
78
|
+
if (isPtr(a) && isPtr(b)) {
|
|
79
|
+
return (a.addressSpace === b.addressSpace &&
|
|
80
|
+
a.access === b.access &&
|
|
81
|
+
a.implicit === b.implicit &&
|
|
82
|
+
schemasEqual(a.inner, b.inner, structural));
|
|
83
|
+
}
|
|
84
|
+
if (isAtomic(a) && isAtomic(b)) {
|
|
85
|
+
return schemasEqual(a.inner, b.inner, structural);
|
|
86
|
+
}
|
|
87
|
+
if (isWgslTexture(a) && isWgslTexture(b)) {
|
|
88
|
+
// Dimension and multisampling are already encoded in the type
|
|
89
|
+
return schemasEqual(a.sampleType, b.sampleType, structural);
|
|
90
|
+
}
|
|
91
|
+
if (isWgslStorageTexture(a) && isWgslStorageTexture(b)) {
|
|
92
|
+
// Dimension is already encoded in the type
|
|
93
|
+
return a.format === b.format && a.access === b.access;
|
|
94
|
+
}
|
|
95
|
+
if ((isDecorated(a) && isDecorated(b)) || (isLooseDecorated(a) && isLooseDecorated(b))) {
|
|
96
|
+
if (!schemasEqual(a.inner, b.inner, structural)) {
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
// Attribute order carries no meaning, so we compare attributes by type.
|
|
100
|
+
// A decorator replaces an existing one of the same type, so each type appears once.
|
|
101
|
+
const attribsA = a.attribs;
|
|
102
|
+
const attribsB = b.attribs;
|
|
103
|
+
if (attribsA.length !== attribsB.length) {
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
for (const attrA of attribsA) {
|
|
107
|
+
const attrB = attribsB.find((attr) => attr.type === attrA.type);
|
|
108
|
+
if (!attrB) {
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
const paramsA = attrA.params ?? [];
|
|
112
|
+
const paramsB = attrB.params ?? [];
|
|
113
|
+
if (paramsA.length !== paramsB.length ||
|
|
114
|
+
paramsA.some((param, idx) => param !== paramsB[idx])) {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return true;
|
|
119
|
+
}
|
|
120
|
+
// All other types have been checked for equality at the start
|
|
121
|
+
return true;
|
|
122
|
+
}
|
package/data/sizeOf.d.ts
CHANGED
|
@@ -4,4 +4,4 @@ export declare function sizeOf(schema: BaseData): number;
|
|
|
4
4
|
/**
|
|
5
5
|
* Returns the size (in bytes) of data represented by the `schema`.
|
|
6
6
|
*/
|
|
7
|
-
export declare
|
|
7
|
+
export declare const PUBLIC_sizeOf: import("../indexNamedExports.ts").TgpuComptime<(schema: AnyData) => number>;
|
package/data/sizeOf.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { comptime } from "../core/function/comptime.js";
|
|
1
2
|
import { getLayoutInfo } from "./schemaMemoryLayout.js";
|
|
2
3
|
export function sizeOf(schema) {
|
|
3
4
|
return getLayoutInfo(schema, 'size');
|
|
@@ -5,6 +6,4 @@ export function sizeOf(schema) {
|
|
|
5
6
|
/**
|
|
6
7
|
* Returns the size (in bytes) of data represented by the `schema`.
|
|
7
8
|
*/
|
|
8
|
-
export
|
|
9
|
-
return sizeOf(schema);
|
|
10
|
-
}
|
|
9
|
+
export const PUBLIC_sizeOf = comptime((schema) => sizeOf(schema));
|
package/data/snippet.d.ts
CHANGED
|
@@ -95,7 +95,7 @@ export interface ResolvedSnippet extends Snippet {
|
|
|
95
95
|
readonly dataType: BaseData;
|
|
96
96
|
}
|
|
97
97
|
export type MapValueToSnippet<T> = {
|
|
98
|
-
[K in keyof T]: Snippet;
|
|
98
|
+
readonly [K in keyof T]: Snippet;
|
|
99
99
|
};
|
|
100
100
|
export declare function isSnippet(value: unknown): value is Snippet;
|
|
101
101
|
export declare function isSnippetNumeric(snippet: Snippet): boolean;
|
package/data/struct.js
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
|
+
import { WgslTypeError } from "../errors.js";
|
|
1
2
|
import { validateProp } from "../nameUtils.js";
|
|
2
3
|
import { getName, setName } from "../shared/meta.js";
|
|
3
|
-
import { $internal } from "../shared/symbols.js";
|
|
4
|
+
import { $gpuCallable, $gpuCallableStrictSignature, $internal } from "../shared/symbols.js";
|
|
5
|
+
import { tryConvertSnippet } from "../tgsl/conversion.js";
|
|
4
6
|
import { schemaCallWrapper } from "./schemaCallWrapper.js";
|
|
7
|
+
import { fallthroughCopyOrigin, snip } from "./snippet.js";
|
|
5
8
|
// ----------
|
|
6
9
|
// Public API
|
|
7
10
|
// ----------
|
|
@@ -16,12 +19,14 @@ import { schemaCallWrapper } from "./schemaCallWrapper.js";
|
|
|
16
19
|
* @param props Record with `string` keys and `TgpuData` values,
|
|
17
20
|
* each entry describing one struct member.
|
|
18
21
|
*/
|
|
22
|
+
/*#__NO_SIDE_EFFECTS__*/
|
|
19
23
|
export function struct(props) {
|
|
20
24
|
if (Object.keys(props).length === 0) {
|
|
21
25
|
throw new Error('Cannot create struct schema with no properties.');
|
|
22
26
|
}
|
|
23
27
|
return INTERNAL_createStruct(props, false);
|
|
24
28
|
}
|
|
29
|
+
/*#__NO_SIDE_EFFECTS__*/
|
|
25
30
|
export function abstruct(props) {
|
|
26
31
|
return INTERNAL_createStruct(props, true);
|
|
27
32
|
}
|
|
@@ -59,4 +64,25 @@ const WgslStructImpl = {
|
|
|
59
64
|
toString() {
|
|
60
65
|
return `struct:${getName(this) ?? '<unnamed>'}`;
|
|
61
66
|
},
|
|
67
|
+
get [$gpuCallableStrictSignature]() {
|
|
68
|
+
return { argTypes: [this], returnType: this };
|
|
69
|
+
},
|
|
70
|
+
[$gpuCallable](ctx, args) {
|
|
71
|
+
if (args.length > 1) {
|
|
72
|
+
throw new WgslTypeError('Struct schemas should always be called with at most 1 argument');
|
|
73
|
+
}
|
|
74
|
+
// No arguments `Struct()`, resolve struct name and return.
|
|
75
|
+
if (!args[0]) {
|
|
76
|
+
// The schema becomes the data type.
|
|
77
|
+
return ctx.gen.typeInstantiation(this, []);
|
|
78
|
+
}
|
|
79
|
+
const arg = tryConvertSnippet(ctx, args[0], this);
|
|
80
|
+
// Either `Struct({ x: 1, y: 2 })`, or `Struct(otherStruct)`.
|
|
81
|
+
// In both cases, we just let the argument resolve everything.
|
|
82
|
+
return snip(ctx.resolveSnippet(arg).value, this,
|
|
83
|
+
// A new struct, so not a reference.
|
|
84
|
+
/* origin */ fallthroughCopyOrigin(arg.origin), arg.possibleSideEffects);
|
|
85
|
+
},
|
|
62
86
|
};
|
|
87
|
+
// Schemas are still functions, so they should keep `call`, `apply`, `bind`, etc.
|
|
88
|
+
Object.setPrototypeOf(WgslStructImpl, Function.prototype);
|
package/data/unstruct.js
CHANGED
package/data/vectorImpl.js
CHANGED
|
@@ -14,16 +14,25 @@ export class VecBase extends Array {
|
|
|
14
14
|
for (let e3 = 0; e3 < 4; e3++) {
|
|
15
15
|
const xyzwProp = `${XYZW[e0]}${XYZW[e1]}${XYZW[e2]}${XYZW[e3]}`;
|
|
16
16
|
const rgbaProp = `${RGBA[e0]}${RGBA[e1]}${RGBA[e2]}${RGBA[e3]}`;
|
|
17
|
-
|
|
17
|
+
const permutation = new Set([e0, e1, e2, e3]).size === 4;
|
|
18
|
+
const property = {
|
|
18
19
|
get() {
|
|
19
20
|
return new this._Vec4(this[e0], this[e1], this[e2], this[e3]);
|
|
20
21
|
},
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
22
|
+
...(permutation
|
|
23
|
+
? {
|
|
24
|
+
set(other) {
|
|
25
|
+
const [v0, v1, v2, v3] = other;
|
|
26
|
+
this[e0] = v0;
|
|
27
|
+
this[e1] = v1;
|
|
28
|
+
this[e2] = v2;
|
|
29
|
+
this[e3] = v3;
|
|
30
|
+
},
|
|
31
|
+
}
|
|
32
|
+
: {}),
|
|
33
|
+
};
|
|
34
|
+
Object.defineProperty(VecBase.prototype, xyzwProp, property);
|
|
35
|
+
Object.defineProperty(VecBase.prototype, rgbaProp, property);
|
|
27
36
|
}
|
|
28
37
|
}
|
|
29
38
|
}
|
|
@@ -34,16 +43,24 @@ export class VecBase extends Array {
|
|
|
34
43
|
for (let e2 = 0; e2 < 4; e2++) {
|
|
35
44
|
const xyzwProp = `${XYZW[e0]}${XYZW[e1]}${XYZW[e2]}`;
|
|
36
45
|
const rgbaProp = `${RGBA[e0]}${RGBA[e1]}${RGBA[e2]}`;
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
return new this._Vec3(this[e0], this[e1], this[e2]);
|
|
40
|
-
},
|
|
41
|
-
});
|
|
42
|
-
Object.defineProperty(VecBase.prototype, rgbaProp, {
|
|
46
|
+
const permutation = new Set([e0, e1, e2]).size === 3;
|
|
47
|
+
const property = {
|
|
43
48
|
get() {
|
|
44
49
|
return new this._Vec3(this[e0], this[e1], this[e2]);
|
|
45
50
|
},
|
|
46
|
-
|
|
51
|
+
...(permutation
|
|
52
|
+
? {
|
|
53
|
+
set(other) {
|
|
54
|
+
const [v0, v1, v2] = other;
|
|
55
|
+
this[e0] = v0;
|
|
56
|
+
this[e1] = v1;
|
|
57
|
+
this[e2] = v2;
|
|
58
|
+
},
|
|
59
|
+
}
|
|
60
|
+
: {}),
|
|
61
|
+
};
|
|
62
|
+
Object.defineProperty(VecBase.prototype, xyzwProp, property);
|
|
63
|
+
Object.defineProperty(VecBase.prototype, rgbaProp, property);
|
|
47
64
|
}
|
|
48
65
|
}
|
|
49
66
|
}
|
|
@@ -52,16 +69,23 @@ export class VecBase extends Array {
|
|
|
52
69
|
for (let e1 = 0; e1 < 4; e1++) {
|
|
53
70
|
const xyzwProp = `${XYZW[e0]}${XYZW[e1]}`;
|
|
54
71
|
const rgbaProp = `${RGBA[e0]}${RGBA[e1]}`;
|
|
55
|
-
|
|
72
|
+
const permutation = new Set([e0, e1]).size === 2;
|
|
73
|
+
const property = {
|
|
56
74
|
get() {
|
|
57
75
|
return new this._Vec2(this[e0], this[e1]);
|
|
58
76
|
},
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
77
|
+
...(permutation
|
|
78
|
+
? {
|
|
79
|
+
set(other) {
|
|
80
|
+
const [v0, v1] = other;
|
|
81
|
+
this[e0] = v0;
|
|
82
|
+
this[e1] = v1;
|
|
83
|
+
},
|
|
84
|
+
}
|
|
85
|
+
: {}),
|
|
86
|
+
};
|
|
87
|
+
Object.defineProperty(VecBase.prototype, xyzwProp, property);
|
|
88
|
+
Object.defineProperty(VecBase.prototype, rgbaProp, property);
|
|
65
89
|
}
|
|
66
90
|
}
|
|
67
91
|
}
|
package/data/vertexFormatData.js
CHANGED
|
@@ -3,17 +3,15 @@ import { f32, i32, u32 } from "./numeric.js";
|
|
|
3
3
|
import { vec2f, vec2i, vec2u, vec3f, vec3i, vec3u, vec4f, vec4i, vec4u } from "./vector.js";
|
|
4
4
|
import { schemaCallWrapper, schemaCallWrapperGPU } from "./schemaCallWrapper.js";
|
|
5
5
|
class TgpuVertexFormatDataImpl {
|
|
6
|
-
[$internal] = {};
|
|
7
6
|
type;
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
static {
|
|
8
|
+
TgpuVertexFormatDataImpl.prototype[$internal] = {};
|
|
9
|
+
}
|
|
10
10
|
constructor(type) {
|
|
11
11
|
this.type = type;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
},
|
|
16
|
-
};
|
|
12
|
+
}
|
|
13
|
+
[$gpuCallable](ctx, [v]) {
|
|
14
|
+
return schemaCallWrapperGPU(ctx, formatToWGSLType[this.type], v);
|
|
17
15
|
}
|
|
18
16
|
[$cast](v) {
|
|
19
17
|
return schemaCallWrapper(formatToWGSLType[this.type], v);
|
|
@@ -105,5 +103,5 @@ export const sint32x4 = new TgpuVertexFormatDataImpl('sint32x4');
|
|
|
105
103
|
export const unorm10_10_10_2 = new TgpuVertexFormatDataImpl('unorm10-10-10-2');
|
|
106
104
|
export const unorm8x4_bgra = new TgpuVertexFormatDataImpl('unorm8x4-bgra');
|
|
107
105
|
export function isPackedData(value) {
|
|
108
|
-
return
|
|
106
|
+
return packedFormats.has(value?.type) && isMarkedInternal(value);
|
|
109
107
|
}
|
package/data/wgslTypes.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ import type { Prettify, SwapNever } from '../shared/utilityTypes.ts';
|
|
|
7
7
|
import type { WgslExternalTexture, WgslStorageTexture, WgslTexture } from './texture.ts';
|
|
8
8
|
import type { WgslComparisonSampler, WgslSampler } from './sampler.ts';
|
|
9
9
|
import type { _ref as ref } from './ref.ts';
|
|
10
|
-
import type { DualFn } from '../types.ts';
|
|
10
|
+
import type { DualFn, GPUCallable } from '../types.ts';
|
|
11
11
|
type DecoratedLocation<T extends BaseData> = Decorated<T, Location[]>;
|
|
12
12
|
export interface BaseData {
|
|
13
13
|
readonly [$internal]: Record<string, unknown>;
|
|
@@ -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
|
*
|
|
@@ -96,12 +96,23 @@ type XYZW = 'x' | 'y' | 'z' | 'w';
|
|
|
96
96
|
type RG = 'r' | 'g';
|
|
97
97
|
type RGB = 'r' | 'g' | 'b';
|
|
98
98
|
type RGBA = 'r' | 'g' | 'b' | 'a';
|
|
99
|
+
type Perm2<T extends string> = {
|
|
100
|
+
[Fst in T]: `${Fst}${Exclude<T, Fst>}`;
|
|
101
|
+
}[T];
|
|
102
|
+
type Perm3<T extends string> = {
|
|
103
|
+
[Fst in T]: `${Fst}${Perm2<Exclude<T, Fst>>}`;
|
|
104
|
+
}[T];
|
|
105
|
+
type Perm4<T extends string> = {
|
|
106
|
+
[Fst in T]: `${Fst}${Perm3<Exclude<T, Fst>>}`;
|
|
107
|
+
}[T];
|
|
99
108
|
type Swizzle2<T2, T3, T4> = {
|
|
100
109
|
readonly [K in `${XY}${XY}` | `${RG}${RG}`]: T2;
|
|
101
110
|
} & {
|
|
102
111
|
readonly [K in `${XY}${XY}${XY}` | `${RG}${RG}${RG}`]: T3;
|
|
103
112
|
} & {
|
|
104
113
|
readonly [K in `${XY}${XY}${XY}${XY}` | `${RG}${RG}${RG}${RG}`]: T4;
|
|
114
|
+
} & {
|
|
115
|
+
[K in Perm2<XY> | Perm2<RG>]: T2;
|
|
105
116
|
};
|
|
106
117
|
type Swizzle3<T2, T3, T4> = {
|
|
107
118
|
readonly [K in `${XYZ}${XYZ}` | `${RGB}${RGB}`]: T2;
|
|
@@ -109,6 +120,10 @@ type Swizzle3<T2, T3, T4> = {
|
|
|
109
120
|
readonly [K in `${XYZ}${XYZ}${XYZ}` | `${RGB}${RGB}${RGB}`]: T3;
|
|
110
121
|
} & {
|
|
111
122
|
readonly [K in `${XYZ}${XYZ}${XYZ}${XYZ}` | `${RGB}${RGB}${RGB}${RGB}`]: T4;
|
|
123
|
+
} & {
|
|
124
|
+
[K in Perm2<XYZ> | Perm2<RGB>]: T2;
|
|
125
|
+
} & {
|
|
126
|
+
[K in Perm3<XYZ> | Perm3<RGB>]: T3;
|
|
112
127
|
};
|
|
113
128
|
type Swizzle4<T2, T3, T4> = {
|
|
114
129
|
readonly [K in `${XYZW}${XYZW}` | `${RGBA}${RGBA}`]: T2;
|
|
@@ -116,6 +131,12 @@ type Swizzle4<T2, T3, T4> = {
|
|
|
116
131
|
readonly [K in `${XYZW}${XYZW}${XYZW}` | `${RGBA}${RGBA}${RGBA}`]: T3;
|
|
117
132
|
} & {
|
|
118
133
|
readonly [K in `${XYZW}${XYZW}${XYZW}${XYZW}` | `${RGBA}${RGBA}${RGBA}${RGBA}`]: T4;
|
|
134
|
+
} & {
|
|
135
|
+
[K in Perm2<XYZW> | Perm2<RGBA>]: T2;
|
|
136
|
+
} & {
|
|
137
|
+
[K in Perm3<XYZW> | Perm3<RGBA>]: T3;
|
|
138
|
+
} & {
|
|
139
|
+
[K in Perm4<XYZW> | Perm4<RGBA>]: T4;
|
|
119
140
|
};
|
|
120
141
|
type Tuple2<S> = [S, S];
|
|
121
142
|
type Tuple3<S> = [S, S, S];
|
|
@@ -365,7 +386,6 @@ export interface v4b extends Tuple4<boolean>, Swizzle4<v2b, v3b, v4b> {
|
|
|
365
386
|
a: boolean;
|
|
366
387
|
}
|
|
367
388
|
export type AnyFloat32VecInstance = v2f | v3f | v4f;
|
|
368
|
-
export type AnyFloat16VecInstance = v2h | v3h | v4h;
|
|
369
389
|
export type AnyFloatVecInstance = v2f | v2h | v3f | v3h | v4f | v4h;
|
|
370
390
|
export type AnyUnsignedVecInstance = v2u | v3u | v4u;
|
|
371
391
|
export type AnyIntegerVecInstance = v2i | v2u | v3i | v3u | v4i | v4u;
|
|
@@ -757,12 +777,12 @@ export interface Mat4x4f extends BaseData {
|
|
|
757
777
|
* between binary and JS representation. Takes into account
|
|
758
778
|
* the `byteAlignment` requirement of its elementType.
|
|
759
779
|
*/
|
|
760
|
-
export interface WgslArray<out TElement extends BaseData = BaseData> extends BaseData {
|
|
761
|
-
<T extends TElement>(elements: Infer<T>[]): Infer<T>[];
|
|
762
|
-
(): Infer<TElement>[];
|
|
780
|
+
export interface WgslArray<out TElement extends BaseData = BaseData> extends BaseData, GPUCallable<[elements?: Infer<TElement>[]]> {
|
|
763
781
|
readonly type: 'array';
|
|
764
782
|
readonly elementCount: number;
|
|
765
783
|
readonly elementType: TElement;
|
|
784
|
+
<T extends TElement>(elements: Infer<T>[]): Infer<T>[];
|
|
785
|
+
(): Infer<TElement>[];
|
|
766
786
|
readonly [$repr]: Infer<TElement>[];
|
|
767
787
|
readonly [$inRepr]: readonly InferInput<TElement>[] | TypedArrayFor<TElement>;
|
|
768
788
|
readonly [$gpuRepr]: InferGPU<TElement>[];
|
|
@@ -784,14 +804,13 @@ export interface WgslArray<out TElement extends BaseData = BaseData> extends Bas
|
|
|
784
804
|
* between binary and JS representation. Takes into account
|
|
785
805
|
* the `byteAlignment` requirement of its members.
|
|
786
806
|
*/
|
|
787
|
-
export interface WgslStruct<out TProps extends Record<string, BaseData> = Record<string, BaseData>> extends BaseData, TgpuNamable {
|
|
807
|
+
export interface WgslStruct<out TProps extends Record<string, BaseData> = Record<string, BaseData>> extends BaseData, TgpuNamable, GPUCallable<[props?: Prettify<InferRecord<TProps>>]> {
|
|
788
808
|
readonly [$internal]: {
|
|
789
809
|
isAbstruct: boolean;
|
|
790
810
|
};
|
|
791
811
|
readonly type: 'struct';
|
|
792
812
|
readonly propTypes: TProps;
|
|
793
|
-
(props
|
|
794
|
-
(): Prettify<InferRecord<TProps>>;
|
|
813
|
+
(props?: Prettify<InferRecord<TProps>>): Prettify<InferRecord<TProps>>;
|
|
795
814
|
readonly [$repr]: Prettify<InferRecord<TProps>>;
|
|
796
815
|
readonly [$inRepr]: Prettify<InferInputRecord<TProps>>;
|
|
797
816
|
readonly [$gpuRepr]: Prettify<InferGPURecord<TProps>>;
|
|
@@ -907,11 +926,8 @@ export type ScalarData = Bool | F32 | F16 | I32 | U32 | AbstractInt | AbstractFl
|
|
|
907
926
|
export type VecData = Vec2f | Vec2h | Vec2i | Vec2u | Vec2b | Vec3f | Vec3h | Vec3i | Vec3u | Vec3b | Vec4f | Vec4h | Vec4i | Vec4u | Vec4b;
|
|
908
927
|
export type MatData = Mat2x2f | Mat3x3f | Mat4x4f;
|
|
909
928
|
export type StorableData = ScalarData | VecData | MatData | Atomic<I32> | Atomic<U32> | WgslArray | WgslStruct;
|
|
910
|
-
export type AnyFloat32VecData = Vec2f | Vec3f | Vec4f;
|
|
911
|
-
export type AnyFloat16VecData = Vec2h | Vec3h | Vec4h;
|
|
912
929
|
export type AnyWgslData = Bool | F32 | F16 | I32 | U32 | U16 | Vec2f | Vec2h | Vec2i | Vec2u | Vec2b | Vec3f | Vec3h | Vec3i | Vec3u | Vec3b | Vec4f | Vec4h | Vec4i | Vec4u | Vec4b | Mat2x2f | Mat3x3f | Mat4x4f | WgslStruct | WgslArray | Ptr | Atomic<U32> | Atomic<I32> | Decorated | AbstractInt | AbstractFloat | Void | WgslTexture | WgslStorageTexture | WgslExternalTexture | WgslSampler | WgslComparisonSampler;
|
|
913
930
|
export declare function isVecInstance(value: unknown): value is AnyVecInstance;
|
|
914
|
-
export declare function isVecBoolInstance(value: unknown): value is v2b | v3b | v4b;
|
|
915
931
|
export declare function isVec2(value: unknown): value is Vec2f | Vec2h | Vec2i | Vec2u;
|
|
916
932
|
export declare function isVec3(value: unknown): value is Vec3f | Vec3h | Vec3i | Vec3u;
|
|
917
933
|
export declare function isVec4(value: unknown): value is Vec4f | Vec4h | Vec4i | Vec4u;
|
|
@@ -981,7 +997,6 @@ export declare function isDecorated(value: unknown): value is Decorated;
|
|
|
981
997
|
export declare function isAbstractFloat(value: unknown): value is AbstractFloat;
|
|
982
998
|
export declare function isAbstractInt(value: unknown): value is AbstractInt;
|
|
983
999
|
export declare function isAbstract(value: unknown): value is AbstractFloat | AbstractInt;
|
|
984
|
-
export declare function isConcrete(value: unknown): boolean;
|
|
985
1000
|
export declare function isVoid(value: unknown): value is Void;
|
|
986
1001
|
export declare function isBool(value: unknown): value is Bool;
|
|
987
1002
|
export declare function isNumericSchema(schema: unknown): schema is AbstractInt | AbstractFloat | F32 | F16 | I32 | U32;
|