Struct DeferredContext

Source
pub struct DeferredContext { /* private fields */ }
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 from [wgpu::Instance] and [winit::window::Window]. To top it off, this annoying back-and-forth involves some async functions.

On wasm, 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, pollster doesn’t seem to get along with wasm.

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 (wasm), at this point it will have an unconfigured surface and no config.
        let window = self.window.get_or_insert_with(|| {
            let attributes = WindowAttributes::default().with_canvas(get_webgl_canvas());
            // Will additionally try to configure surface on native.
            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() {
            // Something went wrong building the context, abort.
            GetDeferredContextError::Build(e) => panic!("Error building context: {e}"),
            // resume() didn't run for some reason, RenderContext has no surface to use to initialize with.
            GetDeferredContextError::RequiresSurface => unreachable!("Surface should have been provided in resume()"),
            // Still building, will try again next window_event()
            GetDeferredContextError::StillBuilding => return,
        };

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

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

        Self::finally_render(context, window, surface)
    }

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

Implementations§

Source§

impl DeferredContext

Source

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

Source

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

Source

pub fn get_instance(&self) -> Instance

Source

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

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