Create surface from window's native handles.
75{
78 "Cannot create surface: null window");
79 return nullptr;
80 }
81
82#if defined(COCOA_BACKEND)
84 auto* cocoa_window =
dynamic_cast<CocoaWindow*
>(
window.get());
85 if (!cocoa_window) {
87 "Cannot create surface: window is not a CocoaWindow");
88 return nullptr;
89 }
90
91 const void* layer = cocoa_window->get_metal_layer();
92 if (!layer) {
94 "Cannot create surface: null CAMetalLayer");
95 return nullptr;
96 }
97
99 auto create_metal_surface = reinterpret_cast<PFN_vkCreateMetalSurfaceEXT>(
100 vkGetInstanceProcAddr(
instance,
"vkCreateMetalSurfaceEXT"));
101 if (!create_metal_surface) {
103 "Cannot create surface: vkCreateMetalSurfaceEXT unavailable");
104 return nullptr;
105 }
106
107 VkMetalSurfaceCreateInfoEXT info {};
108 info.sType = VK_STRUCTURE_TYPE_METAL_SURFACE_CREATE_INFO_EXT;
109 info.pLayer = static_cast<const CAMetalLayer*>(layer);
110
111 VkSurfaceKHR c_surface {};
113 return create_metal_surface(
instance, &info,
nullptr, &c_surface);
114 });
115
116 if (result != VK_SUCCESS) {
118 "Failed to create Metal surface for window '{}'",
119 window->get_create_info().title);
120 return nullptr;
121 }
122
123 vk::SurfaceKHR surface(c_surface);
125
127 "Metal surface created for window '{}'",
window->get_create_info().title);
128
129 return surface;
130 }
131#endif
132
133#if defined(WIN32_BACKEND)
135 auto hwnd =
static_cast<HWND
>(
window->get_native_handle());
136 if (!hwnd) {
138 "Cannot create surface: null HWND");
139 return nullptr;
140 }
141
142 VkWin32SurfaceCreateInfoKHR info {};
143 info.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
144 info.hinstance = GetModuleHandleW(nullptr);
145 info.hwnd = hwnd;
146
147 VkSurfaceKHR c_surface {};
148 if (vkCreateWin32SurfaceKHR(
150 &info, nullptr, &c_surface)
151 != VK_SUCCESS) {
153 "Failed to create Win32 Vulkan surface for window '{}'",
154 window->get_create_info().title);
155 return nullptr;
156 }
157
158 vk::SurfaceKHR surface(c_surface);
161 "Win32 surface created for window '{}'",
window->get_create_info().title);
162 return surface;
163 }
164#elif defined(WAYLAND_BACKEND)
165 {
166 auto* wl_display =
static_cast<struct wl_display*
>(
window->get_native_display());
167 auto* wl_surface =
static_cast<struct wl_surface*
>(
window->get_native_handle());
168 if (!wl_display || !wl_surface) {
170 "Cannot create surface: null Wayland display or surface handle");
171 return nullptr;
172 }
173
174 VkWaylandSurfaceCreateInfoKHR info {};
175 info.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
176 info.display = wl_display;
177 info.surface = wl_surface;
178
179 VkSurfaceKHR c_surface {};
180 if (vkCreateWaylandSurfaceKHR(
182 &info, nullptr, &c_surface)
183 != VK_SUCCESS) {
185 "Failed to create Wayland Vulkan surface for window '{}'",
186 window->get_create_info().title);
187 return nullptr;
188 }
189
190 vk::SurfaceKHR surface(c_surface);
193 "Wayland surface created for window '{}'",
window->get_create_info().title);
194 return surface;
195 }
196#endif
197
199 "No windowing backend available for surface creation");
200 return nullptr;
201}
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
std::weak_ptr< Core::Window > window
std::vector< vk::SurfaceKHR > m_surfaces
GlobalGraphicsConfig m_graphics_config
vk::Instance get_instance() const
Get the Vulkan instance handle.
@ GraphicsBackend
Graphics/visual rendering backend (Vulkan, OpenGL)
@ Core
Core engine, backend, subsystems.
auto dispatch_main_sync(Func &&func, Args &&... args) -> decltype(auto)
WindowingBackend windowing_backend
Selected windowing backend.