-
Notifications
You must be signed in to change notification settings - Fork 1
/
vk_ddisplay_app.cpp
433 lines (409 loc) · 14.3 KB
/
vk_ddisplay_app.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
* Copyright (c) 2019-2024, NVIDIA CORPORATION. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION
* SPDX-License-Identifier: Apache-2.0
*/
#include "vk_ddisplay_app.hpp"
#include "canvas_region_render_thread.hpp"
#include "logical_device.hpp"
#include "logical_display.hpp"
#include <backends/imgui_impl_glfw.h>
#include <imgui/backends/imgui_impl_gl.h>
#include <imgui/imgui_helper.h>
#include <json.hpp>
#include <fstream>
namespace vkdd {
VkDDisplayApp::VkDDisplayApp(vk::UniqueInstance instance)
: m_instance(std::move(instance))
, m_possibleSelections{std::make_pair(nullptr, nullptr)}
, m_activeSelectionIndex(0)
{
m_parameterList.add("config|Path to the json file containing the ddisplay configuration", &m_configPath);
m_parameterList.add("topology-only|If set, the app closes automatically after printing the system's topology",
[](uint32_t t) { exit(0); });
this->queryTolopogy();
this->setVsync(false);
}
VkDDisplayApp::~VkDDisplayApp() {}
void VkDDisplayApp::queryTolopogy()
{
// vk_ddisplay
// first we will collect the system topology
// nothe that in case of an active Mosaic configuration across multiple GPUs, the same vkDisplay will show up on multiple vkPhysicalDevices
std::vector<vk::PhysicalDeviceGroupProperties> devGroups = m_instance->enumeratePhysicalDeviceGroups();
LOGI(
"--------------------------------------------------------------------------------\n"
"System topology:\n");
for(uint32_t devGroupIdx = 0; devGroupIdx < devGroups.size(); ++devGroupIdx)
{
LOGI("device group [%d]\n", devGroupIdx);
for(uint32_t devIdx = 0; devIdx < devGroups[devGroupIdx].physicalDeviceCount; ++devIdx)
{
vk::PhysicalDevice physicalDevice = devGroups[devGroupIdx].physicalDevices[devIdx];
LOGI(" physical device [%d]: %s\n", devIdx, formatVkDeviceName(physicalDevice).c_str());
auto physicalDeviceDisplays = physicalDevice.getDisplayPropertiesKHR();
for(uint32_t dispIdx = 0; dispIdx < physicalDeviceDisplays.size(); ++dispIdx)
{
vk::DisplayPropertiesKHR dispProps = physicalDeviceDisplays[dispIdx];
auto displayInfoPred = [&](DisplayInfo const& di) { return di.m_props.display == dispProps.display; };
if(auto findIt = std::find_if(m_displayInfos.begin(), m_displayInfos.end(), displayInfoPred);
findIt != m_displayInfos.end())
{
findIt->m_physicalDeviceIndices.insert(devIdx);
}
else
{
m_displayInfos.emplace_back(DisplayInfo{dispProps, devGroupIdx, {devIdx}});
}
LOGI(" display [%d]: %s; supported display modes: ", dispIdx, dispProps.displayName);
char const* sep = "";
for(vk::DisplayModePropertiesKHR modeProps : physicalDevice.getDisplayModePropertiesKHR(dispProps.display))
{
LOGI("%s%d x %d @ %.3f Hz", sep, modeProps.parameters.visibleRegion.width,
modeProps.parameters.visibleRegion.height, 1e-3f * (float)modeProps.parameters.refreshRate);
sep = ", ";
}
LOGI("\n");
}
}
}
LOGI(
"--------------------------------------------------------------------------------\n"
"Usable displays:\n");
for(uint32_t i = 0; i < m_displayInfos.size(); ++i)
{
LOGI("[%d] %s: attached to physical device(s) { ", i, m_displayInfos[i].m_props.displayName);
char const* sep = "";
for(uint32_t devIdx : m_displayInfos[i].m_physicalDeviceIndices)
{
LOGI("%s%d", sep, devIdx);
sep = ", ";
}
LOGI(" } of device group %d.\n", m_displayInfos[i].m_deviceGroupIndex);
}
if(m_displayInfos.empty())
{
LOGE("None\n");
}
LOGI("--------------------------------------------------------------------------------\n");
}
bool VkDDisplayApp::begin()
{
ImGuiH::Init(this->getWidth(), this->getHeight(), this, ImGuiH::FONT_MONOSPACED_SCALED);
if(!ImGui_ImplGlfw_InitForOpenGL(m_internal, true))
{
LOGE("ImGui_ImplGlfw_InitForOpenGL() failed.\n");
return false;
}
ImGui::InitGL();
if(!m_configPath.empty())
{
if(!this->parseDDisplayConfig())
{
return false;
}
}
else if(!this->enableDisplay(0, {}))
{
LOGE("Default configuration failed.\n");
return false;
}
// vk_ddisplay
// when everything is set up, the rendering can be started
if(m_logicalDevices.empty())
{
LOGE("No displays were enabled.\n");
return false;
}
for(auto const& logicalDevicesIt : m_logicalDevices)
{
if(!logicalDevicesIt.second->start())
{
LOGE("Failed to start logical device.");
return false;
}
}
return true;
}
bool VkDDisplayApp::enableDisplay(uint32_t globalDisplayIndex, CanvasRegion canvasRegion)
{
if(m_displayInfos.size() <= globalDisplayIndex)
{
LOGE("Display index (%d) must be positive and less than the number of displays (%d).\n", globalDisplayIndex,
m_displayInfos.size());
return false;
}
else
{
// vk_ddisplay
// at this point an individual logical display will be activated
// one needs to find the logical device (which represents a Vulkan device group) to which the display is connected and enable it
DisplayInfo const& dispInfo = m_displayInfos[globalDisplayIndex];
LogicalDisplay* logicalDisplay =
this->getLogicalDevice(dispInfo.m_deviceGroupIndex)->enableDisplay(m_scene, dispInfo.m_props.display, canvasRegion);
if(logicalDisplay)
{
m_possibleSelections.emplace_back(std::make_pair(logicalDisplay, nullptr));
for(uint32_t i = 0; i < logicalDisplay->getNumRenderThreads(); ++i)
{
m_possibleSelections.emplace_back(std::make_pair(logicalDisplay, logicalDisplay->getRenderThread(i)));
}
return true;
}
else
{
LOGE("Failed to enable display %d.\n", globalDisplayIndex);
return false;
}
}
}
void VkDDisplayApp::think(double time)
{
this->handleInput();
static double lastTime = time;
if(!m_paused)
{
float frameTimeMillis = 1e3f * (time - lastTime);
m_scene.update(frameTimeMillis);
for(auto& logicalDeviceIt : m_logicalDevices)
{
logicalDeviceIt.second->render();
}
}
this->renderGui();
lastTime = time;
}
void VkDDisplayApp::renderGui()
{
glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ImGui::SetNextWindowSize(ImGuiH::dpiScaled(480, 0), ImGuiCond_FirstUseEver);
ImGui::SetNextWindowPos(ImGuiH::dpiScaled(20, 20), ImGuiCond_FirstUseEver);
if(ImGui::Begin("Scene", 0, ImGuiWindowFlags_NoResize))
{
ImGui::Checkbox("Pause rendering", &m_paused);
ImGui::SliderInt("Number of donuts X", &m_scene.getDesiredNumDonutsX(), 1, 48);
ImGui::SliderInt("Number of donuts Y", &m_scene.getDesiredNumDonutsY(), 1, 48);
ImGui::End();
}
int i = 0;
for(std::pair<LogicalDisplay*, CanvasRegionRenderThread*> s : m_possibleSelections)
{
if(s.second)
{
std::stringstream title;
title << "Render Thread " << i;
ImGui::SetNextWindowSize(ImGuiH::dpiScaled(480, 0), ImGuiCond_FirstUseEver);
ImGui::SetNextWindowPos(ImGuiH::dpiScaled(20, 120 + i * 70), ImGuiCond_FirstUseEver);
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, {14, 12});
if(ImGui::Begin(title.str().c_str(), 0, ImGuiWindowFlags_NoResize))
{
ImDrawList* drawList = ImGui::GetWindowDrawList();
uint32_t color = IM_COL32((uint32_t)255.0f * s.second->getLastClearColor().x,
(uint32_t)255.0f * s.second->getLastClearColor().y,
(uint32_t)255.0f * s.second->getLastClearColor().z, 255);
ImVec2 tl = {ImGui::GetWindowPos().x + ImGui::GetWindowContentRegionMin().x - 8,
ImGui::GetWindowPos().y + ImGui::GetWindowContentRegionMin().y - 8};
ImVec2 br1 = {ImGui::GetWindowPos().x + ImGui::GetWindowContentRegionMax().x, tl.y + 6};
ImVec2 br2 = {tl.x + 6, ImGui::GetWindowPos().y + ImGui::GetWindowContentRegionMax().y};
drawList->AddRectFilled(tl, br1, color);
drawList->AddRectFilled(tl, br2, color);
ImGui::SliderInt("Fur layers", &s.second->getNumFurLayers(), 1, 128);
ImGui::End();
}
ImGui::PopStyleVar();
++i;
}
}
ImGui::Render();
ImGui::RenderDrawDataGL(ImGui::GetDrawData());
ImGui::EndFrame();
}
void VkDDisplayApp::end()
{
for(auto& logicalDeviceIt : m_logicalDevices)
{
logicalDeviceIt.second->interrupt();
}
for(auto& logicalDeviceIt : m_logicalDevices)
{
logicalDeviceIt.second->join();
}
}
LogicalDevice* VkDDisplayApp::getLogicalDevice(uint32_t devGroupIdx)
{
if(auto findIt = m_logicalDevices.find(devGroupIdx); findIt != m_logicalDevices.end())
{
return findIt->second.get();
}
return m_logicalDevices.emplace(devGroupIdx, std::make_unique<LogicalDevice>(m_instance.get(), devGroupIdx))
.first->second.get();
}
void VkDDisplayApp::setActiveSelection(uint32_t activeSelectionIndex)
{
this->visitSelection([](CanvasRegionRenderThread* renderThread) { renderThread->setHighlighted(false); });
m_activeSelectionIndex = (activeSelectionIndex % m_possibleSelections.size() + (uint32_t)m_possibleSelections.size())
% m_possibleSelections.size();
if(m_activeSelectionIndex != 0)
{
this->visitSelection([](CanvasRegionRenderThread* renderThread) { renderThread->setHighlighted(true); });
}
}
void VkDDisplayApp::visitSelection(std ::function<void(CanvasRegionRenderThread*)> visitor)
{
std::pair<LogicalDisplay*, CanvasRegionRenderThread*> selection = m_possibleSelections[m_activeSelectionIndex];
if(selection.second)
{
visitor(selection.second);
}
else if(selection.first)
{
for(uint32_t i = 0; i < selection.first->getNumRenderThreads(); ++i)
{
visitor(selection.first->getRenderThread(i));
}
}
else
{
for(std::pair<LogicalDisplay*, CanvasRegionRenderThread*> s : m_possibleSelections)
{
if(s.second)
{
visitor(s.second);
}
}
}
}
bool VkDDisplayApp::parseDDisplayConfig()
{
std::ifstream configJson(m_configPath);
if(!configJson)
{
LOGE("Failed to read config json file %s.\n", m_configPath.c_str());
return false;
}
nlohmann::json config = nlohmann::json::parse(configJson);
if(!config.contains("canvas") || !config["canvas"].is_object())
{
LOGE("The config must contain a \"canvas\" object entry.\n");
return false;
}
if(!config["canvas"].contains("aspectNum") || !config["canvas"]["aspectNum"].is_number())
{
LOGE("The \"canvas\" object must define an \"aspectNum\" float value.\n");
return false;
}
if(!config["canvas"].contains("aspectDen") || !config["canvas"]["aspectDen"].is_number())
{
LOGE("The \"canvas\" object must define an \"aspectDen\" float value.\n");
return false;
}
if(!config["canvas"].contains("fov") || !config["canvas"]["fov"].is_number())
{
LOGE("The \"canvas\" object must define an \"fov\" float value.\n");
return false;
}
m_scene.setPerspectiveCamera((float)config["canvas"]["aspectNum"] / (float)config["canvas"]["aspectDen"],
Angle::degree(config["canvas"]["fov"]), 1e-2f, 1e+2f);
if(!config.contains("displays") || !config["displays"].is_array())
{
LOGE("The config must contain a \"displays\" array entry.\n");
return false;
}
for(nlohmann::json const& displayJson : config["displays"])
{
uint32_t dispIdx;
CanvasRegion canvasRegion;
if(displayJson.is_number_unsigned())
{
dispIdx = displayJson;
}
else if(displayJson.is_object())
{
if(!displayJson.contains("index") || !displayJson["index"].is_number_unsigned())
{
LOGE("Each object entry of \"displays\" must provide an positive \"index\" integer.\n");
return false;
}
dispIdx = displayJson["index"];
if(displayJson.contains("canvasOffsetX") && displayJson["canvasOffsetX"].is_number())
{
canvasRegion.m_offsetX = displayJson["canvasOffsetX"];
}
if(displayJson.contains("canvasOffsetY") && displayJson["canvasOffsetY"].is_number())
{
canvasRegion.m_offsetY = displayJson["canvasOffsetY"];
}
if(displayJson.contains("canvasWidth") && displayJson["canvasWidth"].is_number())
{
canvasRegion.m_width = displayJson["canvasWidth"];
}
if(displayJson.contains("canvasHeight") && displayJson["canvasHeight"].is_number())
{
canvasRegion.m_height = displayJson["canvasHeight"];
}
}
else
{
LOGE("All entries of the the \"displays\" array must be single unsigned integers or objects.\n");
return false;
}
this->enableDisplay(dispIdx, canvasRegion);
}
return true;
}
void VkDDisplayApp::handleInput()
{
if(m_windowState.onPress(KeyCode::KEY_RIGHT))
{
m_scene.increaseNumDonutsX();
}
if(m_windowState.onPress(KeyCode::KEY_LEFT))
{
m_scene.decreaseNumDonutsX();
}
if(m_windowState.onPress(KeyCode::KEY_UP))
{
m_scene.increaseNumDonutsY();
}
if(m_windowState.onPress(KeyCode::KEY_DOWN))
{
m_scene.decreaseNumDonutsY();
}
if(m_windowState.onPress(KeyCode::KEY_SPACE))
{
m_paused = !m_paused;
}
if(m_windowState.onPress(KeyCode::KEY_PAGE_UP))
{
this->setActiveSelection(m_activeSelectionIndex + 1);
}
if(m_windowState.onPress(KeyCode::KEY_PAGE_DOWN))
{
this->setActiveSelection(m_activeSelectionIndex - 1);
}
if(m_windowState.onPress(KeyCode::KEY_KP_ADD))
{
this->visitSelection([](CanvasRegionRenderThread* renderThread) { renderThread->incNumFurLayers(); });
}
if(m_windowState.onPress(KeyCode::KEY_KP_SUBTRACT))
{
this->visitSelection([](CanvasRegionRenderThread* renderThread) { renderThread->decNumFurLayers(); });
}
}
} // namespace vkdd