|
|
@@ -1738,6 +1738,291 @@ void main(uint3 t : SV_DispatchThreadID) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /// Whether lists on the card may read a texture of a block made after
|
|
|
+ /// they were sent, through a slot they read by index (ignored: a
|
|
|
+ /// measurement that loses the device, so each way runs in a process of
|
|
|
+ /// its own; `VICTORY_BENCH_S` seconds a way, 30; `VICTORY_BENCH_N` runs of
|
|
|
+ /// each, 3). Threads (`VICTORY_BENCH_THREADS`, 4) make blocks of 64 MiB, a texture in each and its
|
|
|
+ /// view, and copies the view into a slot; long dispatches that read all
|
|
|
+ /// the slots are always on the card.
|
|
|
+ #[test]
|
|
|
+ #[ignore]
|
|
|
+ fn new_blocks_under_readers() {
|
|
|
+ if let Ok(way) = std::env::var("VICTORY_BLOCK_WAY") {
|
|
|
+ return new_blocks(&way);
|
|
|
+ }
|
|
|
+ let times: u32 = std::env::var("VICTORY_BENCH_N").ok().and_then(|v| v.parse().ok()).unwrap_or(3);
|
|
|
+ // "never": never resident, what must lose the device.
|
|
|
+ for way in ["made", "waited", "enqueued", "never"] {
|
|
|
+ for n in 0..if way == "never" { 1 } else { times } {
|
|
|
+ let out = std::process::Command::new(std::env::current_exe().unwrap())
|
|
|
+ .args(["indirect::tests::new_blocks_under_readers", "--exact", "--ignored", "--nocapture"])
|
|
|
+ .env("VICTORY_BLOCK_WAY", way)
|
|
|
+ .output()
|
|
|
+ .unwrap();
|
|
|
+ let text = String::from_utf8_lossy(&out.stdout).into_owned();
|
|
|
+ let said = |what: &str| text.lines().find(|l| l.contains(what)).unwrap_or("(nothing said)").trim().to_owned();
|
|
|
+ println!("{way}, run {n}: {}; {} (exit {:?})", said("dispatches a"), said("blocks,"), out.status.code());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ fn new_blocks(way: &str) {
|
|
|
+ let Some(mut gpu) = WarpHarness::on(false) else {
|
|
|
+ println!("no card: 0 blocks,");
|
|
|
+ return;
|
|
|
+ };
|
|
|
+ const SLOTS: usize = 64;
|
|
|
+ const BLOCK: u64 = 64 << 20;
|
|
|
+ let seconds = std::env::var("VICTORY_BENCH_S").ok().and_then(|v| v.parse().ok()).unwrap_or(30u64);
|
|
|
+ const SOURCE: &str = r#"
|
|
|
+#define RS "DescriptorTable(SRV(t0,numDescriptors=64,flags=DESCRIPTORS_VOLATILE|DATA_VOLATILE)), UAV(u0)"
|
|
|
+Texture2D<float4> t[64] : register(t0);
|
|
|
+RWByteAddressBuffer o : register(u0);
|
|
|
+[RootSignature(RS)]
|
|
|
+[numthreads(64, 1, 1)]
|
|
|
+void main(uint3 g : SV_GroupID, uint i : SV_GroupIndex) {
|
|
|
+ uint which = g.x & 63;
|
|
|
+ float4 c = 0;
|
|
|
+ for (uint k = 0; k < 8; k++)
|
|
|
+ c += t[(which + k * 8) & 63].Load(int3((i + k * 13) & 127, (g.y + k * 29) & 255, 0));
|
|
|
+ if (c.x < -1) o.Store(0, 1);
|
|
|
+}
|
|
|
+"#;
|
|
|
+ let pass = compile_cs(&gpu.device, SOURCE).expect("the pass must compile");
|
|
|
+ // SAFETY: plain heap creation.
|
|
|
+ let heap = |visible: bool, n: u32| -> ID3D12DescriptorHeap {
|
|
|
+ unsafe {
|
|
|
+ gpu.device.CreateDescriptorHeap(&D3D12_DESCRIPTOR_HEAP_DESC {
|
|
|
+ Type: D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV,
|
|
|
+ NumDescriptors: n,
|
|
|
+ Flags: if visible { D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE } else { D3D12_DESCRIPTOR_HEAP_FLAG_NONE },
|
|
|
+ NodeMask: 0,
|
|
|
+ })
|
|
|
+ }
|
|
|
+ .unwrap()
|
|
|
+ };
|
|
|
+ let (shown, beside) = (heap(true, SLOTS as u32), heap(false, 65));
|
|
|
+ // SAFETY: live heaps.
|
|
|
+ let (to, from, step) = unsafe {
|
|
|
+ (
|
|
|
+ shown.GetCPUDescriptorHandleForHeapStart().ptr,
|
|
|
+ beside.GetCPUDescriptorHandleForHeapStart().ptr,
|
|
|
+ gpu.device.GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV) as usize,
|
|
|
+ )
|
|
|
+ };
|
|
|
+ let null = D3D12_SHADER_RESOURCE_VIEW_DESC {
|
|
|
+ Format: DXGI_FORMAT(28),
|
|
|
+ ViewDimension: D3D12_SRV_DIMENSION_TEXTURE2D,
|
|
|
+ Shader4ComponentMapping: D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING,
|
|
|
+ Anonymous: D3D12_SHADER_RESOURCE_VIEW_DESC_0 {
|
|
|
+ Texture2D: D3D12_TEX2D_SRV { MipLevels: 1, ..Default::default() },
|
|
|
+ },
|
|
|
+ };
|
|
|
+ let slot = |i: usize| D3D12_CPU_DESCRIPTOR_HANDLE { ptr: to + i * step };
|
|
|
+ let own = |i: usize| D3D12_CPU_DESCRIPTOR_HANDLE { ptr: from + i * step };
|
|
|
+ // SAFETY: slots of our heaps.
|
|
|
+ unsafe {
|
|
|
+ gpu.device.CreateShaderResourceView(None::<&ID3D12Resource>, Some(&null), own(0));
|
|
|
+ for i in 0..SLOTS {
|
|
|
+ gpu.device.CopyDescriptorsSimple(1, slot(i), own(0), D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ let (_out, _, out_gpu) = gpu.shared_buffer(1 << 12);
|
|
|
+ // SAFETY: a live heap.
|
|
|
+ let table = unsafe { shown.GetGPUDescriptorHandleForHeapStart() };
|
|
|
+ let list = |dispatches: u32| -> (ID3D12CommandAllocator, ID3D12GraphicsCommandList) {
|
|
|
+ // SAFETY: plain creation and recording.
|
|
|
+ unsafe {
|
|
|
+ let allocator: ID3D12CommandAllocator =
|
|
|
+ gpu.device.CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT).unwrap();
|
|
|
+ let list: ID3D12GraphicsCommandList =
|
|
|
+ gpu.device.CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, &allocator, None).unwrap();
|
|
|
+ list.SetDescriptorHeaps(&[Some(shown.clone())]);
|
|
|
+ list.SetComputeRootSignature(&pass.root);
|
|
|
+ list.SetPipelineState(&pass.pso);
|
|
|
+ list.SetComputeRootDescriptorTable(0, table);
|
|
|
+ list.SetComputeRootUnorderedAccessView(1, out_gpu);
|
|
|
+ for _ in 0..dispatches {
|
|
|
+ list.Dispatch(1024, 64, 1);
|
|
|
+ }
|
|
|
+ list.Close().unwrap();
|
|
|
+ (allocator, list)
|
|
|
+ }
|
|
|
+ };
|
|
|
+ // Lists of some 5 ms, always five sent: some run while a slot changes.
|
|
|
+ let mut sent = 0u64;
|
|
|
+ let mut dispatches = 8;
|
|
|
+ let ms = loop {
|
|
|
+ let one = list(dispatches);
|
|
|
+ let began = std::time::Instant::now();
|
|
|
+ // SAFETY: a closed list of ours; waited for.
|
|
|
+ unsafe {
|
|
|
+ gpu.queue.ExecuteCommandLists(&[Some(one.1.cast().unwrap())]);
|
|
|
+ sent += 1;
|
|
|
+ gpu.queue.Signal(&gpu.fence, sent).unwrap();
|
|
|
+ let _ = gpu.fence.SetEventOnCompletion(sent, gpu.event);
|
|
|
+ WaitForSingleObject(gpu.event, 10_000);
|
|
|
+ }
|
|
|
+ let ms = began.elapsed().as_secs_f64() * 1e3;
|
|
|
+ if ms >= 4.0 || dispatches >= 1 << 14 {
|
|
|
+ break ms;
|
|
|
+ }
|
|
|
+ dispatches *= 2;
|
|
|
+ };
|
|
|
+ println!("{dispatches} dispatches a list, {ms:.1} ms each");
|
|
|
+ let lists: Vec<_> = (0..6).map(|_| list(dispatches)).collect();
|
|
|
+ use std::sync::atomic::{AtomicBool, AtomicU64, Ordering::SeqCst};
|
|
|
+ let sent = AtomicU64::new(sent);
|
|
|
+ let stop = AtomicBool::new(false);
|
|
|
+ // Blocks; microseconds making a block, the longest; waiting, the longest.
|
|
|
+ let counts: [AtomicU64; 5] = [const { AtomicU64::new(0) }; 5];
|
|
|
+ let threads: usize = std::env::var("VICTORY_BENCH_THREADS").ok().and_then(|v| v.parse().ok()).unwrap_or(4);
|
|
|
+ let started = std::time::Instant::now();
|
|
|
+ let lost = std::thread::scope(|scope| {
|
|
|
+ for thread in 0..threads {
|
|
|
+ let (device, fence) = (gpu.device.clone(), gpu.fence.clone());
|
|
|
+ let (sent, stop, counts) = (&sent, &stop, &counts);
|
|
|
+ scope.spawn(move || {
|
|
|
+ // Slots of its own, and a staged view of its own.
|
|
|
+ let mine = SLOTS / threads;
|
|
|
+ let slot = |i: usize| D3D12_CPU_DESCRIPTOR_HANDLE { ptr: to + (thread * mine + i) * step };
|
|
|
+ let own = |i: usize| D3D12_CPU_DESCRIPTOR_HANDLE { ptr: from + i * step };
|
|
|
+ let wait = way != "made";
|
|
|
+ let mut alive: Vec<(ID3D12Heap, ID3D12Resource)> = Vec::new();
|
|
|
+ let mut n = 0u64;
|
|
|
+ while !stop.load(SeqCst) {
|
|
|
+ // SAFETY: plain creation; slots of our heaps, this thread's.
|
|
|
+ unsafe {
|
|
|
+ let mut block: Option<ID3D12Heap> = None;
|
|
|
+ let flags = D3D12_HEAP_FLAG_ALLOW_ONLY_NON_RT_DS_TEXTURES
|
|
|
+ | if wait { D3D12_HEAP_FLAG_CREATE_NOT_RESIDENT } else { D3D12_HEAP_FLAG_NONE };
|
|
|
+ let heap = D3D12_HEAP_DESC {
|
|
|
+ SizeInBytes: BLOCK,
|
|
|
+ Properties: D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE_DEFAULT, ..Default::default() },
|
|
|
+ Alignment: 0,
|
|
|
+ Flags: flags,
|
|
|
+ };
|
|
|
+ let began = std::time::Instant::now();
|
|
|
+ if device.CreateHeap(&heap, &mut block).is_err() {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ let us = began.elapsed().as_micros() as u64;
|
|
|
+ counts[1].fetch_add(us, SeqCst);
|
|
|
+ counts[2].fetch_max(us, SeqCst);
|
|
|
+ let block = block.unwrap();
|
|
|
+ let pageable: ID3D12Pageable = block.cast().unwrap();
|
|
|
+ let began = std::time::Instant::now();
|
|
|
+ if way == "waited" && device.MakeResident(&[Some(pageable.clone())]).is_err() {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ if way == "enqueued" {
|
|
|
+ let device: ID3D12Device3 = device.cast().unwrap();
|
|
|
+ let paged: ID3D12Fence = device.CreateFence(0, D3D12_FENCE_FLAG_NONE).unwrap();
|
|
|
+ let asked =
|
|
|
+ device.EnqueueMakeResident(D3D12_RESIDENCY_FLAG_NONE, &[Some(pageable)], &paged, 1);
|
|
|
+ if asked.is_err() {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ while paged.GetCompletedValue() < 1 {
|
|
|
+ std::hint::spin_loop();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ let us = began.elapsed().as_micros() as u64;
|
|
|
+ counts[3].fetch_add(us, SeqCst);
|
|
|
+ counts[4].fetch_max(us, SeqCst);
|
|
|
+ let desc = D3D12_RESOURCE_DESC {
|
|
|
+ Dimension: D3D12_RESOURCE_DIMENSION_TEXTURE2D,
|
|
|
+ Width: 128,
|
|
|
+ Height: 256,
|
|
|
+ DepthOrArraySize: 1,
|
|
|
+ MipLevels: 1,
|
|
|
+ Format: DXGI_FORMAT(28),
|
|
|
+ SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 },
|
|
|
+ ..Default::default()
|
|
|
+ };
|
|
|
+ let mut texture: Option<ID3D12Resource> = None;
|
|
|
+ // Somewhere in the block, as the pool's pieces are.
|
|
|
+ let at = (n * 7 % 900) << 16;
|
|
|
+ if device
|
|
|
+ .CreatePlacedResource(&block, at, &desc, D3D12_RESOURCE_STATE_COMMON, None, &mut texture)
|
|
|
+ .is_err()
|
|
|
+ {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ let texture = texture.unwrap();
|
|
|
+ let kind = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
|
|
|
+ device.CreateShaderResourceView(&texture, None, own(1 + thread));
|
|
|
+ device.CopyDescriptorsSimple(1, slot(n as usize % mine), own(1 + thread), kind);
|
|
|
+ alive.push((block, texture));
|
|
|
+ n += 1;
|
|
|
+ counts[0].fetch_add(1, SeqCst);
|
|
|
+ if alive.len() >= 24 {
|
|
|
+ // Nothing shows or reads them any more when they go.
|
|
|
+ for i in 0..mine {
|
|
|
+ device.CopyDescriptorsSimple(1, slot(i), own(0), kind);
|
|
|
+ }
|
|
|
+ let until = sent.load(SeqCst) + 1;
|
|
|
+ while fence.GetCompletedValue() < until && !stop.load(SeqCst) {
|
|
|
+ std::thread::sleep(std::time::Duration::from_millis(1));
|
|
|
+ }
|
|
|
+ if fence.GetCompletedValue() < until {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ alive.clear();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // The lists sent last may read them: the process goes next.
|
|
|
+ std::mem::forget(alive);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ let lost = loop {
|
|
|
+ // SAFETY: closed lists of ours, each run again only when it has run.
|
|
|
+ unsafe {
|
|
|
+ let done = gpu.fence.GetCompletedValue();
|
|
|
+ if done == u64::MAX || gpu.device.GetDeviceRemovedReason().is_err() {
|
|
|
+ break true;
|
|
|
+ }
|
|
|
+ while sent.load(SeqCst) < done + 5 {
|
|
|
+ let n = sent.load(SeqCst);
|
|
|
+ let list: ID3D12CommandList = lists[(n % 6) as usize].1.cast().unwrap();
|
|
|
+ gpu.queue.ExecuteCommandLists(&[Some(list)]);
|
|
|
+ let _ = gpu.queue.Signal(&gpu.fence, n + 1);
|
|
|
+ sent.store(n + 1, SeqCst);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if started.elapsed().as_secs() >= seconds {
|
|
|
+ break false;
|
|
|
+ }
|
|
|
+ std::thread::sleep(std::time::Duration::from_micros(300));
|
|
|
+ };
|
|
|
+ stop.store(true, SeqCst);
|
|
|
+ lost
|
|
|
+ });
|
|
|
+ let sent = sent.into_inner();
|
|
|
+ let counts = counts.map(|c| c.into_inner());
|
|
|
+ let blocks = counts[0];
|
|
|
+ // SAFETY: a plain query.
|
|
|
+ let reason = unsafe { gpu.device.GetDeviceRemovedReason() };
|
|
|
+ println!(
|
|
|
+ "{blocks} blocks, {sent} lists in {:.1} s; making {:.0} us a block, the longest {}; waiting {:.0} us, the longest {}; {}",
|
|
|
+ started.elapsed().as_secs_f64(),
|
|
|
+ counts[1] as f64 / blocks.max(1) as f64,
|
|
|
+ counts[2],
|
|
|
+ counts[3] as f64 / blocks.max(1) as f64,
|
|
|
+ counts[4],
|
|
|
+ if lost { format!("LOST: {reason:?}") } else { "the device lives".to_owned() }
|
|
|
+ );
|
|
|
+ if !lost {
|
|
|
+ // SAFETY: waits for what was sent.
|
|
|
+ unsafe {
|
|
|
+ let _ = gpu.fence.SetEventOnCompletion(sent, gpu.event);
|
|
|
+ WaitForSingleObject(gpu.event, 10_000);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/// The clamp compiles with the system's compiler and the host takes it.
|
|
|
#[test]
|
|
|
fn argument_clamp_builds() {
|