typegpu 0.10.0 → 0.10.2
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/{chunk-BYypO7fO.js → _virtual/_rolldown/runtime.js} +1 -1
- package/builtin.d.ts +50 -0
- package/builtin.js +37 -0
- package/common/fullScreenTriangle.d.ts +26 -0
- package/common/fullScreenTriangle.js +36 -0
- package/common/index.d.ts +2 -3
- package/common/index.js +3 -4
- package/core/buffer/buffer.d.ts +74 -0
- package/core/buffer/buffer.js +197 -0
- package/core/buffer/bufferShorthand.d.ts +48 -0
- package/core/buffer/bufferShorthand.js +49 -0
- package/core/buffer/bufferUsage.d.ts +45 -0
- package/core/buffer/bufferUsage.js +163 -0
- package/core/constant/tgpuConstant.d.ts +28 -0
- package/core/constant/tgpuConstant.js +67 -0
- package/core/declare/tgpuDeclare.d.ts +18 -0
- package/core/declare/tgpuDeclare.js +40 -0
- package/core/function/autoIO.d.ts +37 -0
- package/core/function/autoIO.js +87 -0
- package/core/function/comptime.d.ts +39 -0
- package/core/function/comptime.js +51 -0
- package/core/function/createCallableSchema.js +42 -0
- package/core/function/dualImpl.js +54 -0
- package/core/function/extractArgs.js +204 -0
- package/core/function/fnCore.js +79 -0
- package/core/function/fnTypes.d.ts +34 -0
- package/core/function/ioSchema.d.ts +10 -0
- package/core/function/ioSchema.js +30 -0
- package/core/function/shelllessImpl.d.ts +28 -0
- package/core/function/shelllessImpl.js +23 -0
- package/core/function/templateUtils.js +13 -0
- package/core/function/tgpuComputeFn.d.ts +49 -0
- package/core/function/tgpuComputeFn.js +62 -0
- package/core/function/tgpuFn.d.ts +52 -0
- package/core/function/tgpuFn.js +170 -0
- package/core/function/tgpuFragmentFn.d.ts +68 -0
- package/core/function/tgpuFragmentFn.js +68 -0
- package/core/function/tgpuVertexFn.d.ts +55 -0
- package/core/function/tgpuVertexFn.js +65 -0
- package/core/pipeline/applyPipelineState.js +37 -0
- package/core/pipeline/computePipeline.d.ts +58 -0
- package/core/pipeline/computePipeline.js +226 -0
- package/core/pipeline/connectAttachmentToShader.js +26 -0
- package/core/pipeline/connectTargetsToShader.js +29 -0
- package/core/pipeline/limitsOverflow.js +13 -0
- package/core/pipeline/renderPipeline.d.ts +266 -0
- package/core/pipeline/renderPipeline.js +471 -0
- package/core/pipeline/timeable.d.ts +23 -0
- package/core/pipeline/timeable.js +61 -0
- package/core/pipeline/typeGuards.js +29 -0
- package/core/querySet/querySet.d.ts +22 -0
- package/core/querySet/querySet.js +103 -0
- package/core/rawCodeSnippet/tgpuRawCodeSnippet.d.ts +59 -0
- package/core/rawCodeSnippet/tgpuRawCodeSnippet.js +96 -0
- package/core/resolve/externals.d.ts +10 -0
- package/core/resolve/externals.js +58 -0
- package/core/resolve/namespace.d.ts +38 -0
- package/core/resolve/namespace.js +41 -0
- package/core/resolve/resolveData.js +146 -0
- package/core/resolve/stitch.js +25 -0
- package/core/resolve/tgpuResolve.d.ts +151 -0
- package/core/resolve/tgpuResolve.js +68 -0
- package/core/root/configurableImpl.js +18 -0
- package/core/root/init.d.ts +69 -0
- package/core/root/init.js +457 -0
- package/core/root/rootTypes.d.ts +622 -0
- package/core/sampler/sampler.d.ts +35 -0
- package/core/sampler/sampler.js +116 -0
- package/core/simulate/tgpuSimulate.d.ts +36 -0
- package/core/simulate/tgpuSimulate.js +76 -0
- package/core/slot/accessor.d.ts +13 -0
- package/core/slot/accessor.js +97 -0
- package/core/slot/internalSlots.js +7 -0
- package/core/slot/lazy.d.ts +6 -0
- package/core/slot/lazy.js +42 -0
- package/core/slot/slot.d.ts +6 -0
- package/core/slot/slot.js +40 -0
- package/core/slot/slotTypes.d.ts +92 -0
- package/core/slot/slotTypes.js +21 -0
- package/core/texture/externalTexture.d.ts +12 -0
- package/core/texture/externalTexture.js +48 -0
- package/core/texture/texture.d.ts +118 -0
- package/core/texture/texture.js +312 -0
- package/core/texture/textureFormats.d.ts +29 -0
- package/core/texture/textureFormats.js +99 -0
- package/core/texture/textureProps.d.ts +11 -0
- package/core/texture/textureUtils.js +224 -0
- package/core/texture/usageExtension.d.ts +21 -0
- package/core/texture/usageExtension.js +21 -0
- package/core/unroll/tgpuUnroll.d.ts +13 -0
- package/core/unroll/tgpuUnroll.js +36 -0
- package/core/valueProxyUtils.js +44 -0
- package/core/variable/tgpuVariable.d.ts +38 -0
- package/core/variable/tgpuVariable.js +101 -0
- package/core/vertexLayout/connectAttributesToShader.js +59 -0
- package/core/vertexLayout/vertexAttribute.d.ts +29 -0
- package/core/vertexLayout/vertexLayout.d.ts +19 -0
- package/core/vertexLayout/vertexLayout.js +103 -0
- package/data/alignIO.js +15 -0
- package/data/alignmentOf.d.ts +10 -0
- package/data/alignmentOf.js +88 -0
- package/data/array.d.ts +28 -0
- package/data/array.js +48 -0
- package/data/atomic.d.ts +15 -0
- package/data/atomic.js +25 -0
- package/data/attributes.d.ts +121 -0
- package/data/attributes.js +145 -0
- package/data/autoStruct.d.ts +3 -0
- package/data/autoStruct.js +83 -0
- package/data/compiledIO.js +231 -0
- package/data/dataIO.js +549 -0
- package/data/dataTypes.d.ts +115 -0
- package/data/dataTypes.js +97 -0
- package/data/deepEqual.d.ts +25 -0
- package/data/deepEqual.js +58 -0
- package/data/disarray.d.ts +34 -0
- package/data/disarray.js +52 -0
- package/data/getLongestContiguousPrefix.d.ts +10 -0
- package/data/getLongestContiguousPrefix.js +15 -0
- package/data/index.d.ts +26 -4
- package/data/index.js +27 -7
- package/data/instanceToSchema.d.ts +33 -0
- package/data/isContiguous.d.ts +10 -0
- package/data/isContiguous.js +15 -0
- package/data/matrix.d.ts +126 -0
- package/data/matrix.js +517 -0
- package/data/numberOps.js +24 -0
- package/data/numeric.d.ts +81 -0
- package/data/numeric.js +234 -0
- package/data/offsetUtils.d.ts +33 -0
- package/data/offsetUtils.js +167 -0
- package/data/offsets.js +36 -0
- package/data/partialIO.js +68 -0
- package/data/ptr.d.ts +12 -0
- package/data/ptr.js +46 -0
- package/data/ref.d.ts +37 -0
- package/data/ref.js +96 -0
- package/data/sampler.d.ts +107 -0
- package/data/sampler.js +26 -0
- package/data/schemaCallWrapper.js +32 -0
- package/data/schemaMemoryLayout.js +200 -0
- package/data/sizeOf.d.ts +10 -0
- package/data/sizeOf.js +15 -0
- package/data/snippet.d.ts +26 -0
- package/data/snippet.js +61 -0
- package/data/struct.d.ts +17 -0
- package/data/struct.js +46 -0
- package/data/texture.d.ts +292 -0
- package/{texture-Dg5ybJro.js → data/texture.js} +6 -3
- package/data/unstruct.d.ts +24 -0
- package/data/unstruct.js +43 -0
- package/data/vector.d.ts +191 -0
- package/data/vector.js +247 -0
- package/data/vectorImpl.js +516 -0
- package/data/vectorOps.js +664 -0
- package/data/vertexFormatData.d.ts +190 -0
- package/data/vertexFormatData.js +110 -0
- package/data/wgslTypes.d.ts +896 -0
- package/data/wgslTypes.js +215 -0
- package/errors.d.ts +44 -0
- package/errors.js +128 -0
- package/execMode.js +51 -0
- package/extension.d.ts +11 -0
- package/extension.js +18 -0
- package/getGPUValue.js +9 -0
- package/index.d.ts +40 -243
- package/index.js +19 -6318
- package/indexNamedExports.d.ts +38 -0
- package/mathUtils.js +13 -0
- package/memo.js +22 -0
- package/nameRegistry.d.ts +30 -0
- package/nameRegistry.js +449 -0
- package/package.js +5 -0
- package/package.json +23 -23
- package/resolutionCtx.d.ts +29 -0
- package/resolutionCtx.js +546 -0
- package/shared/env.js +13 -0
- package/shared/generators.js +14 -0
- package/shared/meta.d.ts +39 -0
- package/shared/meta.js +63 -0
- package/shared/repr.d.ts +108 -0
- package/shared/stringify.js +22 -0
- package/shared/symbols.d.ts +61 -0
- package/shared/symbols.js +71 -0
- package/shared/utilityTypes.d.ts +29 -0
- package/shared/utilityTypes.js +7 -0
- package/shared/vertexFormat.d.ts +70 -0
- package/shared/vertexFormat.js +64 -0
- package/std/array.d.ts +7 -0
- package/std/array.js +27 -0
- package/std/atomic.d.ts +19 -0
- package/std/atomic.js +113 -0
- package/std/bitcast.d.ts +10 -0
- package/std/bitcast.js +43 -0
- package/std/boolean.d.ts +127 -0
- package/std/boolean.js +274 -0
- package/std/derivative.d.ts +16 -0
- package/std/derivative.js +89 -0
- package/std/discard.d.ts +6 -0
- package/std/discard.js +16 -0
- package/std/extensions.d.ts +8 -0
- package/std/extensions.js +14 -0
- package/std/index.d.ts +15 -3
- package/std/index.js +16 -5
- package/std/matrix.d.ts +41 -0
- package/std/matrix.js +87 -0
- package/std/numeric.d.ts +254 -0
- package/std/numeric.js +847 -0
- package/std/operators.d.ts +48 -0
- package/std/operators.js +153 -0
- package/std/packing.d.ts +26 -0
- package/std/packing.js +86 -0
- package/std/subgroup.d.ts +47 -0
- package/std/subgroup.js +220 -0
- package/std/texture.d.ts +108 -0
- package/std/texture.js +197 -0
- package/tgpu.js +44 -0
- package/tgpuBindGroupLayout.d.ts +161 -0
- package/tgpuBindGroupLayout.js +271 -0
- package/tgpuUnstable.d.ts +48 -0
- package/tgpuUnstable.js +66 -0
- package/tgsl/accessIndex.js +45 -0
- package/tgsl/accessProp.js +113 -0
- package/tgsl/consoleLog/deserializers.js +117 -0
- package/tgsl/consoleLog/logGenerator.js +86 -0
- package/tgsl/consoleLog/serializers.js +225 -0
- package/tgsl/consoleLog/types.d.ts +54 -0
- package/tgsl/consoleLog/types.js +12 -0
- package/tgsl/conversion.js +200 -0
- package/tgsl/forOfUtils.js +45 -0
- package/tgsl/generationHelpers.d.ts +37 -0
- package/tgsl/generationHelpers.js +67 -0
- package/tgsl/math.js +45 -0
- package/tgsl/shaderGenerator.d.ts +18 -0
- package/tgsl/shellless.d.ts +11 -0
- package/tgsl/shellless.js +53 -0
- package/tgsl/wgslGenerator.js +585 -0
- package/types.d.ts +255 -0
- package/types.js +43 -0
- package/unwrapper.d.ts +27 -0
- package/wgslExtensions.d.ts +5 -0
- package/wgslExtensions.js +18 -0
- package/builtin-ClEnM-Ye.js +0 -818
- package/builtin-ClEnM-Ye.js.map +0 -1
- package/common/index.d.ts.map +0 -1
- package/common/index.js.map +0 -1
- package/data/index.d.ts.map +0 -1
- package/data/index.js.map +0 -1
- package/deepEqual-yZXvaV2C.js +0 -413
- package/deepEqual-yZXvaV2C.js.map +0 -1
- package/extensions-0SFbU9FH.js +0 -2032
- package/extensions-0SFbU9FH.js.map +0 -1
- package/fullScreenTriangle-MdLGaAMR.js +0 -543
- package/fullScreenTriangle-MdLGaAMR.js.map +0 -1
- package/index.d.ts.map +0 -1
- package/index.js.map +0 -1
- package/indexNamedExports-Cdy7USiY.d.ts +0 -5696
- package/indexNamedExports-Cdy7USiY.d.ts.map +0 -1
- package/operators-HTxa_0k9.js +0 -4156
- package/operators-HTxa_0k9.js.map +0 -1
- package/std/index.d.ts.map +0 -1
- package/std/index.js.map +0 -1
- package/texture-Dg5ybJro.js.map +0 -1
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { $internal, $resolve } from "../shared/symbols.js";
|
|
2
|
+
import { getName, setName } from "../shared/meta.js";
|
|
3
|
+
import { isValidProp } from "../nameRegistry.js";
|
|
4
|
+
import { createIoSchema } from "../core/function/ioSchema.js";
|
|
5
|
+
|
|
6
|
+
//#region src/data/autoStruct.ts
|
|
7
|
+
/**
|
|
8
|
+
* A requirement for the generated struct is that non-builtin properties need to
|
|
9
|
+
* have the same name in WGSL as they do in JS. This allows locations to be properly
|
|
10
|
+
* matched between the Vertex output and Fragment input.
|
|
11
|
+
*/
|
|
12
|
+
var AutoStruct = class {
|
|
13
|
+
/**
|
|
14
|
+
* js key -> data type
|
|
15
|
+
*/
|
|
16
|
+
#validProps;
|
|
17
|
+
/**
|
|
18
|
+
* js key -> { prop: 'wgsl key', type: ... }
|
|
19
|
+
* @example '$position' -> { prop: 'position', type: ... }
|
|
20
|
+
*/
|
|
21
|
+
#allocated;
|
|
22
|
+
#usedWgslKeys;
|
|
23
|
+
#locations;
|
|
24
|
+
#cachedStruct;
|
|
25
|
+
#typeForExtraProps;
|
|
26
|
+
constructor(validProps, typeForExtraProps, locations) {
|
|
27
|
+
this.#validProps = validProps;
|
|
28
|
+
this.#typeForExtraProps = typeForExtraProps;
|
|
29
|
+
this.#allocated = {};
|
|
30
|
+
this.#locations = locations;
|
|
31
|
+
this.#usedWgslKeys = /* @__PURE__ */ new Set();
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Used for accessing builtins, varying and attributes in code.
|
|
35
|
+
*/
|
|
36
|
+
accessProp(key) {
|
|
37
|
+
const dataType = this.#validProps[key] ?? this.#typeForExtraProps;
|
|
38
|
+
if (!dataType) return;
|
|
39
|
+
return this.provideProp(key, dataType);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Used for providing new varyings.
|
|
43
|
+
*
|
|
44
|
+
* @privateRemarks
|
|
45
|
+
* Internally used by `accessProp`.
|
|
46
|
+
*/
|
|
47
|
+
provideProp(key, dataType) {
|
|
48
|
+
let alloc = this.#allocated[key];
|
|
49
|
+
if (!alloc) {
|
|
50
|
+
const wgslKey = key.replaceAll("$", "");
|
|
51
|
+
if (this.#usedWgslKeys.has(wgslKey)) throw new Error(`Property name '${wgslKey}' causes naming clashes. Choose a different name.`);
|
|
52
|
+
if (!isValidProp(wgslKey)) throw new Error(`Property key '${key}' is a reserved WGSL word. Choose a different name.`);
|
|
53
|
+
this.#usedWgslKeys.add(wgslKey);
|
|
54
|
+
alloc = {
|
|
55
|
+
prop: wgslKey,
|
|
56
|
+
type: dataType
|
|
57
|
+
};
|
|
58
|
+
this.#allocated[key] = alloc;
|
|
59
|
+
}
|
|
60
|
+
return alloc;
|
|
61
|
+
}
|
|
62
|
+
get completeStruct() {
|
|
63
|
+
if (!this.#cachedStruct) {
|
|
64
|
+
this.#cachedStruct = createIoSchema(Object.fromEntries(Object.values(this.#allocated).map((alloc) => {
|
|
65
|
+
return [alloc.prop, alloc.type];
|
|
66
|
+
})), this.#locations);
|
|
67
|
+
const ownName = getName(this);
|
|
68
|
+
if (ownName) setName(this.#cachedStruct, ownName);
|
|
69
|
+
}
|
|
70
|
+
return this.#cachedStruct;
|
|
71
|
+
}
|
|
72
|
+
[$resolve](ctx) {
|
|
73
|
+
return ctx.resolve(this.completeStruct);
|
|
74
|
+
}
|
|
75
|
+
toString() {
|
|
76
|
+
return `auto-struct:${getName(this) ?? "<unnamed>"}`;
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
AutoStruct.prototype[$internal] = {};
|
|
80
|
+
AutoStruct.prototype.type = "auto-struct";
|
|
81
|
+
|
|
82
|
+
//#endregion
|
|
83
|
+
export { AutoStruct };
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import { isAtomic, isDecorated, isMat, isMat2x2f, isMat3x3f, isVec, isVecBool, isWgslArray, isWgslStruct } from "./wgslTypes.js";
|
|
2
|
+
import { isDisarray, isUnstruct } from "./dataTypes.js";
|
|
3
|
+
import { formatToWGSLType, isPackedData } from "./vertexFormatData.js";
|
|
4
|
+
import { alignmentOf } from "./alignmentOf.js";
|
|
5
|
+
import { roundUp } from "../mathUtils.js";
|
|
6
|
+
import { sizeOf } from "./sizeOf.js";
|
|
7
|
+
import { offsetsForProps } from "./offsets.js";
|
|
8
|
+
|
|
9
|
+
//#region src/data/compiledIO.ts
|
|
10
|
+
const EVAL_ALLOWED_IN_ENV = (() => {
|
|
11
|
+
try {
|
|
12
|
+
new Function("return true");
|
|
13
|
+
return true;
|
|
14
|
+
} catch {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
})();
|
|
18
|
+
const compiledWriters = /* @__PURE__ */ new WeakMap();
|
|
19
|
+
const typeToPrimitive = {
|
|
20
|
+
u32: "u32",
|
|
21
|
+
vec2u: "u32",
|
|
22
|
+
vec3u: "u32",
|
|
23
|
+
vec4u: "u32",
|
|
24
|
+
u16: "u16",
|
|
25
|
+
i32: "i32",
|
|
26
|
+
vec2i: "i32",
|
|
27
|
+
vec3i: "i32",
|
|
28
|
+
vec4i: "i32",
|
|
29
|
+
f32: "f32",
|
|
30
|
+
vec2f: "f32",
|
|
31
|
+
vec3f: "f32",
|
|
32
|
+
vec4f: "f32",
|
|
33
|
+
f16: "f16",
|
|
34
|
+
vec2h: "f16",
|
|
35
|
+
vec3h: "f16",
|
|
36
|
+
vec4h: "f16",
|
|
37
|
+
mat2x2f: "f32",
|
|
38
|
+
mat3x3f: "f32",
|
|
39
|
+
mat4x4f: "f32"
|
|
40
|
+
};
|
|
41
|
+
const vertexFormatToPrimitive = {
|
|
42
|
+
uint8: "u8",
|
|
43
|
+
uint8x2: "u8",
|
|
44
|
+
uint8x4: "u8",
|
|
45
|
+
sint8: "i8",
|
|
46
|
+
sint8x2: "i8",
|
|
47
|
+
sint8x4: "i8",
|
|
48
|
+
unorm8: "u8",
|
|
49
|
+
unorm8x2: "u8",
|
|
50
|
+
unorm8x4: "u8",
|
|
51
|
+
snorm8: "i8",
|
|
52
|
+
snorm8x2: "i8",
|
|
53
|
+
snorm8x4: "i8",
|
|
54
|
+
uint16: "u16",
|
|
55
|
+
uint16x2: "u16",
|
|
56
|
+
uint16x4: "u16",
|
|
57
|
+
sint16: "i16",
|
|
58
|
+
sint16x2: "i16",
|
|
59
|
+
sint16x4: "i16",
|
|
60
|
+
unorm16: "u16",
|
|
61
|
+
unorm16x2: "u16",
|
|
62
|
+
unorm16x4: "u16",
|
|
63
|
+
snorm16: "i16",
|
|
64
|
+
snorm16x2: "i16",
|
|
65
|
+
snorm16x4: "i16",
|
|
66
|
+
float16: "f16",
|
|
67
|
+
float16x2: "f16",
|
|
68
|
+
float16x4: "f16",
|
|
69
|
+
float32: "f32",
|
|
70
|
+
float32x2: "f32",
|
|
71
|
+
float32x3: "f32",
|
|
72
|
+
float32x4: "f32",
|
|
73
|
+
uint32: "u32",
|
|
74
|
+
uint32x2: "u32",
|
|
75
|
+
uint32x3: "u32",
|
|
76
|
+
uint32x4: "u32",
|
|
77
|
+
sint32: "i32",
|
|
78
|
+
sint32x2: "i32",
|
|
79
|
+
sint32x3: "i32",
|
|
80
|
+
sint32x4: "i32"
|
|
81
|
+
};
|
|
82
|
+
const primitiveToWriteFunction = {
|
|
83
|
+
u32: "setUint32",
|
|
84
|
+
i32: "setInt32",
|
|
85
|
+
f32: "setFloat32",
|
|
86
|
+
u16: "setUint16",
|
|
87
|
+
i16: "setInt16",
|
|
88
|
+
f16: "setFloat16",
|
|
89
|
+
u8: "setUint8",
|
|
90
|
+
i8: "setInt8"
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* @privateRemarks
|
|
94
|
+
* based on the `Channel Formats` table https://www.w3.org/TR/WGSL/#texel-formats
|
|
95
|
+
*/
|
|
96
|
+
const vertexFormatValueTransform = {
|
|
97
|
+
unorm8: (value) => `Math.round(${value} * 255)`,
|
|
98
|
+
unorm8x2: (value) => `Math.round(${value} * 255)`,
|
|
99
|
+
unorm8x4: (value) => `Math.round(${value} * 255)`,
|
|
100
|
+
snorm8: (value) => `Math.round(${value} * 127)`,
|
|
101
|
+
snorm8x2: (value) => `Math.round(${value} * 127)`,
|
|
102
|
+
snorm8x4: (value) => `Math.round(${value} * 127)`,
|
|
103
|
+
unorm16: (value) => `Math.round(${value} * 65535)`,
|
|
104
|
+
unorm16x2: (value) => `Math.round(${value} * 65535)`,
|
|
105
|
+
unorm16x4: (value) => `Math.round(${value} * 65535)`,
|
|
106
|
+
snorm16: (value) => `Math.round(${value} * 32767)`,
|
|
107
|
+
snorm16x2: (value) => `Math.round(${value} * 32767)`,
|
|
108
|
+
snorm16x4: (value) => `Math.round(${value} * 32767)`
|
|
109
|
+
};
|
|
110
|
+
const specialPackedFormats = {
|
|
111
|
+
"unorm10-10-10-2": {
|
|
112
|
+
writeFunction: "setUint32",
|
|
113
|
+
generator: (offsetExpr, valueExpr) => `output.setUint32(${offsetExpr}, ((${valueExpr}.x*1023&0x3FF)<<22)|((${valueExpr}.y*1023&0x3FF)<<12)|((${valueExpr}.z*1023&0x3FF)<<2)|(${valueExpr}.w*3&3), littleEndian);\n`
|
|
114
|
+
},
|
|
115
|
+
"unorm8x4-bgra": {
|
|
116
|
+
writeFunction: "setUint8",
|
|
117
|
+
generator: (offsetExpr, valueExpr) => {
|
|
118
|
+
const bgraComponents = [
|
|
119
|
+
"z",
|
|
120
|
+
"y",
|
|
121
|
+
"x",
|
|
122
|
+
"w"
|
|
123
|
+
];
|
|
124
|
+
let code = "";
|
|
125
|
+
for (let idx = 0; idx < 4; idx++) code += `output.setUint8((${offsetExpr} + ${idx}), Math.round(${valueExpr}.${bgraComponents[idx]} * 255), littleEndian);\n`;
|
|
126
|
+
return code;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
function buildWriter(node, offsetExpr, valueExpr, depth = 0) {
|
|
131
|
+
const loopVar = [
|
|
132
|
+
"i",
|
|
133
|
+
"j",
|
|
134
|
+
"k"
|
|
135
|
+
][depth] || `i${depth}`;
|
|
136
|
+
if (isAtomic(node) || isDecorated(node)) return buildWriter(node.inner, offsetExpr, valueExpr, depth);
|
|
137
|
+
if (isWgslStruct(node) || isUnstruct(node)) {
|
|
138
|
+
const propOffsets = offsetsForProps(node);
|
|
139
|
+
let code = "";
|
|
140
|
+
for (const [key, propOffset] of Object.entries(propOffsets)) {
|
|
141
|
+
const subSchema = node.propTypes[key];
|
|
142
|
+
if (!subSchema) continue;
|
|
143
|
+
code += buildWriter(subSchema, `(${offsetExpr} + ${propOffset.offset})`, `${valueExpr}.${key}`, depth);
|
|
144
|
+
}
|
|
145
|
+
return code;
|
|
146
|
+
}
|
|
147
|
+
if (isWgslArray(node) || isDisarray(node)) {
|
|
148
|
+
const elementSize = roundUp(sizeOf(node.elementType), alignmentOf(node));
|
|
149
|
+
let code = "";
|
|
150
|
+
code += `for (let ${loopVar} = 0; ${loopVar} < ${node.elementCount}; ${loopVar}++) {\n`;
|
|
151
|
+
code += buildWriter(node.elementType, `(${offsetExpr} + ${loopVar} * ${elementSize})`, `${valueExpr}[${loopVar}]`, depth + 1);
|
|
152
|
+
code += "}\n";
|
|
153
|
+
return code;
|
|
154
|
+
}
|
|
155
|
+
if (isVec(node)) {
|
|
156
|
+
if (isVecBool(node)) throw new Error("Compiled writers do not support boolean vectors");
|
|
157
|
+
const primitive = typeToPrimitive[node.type];
|
|
158
|
+
let code = "";
|
|
159
|
+
const writeFunc = primitiveToWriteFunction[primitive];
|
|
160
|
+
const components = [
|
|
161
|
+
"x",
|
|
162
|
+
"y",
|
|
163
|
+
"z",
|
|
164
|
+
"w"
|
|
165
|
+
];
|
|
166
|
+
for (let i = 0; i < node.componentCount; i++) code += `output.${writeFunc}((${offsetExpr} + ${i * 4}), ${valueExpr}.${components[i]}, littleEndian);\n`;
|
|
167
|
+
return code;
|
|
168
|
+
}
|
|
169
|
+
if (isMat(node)) {
|
|
170
|
+
const writeFunc = primitiveToWriteFunction[typeToPrimitive[node.type]];
|
|
171
|
+
const matSize = isMat2x2f(node) ? 2 : isMat3x3f(node) ? 3 : 4;
|
|
172
|
+
const elementCount = matSize * matSize;
|
|
173
|
+
const rowStride = roundUp(matSize * 4, 8);
|
|
174
|
+
let code = "";
|
|
175
|
+
for (let idx = 0; idx < elementCount; idx++) {
|
|
176
|
+
const colIndex = Math.floor(idx / matSize);
|
|
177
|
+
const rowIndex = idx % matSize;
|
|
178
|
+
const byteOffset = colIndex * rowStride + rowIndex * 4;
|
|
179
|
+
code += `output.${writeFunc}((${offsetExpr} + ${byteOffset}), ${valueExpr}.columns[${colIndex}].${[
|
|
180
|
+
"x",
|
|
181
|
+
"y",
|
|
182
|
+
"z",
|
|
183
|
+
"w"
|
|
184
|
+
][rowIndex]}, littleEndian);\n`;
|
|
185
|
+
}
|
|
186
|
+
return code;
|
|
187
|
+
}
|
|
188
|
+
if (isPackedData(node)) {
|
|
189
|
+
const formatName = node.type;
|
|
190
|
+
if (formatName in specialPackedFormats) return specialPackedFormats[formatName].generator(offsetExpr, valueExpr);
|
|
191
|
+
const primitive = vertexFormatToPrimitive[formatName];
|
|
192
|
+
const writeFunc = primitiveToWriteFunction[primitive];
|
|
193
|
+
const wgslType = formatToWGSLType[formatName];
|
|
194
|
+
const componentCount = isVec(wgslType) ? wgslType.componentCount : 1;
|
|
195
|
+
const componentSize = primitive === "u8" || primitive === "i8" ? 1 : primitive === "u16" || primitive === "i16" || primitive === "f16" ? 2 : 4;
|
|
196
|
+
const components = [
|
|
197
|
+
"x",
|
|
198
|
+
"y",
|
|
199
|
+
"z",
|
|
200
|
+
"w"
|
|
201
|
+
];
|
|
202
|
+
const transform = vertexFormatValueTransform[formatName];
|
|
203
|
+
let code = "";
|
|
204
|
+
for (let idx = 0; idx < componentCount; idx++) {
|
|
205
|
+
const accessor = componentCount === 1 ? valueExpr : `${valueExpr}.${components[idx]}`;
|
|
206
|
+
const value = transform ? transform(accessor) : accessor;
|
|
207
|
+
code += `output.${writeFunc}((${offsetExpr} + ${idx * componentSize}), ${value}, littleEndian);\n`;
|
|
208
|
+
}
|
|
209
|
+
return code;
|
|
210
|
+
}
|
|
211
|
+
if (!Object.hasOwn(typeToPrimitive, node.type)) throw new Error(`Primitive ${node.type} is unsupported by compiled writer`);
|
|
212
|
+
return `output.${primitiveToWriteFunction[typeToPrimitive[node.type]]}(${offsetExpr}, ${valueExpr}, littleEndian);\n`;
|
|
213
|
+
}
|
|
214
|
+
function getCompiledWriterForSchema(schema) {
|
|
215
|
+
if (!EVAL_ALLOWED_IN_ENV) {
|
|
216
|
+
console.warn("This environment does not allow eval - using default writer as fallback");
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
if (compiledWriters.has(schema)) return compiledWriters.get(schema);
|
|
220
|
+
try {
|
|
221
|
+
const body = buildWriter(schema, "offset", "value", 0);
|
|
222
|
+
const fn = new Function("output", "offset", "value", "littleEndian=true", body);
|
|
223
|
+
compiledWriters.set(schema, fn);
|
|
224
|
+
return fn;
|
|
225
|
+
} catch (error) {
|
|
226
|
+
console.warn(`Failed to compile writer for schema: ${schema}\nReason: ${error instanceof Error ? error.message : String(error)}\nFalling back to default writer`);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
//#endregion
|
|
231
|
+
export { getCompiledWriterForSchema };
|