From e8d2258c5af20f108770ad8e0e983e9ecba8581e Mon Sep 17 00:00:00 2001 From: Ramon Victor Cardoso Date: Wed, 25 Nov 2026 18:47:02 -0300 Subject: [PATCH 1/3] D3D12.X: preserve concrete sRGB plane formats in PresentX compositor SRVs --- crates/xcl/src/hle/d3d12x/present.rs | 150 ++++++++++++++++++++++++++- 1 file changed, 149 insertions(+), 1 deletion(-) diff --git a/crates/xcl/src/hle/d3d12x/present.rs b/crates/xcl/src/hle/d3d12x/present.rs index d47e67841e..ef778cd9dd 100644 --- a/crates/xcl/src/hle/d3d12x/present.rs +++ b/crates/xcl/src/hle/d3d12x/present.rs @@ -276,6 +276,19 @@ pub fn swap_format(f: DXGI_FORMAT) -> Option { }) } +/// Shader-resource view format for a composited `PresentX` plane: resolves typeless +/// formats to a concrete view format while preserving concrete formats (including +/// `*_UNORM_SRGB`, which D3D12 forbids viewing as `*_UNORM`). +fn plane_srv_format(f: DXGI_FORMAT) -> DXGI_FORMAT { + match f { + DXGI_FORMAT_R8G8B8A8_TYPELESS => DXGI_FORMAT_R8G8B8A8_UNORM, + DXGI_FORMAT_B8G8R8A8_TYPELESS => DXGI_FORMAT_B8G8R8A8_UNORM, + DXGI_FORMAT_R10G10B10A2_TYPELESS => DXGI_FORMAT_R10G10B10A2_UNORM, + DXGI_FORMAT_R16G16B16A16_TYPELESS => DXGI_FORMAT_R16G16B16A16_FLOAT, + _ => f, + } +} + unsafe fn backbuffer_like(res: usize) -> Option { let raw = res as *mut c_void; let r = ID3D12Resource::from_raw_borrowed(&raw)?; @@ -851,7 +864,7 @@ impl Compositor { let mut srv_cpu = srv_cpu_base; srv_cpu.ptr += (first + i) * self.srv_inc as usize; let mut srv_desc = D3D12_SHADER_RESOURCE_VIEW_DESC { - Format: swap_format(desc.Format).unwrap_or(desc.Format), + Format: plane_srv_format(desc.Format), ViewDimension: D3D12_SRV_DIMENSION_TEXTURE2D, Shader4ComponentMapping: D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING, ..Default::default() @@ -1826,4 +1839,139 @@ mod frame_dump_tests { rb.Unmap(0, None); } } + + #[test] + fn compositor_preserves_concrete_srgb_plane_srv_format() { + assert_eq!(plane_srv_format(DXGI_FORMAT_R8G8B8A8_UNORM_SRGB), DXGI_FORMAT_R8G8B8A8_UNORM_SRGB); + assert_eq!(plane_srv_format(DXGI_FORMAT_B8G8R8A8_UNORM_SRGB), DXGI_FORMAT_B8G8R8A8_UNORM_SRGB); + assert_eq!(plane_srv_format(DXGI_FORMAT_R8G8B8A8_TYPELESS), DXGI_FORMAT_R8G8B8A8_UNORM); + assert_eq!(plane_srv_format(DXGI_FORMAT_B8G8R8A8_TYPELESS), DXGI_FORMAT_B8G8R8A8_UNORM); + assert_eq!(plane_srv_format(DXGI_FORMAT_R10G10B10A2_TYPELESS), DXGI_FORMAT_R10G10B10A2_UNORM); + assert_eq!(plane_srv_format(DXGI_FORMAT_R16G16B16A16_TYPELESS), DXGI_FORMAT_R16G16B16A16_FLOAT); + assert_eq!(plane_srv_format(DXGI_FORMAT_R8G8B8A8_UNORM), DXGI_FORMAT_R8G8B8A8_UNORM); + assert_eq!(plane_srv_format(DXGI_FORMAT_R16G16B16A16_FLOAT), DXGI_FORMAT_R16G16B16A16_FLOAT); + unsafe { + let factory: IDXGIFactory4 = CreateDXGIFactory2(DXGI_CREATE_FACTORY_FLAGS(0)).unwrap(); + let adapter: IDXGIAdapter = factory.EnumWarpAdapter().unwrap(); + let mut dev: Option = None; + D3D12CreateDevice(&adapter, D3D_FEATURE_LEVEL_11_0, &mut dev).unwrap(); + let dev = dev.unwrap(); + let queue: ID3D12CommandQueue = dev + .CreateCommandQueue(&D3D12_COMMAND_QUEUE_DESC { Type: D3D12_COMMAND_LIST_TYPE_DIRECT, ..Default::default() }) + .unwrap(); + let alloc: ID3D12CommandAllocator = dev.CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT).unwrap(); + let list: ID3D12GraphicsCommandList = dev.CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, &alloc, None).unwrap(); + let fence: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).unwrap(); + let mut comp = Compositor::new(&dev).unwrap(); + + let make_tex = |fmt: DXGI_FORMAT, flags: D3D12_RESOURCE_FLAGS| -> ID3D12Resource { + let mut r: Option = None; + dev.CreateCommittedResource( + &D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE_DEFAULT, ..Default::default() }, + D3D12_HEAP_FLAG_NONE, + &D3D12_RESOURCE_DESC { + Dimension: D3D12_RESOURCE_DIMENSION_TEXTURE2D, + Width: 4, + Height: 4, + DepthOrArraySize: 1, + MipLevels: 1, + Format: fmt, + SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 }, + Flags: flags, + ..Default::default() + }, + D3D12_RESOURCE_STATE_COMMON, + None, + &mut r, + ) + .unwrap(); + r.unwrap() + }; + + let srgb = make_tex(DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, D3D12_RESOURCE_FLAG_NONE); + let out = make_tex(DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET); + let mut up: Option = None; + dev.CreateCommittedResource( + &D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE_UPLOAD, ..Default::default() }, + D3D12_HEAP_FLAG_NONE, + &D3D12_RESOURCE_DESC { + Dimension: D3D12_RESOURCE_DIMENSION_BUFFER, + Width: 256 * 4, + Height: 1, + DepthOrArraySize: 1, + MipLevels: 1, + SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 }, + Layout: D3D12_TEXTURE_LAYOUT_ROW_MAJOR, + ..Default::default() + }, + D3D12_RESOURCE_STATE_GENERIC_READ, + None, + &mut up, + ) + .unwrap(); + let up = up.unwrap(); + let mut p: *mut c_void = std::ptr::null_mut(); + up.Map(0, None, Some(&mut p)).unwrap(); + let bytes = std::slice::from_raw_parts_mut(p as *mut u8, 256 * 4); + for y in 0..4usize { + for x in 0..4usize { + let off = y * 256 + x * 4; + bytes[off..off + 4].copy_from_slice(&[188, 128, 0, 255]); + } + } + up.Unmap(0, None); + let dst_loc = D3D12_TEXTURE_COPY_LOCATION { + pResource: std::mem::transmute_copy(&srgb), + Type: D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, + Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 { SubresourceIndex: 0 }, + }; + let src_loc = D3D12_TEXTURE_COPY_LOCATION { + pResource: std::mem::transmute_copy(&up), + Type: D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT, + Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 { + PlacedFootprint: D3D12_PLACED_SUBRESOURCE_FOOTPRINT { + Offset: 0, + Footprint: D3D12_SUBRESOURCE_FOOTPRINT { + Format: DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, + Width: 4, + Height: 4, + Depth: 1, + RowPitch: 256, + }, + }, + }, + }; + list.ResourceBarrier(&[transition(&srgb, D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_COPY_DEST)]); + list.CopyTextureRegion(&dst_loc, 0, 0, 0, &src_loc, None); + list.ResourceBarrier(&[ + transition(&srgb, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_COMMON), + transition(&out, D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_RENDER_TARGET), + ]); + + let planes = [(srgb.clone(), srgb.GetDesc(), [0.0, 0.0, 1.0, 1.0])]; + assert!(comp.record(&dev, &list, &out, 4, 4, DXGI_FORMAT_R8G8B8A8_UNORM, &planes, 0)); + list.ResourceBarrier(&[transition(&out, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_COPY_SOURCE)]); + let out_desc = out.GetDesc(); + let (rb, fp) = readback_copy(&list, &out, &out_desc).unwrap(); + list.Close().unwrap(); + + let cl: ID3D12CommandList = list.cast().unwrap(); + queue.ExecuteCommandLists(&[Some(cl)]); + queue.Signal(&fence, 1).unwrap(); + fence::wait(&fence, 1, &dev, None, 5000).unwrap(); + + let mut rp: *mut c_void = std::ptr::null_mut(); + rb.Map(0, None, Some(&mut rp)).unwrap(); + let off = fp.Offset as usize + fp.Footprint.RowPitch as usize + 4; + let px = std::slice::from_raw_parts((rp as *const u8).add(off), 4); + let got = [px[0], px[1], px[2], px[3]]; + rb.Unmap(0, None); + // Concrete R8G8B8A8_UNORM_SRGB sampled with an sRGB SRV decodes sRGB -> linear + // (~[128, 55, 0, 255]), whereas forcing UNORM fails SRV creation or skips sRGB decode. + assert!( + (125..=131).contains(&got[0]) && (52..=58).contains(&got[1]) && got[2] == 0 && got[3] == 255, + "expected sRGB-decoded concrete plane sample ~[128, 55, 0, 255], got {got:?}" + ); + } + } } From 0d380f427797fb91a07512d074a5f6f4e1892674 Mon Sep 17 00:00:00 2001 From: CryingLaughing <62956736+CryingLaughing@users.noreply.github.com> Date: Thu, 26 Nov 2026 17:48:56 +0800 Subject: [PATCH 2/3] D3D12.X: encode sRGB compositor output for presentation --- crates/xcl/src/hle/d3d12x/present.rs | 185 ++++++++++++--------------- 1 file changed, 81 insertions(+), 104 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/present.rs b/crates/xcl/src/hle/d3d12x/present.rs index ef778cd9dd..344797f87a 100644 --- a/crates/xcl/src/hle/d3d12x/present.rs +++ b/crates/xcl/src/hle/d3d12x/present.rs @@ -851,10 +851,23 @@ impl Compositor { slot: usize, ) -> bool { let first = slot % PRESENT_SLOTS * MAX_PLANES; - let Some((opaque_pso, blend_pso)) = self.psos_for(dev, dst_fmt) else { return false }; + // Flip-model storage is UNORM. An sRGB view blends linear samples and encodes the + // stored RGB when every drawn plane explicitly carries the sRGB transfer function. + let rtv_format = if planes.len() > 1 && planes.iter().take(MAX_PLANES).all(|(_, desc, _)| { + matches!(desc.Format, DXGI_FORMAT_R8G8B8A8_UNORM_SRGB | DXGI_FORMAT_B8G8R8A8_UNORM_SRGB) + }) { + match dst_fmt { + DXGI_FORMAT_R8G8B8A8_UNORM => DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, + DXGI_FORMAT_B8G8R8A8_UNORM => DXGI_FORMAT_B8G8R8A8_UNORM_SRGB, + _ => dst_fmt, + } + } else { + dst_fmt + }; + let Some((opaque_pso, blend_pso)) = self.psos_for(dev, rtv_format) else { return false }; let rtv_cpu = self.rtv_heap.GetCPUDescriptorHandleForHeapStart(); let rtv_desc = D3D12_RENDER_TARGET_VIEW_DESC { - Format: dst_fmt, + Format: rtv_format, ViewDimension: D3D12_RTV_DIMENSION_TEXTURE2D, ..Default::default() }; @@ -1708,12 +1721,9 @@ mod frame_dump_tests { let queue: ID3D12CommandQueue = dev .CreateCommandQueue(&D3D12_COMMAND_QUEUE_DESC { Type: D3D12_COMMAND_LIST_TYPE_DIRECT, ..Default::default() }) .unwrap(); - let alloc: ID3D12CommandAllocator = dev.CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT).unwrap(); - let list: ID3D12GraphicsCommandList = dev.CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, &alloc, None).unwrap(); - let fence: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).unwrap(); let mut comp = Compositor::new(&dev).unwrap(); - let make_tex = |w: u32, h: u32, flags: D3D12_RESOURCE_FLAGS| -> ID3D12Resource { + let make_tex = |w: u32, h: u32, format: DXGI_FORMAT, flags: D3D12_RESOURCE_FLAGS| -> ID3D12Resource { let mut r: Option = None; dev.CreateCommittedResource( &D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE_DEFAULT, ..Default::default() }, @@ -1724,7 +1734,7 @@ mod frame_dump_tests { Height: h, DepthOrArraySize: 1, MipLevels: 1, - Format: DXGI_FORMAT_R8G8B8A8_UNORM, + Format: format, SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 }, Flags: flags, ..Default::default() @@ -1736,107 +1746,74 @@ mod frame_dump_tests { .unwrap(); r.unwrap() }; - let upload_tex = |tex: &ID3D12Resource, w: u32, h: u32, fill: &dyn Fn(u32, u32) -> [u8; 4]| -> ID3D12Resource { - let pitch = (w * 4).div_ceil(256) * 256; - let mut up: Option = None; - dev.CreateCommittedResource( - &D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE_UPLOAD, ..Default::default() }, - D3D12_HEAP_FLAG_NONE, - &D3D12_RESOURCE_DESC { - Dimension: D3D12_RESOURCE_DIMENSION_BUFFER, - Width: (pitch * h) as u64, - Height: 1, - DepthOrArraySize: 1, - MipLevels: 1, - SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 }, - Layout: D3D12_TEXTURE_LAYOUT_ROW_MAJOR, - ..Default::default() - }, - D3D12_RESOURCE_STATE_GENERIC_READ, - None, - &mut up, - ) - .unwrap(); - let up = up.unwrap(); - let mut p: *mut c_void = std::ptr::null_mut(); - up.Map(0, None, Some(&mut p)).unwrap(); - let bytes = std::slice::from_raw_parts_mut(p as *mut u8, (pitch * h) as usize); - for y in 0..h { - for x in 0..w { - let off = (y * pitch + x * 4) as usize; - bytes[off..off + 4].copy_from_slice(&fill(x, y)); + let cases = [ + (DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_FORMAT_R8G8B8A8_UNORM, [0, 0, 255, 255], [0, 128, 0, 128], [0, 128, 127, 255]), + // Offscreen typeless storage supports either view of the same format family. + // The fractional overlay encodes a premultiplied linear green of about 0.25. + (DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, DXGI_FORMAT_R8G8B8A8_TYPELESS, [188, 128, 0, 255], [0, 137, 0, 128], [137, 161, 0, 255]), + ]; + for (format, target_format, scene_color, blend_color, expected_blend) in cases { + let alloc: ID3D12CommandAllocator = dev.CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT).unwrap(); + let list: ID3D12GraphicsCommandList = dev.CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, &alloc, None).unwrap(); + let fence: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).unwrap(); + let upload_tex = |tex: &ID3D12Resource, fill: &dyn Fn(u32, u32) -> [u8; 4]| { + let desc = tex.GetDesc(); + let mut data = Vec::with_capacity(desc.Width as usize * desc.Height as usize * 4); + for y in 0..desc.Height { + for x in 0..desc.Width as u32 { + data.extend_from_slice(&fill(x, y)); + } } - } - up.Unmap(0, None); - let dst_loc = D3D12_TEXTURE_COPY_LOCATION { - pResource: std::mem::transmute_copy(tex), - Type: D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, - Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 { SubresourceIndex: 0 }, - }; - let src_loc = D3D12_TEXTURE_COPY_LOCATION { - pResource: std::mem::transmute_copy(&up), - Type: D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT, - Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 { - PlacedFootprint: D3D12_PLACED_SUBRESOURCE_FOOTPRINT { - Offset: 0, - Footprint: D3D12_SUBRESOURCE_FOOTPRINT { - Format: DXGI_FORMAT_R8G8B8A8_UNORM, - Width: w, - Height: h, - Depth: 1, - RowPitch: pitch, - }, - }, - }, + let sub = crate::gpu::texture::Subresource { row_bytes: desc.Width as usize * 4, rows: desc.Height as usize, depth: 1, data }; + crate::gpu::texture::record_upload(&dev, &list, tex, &[sub], D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_COMMON).unwrap() }; - list.ResourceBarrier(&[transition(tex, D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_COPY_DEST)]); - list.CopyTextureRegion(&dst_loc, 0, 0, 0, &src_loc, None); - list.ResourceBarrier(&[transition(tex, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_COMMON)]); - up - }; - - let scene = make_tex(4, 4, D3D12_RESOURCE_FLAG_NONE); - let ui = make_tex(8, 8, D3D12_RESOURCE_FLAG_NONE); - let out = make_tex(8, 8, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET); - let _up0 = upload_tex(&scene, 4, 4, &|_, _| [0, 0, 255, 255]); - let _up1 = upload_tex(&ui, 8, 8, &|x, y| { - if y == 2 && x < 4 { - [0, 128, 0, 128] - } else if x < 4 { - [0, 0, 0, 0] - } else { - [255, 0, 0, 255] - } - }); - - list.ResourceBarrier(&[transition(&out, D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_RENDER_TARGET)]); - let planes = [ - (scene.clone(), scene.GetDesc(), [0.0, 0.0, 1.0, 1.0]), - (ui.clone(), ui.GetDesc(), [0.0, 0.0, 1.0, 1.0]), - ]; - assert!(comp.record(&dev, &list, &out, 8, 8, DXGI_FORMAT_R8G8B8A8_UNORM, &planes, 0)); - list.ResourceBarrier(&[transition(&out, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_COPY_SOURCE)]); - let out_desc = out.GetDesc(); - let (rb, fp) = readback_copy(&list, &out, &out_desc).unwrap(); - list.Close().unwrap(); - let cl: ID3D12CommandList = list.cast().unwrap(); - queue.ExecuteCommandLists(&[Some(cl)]); - queue.Signal(&fence, 1).unwrap(); - fence::wait(&fence, 1, &dev, None, 5000).unwrap(); + let scene = make_tex(4, 4, format, D3D12_RESOURCE_FLAG_NONE); + let ui = make_tex(8, 8, format, D3D12_RESOURCE_FLAG_NONE); + let out = make_tex(8, 8, target_format, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET); + let _up0 = upload_tex(&scene, &|_, _| scene_color); + let _up1 = upload_tex(&ui, &|x, y| { + if y == 2 && x < 4 { + blend_color + } else if x < 4 { + [0, 0, 0, 0] + } else { + [255, 0, 0, 255] + } + }); + + list.ResourceBarrier(&[transition(&out, D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_RENDER_TARGET)]); + let planes = [ + (scene.clone(), scene.GetDesc(), [0.0, 0.0, 1.0, 1.0]), + (ui.clone(), ui.GetDesc(), [0.0, 0.0, 1.0, 1.0]), + ]; + assert!(comp.record(&dev, &list, &out, 8, 8, DXGI_FORMAT_R8G8B8A8_UNORM, &planes, 0)); + list.ResourceBarrier(&[transition(&out, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_COPY_SOURCE)]); + let out_desc = D3D12_RESOURCE_DESC { Format: DXGI_FORMAT_R8G8B8A8_UNORM, ..out.GetDesc() }; + let (rb, fp) = readback_copy(&list, &out, &out_desc).unwrap(); + list.Close().unwrap(); + + let cl: ID3D12CommandList = list.cast().unwrap(); + queue.ExecuteCommandLists(&[Some(cl)]); + queue.Signal(&fence, 1).unwrap(); + fence::wait(&fence, 1, &dev, None, 5000).unwrap(); - let mut p: *mut c_void = std::ptr::null_mut(); - rb.Map(0, None, Some(&mut p)).unwrap(); - let px = |x: usize, y: usize| -> [u8; 4] { - let off = fp.Offset as usize + y * fp.Footprint.RowPitch as usize + x * 4; - let s = std::slice::from_raw_parts((p as *const u8).add(off), 4); - [s[0], s[1], s[2], s[3]] - }; - assert_eq!(px(1, 1), [0, 0, 255, 255], "transparent UI exposes scene plane"); - assert_eq!(px(6, 1), [255, 0, 0, 255], "opaque UI overwrites scene plane"); - let blend = px(1, 2); - assert!(blend[0] <= 2 && (126..=130).contains(&blend[1]) && (125..=129).contains(&blend[2]) && blend[3] == 255, "unexpected blend {blend:?}"); - rb.Unmap(0, None); + let mut p: *mut c_void = std::ptr::null_mut(); + rb.Map(0, None, Some(&mut p)).unwrap(); + let px = |x: usize, y: usize| -> [u8; 4] { + let off = fp.Offset as usize + y * fp.Footprint.RowPitch as usize + x * 4; + std::slice::from_raw_parts((p as *const u8).add(off), 4).try_into().unwrap() + }; + let (transparent, opaque, blend) = (px(1, 1), px(6, 1), px(1, 2)); + rb.Unmap(0, None); + + assert_eq!(transparent, scene_color, "transparent overlay in {format:?}"); + assert_eq!(opaque, [255, 0, 0, 255], "opaque overlay in {format:?}"); + assert!( + blend[..3].iter().zip(expected_blend).all(|(&actual, expected)| actual.abs_diff(expected) <= 2) && blend[3] == expected_blend[3], + "fractional overlay in {format:?}: expected {expected_blend:?}, got {blend:?}", + ); + } } } From 96b0b189fd0a8991c8e7883823191cf54d896223 Mon Sep 17 00:00:00 2001 From: CryingLaughing <62956736+CryingLaughing@users.noreply.github.com> Date: Thu, 26 Nov 2026 19:16:05 +0800 Subject: [PATCH 3/3] D3D12.X: preserve the sRGB base with linear overlays Select the output transfer from the opaque primary plane. A transparent UNORM overlay must preserve encoded scene colors. Cover RGBA and BGRA mixed formats and keep UNORM output when only an overlay is sRGB. --- README.md | 2 +- crates/xcl/src/hle/d3d12x/present.rs | 50 ++++++++++++++++------------ 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 321cc03a9e..5b6c07a0a6 100644 --- a/README.md +++ b/README.md @@ -150,7 +150,7 @@ A gamepad is recommended ([Controls](#controls)). The game folder is never modif | **Shader recompiler** | ✅ | RDNA2 → HLSL → DXIL: VS, PS, CS, mesh (NGG), GS; atomics, LDS, scratch, images, samplers incl. static ones, wave ops, ray-tracing instructions; structured control flow; parallel DXC, disk cache | 26 698 / 26 698 programs of the retail pack; 0 DXC errors on random samples; per-instruction GPU tests | | **Pipelines** | ✅ | PSOs from console shaders: VS+PS, CS, MS+PS, VS+GS+PS; root signature with the shaders' static samplers; non-blocking creation – a pipeline whose shaders are not compiled yet gets a stand-in at once and is built in the background | GPU tests drawing the game's own VS / PS pairs, also built in the background | | **D3D12.X compute** | ✅ | console device slots, console descriptor heaps (T# / V# / S#), buffers at their console addresses, compute command lists recorded in the console format and replayed (tables, barriers, clears, copies) | the game's BVH builder and FSR 4 record and run through XCL | -| **D3D12.X graphics** | ✅ | device, resources and heaps (also placed in title memory), pipelines; the title's graphics command lists – draws, indirect draws, graphics root arguments, barriers, copies, clears – and command lists recorded by console code, replayed; `PresentX` and frame pacing into an XCL window | Minecraft Preview plays its worlds (millions of draws, none skipped); Fortnite and XDefiant draw their screens | +| **D3D12.X graphics** | ✅ | device, resources and heaps (also placed in title memory), pipelines; the title's graphics command lists – draws, indirect draws, graphics root arguments, barriers, copies, clears – and command lists recorded by console code, replayed; `PresentX` and frame pacing into an XCL window, with 8-bit SDR output encoding selected by the primary plane during multi-plane composition | Minecraft Preview plays its worlds (millions of draws, none skipped); Fortnite and XDefiant draw their screens | | **Textures** | ✅ | GFX10 tiling (all 14 2D swizzle modes, the 6 3D thick modes, linear), mip chains with mip tail, arrays, cubes and 3D textures → PC textures | 909 / 912 retail textures exact (the 3 volume LUTs decode smooth); GPU upload bit exact | | **Ray tracing** | ✅ | BLAS / TLAS built on the GPU by the game's own XboxBvhBuilder; RDNA2 BVH node format; BVH intersection instructions emulated in shaders | GPU traversal = brute force reference; the game's builder output checked on the GPU | | **Upscaling** | ✅ | the game's AMD FSR 4 console DLL (`amd_fidelityfx_upscaler_xbox.dll`): context, the 29 pipelines in the console driver's own container (FSPX) translated, a frame's 20 passes recorded and run on the GPU | end-to-end with the real DLL: a 1280×720 → 1920×1080 frame on an RX 9070, the input's gradient in the output | diff --git a/crates/xcl/src/hle/d3d12x/present.rs b/crates/xcl/src/hle/d3d12x/present.rs index 344797f87a..253f70b1ce 100644 --- a/crates/xcl/src/hle/d3d12x/present.rs +++ b/crates/xcl/src/hle/d3d12x/present.rs @@ -851,18 +851,15 @@ impl Compositor { slot: usize, ) -> bool { let first = slot % PRESENT_SLOTS * MAX_PLANES; - // Flip-model storage is UNORM. An sRGB view blends linear samples and encodes the - // stored RGB when every drawn plane explicitly carries the sRGB transfer function. - let rtv_format = if planes.len() > 1 && planes.iter().take(MAX_PLANES).all(|(_, desc, _)| { - matches!(desc.Format, DXGI_FORMAT_R8G8B8A8_UNORM_SRGB | DXGI_FORMAT_B8G8R8A8_UNORM_SRGB) - }) { - match dst_fmt { - DXGI_FORMAT_R8G8B8A8_UNORM => DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, - DXGI_FORMAT_B8G8R8A8_UNORM => DXGI_FORMAT_B8G8R8A8_UNORM_SRGB, + // The first plane supplies the swap format and is drawn as the opaque base. + // Retain its sRGB encoding even when overlays use a linear SRV. + let rtv_format = match planes { + [(_, base, _), _, ..] => match (base.Format, dst_fmt) { + (DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, DXGI_FORMAT_R8G8B8A8_UNORM) => base.Format, + (DXGI_FORMAT_B8G8R8A8_UNORM_SRGB, DXGI_FORMAT_B8G8R8A8_UNORM) => base.Format, _ => dst_fmt, - } - } else { - dst_fmt + }, + _ => dst_fmt, }; let Some((opaque_pso, blend_pso)) = self.psos_for(dev, rtv_format) else { return false }; let rtv_cpu = self.rtv_heap.GetCPUDescriptorHandleForHeapStart(); @@ -1747,12 +1744,21 @@ mod frame_dump_tests { r.unwrap() }; let cases = [ - (DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_FORMAT_R8G8B8A8_UNORM, [0, 0, 255, 255], [0, 128, 0, 128], [0, 128, 127, 255]), + (DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_FORMAT_R8G8B8A8_UNORM, + [0, 0, 255, 255], [0, 128, 0, 128], [0, 128, 127, 255]), // Offscreen typeless storage supports either view of the same format family. - // The fractional overlay encodes a premultiplied linear green of about 0.25. - (DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, DXGI_FORMAT_R8G8B8A8_TYPELESS, [188, 128, 0, 255], [0, 137, 0, 128], [137, 161, 0, 255]), + // Both 137 sRGB and 64 UNORM encode premultiplied green of about 0.25. + (DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, DXGI_FORMAT_R8G8B8A8_TYPELESS, + [188, 128, 0, 255], [0, 137, 0, 128], [137, 161, 0, 255]), + (DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_FORMAT_R8G8B8A8_TYPELESS, + [188, 128, 0, 255], [0, 64, 0, 128], [137, 161, 0, 255]), + (DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, DXGI_FORMAT_R8G8B8A8_TYPELESS, + [0, 0, 255, 255], [0, 137, 0, 128], [0, 64, 127, 255]), + (DXGI_FORMAT_B8G8R8A8_UNORM_SRGB, DXGI_FORMAT_B8G8R8A8_UNORM, DXGI_FORMAT_B8G8R8A8_TYPELESS, + [188, 128, 0, 255], [0, 64, 0, 128], [137, 161, 0, 255]), ]; - for (format, target_format, scene_color, blend_color, expected_blend) in cases { + for (scene_format, ui_format, target_format, scene_color, blend_color, expected_blend) in cases { + let output_format = swap_format(scene_format).unwrap(); let alloc: ID3D12CommandAllocator = dev.CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT).unwrap(); let list: ID3D12GraphicsCommandList = dev.CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, &alloc, None).unwrap(); let fence: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).unwrap(); @@ -1768,8 +1774,8 @@ mod frame_dump_tests { crate::gpu::texture::record_upload(&dev, &list, tex, &[sub], D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_COMMON).unwrap() }; - let scene = make_tex(4, 4, format, D3D12_RESOURCE_FLAG_NONE); - let ui = make_tex(8, 8, format, D3D12_RESOURCE_FLAG_NONE); + let scene = make_tex(4, 4, scene_format, D3D12_RESOURCE_FLAG_NONE); + let ui = make_tex(8, 8, ui_format, D3D12_RESOURCE_FLAG_NONE); let out = make_tex(8, 8, target_format, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET); let _up0 = upload_tex(&scene, &|_, _| scene_color); let _up1 = upload_tex(&ui, &|x, y| { @@ -1787,9 +1793,9 @@ mod frame_dump_tests { (scene.clone(), scene.GetDesc(), [0.0, 0.0, 1.0, 1.0]), (ui.clone(), ui.GetDesc(), [0.0, 0.0, 1.0, 1.0]), ]; - assert!(comp.record(&dev, &list, &out, 8, 8, DXGI_FORMAT_R8G8B8A8_UNORM, &planes, 0)); + assert!(comp.record(&dev, &list, &out, 8, 8, output_format, &planes, 0)); list.ResourceBarrier(&[transition(&out, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_COPY_SOURCE)]); - let out_desc = D3D12_RESOURCE_DESC { Format: DXGI_FORMAT_R8G8B8A8_UNORM, ..out.GetDesc() }; + let out_desc = D3D12_RESOURCE_DESC { Format: output_format, ..out.GetDesc() }; let (rb, fp) = readback_copy(&list, &out, &out_desc).unwrap(); list.Close().unwrap(); @@ -1807,11 +1813,11 @@ mod frame_dump_tests { let (transparent, opaque, blend) = (px(1, 1), px(6, 1), px(1, 2)); rb.Unmap(0, None); - assert_eq!(transparent, scene_color, "transparent overlay in {format:?}"); - assert_eq!(opaque, [255, 0, 0, 255], "opaque overlay in {format:?}"); + assert_eq!(transparent, scene_color, "transparent overlay with {scene_format:?} scene and {ui_format:?} UI"); + assert_eq!(opaque, [255, 0, 0, 255], "opaque overlay with {scene_format:?} scene and {ui_format:?} UI"); assert!( blend[..3].iter().zip(expected_blend).all(|(&actual, expected)| actual.abs_diff(expected) <= 2) && blend[3] == expected_blend[3], - "fractional overlay in {format:?}: expected {expected_blend:?}, got {blend:?}", + "fractional overlay with {scene_format:?} scene and {ui_format:?} UI: expected {expected_blend:?}, got {blend:?}", ); } }