Skip to content

Commit

Permalink
Create graphics system via factory function
Browse files Browse the repository at this point in the history
  • Loading branch information
doanamo committed Jan 30, 2025
1 parent c649c45 commit aef3ea2
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 65 deletions.
1 change: 1 addition & 0 deletions Engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ add_library(Engine STATIC
"Graphics/GraphicsSystem.cpp"
"Graphics/GraphicsStats.cpp"
"Graphics/Vulkan/VulkanAllocator.cpp"
"Graphics/Vulkan/GraphicsSystem.cpp"
"Engine.cpp"
)

Expand Down
3 changes: 3 additions & 0 deletions Engine/Graphics/Defines.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#pragma once

// #todo: Graphics API should be defined from CMake.
#define GRAPHICS_VULKAN

#ifdef CONFIG_DEBUG
#define ENABLE_GRAPHICS_DEBUG // Enable graphics debugging
#endif
60 changes: 14 additions & 46 deletions Engine/Graphics/GraphicsSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,64 +4,32 @@
#include "Platform/Window.hpp"
#include "Engine.hpp"

Graphics::System::~System()
{
LOG("Destroying graphics system...");

if(m_instance)
{
vkDestroyInstance(m_instance, &g_vkAllocationCallbacks);
}
}

bool Graphics::System::Setup(Platform::Window* window)
UniquePtr<Graphics::System> Graphics::System::Create(Platform::Window* window)
{
LOG("Creating graphics system...");
UniquePtr<System> instance = Memory::New<System>(PrivateConstructorTag{});

ASSERT(window);
m_window = window;
instance->m_window = window;

if(!CreateInstance())
if(!instance->OnCreate())
{
LOG_ERROR("Failed to setup graphics system");
return false;
LOG_ERROR("Failed to create graphics system");
return nullptr;
}

LOG_SUCCESS("Created graphics system");
return true;
return instance;
}

bool Graphics::System::CreateInstance()
Graphics::System::System(PrivateConstructorTag)
{
InlineArray<const char*, 2> extensions;
extensions.Add(VK_KHR_SURFACE_EXTENSION_NAME);
extensions.Add(Platform::Window::GetVulkanSurfaceExtension());

VkApplicationInfo applicationInfo{};
applicationInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
applicationInfo.apiVersion = VK_API_VERSION_1_3;
applicationInfo.pEngineName = "Bourne Engine";
applicationInfo.pApplicationName = Engine::GetApplicationName();
applicationInfo.engineVersion = VK_MAKE_VERSION(EngineVersion::Major, EngineVersion::Minor, EngineVersion::Patch);
applicationInfo.applicationVersion = VK_MAKE_VERSION(ApplicationVersion::Major, ApplicationVersion::Minor, ApplicationVersion::Patch);

VkInstanceCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
createInfo.pApplicationInfo = &applicationInfo;
createInfo.enabledLayerCount = 0;
createInfo.ppEnabledLayerNames = nullptr;
createInfo.enabledExtensionCount = extensions.GetSize();
createInfo.ppEnabledExtensionNames = extensions.GetData();

const VkResult result = vkCreateInstance(&createInfo, &g_vkAllocationCallbacks, &m_instance);
if(result != VK_SUCCESS)
{
LOG_ERROR("Failed to create Vulkan instance (error: %s)", string_VkResult(result));
return false;
}
LOG_INFO("Creating graphics system...");
}

LOG_SUCCESS("Created Vulkan instance");
return true;
Graphics::System::~System()
{
LOG_INFO("Destroying graphics system...");
OnDestroy();
}

void Graphics::System::BeginFrame()
Expand Down
27 changes: 17 additions & 10 deletions Engine/Graphics/GraphicsSystem.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#pragma once

#include "GraphicsStats.hpp"
#if defined(GRAPHICS_VULKAN)
#include "Vulkan/GraphicsSystem.hpp"
#else
#error "Unknown graphics define!"
#endif

namespace Platform
{
Expand All @@ -9,24 +13,27 @@ namespace Platform

namespace Graphics
{
class System final
class System final : NonCopyable
{
SystemPrivate m_private;
Platform::Window* m_window = nullptr;
VkInstance m_instance;

struct PrivateConstructorTag
{
explicit PrivateConstructorTag() = default;
};

public:
System() = default;
~System();
static UniquePtr<System> Create(Platform::Window* window);

System(const System&) = delete;
System& operator=(const System&) = delete;
System(PrivateConstructorTag);
~System();

bool Setup(Platform::Window* window);
void BeginFrame();
void EndFrame();

private:
bool CreateInstance();
void DestroyInstance();
bool OnCreate();
void OnDestroy();
};
}
8 changes: 5 additions & 3 deletions Engine/Graphics/Includes.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <vulkan/vulkan.h>
#include <vulkan/vk_enum_string_helper.h>
#include "Vulkan/VulkanAllocator.hpp"
#if defined(GRAPHICS_VULKAN)
#include "Vulkan/Includes.hpp"
#else
#error "Unknown graphics define!"
#endif
57 changes: 57 additions & 0 deletions Engine/Graphics/Vulkan/GraphicsSystem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "Shared.hpp"
#include "GraphicsSystem.hpp"

static bool CreateVulkanInstance(Graphics::SystemPrivate& vulkanPrivate)
{
InlineArray<const char*, 2> extensions;
extensions.Add(VK_KHR_SURFACE_EXTENSION_NAME);
extensions.Add(Platform::Window::GetVulkanSurfaceExtension());

VkApplicationInfo applicationInfo{};
applicationInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
applicationInfo.apiVersion = VK_API_VERSION_1_3;
applicationInfo.pEngineName = "Bourne Engine";
applicationInfo.pApplicationName = Engine::GetApplicationName();
applicationInfo.engineVersion = VK_MAKE_VERSION(EngineVersion::Major, EngineVersion::Minor, EngineVersion::Patch);
applicationInfo.applicationVersion = VK_MAKE_VERSION(ApplicationVersion::Major, ApplicationVersion::Minor, ApplicationVersion::Patch);

VkInstanceCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
createInfo.pApplicationInfo = &applicationInfo;
createInfo.enabledLayerCount = 0;
createInfo.ppEnabledLayerNames = nullptr;
createInfo.enabledExtensionCount = extensions.GetSize();
createInfo.ppEnabledExtensionNames = extensions.GetData();

ASSERT_SLOW(vulkanPrivate.instance == nullptr);
const VkResult result = vkCreateInstance(&createInfo, &g_vkAllocationCallbacks, &vulkanPrivate.instance);
if(result != VK_SUCCESS)
{
LOG_ERROR("Failed to create Vulkan instance (error: %s)", string_VkResult(result));
return false;
}

LOG_SUCCESS("Created Vulkan instance");
return true;
}

static void DestroyVulkanInstance(Graphics::SystemPrivate& vulkanPrivate)
{
if(vulkanPrivate.instance)
{
vkDestroyInstance(vulkanPrivate.instance, &g_vkAllocationCallbacks);
}
}

bool Graphics::System::OnCreate()
{
if(!CreateVulkanInstance(m_private))
return false;

return true;
}

void Graphics::System::OnDestroy()
{
DestroyVulkanInstance(m_private);
}
9 changes: 9 additions & 0 deletions Engine/Graphics/Vulkan/GraphicsSystem.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

namespace Graphics
{
struct SystemPrivate
{
VkInstance instance = nullptr;
};
}
5 changes: 5 additions & 0 deletions Engine/Graphics/Vulkan/Includes.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include <vulkan/vulkan.h>
#include <vulkan/vk_enum_string_helper.h>
#include "VulkanAllocator.hpp"
2 changes: 1 addition & 1 deletion Engine/Platform/Includes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
#elif defined(PLATFORM_LINUX)
#include "Linux/Includes.hpp"
#else
#error Unsupported platform!
#error "Unknown platform define!"
#endif
2 changes: 1 addition & 1 deletion Engine/Platform/Window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#elif defined(PLATFORM_LINUX)
#include "Linux/Window.hpp"
#else
#error "Unknown platform!"
#error "Unknown platform define!"
#endif

namespace Platform
Expand Down
8 changes: 4 additions & 4 deletions Example/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ int main(const int argc, const char* const* argv)
return -1;
}

Graphics::System graphics;
if(!graphics.Setup(window.Get()))
UniquePtr<Graphics::System> graphics = Graphics::System::Create(window.Get());
if(graphics == nullptr)
{
LOG_FATAL("Failed to setup graphics system");
return -1;
Expand All @@ -38,10 +38,10 @@ int main(const int argc, const char* const* argv)
if(window->IsClosing())
break;

graphics.BeginFrame();
graphics->BeginFrame();
{
}
graphics.EndFrame();
graphics->EndFrame();
}

LOG_INFO("Exiting application...");
Expand Down

0 comments on commit aef3ea2

Please sign in to comment.