Expand description
A starter crate to get started with wgpu
§Features
enable-sync— Feature that provides an API to createMyDeviceImplwithout awinitwindow instance. Useful for unit testing.enable-sync-winit— Feature that provides an API to create an instance ofMyDeviceImplgiven awinitinstance
§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]. - MyDevice
Impl - An abstraction of the wgpu::Surface<’lifetime>
to create from a given
winitWindow. - MyRender
Pipeline Descriptor - Descriptor to configure creating a render pipeline
Enums§
- GPUStarter
Error - GPUStarterError indicates the kind of error thrown by the
wgpu-quick-start-rsproject
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§
- GPUStarter
Result - Result type that goes with error type
GPUStarterError