hal/vk: command pools, clippy warnings

This commit is contained in:
Dzmitry Malyshau 2021-06-14 09:51:25 -04:00
parent c2bb2d5dfc
commit d8979cad4a
9 changed files with 116 additions and 49 deletions

View File

@ -716,15 +716,15 @@ impl<A: HalApi, F: GlobalIdentityHandlerFactory> Hub<A, F> {
pub struct Hubs<F: GlobalIdentityHandlerFactory> {
#[cfg(vulkan)]
vulkan: Hub<backend::Vulkan, F>,
vulkan: Hub<hal::api::Vulkan, F>,
#[cfg(metal)]
metal: Hub<hal::api::Metal, F>,
#[cfg(dx12)]
dx12: Hub<backend::Dx12, F>,
dx12: Hub<hal::api::Dx12, F>,
#[cfg(dx11)]
dx11: Hub<backend::Dx11, F>,
dx11: Hub<hal::api::Dx11, F>,
#[cfg(gl)]
gl: Hub<backend::Gl, F>,
gl: Hub<hal::api::Gles, F>,
}
impl<F: GlobalIdentityHandlerFactory> Hubs<F> {
@ -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<G: GlobalIdentityHandlerFactory>(global: &Global<G>) -> &Hub<Self, G> {
&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<G: GlobalIdentityHandlerFactory>(global: &Global<G>) -> &Hub<Self, G> {
&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<G: GlobalIdentityHandlerFactory>(global: &Global<G>) -> &Hub<Self, G> {
&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<G: GlobalIdentityHandlerFactory>(global: &Global<G>) -> &Hub<Self, G> {
&global.hubs.gl

View File

@ -22,15 +22,15 @@ pub struct Instance {
#[allow(dead_code)]
name: String,
#[cfg(vulkan)]
pub vulkan: Option<gfx_backend_vulkan::Instance>,
pub vulkan: Option<HalInstance<hal::api::Vulkan>>,
#[cfg(metal)]
pub metal: Option<HalInstance<hal::api::Metal>>,
#[cfg(dx12)]
pub dx12: Option<gfx_backend_dx12::Instance>,
pub dx12: Option<HalInstance<hal::api::Dx12>>,
#[cfg(dx11)]
pub dx11: Option<gfx_backend_dx11::Instance>,
pub dx11: Option<HalInstance<hal::api::Dx11>>,
#[cfg(gl)]
pub gl: Option<gfx_backend_gl::Instance>,
pub gl: Option<HalInstance<hal::api::Gles>>,
}
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<HalSurface<hal::api::Dx11>>,
#[cfg(gl)]
pub gl: Option<HalSurface<hal::api::Gl>>,
pub gl: Option<HalSurface<hal::api::Gles>>,
}
impl crate::hub::Resource for Surface {
@ -489,7 +489,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
// being weird with lifetimes for closure literals...
#[cfg(vulkan)]
let adapters_vk = map((&instance.vulkan, &id_vulkan, {
fn surface_vulkan(surf: &Surface) -> Option<&HalSurface<backend::Vulkan>> {
fn surface_vulkan(surf: &Surface) -> Option<&HalSurface<hal::api::Vulkan>> {
surf.vulkan.as_ref()
}
surface_vulkan
@ -503,21 +503,21 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
}));
#[cfg(dx12)]
let adapters_dx12 = map((&instance.dx12, &id_dx12, {
fn surface_dx12(surf: &Surface) -> Option<&HalSurface<backend::Dx12>> {
fn surface_dx12(surf: &Surface) -> Option<&HalSurface<hal::api::Dx12>> {
surf.dx12.as_ref()
}
surface_dx12
}));
#[cfg(dx11)]
let adapters_dx11 = map((&instance.dx11, &id_dx11, {
fn surface_dx11(surf: &Surface) -> Option<&HalSurface<backend::Dx11>> {
fn surface_dx11(surf: &Surface) -> Option<&HalSurface<hal::api::Dx11>> {
surf.dx11.as_ref()
}
surface_dx11
}));
#[cfg(gl)]
let adapters_gl = map((&instance.gl, &id_gl, {
fn surface_gl(surf: &Surface) -> Option<&HalSurface<backend::Gl>> {
fn surface_gl(surf: &Surface) -> Option<&HalSurface<hal::api::Gles>> {
surf.gl.as_ref()
}
surface_gl

View File

@ -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),* ),
/*

View File

@ -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<A: Api>: 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<A: Api>: Send + Sync {
unsafe fn allocate(
&mut self,

View File

@ -463,9 +463,9 @@ impl super::InstanceShared {
/// # Safety
/// `T` must be a struct bigger than `vk::BaseOutStructure`.
unsafe fn null_p_next<T>(features: &mut Option<T>) {
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<super::Api> 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);

View File

@ -1,8 +1,57 @@
use super::Resource;
use std::ops::Range; //TEMP
use std::{ops::Range, sync::Arc};
impl crate::CommandBuffer<super::Api> 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<super::Api> for super::CommandPool {
unsafe fn allocate(
&mut self,
desc: &crate::CommandBufferDescriptor,
) -> Result<super::CommandBuffer, crate::DeviceError> {
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<super::Api> 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

View File

@ -608,9 +608,21 @@ impl crate::Device<super::Api> for super::Device {
&self,
desc: &crate::CommandPoolDescriptor<super::Api>,
) -> DeviceResult<super::CommandPool> {
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,

View File

@ -307,7 +307,7 @@ impl crate::Instance<super::Api> 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<super::Api> 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

View File

@ -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<DeviceShared>,
}
@ -199,15 +199,21 @@ pub struct BindGroup {
raw: gpu_descriptor::DescriptorSet<vk::DescriptorSet>,
}
#[derive(Debug)]
pub struct CommandPool {
//TODO
raw: vk::CommandPool,
device: Arc<DeviceShared>,
free: Vec<vk::CommandBuffer>,
}
pub struct CommandBuffer {
raw: vk::CommandBuffer,
device: Arc<DeviceShared>,
}
impl crate::Queue<Api> 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<Api> for Queue {
}
}
impl crate::CommandPool<Api> for CommandPool {
unsafe fn allocate(&mut self, desc: &crate::CommandBufferDescriptor) -> DeviceResult<Encoder> {
Ok(Encoder)
}
unsafe fn free(&mut self, cmd_buf: Encoder) {}
unsafe fn clear(&mut self) {}
}
impl From<vk::Result> for crate::DeviceError {
fn from(result: vk::Result) -> Self {
match result {