From ea0b13a05c6e1682684b7b602153f3cf8b12f6c7 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Thu, 28 Nov 2019 23:29:03 +1000 Subject: [PATCH] More changes to accomodate Android - imgui/host display --- dep/imgui/CMakeLists.txt | 9 +++ dep/imgui/include/imconfig.h | 2 - dep/imgui/include/imgui_impl_opengl3.h | 28 ------- dep/imgui/src/imgui_impl_opengl3.cpp | 99 +++++-------------------- src/core/bios.cpp | 3 +- src/core/gpu_hw_d3d11.cpp | 4 +- src/core/host_display.h | 8 +- src/duckstation/d3d11_host_display.cpp | 14 +++- src/duckstation/d3d11_host_display.h | 7 +- src/duckstation/opengl_host_display.cpp | 14 +++- src/duckstation/opengl_host_display.h | 7 +- 11 files changed, 71 insertions(+), 124 deletions(-) diff --git a/dep/imgui/CMakeLists.txt b/dep/imgui/CMakeLists.txt index 51caab5ed..a736538a9 100644 --- a/dep/imgui/CMakeLists.txt +++ b/dep/imgui/CMakeLists.txt @@ -32,3 +32,12 @@ if(SDL2_FOUND) target_link_libraries(imgui PRIVATE glad SDL2::Core) endif() +if(ANDROID) + target_sources(imgui PRIVATE + include/imgui_impl_opengl3.h + src/imgui_impl_opengl3.cpp + ) + target_link_libraries(imgui PRIVATE glad) +endif() + + diff --git a/dep/imgui/include/imconfig.h b/dep/imgui/include/imconfig.h index 884415542..45e75ecfc 100644 --- a/dep/imgui/include/imconfig.h +++ b/dep/imgui/include/imconfig.h @@ -13,8 +13,6 @@ #pragma once -#define IMGUI_IMPL_OPENGL_LOADER_GLAD - //---- Define assertion handler. Defaults to calling assert(). //#define IM_ASSERT(_EXPR) MyAssert(_EXPR) //#define IM_ASSERT(_EXPR) ((void)(_EXPR)) // Disable asserts diff --git a/dep/imgui/include/imgui_impl_opengl3.h b/dep/imgui/include/imgui_impl_opengl3.h index 721a277fa..1b20187f3 100644 --- a/dep/imgui/include/imgui_impl_opengl3.h +++ b/dep/imgui/include/imgui_impl_opengl3.h @@ -35,31 +35,3 @@ IMGUI_IMPL_API bool ImGui_ImplOpenGL3_CreateFontsTexture(); IMGUI_IMPL_API void ImGui_ImplOpenGL3_DestroyFontsTexture(); IMGUI_IMPL_API bool ImGui_ImplOpenGL3_CreateDeviceObjects(); IMGUI_IMPL_API void ImGui_ImplOpenGL3_DestroyDeviceObjects(); - -// Specific OpenGL versions -//#define IMGUI_IMPL_OPENGL_ES2 // Auto-detected on Emscripten -//#define IMGUI_IMPL_OPENGL_ES3 // Auto-detected on iOS/Android - -// Desktop OpenGL: attempt to detect default GL loader based on available header files. -// If auto-detection fails or doesn't select the same GL loader file as used by your application, -// you are likely to get a crash in ImGui_ImplOpenGL3_Init(). -// You can explicitly select a loader by using '#define IMGUI_IMPL_OPENGL_LOADER_XXX' in imconfig.h or compiler command-line. -#if !defined(IMGUI_IMPL_OPENGL_LOADER_GL3W) \ - && !defined(IMGUI_IMPL_OPENGL_LOADER_GLEW) \ - && !defined(IMGUI_IMPL_OPENGL_LOADER_GLAD) \ - && !defined(IMGUI_IMPL_OPENGL_LOADER_CUSTOM) - #if defined(__has_include) - #if __has_include() - #define IMGUI_IMPL_OPENGL_LOADER_GLEW - #elif __has_include() - #define IMGUI_IMPL_OPENGL_LOADER_GLAD - #elif __has_include() - #define IMGUI_IMPL_OPENGL_LOADER_GL3W - #else - #error "Cannot detect OpenGL loader!" - #endif - #else - #define IMGUI_IMPL_OPENGL_LOADER_GL3W // Default to GL3W - #endif -#endif - diff --git a/dep/imgui/src/imgui_impl_opengl3.cpp b/dep/imgui/src/imgui_impl_opengl3.cpp index 8c4e828d4..79d019f4d 100644 --- a/dep/imgui/src/imgui_impl_opengl3.cpp +++ b/dep/imgui/src/imgui_impl_opengl3.cpp @@ -82,44 +82,14 @@ // Auto-detect GL version #if !defined(IMGUI_IMPL_OPENGL_ES2) && !defined(IMGUI_IMPL_OPENGL_ES3) #if (defined(__APPLE__) && (TARGET_OS_IOS || TARGET_OS_TV)) || (defined(__ANDROID__)) -#define IMGUI_IMPL_OPENGL_ES3 // iOS, Android -> GL ES 3, "#version 300 es" -#undef IMGUI_IMPL_OPENGL_LOADER_GL3W -#undef IMGUI_IMPL_OPENGL_LOADER_GLEW -#undef IMGUI_IMPL_OPENGL_LOADER_GLAD -#undef IMGUI_IMPL_OPENGL_LOADER_CUSTOM +#define IMGUI_IMPL_OPENGL_ES2 #elif defined(__EMSCRIPTEN__) -#define IMGUI_IMPL_OPENGL_ES2 // Emscripten -> GL ES 2, "#version 100" -#undef IMGUI_IMPL_OPENGL_LOADER_GL3W -#undef IMGUI_IMPL_OPENGL_LOADER_GLEW -#undef IMGUI_IMPL_OPENGL_LOADER_GLAD -#undef IMGUI_IMPL_OPENGL_LOADER_CUSTOM +#define IMGUI_IMPL_OPENGL_ES2 #endif #endif // GL includes -#if defined(IMGUI_IMPL_OPENGL_ES2) -#include -#elif defined(IMGUI_IMPL_OPENGL_ES3) -#if (defined(__APPLE__) && (TARGET_OS_IOS || TARGET_OS_TV)) -#include // Use GL ES 3 -#else -#include // Use GL ES 3 -#endif -#else -// About Desktop OpenGL function loaders: -// Modern desktop OpenGL doesn't have a standard portable header file to load OpenGL function pointers. -// Helper libraries are often used for this purpose! Here we are supporting a few common ones (gl3w, glew, glad). -// You may use another loader/header of your choice (glext, glLoadGen, etc.), or chose to manually implement your own. -#if defined(IMGUI_IMPL_OPENGL_LOADER_GL3W) -#include // Needs to be initialized with gl3wInit() in user's code -#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLEW) -#include // Needs to be initialized with glewInit() in user's code -#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD) -#include // Needs to be initialized with gladLoadGL() in user's code -#else -#include IMGUI_IMPL_OPENGL_LOADER_CUSTOM -#endif -#endif +#include // Desktop GL has glDrawElementsBaseVertex() which GL ES and WebGL don't have. #if defined(IMGUI_IMPL_OPENGL_ES2) || defined(IMGUI_IMPL_OPENGL_ES3) @@ -147,66 +117,35 @@ bool ImGui_ImplOpenGL3_Init(const char* glsl_version) // Setup back-end capabilities flags ImGuiIO& io = ImGui::GetIO(); io.BackendRendererName = "imgui_impl_opengl3"; -#if IMGUI_IMPL_OPENGL_HAS_DRAW_WITH_BASE_VERTEX - io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes. -#endif io.BackendFlags |= ImGuiBackendFlags_RendererHasViewports; // We can create multi-viewports on the Renderer side (optional) // Store GLSL version string so we can refer to it later in case we recreate shaders. Note: GLSL version is NOT the same as GL version. Leave this to NULL if unsure. -#if defined(IMGUI_IMPL_OPENGL_ES2) - if (glsl_version == NULL) - glsl_version = "#version 100"; - g_IsGLES = true; -#elif defined(IMGUI_IMPL_OPENGL_ES3) - if (glsl_version == NULL) - glsl_version = "#version 300 es"; - g_IsGLES = true; -#else - if (glsl_version == NULL) - glsl_version = "#version 130"; - g_IsGLES = false; -#endif - -#if defined(IMGUI_IMPL_OPENGL_LOADER_GLAD) if (GLAD_GL_ES_VERSION_2_0) { - glsl_version = "#version 100"; + if (glsl_version == NULL) + glsl_version = "#version 100"; g_IsGLES = true; } else if (GLAD_GL_ES_VERSION_3_0) { - glsl_version = "#version 300 es"; + if (glsl_version == NULL) + glsl_version = "#version 300 es"; g_IsGLES = true; } -#endif + else + { + if (glsl_version == NULL) + glsl_version = "#version 130"; + g_IsGLES = false; + } + + if (!g_IsGLES) + io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes. IM_ASSERT((int)strlen(glsl_version) + 2 < IM_ARRAYSIZE(g_GlslVersionString)); strcpy(g_GlslVersionString, glsl_version); strcat(g_GlslVersionString, "\n"); - // Dummy construct to make it easily visible in the IDE and debugger which GL loader has been selected. - // The code actually never uses the 'gl_loader' variable! It is only here so you can read it! - // If auto-detection fails or doesn't select the same GL loader file as used by your application, - // you are likely to get a crash below. - // You can explicitly select a loader by using '#define IMGUI_IMPL_OPENGL_LOADER_XXX' in imconfig.h or compiler command-line. - const char* gl_loader = "Unknown"; - IM_UNUSED(gl_loader); -#if defined(IMGUI_IMPL_OPENGL_LOADER_GL3W) - gl_loader = "GL3W"; -#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLEW) - gl_loader = "GLEW"; -#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD) - gl_loader = "GLAD"; -#else // IMGUI_IMPL_OPENGL_LOADER_CUSTOM - gl_loader = "Custom"; -#endif - - // Make a dummy GL call (we don't actually need the result) - // IF YOU GET A CRASH HERE: it probably means that you haven't initialized the OpenGL function loader used by this code. - // Desktop OpenGL 3/4 need a function loader. See the IMGUI_IMPL_OPENGL_LOADER_xxx explanation above. - GLint current_texture; - glGetIntegerv(GL_TEXTURE_BINDING_2D, ¤t_texture); - if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) ImGui_ImplOpenGL3_InitPlatformInterface(); @@ -256,7 +195,7 @@ static void ImGui_ImplOpenGL3_SetupRenderState(ImDrawData* draw_data, int fb_wid glUseProgram(g_ShaderHandle); glUniform1i(g_AttribLocationTex, 0); glUniformMatrix4fv(g_AttribLocationProjMtx, 1, GL_FALSE, &ortho_projection[0][0]); -#ifdef GL_SAMPLER_BINDING +#ifndef IMGUI_IMPL_OPENGL_ES2 glBindSampler(0, 0); // We use combined texture/sampler state. Applications using GL 3.3 may set that otherwise. #endif @@ -398,11 +337,9 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) // Restore modified GL state glUseProgram(last_program); glBindTexture(GL_TEXTURE_2D, last_texture); -#ifdef GL_SAMPLER_BINDING - glBindSampler(0, last_sampler); -#endif glActiveTexture(last_active_texture); #ifndef IMGUI_IMPL_OPENGL_ES2 + glBindSampler(0, last_sampler); glBindVertexArray(last_vertex_array_object); #endif glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer); diff --git a/src/core/bios.cpp b/src/core/bios.cpp index ab1027604..15ad4084d 100644 --- a/src/core/bios.cpp +++ b/src/core/bios.cpp @@ -2,6 +2,7 @@ #include "YBaseLib/Log.h" #include "YBaseLib/MD5Digest.h" #include "cpu_disasm.h" +#include Log_SetChannel(BIOS); namespace BIOS { @@ -56,7 +57,7 @@ std::optional LoadImageFromFile(std::string_view filename) std::FILE* fp = std::fopen(filename_str.c_str(), "rb"); if (!fp) { - Log_ErrorPrintf("Failed to open BIOS image '%s'", filename_str.c_str()); + Log_ErrorPrintf("Failed to open BIOS image '%s', errno=%d", filename_str.c_str(), errno); return std::nullopt; } diff --git a/src/core/gpu_hw_d3d11.cpp b/src/core/gpu_hw_d3d11.cpp index 6c829846c..2ffa79bf9 100644 --- a/src/core/gpu_hw_d3d11.cpp +++ b/src/core/gpu_hw_d3d11.cpp @@ -34,8 +34,8 @@ bool GPU_HW_D3D11::Initialize(HostDisplay* host_display, System* system, DMA* dm if (!GPU_HW::Initialize(host_display, system, dma, interrupt_controller, timers)) return false; - m_device = static_cast(host_display->GetHostRenderDevice()); - m_context = static_cast(host_display->GetHostRenderContext()); + m_device = static_cast(host_display->GetRenderDevice()); + m_context = static_cast(host_display->GetRenderContext()); if (!m_device || !m_context) return false; diff --git a/src/core/host_display.h b/src/core/host_display.h index bc9643057..38ab7262f 100644 --- a/src/core/host_display.h +++ b/src/core/host_display.h @@ -29,8 +29,12 @@ public: virtual ~HostDisplay() {} virtual RenderAPI GetRenderAPI() const = 0; - virtual void* GetHostRenderDevice() const = 0; - virtual void* GetHostRenderContext() const = 0; + virtual void* GetRenderDevice() const = 0; + virtual void* GetRenderContext() const = 0; + virtual void* GetRenderWindow() const = 0; + + /// Switches the render window, recreating the surface. + virtual void ChangeRenderWindow(void* new_window) = 0; /// Creates an abstracted RGBA8 texture. If dynamic, the texture can be updated with UpdateTexture() below. virtual std::unique_ptr CreateTexture(u32 width, u32 height, const void* data, u32 data_stride, diff --git a/src/duckstation/d3d11_host_display.cpp b/src/duckstation/d3d11_host_display.cpp index 00771e2e4..4f5ed5640 100644 --- a/src/duckstation/d3d11_host_display.cpp +++ b/src/duckstation/d3d11_host_display.cpp @@ -78,16 +78,26 @@ HostDisplay::RenderAPI D3D11HostDisplay::GetRenderAPI() const return HostDisplay::RenderAPI::D3D11; } -void* D3D11HostDisplay::GetHostRenderDevice() const +void* D3D11HostDisplay::GetRenderDevice() const { return m_device.Get(); } -void* D3D11HostDisplay::GetHostRenderContext() const +void* D3D11HostDisplay::GetRenderContext() const { return m_context.Get(); } +void* D3D11HostDisplay::GetRenderWindow() const +{ + return m_window; +} + +void D3D11HostDisplay::ChangeRenderWindow(void* new_window) +{ + Panic("Not supported"); +} + std::unique_ptr D3D11HostDisplay::CreateTexture(u32 width, u32 height, const void* data, u32 data_stride, bool dynamic) { diff --git a/src/duckstation/d3d11_host_display.h b/src/duckstation/d3d11_host_display.h index 0a4319c4d..64e6bf735 100644 --- a/src/duckstation/d3d11_host_display.h +++ b/src/duckstation/d3d11_host_display.h @@ -20,8 +20,11 @@ public: static std::unique_ptr Create(SDL_Window* window); RenderAPI GetRenderAPI() const override; - void* GetHostRenderDevice() const override; - void* GetHostRenderContext() const override; + void* GetRenderDevice() const override; + void* GetRenderContext() const override; + void* GetRenderWindow() const override; + + void ChangeRenderWindow(void* new_window) override; std::unique_ptr CreateTexture(u32 width, u32 height, const void* data, u32 data_stride, bool dynamic) override; diff --git a/src/duckstation/opengl_host_display.cpp b/src/duckstation/opengl_host_display.cpp index 96b2368bd..c272a05e0 100644 --- a/src/duckstation/opengl_host_display.cpp +++ b/src/duckstation/opengl_host_display.cpp @@ -80,16 +80,26 @@ HostDisplay::RenderAPI OpenGLHostDisplay::GetRenderAPI() const return m_is_gles ? HostDisplay::RenderAPI::OpenGLES : HostDisplay::RenderAPI::OpenGL; } -void* OpenGLHostDisplay::GetHostRenderDevice() const +void* OpenGLHostDisplay::GetRenderDevice() const { return nullptr; } -void* OpenGLHostDisplay::GetHostRenderContext() const +void* OpenGLHostDisplay::GetRenderContext() const { return m_gl_context; } +void* OpenGLHostDisplay::GetRenderWindow() const +{ + return m_window; +} + +void OpenGLHostDisplay::ChangeRenderWindow(void* new_window) +{ + Panic("Not implemented"); +} + std::unique_ptr OpenGLHostDisplay::CreateTexture(u32 width, u32 height, const void* data, u32 data_stride, bool dynamic) { diff --git a/src/duckstation/opengl_host_display.h b/src/duckstation/opengl_host_display.h index f915ac08f..0bdd74d87 100644 --- a/src/duckstation/opengl_host_display.h +++ b/src/duckstation/opengl_host_display.h @@ -15,8 +15,11 @@ public: static std::unique_ptr Create(SDL_Window* window); RenderAPI GetRenderAPI() const override; - void* GetHostRenderDevice() const override; - void* GetHostRenderContext() const override; + void* GetRenderDevice() const override; + void* GetRenderContext() const override; + void* GetRenderWindow() const override; + + void ChangeRenderWindow(void* new_window) override; std::unique_ptr CreateTexture(u32 width, u32 height, const void* data, u32 data_stride, bool dynamic) override;