-
-
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
8 changed files
with
397 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,12 @@ | ||
add_sources(${PROJECT_NAME} | ||
context.cpp | ||
tests.cpp | ||
window.cpp | ||
) | ||
|
||
add_demo_cpp(openage::renderer::tests::renderer_demo "open the render window") | ||
|
||
|
||
#add_subdirectory(gl2/) | ||
#add_subdirectory(gl3/) | ||
#add_subdirectory(vulkan/) |
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,198 @@ | ||
// Copyright 2015-2015 the openage authors. See copying.md for legal info. | ||
|
||
#include "window.h" | ||
|
||
#include <epoxy/gl.h> | ||
#include <SDL2/SDL.h> | ||
|
||
#include "../log/log.h" | ||
#include "../util/error.h" | ||
|
||
|
||
namespace openage { | ||
namespace renderer { | ||
|
||
#if WITH_OPENGL | ||
// TODO: get max available gl version | ||
constexpr int opengl_version_major = 2; | ||
constexpr int opengl_version_minor = 1; | ||
#endif | ||
|
||
Context::Context(context_type t) | ||
: | ||
type{t} { | ||
if (this->type == context_type::opengl and not WITH_OPENGL) { | ||
throw util::Error(MSG(err) << "OpenGL support not enabled!"); | ||
} | ||
else if (this->type == context_type::vulkan and not WITH_VULKAN) { | ||
throw util::Error(MSG(err) << "Vulkan support not enabled!"); | ||
} | ||
else if (this->type == context_type::autodetect) { | ||
// priority: vulkan > opengl | ||
if (WITH_VULKAN) { | ||
this->type = context_type::vulkan; | ||
} | ||
else if (WITH_OPENGL) { | ||
this->type = context_type::opengl; | ||
} | ||
else { | ||
throw util::Error(MSG(err) << "No render context available!"); | ||
} | ||
} | ||
else { | ||
throw util::Error(MSG(err) << "Unknown context type requested!"); | ||
} | ||
} | ||
|
||
Context::~Context() {} | ||
|
||
void Context::prepare() { | ||
switch (this->type) { | ||
#if WITH_OPENGL | ||
case context_type::opengl: | ||
this->prepare_gl(); | ||
break; | ||
#endif | ||
#if WITH_VULKAN | ||
case context_type::vulkan: | ||
// TODO; | ||
break; | ||
#endif | ||
default: | ||
throw util::Error(MSG(err) << "Unknown context to be prepared!"); | ||
}; | ||
} | ||
|
||
uint32_t Context::get_window_flags() { | ||
switch (this->type) { | ||
#if WITH_OPENGL | ||
case context_type::opengl: | ||
return SDL_WINDOW_OPENGL; | ||
#endif | ||
#if WITH_VULKAN | ||
case context_type::vulkan: | ||
return SDL_WINDOW_VULKAN; | ||
#endif | ||
default: | ||
throw util::Error(MSG(err) << "Unknown context w-flags requested!"); | ||
}; | ||
} | ||
|
||
void Context::create(SDL_Window *window) { | ||
switch (this->type) { | ||
#if WITH_OPENGL | ||
case context_type::opengl: | ||
this->create_gl(window); | ||
break; | ||
#endif | ||
#if WITH_VULKAN | ||
case context_type::vulkan: | ||
// TODO | ||
break; | ||
#endif | ||
default: | ||
throw util::Error(MSG(err) << "Unknown context to be created!"); | ||
}; | ||
} | ||
|
||
void Context::setup() { | ||
switch (this->type) { | ||
#if WITH_OPENGL | ||
case context_type::opengl: | ||
this->setup_gl(); | ||
break; | ||
#endif | ||
#if WITH_VULKAN | ||
case context_type::vulkan: | ||
// TODO | ||
break; | ||
#endif | ||
default: | ||
throw util::Error(MSG(err) << "Unknown context to be set up!"); | ||
}; | ||
} | ||
|
||
void Context::destroy() { | ||
switch (this->type) { | ||
#if WITH_OPENGL | ||
case context_type::opengl: | ||
this->destroy_gl(); | ||
break; | ||
#endif | ||
#if WITH_VULKAN | ||
case context_type::vulkan: | ||
// TODO | ||
break; | ||
#endif | ||
default: | ||
throw util::Error(MSG(err) << "Unknown context to be set up!"); | ||
}; | ||
} | ||
|
||
|
||
#if WITH_OPENGL | ||
void Context::prepare_gl() { | ||
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 Context::create_gl(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 Context::setup_gl() { | ||
// 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 Context::destroy_gl() { | ||
SDL_GL_DeleteContext(this->glcontext); | ||
} | ||
|
||
#endif | ||
|
||
}} // namespace openage::renderer |
Oops, something went wrong.