buffer zero initialization prior to use in submit

This commit is contained in:
Andreas Reich
2021-01-19 22:59:34 +01:00
parent 875cfb5961
commit a51b4f9232
3 changed files with 43 additions and 2 deletions

View File

@@ -78,6 +78,36 @@ impl<B: GfxBackend> CommandBuffer<B> {
}
}
pub(crate) fn insert_zero_initializations(
raw: &mut B::CommandBuffer,
head: &TrackerSet,
buffer_guard: &mut Storage<Buffer<B>, id::BufferId>,
// TODO: Textures also need initialization!
_texture_guard: &mut Storage<Texture<B>, id::TextureId>,
) {
// Make sure all used buffers are fully initialized.
// TODO This is not optimal since buffer usage may be target of a copy operation.
for id in head.buffers.used() {
let buffer = &mut buffer_guard[id];
let uninitialized_ranges: Vec<std::ops::Range<wgt::BufferAddress>> =
buffer.uninitialized_ranges().collect();
for uninitialized_range in uninitialized_ranges {
// TODO this itself needs resource barriers. How can we make this work?
unsafe {
raw.fill_buffer(
&buffer.raw.as_ref().unwrap().0,
hal::buffer::SubRange {
offset: uninitialized_range.start,
size: Some(uninitialized_range.end - uninitialized_range.start),
},
0,
);
}
buffer.mark_initialized(uninitialized_range);
}
}
}
pub(crate) fn insert_barriers(
raw: &mut B::CommandBuffer,
base: &mut TrackerSet,

View File

@@ -505,7 +505,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
let (compute_pipe_guard, mut token) = hub.compute_pipelines.read(&mut token);
let (render_pipe_guard, mut token) = hub.render_pipelines.read(&mut token);
let (mut buffer_guard, mut token) = hub.buffers.write(&mut token);
let (texture_guard, mut token) = hub.textures.read(&mut token);
let (mut texture_guard, mut token) = hub.textures.write(&mut token);
let (texture_view_guard, mut token) = hub.texture_views.read(&mut token);
let (sampler_guard, _) = hub.samplers.read(&mut token);
@@ -613,6 +613,12 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
.begin_primary(hal::command::CommandBufferFlags::ONE_TIME_SUBMIT);
}
tracing::trace!("Stitching command buffer {:?} before submission", cmb_id);
CommandBuffer::insert_zero_initializations(
&mut transit,
&cmdbuf.trackers,
&mut *buffer_guard,
&mut *texture_guard,
);
CommandBuffer::insert_barriers(
&mut transit,
&mut *trackers,

View File

@@ -169,7 +169,6 @@ pub struct Buffer<B: hal::Backend> {
impl<B: hal::Backend> Buffer<B> {
pub(crate) fn uninitialized_ranges_in_range<
'a,
R: std::iter::FromIterator<Range<wgt::BufferAddress>>,
>(
&self,
@@ -190,6 +189,12 @@ impl<B: hal::Backend> Buffer<B> {
.collect::<R>()
}
pub(crate) fn uninitialized_ranges<'a>(
&'a self,
) -> impl 'a + Iterator<Item = Range<wgt::BufferAddress>> {
self.uninitialized_ranges.allocated_ranges()
}
// Range must be continuous previously uninitialized section.
pub(crate) fn mark_initialized(&mut self, range: Range<wgt::BufferAddress>) {
self.uninitialized_ranges.free_range(range);