diff --git a/crates/victory-gpu/src/compose.rs b/crates/victory-gpu/src/compose.rs new file mode 100644 index 0000000..9aa45cb --- /dev/null +++ b/crates/victory-gpu/src/compose.rs @@ -0,0 +1,244 @@ +//! Ported from fxceless/victory-gta6 PR #1189 (Series S interface plane), `VICTORY_COMPOSE=1`. +//! A present's second plane laid over its first. +//! +//! A Series S title presents two planes (run 2026-11-25: plane 0 the +//! game, 1920x1080 or what the display mode makes it, plane 1 the +//! interface, 3840x2160); the console's display lays the second over the +//! first and scales both to the screen. The host shows one texture, so +//! the interface is laid over the game here, at the game plane's size, +//! into a texture of ours that is shown instead (filtered: the interface +//! is scaled down). shortcut: the planes' placement and source rectangles +//! are not read; both cover the screen. The interface's alpha is taken as +//! straight (`VICTORY_PLANE_PREMULTIPLIED=1`: premultiplied). + +use std::sync::Mutex; + +use windows::Win32::Graphics::Direct3D::Fxc::D3DCompile; +use windows::Win32::Graphics::Direct3D::ID3DBlob; +use windows::Win32::Graphics::Direct3D12::*; +use windows::Win32::Graphics::Dxgi::Common::*; + +/// Frames of descriptors in the ring (three each): presents in flight +/// must not find theirs rewritten. +const SLOTS: u32 = 16; + +const SOURCE: &str = r#" +#define RS "RootConstants(num32BitConstants=3, b0), DescriptorTable(SRV(t0, numDescriptors=2), UAV(u0)), StaticSampler(s0, filter=FILTER_MIN_MAG_MIP_LINEAR, addressU=TEXTURE_ADDRESS_CLAMP, addressV=TEXTURE_ADDRESS_CLAMP)" +cbuffer Size : register(b0) { uint w; uint h; uint premultiplied; }; +Texture2D game : register(t0); +Texture2D ui : register(t1); +RWTexture2D outp : register(u0); +SamplerState linear_clamp : register(s0); +[RootSignature(RS)] +[numthreads(8, 8, 1)] +void main(uint3 id : SV_DispatchThreadID) { + if (id.x >= w || id.y >= h) return; + float2 uv = (float2(id.xy) + 0.5) / float2(w, h); + float4 g = game.Load(int3(id.xy, 0)); + float4 u = ui.SampleLevel(linear_clamp, uv, 0); + float3 c = premultiplied != 0 ? g.rgb * (1.0 - u.a) + u.rgb : lerp(g.rgb, u.rgb, u.a); + outp[id.xy] = float4(c, 1.0); +} +"#; + +struct Pass { + root: ID3D12RootSignature, + pso: ID3D12PipelineState, + views: ID3D12DescriptorHeap, + cpu: usize, + gpu: u64, + step: u32, + next: u32, + /// The texture shown instead, by size. + out: Option<(u64, u32, ID3D12Resource)>, +} +// SAFETY: D3D12 objects are free-threaded; the pass is used under a lock. +unsafe impl Send for Pass {} + +/// The pass: not tried yet, could not be made, or made. +static PASS: Mutex>> = Mutex::new(None); + +fn make(device: &ID3D12Device) -> windows::core::Result { + let mut code: Option = None; + // SAFETY: the source and names live through the call; the out pointer + // is ours. + unsafe { + D3DCompile( + SOURCE.as_ptr().cast(), + SOURCE.len(), + windows::core::s!("present planes"), + None, + None, + windows::core::s!("main"), + windows::core::s!("cs_5_1"), + 0, + 0, + &mut code, + None, + )?; + } + let code = code.ok_or_else(windows::core::Error::empty)?; + // SAFETY: the blob's own bytes (the root signature is inside the + // shader); plain object creation and queries. + unsafe { + let bytes = std::slice::from_raw_parts(code.GetBufferPointer().cast::(), code.GetBufferSize()); + let root: ID3D12RootSignature = device.CreateRootSignature(0, bytes)?; + let pso: ID3D12PipelineState = device.CreateComputePipelineState(&D3D12_COMPUTE_PIPELINE_STATE_DESC { + pRootSignature: std::mem::transmute_copy(&root), + CS: D3D12_SHADER_BYTECODE { + pShaderBytecode: bytes.as_ptr().cast(), + BytecodeLength: bytes.len(), + }, + ..Default::default() + })?; + let kind = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV; + let views: ID3D12DescriptorHeap = device.CreateDescriptorHeap(&D3D12_DESCRIPTOR_HEAP_DESC { + Type: kind, + NumDescriptors: SLOTS * 3, + Flags: D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE, + NodeMask: 0, + })?; + Ok(Pass { + cpu: views.GetCPUDescriptorHandleForHeapStart().ptr, + gpu: views.GetGPUDescriptorHandleForHeapStart().ptr, + step: device.GetDescriptorHandleIncrementSize(kind), + root, + pso, + views, + next: 0, + out: None, + }) + } +} + +/// Whether a host texture's format is one this pass reads and writes +/// (8-bit RGBA, typeless or not). +fn rgba8(format: DXGI_FORMAT) -> bool { + matches!(format.0, 27 | 28) +} + +fn premultiplied() -> bool { + static ON: std::sync::OnceLock = std::sync::OnceLock::new(); + *ON.get_or_init(|| std::env::var_os("VICTORY_PLANE_PREMULTIPLIED").is_some_and(|v| v == "1")) +} + +/// Records, into the present's `list`, `ui` laid over `game` into a +/// texture of ours, and gives that texture, left in the copy-source +/// state. `game` is in the copy-source state (the present put it there); +/// `ui` is in `ui_state` and is left in it. `None` (nothing recorded) if +/// a format is not 8-bit RGBA or the pass could not be made. +pub(crate) fn record( + device: &ID3D12Device, + list: &ID3D12GraphicsCommandList, + game: &ID3D12Resource, + ui: &ID3D12Resource, + ui_state: u32, +) -> Option { + // SAFETY: live resources; plain queries. + let (gd, ud) = unsafe { (game.GetDesc(), ui.GetDesc()) }; + if !rgba8(gd.Format) || !rgba8(ud.Format) { + static ONCE: std::sync::Once = std::sync::Once::new(); + ONCE.call_once(|| { + tracing::warn!(game = gd.Format.0, ui = ud.Format.0, "present planes not laid over each other: a format this pass does not read"); + }); + return None; + } + let mut kept = PASS.lock().unwrap_or_else(|e| e.into_inner()); + if kept.is_none() { + *kept = Some(match make(device) { + Ok(p) => { + tracing::info!(premultiplied = premultiplied(), "present planes: the interface is laid over the game"); + Some(p) + } + Err(e) => { + tracing::warn!(error = %e, "present planes: the pass that lays them over each other could not be made"); + None + } + }); + } + let pass = kept.as_mut()?.as_mut()?; + let (w, h) = (gd.Width, gd.Height); + if pass.out.as_ref().is_none_or(|o| (o.0, o.1) != (w, h)) { + let heap = D3D12_HEAP_PROPERTIES { + Type: D3D12_HEAP_TYPE_DEFAULT, + ..Default::default() + }; + let desc = D3D12_RESOURCE_DESC { + Dimension: D3D12_RESOURCE_DIMENSION_TEXTURE2D, + Width: w, + Height: h, + DepthOrArraySize: 1, + MipLevels: 1, + Format: DXGI_FORMAT_R8G8B8A8_UNORM, + SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 }, + Layout: D3D12_TEXTURE_LAYOUT_UNKNOWN, + Flags: D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, + ..Default::default() + }; + let mut out: Option = None; + // SAFETY: plain creation of a device-local texture. + unsafe { + device + .CreateCommittedResource(&heap, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_COPY_SOURCE, None, &mut out) + .ok()?; + } + pass.out = Some((w, h, out?)); + } + let out = pass.out.as_ref()?.2.clone(); + let first = (pass.next % SLOTS) * 3; + pass.next = pass.next.wrapping_add(1); + let cpu = |i: u32| D3D12_CPU_DESCRIPTOR_HANDLE { ptr: pass.cpu + ((first + i) * pass.step) as usize }; + let gpu = D3D12_GPU_DESCRIPTOR_HANDLE { ptr: pass.gpu + u64::from(first * pass.step) }; + let srv = D3D12_SHADER_RESOURCE_VIEW_DESC { + Format: DXGI_FORMAT_R8G8B8A8_UNORM, + ViewDimension: D3D12_SRV_DIMENSION_TEXTURE2D, + Shader4ComponentMapping: D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING, + Anonymous: D3D12_SHADER_RESOURCE_VIEW_DESC_0 { + Texture2D: D3D12_TEX2D_SRV { + MostDetailedMip: 0, + MipLevels: 1, + ..Default::default() + }, + }, + }; + let uav = D3D12_UNORDERED_ACCESS_VIEW_DESC { + Format: DXGI_FORMAT_R8G8B8A8_UNORM, + ViewDimension: D3D12_UAV_DIMENSION_TEXTURE2D, + Anonymous: D3D12_UNORDERED_ACCESS_VIEW_DESC_0 { + Texture2D: D3D12_TEX2D_UAV { MipSlice: 0, PlaneSlice: 0 }, + }, + }; + let read = D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE; + let ui_state = D3D12_RESOURCE_STATES(ui_state as i32); + let t = crate::host::transition; + let mut before = vec![ + t(game, D3D12_RESOURCE_STATE_COPY_SOURCE, read), + t(&out, D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12_RESOURCE_STATE_UNORDERED_ACCESS), + ]; + let mut after = vec![ + t(game, read, D3D12_RESOURCE_STATE_COPY_SOURCE), + t(&out, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_SOURCE), + ]; + if ui_state != read { + before.push(t(ui, ui_state, read)); + after.push(t(ui, read, ui_state)); + } + let constants: [u32; 3] = [w as u32, h, u32::from(premultiplied())]; + // SAFETY: an open list (the present's); slots of our heap; live + // resources and objects (the pass's own live as long as the process, + // the texture is kept by the present's list). + unsafe { + device.CreateShaderResourceView(game, Some(&srv), cpu(0)); + device.CreateShaderResourceView(ui, Some(&srv), cpu(1)); + device.CreateUnorderedAccessView(&out, None::<&ID3D12Resource>, Some(&uav), cpu(2)); + list.ResourceBarrier(&before); + list.SetDescriptorHeaps(&[Some(pass.views.clone())]); + list.SetComputeRootSignature(&pass.root); + list.SetPipelineState(&pass.pso); + list.SetComputeRoot32BitConstants(0, 3, constants.as_ptr().cast(), 0); + list.SetComputeRootDescriptorTable(1, gpu); + list.Dispatch((w as u32).div_ceil(8), h.div_ceil(8), 1); + list.ResourceBarrier(&after); + } + Some(out) +} diff --git a/crates/victory-gpu/src/host.rs b/crates/victory-gpu/src/host.rs index 1ae56ee..763cefb 100644 --- a/crates/victory-gpu/src/host.rs +++ b/crates/victory-gpu/src/host.rs @@ -8804,6 +8804,19 @@ impl Host { /// as frame `frame` (saved if captures are on). `None` if nothing was /// shown; else, in diagnostics builds, whether the frame was black. pub(crate) fn present(&self, texture: &ID3D12Resource, state: u32, frame: u64) -> Option { + self.present_over(texture, state, None, frame) + } + + /// LOCAL (port of fxceless #1189): [`Self::present`], with a second + /// plane laid over `texture` if there is one (`crate::compose`): its + /// texture and its state, which it is left in. + pub(crate) fn present_over( + &self, + texture: &ID3D12Resource, + state: u32, + over: Option<(&ID3D12Resource, u32)>, + frame: u64, + ) -> Option { // SAFETY: a live resource. let desc = unsafe { texture.GetDesc() }; let Some(format) = swap_format(desc.Format) else { @@ -8887,7 +8900,13 @@ impl Host { D3D12_RESOURCE_STATE_COPY_DEST, ), ]); - list.CopyResource(&back, texture); + // LOCAL (port of fxceless #1189): the second plane laid over the + // first, into a texture of ours shown instead (`crate::compose`). + let laid = over.and_then(|(ui, ui_state)| crate::compose::record(&self.device, list, texture, ui, ui_state)); + if let Some(laid) = &laid { + copy.keep(laid); + } + list.CopyResource(&back, laid.as_ref().unwrap_or(texture)); #[cfg(feature = "diagnostics")] let capture = crate::capture::wanted(frame) .then(|| crate::capture::read_back(&self.device, list, texture)) diff --git a/crates/victory-gpu/src/lib.rs b/crates/victory-gpu/src/lib.rs index 908dbec..9441fe2 100644 --- a/crates/victory-gpu/src/lib.rs +++ b/crates/victory-gpu/src/lib.rs @@ -20,6 +20,9 @@ mod ambient; #[cfg(all(windows, feature = "diagnostics"))] mod capture; pub mod console; +// LOCAL (port of fxceless #1189): Series S interface plane laid over the game (VICTORY_COMPOSE=1). +#[cfg(windows)] +mod compose; #[cfg(all(windows, feature = "diagnostics"))] mod copystat; #[cfg(all(windows, feature = "diagnostics"))] diff --git a/crates/victory-gpu/src/queue.rs b/crates/victory-gpu/src/queue.rs index ede3bb3..0dc2f38 100644 --- a/crates/victory-gpu/src/queue.rs +++ b/crates/victory-gpu/src/queue.rs @@ -924,6 +924,8 @@ extern "system" fn present( crate::capture::dump_targets(frame); } } + #[cfg(all(windows, feature = "diagnostics"))] + note_planes(planes, plane_parameters); #[cfg(windows)] if behind::on() && !this.is_null() { const COPY_SOURCE: u32 = 0x800; @@ -949,7 +951,10 @@ extern "system" fn present( None }; let resource = texture.as_ref().map_or(0, |t| t.0); - behind::present(this, texture.map(|(_, copy, state)| (copy, state)), Shown { frame, resource }); + // LOCAL (port of fxceless #1189): the interface plane, as the first. + let over = second_plane(planes, plane_parameters, resource as This) + .map(|(copy, state)| (copy, if crate::list::thin::wanted() { u32::MAX } else { state })); + behind::present(this, texture.map(|(_, copy, state)| (copy, state)), Shown { frame, resource, over }); called.end(); #[cfg(feature = "diagnostics")] frame_ends(); @@ -969,7 +974,8 @@ extern "system" fn present( const COPY_SOURCE: u32 = 0x800; let state = crate::resource::host_transition(resource, COPY_SOURCE) .map_or(COPY_SOURCE, |(_, before)| before); - show(texture, state, Shown { frame, resource: resource as usize }); + let over = second_plane(planes, plane_parameters, resource); + show(texture, state, Shown { frame, resource: resource as usize, over }); crate::resource::host_transition(resource, state); let _ = host; } @@ -995,10 +1001,91 @@ fn frame_ends() { /// The frame a present shows, and the title's resource it shows. #[cfg(windows)] -#[derive(Clone, Copy)] +#[derive(Clone)] struct Shown { frame: u64, resource: usize, + /// LOCAL (port of fxceless #1189): the second plane to lay over the + /// shown one, and its host state (`u32::MAX`: the state its lists left + /// it in when they ran). + over: Option<(std::sync::Arc, u32)>, +} + +/// LOCAL (port of fxceless #1189): a present's second plane, to be laid over +/// the first (`crate::compose`), with `VICTORY_COMPOSE=1`. Its host copy and +/// the host state the lists recorded so far leave it in (noted as before). +/// A Series S title presents two planes: the game and the interface +/// (menus, loading screens, the intro), 72-byte descriptions each. +#[cfg(windows)] +fn second_plane(planes: u32, plane_parameters: *const Plane, first: This) -> Option<(std::sync::Arc, u32)> { + const STRIDE: usize = 72; + const COPY_SOURCE: u32 = 0x800; + static ON: std::sync::OnceLock = std::sync::OnceLock::new(); + let on = *ON.get_or_init(|| { + let on = std::env::var_os("VICTORY_COMPOSE").is_some_and(|v| v == "1"); + if on { + tracing::warn!("LOCAL present planes: a second plane is laid over the first (VICTORY_COMPOSE=1)"); + } + on + }); + if !on || planes < 2 || plane_parameters.is_null() { + return None; + } + // SAFETY: the title passes `planes` descriptions of `STRIDE` bytes. + let plane = unsafe { plane_parameters.cast::().add(STRIDE).cast::().as_ref() }?; + if plane.count == 0 || plane.resources.is_null() { + return None; + } + // SAFETY: the plane lists `count` resources. + let resource = unsafe { *plane.resources }; + if resource == first { + return None; + } + let copy = crate::resource::host_copy(resource)?; + let state = crate::resource::host_transition(resource, COPY_SOURCE).map_or(COPY_SOURCE, |(_, before)| before); + crate::resource::host_transition(resource, state); + Some((copy, state)) +} + +/// Diagnostics (LOCAL, from fxceless #1189): the planes a present asks for, +/// at presents 1, 2, 4, ... up to 4096 and whenever their number changes. +#[cfg(all(windows, feature = "diagnostics"))] +fn note_planes(planes: u32, plane_parameters: *const Plane) { + const STRIDE: usize = 72; + static LAST: AtomicU64 = AtomicU64::new(u64::MAX); + let frame = FRAMES.load(Ordering::Relaxed); + let changed = LAST.swap(u64::from(planes), Ordering::Relaxed) != u64::from(planes); + if !changed && !(frame.is_power_of_two() && frame <= 4096) { + return; + } + if plane_parameters.is_null() { + return; + } + for i in 0..planes.min(4) as usize { + // SAFETY: the title passes `planes` descriptions of `STRIDE` bytes. + let Some(plane) = (unsafe { plane_parameters.cast::().add(i * STRIDE).cast::().as_ref() }) else { + continue; + }; + if plane.resources.is_null() { + tracing::warn!(frame, planes, plane = i, count = plane.count, "present plane without resources"); + continue; + } + for k in 0..plane.count.min(2) as usize { + // SAFETY: the plane lists `count` resources. + let resource = unsafe { *plane.resources.add(k) }; + let desc = crate::resource::texture_memory(resource).map(|m| m.1); + tracing::warn!( + frame, + planes, + plane = i, + count = plane.count, + resource = ?resource, + hosted = crate::resource::host_copy(resource).is_some(), + desc = ?desc, + "present plane" + ); + } + } } /// Shows `texture`, in host state `state`, in our window. @@ -1007,7 +1094,11 @@ fn show(texture: &windows::Win32::Graphics::Direct3D12::ID3D12Resource, state: u let Some(host) = crate::host::host() else { return; }; - let shown = host.present(texture, state, what.frame); + let over = what.over.as_ref().map(|(t, s)| { + let s = if *s == u32::MAX { t.state.load(Ordering::Relaxed) } else { *s }; + (&t.texture, s) + }); + let shown = host.present_over(texture, state, over, what.frame); #[cfg(feature = "diagnostics")] blinks(what.frame, what.resource as This, state, shown); #[cfg(not(feature = "diagnostics"))] diff --git a/crates/victory-gpu/src/shader_cache.rs b/crates/victory-gpu/src/shader_cache.rs index b291e11..f5e6482 100644 --- a/crates/victory-gpu/src/shader_cache.rs +++ b/crates/victory-gpu/src/shader_cache.rs @@ -63,6 +63,7 @@ fn build() -> [u8; 32] { // served the translations an earlier run of the same // executable had cached with the switch unset. for name in [ + "VICTORY_BVH_MISS", "VICTORY_WAVE_WIDE", "VICTORY_WAVE_DEEP_SKIPS", "VICTORY_WAVE_OWN_BITS",