diff --git a/crates/xcl/src/hle/d3d12x/present.rs b/crates/xcl/src/hle/d3d12x/present.rs index cf37a356d8..f25bcd4102 100644 --- a/crates/xcl/src/hle/d3d12x/present.rs +++ b/crates/xcl/src/hle/d3d12x/present.rs @@ -296,10 +296,26 @@ pub fn swap_format(f: DXGI_FORMAT) -> Option { DXGI_FORMAT_B8G8R8A8_UNORM | DXGI_FORMAT_B8G8R8A8_UNORM_SRGB | DXGI_FORMAT_B8G8R8A8_TYPELESS => DXGI_FORMAT_B8G8R8A8_UNORM, DXGI_FORMAT_R10G10B10A2_UNORM | DXGI_FORMAT_R10G10B10A2_TYPELESS => DXGI_FORMAT_R10G10B10A2_UNORM, DXGI_FORMAT_R16G16B16A16_FLOAT | DXGI_FORMAT_R16G16B16A16_TYPELESS => DXGI_FORMAT_R16G16B16A16_FLOAT, + // packed float planes: no swap chain has these formats, the compositor draws them into a float one + DXGI_FORMAT_R11G11B10_FLOAT | DXGI_FORMAT_R9G9B9E5_SHAREDEXP => DXGI_FORMAT_R16G16B16A16_FLOAT, _ => return None, }) } +/// Format the compositor samples a plane of `f` in: the swap chain's, except for a plane whose +/// format is not of the swap chain's family. +fn plane_view_format(f: DXGI_FORMAT) -> Option { + match f { + DXGI_FORMAT_R11G11B10_FLOAT | DXGI_FORMAT_R9G9B9E5_SHAREDEXP => Some(f), + _ => swap_format(f), + } +} + +/// A plane of `f` cannot be copied into the swap chain's buffer: it has to be drawn into it. +fn needs_conversion(f: DXGI_FORMAT) -> bool { + plane_view_format(f) != swap_format(f) +} + unsafe fn backbuffer_like(res: usize) -> Option { let raw = res as *mut c_void; let r = ID3D12Resource::from_raw_borrowed(&raw)?; @@ -694,7 +710,8 @@ pub unsafe fn note_execute(queue: *mut c_void, lists: &[*mut c_void]) { let Some(h) = handle else { return }; let res = RTVS.lock().unwrap().as_ref().and_then(|m| m.get(&h).copied()); let Some(res) = res else { return }; - if backbuffer_like(res).is_none() { + // (a packed float target is a scene target here, not a back buffer: only PresentX names one as a plane) + if backbuffer_like(res).is_none_or(|d| needs_conversion(d.Format)) { return; } { @@ -1205,6 +1222,18 @@ impl Compositor { Some(pair) } + /// Whether `record` can draw these planes into a target of `dst_fmt`. + unsafe fn can_draw(&mut self, dev: &ID3D12Device, dst_fmt: DXGI_FORMAT, planes: &[(ID3D12Resource, D3D12_RESOURCE_DESC, [f32; 4])]) -> bool { + let active = &planes[..planes.len().min(MAX_PLANES)]; + let plane_ok = |d: &D3D12_RESOURCE_DESC| { + d.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE2D + && d.SampleDesc.Count == 1 + && !d.Flags.contains(D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE) + && swap_format(d.Format).is_some() + }; + !active.is_empty() && active.iter().all(|(_, d, _)| plane_ok(d)) && self.psos_for(dev, dst_fmt).is_some() + } + unsafe fn record( &mut self, dev: &ID3D12Device, @@ -1217,13 +1246,7 @@ impl Compositor { slot: usize, ) -> bool { let active = &planes[..planes.len().min(MAX_PLANES)]; - let plane_ok = |d: &D3D12_RESOURCE_DESC| { - d.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE2D - && d.SampleDesc.Count == 1 - && !d.Flags.contains(D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE) - && swap_format(d.Format).is_some() - }; - if dst_w == 0 || dst_h == 0 || active.is_empty() || !active.iter().all(|(_, d, _)| plane_ok(d)) { + if dst_w == 0 || dst_h == 0 || !self.can_draw(dev, dst_fmt, planes) { return false; } let first = slot % PRESENT_SLOTS * MAX_PLANES; @@ -1240,7 +1263,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_view_format(desc.Format).unwrap_or(desc.Format), ViewDimension: D3D12_SRV_DIMENSION_TEXTURE2D, Shader4ComponentMapping: D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING, ..Default::default() @@ -1409,7 +1432,7 @@ unsafe fn ensure_swapchain(p: &mut Presenter, hwnd: HWND, w: u32, h: u32, fmt: D } fn needs_compositor(planes: &[(ID3D12Resource, D3D12_RESOURCE_DESC, [f32; 4])]) -> bool { - planes.len() > 1 || planes.first().is_some_and(|(_, _, uv)| *uv != [0.0, 0.0, 1.0, 1.0]) + planes.len() > 1 || planes.first().is_some_and(|(_, d, uv)| *uv != [0.0, 0.0, 1.0, 1.0] || needs_conversion(d.Format)) } unsafe fn present_now(min_interval: Duration, frame: Option<(usize, Vec<(usize, Option<[i32; 4]>)>)>) { @@ -1439,8 +1462,13 @@ unsafe fn present_now(min_interval: Duration, frame: Option<(usize, Vec<(usize, let Some(_queue_pin) = pin_queue_for_internal_work(queue) else { return }; let resolved: Vec<(ID3D12Resource, D3D12_RESOURCE_DESC, [f32; 4])> = raw_planes .into_iter() - .filter_map(|(res, rect)| { + .enumerate() + .filter_map(|(i, (res, rect))| { let desc = backbuffer_like(res)?; + // (a packed float plane is taken as the title's first plane only: as an overlay it is left out, as before) + if i > 0 && needs_conversion(desc.Format) { + return None; + } let raw = res as *mut c_void; let r = ID3D12Resource::from_raw_borrowed(&raw)?.clone(); let uv = source_uv_rect(rect, desc.Width as u32, desc.Height); @@ -1479,6 +1507,20 @@ unsafe fn present_now(min_interval: Duration, frame: Option<(usize, Vec<(usize, if p.queue != queue { return; // swap chain is bound to the first queue we presented from } + // a plane that has to be drawn and cannot be: nothing is presented (no resize to a float swap + // chain, no undefined buffer on screen – the window keeps its picture) + if needs_conversion(resolved[0].1.Format) { + if p.compositor.is_none() { + p.compositor = Compositor::new(&p.dev); + } + if !p.compositor.as_mut().is_some_and(|c| c.can_draw(&p.dev, fmt, &resolved)) { + static WARNED: AtomicBool = AtomicBool::new(false); + if !WARNED.swap(true, Ordering::Relaxed) { + crate::xlog_warn!("present: a {:?} plane needs the compositor, which cannot draw it – the plane is not shown", resolved[0].1.Format); + } + return; + } + } // rotate to the oldest in-flight allocator slot and wait only for its fence (frame N-2) let waited = { let _cost = super::look_stats::deep().then(|| crate::hle::api_cost::time(crate::hle::api_cost::Call::PresentCopyWait)); @@ -1583,7 +1625,9 @@ unsafe fn present_now(min_interval: Duration, frame: Option<(usize, Vec<(usize, let (src, desc, _) = &resolved[0]; let b1 = [transition(&bb, D3D12_RESOURCE_STATE_PRESENT, D3D12_RESOURCE_STATE_COPY_DEST)]; p.list.ResourceBarrier(&b1); - if desc.Width as u32 == p.w && desc.Height == p.h { + if needs_conversion(desc.Format) { + // no copy between these formats (not reached: `can_draw` was checked before the swap chain) + } else if desc.Width as u32 == p.w && desc.Height == p.h { p.list.CopyResource(&bb, src); } else if desc.Width > 0 && desc.Height > 0 && p.w > 0 && p.h > 0 { let cw = (desc.Width as u32).min(p.w); @@ -2627,4 +2671,63 @@ mod frame_dump_tests { rb.Unmap(0, None); } } + + #[test] + fn compositor_converts_a_single_packed_float_plane() { + // no swap chain has these formats: the plane gets a float one and is drawn into it + for f in [DXGI_FORMAT_R11G11B10_FLOAT, DXGI_FORMAT_R9G9B9E5_SHAREDEXP] { + assert_eq!(swap_format(f), Some(DXGI_FORMAT_R16G16B16A16_FLOAT), "{f:?}"); + } + let _lock = COMPOSITOR_TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner()); + unsafe { + let (dev, _alloc, list, mut comp) = warp_env(); + let queue: ID3D12CommandQueue = dev.CreateCommandQueue(&D3D12_COMMAND_QUEUE_DESC { Type: D3D12_COMMAND_LIST_TYPE_DIRECT, ..Default::default() }).unwrap(); + let fence: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).unwrap(); + let tex = |format: DXGI_FORMAT, flags: D3D12_RESOURCE_FLAGS| -> ID3D12Resource { + let mut r: Option = None; + let desc = D3D12_RESOURCE_DESC { Dimension: D3D12_RESOURCE_DIMENSION_TEXTURE2D, Width: 8, Height: 8, DepthOrArraySize: 1, MipLevels: 1, Format: format, SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 }, Flags: flags, ..Default::default() }; + dev.CreateCommittedResource(&D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE_DEFAULT, ..Default::default() }, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_COMMON, None, &mut r).unwrap(); + r.unwrap() + }; + let (src, out) = (tex(DXGI_FORMAT_R11G11B10_FLOAT, D3D12_RESOURCE_FLAG_NONE), tex(DXGI_FORMAT_R16G16B16A16_FLOAT, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET)); + let plane = [(src.clone(), src.GetDesc(), [0.0, 0.0, 1.0, 1.0])]; + assert!(needs_compositor(&plane), "a copy cannot change the format"); + // what `present_now` asks before it makes the float swap chain: a plane no view can read is not presented + assert!(comp.can_draw(&dev, DXGI_FORMAT_R16G16B16A16_FLOAT, &plane)); + let mut deny = src.GetDesc(); + deny.Flags |= D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE; + assert!(!comp.can_draw(&dev, DXGI_FORMAT_R16G16B16A16_FLOAT, &[(src.clone(), deny, [0.0, 0.0, 1.0, 1.0])])); + + let mut up: Option = None; + let up_desc = D3D12_RESOURCE_DESC { Dimension: D3D12_RESOURCE_DIMENSION_BUFFER, Width: 256 * 8, Height: 1, DepthOrArraySize: 1, MipLevels: 1, SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 }, Layout: D3D12_TEXTURE_LAYOUT_ROW_MAJOR, ..Default::default() }; + dev.CreateCommittedResource(&D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE_UPLOAD, ..Default::default() }, D3D12_HEAP_FLAG_NONE, &up_desc, D3D12_RESOURCE_STATE_GENERIC_READ, None, &mut up).unwrap(); + let (up, mut ptr) = (up.unwrap(), std::ptr::null_mut()); + up.Map(0, None, Some(&mut ptr)).unwrap(); + let bytes = std::slice::from_raw_parts_mut(ptr as *mut u8, 256 * 8); + // (1.0, 0.5, 0.25): 11-bit floats 0x3C0 and 0x380, 10-bit float 0x1A0 + let texel = (0x3C0u32 | 0x380 << 11 | 0x1A0 << 22).to_le_bytes(); + for y in 0..8usize { + for x in 0..8usize { bytes[y * 256 + x * 4..y * 256 + x * 4 + 4].copy_from_slice(&texel); } + } + up.Unmap(0, None); + let dst_loc = D3D12_TEXTURE_COPY_LOCATION { pResource: std::mem::transmute_copy(&src), 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_R11G11B10_FLOAT, Width: 8, Height: 8, Depth: 1, RowPitch: 256 } } } }; + list.ResourceBarrier(&[transition(&src, D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_COPY_DEST)]); + list.CopyTextureRegion(&dst_loc, 0, 0, 0, &src_loc, None); + list.ResourceBarrier(&[transition(&src, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_COMMON), transition(&out, D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_RENDER_TARGET)]); + assert!(comp.record(&dev, &list, &out, 8, 8, DXGI_FORMAT_R16G16B16A16_FLOAT, &plane, 0)); + list.ResourceBarrier(&[transition(&out, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_COPY_SOURCE)]); + let (rb, fp) = readback_copy(&list, &out, &out.GetDesc()).unwrap(); + list.Close().unwrap(); + queue.ExecuteCommandLists(&[Some(list.cast().unwrap())]); + queue.Signal(&fence, 1).unwrap(); + fence::wait(&fence, 1, &dev, None, 5000).unwrap(); + rb.Map(0, None, Some(&mut ptr)).unwrap(); + for (x, y) in [(0usize, 0usize), (5, 2), (7, 7)] { + let halves = (ptr as *const u8).add(fp.Offset as usize + y * fp.Footprint.RowPitch as usize + x * 8).cast::<[u16; 4]>().read_unaligned(); + assert_eq!(halves, [0x3C00, 0x3800, 0x3400, 0x3C00], "pixel ({x},{y})"); + } + rb.Unmap(0, None); + } + } }