Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove more internal uses of IDs #5848

Merged
merged 27 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
6129aa8
replace uses of `Id.backend()` with `A::VARIANT`
teoxoy Jun 19, 2024
3abcdda
remove `TextureBindGroupState.add_single`'s return type
teoxoy Jun 19, 2024
0b31c06
make `clear_texture_via_render_passes` infallible (in practice it was…
teoxoy Jun 19, 2024
6028052
remove usage of Texture IDs in clear code
teoxoy Jun 19, 2024
c526332
remove usage of Buffer IDs in clear code
teoxoy Jun 19, 2024
0ffb66b
use `Arc::ptr_eq` for resource equality
teoxoy Jun 19, 2024
ee121b4
use the tracker index as key in hashmap instead of ID
teoxoy Jun 19, 2024
aca802d
use `error_ident` for log instead of ID
teoxoy Jun 19, 2024
8be0ac9
make `check_buffer_usage` a buffer method
teoxoy Jun 19, 2024
a37e4bd
make `check_texture_usage` a texture method
teoxoy Jun 19, 2024
696d98e
simplify `BufferTracker.set_single`'s return type
teoxoy Jun 19, 2024
e15a249
remove IDs from `StatelessBindGroupState`
teoxoy Jun 19, 2024
2b45ae9
take buffer lookup out of `BufferBindGroupState.add_single`
teoxoy Jun 19, 2024
f47608b
make return type of `TextureTracker.set_single` non-optional
teoxoy Jun 19, 2024
169115d
consolidate destroyed texture/buffer errors and separate them from in…
teoxoy Jun 19, 2024
02f508d
remove old comment
teoxoy Jun 20, 2024
6144498
take resource lookup out of `StatelessTracker.add_single`
teoxoy Jun 20, 2024
a8b89a1
introduce `TextureView.try_raw`
teoxoy Jun 20, 2024
3ce5eb0
change `BindGroup.raw` to `BindGroup.try_raw`
teoxoy Jun 20, 2024
8d65500
remove ID from `QueueSubmitError::BufferStillMapped`
teoxoy Jun 20, 2024
195b32c
take resource lookup out of `BufferUsageScope.merge_single`
teoxoy Jun 20, 2024
6d93677
move body of `BufferUsageScope.insert_merge_single` in `BufferUsageSc…
teoxoy Jun 20, 2024
dcab7c6
change return type of `ResourceMetadataProvider.get` to `&Arc<T>`
teoxoy Jun 20, 2024
8013f07
remove IDs from `UsageConflict` variants
teoxoy Jun 20, 2024
feeec58
rename `UsageConflict` to `ResourceUsageCompatibilityError`
teoxoy Jun 20, 2024
4bfeb6e
inline id getters
teoxoy Jun 20, 2024
f21a9ba
use `TrackerIndex` instead of IDs in `PendingWrites`'s fields
teoxoy Jun 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 25 additions & 19 deletions wgpu-core/src/binding_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ use crate::{
},
error::{ErrorFormatter, PrettyError},
hal_api::HalApi,
id::{BindGroupLayoutId, BufferId, SamplerId, TextureId, TextureViewId},
id::{BindGroupLayoutId, BufferId, SamplerId, TextureViewId},
init_tracker::{BufferInitTrackerAction, TextureInitTrackerAction},
resource::{ParentDevice, Resource, ResourceInfo, ResourceType},
resource::{
DestroyedResourceError, MissingBufferUsageError, MissingTextureUsageError, ParentDevice,
Resource, ResourceInfo, ResourceType,
},
resource_log,
snatch::{SnatchGuard, Snatchable},
track::{BindGroupStates, UsageConflict},
validation::{MissingBufferUsageError, MissingTextureUsageError},
track::{BindGroupStates, ResourceUsageCompatibilityError},
Label,
};

Expand Down Expand Up @@ -76,14 +78,14 @@ pub enum CreateBindGroupError {
Device(#[from] DeviceError),
#[error("Bind group layout is invalid")]
InvalidLayout,
#[error("Buffer {0:?} is invalid or destroyed")]
InvalidBuffer(BufferId),
#[error("Texture view {0:?} is invalid")]
InvalidTextureView(TextureViewId),
#[error("Texture {0:?} is invalid")]
InvalidTexture(TextureId),
#[error("BufferId {0:?} is invalid")]
InvalidBufferId(BufferId),
#[error("Texture view Id {0:?} is invalid")]
InvalidTextureViewId(TextureViewId),
#[error("Sampler {0:?} is invalid")]
InvalidSampler(SamplerId),
#[error(transparent)]
DestroyedResource(#[from] DestroyedResourceError),
#[error(
"Binding count declared with at most {expected} items, but {actual} items were provided"
)]
Expand Down Expand Up @@ -182,7 +184,7 @@ pub enum CreateBindGroupError {
#[error("The adapter does not support read access for storages texture of format {0:?}")]
StorageReadNotSupported(wgt::TextureFormat),
#[error(transparent)]
ResourceUsageConflict(#[from] UsageConflict),
ResourceUsageCompatibility(#[from] ResourceUsageCompatibilityError),
}

impl PrettyError for CreateBindGroupError {
Expand All @@ -198,10 +200,7 @@ impl PrettyError for CreateBindGroupError {
Self::BindingSizeTooSmall { buffer, .. } => {
fmt.buffer_label(&buffer);
}
Self::InvalidBuffer(id) => {
fmt.buffer_label(&id);
}
Self::InvalidTextureView(id) => {
Self::InvalidTextureViewId(id) => {
fmt.texture_view_label(&id);
}
Self::InvalidSampler(id) => {
Expand Down Expand Up @@ -895,17 +894,24 @@ impl<A: HalApi> Drop for BindGroup<A> {
}

impl<A: HalApi> BindGroup<A> {
pub(crate) fn raw(&self, guard: &SnatchGuard) -> Option<&A::BindGroup> {
pub(crate) fn try_raw<'a>(
&'a self,
guard: &'a SnatchGuard,
) -> Result<&A::BindGroup, DestroyedResourceError> {
// Clippy insist on writing it this way. The idea is to return None
// if any of the raw buffer is not valid anymore.
for buffer in &self.used_buffer_ranges {
let _ = buffer.buffer.raw(guard)?;
buffer.buffer.try_raw(guard)?;
}
for texture in &self.used_texture_ranges {
let _ = texture.texture.raw(guard)?;
texture.texture.try_raw(guard)?;
}
self.raw.get(guard)

self.raw
.get(guard)
.ok_or_else(|| DestroyedResourceError(self.error_ident()))
}

pub(crate) fn validate_dynamic_bindings(
&self,
bind_group_index: u32,
Expand Down
4 changes: 1 addition & 3 deletions wgpu-core/src/command/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,7 @@ impl<A: HalApi> Binder<A> {
bind_group: &Arc<BindGroup<A>>,
offsets: &[wgt::DynamicOffset],
) -> &'a [EntryPayload<A>] {
let bind_group_id = bind_group.as_info().id();
log::trace!("\tBinding [{}] = group {:?}", index, bind_group_id);
debug_assert_eq!(A::VARIANT, bind_group_id.backend());
log::trace!("\tBinding [{}] = group {}", index, bind_group.error_ident());

let payload = &mut self.payloads[index];
payload.group = Some(bind_group.clone());
Expand Down
128 changes: 67 additions & 61 deletions wgpu-core/src/command/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,12 @@ use crate::{
id,
init_tracker::{BufferInitTrackerAction, MemoryInitKind, TextureInitTrackerAction},
pipeline::{PipelineFlags, RenderPipeline, VertexStep},
resource::{Buffer, ParentDevice, Resource, ResourceInfo, ResourceType},
resource::{
Buffer, DestroyedResourceError, ParentDevice, Resource, ResourceInfo, ResourceType,
},
resource_log,
snatch::SnatchGuard,
track::RenderBundleScope,
validation::check_buffer_usage,
Label, LabelHelpers,
};
use arrayvec::ArrayVec;
Expand Down Expand Up @@ -409,13 +410,17 @@ impl RenderBundleEncoder {
} => {
let scope = PassErrorScope::SetBindGroup(bind_group_id);

let bind_group = state
let bind_group = bind_group_guard
.get(bind_group_id)
.map_err(|_| RenderCommandError::InvalidBindGroupId(bind_group_id))
.map_pass_err(scope)?;

state
.trackers
.bind_groups
.write()
.add_single(&*bind_group_guard, bind_group_id)
.ok_or(RenderCommandError::InvalidBindGroup(bind_group_id))
.map_pass_err(scope)?;
.add_single(bind_group);

self.check_valid_to_use(bind_group.device.info.id())
.map_pass_err(scope)?;

Expand Down Expand Up @@ -474,13 +479,17 @@ impl RenderBundleEncoder {
RenderCommand::SetPipeline(pipeline_id) => {
let scope = PassErrorScope::SetPipelineRender(pipeline_id);

let pipeline = state
let pipeline = pipeline_guard
.get(pipeline_id)
.map_err(|_| RenderCommandError::InvalidPipeline(pipeline_id))
.map_pass_err(scope)?;

state
.trackers
.render_pipelines
.write()
.add_single(&*pipeline_guard, pipeline_id)
.ok_or(RenderCommandError::InvalidPipeline(pipeline_id))
.map_pass_err(scope)?;
.add_single(pipeline);

self.check_valid_to_use(pipeline.device.info.id())
.map_pass_err(scope)?;

Expand Down Expand Up @@ -517,16 +526,22 @@ impl RenderBundleEncoder {
size,
} => {
let scope = PassErrorScope::SetIndexBuffer(buffer_id);
let buffer = state

let buffer = buffer_guard
.get(buffer_id)
.map_err(|_| RenderCommandError::InvalidBufferId(buffer_id))
.map_pass_err(scope)?;

state
.trackers
.buffers
.write()
.merge_single(&*buffer_guard, buffer_id, hal::BufferUses::INDEX)
.merge_single(buffer, hal::BufferUses::INDEX)
.map_pass_err(scope)?;

self.check_valid_to_use(buffer.device.info.id())
.map_pass_err(scope)?;
check_buffer_usage(buffer_id, buffer.usage, wgt::BufferUsages::INDEX)
.map_pass_err(scope)?;
buffer.check_usage(wgt::BufferUsages::INDEX).map_pass_err(scope)?;

let end = match size {
Some(s) => offset + s.get(),
Expand Down Expand Up @@ -556,16 +571,20 @@ impl RenderBundleEncoder {
.map_pass_err(scope);
}

let buffer = state
let buffer = buffer_guard
.get(buffer_id)
.map_err(|_| RenderCommandError::InvalidBufferId(buffer_id))
.map_pass_err(scope)?;

state
.trackers
.buffers
.write()
.merge_single(&*buffer_guard, buffer_id, hal::BufferUses::VERTEX)
.buffers.write()
.merge_single(buffer, hal::BufferUses::VERTEX)
.map_pass_err(scope)?;

self.check_valid_to_use(buffer.device.info.id())
.map_pass_err(scope)?;
check_buffer_usage(buffer_id, buffer.usage, wgt::BufferUsages::VERTEX)
.map_pass_err(scope)?;
buffer.check_usage(wgt::BufferUsages::VERTEX).map_pass_err(scope)?;

let end = match size {
Some(s) => offset + s.get(),
Expand Down Expand Up @@ -683,16 +702,20 @@ impl RenderBundleEncoder {
let pipeline = state.pipeline(scope)?;
let used_bind_groups = pipeline.used_bind_groups;

let buffer = state
let buffer = buffer_guard
.get(buffer_id)
.map_err(|_| RenderCommandError::InvalidBufferId(buffer_id))
.map_pass_err(scope)?;

state
.trackers
.buffers
.write()
.merge_single(&*buffer_guard, buffer_id, hal::BufferUses::INDIRECT)
.buffers.write()
.merge_single(buffer, hal::BufferUses::INDIRECT)
.map_pass_err(scope)?;

self.check_valid_to_use(buffer.device.info.id())
.map_pass_err(scope)?;
check_buffer_usage(buffer_id, buffer.usage, wgt::BufferUsages::INDIRECT)
.map_pass_err(scope)?;
buffer.check_usage(wgt::BufferUsages::INDIRECT).map_pass_err(scope)?;

buffer_memory_init_actions.extend(buffer.initialization_status.read().create_action(
buffer,
Expand Down Expand Up @@ -722,16 +745,20 @@ impl RenderBundleEncoder {
let pipeline = state.pipeline(scope)?;
let used_bind_groups = pipeline.used_bind_groups;

let buffer = state
let buffer = buffer_guard
.get(buffer_id)
.map_err(|_| RenderCommandError::InvalidBufferId(buffer_id))
.map_pass_err(scope)?;

state
.trackers
.buffers
.write()
.merge_single(&*buffer_guard, buffer_id, hal::BufferUses::INDIRECT)
.buffers.write()
.merge_single(buffer, hal::BufferUses::INDIRECT)
.map_pass_err(scope)?;

self.check_valid_to_use(buffer.device.info.id())
.map_pass_err(scope)?;
check_buffer_usage(buffer_id, buffer.usage, wgt::BufferUsages::INDIRECT)
.map_pass_err(scope)?;
buffer.check_usage(wgt::BufferUsages::INDIRECT).map_pass_err(scope)?;

buffer_memory_init_actions.extend(buffer.initialization_status.read().create_action(
buffer,
Expand Down Expand Up @@ -830,25 +857,14 @@ pub enum CreateRenderBundleError {
#[derive(Clone, Debug, Error)]
#[non_exhaustive]
pub enum ExecutionError {
#[error("Buffer {0:?} is destroyed")]
DestroyedBuffer(id::BufferId),
#[error("BindGroup {0:?} is invalid")]
InvalidBindGroup(id::BindGroupId),
#[error(transparent)]
DestroyedResource(#[from] DestroyedResourceError),
#[error("Using {0} in a render bundle is not implemented")]
Unimplemented(&'static str),
}
impl PrettyError for ExecutionError {
fn fmt_pretty(&self, fmt: &mut ErrorFormatter) {
fmt.error(self);
match *self {
Self::DestroyedBuffer(id) => {
fmt.buffer_label(&id);
}
Self::InvalidBindGroup(id) => {
fmt.bind_group_label(&id);
}
Self::Unimplemented(_reason) => {}
};
}
}

Expand Down Expand Up @@ -920,9 +936,7 @@ impl<A: HalApi> RenderBundle<A> {
num_dynamic_offsets,
bind_group,
} => {
let raw_bg = bind_group
.raw(snatch_guard)
.ok_or(ExecutionError::InvalidBindGroup(bind_group.info.id()))?;
let raw_bg = bind_group.try_raw(snatch_guard)?;
unsafe {
raw.set_bind_group(
pipeline_layout.as_ref().unwrap().raw(),
Expand All @@ -944,9 +958,7 @@ impl<A: HalApi> RenderBundle<A> {
offset,
size,
} => {
let buffer: &A::Buffer = buffer
.raw(snatch_guard)
.ok_or(ExecutionError::DestroyedBuffer(buffer.info.id()))?;
let buffer: &A::Buffer = buffer.try_raw(snatch_guard)?;
let bb = hal::BufferBinding {
buffer,
offset: *offset,
Expand All @@ -960,9 +972,7 @@ impl<A: HalApi> RenderBundle<A> {
offset,
size,
} => {
let buffer = buffer
.raw(snatch_guard)
.ok_or(ExecutionError::DestroyedBuffer(buffer.info.id()))?;
let buffer = buffer.try_raw(snatch_guard)?;
let bb = hal::BufferBinding {
buffer,
offset: *offset,
Expand Down Expand Up @@ -1047,9 +1057,7 @@ impl<A: HalApi> RenderBundle<A> {
count: None,
indexed: false,
} => {
let buffer = buffer
.raw(snatch_guard)
.ok_or(ExecutionError::DestroyedBuffer(buffer.info.id()))?;
let buffer = buffer.try_raw(snatch_guard)?;
unsafe { raw.draw_indirect(buffer, *offset, 1) };
}
Cmd::MultiDrawIndirect {
Expand All @@ -1058,9 +1066,7 @@ impl<A: HalApi> RenderBundle<A> {
count: None,
indexed: true,
} => {
let buffer = buffer
.raw(snatch_guard)
.ok_or(ExecutionError::DestroyedBuffer(buffer.info.id()))?;
let buffer = buffer.try_raw(snatch_guard)?;
unsafe { raw.draw_indexed_indirect(buffer, *offset, 1) };
}
Cmd::MultiDrawIndirect { .. } | Cmd::MultiDrawIndirectCount { .. } => {
Expand Down Expand Up @@ -1414,7 +1420,7 @@ impl<A: HalApi> State<A> {
) {
match self.index {
Some(ref current)
if Arc::ptr_eq(&current.buffer, &buffer)
if current.buffer.is_equal(&buffer)
&& current.format == format
&& current.range == range =>
{
Expand Down
Loading
Loading