19 std::array<VkImageView, 3> attachments = {
25 VkFramebufferCreateInfo framebufferInfo{};
26 framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
28 framebufferInfo.attachmentCount =
static_cast<uint32_t
>(attachments.size());
29 framebufferInfo.pAttachments = attachments.data();
32 framebufferInfo.layers = 1;
35 throw std::runtime_error(
"Failed to create framebuffer !");
68 VkCommandBufferBeginInfo beginInfo{};
69 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
71 if (vkBeginCommandBuffer(commandBuffer, &beginInfo) != VK_SUCCESS) {
72 throw std::runtime_error(
"Failed to begin recording command buffer !");
75 VkRenderPassBeginInfo renderPassInfo{};
76 renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
79 renderPassInfo.renderArea.offset = {0, 0};
82 std::array<VkClearValue, 2> clearValues{};
83 clearValues[0].color = {{0.0f, 0.0f, 0.0f, 1.0f}};
84 clearValues[1].depthStencil = {1.0f, 0};
86 renderPassInfo.clearValueCount =
static_cast<uint32_t
>(clearValues.size());
87 renderPassInfo.pClearValues = clearValues.data();
89 vkCmdBeginRenderPass(commandBuffer, &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
91 vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS,
graphicsPipeline);
93 VkViewport viewport{};
98 viewport.minDepth = 0.0f;
99 viewport.maxDepth = 1.0f;
100 vkCmdSetViewport(commandBuffer, 0, 1, &viewport);
103 scissor.offset = {0, 0};
105 vkCmdSetScissor(commandBuffer, 0, 1, &scissor);
108 VkDeviceSize offsets[] = {0};
109 vkCmdBindVertexBuffers(commandBuffer, 0, 1, vertexBuffers, offsets);
111 vkCmdBindIndexBuffer(commandBuffer,
indexBuffer, 0, VK_INDEX_TYPE_UINT32);
115 vkCmdDrawIndexed(commandBuffer,
static_cast<uint32_t
>(
indices.size()), 1, 0, 0, 0);
117 vkCmdEndRenderPass(commandBuffer);
119 if (vkEndCommandBuffer(commandBuffer) != VK_SUCCESS) {
120 throw std::runtime_error(
"Failed to record command buffer !");
152 if (result == VK_ERROR_OUT_OF_DATE_KHR) {
155 }
else if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR) {
156 throw std::runtime_error(
"Failed to acquire swap chain image !");
166 VkSubmitInfo submitInfo{};
167 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
170 VkPipelineStageFlags waitStages[] = {VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT};
171 submitInfo.waitSemaphoreCount = 1;
172 submitInfo.pWaitSemaphores = waitSemaphores;
173 submitInfo.pWaitDstStageMask = waitStages;
175 submitInfo.commandBufferCount = 1;
179 submitInfo.signalSemaphoreCount = 1;
180 submitInfo.pSignalSemaphores = signalSemaphores;
183 throw std::runtime_error(
"Failed to submit draw command buffer !");
186 VkPresentInfoKHR presentInfo{};
187 presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
189 presentInfo.waitSemaphoreCount = 1;
190 presentInfo.pWaitSemaphores = signalSemaphores;
192 VkSwapchainKHR swapChains[] = {
swapChain};
193 presentInfo.swapchainCount = 1;
194 presentInfo.pSwapchains = swapChains;
196 presentInfo.pImageIndices = &imageIndex;
200 if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR ||
framebufferResized) {
203 }
else if (result != VK_SUCCESS) {
204 throw std::runtime_error(
"Failed to present swap chain image !");
211 VkCommandBufferAllocateInfo allocInfo{};
212 allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
213 allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
215 allocInfo.commandBufferCount = 1;
217 VkCommandBuffer commandBuffer;
218 vkAllocateCommandBuffers(
device, &allocInfo, &commandBuffer);
220 VkCommandBufferBeginInfo beginInfo{};
221 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
222 beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
224 vkBeginCommandBuffer(commandBuffer, &beginInfo);
226 return commandBuffer;
230 vkEndCommandBuffer(commandBuffer);
232 VkSubmitInfo submitInfo{};
233 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
234 submitInfo.commandBufferCount = 1;
235 submitInfo.pCommandBuffers = &commandBuffer;
237 vkQueueSubmit(
graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE);
void createImage(uint32_t width, uint32_t height, uint32_t mipLevels, VkSampleCountFlagBits numSamples, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties, VkImage &image, VkDeviceMemory &imageMemory)