Skip to content

Commit

Permalink
Store a reference to the error sink in the queue id.
Browse files Browse the repository at this point in the history
This is allows us to make (some of) the queue methods forward errors instead of panicking.
  • Loading branch information
nical committed Nov 7, 2022
1 parent 2f41823 commit c162cb5
Showing 1 changed file with 47 additions and 16 deletions.
63 changes: 47 additions & 16 deletions wgpu/src/backend/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl Context {
hal_device: hal::OpenDevice<A>,
desc: &crate::DeviceDescriptor,
trace_dir: Option<&std::path::Path>,
) -> Result<(Device, wgc::id::QueueId), crate::RequestDeviceError> {
) -> Result<(Device, Queue), crate::RequestDeviceError> {
let global = &self.0;
let (device_id, error) = global.create_device_from_hal(
*adapter,
Expand All @@ -107,12 +107,17 @@ impl Context {
if let Some(err) = error {
self.handle_error_fatal(err, "Adapter::create_device_from_hal");
}
let error_sink = Arc::new(Mutex::new(ErrorSinkRaw::new()));
let device = Device {
id: device_id,
error_sink: Arc::new(Mutex::new(ErrorSinkRaw::new())),
error_sink: error_sink.clone(),
features: desc.features,
};
Ok((device, device_id))
let queue = Queue {
id: device_id,
error_sink,
};
Ok((device, queue))
}

#[cfg(any(not(target_arch = "wasm32"), feature = "emscripten"))]
Expand Down Expand Up @@ -808,6 +813,18 @@ pub struct Texture {
error_sink: ErrorSink,
}

#[derive(Debug)]
pub struct Queue {
id: wgc::id::QueueId,
error_sink: ErrorSink,
}

impl Queue {
pub(crate) fn backend(&self) -> wgt::Backend {
self.id.backend()
}
}

#[derive(Debug)]
pub(crate) struct CommandEncoder {
id: wgc::id::CommandEncoderId,
Expand Down Expand Up @@ -858,10 +875,17 @@ impl crate::GlobalId for CommandEncoder {
}
}

impl crate::GlobalId for Queue {
fn global_id(&self) -> Id {
use wgc::id::TypedId;
self.id.unzip()
}
}

impl crate::Context for Context {
type AdapterId = wgc::id::AdapterId;
type DeviceId = Device;
type QueueId = wgc::id::QueueId;
type QueueId = Queue;
type ShaderModuleId = wgc::id::ShaderModuleId;
type BindGroupLayoutId = wgc::id::BindGroupLayoutId;
type BindGroupId = wgc::id::BindGroupId;
Expand Down Expand Up @@ -951,12 +975,17 @@ impl crate::Context for Context {
log::error!("Error in Adapter::request_device: {}", err);
return ready(Err(crate::RequestDeviceError));
}
let error_sink = Arc::new(Mutex::new(ErrorSinkRaw::new()));
let device = Device {
id: device_id,
error_sink: Arc::new(Mutex::new(ErrorSinkRaw::new())),
error_sink: error_sink.clone(),
features: desc.features,
};
ready(Ok((device, device_id)))
let queue = Queue {
id: device_id,
error_sink,
};
ready(Ok((device, queue)))
}

fn adapter_is_surface_supported(
Expand Down Expand Up @@ -2270,7 +2299,7 @@ impl crate::Context for Context {
) {
let global = &self.0;
match wgc::gfx_select!(
*queue => global.queue_write_buffer(*queue, buffer.id, offset, data)
*queue => global.queue_write_buffer(queue.id, buffer.id, offset, data)
) {
Ok(()) => (),
Err(err) => self.handle_error_fatal(err, "Queue::write_buffer"),
Expand All @@ -2286,7 +2315,7 @@ impl crate::Context for Context {
) {
let global = &self.0;
match wgc::gfx_select!(
*queue => global.queue_validate_write_buffer(*queue, buffer.id, offset, size.get())
*queue => global.queue_validate_write_buffer(queue.id, buffer.id, offset, size.get())
) {
Ok(()) => (),
Err(err) => self.handle_error_fatal(err, "Queue::write_buffer_with"),
Expand All @@ -2300,7 +2329,7 @@ impl crate::Context for Context {
) -> QueueWriteBuffer {
let global = &self.0;
match wgc::gfx_select!(
*queue => global.queue_create_staging_buffer(*queue, size, ())
*queue => global.queue_create_staging_buffer(queue.id, size, ())
) {
Ok((buffer_id, ptr)) => QueueWriteBuffer {
buffer_id,
Expand All @@ -2322,10 +2351,12 @@ impl crate::Context for Context {
) {
let global = &self.0;
match wgc::gfx_select!(
*queue => global.queue_write_staging_buffer(*queue, buffer.id, offset, staging_buffer.buffer_id)
*queue => global.queue_write_staging_buffer(queue.id, buffer.id, offset, staging_buffer.buffer_id)
) {
Ok(()) => (),
Err(err) => self.handle_error_fatal(err, "Queue::write_buffer_with"),
Err(err) => {
self.handle_error_nolabel(&queue.error_sink, err, "Queue::write_buffer_with");
}
}
}

Expand All @@ -2339,14 +2370,14 @@ impl crate::Context for Context {
) {
let global = &self.0;
match wgc::gfx_select!(*queue => global.queue_write_texture(
*queue,
queue.id,
&map_texture_copy_view(texture),
data,
&data_layout,
&size
)) {
Ok(()) => (),
Err(err) => self.handle_error_fatal(err, "Queue::write_texture"),
Err(err) => self.handle_error_nolabel(&queue.error_sink, err, "Queue::write_texture"),
}
}

Expand All @@ -2358,7 +2389,7 @@ impl crate::Context for Context {
let temp_command_buffers = command_buffers.collect::<SmallVec<[_; 4]>>();

let global = &self.0;
match wgc::gfx_select!(*queue => global.queue_submit(*queue, &temp_command_buffers)) {
match wgc::gfx_select!(*queue => global.queue_submit(queue.id, &temp_command_buffers)) {
Ok(index) => index,
Err(err) => self.handle_error_fatal(err, "Queue::submit"),
}
Expand All @@ -2367,7 +2398,7 @@ impl crate::Context for Context {
fn queue_get_timestamp_period(&self, queue: &Self::QueueId) -> f32 {
let global = &self.0;
let res = wgc::gfx_select!(queue => global.queue_get_timestamp_period(
*queue
queue.id
));
match res {
Ok(v) => v,
Expand All @@ -2385,7 +2416,7 @@ impl crate::Context for Context {
let closure = wgc::device::queue::SubmittedWorkDoneClosure::from_rust(callback);

let global = &self.0;
let res = wgc::gfx_select!(queue => global.queue_on_submitted_work_done(*queue, closure));
let res = wgc::gfx_select!(queue => global.queue_on_submitted_work_done(queue.id, closure));
if let Err(cause) = res {
self.handle_error_fatal(cause, "Queue::on_submitted_work_done");
}
Expand Down

0 comments on commit c162cb5

Please sign in to comment.