From d8979cad4ab45d3261a92e513635fae9657e4f12 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Mon, 14 Jun 2021 09:51:25 -0400 Subject: [PATCH] hal/vk: command pools, clippy warnings --- wgpu-core/src/hub.rs | 19 ++++++----- wgpu-core/src/instance.rs | 26 +++++++-------- wgpu-core/src/lib.rs | 2 +- wgpu-hal/src/lib.rs | 9 ++++++ wgpu-hal/src/vulkan/adapter.rs | 9 +++--- wgpu-hal/src/vulkan/command.rs | 57 ++++++++++++++++++++++++++++++--- wgpu-hal/src/vulkan/device.rs | 16 +++++++-- wgpu-hal/src/vulkan/instance.rs | 3 +- wgpu-hal/src/vulkan/mod.rs | 24 +++++++------- 9 files changed, 116 insertions(+), 49 deletions(-) diff --git a/wgpu-core/src/hub.rs b/wgpu-core/src/hub.rs index 8d056232d..bb44cf2a8 100644 --- a/wgpu-core/src/hub.rs +++ b/wgpu-core/src/hub.rs @@ -716,15 +716,15 @@ impl Hub { pub struct Hubs { #[cfg(vulkan)] - vulkan: Hub, + vulkan: Hub, #[cfg(metal)] metal: Hub, #[cfg(dx12)] - dx12: Hub, + dx12: Hub, #[cfg(dx11)] - dx11: Hub, + dx11: Hub, #[cfg(gl)] - gl: Hub, + gl: Hub, } impl Hubs { @@ -811,9 +811,8 @@ pub trait HalApi: hal::Api { fn get_surface_mut(surface: &mut Surface) -> &mut Self::Surface; } -/* #[cfg(vulkan)] -impl HalApi for backend::Vulkan { +impl HalApi for hal::api::Vulkan { const VARIANT: Backend = Backend::Vulkan; fn hub(global: &Global) -> &Hub { &global.hubs.vulkan @@ -821,7 +820,7 @@ impl HalApi for backend::Vulkan { fn get_surface_mut(surface: &mut Surface) -> &mut Self::Surface { surface.vulkan.as_mut().unwrap() } -}*/ +} #[cfg(metal)] impl HalApi for hal::api::Metal { @@ -836,7 +835,7 @@ impl HalApi for hal::api::Metal { /* #[cfg(dx12)] -impl HalApi for backend::Dx12 { +impl HalApi for hal::api::Dx12 { const VARIANT: Backend = Backend::Dx12; fn hub(global: &Global) -> &Hub { &global.hubs.dx12 @@ -847,7 +846,7 @@ impl HalApi for backend::Dx12 { } #[cfg(dx11)] -impl HalApi for backend::Dx11 { +impl HalApi for hal::api::Dx11 { const VARIANT: Backend = Backend::Dx11; fn hub(global: &Global) -> &Hub { &global.hubs.dx11 @@ -858,7 +857,7 @@ impl HalApi for backend::Dx11 { } #[cfg(gl)] -impl HalApi for backend::Gl { +impl HalApi for hal::api::Gles { const VARIANT: Backend = Backend::Gl; fn hub(global: &Global) -> &Hub { &global.hubs.gl diff --git a/wgpu-core/src/instance.rs b/wgpu-core/src/instance.rs index 9026ae71b..b793b3d87 100644 --- a/wgpu-core/src/instance.rs +++ b/wgpu-core/src/instance.rs @@ -22,15 +22,15 @@ pub struct Instance { #[allow(dead_code)] name: String, #[cfg(vulkan)] - pub vulkan: Option, + pub vulkan: Option>, #[cfg(metal)] pub metal: Option>, #[cfg(dx12)] - pub dx12: Option, + pub dx12: Option>, #[cfg(dx11)] - pub dx11: Option, + pub dx11: Option>, #[cfg(gl)] - pub gl: Option, + pub gl: Option>, } impl Instance { @@ -55,15 +55,15 @@ impl Instance { Self { name: name.to_string(), #[cfg(vulkan)] - vulkan: map((Backend::Vulkan)), + vulkan: map(Backend::Vulkan), #[cfg(metal)] metal: map(Backend::Metal), #[cfg(dx12)] - dx12: map((Backend::Dx12)), + dx12: map(Backend::Dx12), #[cfg(dx11)] - dx11: map((Backend::Dx11)), + dx11: map(Backend::Dx11), #[cfg(gl)] - gl: map((Backend::Gl)), + gl: map(Backend::Gl), } } @@ -101,7 +101,7 @@ pub struct Surface { #[cfg(dx11)] pub dx11: Option>, #[cfg(gl)] - pub gl: Option>, + pub gl: Option>, } impl crate::hub::Resource for Surface { @@ -489,7 +489,7 @@ impl Global { // being weird with lifetimes for closure literals... #[cfg(vulkan)] let adapters_vk = map((&instance.vulkan, &id_vulkan, { - fn surface_vulkan(surf: &Surface) -> Option<&HalSurface> { + fn surface_vulkan(surf: &Surface) -> Option<&HalSurface> { surf.vulkan.as_ref() } surface_vulkan @@ -503,21 +503,21 @@ impl Global { })); #[cfg(dx12)] let adapters_dx12 = map((&instance.dx12, &id_dx12, { - fn surface_dx12(surf: &Surface) -> Option<&HalSurface> { + fn surface_dx12(surf: &Surface) -> Option<&HalSurface> { surf.dx12.as_ref() } surface_dx12 })); #[cfg(dx11)] let adapters_dx11 = map((&instance.dx11, &id_dx11, { - fn surface_dx11(surf: &Surface) -> Option<&HalSurface> { + fn surface_dx11(surf: &Surface) -> Option<&HalSurface> { surf.dx11.as_ref() } surface_dx11 })); #[cfg(gl)] let adapters_gl = map((&instance.gl, &id_gl, { - fn surface_gl(surf: &Surface) -> Option<&HalSurface> { + fn surface_gl(surf: &Surface) -> Option<&HalSurface> { surf.gl.as_ref() } surface_gl diff --git a/wgpu-core/src/lib.rs b/wgpu-core/src/lib.rs index c3b85d2d9..48e08bc2d 100644 --- a/wgpu-core/src/lib.rs +++ b/wgpu-core/src/lib.rs @@ -235,7 +235,7 @@ macro_rules! gfx_select { // macro so we must specify their equivalents manually match $id.backend() { #[cfg(all(not(target_arch = "wasm32"), not(target_os = "ios"), not(target_os = "macos")))] - wgt::Backend::Vulkan => $global.$method::<$crate::backend::Vulkan>( $($param),* ), + wgt::Backend::Vulkan => $global.$method::<$crate::api::Vulkan>( $($param),* ), #[cfg(all(not(target_arch = "wasm32"), any(target_os = "ios", target_os = "macos")))] wgt::Backend::Metal => $global.$method::<$crate::api::Metal>( $($param),* ), /* diff --git a/wgpu-hal/src/lib.rs b/wgpu-hal/src/lib.rs index 15ce59a30..7cf2aae5c 100644 --- a/wgpu-hal/src/lib.rs +++ b/wgpu-hal/src/lib.rs @@ -31,6 +31,8 @@ clippy::new_without_default, // Matches are good and extendable, no need to make an exception here. clippy::single_match, + // Push commands are more regular than macros. + clippy::vec_init_then_push, // TODO! clippy::missing_safety_doc, )] @@ -54,6 +56,8 @@ pub mod api { pub use super::empty::Api as Empty; #[cfg(feature = "metal")] pub use super::metal::Api as Metal; + #[cfg(feature = "vulkan")] + pub use super::vulkan::Api as Vulkan; } use std::{ @@ -300,6 +304,11 @@ pub trait Queue: Send + Sync { ) -> Result<(), SurfaceError>; } +/// Allocator for commands, encoded by command buffers. +/// Has the semantics close to DX12: +/// - unlimited allocation +/// - only one command buffer can be recording at a time +/// - clears are needed when nothing is recording or in flight pub trait CommandPool: Send + Sync { unsafe fn allocate( &mut self, diff --git a/wgpu-hal/src/vulkan/adapter.rs b/wgpu-hal/src/vulkan/adapter.rs index 2998eb926..12e01b7ad 100644 --- a/wgpu-hal/src/vulkan/adapter.rs +++ b/wgpu-hal/src/vulkan/adapter.rs @@ -463,9 +463,9 @@ impl super::InstanceShared { /// # Safety /// `T` must be a struct bigger than `vk::BaseOutStructure`. unsafe fn null_p_next(features: &mut Option) { - if let Some(features) = features { + if let Some(ref mut features) = *features { // This is technically invalid since `vk::BaseOutStructure` and `T` will probably never have the same size. - mem::transmute::<_, &mut vk::BaseOutStructure>(features).p_next = ptr::null_mut(); + (*(features as *mut T as *mut vk::BaseOutStructure)).p_next = ptr::null_mut(); } } @@ -706,10 +706,11 @@ impl crate::Adapter for super::Adapter { } }; + let family_index = 0; //TODO let queue = super::Queue { - //TODO: make this nicer - raw: raw_device.get_device_queue(0, 0), + raw: raw_device.get_device_queue(family_index, 0), swapchain_fn, + family_index, }; log::info!("Private capabilities: {:?}", self.private_caps); diff --git a/wgpu-hal/src/vulkan/command.rs b/wgpu-hal/src/vulkan/command.rs index 8d5a7cbf8..de31e51fd 100644 --- a/wgpu-hal/src/vulkan/command.rs +++ b/wgpu-hal/src/vulkan/command.rs @@ -1,8 +1,57 @@ -use super::Resource; -use std::ops::Range; //TEMP +use std::{ops::Range, sync::Arc}; -impl crate::CommandBuffer for super::Encoder { - unsafe fn finish(&mut self) {} +use ash::{version::DeviceV1_0, vk}; + +use super::Resource; // TEMP + +const ALLOCATION_GRANULARITY: u32 = 16; + +impl crate::CommandPool for super::CommandPool { + unsafe fn allocate( + &mut self, + desc: &crate::CommandBufferDescriptor, + ) -> Result { + if self.free.is_empty() { + let vk_info = vk::CommandBufferAllocateInfo::builder() + .command_buffer_count(ALLOCATION_GRANULARITY) + .build(); + let cmd_buf_vec = self.device.raw.allocate_command_buffers(&vk_info)?; + self.free.extend(cmd_buf_vec); + } + let raw = self.free.pop().unwrap(); + + let vk_info = vk::CommandBufferBeginInfo::builder() + .flags(vk::CommandBufferUsageFlags::ONE_TIME_SUBMIT) + .build(); + self.device.raw.begin_command_buffer(raw, &vk_info)?; + + Ok(super::CommandBuffer { + raw, + device: Arc::clone(&self.device), + }) + } + + unsafe fn free(&mut self, cmd_buf: super::CommandBuffer) { + let raw = cmd_buf.raw; + let _ = self + .device + .raw + .reset_command_buffer(raw, vk::CommandBufferResetFlags::empty()); + self.free.push(raw); + } + + unsafe fn clear(&mut self) { + let _ = self + .device + .raw + .reset_command_pool(self.raw, vk::CommandPoolResetFlags::RELEASE_RESOURCES); + } +} + +impl crate::CommandBuffer for super::CommandBuffer { + unsafe fn finish(&mut self) { + self.device.raw.end_command_buffer(self.raw).unwrap(); + } unsafe fn transition_buffers<'a, T>(&mut self, barriers: T) where diff --git a/wgpu-hal/src/vulkan/device.rs b/wgpu-hal/src/vulkan/device.rs index a9c6d1de3..b835a9071 100644 --- a/wgpu-hal/src/vulkan/device.rs +++ b/wgpu-hal/src/vulkan/device.rs @@ -608,9 +608,21 @@ impl crate::Device for super::Device { &self, desc: &crate::CommandPoolDescriptor, ) -> DeviceResult { - Ok(super::CommandPool {}) + let vk_info = vk::CommandPoolCreateInfo::builder() + .queue_family_index(desc.queue.family_index) + .flags(vk::CommandPoolCreateFlags::RESET_COMMAND_BUFFER) + .build(); + let raw = self.shared.raw.create_command_pool(&vk_info, None)?; + + Ok(super::CommandPool { + raw, + device: Arc::clone(&self.shared), + free: Vec::new(), + }) + } + unsafe fn destroy_command_pool(&self, cmd_pool: super::CommandPool) { + self.shared.raw.destroy_command_pool(cmd_pool.raw, None); } - unsafe fn destroy_command_pool(&self, cmd_pool: super::CommandPool) {} unsafe fn create_bind_group_layout( &self, diff --git a/wgpu-hal/src/vulkan/instance.rs b/wgpu-hal/src/vulkan/instance.rs index bdfc0ab73..4fc883958 100644 --- a/wgpu-hal/src/vulkan/instance.rs +++ b/wgpu-hal/src/vulkan/instance.rs @@ -307,7 +307,7 @@ impl crate::Instance for super::Instance { }; let driver_api_version = match entry.try_enumerate_instance_version() { // Vulkan 1.1+ - Ok(Some(version)) => version.into(), + Ok(Some(version)) => version, Ok(None) => vk::API_VERSION_1_0, Err(err) => { log::warn!("try_enumerate_instance_version: {:?}", err); @@ -334,7 +334,6 @@ impl crate::Instance for super::Instance { // - If any are non-KHR-vendored, we must ensure the new behavior is still correct (since backwards-compatibility is not guaranteed). vk::HEADER_VERSION_COMPLETE }) - .into() }); let instance_extensions = entry diff --git a/wgpu-hal/src/vulkan/mod.rs b/wgpu-hal/src/vulkan/mod.rs index a9f4d28cb..1ccd2be93 100644 --- a/wgpu-hal/src/vulkan/mod.rs +++ b/wgpu-hal/src/vulkan/mod.rs @@ -18,7 +18,6 @@ const MILLIS_TO_NANOS: u64 = 1_000_000; #[derive(Clone)] pub struct Api; -pub struct Encoder; #[derive(Debug)] pub struct Resource; @@ -32,7 +31,7 @@ impl crate::Api for Api { type Queue = Queue; type CommandPool = CommandPool; - type CommandBuffer = Encoder; + type CommandBuffer = CommandBuffer; type Buffer = Buffer; type Texture = Texture; @@ -157,6 +156,7 @@ pub struct Device { pub struct Queue { raw: vk::Queue, swapchain_fn: khr::Swapchain, + family_index: u32, //device: Arc, } @@ -199,15 +199,21 @@ pub struct BindGroup { raw: gpu_descriptor::DescriptorSet, } -#[derive(Debug)] pub struct CommandPool { - //TODO + raw: vk::CommandPool, + device: Arc, + free: Vec, +} + +pub struct CommandBuffer { + raw: vk::CommandBuffer, + device: Arc, } impl crate::Queue for Queue { unsafe fn submit( &mut self, - command_buffers: &[&Encoder], + command_buffers: &[&CommandBuffer], signal_fence: Option<(&mut Resource, crate::FenceValue)>, ) -> DeviceResult<()> { Ok(()) @@ -221,14 +227,6 @@ impl crate::Queue for Queue { } } -impl crate::CommandPool for CommandPool { - unsafe fn allocate(&mut self, desc: &crate::CommandBufferDescriptor) -> DeviceResult { - Ok(Encoder) - } - unsafe fn free(&mut self, cmd_buf: Encoder) {} - unsafe fn clear(&mut self) {} -} - impl From for crate::DeviceError { fn from(result: vk::Result) -> Self { match result {