Skip to content

fix(@typegpu/gl): Better support for texture arrays in GLSL - #2910

Open
iwoplaza wants to merge 1 commit into
fix/glsl-reserved-keywordsfrom
fix/glsl-texture-array
Open

fix(@typegpu/gl): Better support for texture arrays in GLSL#2910
iwoplaza wants to merge 1 commit into
fix/glsl-reserved-keywordsfrom
fix/glsl-texture-array

Conversation

@iwoplaza

Copy link
Copy Markdown
Collaborator

No description provided.

Copilot AI lite review requested due to automatic review settings August 24, 2026 12:07
@github-actions

github-actions Bot commented Aug 24, 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/@typegpu/gl@1f586de29303f182af3f7c1fe64a8cacd3ff7fd8

benchmark
view benchmark

commit
view commit

@github-actions

Copy link
Copy Markdown

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

🟢 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 ($${\color{green}-3.5\%}$$)
tgpu_initFromDevice.ts 262.56 kB ($${\color{green}-3.7\%}$$)
tgpu_resolve.ts 161.75 kB ($${\color{green}-40.7\%}$$)
tgpu_resolveWithContext.ts 161.68 kB ($${\color{green}-40.7\%}$$)
tgpu_bindGroupLayout.ts 62.32 kB ($${\color{green}-77.1\%}$$)
tgpu_mutableAccessor.ts 57.04 kB ($${\color{green}-79.1\%}$$)
tgpu_accessor.ts 57.04 kB ($${\color{green}-79.1\%}$$)
tgpu_privateVar.ts 55.73 kB ($${\color{green}-79.6\%}$$)
tgpu_workgroupVar.ts 55.73 kB ($${\color{green}-79.6\%}$$)
tgpu_const.ts 55.15 kB ($${\color{green}-79.8\%}$$)
tgpu_lazy.ts 54.95 kB ($${\color{green}-79.8\%}$$)
tgpu_fragmentFn.ts 39.68 kB ($${\color{green}-85.4\%}$$)
tgpu_fn.ts 39.62 kB ($${\color{green}-85.5\%}$$)
tgpu_vertexFn.ts 39.50 kB ($${\color{green}-85.5\%}$$)
tgpu_computeFn.ts 39.20 kB ($${\color{green}-85.6\%}$$)
tgpu_vertexLayout.ts 28.33 kB ($${\color{green}-89.6\%}$$)
tgpu_comptime.ts 15.93 kB ($${\color{green}-94.2\%}$$)
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.

@github-actions

github-actions Bot commented Aug 24, 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.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]
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.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]
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.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]
Loading

Copilot AI 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.

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) into vec3(coords, array_index) for textureSample* calls on 2D-array textures.
  • Update textureSample, textureSampleBias, and textureSampleLevel emission 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.

Comment thread packages/typegpu-gl/src/glslGenerator.ts Outdated

@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.

✅ No new issues found.

Reviewed changes

  • #normalizeTextureArrayArguments helper — For 2d-array textures, finds the uv vector arg and merges it with the trailing array_index into a single vec3/uvec3/ivec3 constructor (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.
  • emitCall restructuretextureSample/textureSampleBias/textureSampleLevel now normalize args up front, destructure [texture, sampler, coords, ...rest], and re-derive level/bias/offset from rest.
  • 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): textureLoadtexelFetch 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.

Pullfrog  | View workflow run | Using 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>
@iwoplaza
iwoplaza force-pushed the fix/glsl-texture-array branch from eee695e to 1f586de Compare August 24, 2026 21:25
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.

2 participants