Chronos 0.0
A advanced 2D rendering and animation system
Loading...
Searching...
No Matches
editorRenderer.cpp
Go to the documentation of this file.
1/*
2Copyright (c) 2024 Rahul Satish Vadhyar
3
4Permission is hereby granted, free of charge, to any person obtaining a copy
5of this software and associated documentation files (the "Software"), to deal
6in the Software without restriction, including without limitation the rights
7to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8copies of the Software, and to permit persons to whom the Software is
9furnished to do so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall be included in all
12copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20SOFTWARE.
21*/
22
23#include "logging.hpp"
24#include "editorHeaders.hpp"
25#include "editorRenderer.hpp"
26#include "helper.hpp"
27
29 GLFWwindow* window, Chronos::Engine::SwapChain* swapChain,
30 VkInstance instance, VkSurfaceKHR surface)
31{
32 this->device = device;
33 this->swapChain = swapChain;
34 this->surface = surface;
35 this->window = window;
36
38 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
39 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
40 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, false, false, true);
42 *device, *swapChain, renderPass, false);
43 // Setup Dear ImGui context
44 IMGUI_CHECKVERSION();
45 ImGui::CreateContext();
46 ImGuiIO& io = ImGui::GetIO();
47 (void)io;
48 io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
49 io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
50#ifdef WIN32
51 io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable;
52 LOG(3, "Viewports enabled")
53#endif
54 ImGui::StyleColorsDark();
55
56 std::array<VkDescriptorPoolSize, 1> poolSizes {};
57 poolSizes[0].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
58 poolSizes[0].descriptorCount = static_cast<uint32_t>(MAX_FRAMES_IN_FLIGHT);
59
60 VkDescriptorPoolSize pool_sizes[]
61 = { { VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 100 } };
62 VkDescriptorPoolCreateInfo pool_info = {};
63 pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
64 pool_info.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT;
65 pool_info.maxSets = 100;
66 pool_info.poolSizeCount = (uint32_t)IM_ARRAYSIZE(pool_sizes);
67 pool_info.pPoolSizes = pool_sizes;
68 vkCreateDescriptorPool(
69 device->device, &pool_info, nullptr, &descriptorPool);
70
71 ImGui_ImplGlfw_InitForVulkan(window, true);
72 ImGui_ImplVulkan_InitInfo init_info = {};
73 init_info.Instance = instance;
74 init_info.PhysicalDevice = device->physicalDevice;
75 init_info.Device = device->device;
76 init_info.QueueFamily
78 .graphicsFamily.value();
79 init_info.Queue = device->graphicsQueue;
80 init_info.PipelineCache = VK_NULL_HANDLE;
81 init_info.DescriptorPool = descriptorPool;
82 init_info.RenderPass = renderPass;
83 init_info.Allocator = nullptr;
84 init_info.MinImageCount = MAX_FRAMES_IN_FLIGHT;
85 init_info.ImageCount = MAX_FRAMES_IN_FLIGHT;
86 init_info.MSAASamples = VK_SAMPLE_COUNT_1_BIT;
87 init_info.CheckVkResultFn = nullptr; // add a fucntion to this
88 ImGui_ImplVulkan_Init(&init_info);
89
90 VkCommandPoolCreateInfo commandPoolCreateInfo = {};
91 commandPoolCreateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
92 commandPoolCreateInfo.queueFamilyIndex
94 .graphicsFamily.value();
95 commandPoolCreateInfo.flags
96 = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
97
98 if (vkCreateCommandPool(
99 device->device, &commandPoolCreateInfo, nullptr, &commandPool)
100 != VK_SUCCESS) {
101 LOG(1, "Could not create graphics command pool")
102 throw std::runtime_error("Could not create graphics command pool");
103 }
104
105 VkCommandBuffer command_buffer
107 ImGui_ImplVulkan_CreateFontsTexture();
109 &command_buffer, *device, commandPool);
110
112 VkCommandBufferAllocateInfo allocInfo {};
113 allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
114 allocInfo.commandPool = commandPool;
115 allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
116 allocInfo.commandBufferCount = static_cast<uint32_t>(commandBuffers.size());
117 if (vkAllocateCommandBuffers(
118 device->device, &allocInfo, commandBuffers.data())
119 != VK_SUCCESS) {
120 LOG(1, "failed to allocate command buffers!")
121 throw std::runtime_error("failed to allocate command buffers!");
122 }
123 LOG(3, "EditorRenderer initialized")
124}
125
127{
128 ImGui_ImplVulkan_Shutdown();
129 vkDestroyRenderPass(device->device, renderPass, nullptr);
130 for (auto framebuffer : framebuffers)
131 vkDestroyFramebuffer(device->device, framebuffer, nullptr);
132 vkDestroyCommandPool(device->device, commandPool, nullptr);
133 vkDestroyDescriptorPool(device->device, descriptorPool, nullptr);
134 LOG(3, "EditorRenderer destroyed")
135}
136
138{
139 ImGui_ImplVulkan_NewFrame();
140 ImGui_ImplGlfw_NewFrame();
141 ImGui::NewFrame();
142 this->addElements();
143 ImGui::Render();
144 LOG(4, "EditorRenderer updated")
145}
146
148 uint32_t currentFrame, uint32_t imageIndex, float bgColor[3])
149{
150 vkResetCommandBuffer(commandBuffers[currentFrame], 0);
151 VkCommandBufferBeginInfo beginInfo {};
152 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
153 VkClearValue clearColor
154 = { { { bgColor[0], bgColor[1], bgColor[2], 1.0f } } };
155 vkBeginCommandBuffer(commandBuffers[currentFrame], &beginInfo);
156 VkRenderPassBeginInfo info = {};
157 info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
158 info.renderPass = renderPass;
159 info.framebuffer = framebuffers[imageIndex];
160 info.renderArea.extent = swapChain->swapChainExtent;
161 info.clearValueCount = 1;
162 info.pClearValues = &clearColor;
163 vkCmdBeginRenderPass(
164 commandBuffers[currentFrame], &info, VK_SUBPASS_CONTENTS_INLINE);
165 ImGui_ImplVulkan_RenderDrawData(
166 ImGui::GetDrawData(), commandBuffers[currentFrame]);
167 vkCmdEndRenderPass(commandBuffers[currentFrame]);
168 vkEndCommandBuffer(commandBuffers[currentFrame]);
169 LOG(4, "EditorRenderer rendered")
170}
171
173{
174 for (auto framebuffer : framebuffers)
175 vkDestroyFramebuffer(device->device, framebuffer, nullptr);
176 LOG(3, "EditorRenderer cleaned up")
177}
178
180{
181 cleanup();
183 *device, *swapChain, renderPass, false);
184 LOG(3, "EditorRenderer recreated")
185}
186
188{
189#ifdef WIN32
190 if (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_ViewportsEnable) {
191 ImGui::UpdatePlatformWindows();
192 ImGui::RenderPlatformWindowsDefault();
193 }
194 LOG(4, "Rendered additional viewports")
195#endif
196}
197
199{
200 vkDestroyRenderPass(device->device, renderPass, nullptr);
201 renderPass = Chronos::Engine::createRenderPass(*device, *swapChain,
202 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
203 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
204 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, false, false, true);
205 recreate();
206}
Chronos::Engine::Device * device
std::vector< VkFramebuffer > framebuffers
std::vector< VkCommandBuffer > commandBuffers
void init(Chronos::Engine::Device *device, GLFWwindow *window, Chronos::Engine::SwapChain *swapChain, VkInstance instance, VkSurfaceKHR surface)
Chronos::Engine::SwapChain * swapChain
void render(uint32_t currentFrame, uint32_t imageIndex, float bgColor[3])
This initializes, manages and destroys the logical and physical devices(GPU).
Definition device.hpp:47
VkDevice device
This is the logical device that is used by Vulkan.
Definition device.hpp:52
VkPhysicalDevice physicalDevice
This is the physical device that is used by Vulkan.
Definition device.hpp:60
VkQueue graphicsQueue
This is the queue that is used for graphics rendering.
Definition device.hpp:65
std::vector< VkImageView > swapChainImageViews
Image views of the swapchain textures(images, aka window)
Contains all the editor headers.
void addElements()
Contains various common functions used by other classes in the Engine namespace.
#define LOG(LEVEL, MESSAGE)
Definition logging.hpp:60
VkCommandBuffer beginSingleTimeCommands(VkCommandPool commandPool, VkDevice device)
Begins recording of a command buffer that will be used once.
Definition helper.cpp:24
std::vector< VkFramebuffer > createFramebuffer(Chronos::Engine::Device device, Chronos::Engine::SwapChain swapChain, VkRenderPass renderPass, bool msaa)
Creates a set of framebuffers for use.
Definition helper.cpp:276
void endSingleTimeCommands(VkCommandBuffer *commandBuffer, Chronos::Engine::Device device, VkCommandPool commandPool)
Ends recording of single time command buffer and destroys it.
Definition helper.cpp:47
VkRenderPass createRenderPass(Chronos::Engine::Device device, Chronos::Engine::SwapChain swapChain, VkImageLayout initalLayout, VkImageLayout finalLayout, VkImageLayout msaaFinalLayout, bool msaa, bool clearFramebuffer, bool dependency)
Creates a render pass.
Definition helper.cpp:175
QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device, VkSurfaceKHR surface)
Gets the indices of the needed queue families.
Definition helper.cpp:126
std::optional< uint32_t > graphicsFamily
Definition helper.hpp:45
#define MAX_FRAMES_IN_FLIGHT
The number of frames in flight.