mirror of
https://github.com/MobileGL-Dev/MobileGL
synced 2026-09-11 13:48:30 +09:00
[Fix] (MG_Backend/DirectVulkan): Avoid directly using ANativeWindow in abstracted layers.
This commit is contained in:
@@ -14,7 +14,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
Shutdown();
|
Shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
void VulkanContext::Initialize(ANativeWindow* window, const std::string& appName) {
|
void VulkanContext::Initialize(NativeWindowType window, const std::string& appName) {
|
||||||
if (Initialized) return;
|
if (Initialized) return;
|
||||||
CreateInstance(appName);
|
CreateInstance(appName);
|
||||||
CreateSurface(window);
|
CreateSurface(window);
|
||||||
@@ -58,11 +58,20 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
ThrowIfFailed(vkCreateInstance(&ci, nullptr, &Instance), "vkCreateInstance failed");
|
ThrowIfFailed(vkCreateInstance(&ci, nullptr, &Instance), "vkCreateInstance failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
void VulkanContext::CreateSurface(ANativeWindow* window) {
|
void VulkanContext::CreateSurface(NativeWindowType window) {
|
||||||
|
#if __ANDROID__
|
||||||
if (!Instance) throw MobileGL::RuntimeError("Instance not created");
|
if (!Instance) throw MobileGL::RuntimeError("Instance not created");
|
||||||
|
|
||||||
|
auto* nativeWindow = static_cast<ANativeWindow*>(window);
|
||||||
|
if (!nativeWindow) throw MobileGL::RuntimeError("ANativeWindowType is null");
|
||||||
|
|
||||||
VkAndroidSurfaceCreateInfoKHR sci{VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR};
|
VkAndroidSurfaceCreateInfoKHR sci{VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR};
|
||||||
sci.window = window;
|
sci.window = nativeWindow;
|
||||||
ThrowIfFailed(vkCreateAndroidSurfaceKHR(Instance, &sci, nullptr, &Surface), "vkCreateAndroidSurfaceKHR failed");
|
ThrowIfFailed(vkCreateAndroidSurfaceKHR(Instance, &sci, nullptr, &Surface), "vkCreateAndroidSurfaceKHR failed");
|
||||||
|
#else
|
||||||
|
MGLOG_W("VulkanRenderer::Initialize called on a platform which is not supported yet"); // TODO: support more
|
||||||
|
// platforms
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void VulkanContext::PickPhysicalDevice() {
|
void VulkanContext::PickPhysicalDevice() {
|
||||||
@@ -110,4 +119,4 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
vkGetDeviceQueue(Device, GraphicsQueueFamily, 0, &GraphicsQueue);
|
vkGetDeviceQueue(Device, GraphicsQueueFamily, 0, &GraphicsQueue);
|
||||||
MGLOG_D("Logical device created");
|
MGLOG_D("Logical device created");
|
||||||
}
|
}
|
||||||
} // namespace MobileGL::MG_Backend::DirectVulkan
|
} // namespace MobileGL::MG_Backend::DirectVulkan
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
VulkanContext() = default;
|
VulkanContext() = default;
|
||||||
~VulkanContext();
|
~VulkanContext();
|
||||||
|
|
||||||
void Initialize(ANativeWindow* window, const std::string& appName = "VulkanEngineApp");
|
void Initialize(NativeWindowType window, const std::string& appName = "MobileGL-VulkanRenderer");
|
||||||
void Shutdown();
|
void Shutdown();
|
||||||
|
|
||||||
VkInstance GetInstance() const { return Instance; }
|
VkInstance GetInstance() const { return Instance; }
|
||||||
@@ -34,7 +34,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
void CreateInstance(const std::string& appName);
|
void CreateInstance(const std::string& appName);
|
||||||
void CreateSurface(ANativeWindow* window);
|
void CreateSurface(NativeWindowType window);
|
||||||
void PickPhysicalDevice();
|
void PickPhysicalDevice();
|
||||||
void CreateLogicalDevice();
|
void CreateLogicalDevice();
|
||||||
|
|
||||||
@@ -46,4 +46,4 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
VkSurfaceKHR Surface = VK_NULL_HANDLE;
|
VkSurfaceKHR Surface = VK_NULL_HANDLE;
|
||||||
bool Initialized = false;
|
bool Initialized = false;
|
||||||
};
|
};
|
||||||
} // namespace MobileGL::MG_Backend::DirectVulkan
|
} // namespace MobileGL::MG_Backend::DirectVulkan
|
||||||
|
|||||||
@@ -13,7 +13,7 @@
|
|||||||
#include "FrameContext.h"
|
#include "FrameContext.h"
|
||||||
|
|
||||||
namespace MobileGL::MG_Backend::DirectVulkan {
|
namespace MobileGL::MG_Backend::DirectVulkan {
|
||||||
VulkanRenderer::VulkanRenderer(ANativeWindow* window, const RendererConfig& cfg) : Window(window), Config(cfg) {
|
VulkanRenderer::VulkanRenderer(NativeWindowType window, const RendererConfig& cfg) : Window(window), Config(cfg) {
|
||||||
Ctx = std::make_unique<VulkanContext>();
|
Ctx = std::make_unique<VulkanContext>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -22,7 +22,6 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void VulkanRenderer::Initialize() {
|
void VulkanRenderer::Initialize() {
|
||||||
if (!Window) throw MobileGL::RuntimeError("ANativeWindow is null");
|
|
||||||
Ctx->Initialize(Window, Config.AppName);
|
Ctx->Initialize(Window, Config.AppName);
|
||||||
|
|
||||||
Swapchain = std::make_unique<SwapchainManager>(*Ctx);
|
Swapchain = std::make_unique<SwapchainManager>(*Ctx);
|
||||||
@@ -271,4 +270,4 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
void VulkanRenderer::WaitIdle() {
|
void VulkanRenderer::WaitIdle() {
|
||||||
if (Ctx && Ctx->GetDevice() != VK_NULL_HANDLE) vkDeviceWaitIdle(Ctx->GetDevice());
|
if (Ctx && Ctx->GetDevice() != VK_NULL_HANDLE) vkDeviceWaitIdle(Ctx->GetDevice());
|
||||||
}
|
}
|
||||||
} // namespace MobileGL::MG_Backend::DirectVulkan
|
} // namespace MobileGL::MG_Backend::DirectVulkan
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
|
|
||||||
class VulkanRenderer {
|
class VulkanRenderer {
|
||||||
public:
|
public:
|
||||||
VulkanRenderer(ANativeWindow* window, const RendererConfig& cfg = {});
|
VulkanRenderer(NativeWindowType window, const RendererConfig& cfg = {});
|
||||||
~VulkanRenderer();
|
~VulkanRenderer();
|
||||||
|
|
||||||
void Initialize();
|
void Initialize();
|
||||||
@@ -43,7 +43,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
void WaitIdle();
|
void WaitIdle();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ANativeWindow* Window = nullptr;
|
NativeWindowType Window = 0;
|
||||||
RendererConfig Config;
|
RendererConfig Config;
|
||||||
|
|
||||||
std::unique_ptr<VulkanContext> Ctx;
|
std::unique_ptr<VulkanContext> Ctx;
|
||||||
|
|||||||
@@ -86,25 +86,17 @@ namespace MobileGL {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef __ANDROID__
|
void CreateWindowSurfaceForVulkan(NativeWindowType window) {
|
||||||
void CreateWindowSurfaceForVulkan(ANativeWindow* window) {
|
|
||||||
MG_Backend::DirectVulkan::pVulkanRenderer = MakeUnique<MG_Backend::DirectVulkan::VulkanRenderer>(window);
|
MG_Backend::DirectVulkan::pVulkanRenderer = MakeUnique<MG_Backend::DirectVulkan::VulkanRenderer>(window);
|
||||||
MG_Backend::DirectVulkan::pVulkanRenderer->Initialize();
|
MG_Backend::DirectVulkan::pVulkanRenderer->Initialize();
|
||||||
|
|
||||||
PrepareDemoRes(); // for demo use
|
PrepareDemoRes(); // for demo use
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
EGLSurface CreateWindowSurface(EGLDisplay dpy, EGLConfig config, NativeWindowType window,
|
EGLSurface CreateWindowSurface(EGLDisplay dpy, EGLConfig config, NativeWindowType window,
|
||||||
const EGLint* attrib_list) {
|
const EGLint* attrib_list) {
|
||||||
MGLOG_D("EGLForVulkan::CreateWindowSurface called with window=%p", window);
|
MGLOG_D("EGLForVulkan::CreateWindowSurface called with window=%p", window);
|
||||||
#ifdef __ANDROID__
|
CreateWindowSurfaceForVulkan(window);
|
||||||
auto* nativeWindow = static_cast<ANativeWindow*>(window);
|
|
||||||
CreateWindowSurfaceForVulkan(nativeWindow);
|
|
||||||
#else
|
|
||||||
MGLOG_W("EGLForVulkan::CreateWindowSurface is not implemented for this platform"); // TODO: support more
|
|
||||||
// platforms
|
|
||||||
#endif
|
|
||||||
return (EGLSurface)1;
|
return (EGLSurface)1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user