Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to hook/wrap an EventLoop. #1858

Closed
wants to merge 2 commits into from

Conversation

lachlansneff
Copy link

@lachlansneff lachlansneff commented Feb 10, 2021

I've been thinking about how to get wgpu to integrate into the winit event loop (specifically to avoid needing users to continually call device.poll in order for gpu buffers to actually get mapped).

Anyhow, there were two ways of doing this:

  1. Add some special casing to winit for wgpu that will call device.poll once per frame.
  2. Add generalized hooking ability to winit.

The second option seemed preferable to me. Another issue is that, if control_flow is set to Wait, the event handler will only be called if the user interacts. That's not good enough in this case, so the hook wraps the user-specified event handler so it can detect if the handler set the control flow to Wait and change it to WaitUntil or similar instead.

The main things added are the Hook trait and EventLoop::set_hook.

EventLoop::set_hook actually consumes the EventLoop and returns a new type of EventLoop<T, H>, where H: Hook<T>.

@lachlansneff lachlansneff changed the title Add ability to hook/wrap an EventLoop. This is zero-cost–—if no hook is specified, the call is optimized out. Add ability to hook/wrap an EventLoop. Feb 10, 2021
@ArturKovacs
Copy link
Contributor

ArturKovacs commented Apr 12, 2021

I don't see why this would be necessary. I don't quite understand the situation maybe because I've never used wgpu but it seems to me that this sort of thing is not supposed to be managed by winit. I believe you could for example structure you code like this to achieve the same effect:

struct MyHook {
    // Whatever state you need to store for the hook
}
impl MyHook {
    fn got_event<T, F>(
        &mut self,
        event: Event<'_, T>,
        target: &EventLoopWindowTarget<T>,
        control_flow: &mut ControlFlow
        handler: F,
    ) {
        // do before handler stuff
        handler(event, target, control_flow);
        // do after handler stuff
    }
}
fn main() {
    let event_loop = EventLoop::new();
    let window = WindowBuilder::new()
        .with_title("A fantastic window!")
        .build(&event_loop)
        .unwrap();
    let mut hook = MyHook {};
    event_loop.run(move |event, target, control_flow| {
        hook.got_event(event, target, control_flow, |event, _ control_flow| {
            println!("in handler: {:?}", event);
            *control_flow = ControlFlow::Poll;
            match event {
                ...
            }
        });
    });
}

@maroider
Copy link
Member

specifically to avoid needing users to continually call device.poll in order for gpu buffers to actually get mapped

Is this still a problem, actually? I believe I've read that this limitation has been fixed or something like that?

@madsmtm
Copy link
Member

madsmtm commented Mar 14, 2024

We're working on alternative solutions to this in #3432 and #3474, this PR is now obsolete.

@madsmtm madsmtm closed this Mar 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants