This repository has been archived by the owner on Jan 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
range_sample_dx11.cpp
286 lines (229 loc) · 8.46 KB
/
range_sample_dx11.cpp
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
Copyright 2015-2018 Intel Corporation
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <imgui.h>
#include <imgui_impl_dx11.h>
#include <metrics_discovery/metrics_discovery_helper.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <windows.h>
// impl_dx11.cpp:
bool InitializeGraphics(HWND hwnd);
void FinalizeGraphics();
void Resize(UINT width, UINT height);
void Render(UINT width, UINT height);
bool InitializeMetrics(MDH_Context const& mdhContext, MetricsDiscovery::IConcurrentGroup_1_0* concurrentGroup, MetricsDiscovery::IMetricSet_1_0* metricSet);
void FinalizeMetrics();
MDH_ReportValues* UpdateMetrics();
// ImGui:
extern LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
namespace {
UINT Width = 0;
UINT Height = 0;
LRESULT CALLBACK WindowProc(
HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
if (ImGui_ImplWin32_WndProcHandler(hWnd, message, wParam, lParam)) {
return true;
}
switch (message) {
case WM_SIZE:
Width = LOWORD(lParam);
Height = HIWORD(lParam);
Resize(Width, Height);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
HWND InitializeWindow(
char const* title,
uint32_t width,
uint32_t height)
{
auto hInstance = GetModuleHandle(nullptr);
WNDCLASSEX windowClass = {};
windowClass.cbSize = sizeof(WNDCLASSEX);
windowClass.style = CS_HREDRAW | CS_VREDRAW;
windowClass.lpfnWndProc = WindowProc;
windowClass.hInstance = hInstance;
windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
windowClass.lpszClassName = "range_sample_dx11_class";
RegisterClassEx(&windowClass);
RECT windowRect = {};
windowRect.right = width;
windowRect.bottom = height;
AdjustWindowRect(&windowRect, WS_OVERLAPPEDWINDOW, FALSE);
return CreateWindow(
windowClass.lpszClassName,
title,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
windowRect.right - windowRect.left,
windowRect.bottom - windowRect.top,
nullptr,
nullptr,
hInstance,
nullptr);
}
void FinalizeWindow(
HWND hwnd)
{
DestroyWindow(hwnd);
UnregisterClass("range_sample_dx11_class", NULL);
}
bool HandleWindowMessages(
HWND hwnd,
int* ret)
{
MSG msg = {};
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
if (msg.message == WM_QUIT) {
*ret = (int) msg.wParam;
return true;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return false;
}
}
int main(
int argc,
char** argv)
{
// Define default parameters
char const* concurrentGroupName = "OA";
char const* metricSetName = "RenderBasic";
// Parse command line
for (int i = 1; i < argc; ++i) {
if (strcmp(argv[i], "--mdapi") == 0) {
if (i + 2 >= argc) {
fprintf(stderr, "error: --mdapi arguments are missing\n");
} else {
concurrentGroupName = argv[++i];
metricSetName = argv[++i];
continue;
}
}
else {
fprintf(stderr, "error: unrecognized argument '%s'\n", argv[i]);
}
fprintf(stderr, "usage: range_sample_dx11 [options]\n");
fprintf(stderr, "options:\n");
fprintf(stderr, " --mdapi concurrentGroupName metricSetName\n");
fprintf(stderr, "e.g.:\n");
fprintf(stderr, " range_sample_dx11 --mdapi OA RenderBasic\n");
return 1;
}
MDH_Context mdhContext;
int ret = 1;
// Create sample window
char title[1024] = {};
snprintf(title, 1024, "MetricsDiscovery range_sample_dx11 - %s::%s",
concurrentGroupName,
metricSetName);
HWND hwnd = InitializeWindow(title, 720, 640);
if (hwnd == 0) {
fprintf(stderr, "error: failed to initialize window\n");
goto finalize;
}
// Initialize MetricsDiscoveryHelper context, and search for the specified
// metrics.
//
// NOTE: You need to initialize the MDH_Context instance before creating
// the device.
auto result = mdhContext.Initialize();
if (result != MDH_Context::RESULT_OK) {
fprintf(stderr, "error: no metrics are available\n");
goto finalize;
}
auto concurrentGroup = MDH_FindConcurrentGroup(mdhContext.MDDevice, concurrentGroupName);
if (concurrentGroup == nullptr) {
fprintf(stderr, "error: could not find concurrent group '%s'\n", concurrentGroupName);
goto finalize;
}
auto metricSet = MDH_FindMetricSet(concurrentGroup, metricSetName);
if (metricSet == nullptr) {
fprintf(stderr, "error: could not find metric set '%s'\n", metricSetName);
goto finalize;
}
// Initialize the graphics device and resources.
if (!InitializeGraphics(hwnd)) {
fprintf(stderr, "error: failed to initialize graphics device\n");
goto finalize;
}
// Initialize the metrics context for the specified metrics.
if (!InitializeMetrics(mdhContext, concurrentGroup, metricSet)) {
fprintf(stderr, "error: no metrics are available\n");
goto finalize;
}
// Show window and enter application loop
ShowWindow(hwnd, SW_SHOW);
for (;;) {
// Update metric values based on the last frame rendered.
auto mdhReportValues = UpdateMetrics();
// Handle window messages and exit on request
if (HandleWindowMessages(hwnd, &ret)) {
break;
}
// Specify the GUI (just print all the metrics in this metric set)
ImGui_ImplDX11_NewFrame();
if (mdhReportValues != nullptr) {
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.f);
ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(0.f, 0.f, 0.f, 0.0f));
ImGui::SetNextWindowPos(ImVec2(0.f, 0.f), ImGuiSetCond_Always);
ImGui::SetNextWindowSize(ImVec2((float) Width, (float) Height), ImGuiSetCond_Always);
if (ImGui::Begin("", nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove)) {
for (uint32_t i = 0, N = metricSet->GetParams()->MetricsCount; i < N; ++i) {
auto metric = metricSet->GetMetric(i);
auto metricParams = metric->GetParams();
uint32_t rangeIndex = 0;
auto typedValue = mdhReportValues->GetValue(rangeIndex, i);
float fValue = 0.f;
switch (typedValue.ValueType) {
case MetricsDiscovery::VALUE_TYPE_UINT32: fValue = (float) typedValue.ValueUInt32; break;
case MetricsDiscovery::VALUE_TYPE_UINT64: fValue = (float) typedValue.ValueUInt64; break;
case MetricsDiscovery::VALUE_TYPE_FLOAT: fValue = typedValue.ValueFloat; break;
case MetricsDiscovery::VALUE_TYPE_BOOL: fValue = typedValue.ValueBool ? 1.f : 0.f; break;
}
ImGui::Text("%s = %f %s", metricParams->ShortName, fValue, metricParams->MetricResultUnits);
}
}
ImGui::End();
ImGui::PopStyleVar();
ImGui::PopStyleColor();
}
// Render this frame
Render(Width, Height);
}
// Cleanup
finalize:
FinalizeMetrics();
mdhContext.Finalize();
FinalizeGraphics();
FinalizeWindow(hwnd);
return ret;
}