Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2023
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.98 KB | None | 0 0
  1. void VulkanEngine::draw()
  2. {
  3.     Frame& curFrame = getCurrentFrame();
  4.  
  5.     vkWaitForFences(m_device, 1, &curFrame.fence, true, 1000000000);
  6.     vkResetFences(m_device, 1, &curFrame.fence);
  7.  
  8.     uint32_t swapchainImageIndex;
  9.     vkAcquireNextImageKHR(m_device, m_swapchain, 1000000000, curFrame.present, nullptr, &swapchainImageIndex);
  10.  
  11.     vkResetCommandBuffer(curFrame.commandBuffer, 0);
  12.  
  13.     VkCommandBuffer cmd = curFrame.commandBuffer;
  14.  
  15.     VkCommandBufferBeginInfo cmdBeginInfo = {};
  16.     cmdBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
  17.     cmdBeginInfo.pNext = nullptr;
  18.     cmdBeginInfo.pInheritanceInfo = nullptr;
  19.     cmdBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
  20.  
  21.     vkBeginCommandBuffer(cmd, &cmdBeginInfo);
  22.  
  23.     //-------------- G-Buffer ---------------------------------------------------------------------------------------------------------------------------  
  24.  
  25.     VkClearValue colorClear, depthClear;
  26.     colorClear.color = {{0.0f, 0.0f, 0.0f, 0.0f}};
  27.     depthClear.depthStencil.depth = 1.0f;
  28.  
  29.     VkClearValue clearValues[] = {colorClear, depthClear};
  30.  
  31.     VkRenderPassBeginInfo gBufferInfo = {};
  32.     gBufferInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
  33.     gBufferInfo.pNext = nullptr;
  34.     gBufferInfo.renderPass = m_renderPasses.at(0).pass;
  35.     gBufferInfo.renderArea.offset.x = 0;
  36.     gBufferInfo.renderArea.offset.y = 0;
  37.     gBufferInfo.renderArea.extent = c_windowSize;
  38.     gBufferInfo.framebuffer = m_renderPasses.at(0).framebuffers.front();
  39.     gBufferInfo.clearValueCount = 2;
  40.     gBufferInfo.pClearValues = &clearValues[0];
  41.  
  42.     const Pipeline& gPipeline = m_pipelines.at(0);
  43.  
  44.     vkCmdBeginRenderPass(cmd, &gBufferInfo, VK_SUBPASS_CONTENTS_INLINE);
  45.  
  46.     m_allocator.copy(&m_camera, sizeof(Camera), curFrame.cameraBuffer.allocation);
  47.  
  48.     vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, gPipeline.pipeline);
  49.  
  50.     vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, gPipeline.layout, 0, 1, &curFrame.descriptor, 0, nullptr);
  51.  
  52.     for (const auto& model : m_scene)
  53.     {
  54.         vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, gPipeline.layout, 1, 1, model.mesh->getTextureSet(), 0, nullptr);
  55.  
  56.         VkDeviceSize offset = 0;
  57.  
  58.         vkCmdBindVertexBuffers(cmd, 0, 1, &model.mesh->getVerticesBuffer().handle, &offset);
  59.         vkCmdBindIndexBuffer(cmd, model.mesh->getIndicesBuffer().handle, 0, VK_INDEX_TYPE_UINT16);
  60.  
  61.         vkCmdPushConstants(cmd, gPipeline.layout, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(model.matrix), &model.matrix);
  62.  
  63.         vkCmdDrawIndexed(cmd, model.mesh->getFacesNum(), 1, 0, 0, 0);
  64.     }
  65.  
  66.     vkCmdEndRenderPass(cmd);
  67.  
  68.     //-------------- Lighting ---------------------------------------------------------------------------------------------------------------------------
  69.  
  70.     VkRenderPassBeginInfo lighthingInfo = {};
  71.     lighthingInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
  72.     lighthingInfo.pNext = nullptr;
  73.     lighthingInfo.renderPass = m_renderPasses.at(1).pass;
  74.     lighthingInfo.renderArea.offset.x = 0;
  75.     lighthingInfo.renderArea.offset.y = 0;
  76.     lighthingInfo.renderArea.extent = c_windowSize;
  77.     lighthingInfo.framebuffer = m_renderPasses.at(1).framebuffers.at(swapchainImageIndex);
  78.     lighthingInfo.clearValueCount = 1;
  79.     lighthingInfo.pClearValues = &colorClear;
  80.  
  81.     const Pipeline& lPipeline = m_pipelines.at(1);
  82.  
  83.     vkCmdBeginRenderPass(cmd, &lighthingInfo, VK_SUBPASS_CONTENTS_INLINE);
  84.  
  85.     vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, lPipeline.pipeline);
  86.  
  87.     vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, lPipeline.layout, 1, 1, &m_gBufferTexturesSet, 0, nullptr);
  88.     vkCmdPushConstants(cmd, lPipeline.layout, VK_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof(int), &m_mode);
  89.  
  90.     vkCmdDraw(cmd, 6, 1, 0, 0);
  91.  
  92.     vkCmdEndRenderPass(cmd);
  93.  
  94.     vkEndCommandBuffer(cmd);
  95.  
  96.     //----------------------------------------------------------------------------------------------------------------------------------------------------
  97.  
  98.     VkPipelineStageFlags waitStage = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
  99.     VkSubmitInfo submit = {};
  100.     submit.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
  101.     submit.pNext = nullptr;
  102.     submit.pWaitDstStageMask = &waitStage;
  103.     submit.waitSemaphoreCount = 1;
  104.     submit.pWaitSemaphores = &curFrame.present;
  105.     submit.signalSemaphoreCount = 1;
  106.     submit.pSignalSemaphores = &curFrame.render;
  107.     submit.commandBufferCount = 1;
  108.     submit.pCommandBuffers = &cmd;
  109.  
  110.     vkQueueSubmit(m_queue, 1, &submit, curFrame.fence);
  111.  
  112.     VkPresentInfoKHR presentInfo = {};
  113.     presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
  114.     presentInfo.pNext = nullptr;
  115.     presentInfo.pSwapchains = &m_swapchain;
  116.     presentInfo.swapchainCount = 1;
  117.     presentInfo.pWaitSemaphores = &curFrame.render;
  118.     presentInfo.waitSemaphoreCount = 1;
  119.     presentInfo.pImageIndices = &swapchainImageIndex;
  120.  
  121.     vkQueuePresentKHR(m_queue, &presentInfo);
  122.  
  123.     m_frameNumber++;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement