Share one wgpu instance across all windows
This commit is contained in:
parent
0086b9c9ce
commit
8df05d923d
76
src/app.rs
76
src/app.rs
|
@ -35,6 +35,7 @@ fn load_icon() -> anyhow::Result<IconData> {
|
||||||
|
|
||||||
pub struct Application {
|
pub struct Application {
|
||||||
icon: Option<Arc<IconData>>,
|
icon: Option<Arc<IconData>>,
|
||||||
|
wgpu: WgpuState,
|
||||||
client: EmulatorClient,
|
client: EmulatorClient,
|
||||||
proxy: EventLoopProxy<UserEvent>,
|
proxy: EventLoopProxy<UserEvent>,
|
||||||
mappings: MappingProvider,
|
mappings: MappingProvider,
|
||||||
|
@ -51,6 +52,7 @@ impl Application {
|
||||||
proxy: EventLoopProxy<UserEvent>,
|
proxy: EventLoopProxy<UserEvent>,
|
||||||
debug_port: Option<u16>,
|
debug_port: Option<u16>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
|
let wgpu = WgpuState::new();
|
||||||
let icon = load_icon().ok().map(Arc::new);
|
let icon = load_icon().ok().map(Arc::new);
|
||||||
let persistence = Persistence::new();
|
let persistence = Persistence::new();
|
||||||
let mappings = MappingProvider::new(persistence.clone());
|
let mappings = MappingProvider::new(persistence.clone());
|
||||||
|
@ -62,6 +64,7 @@ impl Application {
|
||||||
}
|
}
|
||||||
Self {
|
Self {
|
||||||
icon,
|
icon,
|
||||||
|
wgpu,
|
||||||
client,
|
client,
|
||||||
proxy,
|
proxy,
|
||||||
mappings,
|
mappings,
|
||||||
|
@ -80,7 +83,7 @@ impl Application {
|
||||||
}
|
}
|
||||||
self.viewports.insert(
|
self.viewports.insert(
|
||||||
viewport_id,
|
viewport_id,
|
||||||
Viewport::new(event_loop, self.icon.clone(), window),
|
Viewport::new(event_loop, &self.wgpu, self.icon.clone(), window),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -238,6 +241,58 @@ impl ApplicationHandler<UserEvent> for Application {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct WgpuState {
|
||||||
|
instance: Arc<wgpu::Instance>,
|
||||||
|
adapter: Arc<wgpu::Adapter>,
|
||||||
|
device: Arc<wgpu::Device>,
|
||||||
|
queue: Arc<wgpu::Queue>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl WgpuState {
|
||||||
|
fn new() -> Self {
|
||||||
|
let egui_wgpu::WgpuConfiguration {
|
||||||
|
wgpu_setup:
|
||||||
|
egui_wgpu::WgpuSetup::CreateNew {
|
||||||
|
#[allow(unused)]
|
||||||
|
supported_backends,
|
||||||
|
device_descriptor,
|
||||||
|
..
|
||||||
|
},
|
||||||
|
..
|
||||||
|
} = egui_wgpu::WgpuConfiguration::default()
|
||||||
|
else {
|
||||||
|
panic!("required fields not found")
|
||||||
|
};
|
||||||
|
#[cfg(windows)]
|
||||||
|
let supported_backends = wgpu::util::backend_bits_from_env()
|
||||||
|
.unwrap_or((wgpu::Backends::PRIMARY | wgpu::Backends::GL) - wgpu::Backends::VULKAN);
|
||||||
|
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
|
||||||
|
backends: supported_backends,
|
||||||
|
..wgpu::InstanceDescriptor::default()
|
||||||
|
});
|
||||||
|
|
||||||
|
let adapter = pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions {
|
||||||
|
power_preference: wgpu::PowerPreference::HighPerformance,
|
||||||
|
compatible_surface: None,
|
||||||
|
force_fallback_adapter: false,
|
||||||
|
}))
|
||||||
|
.expect("could not create adapter");
|
||||||
|
|
||||||
|
let trace_path = std::env::var("WGPU_TRACE");
|
||||||
|
let (device, queue) = pollster::block_on(adapter.request_device(
|
||||||
|
&(*device_descriptor)(&adapter),
|
||||||
|
trace_path.ok().as_ref().map(std::path::Path::new),
|
||||||
|
))
|
||||||
|
.expect("could not request device");
|
||||||
|
Self {
|
||||||
|
instance: Arc::new(instance),
|
||||||
|
adapter: Arc::new(adapter),
|
||||||
|
device: Arc::new(device),
|
||||||
|
queue: Arc::new(queue),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
struct Viewport {
|
struct Viewport {
|
||||||
painter: egui_wgpu::winit::Painter,
|
painter: egui_wgpu::winit::Painter,
|
||||||
ctx: Context,
|
ctx: Context,
|
||||||
|
@ -251,6 +306,7 @@ struct Viewport {
|
||||||
impl Viewport {
|
impl Viewport {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
event_loop: &ActiveEventLoop,
|
event_loop: &ActiveEventLoop,
|
||||||
|
wgpu: &WgpuState,
|
||||||
icon: Option<Arc<IconData>>,
|
icon: Option<Arc<IconData>>,
|
||||||
mut app: Box<dyn AppWindow>,
|
mut app: Box<dyn AppWindow>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
|
@ -274,20 +330,16 @@ impl Viewport {
|
||||||
});
|
});
|
||||||
egui_extras::install_image_loaders(&ctx);
|
egui_extras::install_image_loaders(&ctx);
|
||||||
|
|
||||||
#[allow(unused_mut)]
|
let wgpu_config = egui_wgpu::WgpuConfiguration {
|
||||||
let mut wgpu_config = egui_wgpu::WgpuConfiguration {
|
|
||||||
present_mode: wgpu::PresentMode::AutoNoVsync,
|
present_mode: wgpu::PresentMode::AutoNoVsync,
|
||||||
|
wgpu_setup: egui_wgpu::WgpuSetup::Existing {
|
||||||
|
instance: wgpu.instance.clone(),
|
||||||
|
adapter: wgpu.adapter.clone(),
|
||||||
|
device: wgpu.device.clone(),
|
||||||
|
queue: wgpu.queue.clone(),
|
||||||
|
},
|
||||||
..egui_wgpu::WgpuConfiguration::default()
|
..egui_wgpu::WgpuConfiguration::default()
|
||||||
};
|
};
|
||||||
#[cfg(windows)]
|
|
||||||
{
|
|
||||||
if let egui_wgpu::WgpuSetup::CreateNew {
|
|
||||||
supported_backends, ..
|
|
||||||
} = &mut wgpu_config.wgpu_setup
|
|
||||||
{
|
|
||||||
*supported_backends -= wgpu::Backends::VULKAN;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut painter =
|
let mut painter =
|
||||||
egui_wgpu::winit::Painter::new(ctx.clone(), wgpu_config, 1, None, false, true);
|
egui_wgpu::winit::Painter::new(ctx.clone(), wgpu_config, 1, None, false, true);
|
||||||
|
|
Loading…
Reference in New Issue