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)?; let query_pool = device.create_query_pool(2)?;
unsafe { unsafe {
let img = device.create_image2d(256, 1)?; 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 pipeline = device.create_simple_compute_pipeline(SHADER_CODE, 1, 1)?;
let ds = device.create_descriptor_set(&pipeline, &[&dev_buf], &[&img])?; let ds = device.create_descriptor_set(&pipeline, &[&dev_buf], &[&img])?;
let mut cmd_buf = device.create_cmd_buf()?; let mut cmd_buf = device.create_cmd_buf()?;
@ -86,10 +86,10 @@ fn toy() -> Result<(), Error> {
cmd_buf.finish_timestamps(&query_pool); cmd_buf.finish_timestamps(&query_pool);
cmd_buf.host_barrier(); cmd_buf.host_barrier();
cmd_buf.finish(); cmd_buf.finish();
device.run_cmd_buf(&cmd_buf, &[], &[], Some(&fence))?; device.run_cmd_bufs(&[&cmd_buf], &[], &[], Some(&fence))?;
device.wait_and_reset(&[fence])?; device.wait_and_reset(&[fence])?;
let mut readback: Vec<u32> = Vec::new(); let mut readback: Vec<u32> = vec![0u32; 256];
device.read_buffer(&buf, &mut readback)?; device.read_buffer(&buf, readback.as_mut_ptr() as *mut u8, 0, 1024)?;
println!("{:?}", readback); println!("{:?}", readback);
println!("{:?}", device.fetch_query_pool(&query_pool)); 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> { unsafe fn fetch_query_pool(&self, pool: &Self::QueryPool) -> Result<Vec<f64>, Error> {
let mut buf = vec![0u64; pool.n_queries as usize]; 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 ts0 = buf[0];
let tsp = (self.ts_freq as f64).recip(); let tsp = (self.ts_freq as f64).recip();
let result = buf[1..] let result = buf[1..]
@ -254,16 +259,20 @@ impl crate::Device for Dx12Device {
Ok(result) Ok(result)
} }
unsafe fn run_cmd_buf( unsafe fn run_cmd_bufs(
&self, &self,
cmd_buf: &Self::CmdBuf, cmd_bufs: &[&Self::CmdBuf],
wait_semaphores: &[Self::Semaphore], wait_semaphores: &[Self::Semaphore],
signal_semaphores: &[Self::Semaphore], signal_semaphores: &[Self::Semaphore],
fence: Option<&Self::Fence>, fence: Option<&Self::Fence>,
) -> Result<(), Error> { ) -> Result<(), Error> {
// TODO: handle semaphores // TODO: handle semaphores
self.command_queue // SmallVec?
.execute_command_lists(&[cmd_buf.0.as_raw_list()]); 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 { if let Some(fence) = fence {
let val = fence.val.get() + 1; let val = fence.val.get() + 1;
fence.val.set(val); fence.val.set(val);
@ -273,28 +282,29 @@ impl crate::Device for Dx12Device {
Ok(()) Ok(())
} }
unsafe fn read_buffer<T: Sized>( unsafe fn read_buffer(
&self, &self,
buffer: &Self::Buffer, buffer: &Self::Buffer,
result: &mut Vec<T>, dst: *mut u8,
offset: u64,
size: u64,
) -> Result<(), Error> { ) -> Result<(), Error> {
let len = buffer.size as usize / std::mem::size_of::<T>(); buffer
if len > result.len() { .resource
result.reserve(len - result.len()); .read_resource(dst, offset as usize, size as usize)?;
}
buffer.resource.read_resource(result.as_mut_ptr(), len)?;
result.set_len(len);
Ok(()) Ok(())
} }
unsafe fn write_buffer<T: Sized>( unsafe fn write_buffer(
&self, &self,
buffer: &Self::Buffer, buffer: &Self::Buffer,
contents: &[T], contents: *const u8,
offset: u64,
size: u64,
) -> Result<(), Error> { ) -> Result<(), Error> {
let len = buffer.size as usize / std::mem::size_of::<T>(); buffer
assert!(len >= contents.len()); .resource
buffer.resource.write_resource(len, contents.as_ptr())?; .write_resource(contents, offset as usize, size as usize)?;
Ok(()) Ok(())
} }

View file

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