CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Common/GPU/Vulkan/VulkanProfiler.cpp
Views: 1401
1
#include <stdarg.h>
2
3
#include "VulkanProfiler.h"
4
#include "VulkanContext.h"
5
6
using namespace PPSSPP_VK;
7
8
void VulkanProfiler::Init(VulkanContext *vulkan) {
9
vulkan_ = vulkan;
10
11
int graphicsQueueFamilyIndex = vulkan_->GetGraphicsQueueFamilyIndex();
12
_assert_(graphicsQueueFamilyIndex >= 0);
13
14
if (queryPool_) {
15
vulkan->Delete().QueueDeleteQueryPool(queryPool_);
16
}
17
18
validBits_ = vulkan_->GetQueueFamilyProperties(graphicsQueueFamilyIndex).timestampValidBits;
19
20
if (validBits_) {
21
VkQueryPoolCreateInfo ci{ VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO };
22
ci.queryCount = MAX_QUERY_COUNT;
23
ci.queryType = VK_QUERY_TYPE_TIMESTAMP;
24
vkCreateQueryPool(vulkan->GetDevice(), &ci, nullptr, &queryPool_);
25
}
26
}
27
28
void VulkanProfiler::Shutdown() {
29
if (queryPool_) {
30
vulkan_->Delete().QueueDeleteQueryPool(queryPool_);
31
}
32
}
33
34
void VulkanProfiler::BeginFrame(VulkanContext *vulkan, VkCommandBuffer firstCommandBuf) {
35
if (!validBits_) {
36
return;
37
}
38
39
vulkan_ = vulkan;
40
41
// Check for old queries belonging to this frame context that we can log out - these are now
42
// guaranteed to be done.
43
if (numQueries_ > 0) {
44
std::vector<uint64_t> results(numQueries_);
45
vkGetQueryPoolResults(vulkan->GetDevice(), queryPool_, 0, numQueries_, sizeof(uint64_t) * numQueries_, results.data(), sizeof(uint64_t), VK_QUERY_RESULT_64_BIT);
46
47
double timestampConversionFactor = (double)vulkan_->GetPhysicalDeviceProperties().properties.limits.timestampPeriod * (1.0 / 1000000.0);
48
uint64_t timestampDiffMask = validBits_ == 64 ? 0xFFFFFFFFFFFFFFFFULL : ((1ULL << validBits_) - 1);
49
50
static const char * const indent[4] = { "", " ", " ", " " };
51
52
if (!scopes_.empty()) {
53
INFO_LOG(Log::G3D, "Profiling events this frame:");
54
}
55
56
// Log it all out.
57
for (auto &scope : scopes_) {
58
if (scope.endQueryId == -1) {
59
WARN_LOG(Log::G3D, "Unclosed scope: %s", scope.name);
60
continue;
61
}
62
uint64_t startTime = results[scope.startQueryId];
63
uint64_t endTime = results[scope.endQueryId];
64
65
uint64_t delta = (endTime - startTime) & timestampDiffMask;
66
67
double milliseconds = (double)delta * timestampConversionFactor;
68
69
INFO_LOG(Log::G3D, "%s%s (%0.3f ms)", indent[scope.level & 3], scope.name, milliseconds);
70
}
71
72
scopes_.clear();
73
scopeStack_.clear();
74
}
75
76
// Only need to reset all on the first frame.
77
if (firstFrame_) {
78
numQueries_ = MAX_QUERY_COUNT;
79
firstFrame_ = false;
80
}
81
if (numQueries_ > 0) {
82
vkCmdResetQueryPool(firstCommandBuf, queryPool_, 0, numQueries_);
83
}
84
numQueries_ = 0;
85
}
86
87
void VulkanProfiler::Begin(VkCommandBuffer cmdBuf, VkPipelineStageFlagBits stageFlags, const char *fmt, ...) {
88
if (!validBits_ || (enabledPtr_ && !*enabledPtr_) || numQueries_ >= MAX_QUERY_COUNT - 1) {
89
return;
90
}
91
92
ProfilerScope scope;
93
va_list args;
94
va_start(args, fmt);
95
vsnprintf(scope.name, sizeof(scope.name), fmt, args);
96
va_end(args);
97
scope.startQueryId = numQueries_;
98
scope.endQueryId = -1;
99
scope.level = (int)scopeStack_.size();
100
101
scopeStack_.push_back(scopes_.size());
102
scopes_.push_back(scope);
103
104
vkCmdWriteTimestamp(cmdBuf, stageFlags, queryPool_, numQueries_);
105
numQueries_++;
106
}
107
108
void VulkanProfiler::End(VkCommandBuffer cmdBuf, VkPipelineStageFlagBits stageFlags) {
109
if (!validBits_ || (enabledPtr_ && !*enabledPtr_) || numQueries_ >= MAX_QUERY_COUNT - 1) {
110
return;
111
}
112
113
size_t scopeId = scopeStack_.back();
114
scopeStack_.pop_back();
115
116
ProfilerScope &scope = scopes_[scopeId];
117
scope.endQueryId = numQueries_;
118
119
vkCmdWriteTimestamp(cmdBuf, stageFlags, queryPool_, numQueries_);
120
numQueries_++;
121
}
122
123