Moved vulkan files

This commit is contained in:
Pasha Bibko
2025-11-16 21:20:43 +00:00
parent 5dc0974d50
commit edcf13d62a
5 changed files with 5 additions and 5 deletions

View File

@@ -0,0 +1,761 @@
#include "VulkanManager.h"
namespace PB::Renderer
{
VkInstance VulkanManager::s_Instance = VK_NULL_HANDLE;
VkSurfaceKHR VulkanManager::s_Surface = VK_NULL_HANDLE;
VkPhysicalDevice VulkanManager::s_PhysicalDevice = VK_NULL_HANDLE;
QueueFamilyIndices VulkanManager::s_QueueIndices;
VkDevice VulkanManager::s_Device = VK_NULL_HANDLE;
VkQueue VulkanManager::s_GraphicsQueue = VK_NULL_HANDLE;
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;
VkFormat VulkanManager::s_SwapChainImageFormat = {};
VkExtent2D VulkanManager::s_SwapChainExtent;
VkRenderPass VulkanManager::s_RenderPass = VK_NULL_HANDLE;
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;
VkSemaphore VulkanManager::s_ImageAvailableSemaphore = VK_NULL_HANDLE;
VkSemaphore VulkanManager::s_RenderFinishedSemaphore = VK_NULL_HANDLE;
bool VulkanManager::Init(GLFWwindow* window)
{
return
CreateInstance() &&
CreateSurface(window) &&
PickPhysicalDevice() &&
CreateLogicalDevice() &&
CreateSwapChain(window) &&
CreateImageViews() &&
CreateRenderPass() &&
CreateFramebuffer() &&
CreateGraphicsPipeline() &&
CreateCommandBuffers() &&
CreateSemaphores();
}
bool VulkanManager::Cleanup()
{
if (s_Device != VK_NULL_HANDLE)
vkDeviceWaitIdle(s_Device);
if (s_ImageAvailableSemaphore != VK_NULL_HANDLE)
vkDestroySemaphore(s_Device, s_ImageAvailableSemaphore, nullptr);
if (s_RenderFinishedSemaphore != VK_NULL_HANDLE)
vkDestroySemaphore(s_Device, s_RenderFinishedSemaphore, nullptr);
if (s_CommandPool != VK_NULL_HANDLE)
vkDestroyCommandPool(s_Device, s_CommandPool, nullptr);
for (const VkFramebuffer& fb : s_Framebuffers)
vkDestroyFramebuffer(s_Device, fb, nullptr);
if (s_RenderPipeline != VK_NULL_HANDLE)
vkDestroyPipeline(s_Device, s_RenderPipeline, nullptr);
if (s_PipelineLayout != VK_NULL_HANDLE)
vkDestroyPipelineLayout(s_Device, s_PipelineLayout, nullptr);
if (s_RenderPass != VK_NULL_HANDLE)
vkDestroyRenderPass(s_Device, s_RenderPass, nullptr);
for (const VkImageView& view : s_SwapChainImageViews)
vkDestroyImageView(s_Device, view, nullptr);
if (s_SwapChain != VK_NULL_HANDLE)
vkDestroySwapchainKHR(s_Device, s_SwapChain, nullptr);
if (s_Device != VK_NULL_HANDLE)
vkDestroyDevice(s_Device, nullptr);
if (s_Surface != VK_NULL_HANDLE)
vkDestroySurfaceKHR(s_Instance, s_Surface, nullptr);
if (s_Instance != VK_NULL_HANDLE)
vkDestroyInstance(s_Instance, nullptr);
return true;
}
bool VulkanManager::CreateInstance()
{
VkApplicationInfo appInfo;
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
appInfo.pNext = nullptr;
appInfo.pApplicationName = "VulkanRenderer";
appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
appInfo.pEngineName = "VulkanRendererEngine";
appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
appInfo.apiVersion = VK_API_VERSION_1_3;
VkInstanceCreateInfo createInfo;
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
createInfo.pNext = nullptr;
createInfo.pApplicationInfo = &appInfo;
createInfo.enabledLayerCount = 0;
createInfo.ppEnabledLayerNames = nullptr;
/* Imports GLFW extensions */
uint32_t glfwExtensionCount = 0;
const char** glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
createInfo.enabledExtensionCount = glfwExtensionCount;
createInfo.ppEnabledExtensionNames = glfwExtensions;
/* Creates the Vulkan instance */
VkInstance instance;
if (vkCreateInstance(&createInfo, nullptr, &instance) != VK_SUCCESS)
{
std::cout << "PB::Renderer::VulkanManager::Init(): Could not create Vulkan instance" << std::endl;
return false;
}
s_Instance = instance;
return true;
}
bool VulkanManager::CreateSurface(GLFWwindow* window)
{
VkSurfaceKHR surface;
if (glfwCreateWindowSurface(s_Instance, window, nullptr, &surface) != VK_SUCCESS)
{
std::cout << "PB::Renderer::VulkanManager::CreateSurface(): Failed to create Vulkan Surface" << std::endl;
return false;
}
s_Surface = surface;
return true;
}
bool VulkanManager::PickPhysicalDevice()
{
uint32_t deviceCount = 0;
vkEnumeratePhysicalDevices(s_Instance, &deviceCount, nullptr);
if (deviceCount == 0)
{
std::cout << "PB::Renderer::VulkanManager::PickPhysicalDevice(): No GPU with Vulkan support" << std::endl;
return false;
}
std::vector<VkPhysicalDevice> devices(deviceCount);
vkEnumeratePhysicalDevices(s_Instance, &deviceCount, devices.data());
for (const auto& device : devices)
{
if (IsDeviceSuitable(device))
{
s_PhysicalDevice = device;
s_QueueIndices = FindQueueFamilies(device);
VkPhysicalDeviceProperties deviceProperties;
vkGetPhysicalDeviceProperties(device, &deviceProperties);
std::cout << "Selected GPU: "<< deviceProperties.deviceName << std::endl;
return true;
}
}
std::cout << "Failed to find a suitable GPU" << std::endl;
return false;
}
bool VulkanManager::IsDeviceSuitable(const VkPhysicalDevice& device)
{
const QueueFamilyIndices indices = FindQueueFamilies(device);
if (const bool extensionsSupported = CheckDeviceExtensionSupport(device); !extensionsSupported)
return false;
VkSurfaceCapabilitiesKHR capabilities;
uint32_t formatCount, presentCount;
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(device, s_Surface, &capabilities);
vkGetPhysicalDeviceSurfaceFormatsKHR(device, s_Surface, &formatCount, nullptr);
vkGetPhysicalDeviceSurfacePresentModesKHR(device, s_Surface, &presentCount, nullptr);
const bool swapChainAdequate = formatCount > 0 && presentCount > 0;
VkPhysicalDeviceProperties deviceProperties;
vkGetPhysicalDeviceProperties(device, &deviceProperties);
const bool discreteGPU = deviceProperties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU;
return indices.Complete() && swapChainAdequate && discreteGPU;
}
QueueFamilyIndices VulkanManager::FindQueueFamilies(const VkPhysicalDevice& device)
{
QueueFamilyIndices indices;
uint32_t queueFamilyCount;
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, nullptr);
std::vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, queueFamilies.data());
int index = 0;
for (const auto& family : queueFamilies)
{
if (family.queueFlags & VK_QUEUE_GRAPHICS_BIT)
{
indices.graphicsFamily = index;
}
VkBool32 presentSupport = false;
vkGetPhysicalDeviceSurfaceSupportKHR(device, index, s_Surface, &presentSupport);
if (presentSupport)
{
indices.presentFamily = index;
}
if (indices.Complete())
{
break;
}
index++;
}
return indices;
}
bool VulkanManager::CheckDeviceExtensionSupport(const VkPhysicalDevice& device)
{
const std::vector REQUIRED_EXTENSIONS = {
VK_KHR_SWAPCHAIN_EXTENSION_NAME,
};
uint32_t extensionCount;
vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionCount, nullptr);
std::vector<VkExtensionProperties> availableExtensions(extensionCount);
vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionCount, availableExtensions.data());
std::set<std::string> required(REQUIRED_EXTENSIONS.begin(), REQUIRED_EXTENSIONS.end());
for (const auto& [extensionName, specVersion] : availableExtensions)
{
required.erase(extensionName);
}
return required.empty();
}
bool VulkanManager::CreateLogicalDevice()
{
std::set uniqueQueueFamilies =
{
s_QueueIndices.graphicsFamily,
s_QueueIndices.presentFamily
};
std::vector<VkDeviceQueueCreateInfo> queueCreateInfos;
float queuePriority = 1.0f;
for (uint32_t queueFamily : uniqueQueueFamilies)
{
VkDeviceQueueCreateInfo queueCreateInfo{};
queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
queueCreateInfo.queueFamilyIndex = queueFamily;
queueCreateInfo.queueCount = 1;
queueCreateInfo.pQueuePriorities = &queuePriority;
queueCreateInfos.push_back(queueCreateInfo);
}
VkPhysicalDeviceFeatures deviceFeatures{};
const std::vector extensions = {
VK_KHR_SWAPCHAIN_EXTENSION_NAME
};
VkDeviceCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
createInfo.queueCreateInfoCount = static_cast<uint32_t>(queueCreateInfos.size());
createInfo.pQueueCreateInfos = queueCreateInfos.data();
createInfo.pEnabledFeatures = &deviceFeatures;
createInfo.enabledExtensionCount = static_cast<uint32_t>(extensions.size());
createInfo.ppEnabledExtensionNames = extensions.data();
if (vkCreateDevice(s_PhysicalDevice, &createInfo, nullptr, &s_Device))
{
std::cout << "Failed to create logical device" << std::endl;
return false;
}
vkGetDeviceQueue(s_Device, s_QueueIndices.graphicsFamily, 0, &s_GraphicsQueue);
vkGetDeviceQueue(s_Device, s_QueueIndices.presentFamily, 0, &s_PresentQueue);
return true;
}
bool VulkanManager::CreateSwapChain(GLFWwindow* window)
{
auto [capabilities, formats, presentModes] = QuerySwapChainSupport();
auto [format, colorSpace] = ChooseSurfaceFormat(formats);
const VkPresentModeKHR presentMode = ChoosePresentMode(presentModes);
s_SwapChainExtent = ChooseSwapExtent(capabilities, window);
uint32_t imageCount = capabilities.minImageCount + 1;
if (capabilities.maxImageCount > 0 && imageCount > capabilities.maxImageCount)
imageCount = capabilities.maxImageCount;
VkSwapchainCreateInfoKHR createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
createInfo.surface = s_Surface;
createInfo.minImageCount = imageCount;
createInfo.imageFormat = format;
createInfo.imageColorSpace = colorSpace;
createInfo.imageExtent = s_SwapChainExtent;
createInfo.imageArrayLayers = 1;
createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
const uint32_t queueFamilyIndices[] =
{
s_QueueIndices.graphicsFamily,
s_QueueIndices.presentFamily
};
if (s_QueueIndices.graphicsFamily != s_QueueIndices.presentFamily)
{
createInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
createInfo.queueFamilyIndexCount = 2;
createInfo.pQueueFamilyIndices = queueFamilyIndices;
}
else
{
createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
createInfo.queueFamilyIndexCount = 0;
createInfo.pQueueFamilyIndices = nullptr;
}
createInfo.preTransform = capabilities.currentTransform;
createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
createInfo.presentMode = presentMode;
createInfo.clipped = VK_TRUE;
createInfo.oldSwapchain = VK_NULL_HANDLE;
if (vkCreateSwapchainKHR(s_Device, &createInfo, nullptr, &s_SwapChain) != VK_SUCCESS)
{
std::cout << "PB::Renderer::VulkanManager::CreateSwapChain(): Failed to create swap chain" << std::endl;
return false;
}
vkGetSwapchainImagesKHR(s_Device, s_SwapChain, &imageCount, nullptr);
s_SwapChainImages.resize(imageCount);
vkGetSwapchainImagesKHR(s_Device, s_SwapChain, &imageCount, s_SwapChainImages.data());
s_SwapChainImageFormat = format;
return true;
}
SwapChainSupportDetails VulkanManager::QuerySwapChainSupport()
{
SwapChainSupportDetails details;
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(s_PhysicalDevice, s_Surface, &details.capabilities);
uint32_t formatCount;
vkGetPhysicalDeviceSurfaceFormatsKHR(s_PhysicalDevice, s_Surface, &formatCount, nullptr);
details.formats.resize(formatCount);
vkGetPhysicalDeviceSurfaceFormatsKHR(s_PhysicalDevice, s_Surface, &formatCount, details.formats.data());
uint32_t presentModeCount;
vkGetPhysicalDeviceSurfacePresentModesKHR(s_PhysicalDevice, s_Surface, &presentModeCount, nullptr);
details.presentModes.resize(presentModeCount);
vkGetPhysicalDeviceSurfacePresentModesKHR(s_PhysicalDevice, s_Surface, &presentModeCount, details.presentModes.data());
return details;
}
VkSurfaceFormatKHR VulkanManager::ChooseSurfaceFormat(const std::vector<VkSurfaceFormatKHR>& availableFormats)
{
for (const auto& format : availableFormats)
{
if (format.format == VK_FORMAT_B8G8R8A8_SRGB && format.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR)
{
return format;
}
}
return availableFormats[0];
}
VkPresentModeKHR VulkanManager::ChoosePresentMode(const std::vector<VkPresentModeKHR>& availablePresentModes)
{
for (const auto& mode : availablePresentModes)
{
if (mode == VK_PRESENT_MODE_MAILBOX_KHR)
{
return mode;
}
}
return VK_PRESENT_MODE_FIFO_KHR;
}
VkExtent2D VulkanManager::ChooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities, GLFWwindow* window)
{
if (capabilities.currentExtent.width != UINT32_MAX)
{
return capabilities.currentExtent;
}
int width, height;
glfwGetFramebufferSize(window, &width, &height);
VkExtent2D actualExtent =
{
static_cast<uint32_t>(width),
static_cast<uint32_t>(height)
};
actualExtent.width = std::max(capabilities.minImageExtent.width, std::min(capabilities.maxImageExtent.width, actualExtent.width));
actualExtent.height = std::max(capabilities.minImageExtent.height, std::min(capabilities.maxImageExtent.height, actualExtent.height));
return actualExtent;
}
bool VulkanManager::CreateImageViews()
{
s_SwapChainImageViews.resize(s_SwapChainImages.size());
for (size_t i = 0; i < s_SwapChainImages.size(); i++)
{
VkImageViewCreateInfo createInfo = {};
createInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
createInfo.image = s_SwapChainImages[i];
createInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
createInfo.format = s_SwapChainImageFormat;
createInfo.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
createInfo.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
createInfo.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
createInfo.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
createInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
createInfo.subresourceRange.baseMipLevel = 0;
createInfo.subresourceRange.levelCount = 1;
createInfo.subresourceRange.baseArrayLayer = 0;
createInfo.subresourceRange.layerCount = 1;
if (vkCreateImageView(s_Device, &createInfo, nullptr, &s_SwapChainImageViews[i]) != VK_SUCCESS)
{
std::cout << "PB::Renderer::VulkanManager::CreateImageView(): Failed to create swap chain image views" << std::endl;
return false;
}
}
return true;
}
bool VulkanManager::CreateRenderPass()
{
VkAttachmentDescription colorAttachment{};
colorAttachment.format = s_SwapChainImageFormat;
colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
colorAttachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
VkAttachmentReference colorAttachmentRef{};
colorAttachmentRef.attachment = 0;
colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
VkSubpassDescription subpass{};
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
subpass.colorAttachmentCount = 1;
subpass.pColorAttachments = &colorAttachmentRef;
VkSubpassDependency dependency{};
dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
dependency.dstSubpass = 0;
dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
dependency.srcAccessMask = 0;
dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
VkRenderPassCreateInfo renderPassInfo{};
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
renderPassInfo.attachmentCount = 1;
renderPassInfo.pAttachments = &colorAttachment;
renderPassInfo.subpassCount = 1;
renderPassInfo.pSubpasses = &subpass;
renderPassInfo.dependencyCount = 1;
renderPassInfo.pDependencies = &dependency;
if (VkResult result = vkCreateRenderPass(s_Device, &renderPassInfo, nullptr, &s_RenderPass); result != VK_SUCCESS)
{
std::cout << "PB::Renderer::VulkanManager::CreateRenderPass(): Failed to create render pass, VkResult = " << result << std::endl;
return false;
}
return true;
}
bool VulkanManager::CreateFramebuffer()
{
s_Framebuffers.resize(s_SwapChainImageViews.size());
for (size_t index = 0; index < s_SwapChainImageViews.size(); index++)
{
const VkImageView attachments[] = {
s_SwapChainImageViews[index]
};
VkFramebufferCreateInfo framebufferInfo{};
framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
framebufferInfo.renderPass = s_RenderPass;
framebufferInfo.attachmentCount = 1;
framebufferInfo.pAttachments = attachments;
framebufferInfo.width = s_SwapChainExtent.width;
framebufferInfo.height = s_SwapChainExtent.height;
framebufferInfo.layers = 1;
if (VkResult result = vkCreateFramebuffer(s_Device, &framebufferInfo, nullptr, &s_Framebuffers[index]); result != VK_SUCCESS)
{
std::cout << "PB::Renderer::VulkanManager::CreateFramebuffers(): Failed to create framebuffers" << std::endl;
return false;
}
}
return true;
}
bool VulkanManager::CreateGraphicsPipeline()
{
VkShaderModule vertShaderModule = CreateShaderModule("../shaders/vert.spv");
VkShaderModule fragShaderModule = CreateShaderModule("../shaders/frag.spv");
VkPipelineShaderStageCreateInfo vertShaderStageInfo{};
vertShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
vertShaderStageInfo.stage = VK_SHADER_STAGE_VERTEX_BIT;
vertShaderStageInfo.module = vertShaderModule;
vertShaderStageInfo.pName = "main";
VkPipelineShaderStageCreateInfo fragShaderStageInfo{};
fragShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
fragShaderStageInfo.stage = VK_SHADER_STAGE_FRAGMENT_BIT;
fragShaderStageInfo.module = fragShaderModule;
fragShaderStageInfo.pName = "main";
VkPipelineShaderStageCreateInfo shaderStages[] = { vertShaderStageInfo, fragShaderStageInfo };
VkPipelineVertexInputStateCreateInfo vertexInputInfo{};
vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
vertexInputInfo.vertexBindingDescriptionCount = 0;
vertexInputInfo.vertexAttributeDescriptionCount = 0;
VkPipelineInputAssemblyStateCreateInfo inputAssembly{};
inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
inputAssembly.primitiveRestartEnable = VK_FALSE;
VkViewport viewport{};
viewport.x = 0.0f;
viewport.y = 0.0f;
viewport.width = static_cast<float>(s_SwapChainExtent.width);
viewport.height = static_cast<float>(s_SwapChainExtent.height);
viewport.minDepth = 0.0f;
viewport.maxDepth = 1.0f;
VkRect2D scissor{};
scissor.offset = {0,0};
scissor.extent = s_SwapChainExtent;
VkPipelineViewportStateCreateInfo viewportState{};
viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
viewportState.viewportCount = 1;
viewportState.pViewports = &viewport;
viewportState.scissorCount = 1;
viewportState.pScissors = &scissor;
VkPipelineRasterizationStateCreateInfo rasterizer{};
rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
rasterizer.depthClampEnable = VK_FALSE;
rasterizer.rasterizerDiscardEnable = VK_FALSE;
rasterizer.polygonMode = VK_POLYGON_MODE_FILL;
rasterizer.lineWidth = 1.0f;
rasterizer.cullMode = VK_CULL_MODE_BACK_BIT;
rasterizer.frontFace = VK_FRONT_FACE_CLOCKWISE;
rasterizer.depthBiasEnable = VK_FALSE;
VkPipelineMultisampleStateCreateInfo multisampling{};
multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
multisampling.sampleShadingEnable = VK_FALSE;
multisampling.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
VkPipelineColorBlendAttachmentState colorBlendAttachment{};
colorBlendAttachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT
| VK_COLOR_COMPONENT_G_BIT
| VK_COLOR_COMPONENT_B_BIT
| VK_COLOR_COMPONENT_A_BIT;
colorBlendAttachment.blendEnable = VK_FALSE;
VkPipelineColorBlendStateCreateInfo colorBlending{};
colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
colorBlending.logicOpEnable = VK_FALSE;
colorBlending.attachmentCount = 1;
colorBlending.pAttachments = &colorBlendAttachment;
VkPipelineLayoutCreateInfo pipelineLayoutInfo{};
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;
pipelineInfo.stageCount = 2;
pipelineInfo.pStages = shaderStages;
pipelineInfo.pVertexInputState = &vertexInputInfo;
pipelineInfo.pInputAssemblyState = &inputAssembly;
pipelineInfo.pViewportState = &viewportState;
pipelineInfo.pRasterizationState = &rasterizer;
pipelineInfo.pMultisampleState = &multisampling;
pipelineInfo.pColorBlendState = &colorBlending;
pipelineInfo.layout = s_PipelineLayout;
pipelineInfo.renderPass = s_RenderPass;
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);
return true;
}
VkShaderModule VulkanManager::CreateShaderModule(const std::string& filename)
{
std::ifstream file(filename, std::ios::ate | std::ios::binary);
if (!file.is_open())
{
std::cout << "Failed to open shader file at '" << std::filesystem::absolute(filename) << "'" << std::endl;
return VK_NULL_HANDLE;
}
const size_t fileSize = file.tellg();
std::vector<char> buffer(fileSize);
file.seekg(0);
file.read(buffer.data(), static_cast<std::streamsize>(fileSize));
file.close();
VkShaderModuleCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
createInfo.codeSize = buffer.size();
createInfo.pCode = reinterpret_cast<const uint32_t*>(buffer.data());
VkShaderModule shaderModule;
if (vkCreateShaderModule(s_Device, &createInfo, nullptr, &shaderModule) != VK_SUCCESS)
{
return VK_NULL_HANDLE;
}
return shaderModule;
}
bool VulkanManager::CreateCommandBuffers()
{
VkCommandPoolCreateInfo poolInfo{};
poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
poolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
poolInfo.queueFamilyIndex = s_QueueIndices.graphicsFamily;
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());
VkCommandBufferAllocateInfo allocInfo{};
allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
allocInfo.commandPool = s_CommandPool;
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)
{
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++)
{
VkCommandBufferBeginInfo beginInfo{};
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
vkBeginCommandBuffer(s_CommandBuffers[i], &beginInfo);
VkRenderPassBeginInfo renderPassInfo{};
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
renderPassInfo.renderPass = s_RenderPass;
renderPassInfo.framebuffer = s_Framebuffers[i];
renderPassInfo.renderArea.offset = {0, 0};
renderPassInfo.renderArea.extent = s_SwapChainExtent;
VkClearValue clearColor = {0.0f, 0.0f, 0.0f, 1.0f};
renderPassInfo.clearValueCount = 1;
renderPassInfo.pClearValues = &clearColor;
vkCmdBeginRenderPass(s_CommandBuffers[i], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
vkCmdBindPipeline(s_CommandBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, s_RenderPipeline);
vkCmdDraw(s_CommandBuffers[i], 3, 1, 0, 0);
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;
}
bool VulkanManager::CreateSemaphores()
{
VkSemaphoreCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
if (vkCreateSemaphore(s_Device, &createInfo, nullptr, &s_ImageAvailableSemaphore) != VK_SUCCESS)
{
std::cout << "PB::Renderer::VulkanManager::CreateSemaphores(): Could not create semaphore" << std::endl;
return false;
}
if (vkCreateSemaphore(s_Device, &createInfo, nullptr, &s_RenderFinishedSemaphore) != VK_SUCCESS)
{
std::cout << "PB::Renderer::VulkanManager::CreateSemaphores(): Could not create semaphore" << std::endl;
return false;
}
return true;
}
}