diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index b18c6e1c05..91cd4665d4 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -6900,13 +6900,28 @@ pub(super) unsafe extern "system" fn end_query_x(this: *mut c_void, heap: usize, pc::(this, PC_END_QUERY)(this, heap, ty, index); } +/// Byte size written per query by `ResolveQueryData` for each `D3D12_QUERY_TYPE`. +fn query_result_bytes(ty: D3D12_QUERY_TYPE) -> u64 { + use std::mem::size_of; + match ty { + D3D12_QUERY_TYPE_PIPELINE_STATISTICS => size_of::() as u64, + D3D12_QUERY_TYPE_SO_STATISTICS_STREAM0 + | D3D12_QUERY_TYPE_SO_STATISTICS_STREAM1 + | D3D12_QUERY_TYPE_SO_STATISTICS_STREAM2 + | D3D12_QUERY_TYPE_SO_STATISTICS_STREAM3 => size_of::() as u64, + // D3D12_QUERY_DATA_VIDEO_DECODE_STATISTICS: 2x u64 + DXGI_RATIONAL (8 B) + u32 + 4 B pad. + D3D12_QUERY_TYPE_VIDEO_DECODE_STATISTICS => 32, + D3D12_QUERY_TYPE_PIPELINE_STATISTICS1 => size_of::() as u64, + _ => 8, + } +} + /// command list #30 ResolveQueryData(heap, type, first, count, destination, offset) (PC #54): /// outer occlusion queries ended early (or queries whose draws were dropped/pending) are written /// over the resolved data (after it, in COPY_DEST as ResolveQueryData wants the destination). /// Buffers placed below their console address add `buffer_delta(dest)` to the destination offset. pub(super) unsafe extern "system" fn resolve_query_data_x(this: *mut c_void, heap: usize, ty: D3D12_QUERY_TYPE, first: u32, count: u32, dest: usize, offset: u64) { - // (8 bytes a query; pipeline statistics, the largest, are 11 values) - note_copy_into(this as usize, dest, offset, count as u64 * if ty == D3D12_QUERY_TYPE_PIPELINE_STATISTICS { 88 } else { 8 }); + note_copy_into(this as usize, dest, offset, (count as u64).saturating_mul(query_result_bytes(ty))); let pc_offset = offset.saturating_add(real::buffer_delta(dest)); type F = unsafe extern "system" fn(*mut c_void, usize, D3D12_QUERY_TYPE, u32, u32, usize, u64); pc::(this, PC_RESOLVE_QUERY_DATA)(this, heap, ty, first, count, dest, pc_offset); @@ -11789,4 +11804,69 @@ mod tests { assert!(got.iter().all(|&v| v == 0xDEAD_BEEF), "the UAV was not cleared: {:x?}", &got[..4]); } } + + #[test] + fn resolve_query_data_x_notes_exact_result_bytes_for_all_query_types() { + const CHILD: &str = "XCL_TEST_RESOLVE_QUERY_SIZES_CHILD"; + if std::env::var_os(CHILD).is_none() { + let output = std::process::Command::new(std::env::current_exe().unwrap()) + .args([ + "--exact", + "hle::d3d12x::list_x::tests::resolve_query_data_x_notes_exact_result_bytes_for_all_query_types", + "--nocapture", + ]) + .env(CHILD, "1") + .env("XCL_UMA_MB", "64") + .env("XCL_COHERENT_MB", "64") + .output() + .unwrap(); + assert!(output.status.success(), "{}\n{}", String::from_utf8_lossy(&output.stdout), String::from_utf8_lossy(&output.stderr)); + return; + } + let Some(u) = crate::gpu::uma::get() else { return crate::gpu::recomp::runner::skip_test("no UMA") }; + u.set_shadowed(); + let committed_win = (0..16).find(|&k| u.is_committed_window(k)).expect("committed UMA window"); + let buf_ptr = u.alloc_in(committed_win, 4096, 256).expect("committed UMA buffer"); + let buf_va = buf_ptr as usize; + assert!(u.is_committed(buf_va), "buffer must live in committed UMA window"); + + let dest = 0x7777_0001usize; + real::set_test_console_buffer(dest, Some((buf_va, 4096))); + + #[repr(C)] + struct FakeCom { + vt: *const usize, + } + unsafe extern "system" fn fake_resolve(_: *mut c_void, _: usize, _: D3D12_QUERY_TYPE, _: u32, _: u32, _: usize, _: u64) {} + let mut orig_list_vt = [0usize; 70]; + orig_list_vt[PC_RESOLVE_QUERY_DATA] = fake_resolve as usize; + let ext_list_vt = [orig_list_vt.as_ptr() as usize, 0usize]; + let mut fake_list = FakeCom { vt: unsafe { ext_list_vt.as_ptr().add(1) } }; + let list_ptr = &mut fake_list as *mut FakeCom as *mut c_void; + let list_id = list_ptr as usize; + + let cases = [ + ("OCCLUSION", D3D12_QUERY_TYPE_OCCLUSION, 2u32, 16usize), + ("BINARY_OCCLUSION", D3D12_QUERY_TYPE_BINARY_OCCLUSION, 3, 24), + ("TIMESTAMP", D3D12_QUERY_TYPE_TIMESTAMP, 4, 32), + ("PIPELINE_STATISTICS", D3D12_QUERY_TYPE_PIPELINE_STATISTICS, 2, 176), + ("SO_STATISTICS_STREAM0", D3D12_QUERY_TYPE_SO_STATISTICS_STREAM0, 2, 32), + ("SO_STATISTICS_STREAM1", D3D12_QUERY_TYPE_SO_STATISTICS_STREAM1, 1, 16), + ("SO_STATISTICS_STREAM2", D3D12_QUERY_TYPE_SO_STATISTICS_STREAM2, 3, 48), + ("SO_STATISTICS_STREAM3", D3D12_QUERY_TYPE_SO_STATISTICS_STREAM3, 2, 32), + ("VIDEO_DECODE_STATISTICS", D3D12_QUERY_TYPE_VIDEO_DECODE_STATISTICS, 2, 64), + ("PIPELINE_STATISTICS1", D3D12_QUERY_TYPE_PIPELINE_STATISTICS1, 2, 224), + ]; + + for (label, ty, count, expected_bytes) in cases { + if let Some(m) = LISTS.lock().unwrap().as_mut() { + m.remove(&list_id); + } + unsafe { resolve_query_data_x(list_ptr, 0x1000, ty, 0, count, dest, 64) }; + let writes = LISTS.lock().unwrap().as_ref().and_then(|m| m.get(&list_id)).map(|l| l.sparse_writes.clone()).unwrap_or_default(); + assert_eq!(writes, vec![(buf_va + 64, expected_bytes)], "{label}: recorded write span"); + } + real::set_test_console_buffer(dest, None); + u.free(buf_va); + } } diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index eafeccd069..4842676093 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -2280,6 +2280,21 @@ pub(super) fn set_test_buffer_delta(res: usize, delta: Option) { } } +#[cfg(test)] +pub(super) fn set_test_console_buffer(res: usize, entry: Option<(usize, usize)>) { + let mut g = UMA_BUFFERS.lock().unwrap(); + match entry { + Some((p, n)) => { + g.get_or_insert_with(HashMap::new).insert(res, (p, n, false)); + } + None => { + if let Some(m) = g.as_mut() { + m.remove(&res); + } + } + } +} + /// Whether the PC's Map works on `res`: not a reserved resource, and its heap has CPU pages. unsafe fn pc_can_map(res: *mut c_void) -> bool { !is_reserved(res) && pc_mappable(heap_properties(res))