4 * An object oriented GL/GLES Abstraction/Utility Layer
6 * Copyright (C) 2007,2008,2009 Intel Corporation.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
32 #include "cogl-internal.h"
33 #include "cogl-context.h"
34 #include "cogl-feature-private.h"
36 #ifdef HAVE_CLUTTER_OSX
38 really_enable_npot (void)
40 /* OSX backend + ATI Radeon X1600 + NPOT texture + GL_REPEAT seems to crash
41 * http://bugzilla.openedhand.com/show_bug.cgi?id=929
43 * Temporary workaround until post 0.8 we rejig the features set up a
44 * little to allow the backend to overide.
46 const char *gl_renderer;
47 const char *env_string;
49 /* Regardless of hardware, allow user to decide. */
50 env_string = g_getenv ("COGL_ENABLE_NPOT");
51 if (env_string != NULL)
52 return env_string[0] == '1';
54 gl_renderer = (char*)glGetString (GL_RENDERER);
55 if (strstr (gl_renderer, "ATI Radeon X1600") != NULL)
63 _cogl_get_gl_version (int *major_out, int *minor_out)
65 const char *version_string, *major_end, *minor_end;
66 int major = 0, minor = 0;
68 /* Get the OpenGL version number */
69 if ((version_string = (const char *) glGetString (GL_VERSION)) == NULL)
72 /* Extract the major number */
73 for (major_end = version_string; *major_end >= '0'
74 && *major_end <= '9'; major_end++)
75 major = (major * 10) + *major_end - '0';
76 /* If there were no digits or the major number isn't followed by a
77 dot then it is invalid */
78 if (major_end == version_string || *major_end != '.')
81 /* Extract the minor number */
82 for (minor_end = major_end + 1; *minor_end >= '0'
83 && *minor_end <= '9'; minor_end++)
84 minor = (minor * 10) + *minor_end - '0';
85 /* If there were no digits or there is an unexpected character then
87 if (minor_end == major_end + 1
88 || (*minor_end && *minor_end != ' ' && *minor_end != '.'))
98 _cogl_check_driver_valid (GError **error)
101 const char *gl_extensions;
103 if (!_cogl_get_gl_version (&major, &minor))
107 COGL_DRIVER_ERROR_UNKNOWN_VERSION,
108 "The OpenGL version could not be determined");
112 /* GL 1.3 supports all of the required functionality in core */
113 if (COGL_CHECK_GL_VERSION (major, minor, 1, 3))
116 gl_extensions = (const char*) glGetString (GL_EXTENSIONS);
118 /* OpenGL 1.2 is only supported if we have the multitexturing
120 if (!_cogl_check_extension ("GL_ARB_multitexture", gl_extensions))
124 COGL_DRIVER_ERROR_INVALID_VERSION,
125 "The OpenGL driver is missing "
126 "the GL_ARB_multitexture extension");
130 /* OpenGL 1.2 is required */
131 if (!COGL_CHECK_GL_VERSION (major, minor, 1, 2))
135 COGL_DRIVER_ERROR_INVALID_VERSION,
136 "The OpenGL version of your driver (%i.%i) "
137 "is not compatible with Cogl",
145 /* Define a set of arrays containing the functions required from GL
147 #define COGL_FEATURE_BEGIN(name, min_gl_major, min_gl_minor, \
148 namespaces, extension_names, \
149 feature_flags, feature_flags_private) \
150 static const CoglFeatureFunction cogl_feature_ ## name ## _funcs[] = {
151 #define COGL_FEATURE_FUNCTION(ret, name, args) \
152 { G_STRINGIFY (name), G_STRUCT_OFFSET (CoglContext, drv.pf_ ## name) },
153 #define COGL_FEATURE_END() \
156 #include "cogl-feature-functions-gl.h"
158 /* Define an array of features */
159 #undef COGL_FEATURE_BEGIN
160 #define COGL_FEATURE_BEGIN(name, min_gl_major, min_gl_minor, \
161 namespaces, extension_names, \
162 feature_flags, feature_flags_private) \
163 { min_gl_major, min_gl_minor, namespaces, \
164 extension_names, feature_flags, feature_flags_private, \
165 cogl_feature_ ## name ## _funcs },
166 #undef COGL_FEATURE_FUNCTION
167 #define COGL_FEATURE_FUNCTION(ret, name, args)
168 #undef COGL_FEATURE_END
169 #define COGL_FEATURE_END()
171 static const CoglFeatureData cogl_feature_data[] =
173 #include "cogl-feature-functions-gl.h"
177 _cogl_features_init (void)
179 CoglFeatureFlags flags = 0;
180 CoglFeatureFlagsPrivate flags_private = 0;
181 const char *gl_extensions;
182 GLint max_clip_planes = 0;
183 GLint num_stencil_bits = 0;
184 int gl_major = 0, gl_minor = 0;
187 _COGL_GET_CONTEXT (ctx, NO_RETVAL);
189 _cogl_get_gl_version (&gl_major, &gl_minor);
191 flags = (COGL_FEATURE_TEXTURE_READ_PIXELS
192 | COGL_FEATURE_UNSIGNED_INT_INDICES);
194 gl_extensions = (const char*) glGetString (GL_EXTENSIONS);
196 if (COGL_CHECK_GL_VERSION (gl_major, gl_minor, 2, 0) ||
197 _cogl_check_extension ("GL_ARB_texture_non_power_of_two", gl_extensions))
199 #ifdef HAVE_CLUTTER_OSX
200 if (really_enable_npot ())
202 flags |= COGL_FEATURE_TEXTURE_NPOT
203 | COGL_FEATURE_TEXTURE_NPOT_BASIC
204 | COGL_FEATURE_TEXTURE_NPOT_MIPMAP
205 | COGL_FEATURE_TEXTURE_NPOT_REPEAT;
209 if (_cogl_check_extension ("GL_MESA_ycbcr_texture", gl_extensions))
211 flags |= COGL_FEATURE_TEXTURE_YUV;
215 GE( glGetIntegerv (GL_STENCIL_BITS, &num_stencil_bits) );
216 /* We need at least three stencil bits to combine clips */
217 if (num_stencil_bits > 2)
218 flags |= COGL_FEATURE_STENCIL_BUFFER;
220 GE( glGetIntegerv (GL_MAX_CLIP_PLANES, &max_clip_planes) );
221 if (max_clip_planes >= 4)
222 flags |= COGL_FEATURE_FOUR_CLIP_PLANES;
224 for (i = 0; i < G_N_ELEMENTS (cogl_feature_data); i++)
225 if (_cogl_feature_check ("GL", cogl_feature_data + i,
229 flags |= cogl_feature_data[i].feature_flags;
230 flags_private |= cogl_feature_data[i].feature_flags_private;
234 ctx->feature_flags = flags;
235 ctx->feature_flags_private = flags_private;
236 ctx->features_cached = TRUE;