From 215965ca9f1045aecbffbd4b3b506f4f44de1fb7 Mon Sep 17 00:00:00 2001 From: Jarrod Doyle Date: Wed, 24 Apr 2024 19:55:34 +0100 Subject: [PATCH] Change Anyhow usage to ThisError --- Cargo.lock | 1 + Cargo.toml | 1 + src/bind_group.rs | 17 ++++++++++++++--- src/context.rs | 34 +++++++++++++++++++++++++++------- src/texture.rs | 14 +++++++++++--- 5 files changed, 54 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 59fe9f3..f53db3e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -343,6 +343,7 @@ version = "0.1.0" dependencies = [ "bytemuck", "log", + "thiserror", "wgpu", "winit", ] diff --git a/Cargo.toml b/Cargo.toml index e89ff3c..4780962 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,5 +8,6 @@ edition = "2021" [dependencies] bytemuck = "1.15.0" log = "0.4.21" +thiserror = "1.0.59" wgpu = "0.19.4" winit = "0.29.15" diff --git a/src/bind_group.rs b/src/bind_group.rs index 0b09524..3e27cdd 100644 --- a/src/bind_group.rs +++ b/src/bind_group.rs @@ -1,9 +1,15 @@ use std::num::NonZeroU32; -use anyhow::{Context as _, Result}; +use thiserror::Error; use super::Context; +#[derive(Error, Debug)] +pub enum BindGroupError { + #[error("Cannot build BindGroup without a layout")] + NoLayout, +} + #[derive(Debug, Default)] pub struct BindGroupLayoutBuilder<'a> { next_binding: u32, @@ -125,12 +131,17 @@ impl<'a> BindGroupBuilder<'a> { } #[inline] - pub fn build(self, context: &Context) -> Result { + pub fn build(self, context: &Context) -> Result { + let layout = match self.layout { + Some(val) => val, + None => return Err(BindGroupError::NoLayout), + }; + Ok(context .device .create_bind_group(&wgpu::BindGroupDescriptor { label: self.label, - layout: self.layout.context("BindGroupBuilder has no layout.")?, + layout, entries: self.entries.as_slice(), })) } diff --git a/src/context.rs b/src/context.rs index fa34ede..9ea609a 100644 --- a/src/context.rs +++ b/src/context.rs @@ -1,10 +1,22 @@ use std::sync::Arc; -use anyhow::{Context as _, Result}; +use thiserror::Error; use winit::{ dpi::PhysicalSize, event::WindowEvent, event_loop::EventLoopWindowTarget, window::Window, }; +#[derive(Error, Debug)] +pub enum ContextError { + #[error("Surface creation failed: {0}")] + Surface(#[from] wgpu::CreateSurfaceError), + #[error("Surface configuration failed: {0}")] + SurfaceConfig(String), + #[error("No compatible adapter found")] + Adapter, + #[error("Device request failed: {0}")] + Device(#[from] wgpu::RequestDeviceError), +} + pub struct Context<'window> { pub window: Arc, pub instance: wgpu::Instance, @@ -17,7 +29,7 @@ pub struct Context<'window> { } impl<'window> Context<'window> { - pub async fn new(window: Arc, limits: wgpu::Limits) -> Result { + pub async fn new(window: Arc, limits: wgpu::Limits) -> Result { log::info!("Initialising WGPU context..."); let instance = wgpu::Instance::new(wgpu::InstanceDescriptor { backends: wgpu::Backends::VULKAN, @@ -33,14 +45,17 @@ impl<'window> Context<'window> { let surface = instance.create_surface(window.clone())?; log::info!("Requesting GPU adapter..."); - let adapter = instance + let adapter = match instance .request_adapter(&wgpu::RequestAdapterOptions { power_preference: wgpu::PowerPreference::HighPerformance, force_fallback_adapter: false, compatible_surface: Some(&surface), }) .await - .context("Failed to find suitable GPU adapter")?; + { + Some(val) => val, + None => return Err(ContextError::Adapter), + }; log::info!("Checking GPU adapter meets requirements"); log::info!("Requesting GPU device..."); @@ -57,9 +72,14 @@ impl<'window> Context<'window> { log::info!("Configuring window surface..."); let size = window.inner_size(); - let surface_config = surface - .get_default_config(&adapter, size.width, size.height) - .context("Surface configuration unsupported by adapter")?; + let surface_config = match surface.get_default_config(&adapter, size.width, size.height) { + Some(val) => val, + None => { + return Err(ContextError::SurfaceConfig( + "Surface configuration unsupported by adapter".to_string(), + )) + } + }; surface.configure(&device, &surface_config); Ok(Self { diff --git a/src/texture.rs b/src/texture.rs index 182edd8..01005c9 100644 --- a/src/texture.rs +++ b/src/texture.rs @@ -1,7 +1,15 @@ -use anyhow::Result; +use thiserror::Error; + +use crate::bind_group::BindGroupError; use super::{BindGroupBuilder, BindGroupLayoutBuilder, Context}; +#[derive(Error, Debug)] +pub enum TextureError { + #[error("BindGroup failed to build: {0}")] + InvalidBindGroup(#[from] BindGroupError), +} + // TODO: Support mip-mapping and multi-sampling #[derive(Debug, Clone)] pub struct TextureAttributes { @@ -99,7 +107,7 @@ impl TextureBuilder { } #[inline] - pub fn build(self, context: &Context) -> Result { + pub fn build(self, context: &Context) -> Result { Texture::new(context, self.attributes) } } @@ -115,7 +123,7 @@ pub struct Texture { } impl Texture { - pub fn new(context: &Context, attributes: TextureAttributes) -> Result { + pub fn new(context: &Context, attributes: TextureAttributes) -> Result { let texture = context.device.create_texture(&wgpu::TextureDescriptor { label: None, size: attributes.size,