-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhda_instancegpu.cpp
More file actions
431 lines (361 loc) · 13.7 KB
/
Copy pathhda_instancegpu.cpp
File metadata and controls
431 lines (361 loc) · 13.7 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
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
#include "hda_instancegpu.hpp"
#include <cstring>
#include <iostream>
#include <set>
#include <unordered_set>
#include <vector>
using namespace std;
HdaInstanceGpu::HdaInstanceGpu(HdaWindow& window) : window{window} {
//cout << "HdaInstanceGpu(): constructor\n";
cout << ". ";
}
HdaInstanceGpu::~HdaInstanceGpu() {
cout << "HdaInstaceGpu: Destructor\n";
if (eCh == 1)
device.destroy(renderpass);
device.destroy();
instance.destroy(winSurface);
instance.destroy();
}
void HdaInstanceGpu::initGpu() {
instanceInit();
createWinSurface();
findPhysDevice();
deviceInit();
renderpassInit();
}
/*
*
* Instance initialization.
*
* The initialization procedure is inspired by Jan Peèiva's tutorial:
* https://www.root.cz/serialy/tutorial-vulkan/
*
*/
void HdaInstanceGpu::instanceInit() {
// Vulkan version
// get function pointer (dynamic)
auto vkEnumerateInstanceVersion = reinterpret_cast<PFN_vkEnumerateInstanceVersion>(vkGetInstanceProcAddr(nullptr, "vkEnumerateInstanceVersion"));
if (vkEnumerateInstanceVersion) {
uint32_t vkversion;
vkEnumerateInstanceVersion(&vkversion); // in vkEnumerateInstanceVersion is pointer to function, so we can use it this way
cout << "[INFO]: Vulkan instance version: " << VK_VERSION_MAJOR(vkversion) << "." << VK_VERSION_MINOR(vkversion) << "." << VK_VERSION_PATCH(vkversion) << endl;
}
else
cout << "[INFO]: Vulkan instance version: 1.0\n";
std::cout << "instanceInit(): Initialization started.\n";
// get glfw extensions for create vulkan instance
uint32_t glfwExtensionCount = 0;
const char** glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
checkExtensionSupport(glfwExtensions, glfwExtensionCount);
// Vulkan instance
instance =
vk::createInstance(
vk::InstanceCreateInfo{
vk::InstanceCreateFlags(), // flags
&(const vk::ApplicationInfo&)vk::ApplicationInfo{
"HGDA - HDR Demo App", // application name
VK_MAKE_VERSION(1,0,0), // application version
nullptr, // engine name
VK_MAKE_VERSION(1,0,0), // engine version
VK_API_VERSION_1_0, // api version
},
0, // enabled layer count
nullptr, // enabled layer names
glfwExtensionCount, // enabled extension count
glfwExtensions, // enabled extension names
}
);
std::cout << "instanceInit(): Initialization end.\n";
}
/*
*
* Selection of suitable equipment based on criteria.
*
*/
void HdaInstanceGpu::findPhysDevice() {
cout << "findPhysDevice(): Enumerate physical devices and check which is suitable for program requirements started.\n";
vector<vk::PhysicalDevice> devList = instance.enumeratePhysicalDevices();
// if no device is found that supports Vulkan, there is no point in continuing
if (devList.empty()) {
throw std::exception("Not found any Vulkan supported physical devices!\n");
}
uint32_t tmpcheck = 0;
// iterating trough phys devices in device list and check which is suitable for program requirements
cout << "Available physical devices:\n";
for (vk::PhysicalDevice physdev : devList) {
if (isSuitable(physdev)) {
cout << "isSuitable(): Device rating.\n";
/*if (physdev.getProperties().deviceType == vk::PhysicalDeviceType::eDiscreteGpu) {
get<3>(suitablePhysDevices.back()) = 100; // score +100
}
else if (physdev.getProperties().deviceType == vk::PhysicalDeviceType::eIntegratedGpu) {
get<3>(suitablePhysDevices.back()) = 50; // score +50
}*/
// print surface formats
cout << "Surface formats:" << endl;
vector<vk::SurfaceFormatKHR> availableSurfaceFormats = physdev.getSurfaceFormatsKHR(winSurface);
if (availableSurfaceFormats.size() == 0)
throw std::runtime_error("The list of surface formats is empty.\n");
for (vk::SurfaceFormatKHR sf : availableSurfaceFormats) {
cout << vk::to_string(sf.format) << endl;
if (sf.format == vk::Format::eR16G16B16A16Sfloat && sf.colorSpace == vk::ColorSpaceKHR::eSrgbNonlinear) {
get<4>(suitablePhysDevices.back()) = sf;
get<3>(suitablePhysDevices.back()) = 100; // score +100
break;
}
else if (sf.format == vk::Format::eA2B10G10R10UnormPack32 && sf.colorSpace == vk::ColorSpaceKHR::eSrgbNonlinear) {
get<4>(suitablePhysDevices.back()) = sf;
get<3>(suitablePhysDevices.back()) = 150; // score +100
tmpcheck = 1;
}
else if (sf.format == vk::Format::eB8G8R8A8Srgb && sf.colorSpace == vk::ColorSpaceKHR::eSrgbNonlinear) {
if (tmpcheck != 1) {
get<4>(suitablePhysDevices.back()) = sf;
tmpcheck = 1;
}
}
}
}
}
if (suitablePhysDevices.empty())
throw std::runtime_error("Not found any supported physical devices!\n");
uint32_t tmpScore = 0;
for (const auto& spd : suitablePhysDevices) {
if (get<3>(spd) > tmpScore) {
tmpScore = get<3>(spd);
physDevice = get<0>(spd);
presentationQueueFamily = get<1>(spd);
graphicsQueueFamily = get<2>(spd);
surfaceFormat = get<4>(spd);
}
}
// HADRCODED SURFACE FORMAT - KEEP COMMENTED
//surfaceFormat.format = vk::Format::eB8G8R8A8Srgb;
//surfaceFormat.colorSpace = vk::ColorSpaceKHR::eSrgbNonlinear;
cout << "Selected physical device: " << physDevice.getProperties().deviceName << std::endl;
cout << "Selected surfaceFormat:" << vk::to_string(surfaceFormat.format) << " / " << vk::to_string(surfaceFormat.colorSpace) << endl;
cout << "findPhysDevice(): Function done.\n";
}
/*
*
* Create a surface for drawing into the window.
*
*/
void HdaInstanceGpu::createWinSurface() {
window.createWindowSurface(instance, &winSurface);
}
/*
*
* A function that controls support for the extension.
*
*/
void HdaInstanceGpu::checkExtensionSupport(const char** glfwExts, uint32_t exGlfwCount) {
uint32_t extCount = 0;
vkEnumerateInstanceExtensionProperties(nullptr, &extCount, nullptr);
std::vector<VkExtensionProperties> extensions(extCount);
vkEnumerateInstanceExtensionProperties(nullptr, &extCount, extensions.data());
//std::cout << "available extensions:" << std::endl;
std::unordered_set<std::string> available;
for (const auto& extension : extensions) {
std::cout << "\t" << extension.extensionName << std::endl;
available.insert(extension.extensionName);
}
// creates a vector that reconstructs the vector according to the specified range
std::vector<const char*> glfwExtensions(glfwExts, glfwExts + exGlfwCount);
//std::cout << "required extensions:" << std::endl;
for (const auto& required : glfwExtensions) {
//std::cout << "\t" << required << std::endl;
if (available.find(required) == available.end()) {
throw std::runtime_error("The required glfw extension is missing.\n");
}
}
}
/*
*
* The function of selecting a suitable device based on your own criteria.
*
*/
bool HdaInstanceGpu::isSuitable(vk::PhysicalDevice physdev) {
bool swapchainExtCheck = false;
// check for swapchain extension
auto extList = physdev.enumerateDeviceExtensionProperties();
for (vk::ExtensionProperties& extProp : extList) {
if (strcmp(extProp.extensionName, "VK_KHR_swapchain") == 0) {
swapchainExtCheck = true;
}
}
if (!swapchainExtCheck)
return false;
if (!physdev.getFeatures().samplerAnisotropy)
return false;
vk::SurfaceFormatKHR surformat;
uint32_t graphicsFamily = UINT32_MAX;
uint32_t presentationFamily = UINT32_MAX;
vector<vk::QueueFamilyProperties> queueFamilyList = physdev.getQueueFamilyProperties();
for (uint32_t index = 0, listsize = uint32_t(queueFamilyList.size()); index < listsize; index++) {
// presentation support
if (physdev.getSurfaceSupportKHR(index, winSurface)) {
// graphics queue
if (queueFamilyList[index].queueFlags & vk::QueueFlagBits::eGraphics) {
suitablePhysDevices.emplace_back(physdev, index, index, 10, surformat);
return true;
}
else {
// presentation queue
if (presentationFamily == UINT32_MAX) {
presentationFamily = index;
}
}
}
else {
if (queueFamilyList[index].queueFlags & vk::QueueFlagBits::eGraphics)
if (graphicsFamily == UINT32_MAX) {
graphicsFamily = index;
}
}
if (graphicsFamily != UINT32_MAX && presentationFamily != UINT32_MAX) {
suitablePhysDevices.emplace_back(physdev, presentationFamily, graphicsFamily, 10, surformat);
return true;
}
}
return false;
}
/*
*
* Tøída reprezentující celek okenního systému.
*
*/
void HdaInstanceGpu::deviceInit() {
cout << "deviceInit(): Logical device initialization started.\n";
array<const char*, 1> devExt = { VK_KHR_SWAPCHAIN_EXTENSION_NAME };
vk::PhysicalDeviceFeatures devFeatures{};
devFeatures.samplerAnisotropy = VK_TRUE;
// create device
device =
physDevice.createDevice(
vk::DeviceCreateInfo{
vk::DeviceCreateFlags(), // flags
graphicsQueueFamily == presentationQueueFamily ? uint32_t(1) : uint32_t(2), // queueCreateInfoCount
array{ // pQueueCreateInfos
vk::DeviceQueueCreateInfo{
vk::DeviceQueueCreateFlags(), // flags
graphicsQueueFamily, // queueFamilyIndex
1, // queueCount
&(const float&)1.f, // pQueuePriorities
},
vk::DeviceQueueCreateInfo{
vk::DeviceQueueCreateFlags(),
presentationQueueFamily,
1,
&(const float&)1.f,
},
}.data(),
0, nullptr, // no layers
1, devExt.data(), // number of enabled extensions, enabled extension names
&devFeatures // enabled features
}
);
cout << "deviceInit(): Logical device initialization end.\n";
// get queues - graphicsQueueFamily is index into choosen family
graphicsQueue = device.getQueue(graphicsQueueFamily, 0);
presentationQueue = device.getQueue(presentationQueueFamily, 0);
}
/*
*
* Format selection function for the image.
*
*/
vk::Format HdaInstanceGpu::findFormat(vk::ImageTiling tiling) {
vector<vk::Format> requiredFormats = { vk::Format::eD32Sfloat, vk::Format::eD32SfloatS8Uint, vk::Format::eD24UnormS8Uint };
for (vk::Format format : requiredFormats) {
vk::FormatProperties props;
physDevice.getFormatProperties(format, &props);
if (tiling == vk::ImageTiling::eLinear && (props.linearTilingFeatures & vk::FormatFeatureFlagBits::eDepthStencilAttachment) == vk::FormatFeatureFlagBits::eDepthStencilAttachment) {
return format;
}
else if (tiling == vk::ImageTiling::eOptimal && (props.optimalTilingFeatures & vk::FormatFeatureFlagBits::eDepthStencilAttachment) == vk::FormatFeatureFlagBits::eDepthStencilAttachment) {
return format;
}
}
throw std::runtime_error("Required format not found!\n");
}
/*
*
* Method for initializing render pass - Vulkan object.
*
*/
void HdaInstanceGpu::renderpassInit() {
eCh = 1;
// render pass
renderpass =
device.createRenderPass(
vk::RenderPassCreateInfo(
vk::RenderPassCreateFlags(), // flags
2, // attachmentCount
array{ // pAttachments
vk::AttachmentDescription(
vk::AttachmentDescriptionFlags(), // flags
surfaceFormat.format, // format
vk::SampleCountFlagBits::e1, // samples
vk::AttachmentLoadOp::eClear, // loadOp
vk::AttachmentStoreOp::eStore, // storeOp
vk::AttachmentLoadOp::eDontCare, // stencilLoadOp
vk::AttachmentStoreOp::eDontCare, // stencilStoreOp
vk::ImageLayout::eUndefined, // initialLayout
vk::ImageLayout::ePresentSrcKHR // finalLayout
),
vk::AttachmentDescription(
vk::AttachmentDescriptionFlags(), // flags
findFormat(vk::ImageTiling::eOptimal), // format
vk::SampleCountFlagBits::e1, // samples
vk::AttachmentLoadOp::eClear, // loadOp
vk::AttachmentStoreOp::eDontCare, // storeOp
vk::AttachmentLoadOp::eDontCare, // stencilLoadOp
vk::AttachmentStoreOp::eDontCare, // stencilStoreOp
vk::ImageLayout::eUndefined, // initialLayout
vk::ImageLayout::eDepthStencilAttachmentOptimal // finalLayout
),
}.data(),
1, // subpassCount
array{ // pSubpasses
vk::SubpassDescription(
vk::SubpassDescriptionFlags(), // flags
vk::PipelineBindPoint::eGraphics, // pipelineBindPoint
0, // inputAttachmentCount
nullptr, // pInputAttachments
1, // colorAttachmentCount
array{ // pColorAttachments
vk::AttachmentReference(
0, // attachment
vk::ImageLayout::eColorAttachmentOptimal // layout
),
}.data(),
nullptr, // pResolveAttachments
array{
vk::AttachmentReference(
1, // attachment
vk::ImageLayout::eDepthStencilAttachmentOptimal // layout
),
}.data(), // pDepthStencilAttachment
0, // preserveAttachmentCount
nullptr // pPreserveAttachments
),
}.data(),
1, // dependencyCount
array{ // pDependencies
vk::SubpassDependency(
VK_SUBPASS_EXTERNAL, // srcSubpass
0, // dstSubpass
vk::PipelineStageFlags(vk::PipelineStageFlagBits::eColorAttachmentOutput | vk::PipelineStageFlagBits::eEarlyFragmentTests), // srcStageMask
vk::PipelineStageFlags(vk::PipelineStageFlagBits::eColorAttachmentOutput | vk::PipelineStageFlagBits::eEarlyFragmentTests), // dstStageMask
vk::AccessFlags(), // srcAccessMask
vk::AccessFlags(vk::AccessFlagBits::eColorAttachmentWrite | // vk::AccessFlagBits::eColorAttachmentRead |
vk::AccessFlagBits::eDepthStencilAttachmentWrite), // dstAccessMask
vk::DependencyFlags() // dependencyFlags
),
}.data()
)
);
cout << "renderpassInit(): Renderpass is created.\n";
}