use crate::gpu_d3d11::{ChainDescription, Changed};
use crate::gpu_windows::{function_in, inside, system_library};
use crate::layer_d3d11::{B8G8R8A8_UNORM, Box3, Com, function, release};
use crate::windows::named;
use crate::windows::sys::{HANDLE, RECT};
use std::cell::Cell;
use std::ffi::c_void;
use std::ptr::{null, null_mut};
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
use std::time::{Duration, Instant};
#[link(name = "kernel32")]
unsafe extern "system" {
fn CreateEventW(security: *const c_void, manual: i32, initial: i32, name: *const u16) -> HANDLE;
fn WaitForSingleObject(handle: usize, millis: u32) -> u32;
fn CloseHandle(handle: usize) -> i32;
}
type MakeDevice = unsafe extern "system" fn(Com, u32, *const [u32; 4], *mut Com) -> i32;
type MakeFactory = unsafe extern "system" fn(u32, *const [u32; 4], *mut Com) -> i32;
type Ask = unsafe extern "system" fn(Com, *const [u32; 4], *mut Com) -> i32;
const DEVICE: [u32; 4] = named(0x1898_19F1, 0x1DB6, 0x4B57, [0xBE, 0x54, 0x18, 0x21, 0x33, 0x9B, 0x85, 0xF7]);
const QUEUE: [u32; 4] = named(0x0EC8_70A6, 0x5D7E, 0x4C22, [0x8C, 0xFC, 0x5B, 0xAA, 0xE0, 0x76, 0x16, 0xED]);
const ALLOCATOR: [u32; 4] = named(0x6102_DEE4, 0xAF59, 0x4B09, [0xB9, 0x99, 0xB4, 0x4D, 0x73, 0xF0, 0x9B, 0x24]);
const LIST: [u32; 4] = named(0x5B16_0D0F, 0xAC1B, 0x4185, [0x8B, 0xA8, 0xB3, 0xAE, 0x42, 0xA5, 0xA4, 0x55]);
const LIST2: [u32; 4] = named(0x38C3_E585, 0xFF17, 0x412C, [0x91, 0x50, 0x4F, 0xC6, 0xF9, 0xD7, 0x2A, 0x28]);
const RESOURCE: [u32; 4] = named(0x6964_42BE, 0xA72E, 0x4059, [0xBC, 0x79, 0x5B, 0x5C, 0x98, 0x04, 0x0F, 0xAD]);
const FENCE: [u32; 4] = named(0x0A75_3DCF, 0xC4D8, 0x4B91, [0xAD, 0xF6, 0xBE, 0x5A, 0x60, 0xD9, 0x5A, 0x76]);
const FACTORY: [u32; 4] = named(0x1BC6_EA02, 0xEF36, 0x464F, [0xBF, 0x0C, 0x21, 0xCA, 0x39, 0xE5, 0x16, 0x8A]);
const CHAIN3: [u32; 4] = named(0x94D9_9BDB, 0xF1F8, 0x4AB0, [0xB2, 0x36, 0x7D, 0xA0, 0x17, 0x0E, 0xDA, 0xB1]);
const PRESENT: u32 = 0;
const COPY_DESTINATION: u32 = 0x400;
const COPY_SOURCE: u32 = 0x800;
const GENERIC_READ: u32 = 0xAC3;
const DEFAULT_HEAP: u32 = 1;
const UPLOAD_HEAP: u32 = 2;
const READBACK_HEAP: u32 = 3;
const ROW_ALIGNMENT: usize = 256;
#[repr(C)]
struct Heap {
kind: u32,
page: u32,
pool: u32,
creation_mask: u32,
visible_mask: u32,
}
#[repr(C)]
struct ResourceDescription {
dimension: u32,
alignment: u64,
width: u64,
height: u32,
depth: u16,
mip_levels: u16,
format: u32,
samples: [u32; 2],
layout: u32,
flags: u32,
}
#[repr(C)]
pub(crate) struct Place {
pub(crate) resource: Com,
pub(crate) kind: u32,
pub(crate) offset: u64,
pub(crate) footprint: [u32; 5],
}
#[repr(C)]
pub(crate) struct Barrier {
kind: u32,
flags: u32,
resource: Com,
subresource: u32,
before: u32,
after: u32,
}
pub(crate) fn barrier(resource: Com, before: u32, after: u32) -> Barrier {
Barrier { kind: 0, flags: 0, resource, subresource: 0xFFFF_FFFF, before, after }
}
fn committed(device: Com, kind: u32, described: &ResourceDescription, state: u32) -> Option<Com> {
let (heap, mut made) = (Heap { kind, page: 0, pool: 0, creation_mask: 0, visible_mask: 0 }, null_mut());
type Make = unsafe extern "system" fn(Com, *const Heap, u32, *const ResourceDescription, u32, *const c_void, *const [u32; 4], *mut Com) -> i32;
let result = unsafe { function::<Make>(device, 27)(device, &heap, 0, described, state, null(), &RESOURCE, &mut made) };
(result >= 0).then_some(made)
}
fn texture(device: &Device, size: [i32; 2]) -> Option<Com> {
let flags = drawn(device);
let described =
ResourceDescription { dimension: 3, alignment: 0, width: size[0] as u64, height: size[1] as u32, depth: 1, mip_levels: 1, format: B8G8R8A8_UNORM, samples: [1, 0], layout: 0, flags };
committed(device.device, DEFAULT_HEAP, &described, COPY_DESTINATION)
}
#[cfg(feature = "hdr")]
pub(crate) fn texture_of(device: Com, size: [i32; 2], format: u32) -> Option<Com> {
let described = ResourceDescription { dimension: 3, alignment: 0, width: size[0] as u64, height: size[1] as u32, depth: 1, mip_levels: 1, format, samples: [1, 0], layout: 0, flags: 0 };
committed(device, DEFAULT_HEAP, &described, COPY_DESTINATION)
}
#[inline]
fn drawn(device: &Device) -> u32 {
#[cfg(feature = "gpu-draw")]
if device.drawing.is_some() {
return crate::gpu_draw_d3d12::DRAWN_INTO;
}
let _ = device;
0
}
pub(crate) fn buffer(device: Com, kind: u32, bytes: usize, state: u32) -> Option<(Com, *mut u8)> {
let described = ResourceDescription { dimension: 1, alignment: 0, width: bytes as u64, height: 1, depth: 1, mip_levels: 1, format: 0, samples: [1, 0], layout: 1, flags: 0 };
let (made, mut there) = (committed(device, kind, &described, state)?, null_mut());
if unsafe { function::<unsafe extern "system" fn(Com, u32, *const [usize; 2], *mut *mut u8) -> i32>(made, 8)(made, 0, &[0, 0], &mut there) } < 0 {
unsafe { release(made) };
return None;
}
Some((made, there))
}
#[repr(C)]
struct Mark {
at: u64,
value: u32,
}
struct Marks {
list: Com,
buffer: Com,
seen: *const AtomicU32,
at: u64,
last: Cell<u32>,
}
static ADRIFT: AtomicBool = AtomicBool::new(false);
pub(crate) struct Device {
device: Com,
queue: Com,
allocator: Com,
list: Com,
fence: Com,
event: HANDLE,
signalled: Cell<u64>,
factory: Com,
marks: Option<Marks>,
#[cfg(feature = "gpu-draw")]
pub(crate) drawing: Option<crate::gpu_draw_d3d12::Pipeline>,
}
impl Device {
pub(crate) fn gpu() -> Option<Device> {
let make = function_in(system_library("d3d12.dll"), c"D3D12CreateDevice");
let factory = function_in(system_library("dxgi.dll"), c"CreateDXGIFactory2");
let (make, factory) = unsafe { (make != 0 && factory != 0).then(|| (std::mem::transmute::<usize, MakeDevice>(make), std::mem::transmute::<usize, MakeFactory>(factory)))? };
let mut made = Device {
device: null_mut(),
queue: null_mut(),
allocator: null_mut(),
list: null_mut(),
fence: null_mut(),
event: 0,
signalled: Cell::new(0),
factory: null_mut(),
marks: None,
#[cfg(feature = "gpu-draw")]
drawing: None,
};
if unsafe { factory(0, &FACTORY, &mut made.factory) } < 0 {
return None;
}
let software;
(made.device, software) = (0..16).find_map(|at| made.on_adapter(at, make))?;
if !made.with_queue() || (software && !made.with_marks()) {
return None;
}
#[cfg(feature = "gpu-draw")]
{
made.drawing = crate::gpu_draw_d3d12::Pipeline::new(made.device);
}
Some(made)
}
fn on_adapter(&self, at: u32, make: MakeDevice) -> Option<(Com, bool)> {
let (mut adapter, mut described, mut device) = (null_mut(), [0u64; 40], null_mut());
unsafe {
#[cfg(test)]
if crate::gpu_windows::tests::faked().software {
return tests::on_warp(self.factory, at, make).map(|device| (device, true));
}
if function::<unsafe extern "system" fn(Com, u32, *mut Com) -> i32>(self.factory, 12)(self.factory, at, &mut adapter) < 0 {
return None;
}
function::<unsafe extern "system" fn(Com, *mut u64) -> i32>(adapter, 10)(adapter, described.as_mut_ptr());
let software = described[32] as u32 == 0x1414 || described[38] as u32 & 2 != 0;
let made = (!software && make(adapter, 0xB000, &DEVICE, &mut device) >= 0).then_some((device, false));
release(adapter);
made
}
}
fn with_queue(&mut self) -> bool {
let (device, queue) = (self.device, [0u32; 4]);
type MakeAllocator = unsafe extern "system" fn(Com, u32, *const [u32; 4], *mut Com) -> i32;
type MakeList = unsafe extern "system" fn(Com, u32, u32, Com, Com, *const [u32; 4], *mut Com) -> i32;
type MakeFence = unsafe extern "system" fn(Com, u64, u32, *const [u32; 4], *mut Com) -> i32;
unsafe {
function::<unsafe extern "system" fn(Com, *const [u32; 4], *const [u32; 4], *mut Com) -> i32>(device, 8)(device, &queue, &QUEUE, &mut self.queue);
function::<MakeAllocator>(device, 9)(device, 0, &ALLOCATOR, &mut self.allocator);
if !self.allocator.is_null() {
function::<MakeList>(device, 12)(device, 0, 0, self.allocator, null_mut(), &LIST, &mut self.list);
}
if !self.list.is_null() {
function::<unsafe extern "system" fn(Com) -> i32>(self.list, 9)(self.list);
}
function::<MakeFence>(device, 36)(device, 0, 0, &FENCE, &mut self.fence);
self.event = CreateEventW(null(), 0, 0, null());
}
![self.queue, self.allocator, self.list, self.fence].iter().any(|made| made.is_null()) && self.event != 0
}
fn with_marks(&mut self) -> bool {
let Some((buffer, seen)) = buffer(self.device, READBACK_HEAP, 4, COPY_DESTINATION) else { return false };
let mut list = null_mut();
let at = unsafe {
function::<Ask>(self.list, 0)(self.list, &LIST2, &mut list);
function::<unsafe extern "system" fn(Com) -> u64>(buffer, 11)(buffer)
};
self.marks = Some(Marks { list, buffer, seen: seen.cast(), at, last: Cell::new(0) });
!list.is_null()
}
pub(crate) fn removed(&self) -> bool {
unsafe { function::<unsafe extern "system" fn(Com) -> i32>(self.device, 37)(self.device) != 0 }
}
fn open(&self) {
unsafe {
function::<unsafe extern "system" fn(Com) -> i32>(self.allocator, 8)(self.allocator);
function::<unsafe extern "system" fn(Com, Com, Com) -> i32>(self.list, 10)(self.list, self.allocator, null_mut());
}
}
fn run(&self) -> bool {
let next = self.marks.as_ref().map(|marks| self.mark(marks));
unsafe {
if function::<unsafe extern "system" fn(Com) -> i32>(self.list, 9)(self.list) < 0 {
return false;
}
function::<unsafe extern "system" fn(Com, u32, *const Com)>(self.queue, 10)(self.queue, 1, &self.list);
}
if let (Some(marks), Some(next)) = (&self.marks, next) {
marks.last.set(next);
}
self.run_nothing()
}
fn mark(&self, marks: &Marks) -> u32 {
let mark = Mark { at: marks.at, value: marks.last.get().wrapping_add(1) };
unsafe { function::<unsafe extern "system" fn(Com, u32, *const Mark, *const u32)>(marks.list, 66)(marks.list, 1, &mark, &2) };
mark.value
}
fn run_nothing(&self) -> bool {
self.signalled.set(self.signalled.get() + 1);
unsafe { function::<unsafe extern "system" fn(Com, Com, u64) -> i32>(self.queue, 14)(self.queue, self.fence, self.signalled.get()) >= 0 }
}
fn wait(&self) -> bool {
let wanted = self.signalled.get();
let done = || unsafe { function::<unsafe extern "system" fn(Com) -> u64>(self.fence, 8)(self.fence) };
if done() < wanted {
unsafe {
function::<unsafe extern "system" fn(Com, u64, HANDLE) -> i32>(self.fence, 9)(self.fence, wanted, self.event);
WaitForSingleObject(self.event as usize, 2000);
}
}
match done() {
u64::MAX => self.settled(),
done => done >= wanted,
}
}
fn settled(&self) -> bool {
let Some(marks) = &self.marks else { return true };
let start = Instant::now();
while unsafe { (*marks.seen).load(Ordering::Acquire) } != marks.last.get() {
if start.elapsed() > Duration::from_secs(10) {
ADRIFT.store(true, Ordering::Relaxed);
return false;
}
std::thread::sleep(Duration::from_millis(1));
}
true
}
}
unsafe impl Send for Device {}
impl Drop for Device {
fn drop(&mut self) {
if !self.queue.is_null() && !self.fence.is_null() && self.run_nothing() {
self.wait();
}
if ADRIFT.load(Ordering::Relaxed) {
#[cfg(feature = "gpu-draw")]
std::mem::forget(self.drawing.take());
return;
}
#[cfg(feature = "gpu-draw")]
drop(self.drawing.take());
let marks = self.marks.take().map_or([null_mut(); 2], |marks| [marks.list, marks.buffer]);
unsafe {
[marks[0], marks[1], self.list, self.allocator, self.fence, self.queue, self.device, self.factory].into_iter().for_each(|object| release(object));
if self.event != 0 {
CloseHandle(self.event as usize);
}
}
}
}
struct Waiting {
offset: usize,
pitch: u32,
at: [u32; 2],
size: [u32; 2],
}
pub(crate) struct Shown {
chain: Com,
picture: Com,
upload: Com,
written: *mut u8,
room: usize,
next: usize,
waiting: Vec<Waiting>,
size: [i32; 2],
#[cfg(feature = "hdr")]
pub(crate) hdr: crate::hdr_windows::Hdr,
#[cfg(feature = "hdr")]
pass: Option<Option<crate::hdr_windows::Pass12>>,
}
impl Shown {
pub(crate) fn new(device: &Device, hwnd: HANDLE, size: [i32; 2]) -> Option<Shown> {
let [width, height] = size.map(|side| side.max(1) as u32);
let described = ChainDescription { width, height, format: B8G8R8A8_UNORM, stereo: 0, sample_count: 1, sample_quality: 0, usage: 0x20, buffers: 2, scaling: 1, effect: 3, alpha: 3, flags: 0 };
let mut shown = Shown {
chain: null_mut(),
picture: null_mut(),
upload: null_mut(),
written: null_mut(),
room: 0,
next: 0,
waiting: Vec::new(),
size: [0; 2],
#[cfg(feature = "hdr")]
hdr: Default::default(),
#[cfg(feature = "hdr")]
pass: None,
};
type MakeChain = unsafe extern "system" fn(Com, Com, HANDLE, *const ChainDescription, *const c_void, Com, *mut Com) -> i32;
let mut chain = null_mut();
unsafe {
if function::<MakeChain>(device.factory, 15)(device.factory, device.queue, hwnd, &described, null(), null_mut(), &mut chain) < 0 {
return None;
}
function::<unsafe extern "system" fn(Com, HANDLE, u32) -> i32>(device.factory, 8)(device.factory, hwnd, 3);
function::<Ask>(chain, 0)(chain, &CHAIN3, &mut shown.chain);
release(chain);
}
(!shown.chain.is_null()).then_some(shown)
}
pub(crate) fn fit(&mut self, device: &Device, size: [i32; 2]) -> Option<bool> {
if size == self.size {
return Some(false);
}
if ADRIFT.load(Ordering::Relaxed) {
return None;
}
self.size = [0; 2];
self.waiting.clear();
unsafe {
[std::mem::replace(&mut self.picture, null_mut()), std::mem::replace(&mut self.upload, null_mut())].into_iter().for_each(|object| release(object));
if function::<unsafe extern "system" fn(Com, u32, u32, u32, u32, u32) -> i32>(self.chain, 13)(self.chain, 0, size[0] as u32, size[1] as u32, 0, 0) < 0 {
return None;
}
}
self.picture = texture(device, size)?;
self.room = (size[0] as usize * 4).next_multiple_of(ROW_ALIGNMENT) * size[1] as usize + 2 * ROW_ALIGNMENT;
(self.upload, self.written) = buffer(device.device, UPLOAD_HEAP, self.room, GENERIC_READ)?;
self.size = size;
Some(true)
}
pub(crate) unsafe fn upload(&mut self, device: &Device, at: [i32; 2], size: [i32; 2], first: *const u32, pitch: usize) {
let [right, bottom] = [(at[0] + size[0]).min(self.size[0]), (at[1] + size[1]).min(self.size[1])];
if self.upload.is_null() || at[0] < 0 || at[1] < 0 || right <= at[0] || bottom <= at[1] {
return;
}
let ([width, height], row) = ([right - at[0], bottom - at[1]].map(|side| side as usize), ((right - at[0]) as usize * 4).next_multiple_of(ROW_ALIGNMENT));
let mut offset = self.next.next_multiple_of(2 * ROW_ALIGNMENT);
if offset + row * height > self.room {
self.copy_waiting(device, None);
device.wait();
offset = 0;
}
for line in 0..height {
unsafe { std::ptr::copy_nonoverlapping(first.add(line * pitch).cast::<u8>(), self.written.add(offset + line * row), width * 4) };
}
self.next = offset + row * height;
self.waiting.push(Waiting { offset, pitch: row as u32, at: at.map(|side| side as u32), size: [width as u32, height as u32] });
}
fn copy_waiting(&mut self, device: &Device, then: Option<&dyn Fn(Com)>) -> bool {
device.open();
let list = device.list;
type CopyRegion = unsafe extern "system" fn(Com, *const Place, u32, u32, u32, *const Place, *const Box3);
for upload in self.waiting.drain(..) {
let to = Place { resource: self.picture, kind: 0, offset: 0, footprint: [0; 5] };
let from = Place { resource: self.upload, kind: 1, offset: upload.offset as u64, footprint: [B8G8R8A8_UNORM, upload.size[0], upload.size[1], 1, upload.pitch] };
unsafe { function::<CopyRegion>(list, 16)(list, &to, upload.at[0], upload.at[1], 0, &from, null()) };
}
self.next = 0;
if let Some(then) = then {
then(list);
}
device.run()
}
#[cfg(feature = "gpu-draw")]
pub(crate) fn draw(&mut self, device: &mut Device, list: &crate::gpu_draw::DrawList, area: [i32; 4]) -> bool {
let Some(mut drawing) = device.drawing.take() else { return false };
let (picture, size) = (self.picture, self.size);
let ready = !picture.is_null() && drawing.prepare(device.device, list);
let record = |commands: Com| drawing.record(device.device, commands, picture, size, list, area);
let drawn = ready && self.copy_waiting(device, Some(&record)) && device.wait();
if drawn {
drawing.ran();
}
device.drawing = Some(drawing);
drawn
}
pub(crate) fn present(&mut self, device: &Device, dirty: &[RECT]) -> bool {
if self.picture.is_null() {
return true;
}
#[cfg(feature = "hdr")]
if !self.go_hdr(device) {
return false;
}
let (dirty, mut back) = (inside(dirty, self.size), null_mut());
let changed = Changed { count: dirty.len() as u32, rects: dirty.as_ptr(), scroll: null(), offset: null() };
unsafe {
let index = function::<unsafe extern "system" fn(Com) -> u32>(self.chain, 36)(self.chain);
if function::<unsafe extern "system" fn(Com, u32, *const [u32; 4], *mut Com) -> i32>(self.chain, 9)(self.chain, index, &RESOURCE, &mut back) < 0 {
return false;
}
}
#[cfg(test)]
let kept = tests::Kept::new(device, back, self.size, self.chain, dirty);
#[cfg(feature = "hdr")]
let (pass, constants, size) = (self.lit(device), self.hdr.constants(), self.size);
let picture = self.picture;
let to_back = |list: Com| {
#[cfg(feature = "hdr")]
if let Some(pass) = &pass {
return pass.record(device.device, list, picture, back, size, constants);
}
let before = [barrier(picture, COPY_DESTINATION, COPY_SOURCE), barrier(back, PRESENT, COPY_DESTINATION)];
let after = [barrier(picture, COPY_SOURCE, COPY_DESTINATION), barrier(back, COPY_DESTINATION, PRESENT)];
type Barriers = unsafe extern "system" fn(Com, u32, *const Barrier);
unsafe {
function::<Barriers>(list, 26)(list, 2, before.as_ptr());
function::<unsafe extern "system" fn(Com, Com, Com)>(list, 17)(list, back, picture);
#[cfg(test)]
kept.record(list);
function::<Barriers>(list, 26)(list, 2, after.as_ptr());
}
};
let taken = self.copy_waiting(device, Some(&to_back));
#[cfg(test)]
tests::remove_if_asked(device);
let presented = unsafe {
let presented = function::<unsafe extern "system" fn(Com, u32, u32, *const Changed) -> i32>(self.chain, 22)(self.chain, 1, 0, &changed);
release(back);
presented
};
let done = device.run_nothing() && device.wait();
#[cfg(test)]
kept.read();
#[cfg(feature = "hdr")]
self.lit_ran(pass);
taken && presented >= 0 && done
}
#[cfg(feature = "hdr")]
fn go_hdr(&mut self, device: &Device) -> bool {
if self.hdr.wanted() && self.pass.is_none() {
self.pass = Some(crate::hdr_windows::Pass12::new(device.device));
}
self.hdr.switch(self.chain, self.size, self.pass.as_ref().is_some_and(Option::is_some))
}
#[cfg(feature = "hdr")]
fn lit(&mut self, device: &Device) -> Option<crate::hdr_windows::Pass12> {
if !self.hdr.on {
return None;
}
let mut pass = self.pass.as_mut()?.take()?;
if pass.prepare(device.device, &mut self.hdr.gains) {
return Some(pass);
}
self.pass = Some(Some(pass));
None
}
#[cfg(feature = "hdr")]
fn lit_ran(&mut self, pass: Option<crate::hdr_windows::Pass12>) {
let Some(mut pass) = pass else { return };
pass.ran();
#[cfg(test)]
crate::hdr_windows::tests::read_kept();
self.pass = Some(Some(pass));
}
}
impl Drop for Shown {
fn drop(&mut self) {
if ADRIFT.load(Ordering::Relaxed) {
return;
}
unsafe { [self.upload, self.picture, self.chain].into_iter().for_each(|object| release(object)) };
}
}
#[cfg(test)]
pub(crate) mod tests {
use super::*;
#[cfg(feature = "gpu-draw")]
impl Device {
pub(crate) fn com(&self) -> Com {
self.device
}
}
pub(crate) fn warp() -> Device {
crate::gpu_windows::tests::fake(crate::gpu_windows::tests::Faked { software: true, ..Default::default() });
let device = Device::gpu().expect("Direct3D 12 on WARP");
crate::gpu_windows::tests::fake(Default::default());
device
}
#[cfg(feature = "gpu-draw")]
pub(crate) fn drawn_on_warp(device: &Device, size: [i32; 2], under: &[u32], record: impl FnOnce(Com, Com, Com)) -> Vec<u32> {
let ([width, height], pitch) = (size.map(|side| side as usize), (size[0] as usize * 4).next_multiple_of(ROW_ALIGNMENT));
let picture = texture(device, size).expect("a texture");
let (upload, written) = buffer(device.device, UPLOAD_HEAP, pitch * height, GENERIC_READ).expect("an upload buffer");
let (readback, read) = buffer(device.device, 3, pitch * height, COPY_DESTINATION).expect("a buffer to read back");
for row in 0..height {
unsafe { std::ptr::copy_nonoverlapping(under[row * width..].as_ptr().cast::<u8>(), written.add(row * pitch), width * 4) };
}
let footprint = [B8G8R8A8_UNORM, width as u32, height as u32, 1, pitch as u32];
let whole = Place { resource: picture, kind: 0, offset: 0, footprint: [0; 5] };
type CopyRegion = unsafe extern "system" fn(Com, *const Place, u32, u32, u32, *const Place, *const Box3);
device.open();
unsafe { function::<CopyRegion>(device.list, 16)(device.list, &whole, 0, 0, 0, &Place { resource: upload, kind: 1, offset: 0, footprint }, null()) };
record(device.device, device.list, picture);
unsafe {
function::<unsafe extern "system" fn(Com, u32, *const Barrier)>(device.list, 26)(device.list, 1, &barrier(picture, COPY_DESTINATION, COPY_SOURCE));
function::<CopyRegion>(device.list, 16)(device.list, &Place { resource: readback, kind: 1, offset: 0, footprint }, 0, 0, 0, &whole, null());
}
assert!(device.run() && device.wait(), "the commands ran");
let rows = (0..height).map(|row| unsafe { std::slice::from_raw_parts(read.add(row * pitch).cast::<u32>(), width) });
let pixels = rows.flatten().copied().collect();
unsafe { [readback, upload, picture].into_iter().for_each(|object| release(object)) };
pixels
}
pub(crate) fn on_warp(factory: Com, at: u32, make: MakeDevice) -> Option<Com> {
let iid = named(0x2903_8F61, 0x3839, 0x4626, [0x91, 0xFD, 0x08, 0x68, 0x79, 0x01, 0x1A, 0x05]);
let (mut adapter, mut device) = (null_mut(), null_mut());
unsafe {
function::<Ask>(factory, 27)(factory, &iid, &mut adapter);
let made = (at == 0 && !adapter.is_null() && make(adapter, 0xB000, &DEVICE, &mut device) >= 0).then_some(device);
release(adapter);
made
}
}
pub(crate) fn remove_if_asked(device: &Device) {
let mut faked = crate::gpu_windows::tests::faked();
if !std::mem::take(&mut faked.remove) {
return;
}
crate::gpu_windows::tests::fake(faked);
remove(device);
}
fn remove(device: &Device) {
let iid = named(0x8B4F_173B, 0x2FEA, 0x4B80, [0x8F, 0x58, 0x43, 0x07, 0x19, 0x1A, 0xB9, 0x5D]);
let mut five = null_mut();
unsafe {
assert!(function::<Ask>(device.device, 0)(device.device, &iid, &mut five) >= 0, "no ID3D12Device5");
function::<unsafe extern "system" fn(Com)>(five, 58)(five);
release(five);
}
}
#[test]
fn a_removed_software_device_is_waited_for_until_it_has_run_all_it_was_given() {
crate::windows::tests::on_a_desktop_of_its_own("gpu_d3d12::tests::alone_a_removed_software_device_is_waited_for");
}
#[test]
#[ignore = "run by the test before it, in a process of its own"]
fn alone_a_removed_software_device_is_waited_for() {
if std::env::var_os(crate::windows::tests::ALONE).is_none() {
return;
}
let device = warp();
let (from, to) = (texture(&device, [4096, 4096]).expect("a texture"), texture(&device, [4096, 4096]).expect("a texture"));
device.open();
unsafe {
function::<unsafe extern "system" fn(Com, u32, *const Barrier)>(device.list, 26)(device.list, 1, &barrier(from, COPY_DESTINATION, COPY_SOURCE));
(0..8).for_each(|_| function::<unsafe extern "system" fn(Com, Com, Com)>(device.list, 17)(device.list, to, from));
}
assert!(device.run(), "the copies were taken");
remove(&device);
let fence = unsafe { function::<unsafe extern "system" fn(Com) -> u64>(device.fence, 8)(device.fence) };
let marks = device.marks.as_ref().expect("a software device keeps marks");
let ran = || unsafe { (*marks.seen).load(Ordering::Acquire) };
println!("at the removal: the fence {fence:#x}, lists run {} of {}", ran(), marks.last.get());
assert_eq!(fence, u64::MAX);
assert!(device.wait(), "the device was waited for");
assert_eq!(ran(), marks.last.get());
unsafe { [to, from].into_iter().for_each(|object| release(object)) };
}
pub(crate) struct Kept {
back: Com,
buffer: Option<(Com, *mut u8)>,
size: [i32; 2],
hwnd: HANDLE,
dirty: Vec<RECT>,
}
impl Kept {
pub(crate) fn new(device: &Device, back: Com, size: [i32; 2], chain: Com, dirty: &[RECT]) -> Kept {
let mut hwnd = 0;
unsafe { function::<unsafe extern "system" fn(Com, *mut HANDLE) -> i32>(chain, 20)(chain, &mut hwnd) };
let pitch = (size[0] as usize * 4).next_multiple_of(ROW_ALIGNMENT);
let buffer = crate::gpu_windows::tests::faked().keep.then(|| buffer(device.device, 3, pitch * size[1] as usize, COPY_DESTINATION).expect("no buffer to read back"));
Kept { back, buffer, size, hwnd, dirty: dirty.to_vec() }
}
pub(crate) fn record(&self, list: Com) {
let Some((buffer, _)) = self.buffer else { return };
let pitch = (self.size[0] as u32 * 4).next_multiple_of(ROW_ALIGNMENT as u32);
let to = Place { resource: buffer, kind: 1, offset: 0, footprint: [B8G8R8A8_UNORM, self.size[0] as u32, self.size[1] as u32, 1, pitch] };
let from = Place { resource: self.back, kind: 0, offset: 0, footprint: [0; 5] };
type Barriers = unsafe extern "system" fn(Com, u32, *const Barrier);
unsafe {
function::<Barriers>(list, 26)(list, 1, &barrier(self.back, COPY_DESTINATION, COPY_SOURCE));
function::<unsafe extern "system" fn(Com, *const Place, u32, u32, u32, *const Place, *const Box3)>(list, 16)(list, &to, 0, 0, 0, &from, null());
function::<Barriers>(list, 26)(list, 1, &barrier(self.back, COPY_SOURCE, COPY_DESTINATION));
}
}
pub(crate) fn read(self) {
let Some((buffer, there)) = self.buffer else { return };
let ([width, height], pitch) = (self.size.map(|side| side as usize), (self.size[0] as usize * 4).next_multiple_of(ROW_ALIGNMENT));
let rows = (0..height).map(|row| unsafe { std::slice::from_raw_parts(there.add(row * pitch).cast::<u32>(), width) });
let pixels = rows.flatten().copied().collect();
unsafe { release(buffer) };
crate::gpu_windows::tests::kept(self.hwnd, width, pixels, &self.dirty);
}
}
}