Conversation
create_text_sprite makes an empty texture and draws the text into the atlas. When the atlas fills up after some textures were freed, it rebuilds itself by clearing and redrawing every texture from its image, and a text sprite's image was blank, so the sprite went blank. Copy the drawn pixels back into the texture's image with update_texture_image_from_atlas() after drawing. This also makes sprite.texture.image contain the text. It adds about 0.2-0.6 ms per create_text_sprite call for the GPU read. This caused test_same_text_different_style to fail in CI whenever an earlier test left the shared atlas full enough to rebuild. Add a test that rebuilds the atlas directly. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
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.
Problem
create_text_spritesprites go blank when the texture atlas rebuilds itself.create_text_spritemakes an empty texture (Texture.create_empty) and draws the text straight into the atlas withrender_into. The texture's ownimagestays blank. When the atlas runs out of room and some images have been freed,DefaultTextureAtlas._addcallsrebuild()instead of growing.rebuild()clears the atlas and redraws every texture from its image, so every text sprite becomes a blank image. Growing the atlas withresize()copies pixels on the GPU, so it isn't affected, which is why this only shows up sometimes.Reproduced directly: a 20pt red "Hello World" sprite has 155,453 total alpha in the atlas before
atlas.rebuild(), and 0 after.This is why #2917's CI fails.
test_same_text_different_style(added in #2915) fails on Python 3.10, 3.12 and 3.13 there: the red sprite reads back blank after the blue one is created. In that run, an earlier test left the shared atlas full enough that adding the blue sprite triggered a rebuild. #2917 changes which examples run and how often, which changes how full the atlas is by then. I reproduced exactly that sequence with a small atlas: a full atlas with some images freed, then the red sprite, then the blue one, which rebuilds the atlas. The red sprite is blank without this fix and unchanged with it.Fix
After drawing,
create_text_spritecallstexture_atlas.update_texture_image_from_atlas(texture), the atlas method meant for "render into the atlas and need to update the texture with the new pixel data". The texture's image now holds the text, so a rebuild redraws it correctly. As a bonus,sprite.texture.imagenow contains the text, for code that reads it.Cost: the GPU read adds about 0.2–0.6 ms per
create_text_spritecall, measured 2 × alternating:"Score: 100", 16ptcreate_text_spriteis called once per sprite, not every frame, so this seemed worth it for sprites that don't randomly vanish.Tests
test_survives_atlas_rebuild: callsatlas.rebuild()directly and checks the sprite's pixels are unchanged, and thattexture.imagematches the atlas. It fails without the fix, whatever state the shared atlas is in.test_same_text_different_stylenow passes even when a rebuild happens during it.create_text_spritefixes entry.Once this is merged, #2917 can be updated from
developmentand its CI should pass.🤖 Generated with Claude Code