From 0e00f8346b7a62e04819f4c906027935ed6d4f2d Mon Sep 17 00:00:00 2001 From: Jerkin42 <66524771+Jerkin42@users.noreply.github.com> Date: Wed, 25 Nov 2026 03:34:46 +0000 Subject: [PATCH] d3d12: give 16-bit depth with stencil a writable depth plane GTA VI creates its scene depth as D16_UNORM_S8_UINT with both depth-stencil and unordered access, and writes depth from compute through an R16_UNORM view of the depth plane. Host depth images cannot have unordered access, so these views had nothing to write to. Use the float depth pair that R32G8X24 surfaces already have. The host depth becomes D32_FLOAT_S8X24_UINT, whose float holds every 16-bit value exactly, so an EQUAL test against a depth a shader wrote still passes. A view of the stencil plane becomes a null view and is reported once. Co-Authored-By: Claude Opus 5.5 --- runtime/d3d12/src/device/depth.rs | 9 ++++++--- runtime/d3d12/src/device/format.rs | 13 +++++++------ runtime/d3d12/src/device/heap.rs | 11 ++++++++--- runtime/d3d12/src/device/views.rs | 27 ++++++++++++++++++++++++--- 4 files changed, 45 insertions(+), 15 deletions(-) diff --git a/runtime/d3d12/src/device/depth.rs b/runtime/d3d12/src/device/depth.rs index 38269680..7fdd1c17 100644 --- a/runtime/d3d12/src/device/depth.rs +++ b/runtime/d3d12/src/device/depth.rs @@ -1,4 +1,4 @@ -//! A Scarlett depth texture can expose its float plane as a writable UAV. On +//! A Scarlett depth texture can expose its depth plane as a writable UAV. On //! desktop D3D12 the depth/stencil image and writable R32 image must be distinct. use super::{Backing, Device, Out, RESOURCE, wrap_pageable}; use windows::Win32::Foundation::{E_INVALIDARG, E_POINTER}; @@ -16,15 +16,18 @@ pub(super) fn primary(desc: &D3D12_RESOURCE_DESC) -> Option } fn pair_primary(desc: &D3D12_RESOURCE_DESC) -> Option { + let writable = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL.0 | D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS.0; (desc.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE2D - && desc.Format == DXGI_FORMAT_R32G8X24_TYPELESS - && matches!(desc.Flags.0 as u32, 0x0100_0006 | 0x8030_0006) + && (desc.Format == DXGI_FORMAT_R32G8X24_TYPELESS + && matches!(desc.Flags.0 as u32, 0x0100_0006 | 0x8030_0006) + || desc.Format == super::format::D16_UNORM_S8_UINT && desc.Flags.0 & super::format::HOST_FLAGS == writable) && desc.MipLevels == 1 && desc.DepthOrArraySize == 1 && desc.SampleDesc.Count == 1 && desc.SampleDesc.Quality == 0 && desc.Layout == D3D12_TEXTURE_LAYOUT_UNKNOWN) .then_some(D3D12_RESOURCE_DESC { + Format: DXGI_FORMAT_R32G8X24_TYPELESS, Flags: D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL, ..*desc }) diff --git a/runtime/d3d12/src/device/format.rs b/runtime/d3d12/src/device/format.rs index 76a3f285..ac294eb3 100644 --- a/runtime/d3d12/src/device/format.rs +++ b/runtime/d3d12/src/device/format.rs @@ -3,8 +3,9 @@ //! pipeline target formats are translated to match. Copies see host texels. use windows::Win32::Graphics::{Direct3D12::*, Dxgi::Common::*}; -// Desktop D3D12 has no 16-bit depth with stencil. D24S8 also stores unsigned -// normalized depth, and its extra precision does not change test results. +// Desktop D3D12 has no 16-bit depth with stencil. The float format holds each +// 16-bit value a shader writes through the companion image of `depth::Pair` +// exactly, so a depth test for equality against that value still passes. pub(crate) const D16_UNORM_S8_UINT: DXGI_FORMAT = DXGI_FORMAT(118); const R16_UNORM_X8_TYPELESS: DXGI_FORMAT = DXGI_FORMAT(119); const X16_TYPELESS_G8_UINT: DXGI_FORMAT = DXGI_FORMAT(120); @@ -14,9 +15,9 @@ const X16_TYPELESS_G8_UINT: DXGI_FORMAT = DXGI_FORMAT(120); /// samples the same normalized channels. pub(crate) fn host_format(format: DXGI_FORMAT) -> DXGI_FORMAT { match format { - D16_UNORM_S8_UINT => DXGI_FORMAT_D24_UNORM_S8_UINT, - R16_UNORM_X8_TYPELESS => DXGI_FORMAT_R24_UNORM_X8_TYPELESS, - X16_TYPELESS_G8_UINT => DXGI_FORMAT_X24_TYPELESS_G8_UINT, + D16_UNORM_S8_UINT => DXGI_FORMAT_D32_FLOAT_S8X24_UINT, + R16_UNORM_X8_TYPELESS => DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS, + X16_TYPELESS_G8_UINT => DXGI_FORMAT_X32_TYPELESS_G8X24_UINT, other => other, } } @@ -64,7 +65,7 @@ pub(super) fn substitute(desc: &D3D12_RESOURCE_DESC) -> Option { - (DXGI_FORMAT_R24G8_TYPELESS, flags & !D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS.0) + (DXGI_FORMAT_R32G8X24_TYPELESS, flags & !D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS.0) } DXGI_FORMAT_B4G4R4A4_UNORM if flags & D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET.0 != 0 => { (DXGI_FORMAT_R8G8B8A8_UNORM, flags) diff --git a/runtime/d3d12/src/device/heap.rs b/runtime/d3d12/src/device/heap.rs index 84700d70..b46d9b84 100644 --- a/runtime/d3d12/src/device/heap.rs +++ b/runtime/d3d12/src/device/heap.rs @@ -878,9 +878,9 @@ pub(super) unsafe extern "system" fn committed_layout( let packed = super::allocation::packed_uav_formats(desc.Format, desc.Flags); let castable = if count == 0 { (!packed.is_empty()).then_some(packed) } else { Some(unsafe { std::slice::from_raw_parts(formats, count as usize) }) }; - // Only the format translation applies here; a float depth plane with a - // writable companion is not created through this entry point. - let translated = super::depth::primary(desc).filter(|_| !super::depth::paired(desc)); + // A float depth plane with a writable companion is not created through this entry point. + let paired = super::depth::paired(desc); + let translated = super::depth::primary(desc).filter(|_| !paired || desc.Format == super::format::D16_UNORM_S8_UINT); let initial = super::format::host_initial_layout(desc, initial); let host_clear = super::format::host_clear(clear); let clear = host_clear.as_ref().map_or(clear, core::ptr::from_ref); @@ -920,6 +920,11 @@ pub(super) unsafe extern "system" fn committed_layout( "display surface res={:#x} at {:#x}+{:#x}", native.as_raw() as usize, memory.address, plane.size)); let address = super::views::TextureAddress::at(device, &native, memory.address, false, Some(memory)); super::Backing::Addressed(address, *desc) + } else if let Some(primary) = translated.as_ref().filter(|_| paired) { + match super::depth::Pair::new(&device.native, heap, desc, primary) { + Ok(pair) => super::Backing::Depth(Box::new(pair)), + Err(error) => return error.code(), + } } else if translated.is_some() { super::Backing::Translated(*desc) } else { diff --git a/runtime/d3d12/src/device/views.rs b/runtime/d3d12/src/device/views.rs index b20175ad..3ed87db7 100644 --- a/runtime/d3d12/src/device/views.rs +++ b/runtime/d3d12/src/device/views.rs @@ -927,15 +927,35 @@ pub(super) unsafe extern "system" fn uav( .and_then(|r| r.state.depth()) { use windows::Win32::Graphics::Dxgi::Common::{ - DXGI_FORMAT_R32_FLOAT, DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS, + DXGI_FORMAT_R16_UNORM, DXGI_FORMAT_R32_FLOAT, DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS, }; let Some(view) = (unsafe { desc.as_ref() }) else { fail_call(format_args!("depth UAV needs a typed plane view")); }; + if pair.guest.Format == super::format::D16_UNORM_S8_UINT + && view.ViewDimension == D3D12_UAV_DIMENSION_TEXTURE2D + && unsafe { view.Anonymous.Texture2D.PlaneSlice } == 1 + { + // The companion has no stencil. Keep the descriptor defined (null: + // reads return zero, writes are dropped) and report it. + static STENCIL_UAV: std::sync::Once = std::sync::Once::new(); + crate::diagnostics::log_once(&STENCIL_UAV, || format!( + "null UAV for the Scarlett stencil plane of {}x{}: view format={}", + pair.guest.Width, pair.guest.Height, view.Format.0 + )); + let null = D3D12_UNORDERED_ACCESS_VIEW_DESC { + Anonymous: D3D12_UNORDERED_ACCESS_VIEW_DESC_0 { Texture2D: D3D12_TEX2D_UAV::default() }, + ..*view + }; + unsafe { device.native.CreateUnorderedAccessView(None, None, Some(&null), handle) }; + return; + } if counter.is_some() || view.ViewDimension != D3D12_UAV_DIMENSION_TEXTURE2D // Either the plane format or the float format of the depth plane. - || !matches!(view.Format, DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS | DXGI_FORMAT_R32_FLOAT) + || !(matches!(view.Format, DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS | DXGI_FORMAT_R32_FLOAT) + // The companion of a 16-bit plane holds its normalized values as floats. + || view.Format == DXGI_FORMAT_R16_UNORM && pair.guest.Format == super::format::D16_UNORM_S8_UINT) || unsafe { view.Anonymous.Texture2D.MipSlice != 0 || view.Anonymous.Texture2D.PlaneSlice != 0 } @@ -1017,7 +1037,8 @@ pub(super) unsafe extern "system" fn dsv( &raw const translated } None if let Some(guest) = unsafe { super::resource_proxy(object, identity(device).as_raw()) } - .and_then(|p| p.state.translated()) => { + .and_then(|p| p.state.translated().or_else(|| p.state.depth().map(|pair| &pair.guest) + .filter(|guest| guest.Format == super::format::D16_UNORM_S8_UINT))) => { // A typeless host image needs the typed depth format the guest implied. let mut view = D3D12_DEPTH_STENCIL_VIEW_DESC { Format: super::format::host_format(guest.Format), ..Default::default() }; let layers = u32::from(guest.DepthOrArraySize);