fix(@typegpu/gl): Better support for texture arrays in GLSL - #2910
fix(@typegpu/gl): Better support for texture arrays in GLSL#2910iwoplaza wants to merge 1 commit into
Conversation
|
pkg.pr.new packages benchmark commit |
Bundle size comparison (
|
| 🟢 Decreased | ➖ Unchanged | 🔴 Increased | ❔ Unknown |
|---|---|---|---|
| 0 | 325 | 0 | 0 |
import { ... } in PR vs import * as ... in PR (is the library tree-Shakeable?):
| Test | tsdown |
|---|---|
| tgpu_init.ts | 263.10 kB ( |
| tgpu_initFromDevice.ts | 262.56 kB ( |
| tgpu_resolve.ts | 161.75 kB ( |
| tgpu_resolveWithContext.ts | 161.68 kB ( |
| tgpu_bindGroupLayout.ts | 62.32 kB ( |
| tgpu_mutableAccessor.ts | 57.04 kB ( |
| tgpu_accessor.ts | 57.04 kB ( |
| tgpu_privateVar.ts | 55.73 kB ( |
| tgpu_workgroupVar.ts | 55.73 kB ( |
| tgpu_const.ts | 55.15 kB ( |
| tgpu_lazy.ts | 54.95 kB ( |
| tgpu_fragmentFn.ts | 39.68 kB ( |
| tgpu_fn.ts | 39.62 kB ( |
| tgpu_vertexFn.ts | 39.50 kB ( |
| tgpu_computeFn.ts | 39.20 kB ( |
| tgpu_vertexLayout.ts | 28.33 kB ( |
| tgpu_comptime.ts | 15.93 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.
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.93, 1.84, 4.35, 6.34, 8.07, 12.18, 23.53, 27.29]
line [0.99, 2.00, 4.01, 6.65, 7.73, 13.39, 23.51, 23.85]
line [0.98, 1.98, 4.46, 6.53, 7.59, 11.46, 24.51, 24.21]
---
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.48, 0.65, 0.76, 1.02, 1.15, 1.26, 1.48]
line [0.29, 0.50, 0.66, 0.79, 1.11, 1.19, 1.40, 1.54]
line [0.30, 0.51, 0.69, 0.80, 1.07, 1.18, 1.38, 1.53]
---
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.88, 2.21, 3.49, 7.21, 12.28, 26.65, 55.29, 112.98]
line [0.86, 2.21, 3.99, 7.32, 12.24, 26.51, 54.34, 111.84]
line [0.92, 2.01, 3.34, 6.99, 12.75, 27.18, 54.95, 115.17]
|
There was a problem hiding this comment.
Pull request overview
Improves the GLSL generator’s handling of texture_2d_array sampling by normalizing arguments so the array index is combined with UV coordinates into a single vec3(...), matching GLSL’s sampler2DArray sampling signatures.
Changes:
- Add GLSL argument normalization to combine
(vec2 coords, array_index)intovec3(coords, array_index)fortextureSample*calls on 2D-array textures. - Update
textureSample,textureSampleBias, andtextureSampleLevelemission to use the normalized argument list consistently. - Add test coverage asserting correct GLSL output for 2D-array sampling with offsets, bias, and explicit LOD.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| packages/typegpu-gl/src/glslGenerator.ts | Normalizes 2D-array texture sampling arguments and updates GLSL emission for textureSample*. |
| packages/typegpu-gl/tests/glslGenerator.test.ts | Adds regression tests for 2D-array sampling codegen (coords+layer merging; bias/level with offsets). |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
#normalizeTextureArrayArgumentshelper — For 2d-array textures, finds the uv vector arg and merges it with the trailingarray_indexinto a singlevec3/uvec3/ivec3constructor (dispatch on the coord primitive type), applying the existing flip-Y conditional to the coords first. For plain textures it splices the oriented coords back in place.emitCallrestructure —textureSample/textureSampleBias/textureSampleLevelnow normalize args up front, destructure[texture, sampler, coords, ...rest], and re-derivelevel/bias/offsetfromrest.- Three new snapshot tests for 2d-array sampling (plain/offset, bias+offset, level+offset).
I traced the new argument-shuffling against the WGSL std signatures (e.g. textureSample(texture2dArray, coords: v2f, arrayIndex, offset?)) and confirmed the merged-vec3 forms are correct, and the refactor preserves non-array output. The new tests are exact snapshots (not loose assertions) and all 28 tests pass.
ℹ️ Array-texture support is scoped to the sample trio and the 2d-array dimension
Since this PR positions itself as "better support for texture arrays," one follow-up to keep on the radar (not blocking): textureLoad → texelFetch on a 2d-array texture also takes coords, arrayIndex, level, but that path destructures const [texture, coords, level] = args and is not normalized here, so it would read arrayIndex as level. The same is true of cube-array textures, which fall through the isTextureArray 2d-array check entirely.
Also a low-priority robustness nit: the coord-primitive dispatch in #normalizeTextureArrayArguments (f32/u32/i32) silently falls back to leaving the uv and array_index as separate args for any unhandled coord vector type, which would emit an incorrect sampling call rather than throwing. It is unreachable through the typed std API (2d-array coords are pinned to v2f), so a defensive guard is optional.
Technical details
# Array-texture emission scope
## Affected sites
- packages/typegpu-gl/src/glslGenerator.ts:647 (`textureLoad` → `texelFetch`, args destructured as `[texture, coords, level]`, no array-index normalization).
- packages/typegpu-gl/src/glslGenerator.ts:555 — `isTextureArray` only matches `dimension === '2d-array'`.
## Required outcome
- (Optional, author discretion) Normalize 2d-array `textureLoad` coords+index and/or fold `cube-array` into the same merge, or explicitly document them as out of scope.
- Add a defensive guard so an unhandled coordinate vector type throws rather than emitting malformed GLSL.DeepSeek Flash (free via Pullfrog for OSS) | 𝕏
Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
eee695e to
1f586de
Compare

No description provided.