[Feat] (MG_Backend/DirectVulkan): hooking up Windows VK WSI

This commit is contained in:
2026-02-10 13:28:05 +08:00
parent 1c2924eef9
commit 162dbf8750
4 changed files with 24 additions and 6 deletions
+8 -1
View File
@@ -91,12 +91,19 @@
#endif
#ifdef __ANDROID__
#define VK_USE_PLATFORM_ANDROID_KHR
#include <unistd.h>
#include <pthread.h>
#include <android/log.h>
#include <android/native_window.h>
#endif
#ifdef __ANDROID__
#define VK_USE_PLATFORM_ANDROID_KHR
#elif _WIN32
#define VK_USE_PLATFORM_WIN32_KHR
#else
#warning "VK_USE_PLATFORM_*_KHR not defined for this platform!"
#endif
#include <vulkan/vulkan.h>
#ifdef TRACY_ENABLE
@@ -51,6 +51,10 @@ namespace MobileGL::MG_Backend::DirectVulkan {
const char* exts[] = {VK_KHR_SURFACE_EXTENSION_NAME,
#if __ANDROID__
VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
#elif _WIN32
VK_KHR_WIN32_SURFACE_EXTENSION_NAME
#else
#error "VulkanContext::CreateInstance: VK_KHR_*_surface extension not defined on this platform"
#endif
}; // TODO: support more platforms
@@ -63,16 +67,23 @@ namespace MobileGL::MG_Backend::DirectVulkan {
}
void VulkanContext::CreateSurface(NativeWindowType window) {
#if __ANDROID__
if (!Instance) throw RuntimeError("Instance not created");
#if defined VK_USE_PLATFORM_ANDROID_KHR
auto* nativeWindow = static_cast<ANativeWindow*>(window);
if (!nativeWindow) throw RuntimeError("ANativeWindowType is null");
VkAndroidSurfaceCreateInfoKHR sci{VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR};
sci.window = nativeWindow;
VK_VERIFY(vkCreateAndroidSurfaceKHR(Instance, &sci, nullptr, &Surface), "vkCreateAndroidSurfaceKHR failed");
#elif defined VK_USE_PLATFORM_WIN32_KHR
auto hwnd = static_cast<HWND>(window);
MOBILEGL_ASSERT(hwnd, "HWND is null");
VkWin32SurfaceCreateInfoKHR sci{VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR};
sci.hwnd = hwnd;
VK_VERIFY(vkCreateWin32SurfaceKHR(Instance, &sci, nullptr, &Surface), "vkCreateWin32SurfaceKHR failed");
#else
//#warning "VulkanRenderer::Initialize called on a platform which is not supported yet"
MGLOG_W("VulkanRenderer::Initialize called on a platform which is not supported yet"); // TODO: support more
// platforms
#endif
@@ -12,9 +12,7 @@
#define VK_VERIFY(expr, ...) \
do { \
VkResult _vk_verify_result = (expr); \
if (_vk_verify_result != VK_SUCCESS) { \
MGLOG_E("Vulkan error %d at %s:%d" __VA_OPT__(" - ") __VA_ARGS__, _vk_verify_result, __FILE__, __LINE__); \
} \
MOBILEGL_ASSERT(_vk_verify_result == VK_SUCCESS, "Vulkan error %d at %s:%d" __VA_OPT__(" - ") __VA_ARGS__, _vk_verify_result, __FILE__, __LINE__); \
} while (0)
namespace MobileGL::MG_Backend::DirectVulkan {
@@ -41,6 +41,8 @@ namespace MobileGL::MG_Backend::DirectVulkan {
void VulkanRenderer::Shutdown() {
if (!Ctx) return;
if (Ctx->GetDevice() == VK_NULL_HANDLE) return;
vkDeviceWaitIdle(Ctx->GetDevice());
DestroyFrameResources();