From 24668975310c8f62a14be31b7371998d435a07de Mon Sep 17 00:00:00 2001 From: BZLZHH Date: Sun, 25 May 2025 10:01:57 +0800 Subject: [PATCH] [Feat] (Diligent): Add Diligent Vulkan backend. --- CMakeLists.txt | 3 +- MG/Constants.h | 4 ++ MG/Includes.h | 1 + .../EGL/Diligent/DiligentConfig.h | 10 ++++ .../EGL/Diligent/{GLES => }/EGL_impl.cpp | 47 ++++++++++++++++--- .../EGL/Diligent/{GLES => }/EGL_impl.h | 17 +++---- 6 files changed, 64 insertions(+), 18 deletions(-) create mode 100644 MG/MG_GL/Implementations/EGL/Diligent/DiligentConfig.h rename MG/MG_GL/Implementations/EGL/Diligent/{GLES => }/EGL_impl.cpp (88%) rename MG/MG_GL/Implementations/EGL/Diligent/{GLES => }/EGL_impl.h (89%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 52bda6f1..67a922b2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,7 +53,7 @@ add_library(${CMAKE_PROJECT_NAME} SHARED MG/MG_GL/Implementations/EGL/GLES/EGL_WRAP.cpp - MG/MG_GL/Implementations/EGL/Diligent/GLES/EGL_impl.cpp + MG/MG_GL/Implementations/EGL/Diligent/EGL_impl.cpp # MG_GL/State MG/MG_GL/State/Core/GLState.cpp @@ -119,6 +119,7 @@ target_link_libraries(MobileGL PRIVATE Diligent-Common Diligent-GraphicsEngineOpenGL-shared + Diligent-GraphicsEngineVk-shared ) if (PROFILING) diff --git a/MG/Constants.h b/MG/Constants.h index fc0a631d..e4f43943 100644 --- a/MG/Constants.h +++ b/MG/Constants.h @@ -178,6 +178,10 @@ namespace MG_Constants { #define BACKEND_METAL 0x0016 #define BACKEND_GLES 0x0017 #define BACKEND_DILIGENT 0x0018 + +inline const int BACKEND_DILIGENT_VULKAN = 0x0020; +inline const int BACKEND_DILIGENT_METAL = 0x0021; // not ready currently +inline const int BACKEND_DILIGENT_OPENGL = 0x0022; } inline const char* RenderName = "MobileGL"; diff --git a/MG/Includes.h b/MG/Includes.h index 3519ac28..9313a183 100644 --- a/MG/Includes.h +++ b/MG/Includes.h @@ -138,6 +138,7 @@ #if BACKEND_TYPE == BACKEND_DILIGENT #include "EngineFactory.h" #include "EngineFactoryOpenGL.h" +#include "EngineFactoryVk.h" #include "RefCntAutoPtr.hpp" #include "PipelineState.h" diff --git a/MG/MG_GL/Implementations/EGL/Diligent/DiligentConfig.h b/MG/MG_GL/Implementations/EGL/Diligent/DiligentConfig.h new file mode 100644 index 00000000..40a4e64b --- /dev/null +++ b/MG/MG_GL/Implementations/EGL/Diligent/DiligentConfig.h @@ -0,0 +1,10 @@ +// +// Created by BZLZHH on 2025/5/25. +// + +#pragma once +#include "../../../../Includes.h" + +namespace MG_EGL::Diligent { + inline const int DILIGENT_BACKEND_TYPE = MG_Constants::Backend::BACKEND_DILIGENT_VULKAN; +} \ No newline at end of file diff --git a/MG/MG_GL/Implementations/EGL/Diligent/GLES/EGL_impl.cpp b/MG/MG_GL/Implementations/EGL/Diligent/EGL_impl.cpp similarity index 88% rename from MG/MG_GL/Implementations/EGL/Diligent/GLES/EGL_impl.cpp rename to MG/MG_GL/Implementations/EGL/Diligent/EGL_impl.cpp index dac1bcbe..2be345e5 100644 --- a/MG/MG_GL/Implementations/EGL/Diligent/GLES/EGL_impl.cpp +++ b/MG/MG_GL/Implementations/EGL/Diligent/EGL_impl.cpp @@ -4,6 +4,7 @@ #include "EGL_impl.h" +typedef Diligent::IEngineFactoryVk* (*Diligent_GetEngineFactoryVk_t)(); typedef Diligent::IEngineFactoryOpenGL* (*Diligent_GetEngineFactoryOpenGL_t)(); using namespace Diligent; @@ -53,9 +54,8 @@ void main(in PSInput PSIn, PSOut.Color = float4(PSIn.Color.rgb, 1.0); } )"; - - EGLSurface eglCreateWindowSurface(EGLDisplay dpy, EGLConfig config, NativeWindowType window, - const EGLint *attrib_list) { + + void LoadDiligentCoreOpenGL(NativeWindowType window) { void *handle = dlopen("libGraphicsEngineOpenGL.so", RTLD_LAZY); auto Diligent_GetEngineFactoryOpenGL = (Diligent_GetEngineFactoryOpenGL_t) dlsym(handle, @@ -68,10 +68,44 @@ void main(in PSInput PSIn, ::Diligent::SwapChainDesc SCDesc; - auto &ctx = MG_EGL::Diligent::DeviceContext::GetInstance(); - pFactoryOpenGL->CreateDeviceAndSwapChainGL( EngineCI, &ctx.pDevice, &ctx.pContext, SCDesc, &ctx.pSwapChain); + } + + void LoadDiligentCoreVulkan(NativeWindowType window) { + void *handle = dlopen("libGraphicsEngineVk.so", RTLD_LAZY); + auto Diligent_GetEngineFactoryVk = + (Diligent_GetEngineFactoryVk_t) dlsym(handle, + "Diligent_GetEngineFactoryVk"); + + auto *pFactoryVk = Diligent_GetEngineFactoryVk(); + ::Diligent::EngineVkCreateInfo EngineCI; + + ::Diligent::SwapChainDesc SCDesc; + + pFactoryVk->CreateDeviceAndContextsVk( + EngineCI, &ctx.pDevice, &ctx.pContext); + AndroidNativeWindow nativeWindow{window}; + pFactoryVk->CreateSwapChainVk( + ctx.pDevice, ctx.pContext, SCDesc, nativeWindow, &ctx.pSwapChain); + } + + void LoadDiligentCore(NativeWindowType window) { + switch (DILIGENT_BACKEND_TYPE) { + case MG_Constants::Backend::BACKEND_DILIGENT_VULKAN: + LoadDiligentCoreVulkan(window); + break; + case MG_Constants::Backend::BACKEND_DILIGENT_OPENGL: + default: + LoadDiligentCoreOpenGL(window); + break; + } + + } + + EGLSurface eglCreateWindowSurface(EGLDisplay dpy, EGLConfig config, NativeWindowType window, + const EGLint *attrib_list) { + LoadDiligentCore(window); // Pipeline state object encompasses configuration of all GPU stages @@ -205,8 +239,6 @@ void main(in PSInput PSIn, } EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw) { - auto &ctx = MG_EGL::Diligent::DeviceContext::GetInstance(); - // Clear the back buffer const float ClearColor[] = {0.350f, 0.350f, 0.350f, 1.0f}; // Let the engine perform required state transitions @@ -226,6 +258,7 @@ void main(in PSInput PSIn, DrawAttribs drawAttrs; drawAttrs.NumVertices = 3; // We will render 3 vertices ctx.pContext->Draw(drawAttrs); + ctx.pContext->Flush(); ctx.pSwapChain->Present(); diff --git a/MG/MG_GL/Implementations/EGL/Diligent/GLES/EGL_impl.h b/MG/MG_GL/Implementations/EGL/Diligent/EGL_impl.h similarity index 89% rename from MG/MG_GL/Implementations/EGL/Diligent/GLES/EGL_impl.h rename to MG/MG_GL/Implementations/EGL/Diligent/EGL_impl.h index c80908ca..83c1f85d 100644 --- a/MG/MG_GL/Implementations/EGL/Diligent/GLES/EGL_impl.h +++ b/MG/MG_GL/Implementations/EGL/Diligent/EGL_impl.h @@ -2,22 +2,17 @@ // Created by Swung 0x48 on 2025-05-18. // -#ifndef MOBILEGLUES_EGL_IMPL_H -#define MOBILEGLUES_EGL_IMPL_H +#ifndef MOBILEGL_EGL_IMPL_H +#define MOBILEGL_EGL_IMPL_H - -#include "../../../../../Includes.h" +#include "../../../../Includes.h" +#include "DiligentConfig.h" #if BACKEND_TYPE == BACKEND_DILIGENT namespace MG_EGL::Diligent { struct DeviceContext { public: - static inline DeviceContext& GetInstance() { - static DeviceContext devctx; - return devctx; - } - ::Diligent::RefCntAutoPtr<::Diligent::IRenderDevice> pDevice; ::Diligent::RefCntAutoPtr<::Diligent::IDeviceContext> pContext; ::Diligent::RefCntAutoPtr<::Diligent::ISwapChain> pSwapChain; @@ -27,6 +22,8 @@ namespace MG_EGL::Diligent { int minorVer = 5; }; + + static struct DeviceContext ctx; EGLSurface eglCreateWindowSurface(EGLDisplay dpy, EGLConfig config, NativeWindowType window, const EGLint* attrib_list); EGLBoolean eglChooseConfig(EGLDisplay dpy, const EGLint* attrib_list, EGLConfig* configs, EGLint config_size, EGLint* num_config); @@ -59,4 +56,4 @@ MG_EXPORT __eglMustCastToProperFunctionPointerType eglGetProcAddress(const char #endif -#endif //MOBILEGLUES_EGL_IMPL_H +#endif //MOBILEGL_EGL_IMPL_H