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
package/std/atomic.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { Void, isAtomic } from "../data/wgslTypes.js";
|
|
2
|
+
import { stitch } from "../core/resolve/stitch.js";
|
|
3
|
+
import { safeStringify } from "../shared/stringify.js";
|
|
4
|
+
import { i32, u32 } from "../data/numeric.js";
|
|
5
|
+
import { dualImpl } from "../core/function/dualImpl.js";
|
|
6
|
+
//#region src/std/atomic.ts
|
|
7
|
+
const workgroupBarrier = dualImpl({
|
|
8
|
+
name: "workgroupBarrier",
|
|
9
|
+
normalImpl: "workgroupBarrier is a no-op outside of CODEGEN mode.",
|
|
10
|
+
signature: {
|
|
11
|
+
argTypes: [],
|
|
12
|
+
returnType: Void
|
|
13
|
+
},
|
|
14
|
+
codegenImpl: () => "workgroupBarrier()"
|
|
15
|
+
});
|
|
16
|
+
const storageBarrier = dualImpl({
|
|
17
|
+
name: "storageBarrier",
|
|
18
|
+
normalImpl: "storageBarrier is a no-op outside of CODEGEN mode.",
|
|
19
|
+
signature: {
|
|
20
|
+
argTypes: [],
|
|
21
|
+
returnType: Void
|
|
22
|
+
},
|
|
23
|
+
codegenImpl: () => "storageBarrier()"
|
|
24
|
+
});
|
|
25
|
+
const textureBarrier = dualImpl({
|
|
26
|
+
name: "textureBarrier",
|
|
27
|
+
normalImpl: "textureBarrier is a no-op outside of CODEGEN mode.",
|
|
28
|
+
signature: {
|
|
29
|
+
argTypes: [],
|
|
30
|
+
returnType: Void
|
|
31
|
+
},
|
|
32
|
+
codegenImpl: () => "textureBarrier()"
|
|
33
|
+
});
|
|
34
|
+
const atomicNormalError = "Atomic operations are not supported outside of CODEGEN mode.";
|
|
35
|
+
const atomicLoad = dualImpl({
|
|
36
|
+
name: "atomicLoad",
|
|
37
|
+
normalImpl: atomicNormalError,
|
|
38
|
+
signature: (a) => {
|
|
39
|
+
if (!isAtomic(a)) throw new Error(`Invalid atomic type: ${safeStringify(a)}`);
|
|
40
|
+
return {
|
|
41
|
+
argTypes: [a],
|
|
42
|
+
returnType: a.inner
|
|
43
|
+
};
|
|
44
|
+
},
|
|
45
|
+
codegenImpl: (_ctx, [a]) => stitch`atomicLoad(&${a})`
|
|
46
|
+
});
|
|
47
|
+
const atomicActionSignature = (a) => {
|
|
48
|
+
if (!isAtomic(a)) throw new Error(`Invalid atomic type: ${safeStringify(a)}`);
|
|
49
|
+
return {
|
|
50
|
+
argTypes: [a, a.inner.type === "u32" ? u32 : i32],
|
|
51
|
+
returnType: Void
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
const atomicOpSignature = (a) => {
|
|
55
|
+
if (!isAtomic(a)) throw new Error(`Invalid atomic type: ${safeStringify(a)}`);
|
|
56
|
+
const paramType = a.inner.type === "u32" ? u32 : i32;
|
|
57
|
+
return {
|
|
58
|
+
argTypes: [a, paramType],
|
|
59
|
+
returnType: paramType
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
const atomicStore = dualImpl({
|
|
63
|
+
name: "atomicStore",
|
|
64
|
+
normalImpl: atomicNormalError,
|
|
65
|
+
signature: atomicActionSignature,
|
|
66
|
+
codegenImpl: (_ctx, [a, value]) => stitch`atomicStore(&${a}, ${value})`
|
|
67
|
+
});
|
|
68
|
+
const atomicAdd = dualImpl({
|
|
69
|
+
name: "atomicAdd",
|
|
70
|
+
normalImpl: atomicNormalError,
|
|
71
|
+
signature: atomicOpSignature,
|
|
72
|
+
codegenImpl: (_ctx, [a, value]) => stitch`atomicAdd(&${a}, ${value})`
|
|
73
|
+
});
|
|
74
|
+
const atomicSub = dualImpl({
|
|
75
|
+
name: "atomicSub",
|
|
76
|
+
normalImpl: atomicNormalError,
|
|
77
|
+
signature: atomicOpSignature,
|
|
78
|
+
codegenImpl: (_ctx, [a, value]) => stitch`atomicSub(&${a}, ${value})`
|
|
79
|
+
});
|
|
80
|
+
const atomicMax = dualImpl({
|
|
81
|
+
name: "atomicMax",
|
|
82
|
+
normalImpl: atomicNormalError,
|
|
83
|
+
signature: atomicOpSignature,
|
|
84
|
+
codegenImpl: (_ctx, [a, value]) => stitch`atomicMax(&${a}, ${value})`
|
|
85
|
+
});
|
|
86
|
+
const atomicMin = dualImpl({
|
|
87
|
+
name: "atomicMin",
|
|
88
|
+
normalImpl: atomicNormalError,
|
|
89
|
+
signature: atomicOpSignature,
|
|
90
|
+
codegenImpl: (_ctx, [a, value]) => stitch`atomicMin(&${a}, ${value})`
|
|
91
|
+
});
|
|
92
|
+
const atomicAnd = dualImpl({
|
|
93
|
+
name: "atomicAnd",
|
|
94
|
+
normalImpl: atomicNormalError,
|
|
95
|
+
signature: atomicOpSignature,
|
|
96
|
+
codegenImpl: (_ctx, [a, value]) => stitch`atomicAnd(&${a}, ${value})`
|
|
97
|
+
});
|
|
98
|
+
const atomicOr = dualImpl({
|
|
99
|
+
name: "atomicOr",
|
|
100
|
+
normalImpl: atomicNormalError,
|
|
101
|
+
signature: atomicOpSignature,
|
|
102
|
+
codegenImpl: (_ctx, [a, value]) => stitch`atomicOr(&${a}, ${value})`
|
|
103
|
+
});
|
|
104
|
+
const atomicXor = dualImpl({
|
|
105
|
+
name: "atomicXor",
|
|
106
|
+
normalImpl: atomicNormalError,
|
|
107
|
+
signature: atomicOpSignature,
|
|
108
|
+
codegenImpl: (_ctx, [a, value]) => stitch`atomicXor(&${a}, ${value})`
|
|
109
|
+
});
|
|
110
|
+
//#endregion
|
|
111
|
+
export { atomicAdd, atomicAnd, atomicLoad, atomicMax, atomicMin, atomicOr, atomicStore, atomicSub, atomicXor, storageBarrier, textureBarrier, workgroupBarrier };
|
package/std/bitcast.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { DualFn } from "../types.js";
|
|
2
|
+
import { v2f, v2i, v2u, v3f, v3i, v3u, v4f, v4i, v4u } from "../data/wgslTypes.js";
|
|
3
|
+
|
|
4
|
+
//#region src/std/bitcast.d.ts
|
|
5
|
+
type BitcastU32toF32Overload = ((value: number) => number) & ((value: v2u) => v2f) & ((value: v3u) => v3f) & ((value: v4u) => v4f);
|
|
6
|
+
declare const bitcastU32toF32: DualFn;
|
|
7
|
+
type BitcastU32toI32Overload = ((value: number) => number) & ((value: v2u) => v2i) & ((value: v3u) => v3i) & ((value: v4u) => v4i);
|
|
8
|
+
declare const bitcastU32toI32: DualFn;
|
|
9
|
+
//#endregion
|
|
10
|
+
export { bitcastU32toF32, bitcastU32toI32 };
|
package/std/bitcast.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { isVec } from "../data/wgslTypes.js";
|
|
2
|
+
import { stitch } from "../core/resolve/stitch.js";
|
|
3
|
+
import { unify } from "../tgsl/conversion.js";
|
|
4
|
+
import { f32, i32, u32 } from "../data/numeric.js";
|
|
5
|
+
import { vec2f, vec2i, vec3f, vec3i, vec4f, vec4i } from "../data/vector.js";
|
|
6
|
+
import { dualImpl } from "../core/function/dualImpl.js";
|
|
7
|
+
import { bitcastU32toF32Impl, bitcastU32toI32Impl } from "../data/numberOps.js";
|
|
8
|
+
import { VectorOps } from "../data/vectorOps.js";
|
|
9
|
+
//#region src/std/bitcast.ts
|
|
10
|
+
const bitcastU32toF32 = dualImpl({
|
|
11
|
+
name: "bitcastU32toF32",
|
|
12
|
+
normalImpl: ((value) => {
|
|
13
|
+
if (typeof value === "number") return bitcastU32toF32Impl(value);
|
|
14
|
+
return VectorOps.bitcastU32toF32[value.kind](value);
|
|
15
|
+
}),
|
|
16
|
+
codegenImpl: (_ctx, [n]) => stitch`bitcast<f32>(${n})`,
|
|
17
|
+
signature: (...arg) => {
|
|
18
|
+
const uargs = unify(arg, [u32]) ?? arg;
|
|
19
|
+
return {
|
|
20
|
+
argTypes: uargs,
|
|
21
|
+
returnType: isVec(uargs[0]) ? uargs[0].type === "vec2u" ? vec2f : uargs[0].type === "vec3u" ? vec3f : vec4f : f32
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
const bitcastU32toI32 = dualImpl({
|
|
26
|
+
name: "bitcastU32toI32",
|
|
27
|
+
normalImpl: ((value) => {
|
|
28
|
+
if (typeof value === "number") return bitcastU32toI32Impl(value);
|
|
29
|
+
return VectorOps.bitcastU32toI32[value.kind](value);
|
|
30
|
+
}),
|
|
31
|
+
codegenImpl: (_ctx, [n]) => stitch`bitcast<i32>(${n})`,
|
|
32
|
+
signature: (...arg) => {
|
|
33
|
+
const uargs = unify(arg, [u32]) ?? arg;
|
|
34
|
+
return {
|
|
35
|
+
argTypes: uargs,
|
|
36
|
+
returnType: isVec(uargs[0]) ? uargs[0].type === "vec2u" ? vec2i : uargs[0].type === "vec3u" ? vec3i : vec4i : i32
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
//#endregion
|
|
41
|
+
export { bitcastU32toF32, bitcastU32toI32 };
|
package/std/boolean.d.ts
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { DualFn } from "../types.js";
|
|
2
|
+
import { AnyBooleanVecInstance, AnyFloatVecInstance, AnyNumericVecInstance, AnyVec2Instance, AnyVec3Instance, AnyVecInstance, v2b, v3b, v4b } from "../data/wgslTypes.js";
|
|
3
|
+
|
|
4
|
+
//#region src/std/boolean.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Checks whether `lhs == rhs` on all components.
|
|
7
|
+
* Equivalent to `all(eq(lhs, rhs))`.
|
|
8
|
+
* @example
|
|
9
|
+
* allEq(vec2f(0.0, 1.0), vec2f(0.0, 2.0)) // returns false
|
|
10
|
+
* allEq(vec3u(0, 1, 2), vec3u(0, 1, 2)) // returns true
|
|
11
|
+
*/
|
|
12
|
+
declare const allEq: DualFn;
|
|
13
|
+
/**
|
|
14
|
+
* Checks **component-wise** whether `lhs == rhs`.
|
|
15
|
+
* This function does **not** return `bool`, for that use-case, wrap the result in `all`, or use `allEq`.
|
|
16
|
+
* @example
|
|
17
|
+
* eq(vec2f(0.0, 1.0), vec2f(0.0, 2.0)) // returns vec2b(true, false)
|
|
18
|
+
* eq(vec3u(0, 1, 2), vec3u(2, 1, 0)) // returns vec3b(false, true, false)
|
|
19
|
+
* all(eq(vec4i(4, 3, 2, 1), vec4i(4, 3, 2, 1))) // returns true
|
|
20
|
+
* allEq(vec4i(4, 3, 2, 1), vec4i(4, 3, 2, 1)) // returns true
|
|
21
|
+
*/
|
|
22
|
+
declare const eq: DualFn;
|
|
23
|
+
/**
|
|
24
|
+
* Checks **component-wise** whether `lhs != rhs`.
|
|
25
|
+
* This function does **not** return `bool`, for that use-case, wrap the result in `any`.
|
|
26
|
+
* @example
|
|
27
|
+
* ne(vec2f(0.0, 1.0), vec2f(0.0, 2.0)) // returns vec2b(false, true)
|
|
28
|
+
* ne(vec3u(0, 1, 2), vec3u(2, 1, 0)) // returns vec3b(true, false, true)
|
|
29
|
+
* any(ne(vec4i(4, 3, 2, 1), vec4i(4, 2, 2, 1))) // returns true
|
|
30
|
+
*/
|
|
31
|
+
declare const ne: DualFn;
|
|
32
|
+
/**
|
|
33
|
+
* Checks **component-wise** whether `lhs < rhs`.
|
|
34
|
+
* This function does **not** return `bool`, for that use-case, wrap the result in `all`.
|
|
35
|
+
* @example
|
|
36
|
+
* lt(vec2f(0.0, 0.0), vec2f(0.0, 1.0)) // returns vec2b(false, true)
|
|
37
|
+
* lt(vec3u(0, 1, 2), vec3u(2, 1, 0)) // returns vec3b(true, false, false)
|
|
38
|
+
* all(lt(vec4i(1, 2, 3, 4), vec4i(2, 3, 4, 5))) // returns true
|
|
39
|
+
*/
|
|
40
|
+
declare const lt: DualFn;
|
|
41
|
+
/**
|
|
42
|
+
* Checks **component-wise** whether `lhs <= rhs`.
|
|
43
|
+
* This function does **not** return `bool`, for that use-case, wrap the result in `all`.
|
|
44
|
+
* @example
|
|
45
|
+
* le(vec2f(0.0, 0.0), vec2f(0.0, 1.0)) // returns vec2b(true, true)
|
|
46
|
+
* le(vec3u(0, 1, 2), vec3u(2, 1, 0)) // returns vec3b(true, true, false)
|
|
47
|
+
* all(le(vec4i(1, 2, 3, 4), vec4i(2, 3, 3, 5))) // returns true
|
|
48
|
+
*/
|
|
49
|
+
declare const le: DualFn;
|
|
50
|
+
/**
|
|
51
|
+
* Checks **component-wise** whether `lhs > rhs`.
|
|
52
|
+
* This function does **not** return `bool`, for that use-case, wrap the result in `all`.
|
|
53
|
+
* @example
|
|
54
|
+
* gt(vec2f(0.0, 0.0), vec2f(0.0, 1.0)) // returns vec2b(false, false)
|
|
55
|
+
* gt(vec3u(0, 1, 2), vec3u(2, 1, 0)) // returns vec3b(false, false, true)
|
|
56
|
+
* all(gt(vec4i(2, 3, 4, 5), vec4i(1, 2, 3, 4))) // returns true
|
|
57
|
+
*/
|
|
58
|
+
declare const gt: DualFn;
|
|
59
|
+
/**
|
|
60
|
+
* Checks **component-wise** whether `lhs >= rhs`.
|
|
61
|
+
* This function does **not** return `bool`, for that use-case, wrap the result in `all`.
|
|
62
|
+
* @example
|
|
63
|
+
* ge(vec2f(0.0, 0.0), vec2f(0.0, 1.0)) // returns vec2b(true, false)
|
|
64
|
+
* ge(vec3u(0, 1, 2), vec3u(2, 1, 0)) // returns vec3b(false, true, true)
|
|
65
|
+
* all(ge(vec4i(2, 2, 4, 5), vec4i(1, 2, 3, 4))) // returns true
|
|
66
|
+
*/
|
|
67
|
+
declare const ge: DualFn;
|
|
68
|
+
type VecInstanceToBooleanVecInstance<T extends AnyVecInstance> = T extends AnyVec2Instance ? v2b : T extends AnyVec3Instance ? v3b : v4b;
|
|
69
|
+
declare function cpuNot(value: boolean): boolean;
|
|
70
|
+
declare function cpuNot(value: number): boolean;
|
|
71
|
+
declare function cpuNot<T extends AnyVecInstance>(value: T): VecInstanceToBooleanVecInstance<T>;
|
|
72
|
+
declare function cpuNot(value: unknown): boolean;
|
|
73
|
+
/**
|
|
74
|
+
* Returns the logical negation of the given value.
|
|
75
|
+
* For scalars (bool, number), returns `!value`.
|
|
76
|
+
* For boolean vectors, returns **component-wise** `!value`.
|
|
77
|
+
* For numeric vectors, returns a boolean vector with component-wise truthiness negation.
|
|
78
|
+
* For all other types, returns the truthiness negation (in WGSL, this applies only if the value is known at compile-time).
|
|
79
|
+
* @example
|
|
80
|
+
* not(true) // returns false
|
|
81
|
+
* not(-1) // returns false
|
|
82
|
+
* not(0) // returns true
|
|
83
|
+
* not(vec3b(true, true, false)) // returns vec3b(false, false, true)
|
|
84
|
+
* not(vec3f(1.0, 0.0, -1.0)) // returns vec3b(false, true, false)
|
|
85
|
+
* not({a: 1882}) // returns false
|
|
86
|
+
* not(NaN) // returns false **as in WGSL**
|
|
87
|
+
*/
|
|
88
|
+
declare const not: DualFn;
|
|
89
|
+
/**
|
|
90
|
+
* Returns **component-wise** logical `or` result.
|
|
91
|
+
* @example
|
|
92
|
+
* or(vec2b(false, true), vec2b(false, false)) // returns vec2b(false, true)
|
|
93
|
+
* or(vec3b(true, true, false), vec3b(false, true, false)) // returns vec3b(true, true, false)
|
|
94
|
+
*/
|
|
95
|
+
declare const or: DualFn;
|
|
96
|
+
/**
|
|
97
|
+
* Returns **component-wise** logical `and` result.
|
|
98
|
+
* @example
|
|
99
|
+
* and(vec2b(false, true), vec2b(true, true)) // returns vec2b(false, true)
|
|
100
|
+
* and(vec3b(true, true, false), vec3b(false, true, false)) // returns vec3b(false, true, false)
|
|
101
|
+
*/
|
|
102
|
+
declare const and: DualFn;
|
|
103
|
+
/**
|
|
104
|
+
* Returns `true` if each component of `value` is true.
|
|
105
|
+
* @example
|
|
106
|
+
* all(vec2b(false, true)) // returns false
|
|
107
|
+
* all(vec3b(true, true, true)) // returns true
|
|
108
|
+
*/
|
|
109
|
+
declare const all: DualFn;
|
|
110
|
+
/**
|
|
111
|
+
* Returns `true` if any component of `value` is true.
|
|
112
|
+
* @example
|
|
113
|
+
* any(vec2b(false, true)) // returns true
|
|
114
|
+
* any(vec3b(false, false, false)) // returns false
|
|
115
|
+
*/
|
|
116
|
+
declare const any: DualFn;
|
|
117
|
+
/**
|
|
118
|
+
* Checks whether the given elements differ by at most the `precision` value.
|
|
119
|
+
* Checks all elements of `lhs` and `rhs` if arguments are vectors.
|
|
120
|
+
* @example
|
|
121
|
+
* isCloseTo(0, 0.1) // returns false
|
|
122
|
+
* isCloseTo(vec3f(0, 0, 0), vec3f(0.002, -0.009, 0)) // returns true
|
|
123
|
+
*
|
|
124
|
+
* @param {number} precision argument that specifies the maximum allowed difference, 0.01 by default.
|
|
125
|
+
*/
|
|
126
|
+
declare const isCloseTo: DualFn;
|
|
127
|
+
declare function cpuSelect(f: boolean, t: boolean, cond: boolean): boolean;
|
|
128
|
+
declare function cpuSelect(f: number, t: number, cond: boolean): number;
|
|
129
|
+
declare function cpuSelect<T extends AnyVecInstance>(f: T, t: T, cond: boolean | (T extends AnyVec2Instance ? v2b : T extends AnyVec3Instance ? v3b : v4b)): T;
|
|
130
|
+
/**
|
|
131
|
+
* Returns `t` if `cond` is `true`, and `f` otherwise.
|
|
132
|
+
* Component-wise if `cond` is a vector.
|
|
133
|
+
* @example
|
|
134
|
+
* select(1, 2, false) // returns 1
|
|
135
|
+
* select(1, 2, true) // returns 2
|
|
136
|
+
* select(vec2i(1, 2), vec2i(3, 4), true) // returns vec2i(3, 4)
|
|
137
|
+
* select(vec2i(1, 2), vec2i(3, 4), vec2b(false, true)) // returns vec2i(1, 4)
|
|
138
|
+
*/
|
|
139
|
+
declare const select: DualFn;
|
|
140
|
+
//#endregion
|
|
141
|
+
export { all, allEq, and, any, eq, ge, gt, isCloseTo, le, lt, ne, not, or, select };
|
package/std/boolean.js
ADDED
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
import { isBool, isNumericSchema, isVec, isVecBool, isVecInstance } from "../data/wgslTypes.js";
|
|
2
|
+
import { isSnippetNumeric, snip } from "../data/snippet.js";
|
|
3
|
+
import { stitch } from "../core/resolve/stitch.js";
|
|
4
|
+
import { unify } from "../tgsl/conversion.js";
|
|
5
|
+
import { bool, f32 } from "../data/numeric.js";
|
|
6
|
+
import { vec2b, vec3b, vec4b } from "../data/vector.js";
|
|
7
|
+
import { dualImpl } from "../core/function/dualImpl.js";
|
|
8
|
+
import { VectorOps } from "../data/vectorOps.js";
|
|
9
|
+
import { sub } from "./operators.js";
|
|
10
|
+
//#region src/std/boolean.ts
|
|
11
|
+
function correspondingBooleanVectorSchema(dataType) {
|
|
12
|
+
if (dataType.type.includes("2")) return vec2b;
|
|
13
|
+
if (dataType.type.includes("3")) return vec3b;
|
|
14
|
+
return vec4b;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Checks whether `lhs == rhs` on all components.
|
|
18
|
+
* Equivalent to `all(eq(lhs, rhs))`.
|
|
19
|
+
* @example
|
|
20
|
+
* allEq(vec2f(0.0, 1.0), vec2f(0.0, 2.0)) // returns false
|
|
21
|
+
* allEq(vec3u(0, 1, 2), vec3u(0, 1, 2)) // returns true
|
|
22
|
+
*/
|
|
23
|
+
const allEq = dualImpl({
|
|
24
|
+
name: "allEq",
|
|
25
|
+
signature: (...argTypes) => ({
|
|
26
|
+
argTypes,
|
|
27
|
+
returnType: bool
|
|
28
|
+
}),
|
|
29
|
+
normalImpl: (lhs, rhs) => cpuAll(cpuEq(lhs, rhs)),
|
|
30
|
+
codegenImpl: (_ctx, [lhs, rhs]) => stitch`all(${lhs} == ${rhs})`
|
|
31
|
+
});
|
|
32
|
+
const cpuEq = (lhs, rhs) => VectorOps.eq[lhs.kind](lhs, rhs);
|
|
33
|
+
/**
|
|
34
|
+
* Checks **component-wise** whether `lhs == rhs`.
|
|
35
|
+
* This function does **not** return `bool`, for that use-case, wrap the result in `all`, or use `allEq`.
|
|
36
|
+
* @example
|
|
37
|
+
* eq(vec2f(0.0, 1.0), vec2f(0.0, 2.0)) // returns vec2b(true, false)
|
|
38
|
+
* eq(vec3u(0, 1, 2), vec3u(2, 1, 0)) // returns vec3b(false, true, false)
|
|
39
|
+
* all(eq(vec4i(4, 3, 2, 1), vec4i(4, 3, 2, 1))) // returns true
|
|
40
|
+
* allEq(vec4i(4, 3, 2, 1), vec4i(4, 3, 2, 1)) // returns true
|
|
41
|
+
*/
|
|
42
|
+
const eq = dualImpl({
|
|
43
|
+
name: "eq",
|
|
44
|
+
signature: (...argTypes) => ({
|
|
45
|
+
argTypes,
|
|
46
|
+
returnType: correspondingBooleanVectorSchema(argTypes[0])
|
|
47
|
+
}),
|
|
48
|
+
normalImpl: cpuEq,
|
|
49
|
+
codegenImpl: (_ctx, [lhs, rhs]) => stitch`(${lhs} == ${rhs})`
|
|
50
|
+
});
|
|
51
|
+
/**
|
|
52
|
+
* Checks **component-wise** whether `lhs != rhs`.
|
|
53
|
+
* This function does **not** return `bool`, for that use-case, wrap the result in `any`.
|
|
54
|
+
* @example
|
|
55
|
+
* ne(vec2f(0.0, 1.0), vec2f(0.0, 2.0)) // returns vec2b(false, true)
|
|
56
|
+
* ne(vec3u(0, 1, 2), vec3u(2, 1, 0)) // returns vec3b(true, false, true)
|
|
57
|
+
* any(ne(vec4i(4, 3, 2, 1), vec4i(4, 2, 2, 1))) // returns true
|
|
58
|
+
*/
|
|
59
|
+
const ne = dualImpl({
|
|
60
|
+
name: "ne",
|
|
61
|
+
signature: (...argTypes) => ({
|
|
62
|
+
argTypes,
|
|
63
|
+
returnType: correspondingBooleanVectorSchema(argTypes[0])
|
|
64
|
+
}),
|
|
65
|
+
normalImpl: (lhs, rhs) => cpuNot(cpuEq(lhs, rhs)),
|
|
66
|
+
codegenImpl: (_ctx, [lhs, rhs]) => stitch`(${lhs} != ${rhs})`
|
|
67
|
+
});
|
|
68
|
+
const cpuLt = (lhs, rhs) => VectorOps.lt[lhs.kind](lhs, rhs);
|
|
69
|
+
/**
|
|
70
|
+
* Checks **component-wise** whether `lhs < rhs`.
|
|
71
|
+
* This function does **not** return `bool`, for that use-case, wrap the result in `all`.
|
|
72
|
+
* @example
|
|
73
|
+
* lt(vec2f(0.0, 0.0), vec2f(0.0, 1.0)) // returns vec2b(false, true)
|
|
74
|
+
* lt(vec3u(0, 1, 2), vec3u(2, 1, 0)) // returns vec3b(true, false, false)
|
|
75
|
+
* all(lt(vec4i(1, 2, 3, 4), vec4i(2, 3, 4, 5))) // returns true
|
|
76
|
+
*/
|
|
77
|
+
const lt = dualImpl({
|
|
78
|
+
name: "lt",
|
|
79
|
+
signature: (...argTypes) => ({
|
|
80
|
+
argTypes,
|
|
81
|
+
returnType: correspondingBooleanVectorSchema(argTypes[0])
|
|
82
|
+
}),
|
|
83
|
+
normalImpl: cpuLt,
|
|
84
|
+
codegenImpl: (_ctx, [lhs, rhs]) => stitch`(${lhs} < ${rhs})`
|
|
85
|
+
});
|
|
86
|
+
/**
|
|
87
|
+
* Checks **component-wise** whether `lhs <= rhs`.
|
|
88
|
+
* This function does **not** return `bool`, for that use-case, wrap the result in `all`.
|
|
89
|
+
* @example
|
|
90
|
+
* le(vec2f(0.0, 0.0), vec2f(0.0, 1.0)) // returns vec2b(true, true)
|
|
91
|
+
* le(vec3u(0, 1, 2), vec3u(2, 1, 0)) // returns vec3b(true, true, false)
|
|
92
|
+
* all(le(vec4i(1, 2, 3, 4), vec4i(2, 3, 3, 5))) // returns true
|
|
93
|
+
*/
|
|
94
|
+
const le = dualImpl({
|
|
95
|
+
name: "le",
|
|
96
|
+
signature: (...argTypes) => ({
|
|
97
|
+
argTypes,
|
|
98
|
+
returnType: correspondingBooleanVectorSchema(argTypes[0])
|
|
99
|
+
}),
|
|
100
|
+
normalImpl: (lhs, rhs) => cpuOr(cpuLt(lhs, rhs), cpuEq(lhs, rhs)),
|
|
101
|
+
codegenImpl: (_ctx, [lhs, rhs]) => stitch`(${lhs} <= ${rhs})`
|
|
102
|
+
});
|
|
103
|
+
/**
|
|
104
|
+
* Checks **component-wise** whether `lhs > rhs`.
|
|
105
|
+
* This function does **not** return `bool`, for that use-case, wrap the result in `all`.
|
|
106
|
+
* @example
|
|
107
|
+
* gt(vec2f(0.0, 0.0), vec2f(0.0, 1.0)) // returns vec2b(false, false)
|
|
108
|
+
* gt(vec3u(0, 1, 2), vec3u(2, 1, 0)) // returns vec3b(false, false, true)
|
|
109
|
+
* all(gt(vec4i(2, 3, 4, 5), vec4i(1, 2, 3, 4))) // returns true
|
|
110
|
+
*/
|
|
111
|
+
const gt = dualImpl({
|
|
112
|
+
name: "gt",
|
|
113
|
+
signature: (...argTypes) => ({
|
|
114
|
+
argTypes,
|
|
115
|
+
returnType: correspondingBooleanVectorSchema(argTypes[0])
|
|
116
|
+
}),
|
|
117
|
+
normalImpl: (lhs, rhs) => cpuAnd(cpuNot(cpuLt(lhs, rhs)), cpuNot(cpuEq(lhs, rhs))),
|
|
118
|
+
codegenImpl: (_ctx, [lhs, rhs]) => stitch`(${lhs} > ${rhs})`
|
|
119
|
+
});
|
|
120
|
+
/**
|
|
121
|
+
* Checks **component-wise** whether `lhs >= rhs`.
|
|
122
|
+
* This function does **not** return `bool`, for that use-case, wrap the result in `all`.
|
|
123
|
+
* @example
|
|
124
|
+
* ge(vec2f(0.0, 0.0), vec2f(0.0, 1.0)) // returns vec2b(true, false)
|
|
125
|
+
* ge(vec3u(0, 1, 2), vec3u(2, 1, 0)) // returns vec3b(false, true, true)
|
|
126
|
+
* all(ge(vec4i(2, 2, 4, 5), vec4i(1, 2, 3, 4))) // returns true
|
|
127
|
+
*/
|
|
128
|
+
const ge = dualImpl({
|
|
129
|
+
name: "ge",
|
|
130
|
+
signature: (...argTypes) => ({
|
|
131
|
+
argTypes,
|
|
132
|
+
returnType: correspondingBooleanVectorSchema(argTypes[0])
|
|
133
|
+
}),
|
|
134
|
+
normalImpl: (lhs, rhs) => cpuNot(cpuLt(lhs, rhs)),
|
|
135
|
+
codegenImpl: (_ctx, [lhs, rhs]) => stitch`(${lhs} >= ${rhs})`
|
|
136
|
+
});
|
|
137
|
+
function cpuNot(value) {
|
|
138
|
+
if (typeof value === "number" && isNaN(value)) return false;
|
|
139
|
+
if (isVecInstance(value)) {
|
|
140
|
+
if (value.length === 2) return vec2b(cpuNot(value.x), cpuNot(value.y));
|
|
141
|
+
if (value.length === 3) return vec3b(cpuNot(value.x), cpuNot(value.y), cpuNot(value.z));
|
|
142
|
+
if (value.length === 4) return vec4b(cpuNot(value.x), cpuNot(value.y), cpuNot(value.z), cpuNot(value.w));
|
|
143
|
+
}
|
|
144
|
+
return !value;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Returns the logical negation of the given value.
|
|
148
|
+
* For scalars (bool, number), returns `!value`.
|
|
149
|
+
* For boolean vectors, returns **component-wise** `!value`.
|
|
150
|
+
* For numeric vectors, returns a boolean vector with component-wise truthiness negation.
|
|
151
|
+
* For all other types, returns the truthiness negation (in WGSL, this applies only if the value is known at compile-time).
|
|
152
|
+
* @example
|
|
153
|
+
* not(true) // returns false
|
|
154
|
+
* not(-1) // returns false
|
|
155
|
+
* not(0) // returns true
|
|
156
|
+
* not(vec3b(true, true, false)) // returns vec3b(false, false, true)
|
|
157
|
+
* not(vec3f(1.0, 0.0, -1.0)) // returns vec3b(false, true, false)
|
|
158
|
+
* not({a: 1882}) // returns false
|
|
159
|
+
* not(NaN) // returns false **as in WGSL**
|
|
160
|
+
*/
|
|
161
|
+
const not = dualImpl({
|
|
162
|
+
name: "not",
|
|
163
|
+
signature: (arg) => {
|
|
164
|
+
const returnType = isVec(arg) ? correspondingBooleanVectorSchema(arg) : bool;
|
|
165
|
+
return {
|
|
166
|
+
argTypes: [arg],
|
|
167
|
+
returnType
|
|
168
|
+
};
|
|
169
|
+
},
|
|
170
|
+
normalImpl: cpuNot,
|
|
171
|
+
codegenImpl: (_ctx, [arg]) => {
|
|
172
|
+
const { dataType } = arg;
|
|
173
|
+
if (isBool(dataType)) return stitch`!${arg}`;
|
|
174
|
+
if (isNumericSchema(dataType)) return stitch`!bool(${arg})`;
|
|
175
|
+
if (isVecBool(dataType)) return stitch`!(${arg})`;
|
|
176
|
+
if (isVec(dataType)) return stitch`!(${`vec${dataType.componentCount}<bool>`}(${arg}))`;
|
|
177
|
+
return "false";
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
const cpuOr = (lhs, rhs) => VectorOps.or[lhs.kind](lhs, rhs);
|
|
181
|
+
/**
|
|
182
|
+
* Returns **component-wise** logical `or` result.
|
|
183
|
+
* @example
|
|
184
|
+
* or(vec2b(false, true), vec2b(false, false)) // returns vec2b(false, true)
|
|
185
|
+
* or(vec3b(true, true, false), vec3b(false, true, false)) // returns vec3b(true, true, false)
|
|
186
|
+
*/
|
|
187
|
+
const or = dualImpl({
|
|
188
|
+
name: "or",
|
|
189
|
+
signature: (...argTypes) => ({
|
|
190
|
+
argTypes,
|
|
191
|
+
returnType: argTypes[0]
|
|
192
|
+
}),
|
|
193
|
+
normalImpl: cpuOr,
|
|
194
|
+
codegenImpl: (_ctx, [lhs, rhs]) => stitch`(${lhs} | ${rhs})`
|
|
195
|
+
});
|
|
196
|
+
const cpuAnd = (lhs, rhs) => cpuNot(cpuOr(cpuNot(lhs), cpuNot(rhs)));
|
|
197
|
+
/**
|
|
198
|
+
* Returns **component-wise** logical `and` result.
|
|
199
|
+
* @example
|
|
200
|
+
* and(vec2b(false, true), vec2b(true, true)) // returns vec2b(false, true)
|
|
201
|
+
* and(vec3b(true, true, false), vec3b(false, true, false)) // returns vec3b(false, true, false)
|
|
202
|
+
*/
|
|
203
|
+
const and = dualImpl({
|
|
204
|
+
name: "and",
|
|
205
|
+
signature: (...argTypes) => ({
|
|
206
|
+
argTypes,
|
|
207
|
+
returnType: argTypes[0]
|
|
208
|
+
}),
|
|
209
|
+
normalImpl: cpuAnd,
|
|
210
|
+
codegenImpl: (_ctx, [lhs, rhs]) => stitch`(${lhs} & ${rhs})`
|
|
211
|
+
});
|
|
212
|
+
const cpuAll = (value) => VectorOps.all[value.kind](value);
|
|
213
|
+
/**
|
|
214
|
+
* Returns `true` if each component of `value` is true.
|
|
215
|
+
* @example
|
|
216
|
+
* all(vec2b(false, true)) // returns false
|
|
217
|
+
* all(vec3b(true, true, true)) // returns true
|
|
218
|
+
*/
|
|
219
|
+
const all = dualImpl({
|
|
220
|
+
name: "all",
|
|
221
|
+
signature: (...argTypes) => ({
|
|
222
|
+
argTypes,
|
|
223
|
+
returnType: bool
|
|
224
|
+
}),
|
|
225
|
+
normalImpl: cpuAll,
|
|
226
|
+
codegenImpl: (_ctx, [value]) => stitch`all(${value})`
|
|
227
|
+
});
|
|
228
|
+
/**
|
|
229
|
+
* Returns `true` if any component of `value` is true.
|
|
230
|
+
* @example
|
|
231
|
+
* any(vec2b(false, true)) // returns true
|
|
232
|
+
* any(vec3b(false, false, false)) // returns false
|
|
233
|
+
*/
|
|
234
|
+
const any = dualImpl({
|
|
235
|
+
name: "any",
|
|
236
|
+
signature: (...argTypes) => ({
|
|
237
|
+
argTypes,
|
|
238
|
+
returnType: bool
|
|
239
|
+
}),
|
|
240
|
+
normalImpl: (value) => !cpuAll(cpuNot(value)),
|
|
241
|
+
codegenImpl: (_ctx, [arg]) => stitch`any(${arg})`
|
|
242
|
+
});
|
|
243
|
+
/**
|
|
244
|
+
* Checks whether the given elements differ by at most the `precision` value.
|
|
245
|
+
* Checks all elements of `lhs` and `rhs` if arguments are vectors.
|
|
246
|
+
* @example
|
|
247
|
+
* isCloseTo(0, 0.1) // returns false
|
|
248
|
+
* isCloseTo(vec3f(0, 0, 0), vec3f(0.002, -0.009, 0)) // returns true
|
|
249
|
+
*
|
|
250
|
+
* @param {number} precision argument that specifies the maximum allowed difference, 0.01 by default.
|
|
251
|
+
*/
|
|
252
|
+
const isCloseTo = dualImpl({
|
|
253
|
+
name: "isCloseTo",
|
|
254
|
+
signature: (...args) => ({
|
|
255
|
+
argTypes: args,
|
|
256
|
+
returnType: bool
|
|
257
|
+
}),
|
|
258
|
+
normalImpl: (lhs, rhs, precision = .01) => {
|
|
259
|
+
if (typeof lhs === "number" && typeof rhs === "number") return Math.abs(lhs - rhs) < precision;
|
|
260
|
+
if (isVecInstance(lhs) && isVecInstance(rhs)) return VectorOps.isCloseToZero[lhs.kind](sub(lhs, rhs), precision);
|
|
261
|
+
return false;
|
|
262
|
+
},
|
|
263
|
+
codegenImpl: (_ctx, [lhs, rhs, precision = snip(.01, f32, "constant")]) => {
|
|
264
|
+
if (isSnippetNumeric(lhs) && isSnippetNumeric(rhs)) return stitch`(abs(f32(${lhs}) - f32(${rhs})) <= ${precision})`;
|
|
265
|
+
if (!isSnippetNumeric(lhs) && !isSnippetNumeric(rhs)) return stitch`all(abs(${lhs} - ${rhs}) <= (${lhs} - ${lhs}) + ${precision})`;
|
|
266
|
+
return "false";
|
|
267
|
+
}
|
|
268
|
+
});
|
|
269
|
+
function cpuSelect(f, t, cond) {
|
|
270
|
+
if (typeof cond === "boolean") return cond ? t : f;
|
|
271
|
+
return VectorOps.select[f.kind](f, t, cond);
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Returns `t` if `cond` is `true`, and `f` otherwise.
|
|
275
|
+
* Component-wise if `cond` is a vector.
|
|
276
|
+
* @example
|
|
277
|
+
* select(1, 2, false) // returns 1
|
|
278
|
+
* select(1, 2, true) // returns 2
|
|
279
|
+
* select(vec2i(1, 2), vec2i(3, 4), true) // returns vec2i(3, 4)
|
|
280
|
+
* select(vec2i(1, 2), vec2i(3, 4), vec2b(false, true)) // returns vec2i(1, 4)
|
|
281
|
+
*/
|
|
282
|
+
const select = dualImpl({
|
|
283
|
+
name: "select",
|
|
284
|
+
signature: (f, t, cond) => {
|
|
285
|
+
const [uf, ut] = unify([f, t]) ?? [f, t];
|
|
286
|
+
return {
|
|
287
|
+
argTypes: [
|
|
288
|
+
uf,
|
|
289
|
+
ut,
|
|
290
|
+
cond
|
|
291
|
+
],
|
|
292
|
+
returnType: uf
|
|
293
|
+
};
|
|
294
|
+
},
|
|
295
|
+
normalImpl: cpuSelect,
|
|
296
|
+
codegenImpl: (_ctx, [f, t, cond]) => stitch`select(${f}, ${t}, ${cond})`
|
|
297
|
+
});
|
|
298
|
+
//#endregion
|
|
299
|
+
export { all, allEq, and, any, eq, ge, gt, isCloseTo, le, lt, ne, not, or, select };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { DualFn } from "../types.js";
|
|
2
|
+
import { AnyFloat32VecInstance } from "../data/wgslTypes.js";
|
|
3
|
+
|
|
4
|
+
//#region src/std/derivative.d.ts
|
|
5
|
+
type DerivativeSignature = ((value: number) => number) & (<T extends AnyFloat32VecInstance>(value: T) => T);
|
|
6
|
+
declare const dpdx: DualFn;
|
|
7
|
+
declare const dpdxCoarse: DualFn;
|
|
8
|
+
declare const dpdxFine: DualFn;
|
|
9
|
+
declare const dpdy: DualFn;
|
|
10
|
+
declare const dpdyCoarse: DualFn;
|
|
11
|
+
declare const dpdyFine: DualFn;
|
|
12
|
+
declare const fwidth: DualFn;
|
|
13
|
+
declare const fwidthCoarse: DualFn;
|
|
14
|
+
declare const fwidthFine: DualFn;
|
|
15
|
+
//#endregion
|
|
16
|
+
export { dpdx, dpdxCoarse, dpdxFine, dpdy, dpdyCoarse, dpdyFine, fwidth, fwidthCoarse, fwidthFine };
|