Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Vulkan] Initialize wgpu objects from raw handles, part 2 #1850

Merged
merged 1 commit into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions wgpu-hal/src/vulkan/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,7 @@ impl super::Adapter {
pub unsafe fn device_from_raw(
&self,
raw_device: ash::Device,
handle_is_owned: bool,
enabled_extensions: &[&'static CStr],
family_index: u32,
queue_index: u32,
Expand Down Expand Up @@ -845,6 +846,7 @@ impl super::Adapter {

let shared = Arc::new(super::DeviceShared {
raw: raw_device,
handle_is_owned,
instance: Arc::clone(&self.instance),
extension_fns: super::DeviceExtensionFunctions {
draw_indirect_count: indirect_count_fn,
Expand Down Expand Up @@ -944,6 +946,7 @@ impl crate::Adapter<super::Api> for super::Adapter {

self.device_from_raw(
raw_device,
true,
&enabled_extensions,
family_info.queue_family_index,
0,
Expand Down
16 changes: 8 additions & 8 deletions wgpu-hal/src/vulkan/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,9 @@ impl super::DeviceShared {
for &raw in self.framebuffers.lock().values() {
self.raw.destroy_framebuffer(raw, None);
}
self.raw.destroy_device(None);
if self.handle_is_owned {
self.raw.destroy_device(None);
}
}
}

Expand Down Expand Up @@ -532,21 +534,19 @@ impl super::Device {
})
}

/// The image handle and its memory are not handled by wgpu-hal
///
/// # Safety
///
/// - `vk_image` must be created respecting `desc` and `raw_flags`
/// - The application must manually destroy the texture handle. This can be done inside the
/// `Drop` impl of `drop_guard`.
/// - `vk_image` must be created respecting `desc`
/// - If `drop_guard` is `Some`, the application must manually destroy the image handle. This
/// can be done inside the `Drop` impl of `drop_guard`.
pub unsafe fn texture_from_raw(
vk_image: vk::Image,
desc: &crate::TextureDescriptor,
drop_guard: super::DropGuard,
drop_guard: Option<super::DropGuard>,
) -> super::Texture {
super::Texture {
raw: vk_image,
drop_guard: Some(drop_guard),
drop_guard,
block: None,
usage: desc.usage,
aspects: crate::FormatAspects::from(desc.format),
Expand Down
10 changes: 6 additions & 4 deletions wgpu-hal/src/vulkan/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ impl super::Instance {
driver_api_version: u32,
extensions: Vec<&'static CStr>,
flags: crate::InstanceFlags,
drop_guard: super::DropGuard,
drop_guard: Option<super::DropGuard>,
) -> Result<Self, crate::InstanceError> {
if driver_api_version == vk::API_VERSION_1_0
&& !extensions.contains(&vk::KhrStorageBufferStorageClassFn::name())
Expand Down Expand Up @@ -228,7 +228,7 @@ impl super::Instance {
Ok(Self {
shared: Arc::new(super::InstanceShared {
raw: raw_instance,
_drop_guard: drop_guard,
drop_guard,
flags,
debug_utils,
get_physical_device_properties,
Expand Down Expand Up @@ -418,7 +418,9 @@ impl Drop for super::InstanceShared {
du.extension
.destroy_debug_utils_messenger(du.messenger, None);
}
self.raw.destroy_instance(None);
if let Some(_drop_guard) = self.drop_guard.take() {
self.raw.destroy_instance(None);
}
}
}
}
Expand Down Expand Up @@ -520,7 +522,7 @@ impl crate::Instance<super::Api> for super::Instance {
driver_api_version,
extensions,
desc.flags,
Box::new(()),
Some(Box::new(())), // `Some` signals that wgpu-hal is in charge of destroying vk_instance
)
}

Expand Down
3 changes: 2 additions & 1 deletion wgpu-hal/src/vulkan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ struct DebugUtils {

struct InstanceShared {
raw: ash::Instance,
_drop_guard: DropGuard,
drop_guard: Option<DropGuard>,
flags: crate::InstanceFlags,
debug_utils: Option<DebugUtils>,
get_physical_device_properties: Option<vk::KhrGetPhysicalDeviceProperties2Fn>,
Expand Down Expand Up @@ -216,6 +216,7 @@ struct FramebufferKey {

struct DeviceShared {
raw: ash::Device,
handle_is_owned: bool,
instance: Arc<InstanceShared>,
extension_fns: DeviceExtensionFunctions,
vendor_id: u32,
Expand Down