diff --git a/wgpu-core/src/device/global.rs b/wgpu-core/src/device/global.rs
index 637afa9e3..d7a327eef 100644
--- a/wgpu-core/src/device/global.rs
+++ b/wgpu-core/src/device/global.rs
@@ -159,11 +159,6 @@ impl Global {
}
};
- if desc.usage.is_empty() {
- // Per spec, `usage` must not be zero.
- break 'error CreateBufferError::InvalidUsage(desc.usage);
- }
-
#[cfg(feature = "trace")]
if let Some(ref mut trace) = *device.trace.lock() {
let mut desc = desc.clone();
@@ -174,6 +169,11 @@ impl Global {
trace.add(trace::Action::CreateBuffer(fid.id(), desc));
}
+ if desc.usage.is_empty() {
+ // Per spec, `usage` must not be zero.
+ break 'error CreateBufferError::InvalidUsage(desc.usage);
+ }
+
let buffer = match device.create_buffer(desc, false) {
Ok(buffer) => buffer,
Err(e) => {
@@ -381,15 +381,11 @@ impl Global {
.devices
.get(device_id)
.map_err(|_| DeviceError::InvalidDeviceId)?;
- let snatch_guard = device.snatchable_lock.read();
- device.check_is_valid()?;
let buffer = hub
.buffers
.get(buffer_id)
.map_err(|_| BufferAccessError::InvalidBufferId(buffer_id))?;
- buffer.check_usage(wgt::BufferUsages::MAP_WRITE)?;
- //assert!(buffer isn't used by the GPU);
#[cfg(feature = "trace")]
if let Some(ref mut trace) = *device.trace.lock() {
@@ -402,6 +398,11 @@ impl Global {
});
}
+ device.check_is_valid()?;
+ buffer.check_usage(wgt::BufferUsages::MAP_WRITE)?;
+ //assert!(buffer isn't used by the GPU);
+
+ let snatch_guard = device.snatchable_lock.read();
let raw_buf = buffer.try_raw(&snatch_guard)?;
unsafe {
let mapping = device
@@ -565,6 +566,7 @@ impl Global {
Ok(device) => device,
Err(_) => break 'error DeviceError::InvalidDeviceId.into(),
};
+
#[cfg(feature = "trace")]
if let Some(ref mut trace) = *device.trace.lock() {
trace.add(trace::Action::CreateTexture(fid.id(), desc.clone()));
@@ -808,12 +810,7 @@ impl Global {
}
};
let device = &texture.device;
- {
- let snatch_guard = device.snatchable_lock.read();
- if let Err(e) = texture.check_destroyed(&snatch_guard) {
- break 'error e.into();
- }
- }
+
#[cfg(feature = "trace")]
if let Some(ref mut trace) = *device.trace.lock() {
trace.add(trace::Action::CreateTextureView {
@@ -823,6 +820,13 @@ impl Global {
});
}
+ {
+ let snatch_guard = device.snatchable_lock.read();
+ if let Err(e) = texture.check_destroyed(&snatch_guard) {
+ break 'error e.into();
+ }
+ }
+
let view = match unsafe { device.create_texture_view(&texture, desc) } {
Ok(view) => view,
Err(e) => break 'error e,
@@ -1519,6 +1523,7 @@ impl Global {
Ok(device) => device,
Err(_) => break 'error DeviceError::InvalidDeviceId.into(),
};
+
#[cfg(feature = "trace")]
if let Some(ref mut trace) = *device.trace.lock() {
trace.add(trace::Action::CreateQuerySet {
@@ -1592,6 +1597,7 @@ impl Global {
Ok(device) => device,
Err(_) => break 'error DeviceError::InvalidDeviceId.into(),
};
+
#[cfg(feature = "trace")]
if let Some(ref mut trace) = *device.trace.lock() {
trace.add(trace::Action::CreateRenderPipeline {
@@ -1740,6 +1746,7 @@ impl Global {
implicit_context: implicit_context.clone(),
});
}
+
let pipeline = match device.create_compute_pipeline(desc, implicit_context, hub) {
Ok(pair) => pair,
Err(e) => break 'error e,
@@ -1866,6 +1873,7 @@ impl Global {
// TODO: Handle error properly
Err(crate::storage::InvalidId) => break 'error DeviceError::InvalidDeviceId.into(),
};
+
#[cfg(feature = "trace")]
if let Some(ref mut trace) = *device.trace.lock() {
trace.add(trace::Action::CreatePipelineCache {
@@ -1873,6 +1881,7 @@ impl Global {
desc: desc.clone(),
});
}
+
let cache = unsafe { device.create_pipeline_cache(desc) };
match cache {
Ok(cache) => {
@@ -2037,15 +2046,16 @@ impl Global {
Ok(device) => device,
Err(_) => break 'error DeviceError::InvalidDeviceId.into(),
};
- if let Err(e) = device.check_is_valid() {
- break 'error e.into();
- }
#[cfg(feature = "trace")]
if let Some(ref mut trace) = *device.trace.lock() {
trace.add(trace::Action::ConfigureSurface(surface_id, config.clone()));
}
+ if let Err(e) = device.check_is_valid() {
+ break 'error e.into();
+ }
+
let surface = match surface_guard.get(surface_id) {
Ok(surface) => surface,
Err(_) => break 'error E::InvalidSurface,
diff --git a/wgpu-core/src/device/queue.rs b/wgpu-core/src/device/queue.rs
index 5a890c2e0..a6fee7537 100644
--- a/wgpu-core/src/device/queue.rs
+++ b/wgpu-core/src/device/queue.rs
@@ -436,8 +436,6 @@ impl Global {
let device = queue.device.as_ref().unwrap();
- buffer.same_device_as(queue.as_ref())?;
-
let data_size = data.len() as wgt::BufferAddress;
#[cfg(feature = "trace")]
@@ -451,6 +449,8 @@ impl Global {
});
}
+ buffer.same_device_as(queue.as_ref())?;
+
if data_size == 0 {
log::trace!("Ignoring write_buffer of size 0");
return Ok(());
@@ -1197,8 +1197,6 @@ impl Global {
Err(_) => continue,
};
- cmdbuf.same_device_as(queue.as_ref())?;
-
#[cfg(feature = "trace")]
if let Some(ref mut trace) = *device.trace.lock() {
trace.add(Action::Submit(
@@ -1213,6 +1211,9 @@ impl Global {
.unwrap(),
));
}
+
+ cmdbuf.same_device_as(queue.as_ref())?;
+
if !cmdbuf.is_finished() {
let cmdbuf = Arc::into_inner(cmdbuf).expect(
"Command buffer cannot be destroyed because is still in use",
diff --git a/wgpu-core/src/present.rs b/wgpu-core/src/present.rs
index 95840b133..bd465e910 100644
--- a/wgpu-core/src/present.rs
+++ b/wgpu-core/src/present.rs
@@ -301,14 +301,15 @@ impl Global {
};
let device = present.device.downcast_ref::().unwrap();
- device.check_is_valid()?;
- let queue = device.get_queue().unwrap();
#[cfg(feature = "trace")]
if let Some(ref mut trace) = *device.trace.lock() {
trace.add(Action::Present(surface_id));
}
+ device.check_is_valid()?;
+ let queue = device.get_queue().unwrap();
+
let result = {
let texture_id = present
.acquired_texture
@@ -393,13 +394,14 @@ impl Global {
};
let device = present.device.downcast_ref::().unwrap();
- device.check_is_valid()?;
#[cfg(feature = "trace")]
if let Some(ref mut trace) = *device.trace.lock() {
trace.add(Action::DiscardSurfaceTexture(surface_id));
}
+ device.check_is_valid()?;
+
{
let texture_id = present
.acquired_texture