Skip to content

simple_renderer: Fix TextureGLES filters, mip uploads, unpack alignment - #132

Merged
natetrost merged 1 commit into
android:mainfrom
natetrost:main
Nov 17, 2026
Merged

natetrost merged 1 commit into
android:mainfrom
natetrost:main

Conversation

@natetrost

Copy link
Copy Markdown
Contributor

Fix texture creation in TextureGLES:

  • The min and mag filters were swapped: GL_TEXTURE_MAG_FILTER was set from the min filter table and GL_TEXTURE_MIN_FILTER from the mag filter table. A mipmapped min filter was rejected as a mag filter with GL_INVALID_ENUM (asserting in debug builds), and the texture was left with the wrong min filter.
  • Every mip level was uploaded to level 0, so each mip overwrote the base image and levels 1+ never existed. Each level is now uploaded to its own level.
  • Mip dimensions were halved without clamping, so a non-square texture reached a dimension of 0 (e.g. 8x4 -> ... -> 1x0). They are now clamped to 1.
  • Uncompressed uploads set GL_PACK_ALIGNMENT (readback) instead of GL_UNPACK_ALIGNMENT (upload). Tightly packed rows that are not a multiple of 4 bytes (e.g. 3 pixel wide RGB) were read with a 4 byte stride, corrupting every row after the first.
  • GL_TEXTURE_MAX_LEVEL is set to the number of uploaded levels minus one. Otherwise a mipmapped min filter on a texture with fewer levels than a full chain leaves the texture incomplete, so it samples as black.
  • Texture parameters are set once after the upload loop instead of on every level.
  • Uploads no longer depend on, or disturb, the caller's GL state. The texture binding, GL_PIXEL_UNPACK_BUFFER binding, and unpack alignment/row length/skip settings are saved, set for a tightly packed client memory upload, and restored afterwards. The destructor no longer unbinds whatever texture is bound before deleting its own (glDeleteTextures already unbinds the deleted texture).
  • Uncompressed textures use the sized GL_RGBA8 / GL_RGB8 internal formats, which guarantee 8 bit channels (GL_RGB8 is also color-renderable).
  • Creation parameters are validated before any GL calls: enum ranges, ASTC/ETC2 compression types matching the format, non-null data and sizes, nonzero dimensions, and enough data for each uncompressed level, since glTexImage2D reads width * height * bpp bytes whatever size is passed. Invalid parameters assert in debug builds. In release they are logged and leave the texture empty (object 0) instead of reading out of bounds. The upload uses the base class mip count, which is clamped to the supported maximum and is now 0 if texture_sizes is null.

Fix texture creation in TextureGLES:

- The min and mag filters were swapped: GL_TEXTURE_MAG_FILTER was set
  from the min filter table and GL_TEXTURE_MIN_FILTER from the mag filter
  table. A mipmapped min filter was rejected as a mag filter with
  GL_INVALID_ENUM (asserting in debug builds), and the texture was left
  with the wrong min filter.
- Every mip level was uploaded to level 0, so each mip overwrote the base
  image and levels 1+ never existed. Each level is now uploaded to its
  own level.
- Mip dimensions were halved without clamping, so a non-square texture
  reached a dimension of 0 (e.g. 8x4 -> ... -> 1x0). They are now clamped
  to 1.
- Uncompressed uploads set GL_PACK_ALIGNMENT (readback) instead of
  GL_UNPACK_ALIGNMENT (upload). Tightly packed rows that are not a
  multiple of 4 bytes (e.g. 3 pixel wide RGB) were read with a 4 byte
  stride, corrupting every row after the first.
- GL_TEXTURE_MAX_LEVEL is set to the number of uploaded levels minus one.
  Otherwise a mipmapped min filter on a texture with fewer levels than a
  full chain leaves the texture incomplete, so it samples as black.
- Texture parameters are set once after the upload loop instead of on
  every level.
- Uploads no longer depend on, or disturb, the caller's GL state. The
  texture binding, GL_PIXEL_UNPACK_BUFFER binding, and unpack
  alignment/row length/skip settings are saved, set for a tightly packed
  client memory upload, and restored afterwards. The destructor no longer
  unbinds whatever texture is bound before deleting its own
  (glDeleteTextures already unbinds the deleted texture).
- Uncompressed textures use the sized GL_RGBA8 / GL_RGB8 internal formats,
  which guarantee 8 bit channels (GL_RGB8 is also color-renderable).
- Creation parameters are validated before any GL calls: enum ranges,
  ASTC/ETC2 compression types matching the format, non-null data and
  sizes, nonzero dimensions, and enough data for each uncompressed level,
  since glTexImage2D reads width * height * bpp bytes whatever size is
  passed. Invalid parameters assert in debug builds. In release they are
  logged and leave the texture empty (object 0) instead of reading out of
  bounds. The upload uses the base class mip count, which is clamped to
  the supported maximum and is now 0 if texture_sizes is null.

agdktunnel only creates single-level textures with linear filters, so
none of this was visible in the sample.

Tested on a Pixel 10 Pro in forced GLES mode, using a temporary hook that
creates textures and reads every level back through a framebuffer:
- An 8x4 RGBA texture with 4 mip levels (one color each), trilinear min
  filter and nearest mag filter. Before (release build, GL checks off):
  wrong filters, GL_INVALID_ENUM, all 4 levels incomplete. After: correct
  filters, GL_TEXTURE_MAX_LEVEL 3, all 4 levels hold their own color.
- A 3x2 RGB texture (9 byte rows). Before: row 1 read back black. After:
  row 1 is correct, and GL_UNPACK_ALIGNMENT is restored to 4.
- Creating and destroying a texture while another texture is bound
  leaves that binding unchanged.
- With a pixel unpack buffer bound, alignment 8, row length 5 and skips
  of 1, the RGB texture still uploads correctly, and all of that state is
  restored afterwards. The internal format is GL_RGB8.
- Release build: a mismatched ASTC compression type, too little data,
  null data and an out-of-range filter are each rejected with a logged
  reason and no GL errors.
- No GL errors and no failed GL checks in the debug build. GLES and
  Vulkan gameplay render as before. Debug and release both build.
@natetrost
natetrost merged commit d7aab5f into android:main Nov 17, 2026
7 checks passed
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.

1 participant