You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Using Window functions across DLL boundaries
#4337
Hello everyone! I am writing an engine with support for hot reloading using DLLs, but I am having problems with calling Window functions over the DLL boundary.
The main structure of my project is.
The core engine is a crate which creates the window and runs the event loop
The user application creates an app that implements a Callback trait which then is injected into the core engine.
/// User callbakspubtraitCallbacks{/// Use a custom `ContextBuilder`fninit_ctx() -> ContextBuilder{ContextBuilder::new()}/// Called after context initilization and before game/update loopfnnew(_ctx:&mutContext,cache:&mutAssetCache) -> Self;/// Called once per frame after update////// Return value determines wether to exit game or not////// Must submit at least one render pass, panics otherwisefnrender(&mutself,_ctx:&mutContext,_cache:&mutAssetCache,_screen_view:&wgpu::TextureView,) -> bool{false}/// Called after window resizefnresize(&mutself,_ctx:&mutContext,_cache:&mutAssetCache,_new_size: winit::dpi::PhysicalSize<u32>,){}fnwindow_event(&mutself,_ctx:&mutContext,_event:&winit::event::WindowEvent){}}pubasyncfnrun<C:Callbacks>(){C::init_ctx().init_logging();let event_loop = winit::event_loop::EventLoop::with_user_event().build().expect("could not create event loop");letmut app = App::Uninitialized::<C>{proxy:Some(event_loop.create_proxy()),builder:C::init_ctx(),};
event_loop
.run_app(&mut app).expect("could not run event loop");}#[cfg(not(target_arch = "wasm32"))]pubfnrun_sync<C:Callbacks>(){
pollster::block_on(run::<C>())}/// general engine state////// can be both initialized and uninitialized#[allow(clippy::large_enum_variant)]enumApp<C:Callbacks>{Uninitialized{proxy:Option<winit::event_loop::EventLoopProxy<Context>>,builder:ContextBuilder,},Initialized{ctx:Context,cache:AssetCache,#[cfg(not(feature = "hot_reload"))]callbacks:C,#[cfg(feature = "hot_reload")]callbacks:DllCallbacks<C>,},}impl<C:Callbacks> winit::application::ApplicationHandler<Context>forApp<C>{fnuser_event(&mutself,_event_loop:&winit::event_loop::ActiveEventLoop,mutctx:Context){// TODO: init here?letmut cache = AssetCache::new();#[cfg(not(feature = "hot_reload"))]let callbacks = C::new(&mut ctx,&mut cache);#[cfg(feature = "hot_reload")]let callbacks = DllCallbacks::new(&mut ctx,&mut cache);*self = App::Initialized{
callbacks,
ctx,
cache,};}fnresumed(&mutself,event_loop:&winit::event_loop::ActiveEventLoop){// Ignore if already initializedletApp::Uninitialized{ proxy, builder } = selfelse{return;};if proxy.is_none(){return;}// initialize contextasyncfninit(window: winit::window::Window,builder:ContextBuilder,proxy: winit::event_loop::EventLoopProxy<Context>,){let input = input::InputContext::new();let time = time::TimeContext::default();let filesystem = filesystem::FileSystemContext::new();let audio = audio::AudioContext::new();let render = render::RenderContext::new(window,&builder).await;let random = random::RandomContext::new();let ctx = Context{
input,
time,
filesystem,
audio,
render,
random,#[cfg(feature = "hot_reload")]hot_reload: hot_reload::HotReloadContext::new(),};let sucess = proxy.send_event(ctx).is_ok();assert!(sucess,"could not send context event");}let proxy = proxy.take().unwrap();letmut builder = builder.clone();#[cfg(target_arch = "wasm32")]{use web_sys::wasm_bindgen::JsCast;use winit::platform::web::WindowAttributesExtWebSys;let win = web_sys::window().unwrap();let document = win.document().unwrap();let canvas = document.get_element_by_id("gbase").unwrap();// let html_canvas_element = canvas.unchecked_into();let canvas = document
.get_element_by_id("gbase").expect("could not find canvas").dyn_into::<web_sys::HtmlCanvasElement>().expect("element was not a canvas");let(width, height) = (canvas.width(), canvas.height());
builder.window_attributes = builder
.window_attributes.with_canvas(Some(canvas)).with_inner_size(winit::dpi::LogicalSize::new(width, height));}let window = event_loop
.create_window(builder.window_attributes.clone()).expect("could not create window");#[cfg(target_arch = "wasm32")]
wasm_bindgen_futures::spawn_local(init(window, builder, proxy));#[cfg(not(target_arch = "wasm32"))]
pollster::block_on(init(window, builder, proxy));}fnabout_to_wait(&mutself,_event_loop:&winit::event_loop::ActiveEventLoop){letApp::Initialized{refmut ctx, .. } = selfelse{
tracing::warn!("app not initialized while receiving about to wait event -> skipping");return;};
ctx.render.window().request_redraw();}fndevice_event(&mutself,_event_loop:&winit::event_loop::ActiveEventLoop,_device_id: event::DeviceId,event: event::DeviceEvent,){letApp::Initialized{refmut ctx, .. } = selfelse{
tracing::warn!("app not initialized while receiving device event -> skipping");return;};#[allow(clippy::single_match)]match event {DeviceEvent::MouseMotion{ delta } => {if ctx.input.mouse.mouse_on_screen(){
ctx.input.mouse.set_mouse_delta(delta);}}
_ => {}}}fnwindow_event(&mutself,event_loop:&winit::event_loop::ActiveEventLoop,_window_id: winit::window::WindowId,event: event::WindowEvent,){letApp::Initialized{refmut ctx,
callbacks,
cache,} = selfelse{
tracing::warn!("app not initialized while receiving window event -> skipping");return;};// TODO: temp for eguiprintln!("WINDOW EVENT");
callbacks.window_event(ctx,&event);match event {WindowEvent::RedrawRequested => {// hot reload#[cfg(feature = "hot_reload")]{if ctx.hot_reload.should_reload(){
tracing::info!("Hot reload");
callbacks.hot_reload(ctx, cache);}if ctx.hot_reload.should_restart(){
tracing::info!("Hot restart");
callbacks.hot_restart(ctx, cache);}}// updateifupdate_and_render(ctx, cache, callbacks){
event_loop.exit();}}WindowEvent::CloseRequested => event_loop.exit(),WindowEvent::Resized(new_size) => {
ctx.render.resize_window(new_size);
callbacks.resize(ctx, cache, new_size);}// KeyboardWindowEvent::KeyboardInput{ event, .. } => {let(key, pressed) = (event.physical_key, event.state.is_pressed());match(key, pressed){(PhysicalKey::Code(code),true) => ctx.input.keyboard.set_key(code),(PhysicalKey::Code(code),false) => ctx.input.keyboard.release_key(code),(PhysicalKey::Unidentified(code), _) => {
tracing::error!("pressed/released unidentified key {:?}", code)}};}WindowEvent::ModifiersChanged(modifiers) => {
ctx.input.keyboard.modifiers_changed(modifiers)}// MouseWindowEvent::MouseInput{ state, button, .. } => {match state {
winit::event::ElementState::Pressed => ctx.input.mouse.press_button(button),
winit::event::ElementState::Released => ctx.input.mouse.release_button(button),};}WindowEvent::MouseWheel{ delta, .. } => match delta {
winit::event::MouseScrollDelta::LineDelta(x, y) => {
ctx.input.mouse.set_scroll_delta((x asf64, y asf64));}
winit::event::MouseScrollDelta::PixelDelta(pos) => {
ctx.input.mouse.set_scroll_delta((pos).into());}},WindowEvent::CursorMoved{ position, .. } => {
ctx.input.mouse.set_pos(position.into());}WindowEvent::CursorEntered{ .. } => {
ctx.input.mouse.set_on_screen(true);}WindowEvent::CursorLeft{ .. } => {
ctx.input.mouse.set_on_screen(false);}
_ => {}}}}
typeNewFunc<T> = fn(ctx:&mutcrate::Context,cache:&mutcrate::asset::AssetCache) -> T;typeRenderFunc<T> = fn(callbacks:&mutT,ctx:&mutcrate::Context,cache:&mutcrate::asset::AssetCache,screen_view:&wgpu::TextureView,) -> bool;typeResizeFunc<T> = fn(callbacks:&mutT,ctx:&mutcrate::Context,cache:&mutcrate::asset::AssetCache,new_size: winit::dpi::PhysicalSize<u32>,);typeReloadFunc<T> =
fn(callbacks:&mutT,ctx:&mutcrate::Context,cache:&mutcrate::asset::AssetCache);typeWindowEventFunc<T> =
fn(callbacks:&mutT,ctx:&mutcrate::Context,event:&winit::event::WindowEvent);pubstructDllApi<T>{new_callback:NewFunc<T>,render_callback:Option<RenderFunc<T>>,resize_callback:Option<ResizeFunc<T>>,reload_callback:Option<ReloadFunc<T>>,window_event_callback:Option<WindowEventFunc<T>>,}/// Wrapper for callbacks + dllpubstructDllCallbacks<T>{pubcallbacks:T,pubdll:DllApi<T>,}impl<T>crate::CallbacksforDllCallbacks<T>{// NOTE:// init_ctx should only be called at program startup and never through dll// so we never store it here and we panic if called on DllContextfninit_ctx() -> crate::ContextBuilder{panic!("init_ctx on DllCallbacks should never be called");}fnnew(ctx:&mutcrate::Context,cache:&mutcrate::asset::AssetCache) -> Self{let dll = load_dll();letmut callbacks = (dll.new_callback)(ctx, cache);ifletSome(hot_reload) = dll.reload_callback{hot_reload(&mut callbacks, ctx, cache);}Self{ callbacks, dll }}fnrender(&mutself,ctx:&mutcrate::Context,cache:&mutcrate::asset::AssetCache,screen_view:&wgpu::TextureView,) -> bool{matchself.dll.render_callback{Some(render) => render(&mutself.callbacks, ctx, cache, screen_view),None => false,}}fnresize(&mutself,ctx:&mutcrate::Context,cache:&mutcrate::asset::AssetCache,new_size: winit::dpi::PhysicalSize<u32>,){#[allow(clippy::single_match)]matchself.dll.resize_callback{Some(resize) => resize(&mutself.callbacks, ctx, cache, new_size),None => {}}}fnwindow_event(&mutself,ctx:&mutcrate::Context,event:&winit::event::WindowEvent){#[allow(clippy::single_match)]matchself.dll.window_event_callback{Some(window_event) => window_event(&mutself.callbacks, ctx, event),None => {}}}}impl<T>DllCallbacks<T>{/// reload dll file////// keep game statepubfnhot_reload(&mutself,ctx:&mutcrate::Context,cache:&mutcrate::asset::AssetCache){self.dll = load_dll();ifletSome(hot_reload) = self.dll.reload_callback{hot_reload(&mutself.callbacks, ctx, cache);}}/// reload dll file////// reset game statepubfnhot_restart(&mutself,ctx:&mutcrate::Context,cache:&mutcrate::asset::AssetCache){self.hot_reload(ctx, cache);self.callbacks = (self.dll.new_callback)(ctx, cache);}}fnload_dll<T>() -> DllApi<T>{let lib = dlopen::symbor::Library::open(super::dllname()).unwrap();let new_callback = matchunsafe{ lib.symbol::<NewFunc<T>>("new")}{Ok(f) => *f,Err(err) => {
tracing::error!("could not find function new");
tracing::error!("TIP: make sure callbacks are defined in library and not main.rs");
tracing::error!("TIP: make sure functions are marked with #[no_mangle]");panic!("{}", err);}};let render_callback = matchunsafe{ lib.symbol::<RenderFunc<T>>("render")}{Ok(f) => Some(*f),Err(err) => {
tracing::warn!("could not find function render: {}", err);None}};let resize_callback = matchunsafe{ lib.symbol::<ResizeFunc<T>>("resize")}{Ok(f) => Some(*f),Err(err) => {
tracing::warn!("could not find function resize: {}", err);None}};let reload_callback = matchunsafe{ lib.symbol::<ReloadFunc<T>>("hot_reload")}{Ok(f) => Some(*f),Err(err) => {
tracing::warn!("could not find function hot_reload: {}", err);None}};let window_event_callback = matchunsafe{ lib.symbol::<WindowEventFunc<T>>("window_event")}{Ok(f) => Some(*f),Err(err) => {
tracing::warn!("could not find function window_event: {}", err);None}};DllApi{
new_callback,
render_callback,
resize_callback,
reload_callback,
window_event_callback,}}
When running in release mode, the callback trait is created on the main thread. When running in hot_reload mode a wrapper is used that loads the callback code from a DLL. In release mode everything works as expected. However, in the hot_reload mode the Window functions are not found (on MACOS).
invalid message send to -[WinitApplicationDelegate frame]: method not found
I know window functions must be called from the main thread in MACOS. But how does it work across DLL boundaries, isnt it still being called from the main thread? Any help would be very appreciated.
One solution would be to cache the relevant data in the core engine. However, I want to use crates such as egui which need to call these window methods directly.
I have had problems with logging since its a static variable that gets different values for different compilation units, could this be similar to that problem?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Hello everyone! I am writing an engine with support for hot reloading using DLLs, but I am having problems with calling Window functions over the DLL boundary.
The main structure of my project is.
When running in release mode, the callback trait is created on the main thread. When running in hot_reload mode a wrapper is used that loads the callback code from a DLL. In release mode everything works as expected. However, in the hot_reload mode the Window functions are not found (on MACOS).
I know window functions must be called from the main thread in MACOS. But how does it work across DLL boundaries, isnt it still being called from the main thread? Any help would be very appreciated.
One solution would be to cache the relevant data in the core engine. However, I want to use crates such as egui which need to call these window methods directly.
I have had problems with logging since its a static variable that gets different values for different compilation units, could this be similar to that problem?
All reactions