Skip to content

feat: Route binary operations through the shader generator - #2822

Open
iwoplaza wants to merge 1 commit into
feat/glsl-implicit-pointer-definitionsfrom
feat/route-bin-ops-through-generator
Open

feat: Route binary operations through the shader generator#2822
iwoplaza wants to merge 1 commit into
feat/glsl-implicit-pointer-definitionsfrom
feat/route-bin-ops-through-generator

Conversation

@iwoplaza

Copy link
Copy Markdown
Collaborator

No description provided.

@github-actions

github-actions Bot commented Aug 10, 2026

Copy link
Copy Markdown

pkg.pr.new

packages
Ready to be installed by your favorite package manager ⬇️

https://pkg.pr.new/software-mansion/TypeGPU/eslint-plugin-typegpu@74bc6ffe5212463eb14cf5feca0c024c7f0a67cc
https://pkg.pr.new/software-mansion/TypeGPU/tgpu-gen@74bc6ffe5212463eb14cf5feca0c024c7f0a67cc
https://pkg.pr.new/software-mansion/TypeGPU/tinyest-for-wgsl@74bc6ffe5212463eb14cf5feca0c024c7f0a67cc
https://pkg.pr.new/software-mansion/TypeGPU/typegpu@74bc6ffe5212463eb14cf5feca0c024c7f0a67cc
https://pkg.pr.new/software-mansion/TypeGPU/@typegpu/cli@74bc6ffe5212463eb14cf5feca0c024c7f0a67cc
https://pkg.pr.new/software-mansion/TypeGPU/@typegpu/color@74bc6ffe5212463eb14cf5feca0c024c7f0a67cc
https://pkg.pr.new/software-mansion/TypeGPU/@typegpu/gl@74bc6ffe5212463eb14cf5feca0c024c7f0a67cc
https://pkg.pr.new/software-mansion/TypeGPU/@typegpu/noise@74bc6ffe5212463eb14cf5feca0c024c7f0a67cc
https://pkg.pr.new/software-mansion/TypeGPU/@typegpu/radiance-cascades@74bc6ffe5212463eb14cf5feca0c024c7f0a67cc
https://pkg.pr.new/software-mansion/TypeGPU/@typegpu/react@74bc6ffe5212463eb14cf5feca0c024c7f0a67cc
https://pkg.pr.new/software-mansion/TypeGPU/@typegpu/sdf@74bc6ffe5212463eb14cf5feca0c024c7f0a67cc
https://pkg.pr.new/software-mansion/TypeGPU/@typegpu/three@74bc6ffe5212463eb14cf5feca0c024c7f0a67cc
https://pkg.pr.new/software-mansion/TypeGPU/unplugin-typegpu@74bc6ffe5212463eb14cf5feca0c024c7f0a67cc

benchmark
view benchmark

commit
view commit

@iwoplaza
iwoplaza force-pushed the feat/route-bin-ops-through-generator branch from b5d746f to 077d022 Compare August 10, 2026 10:06
@github-actions

github-actions Bot commented Aug 10, 2026

Copy link
Copy Markdown

Resolution Time Benchmark

---
config:
  themeVariables:
    xyChart:
      plotColorPalette: "#E63946, #3B82F6, #059669"
---
xychart
  title "Random Branching (🔴 PR | 🔵 main | 🟢 release)"
  x-axis "max depth" [1, 2, 3, 4, 5, 6, 7, 8]
  y-axis "time (ms)"
  line [0.80, 1.68, 3.66, 4.94, 5.93, 8.99, 18.18, 22.01]
  line [0.77, 1.54, 3.41, 4.75, 5.86, 9.70, 17.73, 21.12]
  line [0.81, 1.74, 3.46, 5.67, 5.88, 10.12, 18.79, 19.11]
Loading
---
config:
  themeVariables:
    xyChart:
      plotColorPalette: "#E63946, #3B82F6, #059669"
---
xychart
  title "Linear Recursion (🔴 PR | 🔵 main | 🟢 release)"
  x-axis "max depth" [1, 2, 3, 4, 5, 6, 7, 8]
  y-axis "time (ms)"
  line [0.24, 0.48, 0.59, 0.73, 0.95, 1.03, 1.21, 1.30]
  line [0.22, 0.40, 0.56, 0.66, 0.92, 0.97, 1.18, 1.24]
  line [0.29, 0.50, 0.63, 0.81, 1.04, 1.05, 1.21, 1.38]
Loading
---
config:
  themeVariables:
    xyChart:
      plotColorPalette: "#E63946, #3B82F6, #059669"
---
xychart
  title "Full Tree (🔴 PR | 🔵 main | 🟢 release)"
  x-axis "max depth" [1, 2, 3, 4, 5, 6, 7, 8]
  y-axis "time (ms)"
  line [0.77, 1.72, 3.91, 6.10, 10.19, 21.15, 45.54, 91.48]
  line [0.88, 1.71, 3.43, 5.31, 9.83, 20.73, 45.94, 90.59]
  line [0.67, 1.74, 4.11, 6.57, 10.84, 21.93, 47.39, 96.65]
Loading

@github-actions

github-actions Bot commented Aug 10, 2026

Copy link
Copy Markdown

Bundle size comparison (import * as ... in PR vs import * as ... in target):

🟢 Decreased (max -0.25%) ➖ Unchanged 🔴 Increased (max 2.79%) ❔ Unknown
6 129 187 1

import * as ... in PR vs import * as ... in target (did bundle size increase?):

Test tsdown
std_isBeingTranspiled.ts 15.71 kB ($${\color{red}+2.8\%}$$)
std_getTargetShaderLanguage.ts 15.77 kB ($${\color{red}+2.8\%}$$)
STATIC_std.ts 110.08 kB ($${\color{red}+0.5\%}$$)
std_getShaderStage.ts 15.76 kB

import { ... } in PR vs import * as ... in PR (is the library tree-Shakeable?):

Test tsdown
tgpu_init.ts 268.30 kB ($${\color{green}-3.3\%}$$)
tgpu_initFromDevice.ts 267.77 kB ($${\color{green}-3.5\%}$$)
tgpu_resolve.ts 169.05 kB ($${\color{green}-39.1\%}$$)
tgpu_resolveWithContext.ts 168.98 kB ($${\color{green}-39.1\%}$$)
tgpu_bindGroupLayout.ts 73.91 kB ($${\color{green}-73.4\%}$$)
tgpu_mutableAccessor.ts 68.64 kB ($${\color{green}-75.3\%}$$)
tgpu_accessor.ts 68.63 kB ($${\color{green}-75.3\%}$$)
tgpu_privateVar.ts 67.33 kB ($${\color{green}-75.7\%}$$)
tgpu_workgroupVar.ts 67.32 kB ($${\color{green}-75.7\%}$$)
tgpu_const.ts 66.74 kB ($${\color{green}-75.9\%}$$)
tgpu_lazy.ts 66.54 kB ($${\color{green}-76.0\%}$$)
tgpu_fragmentFn.ts 38.92 kB ($${\color{green}-86.0\%}$$)
tgpu_fn.ts 38.86 kB ($${\color{green}-86.0\%}$$)
tgpu_vertexFn.ts 38.73 kB ($${\color{green}-86.0\%}$$)
tgpu_computeFn.ts 38.44 kB ($${\color{green}-86.1\%}$$)
tgpu_vertexLayout.ts 27.57 kB ($${\color{green}-90.1\%}$$)
tgpu_comptime.ts 15.18 kB ($${\color{green}-94.5\%}$$)
tgpu_unroll.ts 1.75 kB ($${\color{green}-99.4\%}$$)
tgpu_slot.ts 1.70 kB ($${\color{green}-99.4\%}$$)

If you wish to run a comparison for other, slower bundlers, run the 'Tree-shake test' from the GitHub Actions menu.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ Minor suggestions only — one small typo in an error message.

Reviewed changes

  • emitBinaryOp route — Added emitBinaryOp plus the BinaryOperator union to the ShaderGenerator interface and WgslGenerator, and rewired + - * / % << >> in std/operators.ts and the binary-expression emitter through it (replacing inline stitch and the old parenthesization block).
  • Method renames — Renamed the unstable ShaderGenerator methods callemitCall and typeAnnotationemitTypeAnnotation across WgslGenerator, GlslGenerator, internal.ts, resolutionCtx.ts, and every std codegen call site. All callers were updated.
  • _callShellless extraction — Factored the shellless-call lowering out of _expression into a reusable protected WgslGenerator._callShellless, and reused it from GlslGenerator.emitBinaryOp to translate floating-point % into a remainder shell helper (GLSL ES has no float %), leaving non-float % on the super path.

The parenthesization and operator-mapping behavior is preserved for WGSL, so existing WGSL snapshots should be unaffected.

ℹ️ Nitpicks

  • packages/typegpu-gl/src/glslGenerator.ts:430 — error message has a doubled (type: prefix on the RHS operand.
  • The %remainder path in the new test only covers scalar f32; vec2f/3f/4f operands hit the same branch but aren't exercised.

Pullfrog  | Fix all ➔Fix 👍s ➔View workflow run | Using DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

Comment thread packages/typegpu-gl/src/glslGenerator.ts Outdated
Copilot AI lite review requested due to automatic review settings August 10, 2026 10:17
@iwoplaza
iwoplaza force-pushed the feat/route-bin-ops-through-generator branch from 077d022 to 728fda6 Compare August 10, 2026 10:17
@iwoplaza
iwoplaza force-pushed the feat/route-bin-ops-through-generator branch from 728fda6 to 74bc6ff Compare August 10, 2026 10:21

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR centralizes emission of binary operations (and related helpers like function calls/type annotations) behind the ShaderGenerator interface so language backends (notably GLSL) can customize operator lowering while reusing the same TGSL resolution pipeline.

Changes:

  • Add emitBinaryOp to ShaderGenerator and route TGSL binary/assignment/logical operator emission through it.
  • Rename typeAnnotation/call to emitTypeAnnotation/emitCall and update stdlib + resolution code to use the new API.
  • Extend the GLSL generator to lower floating-point % to a generated remainder helper, with a new regression test.

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
packages/typegpu/src/tgsl/wgslGenerator.ts Adds _callShellless, routes binary ops via emitBinaryOp, renames generator emission helpers.
packages/typegpu/src/tgsl/shellless.ts Makes ShelllessRepository.get accept readonly snippet arrays.
packages/typegpu/src/tgsl/shaderGenerator.ts Extends generator interface with emitBinaryOp and renames emission methods.
packages/typegpu/src/std/operators.ts Routes arithmetic/shift operator codegen via ctx.gen.emitBinaryOp (and generator-driven instantiation).
packages/typegpu/src/std/numeric.ts Updates built-in call emission to emitCall.
packages/typegpu/src/std/boolean.ts Updates built-in call emission to emitCall.
packages/typegpu/src/std/bitcast.ts Updates built-in call emission to emitCall.
packages/typegpu/src/std/array.ts Updates built-in call emission to emitCall.
packages/typegpu/src/resolutionCtx.ts Updates schema-to-type annotation emission to emitTypeAnnotation.
packages/typegpu/src/internal.ts Re-exports BinaryOperator from internal API surface.
packages/typegpu-gl/tests/glslGenerator.test.ts Adds test ensuring float % becomes a remainder() call in GLSL output.
packages/typegpu-gl/src/glslGenerator.ts Implements GLSL-specific % lowering via a shellless remainder helper and updates renamed methods.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +46 to +63
export type BinaryOperator =
| '='
| '^'
| '|'
| '&'
| '*'
| '/'
| '%'
| '+'
| '-'
| '<<'
| '>>'
| '<'
| '>'
| '<='
| '>='
| '=='
| '!=';
Comment thread packages/typegpu-gl/src/glslGenerator.ts Outdated

@cieplypolar cieplypolar left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

schema: d.BaseData | UnknownData,
): schema is d.F32 | d.Vec2f | d.Vec3f | d.Vec4f {
return (
typeof schema !== 'symbol' &&

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
typeof schema !== 'symbol' &&
schema !== UnknownData &&

It is obvious for us that this symbol is UnknownData, but I think an explicit check is more readable.

new (): T;
}

export type BinaryOperator =

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you create an issue to update BinaryOperator type and link it in the TODO comment?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants