From 7b8c8e30481019ec8c43d563c06122168a5f21b9 Mon Sep 17 00:00:00 2001 From: AgneyaK <82705373+AgneyaK@users.noreply.github.com> Date: Tue, 24 Nov 2026 11:47:04 -0800 Subject: [PATCH 1/2] GPU: no render-target view, barrier or clear for a texture without the render-target use A host texture made without ALLOW_RENDER_TARGET (#55 drops a use the host refuses for the format, e.g. B4G4R4A4_UNORM on NVIDIA) was still cleared and bound as a render target: CreateRenderTargetView, a barrier to RENDER_TARGET and ClearRenderTargetView on it are invalid calls, and the device is removed with DXGI_ERROR_INVALID_CALL. `host::renders_to` tells. The clear skips such a texture (warned once); `set_targets` binds a null RTV in its slot (`Host::null_rtv`), so the other targets keep their slots and draws write nothing there. No change while every texture keeps its render-target use, as on `main` today. Co-Authored-By: Claude Opus 5.5 --- crates/victory-gpu/src/draw.rs | 8 ++++++++ crates/victory-gpu/src/host.rs | 27 +++++++++++++++++++++++++++ crates/victory-gpu/src/list.rs | 12 ++++++++++++ 3 files changed, 47 insertions(+) diff --git a/crates/victory-gpu/src/draw.rs b/crates/victory-gpu/src/draw.rs index 3172caf810..8963c1bd43 100644 --- a/crates/victory-gpu/src/draw.rs +++ b/crates/victory-gpu/src/draw.rs @@ -334,6 +334,14 @@ impl State { let Some(texture) = crate::resource::host_resource(record.resource) else { continue; }; + if !crate::host::renders_to(texture) { + once!( + resource = format_args!("{:#x}", record.resource as usize), + "render target the host cannot render to: a null view in its slot" + ); + rtvs.push(host.null_rtv()); + continue; + } self.targets.push(record.resource as usize); #[cfg(feature = "diagnostics")] crate::capture::note_target(list, record.resource); diff --git a/crates/victory-gpu/src/host.rs b/crates/victory-gpu/src/host.rs index 09314c377c..18ad5dd4b1 100644 --- a/crates/victory-gpu/src/host.rs +++ b/crates/victory-gpu/src/host.rs @@ -1227,6 +1227,25 @@ impl Host { handle } + /// A null render target view, for a slot the title binds a texture the + /// host cannot render to (its copy was made without that use): draws + /// write nothing there, and the other targets keep their slots. + pub(crate) fn null_rtv(&self) -> D3D12_CPU_DESCRIPTOR_HANDLE { + let slot = self.next_rtv.fetch_add(1, Ordering::Relaxed) % RTV_RING; + let desc = D3D12_RENDER_TARGET_VIEW_DESC { + Format: DXGI_FORMAT_R8G8B8A8_UNORM, + ViewDimension: D3D12_RTV_DIMENSION_TEXTURE2D, + ..Default::default() + }; + // SAFETY: a slot of our heap; a null resource is allowed with a desc. + unsafe { + let mut handle = self.rtvs.GetCPUDescriptorHandleForHeapStart(); + handle.ptr += (slot * self.rtv_size) as usize; + self.device.CreateRenderTargetView(None, Some(&desc), handle); + handle + } + } + /// A host depth view of `resource`, valid until it is recorded. pub(crate) fn dsv( &self, @@ -2621,6 +2640,14 @@ pub(crate) fn legal_state(texture: &ID3D12Resource, state: u32) -> u32 { legal_for(unsafe { texture.GetDesc() }.Flags.0 as u32, state) } +/// Whether the host copy of a texture can be a render target: not when the +/// host refused that use for its format and the copy was made without it. +pub(crate) fn renders_to(texture: &ID3D12Resource) -> bool { + // SAFETY: a plain query. + let flags = unsafe { texture.GetDesc() }.Flags; + flags.0 & D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET.0 != 0 +} + /// [`legal_state`] for a texture with resource flags `flags`. pub(crate) fn legal_for(flags: u32, state: u32) -> u32 { // (flag, set or clear forbids, state bits): render target, unordered diff --git a/crates/victory-gpu/src/list.rs b/crates/victory-gpu/src/list.rs index edcebb866e..b812307868 100644 --- a/crates/victory-gpu/src/list.rs +++ b/crates/victory-gpu/src/list.rs @@ -718,6 +718,18 @@ extern "system" fn clear_rtv( unsafe { std::slice::from_raw_parts(rects.cast(), count as usize) } }); const RENDER_TARGET: u32 = 0x4; + if !crate::host::renders_to(texture) { + // Its host copy was made without the render-target use (the + // host refuses it for the format): no view, no barrier, no clear. + static ONCE: std::sync::Once = std::sync::Once::new(); + ONCE.call_once(|| { + tracing::warn!( + resource = format_args!("{:#x}", record.resource as usize), + "clear of a render target the host cannot render to: skipped" + ) + }); + return; + } let rtv = host.rtv(texture, record.desc().as_ref()); // SAFETY: an open list, a fresh view of a live texture. unsafe { From 736fa790eec1f0d9a7a22290c4c77aef31fc1935 Mon Sep 17 00:00:00 2001 From: AgneyaK <82705373+AgneyaK@users.noreply.github.com> Date: Tue, 24 Nov 2026 19:44:27 -0800 Subject: [PATCH 2/2] GPU: a render target the host cannot bind keeps its slot with a null view `set_targets` skipped a target with no recorded view or no host texture (`continue`), so every later target moved down a slot: a draw writing `SV_Target1` wrote it into the texture bound as target 1 only if target 0 existed on the host. On NVIDIA, `main` already has such a target: a 2560x1440 B4G4R4A4_UNORM render target the host refuses gets no host texture (#1754, #1758, #1781), so the targets after it get the wrong outputs. The title's typed null views (no resource) took the same path (#450). Each of those slots now gets a null view: no recorded view, no host texture, a typed null view, or a copy made without the render-target use. The null view takes the title's view format where the host renders to it (`renders_format`, `D3D12_FORMAT_SUPPORT1_RENDER_TARGET`, asked once per format), so it matches the pipeline's target formats; else R8G8B8A8_UNORM. Test `a_target_that_cannot_be_bound_keeps_the_others_in_their_slots` draws SV_Target0 red and SV_Target1 green with slot 0 unbindable (no view; a typed null view) and reads the slot-1 texture back: green. On `main`'s `set_targets` it reads red in both cases. Co-Authored-By: Claude Opus 5.5 --- crates/victory-gpu/src/draw.rs | 123 ++++++++++++++++++++++++++++++++- crates/victory-gpu/src/host.rs | 44 ++++++++++-- 2 files changed, 160 insertions(+), 7 deletions(-) diff --git a/crates/victory-gpu/src/draw.rs b/crates/victory-gpu/src/draw.rs index 8963c1bd43..38450e1139 100644 --- a/crates/victory-gpu/src/draw.rs +++ b/crates/victory-gpu/src/draw.rs @@ -323,15 +323,25 @@ impl State { }; self.targets.clear(); let mut rtvs = Vec::new(); + // A target the host cannot bind gets a null view in its slot: the + // title's slots are positional (`SV_Target`), so skipping one + // would move every later target to the wrong output. for &h in handles { let Some(record) = crate::descriptor::rtv_record(h) else { once!( handle = format_args!("{h:#x}"), - "render target without a recorded view" + "render target without a recorded view: a null view in its slot" ); + rtvs.push(host.null_rtv(None)); continue; }; + let format = record.desc().map(|d| d.Format); let Some(texture) = crate::resource::host_resource(record.resource) else { + once!( + resource = format_args!("{:#x}", record.resource as usize), + "render target without a host texture: a null view in its slot" + ); + rtvs.push(host.null_rtv(format)); continue; }; if !crate::host::renders_to(texture) { @@ -339,7 +349,7 @@ impl State { resource = format_args!("{:#x}", record.resource as usize), "render target the host cannot render to: a null view in its slot" ); - rtvs.push(host.null_rtv()); + rtvs.push(host.null_rtv(format)); continue; } self.targets.push(record.resource as usize); @@ -2143,6 +2153,115 @@ void main(uint3 id : SV_DispatchThreadID) { } "#; + /// Render targets keep their slots when one of them cannot be bound: + /// with no recorded view in slot 0, or the title's typed null view + /// there (no resource), the texture in slot 1 still gets `SV_Target1` + /// (it got `SV_Target0` when the slot was skipped). + #[test] + fn a_target_that_cannot_be_bound_keeps_the_others_in_their_slots() { + let Some(host) = crate::host::host() else { + return; // no host GPU here + }; + const SHADERS: &str = r#" +float4 vs(uint id : SV_VertexID) : SV_Position { + return float4(id == 1 ? 3.0 : -1.0, id == 2 ? -3.0 : 1.0, 0.0, 1.0); +} +struct Out { float4 a : SV_Target0; float4 b : SV_Target1; }; +Out ps() { Out o; o.a = float4(1, 0, 0, 1); o.b = float4(0, 1, 0, 1); return o; } +"#; + let compile = |entry: windows::core::PCSTR, target: windows::core::PCSTR| { + let (mut code, mut errors): (Option, Option) = (None, None); + // SAFETY: the source and names outlive the call. + unsafe { + D3DCompile(SHADERS.as_ptr().cast(), SHADERS.len(), s!("targets"), None, None, entry, target, 0, 0, &mut code, Some(&mut errors)) + .unwrap(); + } + code.unwrap() + }; + let (vs, ps) = (compile(s!("vs"), s!("vs_5_1")), compile(s!("ps"), s!("ps_5_1"))); + let bytecode = |b: &ID3DBlob| D3D12_SHADER_BYTECODE { + // SAFETY: the blob's own bytes. + pShaderBytecode: unsafe { b.GetBufferPointer() }, + // SAFETY: as above. + BytecodeLength: unsafe { b.GetBufferSize() }, + }; + // SAFETY: plain creation from an empty description. + let root: ID3D12RootSignature = unsafe { + let mut blob = None; + D3D12SerializeRootSignature(&D3D12_ROOT_SIGNATURE_DESC::default(), D3D_ROOT_SIGNATURE_VERSION_1, &mut blob, None).unwrap(); + let blob = blob.unwrap(); + host.device + .CreateRootSignature(0, std::slice::from_raw_parts(blob.GetBufferPointer().cast::(), blob.GetBufferSize())) + .unwrap() + }; + let mut formats = [DXGI_FORMAT(0); 8]; + formats[..2].fill(windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_R8G8B8A8_UNORM); + let desc = D3D12_GRAPHICS_PIPELINE_STATE_DESC { + // SAFETY: a borrowed pointer the call does not release. + pRootSignature: unsafe { std::mem::transmute_copy(&root) }, + VS: bytecode(&vs), + PS: bytecode(&ps), + BlendState: D3D12_BLEND_DESC { + RenderTarget: [D3D12_RENDER_TARGET_BLEND_DESC { RenderTargetWriteMask: 0xf, ..Default::default() }; 8], + ..Default::default() + }, + SampleMask: u32::MAX, + RasterizerState: D3D12_RASTERIZER_DESC { FillMode: D3D12_FILL_MODE_SOLID, CullMode: D3D12_CULL_MODE_NONE, ..Default::default() }, + PrimitiveTopologyType: D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE, + NumRenderTargets: 2, + RTVFormats: formats, + SampleDesc: windows::Win32::Graphics::Dxgi::Common::DXGI_SAMPLE_DESC { Count: 1, Quality: 0 }, + ..Default::default() + }; + // SAFETY: a complete description whose parts outlive the call. + let pso: ID3D12PipelineState = unsafe { host.device.CreateGraphicsPipelineState(&desc) }.unwrap(); + // A 16x16 R8G8B8A8_UNORM title texture that may be a render target, + // and its recorded view; slot 0's handle has no recorded view. + let records = vec![0u8; 3 * 128]; + let (none, typed_null, good) = (records.as_ptr() as u64, records.as_ptr() as u64 + 128, records.as_ptr() as u64 + 256); + // A typed null view: R8G8B8A8_UNORM, 2D, no resource. + crate::descriptor::create_rtv(std::ptr::null(), std::ptr::null(), &[28, 4, 0, 0, 0, 0], typed_null); + for slot0 in [none, typed_null] { + let texture = texture_at_address(title_memory(1 << 20), &texture_words(16, 16, 28, 0x105, 1)); + crate::descriptor::create_rtv(std::ptr::null(), texture, std::ptr::null(), good); + let list = host.new_list().unwrap(); + let mut state = State::default(); + const RENDER_TARGET: u32 = 0x4; + if let Some((copy, before)) = list.transition(texture, RENDER_TARGET) { + // SAFETY: an open list; the title texture's live host copy. + unsafe { + list.list.ResourceBarrier(&[crate::host::transition(copy, D3D12_RESOURCE_STATES(before as i32), D3D12_RESOURCE_STATES(RENDER_TARGET as i32))]); + } + } + state.set_targets(&list, &[slot0, good], 0); + // SAFETY: an open list and live objects. + unsafe { + list.list.RSSetViewports(&[D3D12_VIEWPORT { Width: 16.0, Height: 16.0, MaxDepth: 1.0, ..Default::default() }]); + list.list.RSSetScissorRects(&[RECT { left: 0, top: 0, right: 16, bottom: 16 }]); + list.list.SetGraphicsRootSignature(&root); + list.list.SetPipelineState(&pso); + list.list.IASetPrimitiveTopology(windows::Win32::Graphics::Direct3D::D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + list.list.DrawInstanced(3, 1, 0, 0); + } + let slots = Slots::new(1); + let read = compile_cs(READ); + let (out, data) = host.shared_buffer(64 * 64 * 16).unwrap(); + let view = View::Shader(texture as usize, None); + readable(&list, &used(ResourceType::Srv), &view, false); + slots.make(0, &view); + run(&list, &slots, &read, 0, Some(&out), (2, 2)); + list.close(); + host.execute(&[&list]); + for (x, y) in [(0usize, 0usize), (15, 15), (7, 9)] { + // SAFETY: inside the mapped output. + let got = unsafe { (data.add((y * 64 + x) * 16) as *const [f32; 4]).read() }; + let case = if slot0 == none { "no view" } else { "typed null view" }; + assert_eq!(got, [0.0, 1.0, 0.0, 1.0], "{case} in slot 0, texel {x},{y}: SV_Target1, not SV_Target0"); + } + } + drop(records); + } + /// One texture memory written through an unordered view of a resource /// object in one format and read through a placed view in another: a /// castable pair shares one host texture (no copy), a pair of other diff --git a/crates/victory-gpu/src/host.rs b/crates/victory-gpu/src/host.rs index 18ad5dd4b1..0d24db3473 100644 --- a/crates/victory-gpu/src/host.rs +++ b/crates/victory-gpu/src/host.rs @@ -1227,13 +1227,18 @@ impl Host { handle } - /// A null render target view, for a slot the title binds a texture the - /// host cannot render to (its copy was made without that use): draws - /// write nothing there, and the other targets keep their slots. - pub(crate) fn null_rtv(&self) -> D3D12_CPU_DESCRIPTOR_HANDLE { + /// A null render target view, for a slot whose texture the host cannot + /// render to (no view recorded, no host copy, or a copy made without + /// that use): draws write nothing there, and the other targets keep + /// their slots. In `format` (the title's view format) where the host + /// renders to it, so it matches the pipeline's target formats; else in + /// `R8G8B8A8_UNORM`. + pub(crate) fn null_rtv(&self, format: Option) -> D3D12_CPU_DESCRIPTOR_HANDLE { let slot = self.next_rtv.fetch_add(1, Ordering::Relaxed) % RTV_RING; let desc = D3D12_RENDER_TARGET_VIEW_DESC { - Format: DXGI_FORMAT_R8G8B8A8_UNORM, + Format: format + .filter(|&f| renders_format(f)) + .unwrap_or(DXGI_FORMAT_R8G8B8A8_UNORM), ViewDimension: D3D12_RTV_DIMENSION_TEXTURE2D, ..Default::default() }; @@ -2640,6 +2645,35 @@ pub(crate) fn legal_state(texture: &ID3D12Resource, state: u32) -> u32 { legal_for(unsafe { texture.GetDesc() }.Flags.0 as u32, state) } +/// Whether the host renders to `format` (`D3D12_FORMAT_SUPPORT1_RENDER_TARGET`), +/// asked once per format. +pub(crate) fn renders_format(format: DXGI_FORMAT) -> bool { + static KNOWN: Mutex> = Mutex::new(Vec::new()); + let mut known = KNOWN.lock().unwrap_or_else(|e| e.into_inner()); + if let Some(&(_, ok)) = known.iter().find(|(f, _)| *f == format.0) { + return ok; + } + let mut support = D3D12_FEATURE_DATA_FORMAT_SUPPORT { + Format: format, + ..Default::default() + }; + let ok = format != DXGI_FORMAT_UNKNOWN + && host().is_some_and(|h| { + // SAFETY: a plain query into a struct of its size. + unsafe { + h.device.CheckFeatureSupport( + D3D12_FEATURE_FORMAT_SUPPORT, + (&raw mut support).cast(), + size_of_val(&support) as u32, + ) + } + .is_ok() + && support.Support1.0 & D3D12_FORMAT_SUPPORT1_RENDER_TARGET.0 != 0 + }); + known.push((format.0, ok)); + ok +} + /// Whether the host copy of a texture can be a render target: not when the /// host refused that use for its format and the copy was made without it. pub(crate) fn renders_to(texture: &ID3D12Resource) -> bool {