Skip to content

Commit

Permalink
Fixed OpenGL detection issues
Browse files Browse the repository at this point in the history
- Removed an assumption that application will always run using OpenGL 3
- Added support for ES 2/3
- MacOS builds should now be compatible with lower OS versions (Starting with 10.13, High Sierra)
  • Loading branch information
yowidin committed Sep 14, 2021
1 parent 024bc55 commit 9a2647a
Show file tree
Hide file tree
Showing 21 changed files with 1,289 additions and 59 deletions.
9 changes: 8 additions & 1 deletion .ci/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@

import os
from subprocess import run
from sys import platform

if not os.path.exists('_build'):
os.makedirs('_build')

kwargs = {'cwd': '_build', 'check': True}
run(['conan', 'install', '..', '-b', 'missing', '-s', 'compiler.cppstd=17'], **kwargs)
install_args = ['conan', 'install', '..', '-b', 'missing', '-s', 'compiler.cppstd=17']

if platform == 'darwin':
# Build for High Sierra and x64
install_args.extend(['-s', 'os.version=10.13', '-s', 'arch=x86_64'])

run(install_args, **kwargs)
run(['conan', 'build', '..'], **kwargs)
run(['cpack', '-G', 'ZIP'], **kwargs)
13 changes: 12 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ target_sources(serial-plotter PRIVATE

src/inputs/serial.cpp
src/inputs/data.cpp

src/render/backend.cpp
src/render/frontend.cpp
src/render/opengl2.cpp
src/render/opengl3.cpp
src/render/gles2.cpp
src/render/gles3.cpp
src/render/detail/imgui_impl_sdl_es2.cpp
src/render/detail/imgui_impl_sdl_es3.cpp
)

set_target_properties(serial-plotter
Expand All @@ -63,9 +72,11 @@ set_target_properties(serial-plotter
SOVERSION 0
)

find_package(OpenGL REQUIRED)
target_link_libraries(serial-plotter PUBLIC
${CONAN_LIBS_SDL2} ${CONAN_LIBS_GLAD}
imgui::opengl3 imgui::sdl implot::implot Boost::program_options
imgui::opengl2 imgui::opengl3
OpenGL::GL imgui::sdl implot::implot Boost::program_options
)

target_include_directories(serial-plotter SYSTEM
Expand Down
5 changes: 1 addition & 4 deletions conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,4 @@ def build(self):
def configure(self):
self.options['glad'].spec = 'gl'
self.options['glad'].gl_profile = 'core'
if self.settings.os in ['Android', 'iOS']:
self.options['glad'].gles2_version = '3.0'
else:
self.options['glad'].gl_version = '3.2'
self.options['glad'].gl_version = '3.2'
38 changes: 38 additions & 0 deletions include/asp/render/backend.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* @file backend.h
* @author Dennis Sitelew
* @date Sep. 14, 2021
*/
#ifndef INCLUDE_ASP_RENDER_BACKEND_H
#define INCLUDE_ASP_RENDER_BACKEND_H

#include <memory>

namespace asp::render {

enum class version { opengl2 = 2, opengl3 = 3, gles2 = 20, gles3 = 30 };

class frontend;

class backend {
public:
backend(frontend &v)
: frontend_{&v} {}

virtual ~backend() = default;

public:
virtual void init() = 0;
virtual void new_frame() = 0;
virtual void render() = 0;
virtual version get_version() = 0;

static std::unique_ptr<backend> make(version v, frontend &f);

protected:
frontend *frontend_;
};

} // namespace asp::render

#endif /* INCLUDE_ASP_RENDER_BACKEND_H */
26 changes: 26 additions & 0 deletions include/asp/render/detail/imgui_impl_sdl_es2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// ImGui SDL2 binding with OpenGL2
// In this binding, ImTextureID is used to store an OpenGL 'GLuint' texture identifier. Read the FAQ about ImTextureID in imgui.cpp.

// You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
// If you use this binding you'll need to call 4 functions: ImGui_ImplXXXX_Init(), ImGui_ImplXXXX_NewFrame(), ImGui::Render() and ImGui_ImplXXXX_Shutdown().
// If you are new to ImGui, see examples/README.txt and documentation at the top of imgui.cpp.
// https://github.com/ocornut/imgui

#ifndef IMGUI_IMPL_SDL_ES2
#define IMGUI_IMPL_SDL_ES2

#include <imgui.h>

struct SDL_Window;
typedef union SDL_Event SDL_Event;

IMGUI_API bool ImGui_ImplSdlGLES2_Init();
IMGUI_API void ImGui_ImplSdlGLES2_Shutdown();
IMGUI_API void ImGui_ImplSdlGLES2_NewFrame(SDL_Window* window);
IMGUI_API void ImGui_ImplSdlGLES2_RenderDrawLists(ImDrawData* draw_data);

// Use if you want to reset your rendering device without losing ImGui state.
IMGUI_API void ImGui_ImplSdlGLES2_InvalidateDeviceObjects();
IMGUI_API bool ImGui_ImplSdlGLES2_CreateDeviceObjects();

#endif // IMGUI_IMPL_SDL_ES2
26 changes: 26 additions & 0 deletions include/asp/render/detail/imgui_impl_sdl_es3.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// ImGui SDL2 binding with OpenGL3
// In this binding, ImTextureID is used to store an OpenGL 'GLuint' texture identifier. Read the FAQ about ImTextureID in imgui.cpp.

// You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
// If you use this binding you'll need to call 4 functions: ImGui_ImplXXXX_Init(), ImGui_ImplXXXX_NewFrame(), ImGui::Render() and ImGui_ImplXXXX_Shutdown().
// If you are new to ImGui, see examples/README.txt and documentation at the top of imgui.cpp.
// https://github.com/ocornut/imgui

#ifndef IMGUI_IMPL_SDL_ES3
#define IMGUI_IMPL_SDL_ES3

#include <imgui.h>

struct SDL_Window;
typedef union SDL_Event SDL_Event;

IMGUI_API bool ImGui_ImplSdlGLES3_Init();
IMGUI_API void ImGui_ImplSdlGLES3_Shutdown();
IMGUI_API void ImGui_ImplSdlGLES3_NewFrame(SDL_Window* window);
IMGUI_API void ImGui_ImplSdlGLES3_RenderDrawLists(ImDrawData* draw_data);

// Use if you want to reset your rendering device without losing ImGui state.
IMGUI_API void ImGui_ImplSdlGLES3_InvalidateDeviceObjects();
IMGUI_API bool ImGui_ImplSdlGLES3_CreateDeviceObjects();

#endif // IMGUI_IMPL_SDL_ES3
40 changes: 40 additions & 0 deletions include/asp/render/frontend.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @file frontend.h
* @author Dennis Sitelew
* @date Sep. 14, 2021
*/
#ifndef INCLUDE_ASP_RENDER_FRONTEND_H
#define INCLUDE_ASP_RENDER_FRONTEND_H

#include <SDL2/SDL.h>

#include <memory>
#include <string>

namespace asp::render {

class backend;

class frontend {
public:
frontend(SDL_Window &window, SDL_GLContext context);
~frontend();

public:
void new_frame();
void render();
void process_event(const SDL_Event &event);

const std::string &shader_version() const { return shader_version_; }

SDL_Window &window() { return *window_; }

private:
SDL_Window *window_;
std::unique_ptr<backend> backend_;
std::string shader_version_;
};

} // namespace asp::render

#endif /* INCLUDE_ASP_RENDER_FRONTEND_H */
28 changes: 28 additions & 0 deletions include/asp/render/gles2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @file gles2.h
* @author Dennis Sitelew
* @date Sep. 14, 2021
*/
#ifndef INCLUDE_ASP_RENDER_GLES2_H
#define INCLUDE_ASP_RENDER_GLES2_H

#include <asp/render/backend.h>

namespace asp::render {

class GLES2 : public backend {
public:
using backend::backend;
~GLES2();

public:
void init() override;
void new_frame() override;
void render() override;

version get_version() override { return version::gles2; }
};

} // namespace asp::render

#endif /* INCLUDE_ASP_RENDER_GLES2_H */
28 changes: 28 additions & 0 deletions include/asp/render/gles3.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @file gles3.h
* @author Dennis Sitelew
* @date Sep. 14, 2021
*/
#ifndef INCLUDE_ASP_RENDER_GLES3_H
#define INCLUDE_ASP_RENDER_GLES3_H

#include <asp/render/backend.h>

namespace asp::render {

class GLES3 : public backend {
public:
using backend::backend;
~GLES3();

public:
void init() override;
void new_frame() override;
void render() override;

version get_version() override { return version::gles3; }
};

} // namespace asp::render

#endif /* INCLUDE_ASP_RENDER_GLES3_H */
28 changes: 28 additions & 0 deletions include/asp/render/opengl2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @file opengl2.h
* @author Dennis Sitelew
* @date Sep. 14, 2021
*/
#ifndef INCLUDE_ASP_RENDER_OPENGL2_H
#define INCLUDE_ASP_RENDER_OPENGL2_H

#include <asp/render/backend.h>

namespace asp::render {

class OpenGL2 : public backend {
public:
using backend::backend;
~OpenGL2();

public:
void init() override;
void new_frame() override;
void render() override;

version get_version() override { return version::opengl2; }
};

} // namespace asp::render

#endif /* INCLUDE_ASP_RENDER_OPENGL2_H */
28 changes: 28 additions & 0 deletions include/asp/render/opengl3.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @file opengl3.h
* @author Dennis Sitelew
* @date Sep. 14, 2021
*/
#ifndef INCLUDE_ASP_RENDER_OPENGL3_H
#define INCLUDE_ASP_RENDER_OPENGL3_H

#include <asp/render/backend.h>

namespace asp::render {

class OpenGL3 : public backend {
public:
using backend::backend;
~OpenGL3();

public:
void init() override;
void new_frame() override;
void render() override;

version get_version() override { return version::opengl3; }
};

} // namespace asp::render

#endif /* INCLUDE_ASP_RENDER_OPENGL3_H */
4 changes: 4 additions & 0 deletions include/asp/ui/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@

#include <asp/inputs/data.h>
#include <asp/inputs/serial.h>
#include <asp/render/frontend.h>

#include <SDL2/SDL.h>
#include <imgui.h>
#include <implot.h>
#include <vector>
#include <memory>

namespace asp {

Expand Down Expand Up @@ -56,7 +58,9 @@ class window {
bool stop_{false};

SDL_Window *window_{nullptr};
SDL_GLContext context_;
SDL_Event event_{};
std::unique_ptr<render::frontend> frontend_;
ImVec4 clear_color_{0.45f, 0.55f, 0.60f, 1.00f};

std::vector<drawable *> drawables_;
Expand Down
35 changes: 35 additions & 0 deletions src/render/backend.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* @file backend.cpp
* @author Dennis Sitelew
* @date Sep. 14, 2021
*/

#include <asp/render/backend.h>
#include <asp/render/opengl2.h>
#include <asp/render/opengl3.h>
#include <asp/render/gles2.h>
#include <asp/render/gles3.h>

#include <string>
#include <stdexcept>

using namespace asp::render;

std::unique_ptr<backend> backend::make(version v, frontend &f) {
switch (v) {
case version::opengl2:
return std::make_unique<OpenGL2>(f);

case version::opengl3:
return std::make_unique<OpenGL3>(f);

case version::gles2:
return std::make_unique<GLES2>(f);

case version::gles3:
return std::make_unique<GLES3>(f);
}

throw std::runtime_error("Unexpected GL Version: " +
std::to_string(static_cast<int>(v)));
}
Loading

0 comments on commit 9a2647a

Please sign in to comment.