Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void VulkanEngine::draw()
- {
- Frame& curFrame = getCurrentFrame();
- vkWaitForFences(m_device, 1, &curFrame.fence, true, 1000000000);
- vkResetFences(m_device, 1, &curFrame.fence);
- uint32_t swapchainImageIndex;
- vkAcquireNextImageKHR(m_device, m_swapchain, 1000000000, curFrame.present, nullptr, &swapchainImageIndex);
- vkResetCommandBuffer(curFrame.commandBuffer, 0);
- VkCommandBuffer cmd = curFrame.commandBuffer;
- VkCommandBufferBeginInfo cmdBeginInfo = {};
- cmdBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
- cmdBeginInfo.pNext = nullptr;
- cmdBeginInfo.pInheritanceInfo = nullptr;
- cmdBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
- vkBeginCommandBuffer(cmd, &cmdBeginInfo);
- //-------------- G-Buffer ---------------------------------------------------------------------------------------------------------------------------
- VkClearValue colorClear, depthClear;
- colorClear.color = {{0.0f, 0.0f, 0.0f, 0.0f}};
- depthClear.depthStencil.depth = 1.0f;
- VkClearValue clearValues[] = {colorClear, depthClear};
- VkRenderPassBeginInfo gBufferInfo = {};
- gBufferInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
- gBufferInfo.pNext = nullptr;
- gBufferInfo.renderPass = m_renderPasses.at(0).pass;
- gBufferInfo.renderArea.offset.x = 0;
- gBufferInfo.renderArea.offset.y = 0;
- gBufferInfo.renderArea.extent = c_windowSize;
- gBufferInfo.framebuffer = m_renderPasses.at(0).framebuffers.front();
- gBufferInfo.clearValueCount = 2;
- gBufferInfo.pClearValues = &clearValues[0];
- const Pipeline& gPipeline = m_pipelines.at(0);
- vkCmdBeginRenderPass(cmd, &gBufferInfo, VK_SUBPASS_CONTENTS_INLINE);
- m_allocator.copy(&m_camera, sizeof(Camera), curFrame.cameraBuffer.allocation);
- vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, gPipeline.pipeline);
- vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, gPipeline.layout, 0, 1, &curFrame.descriptor, 0, nullptr);
- for (const auto& model : m_scene)
- {
- vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, gPipeline.layout, 1, 1, model.mesh->getTextureSet(), 0, nullptr);
- VkDeviceSize offset = 0;
- vkCmdBindVertexBuffers(cmd, 0, 1, &model.mesh->getVerticesBuffer().handle, &offset);
- vkCmdBindIndexBuffer(cmd, model.mesh->getIndicesBuffer().handle, 0, VK_INDEX_TYPE_UINT16);
- vkCmdPushConstants(cmd, gPipeline.layout, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(model.matrix), &model.matrix);
- vkCmdDrawIndexed(cmd, model.mesh->getFacesNum(), 1, 0, 0, 0);
- }
- vkCmdEndRenderPass(cmd);
- //-------------- Lighting ---------------------------------------------------------------------------------------------------------------------------
- VkRenderPassBeginInfo lighthingInfo = {};
- lighthingInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
- lighthingInfo.pNext = nullptr;
- lighthingInfo.renderPass = m_renderPasses.at(1).pass;
- lighthingInfo.renderArea.offset.x = 0;
- lighthingInfo.renderArea.offset.y = 0;
- lighthingInfo.renderArea.extent = c_windowSize;
- lighthingInfo.framebuffer = m_renderPasses.at(1).framebuffers.at(swapchainImageIndex);
- lighthingInfo.clearValueCount = 1;
- lighthingInfo.pClearValues = &colorClear;
- const Pipeline& lPipeline = m_pipelines.at(1);
- vkCmdBeginRenderPass(cmd, &lighthingInfo, VK_SUBPASS_CONTENTS_INLINE);
- vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, lPipeline.pipeline);
- vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, lPipeline.layout, 1, 1, &m_gBufferTexturesSet, 0, nullptr);
- vkCmdPushConstants(cmd, lPipeline.layout, VK_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof(int), &m_mode);
- vkCmdDraw(cmd, 6, 1, 0, 0);
- vkCmdEndRenderPass(cmd);
- vkEndCommandBuffer(cmd);
- //----------------------------------------------------------------------------------------------------------------------------------------------------
- VkPipelineStageFlags waitStage = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
- VkSubmitInfo submit = {};
- submit.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
- submit.pNext = nullptr;
- submit.pWaitDstStageMask = &waitStage;
- submit.waitSemaphoreCount = 1;
- submit.pWaitSemaphores = &curFrame.present;
- submit.signalSemaphoreCount = 1;
- submit.pSignalSemaphores = &curFrame.render;
- submit.commandBufferCount = 1;
- submit.pCommandBuffers = &cmd;
- vkQueueSubmit(m_queue, 1, &submit, curFrame.fence);
- VkPresentInfoKHR presentInfo = {};
- presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
- presentInfo.pNext = nullptr;
- presentInfo.pSwapchains = &m_swapchain;
- presentInfo.swapchainCount = 1;
- presentInfo.pWaitSemaphores = &curFrame.render;
- presentInfo.waitSemaphoreCount = 1;
- presentInfo.pImageIndices = &swapchainImageIndex;
- vkQueuePresentKHR(m_queue, &presentInfo);
- m_frameNumber++;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement