feat: Handle compatible Math functions in TypeGPU functions#2152
Conversation
📊 Bundle Size Comparison
👀 Notable resultsStatic test results:No major changes. Dynamic test results:No major changes. 📋 All resultsClick to reveal the results table (335 entries).
If you wish to run a comparison for other, slower bundlers, run the 'Tree-shake test' from the GitHub Actions menu. |
|
pkg.pr.new packages benchmark commit |
There was a problem hiding this comment.
Pull request overview
This PR extends TypeGPU’s TGSL resolution so that certain Math.* function member accesses inside GPU functions are treated as compatible std/WGSL equivalents, updates documentation to reflect the new behavior, and fixes the CPU implementation of std.round to match WGSL “ties-to-even” rounding.
Changes:
- Add
Math.*→std.*function mapping during member access resolution (e.g.Math.sin→std.sin). - Fix
std.roundCPU behavior to use round-to-even for.5ties and add a dedicated unit test. - Add tests for supported/unsupported
Mathusage patterns and update docs guidance.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 12 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/typegpu/src/tgsl/wgslGenerator.ts | Intercepts Math member access to return std-callable snippets or throw for unsupported Math functions. |
| packages/typegpu/src/tgsl/math.ts | Introduces the mathToStd mapping table used by the generator. |
| packages/typegpu/src/std/numeric.ts | Adjusts CPU round implementation to match WGSL tie-to-even rounding. |
| packages/typegpu/tests/jsMath.test.ts | Adds coverage for Math.PI, Math.sin (including coercion and precompute), variadic Math.min, Math.fround, and unsupported error messaging. |
| packages/typegpu/tests/std/numeric/round.test.ts | Adds a targeted test validating tie-to-even behavior for std.round, plus WGSL emission snapshot. |
| apps/typegpu-docs/src/content/docs/fundamentals/functions/index.mdx | Updates docs to mention Math support and cautions about using it vs std. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
apps/typegpu-docs/src/content/docs/fundamentals/functions/index.mdx
Outdated
Show resolved
Hide resolved
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Right now, the conversion happens during Math prop access (just like console). If we want to also support the functions directly, (e.g. when the user uses
const { sin, cos } = Math, we can rewrite both in another ticket. Though, if we do so, we will not be able to lint this as discouraged.I patched the std.round cpu impl, since it was not compliant with the spec.