Change Anyhow usage to ThisError
This commit is contained in:
		
							parent
							
								
									8f56358415
								
							
						
					
					
						commit
						215965ca9f
					
				|  | @ -343,6 +343,7 @@ version = "0.1.0" | ||||||
| dependencies = [ | dependencies = [ | ||||||
|  "bytemuck", |  "bytemuck", | ||||||
|  "log", |  "log", | ||||||
|  |  "thiserror", | ||||||
|  "wgpu", |  "wgpu", | ||||||
|  "winit", |  "winit", | ||||||
| ] | ] | ||||||
|  |  | ||||||
|  | @ -8,5 +8,6 @@ edition = "2021" | ||||||
| [dependencies] | [dependencies] | ||||||
| bytemuck = "1.15.0" | bytemuck = "1.15.0" | ||||||
| log = "0.4.21" | log = "0.4.21" | ||||||
|  | thiserror = "1.0.59" | ||||||
| wgpu     = "0.19.4" | wgpu     = "0.19.4" | ||||||
| winit    = "0.29.15" | winit    = "0.29.15" | ||||||
|  |  | ||||||
|  | @ -1,9 +1,15 @@ | ||||||
| use std::num::NonZeroU32; | use std::num::NonZeroU32; | ||||||
| 
 | 
 | ||||||
| use anyhow::{Context as _, Result}; | use thiserror::Error; | ||||||
| 
 | 
 | ||||||
| use super::Context; | use super::Context; | ||||||
| 
 | 
 | ||||||
|  | #[derive(Error, Debug)] | ||||||
|  | pub enum BindGroupError { | ||||||
|  |     #[error("Cannot build BindGroup without a layout")] | ||||||
|  |     NoLayout, | ||||||
|  | } | ||||||
|  | 
 | ||||||
| #[derive(Debug, Default)] | #[derive(Debug, Default)] | ||||||
| pub struct BindGroupLayoutBuilder<'a> { | pub struct BindGroupLayoutBuilder<'a> { | ||||||
|     next_binding: u32, |     next_binding: u32, | ||||||
|  | @ -125,12 +131,17 @@ impl<'a> BindGroupBuilder<'a> { | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     #[inline] |     #[inline] | ||||||
|     pub fn build(self, context: &Context) -> Result<wgpu::BindGroup> { |     pub fn build(self, context: &Context) -> Result<wgpu::BindGroup, BindGroupError> { | ||||||
|  |         let layout = match self.layout { | ||||||
|  |             Some(val) => val, | ||||||
|  |             None => return Err(BindGroupError::NoLayout), | ||||||
|  |         }; | ||||||
|  | 
 | ||||||
|         Ok(context |         Ok(context | ||||||
|             .device |             .device | ||||||
|             .create_bind_group(&wgpu::BindGroupDescriptor { |             .create_bind_group(&wgpu::BindGroupDescriptor { | ||||||
|                 label: self.label, |                 label: self.label, | ||||||
|                 layout: self.layout.context("BindGroupBuilder has no layout.")?, |                 layout, | ||||||
|                 entries: self.entries.as_slice(), |                 entries: self.entries.as_slice(), | ||||||
|             })) |             })) | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  | @ -1,10 +1,22 @@ | ||||||
| use std::sync::Arc; | use std::sync::Arc; | ||||||
| 
 | 
 | ||||||
| use anyhow::{Context as _, Result}; | use thiserror::Error; | ||||||
| use winit::{ | use winit::{ | ||||||
|     dpi::PhysicalSize, event::WindowEvent, event_loop::EventLoopWindowTarget, window::Window, |     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 struct Context<'window> { | ||||||
|     pub window: Arc<Window>, |     pub window: Arc<Window>, | ||||||
|     pub instance: wgpu::Instance, |     pub instance: wgpu::Instance, | ||||||
|  | @ -17,7 +29,7 @@ pub struct Context<'window> { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| impl<'window> Context<'window> { | impl<'window> Context<'window> { | ||||||
|     pub async fn new(window: Arc<Window>, limits: wgpu::Limits) -> Result<Self> { |     pub async fn new(window: Arc<Window>, limits: wgpu::Limits) -> Result<Self, ContextError> { | ||||||
|         log::info!("Initialising WGPU context..."); |         log::info!("Initialising WGPU context..."); | ||||||
|         let instance = wgpu::Instance::new(wgpu::InstanceDescriptor { |         let instance = wgpu::Instance::new(wgpu::InstanceDescriptor { | ||||||
|             backends: wgpu::Backends::VULKAN, |             backends: wgpu::Backends::VULKAN, | ||||||
|  | @ -33,14 +45,17 @@ impl<'window> Context<'window> { | ||||||
|         let surface = instance.create_surface(window.clone())?; |         let surface = instance.create_surface(window.clone())?; | ||||||
| 
 | 
 | ||||||
|         log::info!("Requesting GPU adapter..."); |         log::info!("Requesting GPU adapter..."); | ||||||
|         let adapter = instance |         let adapter = match instance | ||||||
|             .request_adapter(&wgpu::RequestAdapterOptions { |             .request_adapter(&wgpu::RequestAdapterOptions { | ||||||
|                 power_preference: wgpu::PowerPreference::HighPerformance, |                 power_preference: wgpu::PowerPreference::HighPerformance, | ||||||
|                 force_fallback_adapter: false, |                 force_fallback_adapter: false, | ||||||
|                 compatible_surface: Some(&surface), |                 compatible_surface: Some(&surface), | ||||||
|             }) |             }) | ||||||
|             .await |             .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!("Checking GPU adapter meets requirements"); | ||||||
|         log::info!("Requesting GPU device..."); |         log::info!("Requesting GPU device..."); | ||||||
|  | @ -57,9 +72,14 @@ impl<'window> Context<'window> { | ||||||
| 
 | 
 | ||||||
|         log::info!("Configuring window surface..."); |         log::info!("Configuring window surface..."); | ||||||
|         let size = window.inner_size(); |         let size = window.inner_size(); | ||||||
|         let surface_config = surface |         let surface_config = match surface.get_default_config(&adapter, size.width, size.height) { | ||||||
|             .get_default_config(&adapter, size.width, size.height) |             Some(val) => val, | ||||||
|             .context("Surface configuration unsupported by adapter")?; |             None => { | ||||||
|  |                 return Err(ContextError::SurfaceConfig( | ||||||
|  |                     "Surface configuration unsupported by adapter".to_string(), | ||||||
|  |                 )) | ||||||
|  |             } | ||||||
|  |         }; | ||||||
|         surface.configure(&device, &surface_config); |         surface.configure(&device, &surface_config); | ||||||
| 
 | 
 | ||||||
|         Ok(Self { |         Ok(Self { | ||||||
|  |  | ||||||
|  | @ -1,7 +1,15 @@ | ||||||
| use anyhow::Result; | use thiserror::Error; | ||||||
|  | 
 | ||||||
|  | use crate::bind_group::BindGroupError; | ||||||
| 
 | 
 | ||||||
| use super::{BindGroupBuilder, BindGroupLayoutBuilder, Context}; | 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
 | // TODO: Support mip-mapping and multi-sampling
 | ||||||
| #[derive(Debug, Clone)] | #[derive(Debug, Clone)] | ||||||
| pub struct TextureAttributes { | pub struct TextureAttributes { | ||||||
|  | @ -99,7 +107,7 @@ impl TextureBuilder { | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     #[inline] |     #[inline] | ||||||
|     pub fn build(self, context: &Context) -> Result<Texture> { |     pub fn build(self, context: &Context) -> Result<Texture, TextureError> { | ||||||
|         Texture::new(context, self.attributes) |         Texture::new(context, self.attributes) | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | @ -115,7 +123,7 @@ pub struct Texture { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| impl Texture { | impl Texture { | ||||||
|     pub fn new(context: &Context, attributes: TextureAttributes) -> Result<Self> { |     pub fn new(context: &Context, attributes: TextureAttributes) -> Result<Self, TextureError> { | ||||||
|         let texture = context.device.create_texture(&wgpu::TextureDescriptor { |         let texture = context.device.create_texture(&wgpu::TextureDescriptor { | ||||||
|             label: None, |             label: None, | ||||||
|             size: attributes.size, |             size: attributes.size, | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue