10#define GLFW_INCLUDE_VULKAN
11#include <GLFW/glfw3.h>
14#if defined(WIN32_BACKEND)
15#include <vulkan/vulkan_win32.h>
18#if defined(WAYLAND_BACKEND)
19#include <vulkan/vulkan_wayland.h>
27 const std::vector<const char*>& required_extensions)
33 "Vulkan context initialization requested, but graphics API is not set to Vulkan!");
37 std::vector<const char*> extensions = required_extensions;
39#if defined(GLFW_BACKEND)
41 GLFWSingleton::configure(graphics_config.glfw_preinit_config);
42 for (
const char* ext : GLFWSingleton::get_required_instance_extensions()) {
43 if (!std::ranges::contains(extensions, ext))
44 extensions.push_back(ext);
47#elif defined(WIN32_BACKEND)
48 extensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
49 extensions.push_back(VK_KHR_WIN32_SURFACE_EXTENSION_NAME);
50#elif defined(WAYLAND_BACKEND)
51 extensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
52 extensions.push_back(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME);
57 "Failed to initialize Vulkan instance!");
63 "Failed to initialize Vulkan device!");
69 "Vulkan context initialized successfully.");
77 "Cannot create surface: null window");
81#if defined(GLFW_BACKEND)
82 auto* glfw_window =
dynamic_cast<GlfwWindow*
>(window.get());
85 "Cannot create surface: window is not a GlfwWindow");
89 GLFWwindow* glfw_handle = glfw_window->get_glfw_handle();
92 "Cannot create surface: null GLFW handle");
96 VkSurfaceKHR c_surface {};
98 return glfwCreateWindowSurface(
105 if (result != VK_SUCCESS) {
107 "Failed to create GLFW window surface for window '{}'",
108 window->get_create_info().title);
112 vk::SurfaceKHR surface(c_surface);
116 "Surface created for window '{}'", window->get_create_info().title);
120#elif defined(WIN32_BACKEND)
122 auto hwnd =
static_cast<HWND
>(window->get_native_handle());
125 "Cannot create surface: null HWND");
129 VkWin32SurfaceCreateInfoKHR info {};
130 info.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
131 info.hinstance = GetModuleHandleW(
nullptr);
134 VkSurfaceKHR c_surface {};
135 if (vkCreateWin32SurfaceKHR(
137 &info,
nullptr, &c_surface)
140 "Failed to create Win32 Vulkan surface for window '{}'",
141 window->get_create_info().title);
145 vk::SurfaceKHR surface(c_surface);
148 "Win32 surface created for window '{}'", window->get_create_info().title);
151#elif defined(WAYLAND_BACKEND)
153 auto* wl_display =
static_cast<struct wl_display*
>(window->get_native_display());
154 auto* wl_surface =
static_cast<struct wl_surface*
>(window->get_native_handle());
155 if (!wl_display || !wl_surface) {
157 "Cannot create surface: null Wayland display or surface handle");
161 VkWaylandSurfaceCreateInfoKHR info {};
162 info.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
163 info.display = wl_display;
164 info.surface = wl_surface;
166 VkSurfaceKHR c_surface {};
167 if (vkCreateWaylandSurfaceKHR(
169 &info,
nullptr, &c_surface)
172 "Failed to create Wayland Vulkan surface for window '{}'",
173 window->get_create_info().title);
177 vk::SurfaceKHR surface(c_surface);
180 "Wayland surface created for window '{}'", window->get_create_info().title);
186 "No windowing backend available for surface creation");
195 auto it = std::ranges::find(
m_surfaces, surface);
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
bool update_present_family(vk::SurfaceKHR surface)
Update presentation support for a surface.
vk::SurfaceKHR create_surface(std::shared_ptr< Window > window)
Create surface from window's native handles.
std::vector< vk::SurfaceKHR > m_surfaces
void cleanup()
Cleanup all Vulkan resources.
GlobalGraphicsConfig m_graphics_config
bool initialize(const GlobalGraphicsConfig &graphics_config, bool enable_validation=true, const std::vector< const char * > &required_extensions={})
Initialize Vulkan context.
void destroy_surface(vk::SurfaceKHR surface)
Destroy a specific surface Called when window is unregistered.
void cleanup()
Cleanup device resources.
bool update_presentation_queue(vk::SurfaceKHR surface)
Update presentation queue family for a specific surface.
bool initialize(vk::Instance instance, vk::SurfaceKHR temp_surface, const GraphicsBackendInfo &backend_info)
Initialize device (pick physical device and create logical device)
void cleanup()
Cleanup Vulkan instance.
vk::Instance get_instance() const
Get the Vulkan instance handle.
bool initialize(bool enable_validation=true, const std::vector< const char * > &required_extensions={})
Initialize Vulkan instance.
@ GraphicsBackend
Graphics/visual rendering backend (Vulkan, OpenGL)
@ Core
Core engine, backend, subsystems.
auto dispatch_main_sync(Func &&func, Args &&... args) -> decltype(auto)
@ GLFW
GLFW3 (default, cross-platform)
GraphicsBackendInfo backend_info
Graphics backend configuration.
WindowingBackend windowing_backend
Selected windowing backend.
GraphicsApi requested_api
Selected graphics API for rendering.