Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions neomacs-display-runtime/src/render_thread/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::{
use crate::thread_comm::{InputEvent, RenderComms};
use neomacs_renderer_wgpu::{WgpuGlyphAtlas, WgpuRenderer};
use std::sync::Arc;
use winit::event_loop::{ControlFlow, EventLoop};
use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop};
#[cfg(target_os = "linux")]
use winit::platform::wayland::EventLoopBuilderExtWayland;
#[cfg(target_os = "linux")]
Expand All @@ -18,13 +18,10 @@ use crate::backend::wpe::sys::platform as plat;

impl RenderApp {
/// Initialize wgpu with the window
pub(super) fn init_wgpu(&mut self, window: Arc<Window>) {
pub(super) fn init_wgpu(&mut self, event_loop: &ActiveEventLoop, window: Arc<Window>) {
tracing::info!("Initializing wgpu for render thread");

// Create wgpu instance
let mut instance_descriptor = wgpu::InstanceDescriptor::new_without_display_handle();
instance_descriptor.backends = wgpu::Backends::all();
let instance = wgpu::Instance::new(instance_descriptor);
let instance = create_wgpu_instance(event_loop);

// Create surface from window
let surface = match instance.create_surface(window.clone()) {
Expand Down Expand Up @@ -217,6 +214,14 @@ impl RenderApp {
}
}

pub(super) fn create_wgpu_instance(event_loop: &ActiveEventLoop) -> wgpu::Instance {
let display_handle = event_loop.owned_display_handle();
let mut instance_descriptor =
wgpu::InstanceDescriptor::new_with_display_handle_from_env(Box::new(display_handle));
instance_descriptor.backends = wgpu::Backends::all().with_env();
wgpu::Instance::new(instance_descriptor)
}

fn build_render_event_loop_impl(
allow_any_thread: bool,
) -> Result<EventLoop<RenderUserEvent>, String> {
Expand Down
2 changes: 1 addition & 1 deletion neomacs-display-runtime/src/render_thread/lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl RenderApp {
);

// Initialize wgpu with the window
self.init_wgpu(window.clone());
self.init_wgpu(event_loop, window.clone());

// Enable IME input for CJK and compose support
window.set_ime_allowed(true);
Expand Down
5 changes: 1 addition & 4 deletions neomacs-display-runtime/src/render_thread/multi_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,7 @@ impl MultiWindowManager {
let phys = window.inner_size();

// Create surface for this window
let mut instance_descriptor =
wgpu::InstanceDescriptor::new_without_display_handle();
instance_descriptor.backends = wgpu::Backends::all();
let instance = wgpu::Instance::new(instance_descriptor);
let instance = super::bootstrap::create_wgpu_instance(event_loop);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Creating a new wgpu::Instance inside the loop for every pending window creation is inefficient. wgpu::Instance::new is a relatively expensive operation as it enumerates system graphics backends. Since all windows in this event loop share the same display handle, it is better to create the instance once outside the loop and reuse it for all surface creations in the current batch.

let surface = match instance.create_surface(window.clone()) {
Ok(s) => s,
Err(e) => {
Expand Down
Loading