Skip to content

Commit

Permalink
List selected and available adapters in the egui_demo_app backend panel
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Jan 18, 2024
1 parent 6b0c20b commit 90d7524
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 18 deletions.
31 changes: 13 additions & 18 deletions crates/egui-wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ pub struct RenderState {
/// Wgpu adapter used for rendering.
pub adapter: Arc<wgpu::Adapter>,

/// All the avilable adapters.

Check warning on line 59 in crates/egui-wgpu/src/lib.rs

View workflow job for this annotation

GitHub Actions / typos

"avilable" should be "available".
pub available_adapters: Arc<[wgpu::Adapter]>,

/// Wgpu device used for rendering, created from the adapter.
pub device: Arc<wgpu::Device>,

Expand Down Expand Up @@ -83,8 +86,7 @@ impl RenderState {
) -> Result<Self, WgpuError> {
crate::profile_scope!("RenderState::create"); // async yield give bad names using `profile_function`

#[cfg(not(target_arch = "wasm32"))]
let adapters: Vec<_> = instance.enumerate_adapters(wgpu::Backends::all());
let available_adapters: Vec<_> = instance.enumerate_adapters(wgpu::Backends::all());

let adapter = {
crate::profile_scope!("request_adapter");
Expand All @@ -97,42 +99,35 @@ impl RenderState {
.await
.ok_or_else(|| {
#[cfg(not(target_arch = "wasm32"))]
if adapters.is_empty() {
if available_adapters.is_empty() {
log::info!("No wgpu adapters found");
} else if adapters.len() == 1 {
} else if available_adapters.len() == 1 {
log::info!(
"The only available wgpu adapter was not suitable: {}",
adapter_info_summary(&adapters[0].get_info())
adapter_info_summary(&available_adapters[0].get_info())
);
} else {
log::info!(
"No suitable wgpu adapter found out of the {} available ones: {}",
adapters.len(),
describe_adapters(&adapters)
available_adapters.len(),
describe_adapters(&available_adapters)
);
}

WgpuError::NoSuitableAdapterFound
})?
};

#[cfg(target_arch = "wasm32")]
log::debug!(
"Picked wgpu adapter: {}",
adapter_info_summary(&adapter.get_info())
);

#[cfg(not(target_arch = "wasm32"))]
if adapters.len() == 1 {
if available_adapters.len() == 1 {
log::debug!(
"Picked the only available wgpu adapter: {}",
adapter_info_summary(&adapter.get_info())
);
} else {
log::info!(
"There were {} available wgpu adapters: {}",
adapters.len(),
describe_adapters(&adapters)
available_adapters.len(),
describe_adapters(&available_adapters)
);
log::debug!(
"Picked wgpu adapter: {}",
Expand All @@ -157,6 +152,7 @@ impl RenderState {

Ok(Self {
adapter: Arc::new(adapter),
available_adapters: available_adapters.into(),
device: Arc::new(device),
queue: Arc::new(queue),
target_format,
Expand All @@ -165,7 +161,6 @@ impl RenderState {
}
}

#[cfg(not(target_arch = "wasm32"))]
fn describe_adapters(adapters: &[wgpu::Adapter]) -> String {
if adapters.is_empty() {
"(none)".to_owned()
Expand Down
96 changes: 96 additions & 0 deletions crates/egui_demo_app/src/backend_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,102 @@ fn integration_ui(ui: &mut egui::Ui, _frame: &mut eframe::Frame) {
ui.monospace(format!("{:#?}", _frame.info().web_info.location));
});

#[cfg(feature = "glow")]
if _frame.gl().is_some() {
ui.horizontal(|ui| {
ui.label("Renderer:");
ui.hyperlink_to("glow", "https://github.com/grovesNL/glow");
});
}

#[cfg(feature = "wgpu")]
if let Some(render_state) = _frame.wgpu_render_state() {
let wgpu_adapter_details_ui = |ui: &mut egui::Ui, adapter: &eframe::wgpu::Adapter| {
let info = &adapter.get_info();

let wgpu::AdapterInfo {
name,
vendor,
device,
device_type,
driver,
driver_info,
backend,
} = &info;

// Example values:
// > name: "llvmpipe (LLVM 16.0.6, 256 bits)", device_type: Cpu, backend: Vulkan, driver: "llvmpipe", driver_info: "Mesa 23.1.6-arch1.4 (LLVM 16.0.6)"
// > name: "Apple M1 Pro", device_type: IntegratedGpu, backend: Metal, driver: "", driver_info: ""
// > name: "ANGLE (Apple, Apple M1 Pro, OpenGL 4.1)", device_type: IntegratedGpu, backend: Gl, driver: "", driver_info: ""

egui::Grid::new("adapter_info").show(ui, |ui| {
ui.label("Backend:");
ui.label(format!("{backend:?}"));
ui.end_row();

ui.label("Device Type:");
ui.label(format!("{device_type:?}"));
ui.end_row();

if !name.is_empty() {
ui.label("Name:");
ui.label(format!("{name:?}"));
ui.end_row();
}
if !driver.is_empty() {
ui.label("Driver:");
ui.label(format!("{driver:?}"));
ui.end_row();
}
if !driver_info.is_empty() {
ui.label("Driver info:");
ui.label(format!("{driver_info:?}"));
ui.end_row();
}
if *vendor != 0 {
// TODO(emilk): decode using https://github.com/gfx-rs/wgpu/blob/767ac03245ee937d3dc552edc13fe7ab0a860eec/wgpu-hal/src/auxil/mod.rs#L7
ui.label("Vendor:");
ui.label(format!("0x{vendor:04X}"));
ui.end_row();
}
if *device != 0 {
ui.label("Device:");
ui.label(format!("0x{device:02X}"));
ui.end_row();
}
});
};

let wgpu_adapter_ui = |ui: &mut egui::Ui, adapter: &eframe::wgpu::Adapter| {
let info = &adapter.get_info();
ui.label(format!("{:?}", info.backend)).on_hover_ui(|ui| {
wgpu_adapter_details_ui(ui, adapter);
});
};

egui::Grid::new("wgpu_info").num_columns(2).show(ui, |ui| {
ui.label("Renderer:");
ui.hyperlink_to("wgpu", "https://wgpu.rs/");
ui.end_row();

ui.label("Backend:");
wgpu_adapter_ui(ui, &render_state.adapter);
ui.end_row();

if 1 < render_state.available_adapters.len() {
ui.label("Others:");
ui.vertical(|ui| {
for adapter in &*render_state.available_adapters {
if adapter.get_info() != render_state.adapter.get_info() {
wgpu_adapter_ui(ui, adapter);
}
}
});
ui.end_row();
}
});
}

#[cfg(not(target_arch = "wasm32"))]
{
ui.horizontal(|ui| {
Expand Down

0 comments on commit 90d7524

Please sign in to comment.