Skip to content

Commit

Permalink
Add Device::from_preferred_adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
hannobraun committed Sep 29, 2023
1 parent 6edf455 commit 8e19006
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
22 changes: 22 additions & 0 deletions crates/fj-viewer/src/graphics/device.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
use tracing::debug;

#[derive(Debug)]
pub struct Device {
pub device: wgpu::Device,
pub queue: wgpu::Queue,
}

impl Device {
pub async fn from_preferred_adapter(
instance: &wgpu::Instance,
surface: &wgpu::Surface,
) -> Result<(Self, wgpu::Adapter, wgpu::Features), DeviceError> {
let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::None,
force_fallback_adapter: false,
compatible_surface: Some(surface),
})
.await
.ok_or(DeviceError::RequestAdapter)?;

debug!("Using adapter: {:?}", adapter.get_info());

let (device, features) = Device::new(&adapter).await?;

Ok((device, adapter, features))
}

pub async fn new(
adapter: &wgpu::Adapter,
) -> Result<(Self, wgpu::Features), DeviceError> {
Expand Down
14 changes: 2 additions & 12 deletions crates/fj-viewer/src/graphics/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,8 @@ impl Renderer {
debug!("Available adapter: {:?}", adapter.get_info());
}

let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::None,
force_fallback_adapter: false,
compatible_surface: Some(&surface),
})
.await
.ok_or(RendererInitError::Device(DeviceError::RequestAdapter))?;

debug!("Using adapter: {:?}", adapter.get_info());

let (device, features) = Device::new(&adapter).await?;
let (device, adapter, features) =
Device::from_preferred_adapter(&instance, &surface).await?;

let color_format = 'color_format: {
let capabilities = surface.get_capabilities(&adapter);
Expand Down

0 comments on commit 8e19006

Please sign in to comment.