style(core): keep device::resource module's content < 100 columns (#9193)

This commit is contained in:
Erich Gubler authored and GitHub committed 2026-03-11 13:36:49 -07:00
1 parent e5b0edea79
commit c1f4d0c5db
1 file changed
+86 -82
+86 -82
View File
@@ -26,7 +26,8 @@ use crate::device::trace;
use crate::{
api_log,
binding_model::{
self, BindGroup, BindGroupLateBufferBindingInfo, BindGroupLayout, BindGroupLayoutEntryError,
self, BindGroup, BindGroupLateBufferBindingInfo, BindGroupLayout,
BindGroupLayoutEntryError, CreateBindGroupError, CreateBindGroupLayoutError,
},
command, conv,
device::{
@@ -41,7 +42,7 @@ use crate::{
},
instance::{Adapter, RequestDeviceError},
lock::{rank, Mutex, RwLock},
pipeline,
pipeline::{self, ColorStateError},
pool::ResourcePool,
present,
resource::{
@@ -297,7 +298,8 @@ impl Drop for Device {
fn drop(&mut self) {
resource_log!("Drop {}", self.error_ident());
// SAFETY: We are in the Drop impl and we don't use self.zero_buffer anymore after this point.
// SAFETY: We are in the Drop impl and we don't use self.zero_buffer anymore after this
// point.
let zero_buffer = unsafe { ManuallyDrop::take(&mut self.zero_buffer) };
// SAFETY: We are in the Drop impl and we don't use self.empty_bgl anymore after this point.
let empty_bgl = unsafe { ManuallyDrop::take(&mut self.empty_bgl) };
@@ -880,10 +882,11 @@ impl Device {
}
};
// Maintain all finished submissions on the queue, updating the relevant user closures and collecting if the queue is empty.
// Maintain all finished submissions on the queue, updating the relevant user closures and
// collecting if the queue is empty.
//
// We don't use the result of the wait here, as we want to progress forward as far as possible
// and the wait could have been for submissions that finished long ago.
// We don't use the result of the wait here, as we want to progress forward as far as
// possible and the wait could have been for submissions that finished long ago.
let mut queue_empty = false;
if let Some(queue) = self.get_queue() {
let queue_result = queue.maintain(current_finished_submission, &snatch_guard);
@@ -896,10 +899,13 @@ impl Device {
// DEADLOCK PREVENTION: We must drop `snatch_guard` before `queue` goes out of scope.
//
// `Queue::drop` acquires the snatch guard. If we still hold it when `queue` is dropped
// at the end of this block, we would deadlock. This can happen in the following scenario:
// at the end of this block, we would deadlock. This can happen in the following
// scenario:
//
// - Thread A calls `Device::maintain` while Thread B holds the last strong ref to the queue.
// - Thread A calls `self.get_queue()`, obtaining a new strong ref, and enters this branch.
// - Thread A calls `Device::maintain` while Thread B holds the last strong ref to the
// queue.
// - Thread A calls `self.get_queue()`, obtaining a new strong ref, and enters this
// branch.
// - Thread B drops its strong ref, making Thread A's ref the last one.
// - When `queue` goes out of scope here, `Queue::drop` runs and tries to acquire the
// snatch guard — but Thread A (this thread) still holds it, causing a deadlock.
@@ -908,14 +914,20 @@ impl Device {
drop(snatch_guard);
};
// Based on the queue empty status, and the current finished submission index, determine the result of the poll.
// Based on the queue empty status, and the current finished submission index, determine
// the result of the poll.
let result = if queue_empty {
if let Some(wait_submission_index) = wait_submission_index {
// Assert to ensure that if we received a queue empty status, the fence shows the correct value.
// This is defensive, as this should never be hit.
// Assert to ensure that if we received a queue empty status, the fence shows the
// correct value. This is defensive, as this should never be hit.
assert!(
current_finished_submission >= wait_submission_index,
"If the queue is empty, the current submission index ({current_finished_submission}) should be at least the wait submission index ({wait_submission_index})"
concat!(
"If the queue is empty, the current submission index ",
"({}) should be at least the wait submission index ({})",
),
current_finished_submission,
wait_submission_index,
);
}
@@ -2517,7 +2529,7 @@ impl Device {
pub fn create_bind_group_layout(
self: &Arc<Self>,
desc: &binding_model::BindGroupLayoutDescriptor,
) -> Result<Arc<BindGroupLayout>, binding_model::CreateBindGroupLayoutError> {
) -> Result<Arc<BindGroupLayout>, CreateBindGroupLayoutError> {
self.check_is_valid()?;
let entry_map = bgl::EntryMap::from_entries(&desc.entries)?;
@@ -2542,7 +2554,7 @@ impl Device {
label: &crate::Label,
entry_map: bgl::EntryMap,
origin: bgl::Origin,
) -> Result<Arc<BindGroupLayout>, binding_model::CreateBindGroupLayoutError> {
) -> Result<Arc<BindGroupLayout>, CreateBindGroupLayoutError> {
#[derive(PartialEq)]
enum WritableStorage {
Yes,
@@ -2551,12 +2563,10 @@ impl Device {
for entry in entry_map.values() {
if entry.binding >= self.limits.max_bindings_per_bind_group {
return Err(
binding_model::CreateBindGroupLayoutError::InvalidBindingIndex {
binding: entry.binding,
maximum: self.limits.max_bindings_per_bind_group,
},
);
return Err(CreateBindGroupLayoutError::InvalidBindingIndex {
binding: entry.binding,
maximum: self.limits.max_bindings_per_bind_group,
});
}
use wgt::BindingType as Bt;
@@ -2602,7 +2612,7 @@ impl Device {
sample_type: TextureSampleType::Float { filterable: true },
..
} => {
return Err(binding_model::CreateBindGroupLayoutError::Entry {
return Err(CreateBindGroupLayoutError::Entry {
binding: entry.binding,
error:
BindGroupLayoutEntryError::SampleTypeFloatFilterableBindingMultisampled,
@@ -2614,7 +2624,7 @@ impl Device {
..
} => {
if multisampled && view_dimension != TextureViewDimension::D2 {
return Err(binding_model::CreateBindGroupLayoutError::Entry {
return Err(CreateBindGroupLayoutError::Entry {
binding: entry.binding,
error: BindGroupLayoutEntryError::Non2DMultisampled(view_dimension),
});
@@ -2634,7 +2644,7 @@ impl Device {
match view_dimension {
TextureViewDimension::Cube | TextureViewDimension::CubeArray => {
return Err(binding_model::CreateBindGroupLayoutError::Entry {
return Err(CreateBindGroupLayoutError::Entry {
binding: entry.binding,
error: BindGroupLayoutEntryError::StorageTextureCube,
})
@@ -2645,7 +2655,7 @@ impl Device {
wgt::StorageTextureAccess::Atomic
if !self.features.contains(wgt::Features::TEXTURE_ATOMIC) =>
{
return Err(binding_model::CreateBindGroupLayoutError::Entry {
return Err(CreateBindGroupLayoutError::Entry {
binding: entry.binding,
error: BindGroupLayoutEntryError::StorageTextureAtomic,
});
@@ -2655,7 +2665,7 @@ impl Device {
let format_features =
self.describe_format_features(format).map_err(|error| {
binding_model::CreateBindGroupLayoutError::Entry {
CreateBindGroupLayoutError::Entry {
binding: entry.binding,
error: BindGroupLayoutEntryError::MissingFeatures(error),
}
@@ -2669,11 +2679,13 @@ impl Device {
};
if !format_features.flags.contains(required_feature_flag) {
return Err(binding_model::CreateBindGroupLayoutError::UnsupportedStorageTextureAccess {
binding: entry.binding,
access,
format,
});
return Err(
CreateBindGroupLayoutError::UnsupportedStorageTextureAccess {
binding: entry.binding,
access,
format,
},
);
}
(
@@ -2694,13 +2706,13 @@ impl Device {
}
Bt::AccelerationStructure { vertex_return } => {
self.require_features(wgt::Features::EXPERIMENTAL_RAY_QUERY)
.map_err(|e| binding_model::CreateBindGroupLayoutError::Entry {
.map_err(|e| CreateBindGroupLayoutError::Entry {
binding: entry.binding,
error: e.into(),
})?;
if vertex_return {
self.require_features(wgt::Features::EXPERIMENTAL_RAY_HIT_VERTEX_RETURN)
.map_err(|e| binding_model::CreateBindGroupLayoutError::Entry {
.map_err(|e| CreateBindGroupLayoutError::Entry {
binding: entry.binding,
error: e.into(),
})?;
@@ -2712,7 +2724,7 @@ impl Device {
}
Bt::ExternalTexture => {
self.require_features(wgt::Features::EXTERNAL_TEXTURE)
.map_err(|e| binding_model::CreateBindGroupLayoutError::Entry {
.map_err(|e| CreateBindGroupLayoutError::Entry {
binding: entry.binding,
error: e.into(),
})?;
@@ -2724,16 +2736,16 @@ impl Device {
if entry.count.is_some() {
required_features |= array_feature
.ok_or(BindGroupLayoutEntryError::ArrayUnsupported)
.map_err(|error| binding_model::CreateBindGroupLayoutError::Entry {
.map_err(|error| CreateBindGroupLayoutError::Entry {
binding: entry.binding,
error,
})?;
}
if entry.visibility.contains_unknown_bits() {
return Err(
binding_model::CreateBindGroupLayoutError::InvalidVisibility(entry.visibility),
);
return Err(CreateBindGroupLayoutError::InvalidVisibility(
entry.visibility,
));
}
if entry.visibility.contains(wgt::ShaderStages::VERTEX) {
@@ -2756,13 +2768,13 @@ impl Device {
self.require_features(required_features)
.map_err(BindGroupLayoutEntryError::MissingFeatures)
.map_err(|error| binding_model::CreateBindGroupLayoutError::Entry {
.map_err(|error| CreateBindGroupLayoutError::Entry {
binding: entry.binding,
error,
})?;
self.require_downlevel_flags(required_downlevel_flags)
.map_err(BindGroupLayoutEntryError::MissingDownlevelFlags)
.map_err(|error| binding_model::CreateBindGroupLayoutError::Entry {
.map_err(|error| CreateBindGroupLayoutError::Entry {
binding: entry.binding,
error,
})?;
@@ -2785,7 +2797,7 @@ impl Device {
// definitely going to violate limits too, lets catch it now.
count_validator
.validate(&self.limits)
.map_err(binding_model::CreateBindGroupLayoutError::TooManyBindings)?;
.map_err(CreateBindGroupLayoutError::TooManyBindings)?;
// Validate that binding arrays don't conflict with dynamic offsets.
count_validator.validate_binding_arrays()?;
@@ -2818,8 +2830,7 @@ impl Device {
late_buffer_binding_sizes: &mut FastHashMap<u32, wgt::BufferSize>,
used: &mut BindGroupStates,
snatch_guard: &'a SnatchGuard<'a>,
) -> Result<hal::BufferBinding<'a, dyn hal::DynBuffer>, binding_model::CreateBindGroupError>
{
) -> Result<hal::BufferBinding<'a, dyn hal::DynBuffer>, CreateBindGroupError> {
use crate::binding_model::CreateBindGroupError as Error;
let (binding_ty, dynamic, min_size) = match decl.ty {
@@ -2878,11 +2889,7 @@ impl Device {
// Requested size not specified
None => None,
// Requested zero size
Some(None) => {
return Err(binding_model::CreateBindGroupError::BindingZeroSize(
buffer.error_ident(),
))
}
Some(None) => return Err(CreateBindGroupError::BindingZeroSize(buffer.error_ident())),
};
let (bb, bind_size) = buffer.binding(bb.offset, req_size, snatch_guard)?;
@@ -2958,7 +2965,7 @@ impl Device {
binding: u32,
decl: &wgt::BindGroupLayoutEntry,
sampler: &'a Arc<Sampler>,
) -> Result<&'a dyn hal::DynSampler, binding_model::CreateBindGroupError> {
) -> Result<&'a dyn hal::DynSampler, CreateBindGroupError> {
use crate::binding_model::CreateBindGroupError as Error;
used.samplers.insert_single(sampler.clone());
@@ -3009,8 +3016,7 @@ impl Device {
used: &mut BindGroupStates,
used_texture_ranges: &mut Vec<TextureInitTrackerAction>,
snatch_guard: &'a SnatchGuard<'a>,
) -> Result<hal::TextureBinding<'a, dyn hal::DynTextureView>, binding_model::CreateBindGroupError>
{
) -> Result<hal::TextureBinding<'a, dyn hal::DynTextureView>, CreateBindGroupError> {
view.same_device(self)?;
let internal_use = self.texture_use_parameters(
@@ -3049,7 +3055,7 @@ impl Device {
decl: &wgt::BindGroupLayoutEntry,
tlas: &'a Arc<Tlas>,
snatch_guard: &'a SnatchGuard<'a>,
) -> Result<&'a dyn hal::DynAccelerationStructure, binding_model::CreateBindGroupError> {
) -> Result<&'a dyn hal::DynAccelerationStructure, CreateBindGroupError> {
use crate::binding_model::CreateBindGroupError as Error;
used.acceleration_structures.insert_single(tlas.clone());
@@ -3087,7 +3093,7 @@ impl Device {
snatch_guard: &'a SnatchGuard,
) -> Result<
hal::ExternalTextureBinding<'a, dyn hal::DynBuffer, dyn hal::DynTextureView>,
binding_model::CreateBindGroupError,
CreateBindGroupError,
> {
use crate::binding_model::CreateBindGroupError as Error;
@@ -3145,7 +3151,7 @@ impl Device {
snatch_guard: &'a SnatchGuard,
) -> Result<
hal::ExternalTextureBinding<'a, dyn hal::DynBuffer, dyn hal::DynTextureView>,
binding_model::CreateBindGroupError,
CreateBindGroupError,
> {
use crate::binding_model::CreateBindGroupError as Error;
@@ -3194,7 +3200,7 @@ impl Device {
pub fn create_bind_group(
self: &Arc<Self>,
desc: binding_model::ResolvedBindGroupDescriptor,
) -> Result<Arc<BindGroup>, binding_model::CreateBindGroupError> {
) -> Result<Arc<BindGroup>, CreateBindGroupError> {
use crate::binding_model::{CreateBindGroupError as Error, ResolvedBindingResource as Br};
let layout = desc.layout;
@@ -3457,7 +3463,7 @@ impl Device {
features: wgt::Features,
count: Option<NonZeroU32>,
num_bindings: usize,
) -> Result<(), binding_model::CreateBindGroupError> {
) -> Result<(), CreateBindGroupError> {
use super::binding_model::CreateBindGroupError as Error;
if let Some(count) = count {
@@ -3492,7 +3498,7 @@ impl Device {
decl: &wgt::BindGroupLayoutEntry,
view: &TextureView,
expected: &'static str,
) -> Result<wgt::TextureUses, binding_model::CreateBindGroupError> {
) -> Result<wgt::TextureUses, CreateBindGroupError> {
use crate::binding_model::CreateBindGroupError as Error;
if view
.desc
@@ -3534,7 +3540,9 @@ impl Device {
// unfilterable if filterable feature is explicitly enabled (only hit
// if wgt::Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES is
// enabled)
(Tst::Float { filterable: true }, Tst::Float { .. }) if view.format_features.flags.contains(wgt::TextureFormatFeatureFlags::FILTERABLE) => {}
(Tst::Float { filterable: true }, Tst::Float { .. })
if view.format_features.flags
.contains(wgt::TextureFormatFeatureFlags::FILTERABLE) => {}
_ => {
return Err(Error::InvalidTextureSampleType {
binding,
@@ -3919,7 +3927,8 @@ impl Device {
continue;
};
// `bind_group_layouts` might contain duplicate entries, so we need to ignore the result.
// `bind_group_layouts` might contain duplicate entries, so we need to ignore the
// result.
let _ = bgl
.exclusive_pipeline
.set(binding_model::ExclusivePipeline::Compute(Arc::downgrade(
@@ -4144,9 +4153,7 @@ impl Device {
// values (larger than 15), because WebGPU requires that it be validated
// on the device timeline.
if cs.write_mask.contains_unknown_bits() {
break 'error Some(pipeline::ColorStateError::InvalidWriteMask(
cs.write_mask,
));
break 'error Some(ColorStateError::InvalidWriteMask(cs.write_mask));
}
let format_features = self.describe_format_features(cs.format)?;
@@ -4154,17 +4161,13 @@ impl Device {
.allowed_usages
.contains(wgt::TextureUsages::RENDER_ATTACHMENT)
{
break 'error Some(pipeline::ColorStateError::FormatNotRenderable(
cs.format,
));
break 'error Some(ColorStateError::FormatNotRenderable(cs.format));
}
if cs.blend.is_some() && !format_features.flags.contains(Tfff::BLENDABLE) {
break 'error Some(pipeline::ColorStateError::FormatNotBlendable(
cs.format,
));
break 'error Some(ColorStateError::FormatNotBlendable(cs.format));
}
if !hal::FormatAspects::from(cs.format).contains(hal::FormatAspects::COLOR) {
break 'error Some(pipeline::ColorStateError::FormatNotColor(cs.format));
break 'error Some(ColorStateError::FormatNotColor(cs.format));
}
if desc.multisample.count > 1
@@ -4172,7 +4175,7 @@ impl Device {
.flags
.sample_count_supported(desc.multisample.count)
{
break 'error Some(pipeline::ColorStateError::InvalidSampleCount(
break 'error Some(ColorStateError::InvalidSampleCount(
desc.multisample.count,
cs.format,
cs.format
@@ -4195,10 +4198,10 @@ impl Device {
dual_source_blending = true;
} else {
break 'error Some(
pipeline::ColorStateError::BlendFactorOnUnsupportedTarget {
ColorStateError::BlendFactorOnUnsupportedTarget {
factor,
target: i as u32,
}
},
);
}
}
@@ -4207,12 +4210,10 @@ impl Device {
.contains(&component.operation)
&& factor != wgt::BlendFactor::One
{
break 'error Some(
pipeline::ColorStateError::InvalidMinMaxBlendFactor {
factor,
target: i as u32,
},
);
break 'error Some(ColorStateError::InvalidMinMaxBlendFactor {
factor,
target: i as u32,
});
}
}
}
@@ -4549,7 +4550,7 @@ impl Device {
|pipeline| {
pipeline::CreateRenderPipelineError::ColorState(
*i as u8,
pipeline::ColorStateError::IncompatibleFormat {
ColorStateError::IncompatibleFormat {
pipeline,
shader: output.ty,
},
@@ -4736,7 +4737,8 @@ impl Device {
continue;
};
// `bind_group_layouts` might contain duplicate entries, so we need to ignore the result.
// `bind_group_layouts` might contain duplicate entries, so we need to ignore the
// result.
let _ = bgl
.exclusive_pipeline
.set(binding_model::ExclusivePipeline::Render(Arc::downgrade(
@@ -4749,7 +4751,8 @@ impl Device {
}
/// # Safety
/// The `data` field on `desc` must have previously been returned from [`crate::global::Global::pipeline_cache_get_data`]
/// The `data` field on `desc` must have previously been returned from
/// [`crate::global::Global::pipeline_cache_get_data`]
pub unsafe fn create_pipeline_cache(
self: &Arc<Self>,
desc: &pipeline::PipelineCacheDescriptor,
@@ -5121,7 +5124,8 @@ impl Device {
Err(WaitIdleError::Timeout) if cfg!(target_arch = "wasm32") => {
// On wasm, you cannot actually successfully wait for the surface.
// However WebGL does not actually require you do this, so ignoring
// the failure is totally fine. See https://github.com/gfx-rs/wgpu/issues/7363
// the failure is totally fine. See
// https://github.com/gfx-rs/wgpu/issues/7363
}
Err(e) => {
break 'error e.into();