Skip to content

simple_renderer: Fix TextureVk formats, mip uploads, filters, null bind - #133

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

natetrost merged 1 commit into
android:mainfrom
natetrost:main

Conversation

@natetrost

Copy link
Copy Markdown
Contributor
  • Every ASTC block size was mapped to VK_FORMAT_ASTC_4x4_UNORM_BLOCK, so any non-4x4 ASTC texture was created with the wrong format and its copy read past the end of the staging buffer (VUID-vkCmdCopyBufferToImage-pRegions-00171). Each block size now maps to its own format.
  • TextureVk only created and uploaded mip level 0 (mipLevels = 1, one copy of texture_sizes[0]), ignoring mip_count. The image, the copy regions, the layout transitions and the image view now cover every mip level. Each level is placed in the staging buffer at an offset that is a multiple of its texel block size.
  • The sampler mipmap mode was swapped for kMinFilter_Linear_Mipmap_Nearest (LINEAR instead of NEAREST) and kMinFilter_Nearest_Mipmap_Linear (NEAREST instead of LINEAR); the min filter table itself was correct. The non-mipmapped min filters now clamp sampling to level 0 (maxLod = 0.25), matching GLES.
  • VK_FORMAT_R8G8B8_UNORM is not sampleable on many GPUs (no optimal tiling features at all on the Pixel 10 Pro's PowerVR). RGB_888 textures, including agdktunnel's fallback wall texture, failed with VUID-VkImageCreateInfo-imageCreateMaxMipLevels-02251, VUID-VkImageViewCreateInfo-None-02273 and VUID-vkCmdCopyBufferToImage-dstImage-01997. They are now expanded to RGBA8 on upload when R8G8B8 can't be sampled.
  • A format the device can't sample, or a size or mip count over the device's limits for the format (vkGetPhysicalDeviceImageFormatProperties), is reported and leaves the texture empty, before anything is staged. GetFeatureAvailable(kFeature_ASTC) asked nothing and always returned true; it now reports the device's textureCompressionASTC_LDR feature.
  • Creation parameters are validated once, in the Texture base class (the new renderer_texture.cpp, shared by TextureGLES and TextureVk): enum ranges, a compression type matching the format (none for RGBA/RGB), non-null data, nonzero dimensions, 1 to 16 mip levels and no more than a full mip chain, and enough bytes for each level, including compressed ASTC and ETC2 block data. Mip counts over 16 were silently truncated. Byte counts are computed in 64 bits, so huge dimensions can't wrap the required size to a small number (size_t is 32 bits on 32-bit ABIs). Invalid parameters assert in debug builds and leave the texture empty in release, with no mip levels. Allocation and object creation failures no longer continue with null handles, and release what was created, so a texture has either both an image view and a sampler or neither.
  • RendererVk::BindTexture(nullptr) fell through and dereferenced the null texture (crash). It now drops the bind, since a null descriptor set can't be bound. The same applies to a texture whose creation failed. BindTexture asserts instead of crashing when called with a texture but no render state, and does nothing for render states whose vertex format has no texture coordinates: their descriptor set layout has no sampler binding, and writing one failed with VUID-VkWriteDescriptorSet-dstBinding-10009 (and crashed the validation layer).
  • A texture bound but not yet drawn with left the descriptor set marked dirty when SetRenderState, SetRenderPass or EndFrame forgot the bound set, so the next draw bound VK_NULL_HANDLE (VUID-vkCmdBindDescriptorSets-pDescriptorSets-06563). Forgetting the bound set now also drops the pending bind, and draws never bind a null set.
  • Per-frame texture descriptor sets were cached by image view alone, but render states can use different descriptor set layouts. The cache is now keyed by image view and layout. It was also constructed with 128 empty entries that every lookup scanned; the space is now reserved instead.

Fix Vulkan texture creation and binding:

- Every ASTC block size was mapped to VK_FORMAT_ASTC_4x4_UNORM_BLOCK, so
  any non-4x4 ASTC texture was created with the wrong format and its copy
  read past the end of the staging buffer
  (VUID-vkCmdCopyBufferToImage-pRegions-00171). Each block size now maps
  to its own format.
- TextureVk only created and uploaded mip level 0 (mipLevels = 1, one
  copy of texture_sizes[0]), ignoring mip_count. The image, the copy
  regions, the layout transitions and the image view now cover every mip
  level. Each level is placed in the staging buffer at an offset that is a
  multiple of its texel block size.
- The sampler mipmap mode was swapped for kMinFilter_Linear_Mipmap_Nearest
  (LINEAR instead of NEAREST) and kMinFilter_Nearest_Mipmap_Linear (NEAREST
  instead of LINEAR); the min filter table itself was correct. The
  non-mipmapped min filters now clamp sampling to level 0
  (maxLod = 0.25), matching GLES.
- VK_FORMAT_R8G8B8_UNORM is not sampleable on many GPUs (no optimal
  tiling features at all on the Pixel 10 Pro's PowerVR). RGB_888 textures,
  including agdktunnel's fallback wall texture, failed with
  VUID-VkImageCreateInfo-imageCreateMaxMipLevels-02251,
  VUID-VkImageViewCreateInfo-None-02273 and
  VUID-vkCmdCopyBufferToImage-dstImage-01997. They are now expanded to
  RGBA8 on upload when R8G8B8 can't be sampled.
- A format the device can't sample, or a size or mip count over the
  device's limits for the format (vkGetPhysicalDeviceImageFormatProperties),
  is reported and leaves the texture empty, before anything is staged.
  GetFeatureAvailable(kFeature_ASTC) asked nothing and always returned
  true; it now reports the device's textureCompressionASTC_LDR feature.
- Creation parameters are validated once, in the Texture base class (the
  new renderer_texture.cpp, shared by TextureGLES and TextureVk): enum
  ranges, a compression type matching the format (none for RGBA/RGB),
  non-null data, nonzero dimensions, 1 to 16 mip levels and no more than a
  full mip chain, and enough bytes for each level, including compressed
  ASTC and ETC2 block data. Mip counts over 16 were silently truncated.
  Byte counts are computed in 64 bits, so huge dimensions can't wrap the
  required size to a small number (size_t is 32 bits on 32-bit ABIs).
  Invalid parameters assert in debug builds and leave the texture empty in
  release, with no mip levels. Allocation and object creation failures
  no longer continue with null handles, and release what was created, so
  a texture has either both an image view and a sampler or neither.
- RendererVk::BindTexture(nullptr) fell through and dereferenced the null
  texture (crash). It now drops the bind, since a null descriptor set
  can't be bound. The same applies to a texture whose creation failed.
  BindTexture asserts instead of crashing when called with a texture but
  no render state, and does nothing for render states whose vertex format
  has no texture coordinates: their descriptor set layout has no sampler
  binding, and writing one failed with
  VUID-VkWriteDescriptorSet-dstBinding-10009 (and crashed the validation
  layer).
- A texture bound but not yet drawn with left the descriptor set marked
  dirty when SetRenderState, SetRenderPass or EndFrame forgot the bound
  set, so the next draw bound VK_NULL_HANDLE
  (VUID-vkCmdBindDescriptorSets-pDescriptorSets-06563). Forgetting the
  bound set now also drops the pending bind, and draws never bind a null
  set.
- Per-frame texture descriptor sets were cached by image view alone, but
  render states can use different descriptor set layouts. The cache is
  now keyed by image view and layout. It was also constructed with 128
  empty entries that every lookup scanned; the space is now reserved
  instead.

Tested on a Pixel 10 Pro with agdktunnel and Vulkan Validation Layers
1.4.363.1, using a temporary hook that creates test textures:
- A 256x256 RGBA wall texture with 9 mip levels, each a different color,
  trilinear-nearest filtered. Before: the tunnel showed only level 0.
  After: the walls show a band of a different color per mip level as
  they recede, matching the same texture under GLES.
- ASTC textures of all 14 block sizes. Before: 20 copy errors (00171).
  After: no errors.
- RGB_888 textures. Before: image, view and copy errors. After: no errors,
  and a red/green/blue striped RGB wall texture renders with the right
  colors.
- BindTexture(nullptr). Before: SIGSEGV. After: returns, rendering
  continues.
- A texture bound after the last tunnel draw, followed by the HUD's
  untextured render state and draws. Before: 06563 on every frame. After:
  no errors.
- A texture bound with the HUD's untextured render state. Before: 10009
  and a SIGSEGV in the validation layer. After: no errors.
- No validation errors other than the semaphore reuse already on main
  (finding android#23, PR 12). GLES gameplay renders as before. Debug and
  release both build.
- Host test of the parameter validation: compression/format mismatch,
  out-of-range enums, more than 16 mips, more than a full chain, a
  32768x32768 RGBA texture (4 GiB, previously wrapped to 0 on 32-bit),
  and ASTC/ETC2 widths near UINT32_MAX are rejected; exact ASTC sizes
  are accepted and one byte short is rejected.
@natetrost
natetrost merged commit 25e9e9d into android:main Nov 23, 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