typegpu 0.8.2 → 0.10.0
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/README.md +2 -2
- package/builtin-ClEnM-Ye.js +818 -0
- package/builtin-ClEnM-Ye.js.map +1 -0
- package/chunk-BYypO7fO.js +18 -0
- package/common/index.d.ts +8 -23
- package/common/index.d.ts.map +1 -0
- package/common/index.js +7 -5
- package/common/index.js.map +1 -1
- package/data/index.d.ts +7 -400
- package/data/index.d.ts.map +1 -0
- package/data/index.js +164 -1
- package/data/index.js.map +1 -1
- package/deepEqual-yZXvaV2C.js +413 -0
- package/deepEqual-yZXvaV2C.js.map +1 -0
- package/extensions-0SFbU9FH.js +2032 -0
- package/extensions-0SFbU9FH.js.map +1 -0
- package/fullScreenTriangle-MdLGaAMR.js +543 -0
- package/fullScreenTriangle-MdLGaAMR.js.map +1 -0
- package/index.d.ts +167 -264
- package/index.d.ts.map +1 -0
- package/index.js +6294 -160
- package/index.js.map +1 -1
- package/indexNamedExports-Cdy7USiY.d.ts +5696 -0
- package/indexNamedExports-Cdy7USiY.d.ts.map +1 -0
- package/operators-HTxa_0k9.js +4156 -0
- package/operators-HTxa_0k9.js.map +1 -0
- package/package.json +3 -2
- package/std/index.d.ts +7 -637
- package/std/index.d.ts.map +1 -0
- package/std/index.js +165 -1
- package/std/index.js.map +1 -1
- package/texture-Dg5ybJro.js +205 -0
- package/texture-Dg5ybJro.js.map +1 -0
- package/chunk-6U5HPHCJ.js +0 -2
- package/chunk-6U5HPHCJ.js.map +0 -1
- package/chunk-CGQB3SER.js +0 -3
- package/chunk-CGQB3SER.js.map +0 -1
- package/chunk-CRZWTZM2.js +0 -10
- package/chunk-CRZWTZM2.js.map +0 -1
- package/chunk-ECR2EGZX.js +0 -7
- package/chunk-ECR2EGZX.js.map +0 -1
- package/chunk-PWBIFP67.js +0 -2
- package/chunk-PWBIFP67.js.map +0 -1
- package/matrix-DIfOiRyz.d.ts +0 -122
- package/tgpuConstant-BTQFNlQH.d.ts +0 -5201
|
@@ -0,0 +1,4156 @@
|
|
|
1
|
+
//#region package.json
|
|
2
|
+
var version = "0.10.0";
|
|
3
|
+
|
|
4
|
+
//#endregion
|
|
5
|
+
//#region src/shared/env.ts
|
|
6
|
+
/**
|
|
7
|
+
* This can be used to branch functionality between "dev" and "prod" modes, so that our
|
|
8
|
+
* library can omit doing unnecessary work once it's out in the wild
|
|
9
|
+
*
|
|
10
|
+
* Even though the value of this constant uses Node.js specific APIs, pretty much every
|
|
11
|
+
* bundler replaces the expression below with either `development` or `production`
|
|
12
|
+
*/
|
|
13
|
+
const DEV = process.env.NODE_ENV === "development";
|
|
14
|
+
const TEST = process.env.NODE_ENV === "test";
|
|
15
|
+
|
|
16
|
+
//#endregion
|
|
17
|
+
//#region src/shared/symbols.ts
|
|
18
|
+
const $internal = Symbol(`typegpu:${version}:$internal`);
|
|
19
|
+
/**
|
|
20
|
+
* The getter to the value of this resource, accessible on the GPU
|
|
21
|
+
*/
|
|
22
|
+
const $gpuValueOf = Symbol(`typegpu:${version}:$gpuValueOf`);
|
|
23
|
+
/**
|
|
24
|
+
* If this symbol is present, this means that getName and setName
|
|
25
|
+
* will refer to object behind this property instead.
|
|
26
|
+
*/
|
|
27
|
+
const $getNameForward = Symbol(`typegpu:${version}:$getNameForward`);
|
|
28
|
+
/**
|
|
29
|
+
* Marks an object with slot-value bindings
|
|
30
|
+
*/
|
|
31
|
+
const $providing = Symbol(`typegpu:${version}:$providing`);
|
|
32
|
+
/**
|
|
33
|
+
* Objects can provide the snippet that represents them.
|
|
34
|
+
*/
|
|
35
|
+
const $ownSnippet = Symbol(`typegpu:${version}:$ownSnippet`);
|
|
36
|
+
const $resolve = Symbol(`typegpu:${version}:$resolve`);
|
|
37
|
+
/**
|
|
38
|
+
* A way for a schema to provide casting behavior, without the need to be explicitly
|
|
39
|
+
* callable by the end-user (e.g. vertex formats)
|
|
40
|
+
*/
|
|
41
|
+
const $cast = Symbol(`typegpu:${version}:$cast`);
|
|
42
|
+
/**
|
|
43
|
+
* Can be called on the GPU
|
|
44
|
+
*/
|
|
45
|
+
const $gpuCallable = Symbol(`typegpu:${version}:$gpuCallable`);
|
|
46
|
+
/**
|
|
47
|
+
* Type token for the inferred (CPU & GPU) representation of a resource
|
|
48
|
+
*/
|
|
49
|
+
const $repr = Symbol(`typegpu:${version}:$repr`);
|
|
50
|
+
/**
|
|
51
|
+
* Type token for the inferred (GPU-side) representation of a resource
|
|
52
|
+
* If present, it shadows the value of `$repr` for GPU-side inference.
|
|
53
|
+
*/
|
|
54
|
+
const $gpuRepr = Symbol(`typegpu:${version}:$gpuRepr`);
|
|
55
|
+
/**
|
|
56
|
+
* Type token for the inferred partial representation of a resource.
|
|
57
|
+
* If present, it shadows the value of `$repr` for use in partial IO.
|
|
58
|
+
*/
|
|
59
|
+
const $reprPartial = Symbol(`typegpu:${version}:$reprPartial`);
|
|
60
|
+
/**
|
|
61
|
+
* Type token holding schemas that are identical in memory layout.
|
|
62
|
+
*/
|
|
63
|
+
const $memIdent = Symbol(`typegpu:${version}:$memIdent`);
|
|
64
|
+
/**
|
|
65
|
+
* Type token, signaling that a schema can be used in a storage buffer.
|
|
66
|
+
*/
|
|
67
|
+
const $validStorageSchema = Symbol(`typegpu:${version}:$invalidStorageSchema`);
|
|
68
|
+
/**
|
|
69
|
+
* Type token, signaling that a schema can be used in a uniform buffer.
|
|
70
|
+
*/
|
|
71
|
+
const $validUniformSchema = Symbol(`typegpu:${version}:$validUniformSchema`);
|
|
72
|
+
/**
|
|
73
|
+
* Type token, signaling that a schema can be used in a vertex buffer.
|
|
74
|
+
*/
|
|
75
|
+
const $validVertexSchema = Symbol(`typegpu:${version}:$validVertexSchema`);
|
|
76
|
+
/**
|
|
77
|
+
* Type token, containing a reason for why the schema is invalid (if it is).
|
|
78
|
+
*/
|
|
79
|
+
const $invalidSchemaReason = Symbol(`typegpu:${version}:$invalidSchemaReason`);
|
|
80
|
+
function isMarkedInternal(value) {
|
|
81
|
+
return !!value?.[$internal];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
//#endregion
|
|
85
|
+
//#region src/shared/meta.ts
|
|
86
|
+
const globalWithMeta = globalThis;
|
|
87
|
+
if (globalWithMeta.__TYPEGPU_VERSION__ !== void 0) console.warn(`Found duplicate TypeGPU version. First was ${globalWithMeta.__TYPEGPU_VERSION__}, this one is ${version}. This may cause unexpected behavior.`);
|
|
88
|
+
globalWithMeta.__TYPEGPU_VERSION__ = version;
|
|
89
|
+
globalWithMeta.__TYPEGPU_AUTONAME__ = (exp, label) => isNamable(exp) && isMarkedInternal(exp) && !getName(exp) ? exp.$name(label) : exp;
|
|
90
|
+
/**
|
|
91
|
+
* Performance measurements are only enabled in dev & test environments for now
|
|
92
|
+
*/
|
|
93
|
+
const PERF = (DEV || TEST) && {
|
|
94
|
+
get enabled() {
|
|
95
|
+
return !!globalWithMeta.__TYPEGPU_MEASURE_PERF__;
|
|
96
|
+
},
|
|
97
|
+
record(name, data) {
|
|
98
|
+
const records = globalWithMeta.__TYPEGPU_PERF_RECORDS__ ??= /* @__PURE__ */ new Map();
|
|
99
|
+
let entries = records.get(name);
|
|
100
|
+
if (!entries) {
|
|
101
|
+
entries = [];
|
|
102
|
+
records.set(name, entries);
|
|
103
|
+
}
|
|
104
|
+
entries.push(data);
|
|
105
|
+
}
|
|
106
|
+
} || void 0;
|
|
107
|
+
function isForwarded(value) {
|
|
108
|
+
return !!value?.[$getNameForward];
|
|
109
|
+
}
|
|
110
|
+
function getName(definition) {
|
|
111
|
+
if (isForwarded(definition)) return getName(definition[$getNameForward]);
|
|
112
|
+
return getMetaData(definition)?.name;
|
|
113
|
+
}
|
|
114
|
+
function setName(definition, name) {
|
|
115
|
+
if (isForwarded(definition)) {
|
|
116
|
+
setName(definition[$getNameForward], name);
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
setMetaData(definition, { name });
|
|
120
|
+
}
|
|
121
|
+
function isNamable(value) {
|
|
122
|
+
return !!value?.$name;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* AST's are given to functions with a 'use gpu' directive, which this function checks for.
|
|
126
|
+
*/
|
|
127
|
+
function hasTinyestMetadata(value) {
|
|
128
|
+
return !!getMetaData(value)?.ast;
|
|
129
|
+
}
|
|
130
|
+
function getMetaData(definition) {
|
|
131
|
+
return globalWithMeta.__TYPEGPU_META__.get(definition);
|
|
132
|
+
}
|
|
133
|
+
function setMetaData(definition, metaData) {
|
|
134
|
+
globalWithMeta.__TYPEGPU_META__ ??= /* @__PURE__ */ new WeakMap();
|
|
135
|
+
const map = globalWithMeta.__TYPEGPU_META__;
|
|
136
|
+
map.set(definition, {
|
|
137
|
+
...map.get(definition),
|
|
138
|
+
...metaData
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
//#endregion
|
|
143
|
+
//#region src/shared/vertexFormat.ts
|
|
144
|
+
const vertexFormats = [
|
|
145
|
+
"uint8",
|
|
146
|
+
"uint8x2",
|
|
147
|
+
"uint8x4",
|
|
148
|
+
"sint8",
|
|
149
|
+
"sint8x2",
|
|
150
|
+
"sint8x4",
|
|
151
|
+
"unorm8",
|
|
152
|
+
"unorm8x2",
|
|
153
|
+
"unorm8x4",
|
|
154
|
+
"snorm8",
|
|
155
|
+
"snorm8x2",
|
|
156
|
+
"snorm8x4",
|
|
157
|
+
"uint16",
|
|
158
|
+
"uint16x2",
|
|
159
|
+
"uint16x4",
|
|
160
|
+
"sint16",
|
|
161
|
+
"sint16x2",
|
|
162
|
+
"sint16x4",
|
|
163
|
+
"unorm16",
|
|
164
|
+
"unorm16x2",
|
|
165
|
+
"unorm16x4",
|
|
166
|
+
"snorm16",
|
|
167
|
+
"snorm16x2",
|
|
168
|
+
"snorm16x4",
|
|
169
|
+
"float16",
|
|
170
|
+
"float16x2",
|
|
171
|
+
"float16x4",
|
|
172
|
+
"float32",
|
|
173
|
+
"float32x2",
|
|
174
|
+
"float32x3",
|
|
175
|
+
"float32x4",
|
|
176
|
+
"uint32",
|
|
177
|
+
"uint32x2",
|
|
178
|
+
"uint32x3",
|
|
179
|
+
"uint32x4",
|
|
180
|
+
"sint32",
|
|
181
|
+
"sint32x2",
|
|
182
|
+
"sint32x3",
|
|
183
|
+
"sint32x4",
|
|
184
|
+
"unorm10-10-10-2",
|
|
185
|
+
"unorm8x4-bgra"
|
|
186
|
+
];
|
|
187
|
+
const kindToDefaultFormatMap = {
|
|
188
|
+
f32: "float32",
|
|
189
|
+
vec2f: "float32x2",
|
|
190
|
+
vec3f: "float32x3",
|
|
191
|
+
vec4f: "float32x4",
|
|
192
|
+
f16: "float16",
|
|
193
|
+
vec2h: "float16x2",
|
|
194
|
+
vec4h: "float16x4",
|
|
195
|
+
u32: "uint32",
|
|
196
|
+
vec2u: "uint32x2",
|
|
197
|
+
vec3u: "uint32x3",
|
|
198
|
+
vec4u: "uint32x4",
|
|
199
|
+
i32: "sint32",
|
|
200
|
+
vec2i: "sint32x2",
|
|
201
|
+
vec3i: "sint32x3",
|
|
202
|
+
vec4i: "sint32x4"
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
//#endregion
|
|
206
|
+
//#region src/data/wgslTypes.ts
|
|
207
|
+
const Void = {
|
|
208
|
+
[$internal]: {},
|
|
209
|
+
type: "void",
|
|
210
|
+
toString() {
|
|
211
|
+
return "void";
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
const wgslTypeLiterals = [
|
|
215
|
+
"bool",
|
|
216
|
+
"f32",
|
|
217
|
+
"f16",
|
|
218
|
+
"i32",
|
|
219
|
+
"u32",
|
|
220
|
+
"u16",
|
|
221
|
+
"vec2f",
|
|
222
|
+
"vec2h",
|
|
223
|
+
"vec2i",
|
|
224
|
+
"vec2u",
|
|
225
|
+
"vec2<bool>",
|
|
226
|
+
"vec3f",
|
|
227
|
+
"vec3h",
|
|
228
|
+
"vec3i",
|
|
229
|
+
"vec3u",
|
|
230
|
+
"vec3<bool>",
|
|
231
|
+
"vec4f",
|
|
232
|
+
"vec4h",
|
|
233
|
+
"vec4i",
|
|
234
|
+
"vec4u",
|
|
235
|
+
"vec4<bool>",
|
|
236
|
+
"mat2x2f",
|
|
237
|
+
"mat3x3f",
|
|
238
|
+
"mat4x4f",
|
|
239
|
+
"struct",
|
|
240
|
+
"array",
|
|
241
|
+
"ptr",
|
|
242
|
+
"atomic",
|
|
243
|
+
"decorated",
|
|
244
|
+
"abstractInt",
|
|
245
|
+
"abstractFloat",
|
|
246
|
+
"void",
|
|
247
|
+
"texture_1d",
|
|
248
|
+
"texture_storage_1d",
|
|
249
|
+
"texture_2d",
|
|
250
|
+
"texture_storage_2d",
|
|
251
|
+
"texture_multisampled_2d",
|
|
252
|
+
"texture_depth_2d",
|
|
253
|
+
"texture_depth_multisampled_2d",
|
|
254
|
+
"texture_2d_array",
|
|
255
|
+
"texture_storage_2d_array",
|
|
256
|
+
"texture_depth_2d_array",
|
|
257
|
+
"texture_cube",
|
|
258
|
+
"texture_depth_cube",
|
|
259
|
+
"texture_cube_array",
|
|
260
|
+
"texture_depth_cube_array",
|
|
261
|
+
"texture_3d",
|
|
262
|
+
"texture_storage_3d",
|
|
263
|
+
"texture_external",
|
|
264
|
+
"sampler",
|
|
265
|
+
"sampler_comparison"
|
|
266
|
+
];
|
|
267
|
+
function isVecInstance(value) {
|
|
268
|
+
const v = value;
|
|
269
|
+
return isMarkedInternal(v) && typeof v.kind === "string" && v.kind.startsWith("vec");
|
|
270
|
+
}
|
|
271
|
+
function isVec2(value) {
|
|
272
|
+
const v = value;
|
|
273
|
+
return isMarkedInternal(v) && typeof v.type === "string" && v.type.startsWith("vec2");
|
|
274
|
+
}
|
|
275
|
+
function isVec3(value) {
|
|
276
|
+
const v = value;
|
|
277
|
+
return isMarkedInternal(v) && typeof v.type === "string" && v.type.startsWith("vec3");
|
|
278
|
+
}
|
|
279
|
+
function isVec4(value) {
|
|
280
|
+
const v = value;
|
|
281
|
+
return isMarkedInternal(v) && typeof v.type === "string" && v.type.startsWith("vec4");
|
|
282
|
+
}
|
|
283
|
+
function isVec(value) {
|
|
284
|
+
return isVec2(value) || isVec3(value) || isVec4(value);
|
|
285
|
+
}
|
|
286
|
+
function isVecBool(value) {
|
|
287
|
+
return isVec(value) && value.type.includes("b");
|
|
288
|
+
}
|
|
289
|
+
function isMatInstance(value) {
|
|
290
|
+
const v = value;
|
|
291
|
+
return isMarkedInternal(v) && typeof v.kind?.startsWith === "function" && v.kind.startsWith("mat");
|
|
292
|
+
}
|
|
293
|
+
function isMat2x2f(value) {
|
|
294
|
+
return isMarkedInternal(value) && value?.type === "mat2x2f";
|
|
295
|
+
}
|
|
296
|
+
function isMat3x3f(value) {
|
|
297
|
+
return isMarkedInternal(value) && value?.type === "mat3x3f";
|
|
298
|
+
}
|
|
299
|
+
function isMat4x4f(value) {
|
|
300
|
+
return isMarkedInternal(value) && value?.type === "mat4x4f";
|
|
301
|
+
}
|
|
302
|
+
function isMat(value) {
|
|
303
|
+
return isMat2x2f(value) || isMat3x3f(value) || isMat4x4f(value);
|
|
304
|
+
}
|
|
305
|
+
function isFloat32VecInstance(element) {
|
|
306
|
+
return isVecInstance(element) && [
|
|
307
|
+
"vec2f",
|
|
308
|
+
"vec3f",
|
|
309
|
+
"vec4f"
|
|
310
|
+
].includes(element.kind);
|
|
311
|
+
}
|
|
312
|
+
function isWgslData(value) {
|
|
313
|
+
return isMarkedInternal(value) && wgslTypeLiterals.includes(value?.type);
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Checks whether passed in value is an array schema,
|
|
317
|
+
* as opposed to, e.g., a disarray schema.
|
|
318
|
+
*
|
|
319
|
+
* Array schemas can be used to describe uniform and storage buffers,
|
|
320
|
+
* whereas disarray schemas cannot.
|
|
321
|
+
*
|
|
322
|
+
* @example
|
|
323
|
+
* isWgslArray(d.arrayOf(d.u32, 4)) // true
|
|
324
|
+
* isWgslArray(d.disarray(d.u32, 4)) // false
|
|
325
|
+
* isWgslArray(d.vec3f) // false
|
|
326
|
+
*/
|
|
327
|
+
function isWgslArray(schema) {
|
|
328
|
+
return isMarkedInternal(schema) && schema?.type === "array";
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* Checks whether passed in value is a struct schema,
|
|
332
|
+
* as opposed to, e.g., an unstruct schema.
|
|
333
|
+
*
|
|
334
|
+
* Struct schemas can be used to describe uniform and storage buffers,
|
|
335
|
+
* whereas unstruct schemas cannot.
|
|
336
|
+
*
|
|
337
|
+
* @example
|
|
338
|
+
* isWgslStruct(d.struct({ a: d.u32 })) // true
|
|
339
|
+
* isWgslStruct(d.unstruct({ a: d.u32 })) // false
|
|
340
|
+
* isWgslStruct(d.vec3f) // false
|
|
341
|
+
*/
|
|
342
|
+
function isWgslStruct(schema) {
|
|
343
|
+
return isMarkedInternal(schema) && schema?.type === "struct";
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* Checks whether passed in value is a pointer schema.
|
|
347
|
+
*
|
|
348
|
+
* @example
|
|
349
|
+
* isPtr(d.ptrFn(d.f32)) // true
|
|
350
|
+
* isPtr(d.ptrPrivate(d.f32)) // true
|
|
351
|
+
* isPtr(d.f32) // false
|
|
352
|
+
*/
|
|
353
|
+
function isPtr(schema) {
|
|
354
|
+
return isMarkedInternal(schema) && schema?.type === "ptr";
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* Checks whether the passed in value is an atomic schema.
|
|
358
|
+
*
|
|
359
|
+
* @example
|
|
360
|
+
* isAtomic(d.atomic(d.u32)) // true
|
|
361
|
+
* isAtomic(d.u32) // false
|
|
362
|
+
*/
|
|
363
|
+
function isAtomic(schema) {
|
|
364
|
+
return isMarkedInternal(schema) && schema?.type === "atomic";
|
|
365
|
+
}
|
|
366
|
+
function isAlignAttrib(value) {
|
|
367
|
+
return isMarkedInternal(value) && value?.type === "@align";
|
|
368
|
+
}
|
|
369
|
+
function isSizeAttrib(value) {
|
|
370
|
+
return isMarkedInternal(value) && value?.type === "@size";
|
|
371
|
+
}
|
|
372
|
+
function isLocationAttrib(value) {
|
|
373
|
+
return isMarkedInternal(value) && value?.type === "@location";
|
|
374
|
+
}
|
|
375
|
+
function isInterpolateAttrib(value) {
|
|
376
|
+
return isMarkedInternal(value) && value?.type === "@interpolate";
|
|
377
|
+
}
|
|
378
|
+
function isBuiltinAttrib(value) {
|
|
379
|
+
return isMarkedInternal(value) && value?.type === "@builtin";
|
|
380
|
+
}
|
|
381
|
+
function isDecorated(value) {
|
|
382
|
+
return isMarkedInternal(value) && value?.type === "decorated";
|
|
383
|
+
}
|
|
384
|
+
function isVoid(value) {
|
|
385
|
+
return isMarkedInternal(value) && value.type === "void";
|
|
386
|
+
}
|
|
387
|
+
function isNumericSchema(schema) {
|
|
388
|
+
const type = schema?.type;
|
|
389
|
+
return isMarkedInternal(schema) && (type === "abstractInt" || type === "abstractFloat" || type === "f32" || type === "f16" || type === "i32" || type === "u32");
|
|
390
|
+
}
|
|
391
|
+
function isHalfPrecisionSchema(schema) {
|
|
392
|
+
const type = schema?.type;
|
|
393
|
+
return isMarkedInternal(schema) && (type === "f16" || type === "vec2h" || type === "vec3h" || type === "vec4h");
|
|
394
|
+
}
|
|
395
|
+
const ephemeralTypes = [
|
|
396
|
+
"abstractInt",
|
|
397
|
+
"abstractFloat",
|
|
398
|
+
"f32",
|
|
399
|
+
"f16",
|
|
400
|
+
"i32",
|
|
401
|
+
"u32",
|
|
402
|
+
"bool"
|
|
403
|
+
];
|
|
404
|
+
/**
|
|
405
|
+
* Returns true for schemas that are not naturally referential in JS (primitives).
|
|
406
|
+
* @param schema
|
|
407
|
+
* @returns
|
|
408
|
+
*/
|
|
409
|
+
function isNaturallyEphemeral(schema) {
|
|
410
|
+
return !isMarkedInternal(schema) || ephemeralTypes.includes(schema?.type);
|
|
411
|
+
}
|
|
412
|
+
function WORKAROUND_getSchema(vec) {
|
|
413
|
+
return vec.schema;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
//#endregion
|
|
417
|
+
//#region src/data/dataTypes.ts
|
|
418
|
+
/**
|
|
419
|
+
* Runtime function to extract the inner data type from decorated types.
|
|
420
|
+
* If the data is not decorated, returns the data as-is.
|
|
421
|
+
*/
|
|
422
|
+
function undecorate(data) {
|
|
423
|
+
if (isDecorated(data) || isLooseDecorated(data)) return data.inner;
|
|
424
|
+
return data;
|
|
425
|
+
}
|
|
426
|
+
function unptr(data) {
|
|
427
|
+
if (isPtr(data)) return data.inner;
|
|
428
|
+
return data;
|
|
429
|
+
}
|
|
430
|
+
const looseTypeLiterals = [
|
|
431
|
+
"unstruct",
|
|
432
|
+
"disarray",
|
|
433
|
+
"loose-decorated",
|
|
434
|
+
...vertexFormats
|
|
435
|
+
];
|
|
436
|
+
function isLooseData(data) {
|
|
437
|
+
return isMarkedInternal(data) && looseTypeLiterals.includes(data?.type);
|
|
438
|
+
}
|
|
439
|
+
/**
|
|
440
|
+
* Checks whether the passed in value is a disarray schema,
|
|
441
|
+
* as opposed to, e.g., a regular array schema.
|
|
442
|
+
*
|
|
443
|
+
* Array schemas can be used to describe uniform and storage buffers,
|
|
444
|
+
* whereas disarray schemas cannot. Disarrays are useful for
|
|
445
|
+
* defining vertex buffers instead.
|
|
446
|
+
*
|
|
447
|
+
* @example
|
|
448
|
+
* isDisarray(d.arrayOf(d.u32, 4)) // false
|
|
449
|
+
* isDisarray(d.disarrayOf(d.u32, 4)) // true
|
|
450
|
+
* isDisarray(d.vec3f) // false
|
|
451
|
+
*/
|
|
452
|
+
function isDisarray(schema) {
|
|
453
|
+
return isMarkedInternal(schema) && schema?.type === "disarray";
|
|
454
|
+
}
|
|
455
|
+
/**
|
|
456
|
+
* Checks whether passed in value is a unstruct schema,
|
|
457
|
+
* as opposed to, e.g., a struct schema.
|
|
458
|
+
*
|
|
459
|
+
* Struct schemas can be used to describe uniform and storage buffers,
|
|
460
|
+
* whereas unstruct schemas cannot. Unstructs are useful for
|
|
461
|
+
* defining vertex buffers instead.
|
|
462
|
+
*
|
|
463
|
+
* @example
|
|
464
|
+
* isUnstruct(d.struct({ a: d.u32 })) // false
|
|
465
|
+
* isUnstruct(d.unstruct({ a: d.u32 })) // true
|
|
466
|
+
* isUnstruct(d.vec3f) // false
|
|
467
|
+
*/
|
|
468
|
+
function isUnstruct(schema) {
|
|
469
|
+
return isMarkedInternal(schema) && schema?.type === "unstruct";
|
|
470
|
+
}
|
|
471
|
+
function isLooseDecorated(value) {
|
|
472
|
+
return isMarkedInternal(value) && value?.type === "loose-decorated";
|
|
473
|
+
}
|
|
474
|
+
function getCustomAlignment(data) {
|
|
475
|
+
return data.attribs?.find(isAlignAttrib)?.params[0];
|
|
476
|
+
}
|
|
477
|
+
function getCustomSize(data) {
|
|
478
|
+
return data.attribs?.find(isSizeAttrib)?.params[0];
|
|
479
|
+
}
|
|
480
|
+
function getCustomLocation(data) {
|
|
481
|
+
return data.attribs?.find(isLocationAttrib)?.params[0];
|
|
482
|
+
}
|
|
483
|
+
function isData(value) {
|
|
484
|
+
return isWgslData(value) || isLooseData(value);
|
|
485
|
+
}
|
|
486
|
+
const UnknownData = Symbol("UNKNOWN");
|
|
487
|
+
var InfixDispatch = class {
|
|
488
|
+
constructor(name, lhs, operator) {
|
|
489
|
+
this.name = name;
|
|
490
|
+
this.lhs = lhs;
|
|
491
|
+
this.operator = operator;
|
|
492
|
+
}
|
|
493
|
+
};
|
|
494
|
+
var MatrixColumnsAccess = class {
|
|
495
|
+
constructor(matrix) {
|
|
496
|
+
this.matrix = matrix;
|
|
497
|
+
}
|
|
498
|
+
};
|
|
499
|
+
var ConsoleLog = class {
|
|
500
|
+
[$internal] = true;
|
|
501
|
+
constructor(op) {
|
|
502
|
+
this.op = op;
|
|
503
|
+
setName(this, "consoleLog");
|
|
504
|
+
}
|
|
505
|
+
};
|
|
506
|
+
|
|
507
|
+
//#endregion
|
|
508
|
+
//#region src/data/snippet.ts
|
|
509
|
+
function isEphemeralOrigin(space) {
|
|
510
|
+
return space === "runtime" || space === "constant" || space === "argument";
|
|
511
|
+
}
|
|
512
|
+
function isEphemeralSnippet(snippet) {
|
|
513
|
+
return isEphemeralOrigin(snippet.origin);
|
|
514
|
+
}
|
|
515
|
+
const originToPtrParams = {
|
|
516
|
+
uniform: {
|
|
517
|
+
space: "uniform",
|
|
518
|
+
access: "read"
|
|
519
|
+
},
|
|
520
|
+
readonly: {
|
|
521
|
+
space: "storage",
|
|
522
|
+
access: "read"
|
|
523
|
+
},
|
|
524
|
+
mutable: {
|
|
525
|
+
space: "storage",
|
|
526
|
+
access: "read-write"
|
|
527
|
+
},
|
|
528
|
+
workgroup: {
|
|
529
|
+
space: "workgroup",
|
|
530
|
+
access: "read-write"
|
|
531
|
+
},
|
|
532
|
+
private: {
|
|
533
|
+
space: "private",
|
|
534
|
+
access: "read-write"
|
|
535
|
+
},
|
|
536
|
+
function: {
|
|
537
|
+
space: "function",
|
|
538
|
+
access: "read-write"
|
|
539
|
+
},
|
|
540
|
+
"this-function": {
|
|
541
|
+
space: "function",
|
|
542
|
+
access: "read-write"
|
|
543
|
+
}
|
|
544
|
+
};
|
|
545
|
+
var SnippetImpl = class {
|
|
546
|
+
constructor(value, dataType, origin) {
|
|
547
|
+
this.value = value;
|
|
548
|
+
this.dataType = dataType;
|
|
549
|
+
this.origin = origin;
|
|
550
|
+
}
|
|
551
|
+
};
|
|
552
|
+
function isSnippet(value) {
|
|
553
|
+
return value instanceof SnippetImpl;
|
|
554
|
+
}
|
|
555
|
+
function isSnippetNumeric(snippet) {
|
|
556
|
+
return isNumericSchema(snippet.dataType);
|
|
557
|
+
}
|
|
558
|
+
function snip(value, dataType, origin) {
|
|
559
|
+
if (DEV && isSnippet(value)) throw new Error("Cannot nest snippets");
|
|
560
|
+
return new SnippetImpl(value, undecorate(dataType), origin);
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
//#endregion
|
|
564
|
+
//#region src/errors.ts
|
|
565
|
+
const prefix = "Invariant failed";
|
|
566
|
+
/**
|
|
567
|
+
* Inspired by: https://github.com/alexreardon/tiny-invariant/blob/master/src/tiny-invariant.ts
|
|
568
|
+
*/
|
|
569
|
+
function invariant(condition, message) {
|
|
570
|
+
if (condition) return;
|
|
571
|
+
if (!DEV && !TEST) throw new Error(prefix);
|
|
572
|
+
const provided = typeof message === "function" ? message() : message;
|
|
573
|
+
const value = provided ? `${prefix}: ${provided}` : prefix;
|
|
574
|
+
throw new Error(value);
|
|
575
|
+
}
|
|
576
|
+
/**
|
|
577
|
+
* An error that happens during resolution of WGSL code.
|
|
578
|
+
* Contains a trace of all ancestor resolvables in
|
|
579
|
+
* which this error originated.
|
|
580
|
+
*
|
|
581
|
+
* @category Errors
|
|
582
|
+
*/
|
|
583
|
+
var ResolutionError = class ResolutionError extends Error {
|
|
584
|
+
constructor(cause, trace) {
|
|
585
|
+
let entries = trace.map((ancestor) => `- ${hasTinyestMetadata(ancestor) ? `fn*:${getName(ancestor)}` : ancestor}`);
|
|
586
|
+
if (entries.length > 20) entries = [
|
|
587
|
+
...entries.slice(0, 11),
|
|
588
|
+
"...",
|
|
589
|
+
...entries.slice(-10)
|
|
590
|
+
];
|
|
591
|
+
super(`Resolution of the following tree failed:\n${entries.join("\n")}: ${cause && typeof cause === "object" && "message" in cause ? cause.message : cause}`);
|
|
592
|
+
this.cause = cause;
|
|
593
|
+
this.trace = trace;
|
|
594
|
+
Object.setPrototypeOf(this, ResolutionError.prototype);
|
|
595
|
+
}
|
|
596
|
+
appendToTrace(ancestor) {
|
|
597
|
+
const newTrace = [ancestor, ...this.trace];
|
|
598
|
+
return new ResolutionError(this.cause, newTrace);
|
|
599
|
+
}
|
|
600
|
+
};
|
|
601
|
+
/**
|
|
602
|
+
* An error that happens during execution of TypeGPU functions.
|
|
603
|
+
* Contains a trace of all TypeGPU functions called along the way.
|
|
604
|
+
*
|
|
605
|
+
* @category Errors
|
|
606
|
+
*/
|
|
607
|
+
var ExecutionError = class ExecutionError extends Error {
|
|
608
|
+
constructor(cause, trace) {
|
|
609
|
+
let entries = trace.map((ancestor) => `- ${ancestor}`);
|
|
610
|
+
if (entries.length > 20) entries = [
|
|
611
|
+
...entries.slice(0, 11),
|
|
612
|
+
"...",
|
|
613
|
+
...entries.slice(-10)
|
|
614
|
+
];
|
|
615
|
+
super(`Execution of the following tree failed:\n${entries.join("\n")}: ${cause && typeof cause === "object" && "message" in cause ? cause.message : cause}`);
|
|
616
|
+
this.cause = cause;
|
|
617
|
+
this.trace = trace;
|
|
618
|
+
Object.setPrototypeOf(this, ExecutionError.prototype);
|
|
619
|
+
}
|
|
620
|
+
appendToTrace(ancestor) {
|
|
621
|
+
const newTrace = [ancestor, ...this.trace];
|
|
622
|
+
return new ExecutionError(this.cause, newTrace);
|
|
623
|
+
}
|
|
624
|
+
};
|
|
625
|
+
/**
|
|
626
|
+
* @category Errors
|
|
627
|
+
*/
|
|
628
|
+
var MissingSlotValueError = class MissingSlotValueError extends Error {
|
|
629
|
+
constructor(slot) {
|
|
630
|
+
super(`Missing value for '${slot}'`);
|
|
631
|
+
this.slot = slot;
|
|
632
|
+
Object.setPrototypeOf(this, MissingSlotValueError.prototype);
|
|
633
|
+
}
|
|
634
|
+
};
|
|
635
|
+
/**
|
|
636
|
+
* @category Errors
|
|
637
|
+
*/
|
|
638
|
+
var NotUniformError = class NotUniformError extends Error {
|
|
639
|
+
constructor(value) {
|
|
640
|
+
super(`Buffer '${getName(value) ?? "<unnamed>"}' is not bindable as a uniform. Use .$usage('uniform') to allow it.`);
|
|
641
|
+
Object.setPrototypeOf(this, NotUniformError.prototype);
|
|
642
|
+
}
|
|
643
|
+
};
|
|
644
|
+
var MissingLinksError = class MissingLinksError extends Error {
|
|
645
|
+
constructor(fnLabel, externalNames) {
|
|
646
|
+
super(`The function '${fnLabel ?? "<unnamed>"}' is missing links to the following external values: ${externalNames}.`);
|
|
647
|
+
Object.setPrototypeOf(this, MissingLinksError.prototype);
|
|
648
|
+
}
|
|
649
|
+
};
|
|
650
|
+
var MissingBindGroupsError = class MissingBindGroupsError extends Error {
|
|
651
|
+
constructor(layouts) {
|
|
652
|
+
super(`Missing bind groups for layouts: '${[...layouts].map((layout) => getName(layout) ?? "<unnamed>").join(", ")}'. Please provide it using pipeline.with(bindGroup).(...)`);
|
|
653
|
+
Object.setPrototypeOf(this, MissingBindGroupsError.prototype);
|
|
654
|
+
}
|
|
655
|
+
};
|
|
656
|
+
var MissingVertexBuffersError = class MissingVertexBuffersError extends Error {
|
|
657
|
+
constructor(layouts) {
|
|
658
|
+
super(`Missing vertex buffers for layouts: '${[...layouts].map((layout) => getName(layout) ?? "<unnamed>").join(", ")}'. Please provide it using pipeline.with(layout, buffer).(...)`);
|
|
659
|
+
Object.setPrototypeOf(this, MissingVertexBuffersError.prototype);
|
|
660
|
+
}
|
|
661
|
+
};
|
|
662
|
+
var IllegalVarAccessError = class IllegalVarAccessError extends Error {
|
|
663
|
+
constructor(msg) {
|
|
664
|
+
super(msg);
|
|
665
|
+
Object.setPrototypeOf(this, IllegalVarAccessError.prototype);
|
|
666
|
+
}
|
|
667
|
+
};
|
|
668
|
+
var IllegalBufferAccessError = class IllegalBufferAccessError extends Error {
|
|
669
|
+
constructor(msg) {
|
|
670
|
+
super(msg);
|
|
671
|
+
Object.setPrototypeOf(this, IllegalBufferAccessError.prototype);
|
|
672
|
+
}
|
|
673
|
+
};
|
|
674
|
+
var WgslTypeError = class WgslTypeError extends Error {
|
|
675
|
+
constructor(msg) {
|
|
676
|
+
super(msg);
|
|
677
|
+
Object.setPrototypeOf(this, WgslTypeError.prototype);
|
|
678
|
+
}
|
|
679
|
+
};
|
|
680
|
+
var SignatureNotSupportedError = class SignatureNotSupportedError extends Error {
|
|
681
|
+
constructor(actual, candidates) {
|
|
682
|
+
super(`Unsupported data types: ${actual.map((a) => a.type).join(", ")}. Supported types are: ${candidates.map((r) => r.type).join(", ")}.`);
|
|
683
|
+
Object.setPrototypeOf(this, SignatureNotSupportedError.prototype);
|
|
684
|
+
}
|
|
685
|
+
};
|
|
686
|
+
|
|
687
|
+
//#endregion
|
|
688
|
+
//#region src/core/slot/slotTypes.ts
|
|
689
|
+
function isSlot(value) {
|
|
690
|
+
return value?.resourceType === "slot";
|
|
691
|
+
}
|
|
692
|
+
function isLazy(value) {
|
|
693
|
+
return value?.resourceType === "lazy";
|
|
694
|
+
}
|
|
695
|
+
function isProviding(value) {
|
|
696
|
+
return value?.[$providing] !== void 0;
|
|
697
|
+
}
|
|
698
|
+
function isAccessor(value) {
|
|
699
|
+
return value?.resourceType === "accessor";
|
|
700
|
+
}
|
|
701
|
+
function isMutableAccessor(value) {
|
|
702
|
+
return value?.resourceType === "mutable-accessor";
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
//#endregion
|
|
706
|
+
//#region src/types.ts
|
|
707
|
+
var NormalState = class {
|
|
708
|
+
type = "normal";
|
|
709
|
+
};
|
|
710
|
+
var CodegenState = class {
|
|
711
|
+
type = "codegen";
|
|
712
|
+
};
|
|
713
|
+
var SimulationState = class {
|
|
714
|
+
type = "simulate";
|
|
715
|
+
constructor(buffers, vars) {
|
|
716
|
+
this.buffers = buffers;
|
|
717
|
+
this.vars = vars;
|
|
718
|
+
}
|
|
719
|
+
};
|
|
720
|
+
function isSelfResolvable(value) {
|
|
721
|
+
return !!value?.[$resolve];
|
|
722
|
+
}
|
|
723
|
+
function getOwnSnippet(value) {
|
|
724
|
+
return value?.[$ownSnippet];
|
|
725
|
+
}
|
|
726
|
+
function isGPUCallable(value) {
|
|
727
|
+
return !!value?.[$gpuCallable];
|
|
728
|
+
}
|
|
729
|
+
function hasCast(value) {
|
|
730
|
+
return !!value?.[$cast];
|
|
731
|
+
}
|
|
732
|
+
function isKnownAtComptime(snippet) {
|
|
733
|
+
return (typeof snippet.value !== "string" || snippet.dataType === UnknownData) && getOwnSnippet(snippet.value) === void 0;
|
|
734
|
+
}
|
|
735
|
+
function isWgsl(value) {
|
|
736
|
+
return typeof value === "number" || typeof value === "boolean" || typeof value === "string" || isSelfResolvable(value) || isWgslData(value) || isSlot(value) || isLazy(value) || isProviding(value);
|
|
737
|
+
}
|
|
738
|
+
function isGPUBuffer(value) {
|
|
739
|
+
return !!value && typeof value === "object" && "getMappedRange" in value && "mapAsync" in value;
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
//#endregion
|
|
743
|
+
//#region src/execMode.ts
|
|
744
|
+
/**
|
|
745
|
+
* Used to track if the code we're currently
|
|
746
|
+
* executing is inside an executing TypeGPU function.
|
|
747
|
+
*
|
|
748
|
+
* Helpful for providing better error messages.
|
|
749
|
+
*/
|
|
750
|
+
let insideTgpuFn = false;
|
|
751
|
+
function provideInsideTgpuFn(callback) {
|
|
752
|
+
if (insideTgpuFn) return callback();
|
|
753
|
+
try {
|
|
754
|
+
insideTgpuFn = true;
|
|
755
|
+
return callback();
|
|
756
|
+
} finally {
|
|
757
|
+
insideTgpuFn = false;
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
function isInsideTgpuFn() {
|
|
761
|
+
return insideTgpuFn;
|
|
762
|
+
}
|
|
763
|
+
let resolutionCtx;
|
|
764
|
+
function provideCtx(ctx, callback) {
|
|
765
|
+
invariant(resolutionCtx === void 0 || resolutionCtx === ctx, "Cannot nest context providers");
|
|
766
|
+
if (resolutionCtx === ctx) return callback();
|
|
767
|
+
resolutionCtx = ctx;
|
|
768
|
+
try {
|
|
769
|
+
return callback();
|
|
770
|
+
} finally {
|
|
771
|
+
resolutionCtx = void 0;
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
function getResolutionCtx() {
|
|
775
|
+
return resolutionCtx;
|
|
776
|
+
}
|
|
777
|
+
/**
|
|
778
|
+
* Applicable when code is being executed outside of any
|
|
779
|
+
* execution-altering APIs.
|
|
780
|
+
*/
|
|
781
|
+
const topLevelState = new NormalState();
|
|
782
|
+
function getExecMode() {
|
|
783
|
+
return resolutionCtx?.mode ?? topLevelState;
|
|
784
|
+
}
|
|
785
|
+
function inCodegenMode() {
|
|
786
|
+
return resolutionCtx?.mode.type === "codegen";
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
//#endregion
|
|
790
|
+
//#region src/core/resolve/stitch.ts
|
|
791
|
+
/**
|
|
792
|
+
* "The reverse of snipping"
|
|
793
|
+
* Injects resolved snippets into a template string.
|
|
794
|
+
*/
|
|
795
|
+
function stitch(strings, ...snippets) {
|
|
796
|
+
const ctx = getResolutionCtx();
|
|
797
|
+
function resolveSnippet(maybeSnippet) {
|
|
798
|
+
return isSnippet(maybeSnippet) ? ctx.resolve(maybeSnippet.value, maybeSnippet.dataType).value : maybeSnippet;
|
|
799
|
+
}
|
|
800
|
+
let result = "";
|
|
801
|
+
for (let i = 0; i < strings.length; ++i) {
|
|
802
|
+
result += strings[i];
|
|
803
|
+
const snippet = snippets[i];
|
|
804
|
+
if (Array.isArray(snippet)) result += snippet.filter((s) => s !== void 0).map(resolveSnippet).join(", ");
|
|
805
|
+
else if (snippet) result += resolveSnippet(snippet);
|
|
806
|
+
}
|
|
807
|
+
return result;
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
//#endregion
|
|
811
|
+
//#region src/data/ptr.ts
|
|
812
|
+
function ptrFn(inner) {
|
|
813
|
+
return INTERNAL_createPtr("function", inner, "read-write");
|
|
814
|
+
}
|
|
815
|
+
function ptrPrivate(inner) {
|
|
816
|
+
return INTERNAL_createPtr("private", inner, "read-write");
|
|
817
|
+
}
|
|
818
|
+
function ptrWorkgroup(inner) {
|
|
819
|
+
return INTERNAL_createPtr("workgroup", inner, "read-write");
|
|
820
|
+
}
|
|
821
|
+
function ptrStorage(inner, access = "read") {
|
|
822
|
+
return INTERNAL_createPtr("storage", inner, access);
|
|
823
|
+
}
|
|
824
|
+
function ptrUniform(inner) {
|
|
825
|
+
return INTERNAL_createPtr("uniform", inner, "read");
|
|
826
|
+
}
|
|
827
|
+
function ptrHandle(inner) {
|
|
828
|
+
return INTERNAL_createPtr("handle", inner, "read");
|
|
829
|
+
}
|
|
830
|
+
function INTERNAL_createPtr(addressSpace, inner, access, implicit = false) {
|
|
831
|
+
return {
|
|
832
|
+
[$internal]: {},
|
|
833
|
+
type: "ptr",
|
|
834
|
+
addressSpace,
|
|
835
|
+
inner,
|
|
836
|
+
access,
|
|
837
|
+
implicit,
|
|
838
|
+
toString: () => `ptr<${addressSpace}, ${inner}, ${access}>`
|
|
839
|
+
};
|
|
840
|
+
}
|
|
841
|
+
function createPtrFromOrigin(origin, innerDataType) {
|
|
842
|
+
const ptrParams = originToPtrParams[origin];
|
|
843
|
+
if (ptrParams) return INTERNAL_createPtr(ptrParams.space, innerDataType, ptrParams.access);
|
|
844
|
+
}
|
|
845
|
+
function implicitFrom(ptr) {
|
|
846
|
+
return INTERNAL_createPtr(ptr.addressSpace, ptr.inner, ptr.access, true);
|
|
847
|
+
}
|
|
848
|
+
function explicitFrom(ptr) {
|
|
849
|
+
return INTERNAL_createPtr(ptr.addressSpace, ptr.inner, ptr.access, false);
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
//#endregion
|
|
853
|
+
//#region src/data/ref.ts
|
|
854
|
+
const _ref = (() => {
|
|
855
|
+
const impl = ((value) => INTERNAL_createRef(value));
|
|
856
|
+
setName(impl, "ref");
|
|
857
|
+
impl.toString = () => "ref";
|
|
858
|
+
impl[$internal] = true;
|
|
859
|
+
impl[$gpuCallable] = { call(_ctx, [value]) {
|
|
860
|
+
if (value.origin === "argument") throw new WgslTypeError(stitch`d.ref(${value}) is illegal, cannot take a reference of an argument. Copy the value locally first, and take a reference of the copy.`);
|
|
861
|
+
if (isPtr(value.dataType)) return snip(value.value, explicitFrom(value.dataType), value.origin);
|
|
862
|
+
/**
|
|
863
|
+
* Pointer type only exists if the ref was created from a reference (buttery-butter).
|
|
864
|
+
*
|
|
865
|
+
* @example
|
|
866
|
+
* ```ts
|
|
867
|
+
* const life = ref(42); // created from a value
|
|
868
|
+
* const boid = ref(layout.$.boids[0]); // created from a reference
|
|
869
|
+
* ```
|
|
870
|
+
*/
|
|
871
|
+
const ptrType = createPtrFromOrigin(value.origin, value.dataType);
|
|
872
|
+
return snip(new RefOperator(value, ptrType), ptrType ?? UnknownData, "runtime");
|
|
873
|
+
} };
|
|
874
|
+
return impl;
|
|
875
|
+
})();
|
|
876
|
+
function isRef(value) {
|
|
877
|
+
return value?.[$internal]?.type === "ref";
|
|
878
|
+
}
|
|
879
|
+
function INTERNAL_createRef(value) {
|
|
880
|
+
const target = {
|
|
881
|
+
[$internal]: { type: "ref" },
|
|
882
|
+
get $() {
|
|
883
|
+
return value;
|
|
884
|
+
},
|
|
885
|
+
set $(newValue) {
|
|
886
|
+
if (newValue && typeof newValue === "object") for (const key of Object.keys(newValue)) value[key] = newValue[key];
|
|
887
|
+
else value = newValue;
|
|
888
|
+
}
|
|
889
|
+
};
|
|
890
|
+
if (value === void 0 || value === null) throw new Error("Cannot create a ref from undefined or null");
|
|
891
|
+
if (typeof value === "object") return new Proxy(target, {
|
|
892
|
+
get(target, prop) {
|
|
893
|
+
if (prop in target) return target[prop];
|
|
894
|
+
return value[prop];
|
|
895
|
+
},
|
|
896
|
+
set(_target, prop, propValue) {
|
|
897
|
+
if (prop === $internal) return false;
|
|
898
|
+
if (prop === "$") {
|
|
899
|
+
console.log("Setting ref value:", propValue);
|
|
900
|
+
return Reflect.set(target, prop, propValue);
|
|
901
|
+
}
|
|
902
|
+
return Reflect.set(value, prop, propValue);
|
|
903
|
+
}
|
|
904
|
+
});
|
|
905
|
+
return target;
|
|
906
|
+
}
|
|
907
|
+
/**
|
|
908
|
+
* The result of calling `d.ref(...)`. The code responsible for
|
|
909
|
+
* generating shader code can check if the value of a snippet is
|
|
910
|
+
* an instance of `RefOperator`, and act accordingly.
|
|
911
|
+
*/
|
|
912
|
+
var RefOperator = class {
|
|
913
|
+
[$internal];
|
|
914
|
+
snippet;
|
|
915
|
+
#ptrType;
|
|
916
|
+
constructor(snippet, ptrType) {
|
|
917
|
+
this[$internal] = true;
|
|
918
|
+
this.snippet = snippet;
|
|
919
|
+
this.#ptrType = ptrType;
|
|
920
|
+
}
|
|
921
|
+
get [$ownSnippet]() {
|
|
922
|
+
if (!this.#ptrType) throw new Error(stitch`Cannot take a reference of ${this.snippet}`);
|
|
923
|
+
return snip(this, this.#ptrType, this.snippet.origin);
|
|
924
|
+
}
|
|
925
|
+
[$resolve]() {
|
|
926
|
+
if (!this.#ptrType) throw new Error(stitch`Cannot take a reference of ${this.snippet}`);
|
|
927
|
+
return snip(stitch`(&${this.snippet})`, this.#ptrType, this.snippet.origin);
|
|
928
|
+
}
|
|
929
|
+
};
|
|
930
|
+
function derefSnippet(snippet) {
|
|
931
|
+
if (!isPtr(snippet.dataType)) return snippet;
|
|
932
|
+
const innerType = snippet.dataType.inner;
|
|
933
|
+
const origin = isNaturallyEphemeral(innerType) ? "runtime" : snippet.origin;
|
|
934
|
+
if (snippet.value instanceof RefOperator) return snip(stitch`${snippet.value.snippet}`, innerType, origin);
|
|
935
|
+
return snip(stitch`(*${snippet})`, innerType, origin);
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
//#endregion
|
|
939
|
+
//#region src/data/schemaCallWrapper.ts
|
|
940
|
+
/**
|
|
941
|
+
* A wrapper for `schema(item)` or `schema()` call on JS side.
|
|
942
|
+
* If the schema is a pointer, returns the value pointed to without copying.
|
|
943
|
+
* If the schema is a TgpuVertexFormatData, calls the corresponding constructible schema instead.
|
|
944
|
+
* If the schema is not callable, throws an error.
|
|
945
|
+
* Otherwise, returns `schema(item)` or `schema()`.
|
|
946
|
+
*/
|
|
947
|
+
function schemaCallWrapper(schema, item) {
|
|
948
|
+
const callSchema = schema;
|
|
949
|
+
if (hasCast(callSchema)) return callSchema[$cast](item);
|
|
950
|
+
if (typeof callSchema !== "function") return item;
|
|
951
|
+
return item === void 0 ? callSchema() : callSchema(item);
|
|
952
|
+
}
|
|
953
|
+
/**
|
|
954
|
+
* A wrapper for `schema(item)` or `schema()` call on the GPU side.
|
|
955
|
+
* If the schema is a pointer, returns the value pointed to without copying.
|
|
956
|
+
* If the schema is a TgpuVertexFormatData, calls the corresponding constructible schema instead.
|
|
957
|
+
* If the schema is not callable, throws an error.
|
|
958
|
+
* Otherwise, returns `schema(item)` or `schema()`.
|
|
959
|
+
*/
|
|
960
|
+
function schemaCallWrapperGPU(ctx, schema, item) {
|
|
961
|
+
if (!isGPUCallable(schema)) return item;
|
|
962
|
+
const callSchema = schema;
|
|
963
|
+
return item === void 0 || item.value === void 0 ? callSchema[$gpuCallable].call(ctx, []) : callSchema[$gpuCallable].call(ctx, [item]);
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
//#endregion
|
|
967
|
+
//#region src/shared/stringify.ts
|
|
968
|
+
function safeStringify(item) {
|
|
969
|
+
const asString = String(item);
|
|
970
|
+
if (asString !== "[object Object]") return asString;
|
|
971
|
+
try {
|
|
972
|
+
return JSON.stringify(item);
|
|
973
|
+
} catch (error) {
|
|
974
|
+
console.error("Error parsing JSON:", error);
|
|
975
|
+
return "<invalid json>";
|
|
976
|
+
}
|
|
977
|
+
}
|
|
978
|
+
function niceStringify(item) {
|
|
979
|
+
if (isVecInstance(item) || isMatInstance(item)) return item.toString();
|
|
980
|
+
if (Array.isArray(item)) return `[${item.map(niceStringify).join(", ")}]`;
|
|
981
|
+
if (item && typeof item === "object") return `{ ${Object.entries(item).map(([key, value]) => `${key}: ${niceStringify(value)}`).join(", ")} }`;
|
|
982
|
+
return String(item);
|
|
983
|
+
}
|
|
984
|
+
|
|
985
|
+
//#endregion
|
|
986
|
+
//#region src/shared/utilityTypes.ts
|
|
987
|
+
function assertExhaustive(x, location) {
|
|
988
|
+
throw new Error(`Failed to handle ${x} at ${location}`);
|
|
989
|
+
}
|
|
990
|
+
|
|
991
|
+
//#endregion
|
|
992
|
+
//#region src/tgsl/conversion.ts
|
|
993
|
+
const INFINITE_RANK = {
|
|
994
|
+
rank: Number.POSITIVE_INFINITY,
|
|
995
|
+
action: "none"
|
|
996
|
+
};
|
|
997
|
+
function getAutoConversionRank(src, dest) {
|
|
998
|
+
const trueSrc = undecorate(src);
|
|
999
|
+
const trueDst = undecorate(dest);
|
|
1000
|
+
if (trueSrc.type === trueDst.type) return {
|
|
1001
|
+
rank: 0,
|
|
1002
|
+
action: "none"
|
|
1003
|
+
};
|
|
1004
|
+
if (trueSrc.type === "abstractFloat") {
|
|
1005
|
+
if (trueDst.type === "f32") return {
|
|
1006
|
+
rank: 1,
|
|
1007
|
+
action: "none"
|
|
1008
|
+
};
|
|
1009
|
+
if (trueDst.type === "f16") return {
|
|
1010
|
+
rank: 2,
|
|
1011
|
+
action: "none"
|
|
1012
|
+
};
|
|
1013
|
+
}
|
|
1014
|
+
if (trueSrc.type === "abstractInt") {
|
|
1015
|
+
if (trueDst.type === "i32") return {
|
|
1016
|
+
rank: 3,
|
|
1017
|
+
action: "none"
|
|
1018
|
+
};
|
|
1019
|
+
if (trueDst.type === "u32") return {
|
|
1020
|
+
rank: 4,
|
|
1021
|
+
action: "none"
|
|
1022
|
+
};
|
|
1023
|
+
if (trueDst.type === "abstractFloat") return {
|
|
1024
|
+
rank: 5,
|
|
1025
|
+
action: "none"
|
|
1026
|
+
};
|
|
1027
|
+
if (trueDst.type === "f32") return {
|
|
1028
|
+
rank: 6,
|
|
1029
|
+
action: "none"
|
|
1030
|
+
};
|
|
1031
|
+
if (trueDst.type === "f16") return {
|
|
1032
|
+
rank: 7,
|
|
1033
|
+
action: "none"
|
|
1034
|
+
};
|
|
1035
|
+
}
|
|
1036
|
+
if (isVec(trueSrc) && isVec(trueDst) && trueSrc.type[3] === trueDst.type[3]) return getAutoConversionRank(trueSrc.primitive, trueDst.primitive);
|
|
1037
|
+
if (isMat(trueSrc) && isMat(trueDst) && trueSrc.type[3] === trueDst.type[3]) return {
|
|
1038
|
+
rank: 0,
|
|
1039
|
+
action: "none"
|
|
1040
|
+
};
|
|
1041
|
+
return INFINITE_RANK;
|
|
1042
|
+
}
|
|
1043
|
+
function getImplicitConversionRank(src, dest) {
|
|
1044
|
+
const trueSrc = undecorate(src);
|
|
1045
|
+
const trueDst = undecorate(dest);
|
|
1046
|
+
if (isPtr(trueSrc) && trueSrc.implicit && getAutoConversionRank(trueSrc.inner, trueDst).rank < Number.POSITIVE_INFINITY) return {
|
|
1047
|
+
rank: 0,
|
|
1048
|
+
action: "deref"
|
|
1049
|
+
};
|
|
1050
|
+
if (isPtr(trueDst) && getAutoConversionRank(trueSrc, trueDst.inner).rank < Number.POSITIVE_INFINITY) return {
|
|
1051
|
+
rank: 1,
|
|
1052
|
+
action: "ref"
|
|
1053
|
+
};
|
|
1054
|
+
const primitivePreference = {
|
|
1055
|
+
f32: 0,
|
|
1056
|
+
f16: 1,
|
|
1057
|
+
i32: 2,
|
|
1058
|
+
u32: 3,
|
|
1059
|
+
bool: 4
|
|
1060
|
+
};
|
|
1061
|
+
if (trueSrc.type in primitivePreference && trueDst.type in primitivePreference) {
|
|
1062
|
+
const srcType = trueSrc.type;
|
|
1063
|
+
const destType = trueDst.type;
|
|
1064
|
+
if (srcType !== destType) {
|
|
1065
|
+
const srcPref = primitivePreference[srcType];
|
|
1066
|
+
return {
|
|
1067
|
+
rank: primitivePreference[destType] < srcPref ? 10 : 20,
|
|
1068
|
+
action: "cast",
|
|
1069
|
+
targetType: trueDst
|
|
1070
|
+
};
|
|
1071
|
+
}
|
|
1072
|
+
}
|
|
1073
|
+
if (trueSrc.type === "abstractFloat") {
|
|
1074
|
+
if (trueDst.type === "u32") return {
|
|
1075
|
+
rank: 2,
|
|
1076
|
+
action: "cast",
|
|
1077
|
+
targetType: trueDst
|
|
1078
|
+
};
|
|
1079
|
+
if (trueDst.type === "i32") return {
|
|
1080
|
+
rank: 1,
|
|
1081
|
+
action: "cast",
|
|
1082
|
+
targetType: trueDst
|
|
1083
|
+
};
|
|
1084
|
+
}
|
|
1085
|
+
return INFINITE_RANK;
|
|
1086
|
+
}
|
|
1087
|
+
function getConversionRank(src, dest, allowImplicit) {
|
|
1088
|
+
const autoRank = getAutoConversionRank(src, dest);
|
|
1089
|
+
if (autoRank.rank < Number.POSITIVE_INFINITY) return autoRank;
|
|
1090
|
+
if (allowImplicit) return getImplicitConversionRank(src, dest);
|
|
1091
|
+
return INFINITE_RANK;
|
|
1092
|
+
}
|
|
1093
|
+
function findBestType(types, uniqueTypes, allowImplicit) {
|
|
1094
|
+
let bestResult;
|
|
1095
|
+
for (const targetType of uniqueTypes) {
|
|
1096
|
+
const details = [];
|
|
1097
|
+
let sum = 0;
|
|
1098
|
+
for (const sourceType of types) {
|
|
1099
|
+
const conversion = getConversionRank(sourceType, targetType, allowImplicit);
|
|
1100
|
+
sum += conversion.rank;
|
|
1101
|
+
if (conversion.rank === Number.POSITIVE_INFINITY) break;
|
|
1102
|
+
details.push(conversion);
|
|
1103
|
+
}
|
|
1104
|
+
if (sum < (bestResult?.sum ?? Number.POSITIVE_INFINITY)) bestResult = {
|
|
1105
|
+
type: targetType,
|
|
1106
|
+
details,
|
|
1107
|
+
sum
|
|
1108
|
+
};
|
|
1109
|
+
}
|
|
1110
|
+
if (!bestResult) return;
|
|
1111
|
+
const actions = bestResult.details.map((detail, index) => ({
|
|
1112
|
+
sourceIndex: index,
|
|
1113
|
+
action: detail.action,
|
|
1114
|
+
...detail.action === "cast" && { targetType: detail.targetType }
|
|
1115
|
+
}));
|
|
1116
|
+
return {
|
|
1117
|
+
targetType: bestResult.type,
|
|
1118
|
+
actions,
|
|
1119
|
+
hasImplicitConversions: actions.some((action) => action.action === "cast")
|
|
1120
|
+
};
|
|
1121
|
+
}
|
|
1122
|
+
function getBestConversion(types, targetTypes) {
|
|
1123
|
+
if (types.length === 0) return void 0;
|
|
1124
|
+
const uniqueTargetTypes = [...new Set((targetTypes || types).map(undecorate))];
|
|
1125
|
+
const explicitResult = findBestType(types, uniqueTargetTypes, false);
|
|
1126
|
+
if (explicitResult) return explicitResult;
|
|
1127
|
+
const implicitResult = findBestType(types, uniqueTargetTypes, true);
|
|
1128
|
+
if (implicitResult) return implicitResult;
|
|
1129
|
+
}
|
|
1130
|
+
function applyActionToSnippet(ctx, snippet, action, targetType) {
|
|
1131
|
+
if (action.action === "none") return snip(snippet.value, targetType, snippet.origin);
|
|
1132
|
+
switch (action.action) {
|
|
1133
|
+
case "ref": return snip(new RefOperator(snippet, targetType), targetType, snippet.origin);
|
|
1134
|
+
case "deref": return derefSnippet(snippet);
|
|
1135
|
+
case "cast": return schemaCallWrapperGPU(ctx, targetType, snippet);
|
|
1136
|
+
default: assertExhaustive(action.action, "applyActionToSnippet");
|
|
1137
|
+
}
|
|
1138
|
+
}
|
|
1139
|
+
function unify(inTypes, restrictTo) {
|
|
1140
|
+
if (inTypes.some((type) => type === UnknownData)) return;
|
|
1141
|
+
const conversion = getBestConversion(inTypes, restrictTo);
|
|
1142
|
+
if (!conversion) return;
|
|
1143
|
+
return inTypes.map((type) => isVec(type) || isMat(type) ? type : conversion.targetType);
|
|
1144
|
+
}
|
|
1145
|
+
function convertToCommonType(ctx, values, restrictTo, verbose = true) {
|
|
1146
|
+
const types = values.map((value) => value.dataType);
|
|
1147
|
+
if (types.some((type) => type === UnknownData)) return;
|
|
1148
|
+
if (DEV && Array.isArray(restrictTo) && restrictTo.length === 0) console.warn("convertToCommonType was called with an empty restrictTo array, which prevents any conversions from being made. If you intend to allow all conversions, pass undefined instead. If this was intended call the function conditionally since the result will always be undefined.");
|
|
1149
|
+
const conversion = getBestConversion(types, restrictTo);
|
|
1150
|
+
if (!conversion) return;
|
|
1151
|
+
if ((TEST || DEV) && verbose && conversion.hasImplicitConversions) console.warn(`Implicit conversions from [\n${values.map((v) => ` ${v.value}: ${safeStringify(v.dataType)}`).join(",\n")}\n] to ${conversion.targetType.type} are supported, but not recommended.
|
|
1152
|
+
Consider using explicit conversions instead.`);
|
|
1153
|
+
return values.map((value, index) => {
|
|
1154
|
+
const action = conversion.actions[index];
|
|
1155
|
+
invariant(action, "Action should not be undefined");
|
|
1156
|
+
return applyActionToSnippet(ctx, value, action, conversion.targetType);
|
|
1157
|
+
});
|
|
1158
|
+
}
|
|
1159
|
+
function tryConvertSnippet(ctx, snippet, targetDataTypes, verbose = true) {
|
|
1160
|
+
const targets = Array.isArray(targetDataTypes) ? targetDataTypes : [targetDataTypes];
|
|
1161
|
+
const { value, dataType, origin } = snippet;
|
|
1162
|
+
if (targets.length === 1) {
|
|
1163
|
+
const target = targets[0];
|
|
1164
|
+
if (target === dataType) return snip(value, target, origin);
|
|
1165
|
+
if (dataType === UnknownData) return snip(stitch`${snip(value, target, origin)}`, target, origin);
|
|
1166
|
+
}
|
|
1167
|
+
const converted = convertToCommonType(ctx, [snippet], targets, verbose);
|
|
1168
|
+
if (converted) return converted[0];
|
|
1169
|
+
throw new WgslTypeError(`Cannot convert value of type '${String(dataType)}' to any of the target types: [${targets.map((t) => t.type).join(", ")}]`);
|
|
1170
|
+
}
|
|
1171
|
+
function convertStructValues(ctx, structType, values) {
|
|
1172
|
+
return Object.entries(structType.propTypes).map(([key, targetType]) => {
|
|
1173
|
+
const val = values[key];
|
|
1174
|
+
if (!val) throw new Error(`Missing property ${key}`);
|
|
1175
|
+
return convertToCommonType(ctx, [val], [targetType])?.[0] ?? val;
|
|
1176
|
+
});
|
|
1177
|
+
}
|
|
1178
|
+
|
|
1179
|
+
//#endregion
|
|
1180
|
+
//#region src/core/function/createCallableSchema.ts
|
|
1181
|
+
function callableSchema(options) {
|
|
1182
|
+
const impl = ((...args) => {
|
|
1183
|
+
return options.normalImpl(...args);
|
|
1184
|
+
});
|
|
1185
|
+
setName(impl, options.name);
|
|
1186
|
+
impl.toString = () => options.name;
|
|
1187
|
+
impl[$gpuCallable] = {
|
|
1188
|
+
get strictSignature() {},
|
|
1189
|
+
call(ctx, args) {
|
|
1190
|
+
const { argTypes, returnType } = options.signature(...args.map((s) => {
|
|
1191
|
+
if (isPtr(s.dataType) && s.dataType.implicit) return s.dataType.inner;
|
|
1192
|
+
return s.dataType;
|
|
1193
|
+
}));
|
|
1194
|
+
const converted = args.map((s, idx) => {
|
|
1195
|
+
const argType = argTypes[idx];
|
|
1196
|
+
if (!argType) throw new Error("Function called with invalid arguments");
|
|
1197
|
+
return tryConvertSnippet(ctx, s, argType, false);
|
|
1198
|
+
});
|
|
1199
|
+
if (converted.every((s) => isKnownAtComptime(s))) {
|
|
1200
|
+
ctx.pushMode(new NormalState());
|
|
1201
|
+
try {
|
|
1202
|
+
return snip(options.normalImpl(...converted.map((s) => s.value)), returnType, "constant");
|
|
1203
|
+
} finally {
|
|
1204
|
+
ctx.popMode("normal");
|
|
1205
|
+
}
|
|
1206
|
+
}
|
|
1207
|
+
return snip(options.codegenImpl(ctx, converted), returnType, "runtime");
|
|
1208
|
+
}
|
|
1209
|
+
};
|
|
1210
|
+
return impl;
|
|
1211
|
+
}
|
|
1212
|
+
|
|
1213
|
+
//#endregion
|
|
1214
|
+
//#region src/data/numeric.ts
|
|
1215
|
+
const abstractInt = {
|
|
1216
|
+
[$internal]: {},
|
|
1217
|
+
type: "abstractInt",
|
|
1218
|
+
toString() {
|
|
1219
|
+
return "abstractInt";
|
|
1220
|
+
}
|
|
1221
|
+
};
|
|
1222
|
+
const abstractFloat = {
|
|
1223
|
+
[$internal]: {},
|
|
1224
|
+
type: "abstractFloat",
|
|
1225
|
+
toString() {
|
|
1226
|
+
return "abstractFloat";
|
|
1227
|
+
}
|
|
1228
|
+
};
|
|
1229
|
+
const boolCast = callableSchema({
|
|
1230
|
+
name: "bool",
|
|
1231
|
+
signature: (arg) => ({
|
|
1232
|
+
argTypes: arg ? [arg] : [],
|
|
1233
|
+
returnType: bool
|
|
1234
|
+
}),
|
|
1235
|
+
normalImpl(v) {
|
|
1236
|
+
if (v === void 0) return false;
|
|
1237
|
+
if (typeof v === "boolean") return v;
|
|
1238
|
+
return !!v;
|
|
1239
|
+
},
|
|
1240
|
+
codegenImpl: (_ctx, [arg]) => arg?.dataType === bool ? stitch`${arg}` : stitch`bool(${arg})`
|
|
1241
|
+
});
|
|
1242
|
+
/**
|
|
1243
|
+
* A schema that represents a boolean value. (equivalent to `bool` in WGSL)
|
|
1244
|
+
*
|
|
1245
|
+
* Can also be called to cast a value to a bool in accordance with WGSL casting rules.
|
|
1246
|
+
*
|
|
1247
|
+
* @example
|
|
1248
|
+
* const value = bool(); // false
|
|
1249
|
+
* @example
|
|
1250
|
+
* const value = bool(0); // false
|
|
1251
|
+
* @example
|
|
1252
|
+
* const value = bool(-0); // false
|
|
1253
|
+
* @example
|
|
1254
|
+
* const value = bool(21.37); // true
|
|
1255
|
+
*/
|
|
1256
|
+
const bool = Object.assign(boolCast, {
|
|
1257
|
+
[$internal]: {},
|
|
1258
|
+
type: "bool"
|
|
1259
|
+
});
|
|
1260
|
+
const u32Cast = callableSchema({
|
|
1261
|
+
name: "u32",
|
|
1262
|
+
signature: (arg) => ({
|
|
1263
|
+
argTypes: arg ? [arg] : [],
|
|
1264
|
+
returnType: u32
|
|
1265
|
+
}),
|
|
1266
|
+
normalImpl(v) {
|
|
1267
|
+
if (v === void 0) return 0;
|
|
1268
|
+
if (typeof v === "boolean") return v ? 1 : 0;
|
|
1269
|
+
if (!Number.isInteger(v)) {
|
|
1270
|
+
const truncated = Math.trunc(v);
|
|
1271
|
+
if (truncated < 0) return 0;
|
|
1272
|
+
if (truncated > 4294967295) return 4294967295;
|
|
1273
|
+
return truncated;
|
|
1274
|
+
}
|
|
1275
|
+
return (v & 4294967295) >>> 0;
|
|
1276
|
+
},
|
|
1277
|
+
codegenImpl: (_ctx, [arg]) => arg?.dataType === u32 ? stitch`${arg}` : stitch`u32(${arg})`
|
|
1278
|
+
});
|
|
1279
|
+
/**
|
|
1280
|
+
* A schema that represents an unsigned 32-bit integer value. (equivalent to `u32` in WGSL)
|
|
1281
|
+
*
|
|
1282
|
+
* Can also be called to cast a value to an u32 in accordance with WGSL casting rules.
|
|
1283
|
+
*
|
|
1284
|
+
* @example
|
|
1285
|
+
* const value = u32(); // 0
|
|
1286
|
+
* @example
|
|
1287
|
+
* const value = u32(7); // 7
|
|
1288
|
+
* @example
|
|
1289
|
+
* const value = u32(3.14); // 3
|
|
1290
|
+
* @example
|
|
1291
|
+
* const value = u32(-1); // 4294967295
|
|
1292
|
+
* @example
|
|
1293
|
+
* const value = u32(-3.1); // 0
|
|
1294
|
+
*/
|
|
1295
|
+
const u32 = Object.assign(u32Cast, {
|
|
1296
|
+
[$internal]: {},
|
|
1297
|
+
type: "u32"
|
|
1298
|
+
});
|
|
1299
|
+
const i32Cast = callableSchema({
|
|
1300
|
+
name: "i32",
|
|
1301
|
+
signature: (arg) => ({
|
|
1302
|
+
argTypes: arg ? [arg] : [],
|
|
1303
|
+
returnType: i32
|
|
1304
|
+
}),
|
|
1305
|
+
normalImpl(v) {
|
|
1306
|
+
if (v === void 0) return 0;
|
|
1307
|
+
if (typeof v === "boolean") return v ? 1 : 0;
|
|
1308
|
+
return v | 0;
|
|
1309
|
+
},
|
|
1310
|
+
codegenImpl: (_ctx, [arg]) => arg?.dataType === i32 ? stitch`${arg}` : stitch`i32(${arg})`
|
|
1311
|
+
});
|
|
1312
|
+
const u16 = {
|
|
1313
|
+
[$internal]: {},
|
|
1314
|
+
type: "u16"
|
|
1315
|
+
};
|
|
1316
|
+
/**
|
|
1317
|
+
* A schema that represents a signed 32-bit integer value. (equivalent to `i32` in WGSL)
|
|
1318
|
+
*
|
|
1319
|
+
* Can also be called to cast a value to an i32 in accordance with WGSL casting rules.
|
|
1320
|
+
*
|
|
1321
|
+
* @example
|
|
1322
|
+
* const value = i32(); // 0
|
|
1323
|
+
* @example
|
|
1324
|
+
* const value = i32(3.14); // 3
|
|
1325
|
+
* @example
|
|
1326
|
+
* const value = i32(-3.9); // -3
|
|
1327
|
+
* @example
|
|
1328
|
+
* const value = i32(10000000000) // 1410065408
|
|
1329
|
+
*/
|
|
1330
|
+
const i32 = Object.assign(i32Cast, {
|
|
1331
|
+
[$internal]: {},
|
|
1332
|
+
type: "i32"
|
|
1333
|
+
});
|
|
1334
|
+
const f32Cast = callableSchema({
|
|
1335
|
+
name: "f32",
|
|
1336
|
+
signature: (arg) => ({
|
|
1337
|
+
argTypes: arg ? [arg] : [],
|
|
1338
|
+
returnType: f32
|
|
1339
|
+
}),
|
|
1340
|
+
normalImpl(v) {
|
|
1341
|
+
if (v === void 0) return 0;
|
|
1342
|
+
if (typeof v === "boolean") return v ? 1 : 0;
|
|
1343
|
+
return Math.fround(v);
|
|
1344
|
+
},
|
|
1345
|
+
codegenImpl: (_ctx, [arg]) => arg?.dataType === f32 ? stitch`${arg}` : stitch`f32(${arg})`
|
|
1346
|
+
});
|
|
1347
|
+
/**
|
|
1348
|
+
* A schema that represents a 32-bit float value. (equivalent to `f32` in WGSL)
|
|
1349
|
+
*
|
|
1350
|
+
* Can also be called to cast a value to an f32.
|
|
1351
|
+
*
|
|
1352
|
+
* @example
|
|
1353
|
+
* const value = f32(); // 0
|
|
1354
|
+
* @example
|
|
1355
|
+
* const value = f32(1.23); // 1.23
|
|
1356
|
+
* @example
|
|
1357
|
+
* const value = f32(true); // 1
|
|
1358
|
+
*/
|
|
1359
|
+
const f32 = Object.assign(f32Cast, {
|
|
1360
|
+
[$internal]: {},
|
|
1361
|
+
type: "f32"
|
|
1362
|
+
});
|
|
1363
|
+
const buf32 = /* @__PURE__ */ new ArrayBuffer(4);
|
|
1364
|
+
const f32arr = new Float32Array(buf32);
|
|
1365
|
+
const u32arr = new Uint32Array(buf32);
|
|
1366
|
+
/**
|
|
1367
|
+
* Convert a JavaScript number (treated as float32) to **binary16** bit pattern.
|
|
1368
|
+
* @param x 32-bit floating-point value
|
|
1369
|
+
* @returns 16-bit half-precision encoding (stored in a JS number)
|
|
1370
|
+
*/
|
|
1371
|
+
function toHalfBits(x) {
|
|
1372
|
+
f32arr[0] = x;
|
|
1373
|
+
const bits = u32arr[0];
|
|
1374
|
+
const sign = bits >>> 31 & 1;
|
|
1375
|
+
let exp = bits >>> 23 & 255;
|
|
1376
|
+
let mant = bits & 8388607;
|
|
1377
|
+
if (exp === 255) return sign << 15 | 31744 | (mant ? 512 : 0);
|
|
1378
|
+
exp = exp - 127 + 15;
|
|
1379
|
+
if (exp <= 0) {
|
|
1380
|
+
if (exp < -10) return sign << 15;
|
|
1381
|
+
mant = (mant | 8388608) >> 1 - exp;
|
|
1382
|
+
mant = mant + 4096 >> 13;
|
|
1383
|
+
return sign << 15 | mant;
|
|
1384
|
+
}
|
|
1385
|
+
if (exp >= 31) return sign << 15 | 31744;
|
|
1386
|
+
mant = mant + 4096;
|
|
1387
|
+
if (mant & 8388608) {
|
|
1388
|
+
mant = 0;
|
|
1389
|
+
++exp;
|
|
1390
|
+
if (exp >= 31) return sign << 15 | 31744;
|
|
1391
|
+
}
|
|
1392
|
+
return sign << 15 | exp << 10 | mant >> 13;
|
|
1393
|
+
}
|
|
1394
|
+
/**
|
|
1395
|
+
* Convert a **binary16** encoded bit pattern back to JavaScript number.
|
|
1396
|
+
* @param h 16-bit half-precision bits
|
|
1397
|
+
* @returns JavaScript number (64-bit float) with same numerical value
|
|
1398
|
+
*/
|
|
1399
|
+
function fromHalfBits(h) {
|
|
1400
|
+
const sign = h & 32768 ? -1 : 1;
|
|
1401
|
+
const exp = h >> 10 & 31;
|
|
1402
|
+
const mant = h & 1023;
|
|
1403
|
+
if (exp === 0) return mant ? sign * mant * 2 ** -24 : sign * 0;
|
|
1404
|
+
if (exp === 31) return mant ? NaN : sign === 1 ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;
|
|
1405
|
+
return sign * (1 + mant / 1024) * 2 ** (exp - 15);
|
|
1406
|
+
}
|
|
1407
|
+
function roundToF16(x) {
|
|
1408
|
+
return fromHalfBits(toHalfBits(x));
|
|
1409
|
+
}
|
|
1410
|
+
const f16Cast = callableSchema({
|
|
1411
|
+
name: "f16",
|
|
1412
|
+
signature: (arg) => ({
|
|
1413
|
+
argTypes: arg ? [arg] : [],
|
|
1414
|
+
returnType: f16
|
|
1415
|
+
}),
|
|
1416
|
+
normalImpl(v) {
|
|
1417
|
+
if (v === void 0) return 0;
|
|
1418
|
+
if (typeof v === "boolean") return v ? 1 : 0;
|
|
1419
|
+
return roundToF16(v);
|
|
1420
|
+
},
|
|
1421
|
+
codegenImpl: (_ctx, [arg]) => arg?.dataType === f16 ? stitch`${arg}` : stitch`f16(${arg})`
|
|
1422
|
+
});
|
|
1423
|
+
/**
|
|
1424
|
+
* A schema that represents a 16-bit float value. (equivalent to `f16` in WGSL)
|
|
1425
|
+
*
|
|
1426
|
+
* Can also be called to cast a value to an f16.
|
|
1427
|
+
*
|
|
1428
|
+
* @example
|
|
1429
|
+
* const value = f16(); // 0
|
|
1430
|
+
* @example
|
|
1431
|
+
* const value = f32(1.23); // 1.23
|
|
1432
|
+
* @example
|
|
1433
|
+
* const value = f16(true); // 1
|
|
1434
|
+
* @example
|
|
1435
|
+
* const value = f16(21877.5); // 21872
|
|
1436
|
+
*/
|
|
1437
|
+
const f16 = Object.assign(f16Cast, {
|
|
1438
|
+
[$internal]: {},
|
|
1439
|
+
type: "f16"
|
|
1440
|
+
});
|
|
1441
|
+
|
|
1442
|
+
//#endregion
|
|
1443
|
+
//#region src/data/vectorImpl.ts
|
|
1444
|
+
const XYZW = [
|
|
1445
|
+
"x",
|
|
1446
|
+
"y",
|
|
1447
|
+
"z",
|
|
1448
|
+
"w"
|
|
1449
|
+
];
|
|
1450
|
+
const RGBA = [
|
|
1451
|
+
"r",
|
|
1452
|
+
"g",
|
|
1453
|
+
"b",
|
|
1454
|
+
"a"
|
|
1455
|
+
];
|
|
1456
|
+
var VecBase = class VecBase extends Array {
|
|
1457
|
+
static {
|
|
1458
|
+
for (let e0 = 0; e0 < 4; e0++) for (let e1 = 0; e1 < 4; e1++) for (let e2 = 0; e2 < 4; e2++) for (let e3 = 0; e3 < 4; e3++) {
|
|
1459
|
+
const xyzwProp = `${XYZW[e0]}${XYZW[e1]}${XYZW[e2]}${XYZW[e3]}`;
|
|
1460
|
+
const rgbaProp = `${RGBA[e0]}${RGBA[e1]}${RGBA[e2]}${RGBA[e3]}`;
|
|
1461
|
+
Object.defineProperty(VecBase.prototype, xyzwProp, { get() {
|
|
1462
|
+
return new this._Vec4(this[e0], this[e1], this[e2], this[e3]);
|
|
1463
|
+
} });
|
|
1464
|
+
Object.defineProperty(VecBase.prototype, rgbaProp, { get() {
|
|
1465
|
+
return new this._Vec4(this[e0], this[e1], this[e2], this[e3]);
|
|
1466
|
+
} });
|
|
1467
|
+
}
|
|
1468
|
+
for (let e0 = 0; e0 < 4; e0++) for (let e1 = 0; e1 < 4; e1++) for (let e2 = 0; e2 < 4; e2++) {
|
|
1469
|
+
const xyzwProp = `${XYZW[e0]}${XYZW[e1]}${XYZW[e2]}`;
|
|
1470
|
+
const rgbaProp = `${RGBA[e0]}${RGBA[e1]}${RGBA[e2]}`;
|
|
1471
|
+
Object.defineProperty(VecBase.prototype, xyzwProp, { get() {
|
|
1472
|
+
return new this._Vec3(this[e0], this[e1], this[e2]);
|
|
1473
|
+
} });
|
|
1474
|
+
Object.defineProperty(VecBase.prototype, rgbaProp, { get() {
|
|
1475
|
+
return new this._Vec3(this[e0], this[e1], this[e2]);
|
|
1476
|
+
} });
|
|
1477
|
+
}
|
|
1478
|
+
for (let e0 = 0; e0 < 4; e0++) for (let e1 = 0; e1 < 4; e1++) {
|
|
1479
|
+
const xyzwProp = `${XYZW[e0]}${XYZW[e1]}`;
|
|
1480
|
+
const rgbaProp = `${RGBA[e0]}${RGBA[e1]}`;
|
|
1481
|
+
Object.defineProperty(VecBase.prototype, xyzwProp, { get() {
|
|
1482
|
+
return new this._Vec2(this[e0], this[e1]);
|
|
1483
|
+
} });
|
|
1484
|
+
Object.defineProperty(VecBase.prototype, rgbaProp, { get() {
|
|
1485
|
+
return new this._Vec2(this[e0], this[e1]);
|
|
1486
|
+
} });
|
|
1487
|
+
}
|
|
1488
|
+
}
|
|
1489
|
+
castElement() {
|
|
1490
|
+
return this[$internal].elementSchema;
|
|
1491
|
+
}
|
|
1492
|
+
[$resolve]() {
|
|
1493
|
+
const schema = this[$internal].elementSchema;
|
|
1494
|
+
if (this.every((e) => !e)) return snip(`${this.kind}()`, schema, "constant");
|
|
1495
|
+
if (this.every((e) => this[0] === e)) return snip(`${this.kind}(${this[0]})`, schema, "runtime");
|
|
1496
|
+
return snip(`${this.kind}(${this.join(", ")})`, schema, "runtime");
|
|
1497
|
+
}
|
|
1498
|
+
toString() {
|
|
1499
|
+
return this[$resolve]().value;
|
|
1500
|
+
}
|
|
1501
|
+
};
|
|
1502
|
+
var Vec2 = class extends VecBase {
|
|
1503
|
+
e0;
|
|
1504
|
+
e1;
|
|
1505
|
+
constructor(x, y) {
|
|
1506
|
+
super(2);
|
|
1507
|
+
this.e0 = this.castElement()(x);
|
|
1508
|
+
this.e1 = this.castElement()(y ?? x);
|
|
1509
|
+
}
|
|
1510
|
+
get 0() {
|
|
1511
|
+
return this.e0;
|
|
1512
|
+
}
|
|
1513
|
+
get 1() {
|
|
1514
|
+
return this.e1;
|
|
1515
|
+
}
|
|
1516
|
+
set 0(value) {
|
|
1517
|
+
this.e0 = this.castElement()(value);
|
|
1518
|
+
}
|
|
1519
|
+
set 1(value) {
|
|
1520
|
+
this.e1 = this.castElement()(value);
|
|
1521
|
+
}
|
|
1522
|
+
get x() {
|
|
1523
|
+
return this[0];
|
|
1524
|
+
}
|
|
1525
|
+
get y() {
|
|
1526
|
+
return this[1];
|
|
1527
|
+
}
|
|
1528
|
+
set x(value) {
|
|
1529
|
+
this[0] = this.castElement()(value);
|
|
1530
|
+
}
|
|
1531
|
+
set y(value) {
|
|
1532
|
+
this[1] = this.castElement()(value);
|
|
1533
|
+
}
|
|
1534
|
+
get r() {
|
|
1535
|
+
return this[0];
|
|
1536
|
+
}
|
|
1537
|
+
get g() {
|
|
1538
|
+
return this[1];
|
|
1539
|
+
}
|
|
1540
|
+
set r(value) {
|
|
1541
|
+
this[0] = this.castElement()(value);
|
|
1542
|
+
}
|
|
1543
|
+
set g(value) {
|
|
1544
|
+
this[1] = this.castElement()(value);
|
|
1545
|
+
}
|
|
1546
|
+
};
|
|
1547
|
+
var Vec3 = class extends VecBase {
|
|
1548
|
+
e0;
|
|
1549
|
+
e1;
|
|
1550
|
+
e2;
|
|
1551
|
+
constructor(x, y, z) {
|
|
1552
|
+
super(3);
|
|
1553
|
+
this.e0 = this.castElement()(x);
|
|
1554
|
+
this.e1 = this.castElement()(y ?? x);
|
|
1555
|
+
this.e2 = this.castElement()(z ?? x);
|
|
1556
|
+
}
|
|
1557
|
+
get 0() {
|
|
1558
|
+
return this.e0;
|
|
1559
|
+
}
|
|
1560
|
+
get 1() {
|
|
1561
|
+
return this.e1;
|
|
1562
|
+
}
|
|
1563
|
+
get 2() {
|
|
1564
|
+
return this.e2;
|
|
1565
|
+
}
|
|
1566
|
+
set 0(value) {
|
|
1567
|
+
this.e0 = this.castElement()(value);
|
|
1568
|
+
}
|
|
1569
|
+
set 1(value) {
|
|
1570
|
+
this.e1 = this.castElement()(value);
|
|
1571
|
+
}
|
|
1572
|
+
set 2(value) {
|
|
1573
|
+
this.e2 = this.castElement()(value);
|
|
1574
|
+
}
|
|
1575
|
+
get x() {
|
|
1576
|
+
return this[0];
|
|
1577
|
+
}
|
|
1578
|
+
get y() {
|
|
1579
|
+
return this[1];
|
|
1580
|
+
}
|
|
1581
|
+
get z() {
|
|
1582
|
+
return this[2];
|
|
1583
|
+
}
|
|
1584
|
+
set x(value) {
|
|
1585
|
+
this[0] = this.castElement()(value);
|
|
1586
|
+
}
|
|
1587
|
+
set y(value) {
|
|
1588
|
+
this[1] = this.castElement()(value);
|
|
1589
|
+
}
|
|
1590
|
+
set z(value) {
|
|
1591
|
+
this[2] = this.castElement()(value);
|
|
1592
|
+
}
|
|
1593
|
+
get r() {
|
|
1594
|
+
return this[0];
|
|
1595
|
+
}
|
|
1596
|
+
get g() {
|
|
1597
|
+
return this[1];
|
|
1598
|
+
}
|
|
1599
|
+
get b() {
|
|
1600
|
+
return this[2];
|
|
1601
|
+
}
|
|
1602
|
+
set r(value) {
|
|
1603
|
+
this[0] = this.castElement()(value);
|
|
1604
|
+
}
|
|
1605
|
+
set g(value) {
|
|
1606
|
+
this[1] = this.castElement()(value);
|
|
1607
|
+
}
|
|
1608
|
+
set b(value) {
|
|
1609
|
+
this[2] = this.castElement()(value);
|
|
1610
|
+
}
|
|
1611
|
+
};
|
|
1612
|
+
var Vec4 = class extends VecBase {
|
|
1613
|
+
e0;
|
|
1614
|
+
e1;
|
|
1615
|
+
e2;
|
|
1616
|
+
e3;
|
|
1617
|
+
constructor(x, y, z, w) {
|
|
1618
|
+
super(4);
|
|
1619
|
+
this.e0 = this.castElement()(x);
|
|
1620
|
+
this.e1 = this.castElement()(y ?? x);
|
|
1621
|
+
this.e2 = this.castElement()(z ?? x);
|
|
1622
|
+
this.e3 = this.castElement()(w ?? x);
|
|
1623
|
+
}
|
|
1624
|
+
get 0() {
|
|
1625
|
+
return this.e0;
|
|
1626
|
+
}
|
|
1627
|
+
get 1() {
|
|
1628
|
+
return this.e1;
|
|
1629
|
+
}
|
|
1630
|
+
get 2() {
|
|
1631
|
+
return this.e2;
|
|
1632
|
+
}
|
|
1633
|
+
get 3() {
|
|
1634
|
+
return this.e3;
|
|
1635
|
+
}
|
|
1636
|
+
set 0(value) {
|
|
1637
|
+
this.e0 = this.castElement()(value);
|
|
1638
|
+
}
|
|
1639
|
+
set 1(value) {
|
|
1640
|
+
this.e1 = this.castElement()(value);
|
|
1641
|
+
}
|
|
1642
|
+
set 2(value) {
|
|
1643
|
+
this.e2 = this.castElement()(value);
|
|
1644
|
+
}
|
|
1645
|
+
set 3(value) {
|
|
1646
|
+
this.e3 = this.castElement()(value);
|
|
1647
|
+
}
|
|
1648
|
+
get x() {
|
|
1649
|
+
return this[0];
|
|
1650
|
+
}
|
|
1651
|
+
get y() {
|
|
1652
|
+
return this[1];
|
|
1653
|
+
}
|
|
1654
|
+
get r() {
|
|
1655
|
+
return this[0];
|
|
1656
|
+
}
|
|
1657
|
+
get g() {
|
|
1658
|
+
return this[1];
|
|
1659
|
+
}
|
|
1660
|
+
get b() {
|
|
1661
|
+
return this[2];
|
|
1662
|
+
}
|
|
1663
|
+
get a() {
|
|
1664
|
+
return this[3];
|
|
1665
|
+
}
|
|
1666
|
+
set r(value) {
|
|
1667
|
+
this[0] = value;
|
|
1668
|
+
}
|
|
1669
|
+
set g(value) {
|
|
1670
|
+
this[1] = value;
|
|
1671
|
+
}
|
|
1672
|
+
set b(value) {
|
|
1673
|
+
this[2] = value;
|
|
1674
|
+
}
|
|
1675
|
+
set a(value) {
|
|
1676
|
+
this[3] = value;
|
|
1677
|
+
}
|
|
1678
|
+
get z() {
|
|
1679
|
+
return this[2];
|
|
1680
|
+
}
|
|
1681
|
+
get w() {
|
|
1682
|
+
return this[3];
|
|
1683
|
+
}
|
|
1684
|
+
set x(value) {
|
|
1685
|
+
this[0] = value;
|
|
1686
|
+
}
|
|
1687
|
+
set y(value) {
|
|
1688
|
+
this[1] = value;
|
|
1689
|
+
}
|
|
1690
|
+
set z(value) {
|
|
1691
|
+
this[2] = value;
|
|
1692
|
+
}
|
|
1693
|
+
set w(value) {
|
|
1694
|
+
this[3] = value;
|
|
1695
|
+
}
|
|
1696
|
+
};
|
|
1697
|
+
var Vec2fImpl = class Vec2fImpl extends Vec2 {
|
|
1698
|
+
get [$internal]() {
|
|
1699
|
+
return { elementSchema: f32 };
|
|
1700
|
+
}
|
|
1701
|
+
get kind() {
|
|
1702
|
+
return "vec2f";
|
|
1703
|
+
}
|
|
1704
|
+
get _Vec2() {
|
|
1705
|
+
return Vec2fImpl;
|
|
1706
|
+
}
|
|
1707
|
+
get _Vec3() {
|
|
1708
|
+
return Vec3fImpl;
|
|
1709
|
+
}
|
|
1710
|
+
get _Vec4() {
|
|
1711
|
+
return Vec4fImpl;
|
|
1712
|
+
}
|
|
1713
|
+
};
|
|
1714
|
+
var Vec2hImpl = class Vec2hImpl extends Vec2 {
|
|
1715
|
+
get [$internal]() {
|
|
1716
|
+
return { elementSchema: f16 };
|
|
1717
|
+
}
|
|
1718
|
+
get kind() {
|
|
1719
|
+
return "vec2h";
|
|
1720
|
+
}
|
|
1721
|
+
get _Vec2() {
|
|
1722
|
+
return Vec2hImpl;
|
|
1723
|
+
}
|
|
1724
|
+
get _Vec3() {
|
|
1725
|
+
return Vec3hImpl;
|
|
1726
|
+
}
|
|
1727
|
+
get _Vec4() {
|
|
1728
|
+
return Vec4hImpl;
|
|
1729
|
+
}
|
|
1730
|
+
};
|
|
1731
|
+
var Vec2iImpl = class Vec2iImpl extends Vec2 {
|
|
1732
|
+
get [$internal]() {
|
|
1733
|
+
return { elementSchema: i32 };
|
|
1734
|
+
}
|
|
1735
|
+
get kind() {
|
|
1736
|
+
return "vec2i";
|
|
1737
|
+
}
|
|
1738
|
+
get _Vec2() {
|
|
1739
|
+
return Vec2iImpl;
|
|
1740
|
+
}
|
|
1741
|
+
get _Vec3() {
|
|
1742
|
+
return Vec3iImpl;
|
|
1743
|
+
}
|
|
1744
|
+
get _Vec4() {
|
|
1745
|
+
return Vec4iImpl;
|
|
1746
|
+
}
|
|
1747
|
+
};
|
|
1748
|
+
var Vec2uImpl = class Vec2uImpl extends Vec2 {
|
|
1749
|
+
get [$internal]() {
|
|
1750
|
+
return { elementSchema: u32 };
|
|
1751
|
+
}
|
|
1752
|
+
get kind() {
|
|
1753
|
+
return "vec2u";
|
|
1754
|
+
}
|
|
1755
|
+
get _Vec2() {
|
|
1756
|
+
return Vec2uImpl;
|
|
1757
|
+
}
|
|
1758
|
+
get _Vec3() {
|
|
1759
|
+
return Vec3uImpl;
|
|
1760
|
+
}
|
|
1761
|
+
get _Vec4() {
|
|
1762
|
+
return Vec4uImpl;
|
|
1763
|
+
}
|
|
1764
|
+
};
|
|
1765
|
+
var Vec2bImpl = class Vec2bImpl extends Vec2 {
|
|
1766
|
+
get [$internal]() {
|
|
1767
|
+
return { elementSchema: bool };
|
|
1768
|
+
}
|
|
1769
|
+
get kind() {
|
|
1770
|
+
return "vec2<bool>";
|
|
1771
|
+
}
|
|
1772
|
+
get _Vec2() {
|
|
1773
|
+
return Vec2bImpl;
|
|
1774
|
+
}
|
|
1775
|
+
get _Vec3() {
|
|
1776
|
+
return Vec3bImpl;
|
|
1777
|
+
}
|
|
1778
|
+
get _Vec4() {
|
|
1779
|
+
return Vec4bImpl;
|
|
1780
|
+
}
|
|
1781
|
+
};
|
|
1782
|
+
var Vec3fImpl = class Vec3fImpl extends Vec3 {
|
|
1783
|
+
get [$internal]() {
|
|
1784
|
+
return { elementSchema: f32 };
|
|
1785
|
+
}
|
|
1786
|
+
get kind() {
|
|
1787
|
+
return "vec3f";
|
|
1788
|
+
}
|
|
1789
|
+
get _Vec2() {
|
|
1790
|
+
return Vec2fImpl;
|
|
1791
|
+
}
|
|
1792
|
+
get _Vec3() {
|
|
1793
|
+
return Vec3fImpl;
|
|
1794
|
+
}
|
|
1795
|
+
get _Vec4() {
|
|
1796
|
+
return Vec4fImpl;
|
|
1797
|
+
}
|
|
1798
|
+
};
|
|
1799
|
+
var Vec3hImpl = class Vec3hImpl extends Vec3 {
|
|
1800
|
+
get [$internal]() {
|
|
1801
|
+
return { elementSchema: f16 };
|
|
1802
|
+
}
|
|
1803
|
+
get kind() {
|
|
1804
|
+
return "vec3h";
|
|
1805
|
+
}
|
|
1806
|
+
get _Vec2() {
|
|
1807
|
+
return Vec2hImpl;
|
|
1808
|
+
}
|
|
1809
|
+
get _Vec3() {
|
|
1810
|
+
return Vec3hImpl;
|
|
1811
|
+
}
|
|
1812
|
+
get _Vec4() {
|
|
1813
|
+
return Vec4hImpl;
|
|
1814
|
+
}
|
|
1815
|
+
};
|
|
1816
|
+
var Vec3iImpl = class Vec3iImpl extends Vec3 {
|
|
1817
|
+
get [$internal]() {
|
|
1818
|
+
return { elementSchema: i32 };
|
|
1819
|
+
}
|
|
1820
|
+
get kind() {
|
|
1821
|
+
return "vec3i";
|
|
1822
|
+
}
|
|
1823
|
+
get _Vec2() {
|
|
1824
|
+
return Vec2iImpl;
|
|
1825
|
+
}
|
|
1826
|
+
get _Vec3() {
|
|
1827
|
+
return Vec3iImpl;
|
|
1828
|
+
}
|
|
1829
|
+
get _Vec4() {
|
|
1830
|
+
return Vec4iImpl;
|
|
1831
|
+
}
|
|
1832
|
+
};
|
|
1833
|
+
var Vec3uImpl = class Vec3uImpl extends Vec3 {
|
|
1834
|
+
get [$internal]() {
|
|
1835
|
+
return { elementSchema: u32 };
|
|
1836
|
+
}
|
|
1837
|
+
get kind() {
|
|
1838
|
+
return "vec3u";
|
|
1839
|
+
}
|
|
1840
|
+
get _Vec2() {
|
|
1841
|
+
return Vec2uImpl;
|
|
1842
|
+
}
|
|
1843
|
+
get _Vec3() {
|
|
1844
|
+
return Vec3uImpl;
|
|
1845
|
+
}
|
|
1846
|
+
get _Vec4() {
|
|
1847
|
+
return Vec4uImpl;
|
|
1848
|
+
}
|
|
1849
|
+
};
|
|
1850
|
+
var Vec3bImpl = class Vec3bImpl extends Vec3 {
|
|
1851
|
+
get [$internal]() {
|
|
1852
|
+
return { elementSchema: bool };
|
|
1853
|
+
}
|
|
1854
|
+
get kind() {
|
|
1855
|
+
return "vec3<bool>";
|
|
1856
|
+
}
|
|
1857
|
+
get _Vec2() {
|
|
1858
|
+
return Vec2bImpl;
|
|
1859
|
+
}
|
|
1860
|
+
get _Vec3() {
|
|
1861
|
+
return Vec3bImpl;
|
|
1862
|
+
}
|
|
1863
|
+
get _Vec4() {
|
|
1864
|
+
return Vec4bImpl;
|
|
1865
|
+
}
|
|
1866
|
+
};
|
|
1867
|
+
var Vec4fImpl = class Vec4fImpl extends Vec4 {
|
|
1868
|
+
get [$internal]() {
|
|
1869
|
+
return { elementSchema: f32 };
|
|
1870
|
+
}
|
|
1871
|
+
get kind() {
|
|
1872
|
+
return "vec4f";
|
|
1873
|
+
}
|
|
1874
|
+
get _Vec2() {
|
|
1875
|
+
return Vec2fImpl;
|
|
1876
|
+
}
|
|
1877
|
+
get _Vec3() {
|
|
1878
|
+
return Vec3fImpl;
|
|
1879
|
+
}
|
|
1880
|
+
get _Vec4() {
|
|
1881
|
+
return Vec4fImpl;
|
|
1882
|
+
}
|
|
1883
|
+
};
|
|
1884
|
+
var Vec4hImpl = class Vec4hImpl extends Vec4 {
|
|
1885
|
+
get [$internal]() {
|
|
1886
|
+
return { elementSchema: f16 };
|
|
1887
|
+
}
|
|
1888
|
+
get kind() {
|
|
1889
|
+
return "vec4h";
|
|
1890
|
+
}
|
|
1891
|
+
get _Vec2() {
|
|
1892
|
+
return Vec2hImpl;
|
|
1893
|
+
}
|
|
1894
|
+
get _Vec3() {
|
|
1895
|
+
return Vec3hImpl;
|
|
1896
|
+
}
|
|
1897
|
+
get _Vec4() {
|
|
1898
|
+
return Vec4hImpl;
|
|
1899
|
+
}
|
|
1900
|
+
};
|
|
1901
|
+
var Vec4iImpl = class Vec4iImpl extends Vec4 {
|
|
1902
|
+
get [$internal]() {
|
|
1903
|
+
return { elementSchema: i32 };
|
|
1904
|
+
}
|
|
1905
|
+
get kind() {
|
|
1906
|
+
return "vec4i";
|
|
1907
|
+
}
|
|
1908
|
+
get _Vec2() {
|
|
1909
|
+
return Vec2iImpl;
|
|
1910
|
+
}
|
|
1911
|
+
get _Vec3() {
|
|
1912
|
+
return Vec3iImpl;
|
|
1913
|
+
}
|
|
1914
|
+
get _Vec4() {
|
|
1915
|
+
return Vec4iImpl;
|
|
1916
|
+
}
|
|
1917
|
+
};
|
|
1918
|
+
var Vec4uImpl = class Vec4uImpl extends Vec4 {
|
|
1919
|
+
get [$internal]() {
|
|
1920
|
+
return { elementSchema: u32 };
|
|
1921
|
+
}
|
|
1922
|
+
get kind() {
|
|
1923
|
+
return "vec4u";
|
|
1924
|
+
}
|
|
1925
|
+
get _Vec2() {
|
|
1926
|
+
return Vec2uImpl;
|
|
1927
|
+
}
|
|
1928
|
+
get _Vec3() {
|
|
1929
|
+
return Vec3uImpl;
|
|
1930
|
+
}
|
|
1931
|
+
get _Vec4() {
|
|
1932
|
+
return Vec4uImpl;
|
|
1933
|
+
}
|
|
1934
|
+
};
|
|
1935
|
+
var Vec4bImpl = class Vec4bImpl extends Vec4 {
|
|
1936
|
+
get [$internal]() {
|
|
1937
|
+
return { elementSchema: bool };
|
|
1938
|
+
}
|
|
1939
|
+
get kind() {
|
|
1940
|
+
return "vec4<bool>";
|
|
1941
|
+
}
|
|
1942
|
+
get _Vec2() {
|
|
1943
|
+
return Vec2bImpl;
|
|
1944
|
+
}
|
|
1945
|
+
get _Vec3() {
|
|
1946
|
+
return Vec3bImpl;
|
|
1947
|
+
}
|
|
1948
|
+
get _Vec4() {
|
|
1949
|
+
return Vec4bImpl;
|
|
1950
|
+
}
|
|
1951
|
+
};
|
|
1952
|
+
|
|
1953
|
+
//#endregion
|
|
1954
|
+
//#region src/data/vector.ts
|
|
1955
|
+
/**
|
|
1956
|
+
* Schema representing vec2f - a vector with 2 elements of type f32.
|
|
1957
|
+
* Also a constructor function for this vector value.
|
|
1958
|
+
*
|
|
1959
|
+
* @example
|
|
1960
|
+
* const vector = d.vec2f(); // (0.0, 0.0)
|
|
1961
|
+
* const vector = d.vec2f(1); // (1.0, 1.0)
|
|
1962
|
+
* const vector = d.vec2f(0.5, 0.1); // (0.5, 0.1)
|
|
1963
|
+
*
|
|
1964
|
+
* @example
|
|
1965
|
+
* const buffer = root.createBuffer(d.vec2f, d.vec2f(0, 1)); // buffer holding a d.vec2f value, with an initial value of vec2f(0, 1);
|
|
1966
|
+
*/
|
|
1967
|
+
const vec2f$1 = makeVecSchema(Vec2fImpl, f32);
|
|
1968
|
+
/**
|
|
1969
|
+
* Schema representing vec2h - a vector with 2 elements of type f16.
|
|
1970
|
+
* Also a constructor function for this vector value.
|
|
1971
|
+
*
|
|
1972
|
+
* @example
|
|
1973
|
+
* const vector = d.vec2h(); // (0.0, 0.0)
|
|
1974
|
+
* const vector = d.vec2h(1); // (1.0, 1.0)
|
|
1975
|
+
* const vector = d.vec2h(0.5, 0.1); // (0.5, 0.1)
|
|
1976
|
+
*
|
|
1977
|
+
* @example
|
|
1978
|
+
* const buffer = root.createBuffer(d.vec2h, d.vec2h(0, 1)); // buffer holding a d.vec2h value, with an initial value of vec2h(0, 1);
|
|
1979
|
+
*/
|
|
1980
|
+
const vec2h$1 = makeVecSchema(Vec2hImpl, f16);
|
|
1981
|
+
/**
|
|
1982
|
+
* Schema representing vec2i - a vector with 2 elements of type i32.
|
|
1983
|
+
* Also a constructor function for this vector value.
|
|
1984
|
+
*
|
|
1985
|
+
* @example
|
|
1986
|
+
* const vector = d.vec2i(); // (0, 0)
|
|
1987
|
+
* const vector = d.vec2i(1); // (1, 1)
|
|
1988
|
+
* const vector = d.vec2i(-1, 1); // (-1, 1)
|
|
1989
|
+
*
|
|
1990
|
+
* @example
|
|
1991
|
+
* const buffer = root.createBuffer(d.vec2i, d.vec2i(0, 1)); // buffer holding a d.vec2i value, with an initial value of vec2i(0, 1);
|
|
1992
|
+
*/
|
|
1993
|
+
const vec2i$1 = makeVecSchema(Vec2iImpl, i32);
|
|
1994
|
+
/**
|
|
1995
|
+
* Schema representing vec2u - a vector with 2 elements of type u32.
|
|
1996
|
+
* Also a constructor function for this vector value.
|
|
1997
|
+
*
|
|
1998
|
+
* @example
|
|
1999
|
+
* const vector = d.vec2u(); // (0, 0)
|
|
2000
|
+
* const vector = d.vec2u(1); // (1, 1)
|
|
2001
|
+
* const vector = d.vec2u(1, 2); // (1, 2)
|
|
2002
|
+
*
|
|
2003
|
+
* @example
|
|
2004
|
+
* const buffer = root.createBuffer(d.vec2u, d.vec2u(0, 1)); // buffer holding a d.vec2u value, with an initial value of vec2u(0, 1);
|
|
2005
|
+
*/
|
|
2006
|
+
const vec2u$1 = makeVecSchema(Vec2uImpl, u32);
|
|
2007
|
+
/**
|
|
2008
|
+
* Schema representing `vec2<bool>` - a vector with 2 elements of type `bool`.
|
|
2009
|
+
* Also a constructor function for this vector value.
|
|
2010
|
+
*
|
|
2011
|
+
* @example
|
|
2012
|
+
* const vector = d.vec2b(); // (false, false)
|
|
2013
|
+
* const vector = d.vec2b(true); // (true, true)
|
|
2014
|
+
* const vector = d.vec2b(false, true); // (false, true)
|
|
2015
|
+
*/
|
|
2016
|
+
const vec2b$1 = makeVecSchema(Vec2bImpl, bool);
|
|
2017
|
+
/**
|
|
2018
|
+
* Schema representing vec3f - a vector with 3 elements of type f32.
|
|
2019
|
+
* Also a constructor function for this vector value.
|
|
2020
|
+
*
|
|
2021
|
+
* @example
|
|
2022
|
+
* const vector = d.vec3f(); // (0.0, 0.0, 0.0)
|
|
2023
|
+
* const vector = d.vec3f(1); // (1.0, 1.0, 1.0)
|
|
2024
|
+
* const vector = d.vec3f(1, 2, 3.5); // (1.0, 2.0, 3.5)
|
|
2025
|
+
*
|
|
2026
|
+
* @example
|
|
2027
|
+
* const buffer = root.createBuffer(d.vec3f, d.vec3f(0, 1, 2)); // buffer holding a d.vec3f value, with an initial value of vec3f(0, 1, 2);
|
|
2028
|
+
*/
|
|
2029
|
+
const vec3f$1 = makeVecSchema(Vec3fImpl, f32);
|
|
2030
|
+
/**
|
|
2031
|
+
* Schema representing vec3h - a vector with 3 elements of type f16.
|
|
2032
|
+
* Also a constructor function for this vector value.
|
|
2033
|
+
*
|
|
2034
|
+
* @example
|
|
2035
|
+
* const vector = d.vec3h(); // (0.0, 0.0, 0.0)
|
|
2036
|
+
* const vector = d.vec3h(1); // (1.0, 1.0, 1.0)
|
|
2037
|
+
* const vector = d.vec3h(1, 2, 3.5); // (1.0, 2.0, 3.5)
|
|
2038
|
+
*
|
|
2039
|
+
* @example
|
|
2040
|
+
* const buffer = root.createBuffer(d.vec3h, d.vec3h(0, 1, 2)); // buffer holding a d.vec3h value, with an initial value of vec3h(0, 1, 2);
|
|
2041
|
+
*/
|
|
2042
|
+
const vec3h$1 = makeVecSchema(Vec3hImpl, f16);
|
|
2043
|
+
/**
|
|
2044
|
+
* Schema representing vec3i - a vector with 3 elements of type i32.
|
|
2045
|
+
* Also a constructor function for this vector value.
|
|
2046
|
+
*
|
|
2047
|
+
* @example
|
|
2048
|
+
* const vector = d.vec3i(); // (0, 0, 0)
|
|
2049
|
+
* const vector = d.vec3i(1); // (1, 1, 1)
|
|
2050
|
+
* const vector = d.vec3i(1, 2, -3); // (1, 2, -3)
|
|
2051
|
+
*
|
|
2052
|
+
* @example
|
|
2053
|
+
* const buffer = root.createBuffer(d.vec3i, d.vec3i(0, 1, 2)); // buffer holding a d.vec3i value, with an initial value of vec3i(0, 1, 2);
|
|
2054
|
+
*/
|
|
2055
|
+
const vec3i$1 = makeVecSchema(Vec3iImpl, i32);
|
|
2056
|
+
/**
|
|
2057
|
+
* Schema representing vec3u - a vector with 3 elements of type u32.
|
|
2058
|
+
* Also a constructor function for this vector value.
|
|
2059
|
+
*
|
|
2060
|
+
* @example
|
|
2061
|
+
* const vector = d.vec3u(); // (0, 0, 0)
|
|
2062
|
+
* const vector = d.vec3u(1); // (1, 1, 1)
|
|
2063
|
+
* const vector = d.vec3u(1, 2, 3); // (1, 2, 3)
|
|
2064
|
+
*
|
|
2065
|
+
* @example
|
|
2066
|
+
* const buffer = root.createBuffer(d.vec3u, d.vec3u(0, 1, 2)); // buffer holding a d.vec3u value, with an initial value of vec3u(0, 1, 2);
|
|
2067
|
+
*/
|
|
2068
|
+
const vec3u$1 = makeVecSchema(Vec3uImpl, u32);
|
|
2069
|
+
/**
|
|
2070
|
+
* Schema representing `vec3<bool>` - a vector with 3 elements of type `bool`.
|
|
2071
|
+
* Also a constructor function for this vector value.
|
|
2072
|
+
*
|
|
2073
|
+
* @example
|
|
2074
|
+
* const vector = d.vec3b(); // (false, false, false)
|
|
2075
|
+
* const vector = d.vec3b(true); // (true, true, true)
|
|
2076
|
+
* const vector = d.vec3b(false, true, false); // (false, true, false)
|
|
2077
|
+
*/
|
|
2078
|
+
const vec3b$1 = makeVecSchema(Vec3bImpl, bool);
|
|
2079
|
+
/**
|
|
2080
|
+
* Schema representing vec4f - a vector with 4 elements of type f32.
|
|
2081
|
+
* Also a constructor function for this vector value.
|
|
2082
|
+
*
|
|
2083
|
+
* @example
|
|
2084
|
+
* const vector = d.vec4f(); // (0.0, 0.0, 0.0, 0.0)
|
|
2085
|
+
* const vector = d.vec4f(1); // (1.0, 1.0, 1.0, 1.0)
|
|
2086
|
+
* const vector = d.vec4f(1, 2, 3, 4.5); // (1.0, 2.0, 3.0, 4.5)
|
|
2087
|
+
*
|
|
2088
|
+
* @example
|
|
2089
|
+
* const buffer = root.createBuffer(d.vec4f, d.vec4f(0, 1, 2, 3)); // buffer holding a d.vec4f value, with an initial value of vec4f(0, 1, 2, 3);
|
|
2090
|
+
*/
|
|
2091
|
+
const vec4f$1 = makeVecSchema(Vec4fImpl, f32);
|
|
2092
|
+
/**
|
|
2093
|
+
* Schema representing vec4h - a vector with 4 elements of type f16.
|
|
2094
|
+
* Also a constructor function for this vector value.
|
|
2095
|
+
*
|
|
2096
|
+
* @example
|
|
2097
|
+
* const vector = d.vec4h(); // (0.0, 0.0, 0.0, 0.0)
|
|
2098
|
+
* const vector = d.vec4h(1); // (1.0, 1.0, 1.0, 1.0)
|
|
2099
|
+
* const vector = d.vec4h(1, 2, 3, 4.5); // (1.0, 2.0, 3.0, 4.5)
|
|
2100
|
+
*
|
|
2101
|
+
* @example
|
|
2102
|
+
* const buffer = root.createBuffer(d.vec4h, d.vec4h(0, 1, 2, 3)); // buffer holding a d.vec4h value, with an initial value of vec4h(0, 1, 2, 3);
|
|
2103
|
+
*/
|
|
2104
|
+
const vec4h$1 = makeVecSchema(Vec4hImpl, f16);
|
|
2105
|
+
/**
|
|
2106
|
+
* Schema representing vec4i - a vector with 4 elements of type i32.
|
|
2107
|
+
* Also a constructor function for this vector value.
|
|
2108
|
+
*
|
|
2109
|
+
* @example
|
|
2110
|
+
* const vector = d.vec4i(); // (0, 0, 0, 0)
|
|
2111
|
+
* const vector = d.vec4i(1); // (1, 1, 1, 1)
|
|
2112
|
+
* const vector = d.vec4i(1, 2, 3, -4); // (1, 2, 3, -4)
|
|
2113
|
+
*
|
|
2114
|
+
* @example
|
|
2115
|
+
* const buffer = root.createBuffer(d.vec4i, d.vec4i(0, 1, 2, 3)); // buffer holding a d.vec4i value, with an initial value of vec4i(0, 1, 2, 3);
|
|
2116
|
+
*/
|
|
2117
|
+
const vec4i$1 = makeVecSchema(Vec4iImpl, i32);
|
|
2118
|
+
/**
|
|
2119
|
+
* Schema representing vec4u - a vector with 4 elements of type u32.
|
|
2120
|
+
* Also a constructor function for this vector value.
|
|
2121
|
+
*
|
|
2122
|
+
* @example
|
|
2123
|
+
* const vector = d.vec4u(); // (0, 0, 0, 0)
|
|
2124
|
+
* const vector = d.vec4u(1); // (1, 1, 1, 1)
|
|
2125
|
+
* const vector = d.vec4u(1, 2, 3, 4); // (1, 2, 3, 4)
|
|
2126
|
+
*
|
|
2127
|
+
* @example
|
|
2128
|
+
* const buffer = root.createBuffer(d.vec4u, d.vec4u(0, 1, 2, 3)); // buffer holding a d.vec4u value, with an initial value of vec4u(0, 1, 2, 3);
|
|
2129
|
+
*/
|
|
2130
|
+
const vec4u$1 = makeVecSchema(Vec4uImpl, u32);
|
|
2131
|
+
/**
|
|
2132
|
+
* Schema representing `vec4<bool>` - a vector with 4 elements of type `bool`.
|
|
2133
|
+
* Also a constructor function for this vector value.
|
|
2134
|
+
*
|
|
2135
|
+
* @example
|
|
2136
|
+
* const vector = d.vec4b(); // (false, false, false, false)
|
|
2137
|
+
* const vector = d.vec4b(true); // (true, true, true, true)
|
|
2138
|
+
* const vector = d.vec4b(false, true, false, true); // (false, true, false, true)
|
|
2139
|
+
*/
|
|
2140
|
+
const vec4b$1 = makeVecSchema(Vec4bImpl, bool);
|
|
2141
|
+
const vecTypeToConstructor = {
|
|
2142
|
+
vec2f: vec2f$1,
|
|
2143
|
+
vec2h: vec2h$1,
|
|
2144
|
+
vec2i: vec2i$1,
|
|
2145
|
+
vec2u: vec2u$1,
|
|
2146
|
+
"vec2<bool>": vec2b$1,
|
|
2147
|
+
vec3f: vec3f$1,
|
|
2148
|
+
vec3h: vec3h$1,
|
|
2149
|
+
vec3i: vec3i$1,
|
|
2150
|
+
vec3u: vec3u$1,
|
|
2151
|
+
"vec3<bool>": vec3b$1,
|
|
2152
|
+
vec4f: vec4f$1,
|
|
2153
|
+
vec4h: vec4h$1,
|
|
2154
|
+
vec4i: vec4i$1,
|
|
2155
|
+
vec4u: vec4u$1,
|
|
2156
|
+
"vec4<bool>": vec4b$1
|
|
2157
|
+
};
|
|
2158
|
+
function makeVecSchema(VecImpl, primitive) {
|
|
2159
|
+
const { kind: type, length } = new VecImpl();
|
|
2160
|
+
const componentCount = length;
|
|
2161
|
+
const cpuConstruct = (...args) => {
|
|
2162
|
+
const values = Array.from({ length: args.length });
|
|
2163
|
+
let j = 0;
|
|
2164
|
+
for (const arg of args) if (typeof arg === "number" || typeof arg === "boolean") values[j++] = arg;
|
|
2165
|
+
else for (let c = 0; c < arg.length; ++c) values[j++] = arg[c];
|
|
2166
|
+
if (values.length <= 1 || values.length === componentCount) return new VecImpl(...values);
|
|
2167
|
+
throw new Error(`'${type}' constructor called with invalid number of arguments.`);
|
|
2168
|
+
};
|
|
2169
|
+
const construct = callableSchema({
|
|
2170
|
+
name: type,
|
|
2171
|
+
signature: (...args) => ({
|
|
2172
|
+
argTypes: args.map((arg) => isVec(arg) ? arg : primitive),
|
|
2173
|
+
returnType: schema
|
|
2174
|
+
}),
|
|
2175
|
+
normalImpl: cpuConstruct,
|
|
2176
|
+
codegenImpl: (_ctx, args) => {
|
|
2177
|
+
if (args.length === 1 && args[0]?.dataType === schema) return stitch`${args[0]}`;
|
|
2178
|
+
return stitch`${type}(${args})`;
|
|
2179
|
+
}
|
|
2180
|
+
});
|
|
2181
|
+
const schema = Object.assign(construct, {
|
|
2182
|
+
[$internal]: {},
|
|
2183
|
+
type,
|
|
2184
|
+
primitive,
|
|
2185
|
+
componentCount,
|
|
2186
|
+
[$repr]: void 0
|
|
2187
|
+
});
|
|
2188
|
+
VecImpl.prototype.schema = schema;
|
|
2189
|
+
return schema;
|
|
2190
|
+
}
|
|
2191
|
+
|
|
2192
|
+
//#endregion
|
|
2193
|
+
//#region src/nameRegistry.ts
|
|
2194
|
+
const bannedTokens = new Set([
|
|
2195
|
+
"alias",
|
|
2196
|
+
"break",
|
|
2197
|
+
"case",
|
|
2198
|
+
"const",
|
|
2199
|
+
"const_assert",
|
|
2200
|
+
"continue",
|
|
2201
|
+
"continuing",
|
|
2202
|
+
"default",
|
|
2203
|
+
"diagnostic",
|
|
2204
|
+
"discard",
|
|
2205
|
+
"else",
|
|
2206
|
+
"enable",
|
|
2207
|
+
"false",
|
|
2208
|
+
"fn",
|
|
2209
|
+
"for",
|
|
2210
|
+
"if",
|
|
2211
|
+
"let",
|
|
2212
|
+
"loop",
|
|
2213
|
+
"override",
|
|
2214
|
+
"requires",
|
|
2215
|
+
"return",
|
|
2216
|
+
"struct",
|
|
2217
|
+
"switch",
|
|
2218
|
+
"true",
|
|
2219
|
+
"var",
|
|
2220
|
+
"while",
|
|
2221
|
+
"NULL",
|
|
2222
|
+
"Self",
|
|
2223
|
+
"abstract",
|
|
2224
|
+
"active",
|
|
2225
|
+
"alignas",
|
|
2226
|
+
"alignof",
|
|
2227
|
+
"as",
|
|
2228
|
+
"asm",
|
|
2229
|
+
"asm_fragment",
|
|
2230
|
+
"async",
|
|
2231
|
+
"attribute",
|
|
2232
|
+
"auto",
|
|
2233
|
+
"await",
|
|
2234
|
+
"become",
|
|
2235
|
+
"cast",
|
|
2236
|
+
"catch",
|
|
2237
|
+
"class",
|
|
2238
|
+
"co_await",
|
|
2239
|
+
"co_return",
|
|
2240
|
+
"co_yield",
|
|
2241
|
+
"coherent",
|
|
2242
|
+
"column_major",
|
|
2243
|
+
"common",
|
|
2244
|
+
"compile",
|
|
2245
|
+
"compile_fragment",
|
|
2246
|
+
"concept",
|
|
2247
|
+
"const_cast",
|
|
2248
|
+
"consteval",
|
|
2249
|
+
"constexpr",
|
|
2250
|
+
"constinit",
|
|
2251
|
+
"crate",
|
|
2252
|
+
"debugger",
|
|
2253
|
+
"decltype",
|
|
2254
|
+
"delete",
|
|
2255
|
+
"demote",
|
|
2256
|
+
"demote_to_helper",
|
|
2257
|
+
"do",
|
|
2258
|
+
"dynamic_cast",
|
|
2259
|
+
"enum",
|
|
2260
|
+
"explicit",
|
|
2261
|
+
"export",
|
|
2262
|
+
"extends",
|
|
2263
|
+
"extern",
|
|
2264
|
+
"external",
|
|
2265
|
+
"fallthrough",
|
|
2266
|
+
"filter",
|
|
2267
|
+
"final",
|
|
2268
|
+
"finally",
|
|
2269
|
+
"friend",
|
|
2270
|
+
"from",
|
|
2271
|
+
"fxgroup",
|
|
2272
|
+
"get",
|
|
2273
|
+
"goto",
|
|
2274
|
+
"groupshared",
|
|
2275
|
+
"highp",
|
|
2276
|
+
"impl",
|
|
2277
|
+
"implements",
|
|
2278
|
+
"import",
|
|
2279
|
+
"inline",
|
|
2280
|
+
"instanceof",
|
|
2281
|
+
"interface",
|
|
2282
|
+
"layout",
|
|
2283
|
+
"lowp",
|
|
2284
|
+
"macro",
|
|
2285
|
+
"macro_rules",
|
|
2286
|
+
"match",
|
|
2287
|
+
"mediump",
|
|
2288
|
+
"meta",
|
|
2289
|
+
"mod",
|
|
2290
|
+
"module",
|
|
2291
|
+
"move",
|
|
2292
|
+
"mut",
|
|
2293
|
+
"mutable",
|
|
2294
|
+
"namespace",
|
|
2295
|
+
"new",
|
|
2296
|
+
"nil",
|
|
2297
|
+
"noexcept",
|
|
2298
|
+
"noinline",
|
|
2299
|
+
"nointerpolation",
|
|
2300
|
+
"non_coherent",
|
|
2301
|
+
"noncoherent",
|
|
2302
|
+
"noperspective",
|
|
2303
|
+
"null",
|
|
2304
|
+
"nullptr",
|
|
2305
|
+
"of",
|
|
2306
|
+
"operator",
|
|
2307
|
+
"package",
|
|
2308
|
+
"packoffset",
|
|
2309
|
+
"partition",
|
|
2310
|
+
"pass",
|
|
2311
|
+
"patch",
|
|
2312
|
+
"pixelfragment",
|
|
2313
|
+
"precise",
|
|
2314
|
+
"precision",
|
|
2315
|
+
"premerge",
|
|
2316
|
+
"priv",
|
|
2317
|
+
"protected",
|
|
2318
|
+
"pub",
|
|
2319
|
+
"public",
|
|
2320
|
+
"readonly",
|
|
2321
|
+
"ref",
|
|
2322
|
+
"regardless",
|
|
2323
|
+
"register",
|
|
2324
|
+
"reinterpret_cast",
|
|
2325
|
+
"require",
|
|
2326
|
+
"resource",
|
|
2327
|
+
"restrict",
|
|
2328
|
+
"self",
|
|
2329
|
+
"set",
|
|
2330
|
+
"shared",
|
|
2331
|
+
"sizeof",
|
|
2332
|
+
"smooth",
|
|
2333
|
+
"snorm",
|
|
2334
|
+
"static",
|
|
2335
|
+
"static_assert",
|
|
2336
|
+
"static_cast",
|
|
2337
|
+
"std",
|
|
2338
|
+
"subroutine",
|
|
2339
|
+
"super",
|
|
2340
|
+
"target",
|
|
2341
|
+
"template",
|
|
2342
|
+
"this",
|
|
2343
|
+
"thread_local",
|
|
2344
|
+
"throw",
|
|
2345
|
+
"trait",
|
|
2346
|
+
"try",
|
|
2347
|
+
"type",
|
|
2348
|
+
"typedef",
|
|
2349
|
+
"typeid",
|
|
2350
|
+
"typename",
|
|
2351
|
+
"typeof",
|
|
2352
|
+
"union",
|
|
2353
|
+
"unless",
|
|
2354
|
+
"unorm",
|
|
2355
|
+
"unsafe",
|
|
2356
|
+
"unsized",
|
|
2357
|
+
"use",
|
|
2358
|
+
"using",
|
|
2359
|
+
"varying",
|
|
2360
|
+
"virtual",
|
|
2361
|
+
"volatile",
|
|
2362
|
+
"wgsl",
|
|
2363
|
+
"where",
|
|
2364
|
+
"with",
|
|
2365
|
+
"writeonly",
|
|
2366
|
+
"yield",
|
|
2367
|
+
"sampler",
|
|
2368
|
+
"uniform",
|
|
2369
|
+
"storage"
|
|
2370
|
+
]);
|
|
2371
|
+
const builtins = new Set([
|
|
2372
|
+
"array",
|
|
2373
|
+
"bool",
|
|
2374
|
+
"f16",
|
|
2375
|
+
"f32",
|
|
2376
|
+
"i32",
|
|
2377
|
+
"u32",
|
|
2378
|
+
"mat2x2",
|
|
2379
|
+
"mat2x3",
|
|
2380
|
+
"mat2x4",
|
|
2381
|
+
"mat3x2",
|
|
2382
|
+
"mat3x3",
|
|
2383
|
+
"mat3x4",
|
|
2384
|
+
"mat4x2",
|
|
2385
|
+
"mat4x3",
|
|
2386
|
+
"mat4x4",
|
|
2387
|
+
"vec2",
|
|
2388
|
+
"vec3",
|
|
2389
|
+
"vec4",
|
|
2390
|
+
"bitcast",
|
|
2391
|
+
"all",
|
|
2392
|
+
"any",
|
|
2393
|
+
"select",
|
|
2394
|
+
"arrayLength",
|
|
2395
|
+
"abs",
|
|
2396
|
+
"acos",
|
|
2397
|
+
"acosh",
|
|
2398
|
+
"asin",
|
|
2399
|
+
"asinh",
|
|
2400
|
+
"atan",
|
|
2401
|
+
"atanh",
|
|
2402
|
+
"atan2",
|
|
2403
|
+
"ceil",
|
|
2404
|
+
"clamp",
|
|
2405
|
+
"cos",
|
|
2406
|
+
"cosh",
|
|
2407
|
+
"countLeadingZeros",
|
|
2408
|
+
"countOneBits",
|
|
2409
|
+
"countTrailingZeros",
|
|
2410
|
+
"cross",
|
|
2411
|
+
"degrees",
|
|
2412
|
+
"determinant",
|
|
2413
|
+
"distance",
|
|
2414
|
+
"dot",
|
|
2415
|
+
"dot4U8Packed",
|
|
2416
|
+
"dot4I8Packed",
|
|
2417
|
+
"exp",
|
|
2418
|
+
"exp2",
|
|
2419
|
+
"extractBits",
|
|
2420
|
+
"faceForward",
|
|
2421
|
+
"firstLeadingBit",
|
|
2422
|
+
"firstTrailingBit",
|
|
2423
|
+
"floor",
|
|
2424
|
+
"fma",
|
|
2425
|
+
"fract",
|
|
2426
|
+
"frexp",
|
|
2427
|
+
"insertBits",
|
|
2428
|
+
"inverseSqrt",
|
|
2429
|
+
"ldexp",
|
|
2430
|
+
"length",
|
|
2431
|
+
"log",
|
|
2432
|
+
"log2",
|
|
2433
|
+
"max",
|
|
2434
|
+
"min",
|
|
2435
|
+
"mix",
|
|
2436
|
+
"modf",
|
|
2437
|
+
"normalize",
|
|
2438
|
+
"pow",
|
|
2439
|
+
"quantizeToF16",
|
|
2440
|
+
"radians",
|
|
2441
|
+
"reflect",
|
|
2442
|
+
"refract",
|
|
2443
|
+
"reverseBits",
|
|
2444
|
+
"round",
|
|
2445
|
+
"saturate",
|
|
2446
|
+
"sign",
|
|
2447
|
+
"sin",
|
|
2448
|
+
"sinh",
|
|
2449
|
+
"smoothstep",
|
|
2450
|
+
"sqrt",
|
|
2451
|
+
"step",
|
|
2452
|
+
"tan",
|
|
2453
|
+
"tanh",
|
|
2454
|
+
"transpose",
|
|
2455
|
+
"trunc",
|
|
2456
|
+
"dpdx",
|
|
2457
|
+
"dpdxCoarse",
|
|
2458
|
+
"dpdxFine",
|
|
2459
|
+
"dpdy",
|
|
2460
|
+
"dpdyCoarse",
|
|
2461
|
+
"dpdyFine",
|
|
2462
|
+
"fwidth",
|
|
2463
|
+
"fwidthCoarse",
|
|
2464
|
+
"fwidthFine",
|
|
2465
|
+
"textureDimensions",
|
|
2466
|
+
"textureGather",
|
|
2467
|
+
"textureGatherCompare",
|
|
2468
|
+
"textureLoad",
|
|
2469
|
+
"textureNumLayers",
|
|
2470
|
+
"textureNumLevels",
|
|
2471
|
+
"textureNumSamples",
|
|
2472
|
+
"textureSample",
|
|
2473
|
+
"textureSampleBias",
|
|
2474
|
+
"textureSampleCompare",
|
|
2475
|
+
"textureSampleCompareLevel",
|
|
2476
|
+
"textureSampleGrad",
|
|
2477
|
+
"textureSampleLevel",
|
|
2478
|
+
"textureSampleBaseClampToEdge",
|
|
2479
|
+
"textureStore",
|
|
2480
|
+
"atomicLoad",
|
|
2481
|
+
"atomicStore",
|
|
2482
|
+
"atomicAdd",
|
|
2483
|
+
"atomicSub",
|
|
2484
|
+
"atomicMax",
|
|
2485
|
+
"atomicMin",
|
|
2486
|
+
"atomicAnd",
|
|
2487
|
+
"atomicOr",
|
|
2488
|
+
"atomicXor",
|
|
2489
|
+
"atomicExchange",
|
|
2490
|
+
"atomicCompareExchangeWeak",
|
|
2491
|
+
"pack4x8snorm",
|
|
2492
|
+
"pack4x8unorm",
|
|
2493
|
+
"pack4xI8",
|
|
2494
|
+
"pack4xU8",
|
|
2495
|
+
"pack4xI8Clamp",
|
|
2496
|
+
"pack4xU8Clamp",
|
|
2497
|
+
"pack2x16snorm",
|
|
2498
|
+
"pack2x16unorm",
|
|
2499
|
+
"pack2x16float",
|
|
2500
|
+
"unpack4x8snorm",
|
|
2501
|
+
"unpack4x8unorm",
|
|
2502
|
+
"unpack4xI8",
|
|
2503
|
+
"unpack4xU8",
|
|
2504
|
+
"unpack2x16snorm",
|
|
2505
|
+
"unpack2x16unorm",
|
|
2506
|
+
"unpack2x16float",
|
|
2507
|
+
"storageBarrier",
|
|
2508
|
+
"textureBarrier",
|
|
2509
|
+
"workgroupBarrier",
|
|
2510
|
+
"workgroupUniformLoad",
|
|
2511
|
+
"subgroupAdd",
|
|
2512
|
+
"subgroupExclusiveAdd",
|
|
2513
|
+
"subgroupInclusiveAdd",
|
|
2514
|
+
"subgroupAll",
|
|
2515
|
+
"subgroupAnd",
|
|
2516
|
+
"subgroupAny",
|
|
2517
|
+
"subgroupBallot",
|
|
2518
|
+
"subgroupBroadcast",
|
|
2519
|
+
"subgroupBroadcastFirst",
|
|
2520
|
+
"subgroupElect",
|
|
2521
|
+
"subgroupMax",
|
|
2522
|
+
"subgroupMin",
|
|
2523
|
+
"subgroupMul",
|
|
2524
|
+
"subgroupExclusiveMul",
|
|
2525
|
+
"subgroupInclusiveMul",
|
|
2526
|
+
"subgroupOr",
|
|
2527
|
+
"subgroupShuffle",
|
|
2528
|
+
"subgroupShuffleDown",
|
|
2529
|
+
"subgroupShuffleUp",
|
|
2530
|
+
"subgroupShuffleXor",
|
|
2531
|
+
"subgroupXor",
|
|
2532
|
+
"quadBroadcast",
|
|
2533
|
+
"quadSwapDiagonal",
|
|
2534
|
+
"quadSwapX",
|
|
2535
|
+
"quadSwapY"
|
|
2536
|
+
]);
|
|
2537
|
+
function sanitizePrimer(primer) {
|
|
2538
|
+
if (primer) return primer.replaceAll(/\s/g, "_").replaceAll(/[^\w\d]/g, "");
|
|
2539
|
+
return "item";
|
|
2540
|
+
}
|
|
2541
|
+
/**
|
|
2542
|
+
* A function for checking whether an identifier needs renaming.
|
|
2543
|
+
* Throws if provided with an invalid identifier that cannot be easily renamed.
|
|
2544
|
+
* @example
|
|
2545
|
+
* isValidIdentifier("ident"); // true
|
|
2546
|
+
* isValidIdentifier("struct"); // false
|
|
2547
|
+
* isValidIdentifier("struct_1"); // false
|
|
2548
|
+
* isValidIdentifier("_"); // ERROR
|
|
2549
|
+
* isValidIdentifier("my variable"); // ERROR
|
|
2550
|
+
*/
|
|
2551
|
+
function isValidIdentifier(ident) {
|
|
2552
|
+
if (ident === "_" || ident.startsWith("__") || /\s/.test(ident)) throw new Error(`Invalid identifier '${ident}'. Choose an identifier without whitespaces or leading underscores.`);
|
|
2553
|
+
const prefix = ident.split("_")[0];
|
|
2554
|
+
return !bannedTokens.has(prefix) && !builtins.has(prefix);
|
|
2555
|
+
}
|
|
2556
|
+
/**
|
|
2557
|
+
* Same as `isValidIdentifier`, except does not check for builtin clashes.
|
|
2558
|
+
*/
|
|
2559
|
+
function isValidProp(ident) {
|
|
2560
|
+
if (ident === "_" || ident.startsWith("__") || /\s/.test(ident)) throw new Error(`Invalid identifier '${ident}'. Choose an identifier without whitespaces or leading underscores.`);
|
|
2561
|
+
const prefix = ident.split("_")[0];
|
|
2562
|
+
return !bannedTokens.has(prefix);
|
|
2563
|
+
}
|
|
2564
|
+
var NameRegistryImpl = class {
|
|
2565
|
+
#usedNames;
|
|
2566
|
+
#scopeStack;
|
|
2567
|
+
constructor() {
|
|
2568
|
+
this.#usedNames = new Set([...bannedTokens, ...builtins]);
|
|
2569
|
+
this.#scopeStack = [];
|
|
2570
|
+
}
|
|
2571
|
+
get #usedBlockScopeNames() {
|
|
2572
|
+
return this.#scopeStack[this.#scopeStack.length - 1]?.usedBlockScopeNames;
|
|
2573
|
+
}
|
|
2574
|
+
makeUnique(primer, global) {
|
|
2575
|
+
const sanitizedPrimer = sanitizePrimer(primer);
|
|
2576
|
+
const name = this.getUniqueVariant(sanitizedPrimer);
|
|
2577
|
+
if (global) this.#usedNames.add(name);
|
|
2578
|
+
else this.#usedBlockScopeNames?.add(name);
|
|
2579
|
+
return name;
|
|
2580
|
+
}
|
|
2581
|
+
#isUsedInBlocksBefore(name) {
|
|
2582
|
+
const functionScopeIndex = this.#scopeStack.findLastIndex((scope) => scope.type === "functionScope");
|
|
2583
|
+
return this.#scopeStack.slice(functionScopeIndex + 1).some((scope) => scope.usedBlockScopeNames.has(name));
|
|
2584
|
+
}
|
|
2585
|
+
makeValid(primer) {
|
|
2586
|
+
if (isValidIdentifier(primer) && !this.#usedNames.has(primer) && !this.#isUsedInBlocksBefore(primer)) {
|
|
2587
|
+
this.#usedBlockScopeNames?.add(primer);
|
|
2588
|
+
return primer;
|
|
2589
|
+
}
|
|
2590
|
+
return this.makeUnique(primer, false);
|
|
2591
|
+
}
|
|
2592
|
+
isUsed(name) {
|
|
2593
|
+
return this.#usedNames.has(name) || this.#isUsedInBlocksBefore(name);
|
|
2594
|
+
}
|
|
2595
|
+
pushFunctionScope() {
|
|
2596
|
+
this.#scopeStack.push({ type: "functionScope" });
|
|
2597
|
+
this.#scopeStack.push({
|
|
2598
|
+
type: "blockScope",
|
|
2599
|
+
usedBlockScopeNames: /* @__PURE__ */ new Set()
|
|
2600
|
+
});
|
|
2601
|
+
}
|
|
2602
|
+
popFunctionScope() {
|
|
2603
|
+
const functionScopeIndex = this.#scopeStack.findLastIndex((scope) => scope.type === "functionScope");
|
|
2604
|
+
if (functionScopeIndex === -1) throw new Error("Tried to pop function scope when no scope was present.");
|
|
2605
|
+
this.#scopeStack.splice(functionScopeIndex);
|
|
2606
|
+
}
|
|
2607
|
+
pushBlockScope() {
|
|
2608
|
+
this.#scopeStack.push({
|
|
2609
|
+
type: "blockScope",
|
|
2610
|
+
usedBlockScopeNames: /* @__PURE__ */ new Set()
|
|
2611
|
+
});
|
|
2612
|
+
}
|
|
2613
|
+
popBlockScope() {
|
|
2614
|
+
invariant(this.#scopeStack[this.#scopeStack.length - 1]?.type === "blockScope", "Tried to pop block scope, but it is not present");
|
|
2615
|
+
this.#scopeStack.pop();
|
|
2616
|
+
}
|
|
2617
|
+
};
|
|
2618
|
+
var RandomNameRegistry = class extends NameRegistryImpl {
|
|
2619
|
+
#lastUniqueId = 0;
|
|
2620
|
+
getUniqueVariant(base) {
|
|
2621
|
+
let name = `${base}_${this.#lastUniqueId++}`;
|
|
2622
|
+
while (this.isUsed(name)) name = `${base}_${this.#lastUniqueId++}`;
|
|
2623
|
+
return name;
|
|
2624
|
+
}
|
|
2625
|
+
};
|
|
2626
|
+
var StrictNameRegistry = class extends NameRegistryImpl {
|
|
2627
|
+
getUniqueVariant(base) {
|
|
2628
|
+
let index = 0;
|
|
2629
|
+
let name = base;
|
|
2630
|
+
while (this.isUsed(name)) {
|
|
2631
|
+
index++;
|
|
2632
|
+
name = `${base}_${index}`;
|
|
2633
|
+
}
|
|
2634
|
+
return name;
|
|
2635
|
+
}
|
|
2636
|
+
};
|
|
2637
|
+
|
|
2638
|
+
//#endregion
|
|
2639
|
+
//#region src/data/struct.ts
|
|
2640
|
+
/**
|
|
2641
|
+
* Creates a struct schema that can be used to construct GPU buffers.
|
|
2642
|
+
* Ensures proper alignment and padding of properties (as opposed to a `d.unstruct` schema).
|
|
2643
|
+
* The order of members matches the passed in properties object.
|
|
2644
|
+
*
|
|
2645
|
+
* @example
|
|
2646
|
+
* const CircleStruct = d.struct({ radius: d.f32, pos: d.vec3f });
|
|
2647
|
+
*
|
|
2648
|
+
* @param props Record with `string` keys and `TgpuData` values,
|
|
2649
|
+
* each entry describing one struct member.
|
|
2650
|
+
*/
|
|
2651
|
+
function struct(props) {
|
|
2652
|
+
return INTERNAL_createStruct(props, false);
|
|
2653
|
+
}
|
|
2654
|
+
function abstruct(props) {
|
|
2655
|
+
return INTERNAL_createStruct(props, true);
|
|
2656
|
+
}
|
|
2657
|
+
function INTERNAL_createStruct(props, isAbstruct) {
|
|
2658
|
+
Object.keys(props).forEach((key) => {
|
|
2659
|
+
if (!isValidProp(key)) throw new Error(`Property key '${key}' is a reserved WGSL word. Choose a different name.`);
|
|
2660
|
+
});
|
|
2661
|
+
const structSchema = (instanceProps) => Object.fromEntries(Object.entries(props).map(([key, schema]) => [key, schemaCallWrapper(schema, instanceProps?.[key])]));
|
|
2662
|
+
Object.setPrototypeOf(structSchema, WgslStructImpl);
|
|
2663
|
+
structSchema.propTypes = props;
|
|
2664
|
+
Object.defineProperty(structSchema, $internal, { value: { isAbstruct } });
|
|
2665
|
+
return structSchema;
|
|
2666
|
+
}
|
|
2667
|
+
const WgslStructImpl = {
|
|
2668
|
+
type: "struct",
|
|
2669
|
+
$name(label) {
|
|
2670
|
+
setName(this, label);
|
|
2671
|
+
return this;
|
|
2672
|
+
},
|
|
2673
|
+
toString() {
|
|
2674
|
+
return `struct:${getName(this) ?? "<unnamed>"}`;
|
|
2675
|
+
}
|
|
2676
|
+
};
|
|
2677
|
+
|
|
2678
|
+
//#endregion
|
|
2679
|
+
//#region src/tgsl/generationHelpers.ts
|
|
2680
|
+
function numericLiteralToSnippet(value) {
|
|
2681
|
+
if (value >= 2 ** 63 || value < -(2 ** 63)) return snip(value, abstractFloat, "constant");
|
|
2682
|
+
if (Number.isInteger(value)) {
|
|
2683
|
+
if (!Number.isSafeInteger(value)) console.warn(`The integer ${value} exceeds the safe integer range and may have lost precision.`);
|
|
2684
|
+
return snip(value, abstractInt, "constant");
|
|
2685
|
+
}
|
|
2686
|
+
return snip(value, abstractFloat, "constant");
|
|
2687
|
+
}
|
|
2688
|
+
function concretize(type) {
|
|
2689
|
+
if (type.type === "abstractFloat") return f32;
|
|
2690
|
+
if (type.type === "abstractInt") return i32;
|
|
2691
|
+
return type;
|
|
2692
|
+
}
|
|
2693
|
+
function concretizeSnippet(snippet) {
|
|
2694
|
+
return snip(snippet.value, concretize(snippet.dataType), snippet.origin);
|
|
2695
|
+
}
|
|
2696
|
+
function concretizeSnippets(args) {
|
|
2697
|
+
return args.map(concretizeSnippet);
|
|
2698
|
+
}
|
|
2699
|
+
function coerceToSnippet(value) {
|
|
2700
|
+
if (isSnippet(value)) return value;
|
|
2701
|
+
if (isRef(value)) throw new Error("Cannot use refs (d.ref(...)) from the outer scope.");
|
|
2702
|
+
const ownSnippet = getOwnSnippet(value);
|
|
2703
|
+
if (ownSnippet) return ownSnippet;
|
|
2704
|
+
if (isVecInstance(value) || isMatInstance(value)) return snip(value, WORKAROUND_getSchema(value), "constant");
|
|
2705
|
+
if (typeof value === "string" || typeof value === "function" || typeof value === "object" || typeof value === "symbol" || typeof value === "undefined" || value === null) return snip(value, UnknownData, "constant");
|
|
2706
|
+
if (typeof value === "number") return numericLiteralToSnippet(value);
|
|
2707
|
+
if (typeof value === "boolean") return snip(value, bool, "constant");
|
|
2708
|
+
return snip(value, UnknownData, "constant");
|
|
2709
|
+
}
|
|
2710
|
+
/**
|
|
2711
|
+
* Intermediate representation for WGSL array expressions.
|
|
2712
|
+
* Defers resolution. Stores array elements as snippets so the
|
|
2713
|
+
* generator can access them when needed.
|
|
2714
|
+
*/
|
|
2715
|
+
var ArrayExpression = class {
|
|
2716
|
+
[$internal] = true;
|
|
2717
|
+
constructor(type, elements) {
|
|
2718
|
+
this.type = type;
|
|
2719
|
+
this.elements = elements;
|
|
2720
|
+
}
|
|
2721
|
+
toString() {
|
|
2722
|
+
return "ArrayExpression";
|
|
2723
|
+
}
|
|
2724
|
+
[$resolve](ctx) {
|
|
2725
|
+
for (const elem of this.elements) if (elem.origin === "argument" && !isNaturallyEphemeral(elem.dataType) || !isEphemeralSnippet(elem)) {
|
|
2726
|
+
const snippetStr = ctx.resolve(elem.value, elem.dataType).value;
|
|
2727
|
+
const snippetType = ctx.resolve(concretize(elem.dataType)).value;
|
|
2728
|
+
throw new WgslTypeError(`'${snippetStr}' reference cannot be used in an array constructor.\n-----\nTry '${snippetType}(${snippetStr})' or 'arrayOf(${snippetType}, count)([...])' to copy the value instead.\n-----`);
|
|
2729
|
+
}
|
|
2730
|
+
return snip(stitch`${`array<${ctx.resolve(this.type.elementType).value}, ${this.elements.length}>`}(${this.elements})`, this.type, "runtime");
|
|
2731
|
+
}
|
|
2732
|
+
};
|
|
2733
|
+
|
|
2734
|
+
//#endregion
|
|
2735
|
+
//#region src/core/function/dualImpl.ts
|
|
2736
|
+
var MissingCpuImplError = class extends Error {
|
|
2737
|
+
constructor(message) {
|
|
2738
|
+
super(message);
|
|
2739
|
+
this.name = this.constructor.name;
|
|
2740
|
+
}
|
|
2741
|
+
};
|
|
2742
|
+
function dualImpl(options) {
|
|
2743
|
+
const impl = ((...args) => {
|
|
2744
|
+
if (typeof options.normalImpl === "string") throw new MissingCpuImplError(options.normalImpl);
|
|
2745
|
+
return options.normalImpl(...args);
|
|
2746
|
+
});
|
|
2747
|
+
setName(impl, options.name);
|
|
2748
|
+
impl.toString = () => options.name ?? "<unknown>";
|
|
2749
|
+
impl[$gpuCallable] = {
|
|
2750
|
+
get strictSignature() {
|
|
2751
|
+
return typeof options.signature !== "function" ? options.signature : void 0;
|
|
2752
|
+
},
|
|
2753
|
+
call(ctx, args) {
|
|
2754
|
+
const { argTypes, returnType } = typeof options.signature === "function" ? options.signature(...args.map((s) => {
|
|
2755
|
+
if (isPtr(s.dataType) && s.dataType.implicit) return s.dataType.inner;
|
|
2756
|
+
return s.dataType;
|
|
2757
|
+
})) : options.signature;
|
|
2758
|
+
const converted = args.map((s, idx) => {
|
|
2759
|
+
const argType = argTypes[idx];
|
|
2760
|
+
if (!argType) throw new Error("Function called with invalid arguments");
|
|
2761
|
+
return tryConvertSnippet(ctx, s, argType, !options.ignoreImplicitCastWarning);
|
|
2762
|
+
});
|
|
2763
|
+
if (!options.noComptime && converted.every((s) => isKnownAtComptime(s)) && typeof options.normalImpl === "function") {
|
|
2764
|
+
ctx.pushMode(new NormalState());
|
|
2765
|
+
try {
|
|
2766
|
+
return snip(options.normalImpl(...converted.map((s) => s.value)), returnType, "constant");
|
|
2767
|
+
} catch (e) {
|
|
2768
|
+
if (!(e instanceof MissingCpuImplError)) throw e;
|
|
2769
|
+
} finally {
|
|
2770
|
+
ctx.popMode("normal");
|
|
2771
|
+
}
|
|
2772
|
+
}
|
|
2773
|
+
return snip(options.codegenImpl(ctx, converted), concretize(returnType), "runtime");
|
|
2774
|
+
}
|
|
2775
|
+
};
|
|
2776
|
+
return impl;
|
|
2777
|
+
}
|
|
2778
|
+
|
|
2779
|
+
//#endregion
|
|
2780
|
+
//#region src/core/function/comptime.ts
|
|
2781
|
+
/**
|
|
2782
|
+
* Creates a version of `func` that can called safely in a TypeGPU function to
|
|
2783
|
+
* precompute and inject a value into the final shader code.
|
|
2784
|
+
*
|
|
2785
|
+
* Note how the function passed into `comptime` doesn't have to be marked with
|
|
2786
|
+
* 'use gpu'. That's because the function doesn't execute on the GPU, it gets
|
|
2787
|
+
* executed before the shader code gets sent to the GPU.
|
|
2788
|
+
*
|
|
2789
|
+
* @example
|
|
2790
|
+
* ```ts
|
|
2791
|
+
* const color = tgpu.comptime((int: number) => {
|
|
2792
|
+
* const r = (int >> 16) & 0xff;
|
|
2793
|
+
* const g = (int >> 8) & 0xff;
|
|
2794
|
+
* const b = int & 0xff;
|
|
2795
|
+
* return d.vec3f(r / 255, g / 255, b / 255);
|
|
2796
|
+
* });
|
|
2797
|
+
*
|
|
2798
|
+
* const material = (diffuse: d.v3f): d.v3f => {
|
|
2799
|
+
* 'use gpu';
|
|
2800
|
+
* const albedo = color(0xff00ff);
|
|
2801
|
+
* return albedo.mul(diffuse);
|
|
2802
|
+
* };
|
|
2803
|
+
* ```
|
|
2804
|
+
*/
|
|
2805
|
+
function comptime(func) {
|
|
2806
|
+
const impl = ((...args) => {
|
|
2807
|
+
return func(...args);
|
|
2808
|
+
});
|
|
2809
|
+
impl.toString = () => "comptime";
|
|
2810
|
+
impl[$getNameForward] = func;
|
|
2811
|
+
impl[$gpuCallable] = { call(_ctx, args) {
|
|
2812
|
+
if (!args.every((s) => isKnownAtComptime(s))) throw new WgslTypeError(`Called comptime function with runtime-known values: ${args.filter((s) => !isKnownAtComptime(s)).map((s) => `'${s.value}'`).join(", ")}`);
|
|
2813
|
+
return coerceToSnippet(func(...args.map((s) => s.value)));
|
|
2814
|
+
} };
|
|
2815
|
+
impl.$name = (label) => {
|
|
2816
|
+
setName(func, label);
|
|
2817
|
+
return impl;
|
|
2818
|
+
};
|
|
2819
|
+
Object.defineProperty(impl, $internal, { value: { isComptime: true } });
|
|
2820
|
+
return impl;
|
|
2821
|
+
}
|
|
2822
|
+
|
|
2823
|
+
//#endregion
|
|
2824
|
+
//#region src/data/matrix.ts
|
|
2825
|
+
var MatBase = class {};
|
|
2826
|
+
function createMatSchema(options) {
|
|
2827
|
+
const construct = callableSchema({
|
|
2828
|
+
name: options.type,
|
|
2829
|
+
normalImpl: (...args) => {
|
|
2830
|
+
const elements = [];
|
|
2831
|
+
for (const arg of args) if (typeof arg === "number") elements.push(arg);
|
|
2832
|
+
else for (let i = 0; i < arg.length; ++i) elements.push(arg[i]);
|
|
2833
|
+
if (elements.length !== 0 && elements.length !== options.columns * options.rows) throw new Error(`'${options.type}' constructor called with invalid number of arguments.`);
|
|
2834
|
+
for (let i = elements.length; i < options.columns * options.rows; ++i) elements.push(0);
|
|
2835
|
+
return new options.MatImpl(...elements);
|
|
2836
|
+
},
|
|
2837
|
+
signature: (...args) => ({
|
|
2838
|
+
argTypes: args.map((arg) => isVec(arg) ? arg : f32),
|
|
2839
|
+
returnType: schema
|
|
2840
|
+
}),
|
|
2841
|
+
codegenImpl: (_ctx, args) => stitch`${options.type}(${args})`
|
|
2842
|
+
});
|
|
2843
|
+
const schema = Object.assign(construct, {
|
|
2844
|
+
[$internal]: {},
|
|
2845
|
+
type: options.type,
|
|
2846
|
+
primitive: f32,
|
|
2847
|
+
identity: identityFunctions[options.columns],
|
|
2848
|
+
translation: options.columns === 4 ? translation4 : void 0,
|
|
2849
|
+
scaling: options.columns === 4 ? scaling4 : void 0,
|
|
2850
|
+
rotationX: options.columns === 4 ? rotationX4 : void 0,
|
|
2851
|
+
rotationY: options.columns === 4 ? rotationY4 : void 0,
|
|
2852
|
+
rotationZ: options.columns === 4 ? rotationZ4 : void 0
|
|
2853
|
+
});
|
|
2854
|
+
options.MatImpl.prototype.schema = schema;
|
|
2855
|
+
return schema;
|
|
2856
|
+
}
|
|
2857
|
+
var mat2x2Impl = class extends MatBase {
|
|
2858
|
+
[$internal] = true;
|
|
2859
|
+
columns;
|
|
2860
|
+
length = 4;
|
|
2861
|
+
constructor(...elements) {
|
|
2862
|
+
super();
|
|
2863
|
+
this.columns = [this.makeColumn(elements[0], elements[1]), this.makeColumn(elements[2], elements[3])];
|
|
2864
|
+
}
|
|
2865
|
+
get 0() {
|
|
2866
|
+
return this.columns[0].x;
|
|
2867
|
+
}
|
|
2868
|
+
get 1() {
|
|
2869
|
+
return this.columns[0].y;
|
|
2870
|
+
}
|
|
2871
|
+
get 2() {
|
|
2872
|
+
return this.columns[1].x;
|
|
2873
|
+
}
|
|
2874
|
+
get 3() {
|
|
2875
|
+
return this.columns[1].y;
|
|
2876
|
+
}
|
|
2877
|
+
set 0(value) {
|
|
2878
|
+
this.columns[0].x = value;
|
|
2879
|
+
}
|
|
2880
|
+
set 1(value) {
|
|
2881
|
+
this.columns[0].y = value;
|
|
2882
|
+
}
|
|
2883
|
+
set 2(value) {
|
|
2884
|
+
this.columns[1].x = value;
|
|
2885
|
+
}
|
|
2886
|
+
set 3(value) {
|
|
2887
|
+
this.columns[1].y = value;
|
|
2888
|
+
}
|
|
2889
|
+
*[Symbol.iterator]() {
|
|
2890
|
+
yield this[0];
|
|
2891
|
+
yield this[1];
|
|
2892
|
+
yield this[2];
|
|
2893
|
+
yield this[3];
|
|
2894
|
+
}
|
|
2895
|
+
[$resolve]() {
|
|
2896
|
+
return snip(`${this.kind}(${Array.from({ length: this.length }).map((_, i) => this[i]).join(", ")})`, mat2x2f, "runtime");
|
|
2897
|
+
}
|
|
2898
|
+
toString() {
|
|
2899
|
+
return this[$resolve]().value;
|
|
2900
|
+
}
|
|
2901
|
+
};
|
|
2902
|
+
var mat2x2fImpl = class extends mat2x2Impl {
|
|
2903
|
+
kind = "mat2x2f";
|
|
2904
|
+
makeColumn(e0, e1) {
|
|
2905
|
+
return vec2f$1(e0, e1);
|
|
2906
|
+
}
|
|
2907
|
+
};
|
|
2908
|
+
var mat3x3Impl = class extends MatBase {
|
|
2909
|
+
[$internal] = true;
|
|
2910
|
+
columns;
|
|
2911
|
+
length = 12;
|
|
2912
|
+
constructor(...elements) {
|
|
2913
|
+
super();
|
|
2914
|
+
this.columns = [
|
|
2915
|
+
this.makeColumn(elements[0], elements[1], elements[2]),
|
|
2916
|
+
this.makeColumn(elements[3], elements[4], elements[5]),
|
|
2917
|
+
this.makeColumn(elements[6], elements[7], elements[8])
|
|
2918
|
+
];
|
|
2919
|
+
}
|
|
2920
|
+
get 0() {
|
|
2921
|
+
return this.columns[0].x;
|
|
2922
|
+
}
|
|
2923
|
+
get 1() {
|
|
2924
|
+
return this.columns[0].y;
|
|
2925
|
+
}
|
|
2926
|
+
get 2() {
|
|
2927
|
+
return this.columns[0].z;
|
|
2928
|
+
}
|
|
2929
|
+
get 3() {
|
|
2930
|
+
return 0;
|
|
2931
|
+
}
|
|
2932
|
+
get 4() {
|
|
2933
|
+
return this.columns[1].x;
|
|
2934
|
+
}
|
|
2935
|
+
get 5() {
|
|
2936
|
+
return this.columns[1].y;
|
|
2937
|
+
}
|
|
2938
|
+
get 6() {
|
|
2939
|
+
return this.columns[1].z;
|
|
2940
|
+
}
|
|
2941
|
+
get 7() {
|
|
2942
|
+
return 0;
|
|
2943
|
+
}
|
|
2944
|
+
get 8() {
|
|
2945
|
+
return this.columns[2].x;
|
|
2946
|
+
}
|
|
2947
|
+
get 9() {
|
|
2948
|
+
return this.columns[2].y;
|
|
2949
|
+
}
|
|
2950
|
+
get 10() {
|
|
2951
|
+
return this.columns[2].z;
|
|
2952
|
+
}
|
|
2953
|
+
get 11() {
|
|
2954
|
+
return 0;
|
|
2955
|
+
}
|
|
2956
|
+
set 0(value) {
|
|
2957
|
+
this.columns[0].x = value;
|
|
2958
|
+
}
|
|
2959
|
+
set 1(value) {
|
|
2960
|
+
this.columns[0].y = value;
|
|
2961
|
+
}
|
|
2962
|
+
set 2(value) {
|
|
2963
|
+
this.columns[0].z = value;
|
|
2964
|
+
}
|
|
2965
|
+
set 3(_) {}
|
|
2966
|
+
set 4(value) {
|
|
2967
|
+
this.columns[1].x = value;
|
|
2968
|
+
}
|
|
2969
|
+
set 5(value) {
|
|
2970
|
+
this.columns[1].y = value;
|
|
2971
|
+
}
|
|
2972
|
+
set 6(value) {
|
|
2973
|
+
this.columns[1].z = value;
|
|
2974
|
+
}
|
|
2975
|
+
set 7(_) {}
|
|
2976
|
+
set 8(value) {
|
|
2977
|
+
this.columns[2].x = value;
|
|
2978
|
+
}
|
|
2979
|
+
set 9(value) {
|
|
2980
|
+
this.columns[2].y = value;
|
|
2981
|
+
}
|
|
2982
|
+
set 10(value) {
|
|
2983
|
+
this.columns[2].z = value;
|
|
2984
|
+
}
|
|
2985
|
+
set 11(_) {}
|
|
2986
|
+
*[Symbol.iterator]() {
|
|
2987
|
+
for (let i = 0; i < 12; i++) yield this[i];
|
|
2988
|
+
}
|
|
2989
|
+
[$resolve]() {
|
|
2990
|
+
return snip(`${this.kind}(${this[0]}, ${this[1]}, ${this[2]}, ${this[4]}, ${this[5]}, ${this[6]}, ${this[8]}, ${this[9]}, ${this[10]})`, mat3x3f, "runtime");
|
|
2991
|
+
}
|
|
2992
|
+
toString() {
|
|
2993
|
+
return this[$resolve]().value;
|
|
2994
|
+
}
|
|
2995
|
+
};
|
|
2996
|
+
var mat3x3fImpl = class extends mat3x3Impl {
|
|
2997
|
+
kind = "mat3x3f";
|
|
2998
|
+
makeColumn(x, y, z) {
|
|
2999
|
+
return vec3f$1(x, y, z);
|
|
3000
|
+
}
|
|
3001
|
+
};
|
|
3002
|
+
var mat4x4Impl = class extends MatBase {
|
|
3003
|
+
[$internal] = true;
|
|
3004
|
+
columns;
|
|
3005
|
+
constructor(...elements) {
|
|
3006
|
+
super();
|
|
3007
|
+
this.columns = [
|
|
3008
|
+
this.makeColumn(elements[0], elements[1], elements[2], elements[3]),
|
|
3009
|
+
this.makeColumn(elements[4], elements[5], elements[6], elements[7]),
|
|
3010
|
+
this.makeColumn(elements[8], elements[9], elements[10], elements[11]),
|
|
3011
|
+
this.makeColumn(elements[12], elements[13], elements[14], elements[15])
|
|
3012
|
+
];
|
|
3013
|
+
}
|
|
3014
|
+
length = 16;
|
|
3015
|
+
get 0() {
|
|
3016
|
+
return this.columns[0].x;
|
|
3017
|
+
}
|
|
3018
|
+
get 1() {
|
|
3019
|
+
return this.columns[0].y;
|
|
3020
|
+
}
|
|
3021
|
+
get 2() {
|
|
3022
|
+
return this.columns[0].z;
|
|
3023
|
+
}
|
|
3024
|
+
get 3() {
|
|
3025
|
+
return this.columns[0].w;
|
|
3026
|
+
}
|
|
3027
|
+
get 4() {
|
|
3028
|
+
return this.columns[1].x;
|
|
3029
|
+
}
|
|
3030
|
+
get 5() {
|
|
3031
|
+
return this.columns[1].y;
|
|
3032
|
+
}
|
|
3033
|
+
get 6() {
|
|
3034
|
+
return this.columns[1].z;
|
|
3035
|
+
}
|
|
3036
|
+
get 7() {
|
|
3037
|
+
return this.columns[1].w;
|
|
3038
|
+
}
|
|
3039
|
+
get 8() {
|
|
3040
|
+
return this.columns[2].x;
|
|
3041
|
+
}
|
|
3042
|
+
get 9() {
|
|
3043
|
+
return this.columns[2].y;
|
|
3044
|
+
}
|
|
3045
|
+
get 10() {
|
|
3046
|
+
return this.columns[2].z;
|
|
3047
|
+
}
|
|
3048
|
+
get 11() {
|
|
3049
|
+
return this.columns[2].w;
|
|
3050
|
+
}
|
|
3051
|
+
get 12() {
|
|
3052
|
+
return this.columns[3].x;
|
|
3053
|
+
}
|
|
3054
|
+
get 13() {
|
|
3055
|
+
return this.columns[3].y;
|
|
3056
|
+
}
|
|
3057
|
+
get 14() {
|
|
3058
|
+
return this.columns[3].z;
|
|
3059
|
+
}
|
|
3060
|
+
get 15() {
|
|
3061
|
+
return this.columns[3].w;
|
|
3062
|
+
}
|
|
3063
|
+
set 0(value) {
|
|
3064
|
+
this.columns[0].x = value;
|
|
3065
|
+
}
|
|
3066
|
+
set 1(value) {
|
|
3067
|
+
this.columns[0].y = value;
|
|
3068
|
+
}
|
|
3069
|
+
set 2(value) {
|
|
3070
|
+
this.columns[0].z = value;
|
|
3071
|
+
}
|
|
3072
|
+
set 3(value) {
|
|
3073
|
+
this.columns[0].w = value;
|
|
3074
|
+
}
|
|
3075
|
+
set 4(value) {
|
|
3076
|
+
this.columns[1].x = value;
|
|
3077
|
+
}
|
|
3078
|
+
set 5(value) {
|
|
3079
|
+
this.columns[1].y = value;
|
|
3080
|
+
}
|
|
3081
|
+
set 6(value) {
|
|
3082
|
+
this.columns[1].z = value;
|
|
3083
|
+
}
|
|
3084
|
+
set 7(value) {
|
|
3085
|
+
this.columns[1].w = value;
|
|
3086
|
+
}
|
|
3087
|
+
set 8(value) {
|
|
3088
|
+
this.columns[2].x = value;
|
|
3089
|
+
}
|
|
3090
|
+
set 9(value) {
|
|
3091
|
+
this.columns[2].y = value;
|
|
3092
|
+
}
|
|
3093
|
+
set 10(value) {
|
|
3094
|
+
this.columns[2].z = value;
|
|
3095
|
+
}
|
|
3096
|
+
set 11(value) {
|
|
3097
|
+
this.columns[2].w = value;
|
|
3098
|
+
}
|
|
3099
|
+
set 12(value) {
|
|
3100
|
+
this.columns[3].x = value;
|
|
3101
|
+
}
|
|
3102
|
+
set 13(value) {
|
|
3103
|
+
this.columns[3].y = value;
|
|
3104
|
+
}
|
|
3105
|
+
set 14(value) {
|
|
3106
|
+
this.columns[3].z = value;
|
|
3107
|
+
}
|
|
3108
|
+
set 15(value) {
|
|
3109
|
+
this.columns[3].w = value;
|
|
3110
|
+
}
|
|
3111
|
+
*[Symbol.iterator]() {
|
|
3112
|
+
for (let i = 0; i < 16; i++) yield this[i];
|
|
3113
|
+
}
|
|
3114
|
+
[$resolve]() {
|
|
3115
|
+
return snip(`${this.kind}(${Array.from({ length: this.length }).map((_, i) => this[i]).join(", ")})`, mat4x4f, "runtime");
|
|
3116
|
+
}
|
|
3117
|
+
toString() {
|
|
3118
|
+
return this[$resolve]().value;
|
|
3119
|
+
}
|
|
3120
|
+
};
|
|
3121
|
+
var mat4x4fImpl = class extends mat4x4Impl {
|
|
3122
|
+
kind = "mat4x4f";
|
|
3123
|
+
makeColumn(x, y, z, w) {
|
|
3124
|
+
return vec4f$1(x, y, z, w);
|
|
3125
|
+
}
|
|
3126
|
+
};
|
|
3127
|
+
/**
|
|
3128
|
+
* Returns a 2-by-2 identity matrix.
|
|
3129
|
+
* @returns {m2x2f} The result matrix.
|
|
3130
|
+
*/
|
|
3131
|
+
const identity2 = comptime(() => mat2x2f(1, 0, 0, 1)).$name("identity2");
|
|
3132
|
+
/**
|
|
3133
|
+
* Returns a 3-by-3 identity matrix.
|
|
3134
|
+
* @returns {m3x3f} The result matrix.
|
|
3135
|
+
*/
|
|
3136
|
+
const identity3 = comptime(() => mat3x3f(1, 0, 0, 0, 1, 0, 0, 0, 1)).$name("identity3");
|
|
3137
|
+
/**
|
|
3138
|
+
* Returns a 4-by-4 identity matrix.
|
|
3139
|
+
* @returns {m4x4f} The result matrix.
|
|
3140
|
+
*/
|
|
3141
|
+
const identity4 = comptime(() => mat4x4f(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1)).$name("identity4");
|
|
3142
|
+
const identityFunctions = {
|
|
3143
|
+
2: identity2,
|
|
3144
|
+
3: identity3,
|
|
3145
|
+
4: identity4
|
|
3146
|
+
};
|
|
3147
|
+
/**
|
|
3148
|
+
* Creates a 4-by-4 matrix which translates by the given vector v.
|
|
3149
|
+
* @param {v3f} vector - The vector by which to translate.
|
|
3150
|
+
* @returns {m4x4f} The translation matrix.
|
|
3151
|
+
*/
|
|
3152
|
+
const translation4 = dualImpl({
|
|
3153
|
+
name: "translation4",
|
|
3154
|
+
normalImpl: (vector) => mat4x4f(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, vector.x, vector.y, vector.z, 1),
|
|
3155
|
+
get signature() {
|
|
3156
|
+
return {
|
|
3157
|
+
argTypes: [vec3f$1],
|
|
3158
|
+
returnType: mat4x4f
|
|
3159
|
+
};
|
|
3160
|
+
},
|
|
3161
|
+
codegenImpl: (_ctx, [v]) => stitch`mat4x4f(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, ${v}.x, ${v}.y, ${v}.z, 1)`
|
|
3162
|
+
});
|
|
3163
|
+
/**
|
|
3164
|
+
* Creates a 4-by-4 matrix which scales in each dimension by an amount given by the corresponding entry in the given vector.
|
|
3165
|
+
* @param {v3f} vector - A vector of three entries specifying the factor by which to scale in each dimension.
|
|
3166
|
+
* @returns {m4x4f} The scaling matrix.
|
|
3167
|
+
*/
|
|
3168
|
+
const scaling4 = dualImpl({
|
|
3169
|
+
name: "scaling4",
|
|
3170
|
+
normalImpl: (vector) => mat4x4f(vector.x, 0, 0, 0, 0, vector.y, 0, 0, 0, 0, vector.z, 0, 0, 0, 0, 1),
|
|
3171
|
+
get signature() {
|
|
3172
|
+
return {
|
|
3173
|
+
argTypes: [vec3f$1],
|
|
3174
|
+
returnType: mat4x4f
|
|
3175
|
+
};
|
|
3176
|
+
},
|
|
3177
|
+
codegenImpl: (_ctx, [v]) => stitch`mat4x4f(${v}.x, 0, 0, 0, 0, ${v}.y, 0, 0, 0, 0, ${v}.z, 0, 0, 0, 0, 1)`
|
|
3178
|
+
});
|
|
3179
|
+
/**
|
|
3180
|
+
* Creates a 4-by-4 matrix which rotates around the x-axis by the given angle.
|
|
3181
|
+
* @param {number} angle - The angle by which to rotate (in radians).
|
|
3182
|
+
* @returns {m4x4f} The rotation matrix.
|
|
3183
|
+
*/
|
|
3184
|
+
const rotationX4 = dualImpl({
|
|
3185
|
+
name: "rotationX4",
|
|
3186
|
+
normalImpl: (a) => mat4x4f(1, 0, 0, 0, 0, Math.cos(a), Math.sin(a), 0, 0, -Math.sin(a), Math.cos(a), 0, 0, 0, 0, 1),
|
|
3187
|
+
get signature() {
|
|
3188
|
+
return {
|
|
3189
|
+
argTypes: [f32],
|
|
3190
|
+
returnType: mat4x4f
|
|
3191
|
+
};
|
|
3192
|
+
},
|
|
3193
|
+
codegenImpl: (_ctx, [a]) => stitch`mat4x4f(1, 0, 0, 0, 0, cos(${a}), sin(${a}), 0, 0, -sin(${a}), cos(${a}), 0, 0, 0, 0, 1)`
|
|
3194
|
+
});
|
|
3195
|
+
/**
|
|
3196
|
+
* Creates a 4-by-4 matrix which rotates around the y-axis by the given angle.
|
|
3197
|
+
* @param {number} angle - The angle by which to rotate (in radians).
|
|
3198
|
+
* @returns {m4x4f} The rotation matrix.
|
|
3199
|
+
*/
|
|
3200
|
+
const rotationY4 = dualImpl({
|
|
3201
|
+
name: "rotationY4",
|
|
3202
|
+
normalImpl: (a) => mat4x4f(Math.cos(a), 0, -Math.sin(a), 0, 0, 1, 0, 0, Math.sin(a), 0, Math.cos(a), 0, 0, 0, 0, 1),
|
|
3203
|
+
get signature() {
|
|
3204
|
+
return {
|
|
3205
|
+
argTypes: [f32],
|
|
3206
|
+
returnType: mat4x4f
|
|
3207
|
+
};
|
|
3208
|
+
},
|
|
3209
|
+
codegenImpl: (_ctx, [a]) => stitch`mat4x4f(cos(${a}), 0, -sin(${a}), 0, 0, 1, 0, 0, sin(${a}), 0, cos(${a}), 0, 0, 0, 0, 1)`
|
|
3210
|
+
});
|
|
3211
|
+
/**
|
|
3212
|
+
* Creates a 4-by-4 matrix which rotates around the z-axis by the given angle.
|
|
3213
|
+
* @param {number} angle - The angle by which to rotate (in radians).
|
|
3214
|
+
* @returns {m4x4f} The rotation matrix.
|
|
3215
|
+
*/
|
|
3216
|
+
const rotationZ4 = dualImpl({
|
|
3217
|
+
name: "rotationZ4",
|
|
3218
|
+
normalImpl: (a) => mat4x4f(Math.cos(a), Math.sin(a), 0, 0, -Math.sin(a), Math.cos(a), 0, 0, 0, 0, 1, 0, 0, 0, 0, 1),
|
|
3219
|
+
get signature() {
|
|
3220
|
+
return {
|
|
3221
|
+
argTypes: [f32],
|
|
3222
|
+
returnType: mat4x4f
|
|
3223
|
+
};
|
|
3224
|
+
},
|
|
3225
|
+
codegenImpl: (_ctx, [a]) => stitch`mat4x4f(cos(${a}), sin(${a}), 0, 0, -sin(${a}), cos(${a}), 0, 0, 0, 0, 1, 0, 0, 0, 0, 1)`
|
|
3226
|
+
});
|
|
3227
|
+
/**
|
|
3228
|
+
* Schema representing mat2x2f - a matrix with 2 rows and 2 columns, with elements of type f32.
|
|
3229
|
+
* Also a constructor function for this matrix type.
|
|
3230
|
+
*
|
|
3231
|
+
* @example
|
|
3232
|
+
* const zero2x2 = mat2x2f(); // filled with zeros
|
|
3233
|
+
*
|
|
3234
|
+
* @example
|
|
3235
|
+
* const mat = mat2x2f(0, 1, 2, 3);
|
|
3236
|
+
* mat.columns[0] // vec2f(0, 1)
|
|
3237
|
+
* mat.columns[1] // vec2f(2, 3)
|
|
3238
|
+
*
|
|
3239
|
+
* @example
|
|
3240
|
+
* const mat = mat2x2f(
|
|
3241
|
+
* vec2f(0, 1), // column 0
|
|
3242
|
+
* vec2f(1, 2), // column 1
|
|
3243
|
+
* );
|
|
3244
|
+
*
|
|
3245
|
+
* @example
|
|
3246
|
+
* const buffer = root.createBuffer(d.mat2x2f, d.mat2x2f(0, 1, 2, 3)); // buffer holding a d.mat2x2f value, with an initial value of ((0, 1), (2, 3))
|
|
3247
|
+
*/
|
|
3248
|
+
const mat2x2f = createMatSchema({
|
|
3249
|
+
type: "mat2x2f",
|
|
3250
|
+
rows: 2,
|
|
3251
|
+
columns: 2,
|
|
3252
|
+
MatImpl: mat2x2fImpl
|
|
3253
|
+
});
|
|
3254
|
+
/**
|
|
3255
|
+
* Schema representing mat3x3f - a matrix with 3 rows and 3 columns, with elements of type f32.
|
|
3256
|
+
* Also a constructor function for this matrix type.
|
|
3257
|
+
*
|
|
3258
|
+
* @example
|
|
3259
|
+
* const zero3x3 = mat3x3f(); // filled with zeros
|
|
3260
|
+
*
|
|
3261
|
+
* @example
|
|
3262
|
+
* const mat = mat3x3f(0, 1, 2, 3, 4, 5, 6, 7, 8);
|
|
3263
|
+
* mat.columns[0] // vec3f(0, 1, 2)
|
|
3264
|
+
* mat.columns[1] // vec3f(3, 4, 5)
|
|
3265
|
+
* mat.columns[2] // vec3f(6, 7, 8)
|
|
3266
|
+
*
|
|
3267
|
+
* @example
|
|
3268
|
+
* const mat = mat3x3f(
|
|
3269
|
+
* vec3f(0, 1, 2), // column 0
|
|
3270
|
+
* vec3f(2, 3, 4), // column 1
|
|
3271
|
+
* vec3f(5, 6, 7), // column 2
|
|
3272
|
+
* );
|
|
3273
|
+
*
|
|
3274
|
+
* @example
|
|
3275
|
+
* const buffer = root.createBuffer(d.mat3x3f, d.mat3x3f()); // buffer holding a d.mat3x3f value, with an initial value of mat3x3f filled with zeros
|
|
3276
|
+
*/
|
|
3277
|
+
const mat3x3f = createMatSchema({
|
|
3278
|
+
type: "mat3x3f",
|
|
3279
|
+
rows: 3,
|
|
3280
|
+
columns: 3,
|
|
3281
|
+
MatImpl: mat3x3fImpl
|
|
3282
|
+
});
|
|
3283
|
+
/**
|
|
3284
|
+
* Schema representing mat4x4f - a matrix with 4 rows and 4 columns, with elements of type f32.
|
|
3285
|
+
* Also a constructor function for this matrix type.
|
|
3286
|
+
*
|
|
3287
|
+
* @example
|
|
3288
|
+
* const zero4x4 = mat4x4f(); // filled with zeros
|
|
3289
|
+
*
|
|
3290
|
+
* @example
|
|
3291
|
+
* const mat = mat4x4f(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
|
|
3292
|
+
* mat.columns[0] // vec4f(0, 1, 2, 3)
|
|
3293
|
+
* mat.columns[1] // vec4f(4, 5, 6, 7)
|
|
3294
|
+
* mat.columns[2] // vec4f(8, 9, 10, 11)
|
|
3295
|
+
* mat.columns[3] // vec4f(12, 13, 14, 15)
|
|
3296
|
+
*
|
|
3297
|
+
* @example
|
|
3298
|
+
* const mat = mat4x4f(
|
|
3299
|
+
* vec4f(0, 1, 2, 3), // column 0
|
|
3300
|
+
* vec4f(4, 5, 6, 7), // column 1
|
|
3301
|
+
* vec4f(8, 9, 10, 11), // column 2
|
|
3302
|
+
* vec4f(12, 13, 14, 15), // column 3
|
|
3303
|
+
* );
|
|
3304
|
+
*
|
|
3305
|
+
* @example
|
|
3306
|
+
* const buffer = root.createBuffer(d.mat4x4f, d.mat4x4f()); // buffer holding a d.mat4x4f value, with an initial value of mat4x4f filled with zeros
|
|
3307
|
+
*/
|
|
3308
|
+
const mat4x4f = createMatSchema({
|
|
3309
|
+
type: "mat4x4f",
|
|
3310
|
+
rows: 4,
|
|
3311
|
+
columns: 4,
|
|
3312
|
+
MatImpl: mat4x4fImpl
|
|
3313
|
+
});
|
|
3314
|
+
function matToArray(mat) {
|
|
3315
|
+
if (mat.kind === "mat3x3f") return [
|
|
3316
|
+
mat[0],
|
|
3317
|
+
mat[1],
|
|
3318
|
+
mat[2],
|
|
3319
|
+
mat[4],
|
|
3320
|
+
mat[5],
|
|
3321
|
+
mat[6],
|
|
3322
|
+
mat[8],
|
|
3323
|
+
mat[9],
|
|
3324
|
+
mat[10]
|
|
3325
|
+
];
|
|
3326
|
+
return Array.from({ length: mat.length }).map((_, idx) => mat[idx]);
|
|
3327
|
+
}
|
|
3328
|
+
|
|
3329
|
+
//#endregion
|
|
3330
|
+
//#region src/data/numberOps.ts
|
|
3331
|
+
const smoothstepScalar = (edge0, edge1, x) => {
|
|
3332
|
+
if (edge0 === edge1) return 0;
|
|
3333
|
+
const t = clamp((x - edge0) / (edge1 - edge0), 0, 1);
|
|
3334
|
+
return t * t * (3 - 2 * t);
|
|
3335
|
+
};
|
|
3336
|
+
const clamp = (value, low, high) => Math.min(Math.max(low, value), high);
|
|
3337
|
+
const divInteger = (lhs, rhs) => {
|
|
3338
|
+
if (rhs === 0) return lhs;
|
|
3339
|
+
return Math.trunc(lhs / rhs);
|
|
3340
|
+
};
|
|
3341
|
+
function bitcastU32toF32Impl(n) {
|
|
3342
|
+
const dataView = /* @__PURE__ */ new DataView(/* @__PURE__ */ new ArrayBuffer(4));
|
|
3343
|
+
dataView.setUint32(0, n, true);
|
|
3344
|
+
return dataView.getFloat32(0, true);
|
|
3345
|
+
}
|
|
3346
|
+
function bitcastU32toI32Impl(n) {
|
|
3347
|
+
const dataView = /* @__PURE__ */ new DataView(/* @__PURE__ */ new ArrayBuffer(4));
|
|
3348
|
+
dataView.setUint32(0, n, true);
|
|
3349
|
+
return dataView.getInt32(0, true);
|
|
3350
|
+
}
|
|
3351
|
+
|
|
3352
|
+
//#endregion
|
|
3353
|
+
//#region src/data/vectorOps.ts
|
|
3354
|
+
const vec2b = vec2b$1;
|
|
3355
|
+
const vec2f = vec2f$1;
|
|
3356
|
+
const vec2h = vec2h$1;
|
|
3357
|
+
const vec2i = vec2i$1;
|
|
3358
|
+
const vec2u = vec2u$1;
|
|
3359
|
+
const vec3b = vec3b$1;
|
|
3360
|
+
const vec3f = vec3f$1;
|
|
3361
|
+
const vec3h = vec3h$1;
|
|
3362
|
+
const vec3i = vec3i$1;
|
|
3363
|
+
const vec3u = vec3u$1;
|
|
3364
|
+
const vec4b = vec4b$1;
|
|
3365
|
+
const vec4f = vec4f$1;
|
|
3366
|
+
const vec4h = vec4h$1;
|
|
3367
|
+
const vec4i = vec4i$1;
|
|
3368
|
+
const vec4u = vec4u$1;
|
|
3369
|
+
const lengthVec2 = (v) => Math.sqrt(v.x ** 2 + v.y ** 2);
|
|
3370
|
+
const lengthVec3 = (v) => Math.sqrt(v.x ** 2 + v.y ** 2 + v.z ** 2);
|
|
3371
|
+
const lengthVec4 = (v) => Math.sqrt(v.x ** 2 + v.y ** 2 + v.z ** 2 + v.w ** 2);
|
|
3372
|
+
const dotVec2 = (lhs, rhs) => lhs.x * rhs.x + lhs.y * rhs.y;
|
|
3373
|
+
const dotVec3 = (lhs, rhs) => lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z;
|
|
3374
|
+
const dotVec4 = (lhs, rhs) => lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z + lhs.w * rhs.w;
|
|
3375
|
+
const unary2f = (op) => (a) => vec2f(op(a.x), op(a.y));
|
|
3376
|
+
const unary2h = (op) => (a) => vec2h(op(a.x), op(a.y));
|
|
3377
|
+
const unary2i = (op) => (a) => vec2i(op(a.x), op(a.y));
|
|
3378
|
+
const unary2u = (op) => (a) => vec2u(op(a.x), op(a.y));
|
|
3379
|
+
const unary3f = (op) => (a) => vec3f(op(a.x), op(a.y), op(a.z));
|
|
3380
|
+
const unary3h = (op) => (a) => vec3h(op(a.x), op(a.y), op(a.z));
|
|
3381
|
+
const unary3i = (op) => (a) => vec3i(op(a.x), op(a.y), op(a.z));
|
|
3382
|
+
const unary3u = (op) => (a) => vec3u(op(a.x), op(a.y), op(a.z));
|
|
3383
|
+
const unary4f = (op) => (a) => vec4f(op(a.x), op(a.y), op(a.z), op(a.w));
|
|
3384
|
+
const unary4h = (op) => (a) => vec4h(op(a.x), op(a.y), op(a.z), op(a.w));
|
|
3385
|
+
const unary4i = (op) => (a) => vec4i(op(a.x), op(a.y), op(a.z), op(a.w));
|
|
3386
|
+
const unary4u = (op) => (a) => vec4u(op(a.x), op(a.y), op(a.z), op(a.w));
|
|
3387
|
+
const unary2x2f = (op) => (a) => {
|
|
3388
|
+
const a_ = a.columns;
|
|
3389
|
+
return mat2x2f(unary2f(op)(a_[0]), unary2f(op)(a_[1]));
|
|
3390
|
+
};
|
|
3391
|
+
const unary3x3f = (op) => (a) => {
|
|
3392
|
+
const a_ = a.columns;
|
|
3393
|
+
return mat3x3f(unary3f(op)(a_[0]), unary3f(op)(a_[1]), unary3f(op)(a_[2]));
|
|
3394
|
+
};
|
|
3395
|
+
const unary4x4f = (op) => (a) => {
|
|
3396
|
+
const a_ = a.columns;
|
|
3397
|
+
return mat4x4f(unary4f(op)(a_[0]), unary4f(op)(a_[1]), unary4f(op)(a_[2]), unary4f(op)(a_[3]));
|
|
3398
|
+
};
|
|
3399
|
+
const binaryComponentWise2f = (op) => (a, b) => vec2f(op(a.x, b.x), op(a.y, b.y));
|
|
3400
|
+
const binaryComponentWise2h = (op) => (a, b) => vec2h(op(a.x, b.x), op(a.y, b.y));
|
|
3401
|
+
const binaryComponentWise2i = (op) => (a, b) => vec2i(op(a.x, b.x), op(a.y, b.y));
|
|
3402
|
+
const binaryComponentWise2u = (op) => (a, b) => vec2u(op(a.x, b.x), op(a.y, b.y));
|
|
3403
|
+
const binaryComponentWise3f = (op) => (a, b) => vec3f(op(a.x, b.x), op(a.y, b.y), op(a.z, b.z));
|
|
3404
|
+
const binaryComponentWise3h = (op) => (a, b) => vec3h(op(a.x, b.x), op(a.y, b.y), op(a.z, b.z));
|
|
3405
|
+
const binaryComponentWise3i = (op) => (a, b) => vec3i(op(a.x, b.x), op(a.y, b.y), op(a.z, b.z));
|
|
3406
|
+
const binaryComponentWise3u = (op) => (a, b) => vec3u(op(a.x, b.x), op(a.y, b.y), op(a.z, b.z));
|
|
3407
|
+
const binaryComponentWise4f = (op) => (a, b) => vec4f(op(a.x, b.x), op(a.y, b.y), op(a.z, b.z), op(a.w, b.w));
|
|
3408
|
+
const binaryComponentWise4h = (op) => (a, b) => vec4h(op(a.x, b.x), op(a.y, b.y), op(a.z, b.z), op(a.w, b.w));
|
|
3409
|
+
const binaryComponentWise4i = (op) => (a, b) => vec4i(op(a.x, b.x), op(a.y, b.y), op(a.z, b.z), op(a.w, b.w));
|
|
3410
|
+
const binaryComponentWise4u = (op) => (a, b) => vec4u(op(a.x, b.x), op(a.y, b.y), op(a.z, b.z), op(a.w, b.w));
|
|
3411
|
+
const binaryComponentWise2x2f = (op) => (a, b) => {
|
|
3412
|
+
const a_ = a.columns;
|
|
3413
|
+
const b_ = b.columns;
|
|
3414
|
+
return mat2x2f(binaryComponentWise2f(op)(a_[0], b_[0]), binaryComponentWise2f(op)(a_[1], b_[1]));
|
|
3415
|
+
};
|
|
3416
|
+
const binaryComponentWise3x3f = (op) => (a, b) => {
|
|
3417
|
+
const a_ = a.columns;
|
|
3418
|
+
const b_ = b.columns;
|
|
3419
|
+
return mat3x3f(binaryComponentWise3f(op)(a_[0], b_[0]), binaryComponentWise3f(op)(a_[1], b_[1]), binaryComponentWise3f(op)(a_[2], b_[2]));
|
|
3420
|
+
};
|
|
3421
|
+
const binaryComponentWise4x4f = (op) => (a, b) => {
|
|
3422
|
+
const a_ = a.columns;
|
|
3423
|
+
const b_ = b.columns;
|
|
3424
|
+
return mat4x4f(binaryComponentWise4f(op)(a_[0], b_[0]), binaryComponentWise4f(op)(a_[1], b_[1]), binaryComponentWise4f(op)(a_[2], b_[2]), binaryComponentWise4f(op)(a_[3], b_[3]));
|
|
3425
|
+
};
|
|
3426
|
+
const ternaryComponentWise2f = (op) => (a, b, c) => vec2f(op(a.x, b.x, c.x), op(a.y, b.y, c.y));
|
|
3427
|
+
const ternaryComponentWise2h = (op) => (a, b, c) => vec2h(op(a.x, b.x, c.x), op(a.y, b.y, c.y));
|
|
3428
|
+
const ternaryComponentWise3f = (op) => (a, b, c) => vec3f(op(a.x, b.x, c.x), op(a.y, b.y, c.y), op(a.z, b.z, c.z));
|
|
3429
|
+
const ternaryComponentWise3h = (op) => (a, b, c) => vec3h(op(a.x, b.x, c.x), op(a.y, b.y, c.y), op(a.z, b.z, c.z));
|
|
3430
|
+
const ternaryComponentWise4f = (op) => (a, b, c) => vec4f(op(a.x, b.x, c.x), op(a.y, b.y, c.y), op(a.z, b.z, c.z), op(a.w, b.w, c.w));
|
|
3431
|
+
const ternaryComponentWise4h = (op) => (a, b, c) => vec4h(op(a.x, b.x, c.x), op(a.y, b.y, c.y), op(a.z, b.z, c.z), op(a.w, b.w, c.w));
|
|
3432
|
+
const VectorOps = {
|
|
3433
|
+
eq: {
|
|
3434
|
+
vec2f: (e1, e2) => vec2b(e1.x === e2.x, e1.y === e2.y),
|
|
3435
|
+
vec2h: (e1, e2) => vec2b(e1.x === e2.x, e1.y === e2.y),
|
|
3436
|
+
vec2i: (e1, e2) => vec2b(e1.x === e2.x, e1.y === e2.y),
|
|
3437
|
+
vec2u: (e1, e2) => vec2b(e1.x === e2.x, e1.y === e2.y),
|
|
3438
|
+
"vec2<bool>": (e1, e2) => vec2b(e1.x === e2.x, e1.y === e2.y),
|
|
3439
|
+
vec3f: (e1, e2) => vec3b(e1.x === e2.x, e1.y === e2.y, e1.z === e2.z),
|
|
3440
|
+
vec3h: (e1, e2) => vec3b(e1.x === e2.x, e1.y === e2.y, e1.z === e2.z),
|
|
3441
|
+
vec3i: (e1, e2) => vec3b(e1.x === e2.x, e1.y === e2.y, e1.z === e2.z),
|
|
3442
|
+
vec3u: (e1, e2) => vec3b(e1.x === e2.x, e1.y === e2.y, e1.z === e2.z),
|
|
3443
|
+
"vec3<bool>": (e1, e2) => vec3b(e1.x === e2.x, e1.y === e2.y, e1.z === e2.z),
|
|
3444
|
+
vec4f: (e1, e2) => vec4b(e1.x === e2.x, e1.y === e2.y, e1.z === e2.z, e1.w === e2.w),
|
|
3445
|
+
vec4h: (e1, e2) => vec4b(e1.x === e2.x, e1.y === e2.y, e1.z === e2.z, e1.w === e2.w),
|
|
3446
|
+
vec4i: (e1, e2) => vec4b(e1.x === e2.x, e1.y === e2.y, e1.z === e2.z, e1.w === e2.w),
|
|
3447
|
+
vec4u: (e1, e2) => vec4b(e1.x === e2.x, e1.y === e2.y, e1.z === e2.z, e1.w === e2.w),
|
|
3448
|
+
"vec4<bool>": (e1, e2) => vec4b(e1.x === e2.x, e1.y === e2.y, e1.z === e2.z, e1.w === e2.w)
|
|
3449
|
+
},
|
|
3450
|
+
lt: {
|
|
3451
|
+
vec2f: (e1, e2) => vec2b(e1.x < e2.x, e1.y < e2.y),
|
|
3452
|
+
vec2h: (e1, e2) => vec2b(e1.x < e2.x, e1.y < e2.y),
|
|
3453
|
+
vec2i: (e1, e2) => vec2b(e1.x < e2.x, e1.y < e2.y),
|
|
3454
|
+
vec2u: (e1, e2) => vec2b(e1.x < e2.x, e1.y < e2.y),
|
|
3455
|
+
vec3f: (e1, e2) => vec3b(e1.x < e2.x, e1.y < e2.y, e1.z < e2.z),
|
|
3456
|
+
vec3h: (e1, e2) => vec3b(e1.x < e2.x, e1.y < e2.y, e1.z < e2.z),
|
|
3457
|
+
vec3i: (e1, e2) => vec3b(e1.x < e2.x, e1.y < e2.y, e1.z < e2.z),
|
|
3458
|
+
vec3u: (e1, e2) => vec3b(e1.x < e2.x, e1.y < e2.y, e1.z < e2.z),
|
|
3459
|
+
vec4f: (e1, e2) => vec4b(e1.x < e2.x, e1.y < e2.y, e1.z < e2.z, e1.w < e2.w),
|
|
3460
|
+
vec4h: (e1, e2) => vec4b(e1.x < e2.x, e1.y < e2.y, e1.z < e2.z, e1.w < e2.w),
|
|
3461
|
+
vec4i: (e1, e2) => vec4b(e1.x < e2.x, e1.y < e2.y, e1.z < e2.z, e1.w < e2.w),
|
|
3462
|
+
vec4u: (e1, e2) => vec4b(e1.x < e2.x, e1.y < e2.y, e1.z < e2.z, e1.w < e2.w)
|
|
3463
|
+
},
|
|
3464
|
+
or: {
|
|
3465
|
+
"vec2<bool>": (e1, e2) => vec2b(e1.x || e2.x, e1.y || e2.y),
|
|
3466
|
+
"vec3<bool>": (e1, e2) => vec3b(e1.x || e2.x, e1.y || e2.y, e1.z || e2.z),
|
|
3467
|
+
"vec4<bool>": (e1, e2) => vec4b(e1.x || e2.x, e1.y || e2.y, e1.z || e2.z, e1.w || e2.w)
|
|
3468
|
+
},
|
|
3469
|
+
all: {
|
|
3470
|
+
"vec2<bool>": (e) => e.x && e.y,
|
|
3471
|
+
"vec3<bool>": (e) => e.x && e.y && e.z,
|
|
3472
|
+
"vec4<bool>": (e) => e.x && e.y && e.z && e.w
|
|
3473
|
+
},
|
|
3474
|
+
abs: {
|
|
3475
|
+
vec2f: unary2f(Math.abs),
|
|
3476
|
+
vec2h: unary2h(Math.abs),
|
|
3477
|
+
vec2i: unary2i(Math.abs),
|
|
3478
|
+
vec2u: unary2u(Math.abs),
|
|
3479
|
+
vec3f: unary3f(Math.abs),
|
|
3480
|
+
vec3h: unary3h(Math.abs),
|
|
3481
|
+
vec3i: unary3i(Math.abs),
|
|
3482
|
+
vec3u: unary3u(Math.abs),
|
|
3483
|
+
vec4f: unary4f(Math.abs),
|
|
3484
|
+
vec4h: unary4h(Math.abs),
|
|
3485
|
+
vec4i: unary4i(Math.abs),
|
|
3486
|
+
vec4u: unary4u(Math.abs)
|
|
3487
|
+
},
|
|
3488
|
+
atan2: {
|
|
3489
|
+
vec2f: binaryComponentWise2f(Math.atan2),
|
|
3490
|
+
vec2h: binaryComponentWise2h(Math.atan2),
|
|
3491
|
+
vec3f: binaryComponentWise3f(Math.atan2),
|
|
3492
|
+
vec3h: binaryComponentWise3h(Math.atan2),
|
|
3493
|
+
vec4f: binaryComponentWise4f(Math.atan2),
|
|
3494
|
+
vec4h: binaryComponentWise4h(Math.atan2)
|
|
3495
|
+
},
|
|
3496
|
+
acos: {
|
|
3497
|
+
vec2f: unary2f(Math.acos),
|
|
3498
|
+
vec2h: unary2h(Math.acos),
|
|
3499
|
+
vec2i: unary2i(Math.acos),
|
|
3500
|
+
vec2u: unary2u(Math.acos),
|
|
3501
|
+
vec3f: unary3f(Math.acos),
|
|
3502
|
+
vec3h: unary3h(Math.acos),
|
|
3503
|
+
vec3i: unary3i(Math.acos),
|
|
3504
|
+
vec3u: unary3u(Math.acos),
|
|
3505
|
+
vec4f: unary4f(Math.acos),
|
|
3506
|
+
vec4h: unary4h(Math.acos),
|
|
3507
|
+
vec4i: unary4i(Math.acos),
|
|
3508
|
+
vec4u: unary4u(Math.acos)
|
|
3509
|
+
},
|
|
3510
|
+
acosh: {
|
|
3511
|
+
vec2f: unary2f(Math.acosh),
|
|
3512
|
+
vec2h: unary2h(Math.acosh),
|
|
3513
|
+
vec3f: unary3f(Math.acosh),
|
|
3514
|
+
vec3h: unary3h(Math.acosh),
|
|
3515
|
+
vec4f: unary4f(Math.acosh),
|
|
3516
|
+
vec4h: unary4h(Math.acosh)
|
|
3517
|
+
},
|
|
3518
|
+
asin: {
|
|
3519
|
+
vec2f: unary2f(Math.asin),
|
|
3520
|
+
vec2h: unary2h(Math.asin),
|
|
3521
|
+
vec3f: unary3f(Math.asin),
|
|
3522
|
+
vec3h: unary3h(Math.asin),
|
|
3523
|
+
vec4f: unary4f(Math.asin),
|
|
3524
|
+
vec4h: unary4h(Math.asin)
|
|
3525
|
+
},
|
|
3526
|
+
asinh: {
|
|
3527
|
+
vec2f: unary2f(Math.asinh),
|
|
3528
|
+
vec2h: unary2h(Math.asinh),
|
|
3529
|
+
vec3f: unary3f(Math.asinh),
|
|
3530
|
+
vec3h: unary3h(Math.asinh),
|
|
3531
|
+
vec4f: unary4f(Math.asinh),
|
|
3532
|
+
vec4h: unary4h(Math.asinh)
|
|
3533
|
+
},
|
|
3534
|
+
atan: {
|
|
3535
|
+
vec2f: unary2f(Math.atan),
|
|
3536
|
+
vec2h: unary2h(Math.atan),
|
|
3537
|
+
vec3f: unary3f(Math.atan),
|
|
3538
|
+
vec3h: unary3h(Math.atan),
|
|
3539
|
+
vec4f: unary4f(Math.atan),
|
|
3540
|
+
vec4h: unary4h(Math.atan)
|
|
3541
|
+
},
|
|
3542
|
+
atanh: {
|
|
3543
|
+
vec2f: unary2f(Math.atanh),
|
|
3544
|
+
vec2h: unary2h(Math.atanh),
|
|
3545
|
+
vec3f: unary3f(Math.atanh),
|
|
3546
|
+
vec3h: unary3h(Math.atanh),
|
|
3547
|
+
vec4f: unary4f(Math.atanh),
|
|
3548
|
+
vec4h: unary4h(Math.atanh)
|
|
3549
|
+
},
|
|
3550
|
+
ceil: {
|
|
3551
|
+
vec2f: unary2f(Math.ceil),
|
|
3552
|
+
vec2h: unary2h(Math.ceil),
|
|
3553
|
+
vec3f: unary3f(Math.ceil),
|
|
3554
|
+
vec3h: unary3h(Math.ceil),
|
|
3555
|
+
vec4f: unary4f(Math.ceil),
|
|
3556
|
+
vec4h: unary4h(Math.ceil)
|
|
3557
|
+
},
|
|
3558
|
+
clamp: {
|
|
3559
|
+
vec2f: (v, low, high) => vec2f(clamp(v.x, low.x, high.x), clamp(v.y, low.y, high.y)),
|
|
3560
|
+
vec2h: (v, low, high) => vec2h(clamp(v.x, low.x, high.x), clamp(v.y, low.y, high.y)),
|
|
3561
|
+
vec2i: (v, low, high) => vec2i(clamp(v.x, low.x, high.x), clamp(v.y, low.y, high.y)),
|
|
3562
|
+
vec2u: (v, low, high) => vec2u(clamp(v.x, low.x, high.x), clamp(v.y, low.y, high.y)),
|
|
3563
|
+
vec3f: (v, low, high) => vec3f(clamp(v.x, low.x, high.x), clamp(v.y, low.y, high.y), clamp(v.z, low.z, high.z)),
|
|
3564
|
+
vec3h: (v, low, high) => vec3h(clamp(v.x, low.x, high.x), clamp(v.y, low.y, high.y), clamp(v.z, low.z, high.z)),
|
|
3565
|
+
vec3i: (v, low, high) => vec3i(clamp(v.x, low.x, high.x), clamp(v.y, low.y, high.y), clamp(v.z, low.z, high.z)),
|
|
3566
|
+
vec3u: (v, low, high) => vec3u(clamp(v.x, low.x, high.x), clamp(v.y, low.y, high.y), clamp(v.z, low.z, high.z)),
|
|
3567
|
+
vec4f: (v, low, high) => vec4f(clamp(v.x, low.x, high.x), clamp(v.y, low.y, high.y), clamp(v.z, low.z, high.z), clamp(v.w, low.w, high.w)),
|
|
3568
|
+
vec4h: (v, low, high) => vec4h(clamp(v.x, low.x, high.x), clamp(v.y, low.y, high.y), clamp(v.z, low.z, high.z), clamp(v.w, low.w, high.w)),
|
|
3569
|
+
vec4i: (v, low, high) => vec4i(clamp(v.x, low.x, high.x), clamp(v.y, low.y, high.y), clamp(v.z, low.z, high.z), clamp(v.w, low.w, high.w)),
|
|
3570
|
+
vec4u: (v, low, high) => vec4u(clamp(v.x, low.x, high.x), clamp(v.y, low.y, high.y), clamp(v.z, low.z, high.z), clamp(v.w, low.w, high.w))
|
|
3571
|
+
},
|
|
3572
|
+
length: {
|
|
3573
|
+
vec2f: lengthVec2,
|
|
3574
|
+
vec2h: lengthVec2,
|
|
3575
|
+
vec3f: lengthVec3,
|
|
3576
|
+
vec3h: lengthVec3,
|
|
3577
|
+
vec4f: lengthVec4,
|
|
3578
|
+
vec4h: lengthVec4
|
|
3579
|
+
},
|
|
3580
|
+
add: {
|
|
3581
|
+
vec2f: binaryComponentWise2f((a, b) => a + b),
|
|
3582
|
+
vec2h: binaryComponentWise2h((a, b) => a + b),
|
|
3583
|
+
vec2i: binaryComponentWise2i((a, b) => a + b),
|
|
3584
|
+
vec2u: binaryComponentWise2u((a, b) => a + b),
|
|
3585
|
+
vec3f: binaryComponentWise3f((a, b) => a + b),
|
|
3586
|
+
vec3h: binaryComponentWise3h((a, b) => a + b),
|
|
3587
|
+
vec3i: binaryComponentWise3i((a, b) => a + b),
|
|
3588
|
+
vec3u: binaryComponentWise3u((a, b) => a + b),
|
|
3589
|
+
vec4f: binaryComponentWise4f((a, b) => a + b),
|
|
3590
|
+
vec4h: binaryComponentWise4h((a, b) => a + b),
|
|
3591
|
+
vec4i: binaryComponentWise4i((a, b) => a + b),
|
|
3592
|
+
vec4u: binaryComponentWise4u((a, b) => a + b),
|
|
3593
|
+
mat2x2f: binaryComponentWise2x2f((a, b) => a + b),
|
|
3594
|
+
mat3x3f: binaryComponentWise3x3f((a, b) => a + b),
|
|
3595
|
+
mat4x4f: binaryComponentWise4x4f((a, b) => a + b)
|
|
3596
|
+
},
|
|
3597
|
+
smoothstep: {
|
|
3598
|
+
vec2f: ternaryComponentWise2f(smoothstepScalar),
|
|
3599
|
+
vec2h: ternaryComponentWise2h(smoothstepScalar),
|
|
3600
|
+
vec3f: ternaryComponentWise3f(smoothstepScalar),
|
|
3601
|
+
vec3h: ternaryComponentWise3h(smoothstepScalar),
|
|
3602
|
+
vec4f: ternaryComponentWise4f(smoothstepScalar),
|
|
3603
|
+
vec4h: ternaryComponentWise4h(smoothstepScalar)
|
|
3604
|
+
},
|
|
3605
|
+
addMixed: {
|
|
3606
|
+
vec2f: (a, b) => unary2f((e) => e + b)(a),
|
|
3607
|
+
vec2h: (a, b) => unary2h((e) => e + b)(a),
|
|
3608
|
+
vec2i: (a, b) => unary2i((e) => e + b)(a),
|
|
3609
|
+
vec2u: (a, b) => unary2u((e) => e + b)(a),
|
|
3610
|
+
vec3f: (a, b) => unary3f((e) => e + b)(a),
|
|
3611
|
+
vec3h: (a, b) => unary3h((e) => e + b)(a),
|
|
3612
|
+
vec3i: (a, b) => unary3i((e) => e + b)(a),
|
|
3613
|
+
vec3u: (a, b) => unary3u((e) => e + b)(a),
|
|
3614
|
+
vec4f: (a, b) => unary4f((e) => e + b)(a),
|
|
3615
|
+
vec4h: (a, b) => unary4h((e) => e + b)(a),
|
|
3616
|
+
vec4i: (a, b) => unary4i((e) => e + b)(a),
|
|
3617
|
+
vec4u: (a, b) => unary4u((e) => e + b)(a),
|
|
3618
|
+
mat2x2f: (a, b) => unary2x2f((e) => e + b)(a),
|
|
3619
|
+
mat3x3f: (a, b) => unary3x3f((e) => e + b)(a),
|
|
3620
|
+
mat4x4f: (a, b) => unary4x4f((e) => e + b)(a)
|
|
3621
|
+
},
|
|
3622
|
+
mulSxV: {
|
|
3623
|
+
vec2f: (s, v) => unary2f((e) => s * e)(v),
|
|
3624
|
+
vec2h: (s, v) => unary2h((e) => s * e)(v),
|
|
3625
|
+
vec2i: (s, v) => unary2i((e) => s * e)(v),
|
|
3626
|
+
vec2u: (s, v) => unary2u((e) => s * e)(v),
|
|
3627
|
+
vec3f: (s, v) => unary3f((e) => s * e)(v),
|
|
3628
|
+
vec3h: (s, v) => unary3h((e) => s * e)(v),
|
|
3629
|
+
vec3i: (s, v) => unary3i((e) => s * e)(v),
|
|
3630
|
+
vec3u: (s, v) => unary3u((e) => s * e)(v),
|
|
3631
|
+
vec4f: (s, v) => unary4f((e) => s * e)(v),
|
|
3632
|
+
vec4h: (s, v) => unary4h((e) => s * e)(v),
|
|
3633
|
+
vec4i: (s, v) => unary4i((e) => s * e)(v),
|
|
3634
|
+
vec4u: (s, v) => unary4u((e) => s * e)(v),
|
|
3635
|
+
mat2x2f: (s, m) => unary2x2f((e) => s * e)(m),
|
|
3636
|
+
mat3x3f: (s, m) => unary3x3f((e) => s * e)(m),
|
|
3637
|
+
mat4x4f: (s, m) => unary4x4f((e) => s * e)(m)
|
|
3638
|
+
},
|
|
3639
|
+
mulVxV: {
|
|
3640
|
+
vec2f: binaryComponentWise2f((a, b) => a * b),
|
|
3641
|
+
vec2h: binaryComponentWise2h((a, b) => a * b),
|
|
3642
|
+
vec2i: binaryComponentWise2i((a, b) => a * b),
|
|
3643
|
+
vec2u: binaryComponentWise2u((a, b) => a * b),
|
|
3644
|
+
vec3f: binaryComponentWise3f((a, b) => a * b),
|
|
3645
|
+
vec3h: binaryComponentWise3h((a, b) => a * b),
|
|
3646
|
+
vec3i: binaryComponentWise3i((a, b) => a * b),
|
|
3647
|
+
vec3u: binaryComponentWise3u((a, b) => a * b),
|
|
3648
|
+
vec4f: binaryComponentWise4f((a, b) => a * b),
|
|
3649
|
+
vec4h: binaryComponentWise4h((a, b) => a * b),
|
|
3650
|
+
vec4i: binaryComponentWise4i((a, b) => a * b),
|
|
3651
|
+
vec4u: binaryComponentWise4u((a, b) => a * b),
|
|
3652
|
+
mat2x2f: (a, b) => {
|
|
3653
|
+
const a_ = a.columns;
|
|
3654
|
+
const b_ = b.columns;
|
|
3655
|
+
return mat2x2f(a_[0].x * b_[0].x + a_[1].x * b_[0].y, a_[0].y * b_[0].x + a_[1].y * b_[0].y, a_[0].x * b_[1].x + a_[1].x * b_[1].y, a_[0].y * b_[1].x + a_[1].y * b_[1].y);
|
|
3656
|
+
},
|
|
3657
|
+
mat3x3f: (a, b) => {
|
|
3658
|
+
const a_ = a.columns;
|
|
3659
|
+
const b_ = b.columns;
|
|
3660
|
+
return mat3x3f(a_[0].x * b_[0].x + a_[1].x * b_[0].y + a_[2].x * b_[0].z, a_[0].y * b_[0].x + a_[1].y * b_[0].y + a_[2].y * b_[0].z, a_[0].z * b_[0].x + a_[1].z * b_[0].y + a_[2].z * b_[0].z, a_[0].x * b_[1].x + a_[1].x * b_[1].y + a_[2].x * b_[1].z, a_[0].y * b_[1].x + a_[1].y * b_[1].y + a_[2].y * b_[1].z, a_[0].z * b_[1].x + a_[1].z * b_[1].y + a_[2].z * b_[1].z, a_[0].x * b_[2].x + a_[1].x * b_[2].y + a_[2].x * b_[2].z, a_[0].y * b_[2].x + a_[1].y * b_[2].y + a_[2].y * b_[2].z, a_[0].z * b_[2].x + a_[1].z * b_[2].y + a_[2].z * b_[2].z);
|
|
3661
|
+
},
|
|
3662
|
+
mat4x4f: (a, b) => {
|
|
3663
|
+
const a_ = a.columns;
|
|
3664
|
+
const b_ = b.columns;
|
|
3665
|
+
return mat4x4f(a_[0].x * b_[0].x + a_[1].x * b_[0].y + a_[2].x * b_[0].z + a_[3].x * b_[0].w, a_[0].y * b_[0].x + a_[1].y * b_[0].y + a_[2].y * b_[0].z + a_[3].y * b_[0].w, a_[0].z * b_[0].x + a_[1].z * b_[0].y + a_[2].z * b_[0].z + a_[3].z * b_[0].w, a_[0].w * b_[0].x + a_[1].w * b_[0].y + a_[2].w * b_[0].z + a_[3].w * b_[0].w, a_[0].x * b_[1].x + a_[1].x * b_[1].y + a_[2].x * b_[1].z + a_[3].x * b_[1].w, a_[0].y * b_[1].x + a_[1].y * b_[1].y + a_[2].y * b_[1].z + a_[3].y * b_[1].w, a_[0].z * b_[1].x + a_[1].z * b_[1].y + a_[2].z * b_[1].z + a_[3].z * b_[1].w, a_[0].w * b_[1].x + a_[1].w * b_[1].y + a_[2].w * b_[1].z + a_[3].w * b_[1].w, a_[0].x * b_[2].x + a_[1].x * b_[2].y + a_[2].x * b_[2].z + a_[3].x * b_[2].w, a_[0].y * b_[2].x + a_[1].y * b_[2].y + a_[2].y * b_[2].z + a_[3].y * b_[2].w, a_[0].z * b_[2].x + a_[1].z * b_[2].y + a_[2].z * b_[2].z + a_[3].z * b_[2].w, a_[0].w * b_[2].x + a_[1].w * b_[2].y + a_[2].w * b_[2].z + a_[3].w * b_[2].w, a_[0].x * b_[3].x + a_[1].x * b_[3].y + a_[2].x * b_[3].z + a_[3].x * b_[3].w, a_[0].y * b_[3].x + a_[1].y * b_[3].y + a_[2].y * b_[3].z + a_[3].y * b_[3].w, a_[0].z * b_[3].x + a_[1].z * b_[3].y + a_[2].z * b_[3].z + a_[3].z * b_[3].w, a_[0].w * b_[3].x + a_[1].w * b_[3].y + a_[2].w * b_[3].z + a_[3].w * b_[3].w);
|
|
3666
|
+
}
|
|
3667
|
+
},
|
|
3668
|
+
mulMxV: {
|
|
3669
|
+
mat2x2f: (m, v) => {
|
|
3670
|
+
const m_ = m.columns;
|
|
3671
|
+
return vec2f(m_[0].x * v.x + m_[1].x * v.y, m_[0].y * v.x + m_[1].y * v.y);
|
|
3672
|
+
},
|
|
3673
|
+
mat3x3f: (m, v) => {
|
|
3674
|
+
const m_ = m.columns;
|
|
3675
|
+
return vec3f(m_[0].x * v.x + m_[1].x * v.y + m_[2].x * v.z, m_[0].y * v.x + m_[1].y * v.y + m_[2].y * v.z, m_[0].z * v.x + m_[1].z * v.y + m_[2].z * v.z);
|
|
3676
|
+
},
|
|
3677
|
+
mat4x4f: (m, v) => {
|
|
3678
|
+
const m_ = m.columns;
|
|
3679
|
+
return vec4f(m_[0].x * v.x + m_[1].x * v.y + m_[2].x * v.z + m_[3].x * v.w, m_[0].y * v.x + m_[1].y * v.y + m_[2].y * v.z + m_[3].y * v.w, m_[0].z * v.x + m_[1].z * v.y + m_[2].z * v.z + m_[3].z * v.w, m_[0].w * v.x + m_[1].w * v.y + m_[2].w * v.z + m_[3].w * v.w);
|
|
3680
|
+
}
|
|
3681
|
+
},
|
|
3682
|
+
mulVxM: {
|
|
3683
|
+
mat2x2f: (v, m) => {
|
|
3684
|
+
const m_ = m.columns;
|
|
3685
|
+
return vec2f(v.x * m_[0].x + v.y * m_[0].y, v.x * m_[1].x + v.y * m_[1].y);
|
|
3686
|
+
},
|
|
3687
|
+
mat3x3f: (v, m) => {
|
|
3688
|
+
const m_ = m.columns;
|
|
3689
|
+
return vec3f(v.x * m_[0].x + v.y * m_[0].y + v.z * m_[0].z, v.x * m_[1].x + v.y * m_[1].y + v.z * m_[1].z, v.x * m_[2].x + v.y * m_[2].y + v.z * m_[2].z);
|
|
3690
|
+
},
|
|
3691
|
+
mat4x4f: (v, m) => {
|
|
3692
|
+
const m_ = m.columns;
|
|
3693
|
+
return vec4f(v.x * m_[0].x + v.y * m_[0].y + v.z * m_[0].z + v.w * m_[0].w, v.x * m_[1].x + v.y * m_[1].y + v.z * m_[1].z + v.w * m_[1].w, v.x * m_[2].x + v.y * m_[2].y + v.z * m_[2].z + v.w * m_[2].w, v.x * m_[3].x + v.y * m_[3].y + v.z * m_[3].z + v.w * m_[3].w);
|
|
3694
|
+
}
|
|
3695
|
+
},
|
|
3696
|
+
div: {
|
|
3697
|
+
vec2f: binaryComponentWise2f((a, b) => a / b),
|
|
3698
|
+
vec2h: binaryComponentWise2h((a, b) => a / b),
|
|
3699
|
+
vec2i: binaryComponentWise2i(divInteger),
|
|
3700
|
+
vec2u: binaryComponentWise2u(divInteger),
|
|
3701
|
+
vec3f: binaryComponentWise3f((a, b) => a / b),
|
|
3702
|
+
vec3h: binaryComponentWise3h((a, b) => a / b),
|
|
3703
|
+
vec3i: binaryComponentWise3i(divInteger),
|
|
3704
|
+
vec3u: binaryComponentWise3u(divInteger),
|
|
3705
|
+
vec4f: binaryComponentWise4f((a, b) => a / b),
|
|
3706
|
+
vec4h: binaryComponentWise4h((a, b) => a / b),
|
|
3707
|
+
vec4i: binaryComponentWise4i(divInteger),
|
|
3708
|
+
vec4u: binaryComponentWise4u(divInteger)
|
|
3709
|
+
},
|
|
3710
|
+
dot: {
|
|
3711
|
+
vec2f: dotVec2,
|
|
3712
|
+
vec2h: dotVec2,
|
|
3713
|
+
vec2i: dotVec2,
|
|
3714
|
+
vec2u: dotVec2,
|
|
3715
|
+
vec3f: dotVec3,
|
|
3716
|
+
vec3h: dotVec3,
|
|
3717
|
+
vec3i: dotVec3,
|
|
3718
|
+
vec3u: dotVec3,
|
|
3719
|
+
vec4f: dotVec4,
|
|
3720
|
+
vec4h: dotVec4,
|
|
3721
|
+
vec4i: dotVec4,
|
|
3722
|
+
vec4u: dotVec4
|
|
3723
|
+
},
|
|
3724
|
+
normalize: {
|
|
3725
|
+
vec2f: (v) => {
|
|
3726
|
+
const len = lengthVec2(v);
|
|
3727
|
+
return vec2f(v.x / len, v.y / len);
|
|
3728
|
+
},
|
|
3729
|
+
vec2h: (v) => {
|
|
3730
|
+
const len = lengthVec2(v);
|
|
3731
|
+
return vec2h(v.x / len, v.y / len);
|
|
3732
|
+
},
|
|
3733
|
+
vec2i: (v) => {
|
|
3734
|
+
const len = lengthVec2(v);
|
|
3735
|
+
return vec2i(v.x / len, v.y / len);
|
|
3736
|
+
},
|
|
3737
|
+
vec2u: (v) => {
|
|
3738
|
+
const len = lengthVec2(v);
|
|
3739
|
+
return vec2u(v.x / len, v.y / len);
|
|
3740
|
+
},
|
|
3741
|
+
vec3f: (v) => {
|
|
3742
|
+
const len = lengthVec3(v);
|
|
3743
|
+
return vec3f(v.x / len, v.y / len, v.z / len);
|
|
3744
|
+
},
|
|
3745
|
+
vec3h: (v) => {
|
|
3746
|
+
const len = lengthVec3(v);
|
|
3747
|
+
return vec3h(v.x / len, v.y / len, v.z / len);
|
|
3748
|
+
},
|
|
3749
|
+
vec3i: (v) => {
|
|
3750
|
+
const len = lengthVec3(v);
|
|
3751
|
+
return vec3i(v.x / len, v.y / len, v.z / len);
|
|
3752
|
+
},
|
|
3753
|
+
vec3u: (v) => {
|
|
3754
|
+
const len = lengthVec3(v);
|
|
3755
|
+
return vec3u(v.x / len, v.y / len, v.z / len);
|
|
3756
|
+
},
|
|
3757
|
+
vec4f: (v) => {
|
|
3758
|
+
const len = lengthVec4(v);
|
|
3759
|
+
return vec4f(v.x / len, v.y / len, v.z / len, v.w / len);
|
|
3760
|
+
},
|
|
3761
|
+
vec4h: (v) => {
|
|
3762
|
+
const len = lengthVec4(v);
|
|
3763
|
+
return vec4h(v.x / len, v.y / len, v.z / len, v.w / len);
|
|
3764
|
+
},
|
|
3765
|
+
vec4i: (v) => {
|
|
3766
|
+
const len = lengthVec4(v);
|
|
3767
|
+
return vec4i(v.x / len, v.y / len, v.z / len, v.w / len);
|
|
3768
|
+
},
|
|
3769
|
+
vec4u: (v) => {
|
|
3770
|
+
const len = lengthVec4(v);
|
|
3771
|
+
return vec4u(v.x / len, v.y / len, v.z / len, v.w / len);
|
|
3772
|
+
}
|
|
3773
|
+
},
|
|
3774
|
+
cross: {
|
|
3775
|
+
vec3f: (a, b) => {
|
|
3776
|
+
return vec3f(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
|
|
3777
|
+
},
|
|
3778
|
+
vec3h: (a, b) => {
|
|
3779
|
+
return vec3h(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
|
|
3780
|
+
}
|
|
3781
|
+
},
|
|
3782
|
+
mod: {
|
|
3783
|
+
vec2f: binaryComponentWise2f((a, b) => a % b),
|
|
3784
|
+
vec2h: binaryComponentWise2h((a, b) => a % b),
|
|
3785
|
+
vec2i: binaryComponentWise2i((a, b) => a % b),
|
|
3786
|
+
vec2u: binaryComponentWise2u((a, b) => a % b),
|
|
3787
|
+
vec3f: binaryComponentWise3f((a, b) => a % b),
|
|
3788
|
+
vec3h: binaryComponentWise3h((a, b) => a % b),
|
|
3789
|
+
vec3i: binaryComponentWise3i((a, b) => a % b),
|
|
3790
|
+
vec3u: binaryComponentWise3u((a, b) => a % b),
|
|
3791
|
+
vec4f: binaryComponentWise4f((a, b) => a % b),
|
|
3792
|
+
vec4h: binaryComponentWise4h((a, b) => a % b),
|
|
3793
|
+
vec4i: binaryComponentWise4i((a, b) => a % b),
|
|
3794
|
+
vec4u: binaryComponentWise4u((a, b) => a % b)
|
|
3795
|
+
},
|
|
3796
|
+
floor: {
|
|
3797
|
+
vec2f: unary2f(Math.floor),
|
|
3798
|
+
vec2h: unary2h(Math.floor),
|
|
3799
|
+
vec3f: unary3f(Math.floor),
|
|
3800
|
+
vec3h: unary3h(Math.floor),
|
|
3801
|
+
vec4f: unary4f(Math.floor),
|
|
3802
|
+
vec4h: unary4h(Math.floor)
|
|
3803
|
+
},
|
|
3804
|
+
max: {
|
|
3805
|
+
vec2f: binaryComponentWise2f(Math.max),
|
|
3806
|
+
vec2h: binaryComponentWise2h(Math.max),
|
|
3807
|
+
vec2i: binaryComponentWise2i(Math.max),
|
|
3808
|
+
vec2u: binaryComponentWise2u(Math.max),
|
|
3809
|
+
vec3f: binaryComponentWise3f(Math.max),
|
|
3810
|
+
vec3h: binaryComponentWise3h(Math.max),
|
|
3811
|
+
vec3i: binaryComponentWise3i(Math.max),
|
|
3812
|
+
vec3u: binaryComponentWise3u(Math.max),
|
|
3813
|
+
vec4f: binaryComponentWise4f(Math.max),
|
|
3814
|
+
vec4h: binaryComponentWise4h(Math.max),
|
|
3815
|
+
vec4i: binaryComponentWise4i(Math.max),
|
|
3816
|
+
vec4u: binaryComponentWise4u(Math.max)
|
|
3817
|
+
},
|
|
3818
|
+
min: {
|
|
3819
|
+
vec2f: binaryComponentWise2f(Math.min),
|
|
3820
|
+
vec2h: binaryComponentWise2h(Math.min),
|
|
3821
|
+
vec2i: binaryComponentWise2i(Math.min),
|
|
3822
|
+
vec2u: binaryComponentWise2u(Math.min),
|
|
3823
|
+
vec3f: binaryComponentWise3f(Math.min),
|
|
3824
|
+
vec3h: binaryComponentWise3h(Math.min),
|
|
3825
|
+
vec3i: binaryComponentWise3i(Math.min),
|
|
3826
|
+
vec3u: binaryComponentWise3u(Math.min),
|
|
3827
|
+
vec4f: binaryComponentWise4f(Math.min),
|
|
3828
|
+
vec4h: binaryComponentWise4h(Math.min),
|
|
3829
|
+
vec4i: binaryComponentWise4i(Math.min),
|
|
3830
|
+
vec4u: binaryComponentWise4u(Math.min)
|
|
3831
|
+
},
|
|
3832
|
+
pow: {
|
|
3833
|
+
vec2f: (base, exponent) => vec2f(base.x ** exponent.x, base.y ** exponent.y),
|
|
3834
|
+
vec2h: (base, exponent) => vec2h(base.x ** exponent.x, base.y ** exponent.y),
|
|
3835
|
+
vec3f: (base, exponent) => vec3f(base.x ** exponent.x, base.y ** exponent.y, base.z ** exponent.z),
|
|
3836
|
+
vec3h: (base, exponent) => vec3h(base.x ** exponent.x, base.y ** exponent.y, base.z ** exponent.z),
|
|
3837
|
+
vec4f: (base, exponent) => vec4f(base.x ** exponent.x, base.y ** exponent.y, base.z ** exponent.z, base.w ** exponent.w),
|
|
3838
|
+
vec4h: (base, exponent) => vec4h(base.x ** exponent.x, base.y ** exponent.y, base.z ** exponent.z, base.w ** exponent.w)
|
|
3839
|
+
},
|
|
3840
|
+
sign: {
|
|
3841
|
+
vec2f: unary2f(Math.sign),
|
|
3842
|
+
vec2h: unary2h(Math.sign),
|
|
3843
|
+
vec2i: unary2i(Math.sign),
|
|
3844
|
+
vec3f: unary3f(Math.sign),
|
|
3845
|
+
vec3h: unary3h(Math.sign),
|
|
3846
|
+
vec3i: unary3i(Math.sign),
|
|
3847
|
+
vec4f: unary4f(Math.sign),
|
|
3848
|
+
vec4h: unary4h(Math.sign),
|
|
3849
|
+
vec4i: unary4i(Math.sign)
|
|
3850
|
+
},
|
|
3851
|
+
sqrt: {
|
|
3852
|
+
vec2f: unary2f(Math.sqrt),
|
|
3853
|
+
vec2h: unary2h(Math.sqrt),
|
|
3854
|
+
vec3f: unary3f(Math.sqrt),
|
|
3855
|
+
vec3h: unary3h(Math.sqrt),
|
|
3856
|
+
vec4f: unary4f(Math.sqrt),
|
|
3857
|
+
vec4h: unary4h(Math.sqrt)
|
|
3858
|
+
},
|
|
3859
|
+
mix: {
|
|
3860
|
+
vec2f: (e1, e2, e3) => {
|
|
3861
|
+
if (typeof e3 === "number") return vec2f(e1.x * (1 - e3) + e2.x * e3, e1.y * (1 - e3) + e2.y * e3);
|
|
3862
|
+
return vec2f(e1.x * (1 - e3.x) + e2.x * e3.x, e1.y * (1 - e3.y) + e2.y * e3.y);
|
|
3863
|
+
},
|
|
3864
|
+
vec2h: (e1, e2, e3) => {
|
|
3865
|
+
if (typeof e3 === "number") return vec2h(e1.x * (1 - e3) + e2.x * e3, e1.y * (1 - e3) + e2.y * e3);
|
|
3866
|
+
return vec2h(e1.x * (1 - e3.x) + e2.x * e3.x, e1.y * (1 - e3.y) + e2.y * e3.y);
|
|
3867
|
+
},
|
|
3868
|
+
vec3f: (e1, e2, e3) => {
|
|
3869
|
+
if (typeof e3 === "number") return vec3f(e1.x * (1 - e3) + e2.x * e3, e1.y * (1 - e3) + e2.y * e3, e1.z * (1 - e3) + e2.z * e3);
|
|
3870
|
+
return vec3f(e1.x * (1 - e3.x) + e2.x * e3.x, e1.y * (1 - e3.y) + e2.y * e3.y, e1.z * (1 - e3.z) + e2.z * e3.z);
|
|
3871
|
+
},
|
|
3872
|
+
vec3h: (e1, e2, e3) => {
|
|
3873
|
+
if (typeof e3 === "number") return vec3h(e1.x * (1 - e3) + e2.x * e3, e1.y * (1 - e3) + e2.y * e3, e1.z * (1 - e3) + e2.z * e3);
|
|
3874
|
+
return vec3h(e1.x * (1 - e3.x) + e2.x * e3.x, e1.y * (1 - e3.y) + e2.y * e3.y, e1.z * (1 - e3.z) + e2.z * e3.z);
|
|
3875
|
+
},
|
|
3876
|
+
vec4f: (e1, e2, e3) => {
|
|
3877
|
+
if (typeof e3 === "number") return vec4f(e1.x * (1 - e3) + e2.x * e3, e1.y * (1 - e3) + e2.y * e3, e1.z * (1 - e3) + e2.z * e3, e1.w * (1 - e3) + e2.w * e3);
|
|
3878
|
+
return vec4f(e1.x * (1 - e3.x) + e2.x * e3.x, e1.y * (1 - e3.y) + e2.y * e3.y, e1.z * (1 - e3.z) + e2.z * e3.z, e1.w * (1 - e3.w) + e2.w * e3.w);
|
|
3879
|
+
},
|
|
3880
|
+
vec4h: (e1, e2, e3) => {
|
|
3881
|
+
if (typeof e3 === "number") return vec4h(e1.x * (1 - e3) + e2.x * e3, e1.y * (1 - e3) + e2.y * e3, e1.z * (1 - e3) + e2.z * e3, e1.w * (1 - e3) + e2.w * e3);
|
|
3882
|
+
return vec4h(e1.x * (1 - e3.x) + e2.x * e3.x, e1.y * (1 - e3.y) + e2.y * e3.y, e1.z * (1 - e3.z) + e2.z * e3.z, e1.w * (1 - e3.w) + e2.w * e3.w);
|
|
3883
|
+
}
|
|
3884
|
+
},
|
|
3885
|
+
sin: {
|
|
3886
|
+
vec2f: unary2f(Math.sin),
|
|
3887
|
+
vec2h: unary2h(Math.sin),
|
|
3888
|
+
vec3f: unary3f(Math.sin),
|
|
3889
|
+
vec3h: unary3h(Math.sin),
|
|
3890
|
+
vec4f: unary4f(Math.sin),
|
|
3891
|
+
vec4h: unary4h(Math.sin)
|
|
3892
|
+
},
|
|
3893
|
+
cos: {
|
|
3894
|
+
vec2f: unary2f(Math.cos),
|
|
3895
|
+
vec2h: unary2h(Math.cos),
|
|
3896
|
+
vec3f: unary3f(Math.cos),
|
|
3897
|
+
vec3h: unary3h(Math.cos),
|
|
3898
|
+
vec4f: unary4f(Math.cos),
|
|
3899
|
+
vec4h: unary4h(Math.cos)
|
|
3900
|
+
},
|
|
3901
|
+
cosh: {
|
|
3902
|
+
vec2f: unary2f(Math.cosh),
|
|
3903
|
+
vec2h: unary2h(Math.cosh),
|
|
3904
|
+
vec3f: unary3f(Math.cosh),
|
|
3905
|
+
vec3h: unary3h(Math.cosh),
|
|
3906
|
+
vec4f: unary4f(Math.cosh),
|
|
3907
|
+
vec4h: unary4h(Math.cosh)
|
|
3908
|
+
},
|
|
3909
|
+
exp: {
|
|
3910
|
+
vec2f: unary2f(Math.exp),
|
|
3911
|
+
vec2h: unary2h(Math.exp),
|
|
3912
|
+
vec3f: unary3f(Math.exp),
|
|
3913
|
+
vec3h: unary3h(Math.exp),
|
|
3914
|
+
vec4f: unary4f(Math.exp),
|
|
3915
|
+
vec4h: unary4h(Math.exp)
|
|
3916
|
+
},
|
|
3917
|
+
exp2: {
|
|
3918
|
+
vec2f: unary2f((val) => 2 ** val),
|
|
3919
|
+
vec2h: unary2h((val) => 2 ** val),
|
|
3920
|
+
vec3f: unary3f((val) => 2 ** val),
|
|
3921
|
+
vec3h: unary3h((val) => 2 ** val),
|
|
3922
|
+
vec4f: unary4f((val) => 2 ** val),
|
|
3923
|
+
vec4h: unary4h((val) => 2 ** val)
|
|
3924
|
+
},
|
|
3925
|
+
log: {
|
|
3926
|
+
vec2f: unary2f(Math.log),
|
|
3927
|
+
vec2h: unary2h(Math.log),
|
|
3928
|
+
vec3f: unary3f(Math.log),
|
|
3929
|
+
vec3h: unary3h(Math.log),
|
|
3930
|
+
vec4f: unary4f(Math.log),
|
|
3931
|
+
vec4h: unary4h(Math.log)
|
|
3932
|
+
},
|
|
3933
|
+
log2: {
|
|
3934
|
+
vec2f: unary2f(Math.log2),
|
|
3935
|
+
vec2h: unary2h(Math.log2),
|
|
3936
|
+
vec3f: unary3f(Math.log2),
|
|
3937
|
+
vec3h: unary3h(Math.log2),
|
|
3938
|
+
vec4f: unary4f(Math.log2),
|
|
3939
|
+
vec4h: unary4h(Math.log2)
|
|
3940
|
+
},
|
|
3941
|
+
fract: {
|
|
3942
|
+
vec2f: unary2f((value) => value - Math.floor(value)),
|
|
3943
|
+
vec2h: unary2h((value) => value - Math.floor(value)),
|
|
3944
|
+
vec3f: unary3f((value) => value - Math.floor(value)),
|
|
3945
|
+
vec3h: unary3h((value) => value - Math.floor(value)),
|
|
3946
|
+
vec4f: unary4f((value) => value - Math.floor(value)),
|
|
3947
|
+
vec4h: unary4h((value) => value - Math.floor(value))
|
|
3948
|
+
},
|
|
3949
|
+
isCloseToZero: {
|
|
3950
|
+
vec2f: (v, n) => Math.abs(v.x) <= n && Math.abs(v.y) <= n,
|
|
3951
|
+
vec2h: (v, n) => Math.abs(v.x) <= n && Math.abs(v.y) <= n,
|
|
3952
|
+
vec3f: (v, n) => Math.abs(v.x) <= n && Math.abs(v.y) <= n && Math.abs(v.z) <= n,
|
|
3953
|
+
vec3h: (v, n) => Math.abs(v.x) <= n && Math.abs(v.y) <= n && Math.abs(v.z) <= n,
|
|
3954
|
+
vec4f: (v, n) => Math.abs(v.x) <= n && Math.abs(v.y) <= n && Math.abs(v.z) <= n && Math.abs(v.w) <= n,
|
|
3955
|
+
vec4h: (v, n) => Math.abs(v.x) <= n && Math.abs(v.y) <= n && Math.abs(v.z) <= n && Math.abs(v.w) <= n
|
|
3956
|
+
},
|
|
3957
|
+
neg: {
|
|
3958
|
+
vec2f: unary2f((value) => -value),
|
|
3959
|
+
vec2h: unary2h((value) => -value),
|
|
3960
|
+
vec2i: unary2i((value) => -value),
|
|
3961
|
+
vec2u: unary2u((value) => -value),
|
|
3962
|
+
"vec2<bool>": (e) => vec2b(!e.x, !e.y),
|
|
3963
|
+
vec3f: unary3f((value) => -value),
|
|
3964
|
+
vec3h: unary3h((value) => -value),
|
|
3965
|
+
vec3i: unary3i((value) => -value),
|
|
3966
|
+
vec3u: unary3u((value) => -value),
|
|
3967
|
+
"vec3<bool>": (e) => vec3b(!e.x, !e.y, !e.z),
|
|
3968
|
+
vec4f: unary4f((value) => -value),
|
|
3969
|
+
vec4h: unary4h((value) => -value),
|
|
3970
|
+
vec4i: unary4i((value) => -value),
|
|
3971
|
+
vec4u: unary4u((value) => -value),
|
|
3972
|
+
"vec4<bool>": (e) => vec4b(!e.x, !e.y, !e.z, !e.w)
|
|
3973
|
+
},
|
|
3974
|
+
select: {
|
|
3975
|
+
vec2f: (f, t, c) => vec2f(c.x ? t.x : f.x, c.y ? t.y : f.y),
|
|
3976
|
+
vec2h: (f, t, c) => vec2h(c.x ? t.x : f.x, c.y ? t.y : f.y),
|
|
3977
|
+
vec2i: (f, t, c) => vec2i(c.x ? t.x : f.x, c.y ? t.y : f.y),
|
|
3978
|
+
vec2u: (f, t, c) => vec2u(c.x ? t.x : f.x, c.y ? t.y : f.y),
|
|
3979
|
+
"vec2<bool>": (f, t, c) => vec2b(c.x ? t.x : f.x, c.y ? t.y : f.y),
|
|
3980
|
+
vec3f: (f, t, c) => vec3f(c.x ? t.x : f.x, c.y ? t.y : f.y, c.z ? t.z : f.z),
|
|
3981
|
+
vec3h: (f, t, c) => vec3h(c.x ? t.x : f.x, c.y ? t.y : f.y, c.z ? t.z : f.z),
|
|
3982
|
+
vec3i: (f, t, c) => vec3i(c.x ? t.x : f.x, c.y ? t.y : f.y, c.z ? t.z : f.z),
|
|
3983
|
+
vec3u: (f, t, c) => vec3u(c.x ? t.x : f.x, c.y ? t.y : f.y, c.z ? t.z : f.z),
|
|
3984
|
+
"vec3<bool>": (f, t, c) => vec3b(c.x ? t.x : f.x, c.y ? t.y : f.y, c.z ? t.z : f.z),
|
|
3985
|
+
vec4f: (f, t, c) => vec4f(c.x ? t.x : f.x, c.y ? t.y : f.y, c.z ? t.z : f.z, c.w ? t.w : f.w),
|
|
3986
|
+
vec4h: (f, t, c) => vec4h(c.x ? t.x : f.x, c.y ? t.y : f.y, c.z ? t.z : f.z, c.w ? t.w : f.w),
|
|
3987
|
+
vec4i: (f, t, c) => vec4i(c.x ? t.x : f.x, c.y ? t.y : f.y, c.z ? t.z : f.z, c.w ? t.w : f.w),
|
|
3988
|
+
vec4u: (f, t, c) => vec4u(c.x ? t.x : f.x, c.y ? t.y : f.y, c.z ? t.z : f.z, c.w ? t.w : f.w),
|
|
3989
|
+
"vec4<bool>": (f, t, c) => vec4b(c.x ? t.x : f.x, c.y ? t.y : f.y, c.z ? t.z : f.z, c.w ? t.w : f.w)
|
|
3990
|
+
},
|
|
3991
|
+
tanh: {
|
|
3992
|
+
vec2f: unary2f(Math.tanh),
|
|
3993
|
+
vec2h: unary2h(Math.tanh),
|
|
3994
|
+
vec3f: unary3f(Math.tanh),
|
|
3995
|
+
vec3h: unary3h(Math.tanh),
|
|
3996
|
+
vec4f: unary4f(Math.tanh),
|
|
3997
|
+
vec4h: unary4h(Math.tanh)
|
|
3998
|
+
},
|
|
3999
|
+
bitcastU32toF32: {
|
|
4000
|
+
vec2u: (n) => vec2f(bitcastU32toF32Impl(n.x), bitcastU32toF32Impl(n.y)),
|
|
4001
|
+
vec3u: (n) => vec3f(bitcastU32toF32Impl(n.x), bitcastU32toF32Impl(n.y), bitcastU32toF32Impl(n.z)),
|
|
4002
|
+
vec4u: (n) => vec4f(bitcastU32toF32Impl(n.x), bitcastU32toF32Impl(n.y), bitcastU32toF32Impl(n.z), bitcastU32toF32Impl(n.w))
|
|
4003
|
+
},
|
|
4004
|
+
bitcastU32toI32: {
|
|
4005
|
+
vec2u: (n) => vec2i(bitcastU32toI32Impl(n.x), bitcastU32toI32Impl(n.y)),
|
|
4006
|
+
vec3u: (n) => vec3i(bitcastU32toI32Impl(n.x), bitcastU32toI32Impl(n.y), bitcastU32toI32Impl(n.z)),
|
|
4007
|
+
vec4u: (n) => vec4i(bitcastU32toI32Impl(n.x), bitcastU32toI32Impl(n.y), bitcastU32toI32Impl(n.z), bitcastU32toI32Impl(n.w))
|
|
4008
|
+
}
|
|
4009
|
+
};
|
|
4010
|
+
|
|
4011
|
+
//#endregion
|
|
4012
|
+
//#region src/std/operators.ts
|
|
4013
|
+
const getPrimitive = (t) => "primitive" in t ? t.primitive : t;
|
|
4014
|
+
const makeBinarySignature = (opts) => (lhs, rhs) => {
|
|
4015
|
+
const { restrict } = opts ?? {};
|
|
4016
|
+
const fail = (msg) => {
|
|
4017
|
+
if (restrict) throw new SignatureNotSupportedError([lhs, rhs], restrict);
|
|
4018
|
+
throw new Error(`Cannot apply operator to ${lhs.type} and ${rhs.type}: ${msg}`);
|
|
4019
|
+
};
|
|
4020
|
+
if (opts?.noMat && (isMat(lhs) || isMat(rhs))) return fail("matrices not supported");
|
|
4021
|
+
const lhsC = isVec(lhs) || isMat(lhs);
|
|
4022
|
+
const rhsC = isVec(rhs) || isMat(rhs);
|
|
4023
|
+
if (!lhsC && !rhsC) {
|
|
4024
|
+
const unified = unify([lhs, rhs], restrict);
|
|
4025
|
+
if (!unified) return fail("incompatible scalar types");
|
|
4026
|
+
return {
|
|
4027
|
+
argTypes: unified,
|
|
4028
|
+
returnType: unified[0]
|
|
4029
|
+
};
|
|
4030
|
+
}
|
|
4031
|
+
if (lhsC && rhsC) {
|
|
4032
|
+
if (opts?.matVecProduct && isVec(lhs) !== isVec(rhs)) return {
|
|
4033
|
+
argTypes: [lhs, rhs],
|
|
4034
|
+
returnType: isVec(lhs) ? lhs : rhs
|
|
4035
|
+
};
|
|
4036
|
+
if (lhs.type !== rhs.type) return fail("operands must have the same type");
|
|
4037
|
+
return {
|
|
4038
|
+
argTypes: [lhs, rhs],
|
|
4039
|
+
returnType: lhs
|
|
4040
|
+
};
|
|
4041
|
+
}
|
|
4042
|
+
const [scalar, composite] = lhsC ? [rhs, lhs] : [lhs, rhs];
|
|
4043
|
+
const unified = unify([scalar], [getPrimitive(composite)]);
|
|
4044
|
+
if (!unified) return fail(`scalar not convertible to ${getPrimitive(composite).type}`);
|
|
4045
|
+
return {
|
|
4046
|
+
argTypes: lhsC ? [lhs, unified[0]] : [unified[0], rhs],
|
|
4047
|
+
returnType: composite
|
|
4048
|
+
};
|
|
4049
|
+
};
|
|
4050
|
+
const binaryArithmeticSignature = makeBinarySignature();
|
|
4051
|
+
const binaryMulSignature = makeBinarySignature({ matVecProduct: true });
|
|
4052
|
+
const binaryDivSignature = makeBinarySignature({
|
|
4053
|
+
noMat: true,
|
|
4054
|
+
restrict: [
|
|
4055
|
+
f32,
|
|
4056
|
+
f16,
|
|
4057
|
+
abstractFloat
|
|
4058
|
+
]
|
|
4059
|
+
});
|
|
4060
|
+
function cpuAdd(lhs, rhs) {
|
|
4061
|
+
if (typeof lhs === "number" && typeof rhs === "number") return lhs + rhs;
|
|
4062
|
+
if (typeof lhs === "number" && isVecInstance(rhs)) return VectorOps.addMixed[rhs.kind](rhs, lhs);
|
|
4063
|
+
if (isVecInstance(lhs) && typeof rhs === "number") return VectorOps.addMixed[lhs.kind](lhs, rhs);
|
|
4064
|
+
if (isVecInstance(lhs) && isVecInstance(rhs) || isMatInstance(lhs) && isMatInstance(rhs)) return VectorOps.add[lhs.kind](lhs, rhs);
|
|
4065
|
+
throw new Error("Add/Sub called with invalid arguments.");
|
|
4066
|
+
}
|
|
4067
|
+
const add = dualImpl({
|
|
4068
|
+
name: "add",
|
|
4069
|
+
signature: binaryArithmeticSignature,
|
|
4070
|
+
normalImpl: cpuAdd,
|
|
4071
|
+
codegenImpl: (_ctx, [lhs, rhs]) => stitch`(${lhs} + ${rhs})`
|
|
4072
|
+
});
|
|
4073
|
+
function cpuSub(lhs, rhs) {
|
|
4074
|
+
return cpuAdd(lhs, cpuMul(-1, rhs));
|
|
4075
|
+
}
|
|
4076
|
+
const sub = dualImpl({
|
|
4077
|
+
name: "sub",
|
|
4078
|
+
signature: binaryArithmeticSignature,
|
|
4079
|
+
normalImpl: cpuSub,
|
|
4080
|
+
codegenImpl: (_ctx, [lhs, rhs]) => stitch`(${lhs} - ${rhs})`
|
|
4081
|
+
});
|
|
4082
|
+
function cpuMul(lhs, rhs) {
|
|
4083
|
+
if (typeof lhs === "number" && typeof rhs === "number") return lhs * rhs;
|
|
4084
|
+
if (typeof lhs === "number" && (isVecInstance(rhs) || isMatInstance(rhs))) return VectorOps.mulSxV[rhs.kind](lhs, rhs);
|
|
4085
|
+
if ((isVecInstance(lhs) || isMatInstance(lhs)) && typeof rhs === "number") return VectorOps.mulSxV[lhs.kind](rhs, lhs);
|
|
4086
|
+
if (isVecInstance(lhs) && isVecInstance(rhs)) return VectorOps.mulVxV[lhs.kind](lhs, rhs);
|
|
4087
|
+
if (isFloat32VecInstance(lhs) && isMatInstance(rhs)) return VectorOps.mulVxM[rhs.kind](lhs, rhs);
|
|
4088
|
+
if (isMatInstance(lhs) && isFloat32VecInstance(rhs)) return VectorOps.mulMxV[lhs.kind](lhs, rhs);
|
|
4089
|
+
if (isMatInstance(lhs) && isMatInstance(rhs)) return VectorOps.mulVxV[lhs.kind](lhs, rhs);
|
|
4090
|
+
throw new Error("Mul called with invalid arguments.");
|
|
4091
|
+
}
|
|
4092
|
+
const mul = dualImpl({
|
|
4093
|
+
name: "mul",
|
|
4094
|
+
signature: binaryMulSignature,
|
|
4095
|
+
normalImpl: cpuMul,
|
|
4096
|
+
codegenImpl: (_ctx, [lhs, rhs]) => stitch`(${lhs} * ${rhs})`
|
|
4097
|
+
});
|
|
4098
|
+
function cpuDiv(lhs, rhs) {
|
|
4099
|
+
if (typeof lhs === "number" && typeof rhs === "number") return lhs / rhs;
|
|
4100
|
+
if (typeof lhs === "number" && isVecInstance(rhs)) {
|
|
4101
|
+
const schema = vecTypeToConstructor[rhs.kind];
|
|
4102
|
+
return VectorOps.div[rhs.kind](schema(lhs), rhs);
|
|
4103
|
+
}
|
|
4104
|
+
if (isVecInstance(lhs) && typeof rhs === "number") {
|
|
4105
|
+
const schema = vecTypeToConstructor[lhs.kind];
|
|
4106
|
+
return VectorOps.div[lhs.kind](lhs, schema(rhs));
|
|
4107
|
+
}
|
|
4108
|
+
if (isVecInstance(lhs) && isVecInstance(rhs)) return VectorOps.div[lhs.kind](lhs, rhs);
|
|
4109
|
+
throw new Error("Div called with invalid arguments.");
|
|
4110
|
+
}
|
|
4111
|
+
const div = dualImpl({
|
|
4112
|
+
name: "div",
|
|
4113
|
+
signature: binaryDivSignature,
|
|
4114
|
+
normalImpl: cpuDiv,
|
|
4115
|
+
codegenImpl: (_ctx, [lhs, rhs]) => stitch`(${lhs} / ${rhs})`,
|
|
4116
|
+
ignoreImplicitCastWarning: true
|
|
4117
|
+
});
|
|
4118
|
+
/**
|
|
4119
|
+
* @privateRemarks
|
|
4120
|
+
* Both JS and WGSL implementations use truncated definition of modulo
|
|
4121
|
+
*/
|
|
4122
|
+
const mod = dualImpl({
|
|
4123
|
+
name: "mod",
|
|
4124
|
+
signature: binaryDivSignature,
|
|
4125
|
+
normalImpl: ((a, b) => {
|
|
4126
|
+
if (typeof a === "number" && typeof b === "number") return a % b;
|
|
4127
|
+
if (typeof a === "number" && isVecInstance(b)) {
|
|
4128
|
+
const schema = vecTypeToConstructor[b.kind];
|
|
4129
|
+
return VectorOps.mod[b.kind](schema(a), b);
|
|
4130
|
+
}
|
|
4131
|
+
if (isVecInstance(a) && typeof b === "number") {
|
|
4132
|
+
const schema = vecTypeToConstructor[a.kind];
|
|
4133
|
+
return VectorOps.mod[a.kind](a, schema(b));
|
|
4134
|
+
}
|
|
4135
|
+
if (isVecInstance(a) && isVecInstance(b)) return VectorOps.mod[a.kind](a, b);
|
|
4136
|
+
throw new Error("Mod called with invalid arguments, expected types: number or vector.");
|
|
4137
|
+
}),
|
|
4138
|
+
codegenImpl: (_ctx, [lhs, rhs]) => stitch`(${lhs} % ${rhs})`
|
|
4139
|
+
});
|
|
4140
|
+
function cpuNeg(value) {
|
|
4141
|
+
if (typeof value === "number") return -value;
|
|
4142
|
+
return VectorOps.neg[value.kind](value);
|
|
4143
|
+
}
|
|
4144
|
+
const neg = dualImpl({
|
|
4145
|
+
name: "neg",
|
|
4146
|
+
signature: (arg) => ({
|
|
4147
|
+
argTypes: [arg],
|
|
4148
|
+
returnType: arg
|
|
4149
|
+
}),
|
|
4150
|
+
normalImpl: cpuNeg,
|
|
4151
|
+
codegenImpl: (_ctx, [arg]) => stitch`-(${arg})`
|
|
4152
|
+
});
|
|
4153
|
+
|
|
4154
|
+
//#endregion
|
|
4155
|
+
export { VecBase as $, getMetaData as $n, IllegalBufferAccessError as $t, concretizeSnippets as A, isAlignAttrib as An, stitch as At, vec2h$1 as B, isNaturallyEphemeral as Bn, SimulationState as Bt, translation4 as C, isDisarray as Cn, implicitFrom as Ct, ArrayExpression as D, undecorate as Dn, ptrStorage as Dt, dualImpl as E, isUnstruct as En, ptrPrivate as Et, RandomNameRegistry as F, isInterpolateAttrib as Fn, provideCtx as Ft, vec3h$1 as G, isVecInstance as Gn, isSelfResolvable as Gt, vec2u$1 as H, isSizeAttrib as Hn, isGPUBuffer as Ht, StrictNameRegistry as I, isLocationAttrib as In, provideInsideTgpuFn as It, vec4b$1 as J, isWgslData as Jn, isLazy as Jt, vec3i$1 as K, isVoid as Kn, isWgsl as Kt, isValidProp as L, isMat as Ln, topLevelState as Lt, INTERNAL_createStruct as M, isBuiltinAttrib as Mn, getResolutionCtx as Mt, abstruct as N, isDecorated as Nn, inCodegenMode as Nt, coerceToSnippet as O, unptr as On, ptrUniform as Ot, struct as P, isHalfPrecisionSchema as Pn, isInsideTgpuFn as Pt, vec4u$1 as Q, PERF as Qn, ExecutionError as Qt, vec2b$1 as R, isMat2x2f as Rn, CodegenState as Rt, scaling4 as S, isData as Sn, createPtrFromOrigin as St, MissingCpuImplError as T, isLooseDecorated as Tn, ptrHandle as Tt, vec3b$1 as U, isVec as Un, isGPUCallable as Ut, vec2i$1 as V, isPtr as Vn, getOwnSnippet as Vt, vec3f$1 as W, isVecBool as Wn, isKnownAtComptime as Wt, vec4h$1 as X, kindToDefaultFormatMap as Xn, isProviding as Xt, vec4f$1 as Y, isWgslStruct as Yn, isMutableAccessor as Yt, vec4i$1 as Z, vertexFormats as Zn, isSlot as Zt, mat4x4f as _, MatrixColumnsAccess as _n, schemaCallWrapperGPU as _t, neg as a, NotUniformError as an, $getNameForward as ar, i32 as at, rotationY4 as b, getCustomLocation as bn, derefSnippet as bt, bitcastU32toF32Impl as c, WgslTypeError as cn, $internal as cr, convertStructValues as ct, MatBase as d, isEphemeralSnippet as dn, $repr as dr, tryConvertSnippet as dt, IllegalVarAccessError as en, getName as er, abstractFloat as et, identity2 as f, isSnippet as fn, $resolve as fr, unify as ft, mat3x3f as g, InfixDispatch as gn, schemaCallWrapper as gt, mat2x2f as h, ConsoleLog as hn, safeStringify as ht, mul as i, MissingVertexBuffersError as in, $cast as ir, f32 as it, numericLiteralToSnippet as j, isAtomic as jn, getExecMode as jt, concretize as k, Void as kn, ptrWorkgroup as kt, bitcastU32toI32Impl as l, invariant as ln, $ownSnippet as lr, convertToCommonType as lt, identity4 as m, snip as mn, niceStringify as mt, div as n, MissingLinksError as nn, isNamable as nr, bool as nt, sub as o, ResolutionError as on, $gpuCallable as or, u16 as ot, identity3 as p, isSnippetNumeric as pn, isMarkedInternal as pr, assertExhaustive as pt, vec3u$1 as q, isWgslArray as qn, isAccessor as qt, mod as r, MissingSlotValueError as rn, setName as rr, f16 as rt, VectorOps as s, SignatureNotSupportedError as sn, $gpuValueOf as sr, u32 as st, add as t, MissingBindGroupsError as tn, hasTinyestMetadata as tr, abstractInt as tt, smoothstepScalar as u, isEphemeralOrigin as un, $providing as ur, getBestConversion as ut, matToArray as v, UnknownData as vn, RefOperator as vt, comptime as w, isLooseData as wn, ptrFn as wt, rotationZ4 as x, getCustomSize as xn, isRef as xt, rotationX4 as y, getCustomAlignment as yn, _ref as yt, vec2f$1 as z, isMat3x3f as zn, NormalState as zt };
|
|
4156
|
+
//# sourceMappingURL=operators-HTxa_0k9.js.map
|