Context shenanigans

This commit is contained in:
offtkp 2024-07-31 18:40:41 +03:00
parent d1f3c3f003
commit fd30fe77df
3 changed files with 45 additions and 19 deletions

View file

@ -4,9 +4,8 @@
#include "glad/gl.h" #include "glad/gl.h"
#include "opengl.hpp" #include "opengl.hpp"
namespace Frontend::AsyncCompiler { namespace AsyncCompiler {
void* createContext(void* userdata); void* createContext(void* userdata);
void makeCurrent(void* userdata, void* context);
void destroyContext(void* userdata, void* context); void destroyContext(void* userdata, void* context);
} }
@ -40,9 +39,12 @@ bool AsyncCompilerState::PopCompiledProgram(CompiledProgram*& program)
} }
void AsyncCompilerState::Start() { void AsyncCompilerState::Start() {
void* context = Frontend::AsyncCompiler::createContext(contextCreationUserdata); shaderCompilationThread = std::thread([this]() {
shaderCompilationThread = std::thread([this, context]() { void* context = AsyncCompiler::createContext(contextCreationUserdata);
Frontend::AsyncCompiler::makeCurrent(contextCreationUserdata, context); if (!context) {
Helpers::panic("Failed to create async compiler context");
}
printf("Async compiler started, version: %s\n", glGetString(GL_VERSION)); printf("Async compiler started, version: %s\n", glGetString(GL_VERSION));
std::string defaultShadergenVSSource = fragShaderGen.getDefaultVertexShader(); std::string defaultShadergenVSSource = fragShaderGen.getDefaultVertexShader();
defaultShadergenVs.create({defaultShadergenVSSource.c_str(), defaultShadergenVSSource.size()}, OpenGL::Vertex); defaultShadergenVs.create({defaultShadergenVSSource.c_str(), defaultShadergenVSSource.size()}, OpenGL::Vertex);
@ -85,7 +87,7 @@ void AsyncCompilerState::Start() {
std::this_thread::yield(); std::this_thread::yield();
} }
Frontend::AsyncCompiler::destroyContext(contextCreationUserdata, context); AsyncCompiler::destroyContext(contextCreationUserdata, context);
}); });
} }

View file

@ -18,6 +18,10 @@ JavaVM* jvm = nullptr;
jclass alberClass; jclass alberClass;
jmethodID alberClassOpenDocument; jmethodID alberClassOpenDocument;
EGLSurface eglSurface = EGL_NO_SURFACE;
EGLDisplay eglDisplay = EGL_NO_DISPLAY;
int eglVersion = 0;
#define AlberFunction(type, name) JNIEXPORT type JNICALL Java_com_panda3ds_pandroid_AlberDriver_##name #define AlberFunction(type, name) JNIEXPORT type JNICALL Java_com_panda3ds_pandroid_AlberDriver_##name
void throwException(JNIEnv* env, const char* message) { void throwException(JNIEnv* env, const char* message) {
@ -70,6 +74,10 @@ AlberFunction(void, Initialize)(JNIEnv* env, jobject obj) {
return throwException(env, "Failed to load OpenGL ES 2.0"); return throwException(env, "Failed to load OpenGL ES 2.0");
} }
eglDisplay = eglGetCurrentDisplay();
eglSurface = eglGetCurrentSurface(EGL_DRAW);
eglQueryContext(eglDisplay, eglGetCurrentContext(), EGL_CONTEXT_CLIENT_VERSION, &eglVersion);
__android_log_print(ANDROID_LOG_INFO, "AlberDriver", "OpenGL ES %d.%d", GLVersion.major, GLVersion.minor); __android_log_print(ANDROID_LOG_INFO, "AlberDriver", "OpenGL ES %d.%d", GLVersion.major, GLVersion.minor);
emulator->initGraphicsContext(nullptr); emulator->initGraphicsContext(nullptr);
} }
@ -139,4 +147,29 @@ int AndroidUtils::openDocument(const char* path, const char* perms) {
env->DeleteLocalRef(jmode); env->DeleteLocalRef(jmode);
return (int)result; return (int)result;
}
namespace AsyncCompiler {
void createContext(void* userdata) {
EGLint attribList[] = {
EGL_CONTEXT_CLIENT_VERSION, eglVersion,
EGL_NONE
};
EGLContext threadContext = eglCreateContext(eglDisplay, eglSurface, EGL_NO_CONTEXT, attribList);
if (threadContext == EGL_NO_CONTEXT) {
throwException(jniEnv(), "Failed to create EGL context");
}
if (!eglMakeCurrent(eglDisplay, eglSurface, eglSurface, threadContext)) {
throwException(jniEnv(), "Failed to make EGL context current");
}
__android_log_print(ANDROID_LOG_INFO, "AlberDriver", "Async compiler started, version: %s", glGetString(GL_VERSION));
}
void destroyContext(void* userdata, void* context) {
eglMakeCurrent(eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
eglDestroyContext(eglDisplay, (EGLContext)context);
}
} }

View file

@ -344,12 +344,12 @@ void FrontendSDL::run() {
} }
} }
namespace Frontend::AsyncCompiler { namespace AsyncCompiler {
void* createContext(void* userdata) { void* createContext(void* userdata) {
SDL_Window* window = static_cast<SDL_Window*>(userdata); SDL_Window* window = static_cast<SDL_Window*>(userdata);
SDL_GLContext previousContext = SDL_GL_GetCurrentContext();
SDL_GLContext context = SDL_GL_CreateContext(window); // this sets it as current :( // SDL_GL_CreateContext also makes it the current context
SDL_GL_MakeCurrent(window, previousContext); SDL_GLContext context = SDL_GL_CreateContext(window);
if (context == nullptr) { if (context == nullptr) {
Helpers::panic("OpenGL context creation failed: %s", SDL_GetError()); Helpers::panic("OpenGL context creation failed: %s", SDL_GetError());
@ -358,15 +358,6 @@ namespace Frontend::AsyncCompiler {
return context; return context;
} }
void makeCurrent(void* userdata, void* context) {
SDL_Window* window = static_cast<SDL_Window*>(userdata);
int result = SDL_GL_MakeCurrent(window, context);
if (result < 0) {
Helpers::panic("OpenGL context make current failed: %s", SDL_GetError());
}
}
void destroyContext(void* userdata, void* context) { void destroyContext(void* userdata, void* context) {
SDL_GL_DeleteContext(static_cast<SDL_GLContext>(context)); SDL_GL_DeleteContext(static_cast<SDL_GLContext>(context));
} }