forked from oleflaten/QtVulkanApp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenderWindow.cpp
More file actions
568 lines (481 loc) · 23.6 KB
/
Copy pathRenderWindow.cpp
File metadata and controls
568 lines (481 loc) · 23.6 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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
#include "RenderWindow.h"
#include <QVulkanFunctions>
#include <QFile>
// Hardcoded mesh for now. Will be put in its own class soon!
// NB 1: Vulkan's near/far plane (Z axis) is at 0/1 instead of -1/1, as in OpenGL!
// NB 2: Vulkan Y is negated in clip space so we fix that when making the projection matrix
// **PLAY WITH THIS**
static float vertexData[] = {
// Y up, front = CCW
// X, Y, Z, R, G, B
0.0f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, //top vertex - red
-0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, //bottom left vertex - green
0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f //bottom right vertex - blue
};
//Utility variable and function for alignment:
static const int UNIFORM_DATA_SIZE = 16 * sizeof(float); //our MVP matrix contains 16 floats
static inline VkDeviceSize aligned(VkDeviceSize v, VkDeviceSize byteAlign)
{
return (v + byteAlign - 1) & ~(byteAlign - 1);
}
/*** RenderWindow class ***/
RenderWindow::RenderWindow(QVulkanWindow *w, bool msaa)
: mWindow(w)
{
if (msaa) {
const QList<int> counts = w->supportedSampleCounts();
qDebug() << "Supported sample counts:" << counts;
for (int s = 16; s >= 4; s /= 2) {
if (counts.contains(s)) {
qDebug("Requesting sample count %d", s);
mWindow->setSampleCount(s);
break;
}
}
}
}
void RenderWindow::initResources()
{
qDebug("\n ***************************** initResources ******************************************* \n");
VkDevice logicalDevice = mWindow->device();
mDeviceFunctions = mWindow->vulkanInstance()->deviceFunctions(logicalDevice);
/* Prepare the vertex and uniform data.The vertex data will never
change so one buffer is sufficient regardless of the value of
QVulkanWindow::CONCURRENT_FRAME_COUNT. Uniform data is changing per
frame however so active frames have to have a dedicated copy.
Use just one memory allocation and one buffer. We will then specify the
appropriate offsets for uniform buffers in the VkDescriptorBufferInfo.
Have to watch out for
VkPhysicalDeviceLimits::minUniformBufferOffsetAlignment, though.
The uniform buffer is not strictly required in this example, we could
have used push constants as well since our single matrix (64 bytes) fits
into the spec mandated minimum limit of 128 bytes. However, once that
limit is not sufficient, the per-frame buffers, as shown below, will
become necessary.
*/
const int concurrentFrameCount = mWindow->concurrentFrameCount(); // 2 on Oles Machine
const VkPhysicalDeviceLimits *pdevLimits = &mWindow->physicalDeviceProperties()->limits;
const VkDeviceSize uniAlign = pdevLimits->minUniformBufferOffsetAlignment;
qDebug("uniform buffer offset alignment is %u", (uint)uniAlign); //64 on Oles machine
VkBufferCreateInfo bufInfo;
memset(&bufInfo, 0, sizeof(bufInfo)); //Clear out the memory
bufInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; // Set the structure type
// Our internal layout is vertex, uniform, uniform, ... with each uniform buffer
// start offset aligned to uniAlign.
const VkDeviceSize vertexAllocSize = aligned(sizeof(vertexData), uniAlign);
const VkDeviceSize uniformAllocSize = aligned(UNIFORM_DATA_SIZE, uniAlign);
bufInfo.size = vertexAllocSize + concurrentFrameCount * uniformAllocSize; //One vertex buffer and two uniform buffers
bufInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT; // Set the usage to both vertex buffer and uniform buffer
VkResult err = mDeviceFunctions->vkCreateBuffer(logicalDevice, &bufInfo, nullptr, &mBuffer);
if (err != VK_SUCCESS)
qFatal("Failed to create buffer: %d", err);
VkMemoryRequirements memReq;
mDeviceFunctions->vkGetBufferMemoryRequirements(logicalDevice, mBuffer, &memReq);
VkMemoryAllocateInfo memAllocInfo = {
VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
nullptr,
memReq.size,
mWindow->hostVisibleMemoryIndex()
};
err = mDeviceFunctions->vkAllocateMemory(logicalDevice, &memAllocInfo, nullptr, &mBufferMemory);
if (err != VK_SUCCESS)
qFatal("Failed to allocate memory: %d", err);
err = mDeviceFunctions->vkBindBufferMemory(logicalDevice, mBuffer, mBufferMemory, 0);
if (err != VK_SUCCESS)
qFatal("Failed to bind buffer memory: %d", err);
quint8 *p;
err = mDeviceFunctions->vkMapMemory(logicalDevice, mBufferMemory, 0, memReq.size, 0, reinterpret_cast<void **>(&p));
if (err != VK_SUCCESS)
qFatal("Failed to map memory: %d", err);
memcpy(p, vertexData, sizeof(vertexData));
QMatrix4x4 ident;
memset(mUniformBufferInfo, 0, sizeof(mUniformBufferInfo));
for (int i = 0; i < concurrentFrameCount; ++i) {
const VkDeviceSize offset = vertexAllocSize + i * uniformAllocSize;
memcpy(p + offset, ident.constData(), 16 * sizeof(float));
mUniformBufferInfo[i].buffer = mBuffer;
mUniformBufferInfo[i].offset = offset;
mUniformBufferInfo[i].range = uniformAllocSize;
}
mDeviceFunctions->vkUnmapMemory(logicalDevice, mBufferMemory);
/********************************* Vertex layout: *********************************/
//The size of each vertex to be passed to the shader
VkVertexInputBindingDescription vertexBindingDesc = {
0, // binding - has to match that in VkVertexInputAttributeDescription and startNextFrame()s m_devFuncs->vkCmdBindVertexBuffers
6 * sizeof(float), // stride account for X, Y, Z, R, G, B
VK_VERTEX_INPUT_RATE_VERTEX
};
/********************************* Shader bindings: *********************************/
//Descritpion of the attributes used in the shader
VkVertexInputAttributeDescription vertexAttrDesc[] = {
{ // position
0, // location has to correspond to the layout(location = x) in the shader
0, // binding
VK_FORMAT_R32G32B32_SFLOAT,
0
},
{ // color
1, // location has to correspond to the layout(location = x) in the shader
0, // binding
VK_FORMAT_R32G32B32_SFLOAT,
3 * sizeof(float) // offset to account for X, Y, Z
}
};
VkPipelineVertexInputStateCreateInfo vertexInputInfo;
vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
vertexInputInfo.pNext = nullptr;
vertexInputInfo.flags = 0;
vertexInputInfo.vertexBindingDescriptionCount = 1;
vertexInputInfo.pVertexBindingDescriptions = &vertexBindingDesc;
vertexInputInfo.vertexAttributeDescriptionCount = 2;
vertexInputInfo.pVertexAttributeDescriptions = vertexAttrDesc;
// Set up descriptor set and its layout.
VkDescriptorPoolSize descPoolSizes = { VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, uint32_t(concurrentFrameCount) };
VkDescriptorPoolCreateInfo descPoolInfo;
memset(&descPoolInfo, 0, sizeof(descPoolInfo));
descPoolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
descPoolInfo.maxSets = concurrentFrameCount;
descPoolInfo.poolSizeCount = 1;
descPoolInfo.pPoolSizes = &descPoolSizes;
err = mDeviceFunctions->vkCreateDescriptorPool(logicalDevice, &descPoolInfo, nullptr, &mDescriptorPool);
if (err != VK_SUCCESS)
qFatal("Failed to create descriptor pool: %d", err);
/********************************* Uniform (projection matrix) bindings: *********************************/
VkDescriptorSetLayoutBinding layoutBinding = {
0, // binding
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1,
VK_SHADER_STAGE_VERTEX_BIT,
nullptr
};
VkDescriptorSetLayoutCreateInfo descLayoutInfo = {
VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
nullptr,
0,
1,
&layoutBinding
};
err = mDeviceFunctions->vkCreateDescriptorSetLayout(logicalDevice, &descLayoutInfo, nullptr, &mDescriptorSetLayout);
if (err != VK_SUCCESS)
qFatal("Failed to create descriptor set layout: %d", err);
for (int i = 0; i < concurrentFrameCount; ++i) {
VkDescriptorSetAllocateInfo descSetAllocInfo = {
VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
nullptr,
mDescriptorPool,
1,
&mDescriptorSetLayout
};
err = mDeviceFunctions->vkAllocateDescriptorSets(logicalDevice, &descSetAllocInfo, &mDescriptorSet[i]);
if (err != VK_SUCCESS)
qFatal("Failed to allocate descriptor set: %d", err);
VkWriteDescriptorSet descWrite;
memset(&descWrite, 0, sizeof(descWrite));
descWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descWrite.dstSet = mDescriptorSet[i];
descWrite.descriptorCount = 1;
descWrite.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
descWrite.pBufferInfo = &mUniformBufferInfo[i];
mDeviceFunctions->vkUpdateDescriptorSets(logicalDevice, 1, &descWrite, 0, nullptr);
}
// Pipeline cache
VkPipelineCacheCreateInfo pipelineCacheInfo;
memset(&pipelineCacheInfo, 0, sizeof(pipelineCacheInfo));
pipelineCacheInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
err = mDeviceFunctions->vkCreatePipelineCache(logicalDevice, &pipelineCacheInfo, nullptr, &mPipelineCache);
if (err != VK_SUCCESS)
qFatal("Failed to create pipeline cache: %d", err);
// Pipeline layout
VkPipelineLayoutCreateInfo pipelineLayoutInfo;
memset(&pipelineLayoutInfo, 0, sizeof(pipelineLayoutInfo));
pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
pipelineLayoutInfo.setLayoutCount = 1;
pipelineLayoutInfo.pSetLayouts = &mDescriptorSetLayout;
err = mDeviceFunctions->vkCreatePipelineLayout(logicalDevice, &pipelineLayoutInfo, nullptr, &mPipelineLayout);
if (err != VK_SUCCESS)
qFatal("Failed to create pipeline layout: %d", err);
/********************************* Create shaders *********************************/
//Creates our actuall shader modules
VkShaderModule vertShaderModule = createShader(QStringLiteral(":/color_vert.spv"));
VkShaderModule fragShaderModule = createShader(QStringLiteral(":/color_frag.spv"));
// Graphics pipeline
VkGraphicsPipelineCreateInfo pipelineInfo;
memset(&pipelineInfo, 0, sizeof(pipelineInfo));
pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
VkPipelineShaderStageCreateInfo shaderStages[2] = {
{
VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
nullptr,
0,
VK_SHADER_STAGE_VERTEX_BIT,
vertShaderModule,
"main",
nullptr
},
{
VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
nullptr,
0,
VK_SHADER_STAGE_FRAGMENT_BIT,
fragShaderModule,
"main",
nullptr
}
};
pipelineInfo.stageCount = 2;
pipelineInfo.pStages = shaderStages;
pipelineInfo.pVertexInputState = &vertexInputInfo;
VkPipelineInputAssemblyStateCreateInfo ia;
memset(&ia, 0, sizeof(ia));
ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
pipelineInfo.pInputAssemblyState = &ia;
// The viewport and scissor will be set dynamically via vkCmdSetViewport/Scissor.
// This way the pipeline does not need to be touched when resizing the window.
VkPipelineViewportStateCreateInfo vp;
memset(&vp, 0, sizeof(vp));
vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
vp.viewportCount = 1;
vp.scissorCount = 1;
pipelineInfo.pViewportState = &vp;
VkPipelineRasterizationStateCreateInfo rs;
memset(&rs, 0, sizeof(rs));
rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
rs.polygonMode = VK_POLYGON_MODE_FILL;
rs.cullMode = VK_CULL_MODE_NONE; // we want the back face as well
rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
rs.lineWidth = 1.0f;
pipelineInfo.pRasterizationState = &rs;
VkPipelineMultisampleStateCreateInfo ms;
memset(&ms, 0, sizeof(ms));
ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
// Enable multisampling.
ms.rasterizationSamples = mWindow->sampleCountFlagBits();
pipelineInfo.pMultisampleState = &ms;
VkPipelineDepthStencilStateCreateInfo ds;
memset(&ds, 0, sizeof(ds));
ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
ds.depthTestEnable = VK_TRUE;
ds.depthWriteEnable = VK_TRUE;
ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
pipelineInfo.pDepthStencilState = &ds;
VkPipelineColorBlendStateCreateInfo cb;
memset(&cb, 0, sizeof(cb));
cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
// no blend, write out all of rgba
VkPipelineColorBlendAttachmentState att;
memset(&att, 0, sizeof(att));
att.colorWriteMask = 0xF;
cb.attachmentCount = 1;
cb.pAttachments = &att;
pipelineInfo.pColorBlendState = &cb;
VkDynamicState dynEnable[] = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR };
VkPipelineDynamicStateCreateInfo dyn;
memset(&dyn, 0, sizeof(dyn));
dyn.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
dyn.dynamicStateCount = sizeof(dynEnable) / sizeof(VkDynamicState);
dyn.pDynamicStates = dynEnable;
pipelineInfo.pDynamicState = &dyn;
pipelineInfo.layout = mPipelineLayout;
pipelineInfo.renderPass = mWindow->defaultRenderPass();
err = mDeviceFunctions->vkCreateGraphicsPipelines(logicalDevice, mPipelineCache, 1, &pipelineInfo, nullptr, &mPipeline);
if (err != VK_SUCCESS)
qFatal("Failed to create graphics pipeline: %d", err);
if (vertShaderModule)
mDeviceFunctions->vkDestroyShaderModule(logicalDevice, vertShaderModule, nullptr);
if (fragShaderModule)
mDeviceFunctions->vkDestroyShaderModule(logicalDevice, fragShaderModule, nullptr);
qDebug("\n ***************************** initResources finished ******************************************* \n");
getVulkanHWInfo();
}
void RenderWindow::initSwapChainResources()
{
qDebug("\n ***************************** initSwapChainResources ******************************************* \n");
// Projection matrix - how the scene will be projected into the render window
//This function is called at startup and when the app window is resized
mProjectionMatrix.setToIdentity();
//find the size of the window
const QSize sz = mWindow->swapChainImageSize();
// vertical angle , aspect ratio near- , far plane
/**PLAY WITH THIS**/
mProjectionMatrix.perspective(25.0f, sz.width() / (float) sz.height(), 0.01f, 100.0f);
//Camera is -4 away from origo
/**PLAY WITH THIS**/
mProjectionMatrix.translate(0, 0, -4);
//Flip projection because of Vulkan's -Y axis
mProjectionMatrix.scale(1.0f, -1.0f, 1.0);
}
void RenderWindow::startNextFrame()
{
VkDevice dev = mWindow->device();
VkCommandBuffer cb = mWindow->currentCommandBuffer();
const QSize sz = mWindow->swapChainImageSize();
//Backtgound color of the render window - dark grey -
/**PLAY WITH THIS**/
VkClearColorValue clearColor = {{ 0.3, 0.3, 0.3, 1 }};
VkClearDepthStencilValue clearDS = { 1, 0 };
VkClearValue clearValues[3];
memset(clearValues, 0, sizeof(clearValues));
clearValues[0].color = clearValues[2].color = clearColor;
clearValues[1].depthStencil = clearDS;
VkRenderPassBeginInfo rpBeginInfo;
memset(&rpBeginInfo, 0, sizeof(rpBeginInfo));
rpBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
rpBeginInfo.renderPass = mWindow->defaultRenderPass();
rpBeginInfo.framebuffer = mWindow->currentFramebuffer();
rpBeginInfo.renderArea.extent.width = sz.width();
rpBeginInfo.renderArea.extent.height = sz.height();
rpBeginInfo.clearValueCount = mWindow->sampleCountFlagBits() > VK_SAMPLE_COUNT_1_BIT ? 3 : 2;
rpBeginInfo.pClearValues = clearValues;
VkCommandBuffer cmdBuf = mWindow->currentCommandBuffer();
mDeviceFunctions->vkCmdBeginRenderPass(cmdBuf, &rpBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
quint8* GPUmemPointer;
VkResult err = mDeviceFunctions->vkMapMemory(dev, mBufferMemory, mUniformBufferInfo[mWindow->currentFrame()].offset,
UNIFORM_DATA_SIZE, 0, reinterpret_cast<void **>(&GPUmemPointer));
if (err != VK_SUCCESS)
qFatal("Failed to map memory: %d", err);
/********************************* Set the rotation in our matrix *********************************/
//We make a temp of this to now mess up the original matrix
QMatrix4x4 tempMatrix = mProjectionMatrix;
//Rotates the object
// speed, X, Y, Z axis
/**PLAY WITH THIS**/
tempMatrix.rotate(mRotation, 0, 1, 0);
memcpy(GPUmemPointer, tempMatrix.constData(), 16 * sizeof(float));
mDeviceFunctions->vkUnmapMemory(dev, mBufferMemory);
//rotate the triangle 1 degree per frame
/**PLAY WITH THIS**/
mRotation += 1.0f;
mDeviceFunctions->vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_GRAPHICS, mPipeline);
mDeviceFunctions->vkCmdBindDescriptorSets(cb, VK_PIPELINE_BIND_POINT_GRAPHICS, mPipelineLayout, 0, 1,
&mDescriptorSet[mWindow->currentFrame()], 0, nullptr);
VkDeviceSize vbOffset = 0;
//The second parameter here is the binding to the VertexInputBindingDescription,
//so it has to be the same number used there
mDeviceFunctions->vkCmdBindVertexBuffers(cb, 0, 1, &mBuffer, &vbOffset);
VkViewport viewport;
viewport.x = viewport.y = 0;
viewport.width = sz.width();
viewport.height = sz.height();
viewport.minDepth = 0;
viewport.maxDepth = 1;
mDeviceFunctions->vkCmdSetViewport(cb, 0, 1, &viewport);
VkRect2D scissor;
scissor.offset.x = scissor.offset.y = 0;
scissor.extent.width = viewport.width;
scissor.extent.height = viewport.height;
mDeviceFunctions->vkCmdSetScissor(cb, 0, 1, &scissor);
/********************************* Our draw call!: *********************************/
// the number 3 is the number of vertices, so you have to change that if you add more!
mDeviceFunctions->vkCmdDraw(cb, 3, 1, 0, 0);
mDeviceFunctions->vkCmdEndRenderPass(cmdBuf);
/*QVulkanWindow subclasses queue their draw calls in their reimplementation of
QVulkanWindowRenderer::startNextFrame(). Once done, they are required to call back
QVulkanWindow::frameReady(). The example has no asynchronous command generation, so the
frameReady() call is made directly from startNextFrame().
To get continuous updates, the example simply invokes QWindow::requestUpdate() in order to schedule a repaint.
This means that it requests the Qt window system to call the update() method,
which will eventually lead to the paintEvent() being called.
*/
mWindow->frameReady();
mWindow->requestUpdate(); // render continuously, throttled by the presentation rate
}
VkShaderModule RenderWindow::createShader(const QString &name)
{
//This uses Qt's own file opening and resource system
//We probably will replace it with pure C++ when expanding the program
QFile file(name);
if (!file.open(QIODevice::ReadOnly)) {
qWarning("Failed to read shader %s", qPrintable(name));
return VK_NULL_HANDLE;
}
QByteArray blob = file.readAll();
file.close();
VkShaderModuleCreateInfo shaderInfo;
memset(&shaderInfo, 0, sizeof(shaderInfo));
shaderInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
shaderInfo.codeSize = blob.size();
shaderInfo.pCode = reinterpret_cast<const uint32_t *>(blob.constData());
VkShaderModule shaderModule;
VkResult err = mDeviceFunctions->vkCreateShaderModule(mWindow->device(), &shaderInfo, nullptr, &shaderModule);
if (err != VK_SUCCESS) {
qWarning("Failed to create shader module: %d", err);
return VK_NULL_HANDLE;
}
return shaderModule;
}
void RenderWindow::getVulkanHWInfo()
{
qDebug("\n ***************************** Vulkan Hardware Info ******************************************* \n");
QVulkanInstance *inst = mWindow->vulkanInstance();
mDeviceFunctions = inst->deviceFunctions(mWindow->device());
QString info;
info += QString::asprintf("Number of physical devices: %d\n", int(mWindow->availablePhysicalDevices().count()));
QVulkanFunctions *f = inst->functions();
VkPhysicalDeviceProperties props;
f->vkGetPhysicalDeviceProperties(mWindow->physicalDevice(), &props);
info += QString::asprintf("Active physical device name: '%s' version %d.%d.%d\nAPI version %d.%d.%d\n",
props.deviceName,
VK_VERSION_MAJOR(props.driverVersion), VK_VERSION_MINOR(props.driverVersion),
VK_VERSION_PATCH(props.driverVersion),
VK_VERSION_MAJOR(props.apiVersion), VK_VERSION_MINOR(props.apiVersion),
VK_VERSION_PATCH(props.apiVersion));
info += QStringLiteral("Supported instance layers:\n");
for (const QVulkanLayer &layer : inst->supportedLayers())
info += QString::asprintf(" %s v%u\n", layer.name.constData(), layer.version);
info += QStringLiteral("Enabled instance layers:\n");
for (const QByteArray &layer : inst->layers())
info += QString::asprintf(" %s\n", layer.constData());
info += QStringLiteral("Supported instance extensions:\n");
for (const QVulkanExtension &ext : inst->supportedExtensions())
info += QString::asprintf(" %s v%u\n", ext.name.constData(), ext.version);
info += QStringLiteral("Enabled instance extensions:\n");
for (const QByteArray &ext : inst->extensions())
info += QString::asprintf(" %s\n", ext.constData());
info += QString::asprintf("Color format: %u\nDepth-stencil format: %u\n",
mWindow->colorFormat(), mWindow->depthStencilFormat());
info += QStringLiteral("Supported sample counts:");
const QList<int> sampleCounts = mWindow->supportedSampleCounts();
for (int count : sampleCounts)
info += QLatin1Char(' ') + QString::number(count);
info += QLatin1Char('\n');
qDebug(info.toUtf8().constData());
qDebug("\n ***************************** Vulkan Hardware Info finished ******************************************* \n");
}
void RenderWindow::releaseSwapChainResources()
{
qDebug("\n ***************************** releaseSwapChainResources ******************************************* \n");
}
void RenderWindow::releaseResources()
{
qDebug("\n ***************************** releaseResources ******************************************* \n");
VkDevice dev = mWindow->device();
if (mPipeline) {
mDeviceFunctions->vkDestroyPipeline(dev, mPipeline, nullptr);
mPipeline = VK_NULL_HANDLE;
}
if (mPipelineLayout) {
mDeviceFunctions->vkDestroyPipelineLayout(dev, mPipelineLayout, nullptr);
mPipelineLayout = VK_NULL_HANDLE;
}
if (mPipelineCache) {
mDeviceFunctions->vkDestroyPipelineCache(dev, mPipelineCache, nullptr);
mPipelineCache = VK_NULL_HANDLE;
}
if (mDescriptorSetLayout) {
mDeviceFunctions->vkDestroyDescriptorSetLayout(dev, mDescriptorSetLayout, nullptr);
mDescriptorSetLayout = VK_NULL_HANDLE;
}
if (mDescriptorPool) {
mDeviceFunctions->vkDestroyDescriptorPool(dev, mDescriptorPool, nullptr);
mDescriptorPool = VK_NULL_HANDLE;
}
if (mBuffer) {
mDeviceFunctions->vkDestroyBuffer(dev, mBuffer, nullptr);
mBuffer = VK_NULL_HANDLE;
}
if (mBufferMemory) {
mDeviceFunctions->vkFreeMemory(dev, mBufferMemory, nullptr);
mBufferMemory = VK_NULL_HANDLE;
}
}