Skip to main content

DeferredContext

Struct DeferredContext 

Source
pub struct DeferredContext { /* private fields */ }
Available on crate features winit only.
Expand description

Defer the contstruction of the RenderContext until a surface is constructed.

WebGL really screws up the normal workflow of “first create your wgpu handles, then make windows and stuff”, because creating the wgpu::Adapter requires a wgpu::Surface to be passed in after creating it with a wgpu::Instance using the configuration of a winit::window::Window. To top it all off, this painful back-and-forth involves async.

On web, this struct caches a builder and constructs the RenderContext in a JS thread oppurtunistically when it is used to construct a SurfaceWindow.
Native platforms block the thread when constructing, it’s not an operation that should take very long. pollster doesn’t seem to get along with JS however.

See example usage below. There’s a bit of jumping through hoops, but most of it is handled in the background.

impl ApplicationHandler for YourApp {
    fn resume(&mut self, event_loop: &ActiveEventLoop) {
        // Create a window and provide the surface to the deferred context to start building.
        // In the worst case (web), at this point it will have an unconfigured surface and no config.
        let window = self.window.get_or_init(|| {
            let mut attributes = WindowAttributes::default();
            #[cfg(target_arch = "wasm32")]
            attributes.with_canvas(get_webgl_canvas());
            // On web, will provide the surface to the context builder so it can
            // start building the context. The surface isn't configured until it's needed.
            self.deferred_context.create_window(event_loop, attributes, Default::default())
                .expect("Couldn't create window");
        });

        // Can't do much else until we have a valid context, which may take until
        // the next function call to finish building. Move on to main loop.
        self.suspended = false;
    }

    fn window_event(&mut self, event_loop: &ActiveEventLoop, window_id: _, event: _) {
        if self.suspended { return; }

        let context = match self.defered_context.get_context() {
            Ok(c) => c,
            // Something went wrong building the context, abort.
            Err(GetDeferredContextError::Build(e)) => panic!("Error building context: {e}"),
            // resume() didn't run for some reason, RenderContext has no surface
            // to use to initialize with (on web).
            Err(GetDeferredContextError::RequiresSurface) => unreachable!("Surface should have been provided in resume()"),
            // Still building, will try again next window_event().
            Err(GetDeferredContextError::StillBuilding) => return,
        };

        // Now that the context is ready, we can finish configuring the window surface.
        // init_surface() will create everything when needed and make sure it's configured.
        let active_window = self.window.get().expect("Window should have created in resume()")
            .init_with_context(&context).expect("Couldn't init window");

        // Side-note: Vertex/shader buffers, pipelines, etc. are also dependent on the
        // RenderContext being configured (with a surface on web).
        // Put the buffers in an Option/OnceCell and generate them ASAP.
        let buffers_and_shit = self.buffers_and_shit.get_or_insert_with(|| init_buffers_and_shit(&context, active_window));

        Self::finally_render(context, active_window);
    }

    fn suspended(&mut self, event_loop: &ActiveEventLoop) {
        // Android requires surfaces be dropped on suspend. init_surface() above
        // will re-create and configure the window surface automatically when unsuspended.
        self.window.get_mut().map(|w| w.drop_surface());
        self.suspended = true;
    }
}

Implementations§

Source§

impl DeferredContext

Source

pub fn new(builder: RenderContextBuilder) -> Self

Available on crate features webgl or poll only.

Create a new deferred builder from a regular builder, with the appropriate method for the platform.

On web, this will call Self::new_requiring_surface; on native with the poll feature, Self::new_poll.

Source

pub fn new_requiring_surface(builder: RenderContextBuilder) -> Self

Available on crate features webgl or poll only.

Create a new deferred builder from a regular builder, in “waiting-for-surface” mode. On native you don’t have to use this unless you really want to validate the surface format is compatible.

Source

pub async fn new_async(builder: RenderContextBuilder) -> Self

Available on non-crate feature webgl only.

Create a future that immediately tries to await the finalization of a RenderContextBuilder.

It’s not really intended to use mew in an async environment - the app as a whole can contain async logic, sure, but rendering stuff deserves its own system thread.

Source

pub fn new_poll(builder: RenderContextBuilder) -> Self

Available on non-crate feature webgl and crate feature poll only.

Immediately try to finalize a RenderContextBuilder, by blocking on Self::new_async.

Realistically, it should not take long at all to build a RenderContext. Even if it does, it should be fine to block the main render thread because, well, what are you going to render without a context?

Source

pub fn create_window( &self, event_loop: &ActiveEventLoop, attributes: WindowAttributes, config_opts: SurfaceConfigOptions, ) -> Result<SurfaceWindow, CreateWindowError>

Create a SurfaceWindow, and at the same time, provide the builder with a WebGL surface so it can create a valid wgpu::Adapter.

In effect, you can almost ignore the whole initialization workflow - just make a window in the event loop, and then get the context to draw to it. If you can’t get the context, try again until you can.

Source

pub fn get_instance(&self) -> Instance

Get a clone of the wgpu::Instance, which may have been created automatically.

Source

pub fn get_context(&self) -> Result<&RenderContext, GetDeferredContextError>

Try to get the RenderContext.

If it hasn’t been built yet, or if it ran into an error while building, you’ll get an Err(_) - try again next frame.

On web, make sure to create a window with Self::create_window, as you can’t get a wgpu::Adapter before providing a wgpu::Surface.
Makes sense, right? Totally not backwards? That’s why I made this.

Trait Implementations§

Source§

impl Debug for DeferredContext

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> Downcast<T> for T

§

fn downcast(&self) -> &T

§

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> Upcast<T> for T

§

fn upcast(&self) -> Option<&T>

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<T> WasmNotSend for T
where T: Send,