Cleaned up code

This commit is contained in:
Pasha Bibko
2025-11-16 17:25:03 +00:00
parent f4e77e30a8
commit e887b2b393
4 changed files with 57 additions and 42 deletions

View File

@@ -5,7 +5,7 @@
namespace PB::Renderer namespace PB::Renderer
{ {
void CleanupAllAndExit(int code) void CleanupAllAndExit(const int code)
{ {
if (!GLFWManager::Cleanup()) if (!GLFWManager::Cleanup())
std::exit(EXIT_FAILURE); std::exit(EXIT_FAILURE);
@@ -38,7 +38,7 @@ int main()
{ {
glfwPollEvents(); glfwPollEvents();
if (!VulkanManager::RenderPass(window)) if (!VulkanManager::RenderPass())
CleanupAllAndExit(EXIT_FAILURE); CleanupAllAndExit(EXIT_FAILURE);
} }

View File

@@ -43,12 +43,12 @@ namespace PB::Renderer
static bool CreateCommandBuffers(); static bool CreateCommandBuffers();
static void CreateSemaphores(); static void CreateSemaphores();
static bool RenderPass(GLFWwindow* window); static bool RenderPass();
private: private:
static bool IsDeviceSuitable(VkPhysicalDevice device, VkSurfaceKHR surface); static bool IsDeviceSuitable(const VkPhysicalDevice& device);
static QueueFamilyIndices FindQueueFamilies(VkPhysicalDevice device, VkSurfaceKHR surface); static QueueFamilyIndices FindQueueFamilies(const VkPhysicalDevice& device);
static bool CheckDeviceExtensionSupport(VkPhysicalDevice device); static bool CheckDeviceExtensionSupport(const VkPhysicalDevice& device);
static SwapChainSupportDetails QuerySwapChainSupport(); static SwapChainSupportDetails QuerySwapChainSupport();
static VkSurfaceFormatKHR ChooseSurfaceFormat(const std::vector<VkSurfaceFormatKHR>& availableFormats); static VkSurfaceFormatKHR ChooseSurfaceFormat(const std::vector<VkSurfaceFormatKHR>& availableFormats);

View File

@@ -14,45 +14,45 @@ namespace PB::Renderer
VkQueue VulkanManager::s_PresentQueue = VK_NULL_HANDLE; VkQueue VulkanManager::s_PresentQueue = VK_NULL_HANDLE;
VkSwapchainKHR VulkanManager::s_SwapChain = VK_NULL_HANDLE; VkSwapchainKHR VulkanManager::s_SwapChain = VK_NULL_HANDLE;
std::vector<VkImage> VulkanManager::s_SwapChainImages = {}; std::vector<VkImage> VulkanManager::s_SwapChainImages;
std::vector<VkImageView> VulkanManager::s_SwapChainImageViews = {}; std::vector<VkImageView> VulkanManager::s_SwapChainImageViews;
VkFormat VulkanManager::s_SwapChainImageFormat = {}; VkFormat VulkanManager::s_SwapChainImageFormat = {};
VkExtent2D VulkanManager::s_SwapChainExtent = {}; VkExtent2D VulkanManager::s_SwapChainExtent;
VkRenderPass VulkanManager::s_RenderPass = VK_NULL_HANDLE; VkRenderPass VulkanManager::s_RenderPass = VK_NULL_HANDLE;
std::vector<VkFramebuffer> VulkanManager::s_Framebuffers = {}; std::vector<VkFramebuffer> VulkanManager::s_Framebuffers;
VkPipelineLayout VulkanManager::s_PipelineLayout = {}; VkPipelineLayout VulkanManager::s_PipelineLayout = {};
VkPipeline VulkanManager::s_RenderPipeline = {}; VkPipeline VulkanManager::s_RenderPipeline = {};
VkCommandPool VulkanManager::s_CommandPool = VK_NULL_HANDLE; VkCommandPool VulkanManager::s_CommandPool = VK_NULL_HANDLE;
std::vector<VkCommandBuffer> VulkanManager::s_CommandBuffers = {}; std::vector<VkCommandBuffer> VulkanManager::s_CommandBuffers;
VkSemaphore VulkanManager::s_ImageAvailableSemaphore = VK_NULL_HANDLE; VkSemaphore VulkanManager::s_ImageAvailableSemaphore = VK_NULL_HANDLE;
VkSemaphore VulkanManager::s_RenderFinishedSemaphore = VK_NULL_HANDLE; VkSemaphore VulkanManager::s_RenderFinishedSemaphore = VK_NULL_HANDLE;
bool VulkanManager::InitAll(GLFWwindow* window) bool VulkanManager::InitAll(GLFWwindow* window)
{ {
if (const std::optional<VkInstance> instance = VulkanManager::Init(); !instance) if (const std::optional<VkInstance> instance = Init(); !instance)
return false; return false;
if (const std::optional<VkSurfaceKHR> surface = VulkanManager::CreateSurface(window); !surface) if (const std::optional<VkSurfaceKHR> surface = CreateSurface(window); !surface)
return false; return false;
if (!( if (!(
VulkanManager::PickPhysicalDevice() && PickPhysicalDevice() &&
VulkanManager::CreateLogicalDevice() && CreateLogicalDevice() &&
VulkanManager::CreateSwapChain(window) && CreateSwapChain(window) &&
VulkanManager::CreateImageViews() && CreateImageViews() &&
VulkanManager::CreateRenderPass() && CreateRenderPass() &&
VulkanManager::CreateFramebuffer() && CreateFramebuffer() &&
VulkanManager::CreateGraphicsPipeline() && CreateGraphicsPipeline() &&
VulkanManager::CreateCommandBuffers() CreateCommandBuffers()
)) { )) {
return false; return false;
} }
VulkanManager::CreateSemaphores(); CreateSemaphores();
return true; return true;
} }
@@ -136,7 +136,6 @@ namespace PB::Renderer
} }
const VkInstance& instance = s_Instance.value(); const VkInstance& instance = s_Instance.value();
const VkSurfaceKHR& surface = s_Surface.value();
uint32_t deviceCount = 0; uint32_t deviceCount = 0;
vkEnumeratePhysicalDevices(instance, &deviceCount, nullptr); vkEnumeratePhysicalDevices(instance, &deviceCount, nullptr);
@@ -151,10 +150,10 @@ namespace PB::Renderer
for (const auto& device : devices) for (const auto& device : devices)
{ {
if (IsDeviceSuitable(device, surface)) if (IsDeviceSuitable(device))
{ {
s_PhysicalDevice = device; s_PhysicalDevice = device;
s_QueueIndices = FindQueueFamilies(device, surface); s_QueueIndices = FindQueueFamilies(device);
VkPhysicalDeviceProperties deviceProperties; VkPhysicalDeviceProperties deviceProperties;
vkGetPhysicalDeviceProperties(device, &deviceProperties); vkGetPhysicalDeviceProperties(device, &deviceProperties);
@@ -169,9 +168,10 @@ namespace PB::Renderer
return false; return false;
} }
bool VulkanManager::IsDeviceSuitable(VkPhysicalDevice device, VkSurfaceKHR surface) bool VulkanManager::IsDeviceSuitable(const VkPhysicalDevice& device)
{ {
const QueueFamilyIndices indices = FindQueueFamilies(device, surface); const VkSurfaceKHR& surface = s_Surface.value();
const QueueFamilyIndices indices = FindQueueFamilies(device);
if (const bool extensionsSupported = CheckDeviceExtensionSupport(device); !extensionsSupported) if (const bool extensionsSupported = CheckDeviceExtensionSupport(device); !extensionsSupported)
return false; return false;
@@ -191,8 +191,9 @@ namespace PB::Renderer
return indices.Complete() && swapChainAdequate && discreteGPU; return indices.Complete() && swapChainAdequate && discreteGPU;
} }
QueueFamilyIndices VulkanManager::FindQueueFamilies(VkPhysicalDevice device, VkSurfaceKHR surface) QueueFamilyIndices VulkanManager::FindQueueFamilies(const VkPhysicalDevice& device)
{ {
const VkSurfaceKHR& surface = s_Surface.value();
QueueFamilyIndices indices; QueueFamilyIndices indices;
uint32_t queueFamilyCount; uint32_t queueFamilyCount;
@@ -226,9 +227,9 @@ namespace PB::Renderer
return indices; return indices;
} }
bool VulkanManager::CheckDeviceExtensionSupport(VkPhysicalDevice device) bool VulkanManager::CheckDeviceExtensionSupport(const VkPhysicalDevice& device)
{ {
const std::vector<const char*> REQUIRED_EXTENSIONS = { const std::vector REQUIRED_EXTENSIONS = {
VK_KHR_SWAPCHAIN_EXTENSION_NAME, VK_KHR_SWAPCHAIN_EXTENSION_NAME,
}; };
@@ -244,12 +245,11 @@ namespace PB::Renderer
} }
return required.empty(); return required.empty();
} }
bool VulkanManager::CreateLogicalDevice() bool VulkanManager::CreateLogicalDevice()
{ {
std::set<uint32_t> uniqueQueueFamilies = std::set uniqueQueueFamilies =
{ {
s_QueueIndices.graphicsFamily.value(), s_QueueIndices.graphicsFamily.value(),
s_QueueIndices.presentFamily.value() s_QueueIndices.presentFamily.value()
@@ -270,7 +270,7 @@ namespace PB::Renderer
VkPhysicalDeviceFeatures deviceFeatures{}; VkPhysicalDeviceFeatures deviceFeatures{};
const std::vector<const char*> extensions = { const std::vector extensions = {
VK_KHR_SWAPCHAIN_EXTENSION_NAME VK_KHR_SWAPCHAIN_EXTENSION_NAME
}; };
@@ -361,7 +361,7 @@ namespace PB::Renderer
SwapChainSupportDetails VulkanManager::QuerySwapChainSupport() SwapChainSupportDetails VulkanManager::QuerySwapChainSupport()
{ {
SwapChainSupportDetails details; SwapChainSupportDetails details;
const VkSurfaceKHR surface = s_Surface.value(); const VkSurfaceKHR& surface = s_Surface.value();
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(s_PhysicalDevice, surface, &details.capabilities); vkGetPhysicalDeviceSurfaceCapabilitiesKHR(s_PhysicalDevice, surface, &details.capabilities);
uint32_t formatCount; uint32_t formatCount;
@@ -614,7 +614,10 @@ namespace PB::Renderer
pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
if (vkCreatePipelineLayout(s_Device, &pipelineLayoutInfo, nullptr, &s_PipelineLayout) != VK_SUCCESS) if (vkCreatePipelineLayout(s_Device, &pipelineLayoutInfo, nullptr, &s_PipelineLayout) != VK_SUCCESS)
{
std::cout << "PB::Renderer::VulkanManager::CreateGraphicsPipeline(): Could not make pipeline layout" << std::endl;
return false; return false;
}
VkGraphicsPipelineCreateInfo pipelineInfo{}; VkGraphicsPipelineCreateInfo pipelineInfo{};
pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
@@ -631,7 +634,10 @@ namespace PB::Renderer
pipelineInfo.subpass = 0; pipelineInfo.subpass = 0;
if (vkCreateGraphicsPipelines(s_Device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &s_RenderPipeline) != VK_SUCCESS) if (vkCreateGraphicsPipelines(s_Device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &s_RenderPipeline) != VK_SUCCESS)
{
std::cout << "PB::Renderer::VulkanManager::CreateGraphicsPipeline(): Could not make graphics pipeline" << std::endl;
return false; return false;
}
vkDestroyShaderModule(s_Device, vertShaderModule, nullptr); vkDestroyShaderModule(s_Device, vertShaderModule, nullptr);
vkDestroyShaderModule(s_Device, fragShaderModule, nullptr); vkDestroyShaderModule(s_Device, fragShaderModule, nullptr);
@@ -651,7 +657,7 @@ namespace PB::Renderer
const size_t fileSize = file.tellg(); const size_t fileSize = file.tellg();
std::vector<char> buffer(fileSize); std::vector<char> buffer(fileSize);
file.seekg(0); file.seekg(0);
file.read(buffer.data(), fileSize); file.read(buffer.data(), static_cast<std::streamsize>(fileSize));
file.close(); file.close();
VkShaderModuleCreateInfo createInfo{}; VkShaderModuleCreateInfo createInfo{};
@@ -676,7 +682,10 @@ namespace PB::Renderer
poolInfo.queueFamilyIndex = s_QueueIndices.graphicsFamily.value(); poolInfo.queueFamilyIndex = s_QueueIndices.graphicsFamily.value();
if (vkCreateCommandPool(s_Device, &poolInfo, nullptr, &s_CommandPool) != VK_SUCCESS) if (vkCreateCommandPool(s_Device, &poolInfo, nullptr, &s_CommandPool) != VK_SUCCESS)
{
std::cout << "PB::Renderer::VulkanManager::CreateCommandBuffers(): Could not create command pool" << std::endl;
return false; return false;
}
s_CommandBuffers.resize(s_Framebuffers.size()); s_CommandBuffers.resize(s_Framebuffers.size());
@@ -686,8 +695,11 @@ namespace PB::Renderer
allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
allocInfo.commandBufferCount = static_cast<uint32_t>(s_CommandBuffers.size()); allocInfo.commandBufferCount = static_cast<uint32_t>(s_CommandBuffers.size());
if (vkAllocateCommandBuffers(s_Device, &allocInfo, s_CommandBuffers.data()) != VK_SUCCESS) // <- Crashes here if (vkAllocateCommandBuffers(s_Device, &allocInfo, s_CommandBuffers.data()) != VK_SUCCESS)
{
std::cout << "PB::Renderer::VulkanManager::CreateCommandBuffers(): Could not allocate command buffers" << std::endl;
return false; return false;
}
for (size_t i = 0; i < s_CommandBuffers.size(); i++) for (size_t i = 0; i < s_CommandBuffers.size(); i++)
{ {
@@ -716,7 +728,10 @@ namespace PB::Renderer
vkCmdEndRenderPass(s_CommandBuffers[i]); vkCmdEndRenderPass(s_CommandBuffers[i]);
if (vkEndCommandBuffer(s_CommandBuffers[i]) != VK_SUCCESS) if (vkEndCommandBuffer(s_CommandBuffers[i]) != VK_SUCCESS)
{
std::cout << "PB::Renderer::VulkanManager::CreateCommandBuffers(): Could not end command buffer" << std::endl;
return false; return false;
}
} }
return true; return true;

View File

@@ -2,7 +2,7 @@
namespace PB::Renderer namespace PB::Renderer
{ {
bool VulkanManager::RenderPass(GLFWwindow* window) bool VulkanManager::RenderPass()
{ {
uint32_t imageIndex; uint32_t imageIndex;
VkResult result = vkAcquireNextImageKHR( VkResult result = vkAcquireNextImageKHR(
@@ -28,8 +28,8 @@ namespace PB::Renderer
VkSubmitInfo submitInfo{}; VkSubmitInfo submitInfo{};
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
VkSemaphore waitSemaphores[] = { s_ImageAvailableSemaphore }; const VkSemaphore waitSemaphores[] = { s_ImageAvailableSemaphore };
VkPipelineStageFlags waitStages[] = { VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT }; constexpr VkPipelineStageFlags waitStages[] = { VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT };
submitInfo.waitSemaphoreCount = 1; submitInfo.waitSemaphoreCount = 1;
submitInfo.pWaitSemaphores = waitSemaphores; submitInfo.pWaitSemaphores = waitSemaphores;
submitInfo.pWaitDstStageMask = waitStages; submitInfo.pWaitDstStageMask = waitStages;
@@ -37,7 +37,7 @@ namespace PB::Renderer
submitInfo.commandBufferCount = 1; submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &s_CommandBuffers[imageIndex]; submitInfo.pCommandBuffers = &s_CommandBuffers[imageIndex];
VkSemaphore signalSemaphores[] = { s_RenderFinishedSemaphore }; const VkSemaphore signalSemaphores[] = { s_RenderFinishedSemaphore };
submitInfo.signalSemaphoreCount = 1; submitInfo.signalSemaphoreCount = 1;
submitInfo.pSignalSemaphores = signalSemaphores; submitInfo.pSignalSemaphores = signalSemaphores;
@@ -52,7 +52,7 @@ namespace PB::Renderer
presentInfo.waitSemaphoreCount = 1; presentInfo.waitSemaphoreCount = 1;
presentInfo.pWaitSemaphores = signalSemaphores; presentInfo.pWaitSemaphores = signalSemaphores;
VkSwapchainKHR swapChains[] = { s_SwapChain }; const VkSwapchainKHR swapChains[] = { s_SwapChain };
presentInfo.swapchainCount = 1; presentInfo.swapchainCount = 1;
presentInfo.pSwapchains = swapChains; presentInfo.pSwapchains = swapChains;
presentInfo.pImageIndices = &imageIndex; presentInfo.pImageIndices = &imageIndex;
@@ -64,7 +64,7 @@ namespace PB::Renderer
return false; return false;
} }
else if (result != VK_SUCCESS) if (result != VK_SUCCESS)
{ {
std::cout << "PB::Renderer::VulkanManager::RenderPass(): Failed to present swap chain image!" << std::endl; std::cout << "PB::Renderer::VulkanManager::RenderPass(): Failed to present swap chain image!" << std::endl;
return false; return false;