refactor renderer into smaller functions
This commit is contained in:
parent
f52d9bbb68
commit
42f9148f9f
3 changed files with 341 additions and 119 deletions
|
@ -1,20 +1,133 @@
|
|||
use std::borrow::Cow;
|
||||
use std::sync::Arc;
|
||||
use std::time::Instant;
|
||||
use std::{backtrace::Backtrace, borrow::Cow};
|
||||
|
||||
use cgmath::{Matrix4, Point3, Rad, Vector3, perspective};
|
||||
use futures::executor::block_on;
|
||||
use thiserror::Error;
|
||||
use tracing::{error, trace};
|
||||
use wgpu::TextureUsages;
|
||||
use wgpu::{Backends, InstanceDescriptor, util::DeviceExt};
|
||||
use winit::window::Window;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum ContextError {
|
||||
#[error("Failed to create WGPU surface: {0}")]
|
||||
SurfaceCreationFailure(#[from] wgpu::CreateSurfaceError),
|
||||
#[error(transparent)]
|
||||
pub enum ContextErrorKind {
|
||||
#[error("Surface creation failed")]
|
||||
SurfaceCreation,
|
||||
#[error("Surface configuration failed")]
|
||||
SurfaceConfiguration,
|
||||
#[error("Adapter request failed")]
|
||||
AdapterRequest,
|
||||
#[error("Device request failed")]
|
||||
DeviceRequest,
|
||||
#[error("Surface texture acquisition failed")]
|
||||
SurfaceTexture,
|
||||
}
|
||||
|
||||
const CUBE_SHADER: &str = r#"
|
||||
#[derive(Debug, Error)]
|
||||
pub struct RenderContextError {
|
||||
kind: ContextErrorKind,
|
||||
label: Option<Cow<'static, str>>,
|
||||
#[source]
|
||||
source: Option<Box<dyn std::error::Error + Send + Sync>>,
|
||||
}
|
||||
|
||||
impl RenderContextError {
|
||||
pub fn new(
|
||||
kind: ContextErrorKind,
|
||||
label: impl Into<Option<Cow<'static, str>>>,
|
||||
source: impl Into<Option<Box<dyn std::error::Error + Send + Sync>>>,
|
||||
) -> Self {
|
||||
Self {
|
||||
kind,
|
||||
label: label.into(),
|
||||
source: source.into(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_label(mut self, label: impl Into<Cow<'static, str>>) -> Self {
|
||||
self.label = Some(label.into());
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for RenderContextError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
if let Some(label) = &self.label {
|
||||
writeln!(f, "[{}] {}", label, self.kind)?;
|
||||
} else {
|
||||
writeln!(f, "{}", self.kind)?;
|
||||
}
|
||||
|
||||
if let Some(source) = &self.source {
|
||||
fn fmt_chain(
|
||||
err: &(dyn std::error::Error + 'static),
|
||||
indent: usize,
|
||||
f: &mut std::fmt::Formatter<'_>,
|
||||
) -> std::fmt::Result {
|
||||
let indent_str = " ".repeat(indent);
|
||||
writeln!(f, "{}{}", indent_str, err)?;
|
||||
if let Some(next) = err.source() {
|
||||
writeln!(f, "{}Caused by:", indent_str)?;
|
||||
fmt_chain(next, indent + 1, f)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
writeln!(f, "Caused by:")?;
|
||||
fmt_chain(source.as_ref(), 1, f)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
trait IntoRenderContextError<T> {
|
||||
fn ctx_err(
|
||||
self,
|
||||
kind: ContextErrorKind,
|
||||
label: impl Into<Cow<'static, str>>,
|
||||
) -> Result<T, RenderContextError>;
|
||||
}
|
||||
|
||||
impl<T, E> IntoRenderContextError<T> for Result<T, E>
|
||||
where
|
||||
E: std::error::Error + Send + Sync + 'static,
|
||||
{
|
||||
fn ctx_err(
|
||||
self,
|
||||
kind: ContextErrorKind,
|
||||
label: impl Into<Cow<'static, str>>,
|
||||
) -> Result<T, RenderContextError> {
|
||||
self.map_err(|e| {
|
||||
RenderContextError::new(
|
||||
kind,
|
||||
Some(label.into()),
|
||||
Some(Box::new(e) as Box<dyn std::error::Error + Send + Sync>),
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl From<wgpu::CreateSurfaceError> for RenderContextError {
|
||||
fn from(err: wgpu::CreateSurfaceError) -> Self {
|
||||
RenderContextError::new(
|
||||
ContextErrorKind::SurfaceCreation,
|
||||
Some("Surface creation".into()),
|
||||
Some(Box::new(err) as Box<dyn std::error::Error + Send + Sync>),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<wgpu::RequestDeviceError> for RenderContextError {
|
||||
fn from(err: wgpu::RequestDeviceError) -> Self {
|
||||
RenderContextError::new(
|
||||
ContextErrorKind::DeviceRequest,
|
||||
Some("Device setup".into()),
|
||||
Some(Box::new(err) as Box<dyn std::error::Error + Send + Sync>),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
const CUBE_SHADER: &str = r"
|
||||
struct Uniforms {
|
||||
mvp: mat4x4<f32>,
|
||||
};
|
||||
|
@ -40,14 +153,17 @@ fn vs_main(input: VertexInput) -> VertexOutput {
|
|||
return output;
|
||||
}
|
||||
|
||||
|
||||
@fragment
|
||||
fn fs_main(input: VertexOutput) -> @location(0) vec4<f32> {
|
||||
let ambient: f32 = 0.2;
|
||||
let light_dir = normalize(vec3<f32>(0.5, 1.0, 0.5));
|
||||
let brightness = clamp(dot(normalize(input.normal), light_dir), 0.0, 1.0);
|
||||
let diffuse = clamp(dot(normalize(input.normal), light_dir), 0.0, 1.0);
|
||||
// Mix ambient light to ensure no face is completely dark.
|
||||
let brightness = ambient + (1.0 - ambient) * diffuse;
|
||||
return vec4<f32>(0.7 * brightness, 0.7 * brightness, 0.9 * brightness, 1.0);
|
||||
}
|
||||
"#;
|
||||
|
||||
";
|
||||
#[repr(C)]
|
||||
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
|
||||
struct Vertex {
|
||||
|
@ -236,39 +352,52 @@ pub struct WgpuCtx<'window> {
|
|||
vertex_buffer: wgpu::Buffer,
|
||||
start_time: Instant,
|
||||
bg_color: wgpu::Color,
|
||||
last_frame_instant: Instant,
|
||||
frame_count: u32,
|
||||
}
|
||||
|
||||
impl<'window> WgpuCtx<'window> {
|
||||
pub async fn new(window: Arc<Window>) -> Result<WgpuCtx<'window>, ContextError> {
|
||||
pub async fn new(window: Arc<Window>) -> Result<WgpuCtx<'window>, RenderContextError> {
|
||||
let instance = wgpu::Instance::new(&InstanceDescriptor {
|
||||
backends: Backends::from_comma_list("dx12,metal,opengl,webgpu"),
|
||||
..Default::default()
|
||||
});
|
||||
let surface = instance.create_surface(Arc::clone(&window))?;
|
||||
|
||||
let surface = instance
|
||||
.create_surface(Arc::clone(&window))
|
||||
.ctx_err(ContextErrorKind::SurfaceCreation, "Surface initialization")?;
|
||||
|
||||
let adapter = instance
|
||||
.request_adapter(&wgpu::RequestAdapterOptions {
|
||||
power_preference: wgpu::PowerPreference::default(),
|
||||
force_fallback_adapter: false,
|
||||
compatible_surface: Some(&surface),
|
||||
..Default::default()
|
||||
})
|
||||
.await
|
||||
.expect("Failed to obtain render adapter");
|
||||
.ok_or_else(|| {
|
||||
RenderContextError::new(
|
||||
ContextErrorKind::AdapterRequest,
|
||||
Some("Adapter selection".into()),
|
||||
None,
|
||||
)
|
||||
})?;
|
||||
let (device, queue) = adapter
|
||||
.request_device(
|
||||
&wgpu::DeviceDescriptor {
|
||||
label: None,
|
||||
required_features: wgpu::Features::empty(),
|
||||
required_limits: wgpu::Limits::default().using_resolution(adapter.limits()),
|
||||
memory_hints: wgpu::MemoryHints::Performance,
|
||||
},
|
||||
None,
|
||||
)
|
||||
.request_device(&wgpu::DeviceDescriptor::default(), None)
|
||||
.await
|
||||
.expect("Failed to create rendering device");
|
||||
.ctx_err(ContextErrorKind::DeviceRequest, "Device configuration")?;
|
||||
let size = window.inner_size();
|
||||
let width = size.width.max(1);
|
||||
let height = size.height.max(1);
|
||||
let surface_config = surface.get_default_config(&adapter, width, height).unwrap();
|
||||
let surface_config = wgpu::SurfaceConfiguration {
|
||||
width: width.max(1),
|
||||
height: height.max(1),
|
||||
format: wgpu::TextureFormat::Rgba8UnormSrgb,
|
||||
present_mode: wgpu::PresentMode::AutoNoVsync,
|
||||
alpha_mode: wgpu::CompositeAlphaMode::Auto,
|
||||
view_formats: Vec::new(),
|
||||
usage: TextureUsages::RENDER_ATTACHMENT,
|
||||
desired_maximum_frame_latency: 3,
|
||||
};
|
||||
surface.configure(&device, &surface_config);
|
||||
let uniform_buffer = device.create_buffer(&wgpu::BufferDescriptor {
|
||||
label: Some("Uniform Buffer"),
|
||||
|
@ -356,10 +485,12 @@ impl<'window> WgpuCtx<'window> {
|
|||
a: 1.0,
|
||||
},
|
||||
start_time: Instant::now(),
|
||||
last_frame_instant: Instant::now(),
|
||||
frame_count: 0,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn new_blocking(window: Arc<Window>) -> Result<WgpuCtx<'window>, ContextError> {
|
||||
pub fn new_blocking(window: Arc<Window>) -> Result<WgpuCtx<'window>, RenderContextError> {
|
||||
block_on(Self::new(window))
|
||||
}
|
||||
|
||||
|
@ -431,6 +562,15 @@ impl<'window> WgpuCtx<'window> {
|
|||
}
|
||||
self.queue.submit(Some(encoder.finish()));
|
||||
surface_texture.present();
|
||||
|
||||
self.frame_count += 1;
|
||||
let elapsed_secs = self.last_frame_instant.elapsed().as_secs_f32();
|
||||
if elapsed_secs >= 1.0 {
|
||||
let fps = self.frame_count as f32 / elapsed_secs;
|
||||
trace!("FPS: {:.2}", fps);
|
||||
self.frame_count = 0;
|
||||
self.last_frame_instant = Instant::now();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn change_bg_color(&mut self, color: wgpu::Color) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue