feat: std.getShaderStage - #2817
Conversation
|
pkg.pr.new packages benchmark commit |
7c27874 to
80db3bf
Compare
There was a problem hiding this comment.
Pull request overview
This PR adds a new standard-library probe (std.getShaderStage) to allow shader code to branch on the entry-point stage during resolution, while also consolidating the internal stage type (TgpuShaderStage) into a single exported ShaderStage type.
Changes:
- Added
std.getShaderStage()(comptime probe) backed by an internalshaderStageSlot. - Unified
TgpuShaderStageandShaderStageinto a single exportedShaderStagetype and updated internal usages. - Added test coverage and updated docs to describe the new probe.
Reviewed changes
Copilot reviewed 13 out of 13 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| packages/typegpu/tests/std/environment.test.ts | Adds coverage for std.getShaderStage() across top-level, resolution, JS execution, and simulate contexts. |
| packages/typegpu/src/types.ts | Replaces TgpuShaderStage with exported ShaderStage and updates related type unions. |
| packages/typegpu/src/tgsl/shaderGenerator.ts | Updates function definition typing to use ShaderStage. |
| packages/typegpu/src/tgpuBindGroupLayout.ts | Updates bind group visibility typing and defaults to use ShaderStage[]. |
| packages/typegpu/src/std/index.ts | Re-exports getShaderStage from std. |
| packages/typegpu/src/std/environment.ts | Implements getShaderStage() as a comptime probe reading from shaderStageSlot. |
| packages/typegpu/src/resolutionCtx.ts | Updates function-scope typing to use ShaderStage. |
| packages/typegpu/src/internal.ts | Updates internal type exports to use ShaderStage. |
| packages/typegpu/src/indexNamedExports.ts | Exports ShaderStage from the public named exports. |
| packages/typegpu/src/core/slot/internalSlots.ts | Changes shaderStageSlot default to null and types it as `ShaderStage |
| packages/typegpu/src/core/function/fnCore.ts | Updates function core typing to use ShaderStage. |
| packages/typegpu-gl/src/glslGenerator.ts | Updates GLSL generator’s function type tracking to use ShaderStage. |
| apps/typegpu-docs/src/content/docs/apis/utils.mdx | Documents std.getShaderStage() and adjusts the “resolution environment” section text. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
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.95, 1.90, 4.04, 6.46, 7.77, 12.07, 23.21, 25.06]
line [0.96, 1.96, 4.08, 6.21, 7.81, 10.89, 23.08, 24.78]
line [0.97, 1.92, 4.59, 7.16, 8.24, 11.90, 22.65, 27.99]
---
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.31, 0.53, 0.70, 0.82, 1.07, 1.24, 1.43, 1.60]
line [0.28, 0.46, 0.71, 0.87, 1.17, 1.26, 1.49, 1.66]
line [0.31, 0.55, 0.71, 0.87, 1.25, 1.32, 1.51, 1.69]
---
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.84, 2.32, 4.06, 7.25, 12.54, 26.07, 55.53, 116.14]
line [0.84, 2.08, 4.42, 6.76, 12.92, 26.26, 56.43, 113.78]
line [1.09, 2.13, 4.33, 6.81, 13.39, 27.52, 57.56, 119.91]
|
Bundle size comparison (
|
| 🟢 Decreased (max -0.28%) | ➖ Unchanged | 🔴 Increased (max 2.79%) | ❔ Unknown |
|---|---|---|---|
| 6 | 130 | 187 | 1 |
import * as ... in PR vs import * as ... in target (did bundle size increase?):
| Test | tsdown |
|---|---|
| std_isBeingTranspiled.ts | 15.71 kB ( |
| std_getTargetShaderLanguage.ts | 15.77 kB ( |
| std_getShaderStage.ts | 15.76 kB |
import { ... } in PR vs import * as ... in PR (is the library tree-Shakeable?):
| Test | tsdown |
|---|---|
| tgpu_init.ts | 270.29 kB ( |
| tgpu_initFromDevice.ts | 269.74 kB ( |
| tgpu_resolve.ts | 170.54 kB ( |
| tgpu_resolveWithContext.ts | 170.48 kB ( |
| tgpu_bindGroupLayout.ts | 73.82 kB ( |
| tgpu_mutableAccessor.ts | 68.55 kB ( |
| tgpu_accessor.ts | 68.54 kB ( |
| tgpu_privateVar.ts | 67.23 kB ( |
| tgpu_workgroupVar.ts | 67.23 kB ( |
| tgpu_const.ts | 66.65 kB ( |
| tgpu_lazy.ts | 66.45 kB ( |
| tgpu_fragmentFn.ts | 38.92 kB ( |
| tgpu_fn.ts | 38.87 kB ( |
| tgpu_vertexFn.ts | 38.74 kB ( |
| tgpu_computeFn.ts | 38.44 kB ( |
| tgpu_vertexLayout.ts | 27.57 kB ( |
| tgpu_comptime.ts | 15.18 kB ( |
| tgpu_unroll.ts | 1.75 kB ( |
| tgpu_slot.ts | 1.70 kB ( |
If you wish to run a comparison for other, slower bundlers, run the 'Tree-shake test' from the GitHub Actions menu.
80db3bf to
49361fb
Compare
There was a problem hiding this comment.
ℹ️ No critical issues — minor suggestions inline.
Reviewed changes
std.getShaderStage— newcomptimeprobe returning'vertex'/'fragment'/'compute'when the calling'use gpu'function resolves (transitively) inside a matching entry fn,undefinedotherwise. Built on theshaderStageSlotthe entry-fn and auto-IO resolvers already bind.- Type merge —
TgpuShaderStagemerged with the internalShaderStage(previously... | undefined) into a single publicShaderStage = 'compute' | 'vertex' | 'fragment', exported fromindexNamedExports; the slot's unbound sentinel moved tonull.git grepconfirms noTgpuShaderStagereferences remain, andTgpuShaderStagewas never publicly exported, so there's no downstream break. - Tests — five new
getShaderStagecases, including a committed snapshot that pins per-stage duplication of a shared function used across vertex/fragment/compute resolution. - Docs —
getShaderStageadded to the resolution-environment section ofutils.mdx.
Tests (environment.test.ts, bindGroupLayout.test.ts, glsl suite) and both package typechecks pass. The rename and sentinel change are behavior-preserving: logGenerator.ts's shaderStageSlot.$ === 'vertex' check treats null exactly as it did undefined.
DeepSeek Flash (free via Pullfrog for OSS) | 𝕏
There was a problem hiding this comment.
ℹ️ The delta since the prior review is clean — one docs nit from the earlier review remains open, nothing new introduced.
Reviewed changes (delta between prior review 7c27874c and current head 49361fbb)
ShaderStageexport home moved — the type is no longer re-exported fromtypegpu/~internal(internal.ts); it now ships only through the publictypegpuentry (indexNamedExports.ts).GlslGeneratorwas updated to import it from'typegpu'instead, andgit grepconfirms no~internalconsumer ofShaderStageremains.- Docs prose — the comma splice in the resolution-environment section was fixed by splitting the sentence.
Verified on 49361fbb: @typegpu/gl and core package typechecks pass, and environment.test.ts (17 tests) passes.
DeepSeek Flash (free via Pullfrog for OSS) | 𝕏
49361fb to
945885c
Compare
945885c to
e6e99e6
Compare
e6e99e6 to
d94f1db
Compare
d94f1db to
17a430d
Compare
17a430d to
58fbfd9
Compare
58fbfd9 to
0bea5de
Compare
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
- Docs table fix — the env-table header row in the resolution-environment section now keeps its trailing
|, consistent with every other row, resolving the previously open nit atutils.mdx:133. The comma-splice thread was already resolved in an earlier push.
The delta of the PR's own files since the prior review is limited to this docs correction — the core std.getShaderStage() implementation, the TgpuShaderStage→ShaderStage type merge, and the test coverage are unchanged from the previously reviewed, clean pass. The remaining noise in the incremental diff is base-branch (feat/fn-call-through-gen) shader-generator refactor work, not part of this PR. Both prior Pullfrog threads are now resolved, and nothing new was introduced.
DeepSeek Flash (free via Pullfrog for OSS) | 𝕏
0bea5de to
7f7f81e
Compare
7f7f81e to
b48fecd
Compare

I additionally merged our
ShaderStageandTgpuShaderStageinto one type, then exported it fromtypegpuinstead oftypegpu/~internal.