Fixup merge of staging changes

Update the backend to reflect trait changes.
This commit is contained in:
Raph Levien 2021-05-24 15:38:31 -07:00
parent dfac2148a9
commit 6ac46340e6
3 changed files with 56 additions and 34 deletions

View file

@ -69,7 +69,7 @@ fn toy() -> Result<(), Error> {
let query_pool = device.create_query_pool(2)?;
unsafe {
let img = device.create_image2d(256, 1)?;
device.write_buffer(&buf, &data)?;
device.write_buffer(&buf, data.as_ptr() as *const u8, 0, 1024)?;
let pipeline = device.create_simple_compute_pipeline(SHADER_CODE, 1, 1)?;
let ds = device.create_descriptor_set(&pipeline, &[&dev_buf], &[&img])?;
let mut cmd_buf = device.create_cmd_buf()?;
@ -86,10 +86,10 @@ fn toy() -> Result<(), Error> {
cmd_buf.finish_timestamps(&query_pool);
cmd_buf.host_barrier();
cmd_buf.finish();
device.run_cmd_buf(&cmd_buf, &[], &[], Some(&fence))?;
device.run_cmd_bufs(&[&cmd_buf], &[], &[], Some(&fence))?;
device.wait_and_reset(&[fence])?;
let mut readback: Vec<u32> = Vec::new();
device.read_buffer(&buf, &mut readback)?;
let mut readback: Vec<u32> = vec![0u32; 256];
device.read_buffer(&buf, readback.as_mut_ptr() as *mut u8, 0, 1024)?;
println!("{:?}", readback);
println!("{:?}", device.fetch_query_pool(&query_pool));
}

View file

@ -244,7 +244,12 @@ impl crate::Device for Dx12Device {
unsafe fn fetch_query_pool(&self, pool: &Self::QueryPool) -> Result<Vec<f64>, Error> {
let mut buf = vec![0u64; pool.n_queries as usize];
self.read_buffer(&pool.buf, &mut buf)?;
self.read_buffer(
&pool.buf,
buf.as_mut_ptr() as *mut u8,
0,
mem::size_of_val(buf.as_slice()) as u64,
)?;
let ts0 = buf[0];
let tsp = (self.ts_freq as f64).recip();
let result = buf[1..]
@ -254,16 +259,20 @@ impl crate::Device for Dx12Device {
Ok(result)
}
unsafe fn run_cmd_buf(
unsafe fn run_cmd_bufs(
&self,
cmd_buf: &Self::CmdBuf,
cmd_bufs: &[&Self::CmdBuf],
wait_semaphores: &[Self::Semaphore],
signal_semaphores: &[Self::Semaphore],
fence: Option<&Self::Fence>,
) -> Result<(), Error> {
// TODO: handle semaphores
self.command_queue
.execute_command_lists(&[cmd_buf.0.as_raw_list()]);
// SmallVec?
let lists = cmd_bufs
.iter()
.map(|c| c.0.as_raw_command_list())
.collect::<Vec<_>>();
self.command_queue.execute_command_lists(&lists);
if let Some(fence) = fence {
let val = fence.val.get() + 1;
fence.val.set(val);
@ -273,28 +282,29 @@ impl crate::Device for Dx12Device {
Ok(())
}
unsafe fn read_buffer<T: Sized>(
unsafe fn read_buffer(
&self,
buffer: &Self::Buffer,
result: &mut Vec<T>,
dst: *mut u8,
offset: u64,
size: u64,
) -> Result<(), Error> {
let len = buffer.size as usize / std::mem::size_of::<T>();
if len > result.len() {
result.reserve(len - result.len());
}
buffer.resource.read_resource(result.as_mut_ptr(), len)?;
result.set_len(len);
buffer
.resource
.read_resource(dst, offset as usize, size as usize)?;
Ok(())
}
unsafe fn write_buffer<T: Sized>(
unsafe fn write_buffer(
&self,
buffer: &Self::Buffer,
contents: &[T],
contents: *const u8,
offset: u64,
size: u64,
) -> Result<(), Error> {
let len = buffer.size as usize / std::mem::size_of::<T>();
assert!(len >= contents.len());
buffer.resource.write_resource(len, contents.as_ptr())?;
buffer
.resource
.write_resource(contents, offset as usize, size as usize)?;
Ok(())
}

View file

@ -120,34 +120,46 @@ impl Resource {
self.ptr.store(ptr::null_mut(), Ordering::Relaxed);
}
pub unsafe fn write_resource<T>(&self, count: usize, data: *const T) -> Result<(), Error> {
let mut mapped_memory = ptr::null_mut();
pub unsafe fn write_resource(
&self,
data: *const u8,
offset: usize,
size: usize,
) -> Result<(), Error> {
let mut mapped_memory: *mut u8 = ptr::null_mut();
let zero_range = d3d12::D3D12_RANGE { ..mem::zeroed() };
let range = d3d12::D3D12_RANGE {
Begin: offset,
End: offset + size,
};
explain_error(
(*self.get()).Map(0, &zero_range, &mut mapped_memory as *mut _ as *mut _),
"could not map GPU mem to CPU mem",
)?;
ptr::copy_nonoverlapping(data, mapped_memory, count);
(*self.get()).Unmap(0, ptr::null());
ptr::copy_nonoverlapping(data, mapped_memory.add(offset), size);
(*self.get()).Unmap(0, &range);
Ok(())
}
pub unsafe fn read_resource<T>(&self, dst: *mut T, count: usize) -> Result<(), Error> {
let mut mapped_memory = ptr::null_mut();
let n_bytes = count * std::mem::size_of::<T>();
pub unsafe fn read_resource(
&self,
dst: *mut u8,
offset: usize,
size: usize,
) -> Result<(), Error> {
let mut mapped_memory: *mut u8 = ptr::null_mut();
let range = d3d12::D3D12_RANGE {
Begin: 0,
End: n_bytes,
Begin: offset,
End: offset + size,
};
let zero_range = d3d12::D3D12_RANGE { ..mem::zeroed() };
explain_error(
(*self.get()).Map(0, &range, &mut mapped_memory as *mut _ as *mut _),
"could not map GPU mem to CPU mem",
)?;
ptr::copy_nonoverlapping(mapped_memory as *const T, dst, count);
ptr::copy_nonoverlapping(mapped_memory.add(offset), dst, size);
(*self.get()).Unmap(0, &zero_range);
Ok(())
}
@ -1163,7 +1175,7 @@ impl Drop for Event {
}
impl GraphicsCommandList {
pub unsafe fn as_raw_list(&self) -> *mut d3d12::ID3D12CommandList {
pub unsafe fn as_raw_command_list(&self) -> *mut d3d12::ID3D12CommandList {
self.0.as_raw() as *mut d3d12::ID3D12CommandList
}