vengine  0.1.0
3D graphics engine made with Vulkan
Loading...
Searching...
No Matches
renderer.cpp
Go to the documentation of this file.
2
4{
6 VkCommandBufferAllocateInfo allocInfo{};
7 allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
8 allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
9 allocInfo.commandPool = m_device.getCommandPool();
10 allocInfo.commandBufferCount = static_cast<uint32_t>(m_commandBuffers.size());
11
12 if (vkAllocateCommandBuffers(m_device.device(), &allocInfo, m_commandBuffers.data()) != VK_SUCCESS) {
13 throw std::runtime_error("Failed to allocate command buffers");
14 }
15}
16
18{
19 vkFreeCommandBuffers(m_device.device(), m_device.getCommandPool(), static_cast<uint32_t>(m_commandBuffers.size()), m_commandBuffers.data());
20 m_commandBuffers.clear();
21}
22
24{
25 VkExtent2D extent = m_window.getExtent();
26 while (extent.width == 0 || extent.height == 0) {
27 extent = m_window.getExtent();
28 glfwWaitEvents();
29 }
30 vkDeviceWaitIdle(m_device.device());
31 if (m_swapChain == nullptr) {
32 m_swapChain = std::make_unique<SwapChain>(m_device, extent);
33 } else {
34 std::shared_ptr<SwapChain> oldSwapChain = std::move(m_swapChain);
35 m_swapChain = std::make_unique<SwapChain>(m_device, extent, oldSwapChain);
36 if (!oldSwapChain->compareSwapFormats(*m_swapChain)) {
37 throw std::runtime_error("Swap chain image/depth format changed");
38 }
39 }
40 // well be back
41}
42
44{
45 assert(!isFrameStarted && "Can't start new frame while previous one is still in progress");
46
47 const VkResult result = m_swapChain->acquireNextImage(&m_currentImageIndex);
48 if (result == VK_ERROR_OUT_OF_DATE_KHR) {
49 recreateSwapChain();
50 return nullptr;
51 }
52
53 if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR) {
54 throw std::runtime_error("Failed to acquire swap chain image");
55 }
56
57 m_isFrameStarted = true;
58
59 VkCommandBuffer_T *commandBuffer = getCurrentCommandBuffer();
60 VkCommandBufferBeginInfo beginInfo{};
61 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
62
63 if (vkBeginCommandBuffer(commandBuffer, &beginInfo) != VK_SUCCESS) {
64 throw std::runtime_error("Failed to begin recording command m_buffer");
65 }
66 return commandBuffer;
67}
68
70{
71 assert(isFrameStarted && "Can't end frame that hasn't been started");
72
73 VkCommandBuffer_T *commandBuffer = getCurrentCommandBuffer();
74 if (vkEndCommandBuffer(commandBuffer) != VK_SUCCESS) {
75 throw std::runtime_error("Failed to record command m_buffer");
76 }
77 VkResult result = m_swapChain->submitCommandBuffers(&commandBuffer, &m_currentImageIndex);
78 if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR || m_window.wasWindowResized()) {
79 m_window.resetWindowResizedFlag();
80 recreateSwapChain();
81 }
82 else if (result != VK_SUCCESS) {
83 throw std::runtime_error("Failed to submit command m_buffer");
84 }
85
86 m_isFrameStarted = false;
87 m_currentFrameIndex = (m_currentFrameIndex + 1) % SwapChain::MAX_FRAMES_IN_FLIGHT;
88}
89
90void ven::Renderer::beginSwapChainRenderPass(const VkCommandBuffer commandBuffer) const
91{
92 assert(isFrameStarted && "Can't begin render pass when frame not in progress");
93 assert(commandBuffer == getCurrentCommandBuffer() && "Can't begin render pass on command m_buffer from a different frame");
94
95 VkRenderPassBeginInfo renderPassInfo{};
96 renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
97 renderPassInfo.renderPass = m_swapChain->getRenderPass();
98 renderPassInfo.framebuffer = m_swapChain->getFrameBuffer(m_currentImageIndex);
99
100 renderPassInfo.renderArea.offset = {0, 0};
101 renderPassInfo.renderArea.extent = m_swapChain->getSwapChainExtent();
102
103 std::array<VkClearValue, 2> clearValues{};
104 clearValues[0].color = {{0.01F, 0.01F, 0.01F, 1.0F}};
105 clearValues[1].depthStencil = {1.0F, 0};
106 renderPassInfo.clearValueCount = static_cast<uint32_t>(clearValues.size());
107 renderPassInfo.pClearValues = clearValues.data();
108
109 vkCmdBeginRenderPass(commandBuffer, &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
110
111 VkViewport viewport{};
112 viewport.x = 0.0F;
113 viewport.y = 0.0F;
114 viewport.width = static_cast<float>(m_swapChain->getSwapChainExtent().width);
115 viewport.height = static_cast<float>(m_swapChain->getSwapChainExtent().height);
116 viewport.minDepth = 0.0F;
117 viewport.maxDepth = 1.0F;
118 const VkRect2D scissor{{0, 0}, m_swapChain->getSwapChainExtent()};
119 vkCmdSetViewport(commandBuffer, 0, 1, &viewport);
120 vkCmdSetScissor(commandBuffer, 0, 1, &scissor);
121}
122
123void ven::Renderer::endSwapChainRenderPass(const VkCommandBuffer commandBuffer)
124{
125 assert(isFrameStarted && "Can't end render pass when frame not in progress");
126 assert(commandBuffer == getCurrentCommandBuffer() && "Can't end render pass on command m_buffer from a different frame");
127
128 vkCmdEndRenderPass(commandBuffer);
129}
This file contains the Renderer class.
VkDevice device() const
Definition Device.hpp:48
VkCommandPool getCommandPool() const
Definition Device.hpp:47
VkCommandBuffer beginFrame()
Definition renderer.cpp:43
Device & m_device
Definition Renderer.hpp:49
void createCommandBuffers()
Definition renderer.cpp:3
void beginSwapChainRenderPass(VkCommandBuffer commandBuffer) const
Definition renderer.cpp:90
void freeCommandBuffers()
Definition renderer.cpp:17
void recreateSwapChain()
Definition renderer.cpp:23
std::vector< VkCommandBuffer > m_commandBuffers
Definition Renderer.hpp:51
static void endSwapChainRenderPass(VkCommandBuffer commandBuffer)
Definition renderer.cpp:123
void endFrame()
Definition renderer.cpp:69
static constexpr int MAX_FRAMES_IN_FLIGHT
Definition SwapChain.hpp:20