Skip to content

Commit

Permalink
TrackedRenderPass state invalidation when wgpu::RenderPass is used
Browse files Browse the repository at this point in the history
  • Loading branch information
PPakalns committed Aug 28, 2024
1 parent a9039b4 commit 6a9c663
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions crates/bevy_render/src/render_phase/draw_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ struct DrawState {
/// List of vertex buffers by [`BufferId`], offset, and size. See [`DrawState::buffer_slice_key`]
vertex_buffers: Vec<Option<(BufferId, u64, u64)>>,
index_buffer: Option<(BufferId, u64, IndexFormat)>,

/// Stores whether this state is populated or empty for quick state invalidation
stores_state: bool,
}

impl DrawState {
Expand All @@ -34,6 +37,7 @@ impl DrawState {
// self.vertex_buffers.clear();
// self.index_buffer = None;
self.pipeline = Some(pipeline);
self.stores_state = true;
}

/// Checks, whether the `pipeline` is already bound.
Expand All @@ -47,6 +51,7 @@ impl DrawState {
group.0 = Some(bind_group);
group.1.clear();
group.1.extend(dynamic_indices);
self.stores_state = true;
}

/// Checks, whether the `bind_group` is already bound to the `index`.
Expand All @@ -66,6 +71,7 @@ impl DrawState {
/// Marks the vertex `buffer` as bound to the `index`.
fn set_vertex_buffer(&mut self, index: usize, buffer_slice: BufferSlice) {
self.vertex_buffers[index] = Some(self.buffer_slice_key(&buffer_slice));
self.stores_state = true;
}

/// Checks, whether the vertex `buffer` is already bound to the `index`.
Expand All @@ -89,6 +95,7 @@ impl DrawState {
/// Marks the index `buffer` as bound.
fn set_index_buffer(&mut self, buffer: BufferId, offset: u64, index_format: IndexFormat) {
self.index_buffer = Some((buffer, offset, index_format));
self.stores_state = true;
}

/// Checks, whether the index `buffer` is already bound.
Expand All @@ -103,6 +110,9 @@ impl DrawState {

/// Resets tracking state
pub fn reset_tracking(&mut self) {
if !self.stores_state {
return;
}
self.pipeline = None;
self.bind_groups.iter_mut().for_each(|val| {
val.0 = None;
Expand All @@ -112,6 +122,7 @@ impl DrawState {
*val = None;
});
self.index_buffer = None;
self.stores_state = false;
}
}

Expand Down Expand Up @@ -140,16 +151,12 @@ impl<'a> TrackedRenderPass<'a> {
}
}

/// Reset internal tracking state
pub fn reset_tracking(&mut self) {
self.state.reset_tracking();
}

/// Returns the wgpu [`RenderPass`].
///
/// If render pass state is modified using returned reference,
/// method [`TrackedRenderPass::reset_tracking`] may need to be called.
/// Function invalidates internal tracking state,
/// some redundant pipeline operations may not be skipped.
pub fn wgpu_pass(&mut self) -> &mut RenderPass<'a> {
self.state.reset_tracking();
&mut self.pass
}

Expand Down

0 comments on commit 6a9c663

Please sign in to comment.