more effecient rendering, avoid looping calls to redraw (known to fix slow movement on macos, should work on other os)

pull/462/head
Timothy Cronin 1 year ago
parent 3d225e287a
commit 351db7bcfa

@ -30,7 +30,7 @@ impl State {
backends: wgpu::Backends::all(),
dx12_shader_compiler: Default::default(),
});
// # Safety
//
// The surface needs to live as long as the window that created it.
@ -68,7 +68,9 @@ impl State {
// Shader code in this tutorial assumes an Srgb surface texture. Using a different
// one will result all the colors comming out darker. If you want to support non
// Srgb surfaces, you'll need to account for that when drawing to the frame.
let surface_format = surface_caps.formats.iter()
let surface_format = surface_caps
.formats
.iter()
.copied()
.filter(|f| f.describe().srgb)
.next()
@ -109,6 +111,7 @@ impl State {
self.config.width = new_size.width;
self.config.height = new_size.height;
self.surface.configure(&self.device, &self.config);
self.window.request_redraw();
}
}
@ -209,16 +212,15 @@ async fn run() {
match state.render() {
Ok(_) => {}
// Reconfigure the surface if it's lost or outdated
Err(wgpu::SurfaceError::Lost | wgpu::SurfaceError::Outdated) => state.resize(state.size),
Err(wgpu::SurfaceError::Lost | wgpu::SurfaceError::Outdated) => {
state.resize(state.size)
}
// The system is out of memory, we should probably quit
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,
// We're ignoring timeouts
Err(wgpu::SurfaceError::Timeout) => log::warn!("Surface timeout"),
}
}
Event::MainEventsCleared => {
state.window().request_redraw();
}
_ => {}
}
});

@ -34,7 +34,7 @@ impl State {
// The surface needs to live as long as the window that created it.
// State owns the window so this should be safe.
let surface = unsafe { instance.create_surface(&window) }.unwrap();
let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::default(),
@ -67,7 +67,9 @@ impl State {
// Shader code in this tutorial assumes an Srgb surface texture. Using a different
// one will result all the colors comming out darker. If you want to support non
// Srgb surfaces, you'll need to account for that when drawing to the frame.
let surface_format = surface_caps.formats.iter()
let surface_format = surface_caps
.formats
.iter()
.copied()
.filter(|f| f.describe().srgb)
.next()
@ -103,6 +105,7 @@ impl State {
self.config.width = new_size.width;
self.config.height = new_size.height;
self.surface.configure(&self.device, &self.config);
self.window.request_redraw();
}
}
@ -223,18 +226,15 @@ pub async fn run() {
match state.render() {
Ok(_) => {}
// Reconfigure the surface if it's lost or outdated
Err(wgpu::SurfaceError::Lost | wgpu::SurfaceError::Outdated) => state.resize(state.size),
Err(wgpu::SurfaceError::Lost | wgpu::SurfaceError::Outdated) => {
state.resize(state.size)
}
// The system is out of memory, we should probably quit
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,
Err(wgpu::SurfaceError::Timeout) => log::warn!("Surface timeout"),
}
}
Event::RedrawEventsCleared => {
// RedrawRequested will only trigger once, unless we manually
// request it.
state.window().request_redraw();
}
_ => {}
}
});

@ -28,7 +28,7 @@ impl State {
backends: wgpu::Backends::all(),
dx12_shader_compiler: Default::default(),
});
// # Safety
//
// The surface needs to live as long as the window that created it.
@ -66,7 +66,9 @@ impl State {
// Shader code in this tutorial assumes an Srgb surface texture. Using a different
// one will result all the colors comming out darker. If you want to support non
// Srgb surfaces, you'll need to account for that when drawing to the frame.
let surface_format = surface_caps.formats.iter()
let surface_format = surface_caps
.formats
.iter()
.copied()
.filter(|f| f.describe().srgb)
.next()
@ -206,6 +208,7 @@ impl State {
self.config.width = new_size.width;
self.config.height = new_size.height;
self.surface.configure(&self.device, &self.config);
self.window.request_redraw();
}
}
@ -321,18 +324,20 @@ async fn run() {
match state.render() {
Ok(_) => {}
// Reconfigure the surface if it's lost or outdated
Err(wgpu::SurfaceError::Lost | wgpu::SurfaceError::Outdated) => state.resize(state.size),
Err(wgpu::SurfaceError::Lost | wgpu::SurfaceError::Outdated) => {
state.resize(state.size)
}
// The system is out of memory, we should probably quit
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,
// We're ignoring timeouts
Err(wgpu::SurfaceError::Timeout) => log::warn!("Surface timeout"),
}
}
Event::MainEventsCleared => {
// RedrawRequested will only trigger once, unless we manually
// request it.
state.window().request_redraw();
}
// Event::MainEventsCleared => {
// // RedrawRequested will only trigger once, unless we manually
// // request it.
// state.window().request_redraw();
// }
_ => {}
}
});

@ -6,7 +6,7 @@ use winit::{
window::{Window, WindowBuilder},
};
#[cfg(target_arch="wasm32")]
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;
struct State {
@ -30,7 +30,7 @@ impl State {
backends: wgpu::Backends::all(),
dx12_shader_compiler: Default::default(),
});
// # Safety
//
// The surface needs to live as long as the window that created it.
@ -68,7 +68,9 @@ impl State {
// Shader code in this tutorial assumes an Srgb surface texture. Using a different
// one will result all the colors comming out darker. If you want to support non
// Srgb surfaces, you'll need to account for that when drawing to the frame.
let surface_format = surface_caps.formats.iter()
let surface_format = surface_caps
.formats
.iter()
.copied()
.filter(|f| f.describe().srgb)
.next()
@ -161,6 +163,7 @@ impl State {
self.config.width = new_size.width;
self.config.height = new_size.height;
self.surface.configure(&self.device, &self.config);
self.window.request_redraw();
}
}
@ -213,7 +216,7 @@ impl State {
}
}
#[cfg_attr(target_arch="wasm32", wasm_bindgen(start))]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(start))]
pub async fn run() {
cfg_if::cfg_if! {
if #[cfg(target_arch = "wasm32")] {
@ -233,7 +236,7 @@ pub async fn run() {
// the size manually when on web.
use winit::dpi::PhysicalSize;
window.set_inner_size(PhysicalSize::new(450, 400));
use winit::platform::web::WindowExtWebSys;
web_sys::window()
.and_then(|win| win.document())
@ -245,7 +248,7 @@ pub async fn run() {
})
.expect("Couldn't append canvas to document body.");
}
// State::new uses async code, so we're going to wait for it to finish
let mut state = State::new(window).await;
@ -283,18 +286,20 @@ pub async fn run() {
match state.render() {
Ok(_) => {}
// Reconfigure the surface if it's lost or outdated
Err(wgpu::SurfaceError::Lost | wgpu::SurfaceError::Outdated) => state.resize(state.size),
Err(wgpu::SurfaceError::Lost | wgpu::SurfaceError::Outdated) => {
state.resize(state.size)
}
// The system is out of memory, we should probably quit
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,
// We're ignoring timeouts
Err(wgpu::SurfaceError::Timeout) => log::warn!("Surface timeout"),
}
}
Event::MainEventsCleared => {
// RedrawRequested will only trigger once, unless we manually
// request it.
state.window().request_redraw();
}
// Event::MainEventsCleared => {
// // RedrawRequested will only trigger once, unless we manually
// // request it.
// state.window().request_redraw();
// }
_ => {}
}
});

@ -91,7 +91,7 @@ impl State {
backends: wgpu::Backends::all(),
dx12_shader_compiler: Default::default(),
});
// # Safety
//
// The surface needs to live as long as the window that created it.
@ -129,7 +129,9 @@ impl State {
// Shader code in this tutorial assumes an Srgb surface texture. Using a different
// one will result all the colors comming out darker. If you want to support non
// Srgb surfaces, you'll need to account for that when drawing to the frame.
let surface_format = surface_caps.formats.iter()
let surface_format = surface_caps
.formats
.iter()
.copied()
.filter(|f| f.describe().srgb)
.next()
@ -274,6 +276,7 @@ impl State {
self.config.width = new_size.width;
self.config.height = new_size.height;
self.surface.configure(&self.device, &self.config);
self.window.request_redraw();
}
}
@ -398,18 +401,20 @@ async fn run() {
match state.render() {
Ok(_) => {}
// Reconfigure the surface if it's lost or outdated
Err(wgpu::SurfaceError::Lost | wgpu::SurfaceError::Outdated) => state.resize(state.size),
Err(wgpu::SurfaceError::Lost | wgpu::SurfaceError::Outdated) => {
state.resize(state.size)
}
// The system is out of memory, we should probably quit
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,
// We're ignoring timeouts
Err(wgpu::SurfaceError::Timeout) => log::warn!("Surface timeout"),
}
}
Event::MainEventsCleared => {
// RedrawRequested will only trigger once, unless we manually
// request it.
state.window().request_redraw();
}
// Event::MainEventsCleared => {
// // RedrawRequested will only trigger once, unless we manually
// // request it.
// state.window().request_redraw();
// }
_ => {}
}
});

@ -7,7 +7,7 @@ use winit::{
window::{Window, WindowBuilder},
};
#[cfg(target_arch="wasm32")]
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;
#[repr(C)]
@ -87,7 +87,7 @@ impl State {
backends: wgpu::Backends::all(),
dx12_shader_compiler: Default::default(),
});
// # Safety
//
// The surface needs to live as long as the window that created it.
@ -125,7 +125,9 @@ impl State {
// Shader code in this tutorial assumes an Srgb surface texture. Using a different
// one will result all the colors comming out darker. If you want to support non
// Srgb surfaces, you'll need to account for that when drawing to the frame.
let surface_format = surface_caps.formats.iter()
let surface_format = surface_caps
.formats
.iter()
.copied()
.filter(|f| f.describe().srgb)
.next()
@ -233,6 +235,7 @@ impl State {
self.config.width = new_size.width;
self.config.height = new_size.height;
self.surface.configure(&self.device, &self.config);
self.window.request_redraw();
}
}
@ -287,7 +290,7 @@ impl State {
}
}
#[cfg_attr(target_arch="wasm32", wasm_bindgen(start))]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(start))]
pub async fn run() {
cfg_if::cfg_if! {
if #[cfg(target_arch = "wasm32")] {
@ -307,7 +310,7 @@ pub async fn run() {
// the size manually when on web.
use winit::dpi::PhysicalSize;
window.set_inner_size(PhysicalSize::new(450, 400));
use winit::platform::web::WindowExtWebSys;
web_sys::window()
.and_then(|win| win.document())
@ -357,18 +360,20 @@ pub async fn run() {
match state.render() {
Ok(_) => {}
// Reconfigure the surface if it's lost or outdated
Err(wgpu::SurfaceError::Lost | wgpu::SurfaceError::Outdated) => state.resize(state.size),
Err(wgpu::SurfaceError::Lost | wgpu::SurfaceError::Outdated) => {
state.resize(state.size)
}
// The system is out of memory, we should probably quit
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,
// We're ignoring timeouts
Err(wgpu::SurfaceError::Timeout) => log::warn!("Surface timeout"),
}
}
Event::MainEventsCleared => {
// RedrawRequested will only trigger once, unless we manually
// request it.
state.window().request_redraw();
}
// Event::MainEventsCleared => {
// // RedrawRequested will only trigger once, unless we manually
// // request it.
// state.window().request_redraw();
// }
_ => {}
}
});

@ -93,7 +93,7 @@ impl State {
backends: wgpu::Backends::all(),
dx12_shader_compiler: Default::default(),
});
// # Safety
//
// The surface needs to live as long as the window that created it.
@ -130,7 +130,9 @@ impl State {
// Shader code in this tutorial assumes an Srgb surface texture. Using a different
// one will result all the colors comming out darker. If you want to support non
// Srgb surfaces, you'll need to account for that when drawing to the frame.
let surface_format = surface_caps.formats.iter()
let surface_format = surface_caps
.formats
.iter()
.copied()
.filter(|f| f.describe().srgb)
.next()
@ -305,6 +307,7 @@ impl State {
self.config.width = new_size.width;
self.config.height = new_size.height;
self.surface.configure(&self.device, &self.config);
self.window.request_redraw();
}
}
@ -425,18 +428,15 @@ async fn run() {
match state.render() {
Ok(_) => {}
// Reconfigure the surface if it's lost or outdated
Err(wgpu::SurfaceError::Lost | wgpu::SurfaceError::Outdated) => state.resize(state.size),
Err(wgpu::SurfaceError::Lost | wgpu::SurfaceError::Outdated) => {
state.resize(state.size)
}
// The system is out of memory, we should probably quit
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,
// We're ignoring timeouts
Err(wgpu::SurfaceError::Timeout) => log::warn!("Surface timeout"),
}
}
Event::MainEventsCleared => {
// RedrawRequested will only trigger once, unless we manually
// request it.
state.window().request_redraw();
}
_ => {}
}
});

@ -7,7 +7,7 @@ use winit::{
window::{Window, WindowBuilder},
};
#[cfg(target_arch="wasm32")]
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;
mod texture;
@ -93,7 +93,7 @@ impl State {
backends: wgpu::Backends::all(),
dx12_shader_compiler: Default::default(),
});
// # Safety
//
// The surface needs to live as long as the window that created it.
@ -130,7 +130,9 @@ impl State {
// Shader code in this tutorial assumes an Srgb surface texture. Using a different
// one will result all the colors comming out darker. If you want to support non
// Srgb surfaces, you'll need to account for that when drawing to the frame.
let surface_format = surface_caps.formats.iter()
let surface_format = surface_caps
.formats
.iter()
.copied()
.filter(|f| f.describe().srgb)
.next()
@ -282,6 +284,7 @@ impl State {
self.config.width = new_size.width;
self.config.height = new_size.height;
self.surface.configure(&self.device, &self.config);
self.window.request_redraw();
}
}
@ -337,7 +340,7 @@ impl State {
}
}
#[cfg_attr(target_arch="wasm32", wasm_bindgen(start))]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(start))]
pub async fn run() {
cfg_if::cfg_if! {
if #[cfg(target_arch = "wasm32")] {
@ -357,7 +360,7 @@ pub async fn run() {
// the size manually when on web.
use winit::dpi::PhysicalSize;
window.set_inner_size(PhysicalSize::new(450, 400));
use winit::platform::web::WindowExtWebSys;
web_sys::window()
.and_then(|win| win.document())
@ -369,7 +372,7 @@ pub async fn run() {
})
.expect("Couldn't append canvas to document body.");
}
// State::new uses async code, so we're going to wait for it to finish
let mut state = State::new(window).await;
@ -407,18 +410,15 @@ pub async fn run() {
match state.render() {
Ok(_) => {}
// Reconfigure the surface if it's lost or outdated
Err(wgpu::SurfaceError::Lost | wgpu::SurfaceError::Outdated) => state.resize(state.size),
Err(wgpu::SurfaceError::Lost | wgpu::SurfaceError::Outdated) => {
state.resize(state.size)
}
// The system is out of memory, we should probably quit
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,
// We're ignoring timeouts
Err(wgpu::SurfaceError::Timeout) => log::warn!("Surface timeout"),
}
}
Event::MainEventsCleared => {
// RedrawRequested will only trigger once, unless we manually
// request it.
state.window().request_redraw();
}
_ => {}
}
});

@ -243,7 +243,7 @@ impl State {
backends: wgpu::Backends::all(),
dx12_shader_compiler: Default::default(),
});
// # Safety
//
// The surface needs to live as long as the window that created it.
@ -280,7 +280,9 @@ impl State {
// Shader code in this tutorial assumes an Srgb surface texture. Using a different
// one will result all the colors comming out darker. If you want to support non
// Srgb surfaces, you'll need to account for that when drawing to the frame.
let surface_format = surface_caps.formats.iter()
let surface_format = surface_caps
.formats
.iter()
.copied()
.filter(|f| f.describe().srgb)
.next()
@ -485,6 +487,7 @@ impl State {
self.camera_staging.camera.aspect =
self.config.width as f32 / self.config.height as f32;
self.window.request_redraw();
}
}
@ -596,18 +599,20 @@ async fn run() {
match state.render() {
Ok(_) => {}
// Reconfigure the surface if it's lost or outdated
Err(wgpu::SurfaceError::Lost | wgpu::SurfaceError::Outdated) => state.resize(state.size),
Err(wgpu::SurfaceError::Lost | wgpu::SurfaceError::Outdated) => {
state.resize(state.size)
}
// The system is out of memory, we should probably quit
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,
// We're ignoring timeouts
Err(wgpu::SurfaceError::Timeout) => log::warn!("Surface timeout"),
}
}
Event::MainEventsCleared => {
// RedrawRequested will only trigger once, unless we manually
// request it.
state.window().request_redraw();
}
// Event::MainEventsCleared => {
// // RedrawRequested will only trigger once, unless we manually
// // request it.
// state.window().request_redraw();
// }
_ => {}
}
});

@ -7,7 +7,7 @@ use winit::{
window::{Window, WindowBuilder},
};
#[cfg(target_arch="wasm32")]
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;
mod texture;
@ -243,7 +243,7 @@ impl State {
backends: wgpu::Backends::all(),
dx12_shader_compiler: Default::default(),
});
// # Safety
//
// The surface needs to live as long as the window that created it.
@ -280,7 +280,9 @@ impl State {
// Shader code in this tutorial assumes an Srgb surface texture. Using a different
// one will result all the colors comming out darker. If you want to support non
// Srgb surfaces, you'll need to account for that when drawing to the frame.
let surface_format = surface_caps.formats.iter()
let surface_format = surface_caps
.formats
.iter()
.copied()
.filter(|f| f.describe().srgb)
.next()
@ -483,6 +485,7 @@ impl State {
self.surface.configure(&self.device, &self.config);
self.camera.aspect = self.config.width as f32 / self.config.height as f32;
self.window.request_redraw();
}
}
@ -546,7 +549,7 @@ impl State {
}
}
#[cfg_attr(target_arch="wasm32", wasm_bindgen(start))]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(start))]
pub async fn run() {
cfg_if::cfg_if! {
if #[cfg(target_arch = "wasm32")] {
@ -566,7 +569,7 @@ pub async fn run() {
// the size manually when on web.
use winit::dpi::PhysicalSize;
window.set_inner_size(PhysicalSize::new(450, 400));
use winit::platform::web::WindowExtWebSys;
web_sys::window()
.and_then(|win| win.document())
@ -616,18 +619,20 @@ pub async fn run() {
match state.render() {
Ok(_) => {}
// Reconfigure the surface if it's lost or outdated
Err(wgpu::SurfaceError::Lost | wgpu::SurfaceError::Outdated) => state.resize(state.size),
Err(wgpu::SurfaceError::Lost | wgpu::SurfaceError::Outdated) => {
state.resize(state.size)
}
// The system is out of memory, we should probably quit
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,
// We're ignoring timeouts
Err(wgpu::SurfaceError::Timeout) => log::warn!("Surface timeout"),
}
}
Event::MainEventsCleared => {
// RedrawRequested will only trigger once, unless we manually
// request it.
state.window().request_redraw();
}
// Event::MainEventsCleared => {
// // RedrawRequested will only trigger once, unless we manually
// // request it.
// state.window().request_redraw();
// }
_ => {}
}
});

@ -297,7 +297,7 @@ impl State {
backends: wgpu::Backends::all(),
dx12_shader_compiler: Default::default(),
});
// # Safety
//
// The surface needs to live as long as the window that created it.
@ -334,7 +334,9 @@ impl State {
// Shader code in this tutorial assumes an Srgb surface texture. Using a different
// one will result all the colors comming out darker. If you want to support non
// Srgb surfaces, you'll need to account for that when drawing to the frame.
let surface_format = surface_caps.formats.iter()
let surface_format = surface_caps
.formats
.iter()
.copied()
.filter(|f| f.describe().srgb)
.next()
@ -571,6 +573,7 @@ impl State {
self.surface.configure(&self.device, &self.config);
self.camera.aspect = self.config.width as f32 / self.config.height as f32;
self.window.request_redraw();
}
}
@ -697,18 +700,20 @@ async fn run() {
match state.render() {
Ok(_) => {}
// Reconfigure the surface if it's lost or outdated
Err(wgpu::SurfaceError::Lost | wgpu::SurfaceError::Outdated) => state.resize(state.size),
Err(wgpu::SurfaceError::Lost | wgpu::SurfaceError::Outdated) => {
state.resize(state.size)
}
// The system is out of memory, we should probably quit
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,
// We're ignoring timeouts
Err(wgpu::SurfaceError::Timeout) => log::warn!("Surface timeout"),
}
}
Event::MainEventsCleared => {
// RedrawRequested will only trigger once, unless we manually
// request it.
state.window().request_redraw();
}
// Event::MainEventsCleared => {
// // RedrawRequested will only trigger once, unless we manually
// // request it.
// state.window().request_redraw();
// }
_ => {}
}
});

@ -8,7 +8,7 @@ use winit::{
window::{Window, WindowBuilder},
};
#[cfg(target_arch="wasm32")]
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;
mod texture;
@ -304,7 +304,7 @@ impl State {
backends: wgpu::Backends::all(),
dx12_shader_compiler: Default::default(),
});
// # Safety
//
// The surface needs to live as long as the window that created it.
@ -341,7 +341,9 @@ impl State {
// Shader code in this tutorial assumes an Srgb surface texture. Using a different
// one will result all the colors comming out darker. If you want to support non
// Srgb surfaces, you'll need to account for that when drawing to the frame.
let surface_format = surface_caps.formats.iter()
let surface_format = surface_caps
.formats
.iter()
.copied()
.filter(|f| f.describe().srgb)
.next()
@ -579,6 +581,7 @@ impl State {
self.surface.configure(&self.device, &self.config);
self.camera.aspect = self.config.width as f32 / self.config.height as f32;
self.window.request_redraw();
}
}
@ -664,7 +667,7 @@ pub async fn run() {
// the size manually when on web.
use winit::dpi::PhysicalSize;
window.set_inner_size(PhysicalSize::new(450, 400));
use winit::platform::web::WindowExtWebSys;
web_sys::window()
.and_then(|win| win.document())
@ -714,18 +717,20 @@ pub async fn run() {
match state.render() {
Ok(_) => {}
// Reconfigure the surface if it's lost or outdated
Err(wgpu::SurfaceError::Lost | wgpu::SurfaceError::Outdated) => state.resize(state.size),
Err(wgpu::SurfaceError::Lost | wgpu::SurfaceError::Outdated) => {
state.resize(state.size)
}
// The system is out of memory, we should probably quit
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,
// We're ignoring timeouts
Err(wgpu::SurfaceError::Timeout) => log::warn!("Surface timeout"),
}
}
Event::MainEventsCleared => {
// RedrawRequested will only trigger once, unless we manually
// request it.
state.window().request_redraw();
}
// Event::MainEventsCleared => {
// // RedrawRequested will only trigger once, unless we manually
// // request it.
// state.window().request_redraw();
// }
_ => {}
}
});

@ -1,7 +1,7 @@
use std::iter;
use cgmath::prelude::*;
use wgpu::{include_spirv_raw};
use wgpu::include_spirv_raw;
use wgpu::util::DeviceExt;
use winit::{
event::*,
@ -296,7 +296,11 @@ struct DepthPass {
impl DepthPass {
fn new(device: &wgpu::Device, config: &wgpu::SurfaceConfiguration) -> Self {
let texture = texture::Texture::create_depth_texture_non_comparison_sampler(device, config, "depth_texture");
let texture = texture::Texture::create_depth_texture_non_comparison_sampler(
device,
config,
"depth_texture",
);
let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("Depth Pass Layout"),
@ -413,7 +417,11 @@ impl DepthPass {
}
fn resize(&mut self, device: &wgpu::Device, config: &wgpu::SurfaceConfiguration) {
self.texture = texture::Texture::create_depth_texture_non_comparison_sampler(device, config, "depth_texture");
self.texture = texture::Texture::create_depth_texture_non_comparison_sampler(
device,
config,
"depth_texture",
);
self.bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &self.layout,
entries: &[
@ -486,7 +494,7 @@ impl State {
backends: wgpu::Backends::all(),
dx12_shader_compiler: Default::default(),
});
// # Safety
//
// The surface needs to live as long as the window that created it.
@ -523,7 +531,9 @@ impl State {
// Shader code in this tutorial assumes an Srgb surface texture. Using a different
// one will result all the colors comming out darker. If you want to support non
// Srgb surfaces, you'll need to account for that when drawing to the frame.
let surface_format = surface_caps.formats.iter()
let surface_format = surface_caps
.formats
.iter()
.copied()
.filter(|f| f.describe().srgb)
.next()
@ -775,6 +785,7 @@ impl State {
self.depth_pass.resize(&self.device, &self.config);
self.camera.aspect = self.config.width as f32 / self.config.height as f32;
self.window.request_redraw();
}
}
@ -894,18 +905,15 @@ async fn run() {
match state.render() {
Ok(_) => {}
// Reconfigure the surface if it's lost or outdated
Err(wgpu::SurfaceError::Lost | wgpu::SurfaceError::Outdated) => state.resize(state.size),
Err(wgpu::SurfaceError::Lost | wgpu::SurfaceError::Outdated) => {
state.resize(state.size)
}
// The system is out of memory, we should probably quit
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,
// We're ignoring timeouts
Err(wgpu::SurfaceError::Timeout) => log::warn!("Surface timeout"),
}
}
Event::MainEventsCleared => {
// RedrawRequested will only trigger once, unless we manually
// request it.
state.window().request_redraw();
}
_ => {}
}
});

@ -8,7 +8,7 @@ use winit::{
window::{Window, WindowBuilder},
};
#[cfg(target_arch="wasm32")]
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;
mod texture;
@ -314,7 +314,7 @@ impl State {
backends: wgpu::Backends::all(),
dx12_shader_compiler: Default::default(),
});
// # Safety
//
// The surface needs to live as long as the window that created it.
@ -351,7 +351,9 @@ impl State {
// Shader code in this tutorial assumes an Srgb surface texture. Using a different
// one will result all the colors comming out darker. If you want to support non
// Srgb surfaces, you'll need to account for that when drawing to the frame.
let surface_format = surface_caps.formats.iter()
let surface_format = surface_caps
.formats
.iter()
.copied()
.filter(|f| f.describe().srgb)
.next()
@ -600,6 +602,7 @@ impl State {
// NEW!
self.depth_texture =
texture::Texture::create_depth_texture(&self.device, &self.config, "depth_texture");
self.window.request_redraw();
}
}
@ -671,7 +674,7 @@ impl State {
}
}
#[cfg_attr(target_arch="wasm32", wasm_bindgen(start))]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(start))]
pub async fn run() {
cfg_if::cfg_if! {
if #[cfg(target_arch = "wasm32")] {
@ -691,7 +694,7 @@ pub async fn run() {
// the size manually when on web.
use winit::dpi::PhysicalSize;
window.set_inner_size(PhysicalSize::new(450, 400));
use winit::platform::web::WindowExtWebSys;
web_sys::window()
.and_then(|win| win.document())
@ -703,7 +706,7 @@ pub async fn run() {
})
.expect("Couldn't append canvas to document body.");
}
// State::new uses async code, so we're going to wait for it to finish
let mut state = State::new(window).await;
@ -741,18 +744,15 @@ pub async fn run() {
match state.render() {
Ok(_) => {}
// Reconfigure the surface if it's lost or outdated
Err(wgpu::SurfaceError::Lost | wgpu::SurfaceError::Outdated) => state.resize(state.size),
Err(wgpu::SurfaceError::Lost | wgpu::SurfaceError::Outdated) => {
state.resize(state.size)
}
// The system is out of memory, we should probably quit
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,
// We're ignoring timeouts
Err(wgpu::SurfaceError::Timeout) => log::warn!("Surface timeout"),
}
}
Event::MainEventsCleared => {
// RedrawRequested will only trigger once, unless we manually
// request it.
state.window().request_redraw();
}
_ => {}
}
});

@ -8,12 +8,12 @@ use winit::{
window::Window,
};
#[cfg(target_arch="wasm32")]
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;
mod model;
mod texture;
mod resources;
mod texture;
use model::{DrawModel, Vertex};
@ -254,7 +254,7 @@ impl State {
backends: wgpu::Backends::all(),
dx12_shader_compiler: Default::default(),
});
// # Safety
//
// The surface needs to live as long as the window that created it.
@ -294,7 +294,9 @@ impl State {
// Shader code in this tutorial assumes an Srgb surface texture. Using a different
// one will result all the colors comming out darker. If you want to support non
// Srgb surfaces, you'll need to account for that when drawing to the frame.
let surface_format = surface_caps.formats.iter()
let surface_format = surface_caps
.formats
.iter()
.copied()
.filter(|f| f.describe().srgb)
.next()
@ -409,12 +411,10 @@ impl State {
});
log::warn!("Load model");
let obj_model = resources::load_model(
"cube.obj",
&device,
&queue,
&texture_bind_group_layout,
).await.unwrap();
let obj_model =
resources::load_model("cube.obj", &device, &queue, &texture_bind_group_layout)
.await
.unwrap();
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some("shader.wgsl"),
@ -514,6 +514,7 @@ impl State {
self.surface.configure(&self.device, &self.config);
self.depth_texture =
texture::Texture::create_depth_texture(&self.device, &self.config, "depth_texture");
self.window.request_redraw();
}
}
fn input(&mut self, event: &WindowEvent) -> bool {
@ -584,7 +585,7 @@ impl State {
}
}
#[cfg_attr(target_arch="wasm32", wasm_bindgen(start))]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(start))]
pub async fn run() {
cfg_if::cfg_if! {
if #[cfg(target_arch = "wasm32")] {
@ -602,14 +603,13 @@ pub async fn run() {
.build(&event_loop)
.unwrap();
#[cfg(target_arch = "wasm32")]
{
// Winit prevents sizing with CSS, so we have to set
// the size manually when on web.
use winit::dpi::PhysicalSize;
window.set_inner_size(PhysicalSize::new(450, 400));
use winit::platform::web::WindowExtWebSys;
web_sys::window()
.and_then(|win| win.document())
@ -628,7 +628,6 @@ pub async fn run() {
event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Poll;
match event {
Event::MainEventsCleared => state.window().request_redraw(),
Event::WindowEvent {
ref event,
window_id,
@ -660,7 +659,9 @@ pub async fn run() {
match state.render() {
Ok(_) => {}
// Reconfigure the surface if it's lost or outdated
Err(wgpu::SurfaceError::Lost | wgpu::SurfaceError::Outdated) => state.resize(state.size),
Err(wgpu::SurfaceError::Lost | wgpu::SurfaceError::Outdated) => {
state.resize(state.size)
}
// The system is out of memory, we should probably quit
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,
// We're ignoring timeouts

Loading…
Cancel
Save