Skip to content

Commit 6154e94

Browse files
committed
Improve DriverRuntime
1 parent 2a58ace commit 6154e94

File tree

12 files changed

+143
-101
lines changed

12 files changed

+143
-101
lines changed

axmol/base/Director.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1164,7 +1164,7 @@ void Director::cleanupDirector()
11641164

11651165
ProgramManager::destroyInstance();
11661166
VertexLayoutManager::destroyInstance();
1167-
rhi::DriverRuntime::uninit();
1167+
rhi::DriverRuntime::destroyCurrentDriver();
11681168

11691169
// OpenGL view
11701170
if (_renderView)

axmol/platform/android/RenderViewImpl-android.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,10 @@ bool RenderViewImpl::initWithRect(std::string_view /*viewName*/,
109109
{
110110
updateRenderSurface(rect.size.width, rect.size.height, SurfaceUpdateFlag::AllUpdatesSilently);
111111

112-
if (rhi::DriverRuntime::isUnknown())
113-
rhi::DriverRuntime::init(rhi::DriverType::OpenGL);
114-
115-
if (rhi::DriverRuntime::isVulkan())
116-
{
112+
if (rhi::DriverRuntime::isOpenGL())
113+
rhi::DriverRuntime::activateCurrentDriver();
114+
else if (rhi::DriverRuntime::isVulkan())
117115
recreateVkSurface(false);
118-
}
119116

120117
return true;
121118
}

axmol/platform/android/jni/AxmolEngineJni.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ JNIEXPORT void JNICALL Java_dev_axmol_lib_AxmolEngine_nativeInit(JNIEnv* env,
6363
auto app = ax::Application::getInstance();
6464
app->initContextAttrs();
6565

66-
rhi::DriverRuntime::init();
66+
rhi::DriverRuntime::makeCurrentDriver();
6767
}
6868

6969
JNIEXPORT void JNICALL Java_dev_axmol_lib_AxmolEngine_nativeSetEditTextDialogResult(JNIEnv* env,

axmol/platform/desktop/RenderViewImpl.cpp

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ The RenderViewImpl for win32,linux,macos,wasm
107107

108108
namespace ax
109109
{
110+
111+
using namespace rhi;
112+
110113
#if defined(__EMSCRIPTEN__)
111114
struct IVec2
112115
{
@@ -457,15 +460,15 @@ void* RenderViewImpl::getNativeWindow() const
457460

458461
SurfaceHandle RenderViewImpl::getNativeDisplay() const
459462
{
460-
auto driverType = rhi::DriverRuntime::currentDriverType();
461-
if (driverType == rhi::DriverType::Vulkan)
463+
auto driverType = DriverRuntime::currentDriverType();
464+
if (driverType == DriverType::Vulkan)
462465
return _vkSurface;
463466

464467
#if AX_TARGET_PLATFORM == AX_PLATFORM_WIN32
465468
return glfwGetWin32Window(_mainWindow);
466469
#elif AX_TARGET_PLATFORM == AX_PLATFORM_MAC
467-
return driverType == rhi::DriverType::Metal ? (void*)glfwGetCocoaView(_mainWindow)
468-
: (void*)glfwGetNSGLContext(_mainWindow);
470+
return driverType == DriverType::Metal ? (void*)glfwGetCocoaView(_mainWindow)
471+
: (void*)glfwGetNSGLContext(_mainWindow);
469472
return (void*)glfwGetNSGLContext(_mainWindow);
470473
#elif AX_TARGET_PLATFORM == AX_PLATFORM_LINUX
471474
int platform = glfwGetPlatform();
@@ -559,20 +562,14 @@ bool RenderViewImpl::initWithRect(std::string_view viewName,
559562

560563
Vec2 requestWinSize = rect.size * windowZoomFactor;
561564

562-
#if AX_ENABLE_D3D11 || AX_ENABLE_D3D12 || AX_ENABLE_VK || AX_ENABLE_MTL
563565
// Try to initialize a high-performance graphics driver first.
564566
// If any of the high-performance APIs (D3D11/D3D12/Vulkan/Metal) are enabled,
565567
// the runtime will attempt initialization in the default priority order.
566568
// If all attempts fail, OpenGL will then be explicitly selected as the fallback.
567-
ax::rhi::DriverRuntime::init();
568-
#endif
569-
570-
const auto driverType = rhi::DriverRuntime::currentDriverType();
571-
const auto requiresGL = driverType == rhi::DriverType::OpenGL || driverType == rhi::DriverType::Unknown;
572-
if (requiresGL)
569+
DriverRuntime::makeCurrentDriver();
570+
const auto fallbackGL = DriverRuntime::isOpenGL();
571+
if (fallbackGL)
573572
{
574-
if constexpr (!AX_ENABLE_GL)
575-
throw std::runtime_error("OpenGL driver requires AX_ENABLE_GL to be enabled");
576573
#if AX_GLES_PROFILE
577574
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
578575
glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_EGL_CONTEXT_API);
@@ -637,13 +634,11 @@ bool RenderViewImpl::initWithRect(std::string_view viewName,
637634
glfwSetWindowSizeLimits(_mainWindow, 1, 1, GLFW_DONT_CARE, GLFW_DONT_CARE);
638635

639636
#if AX_ENABLE_GL
640-
if (requiresGL)
637+
if (fallbackGL)
641638
{
642639
glfwMakeContextCurrent(_mainWindow);
643-
glfwSetWindowUserPointer(_mainWindow, rhi::gl::__state);
644-
645-
if (driverType == rhi::DriverType::Unknown)
646-
ax::rhi::DriverRuntime::init(rhi::DriverType::OpenGL);
640+
glfwSetWindowUserPointer(_mainWindow, gl::__state);
641+
DriverRuntime::activeCurrentDriver();
647642
}
648643
#endif
649644

@@ -666,13 +661,13 @@ bool RenderViewImpl::initWithRect(std::string_view viewName,
666661
updateRenderSurface(fbWidth, fbHeight, SurfaceUpdateFlag::RenderSizeChanged | SurfaceUpdateFlag::SilentUpdate);
667662

668663
#if AX_ENABLE_VK
669-
if (driverType == rhi::DriverType::Vulkan)
664+
if (DriverRuntime::isVulkan())
670665
{
671666
auto _createSurface = [](VkInstance inst, void* window, VkSurfaceKHR* surface) {
672667
return glfwCreateWindowSurface(inst, static_cast<GLFWwindow*>(window), nullptr, surface);
673668
};
674-
auto driver = static_cast<ax::rhi::vk::DriverImpl*>(axdrv);
675-
const rhi::vk::SurfaceCreateInfo createInfo{
669+
auto driver = static_cast<vk::DriverImpl*>(axdrv);
670+
const vk::SurfaceCreateInfo createInfo{
676671
.window = _mainWindow, .width = fbWidth, .height = fbHeight, .createFunc = _createSurface};
677672
bool ok = driver->recreateSurface(createInfo);
678673
if (!ok)
@@ -729,7 +724,7 @@ bool RenderViewImpl::initWithRect(std::string_view viewName,
729724
glfwSetWindowCloseCallback(_mainWindow, GLFWEventHandler::onGLFWWindowCloseCallback);
730725

731726
#if AX_ENABLE_GL
732-
if (driverType == rhi::DriverType::OpenGL)
727+
if (fallbackGL)
733728
{
734729
# if !defined(__EMSCRIPTEN__)
735730
glfwSwapInterval(contextAttrs.vsync ? 1 : 0);
@@ -817,7 +812,7 @@ void RenderViewImpl::end()
817812
void RenderViewImpl::swapBuffers()
818813
{
819814
#if AX_ENABLE_GL
820-
if (_mainWindow && rhi::DriverRuntime::isOpenGL())
815+
if (_mainWindow && DriverRuntime::isOpenGL())
821816
glfwSwapBuffers(_mainWindow);
822817
#endif
823818
}

axmol/platform/ios/RenderHostView-ios.mm

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
7373

7474
#include "axmol/rhi/DriverRuntime.h"
7575

76+
using namespace ax::rhi;
77+
7678
// CLASS IMPLEMENTATIONS:
7779

7880
#define IOS_MAX_TOUCHES_COUNT 10
@@ -109,7 +111,7 @@ + (Class)layerClass
109111
#elif !AX_ENABLE_MTL && AX_ENABLE_GL
110112
return [CAEAGLLayer class];
111113
#else
112-
return ax::rhi::DriverRuntime::isMetal() ? [CAMetalLayer class] : [CAEAGLLayer class];
114+
return DriverRuntime::isMetal() ? [CAMetalLayer class] : [CAEAGLLayer class];
113115
#endif
114116
}
115117

@@ -182,7 +184,7 @@ - (id)initWithFrame:(CGRect)frame
182184
multiSampling:(BOOL)sampling
183185
numberOfSamples:(unsigned int)nSamples
184186
{
185-
auto driverType = ax::rhi::DriverRuntime::init();
187+
DriverRuntime::makeCurrentDriver();
186188

187189
if ((self = [super initWithFrame:frame]))
188190
{
@@ -195,7 +197,7 @@ - (id)initWithFrame:(CGRect)frame
195197
self.contentScaleFactor = [[UIScreen mainScreen] scale];
196198
}
197199

198-
if (ax::rhi::DriverRuntime::isMetal())
200+
if (DriverRuntime::isMetal())
199201
{
200202
AX_UNUSED_PARAM(format);
201203
AX_UNUSED_PARAM(depth);
@@ -213,9 +215,7 @@ - (id)initWithFrame:(CGRect)frame
213215
[self release];
214216
return nil;
215217
}
216-
217-
if (driverType == ax::rhi::DriverType::Unknown)
218-
ax::rhi::DriverRuntime::init(ax::rhi::DriverType::OpenGL);
218+
DriverRuntime::activeCurrentDriver();
219219
}
220220
}
221221

@@ -227,7 +227,7 @@ - (id)initWithCoder:(NSCoder*)aDecoder
227227
if ((self = [super initWithCoder:aDecoder]))
228228
{
229229
self.textInputView = [[TextInputView alloc] initWithCoder:aDecoder];
230-
if (ax::rhi::DriverRuntime::isMetal())
230+
if (DriverRuntime::isMetal())
231231
{
232232
backingSize_ = [self bounds].size;
233233
}
@@ -297,7 +297,7 @@ - (void)dealloc
297297
{
298298
[[NSNotificationCenter defaultCenter] removeObserver:self]; // remove keyboard notification
299299
#if AX_ENABLE_GL
300-
if (ax::rhi::DriverRuntime::isOpenGL())
300+
if (DriverRuntime::isOpenGL())
301301
[renderer_ release];
302302
#endif
303303
[self.textInputView release];
@@ -312,7 +312,7 @@ - (void)layoutSubviews
312312

313313
savedBounds_ = [self bounds];
314314
self.textInputView.bounds = savedBounds_;
315-
if (ax::rhi::DriverRuntime::isMetal())
315+
if (DriverRuntime::isMetal())
316316
{
317317
backingSize_ = savedBounds_.size;
318318
backingSize_.width *= self.contentScaleFactor;
@@ -342,7 +342,7 @@ - (void)swapBuffers
342342
// -> context_ MUST be the OpenGL context
343343
// -> renderbuffer_ must be the RENDER BUFFER
344344

345-
if (ax::rhi::DriverRuntime::isMetal())
345+
if (DriverRuntime::isMetal())
346346
return;
347347

348348
# ifdef __IPHONE_4_0

axmol/platform/winrt/xaml/SwapChainPage.cpp

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,16 @@ SwapChainPage::SwapChainPage()
7272
appDelegate.reset(new AppDelegate());
7373
ax::Application::getInstance()->initContextAttrs();
7474

75-
#if AX_ENABLE_D3D11 || AX_ENABLE_D3D12
7675
// Try to initialize a high-performance graphics driver first.
7776
// If any of the high-performance APIs (D3D11/D3D12/Vulkan/Metal) are enabled,
7877
// the runtime will attempt initialization in the default priority order.
7978
// If all attempts fail, OpenGL will then be explicitly selected as the fallback.
80-
auto driverType = rhi::DriverRuntime::init();
81-
m_requiresGL = driverType == rhi::DriverType::Unknown || driverType == rhi::DriverType::OpenGL;
82-
#else
83-
m_requiresGL = true;
79+
rhi::DriverRuntime::makeCurrentDriver();
80+
m_fallbackGL = rhi::DriverRuntime::isOpenGL();
8481
#endif
8582

8683
#if AX_ENABLE_GL
87-
if (m_requiresGL)
84+
if (m_fallbackGL)
8885
m_eglSurfaceProvider = new EGLSurfaceProvider();
8986
#endif
9087
InitializeComponent();
@@ -181,7 +178,7 @@ void SwapChainPage::CreateRenderSurface()
181178
{
182179
UpdatePanelSize();
183180
#if AX_ENABLE_GL
184-
if (!m_requiresGL)
181+
if (!m_fallbackGL)
185182
return;
186183
if (m_eglSurfaceProvider && m_eglSurface == EGL_NO_SURFACE)
187184
{
@@ -215,7 +212,7 @@ void SwapChainPage::UpdatePanelSize()
215212
void SwapChainPage::DestroyRenderSurface()
216213
{
217214
#if AX_ENABLE_GL
218-
if (!m_requiresGL)
215+
if (!m_fallbackGL)
219216
return;
220217
if (m_eglSurfaceProvider)
221218
{
@@ -231,7 +228,7 @@ void SwapChainPage::DestroyRenderSurface()
231228
void SwapChainPage::RecoverFromLostDevice()
232229
{
233230
#if AX_ENABLE_GL
234-
if (m_requiresGL)
231+
if (m_fallbackGL)
235232
{
236233
critical_section::scoped_lock lock(m_eglSurfaceCriticalSection);
237234
DestroyRenderSurface();
@@ -248,7 +245,7 @@ void SwapChainPage::RecoverFromLostDevice()
248245
void SwapChainPage::TerminateApp()
249246
{
250247
#if AX_ENABLE_GL
251-
if (m_requiresGL)
248+
if (m_fallbackGL)
252249
{
253250
critical_section::scoped_lock lock(m_eglSurfaceCriticalSection);
254251

@@ -291,11 +288,10 @@ void SwapChainPage::StartRenderLoop()
291288
}
292289

293290
#if AX_ENABLE_GL
294-
if (m_requiresGL)
291+
if (m_fallbackGL)
295292
{
296293
m_eglSurfaceProvider->MakeCurrent(m_eglSurface);
297-
if (rhi::DriverRuntime::isUnknown())
298-
rhi::DriverRuntime::init(rhi::DriverType::OpenGL);
294+
rhi::DriverRuntime::activeCurrentDriver();
299295
}
300296
#endif
301297

axmol/platform/winrt/xaml/SwapChainPage.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ struct SwapChainPage : SwapChainPageT<SwapChainPage>
108108
bool m_deviceLost;
109109
bool m_visible;
110110
bool m_cursorVisible;
111-
bool m_requiresGL{false};
111+
bool m_fallbackGL{false};
112112

113113
Windows::Graphics::Display::DisplayOrientations m_orientation;
114114

0 commit comments

Comments
 (0)