From c9cb20d8496dab334f3e9339012d32d56853a9fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sat, 18 May 2013 11:17:51 +0200 Subject: [PATCH] Call glClearDepth() if glClearDepthf() is not available. --- src/Context.cpp | 2 ++ src/Renderer.cpp | 34 ++++++++++++++++++++++++++++++---- src/Renderer.h | 20 +++++++++++++++++--- 3 files changed, 49 insertions(+), 7 deletions(-) diff --git a/src/Context.cpp b/src/Context.cpp index 4d0ed0e7c..67818d458 100644 --- a/src/Context.cpp +++ b/src/Context.cpp @@ -40,6 +40,7 @@ #include "Framebuffer.h" #include "Mesh.h" #include "Renderbuffer.h" +#include "Renderer.h" #include "Implementation/State.h" @@ -368,6 +369,7 @@ Context::Context() { Framebuffer::initializeContextBasedFunctionality(this); Mesh::initializeContextBasedFunctionality(this); Renderbuffer::initializeContextBasedFunctionality(this); + Renderer::initializeContextBasedFunctionality(this); } Context::~Context() { diff --git a/src/Renderer.cpp b/src/Renderer.cpp index 9284c3896..b74e66e60 100644 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -26,9 +26,17 @@ #include "Color.h" #include "Math/Geometry/Rectangle.h" +#include "Context.h" +#include "Extensions.h" namespace Magnum { +#ifndef MAGNUM_TARGET_GLES +Renderer::ClearDepthfImplementation Renderer::clearDepthfImplementation = &Renderer::clearDepthfImplementationDefault; +#else +Renderer::ClearDepthfImplementation Renderer::clearDepthfImplementation = &Renderer::clearDepthfImplementationES; +#endif + void Renderer::setFeature(const Feature feature, const bool enabled) { enabled ? glEnable(GLenum(feature)) : glDisable(GLenum(feature)); } @@ -47,10 +55,6 @@ void Renderer::setClearDepth(const Double depth) { } #endif -void Renderer::setClearDepth(const Float depth) { - glClearDepthf(depth); -} - void Renderer::setClearStencil(const Int stencil) { glClearStencil(stencil); } @@ -149,4 +153,26 @@ void Renderer::setLogicOperation(const LogicOperation operation) { } #endif +void Renderer::initializeContextBasedFunctionality(Context* context) { + #ifndef MAGNUM_TARGET_GLES + if(context->isExtensionSupported()) { + Debug() << "Renderer: using" << Extensions::GL::ARB::ES2_compatibility::string() << "features"; + + clearDepthfImplementation = &Renderer::clearDepthfImplementationES; + } + #else + static_cast(context); + #endif +} + +#ifndef MAGNUM_TARGET_GLES +void Renderer::clearDepthfImplementationDefault(const GLfloat depth) { + glClearDepth(depth); +} +#endif + +void Renderer::clearDepthfImplementationES(const GLfloat depth) { + glClearDepthf(depth); +} + } diff --git a/src/Renderer.h b/src/Renderer.h index aba39ab23..7798d6ede 100644 --- a/src/Renderer.h +++ b/src/Renderer.h @@ -43,6 +43,8 @@ Access to global renderer configuration. @todo @extension{ARB,viewport_array} */ class MAGNUM_EXPORT Renderer { + friend class Context; + public: Renderer() = delete; @@ -226,10 +228,12 @@ class MAGNUM_EXPORT Renderer { * @overload * * @see @ref Feature "Feature::DepthTest", @fn_gl{ClearDepth} - * @requires_gl41 %Extension @extension{ARB,ES2_compatibility} - * @todo Call double version if the extension is not available + * If OpenGL ES, OpenGL 4.1 or extension @extension{ARB,ES2_compatibility} + * is not available, this function behaves exactly as setClearDepth(Double). */ - static void setClearDepth(Float depth); + inline static void setClearDepth(Float depth) { + clearDepthfImplementation(depth); + } /** * @brief Set clear stencil @@ -835,6 +839,16 @@ class MAGNUM_EXPORT Renderer { } /*@}*/ + + private: + static void MAGNUM_LOCAL initializeContextBasedFunctionality(Context* context); + + typedef void(*ClearDepthfImplementation)(GLfloat); + #ifndef MAGNUM_TARGET_GLES + static void MAGNUM_LOCAL clearDepthfImplementationDefault(GLfloat depth); + #endif + static void MAGNUM_LOCAL clearDepthfImplementationES(GLfloat depth); + static ClearDepthfImplementation clearDepthfImplementation; }; }