Skip to content

Commit 00784a4

Browse files
authored
Merge branch 'main' into fix/staging-buffer-use-abstraction
2 parents 01893a1 + a3f5172 commit 00784a4

28 files changed

Lines changed: 939 additions & 600 deletions

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,5 @@ attachments/simple_engine/android/app/build/**
3030
attachments/simple_engine/android/gradle/wrapper/**
3131
attachments/simple_engine/android/gradlew
3232
attachments/simple_engine/android/gradlew.bat
33+
34+
attachments/template/build/**

attachments/15_hello_triangle.cpp

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,6 @@ class HelloTriangleApplication
327327
vk::PipelineShaderStageCreateInfo fragShaderStageInfo{.stage = vk::ShaderStageFlagBits::eFragment, .module = shaderModule, .pName = "fragMain"};
328328
vk::PipelineShaderStageCreateInfo shaderStages[] = {vertShaderStageInfo, fragShaderStageInfo};
329329

330-
331330
vk::PipelineVertexInputStateCreateInfo vertexInputInfo;
332331
vk::PipelineInputAssemblyStateCreateInfo inputAssembly{.topology = vk::PrimitiveTopology::eTriangleList};
333332
vk::PipelineViewportStateCreateInfo viewportState{.viewportCount = 1, .scissorCount = 1};
@@ -388,7 +387,8 @@ class HelloTriangleApplication
388387
void recordCommandBuffer(uint32_t imageIndex)
389388
{
390389
commandBuffer.begin({});
391-
// Before starting rendering, transition the swapchain image to COLOR_ATTACHMENT_OPTIMAL
390+
391+
// Before starting rendering, transition the swapchain image to vk::ImageLayout::eColorAttachmentOptimal
392392
transition_image_layout(
393393
imageIndex,
394394
vk::ImageLayout::eUndefined,
@@ -417,7 +417,8 @@ class HelloTriangleApplication
417417
commandBuffer.setScissor(0, vk::Rect2D(vk::Offset2D(0, 0), swapChainExtent));
418418
commandBuffer.draw(3, 1, 0, 0);
419419
commandBuffer.endRendering();
420-
// After rendering, transition the swapchain image to PRESENT_SRC
420+
421+
// After rendering, transition the swapchain image to vk::ImageLayout::ePresentSrcKHR
421422
transition_image_layout(
422423
imageIndex,
423424
vk::ImageLayout::eColorAttachmentOptimal,
@@ -471,21 +472,29 @@ class HelloTriangleApplication
471472

472473
void drawFrame()
473474
{
474-
queue.waitIdle(); // NOTE: for simplicity, wait for the queue to be idle before starting the frame
475-
// In the next chapter you see how to use multiple frames in flight and fences to sync
475+
auto fenceResult = device.waitForFences(*drawFence, vk::True, UINT64_MAX);
476+
if (fenceResult != vk::Result::eSuccess)
477+
{
478+
throw std::runtime_error("failed to wait for fence!");
479+
}
480+
device.resetFences(*drawFence);
476481

477482
auto [result, imageIndex] = swapChain.acquireNextImage(UINT64_MAX, *presentCompleteSemaphore, nullptr);
483+
478484
recordCommandBuffer(imageIndex);
479485

480-
device.resetFences(*drawFence);
486+
queue.waitIdle(); // NOTE: for simplicity, wait for the queue to be idle before starting the frame
487+
// In the next chapter you see how to use multiple frames in flight and fences to sync
488+
481489
vk::PipelineStageFlags waitDestinationStageMask(vk::PipelineStageFlagBits::eColorAttachmentOutput);
482-
const vk::SubmitInfo submitInfo{.waitSemaphoreCount = 1, .pWaitSemaphores = &*presentCompleteSemaphore, .pWaitDstStageMask = &waitDestinationStageMask, .commandBufferCount = 1, .pCommandBuffers = &*commandBuffer, .signalSemaphoreCount = 1, .pSignalSemaphores = &*renderFinishedSemaphore};
490+
const vk::SubmitInfo submitInfo{.waitSemaphoreCount = 1,
491+
.pWaitSemaphores = &*presentCompleteSemaphore,
492+
.pWaitDstStageMask = &waitDestinationStageMask,
493+
.commandBufferCount = 1,
494+
.pCommandBuffers = &*commandBuffer,
495+
.signalSemaphoreCount = 1,
496+
.pSignalSemaphores = &*renderFinishedSemaphore};
483497
queue.submit(submitInfo, *drawFence);
484-
result = device.waitForFences(*drawFence, vk::True, UINT64_MAX);
485-
if (result != vk::Result::eSuccess)
486-
{
487-
throw std::runtime_error("failed to wait for fence!");
488-
}
489498

490499
const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1, .pWaitSemaphores = &*renderFinishedSemaphore, .swapchainCount = 1, .pSwapchains = &*swapChain, .pImageIndices = &imageIndex};
491500
result = queue.presentKHR(presentInfoKHR);

attachments/16_frames_in_flight.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,6 @@ class HelloTriangleApplication
330330
vk::PipelineShaderStageCreateInfo fragShaderStageInfo{.stage = vk::ShaderStageFlagBits::eFragment, .module = shaderModule, .pName = "fragMain"};
331331
vk::PipelineShaderStageCreateInfo shaderStages[] = {vertShaderStageInfo, fragShaderStageInfo};
332332

333-
334333
vk::PipelineVertexInputStateCreateInfo vertexInputInfo;
335334
vk::PipelineInputAssemblyStateCreateInfo inputAssembly{.topology = vk::PrimitiveTopology::eTriangleList};
336335
vk::PipelineViewportStateCreateInfo viewportState{.viewportCount = 1, .scissorCount = 1};
@@ -392,7 +391,8 @@ class HelloTriangleApplication
392391
{
393392
auto &commandBuffer = commandBuffers[frameIndex];
394393
commandBuffer.begin({});
395-
// Before starting rendering, transition the swapchain image to COLOR_ATTACHMENT_OPTIMAL
394+
395+
// Before starting rendering, transition the swapchain image to vk::ImageLayout::eColorAttachmentOptimal
396396
transition_image_layout(
397397
imageIndex,
398398
vk::ImageLayout::eUndefined,
@@ -420,7 +420,8 @@ class HelloTriangleApplication
420420
commandBuffer.setScissor(0, vk::Rect2D(vk::Offset2D(0, 0), swapChainExtent));
421421
commandBuffer.draw(3, 1, 0, 0);
422422
commandBuffer.endRendering();
423-
// After rendering, transition the swapchain image to PRESENT_SRC
423+
424+
// After rendering, transition the swapchain image to vk::ImageLayout::ePresentSrcKHR
424425
transition_image_layout(
425426
imageIndex,
426427
vk::ImageLayout::eColorAttachmentOptimal,

attachments/17_swap_chain_recreation.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,8 @@ class HelloTriangleApplication
425425
{
426426
auto &commandBuffer = commandBuffers[frameIndex];
427427
commandBuffer.begin({});
428-
// Before starting rendering, transition the swapchain image to COLOR_ATTACHMENT_OPTIMAL
428+
429+
// Before starting rendering, transition the swapchain image to vk::ImageLayout::eColorAttachmentOptimal
429430
transition_image_layout(
430431
imageIndex,
431432
vk::ImageLayout::eUndefined,
@@ -453,7 +454,8 @@ class HelloTriangleApplication
453454
commandBuffer.setScissor(0, vk::Rect2D(vk::Offset2D(0, 0), swapChainExtent));
454455
commandBuffer.draw(3, 1, 0, 0);
455456
commandBuffer.endRendering();
456-
// After rendering, transition the swapchain image to PRESENT_SRC
457+
458+
// After rendering, transition the swapchain image to vk::ImageLayout::ePresentSrcKHR
457459
transition_image_layout(
458460
imageIndex,
459461
vk::ImageLayout::eColorAttachmentOptimal,
@@ -535,7 +537,7 @@ class HelloTriangleApplication
535537
}
536538
// On other success codes than eSuccess and eSuboptimalKHR we just throw an exception.
537539
// On any error code, aquireNextImage already threw an exception.
538-
else if (result != vk::Result::eSuccess && result != vk::Result::eSuboptimalKHR)
540+
if (result != vk::Result::eSuccess && result != vk::Result::eSuboptimalKHR)
539541
{
540542
assert(result == vk::Result::eTimeout || result == vk::Result::eNotReady);
541543
throw std::runtime_error("failed to acquire swap chain image!");

attachments/19_vertex_buffer.cpp

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,13 @@ struct Vertex
4040

4141
static vk::VertexInputBindingDescription getBindingDescription()
4242
{
43-
return {0, sizeof(Vertex), vk::VertexInputRate::eVertex};
43+
return {.binding = 0, .stride = sizeof(Vertex), .inputRate = vk::VertexInputRate::eVertex};
4444
}
4545

4646
static std::array<vk::VertexInputAttributeDescription, 2> getAttributeDescriptions()
4747
{
48-
return {
49-
vk::VertexInputAttributeDescription(0, 0, vk::Format::eR32G32Sfloat, offsetof(Vertex, pos)),
50-
vk::VertexInputAttributeDescription(1, 0, vk::Format::eR32G32B32Sfloat, offsetof(Vertex, color))};
48+
return {{{.location = 0, .binding = 0, .format = vk::Format::eR32G32Sfloat, .offset = offsetof(Vertex, pos)},
49+
{.location = 1, .binding = 0, .format = vk::Format::eR32G32B32Sfloat, .offset = offsetof(Vertex, color)}}};
5150
}
5251
};
5352

@@ -278,6 +277,7 @@ class HelloTriangleApplication
278277
vk::PhysicalDeviceExtendedDynamicStateFeaturesEXT>();
279278
bool supportsRequiredFeatures = features.template get<vk::PhysicalDeviceVulkan11Features>().shaderDrawParameters &&
280279
features.template get<vk::PhysicalDeviceVulkan13Features>().dynamicRendering &&
280+
features.template get<vk::PhysicalDeviceVulkan13Features>().synchronization2 &&
281281
features.template get<vk::PhysicalDeviceExtendedDynamicStateFeaturesEXT>().extendedDynamicState;
282282

283283
// Return true if the physicalDevice meets all the criteria
@@ -316,12 +316,16 @@ class HelloTriangleApplication
316316
}
317317

318318
// query for required features (Vulkan 1.1 and 1.3)
319-
vk::StructureChain<vk::PhysicalDeviceFeatures2, vk::PhysicalDeviceVulkan11Features, vk::PhysicalDeviceVulkan13Features, vk::PhysicalDeviceExtendedDynamicStateFeaturesEXT> featureChain = {
320-
{}, // vk::PhysicalDeviceFeatures2
321-
{.shaderDrawParameters = true}, // vk::PhysicalDeviceVulkan11Features
322-
{.synchronization2 = true, .dynamicRendering = true}, // vk::PhysicalDeviceVulkan13Features
323-
{.extendedDynamicState = true} // vk::PhysicalDeviceExtendedDynamicStateFeaturesEXT
324-
};
319+
vk::StructureChain<vk::PhysicalDeviceFeatures2,
320+
vk::PhysicalDeviceVulkan11Features,
321+
vk::PhysicalDeviceVulkan13Features,
322+
vk::PhysicalDeviceExtendedDynamicStateFeaturesEXT>
323+
featureChain = {
324+
{}, // vk::PhysicalDeviceFeatures2
325+
{.shaderDrawParameters = true}, // vk::PhysicalDeviceVulkan11Features
326+
{.synchronization2 = true, .dynamicRendering = true}, // vk::PhysicalDeviceVulkan13Features
327+
{.extendedDynamicState = true} // vk::PhysicalDeviceExtendedDynamicStateFeaturesEXT
328+
};
325329

326330
// create a Device
327331
float queuePriority = 0.5f;
@@ -389,7 +393,10 @@ class HelloTriangleApplication
389393

390394
auto bindingDescription = Vertex::getBindingDescription();
391395
auto attributeDescriptions = Vertex::getAttributeDescriptions();
392-
vk::PipelineVertexInputStateCreateInfo vertexInputInfo{.vertexBindingDescriptionCount = 1, .pVertexBindingDescriptions = &bindingDescription, .vertexAttributeDescriptionCount = static_cast<uint32_t>(attributeDescriptions.size()), .pVertexAttributeDescriptions = attributeDescriptions.data()};
396+
vk::PipelineVertexInputStateCreateInfo vertexInputInfo{.vertexBindingDescriptionCount = 1,
397+
.pVertexBindingDescriptions = &bindingDescription,
398+
.vertexAttributeDescriptionCount = static_cast<uint32_t>(attributeDescriptions.size()),
399+
.pVertexAttributeDescriptions = attributeDescriptions.data()};
393400
vk::PipelineInputAssemblyStateCreateInfo inputAssembly{.topology = vk::PrimitiveTopology::eTriangleList};
394401
vk::PipelineViewportStateCreateInfo viewportState{.viewportCount = 1, .scissorCount = 1};
395402

@@ -442,11 +449,15 @@ class HelloTriangleApplication
442449

443450
void createVertexBuffer()
444451
{
445-
vk::BufferCreateInfo bufferInfo{.size = sizeof(vertices[0]) * vertices.size(), .usage = vk::BufferUsageFlagBits::eVertexBuffer, .sharingMode = vk::SharingMode::eExclusive};
452+
vk::BufferCreateInfo bufferInfo{.size = sizeof(vertices[0]) * vertices.size(),
453+
.usage = vk::BufferUsageFlagBits::eVertexBuffer,
454+
.sharingMode = vk::SharingMode::eExclusive};
446455
vertexBuffer = vk::raii::Buffer(device, bufferInfo);
447456

448457
vk::MemoryRequirements memRequirements = vertexBuffer.getMemoryRequirements();
449-
vk::MemoryAllocateInfo memoryAllocateInfo{.allocationSize = memRequirements.size, .memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent)};
458+
vk::MemoryAllocateInfo memoryAllocateInfo{
459+
.allocationSize = memRequirements.size,
460+
.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent)};
450461
vertexBufferMemory = vk::raii::DeviceMemory(device, memoryAllocateInfo);
451462

452463
vertexBuffer.bindMemory(*vertexBufferMemory, 0);
@@ -482,7 +493,8 @@ class HelloTriangleApplication
482493
{
483494
auto &commandBuffer = commandBuffers[frameIndex];
484495
commandBuffer.begin({});
485-
// Before starting rendering, transition the swapchain image to COLOR_ATTACHMENT_OPTIMAL
496+
497+
// Before starting rendering, transition the swapchain image to vk::ImageLayout::eColorAttachmentOptimal
486498
transition_image_layout(
487499
imageIndex,
488500
vk::ImageLayout::eUndefined,
@@ -509,9 +521,10 @@ class HelloTriangleApplication
509521
commandBuffer.setViewport(0, vk::Viewport(0.0f, 0.0f, static_cast<float>(swapChainExtent.width), static_cast<float>(swapChainExtent.height), 0.0f, 1.0f));
510522
commandBuffer.setScissor(0, vk::Rect2D(vk::Offset2D(0, 0), swapChainExtent));
511523
commandBuffer.bindVertexBuffers(0, *vertexBuffer, {0});
512-
commandBuffer.draw(3, 1, 0, 0);
524+
commandBuffer.draw(static_cast<uint32_t>(vertices.size()), 1, 0, 0);
513525
commandBuffer.endRendering();
514-
// After rendering, transition the swapchain image to PRESENT_SRC
526+
527+
// After rendering, transition the swapchain image to vk::ImageLayout::ePresentSrcKHR
515528
transition_image_layout(
516529
imageIndex,
517530
vk::ImageLayout::eColorAttachmentOptimal,

0 commit comments

Comments
 (0)