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

Move ResourceMetadata into its own module, and give it a proper interface #3213

Merged
merged 10 commits into from
Nov 23, 2022
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ Bottom level categories:

- Update the `minimum supported rust version` to 1.65
- Use cargo 1.64 workspace inheritance feature. By @jinleili in [#3107](https://github.com/gfx-rs/wgpu/pull/3107)
- Move `ResourceMetadata` into its own module. By @jimblandy in [#3213](https://github.com/gfx-rs/wgpu/pull/3213)

#### Vulkan

Expand Down
45 changes: 19 additions & 26 deletions wgpu-core/src/track/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use crate::{
id::{BufferId, TypedId, Valid},
resource::Buffer,
track::{
invalid_resource_state, iterate_bitvec_indices, skip_barrier, ResourceMetadata,
ResourceMetadataProvider, ResourceUses, UsageConflict,
invalid_resource_state, skip_barrier, ResourceMetadata, ResourceMetadataProvider,
ResourceUses, UsageConflict,
},
LifeGuard, RefCount,
};
Expand Down Expand Up @@ -124,7 +124,7 @@ impl<A: hub::HalApi> BufferUsageScope<A> {

/// Returns a list of all buffers tracked.
pub fn used(&self) -> impl Iterator<Item = Valid<BufferId>> + '_ {
self.metadata.used()
self.metadata.owned_ids()
}

/// Merge the list of buffer states in the given bind group into this usage scope.
Expand Down Expand Up @@ -180,7 +180,7 @@ impl<A: hub::HalApi> BufferUsageScope<A> {
self.set_size(incoming_size);
}

for index in iterate_bitvec_indices(&scope.metadata.owned) {
for index in scope.metadata.owned_indices() {
self.tracker_assert_in_bounds(index);
scope.tracker_assert_in_bounds(index);

Expand Down Expand Up @@ -293,7 +293,7 @@ impl<A: hub::HalApi> BufferTracker<A> {

/// Returns a list of all buffers tracked.
pub fn used(&self) -> impl Iterator<Item = Valid<BufferId>> + '_ {
self.metadata.used()
self.metadata.owned_ids()
}

/// Drains all currently pending transitions.
Expand All @@ -316,7 +316,7 @@ impl<A: hub::HalApi> BufferTracker<A> {
self.tracker_assert_in_bounds(index);

unsafe {
let currently_owned = self.metadata.owned.get(index).unwrap_unchecked();
let currently_owned = self.metadata.contains_unchecked(index);

if currently_owned {
panic!("Tried to insert buffer already tracked");
Expand Down Expand Up @@ -394,7 +394,7 @@ impl<A: hub::HalApi> BufferTracker<A> {
self.set_size(incoming_size);
}

for index in iterate_bitvec_indices(&tracker.metadata.owned) {
for index in tracker.metadata.owned_indices() {
self.tracker_assert_in_bounds(index);
tracker.tracker_assert_in_bounds(index);
unsafe {
Expand Down Expand Up @@ -434,7 +434,7 @@ impl<A: hub::HalApi> BufferTracker<A> {
self.set_size(incoming_size);
}

for index in iterate_bitvec_indices(&scope.metadata.owned) {
for index in scope.metadata.owned_indices() {
self.tracker_assert_in_bounds(index);
scope.tracker_assert_in_bounds(index);
unsafe {
Expand Down Expand Up @@ -492,7 +492,7 @@ impl<A: hub::HalApi> BufferTracker<A> {

scope.tracker_assert_in_bounds(index);

if unsafe { !scope.metadata.owned.get(index).unwrap_unchecked() } {
if unsafe { !scope.metadata.contains_unchecked(index) } {
continue;
}
unsafe {
Expand All @@ -514,7 +514,7 @@ impl<A: hub::HalApi> BufferTracker<A> {
)
};

unsafe { scope.metadata.reset(index) };
unsafe { scope.metadata.remove(index) };
}
}

Expand All @@ -529,22 +529,19 @@ impl<A: hub::HalApi> BufferTracker<A> {
let (index32, epoch, _) = id.0.unzip();
let index = index32 as usize;

if index > self.metadata.owned.len() {
if index > self.metadata.size() {
return false;
}

self.tracker_assert_in_bounds(index);

unsafe {
if self.metadata.owned.get(index).unwrap_unchecked() {
let existing_epoch = self.metadata.epochs.get_unchecked(index);
let existing_ref_count = self.metadata.ref_counts.get_unchecked(index);

if *existing_epoch == epoch
&& existing_ref_count.as_ref().unwrap_unchecked().load() == 1
{
self.metadata.reset(index);
if self.metadata.contains_unchecked(index) {
let existing_epoch = self.metadata.get_epoch_unchecked(index);
let existing_ref_count = self.metadata.get_ref_count_unchecked(index);

if existing_epoch == epoch && existing_ref_count.load() == 1 {
self.metadata.remove(index);
return true;
}
}
Expand Down Expand Up @@ -600,7 +597,7 @@ unsafe fn insert_or_merge<A: hub::HalApi>(
state_provider: BufferStateProvider<'_>,
metadata_provider: ResourceMetadataProvider<'_, A>,
) -> Result<(), UsageConflict> {
let currently_owned = unsafe { resource_metadata.owned.get(index).unwrap_unchecked() };
let currently_owned = unsafe { resource_metadata.contains_unchecked(index) };

if !currently_owned {
unsafe {
Expand Down Expand Up @@ -659,7 +656,7 @@ unsafe fn insert_or_barrier_update<A: hub::HalApi>(
metadata_provider: ResourceMetadataProvider<'_, A>,
barriers: &mut Vec<PendingTransition<BufferUses>>,
) {
let currently_owned = unsafe { resource_metadata.owned.get(index).unwrap_unchecked() };
let currently_owned = unsafe { resource_metadata.contains_unchecked(index) };

if !currently_owned {
unsafe {
Expand Down Expand Up @@ -720,11 +717,7 @@ unsafe fn insert<A: hub::HalApi>(
*current_states.get_unchecked_mut(index) = new_end_state;

let (epoch, ref_count) = metadata_provider.get_own(life_guard, index);

resource_metadata.owned.set(index, true);

*resource_metadata.epochs.get_unchecked_mut(index) = epoch;
*resource_metadata.ref_counts.get_unchecked_mut(index) = Some(ref_count);
resource_metadata.insert(index, epoch, ref_count);
}
}

Expand Down
Loading