Crate wgpu_quick_start

Crate wgpu_quick_start 

Source
Expand description

A starter crate to get started with wgpu

§Features

  • enable-sync — Feature that provides an API to create MyDeviceImpl without a winit window instance. Useful for unit testing.
  • enable-sync-winit — Feature that provides an API to create an instance of MyDeviceImpl given a winit instance

§Examples

An example to launch a winit window and use this library to get started.

use wgpu_quick_start::{MyDevice};

use wgpu_quick_start::create_new_device;
use winit::{event::WindowEvent, window::WindowAttributes};
use winit_app::{AppWindowEvent, Application};


fn launch() -> Result<(), Box<dyn std::error::Error>> {
   let winit_app = Application::new();
   let mut opt_device: Option<Box<dyn MyDevice>> = None;
   winit_app.run(
       WindowAttributes::default().with_title("wgpu starter app"),
       move |app_window_event| match app_window_event {
           AppWindowEvent::NewWindow(window) => match create_new_device(window) {
               Ok(value) => {
                   opt_device = Some(value);
               }
               Err(err) => {
                   // warning - Error creating new surface from the window
               }
           },
           AppWindowEvent::OnWindowEvent(event, event_loop) => {
               if let Some(local_device) = opt_device.as_mut() {
                   match event {
                       WindowEvent::CloseRequested => {
                           event_loop.exit();
                       }
                       WindowEvent::SurfaceResized(size) => {
                           // Resized
                           local_device.resize((size.width, size.height));
                       }
                       WindowEvent::RedrawRequested => {
                           match local_device.get_current_texture() {
                               Ok(output) => {
                                   let texture_view_descriptor=  wgpu::TextureViewDescriptor::default();
                                   let view = output.texture.create_view(&texture_view_descriptor);
                                   let device = local_device.get_device();
                                   let mut encoder = device.create_command_encoder(
                                        &wgpu::CommandEncoderDescriptor {
                                            label: Some("Render Encoder"),
                                        },
                                   );
                                   {
                                    let _render_pass =
                                       wgpu_quick_start::create_default_render_pass(
                                           &mut encoder,
                                           "root-render-pass".to_owned(),
                                           wgpu::Color {
                                               r: 0.9,
                                               g: 0.9,
                                               b: 0.9,
                                               a: 1.0,
                                           },
                                           &view,
                                       );
                                       // TODO: Render objects using render pass
                                   }
                                   let queue = local_device.get_queue();
                                   queue.submit(std::iter::once(encoder.finish()));
                                   output.present();
                               }
                               Err(err) => {
                                   // warning - error creating the texture
                               }
                           }
                       }
                       _ => {}
                   }
               }
           }
       },
   )?;
   Ok(())
}

Structs§

GPUInstance
GPUInstance is a abstraction of [wgpu::Instance].
MyDeviceImpl
An abstraction of the wgpu::Surface<’lifetime> to create from a given winit Window.
MyRenderPipelineDescriptor
Descriptor to configure creating a render pipeline

Enums§

GPUStarterError
GPUStarterError indicates the kind of error thrown by the wgpu-quick-start-rs project

Traits§

MyDevice
Trait as an abstraction of the underlying device

Functions§

create_default_pipeline_layout
Create a pipeline layout with default values
create_default_render_pass
Create a new render pass for the given encoder with a given label and clear with a particular color to clear
create_default_render_pipeline
Create a new render pipeline with default values.
create_index_buffer
create an index buffer from the given vertices and indices
create_new_device
This synchronous function helps create a new surface from the given window.
create_new_windowless_device
This synchronous function helps create a new device without any window
create_shader
create a shader from the given source, with the given label
create_vertex_buffer
create a vertex buffer from the given vertices

Type Aliases§

GPUStarterResult
Result type that goes with error type GPUStarterError