typegpu 0.10.1 → 0.11.0-rc.1
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/_virtual/_rolldown/runtime.js +13 -0
- package/builtin.d.ts +50 -0
- package/builtin.js +35 -0
- package/common/fullScreenTriangle.d.ts +22 -0
- package/common/fullScreenTriangle.js +34 -0
- package/common/index.d.ts +4 -4
- package/common/index.js +8 -7
- package/common/writeSoA.d.ts +16 -0
- package/common/writeSoA.js +90 -0
- package/core/buffer/buffer.d.ts +79 -0
- package/core/buffer/buffer.js +246 -0
- package/core/buffer/bufferShorthand.d.ts +48 -0
- package/core/buffer/bufferShorthand.js +53 -0
- package/core/buffer/bufferUsage.d.ts +43 -0
- package/core/buffer/bufferUsage.js +165 -0
- package/core/constant/tgpuConstant.d.ts +29 -0
- package/core/constant/tgpuConstant.js +68 -0
- package/core/declare/tgpuDeclare.d.ts +18 -0
- package/core/declare/tgpuDeclare.js +39 -0
- package/core/function/autoIO.d.ts +38 -0
- package/core/function/autoIO.js +85 -0
- package/core/function/comptime.d.ts +39 -0
- package/core/function/comptime.js +49 -0
- package/core/function/createCallableSchema.js +40 -0
- package/core/function/dualImpl.js +52 -0
- package/core/function/entryInputRouter.js +39 -0
- package/core/function/extractArgs.js +204 -0
- package/core/function/fnCore.js +90 -0
- package/core/function/fnTypes.d.ts +40 -0
- package/core/function/ioSchema.d.ts +10 -0
- package/core/function/ioSchema.js +51 -0
- package/core/function/shelllessImpl.d.ts +28 -0
- package/core/function/shelllessImpl.js +21 -0
- package/core/function/templateUtils.js +12 -0
- package/core/function/tgpuComputeFn.d.ts +48 -0
- package/core/function/tgpuComputeFn.js +55 -0
- package/core/function/tgpuFn.d.ts +52 -0
- package/core/function/tgpuFn.js +168 -0
- package/core/function/tgpuFragmentFn.d.ts +72 -0
- package/core/function/tgpuFragmentFn.js +63 -0
- package/core/function/tgpuVertexFn.d.ts +59 -0
- package/core/function/tgpuVertexFn.js +59 -0
- package/core/pipeline/applyPipelineState.js +35 -0
- package/core/pipeline/computePipeline.d.ts +54 -0
- package/core/pipeline/computePipeline.js +227 -0
- package/core/pipeline/connectAttachmentToShader.js +24 -0
- package/core/pipeline/connectTargetsToShader.js +27 -0
- package/core/pipeline/limitsOverflow.js +12 -0
- package/core/pipeline/pipelineUtils.js +29 -0
- package/core/pipeline/renderPipeline.d.ts +284 -0
- package/core/pipeline/renderPipeline.js +489 -0
- package/core/pipeline/timeable.d.ts +20 -0
- package/core/pipeline/timeable.js +55 -0
- package/core/pipeline/typeGuards.js +27 -0
- package/core/querySet/querySet.d.ts +20 -0
- package/core/querySet/querySet.js +104 -0
- package/core/rawCodeSnippet/tgpuRawCodeSnippet.d.ts +59 -0
- package/core/rawCodeSnippet/tgpuRawCodeSnippet.js +94 -0
- package/core/resolve/externals.d.ts +8 -0
- package/core/resolve/externals.js +56 -0
- package/core/resolve/namespace.d.ts +38 -0
- package/core/resolve/namespace.js +39 -0
- package/core/resolve/resolveData.js +144 -0
- package/core/resolve/stitch.js +23 -0
- package/core/resolve/tgpuResolve.d.ts +153 -0
- package/core/resolve/tgpuResolve.js +66 -0
- package/core/root/configurableImpl.js +17 -0
- package/core/root/init.d.ts +64 -0
- package/core/root/init.js +464 -0
- package/core/root/rootTypes.d.ts +642 -0
- package/core/sampler/sampler.d.ts +31 -0
- package/core/sampler/sampler.js +116 -0
- package/core/simulate/tgpuSimulate.d.ts +36 -0
- package/core/simulate/tgpuSimulate.js +74 -0
- package/core/slot/accessor.d.ts +9 -0
- package/core/slot/accessor.js +95 -0
- package/core/slot/internalSlots.js +5 -0
- package/core/slot/lazy.d.ts +6 -0
- package/core/slot/lazy.js +40 -0
- package/core/slot/slot.d.ts +6 -0
- package/core/slot/slot.js +39 -0
- package/core/slot/slotTypes.d.ts +92 -0
- package/core/slot/slotTypes.js +19 -0
- package/core/texture/externalTexture.d.ts +6 -0
- package/core/texture/externalTexture.js +47 -0
- package/core/texture/texture.d.ts +114 -0
- package/core/texture/texture.js +314 -0
- package/core/texture/textureFormats.d.ts +29 -0
- package/core/texture/textureFormats.js +97 -0
- package/core/texture/textureProps.d.ts +11 -0
- package/core/texture/textureUtils.js +222 -0
- package/core/texture/usageExtension.d.ts +21 -0
- package/core/texture/usageExtension.js +19 -0
- package/core/unroll/tgpuUnroll.d.ts +68 -0
- package/core/unroll/tgpuUnroll.js +94 -0
- package/core/valueProxyUtils.js +42 -0
- package/core/variable/tgpuVariable.d.ts +38 -0
- package/core/variable/tgpuVariable.js +99 -0
- package/core/vertexLayout/connectAttributesToShader.js +57 -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 +14 -0
- package/data/alignmentOf.d.ts +9 -0
- package/data/alignmentOf.js +86 -0
- package/data/array.d.ts +26 -0
- package/data/array.js +46 -0
- package/data/atomic.d.ts +15 -0
- package/data/atomic.js +24 -0
- package/data/attributes.d.ts +121 -0
- package/data/attributes.js +145 -0
- package/data/autoStruct.d.ts +1 -0
- package/data/autoStruct.js +81 -0
- package/data/compiledIO.js +228 -0
- package/data/dataIO.js +556 -0
- package/data/dataTypes.d.ts +115 -0
- package/data/dataTypes.js +100 -0
- package/data/deepEqual.d.ts +25 -0
- package/data/deepEqual.js +56 -0
- package/data/disarray.d.ts +32 -0
- package/data/disarray.js +50 -0
- package/data/getLongestContiguousPrefix.d.ts +9 -0
- package/data/getLongestContiguousPrefix.js +13 -0
- package/data/index.d.ts +26 -4
- package/data/index.js +36 -9
- package/data/instanceToSchema.d.ts +33 -0
- package/data/isContiguous.d.ts +9 -0
- package/data/isContiguous.js +13 -0
- package/data/matrix.d.ts +124 -0
- package/data/matrix.js +531 -0
- package/data/numberOps.js +23 -0
- package/data/numeric.d.ts +81 -0
- package/data/numeric.js +221 -0
- package/data/offsetUtils.d.ts +33 -0
- package/data/offsetUtils.js +165 -0
- package/data/offsets.js +34 -0
- package/data/partialIO.js +113 -0
- package/data/ptr.d.ts +11 -0
- package/data/ptr.js +44 -0
- package/data/ref.d.ts +34 -0
- package/data/ref.js +94 -0
- package/data/sampler.d.ts +107 -0
- package/data/sampler.js +24 -0
- package/data/schemaCallWrapper.js +30 -0
- package/data/schemaMemoryLayout.js +198 -0
- package/data/sizeOf.d.ts +9 -0
- package/data/sizeOf.js +13 -0
- package/data/snippet.d.ts +26 -0
- package/data/snippet.js +70 -0
- package/data/struct.d.ts +17 -0
- package/data/struct.js +44 -0
- package/data/texture.d.ts +292 -0
- package/{texture-BagDrrks.js → data/texture.js} +6 -5
- package/data/unstruct.d.ts +24 -0
- package/data/unstruct.js +41 -0
- package/data/vector.d.ts +191 -0
- package/data/vector.js +239 -0
- package/data/vectorImpl.js +515 -0
- package/data/vectorOps.js +681 -0
- package/data/vertexFormatData.d.ts +190 -0
- package/data/vertexFormatData.js +109 -0
- package/data/wgslTypes.d.ts +924 -0
- package/data/wgslTypes.js +222 -0
- package/errors.d.ts +44 -0
- package/errors.js +131 -0
- package/execMode.js +49 -0
- package/extension.d.ts +11 -0
- package/extension.js +16 -0
- package/getGPUValue.js +7 -0
- package/index.d.ts +42 -243
- package/index.js +21 -6320
- package/indexNamedExports.d.ts +40 -0
- package/mathUtils.js +12 -0
- package/memo.js +22 -0
- package/nameRegistry.d.ts +30 -0
- package/nameRegistry.js +447 -0
- package/package.js +4 -0
- package/package.json +26 -26
- package/resolutionCtx.d.ts +19 -0
- package/resolutionCtx.js +612 -0
- package/shared/env.js +12 -0
- package/shared/generators.js +13 -0
- package/shared/meta.d.ts +39 -0
- package/shared/meta.js +61 -0
- package/shared/repr.d.ts +138 -0
- package/shared/stringify.js +20 -0
- package/shared/symbols.d.ts +70 -0
- package/shared/symbols.js +48 -0
- package/shared/utilityTypes.d.ts +33 -0
- package/shared/utilityTypes.js +6 -0
- package/shared/vertexFormat.d.ts +70 -0
- package/shared/vertexFormat.js +63 -0
- package/std/array.d.ts +7 -0
- package/std/array.js +25 -0
- package/std/atomic.d.ts +19 -0
- package/std/atomic.js +111 -0
- package/std/bitcast.d.ts +10 -0
- package/std/bitcast.js +41 -0
- package/std/boolean.d.ts +141 -0
- package/std/boolean.js +299 -0
- package/std/derivative.d.ts +16 -0
- package/std/derivative.js +87 -0
- package/std/discard.d.ts +6 -0
- package/std/discard.js +14 -0
- package/std/extensions.d.ts +6 -0
- package/std/extensions.js +12 -0
- package/std/index.d.ts +17 -4
- package/std/index.js +21 -7
- package/std/matrix.d.ts +41 -0
- package/std/matrix.js +85 -0
- package/std/numeric.d.ts +200 -0
- package/std/numeric.js +845 -0
- package/std/operators.d.ts +56 -0
- package/std/operators.js +227 -0
- package/std/packing.d.ts +26 -0
- package/std/packing.js +84 -0
- package/std/range.d.ts +24 -0
- package/std/range.js +38 -0
- package/std/subgroup.d.ts +47 -0
- package/std/subgroup.js +218 -0
- package/std/texture.d.ts +117 -0
- package/std/texture.js +207 -0
- package/tgpu.js +42 -0
- package/tgpuBindGroupLayout.d.ts +161 -0
- package/tgpuBindGroupLayout.js +272 -0
- package/tgpuUnstable.d.ts +48 -0
- package/tgpuUnstable.js +64 -0
- package/tgsl/accessIndex.js +43 -0
- package/tgsl/accessProp.js +115 -0
- package/tgsl/consoleLog/deserializers.js +115 -0
- package/tgsl/consoleLog/logGenerator.js +84 -0
- package/tgsl/consoleLog/serializers.js +223 -0
- package/tgsl/consoleLog/types.d.ts +52 -0
- package/tgsl/consoleLog/types.js +11 -0
- package/tgsl/conversion.js +198 -0
- package/tgsl/forOfUtils.js +71 -0
- package/tgsl/generationHelpers.d.ts +37 -0
- package/tgsl/generationHelpers.js +67 -0
- package/tgsl/math.js +43 -0
- package/tgsl/shaderGenerator.d.ts +20 -0
- package/tgsl/shaderGenerator_members.d.ts +2 -0
- package/tgsl/shaderGenerator_members.js +6 -0
- package/tgsl/shellless.d.ts +11 -0
- package/tgsl/shellless.js +46 -0
- package/tgsl/wgslGenerator.d.ts +36 -0
- package/tgsl/wgslGenerator.js +639 -0
- package/types.d.ts +265 -0
- package/types.js +43 -0
- package/unwrapper.d.ts +27 -0
- package/wgslExtensions.d.ts +5 -0
- package/wgslExtensions.js +17 -0
- package/builtin-DdtWpk2t.js +0 -818
- package/builtin-DdtWpk2t.js.map +0 -1
- package/chunk-BYypO7fO.js +0 -18
- 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,13 @@
|
|
|
1
|
+
//#region \0rolldown/runtime.js
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __exportAll = (all, no_symbols) => {
|
|
4
|
+
let target = {};
|
|
5
|
+
for (var name in all) __defProp(target, name, {
|
|
6
|
+
get: all[name],
|
|
7
|
+
enumerable: true
|
|
8
|
+
});
|
|
9
|
+
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
10
|
+
return target;
|
|
11
|
+
};
|
|
12
|
+
//#endregion
|
|
13
|
+
export { __exportAll };
|
package/builtin.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { BaseData, Bool, Builtin, Decorated, F32, U32, Vec3u, Vec4f, WgslArray } from "./data/wgslTypes.js";
|
|
2
|
+
|
|
3
|
+
//#region src/builtin.d.ts
|
|
4
|
+
type BuiltinVertexIndex = Decorated<U32, [Builtin<'vertex_index'>]>;
|
|
5
|
+
type BuiltinInstanceIndex = Decorated<U32, [Builtin<'instance_index'>]>;
|
|
6
|
+
type BuiltinClipDistances = Decorated<WgslArray<U32>, [Builtin<'clip_distances'>]>;
|
|
7
|
+
type BuiltinPosition = Decorated<Vec4f, [Builtin<'position'>]>;
|
|
8
|
+
type BuiltinFrontFacing = Decorated<Bool, [Builtin<'front_facing'>]>;
|
|
9
|
+
type BuiltinFragDepth = Decorated<F32, [Builtin<'frag_depth'>]>;
|
|
10
|
+
type BuiltinPrimitiveIndex = Decorated<U32, [Builtin<'primitive_index'>]>;
|
|
11
|
+
type BuiltinSampleIndex = Decorated<U32, [Builtin<'sample_index'>]>;
|
|
12
|
+
type BuiltinSampleMask = Decorated<U32, [Builtin<'sample_mask'>]>;
|
|
13
|
+
type BuiltinLocalInvocationId = Decorated<Vec3u, [Builtin<'local_invocation_id'>]>;
|
|
14
|
+
type BuiltinLocalInvocationIndex = Decorated<U32, [Builtin<'local_invocation_index'>]>;
|
|
15
|
+
type BuiltinGlobalInvocationId = Decorated<Vec3u, [Builtin<'global_invocation_id'>]>;
|
|
16
|
+
type BuiltinWorkgroupId = Decorated<Vec3u, [Builtin<'workgroup_id'>]>;
|
|
17
|
+
type BuiltinNumWorkgroups = Decorated<Vec3u, [Builtin<'num_workgroups'>]>;
|
|
18
|
+
type BuiltinSubgroupInvocationId = Decorated<U32, [Builtin<'subgroup_invocation_id'>]>;
|
|
19
|
+
type BuiltinSubgroupSize = Decorated<U32, [Builtin<'subgroup_size'>]>;
|
|
20
|
+
type BuiltinSubgroupId = Decorated<U32, [Builtin<'subgroup_id'>]>;
|
|
21
|
+
type BuiltinNumSubgroups = Decorated<U32, [Builtin<'num_subgroups'>]>;
|
|
22
|
+
declare const builtin: {
|
|
23
|
+
readonly vertexIndex: BuiltinVertexIndex;
|
|
24
|
+
readonly instanceIndex: BuiltinInstanceIndex;
|
|
25
|
+
readonly clipDistances: BuiltinClipDistances;
|
|
26
|
+
readonly position: BuiltinPosition;
|
|
27
|
+
readonly frontFacing: BuiltinFrontFacing;
|
|
28
|
+
readonly fragDepth: BuiltinFragDepth;
|
|
29
|
+
readonly primitiveIndex: BuiltinPrimitiveIndex;
|
|
30
|
+
readonly sampleIndex: BuiltinSampleIndex;
|
|
31
|
+
readonly sampleMask: BuiltinSampleMask;
|
|
32
|
+
readonly localInvocationId: BuiltinLocalInvocationId;
|
|
33
|
+
readonly localInvocationIndex: BuiltinLocalInvocationIndex;
|
|
34
|
+
readonly globalInvocationId: BuiltinGlobalInvocationId;
|
|
35
|
+
readonly workgroupId: BuiltinWorkgroupId;
|
|
36
|
+
readonly numWorkgroups: BuiltinNumWorkgroups;
|
|
37
|
+
readonly subgroupInvocationId: BuiltinSubgroupInvocationId;
|
|
38
|
+
readonly subgroupSize: BuiltinSubgroupSize;
|
|
39
|
+
readonly subgroupId: BuiltinSubgroupId;
|
|
40
|
+
readonly numSubgroups: BuiltinNumSubgroups;
|
|
41
|
+
};
|
|
42
|
+
type AnyBuiltin = (typeof builtin)[keyof typeof builtin];
|
|
43
|
+
type AnyComputeBuiltin = BuiltinLocalInvocationId | BuiltinLocalInvocationIndex | BuiltinGlobalInvocationId | BuiltinWorkgroupId | BuiltinNumWorkgroups | BuiltinSubgroupInvocationId | BuiltinSubgroupSize | BuiltinSubgroupId | BuiltinNumSubgroups;
|
|
44
|
+
type AnyVertexInputBuiltin = BuiltinVertexIndex | BuiltinInstanceIndex;
|
|
45
|
+
type AnyVertexOutputBuiltin = BuiltinClipDistances | BuiltinPosition;
|
|
46
|
+
type AnyFragmentInputBuiltin = BuiltinPosition | BuiltinFrontFacing | BuiltinPrimitiveIndex | BuiltinSampleIndex | BuiltinSampleMask | BuiltinSubgroupInvocationId | BuiltinSubgroupSize;
|
|
47
|
+
type AnyFragmentOutputBuiltin = BuiltinFragDepth | BuiltinSampleMask;
|
|
48
|
+
type OmitBuiltins<S> = S extends AnyBuiltin ? never : S extends BaseData ? S : { [Key in keyof S as S[Key] extends AnyBuiltin ? never : Key extends `$${string}` ? never : Key]: S[Key] };
|
|
49
|
+
//#endregion
|
|
50
|
+
export { AnyBuiltin, AnyComputeBuiltin, AnyFragmentInputBuiltin, AnyFragmentOutputBuiltin, AnyVertexInputBuiltin, AnyVertexOutputBuiltin, BuiltinClipDistances, BuiltinFragDepth, BuiltinFrontFacing, BuiltinGlobalInvocationId, BuiltinInstanceIndex, BuiltinLocalInvocationId, BuiltinLocalInvocationIndex, BuiltinNumWorkgroups, BuiltinPosition, BuiltinPrimitiveIndex, BuiltinSampleIndex, BuiltinSampleMask, BuiltinSubgroupInvocationId, BuiltinSubgroupSize, BuiltinVertexIndex, BuiltinWorkgroupId, OmitBuiltins, builtin };
|
package/builtin.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { $internal } from "./shared/symbols.js";
|
|
2
|
+
import { bool, f32, u32 } from "./data/numeric.js";
|
|
3
|
+
import { vec3u, vec4f } from "./data/vector.js";
|
|
4
|
+
import { attribute } from "./data/attributes.js";
|
|
5
|
+
import { arrayOf } from "./data/array.js";
|
|
6
|
+
//#region src/builtin.ts
|
|
7
|
+
function defineBuiltin(dataType, value) {
|
|
8
|
+
return attribute(dataType, {
|
|
9
|
+
[$internal]: true,
|
|
10
|
+
type: "@builtin",
|
|
11
|
+
params: [value]
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
const builtin = {
|
|
15
|
+
vertexIndex: defineBuiltin(u32, "vertex_index"),
|
|
16
|
+
instanceIndex: defineBuiltin(u32, "instance_index"),
|
|
17
|
+
clipDistances: defineBuiltin(arrayOf(u32, 8), "clip_distances"),
|
|
18
|
+
position: defineBuiltin(vec4f, "position"),
|
|
19
|
+
frontFacing: defineBuiltin(bool, "front_facing"),
|
|
20
|
+
fragDepth: defineBuiltin(f32, "frag_depth"),
|
|
21
|
+
primitiveIndex: defineBuiltin(u32, "primitive_index"),
|
|
22
|
+
sampleIndex: defineBuiltin(u32, "sample_index"),
|
|
23
|
+
sampleMask: defineBuiltin(u32, "sample_mask"),
|
|
24
|
+
localInvocationId: defineBuiltin(vec3u, "local_invocation_id"),
|
|
25
|
+
localInvocationIndex: defineBuiltin(u32, "local_invocation_index"),
|
|
26
|
+
globalInvocationId: defineBuiltin(vec3u, "global_invocation_id"),
|
|
27
|
+
workgroupId: defineBuiltin(vec3u, "workgroup_id"),
|
|
28
|
+
numWorkgroups: defineBuiltin(vec3u, "num_workgroups"),
|
|
29
|
+
subgroupInvocationId: defineBuiltin(u32, "subgroup_invocation_id"),
|
|
30
|
+
subgroupSize: defineBuiltin(u32, "subgroup_size"),
|
|
31
|
+
subgroupId: defineBuiltin(u32, "subgroup_id"),
|
|
32
|
+
numSubgroups: defineBuiltin(u32, "num_subgroups")
|
|
33
|
+
};
|
|
34
|
+
//#endregion
|
|
35
|
+
export { builtin };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { TgpuVertexFn } from "../core/function/tgpuVertexFn.js";
|
|
2
|
+
import { Vec2f } from "../data/wgslTypes.js";
|
|
3
|
+
//#region src/common/fullScreenTriangle.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* A vertex function that defines a single full-screen triangle out
|
|
6
|
+
* of three points.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { common } from 'typegpu';
|
|
11
|
+
*
|
|
12
|
+
* const pipeline = root.createRenderPipeline({
|
|
13
|
+
* vertex: common.fullScreenTriangle,
|
|
14
|
+
* fragment: yourFragmentShader,
|
|
15
|
+
* });
|
|
16
|
+
*
|
|
17
|
+
* pipeline.draw(3);
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
declare const fullScreenTriangle: TgpuVertexFn;
|
|
21
|
+
//#endregion
|
|
22
|
+
export { fullScreenTriangle };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { vec2f } from "../data/vector.js";
|
|
2
|
+
import { builtin } from "../builtin.js";
|
|
3
|
+
import { vertexFn } from "../core/function/tgpuVertexFn.js";
|
|
4
|
+
//#region src/common/fullScreenTriangle.ts
|
|
5
|
+
/**
|
|
6
|
+
* A vertex function that defines a single full-screen triangle out
|
|
7
|
+
* of three points.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { common } from 'typegpu';
|
|
12
|
+
*
|
|
13
|
+
* const pipeline = root.createRenderPipeline({
|
|
14
|
+
* vertex: common.fullScreenTriangle,
|
|
15
|
+
* fragment: yourFragmentShader,
|
|
16
|
+
* });
|
|
17
|
+
*
|
|
18
|
+
* pipeline.draw(3);
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
const fullScreenTriangle = vertexFn({
|
|
22
|
+
in: { vertexIndex: builtin.vertexIndex },
|
|
23
|
+
out: {
|
|
24
|
+
pos: builtin.position,
|
|
25
|
+
uv: vec2f
|
|
26
|
+
}
|
|
27
|
+
})`{
|
|
28
|
+
const pos = array<vec2f, 3>(vec2f(-1, -1), vec2f(3, -1), vec2f(-1, 3));
|
|
29
|
+
const uv = array<vec2f, 3>(vec2f(0, 1), vec2f(2, 1), vec2f(0, -1));
|
|
30
|
+
|
|
31
|
+
return Out(vec4f(pos[in.vertexIndex], 0, 1), uv[in.vertexIndex]);
|
|
32
|
+
}`;
|
|
33
|
+
//#endregion
|
|
34
|
+
export { fullScreenTriangle };
|
package/common/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { fullScreenTriangle } from "./fullScreenTriangle.js";
|
|
2
|
+
import { writeSoA } from "./writeSoA.js";
|
|
2
3
|
|
|
3
4
|
//#region src/common/index.d.ts
|
|
4
5
|
declare namespace index_d_exports {
|
|
5
|
-
export { fullScreenTriangle };
|
|
6
|
+
export { fullScreenTriangle, writeSoA };
|
|
6
7
|
}
|
|
7
8
|
//#endregion
|
|
8
|
-
export { fullScreenTriangle, index_d_exports
|
|
9
|
-
//# sourceMappingURL=index.d.ts.map
|
|
9
|
+
export { fullScreenTriangle, index_d_exports, writeSoA };
|
package/common/index.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
import { __exportAll } from "../_virtual/_rolldown/runtime.js";
|
|
2
|
+
import { fullScreenTriangle } from "./fullScreenTriangle.js";
|
|
3
|
+
import { writeSoA } from "./writeSoA.js";
|
|
4
4
|
//#region src/common/index.ts
|
|
5
|
-
var common_exports = /* @__PURE__ */ __exportAll({
|
|
6
|
-
|
|
5
|
+
var common_exports = /* @__PURE__ */ __exportAll({
|
|
6
|
+
fullScreenTriangle: () => fullScreenTriangle,
|
|
7
|
+
writeSoA: () => writeSoA
|
|
8
|
+
});
|
|
7
9
|
//#endregion
|
|
8
|
-
export { fullScreenTriangle,
|
|
9
|
-
//# sourceMappingURL=index.js.map
|
|
10
|
+
export { common_exports, fullScreenTriangle, writeSoA };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Prettify } from "../shared/utilityTypes.js";
|
|
2
|
+
import { BufferWriteOptions, TgpuBuffer } from "../core/buffer/buffer.js";
|
|
3
|
+
import { BaseData, TypedArrayFor, WgslArray, WgslStruct } from "../data/wgslTypes.js";
|
|
4
|
+
import { Undecorate } from "../data/dataTypes.js";
|
|
5
|
+
|
|
6
|
+
//#region src/common/writeSoA.d.ts
|
|
7
|
+
type PackedScalarFor<T> = Undecorate<T> extends WgslArray<infer TElement> ? PackedScalarFor<TElement> : Undecorate<T>;
|
|
8
|
+
type PackedSoAInputFor<T> = TypedArrayFor<PackedScalarFor<T>>;
|
|
9
|
+
type SoAFieldsFor<T extends Record<string, BaseData>> = { [K in keyof T as [PackedSoAInputFor<T[K]>] extends [never] ? never : K]: PackedSoAInputFor<T[K]> };
|
|
10
|
+
type SoAInputFor<T extends Record<string, BaseData>> = [keyof T] extends [keyof SoAFieldsFor<T>] ? Prettify<SoAFieldsFor<T>> : never;
|
|
11
|
+
declare function writeSoA<TProps extends Record<string, BaseData>>(buffer: TgpuBuffer<WgslArray<WgslStruct<TProps>>>, data: SoAInputFor<TProps>, options?: BufferWriteOptions): void;
|
|
12
|
+
declare namespace writeSoA {
|
|
13
|
+
type InputFor<TProps extends Record<string, BaseData>> = SoAInputFor<TProps>;
|
|
14
|
+
}
|
|
15
|
+
//#endregion
|
|
16
|
+
export { writeSoA };
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { isAtomic, isMat, isMat2x2f, isMat3x3f, isWgslArray } from "../data/wgslTypes.js";
|
|
2
|
+
import { undecorate } from "../data/dataTypes.js";
|
|
3
|
+
import { invariant } from "../errors.js";
|
|
4
|
+
import { alignmentOf } from "../data/alignmentOf.js";
|
|
5
|
+
import { roundUp } from "../mathUtils.js";
|
|
6
|
+
import { sizeOf } from "../data/sizeOf.js";
|
|
7
|
+
import { offsetsForProps } from "../data/offsets.js";
|
|
8
|
+
//#region src/common/writeSoA.ts
|
|
9
|
+
function packedSchemaOf(schema) {
|
|
10
|
+
const unpackedSchema = undecorate(schema);
|
|
11
|
+
return isAtomic(unpackedSchema) ? unpackedSchema.inner : unpackedSchema;
|
|
12
|
+
}
|
|
13
|
+
function packedMatrixDimOf(schema) {
|
|
14
|
+
return isMat3x3f(schema) ? 3 : isMat2x2f(schema) ? 2 : isMat(schema) ? 4 : void 0;
|
|
15
|
+
}
|
|
16
|
+
function packedSizeOf(schema) {
|
|
17
|
+
const packedSchema = packedSchemaOf(schema);
|
|
18
|
+
const matrixDim = packedMatrixDimOf(packedSchema);
|
|
19
|
+
if (matrixDim) return matrixDim * matrixDim * 4;
|
|
20
|
+
if (isWgslArray(packedSchema)) return packedSchema.elementCount * packedSizeOf(packedSchema.elementType);
|
|
21
|
+
return sizeOf(packedSchema);
|
|
22
|
+
}
|
|
23
|
+
function computeSoAByteLength(arraySchema, soaData) {
|
|
24
|
+
const structSchema = arraySchema.elementType;
|
|
25
|
+
let inferredCount;
|
|
26
|
+
for (const key in structSchema.propTypes) {
|
|
27
|
+
const srcArray = soaData[key];
|
|
28
|
+
const fieldSchema = structSchema.propTypes[key];
|
|
29
|
+
if (srcArray === void 0 || fieldSchema === void 0) continue;
|
|
30
|
+
const packedFieldSize = packedSizeOf(fieldSchema);
|
|
31
|
+
if (packedFieldSize === 0) continue;
|
|
32
|
+
const fieldElementCount = Math.floor(srcArray.byteLength / packedFieldSize);
|
|
33
|
+
inferredCount = inferredCount === void 0 ? fieldElementCount : Math.min(inferredCount, fieldElementCount);
|
|
34
|
+
}
|
|
35
|
+
if (inferredCount === void 0) return;
|
|
36
|
+
const elementStride = roundUp(sizeOf(structSchema), alignmentOf(structSchema));
|
|
37
|
+
return inferredCount * elementStride;
|
|
38
|
+
}
|
|
39
|
+
function writePackedValue(target, schema, srcBytes, dstOffset, srcOffset) {
|
|
40
|
+
const unpackedSchema = undecorate(schema);
|
|
41
|
+
const packedSchema = isAtomic(unpackedSchema) ? unpackedSchema.inner : unpackedSchema;
|
|
42
|
+
const matrixDim = packedMatrixDimOf(packedSchema);
|
|
43
|
+
if (matrixDim) {
|
|
44
|
+
const packedColumnSize = matrixDim * 4;
|
|
45
|
+
const gpuColumnStride = roundUp(packedColumnSize, alignmentOf(schema));
|
|
46
|
+
for (let col = 0; col < matrixDim; col++) target.set(srcBytes.subarray(srcOffset + col * packedColumnSize, srcOffset + col * packedColumnSize + packedColumnSize), dstOffset + col * gpuColumnStride);
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
if (isWgslArray(unpackedSchema)) {
|
|
50
|
+
const packedElementSize = packedSizeOf(unpackedSchema.elementType);
|
|
51
|
+
const gpuElementStride = roundUp(sizeOf(unpackedSchema.elementType), alignmentOf(unpackedSchema.elementType));
|
|
52
|
+
for (let i = 0; i < unpackedSchema.elementCount; i++) writePackedValue(target, unpackedSchema.elementType, srcBytes, dstOffset + i * gpuElementStride, srcOffset + i * packedElementSize);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
target.set(srcBytes.subarray(srcOffset, srcOffset + sizeOf(packedSchema)), dstOffset);
|
|
56
|
+
}
|
|
57
|
+
function scatterSoA(target, arraySchema, soaData, startOffset, endOffset) {
|
|
58
|
+
const structSchema = arraySchema.elementType;
|
|
59
|
+
const elementStride = roundUp(sizeOf(structSchema), alignmentOf(structSchema));
|
|
60
|
+
invariant(startOffset % elementStride === 0, `startOffset (${startOffset}) must be aligned to the element stride (${elementStride})`);
|
|
61
|
+
const startElement = Math.floor(startOffset / elementStride);
|
|
62
|
+
const endElement = Math.min(arraySchema.elementCount, Math.ceil(endOffset / elementStride));
|
|
63
|
+
const elementCount = Math.max(0, endElement - startElement);
|
|
64
|
+
const offsets = offsetsForProps(structSchema);
|
|
65
|
+
for (const key in structSchema.propTypes) {
|
|
66
|
+
const fieldSchema = structSchema.propTypes[key];
|
|
67
|
+
if (fieldSchema === void 0) continue;
|
|
68
|
+
const srcArray = soaData[key];
|
|
69
|
+
invariant(srcArray !== void 0, `Missing SoA data for field '${key}'`);
|
|
70
|
+
const fieldOffset = offsets[key]?.offset;
|
|
71
|
+
invariant(fieldOffset !== void 0, `Field ${key} not found in struct schema`);
|
|
72
|
+
const packedFieldSize = packedSizeOf(fieldSchema);
|
|
73
|
+
const srcBytes = new Uint8Array(srcArray.buffer, srcArray.byteOffset, srcArray.byteLength);
|
|
74
|
+
for (let i = 0; i < elementCount; i++) writePackedValue(target, fieldSchema, srcBytes, (startElement + i) * elementStride + fieldOffset, i * packedFieldSize);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function writeSoA(buffer, data, options) {
|
|
78
|
+
const arrayBuffer = buffer.arrayBuffer;
|
|
79
|
+
const startOffset = options?.startOffset ?? 0;
|
|
80
|
+
const bufferSize = sizeOf(buffer.dataType);
|
|
81
|
+
const naturalSize = computeSoAByteLength(buffer.dataType, data);
|
|
82
|
+
const endOffset = options?.endOffset ?? (naturalSize === void 0 ? bufferSize : Math.min(startOffset + naturalSize, bufferSize));
|
|
83
|
+
scatterSoA(new Uint8Array(arrayBuffer), buffer.dataType, data, startOffset, endOffset);
|
|
84
|
+
buffer.write(arrayBuffer, {
|
|
85
|
+
startOffset,
|
|
86
|
+
endOffset
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
//#endregion
|
|
90
|
+
export { writeSoA };
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { TgpuNamable } from "../../shared/meta.js";
|
|
2
|
+
import { $internal } from "../../shared/symbols.js";
|
|
3
|
+
import { Prettify, UnionToIntersection } from "../../shared/utilityTypes.js";
|
|
4
|
+
import { StorageFlag } from "../../extension.js";
|
|
5
|
+
import { TgpuBufferMutable, TgpuBufferReadonly, TgpuBufferUniform, TgpuFixedBufferUsage } from "./bufferUsage.js";
|
|
6
|
+
import { BaseData } from "../../data/wgslTypes.js";
|
|
7
|
+
import { Infer, InferInput, InferPartial, InferPatch, IsValidIndexSchema, IsValidStorageSchema, IsValidUniformSchema, IsValidVertexSchema, MemIdentity } from "../../shared/repr.js";
|
|
8
|
+
//#region src/core/buffer/buffer.d.ts
|
|
9
|
+
interface UniformFlag {
|
|
10
|
+
usableAsUniform: true;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* @deprecated Use UniformFlag instead.
|
|
14
|
+
*/
|
|
15
|
+
type Uniform = UniformFlag;
|
|
16
|
+
interface VertexFlag {
|
|
17
|
+
usableAsVertex: true;
|
|
18
|
+
}
|
|
19
|
+
interface IndexFlag {
|
|
20
|
+
usableAsIndex: true;
|
|
21
|
+
}
|
|
22
|
+
interface IndirectFlag {
|
|
23
|
+
usableAsIndirect: true;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* @deprecated Use VertexFlag instead.
|
|
27
|
+
*/
|
|
28
|
+
type Vertex = VertexFlag;
|
|
29
|
+
type UsageLiteral = 'uniform' | 'storage' | 'vertex' | 'index' | 'indirect';
|
|
30
|
+
type LiteralToUsageType<T extends UsageLiteral> = T extends 'uniform' ? UniformFlag : T extends 'storage' ? StorageFlag : T extends 'vertex' ? VertexFlag : T extends 'index' ? IndexFlag : T extends 'indirect' ? IndirectFlag : never;
|
|
31
|
+
type ViewUsages<TBuffer extends TgpuBuffer<BaseData>> = (boolean extends TBuffer['usableAsUniform'] ? never : 'uniform') | (boolean extends TBuffer['usableAsStorage'] ? never : 'readonly' | 'mutable');
|
|
32
|
+
type UsageTypeToBufferUsage<TData extends BaseData> = {
|
|
33
|
+
uniform: TgpuBufferUniform<TData> & TgpuFixedBufferUsage<TData>;
|
|
34
|
+
mutable: TgpuBufferMutable<TData> & TgpuFixedBufferUsage<TData>;
|
|
35
|
+
readonly: TgpuBufferReadonly<TData> & TgpuFixedBufferUsage<TData>;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Done as an object to later Prettify it
|
|
39
|
+
*/
|
|
40
|
+
type InnerValidUsagesFor<T> = {
|
|
41
|
+
usage: (IsValidStorageSchema<T> extends true ? 'storage' : never) | (IsValidUniformSchema<T> extends true ? 'uniform' : never) | (IsValidVertexSchema<T> extends true ? 'vertex' : never) | (IsValidIndexSchema<T> extends true ? 'index' : never) | 'indirect';
|
|
42
|
+
};
|
|
43
|
+
type ValidUsagesFor<T> = InnerValidUsagesFor<T>['usage'];
|
|
44
|
+
type BufferWriteOptions = {
|
|
45
|
+
startOffset?: number;
|
|
46
|
+
endOffset?: number;
|
|
47
|
+
};
|
|
48
|
+
interface TgpuBuffer<TData extends BaseData> extends TgpuNamable {
|
|
49
|
+
readonly [$internal]: true;
|
|
50
|
+
readonly resourceType: 'buffer';
|
|
51
|
+
readonly dataType: TData;
|
|
52
|
+
readonly initial?: InferInput<TData> | undefined;
|
|
53
|
+
readonly arrayBuffer: ArrayBuffer;
|
|
54
|
+
readonly buffer: GPUBuffer;
|
|
55
|
+
readonly destroyed: boolean;
|
|
56
|
+
usableAsUniform: boolean;
|
|
57
|
+
usableAsStorage: boolean;
|
|
58
|
+
usableAsVertex: boolean;
|
|
59
|
+
usableAsIndex: boolean;
|
|
60
|
+
usableAsIndirect: boolean;
|
|
61
|
+
$usage<T extends [Prettify<InnerValidUsagesFor<TData>>['usage'], ...Prettify<InnerValidUsagesFor<TData>>['usage'][]]>(...usages: T): this & UnionToIntersection<LiteralToUsageType<T[number]>>;
|
|
62
|
+
$addFlags(flags: GPUBufferUsageFlags): this;
|
|
63
|
+
as<T extends ViewUsages<this>>(usage: T): UsageTypeToBufferUsage<TData>[T];
|
|
64
|
+
compileWriter(): void;
|
|
65
|
+
write(data: InferInput<TData>, options?: BufferWriteOptions): void;
|
|
66
|
+
write(data: ArrayBuffer, options?: BufferWriteOptions): void;
|
|
67
|
+
/** @deprecated Use {@link patch} instead. */
|
|
68
|
+
writePartial(data: InferPartial<TData>): void;
|
|
69
|
+
patch(data: InferPatch<TData>): void;
|
|
70
|
+
clear(): void;
|
|
71
|
+
copyFrom(srcBuffer: TgpuBuffer<MemIdentity<TData>>): void;
|
|
72
|
+
read(): Promise<Infer<TData>>;
|
|
73
|
+
destroy(): void;
|
|
74
|
+
toString(): string;
|
|
75
|
+
}
|
|
76
|
+
declare function isBuffer(value: unknown): value is TgpuBuffer<BaseData>;
|
|
77
|
+
declare function isUsableAsVertex<T extends TgpuBuffer<BaseData>>(buffer: T): buffer is T & VertexFlag;
|
|
78
|
+
//#endregion
|
|
79
|
+
export { BufferWriteOptions, IndexFlag, IndirectFlag, TgpuBuffer, Uniform, UniformFlag, ValidUsagesFor, Vertex, VertexFlag, isBuffer, isUsableAsVertex };
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import { $internal } from "../../shared/symbols.js";
|
|
2
|
+
import { getName, setName } from "../../shared/meta.js";
|
|
3
|
+
import { isWgslArray, isWgslData } from "../../data/wgslTypes.js";
|
|
4
|
+
import { isGPUBuffer } from "../../types.js";
|
|
5
|
+
import { alignmentOf } from "../../data/alignmentOf.js";
|
|
6
|
+
import { roundUp } from "../../mathUtils.js";
|
|
7
|
+
import { sizeOf } from "../../data/sizeOf.js";
|
|
8
|
+
import { getCompiledWriter } from "../../data/compiledIO.js";
|
|
9
|
+
import { readData, writeData } from "../../data/dataIO.js";
|
|
10
|
+
import { convertPartialToPatch, getPatchInstructions } from "../../data/partialIO.js";
|
|
11
|
+
import { mutable, readonly, uniform } from "./bufferUsage.js";
|
|
12
|
+
import { BufferReader, BufferWriter, getSystemEndianness } from "typed-binary";
|
|
13
|
+
//#region src/core/buffer/buffer.ts
|
|
14
|
+
const usageToUsageConstructor = {
|
|
15
|
+
uniform,
|
|
16
|
+
mutable,
|
|
17
|
+
readonly
|
|
18
|
+
};
|
|
19
|
+
function INTERNAL_createBuffer(group, typeSchema, initialOrBuffer) {
|
|
20
|
+
if (!isWgslData(typeSchema)) return new TgpuBufferImpl(group, typeSchema, initialOrBuffer, ["storage", "uniform"]);
|
|
21
|
+
return new TgpuBufferImpl(group, typeSchema, initialOrBuffer);
|
|
22
|
+
}
|
|
23
|
+
function isBuffer(value) {
|
|
24
|
+
return value.resourceType === "buffer";
|
|
25
|
+
}
|
|
26
|
+
function isUsableAsVertex(buffer) {
|
|
27
|
+
return !!buffer.usableAsVertex;
|
|
28
|
+
}
|
|
29
|
+
const endianness = getSystemEndianness();
|
|
30
|
+
var TgpuBufferImpl = class {
|
|
31
|
+
[$internal] = true;
|
|
32
|
+
resourceType = "buffer";
|
|
33
|
+
flags = GPUBufferUsage.COPY_DST | GPUBufferUsage.COPY_SRC;
|
|
34
|
+
dataType;
|
|
35
|
+
#device;
|
|
36
|
+
#buffer = null;
|
|
37
|
+
#ownBuffer;
|
|
38
|
+
#destroyed = false;
|
|
39
|
+
#internalBuffer;
|
|
40
|
+
get #hostBuffer() {
|
|
41
|
+
return this.#internalBuffer ??= new ArrayBuffer(sizeOf(this.dataType));
|
|
42
|
+
}
|
|
43
|
+
#mappedRange;
|
|
44
|
+
#initialCallback;
|
|
45
|
+
#disallowedUsages;
|
|
46
|
+
initial;
|
|
47
|
+
usableAsUniform = false;
|
|
48
|
+
usableAsStorage = false;
|
|
49
|
+
usableAsVertex = false;
|
|
50
|
+
usableAsIndex = false;
|
|
51
|
+
usableAsIndirect = false;
|
|
52
|
+
constructor(root, dataType, initialOrBuffer, disallowedUsages) {
|
|
53
|
+
this.dataType = dataType;
|
|
54
|
+
this.#disallowedUsages = disallowedUsages;
|
|
55
|
+
this.#device = root.device;
|
|
56
|
+
if (isGPUBuffer(initialOrBuffer)) {
|
|
57
|
+
this.#ownBuffer = false;
|
|
58
|
+
this.#buffer = initialOrBuffer;
|
|
59
|
+
} else {
|
|
60
|
+
this.#ownBuffer = true;
|
|
61
|
+
if (typeof initialOrBuffer === "function") this.#initialCallback = initialOrBuffer;
|
|
62
|
+
else this.initial = initialOrBuffer;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
get buffer() {
|
|
66
|
+
if (this.#destroyed) throw new Error("This buffer has been destroyed");
|
|
67
|
+
if (!this.#buffer) {
|
|
68
|
+
this.#buffer = this.#device.createBuffer({
|
|
69
|
+
size: sizeOf(this.dataType),
|
|
70
|
+
usage: this.flags,
|
|
71
|
+
mappedAtCreation: !!this.initial || !!this.#initialCallback,
|
|
72
|
+
label: getName(this) ?? "<unnamed>"
|
|
73
|
+
});
|
|
74
|
+
if (this.initial || this.#initialCallback) {
|
|
75
|
+
if (this.#initialCallback) this.#initialCallback(this);
|
|
76
|
+
else if (this.initial) this.#writeToTarget(this.#getMappedRange(), this.initial);
|
|
77
|
+
this.#unmapBuffer();
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return this.#buffer;
|
|
81
|
+
}
|
|
82
|
+
get destroyed() {
|
|
83
|
+
return this.#destroyed;
|
|
84
|
+
}
|
|
85
|
+
get arrayBuffer() {
|
|
86
|
+
if (this.buffer.mapState === "mapped") return this.#getMappedRange();
|
|
87
|
+
return this.#hostBuffer;
|
|
88
|
+
}
|
|
89
|
+
#getMappedRange() {
|
|
90
|
+
if (!this.#buffer || this.#buffer.mapState !== "mapped") throw new Error("Buffer is not mapped.");
|
|
91
|
+
this.#mappedRange ??= this.#buffer.getMappedRange();
|
|
92
|
+
return this.#mappedRange;
|
|
93
|
+
}
|
|
94
|
+
#unmapBuffer() {
|
|
95
|
+
if (!this.#buffer || this.#buffer.mapState !== "mapped") return;
|
|
96
|
+
this.#mappedRange = void 0;
|
|
97
|
+
this.#buffer.unmap();
|
|
98
|
+
}
|
|
99
|
+
$name(label) {
|
|
100
|
+
setName(this, label);
|
|
101
|
+
if (this.#buffer) this.#buffer.label = label;
|
|
102
|
+
return this;
|
|
103
|
+
}
|
|
104
|
+
$usage(...usages) {
|
|
105
|
+
for (const usage of usages) {
|
|
106
|
+
if (this.#disallowedUsages?.includes(usage)) throw new Error(`Buffer of type ${this.dataType.type} cannot be used as ${usage}`);
|
|
107
|
+
this.flags |= usage === "uniform" ? GPUBufferUsage.UNIFORM : 0;
|
|
108
|
+
this.flags |= usage === "storage" ? GPUBufferUsage.STORAGE : 0;
|
|
109
|
+
this.flags |= usage === "vertex" ? GPUBufferUsage.VERTEX : 0;
|
|
110
|
+
this.flags |= usage === "index" ? GPUBufferUsage.INDEX : 0;
|
|
111
|
+
this.flags |= usage === "indirect" ? GPUBufferUsage.INDIRECT : 0;
|
|
112
|
+
this.usableAsUniform = this.usableAsUniform || usage === "uniform";
|
|
113
|
+
this.usableAsStorage = this.usableAsStorage || usage === "storage";
|
|
114
|
+
this.usableAsVertex = this.usableAsVertex || usage === "vertex";
|
|
115
|
+
this.usableAsIndex = this.usableAsIndex || usage === "index";
|
|
116
|
+
this.usableAsIndirect = this.usableAsIndirect || usage === "indirect";
|
|
117
|
+
}
|
|
118
|
+
return this;
|
|
119
|
+
}
|
|
120
|
+
$addFlags(flags) {
|
|
121
|
+
if (!this.#ownBuffer) throw new Error("Cannot add flags to a buffer that is not managed by TypeGPU.");
|
|
122
|
+
if (flags & GPUBufferUsage.MAP_READ) {
|
|
123
|
+
this.flags = GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ;
|
|
124
|
+
return this;
|
|
125
|
+
}
|
|
126
|
+
if (flags & GPUBufferUsage.MAP_WRITE) {
|
|
127
|
+
this.flags = GPUBufferUsage.COPY_SRC | GPUBufferUsage.MAP_WRITE;
|
|
128
|
+
return this;
|
|
129
|
+
}
|
|
130
|
+
this.flags |= flags;
|
|
131
|
+
return this;
|
|
132
|
+
}
|
|
133
|
+
compileWriter() {
|
|
134
|
+
getCompiledWriter(this.dataType);
|
|
135
|
+
}
|
|
136
|
+
#writeToTarget(target, data, options) {
|
|
137
|
+
const startOffset = options?.startOffset ?? 0;
|
|
138
|
+
const endOffset = options?.endOffset ?? target.byteLength;
|
|
139
|
+
if (data instanceof ArrayBuffer || ArrayBuffer.isView(data)) {
|
|
140
|
+
const src = data instanceof ArrayBuffer ? new Uint8Array(data) : new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
141
|
+
const regionSize = endOffset - startOffset;
|
|
142
|
+
if (src.byteLength !== regionSize) console.warn(`Buffer size mismatch: expected ${regionSize} bytes, got ${src.byteLength}. ` + (src.byteLength < regionSize ? "Data truncated." : "Excess ignored."));
|
|
143
|
+
const copyLen = Math.min(src.byteLength, regionSize);
|
|
144
|
+
new Uint8Array(target).set(src.subarray(0, copyLen), startOffset);
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
const dataView = new DataView(target);
|
|
148
|
+
const isLittleEndian = endianness === "little";
|
|
149
|
+
const compiledWriter = getCompiledWriter(this.dataType);
|
|
150
|
+
if (compiledWriter) try {
|
|
151
|
+
compiledWriter(dataView, startOffset, data, isLittleEndian, endOffset);
|
|
152
|
+
return;
|
|
153
|
+
} catch (error) {
|
|
154
|
+
console.error(`Error when using compiled writer for buffer ${getName(this) ?? "<unnamed>"} - this is likely a bug, please submit an issue at https://github.com/software-mansion/TypeGPU/issues\nUsing fallback writer instead.`, error);
|
|
155
|
+
}
|
|
156
|
+
const writer = new BufferWriter(target);
|
|
157
|
+
writer.seekTo(startOffset);
|
|
158
|
+
writeData(writer, this.dataType, data);
|
|
159
|
+
}
|
|
160
|
+
write(data, options) {
|
|
161
|
+
const gpuBuffer = this.buffer;
|
|
162
|
+
const bufferSize = sizeOf(this.dataType);
|
|
163
|
+
const startOffset = options?.startOffset ?? 0;
|
|
164
|
+
let naturalSize = void 0;
|
|
165
|
+
if (isWgslArray(this.dataType) && Array.isArray(data)) naturalSize = data.length * roundUp(sizeOf(this.dataType.elementType), alignmentOf(this.dataType.elementType));
|
|
166
|
+
else if (ArrayBuffer.isView(data) || data instanceof ArrayBuffer) naturalSize = data.byteLength;
|
|
167
|
+
const naturalEndOffset = naturalSize !== void 0 ? Math.min(startOffset + naturalSize, bufferSize) : void 0;
|
|
168
|
+
const size = (options?.endOffset ?? naturalEndOffset ?? bufferSize) - startOffset;
|
|
169
|
+
if (gpuBuffer.mapState === "mapped") {
|
|
170
|
+
const mapped = this.#getMappedRange();
|
|
171
|
+
if (data instanceof ArrayBuffer && data === mapped) return;
|
|
172
|
+
this.#writeToTarget(mapped, data, options);
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
if (!(data instanceof ArrayBuffer && data === this.#hostBuffer)) this.#writeToTarget(this.#hostBuffer, data, options);
|
|
176
|
+
this.#device.queue.writeBuffer(gpuBuffer, startOffset, this.#hostBuffer, startOffset, size);
|
|
177
|
+
}
|
|
178
|
+
/** @deprecated Use {@link patch} instead. */
|
|
179
|
+
writePartial(data) {
|
|
180
|
+
this.#applyInstructions(getPatchInstructions(this.dataType, convertPartialToPatch(this.dataType, data), this.#hostBuffer));
|
|
181
|
+
}
|
|
182
|
+
patch(data) {
|
|
183
|
+
this.#applyInstructions(getPatchInstructions(this.dataType, data, this.#hostBuffer));
|
|
184
|
+
}
|
|
185
|
+
#applyInstructions(instructions) {
|
|
186
|
+
const gpuBuffer = this.buffer;
|
|
187
|
+
if (gpuBuffer.mapState === "mapped") {
|
|
188
|
+
const mappedRange = this.#getMappedRange();
|
|
189
|
+
const mappedView = new Uint8Array(mappedRange);
|
|
190
|
+
for (const { data, gpuOffset } of instructions) mappedView.set(data, gpuOffset);
|
|
191
|
+
} else for (const { data, gpuOffset } of instructions) this.#device.queue.writeBuffer(gpuBuffer, gpuOffset, data);
|
|
192
|
+
}
|
|
193
|
+
clear() {
|
|
194
|
+
const gpuBuffer = this.buffer;
|
|
195
|
+
if (gpuBuffer.mapState === "mapped") {
|
|
196
|
+
new Uint8Array(this.#getMappedRange()).fill(0);
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
const encoder = this.#device.createCommandEncoder();
|
|
200
|
+
encoder.clearBuffer(gpuBuffer);
|
|
201
|
+
this.#device.queue.submit([encoder.finish()]);
|
|
202
|
+
}
|
|
203
|
+
copyFrom(srcBuffer) {
|
|
204
|
+
if (this.buffer.mapState === "mapped") throw new Error("Cannot copy to a mapped buffer.");
|
|
205
|
+
const size = sizeOf(this.dataType);
|
|
206
|
+
const encoder = this.#device.createCommandEncoder();
|
|
207
|
+
encoder.copyBufferToBuffer(srcBuffer.buffer, 0, this.buffer, 0, size);
|
|
208
|
+
this.#device.queue.submit([encoder.finish()]);
|
|
209
|
+
}
|
|
210
|
+
async read() {
|
|
211
|
+
const gpuBuffer = this.buffer;
|
|
212
|
+
if (gpuBuffer.mapState === "mapped") return readData(new BufferReader(this.#getMappedRange()), this.dataType);
|
|
213
|
+
if (gpuBuffer.usage & GPUBufferUsage.MAP_READ) {
|
|
214
|
+
await gpuBuffer.mapAsync(GPUMapMode.READ);
|
|
215
|
+
const res = readData(new BufferReader(this.#getMappedRange()), this.dataType);
|
|
216
|
+
this.#unmapBuffer();
|
|
217
|
+
return res;
|
|
218
|
+
}
|
|
219
|
+
const stagingBuffer = this.#device.createBuffer({
|
|
220
|
+
size: sizeOf(this.dataType),
|
|
221
|
+
usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ
|
|
222
|
+
});
|
|
223
|
+
const commandEncoder = this.#device.createCommandEncoder();
|
|
224
|
+
commandEncoder.copyBufferToBuffer(gpuBuffer, 0, stagingBuffer, 0, sizeOf(this.dataType));
|
|
225
|
+
this.#device.queue.submit([commandEncoder.finish()]);
|
|
226
|
+
await stagingBuffer.mapAsync(GPUMapMode.READ, 0, sizeOf(this.dataType));
|
|
227
|
+
const res = readData(new BufferReader(stagingBuffer.getMappedRange()), this.dataType);
|
|
228
|
+
stagingBuffer.unmap();
|
|
229
|
+
stagingBuffer.destroy();
|
|
230
|
+
return res;
|
|
231
|
+
}
|
|
232
|
+
as(usage) {
|
|
233
|
+
return usageToUsageConstructor[usage]?.(this);
|
|
234
|
+
}
|
|
235
|
+
destroy() {
|
|
236
|
+
if (this.#destroyed) return;
|
|
237
|
+
this.#destroyed = true;
|
|
238
|
+
this.#mappedRange = void 0;
|
|
239
|
+
if (this.#ownBuffer) this.#buffer?.destroy();
|
|
240
|
+
}
|
|
241
|
+
toString() {
|
|
242
|
+
return `buffer:${getName(this) ?? "<unnamed>"}`;
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
//#endregion
|
|
246
|
+
export { INTERNAL_createBuffer, isBuffer, isUsableAsVertex };
|