Skip to content

Commit 30e66e4

Browse files
Wumpfbircni
andauthored
Wgpu resources are no longer wrapped in Arc (since they are now Clone) (#5612)
Co-authored-by: Nicolas <[email protected]>
1 parent cf965aa commit 30e66e4

File tree

6 files changed

+37
-45
lines changed

6 files changed

+37
-45
lines changed

crates/egui-wgpu/src/lib.rs

+11-23
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,20 @@ pub enum WgpuError {
6363
#[derive(Clone)]
6464
pub struct RenderState {
6565
/// Wgpu adapter used for rendering.
66-
pub adapter: Arc<wgpu::Adapter>,
66+
pub adapter: wgpu::Adapter,
6767

6868
/// All the available adapters.
6969
///
7070
/// This is not available on web.
7171
/// On web, we always select WebGPU is available, then fall back to WebGL if not.
72-
// TODO(gfx-rs/wgpu#6665): Remove layer of `Arc` here once we update to wgpu 24
7372
#[cfg(not(target_arch = "wasm32"))]
74-
pub available_adapters: Arc<[Arc<wgpu::Adapter>]>,
73+
pub available_adapters: Vec<wgpu::Adapter>,
7574

7675
/// Wgpu device used for rendering, created from the adapter.
77-
pub device: Arc<wgpu::Device>,
76+
pub device: wgpu::Device,
7877

7978
/// Wgpu queue used for rendering, created from the adapter.
80-
pub queue: Arc<wgpu::Queue>,
79+
pub queue: wgpu::Queue,
8180

8281
/// The target texture format used for presenting to the window.
8382
pub target_format: wgpu::TextureFormat,
@@ -90,8 +89,8 @@ async fn request_adapter(
9089
instance: &wgpu::Instance,
9190
power_preference: wgpu::PowerPreference,
9291
compatible_surface: Option<&wgpu::Surface<'_>>,
93-
_available_adapters: &[Arc<wgpu::Adapter>],
94-
) -> Result<Arc<wgpu::Adapter>, WgpuError> {
92+
_available_adapters: &[wgpu::Adapter],
93+
) -> Result<wgpu::Adapter, WgpuError> {
9594
profiling::function_scope!();
9695

9796
let adapter = instance
@@ -149,10 +148,7 @@ async fn request_adapter(
149148
);
150149
}
151150

152-
// On wasm, depending on feature flags, wgpu objects may or may not implement sync.
153-
// It doesn't make sense to switch to Rc for that special usecase, so simply disable the lint.
154-
#[allow(clippy::arc_with_non_send_sync)]
155-
Ok(Arc::new(adapter))
151+
Ok(adapter)
156152
}
157153

158154
impl RenderState {
@@ -179,12 +175,7 @@ impl RenderState {
179175
wgpu::Backends::all()
180176
};
181177

182-
instance
183-
.enumerate_adapters(backends)
184-
// TODO(gfx-rs/wgpu#6665): Remove layer of `Arc` here once we update to wgpu 24.
185-
.into_iter()
186-
.map(Arc::new)
187-
.collect::<Vec<_>>()
178+
instance.enumerate_adapters(backends)
188179
};
189180

190181
let (adapter, device, queue) = match config.wgpu_setup.clone() {
@@ -222,10 +213,7 @@ impl RenderState {
222213
.await?
223214
};
224215

225-
// On wasm, depending on feature flags, wgpu objects may or may not implement sync.
226-
// It doesn't make sense to switch to Rc for that special usecase, so simply disable the lint.
227-
#[allow(clippy::arc_with_non_send_sync)]
228-
(adapter, Arc::new(device), Arc::new(queue))
216+
(adapter, device, queue)
229217
}
230218
WgpuSetup::Existing(WgpuSetupExisting {
231219
instance: _,
@@ -258,7 +246,7 @@ impl RenderState {
258246
Ok(Self {
259247
adapter,
260248
#[cfg(not(target_arch = "wasm32"))]
261-
available_adapters: available_adapters.into(),
249+
available_adapters,
262250
device,
263251
queue,
264252
target_format,
@@ -268,7 +256,7 @@ impl RenderState {
268256
}
269257

270258
#[cfg(not(target_arch = "wasm32"))]
271-
fn describe_adapters(adapters: &[Arc<wgpu::Adapter>]) -> String {
259+
fn describe_adapters(adapters: &[wgpu::Adapter]) -> String {
272260
if adapters.is_empty() {
273261
"(none)".to_owned()
274262
} else if adapters.len() == 1 {

crates/egui-wgpu/src/setup.rs

+8-13
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl WgpuSetup {
4545
///
4646
/// Does *not* store the wgpu instance, so calling this repeatedly may
4747
/// create a new instance every time!
48-
pub async fn new_instance(&self) -> Arc<wgpu::Instance> {
48+
pub async fn new_instance(&self) -> wgpu::Instance {
4949
match self {
5050
Self::CreateNew(create_new) => {
5151
#[allow(unused_mut)]
@@ -65,12 +65,8 @@ impl WgpuSetup {
6565
}
6666

6767
log::debug!("Creating wgpu instance with backends {:?}", backends);
68-
69-
#[allow(clippy::arc_with_non_send_sync)]
70-
Arc::new(
71-
wgpu::util::new_instance_with_webgpu_detection(&create_new.instance_descriptor)
72-
.await,
73-
)
68+
wgpu::util::new_instance_with_webgpu_detection(&create_new.instance_descriptor)
69+
.await
7470
}
7571
Self::Existing(existing) => existing.instance.clone(),
7672
}
@@ -93,9 +89,8 @@ impl From<WgpuSetupExisting> for WgpuSetup {
9389
///
9490
/// This can be used for fully custom adapter selection.
9591
/// If available, `wgpu::Surface` is passed to allow checking for surface compatibility.
96-
// TODO(gfx-rs/wgpu#6665): Remove layer of `Arc` here.
9792
pub type NativeAdapterSelectorMethod = Arc<
98-
dyn Fn(&[Arc<wgpu::Adapter>], Option<&wgpu::Surface<'_>>) -> Result<Arc<wgpu::Adapter>, String>
93+
dyn Fn(&[wgpu::Adapter], Option<&wgpu::Surface<'_>>) -> Result<wgpu::Adapter, String>
9994
+ Send
10095
+ Sync,
10196
>;
@@ -215,8 +210,8 @@ impl Default for WgpuSetupCreateNew {
215210
/// Used for [`WgpuSetup::Existing`].
216211
#[derive(Clone)]
217212
pub struct WgpuSetupExisting {
218-
pub instance: Arc<wgpu::Instance>,
219-
pub adapter: Arc<wgpu::Adapter>,
220-
pub device: Arc<wgpu::Device>,
221-
pub queue: Arc<wgpu::Queue>,
213+
pub instance: wgpu::Instance,
214+
pub adapter: wgpu::Adapter,
215+
pub device: wgpu::Device,
216+
pub queue: wgpu::Queue,
222217
}

crates/egui-wgpu/src/winit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub struct Painter {
2727
depth_format: Option<wgpu::TextureFormat>,
2828
screen_capture_state: Option<CaptureState>,
2929

30-
instance: Arc<wgpu::Instance>,
30+
instance: wgpu::Instance,
3131
render_state: Option<RenderState>,
3232

3333
// Per viewport/window:

crates/egui_kittest/src/builder.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::app_kind::AppKind;
2-
use crate::wgpu::WgpuTestRenderer;
32
use crate::{Harness, LazyRenderer, TestRenderer};
43
use egui::{Pos2, Rect, Vec2};
54
use std::marker::PhantomData;
@@ -75,16 +74,16 @@ impl<State> HarnessBuilder<State> {
7574

7675
/// Enable wgpu rendering with a default setup suitable for testing.
7776
///
78-
/// This sets up a [`WgpuTestRenderer`] with the default setup.
77+
/// This sets up a [`crate::wgpu::WgpuTestRenderer`] with the default setup.
7978
#[cfg(feature = "wgpu")]
8079
pub fn wgpu(self) -> Self {
81-
self.renderer(WgpuTestRenderer::default())
80+
self.renderer(crate::wgpu::WgpuTestRenderer::default())
8281
}
8382

8483
/// Enable wgpu rendering with the given setup.
8584
#[cfg(feature = "wgpu")]
8685
pub fn wgpu_setup(self, setup: egui_wgpu::WgpuSetup) -> Self {
87-
self.renderer(WgpuTestRenderer::from_setup(setup))
86+
self.renderer(crate::wgpu::WgpuTestRenderer::from_setup(setup))
8887
}
8988

9089
/// Create a new Harness with the given app closure and a state.

crates/egui_kittest/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ impl<'a, State> Harness<'a, State> {
402402
///
403403
/// # Errors
404404
/// Returns an error if the rendering fails.
405+
#[cfg(any(feature = "wgpu", feature = "snapshot"))]
405406
pub fn render(&mut self) -> Result<image::RgbaImage, String> {
406407
self.renderer.render(&self.ctx, &self.output)
407408
}

crates/egui_kittest/src/renderer.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use egui::{Context, FullOutput, TexturesDelta};
2-
use image::RgbaImage;
1+
use egui::TexturesDelta;
32

43
pub trait TestRenderer {
54
/// We use this to pass the glow / wgpu render state to [`eframe::Frame`].
@@ -13,7 +12,12 @@ pub trait TestRenderer {
1312
///
1413
/// # Errors
1514
/// Returns an error if the rendering fails.
16-
fn render(&mut self, ctx: &Context, output: &FullOutput) -> Result<RgbaImage, String>;
15+
#[cfg(any(feature = "wgpu", feature = "snapshot"))]
16+
fn render(
17+
&mut self,
18+
ctx: &egui::Context,
19+
output: &egui::FullOutput,
20+
) -> Result<image::RgbaImage, String>;
1721
}
1822

1923
/// A lazy renderer that initializes the renderer on the first render call.
@@ -58,7 +62,12 @@ impl TestRenderer for LazyRenderer {
5862
}
5963
}
6064

61-
fn render(&mut self, ctx: &Context, output: &FullOutput) -> Result<RgbaImage, String> {
65+
#[cfg(any(feature = "wgpu", feature = "snapshot"))]
66+
fn render(
67+
&mut self,
68+
ctx: &egui::Context,
69+
output: &egui::FullOutput,
70+
) -> Result<image::RgbaImage, String> {
6271
match self {
6372
Self::Uninitialized {
6473
texture_ops,

0 commit comments

Comments
 (0)