-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
renderer: modular render window and context creation
- Loading branch information
Showing
12 changed files
with
428 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
add_sources(${PROJECT_NAME} | ||
context.cpp | ||
tests.cpp | ||
window.cpp | ||
) | ||
|
||
add_demo_cpp(openage::renderer::tests::renderer_demo "open the render window") | ||
|
||
add_subdirectory(gl/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright 2015-2015 the openage authors. See copying.md for legal info. | ||
|
||
#include "context.h" | ||
|
||
#include "../config.h" | ||
#include "../log/log.h" | ||
#include "../util/error.h" | ||
|
||
#if WITH_OPENGL | ||
#include "gl/context.h" | ||
#endif | ||
|
||
#if WITH_VULKAN | ||
#include "vulkan/context.h" | ||
#endif | ||
|
||
|
||
namespace openage { | ||
namespace renderer { | ||
|
||
Context::Context() {} | ||
Context::~Context() {} | ||
|
||
std::unique_ptr<Context> Context::generate(context_type t) { | ||
if (t == context_type::opengl and not WITH_OPENGL) { | ||
throw util::Error(MSG(err) << "OpenGL support not enabled!"); | ||
} | ||
else if (t == context_type::vulkan and not WITH_VULKAN) { | ||
throw util::Error(MSG(err) << "Vulkan support not enabled!"); | ||
} | ||
else if (t == context_type::autodetect) { | ||
// priority: vulkan > opengl | ||
if (WITH_VULKAN) { | ||
#if WITH_VULKAN | ||
log::log(MSG(dbg) << "Using Vulkan context..."); | ||
return std::make_unique<VulkanContext>(); | ||
#endif | ||
} | ||
else if (WITH_OPENGL) { | ||
#if WITH_OPENGL | ||
log::log(MSG(dbg) << "Using OpenGL context..."); | ||
return std::make_unique<GLContext>(); | ||
#endif | ||
} | ||
else { | ||
throw util::Error(MSG(err) << "No render context available!"); | ||
} | ||
} | ||
else { | ||
throw util::Error(MSG(err) << "Unknown context type requested!"); | ||
} | ||
|
||
throw util::Error(MSG(err) << "Context creation dead code reached! Veeery bad."); | ||
} | ||
|
||
}} // namespace openage::renderer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright 2015-2015 the openage authors. See copying.md for legal info. | ||
|
||
#ifndef OPENAGE_RENDERER_CONTEXT_H_ | ||
#define OPENAGE_RENDERER_CONTEXT_H_ | ||
|
||
#include <SDL2/SDL.h> | ||
|
||
#include <memory> | ||
|
||
#include "../coord/window.h" | ||
|
||
|
||
namespace openage { | ||
namespace renderer { | ||
|
||
enum class context_type { | ||
autodetect, | ||
opengl, | ||
vulkan, | ||
}; | ||
|
||
class Context { | ||
public: | ||
Context(); | ||
virtual ~Context(); | ||
|
||
static std::unique_ptr<Context> generate(context_type t); | ||
|
||
virtual void prepare() = 0; | ||
virtual uint32_t get_window_flags() = 0; | ||
virtual void create(SDL_Window *window) = 0; | ||
virtual void setup() = 0; | ||
virtual void destroy() = 0; | ||
}; | ||
|
||
}} // namespace openage::renderer | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
add_sources(${PROJECT_NAME} | ||
context.cpp | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Copyright 2015-2015 the openage authors. See copying.md for legal info. | ||
|
||
#include "../../config.h" | ||
#if WITH_OPENGL | ||
|
||
#include "context.h" | ||
|
||
#include <epoxy/gl.h> | ||
#include <SDL2/SDL.h> | ||
|
||
#include "../../log/log.h" | ||
#include "../../util/error.h" | ||
|
||
namespace openage { | ||
namespace renderer { | ||
|
||
// TODO: get max available gl version | ||
constexpr int opengl_version_major = 2; | ||
constexpr int opengl_version_minor = 1; | ||
|
||
GLContext::GLContext() {} | ||
GLContext::~GLContext() {} | ||
|
||
uint32_t GLContext::get_window_flags() { | ||
return SDL_WINDOW_OPENGL; | ||
} | ||
|
||
void GLContext::prepare() { | ||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); | ||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, opengl_version_major); | ||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, opengl_version_minor); | ||
SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1); | ||
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); | ||
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); | ||
} | ||
|
||
void GLContext::create(SDL_Window *window) { | ||
this->glcontext = SDL_GL_CreateContext(window); | ||
|
||
if (this->glcontext == nullptr) { | ||
throw util::Error(MSG(err) << "Failed creating OpenGL context: " << SDL_GetError()); | ||
} | ||
|
||
// check the OpenGL version, for shaders n stuff | ||
int epoxy_glv = opengl_version_major * 10 + opengl_version_minor; | ||
if (not epoxy_is_desktop_gl() or epoxy_gl_version() < epoxy_glv) { | ||
throw util::Error(MSG(err) << "OpenGL " | ||
<< opengl_version_major << "." << opengl_version_minor | ||
<< " not available"); | ||
} | ||
} | ||
|
||
void GLContext::setup() { | ||
// to quote the standard doc: 'The value gives a rough estimate of the | ||
// largest texture that the GL can handle' | ||
// -> wat? anyways, we need at least 1024x1024. | ||
int max_texture_size; | ||
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_texture_size); | ||
log::log(MSG(dbg) << "Maximum supported texture size: " << max_texture_size); | ||
if (max_texture_size < 1024) { | ||
throw util::Error(MSG(err) << "Maximum supported texture size too small: " << max_texture_size); | ||
} | ||
|
||
int max_texture_units; | ||
glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_texture_units); | ||
log::log(MSG(dbg) << "Maximum supported texture units: " << max_texture_units); | ||
if (max_texture_units < 2) { | ||
throw util::Error(MSG(err) << "Your GPU has too less texture units: " << max_texture_units); | ||
} | ||
|
||
// vsync on | ||
SDL_GL_SetSwapInterval(1); | ||
|
||
// TODO: move the following statements to some other place. | ||
|
||
// enable alpha blending | ||
glEnable(GL_BLEND); | ||
|
||
// order of drawing relevant for depth | ||
// what gets drawn last is displayed on top. | ||
glDisable(GL_DEPTH_TEST); | ||
|
||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | ||
} | ||
|
||
void GLContext::destroy() { | ||
SDL_GL_DeleteContext(this->glcontext); | ||
} | ||
|
||
}} // namespace openage::renderer | ||
|
||
#endif // if WITH_OPENGL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright 2015-2015 the openage authors. See copying.md for legal info. | ||
|
||
#ifndef OPENAGE_RENDERER_GL_CONTEXT_H_ | ||
#define OPENAGE_RENDERER_GL_CONTEXT_H_ | ||
|
||
#include <SDL2/SDL.h> | ||
|
||
#include "../context.h" | ||
#include "../../config.h" | ||
|
||
|
||
namespace openage { | ||
namespace renderer { | ||
|
||
class GLContext : public Context { | ||
public: | ||
GLContext(); | ||
~GLContext(); | ||
|
||
SDL_GLContext glcontext; | ||
|
||
virtual void prepare(); | ||
virtual uint32_t get_window_flags(); | ||
virtual void create(SDL_Window *window); | ||
virtual void setup(); | ||
virtual void destroy(); | ||
}; | ||
|
||
}} // namespace openage::renderer | ||
|
||
#endif |
Oops, something went wrong.