More code cleanup
This commit is contained in:
83
openVulkanoCpp/Vulkan/Context.cpp
Normal file
83
openVulkanoCpp/Vulkan/Context.cpp
Normal file
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#include "Context.hpp"
|
||||
#include "../Base/Utils.hpp"
|
||||
#include "../Base/Logger.hpp"
|
||||
#include "../Base/IGraphicsApp.hpp"
|
||||
#include "../Base/IGraphicsAppManager.hpp"
|
||||
#include "../Base/EngineConstants.hpp"
|
||||
#include "Debuging/ValidationLayer.hpp"
|
||||
|
||||
namespace openVulkanoCpp::Vulkan
|
||||
{
|
||||
void Context::Init(IGraphicsAppManager* graphicsAppManager, IVulkanWindow* window)
|
||||
{
|
||||
if (initialized) throw std::runtime_error("The context is already initialized");
|
||||
this->graphicsAppManager = graphicsAppManager;
|
||||
this->window = window;
|
||||
|
||||
// Get the extensions required to display on the window
|
||||
for (const auto& requiredExtension : window->GetRequiredInstanceExtensions()) { RequireExtension(requiredExtension.c_str()); }
|
||||
|
||||
CreateInstance(); // Create the vulkan instance
|
||||
surface = window->CreateSurface(instance); // Create the surface from the window
|
||||
CreateDevice();
|
||||
|
||||
|
||||
swapChain.Init(device, surface, window);
|
||||
swapChainRenderPass.Init(device, &swapChain);
|
||||
|
||||
pipeline.Init(device->device);
|
||||
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
void Context::Close()
|
||||
{
|
||||
if (!initialized) return;
|
||||
device->WaitIdle();
|
||||
|
||||
pipeline.Close();
|
||||
swapChainRenderPass.Close();
|
||||
swapChain.Close();
|
||||
deviceManager.Close();
|
||||
//TODO
|
||||
|
||||
if (enableValidationLayer) Debug::CloseValidationLayers(instance);
|
||||
initialized = false;
|
||||
}
|
||||
|
||||
void Context::Resize(const uint32_t newWidth, const uint32_t newHeight)
|
||||
{
|
||||
device->WaitIdle();
|
||||
swapChain.Resize(newWidth, newHeight);
|
||||
}
|
||||
|
||||
void Context::CreateInstance()
|
||||
{
|
||||
if (enableValidationLayer) RequireExtension(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
|
||||
vk::ApplicationInfo appInfo(graphicsAppManager->GetGraphicsApp()->GetAppName().c_str(), graphicsAppManager->GetGraphicsApp()->GetAppVersionAsInt(), ENGINE_NAME, ENGINE_VERSION.intVersion, VK_MAKE_VERSION(1, 1, 0));
|
||||
std::vector<const char*> extensions = Utils::toCString(requiredExtensions), layers = Debug::GetValidationLayers();
|
||||
|
||||
const vk::InstanceCreateInfo createInfo(vk::InstanceCreateFlags(), &appInfo, enableValidationLayer ? layers.size() : 0,
|
||||
layers.data(), extensions.size(), extensions.data());
|
||||
|
||||
instance = vk::createInstance(createInfo);
|
||||
|
||||
if (enableValidationLayer) Debug::SetupValidationLayers(instance, vk::DebugReportFlagBitsEXT::eError | vk::DebugReportFlagBitsEXT::eWarning | vk::DebugReportFlagBitsEXT::ePerformanceWarning /*| vk::DebugReportFlagBitsEXT::eInformation | vk::DebugReportFlagBitsEXT::eDebug*/);
|
||||
dynamicDispatch.init(instance, &vkGetInstanceProcAddr);
|
||||
}
|
||||
|
||||
void Context::CreateDevice()
|
||||
{
|
||||
deviceManager.Init(instance);
|
||||
device = deviceManager.GetCompatibleDevice({ VK_KHR_SWAPCHAIN_EXTENSION_NAME });
|
||||
device->PrepareDevice({ VK_KHR_SWAPCHAIN_EXTENSION_NAME }, surface);
|
||||
dynamicDispatch.init(instance, &vkGetInstanceProcAddr, device->device, &vkGetDeviceProcAddr);
|
||||
Logger::RENDER->info("Found device: {0}", device->GetDeviceName());;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user