Skip to content

OpenGL2/3: Backup and restore GL_UNPACK state in UpdateTexture() (#9473)#9473

Closed
Turtle-PB wants to merge 1 commit into
ocornut:masterfrom
Turtle-PB:fix/opengl-state-restore-update-texture
Closed

OpenGL2/3: Backup and restore GL_UNPACK state in UpdateTexture() (#9473)#9473
Turtle-PB wants to merge 1 commit into
ocornut:masterfrom
Turtle-PB:fix/opengl-state-restore-update-texture

Conversation

@Turtle-PB

Copy link
Copy Markdown
Contributor

Problem

The OpenGL2 and OpenGL3 backends set GL_UNPACK_ROW_LENGTH=0 and GL_UNPACK_ALIGNMENT=1 during texture creation and updates, but never restored the original values. This corrupted the calling application's pixel store state when ImGui created or updated textures mid-frame (e.g., dynamic font atlas updates).

This was flagged as a FIXME in ImGui_ImplOpenGL3_UpdateTexture():

// FIXME: Consider backing up and restoring

Fix

Backup GL_UNPACK_ROW_LENGTH and GL_UNPACK_ALIGNMENT at the start of UpdateTexture(), and restore them at the end. This matches the pattern already used for GL_TEXTURE_BINDING_2D in the same function.

OpenGL3 (imgui_impl_opengl3.cpp)

  • Backup GL_UNPACK_ROW_LENGTH and GL_UNPACK_ALIGNMENT via glGetIntegerv() at function entry
  • Restore them at function exit (after all WantCreate/WantUpdates/WantDestroy paths)
  • Removed the FIXME comment (resolved)
  • Added changelog entry

OpenGL2 (imgui_impl_opengl2.cpp)

  • Same backup/restore pattern
  • Added changelog entry

Testing

  • The fix is guarded by #ifdef GL_UNPACK_ROW_LENGTH / #ifdef GL_UNPACK_ALIGNMENT for WebGL/ES compatibility (where these constants may not exist)
  • The restore happens after all code paths (WantCreate, WantUpdates, WantDestroy), so state is always restored
  • The GL_CALL macro is used for all backup/restore calls, matching the existing code style

Related Issues

…rnut#9473)

The OpenGL2 and OpenGL3 backends set GL_UNPACK_ROW_LENGTH=0 and
GL_UNPACK_ALIGNMENT=1 during texture creation and updates, but
never restored the original values. This corrupted the calling
application's pixel store state when ImGui created or updated
textures mid-frame (e.g., dynamic font atlas updates).

Fix: Backup GL_UNPACK_ROW_LENGTH and GL_UNPACK_ALIGNMENT at the
start of UpdateTexture(), and restore them at the end.

This resolves the FIXME in ImGui_ImplOpenGL3_UpdateTexture():
'FIXME: Consider backing up and restoring'

Issues: ocornut#8802 (original UNPACK_ALIGNMENT change), ocornut#9473 (this fix)
ocornut added a commit that referenced this pull request Jul 15, 2026
…ure(). Amends. (#9473, #8802)

Fix setting GL_UNPACK_ROW_LENGTH twice.
@ocornut

ocornut commented Jul 15, 2026

Copy link
Copy Markdown
Owner

Merged as e3927dc + amends 0ff4a8a.
Original code was changing GL_UNPACK_ROW_LENGTH twice so I fixed that, and your code left the "restore to 0" so I fixed that too, removing too call. Made the code breath a little bit by bundling both #ifdef together since they were introduced in same GL version.

Thank you!

@ocornut ocornut closed this Jul 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants