-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpu_profiler.rs
177 lines (151 loc) · 4.58 KB
/
gpu_profiler.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use std::iter;
use ash::vk;
use log::trace;
use crate::vk_ctx::VkCtx;
pub type ScopeId = u32;
/// Name of the scope and timing in milliseconds.
pub type TimesScope = (String, f32);
/// List of scopes and their times
pub type GpuProfilerReport = Vec<TimesScope>;
#[derive(Debug)]
struct ProfilerScope {
name: String,
}
/// Big amount of queries to never have to carry about it
const MAX_QUERY_COUNT: u32 = 1024;
/// Each pass has BEGIN and END timestamp query
const QUERIES_PER_PASS: u32 = 2;
const TOTAL_MAX_QUERIES: u32 = MAX_QUERY_COUNT * QUERIES_PER_PASS;
/// Magic value to disable recording the timestamps for this scope
const SCOPE_ID_IGNORED: ScopeId = MAX_QUERY_COUNT * 10;
/// ### Docs
/// - https://nikitablack.github.io/post/how_to_use_vulkan_timestamp_queries/ <- very good!
/// - https://docs.vulkan.org/spec/latest/chapters/queries.html#queries-timestamps
///
/// ### References
/// - https://github.com/h3r2tic/gpu-profiler/blob/main/src/shared.rs
pub struct GpuProfiler {
enabled: bool,
query_pool: vk::QueryPool,
scopes: Vec<ProfilerScope>,
last_report: Option<GpuProfilerReport>,
}
impl GpuProfiler {
pub fn new(vk_ctx: &VkCtx) -> Self {
let device = vk_ctx.vk_device();
let pool_info = vk::QueryPoolCreateInfo::builder()
.query_type(vk::QueryType::TIMESTAMP)
.query_count(TOTAL_MAX_QUERIES);
let query_pool = unsafe {
device
.create_query_pool(&pool_info, None)
.expect("Failed to create query pool")
};
Self {
enabled: false,
query_pool,
scopes: Vec::new(),
last_report: None,
}
}
pub unsafe fn destroy(&mut self, device: &ash::Device) {
device.destroy_query_pool(self.query_pool, None);
}
pub fn set_enabled(&mut self, enabled: bool) {
self.enabled = enabled
}
pub fn get_last_report(&self) -> &Option<GpuProfilerReport> {
&self.last_report
}
/// Start profiling - will be very slow as we wait after each frame to readback the results
pub fn begin_frame(&mut self, device: &ash::Device, command_buffer: vk::CommandBuffer) {
self.scopes.clear();
unsafe {
device.cmd_reset_query_pool(command_buffer, self.query_pool, 0, TOTAL_MAX_QUERIES);
}
}
/// End profiling and wait for result.
pub fn end_frame(&mut self, device: &ash::Device) {
if self.enabled {
self.last_report = self.create_queries_report(device);
}
}
pub fn begin_scope(
&mut self,
device: &ash::Device,
command_buffer: vk::CommandBuffer,
name: &str,
) -> ScopeId {
if !self.enabled {
return SCOPE_ID_IGNORED;
}
let query_id: u32 = self.scopes.len() as _;
self.scopes.push(ProfilerScope {
name: name.to_string(),
});
unsafe {
device.cmd_write_timestamp(
command_buffer,
vk::PipelineStageFlags::BOTTOM_OF_PIPE,
self.query_pool,
query_id * QUERIES_PER_PASS,
);
}
query_id
}
pub fn end_scope(
&self,
device: &ash::Device,
command_buffer: vk::CommandBuffer,
scope_id: ScopeId,
) {
if !self.enabled || scope_id == SCOPE_ID_IGNORED {
return;
}
let query_id: u32 = scope_id;
unsafe {
device.cmd_write_timestamp(
command_buffer,
vk::PipelineStageFlags::BOTTOM_OF_PIPE,
self.query_pool,
query_id * QUERIES_PER_PASS + 1,
);
}
}
fn create_queries_report(&self, device: &ash::Device) -> Option<GpuProfilerReport> {
if self.scopes.is_empty() {
trace!("No profiling scopes added to the frame");
return None;
}
trace!("Reading profiling result for {:?}", self.scopes);
let scopes_count = self.scopes.len() as u32;
let query_count = scopes_count * QUERIES_PER_PASS;
let mut durations: Vec<u64> = iter::repeat(0u64).take(query_count as _).collect();
unsafe {
device
.get_query_pool_results(
self.query_pool,
0,
query_count,
durations.as_mut_slice(),
vk::QueryResultFlags::TYPE_64 | vk::QueryResultFlags::WAIT,
)
.expect("Failed to get profiler results")
};
let report: GpuProfilerReport = self
.scopes
.iter()
.enumerate()
.map(|(scope_idx, scope)| {
let base_query = scope_idx * (QUERIES_PER_PASS as usize);
let time_start = durations[base_query];
let time_end = durations[base_query + 1];
let duration_nano = time_end - time_start;
let duration_ms = (duration_nano as f32) * 0.000001;
// trace!("'{}' took {:.2}ms", scope.name, duration_ms);
(scope.name.clone(), duration_ms)
})
.collect();
Some(report)
}
}