Skip to content

Commit

Permalink
gpu: Save a smidge of Future size.
Browse files Browse the repository at this point in the history
By moving the creation of the future into a separate `let`, we avoid
keeping the `RequestAdapterOptions` or `DeviceDescriptor` alive across
the await, and in the wasm case, the `Instance` too.

This probably wasn’t even worth doing, but I like practicing keeping
futures small.
  • Loading branch information
kpreid committed Dec 2, 2024
1 parent 34f041b commit c62cfde
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 27 deletions.
15 changes: 7 additions & 8 deletions all-is-cubes-desktop/src/winit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,13 @@ pub async fn create_winit_wgpu_desktop_session(
let mut adapter: Option<wgpu::Adapter> =
wgpu::util::initialize_adapter_from_env(&instance, Some(&surface));
if adapter.is_none() {
adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::util::power_preference_from_env()
.unwrap_or(wgpu::PowerPreference::HighPerformance),
compatible_surface: Some(&surface),
force_fallback_adapter: false,
})
.await;
let request_adapter_future = instance.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::util::power_preference_from_env()
.unwrap_or(wgpu::PowerPreference::HighPerformance),
compatible_surface: Some(&surface),
force_fallback_adapter: false,
});
adapter = request_adapter_future.await;
}
let adapter = adapter
.ok_or_else(|| anyhow::format_err!("Could not request suitable graphics adapter"))?;
Expand Down
17 changes: 8 additions & 9 deletions all-is-cubes-gpu/src/in_wgpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,14 @@ impl<I: time::Instant> SurfaceRenderer<I> {
adapter: wgpu::Adapter,
executor: Arc<dyn Executor>,
) -> Result<Self, wgpu::RequestDeviceError> {
let (device, queue) = adapter
.request_device(
&EverythingRenderer::<I>::device_descriptor(
"SurfaceRenderer::device",
adapter.limits(),
),
None,
)
.await?;
let request_device_future = adapter.request_device(
&EverythingRenderer::<I>::device_descriptor(
"SurfaceRenderer::device",
adapter.limits(),
),
None,
);
let (device, queue) = request_device_future.await?;
#[cfg_attr(target_family = "wasm", expect(clippy::arc_with_non_send_sync))]
let device = Arc::new(device);

Expand Down
23 changes: 13 additions & 10 deletions all-is-cubes-wasm/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,19 +195,22 @@ async fn init_wgpu(
canvas: &web_sys::HtmlCanvasElement,
backends: wgpu::Backends,
) -> Result<(wgpu::Surface<'static>, wgpu::Adapter), Box<dyn core::error::Error>> {
let wgpu_instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
backends,
..Default::default()
});
let surface = wgpu_instance
.create_surface(wgpu::SurfaceTarget::Canvas(canvas.clone()))
.map_err(|e| format!("Requesting {backends:?} context failed: {e:?}"))?;
let adapter = wgpu_instance
.request_adapter(&wgpu::RequestAdapterOptions {
let (surface, request_adapter_future) = {
let wgpu_instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
backends,
..Default::default()
});
let surface = wgpu_instance
.create_surface(wgpu::SurfaceTarget::Canvas(canvas.clone()))
.map_err(|e| format!("Requesting {backends:?} context failed: {e:?}"))?;
let request_adapter_future = wgpu_instance.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::HighPerformance,
compatible_surface: Some(&surface),
force_fallback_adapter: false,
})
});
(surface, request_adapter_future)
};
let adapter = request_adapter_future
.await
.ok_or("Could not request suitable graphics adapter")?;
Ok((surface, adapter))
Expand Down

0 comments on commit c62cfde

Please sign in to comment.