Repository navigation
Create WebGL sprite data textures in rows of 256 - #2938
Merged
Merged
Conversation
On WebGL, a SpriteList stores its sprite data in textures, which the shader reads as rows of 256 sprites and the grow methods resize to (256, rows). SpriteListTextureData created them as one row as wide as the capacity, so with a capacity over 256, sprites past the first 256 were read from rows that didn't exist and weren't drawn: SpriteList(capacity= 1000), clear() on a list that had grown past 256, or a lazy list with more than 256 sprites before its first draw. Create them as (256, capacity // 256). Uploads are row-major whole texture writes, so they don't change. Fixes #2909 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #2909: on WebGL, a
SpriteListwhose storage was created with a capacity over 256 only drew its first 256 sprites.The problem
On WebGL, sprite data is stored in textures (
SpriteListTextureData) instead of buffers:lib/sprite.glsl:texelFetch(..., ivec2(index % 256, index / 256), 0)), and so does the GPU collision shader.grow_*methods resize them to(256, rows).__init__created them as one row as wide as the capacity. So with a capacity over 256, spritei >= 256was read from rowi // 256, which didn't exist.That happens with:
SpriteList(capacity=1000);clear()on a list that had grown past 256 sprites, since it keeps the capacity and creates the storage again;The fix
Create the five textures as
(256, capacity // 256), the layout the shader reads and the grow methods keep. The capacity is always a multiple of 256, and I align it anyway in case a subclass passes one that isn't. Uploads are whole-texture, row-major writes, so they don't change.Verified
I reproduced it on desktop OpenGL by making
SpriteListuse the WebGL storage, drawing 1-pixel sprites into a framebuffer, and counting lit pixels:clear(), then 400 moreTests
New
tests/unit/spritelist/test_spritelist_texture_storage.py. It runs on desktop GL in CI by monkeypatchingSpriteListto useSpriteListTextureData:test_created_with_capacitywith capacities 100, 256, 1000, 1024 and 3000, drawing 600 sprites;test_clear_after_growing;test_lazy_list_grown_before_first_draw;test_storage_is_rows_of_256.On
development, 6 of the 8 fail. Capacities 100 and 256 pass, since they round up to a single 256-wide row either way.Full suite: 1732 passed. Ruff is clean, and mypy has no new errors. I haven't run it in an actual browser build. A quick check there would be worth doing, but the shader and texture code are the same as the desktop path the tests use.
The changelog has an entry under Unreleased → Fixes.
🤖 Generated with Claude Code