Skip to content

Fix WebGL texture.read() for R8/RG8/RGB8 formats and add UNPACK_ALIGNMENT state caching #8406

Merged
mvaligursky merged 2 commits into
mainfrom
mv-webgltexture-fixes
Jan 26, 2026
Merged

Fix WebGL texture.read() for R8/RG8/RGB8 formats and add UNPACK_ALIGNMENT state caching #8406
mvaligursky merged 2 commits into
mainfrom
mv-webgltexture-fixes

Conversation

@mvaligursky

@mvaligursky mvaligursky commented Jan 23, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Fixed texture.read() for non-RGBA 8-bit formats on WebGL: WebGL2's readPixels only guarantees support for RGBA/UNSIGNED_BYTE from renderable framebuffers. Formats like R8, RG8, and RGB8 now correctly read back by using RGBA readback internally and extracting the appropriate channels.
  • Fixed getPixelFormatArrayType for PIXELFORMAT_RG8: Corrected the return type from Uint16Array to Uint8Array.
  • Added setUnpackAlignment() with state caching: Added a cached setter for UNPACK_ALIGNMENT to avoid redundant GL calls and prevent state pollution between texture uploads (e.g., when streaming uploads changed alignment).
  • Added test example: New hidden texture-read.example.mjs that verifies texture read/write roundtrip for 8-bit formats.

Technical Details

WebGL texture.read() Fix

  • Added getPixelFormatChannelsForRgbaReadback() helper in webgl-graphics-device.js
  • Extended readPixelsAsync() with optional forceRgba parameter
  • readTextureAsync() detects formats requiring RGBA readback, reads into an RGBA buffer, then extracts the correct channels into the user's expected format

UNPACK_ALIGNMENT Caching

  • Added this.unpackAlignment state variable initialized to 1
  • Added setUnpackAlignment(alignment) method that only calls gl.pixelStorei when the value changes
  • Updated all texture upload paths (webgl-texture.js, webgl-upload-stream.js) to use the cached setter

Test Example

  • New examples/src/examples/test/texture-read.example.mjs (hidden)
  • Tests write/read roundtrip for R8, RG8, RGB8, and RGBA8 formats
  • Displays pass/fail status on screen and logs details to console
  • Works on both WebGL and WebGPU backends

Test Plan

  • Run texture-read test example on WebGL - all formats pass
  • Run texture-read test example on WebGPU - all formats pass
  • Verify existing texture functionality is unaffected

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

This PR fixes WebGL texture readback for non-RGBA 8-bit formats and reduces GL state churn by caching UNPACK_ALIGNMENT.

Changes:

  • Add RGBA/UNSIGNED_BYTE forced readback path for R8/RG8/RGB8 on WebGL and channel extraction into the expected output format.
  • Fix getPixelFormatArrayType(PIXELFORMAT_RG8) to return Uint8Array.
  • Add cached setUnpackAlignment() and update WebGL texture upload paths to consistently set alignment for typed-array uploads; add a hidden example to validate read/write roundtrips.

Reviewed changes

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

Show a summary per file
File Description
src/platform/graphics/webgl/webgl-upload-stream.js Uses device-cached UNPACK_ALIGNMENT setter during PBO uploads to avoid redundant state calls.
src/platform/graphics/webgl/webgl-texture.js Ensures alignment is set to 1 for typed-array upload paths to prevent row-alignment-related upload errors.
src/platform/graphics/webgl/webgl-graphics-device.js Adds setUnpackAlignment() and implements RGBA-forced readback + channel extraction for R8/RG8/RGB8.
src/platform/graphics/constants.js Corrects getPixelFormatArrayType behavior for PIXELFORMAT_RG8.
examples/thumbnails/test_texture-read_small.webp Adds thumbnail for the new example.
examples/thumbnails/test_texture-read_large.webp Adds thumbnail for the new example.
examples/src/examples/test/texture-read.example.mjs Adds hidden example to validate texture.read() roundtrip for 8-bit formats across WebGL/WebGPU.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/platform/graphics/webgl/webgl-graphics-device.js Outdated
Comment thread examples/src/examples/test/texture-read.example.mjs

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

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


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/platform/graphics/webgl/webgl-graphics-device.js
Comment thread examples/src/examples/test/texture-read.example.mjs
@mvaligursky mvaligursky merged commit e20f13c into main Jan 26, 2026
13 checks passed
@mvaligursky mvaligursky deleted the mv-webgltexture-fixes branch January 26, 2026 08:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: graphics Graphics related issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants