2 * Mesa 3-D graphics library
4 * Copyright (C) 2010 VMware, Inc. All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
34 * Scans 'string' to see if it ends with 'ending'.
37 check_for_ending(const char *string, const char *ending)
39 const size_t len1 = strlen(string);
40 const size_t len2 = strlen(ending);
45 return strcmp(string + (len1 - len2), ending) == 0;
49 * Returns the gl override data
51 * version > 0 indicates there is an override requested
52 * fwd_context is only valid if version > 0
55 get_gl_override(gl_api api, int *version, bool *fwd_context,
58 const char *env_var = (api == API_OPENGL_CORE || api == API_OPENGL_COMPAT)
59 ? "MESA_GL_VERSION_OVERRIDE" : "MESA_GLES_VERSION_OVERRIDE";
60 const char *version_str;
62 static struct override_info {
73 STATIC_ASSERT(ARRAY_SIZE(override) == API_OPENGL_LAST + 1);
75 if (api == API_OPENGLES)
78 if (override[api].version < 0) {
79 override[api].version = 0;
81 version_str = getenv(env_var);
83 override[api].fc_suffix = check_for_ending(version_str, "FC");
84 override[api].compat_suffix = check_for_ending(version_str, "COMPAT");
86 n = sscanf(version_str, "%u.%u", &major, &minor);
88 fprintf(stderr, "error: invalid value for %s: %s\n",
89 env_var, version_str);
90 override[api].version = 0;
92 override[api].version = major * 10 + minor;
94 /* There is no such thing as compatibility or forward-compatible for
95 * OpenGL ES 2.0 or 3.x APIs.
97 if ((override[api].version < 30 && override[api].fc_suffix) ||
98 (api == API_OPENGLES2 && (override[api].fc_suffix ||
99 override[api].compat_suffix))) {
100 fprintf(stderr, "error: invalid value for %s: %s\n",
101 env_var, version_str);
108 *version = override[api].version;
109 *fwd_context = override[api].fc_suffix;
110 *compat_context = override[api].compat_suffix;
114 * Builds the MESA version string.
117 create_version_string(struct gl_context *ctx, const char *prefix)
119 static const int max = 100;
121 ctx->VersionString = malloc(max);
122 if (ctx->VersionString) {
123 _mesa_snprintf(ctx->VersionString, max,
124 "%s%u.%u%s Mesa " PACKAGE_VERSION
126 " (" MESA_GIT_SHA1 ")"
130 ctx->Version / 10, ctx->Version % 10,
131 (ctx->API == API_OPENGL_CORE) ? " (Core Profile)" : ""
137 * Override the context's version and/or API type if the
138 * environment variable MESA_GL_VERSION_OVERRIDE is set.
140 * Example uses of MESA_GL_VERSION_OVERRIDE:
142 * 2.1: select a compatibility (non-Core) profile with GL version 2.1
143 * 3.0: select a compatibility (non-Core) profile with GL version 3.0
144 * 3.0FC: select a Core+Forward Compatible profile with GL version 3.0
145 * 3.1: select a Core profile with GL version 3.1
146 * 3.1FC: select a Core+Forward Compatible profile with GL version 3.1
149 _mesa_override_gl_version_contextless(struct gl_constants *consts,
150 gl_api *apiOut, GLuint *versionOut)
153 bool fwd_context, compat_context;
155 get_gl_override(*apiOut, &version, &fwd_context, &compat_context);
158 *versionOut = version;
160 /* If the API is a desktop API, adjust the context flags. We may also
161 * need to modify the API depending on the version. For example, Mesa
162 * does not support a GL 3.3 compatibility profile.
164 if (*apiOut == API_OPENGL_CORE || *apiOut == API_OPENGL_COMPAT) {
165 if (version >= 30 && fwd_context) {
166 *apiOut = API_OPENGL_CORE;
167 consts->ContextFlags |= GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT;
168 } else if (version >= 31 && !compat_context) {
169 *apiOut = API_OPENGL_CORE;
171 *apiOut = API_OPENGL_COMPAT;
181 _mesa_override_gl_version(struct gl_context *ctx)
183 if (_mesa_override_gl_version_contextless(&ctx->Const, &ctx->API,
185 /* We need to include API in version string for OpenGL ES, otherwise
186 * application can not detect GLES via glGetString(GL_VERSION) query.
188 * From OpenGL ES 3.2 spec, Page 436:
190 * "The VERSION string is laid out as follows:
192 * OpenGL ES N.M vendor-specific information"
194 * From OpenGL 4.5 spec, Page 538:
196 * "The VERSION and SHADING_LANGUAGE_VERSION strings are laid out as
199 * <version number><space><vendor-specific information>"
201 create_version_string(ctx, _mesa_is_gles(ctx) ? "OpenGL ES " : "");
206 * Override the context's GLSL version if the environment variable
207 * MESA_GLSL_VERSION_OVERRIDE is set. Valid values for
208 * MESA_GLSL_VERSION_OVERRIDE are integers, such as "130".
211 _mesa_override_glsl_version(struct gl_constants *consts)
213 const char *env_var = "MESA_GLSL_VERSION_OVERRIDE";
217 version = getenv(env_var);
222 n = sscanf(version, "%u", &consts->GLSLVersion);
224 fprintf(stderr, "error: invalid value for %s: %s\n", env_var, version);
230 * Examine enabled GL extensions to determine GL version.
233 compute_version(const struct gl_extensions *extensions,
234 const struct gl_constants *consts, gl_api api)
236 GLuint major, minor, version;
238 const bool ver_1_3 = (extensions->ARB_texture_border_clamp &&
239 extensions->ARB_texture_cube_map &&
240 extensions->ARB_texture_env_combine &&
241 extensions->ARB_texture_env_dot3);
242 const bool ver_1_4 = (ver_1_3 &&
243 extensions->ARB_depth_texture &&
244 extensions->ARB_shadow &&
245 extensions->ARB_texture_env_crossbar &&
246 extensions->EXT_blend_color &&
247 extensions->EXT_blend_func_separate &&
248 extensions->EXT_blend_minmax &&
249 extensions->EXT_point_parameters);
250 const bool ver_1_5 = (ver_1_4 &&
251 extensions->ARB_occlusion_query);
252 const bool ver_2_0 = (ver_1_5 &&
253 extensions->ARB_point_sprite &&
254 extensions->ARB_vertex_shader &&
255 extensions->ARB_fragment_shader &&
256 extensions->ARB_texture_non_power_of_two &&
257 extensions->EXT_blend_equation_separate &&
259 /* Technically, 2.0 requires the functionality of the
260 * EXT version. Enable 2.0 if either extension is
261 * available, and assume that a driver that only
262 * exposes the ATI extension will fallback to
263 * software when necessary.
265 (extensions->EXT_stencil_two_side
266 || extensions->ATI_separate_stencil));
267 const bool ver_2_1 = (ver_2_0 &&
268 extensions->EXT_pixel_buffer_object &&
269 extensions->EXT_texture_sRGB);
270 const bool ver_3_0 = (ver_2_1 &&
271 consts->GLSLVersion >= 130 &&
272 (consts->MaxSamples >= 4 || consts->FakeSWMSAA) &&
273 (api == API_OPENGL_CORE ||
274 extensions->ARB_color_buffer_float) &&
275 extensions->ARB_depth_buffer_float &&
276 extensions->ARB_half_float_vertex &&
277 extensions->ARB_map_buffer_range &&
278 extensions->ARB_shader_texture_lod &&
279 extensions->ARB_texture_float &&
280 extensions->ARB_texture_rg &&
281 extensions->ARB_texture_compression_rgtc &&
282 extensions->EXT_draw_buffers2 &&
283 extensions->ARB_framebuffer_object &&
284 extensions->EXT_framebuffer_sRGB &&
285 extensions->EXT_packed_float &&
286 extensions->EXT_texture_array &&
287 extensions->EXT_texture_shared_exponent &&
288 extensions->EXT_transform_feedback &&
289 extensions->NV_conditional_render);
290 const bool ver_3_1 = (ver_3_0 &&
291 consts->GLSLVersion >= 140 &&
292 extensions->ARB_draw_instanced &&
293 extensions->ARB_texture_buffer_object &&
294 extensions->ARB_uniform_buffer_object &&
295 extensions->EXT_texture_snorm &&
296 extensions->NV_primitive_restart &&
297 extensions->NV_texture_rectangle &&
298 consts->Program[MESA_SHADER_VERTEX].MaxTextureImageUnits >= 16);
299 const bool ver_3_2 = (ver_3_1 &&
300 consts->GLSLVersion >= 150 &&
301 extensions->ARB_depth_clamp &&
302 extensions->ARB_draw_elements_base_vertex &&
303 extensions->ARB_fragment_coord_conventions &&
304 extensions->EXT_provoking_vertex &&
305 extensions->ARB_seamless_cube_map &&
306 extensions->ARB_sync &&
307 extensions->ARB_texture_multisample &&
308 extensions->EXT_vertex_array_bgra);
309 const bool ver_3_3 = (ver_3_2 &&
310 consts->GLSLVersion >= 330 &&
311 extensions->ARB_blend_func_extended &&
312 extensions->ARB_explicit_attrib_location &&
313 extensions->ARB_instanced_arrays &&
314 extensions->ARB_occlusion_query2 &&
315 extensions->ARB_shader_bit_encoding &&
316 extensions->ARB_texture_rgb10_a2ui &&
317 extensions->ARB_timer_query &&
318 extensions->ARB_vertex_type_2_10_10_10_rev &&
319 extensions->EXT_texture_swizzle);
320 /* ARB_sampler_objects is always enabled in mesa */
322 const bool ver_4_0 = (ver_3_3 &&
323 consts->GLSLVersion >= 400 &&
324 extensions->ARB_draw_buffers_blend &&
325 extensions->ARB_draw_indirect &&
326 extensions->ARB_gpu_shader5 &&
327 extensions->ARB_gpu_shader_fp64 &&
328 extensions->ARB_sample_shading &&
329 extensions->ARB_shader_subroutine &&
330 extensions->ARB_tessellation_shader &&
331 extensions->ARB_texture_buffer_object_rgb32 &&
332 extensions->ARB_texture_cube_map_array &&
333 extensions->ARB_texture_query_lod &&
334 extensions->ARB_transform_feedback2 &&
335 extensions->ARB_transform_feedback3);
336 const bool ver_4_1 = (ver_4_0 &&
337 consts->GLSLVersion >= 410 &&
338 extensions->ARB_ES2_compatibility &&
339 extensions->ARB_shader_precision &&
340 extensions->ARB_vertex_attrib_64bit &&
341 extensions->ARB_viewport_array);
342 const bool ver_4_2 = (ver_4_1 &&
343 consts->GLSLVersion >= 420 &&
344 extensions->ARB_base_instance &&
345 extensions->ARB_conservative_depth &&
346 extensions->ARB_internalformat_query &&
347 extensions->ARB_shader_atomic_counters &&
348 extensions->ARB_shader_image_load_store &&
349 extensions->ARB_shading_language_420pack &&
350 extensions->ARB_shading_language_packing &&
351 extensions->ARB_texture_compression_bptc &&
352 extensions->ARB_transform_feedback_instanced);
407 version = major * 10 + minor;
409 if (api == API_OPENGL_CORE && version < 31)
416 compute_version_es1(const struct gl_extensions *extensions)
418 /* OpenGL ES 1.0 is derived from OpenGL 1.3 */
419 const bool ver_1_0 = (extensions->ARB_texture_env_combine &&
420 extensions->ARB_texture_env_dot3);
421 /* OpenGL ES 1.1 is derived from OpenGL 1.5 */
422 const bool ver_1_1 = (ver_1_0 &&
423 extensions->EXT_point_parameters);
427 } else if (ver_1_0) {
435 compute_version_es2(const struct gl_extensions *extensions)
437 /* OpenGL ES 2.0 is derived from OpenGL 2.0 */
438 const bool ver_2_0 = (extensions->ARB_texture_cube_map &&
439 extensions->EXT_blend_color &&
440 extensions->EXT_blend_func_separate &&
441 extensions->EXT_blend_minmax &&
442 extensions->ARB_vertex_shader &&
443 extensions->ARB_fragment_shader &&
444 extensions->ARB_texture_non_power_of_two &&
445 extensions->EXT_blend_equation_separate);
446 /* FINISHME: This list isn't quite right. */
447 const bool ver_3_0 = (extensions->ARB_half_float_vertex &&
448 extensions->ARB_internalformat_query &&
449 extensions->ARB_map_buffer_range &&
450 extensions->ARB_shader_texture_lod &&
451 extensions->ARB_texture_float &&
452 extensions->ARB_texture_rg &&
453 extensions->ARB_depth_buffer_float &&
454 extensions->EXT_draw_buffers2 &&
455 /* extensions->ARB_framebuffer_object && */
456 extensions->EXT_framebuffer_sRGB &&
457 extensions->EXT_packed_float &&
458 extensions->EXT_texture_array &&
459 extensions->EXT_texture_shared_exponent &&
460 extensions->EXT_transform_feedback &&
461 extensions->ARB_draw_instanced &&
462 extensions->ARB_uniform_buffer_object &&
463 extensions->EXT_texture_snorm &&
464 extensions->NV_primitive_restart &&
465 extensions->OES_depth_texture_cube_map);
466 const bool ver_3_1 = (ver_3_0 &&
467 extensions->ARB_arrays_of_arrays &&
468 extensions->ARB_compute_shader &&
469 extensions->ARB_draw_indirect &&
470 extensions->ARB_explicit_uniform_location &&
471 extensions->ARB_framebuffer_no_attachments &&
472 extensions->ARB_shader_atomic_counters &&
473 extensions->ARB_shader_image_load_store &&
474 extensions->ARB_shader_image_size &&
475 extensions->ARB_shader_storage_buffer_object &&
476 extensions->ARB_shading_language_packing &&
477 extensions->ARB_stencil_texturing &&
478 extensions->ARB_texture_multisample &&
479 extensions->ARB_gpu_shader5 &&
480 extensions->EXT_shader_integer_mix);
484 } else if (ver_3_0) {
486 } else if (ver_2_0) {
494 _mesa_get_version(const struct gl_extensions *extensions,
495 struct gl_constants *consts, gl_api api)
498 case API_OPENGL_COMPAT:
499 /* Disable GLSL 1.40 and later for legacy contexts.
500 * This disallows creation of the GL 3.1 compatibility context. */
501 if (consts->GLSLVersion > 130) {
502 consts->GLSLVersion = 130;
505 case API_OPENGL_CORE:
506 return compute_version(extensions, consts, api);
508 return compute_version_es1(extensions);
510 return compute_version_es2(extensions);
516 * Set the context's Version and VersionString fields.
517 * This should only be called once as part of context initialization
518 * or to perform version check for GLX_ARB_create_context_profile.
521 _mesa_compute_version(struct gl_context *ctx)
526 ctx->Version = _mesa_get_version(&ctx->Extensions, &ctx->Const, ctx->API);
527 ctx->Extensions.Version = ctx->Version;
529 /* Make sure that the GLSL version lines up with the GL version. In some
530 * cases it can be too high, e.g. if an extension is missing.
532 if (ctx->API == API_OPENGL_CORE) {
533 switch (ctx->Version) {
535 ctx->Const.GLSLVersion = 140;
538 ctx->Const.GLSLVersion = 150;
541 ctx->Const.GLSLVersion = ctx->Version * 10;
547 case API_OPENGL_COMPAT:
548 case API_OPENGL_CORE:
549 create_version_string(ctx, "");
554 _mesa_problem(ctx, "Incomplete OpenGL ES 1.0 support.");
557 create_version_string(ctx, "OpenGL ES-CM ");
562 _mesa_problem(ctx, "Incomplete OpenGL ES 2.0 support.");
565 create_version_string(ctx, "OpenGL ES ");