Change Anyhow usage to ThisError

This commit is contained in:
Jarrod Doyle 2024-04-24 19:55:34 +01:00
parent 8f56358415
commit 215965ca9f
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
5 changed files with 54 additions and 13 deletions

1
Cargo.lock generated
View File

@ -343,6 +343,7 @@ version = "0.1.0"
dependencies = [
"bytemuck",
"log",
"thiserror",
"wgpu",
"winit",
]

View File

@ -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"

View File

@ -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<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
.device
.create_bind_group(&wgpu::BindGroupDescriptor {
label: self.label,
layout: self.layout.context("BindGroupBuilder has no layout.")?,
layout,
entries: self.entries.as_slice(),
}))
}

View File

@ -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<Window>,
pub instance: wgpu::Instance,
@ -17,7 +29,7 @@ pub struct 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...");
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 {

View File

@ -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<Texture> {
pub fn build(self, context: &Context) -> Result<Texture, TextureError> {
Texture::new(context, self.attributes)
}
}
@ -115,7 +123,7 @@ pub struct 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 {
label: None,
size: attributes.size,