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
{
void CleanupAllAndExit(int code)
void CleanupAllAndExit(const int code)
{
if (!GLFWManager::Cleanup())
std::exit(EXIT_FAILURE);
@@ -38,7 +38,7 @@ int main()
{
glfwPollEvents();
if (!VulkanManager::RenderPass(window))
if (!VulkanManager::RenderPass())
CleanupAllAndExit(EXIT_FAILURE);
}

View File

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

View File

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

View File

@@ -2,7 +2,7 @@
namespace PB::Renderer
{
bool VulkanManager::RenderPass(GLFWwindow* window)
bool VulkanManager::RenderPass()
{
uint32_t imageIndex;
VkResult result = vkAcquireNextImageKHR(
@@ -28,8 +28,8 @@ namespace PB::Renderer
VkSubmitInfo submitInfo{};
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
VkSemaphore waitSemaphores[] = { s_ImageAvailableSemaphore };
VkPipelineStageFlags waitStages[] = { VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT };
const VkSemaphore waitSemaphores[] = { s_ImageAvailableSemaphore };
constexpr VkPipelineStageFlags waitStages[] = { VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT };
submitInfo.waitSemaphoreCount = 1;
submitInfo.pWaitSemaphores = waitSemaphores;
submitInfo.pWaitDstStageMask = waitStages;
@@ -37,7 +37,7 @@ namespace PB::Renderer
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &s_CommandBuffers[imageIndex];
VkSemaphore signalSemaphores[] = { s_RenderFinishedSemaphore };
const VkSemaphore signalSemaphores[] = { s_RenderFinishedSemaphore };
submitInfo.signalSemaphoreCount = 1;
submitInfo.pSignalSemaphores = signalSemaphores;
@@ -52,7 +52,7 @@ namespace PB::Renderer
presentInfo.waitSemaphoreCount = 1;
presentInfo.pWaitSemaphores = signalSemaphores;
VkSwapchainKHR swapChains[] = { s_SwapChain };
const VkSwapchainKHR swapChains[] = { s_SwapChain };
presentInfo.swapchainCount = 1;
presentInfo.pSwapchains = swapChains;
presentInfo.pImageIndices = &imageIndex;
@@ -64,7 +64,7 @@ namespace PB::Renderer
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;
return false;