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

Update winit to 0.30 and refactor examples #5709

Draft
wants to merge 2 commits into
base: trunk
Choose a base branch
from

Conversation

anima-libera
Copy link

Description
Attempted to update winit to 0.30 and to refactor the examples accordingly. It does not work for the wasm32 target (sorry, I had no idea what I was doing, trying to block in web browsers, not used to that); but for non-wasm32 targets it seems to work (to the extent that I could test) so it may be useful in the end.

About the player crate, it is left untouched (so does not compile), but it can be ignored by changing this line into

winit = { version = "0.29", features = ["android-native-activity"], optional = true }

(which may be a bad idea, since all the other dependencies seem in sync in the workspace) in order to focus on the examples or run xtask now.

There are 5 modules to partially refactor:

  • the example framework
  • the hello_triangle, hello_windows and uniform_values examples (they do not use the framework to handle the creation of their window(s) and event loop)
  • the player crate

Note: I made the hello_triangle and hello_windows examples let the WGPU_ADAPTER_NAME env variable have a say in their choice of adapter (as the example framework does for all the examples that use it). (I did that to be able to run the examples that would no work with the default adapter on my machine, the issue there is most probably on my end and most probably not a concern.)

Note: There were mysterious numbers in the uniform_values example that could be labels referenced from some documentation that I could not find, they are mostly still above what they were labeling but since the code have moved around it may require whatever is referencing these labels to be partially rewritten to not rot.

Testing
This was tested on one machine (X11, Nvidia GPU) and works for the native target (some examples that use the framework were run, the three other examples were also run and they all seemed to work fine). Does not compile for wasm32. Even with player's winit version set back down to 0.29 (to run xtask), xtask's output seems negative (and some part of that negativity is probably about wasm32 target not even compiling in the example crate, hard to tell).

Checklist

  • Run cargo fmt.
  • Run cargo clippy. If applicable, add:
    • --target wasm32-unknown-unknown
    • --target wasm32-unknown-emscripten
  • Run cargo xtask test to run tests.
  • Add change to CHANGELOG.md. See simple instructions inside file.

@anima-libera
Copy link
Author

If it worked it could fix #5214

@anima-libera
Copy link
Author

The problem with wasm32 is that:

  • blocking on async is not allowed, so no trivial calling of async functions from sync functions
  • ApplicationHandler has no async methods
  • initializing the wgpu stuff requires the window, which can only be created in running event_loop, and we can't trivially do that in a method of ApplicationHandler since some of the wgpu stuff initialization is async

cwfitzgerald suggested that

[we] either need a custom event, or in redraw_requested, check to see if the spawn_local is finished, and if it is, take the state

@AustinMReppert
Copy link

Perhaps something like this for wasm

use std::{borrow::Cow, sync::Arc};
use winit::{
    application::ApplicationHandler,
    event::WindowEvent,
    event_loop::{ActiveEventLoop, EventLoop},
    window::{Window, WindowId},
};
use crate::framework;

#[cfg(target_arch = "wasm32")]
use web_time as time;

#[cfg(target_arch = "wasm32")]
use std::cell::RefCell;

#[cfg(target_arch = "wasm32")]
use winit::event_loop::ControlFlow;

#[cfg(target_arch = "wasm32")]
const WAIT_TIME: time::Duration = time::Duration::from_millis(100);

#[cfg(target_arch = "wasm32")]
enum Message {
    Initialized(InitializedLoopState),
}

#[cfg(target_arch = "wasm32")]
thread_local! {
    pub static MESSAGE_QUEUE: RefCell<Vec<Message>> = RefCell::new(Vec::new());
}

struct LoopState {
    // See https://docs.rs/winit/latest/winit/changelog/v0_30/index.html#removed
    // for the recommended pratcice regarding Window creation (from which everything depends)
    // in winit >= 0.30.0.
    // The actual state is in an Option because its initialization is now delayed to after
    // the even loop starts running.
    state: Option<InitializedLoopState>
}

impl LoopState {
    fn new() -> LoopState {
        LoopState { state: None }
    }
}

struct InitializedLoopState {
    window: Arc<Window>,
    _instance: wgpu::Instance,
    surface: wgpu::Surface<'static>,
    _adapter: wgpu::Adapter,
    device: wgpu::Device,
    queue: wgpu::Queue,
    _shader: wgpu::ShaderModule,
    _pipeline_layout: wgpu::PipelineLayout,
    render_pipeline: wgpu::RenderPipeline,
    config: wgpu::SurfaceConfiguration,
}

impl InitializedLoopState {
    async fn new(window: Arc<Window>) -> InitializedLoopState {
        let mut size = window.inner_size();
        size.width = size.width.max(1);
        size.height = size.height.max(1);

        let instance = wgpu::Instance::default();

        let surface = instance.create_surface(window.clone()).unwrap();
        let adapter = wgpu::util::initialize_adapter_from_env_or_default(&instance, Some(&surface))
            .await
            .expect("Failed to find an appropriate adapter");

        // Create the logical device and command queue
        let (device, queue) = adapter
            .request_device(
                &wgpu::DeviceDescriptor {
                    label: None,
                    required_features: wgpu::Features::empty(),
                    // Make sure we use the texture resolution limits from the adapter, so we can support images the size of the swapchain.
                    required_limits: wgpu::Limits::downlevel_webgl2_defaults()
                        .using_resolution(adapter.limits()),
                },
                None,
            )
            .await
            .expect("Failed to create device");

        // Load the shaders from disk
        let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
            label: None,
            source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!("shader.wgsl"))),
        });

        let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
            label: None,
            bind_group_layouts: &[],
            push_constant_ranges: &[],
        });

        let swapchain_capabilities = surface.get_capabilities(&adapter);
        let swapchain_format = swapchain_capabilities.formats[0];

        let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
            label: None,
            layout: Some(&pipeline_layout),
            vertex: wgpu::VertexState {
                module: &shader,
                entry_point: "vs_main",
                buffers: &[],
                compilation_options: Default::default(),
            },
            fragment: Some(wgpu::FragmentState {
                module: &shader,
                entry_point: "fs_main",
                compilation_options: Default::default(),
                targets: &[Some(swapchain_format.into())],
            }),
            primitive: wgpu::PrimitiveState::default(),
            depth_stencil: None,
            multisample: wgpu::MultisampleState::default(),
            multiview: None,
        });

        let config = surface
            .get_default_config(&adapter, size.width, size.height)
            .unwrap();
        surface.configure(&device, &config);

        InitializedLoopState {
            window,
            _instance: instance,
            surface,
            _adapter: adapter,
            device,
            queue,
            _shader: shader,
            _pipeline_layout: pipeline_layout,
            render_pipeline,
            config,
        }
    }
}

impl ApplicationHandler for LoopState {
    fn resumed(&mut self, event_loop: &ActiveEventLoop) {
        if self.state.is_none() {
            #[allow(unused_mut)]
                let mut attributes = Window::default_attributes();
            #[cfg(target_arch = "wasm32")]
            {
                log::info!("Creating canvas element for wasm32 target");
                use wasm_bindgen::JsCast;
                use winit::platform::web::WindowAttributesExtWebSys;
                let canvas = web_sys::window()
                    .unwrap()
                    .document()
                    .unwrap()
                    .get_element_by_id("canvas")
                    .unwrap()
                    .dyn_into::<web_sys::HtmlCanvasElement>()
                    .unwrap();
                attributes = attributes.with_canvas(Some(canvas));
            }

            // Move this into the state struct?
            let window = Arc::new(event_loop.create_window(attributes).unwrap());
            #[cfg(not(target_arch = "wasm32"))]
            {
                self.state = Some(pollster::block_on(InitializedLoopState::new(window)));
            }
            #[cfg(target_arch = "wasm32")]
            {
                wasm_bindgen_futures::spawn_local(async move {
                    let initialized_state = InitializedLoopState::new(window).await;
                    MESSAGE_QUEUE.with_borrow_mut(|queue| {
                        queue.push(Message::Initialized(initialized_state))
                    });
                });

            }
        }
    }

    fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) {
        #[cfg(target_arch = "wasm32")]
        {
            MESSAGE_QUEUE.with_borrow_mut(|queue| {
                for message in queue.drain(..) {
                    match message {
                        Message::Initialized(state) => {
                            self.state = Some(state);
                        }
                    }
                }
            });
            event_loop.set_control_flow(ControlFlow::WaitUntil(time::Instant::now() + WAIT_TIME));
        }
    }

    fn window_event(
        &mut self,
        event_loop: &ActiveEventLoop,
        _window_id: WindowId,
        event: WindowEvent,
    ) {
        if let Some(state) = self.state.as_mut().as_mut() {
            match event {
                WindowEvent::Resized(new_size) => {
                    // Reconfigure the surface with the new size
                    state.config.width = new_size.width.max(1);
                    state.config.height = new_size.height.max(1);
                    state.surface.configure(&state.device, &state.config);
                    // On macos the window needs to be redrawn manually after resizing
                    state.window.request_redraw();
                }
                WindowEvent::RedrawRequested => {
                    let frame = state
                        .surface
                        .get_current_texture()
                        .expect("Failed to acquire next swap chain texture");
                    let view = frame
                        .texture
                        .create_view(&wgpu::TextureViewDescriptor::default());
                    let mut encoder = state
                        .device
                        .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
                    {
                        let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
                            label: None,
                            color_attachments: &[Some(wgpu::RenderPassColorAttachment {
                                view: &view,
                                resolve_target: None,
                                ops: wgpu::Operations {
                                    load: wgpu::LoadOp::Clear(wgpu::Color::GREEN),
                                    store: wgpu::StoreOp::Store,
                                },
                            })],
                            depth_stencil_attachment: None,
                            timestamp_writes: None,
                            occlusion_query_set: None,
                        });
                        rpass.set_pipeline(&state.render_pipeline);
                        rpass.draw(0..3, 0..1);
                    }

                    state.queue.submit(Some(encoder.finish()));
                    frame.present();
                }
                WindowEvent::CloseRequested => event_loop.exit(),
                _ => {}
            }
        }
    }
}

pub fn main() {
    framework::init_logger();

    log::info!("Initializing...");

    let mut loop_state = LoopState::new();
    let event_loop = EventLoop::new().unwrap();

    log::info!("Entering event loop...");
    event_loop.run_app(&mut loop_state).unwrap();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants