ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

context.cpp

Go to the documentation of this file.
00001 /* ResidualVM - A 3D game interpreter
00002  *
00003  * ResidualVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "graphics/opengl/context.h"
00024 
00025 #include "common/debug.h"
00026 #include "common/str.h"
00027 #include "common/textconsole.h"
00028 #include "common/tokenizer.h"
00029 
00030 #include "graphics/opengl/system_headers.h"
00031 
00032 #ifdef USE_OPENGL
00033 
00034 namespace Common {
00035 DECLARE_SINGLETON(OpenGL::Context);
00036 }
00037 
00038 namespace OpenGL {
00039 
00040 Context::Context() {
00041     reset();
00042 }
00043 
00044 void Context::reset() {
00045     maxTextureSize = 0;
00046 
00047     NPOTSupported = false;
00048     shadersSupported = false;
00049     framebufferObjectSupported = false;
00050     packedDepthStencilSupported = false;
00051     unpackSubImageSupported = false;
00052     framebufferObjectMultisampleSupported = false;
00053     OESDepth24 = false;
00054     multisampleMaxSamples = -1;
00055 }
00056 
00057 void Context::initialize(ContextType contextType) {
00058     // Initialize default state.
00059     reset();
00060 
00061     type = contextType;
00062 
00063     // Obtain maximum texture size.
00064     glGetIntegerv(GL_MAX_TEXTURE_SIZE, (GLint *)&maxTextureSize);
00065     debug(5, "OpenGL maximum texture size: %d", maxTextureSize);
00066 
00067     const char *extString = (const char *)glGetString(GL_EXTENSIONS);
00068 
00069     bool ARBShaderObjects = false;
00070     bool ARBShadingLanguage100 = false;
00071     bool ARBVertexShader = false;
00072     bool ARBFragmentShader = false;
00073     bool EXTFramebufferMultisample = false;
00074     bool EXTFramebufferBlit = false;
00075 
00076     Common::StringTokenizer tokenizer(extString, " ");
00077     while (!tokenizer.empty()) {
00078         Common::String token = tokenizer.nextToken();
00079 
00080         if (token == "GL_ARB_texture_non_power_of_two" || token == "GL_OES_texture_npot") {
00081             NPOTSupported = true;
00082         } else if (token == "GL_ARB_shader_objects") {
00083             ARBShaderObjects = true;
00084         } else if (token == "GL_ARB_shading_language_100") {
00085             ARBShadingLanguage100 = true;
00086         } else if (token == "GL_ARB_vertex_shader") {
00087             ARBVertexShader = true;
00088         } else if (token == "GL_ARB_fragment_shader") {
00089             ARBFragmentShader = true;
00090         } else if (token == "GL_EXT_framebuffer_object") {
00091             framebufferObjectSupported = true;
00092         } else if (token == "GL_EXT_packed_depth_stencil" || token == "GL_OES_packed_depth_stencil") {
00093             packedDepthStencilSupported = true;
00094         } else if (token == "GL_EXT_unpack_subimage") {
00095             unpackSubImageSupported = true;
00096         } else if (token == "GL_EXT_framebuffer_multisample") {
00097             EXTFramebufferMultisample = true;
00098         } else if (token == "GL_EXT_framebuffer_blit") {
00099             EXTFramebufferBlit = true;
00100         } else if (token == "GL_OES_depth24") {
00101             OESDepth24 = true;
00102         }
00103         
00104     }
00105 
00106     int glslVersion = getGLSLVersion();
00107     debug(5, "OpenGL GLSL version: %d", glslVersion);
00108 
00109     if (type == kContextGLES2) {
00110         // GLES2 always has (limited) NPOT support.
00111         NPOTSupported = true;
00112 
00113         // GLES2 always has shader support.
00114         shadersSupported = true;
00115 
00116         // GLES2 always has FBO support.
00117         framebufferObjectSupported = true;
00118 
00119         // ResidualVM does not support multisample FBOs with GLES2 for now
00120         framebufferObjectMultisampleSupported = false;
00121         multisampleMaxSamples = -1;
00122     } else {
00123         shadersSupported = ARBShaderObjects && ARBShadingLanguage100 && ARBVertexShader && ARBFragmentShader && glslVersion >= 120;
00124 
00125         // Desktop GL always has unpack sub-image support
00126         unpackSubImageSupported = true;
00127 
00128         framebufferObjectMultisampleSupported = EXTFramebufferMultisample && EXTFramebufferBlit;
00129 
00130         if (framebufferObjectMultisampleSupported) {
00131             glGetIntegerv(GL_MAX_SAMPLES, (GLint *)&multisampleMaxSamples);
00132         }
00133     }
00134 
00135     // Log context type.
00136     switch (type) {
00137         case kContextGL:
00138             debug(5, "OpenGL: GL context initialized");
00139             break;
00140 
00141         case kContextGLES2:
00142             debug(5, "OpenGL: GLES2 context initialized");
00143             break;
00144     }
00145 
00146     // Log features supported by GL context.
00147     debug(5, "OpenGL: NPOT texture support: %d", NPOTSupported);
00148     debug(5, "OpenGL: Shader support: %d", shadersSupported);
00149     debug(5, "OpenGL: FBO support: %d", framebufferObjectSupported);
00150     debug(5, "OpenGL: Packed depth stencil support: %d", packedDepthStencilSupported);
00151     debug(5, "OpenGL: Unpack subimage support: %d", unpackSubImageSupported);
00152 }
00153 
00154 int Context::getGLSLVersion() const {
00155     const char *glslVersionString = (const char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
00156     if (!glslVersionString) {
00157         warning("Could not get GLSL version");
00158         return 0;
00159     }
00160 
00161     const char *glslVersionFormat;
00162     if (type == kContextGL) {
00163         glslVersionFormat = "%d.%d";
00164     } else {
00165         glslVersionFormat = "OpenGL ES GLSL ES %d.%d";
00166     }
00167 
00168     int glslMajorVersion, glslMinorVersion;
00169     if (sscanf(glslVersionString, glslVersionFormat, &glslMajorVersion, &glslMinorVersion) != 2) {
00170         warning("Could not parse GLSL version '%s'", glslVersionString);
00171         return 0;
00172     }
00173 
00174     return glslMajorVersion * 100 + glslMinorVersion;
00175 }
00176 
00177 } // End of namespace OpenGL
00178 
00179 #endif


Generated on Sat May 25 2019 05:00:42 for ResidualVM by doxygen 1.7.1
curved edge   curved edge