Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions runtime/d3d12/src/device/depth.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -16,15 +16,18 @@ pub(super) fn primary(desc: &D3D12_RESOURCE_DESC) -> Option<D3D12_RESOURCE_DESC>
}

fn pair_primary(desc: &D3D12_RESOURCE_DESC) -> Option<D3D12_RESOURCE_DESC> {
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
})
Expand Down
13 changes: 7 additions & 6 deletions runtime/d3d12/src/device/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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,
}
}
Expand Down Expand Up @@ -64,7 +65,7 @@ pub(super) fn substitute(desc: &D3D12_RESOURCE_DESC) -> Option<D3D12_RESOURCE_DE
// Typeless storage lets the depth, stencil and SRV plane views share it.
// Desktop depth images cannot also allow unordered access.
D16_UNORM_S8_UINT if flags & D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL.0 != 0 => {
(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)
Expand Down
11 changes: 8 additions & 3 deletions runtime/d3d12/src/device/heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down
27 changes: 24 additions & 3 deletions runtime/d3d12/src/device/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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);
Expand Down
Loading