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 " : "");
202 ctx->Extensions.Version = ctx->Version;
207 * Override the context's GLSL version if the environment variable
208 * MESA_GLSL_VERSION_OVERRIDE is set. Valid values for
209 * MESA_GLSL_VERSION_OVERRIDE are integers, such as "130".
212 _mesa_override_glsl_version(struct gl_constants *consts)
214 const char *env_var = "MESA_GLSL_VERSION_OVERRIDE";
218 version = getenv(env_var);
223 n = sscanf(version, "%u", &consts->GLSLVersion);
225 fprintf(stderr, "error: invalid value for %s: %s\n", env_var, version);
231 * Examine enabled GL extensions to determine GL version.
234 compute_version(const struct gl_extensions *extensions,
235 const struct gl_constants *consts, gl_api api)
237 GLuint major, minor, version;
239 const bool ver_1_3 = (extensions->ARB_texture_border_clamp &&
240 extensions->ARB_texture_cube_map &&
241 extensions->ARB_texture_env_combine &&
242 extensions->ARB_texture_env_dot3);
243 const bool ver_1_4 = (ver_1_3 &&
244 extensions->ARB_depth_texture &&
245 extensions->ARB_shadow &&
246 extensions->ARB_texture_env_crossbar &&
247 extensions->EXT_blend_color &&
248 extensions->EXT_blend_func_separate &&
249 extensions->EXT_blend_minmax &&
250 extensions->EXT_point_parameters);
251 const bool ver_1_5 = (ver_1_4 &&
252 extensions->ARB_occlusion_query);
253 const bool ver_2_0 = (ver_1_5 &&
254 extensions->ARB_point_sprite &&
255 extensions->ARB_vertex_shader &&
256 extensions->ARB_fragment_shader &&
257 extensions->ARB_texture_non_power_of_two &&
258 extensions->EXT_blend_equation_separate &&
260 /* Technically, 2.0 requires the functionality of the
261 * EXT version. Enable 2.0 if either extension is
262 * available, and assume that a driver that only
263 * exposes the ATI extension will fallback to
264 * software when necessary.
266 (extensions->EXT_stencil_two_side
267 || extensions->ATI_separate_stencil));
268 const bool ver_2_1 = (ver_2_0 &&
269 extensions->EXT_pixel_buffer_object &&
270 extensions->EXT_texture_sRGB);
271 const bool ver_3_0 = (ver_2_1 &&
272 consts->GLSLVersion >= 130 &&
273 (consts->MaxSamples >= 4 || consts->FakeSWMSAA) &&
274 (api == API_OPENGL_CORE ||
275 extensions->ARB_color_buffer_float) &&
276 extensions->ARB_depth_buffer_float &&
277 extensions->ARB_half_float_vertex &&
278 extensions->ARB_map_buffer_range &&
279 extensions->ARB_shader_texture_lod &&
280 extensions->ARB_texture_float &&
281 extensions->ARB_texture_rg &&
282 extensions->ARB_texture_compression_rgtc &&
283 extensions->EXT_draw_buffers2 &&
284 extensions->ARB_framebuffer_object &&
285 extensions->EXT_framebuffer_sRGB &&
286 extensions->EXT_packed_float &&
287 extensions->EXT_texture_array &&
288 extensions->EXT_texture_shared_exponent &&
289 extensions->EXT_transform_feedback &&
290 extensions->NV_conditional_render);
291 const bool ver_3_1 = (ver_3_0 &&
292 consts->GLSLVersion >= 140 &&
293 extensions->ARB_draw_instanced &&
294 extensions->ARB_texture_buffer_object &&
295 extensions->ARB_uniform_buffer_object &&
296 extensions->EXT_texture_snorm &&
297 extensions->NV_primitive_restart &&
298 extensions->NV_texture_rectangle &&
299 consts->Program[MESA_SHADER_VERTEX].MaxTextureImageUnits >= 16);
300 const bool ver_3_2 = (ver_3_1 &&
301 consts->GLSLVersion >= 150 &&
302 extensions->ARB_depth_clamp &&
303 extensions->ARB_draw_elements_base_vertex &&
304 extensions->ARB_fragment_coord_conventions &&
305 extensions->EXT_provoking_vertex &&
306 extensions->ARB_seamless_cube_map &&
307 extensions->ARB_sync &&
308 extensions->ARB_texture_multisample &&
309 extensions->EXT_vertex_array_bgra);
310 const bool ver_3_3 = (ver_3_2 &&
311 consts->GLSLVersion >= 330 &&
312 extensions->ARB_blend_func_extended &&
313 extensions->ARB_explicit_attrib_location &&
314 extensions->ARB_instanced_arrays &&
315 extensions->ARB_occlusion_query2 &&
316 extensions->ARB_shader_bit_encoding &&
317 extensions->ARB_texture_rgb10_a2ui &&
318 extensions->ARB_timer_query &&
319 extensions->ARB_vertex_type_2_10_10_10_rev &&
320 extensions->EXT_texture_swizzle);
321 /* ARB_sampler_objects is always enabled in mesa */
323 const bool ver_4_0 = (ver_3_3 &&
324 consts->GLSLVersion >= 400 &&
325 extensions->ARB_draw_buffers_blend &&
326 extensions->ARB_draw_indirect &&
327 extensions->ARB_gpu_shader5 &&
328 extensions->ARB_gpu_shader_fp64 &&
329 extensions->ARB_sample_shading &&
330 extensions->ARB_shader_subroutine &&
331 extensions->ARB_tessellation_shader &&
332 extensions->ARB_texture_buffer_object_rgb32 &&
333 extensions->ARB_texture_cube_map_array &&
334 extensions->ARB_texture_query_lod &&
335 extensions->ARB_transform_feedback2 &&
336 extensions->ARB_transform_feedback3);
337 const bool ver_4_1 = (ver_4_0 &&
338 consts->GLSLVersion >= 410 &&
339 extensions->ARB_ES2_compatibility &&
340 extensions->ARB_shader_precision &&
341 extensions->ARB_vertex_attrib_64bit &&
342 extensions->ARB_viewport_array);
343 const bool ver_4_2 = (ver_4_1 &&
344 consts->GLSLVersion >= 420 &&
345 extensions->ARB_base_instance &&
346 extensions->ARB_conservative_depth &&
347 extensions->ARB_internalformat_query &&
348 extensions->ARB_shader_atomic_counters &&
349 extensions->ARB_shader_image_load_store &&
350 extensions->ARB_shading_language_420pack &&
351 extensions->ARB_shading_language_packing &&
352 extensions->ARB_texture_compression_bptc &&
353 extensions->ARB_transform_feedback_instanced);
408 version = major * 10 + minor;
410 if (api == API_OPENGL_CORE && version < 31)
417 compute_version_es1(const struct gl_extensions *extensions)
419 /* OpenGL ES 1.0 is derived from OpenGL 1.3 */
420 const bool ver_1_0 = (extensions->ARB_texture_env_combine &&
421 extensions->ARB_texture_env_dot3);
422 /* OpenGL ES 1.1 is derived from OpenGL 1.5 */
423 const bool ver_1_1 = (ver_1_0 &&
424 extensions->EXT_point_parameters);
428 } else if (ver_1_0) {
436 compute_version_es2(const struct gl_extensions *extensions)
438 /* OpenGL ES 2.0 is derived from OpenGL 2.0 */
439 const bool ver_2_0 = (extensions->ARB_texture_cube_map &&
440 extensions->EXT_blend_color &&
441 extensions->EXT_blend_func_separate &&
442 extensions->EXT_blend_minmax &&
443 extensions->ARB_vertex_shader &&
444 extensions->ARB_fragment_shader &&
445 extensions->ARB_texture_non_power_of_two &&
446 extensions->EXT_blend_equation_separate);
447 /* FINISHME: This list isn't quite right. */
448 const bool ver_3_0 = (extensions->ARB_half_float_vertex &&
449 extensions->ARB_internalformat_query &&
450 extensions->ARB_map_buffer_range &&
451 extensions->ARB_shader_texture_lod &&
452 extensions->ARB_texture_float &&
453 extensions->ARB_texture_rg &&
454 extensions->ARB_depth_buffer_float &&
455 extensions->EXT_draw_buffers2 &&
456 /* extensions->ARB_framebuffer_object && */
457 extensions->EXT_framebuffer_sRGB &&
458 extensions->EXT_packed_float &&
459 extensions->EXT_texture_array &&
460 extensions->EXT_texture_shared_exponent &&
461 extensions->EXT_transform_feedback &&
462 extensions->ARB_draw_instanced &&
463 extensions->ARB_uniform_buffer_object &&
464 extensions->EXT_texture_snorm &&
465 extensions->NV_primitive_restart &&
466 extensions->OES_depth_texture_cube_map);
467 const bool ver_3_1 = (ver_3_0 &&
468 extensions->ARB_arrays_of_arrays &&
469 extensions->ARB_compute_shader &&
470 extensions->ARB_draw_indirect &&
471 extensions->ARB_explicit_uniform_location &&
472 extensions->ARB_framebuffer_no_attachments &&
473 extensions->ARB_shader_atomic_counters &&
474 extensions->ARB_shader_image_load_store &&
475 extensions->ARB_shader_image_size &&
476 extensions->ARB_shader_storage_buffer_object &&
477 extensions->ARB_shading_language_packing &&
478 extensions->ARB_stencil_texturing &&
479 extensions->ARB_texture_multisample &&
480 extensions->ARB_gpu_shader5 &&
481 extensions->EXT_shader_integer_mix);
485 } else if (ver_3_0) {
487 } else if (ver_2_0) {
495 _mesa_get_version(const struct gl_extensions *extensions,
496 struct gl_constants *consts, gl_api api)
499 case API_OPENGL_COMPAT:
500 /* Disable GLSL 1.40 and later for legacy contexts.
501 * This disallows creation of the GL 3.1 compatibility context. */
502 if (consts->GLSLVersion > 130) {
503 consts->GLSLVersion = 130;
506 case API_OPENGL_CORE:
507 return compute_version(extensions, consts, api);
509 return compute_version_es1(extensions);
511 return compute_version_es2(extensions);
517 * Set the context's Version and VersionString fields.
518 * This should only be called once as part of context initialization
519 * or to perform version check for GLX_ARB_create_context_profile.
522 _mesa_compute_version(struct gl_context *ctx)
527 ctx->Version = _mesa_get_version(&ctx->Extensions, &ctx->Const, ctx->API);
528 ctx->Extensions.Version = ctx->Version;
530 /* Make sure that the GLSL version lines up with the GL version. In some
531 * cases it can be too high, e.g. if an extension is missing.
533 if (ctx->API == API_OPENGL_CORE) {
534 switch (ctx->Version) {
536 ctx->Const.GLSLVersion = 140;
539 ctx->Const.GLSLVersion = 150;
542 ctx->Const.GLSLVersion = ctx->Version * 10;
548 case API_OPENGL_COMPAT:
549 case API_OPENGL_CORE:
550 create_version_string(ctx, "");
555 _mesa_problem(ctx, "Incomplete OpenGL ES 1.0 support.");
558 create_version_string(ctx, "OpenGL ES-CM ");
563 _mesa_problem(ctx, "Incomplete OpenGL ES 2.0 support.");
566 create_version_string(ctx, "OpenGL ES ");