diff --git a/crates/victory-gpu/src/draw.rs b/crates/victory-gpu/src/draw.rs index 64a6b59..2545bb6 100644 --- a/crates/victory-gpu/src/draw.rs +++ b/crates/victory-gpu/src/draw.rs @@ -1515,6 +1515,18 @@ pub(crate) fn stencil_ref(list: &HostList, reference: u32) { unsafe { list.list.OMSetStencilRef(reference) }; } +/// The title's depth bounds test range, on hosts that support it (a +/// pipeline with the test enabled draws nothing outside it). +pub(crate) fn depth_bounds(list: &HostList, min: f32, max: f32) { + use windows::core::Interface; + if crate::host::host().is_some_and(|h| h.depth_bounds()) + && let Ok(l1) = list.list.cast::() + { + // SAFETY: an open list on a device that supports depth bounds. + unsafe { l1.OMSetDepthBounds(min, max) }; + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/victory-gpu/src/host.rs b/crates/victory-gpu/src/host.rs index 00769ed..7ce96a8 100644 --- a/crates/victory-gpu/src/host.rs +++ b/crates/victory-gpu/src/host.rs @@ -460,6 +460,27 @@ impl Host { }) } + /// Whether the host GPU supports the depth bounds test; logged once. + pub(crate) fn depth_bounds(&self) -> bool { + static BOUNDS: OnceLock = OnceLock::new(); + *BOUNDS.get_or_init(|| { + let mut options = D3D12_FEATURE_DATA_D3D12_OPTIONS2::default(); + // SAFETY: writes `options` of the given size. + let ok = unsafe { + self.device + .CheckFeatureSupport( + D3D12_FEATURE_D3D12_OPTIONS2, + (&raw mut options).cast(), + size_of_val(&options) as u32, + ) + .is_ok() + }; + let bounds = ok && options.DepthBoundsTestSupported.as_bool(); + tracing::info!(bounds, "host depth bounds test support"); + bounds + }) + } + /// A new list, open for recording. pub(crate) fn new_list(&self) -> windows::core::Result { // SAFETY: plain object creation. diff --git a/crates/victory-gpu/src/list.rs b/crates/victory-gpu/src/list.rs index de0b092..19b19de 100644 --- a/crates/victory-gpu/src/list.rs +++ b/crates/victory-gpu/src/list.rs @@ -1008,6 +1008,11 @@ mod state { crate::draw::stencil_ref(list, reference); } } + pub(super) extern "system" fn depth_bounds(this: This, min: f32, max: f32) { + if let Some(list) = host_list(this) { + crate::draw::depth_bounds(list, min, max); + } + } pub(super) extern "system" fn heaps(this: This, count: u32, heaps: *const This) { with_state(this, |_, s| s.set_heaps(items(heaps, count))); } @@ -1196,6 +1201,7 @@ mod state { pub(super) use super::ignored_state as heaps; pub(super) use super::ignored_state as index_buffer; pub(super) use super::ignored_state as scissors; + pub(super) use super::ignored_state as depth_bounds; pub(super) use super::ignored_state as stencil_ref; pub(super) use super::ignored_state as targets; pub(super) use super::ignored_state as topology; @@ -1404,7 +1410,7 @@ const METHODS: [(usize, *const ()); 52] = [ (52, clear_rtv as *const ()), // 0x1a0 (59, execute_indirect as *const ()), // 0x1d8 indirect (60, copy_memory as *const ()), // 0x1e0 - (68, ignored_state as *const ()), // 0x220 depth bounds + (68, state::depth_bounds as *const ()), // 0x220 depth bounds (69, write32 as *const ()), // 0x228 (70, write64 as *const ()), // 0x230 (71, wait_value as *const ()), // 0x238, 32 bits diff --git a/crates/victory-gpu/src/pipeline/host.rs b/crates/victory-gpu/src/pipeline/host.rs index 3b1239a..a2d7064 100644 --- a/crates/victory-gpu/src/pipeline/host.rs +++ b/crates/victory-gpu/src/pipeline/host.rs @@ -26,6 +26,7 @@ pub(crate) struct GraphicsState { sample_mask: u32, rasterizer: D3D12_RASTERIZER_DESC, depth: D3D12_DEPTH_STENCIL_DESC, + depth_bounds: bool, /// `None` if the stream supplied an invalid input layout. input_layout: Option, strip_cut: D3D12_INDEX_BUFFER_STRIP_CUT_VALUE, @@ -85,6 +86,7 @@ impl Default for GraphicsState { BackFace: keep, ..Default::default() }, + depth_bounds: false, input_layout: Some(InputLayout::default()), strip_cut: D3D12_INDEX_BUFFER_STRIP_CUT_VALUE_DISABLED, topology: D3D12_PRIMITIVE_TOPOLOGY_TYPE_UNDEFINED, @@ -179,13 +181,12 @@ impl GraphicsState { } 11 => { self.depth = - clean_depth(data.cast::().read_unaligned()) + clean_depth(data.cast::().read_unaligned()); + self.depth_bounds = false; } 21 => { - // shortcut: the depth bounds test is dropped (the host - // pipeline uses the plain description); bring it back - // with a stream if a title enables it. let d = data.cast::().read_unaligned(); + self.depth_bounds = d.DepthBoundsTestEnable.as_bool(); self.depth = clean_depth(D3D12_DEPTH_STENCIL_DESC { DepthEnable: d.DepthEnable, DepthWriteMask: d.DepthWriteMask, @@ -234,8 +235,13 @@ impl GraphicsState { pub(crate) fn describe(&self) -> String { let inputs = self.input_layout.as_ref().map(InputLayout::describe); let inputs = inputs.as_deref().unwrap_or(""); + let bounds = if self.depth_bounds { + " depth-bounds" + } else { + "" + }; format!( - "{:?} {} {:?} {:?} {inputs} {} {} {:?} {} {:?}", + "{:?} {} {:?} {:?}{bounds} {inputs} {} {} {:?} {} {:?}", self.blend, self.sample_mask, self.rasterizer, @@ -247,6 +253,23 @@ impl GraphicsState { self.samples ) } + + /// The depth state with the depth bounds test flag a plain description + /// cannot hold: pipelines that enable the test are made from a stream. + fn depth1(&self) -> D3D12_DEPTH_STENCIL_DESC1 { + let d = &self.depth; + D3D12_DEPTH_STENCIL_DESC1 { + DepthEnable: d.DepthEnable, + DepthWriteMask: d.DepthWriteMask, + DepthFunc: d.DepthFunc, + StencilEnable: d.StencilEnable, + StencilReadMask: d.StencilReadMask, + StencilWriteMask: d.StencilWriteMask, + FrontFace: d.FrontFace, + BackFace: d.BackFace, + DepthBoundsTestEnable: self.depth_bounds.into(), + } + } } #[derive(Default)] @@ -720,6 +743,21 @@ impl HostPipeline { let Some(g) = g else { return refuse("graphics state not kept (plain description)", shaders); }; + let depth_bounds = if g.depth_bounds { + if host.depth_bounds() { + true + } else { + static SEEN: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); + if SEEN.fetch_add(1, std::sync::atomic::Ordering::Relaxed) < 4 { + tracing::warn!( + "a pipeline enables the depth bounds test, which the host GPU does not support: its draws pass at every depth" + ); + } + false + } + } else { + false + }; let state = g.describe(); if mesh.is_some() { let name = super::library::name( @@ -728,7 +766,15 @@ impl HostPipeline { &[&code_bytes(&code(0)), &code_bytes(&code(1))], &state, ); - let pso = match mesh_pipeline(&host.device, &root, code(0), code(1), g, &name) { + let pso = match mesh_pipeline( + &host.device, + &root, + code(0), + code(1), + g, + depth_bounds, + &name, + ) { Ok(pso) => pso, Err(e) => { tracing::warn!(error = %e, ?shaders, "host mesh pipeline refused"); @@ -788,13 +834,17 @@ impl HostPipeline { &[&code_bytes(&code(0)), &code_bytes(&code(1))], &state, ); - let made = super::library::pipeline( - &name, - // SAFETY: the description points at live data. - |library, name| unsafe { library.LoadGraphicsPipeline(name, &desc) }, - // SAFETY: as above. - || unsafe { host.device.CreateGraphicsPipelineState(&desc) }, - ); + let made = if depth_bounds { + graphics_depth_bounds_pipeline(&host.device, &root, &desc, g.depth1(), &name) + } else { + super::library::pipeline( + &name, + // SAFETY: the description points at live data. + |library, name| unsafe { library.LoadGraphicsPipeline(name, &desc) }, + // SAFETY: as above. + || unsafe { host.device.CreateGraphicsPipelineState(&desc) }, + ) + }; let pso = match made { Ok(pso) => pso, Err(e) => { @@ -828,35 +878,185 @@ impl HostPipeline { } /// A mesh pipeline from the title's graphics state: the pipeline-state -/// stream form (no input layout or topology). -fn mesh_pipeline( +/// A stream subobject: its type, then its description, pointer-aligned. +#[repr(C, align(8))] +struct Sub(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE, T); + +/// A pipeline made from a stream of subobjects, stored in the library +/// under `name` as any other. +fn stream_pipeline( + device: &ID3D12Device, + stream: &mut S, + name: &str, +) -> windows::core::Result { + use windows::core::Interface; + let desc = D3D12_PIPELINE_STATE_STREAM_DESC { + SizeInBytes: size_of::(), + pPipelineStateSubobjectStream: std::ptr::from_mut(stream).cast(), + }; + let device = device.cast::()?; + super::library::pipeline( + name, + // SAFETY: the stream holds whole subobjects pointing at live data. + |library, name| unsafe { library.LoadPipeline(name, &desc) }, + // SAFETY: as above. + || unsafe { device.CreatePipelineState(&desc) }, + ) +} + +/// A graphics pipeline with the depth bounds test enabled: the plain +/// description has nowhere for the flag, so the pipeline is made from a +/// stream with a DEPTH_STENCIL1 subobject. +fn graphics_depth_bounds_pipeline( device: &ID3D12Device, root: &ID3D12RootSignature, - ms: D3D12_SHADER_BYTECODE, - ps: D3D12_SHADER_BYTECODE, - g: &GraphicsState, + d: &D3D12_GRAPHICS_PIPELINE_STATE_DESC, + depth: D3D12_DEPTH_STENCIL_DESC1, name: &str, ) -> windows::core::Result { use windows::core::Interface; - /// A stream subobject: its type, then its description, pointer-aligned. - #[repr(C, align(8))] - struct Sub(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE, T); #[repr(C)] struct Stream { root: Sub<*mut std::ffi::c_void>, - ms: Sub, + vs: Sub, ps: Sub, blend: Sub, mask: Sub, raster: Sub, - depth: Sub, + depth: Sub, + input: Sub, + strip_cut: Sub, + topology: Sub, targets: Sub, dsv: Sub, samples: Sub, } - // SAFETY: `Stream` is plain-old-data; zeroing clears padding inside + #[repr(C)] + struct StreamGs { + base: Stream, + gs: Sub, + } + let targets = D3D12_RT_FORMAT_ARRAY { + RTFormats: d.RTVFormats, + NumRenderTargets: d.NumRenderTargets, + }; + // SAFETY: plain-old-data subobjects; the zeroed base clears padding so + // the library's description comparison is deterministic. + let mut base: Stream = unsafe { std::mem::zeroed() }; + base.root = Sub( + D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_ROOT_SIGNATURE, + root.as_raw(), + ); + base.vs = Sub(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_VS, d.VS); + base.ps = Sub(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_PS, d.PS); + base.blend = Sub(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_BLEND, d.BlendState); + base.mask = Sub( + D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_SAMPLE_MASK, + d.SampleMask, + ); + base.raster = Sub( + D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_RASTERIZER, + d.RasterizerState, + ); + base.depth = Sub(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL1, depth); + base.input = Sub( + D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_INPUT_LAYOUT, + d.InputLayout, + ); + base.strip_cut = Sub( + D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_IB_STRIP_CUT_VALUE, + d.IBStripCutValue, + ); + base.topology = Sub( + D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_PRIMITIVE_TOPOLOGY, + d.PrimitiveTopologyType, + ); + base.targets = Sub( + D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_RENDER_TARGET_FORMATS, + targets, + ); + base.dsv = Sub( + D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL_FORMAT, + d.DSVFormat, + ); + base.samples = Sub( + D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_SAMPLE_DESC, + d.SampleDesc, + ); + if d.GS.pShaderBytecode.is_null() { + let mut stream = base; + stream_pipeline(device, &mut stream, name) + } else { + let mut stream = StreamGs { + base, + gs: Sub(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_GS, d.GS), + }; + stream_pipeline(device, &mut stream, name) + } +} + +/// stream form (no input layout or topology). +fn mesh_pipeline( + device: &ID3D12Device, + root: &ID3D12RootSignature, + ms: D3D12_SHADER_BYTECODE, + ps: D3D12_SHADER_BYTECODE, + g: &GraphicsState, + depth_bounds: bool, + name: &str, +) -> windows::core::Result { + if depth_bounds { + let mut stream = mesh_stream( + root, + ms, + ps, + g, + Sub( + D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL1, + g.depth1(), + ), + ); + stream_pipeline(device, &mut stream, name) + } else { + let mut stream = mesh_stream( + root, + ms, + ps, + g, + Sub(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL, g.depth), + ); + stream_pipeline(device, &mut stream, name) + } +} + +#[repr(C)] +struct MeshStream { + root: Sub<*mut std::ffi::c_void>, + ms: Sub, + ps: Sub, + blend: Sub, + mask: Sub, + raster: Sub, + depth: Sub, + targets: Sub, + dsv: Sub, + samples: Sub, +} + +/// The mesh pipeline's subobject stream, generic over the depth +/// description: DEPTH_STENCIL1 when the pipeline enables the depth bounds +/// test, the plain one otherwise. +fn mesh_stream( + root: &ID3D12RootSignature, + ms: D3D12_SHADER_BYTECODE, + ps: D3D12_SHADER_BYTECODE, + g: &GraphicsState, + depth: Sub, +) -> MeshStream { + use windows::core::Interface; + // SAFETY: `MeshStream` is plain-old-data; zeroing clears padding inside // `Sub` so D3D12's pipeline library desc comparison is deterministic. - let mut stream: Stream = unsafe { std::mem::zeroed() }; + let mut stream: MeshStream = unsafe { std::mem::zeroed() }; stream.root = Sub( D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_ROOT_SIGNATURE, root.as_raw(), @@ -872,7 +1072,7 @@ fn mesh_pipeline( g.sample_mask, ); stream.raster = Sub(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_RASTERIZER, g.rasterizer); - stream.depth = Sub(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL, g.depth); + stream.depth = depth; stream.targets = Sub( D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_RENDER_TARGET_FORMATS, g.targets, @@ -882,18 +1082,7 @@ fn mesh_pipeline( g.depth_format, ); stream.samples = Sub(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_SAMPLE_DESC, g.samples); - let desc = D3D12_PIPELINE_STATE_STREAM_DESC { - SizeInBytes: size_of::(), - pPipelineStateSubobjectStream: (&raw mut stream).cast(), - }; - let device = device.cast::()?; - super::library::pipeline( - name, - // SAFETY: the stream holds whole subobjects pointing at live data. - |library, name| unsafe { library.LoadPipeline(name, &desc) }, - // SAFETY: as above. - || unsafe { device.CreatePipelineState(&desc) }, - ) + stream } /// The bytes of shader code (none for an absent stage).