typegpu 0.10.1 → 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-BagDrrks.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-DdtWpk2t.js +0 -818
- package/builtin-DdtWpk2t.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-DQxK4vdp.js +0 -413
- package/deepEqual-DQxK4vdp.js.map +0 -1
- package/extensions-DIVuAfBM.js +0 -2032
- package/extensions-DIVuAfBM.js.map +0 -1
- package/fullScreenTriangle-CfFyQd_0.js +0 -543
- package/fullScreenTriangle-CfFyQd_0.js.map +0 -1
- package/index.d.ts.map +0 -1
- package/index.js.map +0 -1
- package/indexNamedExports-oL6tyaJ9.d.ts +0 -5697
- package/indexNamedExports-oL6tyaJ9.d.ts.map +0 -1
- package/operators-d-PMVTo7.js +0 -4158
- package/operators-d-PMVTo7.js.map +0 -1
- package/std/index.d.ts.map +0 -1
- package/std/index.js.map +0 -1
- package/texture-BagDrrks.js.map +0 -1
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
import { $gpuValueOf, $internal } from "./shared/symbols.js";
|
|
2
|
+
import { getName, setName } from "./shared/meta.js";
|
|
3
|
+
import { NotUniformError } from "./errors.js";
|
|
4
|
+
import { safeStringify } from "./shared/stringify.js";
|
|
5
|
+
import { f32, i32, u32 } from "./data/numeric.js";
|
|
6
|
+
import { comparisonSampler, sampler } from "./data/sampler.js";
|
|
7
|
+
import { textureDescriptorToSchema } from "./data/texture.js";
|
|
8
|
+
import { NotStorageError, isUsableAsStorage } from "./extension.js";
|
|
9
|
+
import { TgpuLaidOutBufferImpl, isUsableAsUniform } from "./core/buffer/bufferUsage.js";
|
|
10
|
+
import { isBuffer } from "./core/buffer/buffer.js";
|
|
11
|
+
import { TgpuLaidOutSamplerImpl, isComparisonSampler, isSampler } from "./core/sampler/sampler.js";
|
|
12
|
+
import { TgpuExternalTextureImpl } from "./core/texture/externalTexture.js";
|
|
13
|
+
import { TgpuLaidOutTextureViewImpl, isTexture, isTextureView } from "./core/texture/texture.js";
|
|
14
|
+
import { NotSampledError, isUsableAsSampled } from "./core/texture/usageExtension.js";
|
|
15
|
+
|
|
16
|
+
//#region src/tgpuBindGroupLayout.ts
|
|
17
|
+
/**
|
|
18
|
+
* Converts legacy entries to new API format
|
|
19
|
+
*/
|
|
20
|
+
function convertLegacyEntries(entries) {
|
|
21
|
+
const result = {};
|
|
22
|
+
for (const [key, entry] of Object.entries(entries)) {
|
|
23
|
+
if (entry === null) {
|
|
24
|
+
result[key] = null;
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
if ("texture" in entry && typeof entry.texture === "string") {
|
|
28
|
+
const sampleType = entry.texture;
|
|
29
|
+
result[key] = {
|
|
30
|
+
...entry,
|
|
31
|
+
texture: textureDescriptorToSchema({
|
|
32
|
+
dimension: entry.viewDimension ?? "2d",
|
|
33
|
+
sampleType: sampleType === "sint" ? i32 : sampleType === "uint" ? u32 : f32,
|
|
34
|
+
multisampled: entry.multisampled ?? false
|
|
35
|
+
})
|
|
36
|
+
};
|
|
37
|
+
} else if ("storageTexture" in entry && typeof entry.storageTexture === "string") {
|
|
38
|
+
const accessMap = {
|
|
39
|
+
readonly: "read-only",
|
|
40
|
+
writeonly: "write-only",
|
|
41
|
+
mutable: "read-write"
|
|
42
|
+
};
|
|
43
|
+
result[key] = {
|
|
44
|
+
...entry,
|
|
45
|
+
storageTexture: textureDescriptorToSchema({
|
|
46
|
+
access: accessMap[entry.access ?? "writeonly"],
|
|
47
|
+
format: entry.storageTexture,
|
|
48
|
+
dimension: entry.viewDimension ?? "2d"
|
|
49
|
+
})
|
|
50
|
+
};
|
|
51
|
+
} else if ("externalTexture" in entry && Object.keys(entry.externalTexture).length === 0) result[key] = {
|
|
52
|
+
...entry,
|
|
53
|
+
externalTexture: {
|
|
54
|
+
type: "texture_external",
|
|
55
|
+
dimension: "2d"
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
else result[key] = entry;
|
|
59
|
+
}
|
|
60
|
+
return result;
|
|
61
|
+
}
|
|
62
|
+
function bindGroupLayout(entries) {
|
|
63
|
+
return new TgpuBindGroupLayoutImpl(convertLegacyEntries(entries));
|
|
64
|
+
}
|
|
65
|
+
function isBindGroupLayout(value) {
|
|
66
|
+
return !!value && value.resourceType === "bind-group-layout";
|
|
67
|
+
}
|
|
68
|
+
function isBindGroup(value) {
|
|
69
|
+
return !!value && value.resourceType === "bind-group";
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* @category Errors
|
|
73
|
+
*/
|
|
74
|
+
var MissingBindingError = class MissingBindingError extends Error {
|
|
75
|
+
constructor(groupLabel, key) {
|
|
76
|
+
super(`Bind group '${groupLabel ?? "<unnamed>"}' is missing a required binding '${key}'`);
|
|
77
|
+
Object.setPrototypeOf(this, MissingBindingError.prototype);
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
const DEFAULT_MUTABLE_VISIBILITY = ["compute", "fragment"];
|
|
81
|
+
const DEFAULT_READONLY_VISIBILITY = [
|
|
82
|
+
"compute",
|
|
83
|
+
"vertex",
|
|
84
|
+
"fragment"
|
|
85
|
+
];
|
|
86
|
+
var TgpuBindGroupLayoutImpl = class {
|
|
87
|
+
[$internal];
|
|
88
|
+
_index;
|
|
89
|
+
resourceType = "bind-group-layout";
|
|
90
|
+
value = {};
|
|
91
|
+
$ = this.value;
|
|
92
|
+
get [$gpuValueOf]() {
|
|
93
|
+
return this.$;
|
|
94
|
+
}
|
|
95
|
+
constructor(entries) {
|
|
96
|
+
this.entries = entries;
|
|
97
|
+
let idx = 0;
|
|
98
|
+
const bound = {};
|
|
99
|
+
this[$internal] = { bound };
|
|
100
|
+
for (const [key, entry] of Object.entries(entries)) {
|
|
101
|
+
if (entry === null) {
|
|
102
|
+
idx++;
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
const membership = {
|
|
106
|
+
layout: this,
|
|
107
|
+
key,
|
|
108
|
+
idx
|
|
109
|
+
};
|
|
110
|
+
if ("uniform" in entry) bound[key] = new TgpuLaidOutBufferImpl("uniform", entry.uniform, membership);
|
|
111
|
+
if ("storage" in entry) {
|
|
112
|
+
const dataType = "type" in entry.storage ? entry.storage : entry.storage(0);
|
|
113
|
+
bound[key] = new TgpuLaidOutBufferImpl(entry.access ?? "readonly", dataType, membership);
|
|
114
|
+
}
|
|
115
|
+
if ("texture" in entry) bound[key] = new TgpuLaidOutTextureViewImpl(entry.texture, membership);
|
|
116
|
+
if ("storageTexture" in entry) bound[key] = new TgpuLaidOutTextureViewImpl(entry.storageTexture, membership);
|
|
117
|
+
if ("externalTexture" in entry) bound[key] = new TgpuExternalTextureImpl(entry.externalTexture, membership);
|
|
118
|
+
if ("sampler" in entry) bound[key] = new TgpuLaidOutSamplerImpl(entry.sampler === "comparison" ? comparisonSampler() : sampler(), membership);
|
|
119
|
+
Object.defineProperty(this.value, key, { get: () => {
|
|
120
|
+
return bound[key].value;
|
|
121
|
+
} });
|
|
122
|
+
idx++;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
toString() {
|
|
126
|
+
return `bindGroupLayout:${getName(this) ?? "<unnamed>"}`;
|
|
127
|
+
}
|
|
128
|
+
get index() {
|
|
129
|
+
return this._index;
|
|
130
|
+
}
|
|
131
|
+
$name(label) {
|
|
132
|
+
setName(this, label);
|
|
133
|
+
return this;
|
|
134
|
+
}
|
|
135
|
+
get bound() {
|
|
136
|
+
return this[$internal].bound;
|
|
137
|
+
}
|
|
138
|
+
$idx(index) {
|
|
139
|
+
this._index = index;
|
|
140
|
+
return this;
|
|
141
|
+
}
|
|
142
|
+
unwrap(unwrapper) {
|
|
143
|
+
return unwrapper.device.createBindGroupLayout({
|
|
144
|
+
label: getName(this) ?? "<unnamed>",
|
|
145
|
+
entries: Object.values(this.entries).map((entry, idx) => {
|
|
146
|
+
if (entry === null) return null;
|
|
147
|
+
let visibility = entry.visibility;
|
|
148
|
+
const binding = {
|
|
149
|
+
binding: idx,
|
|
150
|
+
visibility: 0
|
|
151
|
+
};
|
|
152
|
+
if ("uniform" in entry) {
|
|
153
|
+
visibility = visibility ?? DEFAULT_READONLY_VISIBILITY;
|
|
154
|
+
binding.buffer = { type: "uniform" };
|
|
155
|
+
} else if ("storage" in entry) {
|
|
156
|
+
visibility = visibility ?? (entry.access === "mutable" ? DEFAULT_MUTABLE_VISIBILITY : DEFAULT_READONLY_VISIBILITY);
|
|
157
|
+
binding.buffer = { type: entry.access === "mutable" ? "storage" : "read-only-storage" };
|
|
158
|
+
} else if ("sampler" in entry) {
|
|
159
|
+
visibility = visibility ?? DEFAULT_READONLY_VISIBILITY;
|
|
160
|
+
binding.sampler = { type: entry.sampler };
|
|
161
|
+
} else if ("texture" in entry) {
|
|
162
|
+
visibility = visibility ?? DEFAULT_READONLY_VISIBILITY;
|
|
163
|
+
const { multisampled, dimension, bindingSampleType } = entry.texture;
|
|
164
|
+
binding.texture = {
|
|
165
|
+
sampleType: entry.sampleType ?? bindingSampleType[0],
|
|
166
|
+
viewDimension: dimension,
|
|
167
|
+
multisampled
|
|
168
|
+
};
|
|
169
|
+
} else if ("storageTexture" in entry) {
|
|
170
|
+
visibility = visibility ?? DEFAULT_MUTABLE_VISIBILITY;
|
|
171
|
+
const { dimension, access, format } = entry.storageTexture;
|
|
172
|
+
binding.storageTexture = {
|
|
173
|
+
access,
|
|
174
|
+
format,
|
|
175
|
+
viewDimension: dimension
|
|
176
|
+
};
|
|
177
|
+
} else if ("externalTexture" in entry) {
|
|
178
|
+
visibility = visibility ?? DEFAULT_READONLY_VISIBILITY;
|
|
179
|
+
binding.externalTexture = {};
|
|
180
|
+
}
|
|
181
|
+
if (visibility?.includes("compute")) binding.visibility |= GPUShaderStage.COMPUTE;
|
|
182
|
+
if (visibility?.includes("vertex")) binding.visibility |= GPUShaderStage.VERTEX;
|
|
183
|
+
if (visibility?.includes("fragment")) binding.visibility |= GPUShaderStage.FRAGMENT;
|
|
184
|
+
return binding;
|
|
185
|
+
}).filter((v) => v !== null)
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
var TgpuBindGroupImpl = class {
|
|
190
|
+
resourceType = "bind-group";
|
|
191
|
+
constructor(layout, entries) {
|
|
192
|
+
this.layout = layout;
|
|
193
|
+
this.entries = entries;
|
|
194
|
+
for (const key of Object.keys(layout.entries)) if (layout.entries[key] !== null && !(key in entries)) throw new MissingBindingError(getName(layout), key);
|
|
195
|
+
}
|
|
196
|
+
unwrap(unwrapper) {
|
|
197
|
+
return unwrapper.device.createBindGroup({
|
|
198
|
+
label: getName(this.layout) ?? "<unnamed>",
|
|
199
|
+
layout: unwrapper.unwrap(this.layout),
|
|
200
|
+
entries: Object.entries(this.layout.entries).map(([key, entry], idx) => {
|
|
201
|
+
if (entry === null) return null;
|
|
202
|
+
const value = this.entries[key];
|
|
203
|
+
if (value === void 0) throw new Error(`'${key}' is a resource required to populate bind group layout '${getName(this.layout) ?? "<unnamed>"}'.`);
|
|
204
|
+
if ("uniform" in entry) {
|
|
205
|
+
let resource;
|
|
206
|
+
if (isBuffer(value)) {
|
|
207
|
+
if (!isUsableAsUniform(value)) throw new NotUniformError(value);
|
|
208
|
+
resource = { buffer: unwrapper.unwrap(value) };
|
|
209
|
+
} else resource = { buffer: value };
|
|
210
|
+
return {
|
|
211
|
+
binding: idx,
|
|
212
|
+
resource
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
if ("storage" in entry) {
|
|
216
|
+
let resource;
|
|
217
|
+
if (isBuffer(value)) {
|
|
218
|
+
if (!isUsableAsStorage(value)) throw new NotStorageError(value);
|
|
219
|
+
resource = { buffer: unwrapper.unwrap(value) };
|
|
220
|
+
} else resource = { buffer: value };
|
|
221
|
+
return {
|
|
222
|
+
binding: idx,
|
|
223
|
+
resource
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
if ("texture" in entry) {
|
|
227
|
+
let resource;
|
|
228
|
+
if (isTexture(value)) {
|
|
229
|
+
if (!isUsableAsSampled(value)) throw new NotSampledError(value);
|
|
230
|
+
resource = unwrapper.unwrap(value.createView(entry.texture));
|
|
231
|
+
} else if (isTextureView(value)) resource = unwrapper.unwrap(value);
|
|
232
|
+
else resource = value;
|
|
233
|
+
return {
|
|
234
|
+
binding: idx,
|
|
235
|
+
resource
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
if ("storageTexture" in entry) {
|
|
239
|
+
let resource;
|
|
240
|
+
if (isTexture(value)) {
|
|
241
|
+
if (!isUsableAsStorage(value)) throw new NotStorageError(value);
|
|
242
|
+
resource = unwrapper.unwrap(value.createView(entry.storageTexture));
|
|
243
|
+
} else if (isTextureView(value)) resource = unwrapper.unwrap(value);
|
|
244
|
+
else resource = value;
|
|
245
|
+
return {
|
|
246
|
+
binding: idx,
|
|
247
|
+
resource
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
if ("sampler" in entry) {
|
|
251
|
+
if (isComparisonSampler(value) || isSampler(value)) return {
|
|
252
|
+
binding: idx,
|
|
253
|
+
resource: unwrapper.unwrap(value)
|
|
254
|
+
};
|
|
255
|
+
return {
|
|
256
|
+
binding: idx,
|
|
257
|
+
resource: value
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
if ("externalTexture" in entry) return {
|
|
261
|
+
binding: idx,
|
|
262
|
+
resource: value
|
|
263
|
+
};
|
|
264
|
+
throw new Error(`Malformed bind group entry: ${safeStringify(value)}`);
|
|
265
|
+
}).filter((v) => v !== null)
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
//#endregion
|
|
271
|
+
export { TgpuBindGroupImpl, bindGroupLayout, isBindGroup, isBindGroupLayout };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { computeFn } from "./core/function/tgpuComputeFn.js";
|
|
2
|
+
import { privateVar, workgroupVar } from "./core/variable/tgpuVariable.js";
|
|
3
|
+
import { fragmentFn } from "./core/function/tgpuFragmentFn.js";
|
|
4
|
+
import { vertexFn } from "./core/function/tgpuVertexFn.js";
|
|
5
|
+
import { vertexLayout } from "./core/vertexLayout/vertexLayout.js";
|
|
6
|
+
import { declare } from "./core/declare/tgpuDeclare.js";
|
|
7
|
+
import { fn } from "./core/function/tgpuFn.js";
|
|
8
|
+
import { constant } from "./core/constant/tgpuConstant.js";
|
|
9
|
+
import { comptime } from "./core/function/comptime.js";
|
|
10
|
+
import { namespace } from "./core/resolve/namespace.js";
|
|
11
|
+
import { slot } from "./core/slot/slot.js";
|
|
12
|
+
import { lazy } from "./core/slot/lazy.js";
|
|
13
|
+
import { accessor, mutableAccessor } from "./core/slot/accessor.js";
|
|
14
|
+
import { rawCodeSnippet } from "./core/rawCodeSnippet/tgpuRawCodeSnippet.js";
|
|
15
|
+
import { simulate } from "./core/simulate/tgpuSimulate.js";
|
|
16
|
+
|
|
17
|
+
//#region src/tgpuUnstable.d.ts
|
|
18
|
+
declare namespace tgpuUnstable_d_exports {
|
|
19
|
+
export { _accessor as accessor, _comptime as comptime, _computeFn as computeFn, _constant as const, declare, _lazy as derived, _fn as fn, _fragmentFn as fragmentFn, _mutableAccessor as mutableAccessor, namespace, _privateVar as privateVar, rawCodeSnippet, simulate, _slot as slot, _vertexFn as vertexFn, _vertexLayout as vertexLayout, _workgroupVar as workgroupVar };
|
|
20
|
+
}
|
|
21
|
+
/** @deprecated This feature is now stable, use tgpu.const. */
|
|
22
|
+
declare const _constant: typeof constant;
|
|
23
|
+
/** @deprecated This feature is now stable, use tgpu.comptime. */
|
|
24
|
+
declare const _comptime: typeof comptime;
|
|
25
|
+
/** @deprecated This feature is now stable, use tgpu.computeFn. */
|
|
26
|
+
declare const _computeFn: typeof computeFn;
|
|
27
|
+
/** @deprecated This feature is now stable, use tgpu.fn. */
|
|
28
|
+
declare const _fn: typeof fn;
|
|
29
|
+
/** @deprecated This feature is now stable, use tgpu.fragmentFn. */
|
|
30
|
+
declare const _fragmentFn: typeof fragmentFn;
|
|
31
|
+
/** @deprecated This feature is now stable, use tgpu.vertexFn. */
|
|
32
|
+
declare const _vertexFn: typeof vertexFn;
|
|
33
|
+
/** @deprecated This feature is now stable, use tgpu.accessor. */
|
|
34
|
+
declare const _accessor: typeof accessor;
|
|
35
|
+
/** @deprecated This feature is now stable, use tgpu.mutableAccessor. */
|
|
36
|
+
declare const _mutableAccessor: typeof mutableAccessor;
|
|
37
|
+
/** @deprecated This feature is now stable, use tgpu.lazy. */
|
|
38
|
+
declare const _lazy: typeof lazy;
|
|
39
|
+
/** @deprecated This feature is now stable, use tgpu.slot. */
|
|
40
|
+
declare const _slot: typeof slot;
|
|
41
|
+
/** @deprecated This feature is now stable, use tgpu.privateVar. */
|
|
42
|
+
declare const _privateVar: typeof privateVar;
|
|
43
|
+
/** @deprecated This feature is now stable, use tgpu.workgroupVar. */
|
|
44
|
+
declare const _workgroupVar: typeof workgroupVar;
|
|
45
|
+
/** @deprecated This feature is now stable, use tgpu.vertexLayout. */
|
|
46
|
+
declare const _vertexLayout: typeof vertexLayout;
|
|
47
|
+
//#endregion
|
|
48
|
+
export { tgpuUnstable_d_exports };
|
package/tgpuUnstable.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { __exportAll } from "./_virtual/_rolldown/runtime.js";
|
|
2
|
+
import { comptime } from "./core/function/comptime.js";
|
|
3
|
+
import { constant } from "./core/constant/tgpuConstant.js";
|
|
4
|
+
import { fn } from "./core/function/tgpuFn.js";
|
|
5
|
+
import { namespace } from "./core/resolve/namespace.js";
|
|
6
|
+
import { slot } from "./core/slot/slot.js";
|
|
7
|
+
import { privateVar, workgroupVar } from "./core/variable/tgpuVariable.js";
|
|
8
|
+
import { computeFn } from "./core/function/tgpuComputeFn.js";
|
|
9
|
+
import { vertexLayout } from "./core/vertexLayout/vertexLayout.js";
|
|
10
|
+
import { lazy } from "./core/slot/lazy.js";
|
|
11
|
+
import { accessor, mutableAccessor } from "./core/slot/accessor.js";
|
|
12
|
+
import { fragmentFn } from "./core/function/tgpuFragmentFn.js";
|
|
13
|
+
import { vertexFn } from "./core/function/tgpuVertexFn.js";
|
|
14
|
+
import { declare } from "./core/declare/tgpuDeclare.js";
|
|
15
|
+
import { rawCodeSnippet } from "./core/rawCodeSnippet/tgpuRawCodeSnippet.js";
|
|
16
|
+
import { simulate } from "./core/simulate/tgpuSimulate.js";
|
|
17
|
+
|
|
18
|
+
//#region src/tgpuUnstable.ts
|
|
19
|
+
var tgpuUnstable_exports = /* @__PURE__ */ __exportAll({
|
|
20
|
+
accessor: () => _accessor,
|
|
21
|
+
comptime: () => _comptime,
|
|
22
|
+
computeFn: () => _computeFn,
|
|
23
|
+
const: () => _constant,
|
|
24
|
+
declare: () => declare,
|
|
25
|
+
derived: () => _lazy,
|
|
26
|
+
fn: () => _fn,
|
|
27
|
+
fragmentFn: () => _fragmentFn,
|
|
28
|
+
mutableAccessor: () => _mutableAccessor,
|
|
29
|
+
namespace: () => namespace,
|
|
30
|
+
privateVar: () => _privateVar,
|
|
31
|
+
rawCodeSnippet: () => rawCodeSnippet,
|
|
32
|
+
simulate: () => simulate,
|
|
33
|
+
slot: () => _slot,
|
|
34
|
+
vertexFn: () => _vertexFn,
|
|
35
|
+
vertexLayout: () => _vertexLayout,
|
|
36
|
+
workgroupVar: () => _workgroupVar
|
|
37
|
+
});
|
|
38
|
+
/** @deprecated This feature is now stable, use tgpu.const. */
|
|
39
|
+
const _constant = constant;
|
|
40
|
+
/** @deprecated This feature is now stable, use tgpu.comptime. */
|
|
41
|
+
const _comptime = comptime;
|
|
42
|
+
/** @deprecated This feature is now stable, use tgpu.computeFn. */
|
|
43
|
+
const _computeFn = computeFn;
|
|
44
|
+
/** @deprecated This feature is now stable, use tgpu.fn. */
|
|
45
|
+
const _fn = fn;
|
|
46
|
+
/** @deprecated This feature is now stable, use tgpu.fragmentFn. */
|
|
47
|
+
const _fragmentFn = fragmentFn;
|
|
48
|
+
/** @deprecated This feature is now stable, use tgpu.vertexFn. */
|
|
49
|
+
const _vertexFn = vertexFn;
|
|
50
|
+
/** @deprecated This feature is now stable, use tgpu.accessor. */
|
|
51
|
+
const _accessor = accessor;
|
|
52
|
+
/** @deprecated This feature is now stable, use tgpu.mutableAccessor. */
|
|
53
|
+
const _mutableAccessor = mutableAccessor;
|
|
54
|
+
/** @deprecated This feature is now stable, use tgpu.lazy. */
|
|
55
|
+
const _lazy = lazy;
|
|
56
|
+
/** @deprecated This feature is now stable, use tgpu.slot. */
|
|
57
|
+
const _slot = slot;
|
|
58
|
+
/** @deprecated This feature is now stable, use tgpu.privateVar. */
|
|
59
|
+
const _privateVar = privateVar;
|
|
60
|
+
/** @deprecated This feature is now stable, use tgpu.workgroupVar. */
|
|
61
|
+
const _workgroupVar = workgroupVar;
|
|
62
|
+
/** @deprecated This feature is now stable, use tgpu.vertexLayout. */
|
|
63
|
+
const _vertexLayout = vertexLayout;
|
|
64
|
+
|
|
65
|
+
//#endregion
|
|
66
|
+
export { tgpuUnstable_exports };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { isNaturallyEphemeral, isPtr, isVec, isWgslArray, isWgslStruct } from "../data/wgslTypes.js";
|
|
2
|
+
import { MatrixColumnsAccess, UnknownData, isDisarray } from "../data/dataTypes.js";
|
|
3
|
+
import { isEphemeralSnippet, snip } from "../data/snippet.js";
|
|
4
|
+
import { isKnownAtComptime } from "../types.js";
|
|
5
|
+
import { stitch } from "../core/resolve/stitch.js";
|
|
6
|
+
import { derefSnippet } from "../data/ref.js";
|
|
7
|
+
import { vec2f, vec3f, vec4f } from "../data/vector.js";
|
|
8
|
+
import { coerceToSnippet } from "./generationHelpers.js";
|
|
9
|
+
import { accessProp } from "./accessProp.js";
|
|
10
|
+
|
|
11
|
+
//#region src/tgsl/accessIndex.ts
|
|
12
|
+
const indexableTypeToResult = {
|
|
13
|
+
mat2x2f: vec2f,
|
|
14
|
+
mat3x3f: vec3f,
|
|
15
|
+
mat4x4f: vec4f
|
|
16
|
+
};
|
|
17
|
+
function accessIndex(target, indexArg) {
|
|
18
|
+
const index = typeof indexArg === "number" ? coerceToSnippet(indexArg) : indexArg;
|
|
19
|
+
if (isWgslArray(target.dataType) || isDisarray(target.dataType)) {
|
|
20
|
+
const elementType = target.dataType.elementType;
|
|
21
|
+
const isElementNatEph = isNaturallyEphemeral(elementType);
|
|
22
|
+
const isTargetEphemeral = isEphemeralSnippet(target);
|
|
23
|
+
const isIndexConstant = index.origin === "constant";
|
|
24
|
+
let origin;
|
|
25
|
+
if (target.origin === "constant-tgpu-const-ref") if (isIndexConstant) origin = isElementNatEph ? "constant" : "constant-tgpu-const-ref";
|
|
26
|
+
else origin = isElementNatEph ? "runtime" : "runtime-tgpu-const-ref";
|
|
27
|
+
else if (target.origin === "runtime-tgpu-const-ref") origin = isElementNatEph ? "runtime" : "runtime-tgpu-const-ref";
|
|
28
|
+
else if (!isTargetEphemeral && !isElementNatEph) origin = target.origin;
|
|
29
|
+
else if (isIndexConstant && target.origin === "constant") origin = "constant";
|
|
30
|
+
else origin = "runtime";
|
|
31
|
+
return snip(isKnownAtComptime(target) && isKnownAtComptime(index) ? target.value[index.value] : stitch`${target}[${index}]`, elementType, origin);
|
|
32
|
+
}
|
|
33
|
+
if (isVec(target.dataType)) return snip(isKnownAtComptime(target) && isKnownAtComptime(index) ? target.value[index.value] : stitch`${target}[${index}]`, target.dataType.primitive, target.origin === "constant" || target.origin === "constant-tgpu-const-ref" ? "constant" : "runtime");
|
|
34
|
+
if (isPtr(target.dataType)) return accessIndex(derefSnippet(target), index);
|
|
35
|
+
if (target.value instanceof MatrixColumnsAccess) {
|
|
36
|
+
const propType = indexableTypeToResult[target.value.matrix.dataType.type];
|
|
37
|
+
return snip(stitch`${target.value.matrix}[${index}]`, propType, target.origin);
|
|
38
|
+
}
|
|
39
|
+
if (target.dataType.type in indexableTypeToResult) throw new Error("The only way of accessing matrix elements in TypeGPU functions is through the 'columns' property.");
|
|
40
|
+
if (isKnownAtComptime(target) && isKnownAtComptime(index) || target.dataType === UnknownData) return coerceToSnippet(target.value[index.value]);
|
|
41
|
+
if (isWgslStruct(target.dataType) && isKnownAtComptime(index) && typeof index.value === "string") return accessProp(target, index.value);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
//#endregion
|
|
45
|
+
export { accessIndex };
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { $gpuCallable } from "../shared/symbols.js";
|
|
2
|
+
import { isMat, isNaturallyEphemeral, isPtr, isVec, isWgslArray, isWgslStruct } from "../data/wgslTypes.js";
|
|
3
|
+
import { InfixDispatch, MatrixColumnsAccess, UnknownData, isUnstruct, undecorate } from "../data/dataTypes.js";
|
|
4
|
+
import { isEphemeralSnippet, snip } from "../data/snippet.js";
|
|
5
|
+
import { isKnownAtComptime } from "../types.js";
|
|
6
|
+
import { stitch } from "../core/resolve/stitch.js";
|
|
7
|
+
import { derefSnippet } from "../data/ref.js";
|
|
8
|
+
import { abstractInt, bool, f16, f32, i32, u32 } from "../data/numeric.js";
|
|
9
|
+
import { vec2b, vec2f, vec2h, vec2i, vec2u, vec3b, vec3f, vec3h, vec3i, vec3u, vec4b, vec4f, vec4h, vec4i, vec4u } from "../data/vector.js";
|
|
10
|
+
import { AutoStruct } from "../data/autoStruct.js";
|
|
11
|
+
import { coerceToSnippet } from "./generationHelpers.js";
|
|
12
|
+
import { add, div, mod, mul, sub } from "../std/operators.js";
|
|
13
|
+
|
|
14
|
+
//#region src/tgsl/accessProp.ts
|
|
15
|
+
const infixKinds = [
|
|
16
|
+
"vec2f",
|
|
17
|
+
"vec3f",
|
|
18
|
+
"vec4f",
|
|
19
|
+
"vec2h",
|
|
20
|
+
"vec3h",
|
|
21
|
+
"vec4h",
|
|
22
|
+
"vec2i",
|
|
23
|
+
"vec3i",
|
|
24
|
+
"vec4i",
|
|
25
|
+
"vec2u",
|
|
26
|
+
"vec3u",
|
|
27
|
+
"vec4u",
|
|
28
|
+
"mat2x2f",
|
|
29
|
+
"mat3x3f",
|
|
30
|
+
"mat4x4f"
|
|
31
|
+
];
|
|
32
|
+
const infixOperators = {
|
|
33
|
+
add,
|
|
34
|
+
sub,
|
|
35
|
+
mul,
|
|
36
|
+
div,
|
|
37
|
+
mod
|
|
38
|
+
};
|
|
39
|
+
const swizzleLenToType = {
|
|
40
|
+
f: {
|
|
41
|
+
1: f32,
|
|
42
|
+
2: vec2f,
|
|
43
|
+
3: vec3f,
|
|
44
|
+
4: vec4f
|
|
45
|
+
},
|
|
46
|
+
h: {
|
|
47
|
+
1: f16,
|
|
48
|
+
2: vec2h,
|
|
49
|
+
3: vec3h,
|
|
50
|
+
4: vec4h
|
|
51
|
+
},
|
|
52
|
+
i: {
|
|
53
|
+
1: i32,
|
|
54
|
+
2: vec2i,
|
|
55
|
+
3: vec3i,
|
|
56
|
+
4: vec4i
|
|
57
|
+
},
|
|
58
|
+
u: {
|
|
59
|
+
1: u32,
|
|
60
|
+
2: vec2u,
|
|
61
|
+
3: vec3u,
|
|
62
|
+
4: vec4u
|
|
63
|
+
},
|
|
64
|
+
b: {
|
|
65
|
+
1: bool,
|
|
66
|
+
2: vec2b,
|
|
67
|
+
3: vec3b,
|
|
68
|
+
4: vec4b
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
function accessProp(target, propName) {
|
|
72
|
+
if (infixKinds.includes(target.dataType.type) && propName in infixOperators) {
|
|
73
|
+
const operator = infixOperators[propName];
|
|
74
|
+
return snip(new InfixDispatch(propName, target, operator[$gpuCallable].call.bind(operator)), UnknownData, target.origin);
|
|
75
|
+
}
|
|
76
|
+
if (isWgslArray(target.dataType) && propName === "length") {
|
|
77
|
+
if (target.dataType.elementCount === 0) return snip(stitch`arrayLength(&${target})`, u32, "runtime");
|
|
78
|
+
return snip(target.dataType.elementCount, abstractInt, "constant");
|
|
79
|
+
}
|
|
80
|
+
if (isMat(target.dataType) && propName === "columns") return snip(new MatrixColumnsAccess(target), UnknownData, target.origin);
|
|
81
|
+
if (isWgslStruct(target.dataType) || isUnstruct(target.dataType)) {
|
|
82
|
+
let propType = target.dataType.propTypes[propName];
|
|
83
|
+
if (!propType) return;
|
|
84
|
+
propType = undecorate(propType);
|
|
85
|
+
return snip(stitch`${target}.${propName}`, propType, target.origin === "argument" ? "argument" : !isEphemeralSnippet(target) && !isNaturallyEphemeral(propType) ? target.origin : target.origin === "constant" || target.origin === "constant-tgpu-const-ref" ? "constant" : "runtime");
|
|
86
|
+
}
|
|
87
|
+
if (target.dataType instanceof AutoStruct) {
|
|
88
|
+
const result = target.dataType.accessProp(propName);
|
|
89
|
+
if (!result) return;
|
|
90
|
+
return snip(stitch`${target}.${result.prop}`, result.type, "argument");
|
|
91
|
+
}
|
|
92
|
+
if (isPtr(target.dataType)) {
|
|
93
|
+
const derefed = derefSnippet(target);
|
|
94
|
+
if (propName === "$") return derefed;
|
|
95
|
+
return accessProp(derefed, propName);
|
|
96
|
+
}
|
|
97
|
+
if (isVec(target.dataType)) {
|
|
98
|
+
if (propName === "kind") return snip(target.dataType.type, UnknownData, "constant");
|
|
99
|
+
}
|
|
100
|
+
const propLength = propName.length;
|
|
101
|
+
if (isVec(target.dataType) && propLength >= 1 && propLength <= 4) {
|
|
102
|
+
const isXYZW = /^[xyzw]+$/.test(propName);
|
|
103
|
+
const isRGBA = /^[rgba]+$/.test(propName);
|
|
104
|
+
if (!isXYZW && !isRGBA) return;
|
|
105
|
+
const swizzleType = swizzleLenToType[target.dataType.type.includes("bool") ? "b" : target.dataType.type[4]][propLength];
|
|
106
|
+
if (!swizzleType) return;
|
|
107
|
+
return snip(isKnownAtComptime(target) ? target.value[propName] : stitch`${target}.${propName}`, swizzleType, target.origin === "argument" && propLength === 1 ? "argument" : target.origin === "constant" || target.origin === "constant-tgpu-const-ref" ? "constant" : "runtime");
|
|
108
|
+
}
|
|
109
|
+
if (isKnownAtComptime(target) || target.dataType === UnknownData) return coerceToSnippet(target.value[propName]);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
//#endregion
|
|
113
|
+
export { accessProp, infixOperators };
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { isWgslArray, isWgslData, isWgslStruct } from "../../data/wgslTypes.js";
|
|
2
|
+
import { niceStringify } from "../../shared/stringify.js";
|
|
3
|
+
import { vec2b, vec2f, vec2h, vec2i, vec2u, vec3b, vec3f, vec3h, vec3i, vec3u, vec4b, vec4f, vec4h, vec4i, vec4u } from "../../data/vector.js";
|
|
4
|
+
import { sizeOf } from "../../data/sizeOf.js";
|
|
5
|
+
import { mat2x2f, mat3x3f, mat4x4f } from "../../data/matrix.js";
|
|
6
|
+
import { bitcastU32toF32, bitcastU32toI32 } from "../../std/bitcast.js";
|
|
7
|
+
import { unpack2x16float } from "../../std/packing.js";
|
|
8
|
+
|
|
9
|
+
//#region src/tgsl/consoleLog/deserializers.ts
|
|
10
|
+
const toF = (n) => bitcastU32toF32(n ?? 0);
|
|
11
|
+
const toI = (n) => bitcastU32toI32(n ?? 0);
|
|
12
|
+
const unpack = (n) => unpack2x16float(n ?? 0);
|
|
13
|
+
const deserializerMap = {
|
|
14
|
+
f32: (d) => toF(d[0]),
|
|
15
|
+
f16: (d) => unpack(d[0]).x,
|
|
16
|
+
i32: (d) => toI(d[0]),
|
|
17
|
+
u32: (d) => d[0] ?? 0,
|
|
18
|
+
bool: (d) => !!d[0],
|
|
19
|
+
vec2f: (d) => vec2f(toF(d[0]), toF(d[1])),
|
|
20
|
+
vec3f: (d) => vec3f(toF(d[0]), toF(d[1]), toF(d[2])),
|
|
21
|
+
vec4f: (d) => vec4f(toF(d[0]), toF(d[1]), toF(d[2]), toF(d[3])),
|
|
22
|
+
vec2h(d) {
|
|
23
|
+
const xyVec = unpack(d[0]);
|
|
24
|
+
return vec2h(xyVec.x, xyVec.y);
|
|
25
|
+
},
|
|
26
|
+
vec3h(d) {
|
|
27
|
+
const xyVec = unpack(d[0]);
|
|
28
|
+
const zVec = unpack(d[1]);
|
|
29
|
+
return vec3h(xyVec.x, xyVec.y, zVec.x);
|
|
30
|
+
},
|
|
31
|
+
vec4h(d) {
|
|
32
|
+
const xyVec = unpack(d[0]);
|
|
33
|
+
const zwVec = unpack(d[1]);
|
|
34
|
+
return vec4h(xyVec.x, xyVec.y, zwVec.x, zwVec.y);
|
|
35
|
+
},
|
|
36
|
+
vec2i: (d) => vec2i(toI(d[0]), toI(d[1])),
|
|
37
|
+
vec3i: (d) => vec3i(toI(d[0]), toI(d[1]), toI(d[2])),
|
|
38
|
+
vec4i: (d) => vec4i(toI(d[0]), toI(d[1]), toI(d[2]), toI(d[3])),
|
|
39
|
+
vec2u: (d) => vec2u(d[0] ?? 0, d[1] ?? 0),
|
|
40
|
+
vec3u: (d) => vec3u(d[0] ?? 0, d[1] ?? 0, d[2] ?? 0),
|
|
41
|
+
vec4u: (d) => vec4u(d[0] ?? 0, d[1] ?? 0, d[2] ?? 0, d[3] ?? 0),
|
|
42
|
+
"vec2<bool>": (d) => vec2b(!!d[0], !!d[1]),
|
|
43
|
+
"vec3<bool>": (d) => vec3b(!!d[0], !!d[1], !!d[2]),
|
|
44
|
+
"vec4<bool>": (d) => vec4b(!!d[0], !!d[1], !!d[2], !!d[3]),
|
|
45
|
+
mat2x2f: (d) => mat2x2f(toF(d[0]), toF(d[1]), toF(d[2]), toF(d[3])),
|
|
46
|
+
mat3x3f: (d) => mat3x3f(toF(d[0]), toF(d[1]), toF(d[2]), toF(d[4]), toF(d[5]), toF(d[6]), toF(d[8]), toF(d[9]), toF(d[10])),
|
|
47
|
+
mat4x4f: (d) => mat4x4f(toF(d[0]), toF(d[1]), toF(d[2]), toF(d[3]), toF(d[4]), toF(d[5]), toF(d[6]), toF(d[7]), toF(d[8]), toF(d[9]), toF(d[10]), toF(d[11]), toF(d[12]), toF(d[13]), toF(d[14]), toF(d[15]))
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Deserializes binary data from a Uint32Array into a JavaScript value based on the provided WGSL data type.
|
|
51
|
+
*
|
|
52
|
+
* @param data - The binary data as a Uint32Array to be deserialized
|
|
53
|
+
* @param dataType - The WGSL data type specification that determines how to interpret the binary data
|
|
54
|
+
*/
|
|
55
|
+
function deserialize(data, dataType) {
|
|
56
|
+
const maybeDeserializer = deserializerMap[dataType.type];
|
|
57
|
+
if (maybeDeserializer) return maybeDeserializer(data);
|
|
58
|
+
if (isWgslStruct(dataType)) {
|
|
59
|
+
const props = Object.keys(dataType.propTypes);
|
|
60
|
+
const decodedProps = deserializeCompound(data, Object.values(dataType.propTypes));
|
|
61
|
+
return Object.fromEntries(props.map((key, index) => [key, decodedProps[index]]));
|
|
62
|
+
}
|
|
63
|
+
if (isWgslArray(dataType)) {
|
|
64
|
+
const elementType = dataType.elementType;
|
|
65
|
+
const length = dataType.elementCount;
|
|
66
|
+
return deserializeCompound(data, Array.from({ length }, () => elementType));
|
|
67
|
+
}
|
|
68
|
+
throw new Error(`Cannot deserialize data of type ${dataType.type}`);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Deserializes a list of elements from a Uint32Array buffer using provided type information.
|
|
72
|
+
* If there is a string value among the type information, it is returned as is.
|
|
73
|
+
*
|
|
74
|
+
* @param data - The Uint32Array buffer containing the serialized data
|
|
75
|
+
* @param dataTypes - The WGSL data type specification that determines how to interpret the binary data, or string literals
|
|
76
|
+
*/
|
|
77
|
+
function deserializeCompound(data, dataTypes) {
|
|
78
|
+
let index = 0;
|
|
79
|
+
return dataTypes.map((info) => {
|
|
80
|
+
if (!isWgslData(info)) return info;
|
|
81
|
+
const size = Math.ceil(sizeOf(info) / 4);
|
|
82
|
+
const value = deserialize(data.subarray(index, index + size), info);
|
|
83
|
+
index += size;
|
|
84
|
+
return value;
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
function deserializeAndStringify(serializedData, argTypes) {
|
|
88
|
+
return deserializeCompound(serializedData, argTypes).map(niceStringify);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Reads and deserializes log data from GPU buffers, logging results to the console.
|
|
92
|
+
*
|
|
93
|
+
* @remarks
|
|
94
|
+
* - Log entries with IDs equal to 0 are filtered out.
|
|
95
|
+
* - Console messages are prepended with options.messagePrefix styled with purple background and white text.
|
|
96
|
+
* - A warning is displayed if the log count exceeds the limit passed in options.
|
|
97
|
+
* - After processing, the index buffer and the data buffer are cleared.
|
|
98
|
+
*/
|
|
99
|
+
function logDataFromGPU(resources) {
|
|
100
|
+
const { indexBuffer, dataBuffer, logIdToMeta, options } = resources;
|
|
101
|
+
dataBuffer.read().then((data) => {
|
|
102
|
+
data.filter((e) => e.id).forEach(({ id, serializedData }) => {
|
|
103
|
+
const { argTypes, op } = logIdToMeta.get(id);
|
|
104
|
+
const results = deserializeAndStringify(new Uint32Array(serializedData), argTypes);
|
|
105
|
+
if (results.length === 0) results.push("");
|
|
106
|
+
console[op](`%c${options.messagePrefix}%c ${results[0]}`, "background: #936ff5; color: white;", "color: inherit; background: none", ...results.slice(1));
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
indexBuffer.read().then((totalCalls) => {
|
|
110
|
+
if (totalCalls > options.logCountLimit) console.warn(`Log count limit per dispatch (${options.logCountLimit}) exceeded by ${totalCalls - options.logCountLimit} calls. Consider increasing the limit by passing appropriate options to tgpu.init().`);
|
|
111
|
+
});
|
|
112
|
+
dataBuffer.buffer.clear();
|
|
113
|
+
indexBuffer.buffer.clear();
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
//#endregion
|
|
117
|
+
export { logDataFromGPU };
|