Skip to content

Commit

Permalink
Replace Range<T> with StateTransition<T> (#6703)
Browse files Browse the repository at this point in the history
Co-authored-by: Erich Gubler <[email protected]>
  • Loading branch information
atlv24 and ErichDonGubler authored Dec 11, 2024
1 parent 22c8f8c commit 5543961
Show file tree
Hide file tree
Showing 14 changed files with 187 additions and 75 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ By @ErichDonGubler in [#6456](https://github.com/gfx-rs/wgpu/pull/6456), [#6148]

#### HAL

- Replace `usage: Range<T>`, for `BufferUses`, `TextureUses`, and `AccelerationStructureBarrier` with a new `StateTransition<T>`. By @atlv24 in [#6703](https://github.com/gfx-rs/wgpu/pull/6703)
- Change the `DropCallback` API to use `FnOnce` instead of `FnMut`. By @jerzywilczek in [#6482](https://github.com/gfx-rs/wgpu/pull/6482)

### Bug Fixes
Expand Down
10 changes: 8 additions & 2 deletions wgpu-core/src/command/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,10 @@ fn dispatch_indirect(
unsafe {
state.raw_encoder.transition_buffers(&[hal::BufferBarrier {
buffer: params.dst_buffer,
usage: hal::BufferUses::INDIRECT..hal::BufferUses::STORAGE_READ_WRITE,
usage: hal::StateTransition {
from: hal::BufferUses::INDIRECT,
to: hal::BufferUses::STORAGE_READ_WRITE,
},
}]);
}

Expand Down Expand Up @@ -990,7 +993,10 @@ fn dispatch_indirect(
unsafe {
state.raw_encoder.transition_buffers(&[hal::BufferBarrier {
buffer: params.dst_buffer,
usage: hal::BufferUses::STORAGE_READ_WRITE..hal::BufferUses::INDIRECT,
usage: hal::StateTransition {
from: hal::BufferUses::STORAGE_READ_WRITE,
to: hal::BufferUses::INDIRECT,
},
}]);
}

Expand Down
50 changes: 36 additions & 14 deletions wgpu-core/src/command/ray_tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,10 @@ impl Global {

let scratch_buffer_barrier = hal::BufferBarrier::<dyn hal::DynBuffer> {
buffer: scratch_buffer.raw(),
usage: BufferUses::ACCELERATION_STRUCTURE_SCRATCH
..BufferUses::ACCELERATION_STRUCTURE_SCRATCH,
usage: hal::StateTransition {
from: hal::BufferUses::ACCELERATION_STRUCTURE_SCRATCH,
to: hal::BufferUses::ACCELERATION_STRUCTURE_SCRATCH,
},
};

let mut tlas_descriptors = Vec::new();
Expand Down Expand Up @@ -343,8 +345,10 @@ impl Global {

cmd_buf_raw.place_acceleration_structure_barrier(
hal::AccelerationStructureBarrier {
usage: hal::AccelerationStructureUses::BUILD_OUTPUT
..hal::AccelerationStructureUses::SHADER_INPUT,
usage: hal::StateTransition {
from: hal::AccelerationStructureUses::BUILD_OUTPUT,
to: hal::AccelerationStructureUses::SHADER_INPUT,
},
},
);
}
Expand Down Expand Up @@ -629,8 +633,10 @@ impl Global {

let scratch_buffer_barrier = hal::BufferBarrier::<dyn hal::DynBuffer> {
buffer: scratch_buffer.raw(),
usage: BufferUses::ACCELERATION_STRUCTURE_SCRATCH
..BufferUses::ACCELERATION_STRUCTURE_SCRATCH,
usage: hal::StateTransition {
from: BufferUses::ACCELERATION_STRUCTURE_SCRATCH,
to: BufferUses::ACCELERATION_STRUCTURE_SCRATCH,
},
};

let mut tlas_descriptors = Vec::with_capacity(tlas_storage.len());
Expand Down Expand Up @@ -703,7 +709,10 @@ impl Global {
if let Some(ref staging_buffer) = staging_buffer {
cmd_buf_raw.transition_buffers(&[hal::BufferBarrier::<dyn hal::DynBuffer> {
buffer: staging_buffer.raw(),
usage: BufferUses::MAP_WRITE..BufferUses::COPY_SRC,
usage: hal::StateTransition {
from: BufferUses::MAP_WRITE,
to: BufferUses::COPY_SRC,
},
}]);
}
}
Expand All @@ -720,12 +729,18 @@ impl Global {
};
instance_buffer_barriers.push(hal::BufferBarrier::<dyn hal::DynBuffer> {
buffer: tlas.instance_buffer.as_ref(),
usage: BufferUses::COPY_DST..BufferUses::TOP_LEVEL_ACCELERATION_STRUCTURE_INPUT,
usage: hal::StateTransition {
from: BufferUses::COPY_DST,
to: BufferUses::TOP_LEVEL_ACCELERATION_STRUCTURE_INPUT,
},
});
unsafe {
cmd_buf_raw.transition_buffers(&[hal::BufferBarrier::<dyn hal::DynBuffer> {
buffer: tlas.instance_buffer.as_ref(),
usage: BufferUses::MAP_READ..BufferUses::COPY_DST,
usage: hal::StateTransition {
from: BufferUses::MAP_READ,
to: BufferUses::COPY_DST,
},
}]);
let temp = hal::BufferCopy {
src_offset: range.start as u64,
Expand All @@ -749,8 +764,10 @@ impl Global {

cmd_buf_raw.place_acceleration_structure_barrier(
hal::AccelerationStructureBarrier {
usage: hal::AccelerationStructureUses::BUILD_OUTPUT
..hal::AccelerationStructureUses::SHADER_INPUT,
usage: hal::StateTransition {
from: hal::AccelerationStructureUses::BUILD_OUTPUT,
to: hal::AccelerationStructureUses::SHADER_INPUT,
},
},
);
}
Expand Down Expand Up @@ -1275,8 +1292,10 @@ fn build_blas<'a>(
if blas_present {
unsafe {
cmd_buf_raw.place_acceleration_structure_barrier(hal::AccelerationStructureBarrier {
usage: hal::AccelerationStructureUses::BUILD_INPUT
..hal::AccelerationStructureUses::BUILD_OUTPUT,
usage: hal::StateTransition {
from: hal::AccelerationStructureUses::BUILD_INPUT,
to: hal::AccelerationStructureUses::BUILD_OUTPUT,
},
});

cmd_buf_raw.build_acceleration_structures(blas_descriptors);
Expand All @@ -1301,7 +1320,10 @@ fn build_blas<'a>(
}
unsafe {
cmd_buf_raw.place_acceleration_structure_barrier(hal::AccelerationStructureBarrier {
usage: source_usage..destination_usage,
usage: hal::StateTransition {
from: source_usage,
to: destination_usage,
},
});
}
}
20 changes: 16 additions & 4 deletions wgpu-core/src/device/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ impl Queue {
.command_encoder
.transition_buffers(&[hal::BufferBarrier {
buffer: zero_buffer,
usage: hal::BufferUses::empty()..hal::BufferUses::COPY_DST,
usage: hal::StateTransition {
from: hal::BufferUses::empty(),
to: hal::BufferUses::COPY_DST,
},
}]);
pending_writes
.command_encoder
Expand All @@ -83,7 +86,10 @@ impl Queue {
.command_encoder
.transition_buffers(&[hal::BufferBarrier {
buffer: zero_buffer,
usage: hal::BufferUses::COPY_DST..hal::BufferUses::COPY_SRC,
usage: hal::StateTransition {
from: hal::BufferUses::COPY_DST,
to: hal::BufferUses::COPY_SRC,
},
}]);
}

Expand Down Expand Up @@ -628,7 +634,10 @@ impl Queue {
};
let barriers = iter::once(hal::BufferBarrier {
buffer: staging_buffer.raw(),
usage: hal::BufferUses::MAP_WRITE..hal::BufferUses::COPY_SRC,
usage: hal::StateTransition {
from: hal::BufferUses::MAP_WRITE,
to: hal::BufferUses::COPY_SRC,
},
})
.chain(transition.map(|pending| pending.into_hal(&buffer, &snatch_guard)))
.collect::<Vec<_>>();
Expand Down Expand Up @@ -847,7 +856,10 @@ impl Queue {
{
let buffer_barrier = hal::BufferBarrier {
buffer: staging_buffer.raw(),
usage: hal::BufferUses::MAP_WRITE..hal::BufferUses::COPY_SRC,
usage: hal::StateTransition {
from: hal::BufferUses::MAP_WRITE,
to: hal::BufferUses::COPY_SRC,
},
};

let mut trackers = self.device.trackers.lock();
Expand Down
10 changes: 8 additions & 2 deletions wgpu-core/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -636,11 +636,17 @@ impl Buffer {
});
let transition_src = hal::BufferBarrier {
buffer: staging_buffer.raw(),
usage: hal::BufferUses::MAP_WRITE..hal::BufferUses::COPY_SRC,
usage: hal::StateTransition {
from: hal::BufferUses::MAP_WRITE,
to: hal::BufferUses::COPY_SRC,
},
};
let transition_dst = hal::BufferBarrier::<dyn hal::DynBuffer> {
buffer: raw_buf,
usage: hal::BufferUses::empty()..hal::BufferUses::COPY_DST,
usage: hal::StateTransition {
from: hal::BufferUses::empty(),
to: hal::BufferUses::COPY_DST,
},
};
let mut pending_writes = queue.pending_writes.lock();
let encoder = pending_writes.activate();
Expand Down
5 changes: 4 additions & 1 deletion wgpu-core/src/track/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,10 @@ unsafe fn barrier(
barriers.push(PendingTransition {
id: index as _,
selector: (),
usage: current_state..new_state,
usage: hal::StateTransition {
from: current_state,
to: new_state,
},
});
}

Expand Down
6 changes: 3 additions & 3 deletions wgpu-core/src/track/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ impl TrackerIndexAllocators {
pub(crate) struct PendingTransition<S: ResourceUses> {
pub id: u32,
pub selector: S::Selector,
pub usage: ops::Range<S>,
pub usage: hal::StateTransition<S>,
}

pub(crate) type PendingTransitionList = Vec<PendingTransition<hal::TextureUses>>;
Expand All @@ -282,8 +282,8 @@ impl PendingTransition<hal::TextureUses> {
texture: &dyn hal::DynTexture,
) -> hal::TextureBarrier<'_, dyn hal::DynTexture> {
// These showing up in a barrier is always a bug
strict_assert_ne!(self.usage.start, hal::TextureUses::UNKNOWN);
strict_assert_ne!(self.usage.end, hal::TextureUses::UNKNOWN);
strict_assert_ne!(self.usage.from, hal::TextureUses::UNKNOWN);
strict_assert_ne!(self.usage.to, hal::TextureUses::UNKNOWN);

let mip_count = self.selector.mips.end - self.selector.mips.start;
strict_assert_ne!(mip_count, 0);
Expand Down
20 changes: 16 additions & 4 deletions wgpu-core/src/track/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1304,7 +1304,10 @@ unsafe fn barrier(
barriers.push(PendingTransition {
id: index as _,
selector: texture_selector.clone(),
usage: current_simple..new_simple,
usage: hal::StateTransition {
from: current_simple,
to: new_simple,
},
});
}
(SingleOrManyStates::Single(current_simple), SingleOrManyStates::Many(new_many)) => {
Expand All @@ -1320,7 +1323,10 @@ unsafe fn barrier(
barriers.push(PendingTransition {
id: index as _,
selector,
usage: current_simple..new_state,
usage: hal::StateTransition {
from: current_simple,
to: new_state,
},
});
}
}
Expand All @@ -1343,7 +1349,10 @@ unsafe fn barrier(
mips: mip_id..mip_id + 1,
layers: layers.clone(),
},
usage: current_layer_state..new_simple,
usage: hal::StateTransition {
from: current_layer_state,
to: new_simple,
},
});
}
}
Expand Down Expand Up @@ -1372,7 +1381,10 @@ unsafe fn barrier(
mips: mip_id..mip_id + 1,
layers,
},
usage: *current_layer_state..new_state,
usage: hal::StateTransition {
from: *current_layer_state,
to: new_state,
},
});
}
}
Expand Down
25 changes: 20 additions & 5 deletions wgpu-hal/examples/halmark/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,17 +331,26 @@ impl<A: hal::Api> Example<A> {
{
let buffer_barrier = hal::BufferBarrier {
buffer: &staging_buffer,
usage: hal::BufferUses::empty()..hal::BufferUses::COPY_SRC,
usage: hal::StateTransition {
from: hal::BufferUses::empty(),
to: hal::BufferUses::COPY_SRC,
},
};
let texture_barrier1 = hal::TextureBarrier {
texture: &texture,
range: wgt::ImageSubresourceRange::default(),
usage: hal::TextureUses::UNINITIALIZED..hal::TextureUses::COPY_DST,
usage: hal::StateTransition {
from: hal::TextureUses::UNINITIALIZED,
to: hal::TextureUses::COPY_DST,
},
};
let texture_barrier2 = hal::TextureBarrier {
texture: &texture,
range: wgt::ImageSubresourceRange::default(),
usage: hal::TextureUses::COPY_DST..hal::TextureUses::RESOURCE,
usage: hal::StateTransition {
from: hal::TextureUses::COPY_DST,
to: hal::TextureUses::RESOURCE,
},
};
let copy = hal::BufferTextureCopy {
buffer_layout: wgt::TexelCopyBufferLayout {
Expand Down Expand Up @@ -665,7 +674,10 @@ impl<A: hal::Api> Example<A> {
let target_barrier0 = hal::TextureBarrier {
texture: surface_tex.borrow(),
range: wgt::ImageSubresourceRange::default(),
usage: hal::TextureUses::UNINITIALIZED..hal::TextureUses::COLOR_TARGET,
usage: hal::StateTransition {
from: hal::TextureUses::UNINITIALIZED,
to: hal::TextureUses::COLOR_TARGET,
},
};
unsafe {
ctx.encoder.begin_encoding(Some("frame")).unwrap();
Expand Down Expand Up @@ -732,7 +744,10 @@ impl<A: hal::Api> Example<A> {
let target_barrier1 = hal::TextureBarrier {
texture: surface_tex.borrow(),
range: wgt::ImageSubresourceRange::default(),
usage: hal::TextureUses::COLOR_TARGET..hal::TextureUses::PRESENT,
usage: hal::StateTransition {
from: hal::TextureUses::COLOR_TARGET,
to: hal::TextureUses::PRESENT,
},
};
unsafe {
ctx.encoder.end_render_pass();
Expand Down
Loading

0 comments on commit 5543961

Please sign in to comment.