Remove emscripten crate features (#3467)

This commit is contained in:
daxpedda 2023-02-15 22:46:00 +01:00 committed by GitHub
parent c51edd36fd
commit e4445205c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
23 changed files with 147 additions and 96 deletions

View File

@ -139,13 +139,13 @@ jobs:
set -e
# build for Emscripten/WebGL
cargo clippy --target ${{ matrix.target }} -p wgpu -p wgpu-hal --no-default-features --features webgl,emscripten
cargo clippy --target ${{ matrix.target }} -p wgpu -p wgpu-hal --no-default-features
# build cube example
cargo clippy --target ${{ matrix.target }} --example cube --features webgl,emscripten
cargo clippy --target ${{ matrix.target }} --example cube
# build raw-gles example
cargo clippy --target ${{ matrix.target }} --example raw-gles --features webgl,emscripten
cargo clippy --target ${{ matrix.target }} --example raw-gles
- name: check native
if: matrix.kind == 'native'

View File

@ -35,6 +35,6 @@ workspace = true
features = ["dx11", "dx12"]
# We want the wgpu-core Vulkan backend on Unix (but not Emscripten) and Windows.
[target.'cfg(any(windows, all(unix, not(target_arch = "emscripten"))))'.dependencies.wgpu-core]
[target.'cfg(any(windows, all(unix, not(target_os = "emscripten"))))'.dependencies.wgpu-core]
workspace = true
features = ["vulkan"]

View File

@ -37,7 +37,7 @@ features = ["metal"]
workspace = true
features = ["dx11", "dx12"]
[target.'cfg(any(windows, all(unix, not(target_arch = "emscripten"), not(target_os = "ios"), not(target_os = "macos"))))'.dependencies.wgc]
[target.'cfg(any(windows, all(unix, not(target_os = "emscripten"), not(target_os = "ios"), not(target_os = "macos"))))'.dependencies.wgc]
workspace = true
features = ["vulkan"]

View File

@ -35,9 +35,6 @@ dx12 = ["hal/dx12"]
# https://renderdoc.org/
renderdoc = ["hal/renderdoc"]
# Compile for the Emscripten POSIX-in-a-web-page emulation environment.
emscripten = ["hal/emscripten"]
# Apply run-time checks, even in release builds. These are in addition
# to the validation carried out at public APIs in all builds.
strict_asserts = ["wgt/strict_asserts"]

View File

@ -34,7 +34,7 @@ pub fn is_valid_copy_dst_texture_format(
}
#[cfg_attr(
any(not(target_arch = "wasm32"), feature = "emscripten"),
any(not(target_arch = "wasm32"), target_os = "emscripten"),
allow(unused)
)]
pub fn is_valid_external_image_copy_dst_texture_format(format: wgt::TextureFormat) -> bool {

View File

@ -824,7 +824,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
Ok(())
}
#[cfg(all(target_arch = "wasm32", not(feature = "emscripten")))]
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
pub fn queue_copy_external_image_to_texture<A: HalApi>(
&self,
queue_id: id::QueueId,

View File

@ -43,7 +43,6 @@ dx12 = ["naga/hlsl-out", "native", "bit-set", "range-alloc", "winapi/std", "wina
windows_rs = ["gpu-allocator"]
dxc_shader_compiler = ["hassle-rs"]
renderdoc = ["libloading", "renderdoc-sys"]
emscripten = ["gles"]
[[example]]
name = "halmark"

View File

@ -4,7 +4,7 @@
//! Emscripten build:
//! 1. install emsdk
//! 2. build this example with cargo:
//! EMCC_CFLAGS="-g -s ERROR_ON_UNDEFINED_SYMBOLS=0 --no-entry -s FULL_ES3=1" cargo build --example raw-gles --target wasm32-unknown-emscripten --features emscripten,webgl
//! EMCC_CFLAGS="-g -s ERROR_ON_UNDEFINED_SYMBOLS=0 --no-entry -s FULL_ES3=1" cargo build --example raw-gles --target wasm32-unknown-emscripten
//! 3. copy raw-gles.em.html into target directory and open it in browser:
//! cp wgpu-hal/examples/raw-gles.em.html target/wasm32-unknown-emscripten/debug/examples
@ -66,7 +66,7 @@ fn main() {
});
}
#[cfg(feature = "emscripten")]
#[cfg(target_os = "emscripten")]
fn main() {
env_logger::init();
@ -116,7 +116,7 @@ fn main() {
fill_screen(&exposed, 640, 400);
}
#[cfg(all(target_arch = "wasm32", not(feature = "emscripten")))]
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
fn main() {}
fn fill_screen(exposed: &hal::ExposedAdapter<hal::api::Gles>, width: u32, height: u32) {

View File

@ -264,7 +264,7 @@ impl crate::CommandEncoder<Api> for Encoder {
unsafe fn copy_buffer_to_buffer<T>(&mut self, src: &Resource, dst: &Resource, regions: T) {}
#[cfg(all(target_arch = "wasm32", not(feature = "emscripten")))]
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
unsafe fn copy_external_image_to_texture<T>(
&mut self,
src: &wgt::ImageCopyExternalImage,

View File

@ -309,7 +309,7 @@ impl crate::CommandEncoder<super::Api> for super::CommandEncoder {
}
}
#[cfg(all(target_arch = "wasm32", not(feature = "emscripten")))]
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
unsafe fn copy_external_image_to_texture<T>(
&mut self,
src: &wgt::ImageCopyExternalImage,

View File

@ -94,7 +94,7 @@ impl super::Device {
/// - If `drop_guard` is [`None`], wgpu-hal will take ownership of the texture. If `drop_guard` is
/// [`Some`], the texture must be valid until the drop implementation
/// of the drop guard is called.
#[cfg(any(not(target_arch = "wasm32"), feature = "emscripten"))]
#[cfg(any(not(target_arch = "wasm32"), target_os = "emscripten"))]
pub unsafe fn texture_from_raw(
&self,
name: std::num::NonZeroU32,
@ -125,7 +125,7 @@ impl super::Device {
/// - If `drop_guard` is [`None`], wgpu-hal will take ownership of the renderbuffer. If `drop_guard` is
/// [`Some`], the renderbuffer must be valid until the drop implementation
/// of the drop guard is called.
#[cfg(any(not(target_arch = "wasm32"), feature = "emscripten"))]
#[cfg(any(not(target_arch = "wasm32"), target_os = "emscripten"))]
pub unsafe fn texture_from_raw_renderbuffer(
&self,
name: std::num::NonZeroU32,

View File

@ -26,10 +26,10 @@ type WlDisplayConnectFun =
type WlDisplayDisconnectFun = unsafe extern "system" fn(display: *const raw::c_void);
#[cfg(not(feature = "emscripten"))]
#[cfg(not(target_os = "emscripten"))]
type EglInstance = egl::DynamicInstance<egl::EGL1_4>;
#[cfg(feature = "emscripten")]
#[cfg(target_os = "emscripten")]
type EglInstance = egl::Instance<egl::Static>;
type WlEglWindowCreateFun = unsafe extern "system" fn(
@ -423,7 +423,7 @@ struct Inner {
version: (i32, i32),
supports_native_window: bool,
config: egl::Config,
#[cfg_attr(feature = "emscripten", allow(dead_code))]
#[cfg_attr(target_os = "emscripten", allow(dead_code))]
wl_display: Option<*mut raw::c_void>,
/// Method by which the framebuffer should support srgb
srgb_kind: SrgbFrameBufferKind,
@ -535,7 +535,7 @@ impl Inner {
// and creating dummy pbuffer surface if not.
let pbuffer = if version >= (1, 5)
|| display_extensions.contains("EGL_KHR_surfaceless_context")
|| cfg!(feature = "emscripten")
|| cfg!(target_os = "emscripten")
{
log::info!("\tEGL context: +surfaceless");
None
@ -624,10 +624,10 @@ unsafe impl Sync for Instance {}
impl crate::Instance<super::Api> for Instance {
unsafe fn init(desc: &crate::InstanceDescriptor) -> Result<Self, crate::InstanceError> {
#[cfg(feature = "emscripten")]
#[cfg(target_os = "emscripten")]
let egl_result: Result<EglInstance, egl::Error> = Ok(egl::Instance::new(egl::Static));
#[cfg(not(feature = "emscripten"))]
#[cfg(not(target_os = "emscripten"))]
let egl_result = if cfg!(windows) {
unsafe {
egl::DynamicInstance::<egl::EGL1_4>::load_required_from_filename("libEGL.dll")
@ -674,10 +674,10 @@ impl crate::Instance<super::Api> for Instance {
None
};
#[cfg(not(feature = "emscripten"))]
#[cfg(not(target_os = "emscripten"))]
let egl1_5 = egl.upcast::<egl::EGL1_5>();
#[cfg(feature = "emscripten")]
#[cfg(target_os = "emscripten")]
let egl1_5: Option<&Arc<EglInstance>> = Some(&egl);
let (display, wsi_library, wsi_kind) = if let (Some(library), Some(egl)) =
@ -776,7 +776,10 @@ impl crate::Instance<super::Api> for Instance {
) -> Result<Surface, crate::InstanceError> {
use raw_window_handle::RawWindowHandle as Rwh;
#[cfg_attr(any(target_os = "android", feature = "emscripten"), allow(unused_mut))]
#[cfg_attr(
any(target_os = "android", target_os = "emscripten"),
allow(unused_mut)
)]
let mut inner = self.inner.lock();
match (window_handle, display_handle) {
@ -801,7 +804,7 @@ impl crate::Instance<super::Api> for Instance {
return Err(crate::InstanceError);
}
}
#[cfg(not(feature = "emscripten"))]
#[cfg(not(target_os = "emscripten"))]
(Rwh::Wayland(_), raw_window_handle::RawDisplayHandle::Wayland(display_handle)) => {
if inner
.wl_display
@ -841,7 +844,7 @@ impl crate::Instance<super::Api> for Instance {
drop(old_inner);
}
}
#[cfg(feature = "emscripten")]
#[cfg(target_os = "emscripten")]
(Rwh::Web(_), _) => {}
other => {
log::error!("Unsupported window: {:?}", other);
@ -1087,7 +1090,7 @@ impl crate::Surface<super::Api> for Surface {
wl_window = Some(window);
window
}
#[cfg(feature = "emscripten")]
#[cfg(target_os = "emscripten")]
(WindowKind::Unknown, Rwh::Web(handle)) => handle.id as *mut std::ffi::c_void,
(WindowKind::Unknown, Rwh::Win32(handle)) => handle.hwnd,
(WindowKind::Unknown, Rwh::AppKit(handle)) => {
@ -1140,10 +1143,10 @@ impl crate::Surface<super::Api> for Surface {
}
attributes.push(egl::ATTRIB_NONE as i32);
#[cfg(not(feature = "emscripten"))]
#[cfg(not(target_os = "emscripten"))]
let egl1_5 = self.egl.instance.upcast::<egl::EGL1_5>();
#[cfg(feature = "emscripten")]
#[cfg(target_os = "emscripten")]
let egl1_5: Option<&Arc<EglInstance>> = Some(&self.egl.instance);
// Careful, we can still be in 1.4 version even if `upcast` succeeds

View File

@ -57,9 +57,9 @@ To address this, we invalidate the vertex buffers based on:
*/
///cbindgen:ignore
#[cfg(any(not(target_arch = "wasm32"), feature = "emscripten"))]
#[cfg(any(not(target_arch = "wasm32"), target_os = "emscripten"))]
mod egl;
#[cfg(all(target_arch = "wasm32", not(feature = "emscripten")))]
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
mod web;
mod adapter;
@ -70,14 +70,14 @@ mod queue;
use crate::{CopyExtent, TextureDescriptor};
#[cfg(any(not(target_arch = "wasm32"), feature = "emscripten"))]
#[cfg(any(not(target_arch = "wasm32"), target_os = "emscripten"))]
pub use self::egl::{AdapterContext, AdapterContextLock};
#[cfg(any(not(target_arch = "wasm32"), feature = "emscripten"))]
#[cfg(any(not(target_arch = "wasm32"), target_os = "emscripten"))]
use self::egl::{Instance, Surface};
#[cfg(all(target_arch = "wasm32", not(feature = "emscripten")))]
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
pub use self::web::AdapterContext;
#[cfg(all(target_arch = "wasm32", not(feature = "emscripten")))]
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
use self::web::{Instance, Surface};
use arrayvec::ArrayVec;
@ -680,7 +680,7 @@ enum Command {
dst_target: BindTarget,
copy: crate::BufferCopy,
},
#[cfg(all(target_arch = "wasm32", not(feature = "emscripten")))]
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
CopyExternalImageToTexture {
src: wgt::ImageCopyExternalImage,
dst: glow::Texture,

View File

@ -379,7 +379,7 @@ impl super::Queue {
unsafe { gl.bind_buffer(copy_dst_target, None) };
}
}
#[cfg(all(target_arch = "wasm32", not(feature = "emscripten")))]
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
C::CopyExternalImageToTexture {
ref src,
dst,
@ -1554,10 +1554,10 @@ impl crate::Queue<super::Api> for super::Queue {
surface: &mut super::Surface,
texture: super::Texture,
) -> Result<(), crate::SurfaceError> {
#[cfg(any(not(target_arch = "wasm32"), feature = "emscripten"))]
#[cfg(any(not(target_arch = "wasm32"), target_os = "emscripten"))]
let gl = unsafe { &self.shared.context.get_without_egl_lock() };
#[cfg(all(target_arch = "wasm32", not(feature = "emscripten")))]
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
let gl = &self.shared.context.glow_context;
unsafe { surface.present(texture, gl) }

View File

@ -407,7 +407,7 @@ pub trait CommandEncoder<A: Api>: Send + Sync + fmt::Debug {
/// Works with a single array layer.
/// Note: `dst` current usage has to be `TextureUses::COPY_DST`.
/// Note: the copy extent is in physical size (rounded to the block size)
#[cfg(all(target_arch = "wasm32", not(feature = "emscripten")))]
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
unsafe fn copy_external_image_to_texture<T>(
&mut self,
src: &wgt::ImageCopyExternalImage,

View File

@ -97,7 +97,6 @@ trace = ["serde", "wgc/trace"]
replay = ["serde", "wgc/replay"]
angle = ["wgc/angle"]
webgl = ["hal", "wgc"]
emscripten = ["webgl"]
vulkan-portability = ["wgc/vulkan"]
expose-ids = []
@ -128,14 +127,10 @@ workspace = true
features = ["dx11", "dx12"]
# We want the wgpu-core Vulkan backend on Unix (but not Emscripten) and Windows.
[target.'cfg(any(windows, all(unix, not(target_arch = "emscripten"), not(target_os = "ios"), not(target_os = "macos"))))'.dependencies.wgc]
[target.'cfg(any(windows, all(unix, not(target_os = "emscripten"), not(target_os = "ios"), not(target_os = "macos"))))'.dependencies.wgc]
workspace = true
features = ["vulkan"]
[target.'cfg(target_os = "emscripten")'.dependencies.wgc]
workspace = true
features = ["emscripten"]
[dependencies.wgt]
workspace = true

View File

@ -41,7 +41,6 @@ impl fmt::Debug for Context {
}
impl Context {
#[cfg(any(not(target_arch = "wasm32"), feature = "emscripten"))]
pub unsafe fn from_hal_instance<A: wgc::hub::HalApi>(hal_instance: A::Instance) -> Self {
Self(unsafe {
wgc::hub::Global::from_hal_instance::<A>(
@ -69,13 +68,13 @@ impl Context {
&self.0
}
#[cfg(any(not(target_arch = "wasm32"), feature = "emscripten"))]
#[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"), feature = "emscripten"))]
#[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>,
@ -94,7 +93,7 @@ impl Context {
}
}
#[cfg(any(not(target_arch = "wasm32"), feature = "emscripten"))]
#[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,
@ -128,7 +127,6 @@ impl Context {
Ok((device, queue))
}
#[cfg(any(not(target_arch = "wasm32"), feature = "emscripten", feature = "webgl"))]
pub unsafe fn create_texture_from_hal<A: wgc::hub::HalApi>(
&self,
hal_texture: A::Texture,
@ -154,7 +152,7 @@ impl Context {
}
}
#[cfg(any(not(target_arch = "wasm32"), feature = "emscripten"))]
#[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,
@ -166,7 +164,7 @@ impl Context {
}
}
#[cfg(any(not(target_arch = "wasm32"), feature = "emscripten"))]
#[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,
@ -182,7 +180,7 @@ impl Context {
}
}
#[cfg(any(not(target_arch = "wasm32"), feature = "emscripten"))]
#[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,
@ -194,7 +192,7 @@ impl Context {
}
}
#[cfg(any(not(target_arch = "wasm32"), feature = "emscripten"))]
#[cfg(any(not(target_arch = "wasm32"), target_os = "emscripten"))]
pub fn generate_report(&self) -> wgc::hub::GlobalReport {
self.0.generate_report()
}
@ -211,7 +209,11 @@ impl Context {
}
}
#[cfg(all(target_arch = "wasm32", feature = "webgl", not(feature = "emscripten")))]
#[cfg(all(
target_arch = "wasm32",
feature = "webgl",
not(target_os = "emscripten")
))]
pub fn instance_create_surface_from_canvas(
&self,
canvas: &web_sys::HtmlCanvasElement,
@ -226,7 +228,11 @@ impl Context {
})
}
#[cfg(all(target_arch = "wasm32", feature = "webgl", not(feature = "emscripten")))]
#[cfg(all(
target_arch = "wasm32",
feature = "webgl",
not(target_os = "emscripten")
))]
pub fn instance_create_surface_from_offscreen_canvas(
&self,
canvas: &web_sys::OffscreenCanvas,
@ -353,7 +359,7 @@ fn map_texture_copy_view(view: crate::ImageCopyTexture) -> wgc::command::ImageCo
}
#[cfg_attr(
any(not(target_arch = "wasm32"), feature = "emscripten"),
any(not(target_arch = "wasm32"), target_os = "emscripten"),
allow(unused)
)]
fn map_texture_tagged_copy_view(
@ -1408,7 +1414,7 @@ impl crate::Context for Context {
fn device_drop(&self, device: &Self::DeviceId, _device_data: &Self::DeviceData) {
let global = &self.0;
#[cfg(any(not(target_arch = "wasm32"), feature = "emscripten"))]
#[cfg(any(not(target_arch = "wasm32"), target_os = "emscripten"))]
{
match wgc::gfx_select!(device => global.device_poll(*device, wgt::Maintain::Wait)) {
Ok(_) => (),
@ -2219,7 +2225,7 @@ impl crate::Context for Context {
}
}
#[cfg(all(target_arch = "wasm32", not(feature = "emscripten")))]
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
fn queue_copy_external_image_to_texture(
&self,
queue: &Self::QueueId,

View File

@ -1,9 +1,23 @@
#[cfg(all(target_arch = "wasm32", not(feature = "webgl")))]
#[cfg(all(
target_arch = "wasm32",
not(any(target_os = "emscripten", feature = "webgl"))
))]
mod web;
#[cfg(all(target_arch = "wasm32", not(feature = "webgl")))]
#[cfg(all(
target_arch = "wasm32",
not(any(target_os = "emscripten", feature = "webgl"))
))]
pub(crate) use web::Context;
#[cfg(any(not(target_arch = "wasm32"), feature = "webgl"))]
#[cfg(any(
not(target_arch = "wasm32"),
target_os = "emscripten",
feature = "webgl"
))]
mod direct;
#[cfg(any(not(target_arch = "wasm32"), feature = "webgl"))]
#[cfg(any(
not(target_arch = "wasm32"),
target_os = "emscripten",
feature = "webgl"
))]
pub(crate) use direct::Context;

View File

@ -2394,7 +2394,6 @@ impl crate::context::Context for Context {
);
}
#[cfg(all(target_arch = "wasm32", not(feature = "emscripten")))]
fn queue_copy_external_image_to_texture(
&self,
queue: &Self::QueueId,

View File

@ -561,7 +561,7 @@ pub trait Context: Debug + Send + Sized + Sync {
data_layout: ImageDataLayout,
size: Extent3d,
);
#[cfg(all(target_arch = "wasm32", not(feature = "emscripten")))]
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
fn queue_copy_external_image_to_texture(
&self,
queue: &Self::QueueId,
@ -1488,7 +1488,7 @@ pub(crate) trait DynContext: Debug + Send + Sync {
data_layout: ImageDataLayout,
size: Extent3d,
);
#[cfg(all(target_arch = "wasm32", not(feature = "emscripten")))]
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
fn queue_copy_external_image_to_texture(
&self,
queue: &ObjectId,
@ -2884,7 +2884,7 @@ where
Context::queue_write_texture(self, &queue, queue_data, texture, data, data_layout, size)
}
#[cfg(all(target_arch = "wasm32", not(feature = "emscripten")))]
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
fn queue_copy_external_image_to_texture(
&self,
queue: &ObjectId,

View File

@ -49,9 +49,9 @@ pub use wgt::{
// wasm-only types, we try to keep as many types non-platform
// specific, but these need to depend on web-sys.
#[cfg(all(target_arch = "wasm32", not(feature = "emscripten")))]
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
pub use wgt::{ExternalImageSource, ImageCopyExternalImage};
#[cfg(all(target_arch = "wasm32", not(feature = "emscripten")))]
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
static_assertions::assert_impl_all!(ExternalImageSource: Send, Sync);
/// Filter for error scopes.
@ -1341,7 +1341,11 @@ impl Instance {
/// # Safety
///
/// Refer to the creation of wgpu-hal Instance for every backend.
#[cfg(any(not(target_arch = "wasm32"), feature = "emscripten"))]
#[cfg(any(
not(target_arch = "wasm32"),
target_os = "emscripten",
feature = "webgl"
))]
pub unsafe fn from_hal<A: wgc::hub::HalApi>(hal_instance: A::Instance) -> Self {
Self {
context: Arc::new(unsafe {
@ -1360,7 +1364,11 @@ impl Instance {
/// - The raw instance handle returned must not be manually destroyed.
///
/// [`Instance`]: hal::Api::Instance
#[cfg(any(not(target_arch = "wasm32"), feature = "webgl"))]
#[cfg(any(
not(target_arch = "wasm32"),
target_os = "emscripten",
feature = "webgl"
))]
pub unsafe fn as_hal<A: wgc::hub::HalApi>(&self) -> Option<&A::Instance> {
unsafe {
self.context
@ -1380,7 +1388,11 @@ impl Instance {
/// # Safety
///
/// Refer to the creation of wgpu-core Instance.
#[cfg(any(not(target_arch = "wasm32"), feature = "webgl"))]
#[cfg(any(
not(target_arch = "wasm32"),
target_os = "emscripten",
feature = "webgl"
))]
pub unsafe fn from_core(core_instance: wgc::instance::Instance) -> Self {
Self {
context: Arc::new(unsafe {
@ -1394,7 +1406,7 @@ impl Instance {
/// # Arguments
///
/// - `backends` - Backends from which to enumerate adapters.
#[cfg(any(not(target_arch = "wasm32"), feature = "emscripten"))]
#[cfg(any(not(target_arch = "wasm32"), target_os = "emscripten"))]
pub fn enumerate_adapters(&self, backends: Backends) -> impl Iterator<Item = Adapter> {
let context = Arc::clone(&self.context);
self.context
@ -1433,7 +1445,7 @@ impl Instance {
/// # Safety
///
/// `hal_adapter` must be created from this instance internal handle.
#[cfg(any(not(target_arch = "wasm32"), feature = "emscripten"))]
#[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>,
@ -1575,7 +1587,7 @@ impl Instance {
///
/// - On WebGL2: Will return an error if the browser does not support WebGL2,
/// or declines to provide GPU access (such as due to a resource shortage).
#[cfg(all(target_arch = "wasm32", not(feature = "emscripten")))]
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
pub fn create_surface_from_canvas(
&self,
canvas: &web_sys::HtmlCanvasElement,
@ -1611,7 +1623,7 @@ impl Instance {
///
/// - On WebGL2: Will return an error if the browser does not support WebGL2,
/// or declines to provide GPU access (such as due to a resource shortage).
#[cfg(all(target_arch = "wasm32", not(feature = "emscripten")))]
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
pub fn create_surface_from_offscreen_canvas(
&self,
canvas: &web_sys::OffscreenCanvas,
@ -1659,7 +1671,7 @@ impl Instance {
}
/// Generates memory report.
#[cfg(any(not(target_arch = "wasm32"), feature = "emscripten"))]
#[cfg(any(not(target_arch = "wasm32"), target_os = "emscripten"))]
pub fn generate_report(&self) -> wgc::hub::GlobalReport {
self.context
.as_any()
@ -1730,7 +1742,7 @@ 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"), feature = "emscripten"))]
#[cfg(any(not(target_arch = "wasm32"), target_os = "emscripten"))]
pub unsafe fn create_device_from_hal<A: wgc::hub::HalApi>(
&self,
hal_device: hal::OpenDevice<A>,
@ -1780,7 +1792,11 @@ impl Adapter {
/// - The raw handle passed to the callback must not be manually destroyed.
///
/// [`A::Adapter`]: hal::Api::Adapter
#[cfg(any(not(target_arch = "wasm32"), feature = "webgl"))]
#[cfg(any(
not(target_arch = "wasm32"),
target_os = "emscripten",
feature = "webgl"
))]
pub unsafe fn as_hal<A: wgc::hub::HalApi, F: FnOnce(Option<&A::Adapter>) -> R, R>(
&self,
hal_adapter_callback: F,
@ -2125,7 +2141,11 @@ impl Device {
/// - `hal_texture` must be created from this device internal handle
/// - `hal_texture` must be created respecting `desc`
/// - `hal_texture` must be initialized
#[cfg(any(not(target_arch = "wasm32"), feature = "emscripten", feature = "webgl"))]
#[cfg(any(
not(target_arch = "wasm32"),
target_os = "emscripten",
feature = "webgl"
))]
pub unsafe fn create_texture_from_hal<A: wgc::hub::HalApi>(
&self,
hal_texture: A::Texture,
@ -2226,7 +2246,7 @@ 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"), feature = "emscripten"))]
#[cfg(any(not(target_arch = "wasm32"), target_os = "emscripten"))]
pub unsafe fn as_hal<A: wgc::hub::HalApi, F: FnOnce(Option<&A::Device>) -> R, R>(
&self,
hal_device_callback: F,
@ -2564,7 +2584,7 @@ impl Texture {
/// # Safety
///
/// - The raw handle obtained from the hal Texture must not be manually destroyed
#[cfg(any(not(target_arch = "wasm32"), feature = "emscripten"))]
#[cfg(any(not(target_arch = "wasm32"), target_os = "emscripten"))]
pub unsafe fn as_hal<A: wgc::hub::HalApi, F: FnOnce(Option<&A::Texture>)>(
&self,
hal_texture_callback: F,
@ -3961,7 +3981,7 @@ impl Queue {
}
/// Schedule a copy of data from `image` into `texture`.
#[cfg(all(target_arch = "wasm32", not(feature = "emscripten")))]
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
pub fn copy_external_image_to_texture(
&self,
source: &wgt::ImageCopyExternalImage,
@ -4173,7 +4193,7 @@ impl Surface {
/// # Safety
///
/// - The raw handle obtained from the hal Surface must not be manually destroyed
#[cfg(any(not(target_arch = "wasm32"), feature = "emscripten"))]
#[cfg(any(not(target_arch = "wasm32"), target_os = "emscripten"))]
pub unsafe fn as_hal_mut<A: wgc::hub::HalApi, F: FnOnce(Option<&mut A::Surface>) -> R, R>(
&mut self,
hal_surface_callback: F,

View File

@ -340,12 +340,18 @@ fn initialize_adapter() -> (Adapter, SurfaceGuard) {
let surface_guard;
let compatible_surface;
#[cfg(not(all(target_arch = "wasm32", feature = "webgl")))]
#[cfg(not(all(
target_arch = "wasm32",
any(target_os = "emscripten", feature = "webgl")
)))]
{
surface_guard = SurfaceGuard {};
compatible_surface = None;
}
#[cfg(all(target_arch = "wasm32", feature = "webgl"))]
#[cfg(all(
target_arch = "wasm32",
any(target_os = "emscripten", feature = "webgl")
))]
{
// On wasm, append a canvas to the document body for initializing the adapter
let canvas = create_html_canvas();
@ -371,14 +377,17 @@ fn initialize_adapter() -> (Adapter, SurfaceGuard) {
}
struct SurfaceGuard {
#[cfg(all(target_arch = "wasm32", feature = "webgl"))]
#[cfg(all(
target_arch = "wasm32",
any(target_os = "emscripten", feature = "webgl")
))]
canvas: web_sys::HtmlCanvasElement,
}
impl SurfaceGuard {
fn check_for_unreported_errors(&self) -> bool {
cfg_if::cfg_if! {
if #[cfg(all(target_arch = "wasm32", feature = "webgl"))] {
if #[cfg(all(target_arch = "wasm32", any(target_os = "emscripten", feature = "webgl")))] {
use wasm_bindgen::JsCast;
self.canvas
@ -396,14 +405,20 @@ impl SurfaceGuard {
}
}
#[cfg(all(target_arch = "wasm32", feature = "webgl"))]
#[cfg(all(
target_arch = "wasm32",
any(target_os = "emscripten", feature = "webgl")
))]
impl Drop for SurfaceGuard {
fn drop(&mut self) {
delete_html_canvas();
}
}
#[cfg(all(target_arch = "wasm32", feature = "webgl"))]
#[cfg(all(
target_arch = "wasm32",
any(target_os = "emscripten", feature = "webgl")
))]
fn create_html_canvas() -> web_sys::HtmlCanvasElement {
use wasm_bindgen::JsCast;
@ -419,7 +434,10 @@ fn create_html_canvas() -> web_sys::HtmlCanvasElement {
.expect("couldn't append canvas to document body")
}
#[cfg(all(target_arch = "wasm32", feature = "webgl"))]
#[cfg(all(
target_arch = "wasm32",
any(target_os = "emscripten", feature = "webgl")
))]
fn delete_html_canvas() {
if let Some(document) = web_sys::window().and_then(|win| win.document()) {
if let Some(element) = document.get_element_by_id(CANVAS_ID) {

View File

@ -1,4 +1,4 @@
#![cfg(all(target_arch = "wasm32", not(features = "emscripten")))]
#![cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
use std::num::NonZeroU32;