forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgpu_tracer_vk.cc
More file actions
133 lines (117 loc) · 4.68 KB
/
gpu_tracer_vk.cc
File metadata and controls
133 lines (117 loc) · 4.68 KB
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
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "impeller/renderer/backend/vulkan/gpu_tracer_vk.h"
#include <utility>
#include "fml/trace_event.h"
#include "impeller/base/validation.h"
#include "impeller/renderer/backend/vulkan/command_buffer_vk.h"
#include "impeller/renderer/backend/vulkan/command_encoder_vk.h"
#include "impeller/renderer/backend/vulkan/context_vk.h"
#include "impeller/renderer/command_buffer.h"
namespace impeller {
static constexpr uint32_t kPoolSize = 128u;
GPUTracerVK::GPUTracerVK(const std::weak_ptr<ContextVK>& context)
: context_(context) {
auto strong_context = context_.lock();
if (!strong_context) {
return;
}
timestamp_period_ = strong_context->GetPhysicalDevice()
.getProperties()
.limits.timestampPeriod;
if (timestamp_period_ <= 0) {
// The device does not support timestamp queries.
return;
}
vk::QueryPoolCreateInfo info;
info.queryCount = kPoolSize;
info.queryType = vk::QueryType::eTimestamp;
auto [status, pool] = strong_context->GetDevice().createQueryPoolUnique(info);
if (status != vk::Result::eSuccess) {
VALIDATION_LOG << "Failed to create query pool.";
return;
}
query_pool_ = std::move(pool);
// Disable tracing in release mode.
#ifdef IMPELLER_DEBUG
valid_ = true;
#endif
}
void GPUTracerVK::RecordStartFrameTime() {
if (!valid_) {
return;
}
auto strong_context = context_.lock();
if (!strong_context) {
return;
}
auto buffer = strong_context->CreateCommandBuffer();
auto vk_trace_cmd_buffer =
CommandBufferVK::Cast(*buffer).GetEncoder()->GetCommandBuffer();
// The two commands below are executed in order, such that writeTimeStamp is
// guaranteed to occur after resetQueryPool has finished. The validation
// layer seem particularly strict, and efforts to reset the entire pool
// were met with validation errors (though seemingly correct measurements).
// To work around this, the tracer only resets the query that will be
// used next.
vk_trace_cmd_buffer.resetQueryPool(query_pool_.get(), current_index_, 1);
vk_trace_cmd_buffer.writeTimestamp(vk::PipelineStageFlagBits::eTopOfPipe,
query_pool_.get(), current_index_);
if (!buffer->SubmitCommands()) {
VALIDATION_LOG << "GPUTracerVK: Failed to record start time.";
}
// The logic in RecordEndFrameTime requires us to have recorded a pair of
// tracing events. If this method failed for any reason we need to be sure we
// don't attempt to record and read back a second value, or we will get values
// that span multiple frames.
started_frame_ = true;
}
void GPUTracerVK::RecordEndFrameTime() {
if (!valid_ || !started_frame_) {
return;
}
auto strong_context = context_.lock();
if (!strong_context) {
return;
}
started_frame_ = false;
auto last_query = current_index_;
current_index_ += 1;
auto buffer = strong_context->CreateCommandBuffer();
auto vk_trace_cmd_buffer =
CommandBufferVK::Cast(*buffer).GetEncoder()->GetCommandBuffer();
vk_trace_cmd_buffer.resetQueryPool(query_pool_.get(), current_index_, 1);
vk_trace_cmd_buffer.writeTimestamp(vk::PipelineStageFlagBits::eBottomOfPipe,
query_pool_.get(), current_index_);
// On completion of the second time stamp recording, we read back this value
// and the previous value. The difference is approximately the frame time.
const auto device_holder = strong_context->GetDeviceHolder();
if (!buffer->SubmitCommands([&, last_query,
device_holder](CommandBuffer::Status status) {
auto strong_context = context_.lock();
if (!strong_context) {
return;
}
uint64_t bits[2] = {0, 0};
auto result = device_holder->GetDevice().getQueryPoolResults(
query_pool_.get(), last_query, 2, sizeof(bits), &bits,
sizeof(int64_t), vk::QueryResultFlagBits::e64);
if (result == vk::Result::eSuccess) {
// This value should probably be available in some form besides a
// timeline event but that is a job for a future Jonah.
auto gpu_ms = (((bits[1] - bits[0]) * timestamp_period_) / 1000000);
FML_TRACE_COUNTER("flutter", "GPUTracer",
1234, // Trace Counter ID
"FrameTimeMS", gpu_ms);
}
})) {
if (!buffer->SubmitCommands()) {
VALIDATION_LOG << "GPUTracerVK failed to record frame end time.";
}
}
if (current_index_ == kPoolSize - 1) {
current_index_ = 0u;
}
}
} // namespace impeller