Repository navigation
Expand file tree
/
Copy pathRenderProfiler.cpp
More file actions
205 lines (184 loc) · 4.41 KB
/
Copy pathRenderProfiler.cpp
File metadata and controls
205 lines (184 loc) · 4.41 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "RenderProfiler.h"
#include "RendererComponent.h"
#include <deki/Object.h>
#include <algorithm>
#include <map>
#include <utility>
namespace DekiRendering
{
namespace RenderProfiler
{
namespace
{
Clock s_Clock = nullptr;
std::map<uint32_t, Entry> s_Entries;
std::map<uint32_t, PathEntry> s_Paths;
// Keyed by the component (or pass) and its object: a pass draws many.
std::map<std::pair<const void*, const void*>, ObjectEntry> s_Objects;
uint32_t s_Frames = 0;
std::string PathOf(const Deki::Object* object)
{
std::string path;
for (const Deki::Object* o = object; o; o = o->GetParent())
{
path = path.empty() ? o->GetName() : o->GetName() + "/" + path;
}
return path;
}
// The row for `renderer`, made the first time it is met.
ObjectEntry& RowOf(const RendererComponent* renderer)
{
const Deki::Object* owner = renderer->GetOwner();
ObjectEntry& e = s_Objects[{ renderer, owner }];
if (!e.type)
{
const Deki::ComponentTypeInfo* info = renderer->GetTypeInfo();
e.type = info && info->shortName ? info->shortName : "Renderer";
e.path = PathOf(owner);
}
e.order = renderer->sortingOrder;
return e;
}
} // namespace
void Enable(Clock clock)
{
s_Clock = clock;
s_Entries.clear();
s_Paths.clear();
s_Objects.clear();
s_Frames = 0;
}
bool IsEnabled()
{
return s_Clock != nullptr;
}
uint64_t Now()
{
return s_Clock ? s_Clock() : 0;
}
void AddContent(uint32_t typeId, uint64_t us, const char* name)
{
Entry& e = s_Entries[typeId];
e.typeId = typeId;
if (name)
{
e.name = name;
}
e.contentUs += us;
++e.draws;
}
void AddBlit(uint32_t typeId, uint64_t us, uint64_t pixels, const char* name)
{
Entry& e = s_Entries[typeId];
e.typeId = typeId;
if (name)
{
e.name = name;
}
e.blitUs += us;
e.pixels += pixels;
}
void CountFrame()
{
if (s_Clock)
{
++s_Frames;
}
}
std::vector<Entry> Take(uint32_t& frames)
{
std::vector<Entry> out;
out.reserve(s_Entries.size());
for (const auto& [id, entry] : s_Entries)
{
out.push_back(entry);
}
std::sort(out.begin(), out.end(),
[](const Entry& a, const Entry& b) { return a.contentUs + a.blitUs > b.contentUs + b.blitUs; });
frames = s_Frames;
s_Entries.clear();
s_Frames = 0;
return out;
}
void AddPath(uint32_t path, const char* name, uint64_t us, uint64_t pixels)
{
PathEntry& e = s_Paths[path];
e.name = name;
++e.blits;
e.pixels += pixels;
e.us += us;
}
std::vector<PathEntry> TakePaths()
{
std::vector<PathEntry> out;
out.reserve(s_Paths.size());
for (const auto& [id, entry] : s_Paths)
{
out.push_back(entry);
}
std::sort(out.begin(), out.end(), [](const PathEntry& a, const PathEntry& b) { return a.us > b.us; });
s_Paths.clear();
return out;
}
void AddObjectMeasure(const RendererComponent* renderer, uint64_t us)
{
if (s_Clock && renderer)
{
RowOf(renderer).measureUs += us;
}
}
void AddObjectDraw(const RendererComponent* renderer, uint64_t contentUs, uint64_t blitUs, uint64_t pixels)
{
if (!s_Clock || !renderer)
{
return;
}
ObjectEntry& e = RowOf(renderer);
e.contentUs += contentUs;
e.blitUs += blitUs;
e.pixels += pixels;
if (blitUs > 0 || pixels > 0)
{
++e.draws;
}
}
void AddObjectRedraw(const RendererComponent* renderer, Redraw why)
{
if (s_Clock && renderer)
{
++RowOf(renderer).redraws[static_cast<int>(why)];
}
}
void AddObjectPass(const void* pass, const char* passName, const Deki::Object* object, uint64_t us)
{
if (!s_Clock || !pass)
{
return;
}
ObjectEntry& e = s_Objects[{ pass, object }];
if (!e.type)
{
e.type = passName ? passName : "Render pass";
e.path = PathOf(object);
}
e.contentUs += us;
++e.draws;
}
std::vector<ObjectEntry> TakeObjects()
{
std::vector<ObjectEntry> out;
out.reserve(s_Objects.size());
for (auto& [key, entry] : s_Objects)
{
out.push_back(std::move(entry));
}
std::sort(out.begin(), out.end(),
[](const ObjectEntry& a, const ObjectEntry& b)
{
return a.measureUs + a.contentUs + a.blitUs > b.measureUs + b.contentUs + b.blitUs;
});
s_Objects.clear();
return out;
}
} // namespace RenderProfiler
} // namespace DekiRendering