Add cfg_aliases to wgpu (#4935)

Co-authored-by: Connor Fitzgerald <connorwadefitzgerald@gmail.com>
This commit is contained in:
daxpedda 2024-01-02 22:05:51 +01:00 committed by GitHub
parent ec920e85d1
commit f004a9def9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 169 additions and 707 deletions

1
Cargo.lock generated
View File

@ -3994,6 +3994,7 @@ version = "0.18.0"
dependencies = [
"arrayvec 0.7.4",
"cfg-if",
"cfg_aliases",
"js-sys",
"log",
"naga",

View File

@ -168,6 +168,9 @@ workspace = true
features = ["clone"]
optional = true
[build-dependencies]
cfg_aliases.workspace = true
# used to test all the example shaders
[dev-dependencies.naga]
workspace = true

14
wgpu/build.rs Normal file
View File

@ -0,0 +1,14 @@
fn main() {
cfg_aliases::cfg_aliases! {
native: { not(target_arch = "wasm32") },
webgl: { all(target_arch = "wasm32", not(target_os = "emscripten"), feature = "webgl") },
webgpu: { all(target_arch = "wasm32", not(target_os = "emscripten"), not(feature = "webgl")) },
emscripten: { all(target_arch = "wasm32", target_os = "emscripten") },
send_sync: { any(
not(target_arch = "wasm32"),
all(feature = "fragile-send-sync-non-atomic-wasm", not(target_feature = "atomics"))
) },
dx12: { all(target_os = "windows", feature = "dx12") },
metal: { all(any(target_os = "ios", target_os = "macos"), feature = "metal") }
}
}

View File

@ -1,3 +1,5 @@
#![allow(clippy::mismatched_target_os)]
use crate::{
context::{ObjectId, Unused},
AdapterInfo, BindGroupDescriptor, BindGroupLayoutDescriptor, BindingResource, BufferBinding,
@ -229,7 +231,7 @@ impl Context {
self.0.generate_report()
}
#[cfg(all(any(target_os = "ios", target_os = "macos"), feature = "metal"))]
#[cfg(metal)]
pub unsafe fn create_surface_from_core_animation_layer(
&self,
layer: *mut std::ffi::c_void,
@ -241,7 +243,7 @@ impl Context {
}
}
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
#[cfg(any(webgpu, webgl))]
pub fn instance_create_surface_from_canvas(
&self,
canvas: web_sys::HtmlCanvasElement,
@ -253,7 +255,7 @@ impl Context {
})
}
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
#[cfg(any(webgpu, webgl))]
pub fn instance_create_surface_from_offscreen_canvas(
&self,
canvas: web_sys::OffscreenCanvas,
@ -265,7 +267,7 @@ impl Context {
})
}
#[cfg(all(target_os = "windows", feature = "dx12"))]
#[cfg(dx12)]
pub unsafe fn create_surface_from_visual(&self, visual: *mut std::ffi::c_void) -> Surface {
let id = unsafe { self.0.instance_create_surface_from_visual(visual, ()) };
Surface {
@ -274,7 +276,7 @@ impl Context {
}
}
#[cfg(all(target_os = "windows", feature = "dx12"))]
#[cfg(dx12)]
pub unsafe fn create_surface_from_surface_handle(
&self,
surface_handle: *mut std::ffi::c_void,
@ -289,7 +291,7 @@ impl Context {
}
}
#[cfg(all(target_os = "windows", feature = "dx12"))]
#[cfg(dx12)]
pub unsafe fn create_surface_from_swap_chain_panel(
&self,
swap_chain_panel: *mut std::ffi::c_void,
@ -1444,9 +1446,9 @@ impl crate::Context for Context {
Err(e) => panic!("Error in Device::create_render_bundle_encoder: {e}"),
}
}
#[cfg_attr(target_arch = "wasm32", allow(unused))]
#[cfg_attr(not(any(native, emscripten)), allow(unused))]
fn device_drop(&self, device: &Self::DeviceId, _device_data: &Self::DeviceData) {
#[cfg(any(not(target_arch = "wasm32"), target_os = "emscripten"))]
#[cfg(any(native, emscripten))]
{
let global = &self.0;
match wgc::gfx_select!(device => global.device_poll(*device, wgt::Maintain::Wait)) {
@ -2310,7 +2312,7 @@ impl crate::Context for Context {
}
}
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
#[cfg(any(webgpu, webgl))]
fn queue_copy_external_image_to_texture(
&self,
queue: &Self::QueueId,
@ -3167,21 +3169,9 @@ pub struct BufferMappedRange {
size: usize,
}
#[cfg(any(
not(target_arch = "wasm32"),
all(
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
)
))]
#[cfg(send_sync)]
unsafe impl Send for BufferMappedRange {}
#[cfg(any(
not(target_arch = "wasm32"),
all(
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
)
))]
#[cfg(send_sync)]
unsafe impl Sync for BufferMappedRange {}
impl crate::context::BufferMappedRange for BufferMappedRange {

View File

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

View File

@ -57,50 +57,26 @@ impl<T> From<Identified<T>> for ObjectId {
#[derive(Clone, Debug)]
pub(crate) struct Sendable<T>(T);
#[cfg(all(
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
))]
#[cfg(send_sync)]
unsafe impl<T> Send for Sendable<T> {}
#[cfg(all(
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
))]
#[cfg(send_sync)]
unsafe impl<T> Sync for Sendable<T> {}
#[derive(Clone, Debug)]
pub(crate) struct Identified<T>(std::num::NonZeroU64, PhantomData<T>);
#[cfg(all(
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
))]
#[cfg(send_sync)]
unsafe impl<T> Send for Identified<T> {}
#[cfg(all(
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
))]
#[cfg(send_sync)]
unsafe impl<T> Sync for Identified<T> {}
pub(crate) struct Context(web_sys::Gpu);
#[cfg(all(
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
))]
#[cfg(send_sync)]
unsafe impl Send for Context {}
#[cfg(all(
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
))]
#[cfg(send_sync)]
unsafe impl Sync for Context {}
#[cfg(all(
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
))]
#[cfg(send_sync)]
unsafe impl Send for BufferMappedRange {}
#[cfg(all(
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
))]
#[cfg(send_sync)]
unsafe impl Sync for BufferMappedRange {}
impl fmt::Debug for Context {
@ -157,10 +133,7 @@ impl<F, M> MakeSendFuture<F, M> {
}
}
#[cfg(all(
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
))]
#[cfg(send_sync)]
unsafe impl<F, M> Send for MakeSendFuture<F, M> {}
fn map_texture_format(texture_format: wgt::TextureFormat) -> web_sys::GpuTextureFormat {

View File

@ -321,10 +321,7 @@ pub trait Context: Debug + WasmNotSendSync + Sized {
buffer_data: &Self::BufferData,
sub_range: Range<BufferAddress>,
) -> Box<dyn BufferMappedRange>;
#[cfg(all(
target_arch = "wasm32",
not(any(target_os = "emscripten", feature = "webgl"))
))]
#[cfg(webgpu)]
fn buffer_get_mapped_range_as_array_buffer(
&self,
buffer: &Self::BufferId,
@ -585,7 +582,7 @@ pub trait Context: Debug + WasmNotSendSync + Sized {
data_layout: ImageDataLayout,
size: Extent3d,
);
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
#[cfg(any(webgl, webgpu))]
fn queue_copy_external_image_to_texture(
&self,
queue: &Self::QueueId,
@ -1068,13 +1065,7 @@ impl ObjectId {
}
}
#[cfg(any(
not(target_arch = "wasm32"),
all(
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
)
))]
#[cfg(send_sync)]
static_assertions::assert_impl_all!(ObjectId: Send, Sync);
pub(crate) fn downcast_ref<T: Debug + WasmNotSendSync + 'static>(data: &crate::Data) -> &T {
@ -1115,109 +1106,37 @@ pub(crate) struct DeviceRequest {
pub queue_data: Box<crate::Data>,
}
#[cfg(any(
not(target_arch = "wasm32"),
all(
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
)
))]
#[cfg(send_sync)]
pub type BufferMapCallback = Box<dyn FnOnce(Result<(), BufferAsyncError>) + Send + 'static>;
#[cfg(not(any(
not(target_arch = "wasm32"),
all(
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
)
)))]
#[cfg(not(send_sync))]
pub type BufferMapCallback = Box<dyn FnOnce(Result<(), BufferAsyncError>) + 'static>;
#[cfg(any(
not(target_arch = "wasm32"),
all(
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
)
))]
#[cfg(send_sync)]
pub(crate) type AdapterRequestDeviceFuture =
Box<dyn Future<Output = Result<DeviceRequest, RequestDeviceError>> + Send>;
#[cfg(not(any(
not(target_arch = "wasm32"),
all(
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
)
)))]
#[cfg(not(send_sync))]
pub(crate) type AdapterRequestDeviceFuture =
Box<dyn Future<Output = Result<DeviceRequest, RequestDeviceError>>>;
#[cfg(any(
not(target_arch = "wasm32"),
all(
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
)
))]
#[cfg(send_sync)]
pub type InstanceRequestAdapterFuture =
Box<dyn Future<Output = Option<(ObjectId, Box<crate::Data>)>> + Send>;
#[cfg(not(any(
not(target_arch = "wasm32"),
all(
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
)
)))]
#[cfg(not(send_sync))]
pub type InstanceRequestAdapterFuture =
Box<dyn Future<Output = Option<(ObjectId, Box<crate::Data>)>>>;
#[cfg(any(
not(target_arch = "wasm32"),
all(
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
)
))]
#[cfg(send_sync)]
pub type DevicePopErrorFuture = Box<dyn Future<Output = Option<Error>> + Send>;
#[cfg(not(any(
not(target_arch = "wasm32"),
all(
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
)
)))]
#[cfg(not(send_sync))]
pub type DevicePopErrorFuture = Box<dyn Future<Output = Option<Error>>>;
#[cfg(any(
not(target_arch = "wasm32"),
all(
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
)
))]
#[cfg(send_sync)]
pub type SubmittedWorkDoneCallback = Box<dyn FnOnce() + Send + 'static>;
#[cfg(not(any(
not(target_arch = "wasm32"),
all(
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
)
)))]
#[cfg(not(send_sync))]
pub type SubmittedWorkDoneCallback = Box<dyn FnOnce() + 'static>;
#[cfg(any(
not(target_arch = "wasm32"),
all(
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
)
))]
#[cfg(send_sync)]
pub type DeviceLostCallback = Box<dyn Fn(DeviceLostReason, String) + Send + 'static>;
#[cfg(not(any(
not(target_arch = "wasm32"),
all(
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
)
)))]
#[cfg(not(send_sync))]
pub type DeviceLostCallback = Box<dyn Fn(DeviceLostReason, String) + 'static>;
/// An object safe variant of [`Context`] implemented by all types that implement [`Context`].
@ -1427,10 +1346,7 @@ pub(crate) trait DynContext: Debug + WasmNotSendSync {
buffer_data: &crate::Data,
sub_range: Range<BufferAddress>,
) -> Box<dyn BufferMappedRange>;
#[cfg(all(
target_arch = "wasm32",
not(any(target_os = "emscripten", feature = "webgl"))
))]
#[cfg(webgpu)]
fn buffer_get_mapped_range_as_array_buffer(
&self,
buffer: &ObjectId,
@ -1651,7 +1567,7 @@ pub(crate) trait DynContext: Debug + WasmNotSendSync {
data_layout: ImageDataLayout,
size: Extent3d,
);
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
#[cfg(any(webgpu, webgl))]
fn queue_copy_external_image_to_texture(
&self,
queue: &ObjectId,
@ -2551,10 +2467,7 @@ where
Context::buffer_get_mapped_range(self, &buffer, buffer_data, sub_range)
}
#[cfg(all(
target_arch = "wasm32",
not(any(target_os = "emscripten", feature = "webgl"))
))]
#[cfg(webgpu)]
fn buffer_get_mapped_range_as_array_buffer(
&self,
buffer: &ObjectId,
@ -3098,7 +3011,7 @@ where
Context::queue_write_texture(self, &queue, queue_data, texture, data, data_layout, size)
}
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
#[cfg(any(webgpu, webgl))]
fn queue_copy_external_image_to_texture(
&self,
queue: &ObjectId,

File diff suppressed because it is too large Load Diff

View File

@ -2,10 +2,11 @@ use wgt::{Backends, PowerPreference, RequestAdapterOptions};
use crate::{Adapter, Instance, Surface};
#[cfg(any(not(target_arch = "wasm32"), feature = "wgc"))]
#[cfg(not(webgpu))]
#[cfg_attr(docsrs, doc(cfg(all())))]
pub use wgc::instance::parse_backends_from_comma_list;
/// Always returns WEBGPU on wasm over webgpu.
#[cfg(all(target_arch = "wasm32", not(feature = "wgc")))]
#[cfg(webgpu)]
pub fn parse_backends_from_comma_list(_string: &str) -> Backends {
Backends::BROWSER_WEBGPU
}
@ -37,7 +38,7 @@ pub fn power_preference_from_env() -> Option<PowerPreference> {
}
/// Initialize the adapter obeying the WGPU_ADAPTER_NAME environment variable.
#[cfg(not(target_arch = "wasm32"))]
#[cfg(native)]
pub fn initialize_adapter_from_env(
instance: &Instance,
compatible_surface: Option<&Surface<'_>>,
@ -69,7 +70,7 @@ pub fn initialize_adapter_from_env(
}
/// Initialize the adapter obeying the WGPU_ADAPTER_NAME environment variable.
#[cfg(target_arch = "wasm32")]
#[cfg(not(native))]
pub fn initialize_adapter_from_env(
_instance: &Instance,
_compatible_surface: Option<&Surface<'_>>,