Skip to content

BGF: Fix Vulkan depth/stencil swapchain formats and image views - #130

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 depth/stencil handling of Vulkan swapchains in base_game_framework:

  • CreateSwapchain picked the depth/stencil VkFormat from the depth format alone (GetVkDepthFormat) and ignored display_stencil_format, so the advertised stencil combinations were never created: D32F + S8 created D32_SFLOAT (no stencil), D16 + S8 created D16_UNORM, and stencil-only S8 created no depth/stencil buffer at all. The VkFormat now comes from the (depth, stencil) pair. GetDepthStencilFormats and the new GetVkDepthStencilFormat share one table, so the offered combinations and the created formats always match. The unused GetVkStencilFormat and the depth-only table are removed. GetDepthStencilFormats also clears its output first.
  • Depth/stencil image views always used VK_IMAGE_ASPECT_DEPTH_BIT. They now use the format's aspects: depth, stencil (S8_UINT), or both for combined formats.
  • Each swapchain image has its own depth/stencil image, but views were picked by a separate counter advanced on every present rather than by the acquired image index. vkAcquireNextImageKHR does not hand out images in a fixed order, so two frames in flight could share a depth/stencil image. Renderers that cache framebuffers per color view would also pair a color image with the wrong depth image for good. The depth/stencil view now uses the acquired image index, and the separate counter is removed.
  • GetSwapchainFrameResourcesVk indexed the depth/stencil views unconditionally. That vector is empty when the swapchain has no depth/stencil buffer, so the read was out of bounds. It now returns VK_NULL_HANDLE in that case.
  • CreateSwapchainImages resets the acquired image index and state, since the new image set may be smaller than the old one.
  • CreateSwapchainImages reports failure. A failed vmaCreateImage or vkCreateImageView used to be logged and ignored, so it went on to create a view of a null image, and InitSwapchain still reported success. InitSwapchain now destroys the swapchain and fails if image creation or Swappy activation fails, instead of leaving a swapchain that GetSwapchainValid reports as valid. RecreateSwapchain stops if swapchain or image creation fails, instead of continuing with a null swapchain. DestroySwapchainImages skips handles that were never created and clears swapchain_images_.
  • The depth/stencil create-info structs are value-initialized, so the file builds with -Wall -Wextra (no -Wmissing-field-initializers warnings).

Fix depth/stencil handling of Vulkan swapchains in base_game_framework:

- CreateSwapchain picked the depth/stencil VkFormat from the depth format
  alone (GetVkDepthFormat) and ignored display_stencil_format, so the
  advertised stencil combinations were never created: D32F + S8 created
  D32_SFLOAT (no stencil), D16 + S8 created D16_UNORM, and stencil-only
  S8 created no depth/stencil buffer at all. The VkFormat now comes from
  the (depth, stencil) pair. GetDepthStencilFormats and the new
  GetVkDepthStencilFormat share one table, so the offered combinations
  and the created formats always match. The unused GetVkStencilFormat
  and the depth-only table are removed. GetDepthStencilFormats also
  clears its output first.
- Depth/stencil image views always used VK_IMAGE_ASPECT_DEPTH_BIT. They
  now use the format's aspects: depth, stencil (S8_UINT), or both for
  combined formats.
- Each swapchain image has its own depth/stencil image, but views were
  picked by a separate counter advanced on every present rather than by
  the acquired image index. vkAcquireNextImageKHR does not hand out images
  in a fixed order, so two frames in flight could share a depth/stencil
  image. Renderers that cache framebuffers per color view would also pair
  a color image with the wrong depth image for good. The depth/stencil
  view now uses the acquired image index, and the separate counter is
  removed.
- GetSwapchainFrameResourcesVk indexed the depth/stencil views
  unconditionally. That vector is empty when the swapchain has no
  depth/stencil buffer, so the read was out of bounds. It now returns
  VK_NULL_HANDLE in that case.
- CreateSwapchainImages resets the acquired image index and state, since
  the new image set may be smaller than the old one.
- CreateSwapchainImages reports failure. A failed vmaCreateImage or
  vkCreateImageView used to be logged and ignored, so it went on to
  create a view of a null image, and InitSwapchain still reported
  success. InitSwapchain now destroys the swapchain and fails if image
  creation or Swappy activation fails, instead of leaving a swapchain that
  GetSwapchainValid reports as valid. RecreateSwapchain stops if swapchain
  or image creation fails, instead of continuing with a null swapchain.
  DestroySwapchainImages skips handles that were never created and clears
  swapchain_images_.
- The depth/stencil create-info structs are value-initialized, so the file
  builds with -Wall -Wextra (no -Wmissing-field-initializers warnings).

Tested on a Pixel 10 Pro with agdktunnel and Vulkan Validation Layers
1.4.363.1, using a temporary test hook:
- For every depth/stencil combination the device offers, InitSwapchain
  succeeds, GetSwapchainFrameResourcesVk returns the expected format, and
  the swapchain is invalid after ShutdownSwapchain:
  none/none -> UNDEFINED with a null view (previously an out-of-bounds
  read), D24S8 -> D24_UNORM_S8_UINT, S8 -> S8_UINT, D16 -> D16_UNORM,
  D32F -> D32_SFLOAT, D32F+S8 -> D32_SFLOAT_S8_UINT (was D32_SFLOAT).
  D16_UNORM_S8_UINT is not supported on this device, so it is not
  offered.
- While rendering, each of the 5 swapchain images always gets its own
  depth/stencil view.
- agdktunnel renders correctly with its default D24S8 (including after
  background/resume) and with D32F+S8.
- No new validation errors. The two errors already on main (finding android#23,
  PR 12) are unchanged.
Not exercised: the image creation failure paths (no allocation failure
could be forced).
Rendering with no depth/stencil buffer still crashes, in simple_renderer:
RenderPassVk always declares a depth attachment, even with an UNDEFINED
format. This is tracked for PR 10.
@natetrost
natetrost merged commit 6b8bf20 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