[Refactor] (...): Optimize code file structure.

This commit is contained in:
BZLZHH
2025-05-01 09:22:50 +08:00
parent 8ab90e8e60
commit 04976daa9f
35 changed files with 65 additions and 66 deletions
@@ -0,0 +1,93 @@
//
// Created by BZLZHH on 2025/3/15.
//
#include "BackendInfo.h"
namespace MG_GL::Getter {
const std::string GetBackendName() {
#if BACKEND_TYPE == BACKEND_VULKAN
return "Vulkan";
#elif BACKEND_TYPE == BACKEND_METAL
return "Metal";
#elif BACKEND_TYPE == BACKEND_GLES
return "OpenGL ES";
#else
return "<Unknown Backend>";
#endif
}
const std::string GetMGName() {
#if BACKEND_TYPE == BACKEND_VULKAN
return "MobileGluvk";
#elif BACKEND_TYPE == BACKEND_METAL
return "MobileGlumtl";
#elif BACKEND_TYPE == BACKEND_GLES
return "MobileGlues";
#else
return "<Unknown MobileGL Version>";
#endif
}
void LogMGInfo() {
std::string MGName = MG_GL::Getter::GetMGName() + " (MobileGL Core)";
std::string MGVersion = std::to_string(MG_Global::MG::VersionMajor) + "."
+ std::to_string(MG_Global::MG::VersionMinor) + "."
+ std::to_string(MG_Global::MG::VersionRevision);
if (MG_Global::MG::VersionPatch != 0)
MGVersion += "." + std::to_string(MG_Global::MG::VersionPatch);
MGVersion += MG_Global::MG::VersionSuffix;
std::string GLVersion = std::to_string(MG_Global::GL::GLVersionMajor) + "."
+ std::to_string(MG_Global::GL::GLVersionMinor) + "."
+ std::to_string(MG_Global::GL::GLVersionRevision);
std::string backendName = MG_GL::Getter::GetBackendName();
std::string backendVersion;
#if BACKEND_TYPE == BACKEND_VULKAN
VkApplicationInfo appInfo = {};
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
appInfo.pApplicationName = "Vulkan Version Query";
appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
appInfo.pEngineName = "No Engine";
appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
appInfo.apiVersion = VK_API_VERSION_1_0;
VkInstanceCreateInfo createInfo = {};
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
createInfo.pApplicationInfo = &appInfo;
VkInstance instance;
if (vkCreateInstance(&createInfo, nullptr, &instance) != VK_SUCCESS) {
throw std::runtime_error("Failed to create Vulkan instance!");
}
uint32_t deviceCount = 0;
vkEnumeratePhysicalDevices(instance, &deviceCount, nullptr);
if (deviceCount == 0) {
throw std::runtime_error("No Vulkan devices found!");
}
VkPhysicalDevice physicalDevice;
vkEnumeratePhysicalDevices(instance, &deviceCount, &physicalDevice);
VkPhysicalDeviceProperties deviceProperties;
vkGetPhysicalDeviceProperties(physicalDevice, &deviceProperties);
uint32_t major = VK_VERSION_MAJOR(deviceProperties.apiVersion);
uint32_t minor = VK_VERSION_MINOR(deviceProperties.apiVersion);
uint32_t patch = VK_VERSION_PATCH(deviceProperties.apiVersion);
vkDestroyInstance(instance, nullptr);
backendVersion = "Vulkan " + std::to_string(major) + "." +
std::to_string(minor)+"." + std::to_string(patch);
#elif BACKEND_TYPE == BACKEND_GLES
backendVersion = std::string((const char*)::GLES::glGetString(GL_VERSION));
#else
backendVersion = "<Unknown Backend>";
#endif
MG_Util::Debug::LogI("MobileGL Renderer Info:");
MG_Util::Debug::LogI("----%s:", MGName.c_str());
MG_Util::Debug::LogI("--------MobileGL Version: %s", MGVersion.c_str());
MG_Util::Debug::LogI("--------Target OpenGL Implementation Version: %s", GLVersion.c_str());
MG_Util::Debug::LogI("--------Backend: %s", backendName.c_str());
MG_Util::Debug::LogI("--------Backend Version: %s", backendVersion.c_str());
}
}
@@ -0,0 +1,16 @@
//
// Created by BZLZHH on 2025/3/15.
//
#ifndef MOBILEGL_BACKENDINFO_H
#define MOBILEGL_BACKENDINFO_H
#include "../../../../Includes.h"
namespace MG_GL::Getter {
const std::string GetBackendName();
const std::string GetMGName();
void LogMGInfo();
}
#endif //MOBILEGL_BACKENDINFO_H
@@ -0,0 +1,157 @@
//
// Created by BZLZHH on 2025/3/15.
//
#include "GL_Getter.h"
namespace MG_GL::GL {
static std::string rendererString;
static bool loggedMGInfo;
const GLubyte* GetString(GLenum name) {
if (!loggedMGInfo) {
loggedMGInfo = true;
MG_GL::Getter::LogMGInfo();
}
MG_Util::Debug::LogD("glGetString, name: %s", MG_Util::Debug::GLEnumToString(name));
switch (name) {
case GL_VENDOR:
return (const GLubyte *)"MobileGL-Dev";
case GL_VERSION: {
static std::string versionStr;
if (versionStr.empty()) {
versionStr == std::to_string(MG_Global::GL::GLVersionMajor) + "."
+ std::to_string(MG_Global::GL::GLVersionMinor) + "."
+ std::to_string(MG_Global::GL::GLVersionRevision)
+ " "+ MG_GL::Getter::GetMGName() +
", MobileGL Core, " + MG_GL::Getter::GetBackendName() + " Backend";
versionStr += std::to_string(MG_Global::MG::VersionMajor) + "."
+ std::to_string(MG_Global::MG::VersionMinor) + "."
+ std::to_string(MG_Global::MG::VersionRevision);
if (MG_Global::MG::VersionPatch != 0)
versionStr += "." + std::to_string(MG_Global::MG::VersionPatch);
versionStr += MG_Global::MG::VersionSuffix;
}
return (const GLubyte *)versionStr.c_str();
}
case GL_RENDERER:
{
/*
if (rendererString == std::string("")) {
const char* gpuName = getGpuName();
const char* glesName = getGLESName();
rendererString = std::string(gpuName) + " | " + std::string(glesName);
}
return (const GLubyte *)rendererString.c_str();
*/
return (const GLubyte *)(MG_GL::Getter::GetMGName() + "(MobileGL Core) Renderer").c_str();
}
case GL_SHADING_LANGUAGE_VERSION:
return (const GLubyte *) "4.50 MobileGL with glslang";
case GL_EXTENSIONS:
return (const GLubyte *) "OpenGL11 "
"OpenGL12 "
"OpenGL13 "
"OpenGL14 "
"OpenGL15 "
"OpenGL20 "
"OpenGL21 "
"OpenGL30 ";
default:
return (const GLubyte *) "Unknown enum";
}
}
const GLubyte* GetStringi(GLenum name, GLuint index) {
MG_Util::Debug::LogD("glGetStringi, name: %s, index: %d", MG_Util::Debug::GLEnumToString(name), index);
if (name != GL_EXTENSIONS)
return (const GLubyte*)"";
typedef struct {
GLenum name;
const char** parts;
GLuint count;
} StringCache;
static StringCache caches[] = {
{GL_EXTENSIONS, nullptr, 0},
{GL_VENDOR, nullptr, 0},
{GL_VERSION, nullptr, 0},
{GL_SHADING_LANGUAGE_VERSION, nullptr, 0}
};
static int initialized = 0;
if (!initialized) {
for (auto & cache : caches) {
GLenum target = cache.name;
const GLubyte* str = nullptr;
const char* delimiter = " ";
str = GetString(GL_EXTENSIONS);
char* copy = strdup((const char*)str);
char* token = strtok(copy, delimiter);
while (token) {
cache.parts = (const char**)realloc(cache.parts, (cache.count + 1) * sizeof(char*));
cache.parts[cache.count++] = strdup(token);
token = strtok(nullptr, delimiter);
}
free(copy);
}
initialized = 1;
}
for (auto & cache : caches) {
if (cache.name == name) {
if (index >= cache.count) {
return nullptr;
}
return (const GLubyte*)cache.parts[index];
}
}
return nullptr;
}
GLenum GetError() {
GLenum error = MG_State::GetError();
MG_Util::Debug::LogD("glGetError -> %s", MG_Util::Debug::GLEnumToString(error));
return error;
}
void GetIntegerv(GLenum pname, GLint *params) {
MG_Util::Debug::LogD("glGetIntegerv, pname: %s", MG_Util::Debug::GLEnumToString(pname));
switch (pname) {
case GL_CONTEXT_PROFILE_MASK:
(*params) = GL_CONTEXT_CORE_PROFILE_BIT;
break;
case GL_NUM_EXTENSIONS:
static GLint num_extensions = -1;
if (num_extensions == -1) {
const GLubyte* ext_str = glGetString(GL_EXTENSIONS);
if (ext_str) {
char* copy = strdup((const char*)ext_str);
char* token = strtok(copy, " ");
num_extensions = 0;
while (token) {
num_extensions++;
token = strtok(nullptr, " ");
}
free(copy);
} else {
num_extensions = 0;
}
}
(*params) = num_extensions;
break;
case GL_MAJOR_VERSION:
(*params) = MG_Global::GL::GLVersionMajor;
break;
case GL_MINOR_VERSION:
(*params) = MG_Global::GL::GLVersionMinor;
break;
case GL_MAX_TEXTURE_SIZE:
// TODO: Get real GL_MAX_TEXTURE_SIZE.
(*params) = 32768;
break;
default:
break;
}
}
}
@@ -0,0 +1,17 @@
//
// Created by BZLZHH on 2025/3/15.
//
#ifndef MOBILEGL_GL_GETTER_H
#define MOBILEGL_GL_GETTER_H
#include "../../../../Includes.h"
namespace MG_GL::GL {
const ::GLubyte* GetString(GLenum name);
const ::GLubyte* GetStringi(GLenum name, GLuint index);
::GLenum GetError();
void GetIntegerv(GLenum pname, GLint *params);
}
#endif //MOBILEGL_GL_GETTER_H