Skip to content

Commit

Permalink
Reuse buffers for render statistics. Now they work similarly to wgpu-…
Browse files Browse the repository at this point in the history
…profiler (while collecting more data)
  • Loading branch information
LeshaInc committed Jul 13, 2023
1 parent 4d287a6 commit 2efd135
Show file tree
Hide file tree
Showing 3 changed files with 225 additions and 126 deletions.
12 changes: 8 additions & 4 deletions crates/bevy_render/src/renderer/graph_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,18 @@ impl RenderGraphRunner {
Self::run_graph(graph, None, &mut render_context, world, &[], None)?;
finalizer(render_context.command_encoder());

{
let (render_device, mut statistics_recorder) = {
#[cfg(feature = "trace")]
let _span = info_span!("submit_graph_commands").entered();
queue.submit(render_context.finish());
}

let (commands, render_device, statistics_recorder) = render_context.finish();
queue.submit(commands);

(render_device, statistics_recorder)
};

let render_statistics_mutex = world.resource::<RenderStatisticsMutex>().0.clone();
let statistics_recorder = render_context.download_statistics(queue, move |statistics| {
statistics_recorder.finish_frame(&render_device, move |statistics| {
*render_statistics_mutex.lock() = Some(statistics);
});

Expand Down
24 changes: 9 additions & 15 deletions crates/bevy_render/src/renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,29 +356,23 @@ impl RenderContext {
}

/// Finalizes the queue and returns the queue of [`CommandBuffer`]s.
pub fn finish(&mut self) -> Vec<CommandBuffer> {
///
/// When the render statistics become available, `statistics_callback` will be invoked.
pub fn finish(mut self) -> (Vec<CommandBuffer>, RenderDevice, StatisticsRecorder) {
let command_encoder = self.command_encoder.get_or_insert_with(|| {
self.render_device
.create_command_encoder(&wgpu::CommandEncoderDescriptor::default())
});

self.statistics_recorder
.resolve(command_encoder, &self.render_device);
self.statistics_recorder.resolve(command_encoder);

self.flush_encoder();
std::mem::take(&mut self.command_buffers)
}

/// Downloads [`RenderStatistics`] from GPU, asynchronously calling the callback
/// when the data is available. Should be caled after `finish`.
pub fn download_statistics(
mut self,
queue: &Queue,
callback: impl FnOnce(RenderStatistics) + Send + 'static,
) -> StatisticsRecorder {
self.statistics_recorder
.download(&self.render_device, queue, callback);
self.statistics_recorder
(
self.command_buffers,
self.render_device,
self.statistics_recorder,
)
}

fn flush_encoder(&mut self) {
Expand Down
Loading

0 comments on commit 2efd135

Please sign in to comment.