From 0a2be6099c92a0faa080f70ddb0893829fa98750 Mon Sep 17 00:00:00 2001 From: Riccardo Zaglia Date: Sun, 29 Aug 2021 13:26:56 +0200 Subject: [PATCH] Add Texture::as_hal() --- wgpu-core/src/instance.rs | 2 +- wgpu-core/src/resource.rs | 23 ++++++++++++++++++++++- wgpu-hal/src/vulkan/mod.rs | 9 +++++++++ wgpu/src/backend/direct.rs | 9 +++++++++ wgpu/src/lib.rs | 15 +++++++++++++++ 5 files changed, 56 insertions(+), 2 deletions(-) diff --git a/wgpu-core/src/instance.rs b/wgpu-core/src/instance.rs index 397fe04237..6272df9750 100644 --- a/wgpu-core/src/instance.rs +++ b/wgpu-core/src/instance.rs @@ -859,7 +859,7 @@ impl Global { trace_path: Option<&std::path::Path>, id_in: Input, ) -> (DeviceId, Option) { - profiling::scope!("request_device", "Adapter"); + profiling::scope!("create_device_from_hal", "Adapter"); let hub = A::hub(self); let mut token = Token::root(); diff --git a/wgpu-core/src/resource.rs b/wgpu-core/src/resource.rs index ab6cf120e3..96a422cc05 100644 --- a/wgpu-core/src/resource.rs +++ b/wgpu-core/src/resource.rs @@ -1,6 +1,6 @@ use crate::{ device::{DeviceError, HostMap, MissingFeatures}, - hub::Resource, + hub::{Global, GlobalIdentityHandlerFactory, HalApi, Resource, Token}, id::{DeviceId, SurfaceId, TextureId, Valid}, init_tracker::BufferInitTracker, track::{TextureSelector, DUMMY_SELECTOR}, @@ -191,6 +191,27 @@ pub struct Texture { pub(crate) life_guard: LifeGuard, } +impl Global { + /// # Safety + /// + /// - The raw texture handle must not be manually destroyed + pub unsafe fn texture_as_hal)>( + &self, + id: TextureId, + hal_texture_callback: F, + ) { + profiling::scope!("as_hal", "Texture"); + + let hub = A::hub(self); + let mut token = Token::root(); + let (guard, _) = hub.textures.read(&mut token); + let texture = guard.get(id).ok(); + let hal_texture = texture.map(|tex| tex.inner.as_raw().unwrap()); + + hal_texture_callback(hal_texture); + } +} + #[derive(Clone, Copy, Debug)] pub enum TextureErrorDimension { X, diff --git a/wgpu-hal/src/vulkan/mod.rs b/wgpu-hal/src/vulkan/mod.rs index 9dbe9d4709..f0ea39be9a 100644 --- a/wgpu-hal/src/vulkan/mod.rs +++ b/wgpu-hal/src/vulkan/mod.rs @@ -268,6 +268,15 @@ pub struct Texture { raw_flags: vk::ImageCreateFlags, } +impl Texture { + /// # Safety + /// + /// - The image handle must not be manually destroyed + pub unsafe fn raw_handle(&self) -> vk::Image { + self.raw + } +} + #[derive(Debug)] pub struct TextureView { raw: vk::ImageView, diff --git a/wgpu/src/backend/direct.rs b/wgpu/src/backend/direct.rs index 22a8d05d40..741c75d554 100644 --- a/wgpu/src/backend/direct.rs +++ b/wgpu/src/backend/direct.rs @@ -118,6 +118,15 @@ impl Context { } } + pub unsafe fn texture_as_hal)>( + &self, + texture: &Texture, + hal_texture_callback: F, + ) { + self.0 + .texture_as_hal::(texture.id, hal_texture_callback) + } + pub fn generate_report(&self) -> wgc::hub::GlobalReport { self.0.generate_report() } diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index 3f47f791e1..a4e9b61ca7 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -2075,6 +2075,21 @@ impl Drop for Buffer { } impl Texture { + /// Returns the inner hal Texture using a callback. The hal texture will be `None` if the + /// backend type argument does not match with this wgpu Texture + /// + /// # Safety + /// + /// - The raw handle obtained from the hal Texture must not be manually destroyed + #[cfg(not(target_arch = "wasm32"))] + pub unsafe fn as_hal( + &self, + hal_texture_callback: impl FnOnce(Option<&A::Texture>), + ) { + self.context + .texture_as_hal::(&self.id, hal_texture_callback) + } + /// Creates a view of this texture. pub fn create_view(&self, desc: &TextureViewDescriptor) -> TextureView { TextureView {