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

Add more hal methods #5452

Merged
merged 8 commits into from
Apr 2, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add TextureView::as_hal()
  • Loading branch information
JMS55 committed Mar 29, 2024
commit 940e6baab68c742eacae1e2b2a28bc42f3d43ad3
21 changes: 20 additions & 1 deletion wgpu-core/src/resource.rs
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ use crate::{
},
global::Global,
hal_api::HalApi,
id::{AdapterId, BufferId, DeviceId, Id, Marker, SurfaceId, TextureId},
id::{AdapterId, BufferId, DeviceId, Id, Marker, SurfaceId, TextureId, TextureViewId},
init_tracker::{BufferInitTracker, TextureInitTracker},
resource, resource_log,
snatch::{ExclusiveSnatchGuard, SnatchGuard, Snatchable},
@@ -940,6 +940,25 @@ impl Global {
hal_texture_callback(hal_texture)
}

/// # Safety
///
/// - The raw texture view handle must not be manually destroyed
pub unsafe fn texture_view_as_hal<A: HalApi, F: FnOnce(Option<&A::TextureView>) -> R, R>(
&self,
id: TextureViewId,
hal_texture_view_callback: F,
) -> R {
profiling::scope!("TextureView::as_hal");

let hub = A::hub(self);
let texture_view_opt = { hub.texture_views.try_get(id).ok().flatten() };
let texture_view = texture_view_opt.as_ref().unwrap();
let snatch_guard = texture_view.device.snatchable_lock.read();
let hal_texture_view = texture_view.raw(&snatch_guard);

hal_texture_view_callback(hal_texture_view)
}

/// # Safety
///
/// - The raw adapter handle must not be manually destroyed
20 changes: 19 additions & 1 deletion wgpu/src/backend/wgpu_core.rs
Original file line number Diff line number Diff line change
@@ -22,8 +22,11 @@ use std::{
slice,
sync::Arc,
};
use wgc::command::{bundle_ffi::*, compute_ffi::*, render_ffi::*};
use wgc::device::DeviceLostClosure;
use wgc::{
command::{bundle_ffi::*, compute_ffi::*, render_ffi::*},
id::TextureViewId,
};
use wgt::WasmNotSendSync;

const LABEL: &str = "label";
@@ -222,6 +225,21 @@ impl ContextWgpuCore {
}
}

pub unsafe fn texture_view_as_hal<
A: wgc::hal_api::HalApi,
F: FnOnce(Option<&A::TextureView>) -> R,
R,
>(
&self,
texture_view_id: TextureViewId,
hal_texture_view_callback: F,
) -> R {
unsafe {
self.0
.texture_view_as_hal::<A, F, R>(texture_view_id, hal_texture_view_callback)
}
}

pub fn generate_report(&self) -> wgc::global::GlobalReport {
self.0.generate_report()
}
28 changes: 28 additions & 0 deletions wgpu/src/lib.rs
Original file line number Diff line number Diff line change
@@ -5021,6 +5021,34 @@ impl TextureView {
pub fn global_id(&self) -> Id<Self> {
Id(self.id.global_id(), PhantomData)
}

/// Returns the inner hal TextureView 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 TextureView must not be manually destroyed
#[cfg(wgpu_core)]
pub unsafe fn as_hal<A: wgc::hal_api::HalApi, F: FnOnce(Option<&A::TextureView>) -> R, R>(
&self,
hal_texture_view_callback: F,
) -> R {
use core::id::TextureViewId;

let texture_view_id = TextureViewId::from(self.id);

if let Some(ctx) = self
.context
.as_any()
.downcast_ref::<crate::backend::ContextWgpuCore>()
{
unsafe {
ctx.texture_view_as_hal::<A, F, R>(texture_view_id, hal_texture_view_callback)
}
} else {
hal_texture_view_callback(None)
}
}
}

impl Sampler {