Skip to content

fix: Ensure 4-byte alignment on texture buffers - #289

Merged
kylebarron merged 1 commit into
mainfrom
kyle/webgl-align-to-4-bytes
Mar 2, 2026
Merged

kylebarron merged 1 commit into
mainfrom
kyle/webgl-align-to-4-bytes

Conversation

@kylebarron

@kylebarron kylebarron commented Mar 2, 2026 •

Copy link
Copy Markdown
Member

Closes #237

With help from Claude:


Root cause: WebGL UNPACK_ALIGNMENT=4

WebGL's default pixel unpack alignment is 4 bytes. When uploading a texture, it expects each row of pixel data to start on a 4-byte boundary. For r8unorm (1 byte/pixel) — which is what palette images use — any tile whose width is not divisible by 4 will trigger the error.

Your image has tiles like:

  • 459px wide (459 % 4 = 3) → WebGL expects rows padded to 460 bytes → needs 460×239 = 109,940 bytes, but we only provide 459×239 = 109,701 → "not big enough"
  • 406px wide (406 % 4 = 2) → same problem
  • 512px wide (512 % 4 = 0) → works fine, no error

Fix: padToAlignment in render-pipeline.ts

Before creating the texture, the pixel data is now padded so each row is rounded up to the next multiple of 4 bytes. For rows already on a 4-byte boundary (e.g. width=512 with 1bpp, or width=256 with 4bpp), it's a no-op — the original byte view is returned unchanged.


Updated rendering:

image

@github-actions github-actions Bot added the fix label Mar 2, 2026
@kylebarron
kylebarron merged commit 7e31f31 into main Mar 2, 2026
4 checks passed
@kylebarron
kylebarron deleted the kyle/webgl-align-to-4-bytes branch March 2, 2026 17:26
@ds-release-bot ds-release-bot Bot mentioned this pull request Mar 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WebGL errors when texture arrays not aligned to 4 bytes

1 participant