vengine  0.0.1
3D graphics engine
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(!m_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(m_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 buffer");
76 }
77 if (const VkResult result = m_swapChain->submitCommandBuffers(&commandBuffer, &m_currentImageIndex); result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR || m_window.wasWindowResized()) {
78 m_window.resetWindowResizedFlag();
79 recreateSwapChain();
80 }
81 else if (result != VK_SUCCESS) {
82 throw std::runtime_error("Failed to submit command buffer");
83 }
84
85 m_isFrameStarted = false;
86 m_currentFrameIndex = (m_currentFrameIndex + 1) % MAX_FRAMES_IN_FLIGHT;
87}
88
89void ven::Renderer::beginSwapChainRenderPass(const VkCommandBuffer commandBuffer) const
90{
91 assert(m_isFrameStarted && "Can't begin render pass when frame not in progress");
92 assert(commandBuffer == getCurrentCommandBuffer() && "Can't begin render pass on command m_buffer from a different frame");
93
94 VkRenderPassBeginInfo renderPassInfo{};
95 renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
96 renderPassInfo.renderPass = m_swapChain->getRenderPass();
97 renderPassInfo.framebuffer = m_swapChain->getFrameBuffer(m_currentImageIndex);
98
99 renderPassInfo.renderArea.offset = {.x=0, .y=0};
100 renderPassInfo.renderArea.extent = m_swapChain->getSwapChainExtent();
101
102 renderPassInfo.clearValueCount = static_cast<uint32_t>(m_clearValues.size());
103 renderPassInfo.pClearValues = m_clearValues.data();
104
105 vkCmdBeginRenderPass(commandBuffer, &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
106
107 VkViewport viewport{};
108 viewport.x = 0.0F;
109 viewport.y = 0.0F;
110 viewport.width = static_cast<float>(m_swapChain->getSwapChainExtent().width);
111 viewport.height = static_cast<float>(m_swapChain->getSwapChainExtent().height);
112 viewport.minDepth = 0.0F;
113 viewport.maxDepth = 1.0F;
114 const VkRect2D scissor{{0, 0}, m_swapChain->getSwapChainExtent()};
115 vkCmdSetViewport(commandBuffer, 0, 1, &viewport);
116 vkCmdSetScissor(commandBuffer, 0, 1, &scissor);
117}
118
119void ven::Renderer::endSwapChainRenderPass(const VkCommandBuffer commandBuffer) const
120{
121 assert(m_isFrameStarted && "Can't end render pass when frame not in progress");
122 assert(commandBuffer == getCurrentCommandBuffer() && "Can't end render pass on command m_buffer from a different frame");
123
124 vkCmdEndRenderPass(commandBuffer);
125}
This file contains the Renderer class.
VkDevice device() const
Definition Device.hpp:54
VkCommandPool getCommandPool() const
Definition Device.hpp:53
VkCommandBuffer beginFrame()
Definition renderer.cpp:43
void endSwapChainRenderPass(VkCommandBuffer commandBuffer) const
Definition renderer.cpp:119
Device & m_device
Definition Renderer.hpp:64
void createCommandBuffers()
Definition renderer.cpp:3
void beginSwapChainRenderPass(VkCommandBuffer commandBuffer) const
Definition renderer.cpp:89
void freeCommandBuffers()
Definition renderer.cpp:17
void recreateSwapChain()
Definition renderer.cpp:23
std::vector< VkCommandBuffer > m_commandBuffers
Definition Renderer.hpp:66
void endFrame()
Definition renderer.cpp:69
static constexpr int MAX_FRAMES_IN_FLIGHT
Definition SwapChain.hpp:15