| | @@ -13,6 +13,7 @@ |
| 13 | 13 | #include <limits>
|
| 14 | 14 | #include <fstream>
|
| 15 | 15 | #include <array>
|
| 16 | +#include <chrono>
|
| 16 | 17 | #include <cassert>
|
| 17 | 18 |
|
| 18 | 19 | #include <util/root.hh>
|
| | @@ -28,6 +29,7 @@ |
| 28 | 29 | vk::CommandBuffer cmdBuf;
|
| 29 | 30 | vk::CommandBufferBeginInfo beginInfo;
|
| 30 | 31 | vk::Queue gfxQueue;
|
| 32 | +vk::PipelineLayout pipelineLayout;
|
| 31 | 33 | std::vector<vk::ImageView> swapChainImageViews;
|
| 32 | 34 | vk::Extent2D swapChainExtent;
|
| 33 | 35 | vk::Pipeline gfxPipeline;
|
| | @@ -41,11 +43,19 @@ |
| 41 | 43 | std::vector<vk::PresentModeKHR> availPresentModes;
|
| 42 | 44 | GLFWwindow *window;
|
| 43 | 45 |
|
| 46 | +vk::DescriptorSetLayout descriptorSetLayout = nullptr;
|
| 47 | +vk::DescriptorPool descriptorPool = nullptr;
|
| 48 | +std::vector<vk::DescriptorSet> descriptorSets;
|
| 49 | +
|
| 44 | 50 | vk::Buffer vertBuf = nullptr;
|
| 45 | 51 | vk::DeviceMemory vertBufMem = nullptr;
|
| 46 | 52 | vk::Buffer idxBuf = nullptr;
|
| 47 | 53 | vk::DeviceMemory idxBufMem = nullptr;
|
| 48 | 54 |
|
| 55 | +std::vector<vk::Buffer> uniformBufs;
|
| 56 | +std::vector<vk::DeviceMemory> uniformBufsMem;
|
| 57 | +std::vector<void *> uniformBufsMapped;
|
| 58 | +
|
| 49 | 59 | #pragma region debugCallback
|
| 50 | 60 | static VKAPI_ATTR vk::Bool32 VKAPI_CALL debugCallback(
|
| 51 | 61 | vk::DebugUtilsMessageSeverityFlagBitsEXT severity,
|
| | @@ -257,6 +267,7 @@ |
| 257 | 267 | cmdBuf.bindPipeline(vk::PipelineBindPoint::eGraphics, gfxPipeline);
|
| 258 | 268 | cmdBuf.bindVertexBuffers(0, vertBuf, {0});
|
| 259 | 269 | cmdBuf.bindIndexBuffer(idxBuf, 0, vk::IndexType::eUint16);
|
| 270 | + cmdBuf.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, pipelineLayout, 0, descriptorSets[frameIdx], nullptr);
|
| 260 | 271 | cmdBuf.setViewport(0, vk::Viewport(0.f, 0.f,
|
| 261 | 272 | static_cast<float>(swapChainExtent.width),
|
| 262 | 273 | static_cast<float>(swapChainExtent.height),
|
| | @@ -535,6 +546,20 @@ |
| 535 | 546 | createImageViews();
|
| 536 | 547 | #pragma endregion
|
| 537 | 548 |
|
| 549 | +#pragma region descriptorSetLayout
|
| 550 | + vk::DescriptorSetLayoutBinding uboLayoutBinding = {};
|
| 551 | + uboLayoutBinding.binding = 0;
|
| 552 | + uboLayoutBinding.descriptorType = vk::DescriptorType::eUniformBuffer;
|
| 553 | + uboLayoutBinding.descriptorCount = 1;
|
| 554 | + uboLayoutBinding.stageFlags = vk::ShaderStageFlagBits::eVertex;
|
| 555 | +
|
| 556 | + vk::DescriptorSetLayoutCreateInfo layoutInfo = {};
|
| 557 | + layoutInfo.bindingCount = 1;
|
| 558 | + layoutInfo.pBindings = &uboLayoutBinding;
|
| 559 | +
|
| 560 | + descriptorSetLayout = device.createDescriptorSetLayout(layoutInfo);
|
| 561 | +#pragma endregion
|
| 562 | +
|
| 538 | 563 | #pragma region gfxPipeline
|
| 539 | 564 | Shaders shader(&device);
|
| 540 | 565 | std::vector<vk::PipelineShaderStageCreateInfo> shaderStages = shader.GetShaderStages();
|
| | @@ -578,7 +603,7 @@ |
| 578 | 603 | rasterizer.rasterizerDiscardEnable = vk::False;
|
| 579 | 604 | rasterizer.polygonMode = vk::PolygonMode::eFill;
|
| 580 | 605 | rasterizer.cullMode = vk::CullModeFlagBits::eBack;
|
| 581 | | - rasterizer.frontFace = vk::FrontFace::eClockwise;
|
| 606 | + rasterizer.frontFace = vk::FrontFace::eCounterClockwise;
|
| 582 | 607 | rasterizer.depthBiasEnable = vk::False;
|
| 583 | 608 | rasterizer.lineWidth = 1.f;
|
| 584 | 609 |
|
| | @@ -606,10 +631,11 @@ |
| 606 | 631 | colorBlending.pAttachments = &colorBlendAttachment;
|
| 607 | 632 |
|
| 608 | 633 | vk::PipelineLayoutCreateInfo pipelineLayoutInfo = {};
|
| 609 | | - pipelineLayoutInfo.setLayoutCount = 0;
|
| 634 | + pipelineLayoutInfo.setLayoutCount = 1;
|
| 635 | + pipelineLayoutInfo.pSetLayouts = &descriptorSetLayout;
|
| 610 | 636 | pipelineLayoutInfo.pushConstantRangeCount = 0;
|
| 611 | 637 |
|
| 612 | | - vk::PipelineLayout pipelineLayout = device.createPipelineLayout(pipelineLayoutInfo);
|
| 638 | + pipelineLayout = device.createPipelineLayout(pipelineLayoutInfo);
|
| 613 | 639 | #pragma endregion
|
| 614 | 640 |
|
| 615 | 641 | #pragma region pipelineRendering
|
| | @@ -682,6 +708,58 @@ |
| 682 | 708 | }
|
| 683 | 709 | #pragma endregion
|
| 684 | 710 |
|
| 711 | +#pragma region uniformBuffers
|
| 712 | + for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; ++i) {
|
| 713 | + vk::DeviceSize bufSize = sizeof(UniformBufferObject);
|
| 714 | + vk::MemoryPropertyFlags flags = vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent;
|
| 715 | + auto [buf, bufMem] = createBuffer(bufSize, vk::BufferUsageFlagBits::eUniformBuffer, flags);
|
| 716 | + uniformBufs.emplace_back(std::move(buf));
|
| 717 | + uniformBufsMem.emplace_back(std::move(bufMem));
|
| 718 | + uniformBufsMapped.emplace_back(device.mapMemory(uniformBufsMem.back(), 0, bufSize));
|
| 719 | + }
|
| 720 | +#pragma endregion
|
| 721 | +
|
| 722 | +#pragma region descriptorPool
|
| 723 | + vk::DescriptorPoolSize descPoolSize = {};
|
| 724 | + descPoolSize.type = vk::DescriptorType::eUniformBuffer;
|
| 725 | + descPoolSize.descriptorCount = MAX_FRAMES_IN_FLIGHT;
|
| 726 | +
|
| 727 | + vk::DescriptorPoolCreateInfo descPoolInfo = {};
|
| 728 | + descPoolInfo.flags = vk::DescriptorPoolCreateFlagBits::eFreeDescriptorSet;
|
| 729 | + descPoolInfo.maxSets = MAX_FRAMES_IN_FLIGHT;
|
| 730 | + descPoolInfo.poolSizeCount = 1;
|
| 731 | + descPoolInfo.pPoolSizes = &descPoolSize;
|
| 732 | +
|
| 733 | + descriptorPool = device.createDescriptorPool(descPoolInfo);
|
| 734 | +#pragma endregion
|
| 735 | +
|
| 736 | +#pragma region descriptorSets
|
| 737 | + std::vector<vk::DescriptorSetLayout> layouts{MAX_FRAMES_IN_FLIGHT, descriptorSetLayout};
|
| 738 | + vk::DescriptorSetAllocateInfo descAllocInfo = {};
|
| 739 | + descAllocInfo.descriptorPool = descriptorPool;
|
| 740 | + descAllocInfo.descriptorSetCount = static_cast<uint32_t>(layouts.size());
|
| 741 | + descAllocInfo.pSetLayouts = layouts.data();
|
| 742 | +
|
| 743 | + descriptorSets = device.allocateDescriptorSets(descAllocInfo);
|
| 744 | +
|
| 745 | + for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; ++i) {
|
| 746 | + vk::DescriptorBufferInfo descBufInfo = {};
|
| 747 | + descBufInfo.buffer = uniformBufs[i];
|
| 748 | + descBufInfo.offset = 0;
|
| 749 | + descBufInfo.range = sizeof(UniformBufferObject);
|
| 750 | +
|
| 751 | + vk::WriteDescriptorSet descWrite = {};
|
| 752 | + descWrite.dstSet = descriptorSets[i];
|
| 753 | + descWrite.dstBinding = 0;
|
| 754 | + descWrite.dstArrayElement = 0;
|
| 755 | + descWrite.descriptorCount = 1;
|
| 756 | + descWrite.descriptorType = vk::DescriptorType::eUniformBuffer;
|
| 757 | + descWrite.pBufferInfo = &descBufInfo;
|
| 758 | +
|
| 759 | + device.updateDescriptorSets(descWrite, {});
|
| 760 | + }
|
| 761 | +#pragma endregion
|
| 762 | +
|
| 685 | 763 | #pragma region commandbuffers
|
| 686 | 764 | vk::CommandBufferAllocateInfo allocInfo = {};
|
| 687 | 765 | allocInfo.commandPool = cmdPool;
|
| | @@ -750,6 +828,19 @@ |
| 750 | 828 |
|
| 751 | 829 | vk::PipelineStageFlags waitStage = vk::PipelineStageFlagBits::eColorAttachmentOutput;
|
| 752 | 830 |
|
| 831 | +#pragma region updateUniformBuffer
|
| 832 | + static auto startTime = std::chrono::high_resolution_clock::now();
|
| 833 | + auto curTime = std::chrono::high_resolution_clock::now();
|
| 834 | + float time = std::chrono::duration<float, std::chrono::seconds::period>(curTime - startTime).count();
|
| 835 | +
|
| 836 | + UniformBufferObject ubo = {};
|
| 837 | + ubo.model = rotate(glm::mat4(1.f), time * glm::radians(90.f), glm::vec3(0.f, 0.f, 1.f));
|
| 838 | + ubo.view = lookAt(glm::vec3(2.f, 2.f, 2.f), glm::vec3(0.f, 0.f, 0.f), glm::vec3(0.f, 0.f, 1.f));
|
| 839 | + ubo.proj = glm::perspective(glm::radians(45.f), static_cast<float>(swapChainExtent.width) / static_cast<float>(swapChainExtent.height), .1f, 10.f);
|
| 840 | + ubo.proj[1][1] *= -1; // -Y軸
|
| 841 | + memcpy(uniformBufsMapped[frameIdx], &ubo, sizeof(ubo));
|
| 842 | +#pragma endregion
|
| 843 | +
|
| 753 | 844 | vk::SubmitInfo submitInfo = {};
|
| 754 | 845 | submitInfo.waitSemaphoreCount = 1;
|
| 755 | 846 | submitInfo.pWaitSemaphores = &presentCompleteSemaphores[frameIdx];
|
| | @@ -789,9 +880,13 @@ |
| 789 | 880 | if (idxBufMem) device.freeMemory(idxBufMem);
|
| 790 | 881 | if (vertBuf) device.destroyBuffer(vertBuf);
|
| 791 | 882 | if (vertBufMem) device.freeMemory(vertBufMem);
|
| 883 | + for (const auto &it : uniformBufs) device.destroyBuffer(it);
|
| 884 | + for (const auto &it : uniformBufsMem) device.freeMemory(it);
|
| 792 | 885 | if (gfxPipeline) device.destroyPipeline(gfxPipeline);
|
| 793 | 886 | if (pipelineLayout) device.destroyPipelineLayout(pipelineLayout);
|
| 794 | 887 | shader.Finalize();
|
| 888 | + if (descriptorSetLayout) device.destroyDescriptorSetLayout(descriptorSetLayout);
|
| 889 | + if (descriptorPool) device.destroyDescriptorPool(descriptorPool);
|
| 795 | 890 | cleanupSwapChain();
|
| 796 | 891 | for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; ++i) device.destroySemaphore(presentCompleteSemaphores[i]);
|
| 797 | 892 | for (size_t i = 0; i < swapChainImages.size(); ++i) device.destroySemaphore(renderFinishedSemaphores[i]);
|