diff --git a/CMakeLists.txt b/CMakeLists.txt index e74e5311..a331ca1c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -128,6 +128,7 @@ endif () set(SOURCE_FILES MobileGL/Init.cpp MobileGL/GlobalObjects.cpp + MobileGL/ConfigLoader.cpp MobileGL/MG_Util/Debug/Log.cpp diff --git a/MobileGL/ConfigLoader.cpp b/MobileGL/ConfigLoader.cpp new file mode 100644 index 00000000..7084701e --- /dev/null +++ b/MobileGL/ConfigLoader.cpp @@ -0,0 +1,75 @@ +// MobileGL - MobileGL/ConfigLoader.cpp +// Copyright (c) 2025-2026 MobileGL-Dev +// Licensed under the GNU Lesser General Public License v3.0: +// https://www.gnu.org/licenses/gpl-3.0.txt +// https://www.gnu.org/licenses/lgpl-3.0.txt +// SPDX-License-Identifier: LGPL-3.0-only +// End of Source File Header + +#include "Config.h" + +namespace MobileGL::MG_ConfigLoader { + static UnorderedMap acceptedEnvVariablesMap; + + static Bool IsAcceptedPrefix(const String& key) { + return (key.compare(0, 6, "LIBGL_") == 0 || key.compare(0, 9, "MOBILEGL_") == 0); + } + + inline void InitializeAcceptedEnvVariables() { + acceptedEnvVariablesMap.clear(); + char** envPtr = nullptr; + +#ifdef _WIN32 + envPtr = _environ; +#else // POSIX + envPtr = ::environ; +#endif + + if (envPtr == nullptr) return; + + for (char** env = envPtr; *env != nullptr; ++env) { + String entry(*env); + SizeT pos = entry.find('='); + if (pos != String::npos) { + String key = entry.substr(0, pos); + String value = entry.substr(pos + 1); + + if (IsAcceptedPrefix(key)) { + acceptedEnvVariablesMap[key] = value; + } + } + } + } + + inline void QueryEnvVariable(const String& key, String& outValue, const String& defaultValue) { + auto it = acceptedEnvVariablesMap.find(key); + if (it != acceptedEnvVariablesMap.end()) { + outValue = it->second; + } else { + outValue = defaultValue; + } + } + + inline void InitBackendType() { + String backendTypeStr; + QueryEnvVariable("MOBILEGL_BACKEND_TYPE", backendTypeStr, "DirectGLES"); +#define ENTRY(backendType) \ + if (backendTypeStr == #backendType) { \ + MG_Config::ActiveBackendType = BackendType::backendType; \ + MGLOG_I("Config: Active backend type set to " #backendType); \ + return; \ + } + ENTRY(DirectGLES) + ENTRY(DirectVulkan) + ENTRY(Unknown) + MG_Config::ActiveBackendType = BackendType::Unknown; +#undef ENTRY + } + + void Init() { + MGLOG_D("Loading configuration from environment variables..."); + InitializeAcceptedEnvVariables(); + + InitBackendType(); + } +} // namespace MobileGL::MG_ConfigLoader diff --git a/MobileGL/Defines.h b/MobileGL/Defines.h index 083d6c9a..80c7366e 100644 --- a/MobileGL/Defines.h +++ b/MobileGL/Defines.h @@ -36,7 +36,7 @@ // ====================== MobileGL configurations ======================= // #define MOBILEGL_LOG_ACTIVE_LEVEL MOBILEGL_LOG_LEVEL_INFO -#define MOBILEGL_LOG_ENABLE_CONSOLE 1 +#define MOBILEGL_LOG_ENABLE_CONSOLE 0 #define MOBILEGL_LOG_ENABLE_FILE 1 #define MOBILEGL_LOG_ENABLE_ANDROID 1 #define MOBILEGL_ENABLE_SCOPE_MARKER 0 diff --git a/MobileGL/GlobalObjects.cpp b/MobileGL/GlobalObjects.cpp index 4b3689cf..f5c40733 100644 --- a/MobileGL/GlobalObjects.cpp +++ b/MobileGL/GlobalObjects.cpp @@ -10,7 +10,7 @@ namespace MobileGL { namespace MG_Config { - BackendType ActiveBackendType = BackendType::DirectVulkan; + BackendType ActiveBackendType; } // namespace MG_Config namespace MG_Backend { diff --git a/MobileGL/Init.cpp b/MobileGL/Init.cpp index 19767622..0a5069c7 100644 --- a/MobileGL/Init.cpp +++ b/MobileGL/Init.cpp @@ -8,28 +8,29 @@ #include "Init.h" #include "Config.h" -#include #include #include #include #include namespace MobileGL { - void MG_Initialize() { + void Initialize() { MG_Util::Debug::InitFile(); MGLOG_I("Initializing MobileGL..."); + MG_ConfigLoader::Init(); + MGLOG_I("Config loaded"); MG_State::Init(); - MGLOG_D("MobileGL State initialized"); + MGLOG_D("MG_State initialized"); MG_Backend::Init(); - MGLOG_D("MobileGL Backend initialized"); + MGLOG_D("MG_Backend initialized"); MG_Impl::Init(); - MGLOG_D("MobileGL Implementation initialized"); + MGLOG_D("MG_Impl initialized"); glslang::InitializeProcess(); MGLOG_D("glslang initialized"); MGLOG_I("MobileGL initialized"); } - void MG_Destroy() { + void Destroy() { MGLOG_I("MobileGL closing..."); glslang::FinalizeProcess(); delete MG_State::pGLContext; @@ -42,11 +43,11 @@ namespace MobileGL { #if defined(__linux__) || defined(__APPLE__) __attribute__((constructor)) static void AutoInit() { - MG_Initialize(); + Initialize(); } __attribute__((destructor)) static void AutoDestroy() { - MG_Destroy(); + Destroy(); } #endif @@ -54,11 +55,11 @@ namespace MobileGL { BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: - MG_Initialize(); + Initialize(); break; case DLL_PROCESS_DETACH: - MG_Destroy(); + Destroy(); break; } return TRUE; diff --git a/MobileGL/Init.h b/MobileGL/Init.h index 5011361c..349f6c9b 100644 --- a/MobileGL/Init.h +++ b/MobileGL/Init.h @@ -10,6 +10,22 @@ #include "Includes.h" namespace MobileGL { - void MG_Initialize(); - void MG_Destroy(); -} // namespace MobileGL \ No newline at end of file + void Initialize(); + void Destroy(); + + namespace MG_Util::Debug { + void InitFile(); + } // namespace MG_Util::Debug + + namespace MG_ConfigLoader { + void Init(); + } // namespace MG_ConfigLoader + + namespace MG_Backend { + void Init(); + } // namespace MG_Backend + + namespace MG_Impl { + void Init(); + } // namespace MG_Impl +} // namespace MobileGL diff --git a/MobileGL/MG_Backend/BackendObjects.h b/MobileGL/MG_Backend/BackendObjects.h index edcf63b4..d95f12dc 100644 --- a/MobileGL/MG_Backend/BackendObjects.h +++ b/MobileGL/MG_Backend/BackendObjects.h @@ -15,6 +15,4 @@ namespace MobileGL::MG_Backend { extern UniquePtr pActiveBackendObject; extern GlobalBackendFunctionsTable gBackendFunctionsTable; - - void Init(); } // namespace MobileGL::MG_Backend diff --git a/MobileGL/MG_Impl/Init.cpp b/MobileGL/MG_Impl/Init.cpp index 448edbbc..c7fbfeb4 100644 --- a/MobileGL/MG_Impl/Init.cpp +++ b/MobileGL/MG_Impl/Init.cpp @@ -6,7 +6,8 @@ // SPDX-License-Identifier: LGPL-3.0-only // End of Source File Header -#include "Init.h" +#include + #include "GLImpl/Texture/ProxyTexture.h" #include "GLImpl/Framebuffer/GL_Framebuffer.h" diff --git a/MobileGL/MG_Impl/Init.h b/MobileGL/MG_Impl/Init.h deleted file mode 100644 index b4561146..00000000 --- a/MobileGL/MG_Impl/Init.h +++ /dev/null @@ -1,15 +0,0 @@ -// MobileGL - MobileGL/MG_Impl/Init.h -// Copyright (c) 2025-2026 MobileGL-Dev -// Licensed under the GNU Lesser General Public License v3.0: -// https://www.gnu.org/licenses/gpl-3.0.txt -// https://www.gnu.org/licenses/lgpl-3.0.txt -// SPDX-License-Identifier: LGPL-3.0-only -// End of Source File Header - -#include - -namespace MobileGL { - namespace MG_Impl { - void Init(); - } // namespace MG_Impl -} // namespace MobileGL \ No newline at end of file diff --git a/MobileGL/MG_Util/Debug/Log.h b/MobileGL/MG_Util/Debug/Log.h index 4ef733c0..16b82b5b 100644 --- a/MobileGL/MG_Util/Debug/Log.h +++ b/MobileGL/MG_Util/Debug/Log.h @@ -59,7 +59,6 @@ namespace MobileGL { namespace MG_Util { namespace Debug { void Log(const char* levelTag, android_LogPriority androidLogLevel, const char* fmt, ...); - void InitFile(); void WriteToFile(const char* msg); std::string GetCurrentTime(); constexpr char* GetOSName();