From 24b91e40c2440d6f2711c2a9ee80ce6a5b5bccae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Tue, 3 Jul 2018 17:49:59 +0200 Subject: [PATCH] Platform: ability to specify color/depth/stencil buffer sizes. --- doc/changelog.dox | 6 +++ src/Magnum/Platform/AndroidApplication.cpp | 15 ++++--- src/Magnum/Platform/AndroidApplication.h | 50 +++++++++++++++++++++- src/Magnum/Platform/GlfwApplication.cpp | 12 +++++- src/Magnum/Platform/GlfwApplication.h | 48 ++++++++++++++++++++- src/Magnum/Platform/Sdl2Application.cpp | 10 ++++- src/Magnum/Platform/Sdl2Application.h | 49 +++++++++++++++++++-- 7 files changed, 174 insertions(+), 16 deletions(-) diff --git a/doc/changelog.dox b/doc/changelog.dox index e90081d11..d99fcc394 100644 --- a/doc/changelog.dox +++ b/doc/changelog.dox @@ -59,6 +59,12 @@ See also: - Implemented @ref Platform::GlfwApplication::MouseMoveEvent::buttons() for feature parity with @ref Platform::Sdl2Application +- Added @ref Platform::Sdl2Application::GLConfiguration::setColorBufferSize() "GLConfiguration::setColorBufferSize()", + @ref Platform::Sdl2Application::GLConfiguration::setDepthBufferSize() "GLConfiguration::setDepthBufferSize()", + @ref Platform::Sdl2Application::GLConfiguration::setStencilBufferSize() "GLConfiguration::setStencilBufferSize()", + to @ref Platform::Sdl2Application, @ref Platform::GlfwApplication and + @ref Platform::AndroidApplication. This also makes the default framebuffer + parameters consistent across the implementations. @subsection changelog-latest-changes Changes and improvements diff --git a/src/Magnum/Platform/AndroidApplication.cpp b/src/Magnum/Platform/AndroidApplication.cpp index c16f1d34e..b17276e05 100644 --- a/src/Magnum/Platform/AndroidApplication.cpp +++ b/src/Magnum/Platform/AndroidApplication.cpp @@ -98,7 +98,7 @@ bool AndroidApplication::tryCreate(const Configuration& configuration) { return tryCreate(configuration, GLConfiguration{}); } -bool AndroidApplication::tryCreate(const Configuration& configuration, const GLConfiguration&) { +bool AndroidApplication::tryCreate(const Configuration& configuration, const GLConfiguration& glConfiguration) { CORRADE_ASSERT(_context->version() == GL::Version::None, "Platform::AndroidApplication::tryCreate(): context already created", false); /* Initialize EGL */ @@ -112,10 +112,12 @@ bool AndroidApplication::tryCreate(const Configuration& configuration, const GLC /* Choose config */ const EGLint configAttributes[] = { EGL_SURFACE_TYPE, EGL_WINDOW_BIT, - EGL_BLUE_SIZE, 8, - EGL_GREEN_SIZE, 8, - EGL_RED_SIZE, 8, - EGL_DEPTH_SIZE, 24, + EGL_RED_SIZE, glConfiguration.colorBufferSize().r(), + EGL_GREEN_SIZE, glConfiguration.colorBufferSize().g(), + EGL_BLUE_SIZE, glConfiguration.colorBufferSize().b(), + EGL_ALPHA_SIZE, glConfiguration.colorBufferSize().a(), + EGL_DEPTH_SIZE, glConfiguration.depthBufferSize(), + EGL_STENCIL_SIZE, glConfiguration.stencilBufferSize(), EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL_NONE }; @@ -242,6 +244,9 @@ std::int32_t AndroidApplication::inputEvent(android_app* state, AInputEvent* eve return 0; } +AndroidApplication::GLConfiguration::GLConfiguration(): + _colorBufferSize{8, 8, 8, 0}, _depthBufferSize{24}, _stencilBufferSize{0} {} + void AndroidApplication::exec(android_app* state, std::unique_ptr(*instancer)(const Arguments&)) { state->onAppCmd = commandEvent; state->onInputEvent = inputEvent; diff --git a/src/Magnum/Platform/AndroidApplication.h b/src/Magnum/Platform/AndroidApplication.h index 7a1402411..e0fd9d8ed 100644 --- a/src/Magnum/Platform/AndroidApplication.h +++ b/src/Magnum/Platform/AndroidApplication.h @@ -37,7 +37,7 @@ #include "Magnum/Magnum.h" #include "Magnum/Tags.h" #include "Magnum/GL/GL.h" -#include "Magnum/Math/Vector2.h" +#include "Magnum/Math/Vector4.h" #include "Magnum/Platform/Platform.h" #if defined(CORRADE_TARGET_ANDROID) || defined(DOXYGEN_GENERATING_OUTPUT) @@ -393,7 +393,7 @@ Double-buffered RGBA canvas with depth and stencil buffers. */ class AndroidApplication::GLConfiguration { public: - constexpr /*implicit*/ GLConfiguration() {} + /*implicit*/ GLConfiguration(); /** * @brief Set context version @@ -404,6 +404,52 @@ class AndroidApplication::GLConfiguration { * settings. */ GLConfiguration& setVersion(GL::Version) { return *this; } + + /** @brief Color buffer size */ + Vector4i colorBufferSize() const { return _colorBufferSize; } + + /** + * @brief Set color buffer size + * + * Default is @cpp {8, 8, 8, 0} @ce (8-bit-per-channel RGB, no alpha). + * @see @ref setDepthBufferSize(), @ref setStencilBufferSize() + */ + GLConfiguration& setColorBufferSize(const Vector4i& size) { + _colorBufferSize = size; + return *this; + } + + /** @brief Depth buffer size */ + Int depthBufferSize() const { return _depthBufferSize; } + + /** + * @brief Set depth buffer size + * + * Default is @cpp 24 @ce bits. + * @see @ref setColorBufferSize(), @ref setStencilBufferSize() + */ + GLConfiguration& setDepthBufferSize(Int size) { + _depthBufferSize = size; + return *this; + } + + /** @brief Stencil buffer size */ + Int stencilBufferSize() const { return _stencilBufferSize; } + + /** + * @brief Set stencil buffer size + * + * Default is @cpp 0 @ce bits (i.e., no stencil buffer). + * @see @ref setColorBufferSize(), @ref setDepthBufferSize() + */ + GLConfiguration& setStencilBufferSize(Int size) { + _stencilBufferSize = size; + return *this; + } + + private: + Vector4i _colorBufferSize; + Int _depthBufferSize, _stencilBufferSize; }; /** diff --git a/src/Magnum/Platform/GlfwApplication.cpp b/src/Magnum/Platform/GlfwApplication.cpp index f3f99c413..122c0c5e7 100644 --- a/src/Magnum/Platform/GlfwApplication.cpp +++ b/src/Magnum/Platform/GlfwApplication.cpp @@ -210,7 +210,13 @@ bool GlfwApplication::tryCreate(const Configuration& configuration, const GLConf } glfwWindowHint(GLFW_FOCUSED, configuration.windowFlags() >= Configuration::WindowFlag::Focused); - /* Context window hints */ + /* Framebuffer setup */ + glfwWindowHint(GLFW_RED_BITS, glConfiguration.colorBufferSize().r()); + glfwWindowHint(GLFW_GREEN_BITS, glConfiguration.colorBufferSize().g()); + glfwWindowHint(GLFW_BLUE_BITS, glConfiguration.colorBufferSize().b()); + glfwWindowHint(GLFW_ALPHA_BITS, glConfiguration.colorBufferSize().a()); + glfwWindowHint(GLFW_DEPTH_BITS, glConfiguration.depthBufferSize()); + glfwWindowHint(GLFW_STENCIL_BITS, glConfiguration.stencilBufferSize()); glfwWindowHint(GLFW_SAMPLES, glConfiguration.sampleCount()); glfwWindowHint(GLFW_SRGB_CAPABLE, glConfiguration.isSRGBCapable()); @@ -473,7 +479,9 @@ void GlfwApplication::mouseScrollEvent(MouseScrollEvent&) {} void GlfwApplication::textInputEvent(TextInputEvent&) {} #ifdef MAGNUM_TARGET_GL -GlfwApplication::GLConfiguration::GLConfiguration(): _sampleCount{0}, _version{GL::Version::None} {} +GlfwApplication::GLConfiguration::GLConfiguration(): + _colorBufferSize{8, 8, 8, 0}, _depthBufferSize{24}, _stencilBufferSize{0}, + _sampleCount{0}, _version{GL::Version::None} {} GlfwApplication::GLConfiguration::~GLConfiguration() = default; #endif diff --git a/src/Magnum/Platform/GlfwApplication.h b/src/Magnum/Platform/GlfwApplication.h index 3c921099c..e86b3c033 100644 --- a/src/Magnum/Platform/GlfwApplication.h +++ b/src/Magnum/Platform/GlfwApplication.h @@ -37,7 +37,7 @@ #include "Magnum/Magnum.h" #include "Magnum/Tags.h" -#include "Magnum/Math/Vector2.h" +#include "Magnum/Math/Vector4.h" #include "Magnum/Platform/Platform.h" #ifdef MAGNUM_TARGET_GL @@ -482,7 +482,7 @@ CORRADE_ENUMSET_OPERATORS(GlfwApplication::Flags) /** @brief OpenGL context configuration -Double-buffered RGBA window with depth and stencil buffers. +The created window is always with double-buffered OpenGL context. @note This function is available only if Magnum is compiled with @ref MAGNUM_TARGET_GL enabled (done by default). See @ref building-features @@ -553,6 +553,48 @@ class GlfwApplication::GLConfiguration { return *this; } + /** @brief Color buffer size */ + Vector4i colorBufferSize() const { return _colorBufferSize; } + + /** + * @brief Set color buffer size + * + * Default is @cpp {8, 8, 8, 0} @ce (8-bit-per-channel RGB, no alpha). + * @see @ref setDepthBufferSize(), @ref setStencilBufferSize() + */ + GLConfiguration& setColorBufferSize(const Vector4i& size) { + _colorBufferSize = size; + return *this; + } + + /** @brief Depth buffer size */ + Int depthBufferSize() const { return _depthBufferSize; } + + /** + * @brief Set depth buffer size + * + * Default is @cpp 24 @ce bits. + * @see @ref setColorBufferSize(), @ref setStencilBufferSize() + */ + GLConfiguration& setDepthBufferSize(Int size) { + _depthBufferSize = size; + return *this; + } + + /** @brief Stencil buffer size */ + Int stencilBufferSize() const { return _stencilBufferSize; } + + /** + * @brief Set stencil buffer size + * + * Default is @cpp 0 @ce bits (i.e., no stencil buffer). + * @see @ref setColorBufferSize(), @ref setDepthBufferSize() + */ + GLConfiguration& setStencilBufferSize(Int size) { + _stencilBufferSize = size; + return *this; + } + /** @brief Sample count */ Int sampleCount() const { return _sampleCount; } @@ -584,6 +626,8 @@ class GlfwApplication::GLConfiguration { } private: + Vector4i _colorBufferSize; + Int _depthBufferSize, _stencilBufferSize; Int _sampleCount; GL::Version _version; Flags _flags; diff --git a/src/Magnum/Platform/Sdl2Application.cpp b/src/Magnum/Platform/Sdl2Application.cpp index 1941936a5..b3471d89c 100644 --- a/src/Magnum/Platform/Sdl2Application.cpp +++ b/src/Magnum/Platform/Sdl2Application.cpp @@ -166,9 +166,14 @@ bool Sdl2Application::tryCreate(const Configuration& configuration, const GLConf CORRADE_ASSERT(_context->version() == GL::Version::None, "Platform::Sdl2Application::tryCreate(): context already created", false); - /* Enable double buffering and 24bt depth buffer */ + /* Enable double buffering, set up buffer sizes */ SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); - SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); + SDL_GL_SetAttribute(SDL_GL_RED_SIZE, glConfiguration.colorBufferSize().r()); + SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, glConfiguration.colorBufferSize().g()); + SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, glConfiguration.colorBufferSize().b()); + SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, glConfiguration.colorBufferSize().a()); + SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, glConfiguration.depthBufferSize()); + SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, glConfiguration.stencilBufferSize()); /* Multisampling */ SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, glConfiguration.sampleCount() > 1 ? 1 : 0); @@ -613,6 +618,7 @@ void Sdl2Application::textEditingEvent(TextEditingEvent&) {} #ifdef MAGNUM_TARGET_GL Sdl2Application::GLConfiguration::GLConfiguration(): + _colorBufferSize{8, 8, 8, 0}, _depthBufferSize{24}, _stencilBufferSize{0}, _sampleCount(0) #ifndef CORRADE_TARGET_EMSCRIPTEN , _version(GL::Version::None), _sRGBCapable{false} diff --git a/src/Magnum/Platform/Sdl2Application.h b/src/Magnum/Platform/Sdl2Application.h index f68f80460..395183de1 100644 --- a/src/Magnum/Platform/Sdl2Application.h +++ b/src/Magnum/Platform/Sdl2Application.h @@ -36,7 +36,7 @@ #include "Magnum/Magnum.h" #include "Magnum/Tags.h" -#include "Magnum/Math/Vector2.h" +#include "Magnum/Math/Vector4.h" #include "Magnum/Platform/Platform.h" #ifdef MAGNUM_TARGET_GL @@ -836,8 +836,7 @@ class Sdl2Application { /** @brief OpenGL context configuration -The created window is always with double-buffered OpenGL context and 24bit -depth buffer. +The created window is always with double-buffered OpenGL context. @note This function is available only if Magnum is compiled with @ref MAGNUM_TARGET_GL enabled (done by default). See @ref building-features @@ -931,6 +930,48 @@ class Sdl2Application::GLConfiguration { return *this; } + /** @brief Color buffer size */ + Vector4i colorBufferSize() const { return _colorBufferSize; } + + /** + * @brief Set color buffer size + * + * Default is @cpp {8, 8, 8, 0} @ce (8-bit-per-channel RGB, no alpha). + * @see @ref setDepthBufferSize(), @ref setStencilBufferSize() + */ + GLConfiguration& setColorBufferSize(const Vector4i& size) { + _colorBufferSize = size; + return *this; + } + + /** @brief Depth buffer size */ + Int depthBufferSize() const { return _depthBufferSize; } + + /** + * @brief Set depth buffer size + * + * Default is @cpp 24 @ce bits. + * @see @ref setColorBufferSize(), @ref setStencilBufferSize() + */ + GLConfiguration& setDepthBufferSize(Int size) { + _depthBufferSize = size; + return *this; + } + + /** @brief Stencil buffer size */ + Int stencilBufferSize() const { return _stencilBufferSize; } + + /** + * @brief Set stencil buffer size + * + * Default is @cpp 0 @ce bits (i.e., no stencil buffer). + * @see @ref setColorBufferSize(), @ref setDepthBufferSize() + */ + GLConfiguration& setStencilBufferSize(Int size) { + _stencilBufferSize = size; + return *this; + } + /** @brief Sample count */ Int sampleCount() const { return _sampleCount; } @@ -969,6 +1010,8 @@ class Sdl2Application::GLConfiguration { #endif private: + Vector4i _colorBufferSize; + Int _depthBufferSize, _stencilBufferSize; Int _sampleCount; #ifndef CORRADE_TARGET_EMSCRIPTEN GL::Version _version;