Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Context

The first step of any renderling program starts with renderling::context::Context.

The Context is responsible for managing the underlying wgpu runtime, including the instance, adapter and queue. It also sets up the RenderTarget, according to how the Context was created.

On that note, it's important to know that there are two main ways to create a Context:

  1. A headless context, which renders to a texture, can be created with Context::headless or Context::try_new_headless, depending on your error handling scenario.
  2. A surface context, with a window (possibly from winit) or a canvas from web-sys.
    use renderling::context::Context;

    let ctx = Context::headless(256, 256).await;

Getting a frame

Another important concept is the Frame. Each time you'd like to present a new image you must acquire a frame from the Context with Context::get_next_frame and present it with Frame::present.

Presenting on WASM

When on WASM (aka running in a browser), Frame::present is a noop. It's still a good idea to use it though, so you don't forget when programming in native.

Saving the frame

You can also read out the frame to an image provided by the image crate. See the Frame docs for help with the read_* functions.

Frame example

    let frame = ctx.get_next_frame().unwrap();
    // ...do some rendering
    //
    // Then capture the frame into an image, if you like
    let _image_capture = frame.read_image().await.unwrap();
    frame.present();