Namespace: "Application.H" "Staticfunctions.H"
Namespace: "Application.H" "Staticfunctions.H"
h"
#include "StaticFunctions.h"
namespace Frost
{
VkApplication::VkApplication()
{
m_Window = std::make_unique<Window>(800, 600, "Frost engine");
CreateInstance();
SetupDebugMessenger();
CreateSurface();
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";
// 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);
}
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::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;
PopulateDebugMessengerCreateInfo(debugCreateInfo);
createInfo.pNext =
(VkDebugUtilsMessengerCreateInfoEXT*)&debugCreateInfo;
}
else
{
createInfo.enabledLayerCount = 0;
createInfo.pNext = nullptr;
}
void VkApplication::CreateSurface()
{
if (glfwCreateWindowSurface(m_Instance, m_Window->GetWindow(), nullptr,
&m_Surface) != VK_SUCCESS)
{
FROST_ASSERT(0, "failed to create window surface!");
}
}
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());
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!");
}