Skip to content

Commit

Permalink
Refactor usage tracker init()
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Jan 7, 2020
1 parent 8ab05ae commit b220566
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 41 deletions.
4 changes: 2 additions & 2 deletions wgpu-core/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ impl<F: IdentityFilter<RenderPassId>> Global<F> {
at.attachment,
view.life_guard.ref_count.clone(),
PhantomData,
).is_some();
).is_ok();

let layouts = match view.inner {
TextureViewInner::Native { ref source_id, .. } => {
Expand Down Expand Up @@ -383,7 +383,7 @@ impl<F: IdentityFilter<RenderPassId>> Global<F> {
resolve_target,
view.life_guard.ref_count.clone(),
PhantomData,
).is_some();
).is_ok();

let layouts = match view.inner {
TextureViewInner::Native { ref source_id, .. } => {
Expand Down
14 changes: 5 additions & 9 deletions wgpu-core/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,6 @@ impl<F: IdentityFilter<BufferId>> Global<F> {
let device = &device_guard[device_id];
let buffer = device.create_buffer(device_id, desc);
let ref_count = buffer.life_guard.ref_count.clone();
let range = buffer.full_range;

let id = hub.buffers.register_identity(id_in, buffer, &mut token);
device
Expand All @@ -815,7 +814,7 @@ impl<F: IdentityFilter<BufferId>> Global<F> {
.init(
id,
ref_count,
BufferState::from_selector(&range),
BufferState::with_usage(resource::BufferUsage::empty()),
)
.unwrap();
id
Expand All @@ -836,7 +835,6 @@ impl<F: IdentityFilter<BufferId>> Global<F> {
let device = &device_guard[device_id];
let mut buffer = device.create_buffer(device_id, &desc);
let ref_count = buffer.life_guard.ref_count.clone();
let range = buffer.full_range;

let pointer = match map_buffer(&device.raw, &mut buffer, 0 .. desc.size, HostMap::Write) {
Ok(ptr) => ptr,
Expand All @@ -852,10 +850,9 @@ impl<F: IdentityFilter<BufferId>> Global<F> {
.buffers.init(
id,
ref_count,
BufferState::from_selector(&range),
BufferState::with_usage(resource::BufferUsage::MAP_WRITE),
)
.unwrap()
.set((), resource::BufferUsage::MAP_WRITE);
.unwrap();

(id, pointer)
}
Expand Down Expand Up @@ -972,10 +969,9 @@ impl<F: IdentityFilter<TextureId>> Global<F> {
.textures.init(
id,
ref_count,
TextureState::from_selector(&range),
TextureState::with_range(&range),
)
.unwrap()
.set(range, resource::TextureUsage::UNINITIALIZED);
.unwrap();
id
}

Expand Down
4 changes: 2 additions & 2 deletions wgpu-core/src/track/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ impl Default for BufferState {
}

impl BufferState {
pub fn from_selector(_full_selector: &()) -> Self {
BufferState::default()
pub fn with_usage(usage: BufferUsage) -> Self {
Unit::new(usage)
}
}

Expand Down
30 changes: 7 additions & 23 deletions wgpu-core/src/track/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,21 +166,6 @@ impl PendingTransition<TextureState> {
}
}

/// Helper initialization structure that allows setting the usage on
/// various sub-resources.
#[derive(Debug)]
pub struct Initializer<'a, S: ResourceState> {
id: S::Id,
state: &'a mut S,
}

impl<S: ResourceState> Initializer<'_, S> {
pub fn set(&mut self, selector: S::Selector, usage: S::Usage) -> bool {
self.state.change(self.id, selector, usage, None)
.is_ok()
}
}

/// A tracker for all resources of a given type.
pub struct ResourceTracker<S: ResourceState> {
/// An association of known resource indices with their tracked states.
Expand Down Expand Up @@ -248,28 +233,27 @@ impl<S: ResourceState> ResourceTracker<S> {

/// Initialize a resource to be used.
///
/// Returns `false` if the resource is already tracked.
/// Returns false if the resource is already registered.
pub fn init(
&mut self,
id: S::Id,
ref_count: RefCount,
state: S,
) -> Option<Initializer<S>> {
) -> Result<(), &S> {
let (index, epoch, backend) = id.unzip();
debug_assert_eq!(backend, self.backend);
match self.map.entry(index) {
Entry::Vacant(e) => {
let res = e.insert(Resource {
e.insert(Resource {
ref_count,
state,
epoch,
});
Some(Initializer {
id,
state: &mut res.state,
})
Ok(())
}
Entry::Occupied(e) => {
Err(&e.into_mut().state)
}
Entry::Occupied(_) => None,
}
}

Expand Down
10 changes: 5 additions & 5 deletions wgpu-core/src/track/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ impl PendingTransition<TextureState> {
}

impl TextureState {
pub fn from_selector(full_selector: &hal::image::SubresourceRange) -> Self {
debug_assert_eq!(full_selector.layers.start, 0);
debug_assert_eq!(full_selector.levels.start, 0);
pub fn with_range(range: &hal::image::SubresourceRange) -> Self {
debug_assert_eq!(range.layers.start, 0);
debug_assert_eq!(range.levels.start, 0);
TextureState {
mips: iter::repeat_with(|| {
PlaneStates::from_range(
0 .. full_selector.layers.end,
0 .. range.layers.end,
Unit::new(TextureUsage::UNINITIALIZED),
)
})
.take(full_selector.levels.end as usize)
.take(range.levels.end as usize)
.collect(),
full: true,
}
Expand Down

0 comments on commit b220566

Please sign in to comment.