graphics: bind stencil-only depth targets with HTile #2387
No reviewers
Labels
No labels
bug
documentation
duplicate
enhancement
GPU
GUI
help wanted
question
Shaders
Video
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/KytyPS5--KytyPS5!2387
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-2387"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@Nawid3333 on GitHub · 2026-11-18 13:49 UTC · original
Nawid3333:fix/stencil-only-depth-target→main· +426 −89 · 10 files · 8 commits · closed without mergingProblem
Baldur's Gate 3 (PPSA18463, #2334) stops before its main menu. @Crispy81's log on official
1e87171ends withunsupported render stateatdepthRenderTarget.cpp:39; a diagnostic fork build printed the register values:The Z format is invalid (
z_format=0): the target has no Z surface, only an 8-bit stencil buffer with HTile. The game still enables the depth test (zfunc=7, always) and clears the stencil.MakeDepthTargetDescrequired a Z address, so the target was rejected.Change
S8_UINTattachment over the stencil range (desc.info.data= the stencil range, one byte per element, guest formatk8UInt). Its HTile metadata and compressed-stencil clears are kept.TileGetDepthSizeaccepts such a surface: it sizes the stencil and HTile planes and returns an empty depth size.depth_active,depth_test_enable,depth_bounds_test_enableanddepth_clear_enableneed a Z format), and the load clear is masked to the attachment's aspects.ResolveHostDepthAttachmentFormatpicksS8_UINTfor such a target when the host supports it as a depth/stencil attachment; otherwise it stops with the existing "no host depth/stencil format" fatal.S8_UINTimages get depth/stencil attachment usage when the format supports it, copies of them use the stencil aspect (PrimaryAspect), and their sampled layout isDepthStencilReadOnlyOptimal.#1879 binds an S8 target without HTile and only with the depth test off (TopSpin 2K25). This target has HTile and an enabled depth test, so it does not cover it.
Tests
shader_recompiler_compute_tests:--image-rebind-only, check "stencil-only HTile target": a stencil-only target with HTile, an enabled depth test and a stencil clear binds as anS8_UINTattachment.--depth-readback-only, check "2560x1440 S8 with HTile and no Z surface": the stencil and HTile footprint of the game's surface.RX 9070 XT (Windows 11, clang-cl, with the harness change of #2038): both checks pass. Four other
--image-rebind-onlychecks (depth feedback loops) fail on this GPU, as they do on main with main's own tests. With main's sources and these tests, the footprint check fails and--image-rebind-onlystops at the game'sDepth target fatal: unsupported depth register state.Fork build of this commit (run 44198591074): Windows, Linux and macOS build.
In a game
2638b1b(191a9a7): the game reaches its main menu and character creation, then stops at a compute shader with more than 64 images (#2292).Written with Claude Code (AI): Claude Code found the cause in the reporter's logs and wrote the change, the tests and the tester builds; Claude Opus reviewed the change; GLM fact-checked this text.
@coderabbitai[bot] on GitHub · 2026-11-18 14:00 UTC · original · edited 2026-11-23 23:44 UTC
No actionable comments were generated in the recent review.
Walkthrough
The changes add support for stencil-only S8 surfaces across image format checks, image aspects and operations, depth-target setup, and guest footprint calculation. Render-target acquisition consumes pending HTile clear aspects according to the attachment format. Image refresh skips S8 images with compressed stencil metadata. Tests cover stencil-only HTile targets and depth, stencil, and HTile footprints.
Suggested reviewers:
nmzikChange: Bug fix
Merge Risk:
Moderate · previous review
2f964Stencil-only depth targets are now supported with several earlier issues fixed. Switching a target between combined depth+stencil and stencil-only over the same stencil memory can still lose stencil contents or hit an unsupported lookup path. The author defers this to a follow-up, so it should be accepted explicitly before merge.
Pre-merge checks:
4
Full details: Title check
Explanation
The title names the main change: binding stencil-only depth targets (no Z surface, S8 stencil) as S8_UINT attachments with HTile metadata. The raw summary shows changes to tile.cpp, textureCache, depthRenderTarget, image, imageInfo, imageView, renderDraw and tests that match this focus. The title is specific and accurate.
Full details: Description check
Explanation
The description explains the problem (Baldur's Gate 3 stopping at an unsupported depth register state), the fix (binding S8-only depth targets as S8_UINT attachments, sizing stencil and HTile planes, disabling depth test and clears without a Z format, and S8 image usage and copy aspect changes), and the tests run. It matches the changeset closely.
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Share
3f856956-e371-4b91-9848-9f964c659595Reviewing files that changed from the base of the PR and between 7b02cd1ac38251a2534701600375e128422ccf0d and
2f964edb09.tests/ShaderRecompilerComputeTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
@ -123,2 +121,2 @@return supports(policy.depth_attachment_format) ? policy.depth_attachment_format: vk::Format::eUndefined;if (policy == nullptr) {return supports(vk::Format::eS8Uint) ? vk::Format::eS8Uint : vk::Format::eUndefined;@coderabbitai[bot] on GitHub · 2026-11-18 14:00 UTC · original
Stencil-only targets must not reach the fatal path on devices without a usable S8 attachment.
A stencil-only target calls
ResolveHostDepthAttachmentFormatwith a null policy. Line 122 queriesVK_FORMAT_S8_UINTwithDepthTargetImageUsage(). Vulkan does not require native S8 support for optimal-tiling depth/stencil use, so this query can fail on a supported GPU. The caller at lines 194-198 then passeseUndefinedtoDepthFatal. A stencil-only target on such a GPU therefore stops the depth target path.Define the required behavior for this case. A stencil-only target must not reach the fatal path only because the S8 query fails. Choose the fallback or unbound behavior for this case explicitly, and keep the Z-plus-stencil paths unchanged.
@ -223,3 +232,1 @@desc.info.data = {depth_address, depth_backing_size};desc.info.stencil =has_stencil ? GuestRange {stencil_address, stencil_backing_size} : GuestRange {};desc.info.data = has_depth ? GuestRange {depth_address, depth_backing_size} : stencil_range;@coderabbitai[bot] on GitHub · 2026-11-18 14:00 UTC · original
Resolve stencil-only targets to native attachments, not association records.
Render to a 64×64 single-sample Z32+S8 target, then disable Z and keep the same stencil allocation. The new descriptor uses that allocation as
info.data. Its backing key matches the existingeS8Uintassociation, soFindImagecan return the association's ID.FindDepthTargetrejects that ID becausedepth_idis set. (raw.githubusercontent.com)For a stencil-only target lookup, distinguish native attachments from association records. If the current contents belong to a combined attachment, transfer them to native S8 before the first load. Keep the association available for lookups that need the combined attachment's stencil plane.
@coderabbitai[bot] on GitHub · 2026-11-18 14:00 UTC · original
On
src/graphics/host_gpu/renderer/depthRenderTarget.cppline 285 (outdated):Ignore unused Z addresses on stencil-only draws.
If the guest leaves distinct Z read and write addresses after a depth copy, then disables Z and enables stencil testing, the draw reaches this check through
stencil_active. Withdepth_write_disableunset, the check terminates the draw even thoughMakeDepthTargetDescdoes not use either Z address. Gate this comparison onhas_depth. Keep the existing check for draws that bind a Z plane.@ -241,1 +241,4 @@const auto* policy = FindGuestDepthFormatPolicy(info.guest_format);if (info.pixel_format == vk::Format::eS8Uint) {return info.guest_format == Prospero::BufferFormat::k8UInt && info.bytes_per_block == 1 &&!info.HasStencil() && info.samples == 1 && info.resources.levels == 1 &&@coderabbitai[bot] on GitHub · 2026-11-18 14:00 UTC · original
Reconcile the stencil-only S8 sample-count policy between depth setup and cache validation.
MakeDepthTargetDescsetsdesc.info.samplesfromz.z_info.num_samples.ResolveHostDepthAttachmentFormatselectseS8Uintfor a null policy when the host supports it at that sample count. On such a host, a stencil-only target with more than one sample reachesFindImage.IsSupportedDepthTargetFormatrejects it because the S8 branch requiresinfo.samples == 1.ValidateImageDesc, whichFindImagecalls, then exits the process.Use one sample-count policy for both stages. Either limit stencil-only S8 selection to one sample before the target reaches the cache, or extend
IsSupportedDepthTargetFormatto accept multisampled S8. If multisampled S8 is accepted, also handle its initialization.InitializeImageskips the guest upload for images with more than one sample, so such an image does not receive guest contents before its first use.@coderabbitai[bot] on GitHub · 2026-11-18 14:00 UTC · original · edited 2026-11-18 18:42 UTC
On
src/graphics/host_gpu/renderer/renderDraw.cppline 491 (outdated):A stencil-only pass consumes a pending depth clear without applying it.
At
renderDraw.cpp:491, the mask removes the depth aspect for a format without depth. The HTile clear state is still consumed at lines 492-493.TouchMeta(..., false)clears the slice bit inclear_mask. A later depth pass on the same HTile address and slice then reads that bit as false and receives no depth clear. The depth buffer keeps its prior contents, which can change depth test results. This applies when the guest rebinds the same HTile range without first re-clearing it.The pending depth clear must survive a pass that does not apply it. Consume the HTile clear state only when the depth clear is applied, or keep the slice clear pending for a later depth pass.
✅ Addressed in commit ffd2fe4
@coderabbitai[bot] on GitHub · 2026-11-18 14:00 UTC · original
On
tests/ShaderRecompilerComputeTests.cppline 17995 (outdated):Guard the stencil-only S8 case against unsupported
eS8Uintattachments.The stencil-only path depends on
ResolveHostDepthAttachmentFormatfindingVK_FORMAT_S8_UINTfor optimal tiling, with the depth-target usage and the target's sample count. Vulkan does not requireVK_FORMAT_S8_UINTas a depth/stencil attachment. TheVulkanHarnessdevice filter does not check this format either. On a device that passes the filter but rejects the query,MakeDepthTargetDesccallsDepthFatal, andDepthFatalexits. The--image-rebind-onlyrun then ends before the later checks.Query the same format, usage, and sample count in the harness before running the stencil-only case. When the query fails, skip that case and report the skip. Keep the S8 case as a required check on devices that report support.
@coderabbitai[bot] on GitHub · 2026-11-18 14:00 UTC · original · reviewed
Actionable comments posted: 6
d54d556d-efe9-42fa-bd98-924a5456778fReviewing files that changed from the base of the PR and between
b3295bebc6and 191a9a76a3103348f12340e6059cd829cb6180cc.src/graphics/guest_gpu/tile.cppsrc/graphics/host_gpu/renderer/cache/textureCache.cppsrc/graphics/host_gpu/renderer/depthRenderTarget.cppsrc/graphics/host_gpu/renderer/image/image.cppsrc/graphics/host_gpu/renderer/image/image.hsrc/graphics/host_gpu/renderer/image/imageInfo.hsrc/graphics/host_gpu/renderer/image/imageView.cppsrc/graphics/host_gpu/renderer/renderDraw.cpptests/ShaderRecompilerComputeTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
@coderabbitai[bot] on GitHub · 2026-11-18 18:11 UTC · original
On
src/graphics/host_gpu/renderer/renderDraw.cppline 486 (outdated):Do not consume pending stencil clears for depth-only attachments.
After
ClearMetasets both bits, a depth-only acquisition requests both aspects here.TakeMetaClearresets both bits, but Line 499 discards the stencil result. A later compressed S8 attachment on the same HTile address and slice receives no stencil clear.Request only the aspects that this acquisition will materialize. Exclude stencil when the attachment has no stencil aspect or
metadata.stencil_compressedis false. Keep both aspects for compressed combined attachments and stencil alone for compressed S8 attachments.@coderabbitai[bot] on GitHub · 2026-11-18 18:11 UTC · original · reviewed
Actionable comments posted: 1
441fcca9-6779-42bf-84f0-8b9c306c98a5Reviewing files that changed from the base of the PR and between 191a9a76a3103348f12340e6059cd829cb6180cc and 56c1102e5c8bf2d4ca8a4c85c2a47791f801a33a.
src/graphics/host_gpu/renderer/cache/textureCache.cppsrc/graphics/host_gpu/renderer/cache/textureCache.hsrc/graphics/host_gpu/renderer/depthRenderTarget.cppsrc/graphics/host_gpu/renderer/renderDraw.cpptests/ShaderRecompilerComputeTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
@Nawid3333 on GitHub · 2026-11-18 19:44 UTC · original
Rebased onto main (
6c618255e) and pushed 56c1102e5, ffd2fe490 and 7b02cd1ac for the CodeRabbit reviews:renderDraw.cpp:491(pending depth clear) andrenderDraw.cpp:486(pending stencil clear): fixed. The HTile clear state is now kept per aspect, and a bind takes only the aspects it has planes for, so a stencil-only bind leaves a pending depth clear and a depth-only bind leaves a pending stencil clear. A stencil plane without HTile stencil compression takes the stencil clear without applying it, as on main. Three new checks clear the HTile with the explicit clear flags off and then bind over the same HTile: stencil-only then depth-only Z32, Z32 then stencil-only, and Z32+S8 with uncompressed stencil then with compressed stencil. "stencil-only bind keeps a pending HTile depth clear" fails with the first commit's sources, "depth-only bind keeps a pending HTile stencil clear" fails with 56c1102e5, "uncompressed stencil bind drops the pending HTile stencil clear" fails with an intermediate version that left that bit pending; all three pass with ffd2fe490.IsMetaClearedkeeps main's meaning (a pending depth clear), which the existing depth-only checks rely on. This also covers the nitpick on a stencil clear that comes only from HTile metadata.textureCache.cpp:267-268(retiring the HTile owner): fixed in 7b02cd1ac.ReleaseHtileOwnerpasses the HTile clear state to a live image with the same HTile range and erases it only when none is left. It runs when the owner is deleted, when an image moves to another HTile, and whenAssociateStenciladopts a stencil-only image as a depth target's stencil plane; before, the last two kept the old range owned, and a deletion also made the survivor's next bind re-create the state with both clears set. Checks "retiring the HTile owner keeps the shared clear state", "a depth image moved to another HTile releases the old one" and "an adopted stencil image releases its HTile" fail with the sources before each fix and pass with 7b02cd1ac.depthRenderTarget.cpp:285: fixed, the Z read/write address check now needs a Z surface. The stencil-only test target now keeps a stale Z read address; with the first commit's sources the test stops atDepth target fatal: unsupported depth register state.imageInfo.h:248: a multisampled stencil-only target now stops inMakeDepthTargetDescwith its own message. Multisampled stencil-only targets stay unsupported in this PR.ShaderRecompilerComputeTests.cpp:17995: the stencil-only cases run only when the host reportsS8_UINTwith the depth-target usage at one sample; otherwise the test prints that they were skipped.depthRenderTarget.cpp:122(noS8_UINTattachment on the host),depthRenderTarget.cpp:229andtextureCache.cpp:1184(a stencil-only target over a Z+S8 target's stencil plane, and back): main stops at every stencil-only target, so these cases stop on main too. Baldur's Gate 3 reached the game with the first commit and #2292 (#2334) without hitting them. They need a fallback format and a stencil copy between native S8 and combined images, which I would rather do in a follow-up PR with its own tests.RX 9070 XT (Windows 11, clang-cl, with the harness change of #2038), 7b02cd1ac:
--image-rebind-onlyfails only the four depth feedback-loop checks that also fail on main;--depth-readback-onlypasses; the full compute suite passes 192 cases and fails only the two checks that also fail with main's sources on this GPU. The macOS job fails on main's ownVirtualMemoryAllocationTests.cppXbyak error, not on this PR.@Nawid3333 on GitHub · 2026-11-26 21:31 UTC · original
Main has this since
b446af9d2(graphics: render stencil-only targets and preserve shared stencil contents), which renders the stencil-only targets of #2334 as well, so I'm closing this one.Pull request closed