forked from jherico/VulkanExamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultisampling.cpp
More file actions
550 lines (457 loc) · 23.2 KB
/
Copy pathmultisampling.cpp
File metadata and controls
550 lines (457 loc) · 23.2 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
/*
* Vulkan Example - Multisampling using resolve attachments
*
* Copyright (C) 2016 by Sascha Willems - www.saschawillems.de
*
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
*/
#include "vulkanExampleBase.h"
#define SAMPLE_COUNT vk::SampleCountFlagBits::e4
struct {
vkx::CreateImageResult color;
vkx::CreateImageResult depth;
} multisampleTarget;
// Vertex layout for this example
std::vector<vkx::VertexLayout> vertexLayout =
{
vkx::VertexLayout::VERTEX_LAYOUT_POSITION,
vkx::VertexLayout::VERTEX_LAYOUT_NORMAL,
vkx::VertexLayout::VERTEX_LAYOUT_UV,
vkx::VertexLayout::VERTEX_LAYOUT_COLOR,
};
class VulkanExample : public vkx::ExampleBase {
public:
struct {
vkx::Texture colorMap;
} textures;
struct {
vk::PipelineVertexInputStateCreateInfo inputState;
std::vector<vk::VertexInputBindingDescription> bindingDescriptions;
std::vector<vk::VertexInputAttributeDescription> attributeDescriptions;
} vertices;
struct {
vkx::MeshBuffer example;
} meshes;
struct {
vkx::UniformData vsScene;
} uniformData;
struct UboVS {
glm::mat4 projection;
glm::mat4 model;
glm::vec4 lightPos = glm::vec4(5.0f, 5.0f, 5.0f, 1.0f);
} uboVS;
struct {
vk::Pipeline solid;
} pipelines;
vk::PipelineLayout pipelineLayout;
vk::DescriptorSet descriptorSet;
vk::DescriptorSetLayout descriptorSetLayout;
VulkanExample() : vkx::ExampleBase(ENABLE_VALIDATION) {
camera.setZoom(-7.5f);
zoomSpeed = 2.5f;
camera.setRotation({ 0.0f, -90.0f, 0.0f });
camera.setTranslation({ 2.5f, 2.5f, -7.5 });
title = "Vulkan Example - Multisampling";
}
~VulkanExample() {
// Clean up used Vulkan resources
// Note : Inherited destructor cleans up resources stored in base class
device.destroyPipeline(pipelines.solid);
device.destroyPipelineLayout(pipelineLayout);
device.destroyDescriptorSetLayout(descriptorSetLayout);
meshes.example.destroy();
// Destroy MSAA target
device.destroyImage(multisampleTarget.color.image);
device.destroyImageView(multisampleTarget.color.view);
device.freeMemory(multisampleTarget.color.memory);
device.destroyImage(multisampleTarget.depth.image);
device.destroyImageView(multisampleTarget.depth.view);
device.freeMemory(multisampleTarget.depth.memory);
textures.colorMap.destroy();
uniformData.vsScene.destroy();
}
// Creates a multi sample render target (image and view) that is used to resolve
// into the visible frame buffer target in the render pass
void setupMultisampleTarget() {
// Check if device supports requested sample count for color and depth frame buffer
vk::SampleCountFlags colorSampleCount = deviceProperties.limits.framebufferColorSampleCounts;
vk::SampleCountFlags depthSampleCount = deviceProperties.limits.framebufferDepthSampleCounts;
vk::SampleCountFlags requiredSamples = SAMPLE_COUNT;
assert((uint32_t)colorSampleCount >= (uint32_t)requiredSamples && (uint32_t)depthSampleCount >= (uint32_t)requiredSamples);
// Color target
vk::ImageCreateInfo info;
info.imageType = vk::ImageType::e2D;
info.format = colorformat;
info.extent.width = size.width;
info.extent.height = size.height;
info.extent.depth = 1;
info.mipLevels = 1;
info.arrayLayers = 1;
info.sharingMode = vk::SharingMode::eExclusive;
info.tiling = vk::ImageTiling::eOptimal;
info.samples = SAMPLE_COUNT;
// vk::Image will only be used as a transient target
info.usage = vk::ImageUsageFlagBits::eTransientAttachment | vk::ImageUsageFlagBits::eColorAttachment;
info.initialLayout = vk::ImageLayout::eUndefined;
multisampleTarget.color = createImage(info, vk::MemoryPropertyFlagBits::eDeviceLocal);
//// We prefer a lazily allocated memory type
//// This means that the memory get allocated when the implementation sees fit, e.g. when first using the images
//vk::Bool32 lazyMemType = getMemoryType(memReqs.memoryTypeBits, vk::MemoryPropertyFlagBits::eLazilyAllocated, &memAlloc.memoryTypeIndex);
//if (!lazyMemType) {
// // If this is not available, fall back to device local memory
// getMemoryType(memReqs.memoryTypeBits, vk::MemoryPropertyFlagBits::eDeviceLocal, &memAlloc.memoryTypeIndex);
//}
// Create image view for the MSAA target
vk::ImageViewCreateInfo viewInfo;
viewInfo.image = multisampleTarget.color.image;
viewInfo.viewType = vk::ImageViewType::e2D;
viewInfo.format = colorformat;
viewInfo.components.r = vk::ComponentSwizzle::eR;
viewInfo.components.g = vk::ComponentSwizzle::eG;
viewInfo.components.b = vk::ComponentSwizzle::eB;
viewInfo.components.a = vk::ComponentSwizzle::eA;
viewInfo.subresourceRange.aspectMask = vk::ImageAspectFlagBits::eColor;
viewInfo.subresourceRange.levelCount = 1;
viewInfo.subresourceRange.layerCount = 1;
multisampleTarget.color.view = device.createImageView(viewInfo);
// Depth target
info.imageType = vk::ImageType::e2D;
info.format = depthFormat;
info.extent.width = size.width;
info.extent.height = size.height;
info.extent.depth = 1;
info.mipLevels = 1;
info.arrayLayers = 1;
info.sharingMode = vk::SharingMode::eExclusive;
info.tiling = vk::ImageTiling::eOptimal;
info.samples = SAMPLE_COUNT;
// vk::Image will only be used as a transient target
info.usage = vk::ImageUsageFlagBits::eTransientAttachment | vk::ImageUsageFlagBits::eDepthStencilAttachment;
info.initialLayout = vk::ImageLayout::eUndefined;
multisampleTarget.depth = createImage(info, vk::MemoryPropertyFlagBits::eDeviceLocal);
// Create image view for the MSAA target
viewInfo.image = multisampleTarget.depth.image;
viewInfo.viewType = vk::ImageViewType::e2D;
viewInfo.format = depthFormat;
viewInfo.subresourceRange.aspectMask = vk::ImageAspectFlagBits::eDepth | vk::ImageAspectFlagBits::eStencil;
viewInfo.subresourceRange.levelCount = 1;
viewInfo.subresourceRange.layerCount = 1;
multisampleTarget.depth.view = device.createImageView(viewInfo);
// Initial image layout transitions
// We need to transform the MSAA target layouts before using them
withPrimaryCommandBuffer([&](const vk::CommandBuffer& setupCmdBuffer) {
// Tansform MSAA color target
vkx::setImageLayout(
setupCmdBuffer,
multisampleTarget.color.image,
vk::ImageAspectFlagBits::eColor,
vk::ImageLayout::eUndefined,
vk::ImageLayout::eColorAttachmentOptimal);
// Tansform MSAA depth target
vkx::setImageLayout(
setupCmdBuffer,
multisampleTarget.depth.image,
vk::ImageAspectFlagBits::eDepth | vk::ImageAspectFlagBits::eStencil,
vk::ImageLayout::eUndefined,
vk::ImageLayout::eDepthStencilAttachmentOptimal);
});
}
// Setup a render pass for using a multi sampled attachment
// and a resolve attachment that the msaa image is resolved
// to at the end of the render pass
void setupRenderPass() {
// Overrides the virtual function of the base class
std::array<vk::AttachmentDescription, 4> attachments = {};
// Multisampled attachment that we render to
attachments[0].format = colorformat;
attachments[0].samples = SAMPLE_COUNT;
attachments[0].loadOp = vk::AttachmentLoadOp::eClear;
// No longer required after resolve, this may save some bandwidth on certain GPUs
attachments[0].storeOp = vk::AttachmentStoreOp::eDontCare;
attachments[0].stencilLoadOp = vk::AttachmentLoadOp::eDontCare;
attachments[0].stencilStoreOp = vk::AttachmentStoreOp::eDontCare;
attachments[0].initialLayout = vk::ImageLayout::eColorAttachmentOptimal;
attachments[0].finalLayout = vk::ImageLayout::eColorAttachmentOptimal;
// This is the frame buffer attachment to where the multisampled image
// will be resolved to and which will be presented to the swapchain
attachments[1].format = colorformat;
attachments[1].samples = vk::SampleCountFlagBits::e1;
attachments[1].loadOp = vk::AttachmentLoadOp::eDontCare;
attachments[1].storeOp = vk::AttachmentStoreOp::eStore;
attachments[1].stencilLoadOp = vk::AttachmentLoadOp::eDontCare;
attachments[1].stencilStoreOp = vk::AttachmentStoreOp::eDontCare;
attachments[1].initialLayout = vk::ImageLayout::eUndefined;
attachments[1].finalLayout = vk::ImageLayout::ePresentSrcKHR;
// Multisampled depth attachment we render to
attachments[2].format = depthFormat;
attachments[2].samples = SAMPLE_COUNT;
attachments[2].loadOp = vk::AttachmentLoadOp::eClear;
attachments[2].storeOp = vk::AttachmentStoreOp::eDontCare;
attachments[2].stencilLoadOp = vk::AttachmentLoadOp::eDontCare;
attachments[2].stencilStoreOp = vk::AttachmentStoreOp::eDontCare;
attachments[2].initialLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal;
attachments[2].finalLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal;
// Depth resolve attachment
attachments[3].format = depthFormat;
attachments[3].samples = vk::SampleCountFlagBits::e1;
attachments[3].loadOp = vk::AttachmentLoadOp::eDontCare;
attachments[3].storeOp = vk::AttachmentStoreOp::eStore;
attachments[3].stencilLoadOp = vk::AttachmentLoadOp::eDontCare;
attachments[3].stencilStoreOp = vk::AttachmentStoreOp::eDontCare;
attachments[3].initialLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal;
attachments[3].finalLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal;
vk::AttachmentReference colorReference;
colorReference.attachment = 0;
colorReference.layout = vk::ImageLayout::eColorAttachmentOptimal;
vk::AttachmentReference depthReference;
depthReference.attachment = 2;
depthReference.layout = vk::ImageLayout::eDepthStencilAttachmentOptimal;
// Two resolve attachment references for color and depth
std::array<vk::AttachmentReference, 2> resolveReferences = {};
resolveReferences[0].attachment = 1;
resolveReferences[0].layout = vk::ImageLayout::eColorAttachmentOptimal;
resolveReferences[1].attachment = 3;
resolveReferences[1].layout = vk::ImageLayout::eDepthStencilAttachmentOptimal;
vk::SubpassDescription subpass;
subpass.pipelineBindPoint = vk::PipelineBindPoint::eGraphics;
subpass.colorAttachmentCount = 1;
subpass.pColorAttachments = &colorReference;
// Pass our resolve attachments to the sub pass
subpass.pResolveAttachments = resolveReferences.data();
subpass.pDepthStencilAttachment = &depthReference;
std::vector<vk::SubpassDependency> dependencies;
{
vk::SubpassDependency dependency;
dependency.srcSubpass = 0;
dependency.srcAccessMask = vk::AccessFlagBits::eColorAttachmentWrite;
dependency.srcStageMask = vk::PipelineStageFlagBits::eBottomOfPipe;
dependency.dstSubpass = VK_SUBPASS_EXTERNAL;
dependency.dstAccessMask = vk::AccessFlagBits::eColorAttachmentRead;
dependency.dstStageMask = vk::PipelineStageFlagBits::eColorAttachmentOutput;
dependencies.push_back(dependency);
}
vk::RenderPassCreateInfo renderPassInfo;
renderPassInfo.attachmentCount = attachments.size();
renderPassInfo.pAttachments = attachments.data();
renderPassInfo.dependencyCount = dependencies.size();
renderPassInfo.pDependencies = dependencies.data();
renderPassInfo.subpassCount = 1;
renderPassInfo.pSubpasses = &subpass;
renderPass = device.createRenderPass(renderPassInfo);
}
// Frame buffer attachments must match with render pass setup,
// so we need to adjust frame buffer creation to cover our
// multisample target
void setupFrameBuffer() {
// Overrides the virtual function of the base class
std::array<vk::ImageView, 4> attachments;
setupMultisampleTarget();
attachments[0] = multisampleTarget.color.view;
// attachment[1] = swapchain image
attachments[2] = multisampleTarget.depth.view;
attachments[3] = depthStencil.view;
vk::FramebufferCreateInfo framebufferCreateInfo;
framebufferCreateInfo.renderPass = renderPass;
framebufferCreateInfo.attachmentCount = attachments.size();
framebufferCreateInfo.pAttachments = attachments.data();
framebufferCreateInfo.width = size.width;
framebufferCreateInfo.height = size.height;
framebufferCreateInfo.layers = 1;
// Create frame buffers for every swap chain image
framebuffers.resize(swapChain.imageCount);
for (uint32_t i = 0; i < framebuffers.size(); i++) {
attachments[1] = swapChain.images[i].view;
framebuffers[i] = device.createFramebuffer(framebufferCreateInfo);
}
}
void updateDrawCommandBuffer(const vk::CommandBuffer& cmdBuffer) {
//vk::CommandBufferBeginInfo cmdBufInfo;
//vk::ClearValue clearValues[3];
//// Clear to a white background for higher contrast
//clearValues[0].color = vkx::clearColor({ 1.0f, 1.0f, 1.0f, 1.0f });
//clearValues[1].color = vkx::clearColor({ 1.0f, 1.0f, 1.0f, 1.0f });
//clearValues[2].depthStencil = { 1.0f, 0 };
cmdBuffer.setViewport(0, vkx::viewport(size));
cmdBuffer.setScissor(0, vkx::rect2D(size));
cmdBuffer.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, pipelineLayout, 0, descriptorSet, nullptr);
cmdBuffer.bindPipeline(vk::PipelineBindPoint::eGraphics, pipelines.solid);
vk::DeviceSize offsets = 0;
cmdBuffer.bindVertexBuffers(VERTEX_BUFFER_BIND_ID, meshes.example.vertices.buffer, offsets);
cmdBuffer.bindIndexBuffer(meshes.example.indices.buffer, 0, vk::IndexType::eUint32);
cmdBuffer.drawIndexed(meshes.example.indexCount, 1, 0, 0, 0);
}
virtual void setupRenderPassBeginInfo() {
clearValues.clear();
clearValues.push_back(vkx::clearColor(glm::vec4(1)));
clearValues.push_back(vkx::clearColor(glm::vec4(1)));
clearValues.push_back(vk::ClearDepthStencilValue{ 1.0f, 0 });
renderPassBeginInfo = vk::RenderPassBeginInfo();
renderPassBeginInfo.renderPass = renderPass;
renderPassBeginInfo.renderArea.extent = size;
renderPassBeginInfo.clearValueCount = clearValues.size();
renderPassBeginInfo.pClearValues = clearValues.data();
}
void loadTextures() {
textures.colorMap = textureLoader->loadTexture(
getAssetPath() + "models/voyager/voyager.ktx",
vk::Format::eBc3UnormBlock);
}
void loadMeshes() {
meshes.example = loadMesh(getAssetPath() + "models/voyager/voyager.dae", vertexLayout, 1.0f);
}
void setupVertexDescriptions() {
// Binding description
vertices.bindingDescriptions.resize(1);
vertices.bindingDescriptions[0] =
vkx::vertexInputBindingDescription(VERTEX_BUFFER_BIND_ID, vkx::vertexSize(vertexLayout), vk::VertexInputRate::eVertex);
// Attribute descriptions
vertices.attributeDescriptions.resize(4);
// Location 0 : Position
vertices.attributeDescriptions[0] =
vkx::vertexInputAttributeDescription(VERTEX_BUFFER_BIND_ID, 0, vk::Format::eR32G32B32Sfloat, 0);
// Location 1 : Normal
vertices.attributeDescriptions[1] =
vkx::vertexInputAttributeDescription(VERTEX_BUFFER_BIND_ID, 1, vk::Format::eR32G32B32Sfloat, sizeof(float) * 3);
// Location 2 : Texture coordinates
vertices.attributeDescriptions[2] =
vkx::vertexInputAttributeDescription(VERTEX_BUFFER_BIND_ID, 2, vk::Format::eR32G32Sfloat, sizeof(float) * 6);
// Location 3 : Color
vertices.attributeDescriptions[3] =
vkx::vertexInputAttributeDescription(VERTEX_BUFFER_BIND_ID, 3, vk::Format::eR32G32B32Sfloat, sizeof(float) * 8);
vertices.inputState = vk::PipelineVertexInputStateCreateInfo();
vertices.inputState.vertexBindingDescriptionCount = vertices.bindingDescriptions.size();
vertices.inputState.pVertexBindingDescriptions = vertices.bindingDescriptions.data();
vertices.inputState.vertexAttributeDescriptionCount = vertices.attributeDescriptions.size();
vertices.inputState.pVertexAttributeDescriptions = vertices.attributeDescriptions.data();
}
void setupDescriptorPool() {
// Example uses one ubo and one combined image sampler
std::vector<vk::DescriptorPoolSize> poolSizes =
{
vkx::descriptorPoolSize(vk::DescriptorType::eUniformBuffer, 1),
vkx::descriptorPoolSize(vk::DescriptorType::eCombinedImageSampler, 1),
};
vk::DescriptorPoolCreateInfo descriptorPoolInfo =
vkx::descriptorPoolCreateInfo(poolSizes.size(), poolSizes.data(), 2);
descriptorPool = device.createDescriptorPool(descriptorPoolInfo);
}
void setupDescriptorSetLayout() {
std::vector<vk::DescriptorSetLayoutBinding> setLayoutBindings =
{
// Binding 0 : Vertex shader uniform buffer
vkx::descriptorSetLayoutBinding(
vk::DescriptorType::eUniformBuffer,
vk::ShaderStageFlagBits::eVertex,
0),
// Binding 1 : Fragment shader combined sampler
vkx::descriptorSetLayoutBinding(
vk::DescriptorType::eCombinedImageSampler,
vk::ShaderStageFlagBits::eFragment,
1),
};
vk::DescriptorSetLayoutCreateInfo descriptorLayout =
vkx::descriptorSetLayoutCreateInfo(setLayoutBindings.data(), setLayoutBindings.size());
descriptorSetLayout = device.createDescriptorSetLayout(descriptorLayout);
vk::PipelineLayoutCreateInfo pPipelineLayoutCreateInfo =
vkx::pipelineLayoutCreateInfo(&descriptorSetLayout, 1);
pipelineLayout = device.createPipelineLayout(pPipelineLayoutCreateInfo);
}
void setupDescriptorSet() {
vk::DescriptorSetAllocateInfo allocInfo =
vkx::descriptorSetAllocateInfo(descriptorPool, &descriptorSetLayout, 1);
descriptorSet = device.allocateDescriptorSets(allocInfo)[0];
vk::DescriptorImageInfo texDescriptor =
vkx::descriptorImageInfo(textures.colorMap.sampler, textures.colorMap.view, vk::ImageLayout::eGeneral);
std::vector<vk::WriteDescriptorSet> writeDescriptorSets =
{
// Binding 0 : Vertex shader uniform buffer
vkx::writeDescriptorSet(
descriptorSet,
vk::DescriptorType::eUniformBuffer,
0,
&uniformData.vsScene.descriptor),
// Binding 1 : Color map
vkx::writeDescriptorSet(
descriptorSet,
vk::DescriptorType::eCombinedImageSampler,
1,
&texDescriptor)
};
device.updateDescriptorSets(writeDescriptorSets.size(), writeDescriptorSets.data(), 0, NULL);
}
void preparePipelines() {
vk::PipelineInputAssemblyStateCreateInfo inputAssemblyState =
vkx::pipelineInputAssemblyStateCreateInfo(vk::PrimitiveTopology::eTriangleList, vk::PipelineInputAssemblyStateCreateFlags(), VK_FALSE);
vk::PipelineRasterizationStateCreateInfo rasterizationState =
vkx::pipelineRasterizationStateCreateInfo(vk::PolygonMode::eFill, vk::CullModeFlagBits::eBack, vk::FrontFace::eClockwise);
vk::PipelineColorBlendAttachmentState blendAttachmentState =
vkx::pipelineColorBlendAttachmentState();
vk::PipelineColorBlendStateCreateInfo colorBlendState =
vkx::pipelineColorBlendStateCreateInfo(1, &blendAttachmentState);
vk::PipelineDepthStencilStateCreateInfo depthStencilState =
vkx::pipelineDepthStencilStateCreateInfo(VK_TRUE, VK_TRUE, vk::CompareOp::eLessOrEqual);
vk::PipelineViewportStateCreateInfo viewportState =
vkx::pipelineViewportStateCreateInfo(1, 1);
vk::PipelineMultisampleStateCreateInfo multisampleState =
vkx::pipelineMultisampleStateCreateInfo(SAMPLE_COUNT);
std::vector<vk::DynamicState> dynamicStateEnables = {
vk::DynamicState::eViewport,
vk::DynamicState::eScissor
};
vk::PipelineDynamicStateCreateInfo dynamicState =
vkx::pipelineDynamicStateCreateInfo(dynamicStateEnables.data(), dynamicStateEnables.size());
// Solid rendering pipeline
// Load shaders
std::array<vk::PipelineShaderStageCreateInfo, 2> shaderStages;
shaderStages[0] = loadShader(getAssetPath() + "shaders/mesh/mesh.vert.spv", vk::ShaderStageFlagBits::eVertex);
shaderStages[1] = loadShader(getAssetPath() + "shaders/mesh/mesh.frag.spv", vk::ShaderStageFlagBits::eFragment);
vk::GraphicsPipelineCreateInfo pipelineCreateInfo =
vkx::pipelineCreateInfo(pipelineLayout, renderPass);
pipelineCreateInfo.pVertexInputState = &vertices.inputState;
pipelineCreateInfo.pInputAssemblyState = &inputAssemblyState;
pipelineCreateInfo.pRasterizationState = &rasterizationState;
pipelineCreateInfo.pColorBlendState = &colorBlendState;
pipelineCreateInfo.pMultisampleState = &multisampleState;
pipelineCreateInfo.pViewportState = &viewportState;
pipelineCreateInfo.pDepthStencilState = &depthStencilState;
pipelineCreateInfo.pDynamicState = &dynamicState;
pipelineCreateInfo.stageCount = shaderStages.size();
pipelineCreateInfo.pStages = shaderStages.data();
pipelines.solid = device.createGraphicsPipelines(pipelineCache, pipelineCreateInfo, nullptr)[0];
}
// Prepare and initialize uniform buffer containing shader uniforms
void prepareUniformBuffers() {
// Vertex shader uniform buffer block
uniformData.vsScene = createUniformBuffer(uboVS);
updateUniformBuffers();
}
void updateUniformBuffers() {
// Vertex shader
uboVS.projection = camera.matrices.perspective;
uboVS.model = camera.matrices.view;
uniformData.vsScene.copy(uboVS);
}
void prepare() {
ExampleBase::prepare();
loadTextures();
loadMeshes();
setupVertexDescriptions();
prepareUniformBuffers();
setupDescriptorSetLayout();
preparePipelines();
setupDescriptorPool();
setupDescriptorSet();
updateDrawCommandBuffers();
prepared = true;
}
virtual void render() {
if (!prepared)
return;
draw();
updateUniformBuffers();
}
virtual void viewChanged() {
updateUniformBuffers();
}
};
RUN_EXAMPLE(VulkanExample)