typegpu 0.11.9 → 0.12.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.
Files changed (366) hide show
  1. package/bin.mjs +28 -6
  2. package/builtin.d.ts +51 -53
  3. package/builtin.js +30 -34
  4. package/common/fullScreenTriangle.d.ts +2 -10
  5. package/common/fullScreenTriangle.js +20 -28
  6. package/common/index.d.ts +2 -9
  7. package/common/index.js +3 -12
  8. package/common/writeSoA.d.ts +11 -12
  9. package/common/writeSoA.js +88 -76
  10. package/core/buffer/buffer.d.ts +79 -62
  11. package/core/buffer/buffer.js +283 -205
  12. package/core/buffer/bufferBinding.d.ts +71 -0
  13. package/core/buffer/bufferBinding.js +154 -0
  14. package/core/buffer/bufferUsage.d.ts +14 -45
  15. package/core/buffer/bufferUsage.js +1 -171
  16. package/core/buffer/laidOutBuffer.d.ts +25 -0
  17. package/core/buffer/laidOutBuffer.js +55 -0
  18. package/core/commandEncoder/attachments.d.ts +125 -0
  19. package/core/commandEncoder/attachments.js +29 -0
  20. package/core/commandEncoder/commandEncoder.d.ts +53 -0
  21. package/core/commandEncoder/commandEncoder.js +56 -0
  22. package/core/commandEncoder/computePass.d.ts +58 -0
  23. package/core/commandEncoder/computePass.js +63 -0
  24. package/core/commandEncoder/renderPass.d.ts +121 -0
  25. package/core/commandEncoder/renderPass.js +196 -0
  26. package/core/constant/tgpuConstant.d.ts +26 -26
  27. package/core/constant/tgpuConstant.js +76 -65
  28. package/core/declare/tgpuDeclare.d.ts +3 -6
  29. package/core/declare/tgpuDeclare.js +36 -36
  30. package/core/function/autoIO.d.ts +61 -33
  31. package/core/function/autoIO.js +68 -73
  32. package/core/function/comptime.d.ts +11 -13
  33. package/core/function/comptime.js +60 -52
  34. package/core/function/createCallableSchema.d.ts +16 -0
  35. package/core/function/createCallableSchema.js +50 -40
  36. package/core/function/dualImpl.d.ts +44 -0
  37. package/core/function/dualImpl.js +71 -50
  38. package/core/function/entryInputRouter.d.ts +26 -0
  39. package/core/function/entryInputRouter.js +31 -33
  40. package/core/function/extractArgs.d.ts +38 -0
  41. package/core/function/extractArgs.js +202 -200
  42. package/core/function/fnCore.d.ts +44 -5
  43. package/core/function/fnCore.js +148 -93
  44. package/core/function/fnTypes.d.ts +31 -29
  45. package/core/function/fnTypes.js +1 -0
  46. package/core/function/ioSchema.d.ts +12 -10
  47. package/core/function/ioSchema.js +65 -47
  48. package/core/function/shelllessImpl.d.ts +9 -12
  49. package/core/function/shelllessImpl.js +17 -22
  50. package/core/function/templateUtils.d.ts +2 -0
  51. package/core/function/templateUtils.js +9 -8
  52. package/core/function/tgpuComputeFn.d.ts +26 -28
  53. package/core/function/tgpuComputeFn.js +47 -48
  54. package/core/function/tgpuFn.d.ts +34 -37
  55. package/core/function/tgpuFn.js +179 -153
  56. package/core/function/tgpuFragmentFn.d.ts +52 -53
  57. package/core/function/tgpuFragmentFn.js +63 -56
  58. package/core/function/tgpuVertexFn.d.ts +36 -39
  59. package/core/function/tgpuVertexFn.js +56 -53
  60. package/core/pipeline/computePipeline.d.ts +141 -53
  61. package/core/pipeline/computePipeline.js +291 -225
  62. package/core/pipeline/connectAttachmentToShader.d.ts +4 -0
  63. package/core/pipeline/connectAttachmentToShader.js +25 -22
  64. package/core/pipeline/connectTargetsToShader.d.ts +3 -0
  65. package/core/pipeline/connectTargetsToShader.js +29 -27
  66. package/core/pipeline/drawState.d.ts +52 -0
  67. package/core/pipeline/drawState.js +225 -0
  68. package/core/pipeline/performanceTracker.d.ts +14 -0
  69. package/core/pipeline/performanceTracker.js +34 -0
  70. package/core/pipeline/pipelineUtils.d.ts +18 -0
  71. package/core/pipeline/pipelineUtils.js +54 -25
  72. package/core/pipeline/priors.d.ts +1 -0
  73. package/core/pipeline/priors.js +12 -0
  74. package/core/pipeline/renderPipeline.d.ts +246 -261
  75. package/core/pipeline/renderPipeline.js +487 -480
  76. package/core/pipeline/timeable.d.ts +28 -18
  77. package/core/pipeline/timeable.js +90 -52
  78. package/core/pipeline/typeGuards.d.ts +17 -0
  79. package/core/pipeline/typeGuards.js +52 -22
  80. package/core/pipeline/webgpuLimitations.d.ts +10 -0
  81. package/core/pipeline/webgpuLimitations.js +81 -0
  82. package/core/querySet/querySet.d.ts +26 -21
  83. package/core/querySet/querySet.js +127 -103
  84. package/core/rawCodeSnippet/tgpuRawCodeSnippet.d.ts +13 -18
  85. package/core/rawCodeSnippet/tgpuRawCodeSnippet.js +89 -90
  86. package/core/resolve/externals.d.ts +25 -8
  87. package/core/resolve/externals.js +78 -54
  88. package/core/resolve/namespace.d.ts +22 -25
  89. package/core/resolve/namespace.js +16 -21
  90. package/core/resolve/resolvableString.d.ts +7 -0
  91. package/core/resolve/resolvableString.js +13 -0
  92. package/core/resolve/resolveData.d.ts +10 -0
  93. package/core/resolve/resolveData.js +162 -121
  94. package/core/resolve/stitch.d.ts +8 -0
  95. package/core/resolve/stitch.js +23 -21
  96. package/core/resolve/tgpuResolve.d.ts +55 -53
  97. package/core/resolve/tgpuResolve.js +106 -53
  98. package/core/root/configurableImpl.d.ts +10 -0
  99. package/core/root/configurableImpl.js +17 -19
  100. package/core/root/init.d.ts +76 -38
  101. package/core/root/init.js +346 -455
  102. package/core/root/rootTypes.d.ts +461 -636
  103. package/core/root/rootTypes.js +1 -0
  104. package/core/sampler/sampler.d.ts +50 -31
  105. package/core/sampler/sampler.js +120 -113
  106. package/core/simulate/tgpuSimulate.d.ts +9 -12
  107. package/core/simulate/tgpuSimulate.js +68 -71
  108. package/core/slot/accessor.d.ts +36 -13
  109. package/core/slot/accessor.js +138 -91
  110. package/core/slot/internalSlots.d.ts +2 -0
  111. package/core/slot/internalSlots.js +1 -6
  112. package/core/slot/lazy.d.ts +2 -6
  113. package/core/slot/lazy.js +71 -39
  114. package/core/slot/slot.d.ts +2 -6
  115. package/core/slot/slot.js +45 -38
  116. package/core/slot/slotTypes.d.ts +78 -80
  117. package/core/slot/slotTypes.js +11 -16
  118. package/core/texture/externalTexture.d.ts +20 -11
  119. package/core/texture/externalTexture.js +46 -46
  120. package/core/texture/texture.d.ts +152 -97
  121. package/core/texture/texture.js +407 -314
  122. package/core/texture/textureFormats.d.ts +42 -23
  123. package/core/texture/textureFormats.js +104 -75
  124. package/core/texture/textureProps.d.ts +7 -10
  125. package/core/texture/textureProps.js +1 -0
  126. package/core/texture/textureUtils.d.ts +8 -0
  127. package/core/texture/textureUtils.js +185 -179
  128. package/core/texture/usageExtension.d.ts +21 -18
  129. package/core/texture/usageExtension.js +13 -17
  130. package/core/unroll/tgpuUnroll.d.ts +20 -19
  131. package/core/unroll/tgpuUnroll.js +82 -91
  132. package/core/valueProxyUtils.d.ts +3 -0
  133. package/core/valueProxyUtils.js +52 -41
  134. package/core/variable/tgpuVariable.d.ts +26 -27
  135. package/core/variable/tgpuVariable.js +93 -96
  136. package/core/vertexLayout/connectAttributesToShader.d.ts +9 -0
  137. package/core/vertexLayout/connectAttributesToShader.js +61 -56
  138. package/core/vertexLayout/vertexAttribute.d.ts +21 -17
  139. package/core/vertexLayout/vertexAttribute.js +1 -0
  140. package/core/vertexLayout/vertexLayout.d.ts +24 -18
  141. package/core/vertexLayout/vertexLayout.js +130 -99
  142. package/core/whitespaces.d.ts +2 -0
  143. package/core/whitespaces.js +16 -0
  144. package/data/alignIO.d.ts +7 -0
  145. package/data/alignIO.js +13 -13
  146. package/data/alignmentOf.d.ts +5 -8
  147. package/data/alignmentOf.js +90 -75
  148. package/data/array.d.ts +17 -9
  149. package/data/array.js +57 -40
  150. package/data/atomic.d.ts +2 -7
  151. package/data/atomic.js +26 -23
  152. package/data/attributes.d.ts +19 -21
  153. package/data/attributes.js +192 -132
  154. package/data/autoStruct.d.ts +36 -3
  155. package/data/autoStruct.js +84 -81
  156. package/data/compiledIO.d.ts +5 -0
  157. package/data/compiledIO.js +231 -215
  158. package/data/dataIO.d.ts +13 -11
  159. package/data/dataIO.js +643 -589
  160. package/data/dataTypes.d.ts +83 -65
  161. package/data/dataTypes.js +66 -68
  162. package/data/deepEqual.d.ts +2 -7
  163. package/data/deepEqual.js +80 -55
  164. package/data/disarray.d.ts +6 -10
  165. package/data/disarray.js +52 -46
  166. package/data/float16Conversion.d.ts +4 -0
  167. package/data/float16Conversion.js +7 -0
  168. package/data/generalizeFn.d.ts +25 -0
  169. package/data/generalizeFn.js +89 -0
  170. package/data/getLongestContiguousPrefix.d.ts +4 -8
  171. package/data/getLongestContiguousPrefix.js +6 -11
  172. package/data/index.d.ts +30 -31
  173. package/data/index.js +39 -172
  174. package/data/instanceToSchema.d.ts +24 -27
  175. package/data/instanceToSchema.js +1 -0
  176. package/data/isContiguous.d.ts +4 -8
  177. package/data/isContiguous.js +6 -11
  178. package/data/matrix.d.ts +21 -21
  179. package/data/matrix.js +513 -505
  180. package/data/numberOps.d.ts +5 -0
  181. package/data/numberOps.js +21 -21
  182. package/data/numeric.d.ts +21 -12
  183. package/data/numeric.js +287 -197
  184. package/data/offsetUtils.d.ts +9 -13
  185. package/data/offsetUtils.js +204 -144
  186. package/data/offsets.d.ts +8 -0
  187. package/data/offsets.js +33 -33
  188. package/data/partialIO.d.ts +13 -8
  189. package/data/partialIO.js +124 -113
  190. package/data/ptr.d.ts +12 -12
  191. package/data/ptr.js +42 -44
  192. package/data/ref.d.ts +40 -27
  193. package/data/ref.js +125 -95
  194. package/data/sampler.d.ts +99 -101
  195. package/data/sampler.js +17 -21
  196. package/data/schemaCallWrapper.d.ts +19 -0
  197. package/data/schemaCallWrapper.js +31 -26
  198. package/data/schemaMemoryLayout.d.ts +8 -0
  199. package/data/schemaMemoryLayout.js +213 -185
  200. package/data/sizeOf.d.ts +4 -8
  201. package/data/sizeOf.js +6 -11
  202. package/data/snippet.d.ts +96 -20
  203. package/data/snippet.js +75 -86
  204. package/data/struct.d.ts +4 -7
  205. package/data/struct.js +51 -39
  206. package/data/texture.d.ts +253 -250
  207. package/data/texture.js +238 -200
  208. package/data/unstruct.d.ts +2 -7
  209. package/data/unstruct.js +43 -37
  210. package/data/vector.d.ts +33 -21
  211. package/data/vector.js +234 -221
  212. package/data/vectorImpl.d.ts +231 -0
  213. package/data/vectorImpl.js +563 -515
  214. package/data/vectorOps.d.ts +25 -0
  215. package/data/vectorOps.js +118 -669
  216. package/data/vertexFormatData.d.ts +181 -184
  217. package/data/vertexFormatData.js +104 -106
  218. package/data/wgslTypes.d.ts +665 -600
  219. package/data/wgslTypes.js +220 -198
  220. package/errors.d.ts +50 -24
  221. package/errors.js +146 -126
  222. package/execMode.d.ts +16 -0
  223. package/execMode.js +47 -40
  224. package/extension.d.ts +13 -8
  225. package/extension.js +10 -16
  226. package/getGPUValue.d.ts +1 -0
  227. package/getGPUValue.js +2 -7
  228. package/index.d.ts +28 -68
  229. package/index.js +8 -28
  230. package/indexNamedExports.d.ts +69 -42
  231. package/indexNamedExports.js +25 -0
  232. package/internal.d.ts +16 -0
  233. package/internal.js +12 -0
  234. package/mathUtils.d.ts +5 -0
  235. package/mathUtils.js +7 -11
  236. package/memo.d.ts +11 -0
  237. package/memo.js +20 -22
  238. package/minify.d.ts +5 -0
  239. package/minify.js +93 -0
  240. package/nameUtils.d.ts +29 -0
  241. package/nameUtils.js +427 -394
  242. package/package.json +5 -1
  243. package/resolutionCtx.d.ts +162 -22
  244. package/resolutionCtx.js +911 -629
  245. package/serial/registry.d.ts +15 -0
  246. package/serial/registry.js +106 -0
  247. package/serial/restore.d.ts +49 -0
  248. package/serial/restore.js +68 -0
  249. package/serial/schema.d.ts +58 -0
  250. package/serial/schema.js +209 -0
  251. package/serial/types.d.ts +5 -0
  252. package/serial/types.js +1 -0
  253. package/shared/env.d.ts +9 -0
  254. package/shared/env.js +9 -12
  255. package/shared/generators.d.ts +4 -0
  256. package/shared/generators.js +10 -12
  257. package/shared/meta.d.ts +29 -14
  258. package/shared/meta.js +71 -55
  259. package/shared/normalizeMetadata.d.ts +32 -27
  260. package/shared/normalizeMetadata.js +23 -36
  261. package/shared/repr.d.ts +73 -55
  262. package/shared/repr.js +1 -0
  263. package/shared/soul.d.ts +17 -0
  264. package/shared/soul.js +1 -0
  265. package/shared/stringify.d.ts +2 -0
  266. package/shared/stringify.js +29 -20
  267. package/shared/symbols.d.ts +26 -20
  268. package/shared/symbols.js +79 -79
  269. package/shared/tseynit.d.ts +2 -0
  270. package/shared/tseynit.js +160 -75
  271. package/shared/utilityTypes.d.ts +30 -14
  272. package/shared/utilityTypes.js +2 -6
  273. package/shared/vertexFormat.d.ts +41 -43
  274. package/shared/vertexFormat.js +59 -62
  275. package/std/array.d.ts +2 -7
  276. package/std/array.js +18 -23
  277. package/std/atomic.d.ts +14 -18
  278. package/std/atomic.js +96 -95
  279. package/std/bitcast.d.ts +193 -10
  280. package/std/bitcast.js +309 -39
  281. package/std/boolean.d.ts +19 -33
  282. package/std/boolean.js +296 -292
  283. package/std/copy.d.ts +2 -7
  284. package/std/copy.js +27 -24
  285. package/std/derivative.d.ts +11 -15
  286. package/std/derivative.js +56 -79
  287. package/std/discard.d.ts +1 -6
  288. package/std/discard.js +7 -14
  289. package/std/environment.d.ts +19 -14
  290. package/std/environment.js +71 -51
  291. package/std/extensions.d.ts +2 -8
  292. package/std/extensions.js +10 -11
  293. package/std/index.d.ts +20 -24
  294. package/std/index.js +32 -187
  295. package/std/matrix.d.ts +6 -12
  296. package/std/matrix.js +60 -69
  297. package/std/numeric.d.ts +150 -146
  298. package/std/numeric.js +771 -727
  299. package/std/operators.d.ts +19 -27
  300. package/std/operators.js +195 -194
  301. package/std/packing.d.ts +5 -11
  302. package/std/packing.js +67 -75
  303. package/std/range.d.ts +6 -11
  304. package/std/range.js +48 -36
  305. package/std/subgroup.d.ts +33 -37
  306. package/std/subgroup.js +180 -217
  307. package/std/texture.d.ts +70 -38
  308. package/std/texture.js +176 -172
  309. package/taskQueue.d.ts +6 -0
  310. package/taskQueue.js +30 -0
  311. package/tgpu.d.ts +16 -0
  312. package/tgpu.js +17 -44
  313. package/tgpuBindGroupLayout.d.ts +132 -152
  314. package/tgpuBindGroupLayout.js +346 -268
  315. package/tgpuLogger.d.ts +34 -0
  316. package/tgpuLogger.js +64 -0
  317. package/tgpuUnstable.d.ts +16 -22
  318. package/tgpuUnstable.js +13 -35
  319. package/tgsl/accessIndex.d.ts +2 -0
  320. package/tgsl/accessIndex.js +76 -33
  321. package/tgsl/accessProp.d.ts +2 -0
  322. package/tgsl/accessProp.js +142 -101
  323. package/tgsl/accessStructProp.d.ts +5 -0
  324. package/tgsl/accessStructProp.js +13 -15
  325. package/tgsl/consoleLog/deserializers.d.ts +13 -0
  326. package/tgsl/consoleLog/deserializers.js +119 -103
  327. package/tgsl/consoleLog/logGenerator.d.ts +21 -0
  328. package/tgsl/consoleLog/logGenerator.js +85 -80
  329. package/tgsl/consoleLog/serializers.d.ts +21 -0
  330. package/tgsl/consoleLog/serializers.js +119 -100
  331. package/tgsl/consoleLog/types.d.ts +40 -39
  332. package/tgsl/consoleLog/types.js +1 -0
  333. package/tgsl/conversion.d.ts +33 -0
  334. package/tgsl/conversion.js +271 -216
  335. package/tgsl/forOfUtils.d.ts +15 -0
  336. package/tgsl/forOfUtils.js +85 -70
  337. package/tgsl/generationHelpers.d.ts +22 -39
  338. package/tgsl/generationHelpers.js +85 -58
  339. package/tgsl/infixDispatch.d.ts +70 -0
  340. package/tgsl/infixDispatch.js +48 -46
  341. package/tgsl/jsPolyfills.d.ts +16 -19
  342. package/tgsl/jsPolyfills.js +44 -42
  343. package/tgsl/makeDereferenceable.d.ts +48 -0
  344. package/tgsl/makeDereferenceable.js +86 -0
  345. package/tgsl/makeResolvable.d.ts +22 -0
  346. package/tgsl/makeResolvable.js +33 -0
  347. package/tgsl/shaderGenerator.d.ts +77 -17
  348. package/tgsl/shaderGenerator.js +1 -0
  349. package/tgsl/shellless.d.ts +6 -9
  350. package/tgsl/shellless.js +73 -43
  351. package/tgsl/wgslGenerator.d.ts +66 -59
  352. package/tgsl/wgslGenerator.js +1345 -760
  353. package/types.d.ts +287 -222
  354. package/types.js +61 -44
  355. package/unwrapper.d.ts +31 -26
  356. package/unwrapper.js +1 -0
  357. package/wgslExtensions.d.ts +3 -5
  358. package/wgslExtensions.js +12 -16
  359. package/_virtual/rolldown_runtime.js +0 -13
  360. package/core/buffer/bufferShorthand.d.ts +0 -50
  361. package/core/buffer/bufferShorthand.js +0 -55
  362. package/core/pipeline/applyPipelineState.js +0 -37
  363. package/core/pipeline/limitsOverflow.js +0 -13
  364. package/package.js +0 -5
  365. package/tgsl/shaderGenerator_members.d.ts +0 -33
  366. package/tgsl/shaderGenerator_members.js +0 -12
package/std/numeric.js CHANGED
@@ -1,847 +1,891 @@
1
- import { isHalfPrecisionSchema, isVecInstance } from "../data/wgslTypes.js";
2
- import { SignatureNotSupportedError } from "../errors.js";
1
+ import { dualImpl, MissingCpuImplError } from "../core/function/dualImpl.js";
3
2
  import { stitch } from "../core/resolve/stitch.js";
4
- import { unify } from "../tgsl/conversion.js";
3
+ import { mat2x2f, mat3x3f, mat4x4f } from "../data/matrix.js";
4
+ import { clampScalar, smoothstepScalar } from "../data/numberOps.js";
5
5
  import { abstractFloat, abstractInt, f16, f32, i32, u32 } from "../data/numeric.js";
6
- import { vec2f, vec2h, vec2i, vec2u, vec3f, vec3h, vec3i, vec3u, vec4f, vec4h, vec4i, vec4u } from "../data/vector.js";
7
6
  import { abstruct } from "../data/struct.js";
8
- import { MissingCpuImplError, dualImpl } from "../core/function/dualImpl.js";
9
- import { mat2x2f, mat3x3f, mat4x4f } from "../data/matrix.js";
10
- import { smoothstepScalar } from "../data/numberOps.js";
7
+ import { vec2f, vec2h, vec2i, vec2u, vec3f, vec3h, vec3i, vec3u, vec4f, vec4h, vec4i, vec4u, } from "../data/vector.js";
11
8
  import { VectorOps } from "../data/vectorOps.js";
9
+ import { generalizeFn, upCast } from "../data/generalizeFn.js";
10
+ import { isHalfPrecisionSchema, WORKAROUND_getSchema, } from "../data/wgslTypes.js";
11
+ import { SignatureNotSupportedError } from "../errors.js";
12
+ import { assertExhaustive } from "../shared/utilityTypes.js";
13
+ import { unify } from "../tgsl/conversion.js";
12
14
  import { mul, sub } from "./operators.js";
13
-
14
- //#region src/std/numeric.ts
15
+ // helpers
15
16
  const unaryIdentitySignature = (arg) => {
16
- return {
17
- argTypes: [arg],
18
- returnType: arg
19
- };
17
+ return {
18
+ argTypes: [arg],
19
+ returnType: arg,
20
+ };
20
21
  };
21
22
  const variadicUnifySignature = (...args) => {
22
- const uargs = unify(args) ?? args;
23
- return {
24
- argTypes: uargs,
25
- returnType: uargs[0]
26
- };
23
+ const uargs = unify(args) ?? args;
24
+ return {
25
+ argTypes: uargs,
26
+ returnType: uargs[0],
27
+ };
27
28
  };
28
29
  const unifyRestrictedSignature = (restrict) => (...args) => {
29
- const uargs = unify(args, restrict);
30
- if (!uargs) throw new SignatureNotSupportedError(args, restrict);
31
- return {
32
- argTypes: uargs,
33
- returnType: uargs[0]
34
- };
30
+ const uargs = unify(args, restrict);
31
+ if (!uargs) {
32
+ throw new SignatureNotSupportedError(args, restrict);
33
+ }
34
+ return {
35
+ argTypes: uargs,
36
+ returnType: uargs[0],
37
+ };
35
38
  };
36
39
  function variadicReduce(fn) {
37
- return (fst, ...rest) => {
38
- let acc = fst;
39
- for (const r of rest) acc = fn(acc, r);
40
- return acc;
41
- };
40
+ return (fst, ...rest) => {
41
+ let acc = fst;
42
+ for (const r of rest) {
43
+ acc = fn(acc, r);
44
+ }
45
+ return acc;
46
+ };
42
47
  }
43
48
  function variadicStitch(wrapper) {
44
- return (_ctx, [fst, ...rest]) => {
45
- let acc = stitch`${fst}`;
46
- for (const r of rest) acc = stitch`${wrapper}(${acc}, ${r})`;
47
- return acc;
48
- };
49
- }
50
- const anyFloatPrimitive = [
51
- f32,
52
- f16,
53
- abstractFloat
54
- ];
55
- const anyFloatVec = [
56
- vec2f,
57
- vec3f,
58
- vec4f,
59
- vec2h,
60
- vec3h,
61
- vec4h
62
- ];
49
+ return (_ctx, [fst, ...rest]) => {
50
+ let acc = stitch `${fst}`;
51
+ for (const r of rest) {
52
+ acc = stitch `${wrapper}(${acc}, ${r})`;
53
+ }
54
+ return acc;
55
+ };
56
+ }
57
+ const anyFloatPrimitive = [f32, f16, abstractFloat];
58
+ const anyFloatVec = [vec2f, vec3f, vec4f, vec2h, vec3h, vec4h];
63
59
  const anyFloat = [...anyFloatPrimitive, ...anyFloatVec];
64
60
  const anyConcreteIntegerPrimitive = [i32, u32];
65
- const anyConcreteIntegerVec = [
66
- vec2i,
67
- vec3i,
68
- vec4i,
69
- vec2u,
70
- vec3u,
71
- vec4u
72
- ];
61
+ const anyConcreteIntegerVec = [vec2i, vec3i, vec4i, vec2u, vec3u, vec4u];
73
62
  const anyConcreteInteger = [...anyConcreteIntegerPrimitive, ...anyConcreteIntegerVec];
74
63
  function cpuAbs(value) {
75
- if (typeof value === "number") return Math.abs(value);
76
- return VectorOps.abs[value.kind](value);
64
+ return generalizeFn(Math.abs, [value]);
77
65
  }
78
- const abs = dualImpl({
79
- name: "abs",
80
- signature: unaryIdentitySignature,
81
- normalImpl: cpuAbs,
82
- codegenImpl: (_ctx, [value]) => stitch`abs(${value})`
66
+ export const abs = dualImpl({
67
+ name: 'abs',
68
+ signature: unaryIdentitySignature,
69
+ normalImpl: cpuAbs,
70
+ codegenImpl: (_ctx, [value]) => stitch `abs(${value})`,
71
+ sideEffects: false,
83
72
  });
84
73
  function cpuAcos(value) {
85
- if (typeof value === "number") return Math.acos(value);
86
- return VectorOps.acos[value.kind](value);
74
+ return generalizeFn(Math.acos, [value]);
87
75
  }
88
- const acos = dualImpl({
89
- name: "acos",
90
- signature: unifyRestrictedSignature(anyFloat),
91
- normalImpl: cpuAcos,
92
- codegenImpl: (_ctx, [value]) => stitch`acos(${value})`
76
+ export const acos = dualImpl({
77
+ name: 'acos',
78
+ signature: unifyRestrictedSignature(anyFloat),
79
+ normalImpl: cpuAcos,
80
+ codegenImpl: (_ctx, [value]) => stitch `acos(${value})`,
81
+ sideEffects: false,
93
82
  });
94
83
  function cpuAcosh(value) {
95
- if (typeof value === "number") return Math.acosh(value);
96
- return VectorOps.acosh[value.kind](value);
84
+ return generalizeFn(Math.acosh, [value]);
97
85
  }
98
- const acosh = dualImpl({
99
- name: "acosh",
100
- signature: unifyRestrictedSignature(anyFloat),
101
- normalImpl: cpuAcosh,
102
- codegenImpl: (_ctx, [value]) => stitch`acosh(${value})`
86
+ export const acosh = dualImpl({
87
+ name: 'acosh',
88
+ signature: unifyRestrictedSignature(anyFloat),
89
+ normalImpl: cpuAcosh,
90
+ codegenImpl: (_ctx, [value]) => stitch `acosh(${value})`,
91
+ sideEffects: false,
103
92
  });
104
93
  function cpuAsin(value) {
105
- if (typeof value === "number") return Math.asin(value);
106
- return VectorOps.asin[value.kind](value);
94
+ return generalizeFn(Math.asin, [value]);
107
95
  }
108
- const asin = dualImpl({
109
- name: "asin",
110
- signature: unifyRestrictedSignature(anyFloat),
111
- normalImpl: cpuAsin,
112
- codegenImpl: (_ctx, [value]) => stitch`asin(${value})`
96
+ export const asin = dualImpl({
97
+ name: 'asin',
98
+ signature: unifyRestrictedSignature(anyFloat),
99
+ normalImpl: cpuAsin,
100
+ codegenImpl: (_ctx, [value]) => stitch `asin(${value})`,
101
+ sideEffects: false,
113
102
  });
114
103
  function cpuAsinh(value) {
115
- if (typeof value === "number") return Math.asinh(value);
116
- return VectorOps.asinh[value.kind](value);
104
+ return generalizeFn(Math.asinh, [value]);
117
105
  }
118
- const asinh = dualImpl({
119
- name: "asinh",
120
- signature: unifyRestrictedSignature(anyFloat),
121
- normalImpl: cpuAsinh,
122
- codegenImpl: (_ctx, [value]) => stitch`asinh(${value})`
106
+ export const asinh = dualImpl({
107
+ name: 'asinh',
108
+ signature: unifyRestrictedSignature(anyFloat),
109
+ normalImpl: cpuAsinh,
110
+ codegenImpl: (_ctx, [value]) => stitch `asinh(${value})`,
111
+ sideEffects: false,
123
112
  });
124
113
  function cpuAtan(value) {
125
- if (typeof value === "number") return Math.atan(value);
126
- return VectorOps.atan[value.kind](value);
114
+ return generalizeFn(Math.atan, [value]);
127
115
  }
128
- const atan = dualImpl({
129
- name: "atan",
130
- signature: unifyRestrictedSignature(anyFloat),
131
- normalImpl: cpuAtan,
132
- codegenImpl: (_ctx, [value]) => stitch`atan(${value})`
116
+ export const atan = dualImpl({
117
+ name: 'atan',
118
+ signature: unifyRestrictedSignature(anyFloat),
119
+ normalImpl: cpuAtan,
120
+ codegenImpl: (_ctx, [value]) => stitch `atan(${value})`,
121
+ sideEffects: false,
133
122
  });
134
123
  function cpuAtanh(value) {
135
- if (typeof value === "number") return Math.atanh(value);
136
- return VectorOps.atanh[value.kind](value);
124
+ return generalizeFn(Math.atanh, [value]);
137
125
  }
138
- const atanh = dualImpl({
139
- name: "atanh",
140
- signature: unifyRestrictedSignature(anyFloat),
141
- normalImpl: cpuAtanh,
142
- codegenImpl: (_ctx, [value]) => stitch`atanh(${value})`
126
+ export const atanh = dualImpl({
127
+ name: 'atanh',
128
+ signature: unifyRestrictedSignature(anyFloat),
129
+ normalImpl: cpuAtanh,
130
+ codegenImpl: (_ctx, [value]) => stitch `atanh(${value})`,
131
+ sideEffects: false,
143
132
  });
144
133
  function cpuAtan2(y, x) {
145
- if (typeof y === "number" && typeof x === "number") return Math.atan2(y, x);
146
- return VectorOps.atan2[y.kind](y, x);
134
+ return generalizeFn(Math.atan2, [y, x]);
147
135
  }
148
- const atan2 = dualImpl({
149
- name: "atan2",
150
- signature: unifyRestrictedSignature(anyFloat),
151
- normalImpl: cpuAtan2,
152
- codegenImpl: (_ctx, [y, x]) => stitch`atan2(${y}, ${x})`
136
+ export const atan2 = dualImpl({
137
+ name: 'atan2',
138
+ signature: unifyRestrictedSignature(anyFloat),
139
+ normalImpl: cpuAtan2,
140
+ codegenImpl: (_ctx, [y, x]) => stitch `atan2(${y}, ${x})`,
141
+ sideEffects: false,
153
142
  });
154
143
  function cpuCeil(value) {
155
- if (typeof value === "number") return Math.ceil(value);
156
- return VectorOps.ceil[value.kind](value);
144
+ return generalizeFn(Math.ceil, [value]);
157
145
  }
158
- const ceil = dualImpl({
159
- name: "ceil",
160
- signature: unifyRestrictedSignature(anyFloat),
161
- normalImpl: cpuCeil,
162
- codegenImpl: (_ctx, [value]) => stitch`ceil(${value})`
146
+ export const ceil = dualImpl({
147
+ name: 'ceil',
148
+ signature: unifyRestrictedSignature(anyFloat),
149
+ normalImpl: cpuCeil,
150
+ codegenImpl: (_ctx, [value]) => stitch `ceil(${value})`,
151
+ sideEffects: false,
163
152
  });
164
153
  function cpuClamp(value, low, high) {
165
- if (typeof value === "number") return Math.min(Math.max(low, value), high);
166
- return VectorOps.clamp[value.kind](value, low, high);
154
+ return generalizeFn(clampScalar, [value, low, high]);
167
155
  }
168
- const clamp = dualImpl({
169
- name: "clamp",
170
- signature: variadicUnifySignature,
171
- normalImpl: cpuClamp,
172
- codegenImpl: (_ctx, [value, low, high]) => stitch`clamp(${value}, ${low}, ${high})`
156
+ export const clamp = dualImpl({
157
+ name: 'clamp',
158
+ signature: variadicUnifySignature,
159
+ normalImpl: cpuClamp,
160
+ codegenImpl: (_ctx, [value, low, high]) => stitch `clamp(${value}, ${low}, ${high})`,
161
+ sideEffects: false,
173
162
  });
174
163
  function cpuCos(value) {
175
- if (typeof value === "number") return Math.cos(value);
176
- return VectorOps.cos[value.kind](value);
164
+ return generalizeFn(Math.cos, [value]);
177
165
  }
178
- const cos = dualImpl({
179
- name: "cos",
180
- signature: unifyRestrictedSignature(anyFloat),
181
- normalImpl: cpuCos,
182
- codegenImpl: (_ctx, [value]) => stitch`cos(${value})`
166
+ export const cos = dualImpl({
167
+ name: 'cos',
168
+ signature: unifyRestrictedSignature(anyFloat),
169
+ normalImpl: cpuCos,
170
+ codegenImpl: (_ctx, [value]) => stitch `cos(${value})`,
171
+ sideEffects: false,
183
172
  });
184
173
  function cpuCosh(value) {
185
- if (typeof value === "number") return Math.cosh(value);
186
- return VectorOps.cosh[value.kind](value);
187
- }
188
- const cosh = dualImpl({
189
- name: "cosh",
190
- signature: unifyRestrictedSignature(anyFloat),
191
- normalImpl: cpuCosh,
192
- codegenImpl: (_ctx, [value]) => stitch`cosh(${value})`
193
- });
194
- const countLeadingZeros = dualImpl({
195
- name: "countLeadingZeros",
196
- signature: unifyRestrictedSignature(anyConcreteInteger),
197
- normalImpl: "CPU implementation for countLeadingZeros not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues",
198
- codegenImpl: (_ctx, [value]) => stitch`countLeadingZeros(${value})`
199
- });
200
- const countOneBits = dualImpl({
201
- name: "countOneBits",
202
- signature: unifyRestrictedSignature(anyConcreteInteger),
203
- normalImpl: "CPU implementation for countOneBits not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues",
204
- codegenImpl: (_ctx, [value]) => stitch`countOneBits(${value})`
205
- });
206
- const countTrailingZeros = dualImpl({
207
- name: "countTrailingZeros",
208
- signature: unifyRestrictedSignature(anyConcreteInteger),
209
- normalImpl: "CPU implementation for countTrailingZeros not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues",
210
- codegenImpl: (_ctx, [value]) => stitch`countTrailingZeros(${value})`
211
- });
212
- const cross = dualImpl({
213
- name: "cross",
214
- signature: unifyRestrictedSignature([vec3f, vec3h]),
215
- normalImpl: (a, b) => VectorOps.cross[a.kind](a, b),
216
- codegenImpl: (_ctx, [a, b]) => stitch`cross(${a}, ${b})`
174
+ return generalizeFn(Math.cosh, [value]);
175
+ }
176
+ export const cosh = dualImpl({
177
+ name: 'cosh',
178
+ signature: unifyRestrictedSignature(anyFloat),
179
+ normalImpl: cpuCosh,
180
+ codegenImpl: (_ctx, [value]) => stitch `cosh(${value})`,
181
+ sideEffects: false,
182
+ });
183
+ function cpuCountLeadingZeros(_value) {
184
+ throw new Error('Unreachable code. The function is only used for the type.');
185
+ }
186
+ export const countLeadingZeros = dualImpl({
187
+ name: 'countLeadingZeros',
188
+ signature: unifyRestrictedSignature(anyConcreteInteger),
189
+ normalImpl: 'CPU implementation for countLeadingZeros not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues',
190
+ codegenImpl: (_ctx, [value]) => stitch `countLeadingZeros(${value})`,
191
+ sideEffects: false,
192
+ });
193
+ function cpuCountOneBits(_value) {
194
+ throw new Error('Unreachable code. The function is only used for the type.');
195
+ }
196
+ export const countOneBits = dualImpl({
197
+ name: 'countOneBits',
198
+ signature: unifyRestrictedSignature(anyConcreteInteger),
199
+ normalImpl: 'CPU implementation for countOneBits not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues',
200
+ codegenImpl: (_ctx, [value]) => stitch `countOneBits(${value})`,
201
+ sideEffects: false,
202
+ });
203
+ function cpuCountTrailingZeros(_value) {
204
+ throw new Error('Unreachable code. The function is only used for the type.');
205
+ }
206
+ export const countTrailingZeros = dualImpl({
207
+ name: 'countTrailingZeros',
208
+ signature: unifyRestrictedSignature(anyConcreteInteger),
209
+ normalImpl: 'CPU implementation for countTrailingZeros not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues',
210
+ codegenImpl: (_ctx, [value]) => stitch `countTrailingZeros(${value})`,
211
+ sideEffects: false,
212
+ });
213
+ export const cross = dualImpl({
214
+ name: 'cross',
215
+ signature: unifyRestrictedSignature([vec3f, vec3h]),
216
+ normalImpl: (a, b) => VectorOps.cross[a.kind](a, b),
217
+ codegenImpl: (_ctx, [a, b]) => stitch `cross(${a}, ${b})`,
218
+ sideEffects: false,
217
219
  });
218
220
  function cpuDegrees(value) {
219
- if (typeof value === "number") return value * 180 / Math.PI;
220
- throw new MissingCpuImplError("CPU implementation for degrees on vectors not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues");
221
- }
222
- const degrees = dualImpl({
223
- name: "degrees",
224
- signature: unifyRestrictedSignature(anyFloat),
225
- normalImpl: cpuDegrees,
226
- codegenImpl: (_ctx, [value]) => stitch`degrees(${value})`
227
- });
228
- const determinant = dualImpl({
229
- name: "determinant",
230
- signature: (arg) => {
231
- if (!(arg.type === "mat2x2f" || arg.type === "mat3x3f" || arg.type === "mat4x4f")) throw new SignatureNotSupportedError([arg], [
232
- mat2x2f,
233
- mat3x3f,
234
- mat4x4f
235
- ]);
236
- return {
237
- argTypes: [arg],
238
- returnType: f32
239
- };
240
- },
241
- normalImpl: "CPU implementation for determinant not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues",
242
- codegenImpl: (_ctx, [value]) => stitch`determinant(${value})`
221
+ if (typeof value === 'number') {
222
+ return ((value * 180) / Math.PI);
223
+ }
224
+ throw new MissingCpuImplError('CPU implementation for degrees on vectors not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues');
225
+ }
226
+ export const degrees = dualImpl({
227
+ name: 'degrees',
228
+ signature: unifyRestrictedSignature(anyFloat),
229
+ normalImpl: cpuDegrees,
230
+ codegenImpl: (_ctx, [value]) => stitch `degrees(${value})`,
231
+ sideEffects: false,
232
+ });
233
+ export const determinant = dualImpl({
234
+ name: 'determinant',
235
+ signature: (arg) => {
236
+ if (!(arg.type === 'mat2x2f' || arg.type === 'mat3x3f' || arg.type === 'mat4x4f')) {
237
+ throw new SignatureNotSupportedError([arg], [mat2x2f, mat3x3f, mat4x4f]);
238
+ }
239
+ return { argTypes: [arg], returnType: f32 };
240
+ },
241
+ normalImpl: 'CPU implementation for determinant not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues',
242
+ codegenImpl: (_ctx, [value]) => stitch `determinant(${value})`,
243
+ sideEffects: false,
243
244
  });
244
245
  function cpuDistance(a, b) {
245
- if (typeof a === "number" && typeof b === "number") return Math.abs(a - b);
246
- return length(sub(a, b));
247
- }
248
- const distance = dualImpl({
249
- name: "distance",
250
- signature: (...args) => {
251
- const uargs = unify(args, anyFloat);
252
- if (!uargs) throw new SignatureNotSupportedError(args, anyFloat);
253
- return {
254
- argTypes: uargs,
255
- returnType: isHalfPrecisionSchema(uargs[0]) ? f16 : f32
256
- };
257
- },
258
- normalImpl: cpuDistance,
259
- codegenImpl: (_ctx, [a, b]) => stitch`distance(${a}, ${b})`
260
- });
261
- const dot = dualImpl({
262
- name: "dot",
263
- signature: (...args) => ({
264
- argTypes: args,
265
- returnType: args[0].primitive
266
- }),
267
- normalImpl: (lhs, rhs) => VectorOps.dot[lhs.kind](lhs, rhs),
268
- codegenImpl: (_ctx, [lhs, rhs]) => stitch`dot(${lhs}, ${rhs})`
269
- });
270
- const dot4U8Packed = dualImpl({
271
- name: "dot4U8Packed",
272
- signature: {
273
- argTypes: [u32, u32],
274
- returnType: u32
275
- },
276
- normalImpl: "CPU implementation for dot4U8Packed not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues",
277
- codegenImpl: (_ctx, [e1, e2]) => stitch`dot4U8Packed(${e1}, ${e2})`
278
- });
279
- const dot4I8Packed = dualImpl({
280
- name: "dot4I8Packed",
281
- signature: {
282
- argTypes: [u32, u32],
283
- returnType: i32
284
- },
285
- normalImpl: "CPU implementation for dot4I8Packed not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues",
286
- codegenImpl: (_ctx, [e1, e2]) => stitch`dot4I8Packed(${e1}, ${e2})`
246
+ if (typeof a === 'number' && typeof b === 'number') {
247
+ return Math.abs(a - b);
248
+ }
249
+ return length(sub(a, b));
250
+ }
251
+ export const distance = dualImpl({
252
+ name: 'distance',
253
+ signature: (...args) => {
254
+ const uargs = unify(args, anyFloat);
255
+ if (!uargs) {
256
+ throw new SignatureNotSupportedError(args, anyFloat);
257
+ }
258
+ return {
259
+ argTypes: uargs,
260
+ returnType: isHalfPrecisionSchema(uargs[0]) ? f16 : f32,
261
+ };
262
+ },
263
+ normalImpl: cpuDistance,
264
+ codegenImpl: (_ctx, [a, b]) => stitch `distance(${a}, ${b})`,
265
+ sideEffects: false,
266
+ });
267
+ export const dot = dualImpl({
268
+ name: 'dot',
269
+ signature: (...args) => ({
270
+ argTypes: args,
271
+ returnType: args[0].primitive,
272
+ }),
273
+ normalImpl: (lhs, rhs) => VectorOps.dot[lhs.kind](lhs, rhs),
274
+ codegenImpl: (_ctx, [lhs, rhs]) => stitch `dot(${lhs}, ${rhs})`,
275
+ sideEffects: false,
276
+ });
277
+ export const dot4U8Packed = dualImpl({
278
+ name: 'dot4U8Packed',
279
+ signature: { argTypes: [u32, u32], returnType: u32 },
280
+ normalImpl: 'CPU implementation for dot4U8Packed not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues',
281
+ codegenImpl: (_ctx, [e1, e2]) => stitch `dot4U8Packed(${e1}, ${e2})`,
282
+ sideEffects: false,
283
+ });
284
+ export const dot4I8Packed = dualImpl({
285
+ name: 'dot4I8Packed',
286
+ signature: { argTypes: [u32, u32], returnType: i32 },
287
+ normalImpl: 'CPU implementation for dot4I8Packed not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues',
288
+ codegenImpl: (_ctx, [e1, e2]) => stitch `dot4I8Packed(${e1}, ${e2})`,
289
+ sideEffects: false,
287
290
  });
288
291
  function cpuExp(value) {
289
- if (typeof value === "number") return Math.exp(value);
290
- return VectorOps.exp[value.kind](value);
292
+ return generalizeFn(Math.exp, [value]);
291
293
  }
292
- const exp = dualImpl({
293
- name: "exp",
294
- signature: unifyRestrictedSignature(anyFloat),
295
- normalImpl: cpuExp,
296
- codegenImpl: (_ctx, [value]) => stitch`exp(${value})`
294
+ export const exp = dualImpl({
295
+ name: 'exp',
296
+ signature: unifyRestrictedSignature(anyFloat),
297
+ normalImpl: cpuExp,
298
+ codegenImpl: (_ctx, [value]) => stitch `exp(${value})`,
299
+ sideEffects: false,
297
300
  });
298
301
  function cpuExp2(value) {
299
- if (typeof value === "number") return 2 ** value;
300
- return VectorOps.exp2[value.kind](value);
301
- }
302
- const exp2 = dualImpl({
303
- name: "exp2",
304
- signature: unifyRestrictedSignature(anyFloat),
305
- normalImpl: cpuExp2,
306
- codegenImpl: (_ctx, [value]) => stitch`exp2(${value})`
307
- });
308
- const extractBits = dualImpl({
309
- name: "extractBits",
310
- signature: (arg, _offset, _count) => {
311
- const argRestricted = unify([arg], anyConcreteInteger)?.[0];
312
- if (!argRestricted) throw new SignatureNotSupportedError([arg], anyConcreteInteger);
313
- return {
314
- argTypes: [
315
- argRestricted,
316
- u32,
317
- u32
318
- ],
319
- returnType: argRestricted
320
- };
321
- },
322
- normalImpl: "CPU implementation for extractBits not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues",
323
- codegenImpl: (_ctx, [e, offset, count]) => stitch`extractBits(${e}, ${offset}, ${count})`
324
- });
325
- const faceForward = dualImpl({
326
- name: "faceForward",
327
- signature: unifyRestrictedSignature(anyFloatVec),
328
- normalImpl: "CPU implementation for faceForward not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues",
329
- codegenImpl: (_ctx, [e1, e2, e3]) => stitch`faceForward(${e1}, ${e2}, ${e3})`
330
- });
331
- const firstLeadingBit = dualImpl({
332
- name: "firstLeadingBit",
333
- signature: unaryIdentitySignature,
334
- normalImpl: "CPU implementation for firstLeadingBit not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues",
335
- codegenImpl: (_ctx, [value]) => stitch`firstLeadingBit(${value})`
336
- });
337
- const firstTrailingBit = dualImpl({
338
- name: "firstTrailingBit",
339
- signature: unifyRestrictedSignature(anyConcreteInteger),
340
- normalImpl: "CPU implementation for firstTrailingBit not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues",
341
- codegenImpl: (_ctx, [value]) => stitch`firstTrailingBit(${value})`
302
+ return generalizeFn((val) => 2 ** val, [value]);
303
+ }
304
+ export const exp2 = dualImpl({
305
+ name: 'exp2',
306
+ signature: unifyRestrictedSignature(anyFloat),
307
+ normalImpl: cpuExp2,
308
+ codegenImpl: (_ctx, [value]) => stitch `exp2(${value})`,
309
+ sideEffects: false,
310
+ });
311
+ function cpuExtractBits(_e, _offset, _count) {
312
+ throw new Error('Unreachable code. The function is only used for the type.');
313
+ }
314
+ export const extractBits = dualImpl({
315
+ name: 'extractBits',
316
+ signature: (arg, _offset, _count) => {
317
+ const argRestricted = unify([arg], anyConcreteInteger)?.[0];
318
+ if (!argRestricted) {
319
+ throw new SignatureNotSupportedError([arg], anyConcreteInteger);
320
+ }
321
+ return {
322
+ argTypes: [argRestricted, u32, u32],
323
+ returnType: argRestricted,
324
+ };
325
+ },
326
+ normalImpl: 'CPU implementation for extractBits not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues',
327
+ codegenImpl: (_ctx, [e, offset, count]) => stitch `extractBits(${e}, ${offset}, ${count})`,
328
+ sideEffects: false,
329
+ });
330
+ export const faceForward = dualImpl({
331
+ name: 'faceForward',
332
+ signature: unifyRestrictedSignature(anyFloatVec),
333
+ normalImpl: 'CPU implementation for faceForward not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues',
334
+ codegenImpl: (_ctx, [e1, e2, e3]) => stitch `faceForward(${e1}, ${e2}, ${e3})`,
335
+ sideEffects: false,
336
+ });
337
+ function cpuFirstLeadingBit(_value) {
338
+ throw new Error('Unreachable code. The function is only used for the type.');
339
+ }
340
+ export const firstLeadingBit = dualImpl({
341
+ name: 'firstLeadingBit',
342
+ signature: unaryIdentitySignature,
343
+ normalImpl: 'CPU implementation for firstLeadingBit not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues',
344
+ codegenImpl: (_ctx, [value]) => stitch `firstLeadingBit(${value})`,
345
+ sideEffects: false,
346
+ });
347
+ function cpuFirstTrailingBit(_value) {
348
+ throw new Error('Unreachable code. The function is only used for the type.');
349
+ }
350
+ export const firstTrailingBit = dualImpl({
351
+ name: 'firstTrailingBit',
352
+ signature: unifyRestrictedSignature(anyConcreteInteger),
353
+ normalImpl: 'CPU implementation for firstTrailingBit not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues',
354
+ codegenImpl: (_ctx, [value]) => stitch `firstTrailingBit(${value})`,
355
+ sideEffects: false,
342
356
  });
343
357
  function cpuFloor(value) {
344
- if (typeof value === "number") return Math.floor(value);
345
- return VectorOps.floor[value.kind](value);
358
+ return generalizeFn(Math.floor, [value]);
346
359
  }
347
- const floor = dualImpl({
348
- name: "floor",
349
- signature: unifyRestrictedSignature(anyFloat),
350
- normalImpl: cpuFloor,
351
- codegenImpl: (_ctx, [arg]) => stitch`floor(${arg})`
360
+ export const floor = dualImpl({
361
+ name: 'floor',
362
+ signature: unifyRestrictedSignature(anyFloat),
363
+ normalImpl: cpuFloor,
364
+ codegenImpl: (_ctx, [arg]) => stitch `floor(${arg})`,
365
+ sideEffects: false,
352
366
  });
353
367
  function cpuFma(e1, e2, e3) {
354
- if (typeof e1 === "number") return e1 * e2 + e3;
355
- throw new MissingCpuImplError("CPU implementation for fma on vectors not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues");
356
- }
357
- const fma = dualImpl({
358
- name: "fma",
359
- signature: unifyRestrictedSignature(anyFloat),
360
- normalImpl: cpuFma,
361
- codegenImpl: (_ctx, [e1, e2, e3]) => stitch`fma(${e1}, ${e2}, ${e3})`
368
+ if (typeof e1 === 'number') {
369
+ return (e1 * e2 + e3);
370
+ }
371
+ throw new MissingCpuImplError('CPU implementation for fma on vectors not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues');
372
+ }
373
+ export const fma = dualImpl({
374
+ name: 'fma',
375
+ signature: unifyRestrictedSignature(anyFloat),
376
+ normalImpl: cpuFma,
377
+ codegenImpl: (_ctx, [e1, e2, e3]) => stitch `fma(${e1}, ${e2}, ${e3})`,
378
+ sideEffects: false,
362
379
  });
363
380
  function cpuFract(value) {
364
- if (typeof value === "number") return value - Math.floor(value);
365
- return VectorOps.fract[value.kind](value);
381
+ return generalizeFn((value) => value - Math.floor(value), [value]);
366
382
  }
367
- const fract = dualImpl({
368
- name: "fract",
369
- signature: unifyRestrictedSignature(anyFloat),
370
- normalImpl: cpuFract,
371
- codegenImpl: (_ctx, [a]) => stitch`fract(${a})`
383
+ export const fract = dualImpl({
384
+ name: 'fract',
385
+ signature: unifyRestrictedSignature(anyFloat),
386
+ normalImpl: cpuFract,
387
+ codegenImpl: (_ctx, [a]) => stitch `fract(${a})`,
388
+ sideEffects: false,
372
389
  });
373
390
  const FrexpResults = {
374
- f32: abstruct({
375
- fract: f32,
376
- exp: i32
377
- }),
378
- f16: abstruct({
379
- fract: f16,
380
- exp: i32
381
- }),
382
- abstractFloat: abstruct({
383
- fract: abstractFloat,
384
- exp: abstractInt
385
- }),
386
- vec2f: abstruct({
387
- fract: vec2f,
388
- exp: vec2i
389
- }),
390
- vec3f: abstruct({
391
- fract: vec3f,
392
- exp: vec3i
393
- }),
394
- vec4f: abstruct({
395
- fract: vec4f,
396
- exp: vec4i
397
- }),
398
- vec2h: abstruct({
399
- fract: vec2h,
400
- exp: vec2i
401
- }),
402
- vec3h: abstruct({
403
- fract: vec3h,
404
- exp: vec3i
405
- }),
406
- vec4h: abstruct({
407
- fract: vec4h,
408
- exp: vec4i
409
- })
391
+ f32: abstruct({ fract: f32, exp: i32 }),
392
+ f16: abstruct({ fract: f16, exp: i32 }),
393
+ abstractFloat: abstruct({ fract: abstractFloat, exp: abstractInt }),
394
+ vec2f: abstruct({ fract: vec2f, exp: vec2i }),
395
+ vec3f: abstruct({ fract: vec3f, exp: vec3i }),
396
+ vec4f: abstruct({ fract: vec4f, exp: vec4i }),
397
+ vec2h: abstruct({ fract: vec2h, exp: vec2i }),
398
+ vec3h: abstruct({ fract: vec3h, exp: vec3i }),
399
+ vec4h: abstruct({ fract: vec4h, exp: vec4i }),
410
400
  };
411
- const frexp = dualImpl({
412
- name: "frexp",
413
- normalImpl: "CPU implementation for frexp not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues",
414
- signature: (value) => {
415
- const returnType = FrexpResults[value.type];
416
- if (!returnType) throw new SignatureNotSupportedError([value], anyFloat);
417
- return {
418
- argTypes: [value],
419
- returnType
420
- };
421
- },
422
- codegenImpl: (_ctx, [value]) => stitch`frexp(${value})`
423
- });
424
- const insertBits = dualImpl({
425
- name: "insertBits",
426
- signature: (e, newbits, _offset, _count) => {
427
- const uargs = unify([e, newbits], anyConcreteInteger);
428
- if (!uargs) throw new SignatureNotSupportedError([e, newbits], anyConcreteInteger);
429
- return {
430
- argTypes: [
431
- ...uargs,
432
- u32,
433
- u32
434
- ],
435
- returnType: uargs[0]
436
- };
437
- },
438
- normalImpl: "CPU implementation for insertBits not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues",
439
- codegenImpl: (_ctx, [e, newbits, offset, count]) => stitch`insertBits(${e}, ${newbits}, ${offset}, ${count})`
401
+ export const frexp = dualImpl({
402
+ name: 'frexp',
403
+ normalImpl: 'CPU implementation for frexp not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues',
404
+ signature: (value) => {
405
+ const returnType = FrexpResults[value.type];
406
+ if (!returnType) {
407
+ throw new SignatureNotSupportedError([value], anyFloat);
408
+ }
409
+ return { argTypes: [value], returnType };
410
+ },
411
+ codegenImpl: (_ctx, [value]) => stitch `frexp(${value})`,
412
+ sideEffects: false,
413
+ });
414
+ function cpuInsertBits(_e, _newbits, _offset, _count) {
415
+ throw new Error('Unreachable code. The function is only used for the type.');
416
+ }
417
+ export const insertBits = dualImpl({
418
+ name: 'insertBits',
419
+ signature: (e, newbits, _offset, _count) => {
420
+ const uargs = unify([e, newbits], anyConcreteInteger);
421
+ if (!uargs) {
422
+ throw new SignatureNotSupportedError([e, newbits], anyConcreteInteger);
423
+ }
424
+ return {
425
+ argTypes: [...uargs, u32, u32],
426
+ returnType: uargs[0],
427
+ };
428
+ },
429
+ normalImpl: 'CPU implementation for insertBits not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues',
430
+ codegenImpl: (_ctx, [e, newbits, offset, count]) => stitch `insertBits(${e}, ${newbits}, ${offset}, ${count})`,
431
+ sideEffects: false,
440
432
  });
441
433
  function cpuInverseSqrt(value) {
442
- if (typeof value === "number") return 1 / Math.sqrt(value);
443
- throw new MissingCpuImplError("CPU implementation for inverseSqrt on vectors not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues");
444
- }
445
- const inverseSqrt = dualImpl({
446
- name: "inverseSqrt",
447
- signature: unifyRestrictedSignature(anyFloat),
448
- normalImpl: cpuInverseSqrt,
449
- codegenImpl: (_ctx, [value]) => stitch`inverseSqrt(${value})`
450
- });
451
- const ldexp = dualImpl({
452
- name: "ldexp",
453
- signature: (e1, _e2) => {
454
- switch (e1.type) {
455
- case "abstractFloat": return {
456
- argTypes: [e1, abstractInt],
457
- returnType: e1
458
- };
459
- case "f32":
460
- case "f16": return {
461
- argTypes: [e1, i32],
462
- returnType: e1
463
- };
464
- case "vec2f":
465
- case "vec2h": return {
466
- argTypes: [e1, vec2i],
467
- returnType: e1
468
- };
469
- case "vec3f":
470
- case "vec3h": return {
471
- argTypes: [e1, vec3i],
472
- returnType: e1
473
- };
474
- case "vec4f":
475
- case "vec4h": return {
476
- argTypes: [e1, vec4i],
477
- returnType: e1
478
- };
479
- default: throw new Error(`Unsupported data type for ldexp: ${e1.type}. Supported types are abstractFloat, f32, f16, vec2f, vec2h, vec3f, vec3h, vec4f, vec4h.`);
480
- }
481
- },
482
- normalImpl: "CPU implementation for ldexp not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues",
483
- codegenImpl: (_ctx, [e1, e2]) => stitch`ldexp(${e1}, ${e2})`
434
+ if (typeof value === 'number') {
435
+ return (1 / Math.sqrt(value));
436
+ }
437
+ throw new MissingCpuImplError('CPU implementation for inverseSqrt on vectors not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues');
438
+ }
439
+ export const inverseSqrt = dualImpl({
440
+ name: 'inverseSqrt',
441
+ signature: unifyRestrictedSignature(anyFloat),
442
+ normalImpl: cpuInverseSqrt,
443
+ codegenImpl: (_ctx, [value]) => stitch `inverseSqrt(${value})`,
444
+ sideEffects: false,
445
+ });
446
+ function cpuLdexp(_e1, _e2) {
447
+ throw new Error('Unreachable code. The function is only used for the type.');
448
+ }
449
+ export const ldexp = dualImpl({
450
+ name: 'ldexp',
451
+ signature: (e1, _e2) => {
452
+ switch (e1.type) {
453
+ case 'abstractFloat':
454
+ return { argTypes: [e1, abstractInt], returnType: e1 };
455
+ case 'f32':
456
+ case 'f16':
457
+ return { argTypes: [e1, i32], returnType: e1 };
458
+ case 'vec2f':
459
+ case 'vec2h':
460
+ return { argTypes: [e1, vec2i], returnType: e1 };
461
+ case 'vec3f':
462
+ case 'vec3h':
463
+ return { argTypes: [e1, vec3i], returnType: e1 };
464
+ case 'vec4f':
465
+ case 'vec4h':
466
+ return { argTypes: [e1, vec4i], returnType: e1 };
467
+ default:
468
+ throw new Error(`Unsupported data type for ldexp: ${e1.type}. Supported types are abstractFloat, f32, f16, vec2f, vec2h, vec3f, vec3h, vec4f, vec4h.`);
469
+ }
470
+ },
471
+ normalImpl: 'CPU implementation for ldexp not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues',
472
+ codegenImpl: (_ctx, [e1, e2]) => stitch `ldexp(${e1}, ${e2})`,
473
+ sideEffects: false,
484
474
  });
485
475
  function cpuLength(value) {
486
- if (typeof value === "number") return Math.abs(value);
487
- return VectorOps.length[value.kind](value);
488
- }
489
- const length = dualImpl({
490
- name: "length",
491
- signature: (arg) => {
492
- const uarg = unify([arg], anyFloat);
493
- if (!uarg) throw new SignatureNotSupportedError([arg], anyFloat);
494
- return {
495
- argTypes: uarg,
496
- returnType: isHalfPrecisionSchema(uarg[0]) ? f16 : f32
497
- };
498
- },
499
- normalImpl: cpuLength,
500
- codegenImpl: (_ctx, [arg]) => stitch`length(${arg})`
476
+ if (typeof value === 'number') {
477
+ return Math.abs(value);
478
+ }
479
+ return VectorOps.length[value.kind](value);
480
+ }
481
+ export const length = dualImpl({
482
+ name: 'length',
483
+ signature: (arg) => {
484
+ const uarg = unify([arg], anyFloat);
485
+ if (!uarg) {
486
+ throw new SignatureNotSupportedError([arg], anyFloat);
487
+ }
488
+ return {
489
+ argTypes: uarg,
490
+ returnType: isHalfPrecisionSchema(uarg[0]) ? f16 : f32,
491
+ };
492
+ },
493
+ normalImpl: cpuLength,
494
+ codegenImpl: (_ctx, [arg]) => stitch `length(${arg})`,
495
+ sideEffects: false,
501
496
  });
502
497
  function cpuLog(value) {
503
- if (typeof value === "number") return Math.log(value);
504
- return VectorOps.log[value.kind](value);
498
+ return generalizeFn(Math.log, [value]);
505
499
  }
506
- const log = dualImpl({
507
- name: "log",
508
- signature: unifyRestrictedSignature(anyFloat),
509
- normalImpl: cpuLog,
510
- codegenImpl: (_ctx, [value]) => stitch`log(${value})`
500
+ export const log = dualImpl({
501
+ name: 'log',
502
+ signature: unifyRestrictedSignature(anyFloat),
503
+ normalImpl: cpuLog,
504
+ codegenImpl: (_ctx, [value]) => stitch `log(${value})`,
505
+ sideEffects: false,
511
506
  });
512
507
  function cpuLog2(value) {
513
- if (typeof value === "number") return Math.log2(value);
514
- return VectorOps.log2[value.kind](value);
508
+ return generalizeFn(Math.log2, [value]);
515
509
  }
516
- const log2 = dualImpl({
517
- name: "log2",
518
- signature: unifyRestrictedSignature(anyFloat),
519
- normalImpl: cpuLog2,
520
- codegenImpl: (_ctx, [value]) => stitch`log2(${value})`
510
+ export const log2 = dualImpl({
511
+ name: 'log2',
512
+ signature: unifyRestrictedSignature(anyFloat),
513
+ normalImpl: cpuLog2,
514
+ codegenImpl: (_ctx, [value]) => stitch `log2(${value})`,
515
+ sideEffects: false,
521
516
  });
522
517
  function cpuMax(a, b) {
523
- if (typeof a === "number") return Math.max(a, b);
524
- return VectorOps.max[a.kind](a, b);
518
+ return generalizeFn(Math.max, [a, b]);
525
519
  }
526
- const max = dualImpl({
527
- name: "max",
528
- signature: variadicUnifySignature,
529
- normalImpl: variadicReduce(cpuMax),
530
- codegenImpl: variadicStitch("max")
520
+ export const max = dualImpl({
521
+ name: 'max',
522
+ signature: variadicUnifySignature,
523
+ normalImpl: variadicReduce(cpuMax),
524
+ codegenImpl: variadicStitch('max'),
525
+ sideEffects: false,
531
526
  });
532
527
  function cpuMin(a, b) {
533
- if (typeof a === "number") return Math.min(a, b);
534
- return VectorOps.min[a.kind](a, b);
528
+ return generalizeFn(Math.min, [a, b]);
535
529
  }
536
- const min = dualImpl({
537
- name: "min",
538
- signature: variadicUnifySignature,
539
- normalImpl: variadicReduce(cpuMin),
540
- codegenImpl: variadicStitch("min")
530
+ export const min = dualImpl({
531
+ name: 'min',
532
+ signature: variadicUnifySignature,
533
+ normalImpl: variadicReduce(cpuMin),
534
+ codegenImpl: variadicStitch('min'),
535
+ sideEffects: false,
541
536
  });
542
537
  function cpuMix(e1, e2, e3) {
543
- if (typeof e1 === "number") {
544
- if (typeof e3 !== "number" || typeof e2 !== "number") throw new Error("When e1 and e2 are numbers, the blend factor must be a number.");
545
- return e1 * (1 - e3) + e2 * e3;
546
- }
547
- if (typeof e1 === "number" || typeof e2 === "number") throw new Error("e1 and e2 need to both be vectors of the same kind.");
548
- return VectorOps.mix[e1.kind](e1, e2, e3);
549
- }
550
- const mix = dualImpl({
551
- name: "mix",
552
- signature: (e1, e2, e3) => {
553
- if (e1.type.startsWith("vec") && !e3.type.startsWith("vec")) {
554
- const uarg = unify([e3], [e1.primitive]);
555
- if (!uarg) throw new SignatureNotSupportedError([e3], [e1.primitive]);
556
- return {
557
- argTypes: [
558
- e1,
559
- e2,
560
- uarg[0]
561
- ],
562
- returnType: e1
563
- };
564
- }
565
- const uargs = unify([
566
- e1,
567
- e2,
568
- e3
569
- ], anyFloat);
570
- if (!uargs) throw new SignatureNotSupportedError([
571
- e1,
572
- e2,
573
- e3
574
- ], anyFloat);
575
- return {
576
- argTypes: uargs,
577
- returnType: uargs[0]
578
- };
579
- },
580
- normalImpl: cpuMix,
581
- codegenImpl: (_ctx, [e1, e2, e3]) => stitch`mix(${e1}, ${e2}, ${e3})`
538
+ return generalizeFn((e1, e2, e3) => e1 * (1 - e3) + e2 * e3, [e1, ...upCast([e2, e3])]);
539
+ }
540
+ export const mix = dualImpl({
541
+ name: 'mix',
542
+ signature: (e1, e2, e3) => {
543
+ if (e1.type.startsWith('vec') && !e3.type.startsWith('vec')) {
544
+ const uarg = unify([e3], [e1.primitive]);
545
+ if (!uarg) {
546
+ throw new SignatureNotSupportedError([e3], [e1.primitive]);
547
+ }
548
+ return { argTypes: [e1, e2, uarg[0]], returnType: e1 };
549
+ }
550
+ const uargs = unify([e1, e2, e3], anyFloat);
551
+ if (!uargs) {
552
+ throw new SignatureNotSupportedError([e1, e2, e3], anyFloat);
553
+ }
554
+ return { argTypes: uargs, returnType: uargs[0] };
555
+ },
556
+ normalImpl: cpuMix,
557
+ codegenImpl: (_ctx, [e1, e2, e3]) => stitch `mix(${e1}, ${e2}, ${e3})`,
558
+ sideEffects: false,
582
559
  });
583
560
  const ModfResult = {
584
- f32: abstruct({
585
- fract: f32,
586
- whole: f32
587
- }),
588
- f16: abstruct({
589
- fract: f16,
590
- whole: f16
591
- }),
592
- abstractFloat: abstruct({
593
- fract: abstractFloat,
594
- whole: abstractFloat
595
- }),
596
- vec2f: abstruct({
597
- fract: vec2f,
598
- whole: vec2f
599
- }),
600
- vec3f: abstruct({
601
- fract: vec3f,
602
- whole: vec3f
603
- }),
604
- vec4f: abstruct({
605
- fract: vec4f,
606
- whole: vec4f
607
- }),
608
- vec2h: abstruct({
609
- fract: vec2h,
610
- whole: vec2h
611
- }),
612
- vec3h: abstruct({
613
- fract: vec3h,
614
- whole: vec3h
615
- }),
616
- vec4h: abstruct({
617
- fract: vec4h,
618
- whole: vec4h
619
- })
561
+ f32: abstruct({ fract: f32, whole: f32 }),
562
+ f16: abstruct({ fract: f16, whole: f16 }),
563
+ abstractFloat: abstruct({ fract: abstractFloat, whole: abstractFloat }),
564
+ vec2f: abstruct({ fract: vec2f, whole: vec2f }),
565
+ vec3f: abstruct({ fract: vec3f, whole: vec3f }),
566
+ vec4f: abstruct({ fract: vec4f, whole: vec4f }),
567
+ vec2h: abstruct({ fract: vec2h, whole: vec2h }),
568
+ vec3h: abstruct({ fract: vec3h, whole: vec3h }),
569
+ vec4h: abstruct({ fract: vec4h, whole: vec4h }),
620
570
  };
621
- const modf = dualImpl({
622
- name: "modf",
623
- signature: (e) => {
624
- const returnType = ModfResult[e.type];
625
- if (!returnType) throw new Error(`Unsupported data type for modf: ${e.type}. Supported types are f32, f16, abstractFloat, vec2f, vec3f, vec4f, vec2h, vec3h, vec4h.`);
626
- return {
627
- argTypes: [e],
628
- returnType
629
- };
630
- },
631
- normalImpl: "CPU implementation for modf not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues",
632
- codegenImpl: (_ctx, [value]) => stitch`modf(${value})`
633
- });
634
- const normalize = dualImpl({
635
- name: "normalize",
636
- signature: unifyRestrictedSignature(anyFloatVec),
637
- normalImpl: (v) => VectorOps.normalize[v.kind](v),
638
- codegenImpl: (_ctx, [value]) => stitch`normalize(${value})`
571
+ function cpuModf(_value) {
572
+ throw new Error('Unreachable code. The function is only used for the type.');
573
+ }
574
+ export const modf = dualImpl({
575
+ name: 'modf',
576
+ signature: (e) => {
577
+ const returnType = ModfResult[e.type];
578
+ if (!returnType) {
579
+ throw new Error(`Unsupported data type for modf: ${e.type}. Supported types are f32, f16, abstractFloat, vec2f, vec3f, vec4f, vec2h, vec3h, vec4h.`);
580
+ }
581
+ return { argTypes: [e], returnType };
582
+ },
583
+ normalImpl: 'CPU implementation for modf not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues',
584
+ codegenImpl: (_ctx, [value]) => stitch `modf(${value})`,
585
+ sideEffects: false,
586
+ });
587
+ export const normalize = dualImpl({
588
+ name: 'normalize',
589
+ signature: unifyRestrictedSignature(anyFloatVec),
590
+ normalImpl: (v) => {
591
+ const len = length(v);
592
+ return generalizeFn((e) => e / len, [v]);
593
+ },
594
+ codegenImpl: (_ctx, [value]) => stitch `normalize(${value})`,
595
+ sideEffects: false,
639
596
  });
640
597
  function powCpu(base, exponent) {
641
- if (typeof base === "number" && typeof exponent === "number") return base ** exponent;
642
- if (isVecInstance(base) && isVecInstance(exponent)) return VectorOps.pow[base.kind](base, exponent);
643
- throw new Error(`Invalid arguments to pow(): '${base}' '${exponent}'`);
644
- }
645
- const pow = dualImpl({
646
- name: "pow",
647
- signature: unifyRestrictedSignature(anyFloat),
648
- normalImpl: powCpu,
649
- codegenImpl: (_ctx, [lhs, rhs]) => stitch`pow(${lhs}, ${rhs})`
650
- });
651
- const quantizeToF16 = dualImpl({
652
- name: "quantizeToF16",
653
- signature: (arg) => {
654
- const candidates = [
655
- vec2f,
656
- vec3f,
657
- vec4f,
658
- f32
659
- ];
660
- const uarg = unify([arg], candidates)?.[0];
661
- if (!uarg) throw new SignatureNotSupportedError([arg], candidates);
662
- return {
663
- argTypes: [uarg],
664
- returnType: uarg
665
- };
666
- },
667
- normalImpl: "CPU implementation for quantizeToF16 not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues",
668
- codegenImpl: (_ctx, [value]) => stitch`quantizeToF16(${value})`
598
+ return generalizeFn((a, b) => a ** b, [base, exponent]);
599
+ }
600
+ export const pow = dualImpl({
601
+ name: 'pow',
602
+ signature: unifyRestrictedSignature(anyFloat),
603
+ normalImpl: powCpu,
604
+ codegenImpl: (_ctx, [lhs, rhs]) => stitch `pow(${lhs}, ${rhs})`,
605
+ sideEffects: false,
606
+ });
607
+ function cpuQuantizeToF16(_value) {
608
+ throw new Error('Unreachable code. The function is only used for the type.');
609
+ }
610
+ export const quantizeToF16 = dualImpl({
611
+ name: 'quantizeToF16',
612
+ signature: (arg) => {
613
+ const candidates = [vec2f, vec3f, vec4f, f32];
614
+ const uarg = unify([arg], candidates)?.[0];
615
+ if (!uarg) {
616
+ throw new SignatureNotSupportedError([arg], candidates);
617
+ }
618
+ return { argTypes: [uarg], returnType: uarg };
619
+ },
620
+ normalImpl: 'CPU implementation for quantizeToF16 not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues',
621
+ codegenImpl: (_ctx, [value]) => stitch `quantizeToF16(${value})`,
622
+ sideEffects: false,
669
623
  });
670
624
  function cpuRadians(value) {
671
- if (typeof value === "number") return value * Math.PI / 180;
672
- throw new MissingCpuImplError("CPU implementation for radians on vectors not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues");
673
- }
674
- const radians = dualImpl({
675
- name: "radians",
676
- signature: unifyRestrictedSignature(anyFloat),
677
- normalImpl: cpuRadians,
678
- codegenImpl: (_ctx, [value]) => stitch`radians(${value})`
679
- });
680
- const reflect = dualImpl({
681
- name: "reflect",
682
- signature: (...args) => {
683
- const uargs = unify(args, anyFloatVec);
684
- if (!uargs) throw new SignatureNotSupportedError(args, anyFloatVec);
685
- return {
686
- argTypes: uargs,
687
- returnType: uargs[0]
688
- };
689
- },
690
- normalImpl: (e1, e2) => sub(e1, mul(2 * dot(e2, e1), e2)),
691
- codegenImpl: (_ctx, [e1, e2]) => stitch`reflect(${e1}, ${e2})`
692
- });
693
- const refract = dualImpl({
694
- name: "refract",
695
- normalImpl: "CPU implementation for refract not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues",
696
- codegenImpl: (_ctx, [e1, e2, e3]) => stitch`refract(${e1}, ${e2}, ${e3})`,
697
- signature: (e1, e2, _e3) => ({
698
- argTypes: [
699
- e1,
700
- e2,
701
- isHalfPrecisionSchema(e1) ? f16 : f32
702
- ],
703
- returnType: e1
704
- })
705
- });
706
- const reverseBits = dualImpl({
707
- name: "reverseBits",
708
- signature: unifyRestrictedSignature(anyConcreteInteger),
709
- normalImpl: "CPU implementation for reverseBits not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues",
710
- codegenImpl: (_ctx, [value]) => stitch`reverseBits(${value})`
625
+ if (typeof value === 'number') {
626
+ return ((value * Math.PI) / 180);
627
+ }
628
+ throw new MissingCpuImplError('CPU implementation for radians on vectors not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues');
629
+ }
630
+ export const radians = dualImpl({
631
+ name: 'radians',
632
+ signature: unifyRestrictedSignature(anyFloat),
633
+ normalImpl: cpuRadians,
634
+ codegenImpl: (_ctx, [value]) => stitch `radians(${value})`,
635
+ sideEffects: false,
636
+ });
637
+ export const reflect = dualImpl({
638
+ name: 'reflect',
639
+ signature: (...args) => {
640
+ const uargs = unify(args, anyFloatVec);
641
+ if (!uargs) {
642
+ throw new SignatureNotSupportedError(args, anyFloatVec);
643
+ }
644
+ return {
645
+ argTypes: uargs,
646
+ returnType: uargs[0],
647
+ };
648
+ },
649
+ normalImpl: (e1, e2) => sub(e1, mul(2 * dot(e2, e1), e2)),
650
+ codegenImpl: (_ctx, [e1, e2]) => stitch `reflect(${e1}, ${e2})`,
651
+ sideEffects: false,
652
+ });
653
+ export const refract = dualImpl({
654
+ name: 'refract',
655
+ normalImpl: 'CPU implementation for refract not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues',
656
+ codegenImpl: (_ctx, [e1, e2, e3]) => stitch `refract(${e1}, ${e2}, ${e3})`,
657
+ signature: (e1, e2, _e3) => ({
658
+ argTypes: [e1, e2, isHalfPrecisionSchema(e1) ? f16 : f32],
659
+ returnType: e1,
660
+ }),
661
+ sideEffects: false,
662
+ });
663
+ function cpuReverseBits(_value) {
664
+ throw new Error('Unreachable code. The function is only used for the type.');
665
+ }
666
+ export const reverseBits = dualImpl({
667
+ name: 'reverseBits',
668
+ signature: unifyRestrictedSignature(anyConcreteInteger),
669
+ normalImpl: 'CPU implementation for reverseBits not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues',
670
+ codegenImpl: (_ctx, [value]) => stitch `reverseBits(${value})`,
671
+ sideEffects: false,
711
672
  });
712
673
  function cpuRound(value) {
713
- if (typeof value === "number") {
714
- const floor$1 = Math.floor(value);
715
- if (value === floor$1 + .5) {
716
- if (floor$1 % 2 === 0) return floor$1;
717
- return floor$1 + 1;
718
- }
719
- return Math.round(value);
720
- }
721
- throw new MissingCpuImplError("CPU implementation for round on vectors not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues");
722
- }
723
- const round = dualImpl({
724
- name: "round",
725
- signature: unifyRestrictedSignature(anyFloat),
726
- normalImpl: cpuRound,
727
- codegenImpl: (_ctx, [value]) => stitch`round(${value})`
674
+ if (typeof value === 'number') {
675
+ const floor = Math.floor(value);
676
+ if (value === floor + 0.5) {
677
+ if (floor % 2 === 0) {
678
+ return floor;
679
+ }
680
+ return (floor + 1);
681
+ }
682
+ return Math.round(value);
683
+ }
684
+ throw new MissingCpuImplError('CPU implementation for round on vectors not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues');
685
+ }
686
+ export const round = dualImpl({
687
+ name: 'round',
688
+ signature: unifyRestrictedSignature(anyFloat),
689
+ normalImpl: cpuRound,
690
+ codegenImpl: (_ctx, [value]) => stitch `round(${value})`,
691
+ sideEffects: false,
728
692
  });
729
693
  function cpuSaturate(value) {
730
- if (typeof value === "number") return Math.max(0, Math.min(1, value));
731
- throw new MissingCpuImplError("CPU implementation for saturate on vectors not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues");
732
- }
733
- const saturate = dualImpl({
734
- name: "saturate",
735
- signature: unifyRestrictedSignature(anyFloat),
736
- normalImpl: cpuSaturate,
737
- codegenImpl: (_ctx, [value]) => stitch`saturate(${value})`
694
+ if (typeof value === 'number') {
695
+ return Math.max(0, Math.min(1, value));
696
+ }
697
+ throw new MissingCpuImplError('CPU implementation for saturate on vectors not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues');
698
+ }
699
+ export const saturate = dualImpl({
700
+ name: 'saturate',
701
+ signature: unifyRestrictedSignature(anyFloat),
702
+ normalImpl: cpuSaturate,
703
+ codegenImpl: (ctx, [value]) => ctx.gen.emitCall('saturate', [], [value]),
704
+ sideEffects: false,
738
705
  });
739
706
  function cpuSign(e) {
740
- if (typeof e === "number") return Math.sign(e);
741
- return VectorOps.sign[e.kind](e);
742
- }
743
- const sign = dualImpl({
744
- name: "sign",
745
- signature: (arg) => {
746
- const candidates = [
747
- ...anyFloat,
748
- i32,
749
- vec2i,
750
- vec3i,
751
- vec4i
752
- ];
753
- const uarg = unify([arg], candidates)?.[0];
754
- if (!uarg) throw new SignatureNotSupportedError([arg], candidates);
755
- return {
756
- argTypes: [uarg],
757
- returnType: uarg
758
- };
759
- },
760
- normalImpl: cpuSign,
761
- codegenImpl: (_ctx, [e]) => stitch`sign(${e})`
707
+ return generalizeFn(Math.sign, [e]);
708
+ }
709
+ export const sign = dualImpl({
710
+ name: 'sign',
711
+ signature: (arg) => {
712
+ const candidates = [...anyFloat, i32, vec2i, vec3i, vec4i];
713
+ const uarg = unify([arg], candidates)?.[0];
714
+ if (!uarg) {
715
+ throw new SignatureNotSupportedError([arg], candidates);
716
+ }
717
+ return { argTypes: [uarg], returnType: uarg };
718
+ },
719
+ normalImpl: cpuSign,
720
+ codegenImpl: (_ctx, [e]) => stitch `sign(${e})`,
721
+ sideEffects: false,
762
722
  });
763
723
  function cpuSin(value) {
764
- if (typeof value === "number") return Math.sin(value);
765
- return VectorOps.sin[value.kind](value);
724
+ return generalizeFn(Math.sin, [value]);
766
725
  }
767
- const sin = dualImpl({
768
- name: "sin",
769
- signature: unifyRestrictedSignature(anyFloat),
770
- normalImpl: cpuSin,
771
- codegenImpl: (_ctx, [value]) => stitch`sin(${value})`
726
+ export const sin = dualImpl({
727
+ name: 'sin',
728
+ signature: unifyRestrictedSignature(anyFloat),
729
+ normalImpl: cpuSin,
730
+ codegenImpl: (_ctx, [value]) => stitch `sin(${value})`,
731
+ sideEffects: false,
772
732
  });
773
733
  function cpuSinh(value) {
774
- if (typeof value === "number") return Math.sinh(value);
775
- throw new MissingCpuImplError("CPU implementation for sinh on vectors not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues");
734
+ return generalizeFn(Math.sinh, [value]);
776
735
  }
777
- const sinh = dualImpl({
778
- name: "sinh",
779
- signature: unifyRestrictedSignature(anyFloat),
780
- normalImpl: cpuSinh,
781
- codegenImpl: (_ctx, [value]) => stitch`sinh(${value})`
736
+ export const sinh = dualImpl({
737
+ name: 'sinh',
738
+ signature: unifyRestrictedSignature(anyFloat),
739
+ normalImpl: cpuSinh,
740
+ codegenImpl: (_ctx, [value]) => stitch `sinh(${value})`,
741
+ sideEffects: false,
782
742
  });
783
743
  function cpuSmoothstep(edge0, edge1, x) {
784
- if (typeof x === "number") return smoothstepScalar(edge0, edge1, x);
785
- return VectorOps.smoothstep[x.kind](edge0, edge1, x);
744
+ return generalizeFn(smoothstepScalar, [edge0, edge1, x]);
786
745
  }
787
- const smoothstep = dualImpl({
788
- name: "smoothstep",
789
- signature: unifyRestrictedSignature(anyFloat),
790
- normalImpl: cpuSmoothstep,
791
- codegenImpl: (_ctx, [edge0, edge1, x]) => stitch`smoothstep(${edge0}, ${edge1}, ${x})`
746
+ export const smoothstep = dualImpl({
747
+ name: 'smoothstep',
748
+ signature: unifyRestrictedSignature(anyFloat),
749
+ normalImpl: cpuSmoothstep,
750
+ codegenImpl: (_ctx, [edge0, edge1, x]) => stitch `smoothstep(${edge0}, ${edge1}, ${x})`,
751
+ sideEffects: false,
792
752
  });
793
753
  function cpuSqrt(value) {
794
- if (typeof value === "number") return Math.sqrt(value);
795
- return VectorOps.sqrt[value.kind](value);
754
+ return generalizeFn(Math.sqrt, [value]);
796
755
  }
797
- const sqrt = dualImpl({
798
- name: "sqrt",
799
- signature: unifyRestrictedSignature(anyFloat),
800
- normalImpl: cpuSqrt,
801
- codegenImpl: (_ctx, [value]) => stitch`sqrt(${value})`
756
+ export const sqrt = dualImpl({
757
+ name: 'sqrt',
758
+ signature: unifyRestrictedSignature(anyFloat),
759
+ normalImpl: cpuSqrt,
760
+ codegenImpl: (_ctx, [value]) => stitch `sqrt(${value})`,
761
+ sideEffects: false,
802
762
  });
803
763
  function cpuStep(edge, x) {
804
- if (typeof edge === "number") return edge <= x ? 1 : 0;
805
- throw new MissingCpuImplError("CPU implementation for step on vectors not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues");
806
- }
807
- const step = dualImpl({
808
- name: "step",
809
- signature: unifyRestrictedSignature(anyFloat),
810
- normalImpl: cpuStep,
811
- codegenImpl: (_ctx, [edge, x]) => stitch`step(${edge}, ${x})`
764
+ if (typeof edge === 'number') {
765
+ return (edge <= x ? 1.0 : 0.0);
766
+ }
767
+ throw new MissingCpuImplError('CPU implementation for step on vectors not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues');
768
+ }
769
+ export const step = dualImpl({
770
+ name: 'step',
771
+ signature: unifyRestrictedSignature(anyFloat),
772
+ normalImpl: cpuStep,
773
+ codegenImpl: (_ctx, [edge, x]) => stitch `step(${edge}, ${x})`,
774
+ sideEffects: false,
812
775
  });
813
776
  function cpuTan(value) {
814
- if (typeof value === "number") return Math.tan(value);
815
- throw new MissingCpuImplError("CPU implementation for tan on vectors not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues");
816
- }
817
- const tan = dualImpl({
818
- name: "tan",
819
- signature: unifyRestrictedSignature(anyFloat),
820
- normalImpl: cpuTan,
821
- codegenImpl: (_ctx, [value]) => stitch`tan(${value})`
777
+ if (typeof value === 'number') {
778
+ return Math.tan(value);
779
+ }
780
+ throw new MissingCpuImplError('CPU implementation for tan on vectors not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues');
781
+ }
782
+ export const tan = dualImpl({
783
+ name: 'tan',
784
+ signature: unifyRestrictedSignature(anyFloat),
785
+ normalImpl: cpuTan,
786
+ codegenImpl: (_ctx, [value]) => stitch `tan(${value})`,
787
+ sideEffects: false,
822
788
  });
823
789
  function cpuTanh(value) {
824
- if (typeof value === "number") return Math.tanh(value);
825
- return VectorOps.tanh[value.kind](value);
826
- }
827
- const tanh = dualImpl({
828
- name: "tanh",
829
- signature: unifyRestrictedSignature(anyFloat),
830
- normalImpl: cpuTanh,
831
- codegenImpl: (_ctx, [value]) => stitch`tanh(${value})`
832
- });
833
- const transpose = dualImpl({
834
- name: "transpose",
835
- signature: unaryIdentitySignature,
836
- normalImpl: "CPU implementation for transpose not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues",
837
- codegenImpl: (_ctx, [e]) => stitch`transpose(${e})`
838
- });
839
- const trunc = dualImpl({
840
- name: "trunc",
841
- signature: unifyRestrictedSignature(anyFloat),
842
- normalImpl: "CPU implementation for trunc not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues",
843
- codegenImpl: (_ctx, [value]) => stitch`trunc(${value})`
844
- });
845
-
846
- //#endregion
847
- export { abs, acos, acosh, asin, asinh, atan, atan2, atanh, ceil, clamp, cos, cosh, countLeadingZeros, countOneBits, countTrailingZeros, cross, degrees, determinant, distance, dot, dot4I8Packed, dot4U8Packed, exp, exp2, extractBits, faceForward, firstLeadingBit, firstTrailingBit, floor, fma, fract, frexp, insertBits, inverseSqrt, ldexp, length, log, log2, max, min, mix, modf, normalize, pow, quantizeToF16, radians, reflect, refract, reverseBits, round, saturate, sign, sin, sinh, smoothstep, sqrt, step, tan, tanh, transpose, trunc };
790
+ return generalizeFn(Math.tanh, [value]);
791
+ }
792
+ export const tanh = dualImpl({
793
+ name: 'tanh',
794
+ signature: unifyRestrictedSignature(anyFloat),
795
+ normalImpl: cpuTanh,
796
+ codegenImpl: (_ctx, [value]) => stitch `tanh(${value})`,
797
+ sideEffects: false,
798
+ });
799
+ function cpuTranspose(value) {
800
+ const schema = WORKAROUND_getSchema(value);
801
+ // NOTE: This assumes all matrices are square
802
+ const transposed = schema();
803
+ const src = value.columns;
804
+ const dst = transposed.columns;
805
+ if (src.length === 2) {
806
+ dst[0][0] = src[0][0];
807
+ dst[0][1] = src[1][0];
808
+ dst[1][0] = src[0][1];
809
+ dst[1][1] = src[1][1];
810
+ }
811
+ else if (src.length === 3) {
812
+ dst[0][0] = src[0][0];
813
+ dst[0][1] = src[1][0];
814
+ dst[0][2] = src[2][0];
815
+ dst[1][0] = src[0][1];
816
+ dst[1][1] = src[1][1];
817
+ dst[1][2] = src[2][1];
818
+ // oxlint-disable-next-line typescript/no-non-null-assertion
819
+ const dst2 = dst[2];
820
+ dst2[0] = src[0][2];
821
+ dst2[1] = src[1][2];
822
+ dst2[2] = src[2][2];
823
+ }
824
+ else if (src.length === 4) {
825
+ dst[0][0] = src[0][0];
826
+ dst[0][1] = src[1][0];
827
+ dst[0][2] = src[2][0];
828
+ dst[0][3] = src[3][0];
829
+ dst[1][0] = src[0][1];
830
+ dst[1][1] = src[1][1];
831
+ dst[1][2] = src[2][1];
832
+ dst[1][3] = src[3][1];
833
+ // oxlint-disable-next-line typescript/no-non-null-assertion
834
+ const dst2 = dst[2];
835
+ dst2[0] = src[0][2];
836
+ dst2[1] = src[1][2];
837
+ dst2[2] = src[2][2];
838
+ dst2[3] = src[3][2];
839
+ // oxlint-disable-next-line typescript/no-non-null-assertion
840
+ const dst3 = dst[3];
841
+ dst3[0] = src[0][3];
842
+ dst3[1] = src[1][3];
843
+ dst3[2] = src[2][3];
844
+ dst3[3] = src[3][3];
845
+ }
846
+ else {
847
+ assertExhaustive(src, 'std/numeric.ts#cpuTranspose');
848
+ }
849
+ return transposed;
850
+ }
851
+ export const transpose = dualImpl({
852
+ name: 'transpose',
853
+ signature: unaryIdentitySignature,
854
+ normalImpl: cpuTranspose,
855
+ codegenImpl: (_ctx, [e]) => stitch `transpose(${e})`,
856
+ sideEffects: false,
857
+ });
858
+ function cpuTrunc(_value) {
859
+ throw new Error('Unreachable code. The function is only used for the type.');
860
+ }
861
+ export const trunc = dualImpl({
862
+ name: 'trunc',
863
+ signature: unifyRestrictedSignature(anyFloat),
864
+ normalImpl: 'CPU implementation for trunc not implemented yet. Please submit an issue at https://github.com/software-mansion/TypeGPU/issues',
865
+ codegenImpl: (_ctx, [value]) => stitch `trunc(${value})`,
866
+ sideEffects: false,
867
+ });
868
+ function cpuIntdiv(lhs, rhs) {
869
+ if (typeof lhs !== 'number' || typeof rhs !== 'number') {
870
+ throw new Error('std.intdiv called with invalid arguments.');
871
+ }
872
+ return Math.trunc(Math.trunc(lhs) / Math.trunc(rhs));
873
+ }
874
+ /**
875
+ * Performs integer division on the passed in scalars.
876
+ * Equivalent to `trunc(trunc(lhs) / trunc(rhs))`. Coerces both
877
+ * arguments to integers if they're floating point.
878
+ */
879
+ export const intdiv = dualImpl({
880
+ name: 'intdiv',
881
+ signature: (lhs, rhs) => {
882
+ const unified = unify([lhs, rhs], [u32, i32]);
883
+ if (!unified) {
884
+ throw new SignatureNotSupportedError([lhs, rhs], [u32, i32, abstractInt]);
885
+ }
886
+ return { argTypes: unified, returnType: unified[0] };
887
+ },
888
+ normalImpl: cpuIntdiv,
889
+ codegenImpl: (ctx, [lhs, rhs]) => ctx.gen.emitBinaryOp(lhs, '/', rhs),
890
+ sideEffects: false,
891
+ });