Skip to content

Commit

Permalink
Enable more wgpu-hal functions for webgl (#3490)
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda authored Feb 15, 2023
1 parent e444520 commit 719ba83
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 24 deletions.
19 changes: 2 additions & 17 deletions wgpu/src/backend/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,11 @@ impl Context {
&self.0
}

#[cfg(any(not(target_arch = "wasm32"), target_os = "emscripten"))]
pub fn enumerate_adapters(&self, backends: wgt::Backends) -> Vec<wgc::id::AdapterId> {
self.0
.enumerate_adapters(wgc::instance::AdapterInputs::Mask(backends, |_| ()))
}

#[cfg(any(not(target_arch = "wasm32"), target_os = "emscripten"))]
pub unsafe fn create_adapter_from_hal<A: wgc::hub::HalApi>(
&self,
hal_adapter: hal::ExposedAdapter<A>,
Expand All @@ -93,7 +91,6 @@ impl Context {
}
}

#[cfg(any(not(target_arch = "wasm32"), target_os = "emscripten"))]
pub unsafe fn create_device_from_hal<A: wgc::hub::HalApi>(
&self,
adapter: &wgc::id::AdapterId,
Expand Down Expand Up @@ -152,7 +149,6 @@ impl Context {
}
}

#[cfg(any(not(target_arch = "wasm32"), target_os = "emscripten"))]
pub unsafe fn device_as_hal<A: wgc::hub::HalApi, F: FnOnce(Option<&A::Device>) -> R, R>(
&self,
device: &Device,
Expand All @@ -164,7 +160,6 @@ impl Context {
}
}

#[cfg(any(not(target_arch = "wasm32"), target_os = "emscripten"))]
pub unsafe fn surface_as_hal_mut<
A: wgc::hub::HalApi,
F: FnOnce(Option<&mut A::Surface>) -> R,
Expand All @@ -180,7 +175,6 @@ impl Context {
}
}

#[cfg(any(not(target_arch = "wasm32"), target_os = "emscripten"))]
pub unsafe fn texture_as_hal<A: wgc::hub::HalApi, F: FnOnce(Option<&A::Texture>)>(
&self,
texture: &Texture,
Expand All @@ -192,7 +186,6 @@ impl Context {
}
}

#[cfg(any(not(target_arch = "wasm32"), target_os = "emscripten"))]
pub fn generate_report(&self) -> wgc::hub::GlobalReport {
self.0.generate_report()
}
Expand All @@ -209,11 +202,7 @@ impl Context {
}
}

#[cfg(all(
target_arch = "wasm32",
feature = "webgl",
not(target_os = "emscripten")
))]
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
pub fn instance_create_surface_from_canvas(
&self,
canvas: &web_sys::HtmlCanvasElement,
Expand All @@ -228,11 +217,7 @@ impl Context {
})
}

#[cfg(all(
target_arch = "wasm32",
feature = "webgl",
not(target_os = "emscripten")
))]
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
pub fn instance_create_surface_from_offscreen_canvas(
&self,
canvas: &web_sys::OffscreenCanvas,
Expand Down
42 changes: 35 additions & 7 deletions wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1406,7 +1406,11 @@ impl Instance {
/// # Arguments
///
/// - `backends` - Backends from which to enumerate adapters.
#[cfg(any(not(target_arch = "wasm32"), target_os = "emscripten"))]
#[cfg(any(
not(target_arch = "wasm32"),
target_os = "emscripten",
feature = "webgl"
))]
pub fn enumerate_adapters(&self, backends: Backends) -> impl Iterator<Item = Adapter> {
let context = Arc::clone(&self.context);
self.context
Expand Down Expand Up @@ -1445,7 +1449,11 @@ impl Instance {
/// # Safety
///
/// `hal_adapter` must be created from this instance internal handle.
#[cfg(any(not(target_arch = "wasm32"), target_os = "emscripten"))]
#[cfg(any(
not(target_arch = "wasm32"),
target_os = "emscripten",
feature = "webgl"
))]
pub unsafe fn create_adapter_from_hal<A: wgc::hub::HalApi>(
&self,
hal_adapter: hal::ExposedAdapter<A>,
Expand Down Expand Up @@ -1671,7 +1679,11 @@ impl Instance {
}

/// Generates memory report.
#[cfg(any(not(target_arch = "wasm32"), target_os = "emscripten"))]
#[cfg(any(
not(target_arch = "wasm32"),
target_os = "emscripten",
feature = "webgl"
))]
pub fn generate_report(&self) -> wgc::hub::GlobalReport {
self.context
.as_any()
Expand Down Expand Up @@ -1742,7 +1754,11 @@ impl Adapter {
///
/// - `hal_device` must be created from this adapter internal handle.
/// - `desc.features` must be a subset of `hal_device` features.
#[cfg(any(not(target_arch = "wasm32"), target_os = "emscripten"))]
#[cfg(any(
not(target_arch = "wasm32"),
target_os = "emscripten",
feature = "webgl"
))]
pub unsafe fn create_device_from_hal<A: wgc::hub::HalApi>(
&self,
hal_device: hal::OpenDevice<A>,
Expand Down Expand Up @@ -2246,7 +2262,11 @@ impl Device {
/// - The raw handle passed to the callback must not be manually destroyed.
///
/// [`A::Device`]: hal::Api::Device
#[cfg(any(not(target_arch = "wasm32"), target_os = "emscripten"))]
#[cfg(any(
not(target_arch = "wasm32"),
target_os = "emscripten",
feature = "webgl"
))]
pub unsafe fn as_hal<A: wgc::hub::HalApi, F: FnOnce(Option<&A::Device>) -> R, R>(
&self,
hal_device_callback: F,
Expand Down Expand Up @@ -2584,7 +2604,11 @@ impl Texture {
/// # Safety
///
/// - The raw handle obtained from the hal Texture must not be manually destroyed
#[cfg(any(not(target_arch = "wasm32"), target_os = "emscripten"))]
#[cfg(any(
not(target_arch = "wasm32"),
target_os = "emscripten",
feature = "webgl"
))]
pub unsafe fn as_hal<A: wgc::hub::HalApi, F: FnOnce(Option<&A::Texture>)>(
&self,
hal_texture_callback: F,
Expand Down Expand Up @@ -4193,7 +4217,11 @@ impl Surface {
/// # Safety
///
/// - The raw handle obtained from the hal Surface must not be manually destroyed
#[cfg(any(not(target_arch = "wasm32"), target_os = "emscripten"))]
#[cfg(any(
not(target_arch = "wasm32"),
target_os = "emscripten",
feature = "webgl"
))]
pub unsafe fn as_hal_mut<A: wgc::hub::HalApi, F: FnOnce(Option<&mut A::Surface>) -> R, R>(
&mut self,
hal_surface_callback: F,
Expand Down

0 comments on commit 719ba83

Please sign in to comment.