Merge pull request #39 from linebender/cleanup

Minor cleanups
This commit is contained in:
Raph Levien 2020-11-18 15:59:13 -08:00 committed by GitHub
commit fdb80c0561
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 60 additions and 3 deletions

View file

@ -24,6 +24,7 @@ fn main() {
cmd_buf.write_timestamp(&query_pool, 0); cmd_buf.write_timestamp(&query_pool, 0);
cmd_buf.dispatch(&pipeline, &descriptor_set, (256, 1, 1)); cmd_buf.dispatch(&pipeline, &descriptor_set, (256, 1, 1));
cmd_buf.write_timestamp(&query_pool, 1); cmd_buf.write_timestamp(&query_pool, 1);
cmd_buf.host_barrier();
cmd_buf.finish(); cmd_buf.finish();
device device
.run_cmd_buf(&cmd_buf, &[], &[], Some(&fence)) .run_cmd_buf(&cmd_buf, &[], &[], Some(&fence))

View file

@ -100,8 +100,21 @@ pub trait CmdBuf<D: Device> {
size: (u32, u32, u32), size: (u32, u32, u32),
); );
/// Insert an execution and memory barrier.
///
/// Compute kernels (and other actions) after this barrier may read from buffers
/// that were written before this barrier.
unsafe fn memory_barrier(&mut self); unsafe fn memory_barrier(&mut self);
/// Insert a barrier for host access to buffers.
///
/// The host may read buffers written before this barrier, after the fence for
/// the command buffer is signaled.
///
/// See http://themaister.net/blog/2019/08/14/yet-another-blog-explaining-vulkan-synchronization/
/// ("Host memory reads") for an explanation of this barrier.
unsafe fn host_barrier(&mut self);
unsafe fn image_barrier( unsafe fn image_barrier(
&mut self, &mut self,
image: &D::Image, image: &D::Image,
@ -120,6 +133,8 @@ pub trait CmdBuf<D: Device> {
unsafe fn copy_image_to_buffer(&self, src: &D::Image, dst: &D::Buffer); unsafe fn copy_image_to_buffer(&self, src: &D::Image, dst: &D::Buffer);
unsafe fn copy_buffer_to_image(&self, src: &D::Buffer, dst: &D::Image);
// low portability, dx12 doesn't support it natively // low portability, dx12 doesn't support it natively
unsafe fn blit_image(&self, src: &D::Image, dst: &D::Image); unsafe fn blit_image(&self, src: &D::Image, dst: &D::Image);

View file

@ -707,7 +707,8 @@ impl crate::Device for VkDevice {
/// Run the command buffer. /// Run the command buffer.
/// ///
/// This version simply blocks until it's complete. /// This submits the command buffer for execution. The provided fence
/// is signalled when the execution is complete.
unsafe fn run_cmd_buf( unsafe fn run_cmd_buf(
&self, &self,
cmd_buf: &CmdBuf, cmd_buf: &CmdBuf,
@ -730,8 +731,8 @@ impl crate::Device for VkDevice {
&[vk::SubmitInfo::builder() &[vk::SubmitInfo::builder()
.command_buffers(&[cmd_buf.cmd_buf]) .command_buffers(&[cmd_buf.cmd_buf])
.wait_semaphores(wait_semaphores) .wait_semaphores(wait_semaphores)
.signal_semaphores(signal_semaphores)
.wait_dst_stage_mask(&wait_stages) .wait_dst_stage_mask(&wait_stages)
.signal_semaphores(signal_semaphores)
.build()], .build()],
fence, fence,
)?; )?;
@ -830,6 +831,22 @@ impl crate::CmdBuf<VkDevice> for CmdBuf {
); );
} }
unsafe fn host_barrier(&mut self) {
let device = &self.device.device;
device.cmd_pipeline_barrier(
self.cmd_buf,
vk::PipelineStageFlags::ALL_COMMANDS,
vk::PipelineStageFlags::HOST,
vk::DependencyFlags::empty(),
&[vk::MemoryBarrier::builder()
.src_access_mask(vk::AccessFlags::MEMORY_WRITE)
.dst_access_mask(vk::AccessFlags::HOST_READ)
.build()],
&[],
&[],
);
}
unsafe fn image_barrier( unsafe fn image_barrier(
&mut self, &mut self,
image: &Image, image: &Image,
@ -900,6 +917,29 @@ impl crate::CmdBuf<VkDevice> for CmdBuf {
); );
} }
unsafe fn copy_buffer_to_image(&self, src: &Buffer, dst: &Image) {
let device = &self.device.device;
device.cmd_copy_buffer_to_image(
self.cmd_buf,
src.buffer,
dst.image,
vk::ImageLayout::TRANSFER_DST_OPTIMAL,
&[vk::BufferImageCopy {
buffer_offset: 0,
buffer_row_length: 0, // tight packing
buffer_image_height: 0, // tight packing
image_subresource: vk::ImageSubresourceLayers {
aspect_mask: vk::ImageAspectFlags::COLOR,
mip_level: 0,
base_array_layer: 0,
layer_count: 1,
},
image_offset: vk::Offset3D { x: 0, y: 0, z: 0 },
image_extent: dst.extent,
}],
);
}
unsafe fn blit_image(&self, src: &Image, dst: &Image) { unsafe fn blit_image(&self, src: &Image, dst: &Image) {
let device = &self.device.device; let device = &self.device.device;
device.cmd_blit_image( device.cmd_blit_image(
@ -974,7 +1014,7 @@ impl VkSwapchain {
let (image_idx, _suboptimal) = self.swapchain_fn.acquire_next_image( let (image_idx, _suboptimal) = self.swapchain_fn.acquire_next_image(
self.swapchain, self.swapchain,
!0, !0,
self.acquisition_semaphores[self.acquisition_idx], acquisition_semaphore,
vk::Fence::null(), vk::Fence::null(),
)?; )?;
self.acquisition_idx = (self.acquisition_idx + 1) % self.acquisition_semaphores.len(); self.acquisition_idx = (self.acquisition_idx + 1) % self.acquisition_semaphores.len();

View file

@ -225,6 +225,7 @@ fn main() -> Result<(), Error> {
cmd_buf.begin(); cmd_buf.begin();
renderer.record(&mut cmd_buf, &query_pool); renderer.record(&mut cmd_buf, &query_pool);
cmd_buf.copy_image_to_buffer(&renderer.image_dev, &image_buf); cmd_buf.copy_image_to_buffer(&renderer.image_dev, &image_buf);
cmd_buf.host_barrier();
cmd_buf.finish(); cmd_buf.finish();
let start = std::time::Instant::now(); let start = std::time::Instant::now();
device.run_cmd_buf(&cmd_buf, &[], &[], Some(&fence))?; device.run_cmd_buf(&cmd_buf, &[], &[], Some(&fence))?;