Skip to content

Commit

Permalink
Context Rework
Browse files Browse the repository at this point in the history
  • Loading branch information
cwfitzgerald committed Dec 3, 2024
1 parent 0b6571a commit 0e53174
Show file tree
Hide file tree
Showing 42 changed files with 4,789 additions and 7,438 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ jobs:

name: Test ${{ matrix.name }}
runs-on: ${{ matrix.os }}
needs: [check]
# needs: [check]

steps:
- name: checkout repo
Expand Down
2 changes: 1 addition & 1 deletion tests/tests/external_texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ static IMAGE_BITMAP_IMPORT: GpuTestConfiguration =
origin: src_origin,
flip_y: src_flip_y,
},
wgpu::CopyExternalImageDestInfo {
wgt::CopyExternalImageDestInfo {
texture: &texture,
mip_level: 0,
origin: dest_origin,
Expand Down
8 changes: 7 additions & 1 deletion wgpu-core/src/global.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::Arc;
use std::{fmt, sync::Arc};

use crate::{
hal_api::HalApi,
Expand Down Expand Up @@ -85,6 +85,12 @@ impl Global {
}
}

impl fmt::Debug for Global {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Global").finish()
}
}

impl Drop for Global {
fn drop(&mut self) {
profiling::scope!("Global::drop");
Expand Down
2 changes: 1 addition & 1 deletion wgpu/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ fn main() {
webgl: { all(target_arch = "wasm32", not(target_os = "emscripten"), feature = "webgl") },
webgpu: { all(target_arch = "wasm32", not(target_os = "emscripten"), feature = "webgpu") },
Emscripten: { all(target_arch = "wasm32", target_os = "emscripten") },
wgpu_core: { any(native, webgl, emscripten) },
wgpu_core: { any(native, webgl, Emscripten) },
send_sync: { any(
not(target_arch = "wasm32"),
all(feature = "fragile-send-sync-non-atomic-wasm", not(target_feature = "atomics"))
Expand Down
115 changes: 33 additions & 82 deletions wgpu/src/api/adapter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::{future::Future, sync::Arc, thread};
use std::future::Future;

use crate::context::{DeviceRequest, DynContext};
use crate::*;

/// Handle to a physical graphics and/or compute device.
Expand All @@ -16,19 +15,12 @@ use crate::*;
/// Corresponds to [WebGPU `GPUAdapter`](https://gpuweb.github.io/gpuweb/#gpu-adapter).
#[derive(Debug)]
pub struct Adapter {
pub(crate) context: Arc<C>,
pub(crate) data: Box<Data>,
pub(crate) inner: dispatch::DispatchAdapter,
}
#[cfg(send_sync)]
static_assertions::assert_impl_all!(Adapter: Send, Sync);

impl Drop for Adapter {
fn drop(&mut self) {
if !thread::panicking() {
self.context.adapter_drop(self.data.as_ref())
}
}
}
super::impl_eq_ord_hash!(Adapter => .inner);

pub use wgt::RequestAdapterOptions as RequestAdapterOptionsBase;
/// Additional information required when requesting an adapter.
Expand Down Expand Up @@ -71,31 +63,11 @@ impl Adapter {
desc: &DeviceDescriptor<'_>,
trace_path: Option<&std::path::Path>,
) -> impl Future<Output = Result<(Device, Queue), RequestDeviceError>> + WasmNotSend {
let context = Arc::clone(&self.context);
let device = DynContext::adapter_request_device(
&*self.context,
self.data.as_ref(),
desc,
trace_path,
);
let device = self.inner.request_device(desc, trace_path);
async move {
device.await.map(
|DeviceRequest {
device_data,
queue_data,
}| {
(
Device {
context: Arc::clone(&context),
data: device_data,
},
Queue {
context,
data: queue_data,
},
)
},
)
device
.await
.map(|(device, queue)| (Device { inner: device }, Queue { inner: queue }))
}
}

Expand All @@ -112,33 +84,21 @@ impl Adapter {
desc: &DeviceDescriptor<'_>,
trace_path: Option<&std::path::Path>,
) -> Result<(Device, Queue), RequestDeviceError> {
let context = Arc::clone(&self.context);
unsafe {
self.context
.as_any()
.downcast_ref::<crate::backend::ContextWgpuCore>()
// Part of the safety requirements is that the device was generated from the same adapter.
// Therefore, unwrap is fine here since only WgpuCoreContext based adapters have the ability to create hal devices.
.unwrap()
.create_device_from_hal(
crate::context::downcast_ref(self.data.as_ref()),
hal_device,
desc,
trace_path,
)
}
.map(|(device, queue)| {
(
Device {
context: Arc::clone(&context),
data: Box::new(device),
},
Queue {
context,
data: Box::new(queue),
},
)
})
let core_adapter = self.inner.as_core();
let (device, queue) = unsafe {
core_adapter
.context
.create_device_from_hal(core_adapter, hal_device, desc, trace_path)
}?;

Ok((
Device {
inner: device.into(),
},
Queue {
inner: queue.into(),
},
))
}

/// Apply a callback to this `Adapter`'s underlying backend adapter.
Expand All @@ -165,16 +125,11 @@ impl Adapter {
&self,
hal_adapter_callback: F,
) -> R {
if let Some(ctx) = self
.context
.as_any()
.downcast_ref::<crate::backend::ContextWgpuCore>()
{
if let Some(adapter) = self.inner.as_core_opt() {
unsafe {
ctx.adapter_as_hal::<A, F, R>(
crate::context::downcast_ref(self.data.as_ref()),
hal_adapter_callback,
)
adapter
.context
.adapter_as_hal::<A, F, R>(adapter, hal_adapter_callback)
}
} else {
hal_adapter_callback(None)
Expand All @@ -183,39 +138,35 @@ impl Adapter {

/// Returns whether this adapter may present to the passed surface.
pub fn is_surface_supported(&self, surface: &Surface<'_>) -> bool {
DynContext::adapter_is_surface_supported(
&*self.context,
self.data.as_ref(),
surface.surface_data.as_ref(),
)
self.inner.is_surface_supported(&surface.inner)
}

/// The features which can be used to create devices on this adapter.
pub fn features(&self) -> Features {
DynContext::adapter_features(&*self.context, self.data.as_ref())
self.inner.features()
}

/// The best limits which can be used to create devices on this adapter.
pub fn limits(&self) -> Limits {
DynContext::adapter_limits(&*self.context, self.data.as_ref())
self.inner.limits()
}

/// Get info about the adapter itself.
pub fn get_info(&self) -> AdapterInfo {
DynContext::adapter_get_info(&*self.context, self.data.as_ref())
self.inner.get_info()
}

/// Get info about the adapter itself.
pub fn get_downlevel_capabilities(&self) -> DownlevelCapabilities {
DynContext::adapter_downlevel_capabilities(&*self.context, self.data.as_ref())
self.inner.downlevel_capabilities()
}

/// Returns the features supported for a given texture format by this adapter.
///
/// Note that the WebGPU spec further restricts the available usages/features.
/// To disable these restrictions on a device, request the [`Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES`] feature.
pub fn get_texture_format_features(&self, format: TextureFormat) -> TextureFormatFeatures {
DynContext::adapter_get_texture_format_features(&*self.context, self.data.as_ref(), format)
self.inner.get_texture_format_features(format)
}

/// Generates a timestamp using the clock used by the presentation engine.
Expand All @@ -240,6 +191,6 @@ impl Adapter {
//
/// [Instant]: std::time::Instant
pub fn get_presentation_timestamp(&self) -> PresentationTimestamp {
DynContext::adapter_get_presentation_timestamp(&*self.context, self.data.as_ref())
self.inner.get_presentation_timestamp()
}
}
15 changes: 2 additions & 13 deletions wgpu/src/api/bind_group.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::{sync::Arc, thread};

use crate::*;

/// Handle to a binding group.
Expand All @@ -12,21 +10,12 @@ use crate::*;
/// Corresponds to [WebGPU `GPUBindGroup`](https://gpuweb.github.io/gpuweb/#gpubindgroup).
#[derive(Debug)]
pub struct BindGroup {
pub(crate) context: Arc<C>,
pub(crate) data: Box<Data>,
pub(crate) inner: dispatch::DispatchBindGroup,
}
#[cfg(send_sync)]
static_assertions::assert_impl_all!(BindGroup: Send, Sync);

super::impl_partialeq_eq_hash!(BindGroup);

impl Drop for BindGroup {
fn drop(&mut self) {
if !thread::panicking() {
self.context.bind_group_drop(self.data.as_ref());
}
}
}
super::impl_eq_ord_hash!(BindGroup => .inner);

/// Resource that can be bound to a pipeline.
///
Expand Down
15 changes: 2 additions & 13 deletions wgpu/src/api/bind_group_layout.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::{sync::Arc, thread};

use crate::*;

/// Handle to a binding group layout.
Expand All @@ -15,21 +13,12 @@ use crate::*;
/// https://gpuweb.github.io/gpuweb/#gpubindgrouplayout).
#[derive(Debug)]
pub struct BindGroupLayout {
pub(crate) context: Arc<C>,
pub(crate) data: Box<Data>,
pub(crate) inner: dispatch::DispatchBindGroupLayout,
}
#[cfg(send_sync)]
static_assertions::assert_impl_all!(BindGroupLayout: Send, Sync);

super::impl_partialeq_eq_hash!(BindGroupLayout);

impl Drop for BindGroupLayout {
fn drop(&mut self) {
if !thread::panicking() {
self.context.bind_group_layout_drop(self.data.as_ref());
}
}
}
super::impl_eq_ord_hash!(BindGroupLayout => .inner);

/// Describes a [`BindGroupLayout`].
///
Expand Down
Loading

0 comments on commit 0e53174

Please sign in to comment.