Skip to content

fix: immediately upload sprites when they become visible again#3087

Draft
contariaa wants to merge 2 commits intoCaffeineMC:devfrom
contariaa:fix/reupload-sprites-instantly
Draft

fix: immediately upload sprites when they become visible again#3087
contariaa wants to merge 2 commits intoCaffeineMC:devfrom
contariaa:fix/reupload-sprites-instantly

Conversation

@contariaa
Copy link
Contributor

When a sprite becomes active again after skipping uploads, it won't immediately update to its correct state, instead it will only do so the next time the frame index changes.
I initially fixed this by just ensuring it will always upload the first tick after becoming active again, however that still leaves the time between being marked as active and being ticked where the texture would be wrong.
The fix for that is to ensure the upload right when the sprite is marked as active, which means we have to look up and bind the atlas texture everytime that happens. I'm unsure about the performance cost of that, if it's a problem I can revert the second commit, the first one alone is still significantly better than the current behaviour.

Closes #2514

this still has a small delay between the sprite being marked as active again and the sprite being ticked
to fix that the SpriteContents.Ticker could be stored in SpriteContents and ensureUpload() called when the sprite is marked as active (after being inactive)
return;
}

Minecraft.getInstance().getTextureManager().getTexture(atlas).bind();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revisiting this, its probably problematic to change the bound texture here since we can't know at what point in the code a sprite is being marked active and it might mess up render state.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This issue is not present with just the first commit, but as stated in the initial pr message the behaviour from just the first commit is not 100% correct since it leaves a delay between the texture becoming visible and when its reticked

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a safe approach to this? Does anyone know whether this is safe?

@douira
Copy link
Contributor

douira commented Feb 2, 2026

Do you have a safe and correct approach to this? it seems you expressed some doubt about the method last year. This PR has also accumulated merge conflicts. It should be profiled to see if there's a performance cost to the second change you made (and there were concerns for safety).

@douira douira added this to the Upcoming Minor milestone Feb 2, 2026
@contariaa
Copy link
Contributor Author

I don't know of any way to achieve instant sprite updates safely, the problem is that the hooks for marking a sprite as active may be called at any time before they are rendered, which makes the sprite upload fundamentally unsafe (it has to bind the sprite texture to upload, thus changing render state).
I do think porting and merging just the first commit is still a valueable improvement, the current code causes the image to only be re-uploaded when the frame changes, even if the current one isn't uploaded yet, example:

  • a sprite with 4 frames at 20 ticks (also called sub-frames in the code) each
  • frame 1 gets uploaded at 0 ticks
  • sprite becomes invisible at 10 ticks
  • frame 2 does not get uploaded at 20 ticks because the sprite is currently invisible
  • sprite becomes visible again at 30 ticks, still on frame 1
  • frame 3 gets uploaded at 40 ticks, after 10 ticks of the wrong frame being visible

If there is interest I can look into porting the first commit, while it still shows the old frame until the sprite is next ticked, it's still better than having the old frame visible until a new frame is uploaded in my opinion.

@douira
Copy link
Contributor

douira commented Feb 4, 2026

Yeah I think the first commit would still be an improvement as you said. Would be interesting see what it looks like in practice with the reproducer shown in the referenced issue.

@douira douira modified the milestones: Upcoming Minor, Sodium 0.8.5 Feb 8, 2026
@douira douira modified the milestones: Sodium 0.8.5, Upcoming Minor Feb 19, 2026
@douira douira marked this pull request as draft February 19, 2026 01:16
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.

Animated GUI textures with repeated frames can desync/freeze when Only Animate Visible Textures is enabled

2 participants