[Fix] (...): Avoid some weird crashes.

This commit is contained in:
BZLZHH
2026-01-01 00:41:23 +08:00
parent 7c2380ccce
commit ee4dc70336
10 changed files with 173 additions and 154 deletions
+11 -1
View File
@@ -5,10 +5,13 @@
// SPDX-License-Identifier: LGPL-2.1-only
// End of Source File Header
#include "Includes.h"
#include "Init.h"
#include "Config.h"
#include <MG_Impl/Init.h>
#include <MG_Backend/Backends.h>
#include <MG_State/GLState/Core.h>
#include <MG_Impl/GLImpl/Texture/ProxyTexture.h>
#include <MG_Impl/GLImpl/Framebuffer/GL_Framebuffer.h>
namespace MobileGL {
void MG_Initialize() {
@@ -16,6 +19,7 @@ namespace MobileGL {
MGLOG_I("Initializing MobileGL...");
MG_State::Init();
MGLOG_D("MobileGL State initialized");
// return;
MG_Backend::Init();
MGLOG_D("MobileGL Backend initialized");
MG_Impl::Init();
@@ -28,7 +32,13 @@ namespace MobileGL {
void MG_Destroy() {
MGLOG_I("MobileGL closing...");
glslang::FinalizeProcess();
delete MG_State::pGLContext;
MG_Config::RendererInfoPtr.reset();
delete MG_Impl::GLImpl::TextureImpl::pProxyTextureManager;
delete MG_Impl::GLImpl::FramebufferImpl::pDefaultFramebufferInfo;
MG_Util::Debug::Close();
// TODO: add and use Destroy functions for other subsystems
}
#if defined(__linux__) || defined(__APPLE__)
+14
View File
@@ -0,0 +1,14 @@
// MobileGL - MobileGL/Init.cpp
// Copyright (c) 2025 MobileGL-Dev
// Licensed under the GNU Lesser General Public License v2.1:
// http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
// SPDX-License-Identifier: LGPL-2.1-only
// End of Source File Header
#pragma once
#include "Includes.h"
namespace MobileGL {
void MG_Initialize();
void MG_Destroy();
} // namespace MobileGL
+17 -21
View File
@@ -9,6 +9,7 @@
#include <vector>
#include <benchmark/benchmark.h>
#include "Init.h"
#include "MG_Impl/GLImpl/Buffer/GL_Buffer.h"
#include "MG_State/GLState/Core.h"
@@ -19,8 +20,6 @@ constexpr GLuint BUFFER_COUNT = 32;
constexpr GLsizeiptr BUFFER_SIZE = 1024 * 1024;
static void BM_GenerateAndDeleteBuffers(benchmark::State& state) {
MG_State::pGLContext = new MG_State::GLState::GLContext();
GLuint n = static_cast<GLuint>(state.range(0));
int repeat = static_cast<int>(state.range(1));
std::vector<GLuint> buffers(n);
@@ -33,7 +32,6 @@ static void BM_GenerateAndDeleteBuffers(benchmark::State& state) {
}
state.SetItemsProcessed(state.iterations() * repeat * n);
delete MG_State::pGLContext;
}
BENCHMARK(BM_GenerateAndDeleteBuffers)
@@ -45,8 +43,6 @@ BENCHMARK(BM_GenerateAndDeleteBuffers)
->UseRealTime();
static void BM_CreateBufferObjectsAndBindBuffer(benchmark::State& state) {
MG_State::pGLContext = new MG_State::GLState::GLContext();
GLuint buffers[BUFFER_COUNT];
GenBuffers(BUFFER_COUNT, buffers);
@@ -60,12 +56,11 @@ static void BM_CreateBufferObjectsAndBindBuffer(benchmark::State& state) {
state.SetItemsProcessed(state.iterations() * BUFFER_COUNT);
DeleteBuffers(BUFFER_COUNT, buffers);
delete MG_State::pGLContext;
}
BENCHMARK(BM_CreateBufferObjectsAndBindBuffer)->Unit(benchmark::kMillisecond)->UseRealTime();
static void BM_DeleteBufferObjects(benchmark::State& state) {
MG_State::pGLContext = new MG_State::GLState::GLContext();
MG_Initialize();
std::vector<GLuint> buffers(BUFFER_COUNT);
for (auto _ : state) {
@@ -81,13 +76,10 @@ static void BM_DeleteBufferObjects(benchmark::State& state) {
}
state.SetItemsProcessed(state.iterations() * BUFFER_COUNT);
delete MG_State::pGLContext;
}
BENCHMARK(BM_DeleteBufferObjects)->Unit(benchmark::kMillisecond)->UseRealTime();
static void BM_UpdateData(benchmark::State& state) {
MG_State::pGLContext = new MG_State::GLState::GLContext();
GLuint buffers[BUFFER_COUNT];
GenBuffers(BUFFER_COUNT, buffers);
@@ -106,13 +98,10 @@ static void BM_UpdateData(benchmark::State& state) {
state.SetItemsProcessed(state.iterations() * BUFFER_COUNT);
DeleteBuffers(BUFFER_COUNT, buffers);
delete MG_State::pGLContext;
}
BENCHMARK(BM_UpdateData)->Unit(benchmark::kMillisecond)->UseRealTime();
static void BM_MapBuffer(benchmark::State& state) {
MG_State::pGLContext = new MG_State::GLState::GLContext();
GLuint buffers[BUFFER_COUNT];
GenBuffers(BUFFER_COUNT, buffers);
for (GLuint i = 0; i < BUFFER_COUNT; i++)
@@ -136,13 +125,10 @@ static void BM_MapBuffer(benchmark::State& state) {
state.SetItemsProcessed(state.iterations() * BUFFER_COUNT);
DeleteBuffers(BUFFER_COUNT, buffers);
delete MG_State::pGLContext;
}
BENCHMARK(BM_MapBuffer)->Unit(benchmark::kMillisecond)->UseRealTime();
static void BM_CopyBufferSubData(benchmark::State& state) {
MG_State::pGLContext = new MG_State::GLState::GLContext();
GLuint buffers[BUFFER_COUNT];
GenBuffers(BUFFER_COUNT, buffers);
@@ -165,13 +151,10 @@ static void BM_CopyBufferSubData(benchmark::State& state) {
state.SetItemsProcessed(state.iterations() * (BUFFER_COUNT / 2));
DeleteBuffers(BUFFER_COUNT, buffers);
delete MG_State::pGLContext;
}
BENCHMARK(BM_CopyBufferSubData)->Unit(benchmark::kMillisecond)->UseRealTime();
static void BM_UpdateDataPartially(benchmark::State& state) {
MG_State::pGLContext = new MG_State::GLState::GLContext();
std::vector<GLuint> buffers(BUFFER_COUNT);
std::vector<char> data(BUFFER_SIZE / 10, 1);
@@ -191,8 +174,21 @@ static void BM_UpdateDataPartially(benchmark::State& state) {
state.SetItemsProcessed(state.iterations() * BUFFER_COUNT);
DeleteBuffers(BUFFER_COUNT, buffers.data());
delete MG_State::pGLContext;
}
BENCHMARK(BM_UpdateDataPartially)->Unit(benchmark::kMillisecond)->UseRealTime();
BENCHMARK_MAIN();
int main(int argc, char** argv) {
MG_Initialize();
benchmark ::MaybeReenterWithoutASLR(argc, argv);
char arg0_default[] = "benchmark";
char* args_default = reinterpret_cast<char*>(arg0_default);
if (!argv) {
argc = 1;
argv = &args_default;
}
::benchmark ::Initialize(&argc, argv);
if (::benchmark ::ReportUnrecognizedArguments(argc, argv)) return 1;
::benchmark ::RunSpecifiedBenchmarks();
::benchmark ::Shutdown();
return 0;
}
+16 -30
View File
@@ -8,6 +8,7 @@
#include <cstring>
#include <benchmark/benchmark.h>
#include "Init.h"
#include "MG_Impl/GLImpl/Program/GL_Program.h"
#include "MG_State/GLState/Core.h"
@@ -89,9 +90,6 @@ void main() {
})";
static void BM_CompileVertexShader(benchmark::State& state) {
// Initialize GL context
MG_State::pGLContext = new MG_State::GLState::GLContext();
SizeT vsLen = strlen(vsSrc);
for (auto _ : state) {
@@ -107,16 +105,10 @@ static void BM_CompileVertexShader(benchmark::State& state) {
// Set the counter for Compiles/Second
state.counters["VS_Compiles/Second"] = state.iterations();
state.counters["VS_Bytes/Second"] = state.iterations() * vsLen;
// Cleanup GL context
delete MG_State::pGLContext;
}
BENCHMARK(BM_CompileVertexShader)->Unit(benchmark::kMillisecond);
static void BM_CompileFragmentShader(benchmark::State& state) {
// Initialize GL context
MG_State::pGLContext = new MG_State::GLState::GLContext();
SizeT fsLen = strlen(fsSrc);
for (auto _ : state) {
@@ -132,15 +124,10 @@ static void BM_CompileFragmentShader(benchmark::State& state) {
// Set the counter for Compiles/Second
state.counters["FS_Compiles/Second"] = state.iterations();
state.counters["FS_Bytes/Second"] = state.iterations() * fsLen;
// Cleanup GL context
delete MG_State::pGLContext;
}
BENCHMARK(BM_CompileFragmentShader)->Unit(benchmark::kMillisecond);
static void BM_CompileBothShaders(benchmark::State& state) {
// Initialize GL context
MG_State::pGLContext = new MG_State::GLState::GLContext();
SizeT vsLen = strlen(vsSrc);
SizeT fsLen = strlen(fsSrc);
@@ -165,16 +152,10 @@ static void BM_CompileBothShaders(benchmark::State& state) {
state.counters["FS_Compiles/Second"] = state.iterations();
state.counters["VS_Bytes/Second"] = state.iterations() * vsLen;
state.counters["FS_Bytes/Second"] = state.iterations() * fsLen;
// Cleanup GL context
delete MG_State::pGLContext;
}
BENCHMARK(BM_CompileBothShaders)->Unit(benchmark::kMillisecond);
static void BM_LinkProgram(benchmark::State& state) {
// Initialize GL context
MG_State::pGLContext = new MG_State::GLState::GLContext();
// Create and compile vertex shader
GLuint vs = CreateShader(GL_VERTEX_SHADER);
ShaderSource(vs, 1, &vsSrc, NULL);
@@ -199,16 +180,10 @@ static void BM_LinkProgram(benchmark::State& state) {
// Set the counter for Compiles/Second
state.counters["Links/Second"] = state.iterations();
// Cleanup GL context
delete MG_State::pGLContext;
}
BENCHMARK(BM_LinkProgram)->Unit(benchmark::kMillisecond);
static void BM_CompileAndLink(benchmark::State& state) {
// Initialize GL context
MG_State::pGLContext = new MG_State::GLState::GLContext();
SizeT vsLen = strlen(vsSrc);
SizeT fsLen = strlen(fsSrc);
@@ -240,11 +215,22 @@ static void BM_CompileAndLink(benchmark::State& state) {
state.counters["VS_Bytes/Second"] = state.iterations() * vsLen;
state.counters["FS_Bytes/Second"] = state.iterations() * fsLen;
state.counters["Links/Second"] = state.iterations();
// Cleanup GL context
delete MG_State::pGLContext;
}
BENCHMARK(BM_CompileAndLink)->Unit(benchmark::kMillisecond);
BENCHMARK_MAIN();
int main(int argc, char** argv) {
MG_Initialize();
benchmark ::MaybeReenterWithoutASLR(argc, argv);
char arg0_default[] = "benchmark";
char* args_default = reinterpret_cast<char*>(arg0_default);
if (!argv) {
argc = 1;
argv = &args_default;
}
::benchmark ::Initialize(&argc, argv);
if (::benchmark ::ReportUnrecognizedArguments(argc, argv)) return 1;
::benchmark ::RunSpecifiedBenchmarks();
::benchmark ::Shutdown();
return 0;
}
+3 -3
View File
@@ -9,9 +9,9 @@
#include "GLImpl/Texture/ProxyTexture.h"
#include "GLImpl/Framebuffer/GL_Framebuffer.h"
#include "MG_State/GLState/TextureState/TextureObject1D.h"
#include "MG_State/GLState/TextureState/TextureObject2D.h"
#include "MG_State/GLState/TextureState/TextureObject3D.h"
#include <MG_State/GLState/TextureState/TextureObject1D.h>
#include <MG_State/GLState/TextureState/TextureObject2D.h>
#include <MG_State/GLState/TextureState/TextureObject3D.h>
namespace MobileGL {
namespace MG_Impl {
+43 -40
View File
@@ -8,6 +8,7 @@
#include <gtest/gtest.h>
#include "Includes.h"
#include "Init.h"
#include <MG_State/GLState/Core.h>
#include <MG_Impl/GLImpl/Buffer/GL_Buffer.h>
@@ -17,17 +18,19 @@ using namespace MobileGL;
class BufferTest : public ::testing::Test {
protected:
MG_State::GLState::GLContext glContext;
void SetUp() override { MobileGL::MG_Initialize(); }
void TearDown() override {}
};
TEST_F(BufferTest, Binding) {
auto bufferNames = glContext.GenBufferNames(3);
auto& arraySlot = glContext.GetBufferBindingSlot(BufferTarget::Vertex);
auto& indexSlot = glContext.GetBufferBindingSlot(BufferTarget::Uniform);
auto bufferNames = MobileGL::MG_State::pGLContext->GenBufferNames(3);
auto& arraySlot = MobileGL::MG_State::pGLContext->GetBufferBindingSlot(BufferTarget::Vertex);
auto& indexSlot = MobileGL::MG_State::pGLContext->GetBufferBindingSlot(BufferTarget::Uniform);
auto obj0 = glContext.CreateBufferObject(bufferNames[0]);
auto obj1 = glContext.CreateBufferObject(bufferNames[1]);
auto obj2 = glContext.CreateBufferObject(bufferNames[2]);
auto obj0 = MobileGL::MG_State::pGLContext->CreateBufferObject(bufferNames[0]);
auto obj1 = MobileGL::MG_State::pGLContext->CreateBufferObject(bufferNames[1]);
auto obj2 = MobileGL::MG_State::pGLContext->CreateBufferObject(bufferNames[2]);
arraySlot.Bind(obj0);
indexSlot.Bind(obj1);
@@ -42,11 +45,11 @@ TEST_F(BufferTest, Binding) {
}
TEST_F(BufferTest, PingPong) {
auto& readSlot = glContext.GetBufferBindingSlot(BufferTarget::CopyRead);
auto& writeSlot = glContext.GetBufferBindingSlot(BufferTarget::CopyWrite);
auto& readSlot = MobileGL::MG_State::pGLContext->GetBufferBindingSlot(BufferTarget::CopyRead);
auto& writeSlot = MobileGL::MG_State::pGLContext->GetBufferBindingSlot(BufferTarget::CopyWrite);
{
auto bufferNames = glContext.GenBufferNames(1);
auto bufObj = glContext.CreateBufferObject(bufferNames[0]);
auto bufferNames = MobileGL::MG_State::pGLContext->GenBufferNames(1);
auto bufObj = MobileGL::MG_State::pGLContext->CreateBufferObject(bufferNames[0]);
writeSlot.Bind(bufObj);
readSlot.Bind(bufObj);
@@ -75,13 +78,13 @@ TEST_F(BufferTest, PingPong) {
TEST_F(BufferTest, GenerateManyNames_NoPrematureCreation) {
const SizeT largeCount = 100000; // generate tons of buffer names
auto names = glContext.GenBufferNames(largeCount);
auto names = MobileGL::MG_State::pGLContext->GenBufferNames(largeCount);
std::vector<SizeT> indices = {0, 600, 5000, 32768, 99999}; // only create a few buffer objects
for (SizeT idx : indices) {
GLuint name = names[idx];
auto bufObj = glContext.CreateBufferObject(name);
auto& slot = glContext.GetBufferBindingSlot(BufferTarget::Uniform);
auto bufObj = MobileGL::MG_State::pGLContext->CreateBufferObject(name);
auto& slot = MobileGL::MG_State::pGLContext->GetBufferBindingSlot(BufferTarget::Uniform);
slot.Bind(bufObj);
Vector<Int> data = {static_cast<Int>(idx + 1), static_cast<Int>(idx + 2)};
@@ -98,9 +101,9 @@ TEST_F(BufferTest, GenerateManyNames_NoPrematureCreation) {
}
TEST_F(BufferTest, AcquireMemory) {
auto& slot = glContext.GetBufferBindingSlot(BufferTarget::Uniform);
auto bufferNames = glContext.GenBufferNames(1);
auto bufObj = glContext.CreateBufferObject(bufferNames[0]);
auto& slot = MobileGL::MG_State::pGLContext->GetBufferBindingSlot(BufferTarget::Uniform);
auto bufferNames = MobileGL::MG_State::pGLContext->GenBufferNames(1);
auto bufObj = MobileGL::MG_State::pGLContext->CreateBufferObject(bufferNames[0]);
slot.Bind(bufObj);
Vector<Int> initData{10, 20, 30, 40, 50};
SizeT byteSize = initData.size() * sizeof(Int);
@@ -125,9 +128,9 @@ TEST_F(BufferTest, AcquireMemory) {
}
TEST_F(BufferTest, AcquireMemoryRangeWithoutExplicit) {
auto& slot = glContext.GetBufferBindingSlot(BufferTarget::Uniform);
auto bufferNames = glContext.GenBufferNames(1);
auto bufObj = glContext.CreateBufferObject(bufferNames[0]);
auto& slot = MobileGL::MG_State::pGLContext->GetBufferBindingSlot(BufferTarget::Uniform);
auto bufferNames = MobileGL::MG_State::pGLContext->GenBufferNames(1);
auto bufObj = MobileGL::MG_State::pGLContext->CreateBufferObject(bufferNames[0]);
slot.Bind(bufObj);
Vector<Int> initData{10, 20, 30, 40, 50};
SizeT byteSize = initData.size() * sizeof(Int);
@@ -152,9 +155,9 @@ TEST_F(BufferTest, AcquireMemoryRangeWithoutExplicit) {
}
TEST_F(BufferTest, AcquireMemoryRangeWithExplicit) {
auto& slot = glContext.GetBufferBindingSlot(BufferTarget::Uniform);
auto bufferNames = glContext.GenBufferNames(1);
auto bufObj = glContext.CreateBufferObject(bufferNames[0]);
auto& slot = MobileGL::MG_State::pGLContext->GetBufferBindingSlot(BufferTarget::Uniform);
auto bufferNames = MobileGL::MG_State::pGLContext->GenBufferNames(1);
auto bufObj = MobileGL::MG_State::pGLContext->CreateBufferObject(bufferNames[0]);
slot.Bind(bufObj);
Vector<Int> initData{10, 20, 30, 40, 50};
@@ -195,11 +198,11 @@ TEST_F(BufferTest, AcquireMemoryRangeWithExplicit) {
}
TEST_F(BufferTest, CopyBufferSubData) {
auto& srcSlot = glContext.GetBufferBindingSlot(BufferTarget::CopyRead);
auto& dstSlot = glContext.GetBufferBindingSlot(BufferTarget::CopyWrite);
auto& srcSlot = MobileGL::MG_State::pGLContext->GetBufferBindingSlot(BufferTarget::CopyRead);
auto& dstSlot = MobileGL::MG_State::pGLContext->GetBufferBindingSlot(BufferTarget::CopyWrite);
auto srcNames = glContext.GenBufferNames(1);
auto srcObj = glContext.CreateBufferObject(srcNames[0]);
auto srcNames = MobileGL::MG_State::pGLContext->GenBufferNames(1);
auto srcObj = MobileGL::MG_State::pGLContext->CreateBufferObject(srcNames[0]);
srcSlot.Bind(srcObj);
Vector<Int> srcData{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
@@ -208,8 +211,8 @@ TEST_F(BufferTest, CopyBufferSubData) {
DataPtr srcPtr{.data = srcData.data(), .size = srcSize};
srcObj->UploadData(srcPtr, 0);
auto dstNames = glContext.GenBufferNames(1);
auto dstObj = glContext.CreateBufferObject(dstNames[0]);
auto dstNames = MobileGL::MG_State::pGLContext->GenBufferNames(1);
auto dstObj = MobileGL::MG_State::pGLContext->CreateBufferObject(dstNames[0]);
dstSlot.Bind(dstObj);
Vector<Int> dstData(15, 0);
@@ -237,9 +240,9 @@ TEST_F(BufferTest, CopyBufferSubData) {
}
TEST_F(BufferTest, WriteWhileMapped) {
auto& slot = glContext.GetBufferBindingSlot(BufferTarget::ShaderStorage);
auto bufferNames = glContext.GenBufferNames(1);
auto bufObj = glContext.CreateBufferObject(bufferNames[0]);
auto& slot = MobileGL::MG_State::pGLContext->GetBufferBindingSlot(BufferTarget::ShaderStorage);
auto bufferNames = MobileGL::MG_State::pGLContext->GenBufferNames(1);
auto bufObj = MobileGL::MG_State::pGLContext->CreateBufferObject(bufferNames[0]);
slot.Bind(bufObj);
Vector<Int> initData(10, 0);
@@ -267,9 +270,9 @@ TEST_F(BufferTest, WriteWhileMapped) {
}
TEST_F(BufferTest, PartialUpdate) {
auto& slot = glContext.GetBufferBindingSlot(BufferTarget::Vertex);
auto bufferNames = glContext.GenBufferNames(1);
auto bufObj = glContext.CreateBufferObject(bufferNames[0]);
auto& slot = MobileGL::MG_State::pGLContext->GetBufferBindingSlot(BufferTarget::Vertex);
auto bufferNames = MobileGL::MG_State::pGLContext->GenBufferNames(1);
auto bufObj = MobileGL::MG_State::pGLContext->CreateBufferObject(bufferNames[0]);
slot.Bind(bufObj);
Vector<Int> initData{100, 200, 300, 400, 500};
@@ -295,14 +298,14 @@ TEST_F(BufferTest, PartialUpdate) {
}
TEST_F(BufferTest, DeleteBufferObject) {
auto bufferNames = glContext.GenBufferNames(1);
auto& slot = glContext.GetBufferBindingSlot(BufferTarget::Vertex);
auto bufObj = glContext.CreateBufferObject(bufferNames[0]);
auto bufferNames = MobileGL::MG_State::pGLContext->GenBufferNames(1);
auto& slot = MobileGL::MG_State::pGLContext->GetBufferBindingSlot(BufferTarget::Vertex);
auto bufObj = MobileGL::MG_State::pGLContext->CreateBufferObject(bufferNames[0]);
slot.Bind(bufObj);
ASSERT_TRUE(slot.GetBoundObject() == bufObj);
glContext.MarkBufferObjectForDeletion(bufferNames[0]);
MobileGL::MG_State::pGLContext->MarkBufferObjectForDeletion(bufferNames[0]);
ASSERT_TRUE(slot.GetBoundObject() == nullptr);
ASSERT_FALSE(glContext.GetBufferObject(bufferNames[0]));
ASSERT_FALSE(MobileGL::MG_State::pGLContext->GetBufferObject(bufferNames[0]));
}
using namespace MobileGL::MG_Impl::GLImpl;
+3 -2
View File
@@ -7,6 +7,7 @@
#include <gtest/gtest.h>
#include "Includes.h"
#include "Init.h"
#include "MG_Impl/GLImpl/Program/GL_Program.h"
#include "MG_State/GLState/Core.h"
#include "MG_Util/ShaderTranspiler/ShaderCompiler.h"
@@ -16,9 +17,9 @@ using namespace MobileGL::MG_Impl::GLImpl;
class ProgramTest : public ::testing::Test {
protected:
void SetUp() override { MG_State::pGLContext = new MG_State::GLState::GLContext(); }
void SetUp() override { MobileGL::MG_Initialize(); }
void TearDown() override { delete MG_State::pGLContext; }
void TearDown() override {}
};
TEST_F(ProgramTest, Sanity) {
@@ -8,6 +8,7 @@
#include <gtest/gtest.h>
#include "Includes.h"
#include "Init.h"
#include <MG_Util/Converters/GLToStr/GLEnumConverter.h>
#include <MG_Util/ShaderTranspiler/ShaderCompiler.h>
#include <MG_Util/ShaderTranspiler/Types.h>
@@ -17,6 +18,9 @@ using namespace MobileGL;
class ProgramUtilTest : public ::testing::Test {
protected:
void SetUp() override { MobileGL::MG_Initialize(); }
void TearDown() override {}
};
TEST_F(ProgramUtilTest, Sanity) {
@@ -6,7 +6,10 @@
// End of Source File Header
#include <gtest/gtest.h>
#include "Includes.h"
#include "Init.h"
#include <MG_Impl/GLImpl/Buffer/GL_Buffer.h>
#include <MG_Impl/GLImpl/Getter/GL_Getter.h>
#include <MG_Impl/GLImpl/VertexArray/GL_VertexArray.h>
@@ -16,12 +19,10 @@ using namespace MobileGL;
class VertexArrayTest : public ::testing::Test {
protected:
MG_State::GLState::GLContext glContext;
SharedPtr<MG_State::GLState::BufferObject> CreateTestVBO() {
auto bufferNames = glContext.GenBufferNames(1);
auto vbo = glContext.CreateBufferObject(bufferNames[0]);
glContext.GetBufferBindingSlot(BufferTarget::Vertex).Bind(vbo);
auto bufferNames = MobileGL::MG_State::pGLContext->GenBufferNames(1);
auto vbo = MobileGL::MG_State::pGLContext->CreateBufferObject(bufferNames[0]);
MobileGL::MG_State::pGLContext->GetBufferBindingSlot(BufferTarget::Vertex).Bind(vbo);
Vector<float> vertexData = {0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f};
SizeT byteSize = vertexData.size() * sizeof(float);
@@ -31,28 +32,31 @@ protected:
return vbo;
}
void SetUp() override { MobileGL::MG_Initialize(); }
void TearDown() override {}
};
TEST_F(VertexArrayTest, GenerateAndBindVAO) {
auto vaoNames = glContext.GenVertexArrayNames(2);
auto vao0 = glContext.CreateVertexArrayObject(vaoNames[0]);
auto vao1 = glContext.CreateVertexArrayObject(vaoNames[1]);
auto vaoNames = MobileGL::MG_State::pGLContext->GenVertexArrayNames(2);
auto vao0 = MobileGL::MG_State::pGLContext->CreateVertexArrayObject(vaoNames[0]);
auto vao1 = MobileGL::MG_State::pGLContext->CreateVertexArrayObject(vaoNames[1]);
glContext.BindVertexArray(vaoNames[0]);
ASSERT_EQ(glContext.GetBoundVertexArray(), vao0);
MobileGL::MG_State::pGLContext->BindVertexArray(vaoNames[0]);
ASSERT_EQ(MobileGL::MG_State::pGLContext->GetBoundVertexArray(), vao0);
glContext.BindVertexArray(vaoNames[1]);
ASSERT_EQ(glContext.GetBoundVertexArray(), vao1);
MobileGL::MG_State::pGLContext->BindVertexArray(vaoNames[1]);
ASSERT_EQ(MobileGL::MG_State::pGLContext->GetBoundVertexArray(), vao1);
// glContext.BindVertexArray(0);
// ASSERT_EQ(glContext.GetBoundVertexArray(), nullptr);
// MobileGL::MG_State::pGLContext->BindVertexArray(0);
// ASSERT_EQ(MobileGL::MG_State::pGLContext->GetBoundVertexArray(), nullptr);
// Do not detect if it supports default VAO
}
TEST_F(VertexArrayTest, VertexAttributeSetup) {
auto vaoNames = glContext.GenVertexArrayNames(1);
auto vao = glContext.CreateVertexArrayObject(vaoNames[0]);
glContext.BindVertexArray(vaoNames[0]);
auto vaoNames = MobileGL::MG_State::pGLContext->GenVertexArrayNames(1);
auto vao = MobileGL::MG_State::pGLContext->CreateVertexArrayObject(vaoNames[0]);
MobileGL::MG_State::pGLContext->BindVertexArray(vaoNames[0]);
auto vbo = CreateTestVBO();
@@ -81,13 +85,13 @@ TEST_F(VertexArrayTest, VertexAttributeSetup) {
}
TEST_F(VertexArrayTest, IndexBufferBinding) {
auto vaoNames = glContext.GenVertexArrayNames(1);
auto vao = glContext.CreateVertexArrayObject(vaoNames[0]);
glContext.BindVertexArray(vaoNames[0]);
auto vaoNames = MobileGL::MG_State::pGLContext->GenVertexArrayNames(1);
auto vao = MobileGL::MG_State::pGLContext->CreateVertexArrayObject(vaoNames[0]);
MobileGL::MG_State::pGLContext->BindVertexArray(vaoNames[0]);
auto bufferNames = glContext.GenBufferNames(1);
auto ebo = glContext.CreateBufferObject(bufferNames[0]);
glContext.GetBufferBindingSlot(BufferTarget::Index).Bind(ebo);
auto bufferNames = MobileGL::MG_State::pGLContext->GenBufferNames(1);
auto ebo = MobileGL::MG_State::pGLContext->CreateBufferObject(bufferNames[0]);
MobileGL::MG_State::pGLContext->GetBufferBindingSlot(BufferTarget::Index).Bind(ebo);
Vector<Uint> indices = {0, 1, 2};
SizeT byteSize = indices.size() * sizeof(Uint);
@@ -95,57 +99,57 @@ TEST_F(VertexArrayTest, IndexBufferBinding) {
DataPtr ptr{.data = indices.data(), .size = byteSize};
ebo->UploadData(ptr, 0);
glContext.GetBufferBindingSlot(BufferTarget::Index).Bind(ebo);
ASSERT_EQ(glContext.GetBufferBindingSlot(BufferTarget::Index).GetBoundObject(), ebo);
MobileGL::MG_State::pGLContext->GetBufferBindingSlot(BufferTarget::Index).Bind(ebo);
ASSERT_EQ(MobileGL::MG_State::pGLContext->GetBufferBindingSlot(BufferTarget::Index).GetBoundObject(), ebo);
auto newEboNames = glContext.GenBufferNames(1);
auto newEbo = glContext.CreateBufferObject(newEboNames[0]);
glContext.GetBufferBindingSlot(BufferTarget::Index).Bind(newEbo);
ASSERT_EQ(glContext.GetBufferBindingSlot(BufferTarget::Index).GetBoundObject(), newEbo);
auto newEboNames = MobileGL::MG_State::pGLContext->GenBufferNames(1);
auto newEbo = MobileGL::MG_State::pGLContext->CreateBufferObject(newEboNames[0]);
MobileGL::MG_State::pGLContext->GetBufferBindingSlot(BufferTarget::Index).Bind(newEbo);
ASSERT_EQ(MobileGL::MG_State::pGLContext->GetBufferBindingSlot(BufferTarget::Index).GetBoundObject(), newEbo);
}
TEST_F(VertexArrayTest, DeleteVAO) {
auto vaoNames = glContext.GenVertexArrayNames(1);
auto vao = glContext.CreateVertexArrayObject(vaoNames[0]);
auto vaoNames = MobileGL::MG_State::pGLContext->GenVertexArrayNames(1);
auto vao = MobileGL::MG_State::pGLContext->CreateVertexArrayObject(vaoNames[0]);
glContext.BindVertexArray(vaoNames[0]);
ASSERT_EQ(glContext.GetBoundVertexArray(), vao);
MobileGL::MG_State::pGLContext->BindVertexArray(vaoNames[0]);
ASSERT_EQ(MobileGL::MG_State::pGLContext->GetBoundVertexArray(), vao);
glContext.MarkVertexArrayForDeletion(vaoNames[0]);
MobileGL::MG_State::pGLContext->MarkVertexArrayForDeletion(vaoNames[0]);
ASSERT_FALSE(glContext.ValidateVertexArrayObject(vaoNames[0]));
ASSERT_EQ(glContext.GetVertexArrayObject(vaoNames[0]), nullptr);
ASSERT_EQ(glContext.GetBoundVertexArray(), nullptr);
ASSERT_FALSE(MobileGL::MG_State::pGLContext->ValidateVertexArrayObject(vaoNames[0]));
ASSERT_EQ(MobileGL::MG_State::pGLContext->GetVertexArrayObject(vaoNames[0]), nullptr);
ASSERT_EQ(MobileGL::MG_State::pGLContext->GetBoundVertexArray(), nullptr);
}
TEST_F(VertexArrayTest, ValidateNamesAndObjects) {
const Uint count = 5;
auto vaoNames = glContext.GenVertexArrayNames(count);
auto vaoNames = MobileGL::MG_State::pGLContext->GenVertexArrayNames(count);
for (Uint i = 0; i < count; i++) {
ASSERT_TRUE(glContext.ValidateVertexArrayName(vaoNames[i]));
ASSERT_FALSE(glContext.ValidateVertexArrayObject(vaoNames[i]));
ASSERT_TRUE(MobileGL::MG_State::pGLContext->ValidateVertexArrayName(vaoNames[i]));
ASSERT_FALSE(MobileGL::MG_State::pGLContext->ValidateVertexArrayObject(vaoNames[i]));
}
for (Uint i = 0; i < count; i += 2) {
glContext.CreateVertexArrayObject(vaoNames[i]);
ASSERT_TRUE(glContext.ValidateVertexArrayObject(vaoNames[i]));
MobileGL::MG_State::pGLContext->CreateVertexArrayObject(vaoNames[i]);
ASSERT_TRUE(MobileGL::MG_State::pGLContext->ValidateVertexArrayObject(vaoNames[i]));
}
for (Uint i = 1; i < count; i += 2) {
glContext.MarkVertexArrayForDeletion(vaoNames[i]);
ASSERT_FALSE(glContext.ValidateVertexArrayName(vaoNames[i]));
MobileGL::MG_State::pGLContext->MarkVertexArrayForDeletion(vaoNames[i]);
ASSERT_FALSE(MobileGL::MG_State::pGLContext->ValidateVertexArrayName(vaoNames[i]));
}
}
TEST_F(VertexArrayTest, MultipleAttributes) {
auto vaoNames = glContext.GenVertexArrayNames(1);
auto vao = glContext.CreateVertexArrayObject(vaoNames[0]);
glContext.BindVertexArray(vaoNames[0]);
auto vaoNames = MobileGL::MG_State::pGLContext->GenVertexArrayNames(1);
auto vao = MobileGL::MG_State::pGLContext->CreateVertexArrayObject(vaoNames[0]);
MobileGL::MG_State::pGLContext->BindVertexArray(vaoNames[0]);
auto vboPos = CreateTestVBO();
auto vboNormalNames = glContext.GenBufferNames(1);
auto vboNormal = glContext.CreateBufferObject(vboNormalNames[0]);
auto vboNormalNames = MobileGL::MG_State::pGLContext->GenBufferNames(1);
auto vboNormal = MobileGL::MG_State::pGLContext->CreateBufferObject(vboNormalNames[0]);
Vector<float> normals(12, 0.5f);
SizeT byteSize = normals.size() * sizeof(float);
@@ -182,22 +186,22 @@ TEST_F(VertexArrayTest, MultipleAttributes) {
}
TEST_F(VertexArrayTest, BoundVAOPreservesState) {
auto vaoNames = glContext.GenVertexArrayNames(2);
auto vao1 = glContext.CreateVertexArrayObject(vaoNames[0]);
auto vao2 = glContext.CreateVertexArrayObject(vaoNames[1]);
auto vaoNames = MobileGL::MG_State::pGLContext->GenVertexArrayNames(2);
auto vao1 = MobileGL::MG_State::pGLContext->CreateVertexArrayObject(vaoNames[0]);
auto vao2 = MobileGL::MG_State::pGLContext->CreateVertexArrayObject(vaoNames[1]);
auto vbo = CreateTestVBO();
glContext.BindVertexArray(vaoNames[0]);
MobileGL::MG_State::pGLContext->BindVertexArray(vaoNames[0]);
vao1->EnableAttribute(0);
vao1->SetAttributeFormat(0, 4, DataType::Float32, false, 0, 0, false);
vao1->BindAttributeBuffer(0, vbo);
glContext.BindVertexArray(vaoNames[1]);
MobileGL::MG_State::pGLContext->BindVertexArray(vaoNames[1]);
vao2->EnableAttribute(1);
vao2->SetAttributeFormat(1, 3, DataType::Float32, true, 0, 0, false);
glContext.BindVertexArray(vaoNames[0]);
MobileGL::MG_State::pGLContext->BindVertexArray(vaoNames[0]);
const auto& attr = vao1->GetAttribute(0);
ASSERT_TRUE(attr.Enabled);
@@ -442,9 +442,10 @@ namespace MobileGL {
void *libGLES = nullptr, *libEGL = nullptr;
static const char* LibPathPrefixes[] = {"", "/opt/vc/lib/", "/usr/local/lib/", "/usr/lib/", nullptr};
static const char* LibPathPrefixes[] = {
"", "/opt/vc/lib/", "/usr/local/lib/", "/usr/lib/", "/usr/lib/x86_64-linux-gnu/", nullptr};
static const char* LibExts[] = {"so", "so.1", "so.2", "dylib", "dll", nullptr};
static const char* GLES3Libs[] = {"libGLESv3_CM", "libGLESv3", nullptr};
static const char* GLES3Libs[] = {"libGLESv3_CM", "libGLESv3", "libGLESv2_CM", "libGLESv2", nullptr};
static const char* EGLLibs[] = {"libEGL", nullptr};
void* OpenLib(const char** names, const char* override) {