From bd20d16d0766cd8312ee9cd016b65b8259ec9dce Mon Sep 17 00:00:00 2001 From: KiritoDv Date: Fri, 23 Jul 2021 19:52:25 -0500 Subject: [PATCH] Migrated from GLES 2 to OpenGL 4.2 on switch --- Makefile | 4 +-- src/moon/imgui/imgui_impl.cpp | 15 ++++++++ src/moon/libs/imgui/imgui_impl_opengl3.cpp | 4 +++ src/moon/libs/imgui/imgui_impl_opengl3.h | 2 +- src/pc/gfx/gfx_opengl.c | 42 ++++++++++++++-------- src/pc/gfx/gfx_sdl2.c | 33 ++++++++++++----- 6 files changed, 74 insertions(+), 26 deletions(-) diff --git a/Makefile b/Makefile index 142c26ad..0effe921 100644 --- a/Makefile +++ b/Makefile @@ -150,7 +150,7 @@ ifeq ($(TARGET_RPI),1) # Define RPi to change SDL2 title & GLES2 hints endif ifeq ($(TARGET_SWITCH),1) - VERSION_CFLAGS += -DUSE_GLES -DTARGET_SWITCH -DLUA_USE_LINUX + VERSION_CFLAGS += -DTARGET_SWITCH -DLUA_USE_LINUX endif ifeq ($(OSX_BUILD),1) # Modify GFX & SDL2 for OSX GL VERSION_CFLAGS += -DOSX_BUILD @@ -549,7 +549,7 @@ else ifeq ($(WINDOW_API),SDL2) ifeq ($(WINDOWS_BUILD),1) BACKEND_LDFLAGS += -lglew32 -lglu32 -lopengl32 else ifneq ($(TARGET_RPI)$(TARGET_SWITCH),00) - BACKEND_LDFLAGS += -lGLESv2 + BACKEND_LDFLAGS += -lglad -lglapi -ldrm_nouveau -lm else ifeq ($(OSX_BUILD),1) BACKEND_LDFLAGS += -framework OpenGL $(shell pkg-config --libs glew) else diff --git a/src/moon/imgui/imgui_impl.cpp b/src/moon/imgui/imgui_impl.cpp index 477bd554..5f621517 100644 --- a/src/moon/imgui/imgui_impl.cpp +++ b/src/moon/imgui/imgui_impl.cpp @@ -22,12 +22,26 @@ # include #endif +#ifdef TARGET_SWITCH +#include "glad/glad.h" +#else #define GL_GLEXT_PROTOTYPES 1 #ifdef USE_GLES # include #else # include #endif +#endif + +#if FOR_WINDOWS +#define PLATFORM "Windows" +#elif defined(OSX_BUILD) +#define PLATFORM "MacOS" +#elif defined(TARGET_SWITCH) +#define PLATFORM "Nintendo Switch" +#else +#define PLATFORM "Linux" +#endif extern "C" { #include "pc/gfx/gfx_pc.h" @@ -90,6 +104,7 @@ namespace MoonInternal { ImGui::PushStyleColor(ImGuiCol_Border, ImVec4(0, 0, 0, 0)); ImGui::Begin("Moon64 Game Stats", NULL, ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize); ImGui::Text("Framerate: %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); + ImGui::Text("Platform: " PLATFORM); ImGui::Text("Branch: " GIT_BRANCH); ImGui::Text("Commit: " GIT_HASH); ImGui::End(); diff --git a/src/moon/libs/imgui/imgui_impl_opengl3.cpp b/src/moon/libs/imgui/imgui_impl_opengl3.cpp index 51a41d15..caed3e32 100644 --- a/src/moon/libs/imgui/imgui_impl_opengl3.cpp +++ b/src/moon/libs/imgui/imgui_impl_opengl3.cpp @@ -101,12 +101,16 @@ # include #endif +#ifdef TARGET_SWITCH +#include "glad/glad.h" +#else #define GL_GLEXT_PROTOTYPES 1 #ifdef USE_GLES # include #else # include #endif +#endif // Desktop GL 3.2+ has glDrawElementsBaseVertex() which GL ES and WebGL don't have. diff --git a/src/moon/libs/imgui/imgui_impl_opengl3.h b/src/moon/libs/imgui/imgui_impl_opengl3.h index 8c0126d8..a35ed8a8 100644 --- a/src/moon/libs/imgui/imgui_impl_opengl3.h +++ b/src/moon/libs/imgui/imgui_impl_opengl3.h @@ -66,7 +66,7 @@ IMGUI_IMPL_API void ImGui_ImplOpenGL3_DestroyDeviceObjects(); // Otherwise try to detect supported Desktop OpenGL loaders.. #elif defined(__has_include) #if __has_include() - #define IMGUI_IMPL_OPENGL_LOADER_GLEW + #define IMGUI_IMPL_OPENGL_LOADER_GLAD2 #elif __has_include() #define IMGUI_IMPL_OPENGL_LOADER_GLAD #elif __has_include() diff --git a/src/pc/gfx/gfx_opengl.c b/src/pc/gfx/gfx_opengl.c index 9ab09ea0..03b39a0b 100644 --- a/src/pc/gfx/gfx_opengl.c +++ b/src/pc/gfx/gfx_opengl.c @@ -21,12 +21,16 @@ #include +#ifdef TARGET_SWITCH +#include "glad/glad.h" +#else #define GL_GLEXT_PROTOTYPES 1 #ifdef USE_GLES # include #else # include #endif +#endif #include "../platform.h" #include "../configfile.h" @@ -399,27 +403,32 @@ static struct ShaderProgram *gfx_opengl_create_and_load_new_shader(uint32_t shad glCompileShader(vertex_shader); glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &success); if (!success) { - GLint max_length = 0; - glGetShaderiv(vertex_shader, GL_INFO_LOG_LENGTH, &max_length); - char error_log[1024]; - fprintf(stderr, "Vertex shader compilation failed\n"); - glGetShaderInfoLog(vertex_shader, max_length, &max_length, &error_log[0]); - fprintf(stderr, "%s\n", &error_log[0]); - sys_fatal("vertex shader compilation failed (see terminal)"); + int length; + glGetShaderiv(vertex_shader, GL_INFO_LOG_LENGTH, &length); + char* message = (char*)malloc(length); + glGetShaderInfoLog(vertex_shader, length, NULL, message); + + printf("[Moon64] Failed to compile vertex shader:\n%s\n", message); + free(message); + + return 0; } GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragment_shader, 1, &sources[1], &lengths[1]); glCompileShader(fragment_shader); glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &success); + if (!success) { - GLint max_length = 0; - glGetShaderiv(fragment_shader, GL_INFO_LOG_LENGTH, &max_length); - char error_log[1024]; - fprintf(stderr, "Fragment shader compilation failed\n"); - glGetShaderInfoLog(fragment_shader, max_length, &max_length, &error_log[0]); - fprintf(stderr, "%s\n", &error_log[0]); - sys_fatal("fragment shader compilation failed (see terminal)"); + int length; + glGetShaderiv(fragment_shader, GL_INFO_LOG_LENGTH, &length); + char* message = (char*)malloc(length); + glGetShaderInfoLog(fragment_shader, length, NULL, message); + + printf("[Moon64] Failed to compile fragment shader:\n%s\n", message); + free(message); + + return 0; } GLuint shader_program = glCreateProgram(); @@ -639,6 +648,7 @@ static void gfx_opengl_init(void) { moon_call_hook(0); } + static void gfx_opengl_on_resize(void) { moon_bind_hook(GFX_ON_REZISE); moon_init_hook(0); @@ -660,11 +670,12 @@ static void gfx_opengl_start_frame(void) { glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_SCISSOR_TEST); +#ifndef USE_GLES if(configWindow.enable_antialias) glEnable(GL_MULTISAMPLE); else glDisable(GL_MULTISAMPLE); - +#endif moon_bind_hook(GFX_POST_START_FRAME); moon_init_hook(0); moon_call_hook(0); @@ -689,6 +700,7 @@ static void gfx_opengl_shutdown(void) { moon_call_hook(0); } + struct GfxRenderingAPI gfx_opengl_api = { gfx_opengl_z_is_from_0_to_1, gfx_opengl_unload_shader, diff --git a/src/pc/gfx/gfx_sdl2.c b/src/pc/gfx/gfx_sdl2.c index a074cc23..da72b4c1 100644 --- a/src/pc/gfx/gfx_sdl2.c +++ b/src/pc/gfx/gfx_sdl2.c @@ -41,6 +41,7 @@ // can't include or even because // the basic libnx types have the same names as some of the types in this extern int appletGetOperationMode(void); +#include "glad/glad.h" #endif // TODO: figure out if this shit even works @@ -213,18 +214,24 @@ static void gfx_sdl_init(const char *window_title) { SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); - // SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); + SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1); - #ifdef USE_GLES +#ifdef TARGET_SWITCH + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); +#endif + +#ifdef USE_GLES SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); // These attributes allow for hardware acceleration on RPis. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); - #endif - +#elif !defined(TARGET_SWITCH) SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, configWindow.antialias_level); - SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1); - #ifdef TARGET_SWITCH +#endif + +#ifdef TARGET_SWITCH configWindow.fullscreen = false; if (appletGetOperationMode() == 1) { configWindow.w = 1920; @@ -235,18 +242,28 @@ static void gfx_sdl_init(const char *window_title) { } int xpos = 0; int ypos = 0; - #else +#else int xpos = (configWindow.x == WAPI_WIN_CENTERPOS) ? SDL_WINDOWPOS_CENTERED : configWindow.x; int ypos = (configWindow.y == WAPI_WIN_CENTERPOS) ? SDL_WINDOWPOS_CENTERED : configWindow.y; - #endif +#endif wnd = SDL_CreateWindow( window_title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, configWindow.w, configWindow.h, SDL_WINDOW_OPENGL | SDL_WINDOW_HIDDEN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI ); + if (wnd == NULL) { + printf("Failed to initialize window\n"); + } + ctx = SDL_GL_CreateContext(wnd); +#ifdef TARGET_SWITCH + if(!gladLoadGLLoader(SDL_GL_GetProcAddress)){ + printf("Failed to initialize glad\n"); + } +#endif + moon_bind_hook(WINDOW_API_INIT); moon_init_hook(2, (struct HookParameter){