|
@@ -9,7 +9,8 @@ use crate::render::{
|
|
|
use std::{
|
|
use std::{
|
|
|
f64::consts::*,
|
|
f64::consts::*,
|
|
|
fs,
|
|
fs,
|
|
|
- iter
|
|
|
|
|
|
|
+ iter,
|
|
|
|
|
+ sync::Arc
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
use bytemuck::cast_slice;
|
|
use bytemuck::cast_slice;
|
|
@@ -20,14 +21,15 @@ use wgpu::{
|
|
|
};
|
|
};
|
|
|
use winit::{
|
|
use winit::{
|
|
|
dpi::*,
|
|
dpi::*,
|
|
|
- window::{CursorGrabMode, Window}
|
|
|
|
|
|
|
+ window::{CursorGrabMode, Window, WindowId}
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
pub type WindowSize = PhysicalSize<u32>;
|
|
pub type WindowSize = PhysicalSize<u32>;
|
|
|
pub type MousePos = PhysicalPosition<f64>;
|
|
pub type MousePos = PhysicalPosition<f64>;
|
|
|
|
|
|
|
|
-pub struct Renderer<'w> {
|
|
|
|
|
- surface: Surface<'w>,
|
|
|
|
|
|
|
+pub struct Renderer {
|
|
|
|
|
+ window: Arc<Window>,
|
|
|
|
|
+ surface: Surface<'static>,
|
|
|
device: Device,
|
|
device: Device,
|
|
|
queue: Queue,
|
|
queue: Queue,
|
|
|
config: SurfaceConfiguration,
|
|
config: SurfaceConfiguration,
|
|
@@ -48,12 +50,13 @@ pub struct Renderer<'w> {
|
|
|
mesh_instances: Buffer
|
|
mesh_instances: Buffer
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-impl<'w> Renderer<'w> {
|
|
|
|
|
- pub async fn new(window: &'w Window, path: &str) -> Self {
|
|
|
|
|
|
|
+impl Renderer {
|
|
|
|
|
+ pub async fn new(window: Window, path: &str) -> Self {
|
|
|
let size = window.inner_size();
|
|
let size = window.inner_size();
|
|
|
|
|
|
|
|
- let instance = Instance::new(InstanceDescriptor::default());
|
|
|
|
|
- let surface = instance.create_surface(window)
|
|
|
|
|
|
|
+ let window = Arc::new(window);
|
|
|
|
|
+ let instance = Instance::new(InstanceDescriptor::new_without_display_handle());
|
|
|
|
|
+ let surface = instance.create_surface(window.clone())
|
|
|
.expect("Failed to create surface");
|
|
.expect("Failed to create surface");
|
|
|
let adapter = instance.request_adapter(
|
|
let adapter = instance.request_adapter(
|
|
|
// want to find the fastest gpu first, must support rendering to our surface
|
|
// want to find the fastest gpu first, must support rendering to our surface
|
|
@@ -62,17 +65,17 @@ impl<'w> Renderer<'w> {
|
|
|
compatible_surface: Some(&surface),
|
|
compatible_surface: Some(&surface),
|
|
|
// no software rendering please
|
|
// no software rendering please
|
|
|
force_fallback_adapter: false,
|
|
force_fallback_adapter: false,
|
|
|
|
|
+ apply_limit_buckets: false // don't care about fingerprinting
|
|
|
}
|
|
}
|
|
|
).await.expect("Failed to find a suitable gpu");
|
|
).await.expect("Failed to find a suitable gpu");
|
|
|
|
|
|
|
|
let (device, queue) = adapter.request_device(
|
|
let (device, queue) = adapter.request_device(
|
|
|
// don't care about what the gpu supports
|
|
// don't care about what the gpu supports
|
|
|
&DeviceDescriptor {
|
|
&DeviceDescriptor {
|
|
|
- label: None,
|
|
|
|
|
required_features: Features::empty(),
|
|
required_features: Features::empty(),
|
|
|
- required_limits: Limits::default()
|
|
|
|
|
- },
|
|
|
|
|
- None // Trace path
|
|
|
|
|
|
|
+ required_limits: Limits::default(),
|
|
|
|
|
+ ..Default::default()
|
|
|
|
|
+ }
|
|
|
).await.expect("Failed to find a suitable physical device");
|
|
).await.expect("Failed to find a suitable physical device");
|
|
|
|
|
|
|
|
let caps = surface.get_capabilities(&adapter);
|
|
let caps = surface.get_capabilities(&adapter);
|
|
@@ -83,6 +86,7 @@ impl<'w> Renderer<'w> {
|
|
|
// using the surface to write to the window
|
|
// using the surface to write to the window
|
|
|
usage: TextureUsages::RENDER_ATTACHMENT,
|
|
usage: TextureUsages::RENDER_ATTACHMENT,
|
|
|
format,
|
|
format,
|
|
|
|
|
+ color_space: SurfaceColorSpace::Auto,
|
|
|
width: size.width,
|
|
width: size.width,
|
|
|
height: size.height,
|
|
height: size.height,
|
|
|
present_mode: PresentMode::AutoVsync,
|
|
present_mode: PresentMode::AutoVsync,
|
|
@@ -138,8 +142,8 @@ impl<'w> Renderer<'w> {
|
|
|
|
|
|
|
|
// create mesh pipeline
|
|
// create mesh pipeline
|
|
|
let mesh_pipeline = pipeline::mesh(&device, &config, &[
|
|
let mesh_pipeline = pipeline::mesh(&device, &config, &[
|
|
|
- &texture_layout,
|
|
|
|
|
- &camera_layout
|
|
|
|
|
|
|
+ Some(&texture_layout),
|
|
|
|
|
+ Some(&camera_layout)
|
|
|
]);
|
|
]);
|
|
|
|
|
|
|
|
// TODO: move elsewhere
|
|
// TODO: move elsewhere
|
|
@@ -148,6 +152,7 @@ impl<'w> Renderer<'w> {
|
|
|
// window.set_cursor_visible(false);
|
|
// window.set_cursor_visible(false);
|
|
|
|
|
|
|
|
Self {
|
|
Self {
|
|
|
|
|
+ window,
|
|
|
surface,
|
|
surface,
|
|
|
device,
|
|
device,
|
|
|
queue,
|
|
queue,
|
|
@@ -168,6 +173,10 @@ impl<'w> Renderer<'w> {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ pub fn window_id(&self) -> WindowId {
|
|
|
|
|
+ self.window.id()
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
pub fn resize(&mut self, size: WindowSize) {
|
|
pub fn resize(&mut self, size: WindowSize) {
|
|
|
self.size = size;
|
|
self.size = size;
|
|
|
self.config.width = size.width;
|
|
self.config.width = size.width;
|
|
@@ -197,8 +206,23 @@ impl<'w> Renderer<'w> {
|
|
|
self.queue.write_buffer(&self.uniforms, 0, bytes);
|
|
self.queue.write_buffer(&self.uniforms, 0, bytes);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- pub fn render(&mut self) -> Result<(), SurfaceError> {
|
|
|
|
|
- let output = self.surface.get_current_texture()?;
|
|
|
|
|
|
|
+ pub fn render(&mut self) {
|
|
|
|
|
+ self.window.request_redraw();
|
|
|
|
|
+
|
|
|
|
|
+ let output = match self.surface.get_current_texture() {
|
|
|
|
|
+ CurrentSurfaceTexture::Success(output) => output,
|
|
|
|
|
+ CurrentSurfaceTexture::Suboptimal(_) | CurrentSurfaceTexture::Outdated => {
|
|
|
|
|
+ self.recreate();
|
|
|
|
|
+ return; // try again next frame
|
|
|
|
|
+ },
|
|
|
|
|
+ CurrentSurfaceTexture::Lost => {
|
|
|
|
|
+ panic!("Surface lost!");
|
|
|
|
|
+ },
|
|
|
|
|
+ _ => {
|
|
|
|
|
+ // probably temporary errors
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
let view = output.texture.create_view(&TextureViewDescriptor::default());
|
|
let view = output.texture.create_view(&TextureViewDescriptor::default());
|
|
|
let mut encoder = self.device.create_command_encoder(&CommandEncoderDescriptor {
|
|
let mut encoder = self.device.create_command_encoder(&CommandEncoderDescriptor {
|
|
|
label: Some("Render Encoder")
|
|
label: Some("Render Encoder")
|
|
@@ -208,9 +232,7 @@ impl<'w> Renderer<'w> {
|
|
|
|
|
|
|
|
// submit commands for rendering
|
|
// submit commands for rendering
|
|
|
self.queue.submit(iter::once(encoder.finish()));
|
|
self.queue.submit(iter::once(encoder.finish()));
|
|
|
- output.present();
|
|
|
|
|
-
|
|
|
|
|
- Ok(())
|
|
|
|
|
|
|
+ self.queue.present(output);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
pub fn center(&self) -> MousePos {
|
|
pub fn center(&self) -> MousePos {
|
|
@@ -224,6 +246,8 @@ impl<'w> Renderer<'w> {
|
|
|
view,
|
|
view,
|
|
|
// no multisampling
|
|
// no multisampling
|
|
|
resolve_target: None,
|
|
resolve_target: None,
|
|
|
|
|
+ // 2D
|
|
|
|
|
+ depth_slice: None,
|
|
|
ops: Operations {
|
|
ops: Operations {
|
|
|
// clear with this colour
|
|
// clear with this colour
|
|
|
load: LoadOp::Clear(Color {
|
|
load: LoadOp::Clear(Color {
|
|
@@ -245,8 +269,7 @@ impl<'w> Renderer<'w> {
|
|
|
}),
|
|
}),
|
|
|
stencil_ops: None
|
|
stencil_ops: None
|
|
|
}),
|
|
}),
|
|
|
- timestamp_writes: None,
|
|
|
|
|
- occlusion_query_set: None
|
|
|
|
|
|
|
+ ..Default::default()
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
pass.set_pipeline(&self.mesh_pipeline);
|
|
pass.set_pipeline(&self.mesh_pipeline);
|