Skip to content

Commit

Permalink
Add cfg_aliases to wgpu-hal
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Jan 12, 2024
1 parent 4c79bfa commit 7011ced
Show file tree
Hide file tree
Showing 15 changed files with 126 additions and 193 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions wgpu-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ path = "../naga"
version = "0.14.0"
features = ["clone"]

[build-dependencies]
cfg_aliases.workspace = true

# DEV dependencies
[dev-dependencies.naga]
path = "../naga"
Expand Down
15 changes: 15 additions & 0 deletions wgpu-hal/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
fn main() {
cfg_aliases::cfg_aliases! {
native: { not(target_arch = "wasm32") },
send_sync: { any(
not(target_arch = "wasm32"),
all(feature = "fragile-send-sync-non-atomic-wasm", not(target_feature = "atomics"))
) },
webgl: { all(target_arch = "wasm32", not(target_os = "emscripten"), gles) },
emscripten: { all(target_os = "emscripten", gles) },
dx12: { all(target_os = "windows", feature = "dx12") },
gles: { all(feature = "gles") },
metal: { all(any(target_os = "ios", target_os = "macos"), feature = "metal") },
vulkan: { all(not(target_arch = "wasm32"), feature = "vulkan") }
}
}
2 changes: 0 additions & 2 deletions wgpu-hal/src/auxil/dxgi/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ use super::result::HResult as _;

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum DxgiFactoryType {
#[cfg(feature = "dx11")]
Factory1,
Factory2,
Factory4,
Factory6,
Expand Down
4 changes: 2 additions & 2 deletions wgpu-hal/src/auxil/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg(all(feature = "dx12", windows))]
#[cfg(dx12)]
pub(super) mod dxgi;

#[cfg(all(not(target_arch = "wasm32"), feature = "renderdoc"))]
#[cfg(all(native, feature = "renderdoc"))]
pub(super) mod renderdoc;

pub mod db {
Expand Down
2 changes: 1 addition & 1 deletion wgpu-hal/src/empty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,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(target_os = "emscripten")))]
#[cfg(webgl)]
unsafe fn copy_external_image_to_texture<T>(
&mut self,
src: &wgt::ImageCopyExternalImage,
Expand Down
46 changes: 20 additions & 26 deletions wgpu-hal/src/gles/adapter.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::mismatched_target_os)]

use glow::HasContext;
use parking_lot::Mutex;
use std::sync::{atomic::AtomicU8, Arc};
Expand Down Expand Up @@ -198,14 +200,14 @@ impl super::Adapter {
let (vendor_const, renderer_const) = if extensions.contains("WEBGL_debug_renderer_info") {
// emscripten doesn't enable "WEBGL_debug_renderer_info" extension by default. so, we do it manually.
// See https://github.com/gfx-rs/wgpu/issues/3245 for context
#[cfg(target_os = "emscripten")]
#[cfg(emscripten)]
if unsafe { super::emscripten::enable_extension("WEBGL_debug_renderer_info\0") } {
(GL_UNMASKED_VENDOR_WEBGL, GL_UNMASKED_RENDERER_WEBGL)
} else {
(glow::VENDOR, glow::RENDERER)
}
// glow already enables WEBGL_debug_renderer_info on wasm32-unknown-unknown target by default.
#[cfg(not(target_os = "emscripten"))]
#[cfg(not(emscripten))]
(GL_UNMASKED_VENDOR_WEBGL, GL_UNMASKED_RENDERER_WEBGL)
} else {
(glow::VENDOR, glow::RENDERER)
Expand Down Expand Up @@ -282,7 +284,7 @@ impl super::Adapter {
let value = sl_major as u16 * 100 + sl_minor as u16 * 10;
naga::back::glsl::Version::Embedded {
version: value,
is_webgl: cfg!(target_arch = "wasm32"),
is_webgl: cfg!(any(webgl, emscripten)),
}
}
};
Expand Down Expand Up @@ -407,16 +409,16 @@ impl super::Adapter {
}
downlevel_flags.set(
wgt::DownlevelFlags::BUFFER_BINDINGS_NOT_16_BYTE_ALIGNED,
!(cfg!(target_arch = "wasm32") || is_angle),
!(cfg!(any(webgl, emscripten)) || is_angle),
);
// see https://registry.khronos.org/webgl/specs/latest/2.0/#BUFFER_OBJECT_BINDING
downlevel_flags.set(
wgt::DownlevelFlags::UNRESTRICTED_INDEX_BUFFER,
!cfg!(target_arch = "wasm32"),
!cfg!(any(webgl, emscripten)),
);
downlevel_flags.set(
wgt::DownlevelFlags::UNRESTRICTED_EXTERNAL_TEXTURE_COPIES,
!cfg!(target_arch = "wasm32"),
!cfg!(any(webgl, emscripten)),
);
downlevel_flags.set(
wgt::DownlevelFlags::FULL_DRAW_INDEX_UINT32,
Expand Down Expand Up @@ -490,7 +492,7 @@ impl super::Adapter {
"EXT_texture_compression_rgtc",
"EXT_texture_compression_bptc",
];
let bcn_exts = if cfg!(target_arch = "wasm32") {
let bcn_exts = if cfg!(any(webgl, emscripten)) {
&webgl_bcn_exts[..]
} else if es_ver.is_some() {
&gles_bcn_exts[..]
Expand All @@ -501,7 +503,7 @@ impl super::Adapter {
wgt::Features::TEXTURE_COMPRESSION_BC,
bcn_exts.iter().all(|&ext| extensions.contains(ext)),
);
let has_etc = if cfg!(target_arch = "wasm32") {
let has_etc = if cfg!(any(webgl, emscripten)) {
extensions.contains("WEBGL_compressed_texture_etc")
} else {
// This is a required part of GLES3, but not part of Desktop GL at all.
Expand All @@ -513,7 +515,7 @@ impl super::Adapter {
if extensions.contains("WEBGL_compressed_texture_astc")
|| extensions.contains("GL_OES_texture_compression_astc")
{
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
#[cfg(webgl)]
{
if context
.glow_context
Expand All @@ -529,7 +531,7 @@ impl super::Adapter {
}
}

#[cfg(any(not(target_arch = "wasm32"), target_os = "emscripten"))]
#[cfg(any(native, emscripten))]
{
features.insert(wgt::Features::TEXTURE_COMPRESSION_ASTC);
features.insert(wgt::Features::TEXTURE_COMPRESSION_ASTC_HDR);
Expand Down Expand Up @@ -582,11 +584,11 @@ impl super::Adapter {
);
private_caps.set(
super::PrivateCapabilities::INDEX_BUFFER_ROLE_CHANGE,
!cfg!(target_arch = "wasm32"),
!cfg!(any(webgl, emscripten)),
);
private_caps.set(
super::PrivateCapabilities::GET_BUFFER_SUB_DATA,
cfg!(target_arch = "wasm32") || full_ver.is_some(),
cfg!(any(webgl, emscripten)) || full_ver.is_some(),
);
let color_buffer_float = extensions.contains("GL_EXT_color_buffer_float")
|| extensions.contains("GL_ARB_color_buffer_float")
Expand Down Expand Up @@ -759,7 +761,7 @@ impl super::Adapter {

workarounds.set(
super::Workarounds::EMULATE_BUFFER_MAP,
cfg!(target_arch = "wasm32"),
cfg!(any(webgl, emscripten)),
);

let r = renderer.to_lowercase();
Expand Down Expand Up @@ -917,7 +919,7 @@ impl crate::Adapter<super::Api> for super::Adapter {
device: super::Device {
shared: Arc::clone(&self.shared),
main_vao,
#[cfg(all(not(target_arch = "wasm32"), feature = "renderdoc"))]
#[cfg(all(native, feature = "renderdoc"))]
render_doc: Default::default(),
},
queue: super::Queue {
Expand Down Expand Up @@ -1112,13 +1114,13 @@ impl crate::Adapter<super::Api> for super::Adapter {
if surface.presentable {
let mut formats = vec![
wgt::TextureFormat::Rgba8Unorm,
#[cfg(not(target_arch = "wasm32"))]
#[cfg(native)]
wgt::TextureFormat::Bgra8Unorm,
];
if surface.supports_srgb() {
formats.extend([
wgt::TextureFormat::Rgba8UnormSrgb,
#[cfg(not(target_arch = "wasm32"))]
#[cfg(native)]
wgt::TextureFormat::Bgra8UnormSrgb,
])
}
Expand Down Expand Up @@ -1178,17 +1180,9 @@ impl super::AdapterShared {
}
}

#[cfg(all(
target_arch = "wasm32",
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
))]
#[cfg(send_sync)]
unsafe impl Sync for super::Adapter {}
#[cfg(all(
target_arch = "wasm32",
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
))]
#[cfg(send_sync)]
unsafe impl Send for super::Adapter {}

#[cfg(test)]
Expand Down
4 changes: 2 additions & 2 deletions wgpu-hal/src/gles/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ impl crate::CommandEncoder<super::Api> for super::CommandEncoder {
}
}

#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
#[cfg(webgl)]
unsafe fn copy_external_image_to_texture<T>(
&mut self,
src: &wgt::ImageCopyExternalImage,
Expand Down Expand Up @@ -507,7 +507,7 @@ impl crate::CommandEncoder<super::Api> for super::CommandEncoder {
.iter()
.filter_map(|at| at.as_ref())
.any(|at| match at.target.view.inner {
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
#[cfg(webgl)]
super::TextureInner::ExternalFramebuffer { .. } => true,
_ => false,
});
Expand Down
42 changes: 18 additions & 24 deletions wgpu-hal/src/gles/device.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::mismatched_target_os)]

use super::{conv, PrivateCapabilities};
use crate::auxil::map_naga_stage;
use glow::HasContext;
Expand All @@ -9,7 +11,7 @@ use std::{
};

use arrayvec::ArrayVec;
#[cfg(not(target_arch = "wasm32"))]
#[cfg(native)]
use std::mem;
use std::sync::atomic::Ordering;

Expand Down Expand Up @@ -115,7 +117,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"), target_os = "emscripten"))]
#[cfg(any(native, emscripten))]
pub unsafe fn texture_from_raw(
&self,
name: std::num::NonZeroU32,
Expand Down Expand Up @@ -143,7 +145,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"), target_os = "emscripten"))]
#[cfg(any(native, emscripten))]
pub unsafe fn texture_from_raw_renderbuffer(
&self,
name: std::num::NonZeroU32,
Expand Down Expand Up @@ -176,7 +178,7 @@ impl super::Device {
};

let raw = unsafe { gl.create_shader(target) }.unwrap();
#[cfg(not(target_arch = "wasm32"))]
#[cfg(native)]
if gl.supports_debug() {
//TODO: remove all transmutes from `object_label`
// https://github.com/grovesNL/glow/issues/186
Expand Down Expand Up @@ -342,7 +344,7 @@ impl super::Device {
naga::back::glsl::Version::Desktop(version) => format!("{version}"),
};
let program = unsafe { gl.create_program() }.unwrap();
#[cfg(not(target_arch = "wasm32"))]
#[cfg(native)]
if let Some(label) = label {
if private_caps.contains(PrivateCapabilities::DEBUG_FNS) {
let name = unsafe { mem::transmute(program) };
Expand Down Expand Up @@ -591,7 +593,7 @@ impl crate::Device<super::Api> for super::Device {
}
//TODO: do we need `glow::MAP_UNSYNCHRONIZED_BIT`?

#[cfg(not(target_arch = "wasm32"))]
#[cfg(native)]
if let Some(label) = desc.label {
if self
.shared
Expand Down Expand Up @@ -734,7 +736,7 @@ impl crate::Device<super::Api> for super::Device {
};
}

#[cfg(not(target_arch = "wasm32"))]
#[cfg(native)]
if let Some(label) = desc.label {
if self
.shared
Expand Down Expand Up @@ -902,7 +904,7 @@ impl crate::Device<super::Api> for super::Device {
};
}

#[cfg(not(target_arch = "wasm32"))]
#[cfg(native)]
if let Some(label) = desc.label {
if self
.shared
Expand Down Expand Up @@ -939,7 +941,7 @@ impl crate::Device<super::Api> for super::Device {
super::TextureInner::Texture { raw, .. } => {
unsafe { gl.delete_texture(raw) };
}
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
#[cfg(webgl)]
super::TextureInner::ExternalFramebuffer { .. } => {}
}
}
Expand Down Expand Up @@ -1045,7 +1047,7 @@ impl crate::Device<super::Api> for super::Device {
};
}

#[cfg(not(target_arch = "wasm32"))]
#[cfg(native)]
if let Some(label) = desc.label {
if self
.shared
Expand Down Expand Up @@ -1432,7 +1434,7 @@ impl crate::Device<super::Api> for super::Device {
) -> Result<bool, crate::DeviceError> {
if fence.last_completed < wait_value {
let gl = &self.shared.context.lock();
let timeout_ns = if cfg!(target_arch = "wasm32") {
let timeout_ns = if cfg!(any(webgl, emscripten)) {
0
} else {
(timeout_ms as u64 * 1_000_000).min(!0u32 as u64)
Expand All @@ -1446,7 +1448,7 @@ impl crate::Device<super::Api> for super::Device {
gl.client_wait_sync(sync, glow::SYNC_FLUSH_COMMANDS_BIT, timeout_ns as i32)
} {
// for some reason firefox returns WAIT_FAILED, to investigate
#[cfg(target_arch = "wasm32")]
#[cfg(any(webgl, emscripten))]
glow::WAIT_FAILED => {
log::warn!("wait failed!");
Ok(false)
Expand All @@ -1461,7 +1463,7 @@ impl crate::Device<super::Api> for super::Device {
}

unsafe fn start_capture(&self) -> bool {
#[cfg(all(not(target_arch = "wasm32"), feature = "renderdoc"))]
#[cfg(all(native, feature = "renderdoc"))]
return unsafe {
self.render_doc
.start_frame_capture(self.shared.context.raw_context(), ptr::null_mut())
Expand All @@ -1470,7 +1472,7 @@ impl crate::Device<super::Api> for super::Device {
false
}
unsafe fn stop_capture(&self) {
#[cfg(all(not(target_arch = "wasm32"), feature = "renderdoc"))]
#[cfg(all(native, feature = "renderdoc"))]
unsafe {
self.render_doc
.end_frame_capture(ptr::null_mut(), ptr::null_mut())
Expand All @@ -1497,15 +1499,7 @@ impl crate::Device<super::Api> for super::Device {
unsafe fn destroy_acceleration_structure(&self, _acceleration_structure: ()) {}
}

#[cfg(all(
target_arch = "wasm32",
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
))]
#[cfg(send_sync)]
unsafe impl Sync for super::Device {}
#[cfg(all(
target_arch = "wasm32",
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
))]
#[cfg(send_sync)]
unsafe impl Send for super::Device {}
Loading

0 comments on commit 7011ced

Please sign in to comment.