Skip to content

Commit

Permalink
Implement missing PrettyError impls (#3066)
Browse files Browse the repository at this point in the history
  • Loading branch information
scoopr committed Oct 5, 2022
1 parent 50e2a5b commit 60fd2a3
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -89,6 +89,7 @@ SurfaceConfiguration {
enabled. By @imberflur in [#3023](https://github.com/gfx-rs/wgpu/pull/3023)
- Fixed `CommandEncoder` not being `Send` and `Sync` on web by @i509VCB in [#3025](https://github.com/gfx-rs/wgpu/pull/3025)
- Document meaning of `vendor` in `AdapterInfo` if the vendor has no PCI id.
- Fix missing resource labels from some Errors by @scoopr in [#3066](https://github.com/gfx-rs/wgpu/pull/3066)

#### Metal
- Add the missing `msg_send![view, retain]` call within `from_view` by @jinleili in [#2976](https://github.com/gfx-rs/wgpu/pull/2976)
Expand Down
12 changes: 12 additions & 0 deletions wgpu-core/src/command/query.rs
Expand Up @@ -112,6 +112,18 @@ pub enum QueryError {
InvalidQuerySet(id::QuerySetId),
}

impl crate::error::PrettyError for QueryError {
fn fmt_pretty(&self, fmt: &mut crate::error::ErrorFormatter) {
fmt.error(self);
match *self {
Self::InvalidBuffer(id) => fmt.buffer_label(&id),
Self::InvalidQuerySet(id) => fmt.query_set_label(&id),

_ => {}
}
}
}

/// Error encountered while trying to use queries
#[derive(Clone, Debug, Error)]
pub enum QueryUseError {
Expand Down
4 changes: 4 additions & 0 deletions wgpu-core/src/device/mod.rs
Expand Up @@ -4759,6 +4759,10 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
.push(id::Valid(query_set_id));
}

pub fn query_set_label<A: HalApi>(&self, id: id::QuerySetId) -> String {
A::hub(self).query_sets.label_for_resource(id)
}

pub fn device_create_render_pipeline<A: HalApi>(
&self,
device_id: id::DeviceId,
Expand Down
15 changes: 15 additions & 0 deletions wgpu-core/src/error.rs
Expand Up @@ -91,6 +91,12 @@ impl<'a> ErrorFormatter<'a> {
let label = gfx_select!(id => global.command_buffer_label(*id));
self.label("command buffer", &label);
}

pub fn query_set_label(&mut self, id: &crate::id::QuerySetId) {
let global = self.global;
let label = gfx_select!(id => global.query_set_label(*id));
self.label("query set", &label);
}
}

pub trait PrettyError: Error + Sized {
Expand Down Expand Up @@ -142,6 +148,15 @@ pub fn format_pretty_any(
if let Some(pretty_err) = error.downcast_ref::<crate::command::TransferError>() {
return pretty_err.fmt_pretty(&mut fmt);
}
if let Some(pretty_err) = error.downcast_ref::<crate::command::PassErrorScope>() {
return pretty_err.fmt_pretty(&mut fmt);
}
if let Some(pretty_err) = error.downcast_ref::<crate::track::UsageConflict>() {
return pretty_err.fmt_pretty(&mut fmt);
}
if let Some(pretty_err) = error.downcast_ref::<crate::command::QueryError>() {
return pretty_err.fmt_pretty(&mut fmt);
}

// default
fmt.error(error)
Expand Down
29 changes: 25 additions & 4 deletions wgpu-core/src/track/mod.rs
Expand Up @@ -240,23 +240,24 @@ fn iterate_bitvec_indices(ownership: &BitVec<usize>) -> impl Iterator<Item = usi

#[derive(Clone, Debug, Error, Eq, PartialEq)]
pub enum UsageConflict {
#[error("Attempted to use buffer {id:?} which is invalid.")]
#[error("Attempted to use invalid buffer")]
BufferInvalid { id: id::BufferId },
#[error("Attempted to use texture {id:?} which is invalid.")]
#[error("Attempted to use invalid texture")]
TextureInvalid { id: id::TextureId },
#[error("Attempted to use buffer {id:?} with {invalid_use}.")]
#[error("Attempted to use buffer with {invalid_use}.")]
Buffer {
id: id::BufferId,
invalid_use: InvalidUse<hal::BufferUses>,
},
#[error("Attempted to use a texture {id:?} mips {mip_levels:?} layers {array_layers:?} with {invalid_use}.")]
#[error("Attempted to use a texture (mips {mip_levels:?} layers {array_layers:?}) with {invalid_use}.")]
Texture {
id: id::TextureId,
mip_levels: ops::Range<u32>,
array_layers: ops::Range<u32>,
invalid_use: InvalidUse<hal::TextureUses>,
},
}

impl UsageConflict {
fn from_buffer(
id: id::BufferId,
Expand Down Expand Up @@ -290,6 +291,26 @@ impl UsageConflict {
}
}

impl crate::error::PrettyError for UsageConflict {
fn fmt_pretty(&self, fmt: &mut crate::error::ErrorFormatter) {
fmt.error(self);
match *self {
Self::BufferInvalid { id } => {
fmt.buffer_label(&id);
}
Self::TextureInvalid { id } => {
fmt.texture_label(&id);
}
Self::Buffer { id, .. } => {
fmt.buffer_label(&id);
}
Self::Texture { id, .. } => {
fmt.texture_label(&id);
}
}
}
}

/// Pretty print helper that shows helpful descriptions of a conflicting usage.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct InvalidUse<T> {
Expand Down

0 comments on commit 60fd2a3

Please sign in to comment.