-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gpu profiler #3998
Closed
Closed
gpu profiler #3998
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
use std::sync::{Arc, Mutex, MutexGuard}; | ||
|
||
use bevy_diagnostic::{Diagnostic, DiagnosticId, Diagnostics}; | ||
use bevy_ecs::system::{Res, ResMut}; | ||
use bevy_utils::HashMap; | ||
use wgpu::{CommandEncoder, Queue}; | ||
|
||
use crate::renderer::RenderDevice; | ||
|
||
pub struct GpuProfilerInner { | ||
profiler: wgpu_profiler::GpuProfiler, | ||
diagnostic_ids: HashMap<String, DiagnosticId>, | ||
} | ||
|
||
pub struct GpuProfileScope<'a>(MutexGuard<'a, GpuProfilerInner>); | ||
|
||
impl<'a> GpuProfileScope<'a> { | ||
pub fn end_scope(mut self, encoder: &mut CommandEncoder) { | ||
self.0.profiler.end_scope(encoder); | ||
} | ||
} | ||
|
||
#[derive(Clone)] | ||
pub struct GpuProfiler(Arc<Mutex<GpuProfilerInner>>); | ||
|
||
impl GpuProfiler { | ||
pub fn new(queue: &Queue) -> Self { | ||
let profiler = GpuProfiler(Arc::new(Mutex::new(GpuProfilerInner { | ||
profiler: wgpu_profiler::GpuProfiler::new(4, queue.get_timestamp_period()), | ||
diagnostic_ids: HashMap::default(), | ||
}))); | ||
profiler | ||
} | ||
} | ||
|
||
impl GpuProfiler { | ||
pub fn begin_scope<'a>( | ||
&'a self, | ||
label: &str, | ||
encoder: &mut CommandEncoder, | ||
device: &RenderDevice, | ||
) -> GpuProfileScope<'a> { | ||
let mut scope = self.0.lock().unwrap(); | ||
scope | ||
.profiler | ||
.begin_scope(label, encoder, device.wgpu_device()); | ||
GpuProfileScope::<'a>(scope) | ||
} | ||
|
||
pub fn resolve_queries(&self, encoder: &mut CommandEncoder) { | ||
let profiler = &mut self.0.lock().unwrap().profiler; | ||
profiler.resolve_queries(encoder); | ||
} | ||
|
||
pub fn end_frame(&self, diagnostics: &mut Diagnostics) { | ||
let profiler = &mut self.0.lock().unwrap(); | ||
profiler.profiler.end_frame().unwrap_or_default(); | ||
if let Some(results) = profiler.profiler.process_finished_frame() { | ||
for result in results { | ||
let id = match profiler.diagnostic_ids.get(&result.label) { | ||
Some(id) => *id, | ||
None => { | ||
let diagnostic_id = DiagnosticId::default(); | ||
profiler | ||
.diagnostic_ids | ||
.insert(result.label.clone(), diagnostic_id); | ||
let diagnostic = | ||
Diagnostic::new(diagnostic_id, result.label, 600).with_suffix("ms"); | ||
diagnostics.add(diagnostic); | ||
diagnostic_id | ||
} | ||
}; | ||
diagnostics.add_measurement(id, (result.time.end - result.time.start) * 1000.0); | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub fn gpu_profiler_system(profiler: Res<GpuProfiler>, mut diagnostics: ResMut<Diagnostics>) { | ||
profiler.end_frame(diagnostics.as_mut()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this will fail on platforms that don't support timestamp queries. I don't know if I think that should be the preferred behaviour rather than silently not working.