0% found this document useful (0 votes)
58 views6 pages

Namespace: "Application.H" "Staticfunctions.H"

The document defines a VkApplication class that initializes a Vulkan application instance and related objects. It creates a window, Vulkan instance, surface, device, swap chain, render pass, graphics pipeline and renderer. It also defines methods for running the main loop, recreating the swap chain and waiting on the device.

Uploaded by

iDenis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views6 pages

Namespace: "Application.H" "Staticfunctions.H"

The document defines a VkApplication class that initializes a Vulkan application instance and related objects. It creates a window, Vulkan instance, surface, device, swap chain, render pass, graphics pipeline and renderer. It also defines methods for running the main loop, recreating the swap chain and waiting on the device.

Uploaded by

iDenis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

#include "Application.

h"

#include "StaticFunctions.h"

namespace Frost
{

VkApplication::VkApplication()
{
m_Window = std::make_unique<Window>(800, 600, "Frost engine");

CreateInstance();
SetupDebugMessenger();
CreateSurface();

m_Device = std::make_unique<VkDeviceSpecs>(&m_Instance, m_DeviceExtension,


&m_Surface);
m_Device->CreateLogicalDevice(m_ValidationLayers);

VkSwapChainInfo swapChainInfo;
swapChainInfo.DeviceSpecs = &m_Device;
swapChainInfo.WindowDevice = &m_Window;
swapChainInfo.Surface = &m_Surface;
m_SwapChain = std::make_unique<VkSwapChain>(swapChainInfo);

m_SwapChain->CreateSwapChain();
m_SwapChain->CreateImageViews();
m_SwapChain->CreateSyncObjects();

SwapChainFormats swapChainFormatsInfo;
swapChainFormatsInfo.Extent = m_SwapChain->GetExtent();
swapChainFormatsInfo.ImageFormat = m_SwapChain->GetImageFormat();

ShaderFilepaths shaderFilepaths;
shaderFilepaths.VertexShaderFilepath = "assets/shader/vert.spv";
shaderFilepaths.FragmentShaderFilepath = "assets/shader/frag.spv";

m_Render = std::make_unique<Renderer>(&m_Device, &m_Surface, &m_SwapChain,


&m_Pipeline);
m_Render->CreateDescriptorLayout();

// TO DO: OPTION TO SELECT WHICH SHADER STAGE THE UBO SHOULD ACCESS
m_Pipeline = std::make_unique<Pipeline>(m_Device->GetDevice(),
swapChainFormatsInfo, shaderFilepaths);
m_Pipeline->CreateGraphicsPipeline(m_Render->GetDescriptorLayout());

m_SwapChain->CreateFramebuffer(m_Pipeline->GetRenderPass());

m_Render->CreateCommadPool();
m_Render->InitData();
m_Render->CreateDescriptorPool();
//m_Render->CreateDescriptorSets();

m_Render->SetCommandBufferData(m_Pipeline);
}

VkApplication::~VkApplication()
{
m_SwapChain->DeleteFramebuffer();

m_Render->DeleteCommandBufferData();

m_Pipeline->DeletePipeline();

m_SwapChain->DeleteSwapChain();

m_Render->DeleteDescriptorLayout();

m_SwapChain->DeleteSyncObjects();

m_Render->DeleteCommandPool();
m_Device->DeleteDevice();

if (enableValidationLayers)
{
DestroyDebugUtilsMessengerEXT(m_Instance, m_DebugMessenger,
nullptr);
}

vkDestroySurfaceKHR(m_Instance, m_Surface, nullptr);


vkDestroyInstance(m_Instance, nullptr);

void VkApplication::Run()
{
while (!IsClosed())
{
VkQueueStack queueStack;
queueStack.Graphics = m_Device->GetQueues().m_GraphicsQueue;
queueStack.Present = m_Device->GetQueues().m_PresentQueue;
m_Render->DrawFrame(m_Render->GetCommandBuffers(), queueStack);

if (m_Render->IsValid())
{
ReacreateSwapChain();
}

glfwPollEvents();

WaitDevice();

void VkApplication::ReacreateSwapChain()
{
int width = 0, height = 0;
m_Window->GetSize(width, height);
while (width == 0 || height == 0) {
m_Window->GetSize(width, height);
glfwWaitEvents();
}

WaitDevice();

m_SwapChain->DeleteFramebuffer();
m_Render->DeleteCommandBufferData();
m_Pipeline->DeletePipeline();
m_SwapChain->DeleteSwapChain();
m_Render->DeleteUBO();
m_Render->DeleteDescriptorPool();

m_SwapChain->CreateSwapChain();
m_SwapChain->CreateImageViews();

SwapChainFormats swapChainFormatsInfo;
swapChainFormatsInfo.Extent = m_SwapChain->GetExtent();
swapChainFormatsInfo.ImageFormat = m_SwapChain->GetImageFormat();

ShaderFilepaths shaderFilepaths;
shaderFilepaths.VertexShaderFilepath = "assets/shader/vert.spv";
shaderFilepaths.FragmentShaderFilepath = "assets/shader/frag.spv";

m_Pipeline = std::make_unique<Pipeline>(m_Device->GetDevice(),
swapChainFormatsInfo, shaderFilepaths);
m_Pipeline->CreateGraphicsPipeline(m_Render->GetDescriptorLayout());

m_SwapChain->CreateFramebuffer(m_Pipeline->GetRenderPass());

m_Render->CreateUBO();
m_Render->CreateDescriptorPool();
m_Render->SetCommandBufferData(m_Pipeline);

void VkApplication::WaitDevice()
{
vkDeviceWaitIdle(m_Device->GetDevice());
}

bool VkApplication::IsClosed()
{
return glfwWindowShouldClose(m_Window->GetWindow());
}

void VkApplication::AddExtension(const char* extension)


{
m_DeviceExtension.push_back(extension);
}

void VkApplication::CreateInstance()
{
if (enableValidationLayers && !CheckValidationLayerSupport())
{
FROST_ASSERT(0, "Validation layers requested, but not available!");
}

VkApplicationInfo appInfo{};
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
appInfo.pApplicationName = "Hello Triangle";
appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
appInfo.pEngineName = "No engine";
appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
appInfo.apiVersion = VK_API_VERSION_1_0;

VkInstanceCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
createInfo.pApplicationInfo = &appInfo;

auto extension = GetRequiredExtensions();


createInfo.enabledExtensionCount = static_cast<uint32_t>(extension.size());
createInfo.ppEnabledExtensionNames = extension.data();

// Setup the debugger


// -------------------------------
VkDebugUtilsMessengerCreateInfoEXT debugCreateInfo;
if (enableValidationLayers)
{
createInfo.enabledLayerCount =
static_cast<uint32_t>(m_ValidationLayers.size());
createInfo.ppEnabledLayerNames = m_ValidationLayers.data();

PopulateDebugMessengerCreateInfo(debugCreateInfo);
createInfo.pNext =
(VkDebugUtilsMessengerCreateInfoEXT*)&debugCreateInfo;
}
else
{
createInfo.enabledLayerCount = 0;

createInfo.pNext = nullptr;
}

if (vkCreateInstance(&createInfo, nullptr, &m_Instance) != VK_SUCCESS)


{
FROST_ASSERT(0, "Failed to create instance!");
}
}

void VkApplication::CreateSurface()
{
if (glfwCreateWindowSurface(m_Instance, m_Window->GetWindow(), nullptr,
&m_Surface) != VK_SUCCESS)
{
FROST_ASSERT(0, "failed to create window surface!");
}
}

std::vector<const char*> VkApplication::GetRequiredExtensions() {


uint32_t glfwExtensionCount = 0;
const char** glfwExtensions;
glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);

std::vector<const char*> extensions(glfwExtensions, glfwExtensions +


glfwExtensionCount);

if (enableValidationLayers) {
extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
}

return extensions;
}

bool VkApplication::CheckValidationLayerSupport()
{
uint32_t layerCount;
vkEnumerateInstanceLayerProperties(&layerCount, nullptr);

std::vector<VkLayerProperties> availableLayers(layerCount);
vkEnumerateInstanceLayerProperties(&layerCount, availableLayers.data());

for (const char* layerName : m_ValidationLayers) {


bool layerFound = false;

for (const auto& layerProperties : availableLayers) {


if (strcmp(layerName, layerProperties.layerName) == 0) {
layerFound = true;
break;
}
}

if (!layerFound) {
return false;
}
}

return true;

void VkApplication::SetupDebugMessenger() {
if (!enableValidationLayers) return;

VkDebugUtilsMessengerCreateInfoEXT createInfo;
PopulateDebugMessengerCreateInfo(createInfo);
if (CreateDebugUtilsMessengerEXT(m_Instance, &createInfo, nullptr,
&m_DebugMessenger) != VK_SUCCESS)
{
FROST_ASSERT(0, "Failed to set up debug messenger!");
}

You might also like