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.
33 * Scans 'string' to see if it ends with 'ending'.
36 check_for_ending(const char *string
, const char *ending
)
38 const size_t len1
= strlen(string
);
39 const size_t len2
= strlen(ending
);
44 return strcmp(string
+ (len1
- len2
), ending
) == 0;
48 * Returns the gl override data
50 * version > 0 indicates there is an override requested
51 * fwd_context is only valid if version > 0
54 get_gl_override(gl_api api
, int *version
, bool *fwd_context
,
57 const char *env_var
= (api
== API_OPENGL_CORE
|| api
== API_OPENGL_COMPAT
)
58 ? "MESA_GL_VERSION_OVERRIDE" : "MESA_GLES_VERSION_OVERRIDE";
59 const char *version_str
;
61 static struct override_info
{
72 STATIC_ASSERT(ARRAY_SIZE(override
) == API_OPENGL_LAST
+ 1);
74 if (api
== API_OPENGLES
)
77 if (override
[api
].version
< 0) {
78 override
[api
].version
= 0;
80 version_str
= getenv(env_var
);
82 override
[api
].fc_suffix
= check_for_ending(version_str
, "FC");
83 override
[api
].compat_suffix
= check_for_ending(version_str
, "COMPAT");
85 n
= sscanf(version_str
, "%u.%u", &major
, &minor
);
87 fprintf(stderr
, "error: invalid value for %s: %s\n",
88 env_var
, version_str
);
89 override
[api
].version
= 0;
91 override
[api
].version
= major
* 10 + minor
;
93 /* There is no such thing as compatibility or forward-compatible for
94 * OpenGL ES 2.0 or 3.x APIs.
96 if ((override
[api
].version
< 30 && override
[api
].fc_suffix
) ||
97 (api
== API_OPENGLES2
&& (override
[api
].fc_suffix
||
98 override
[api
].compat_suffix
))) {
99 fprintf(stderr
, "error: invalid value for %s: %s\n",
100 env_var
, version_str
);
107 *version
= override
[api
].version
;
108 *fwd_context
= override
[api
].fc_suffix
;
109 *compat_context
= override
[api
].compat_suffix
;
113 * Builds the MESA version string.
116 create_version_string(struct gl_context
*ctx
, const char *prefix
)
118 static const int max
= 100;
120 ctx
->VersionString
= malloc(max
);
121 if (ctx
->VersionString
) {
122 _mesa_snprintf(ctx
->VersionString
, max
,
123 "%s%u.%u%s Mesa " PACKAGE_VERSION
125 " (" MESA_GIT_SHA1
")"
129 ctx
->Version
/ 10, ctx
->Version
% 10,
130 (ctx
->API
== API_OPENGL_CORE
) ? " (Core Profile)" : ""
136 * Override the context's version and/or API type if the
137 * environment variable MESA_GL_VERSION_OVERRIDE is set.
139 * Example uses of MESA_GL_VERSION_OVERRIDE:
141 * 2.1: select a compatibility (non-Core) profile with GL version 2.1
142 * 3.0: select a compatibility (non-Core) profile with GL version 3.0
143 * 3.0FC: select a Core+Forward Compatible profile with GL version 3.0
144 * 3.1: select a Core profile with GL version 3.1
145 * 3.1FC: select a Core+Forward Compatible profile with GL version 3.1
148 _mesa_override_gl_version_contextless(struct gl_constants
*consts
,
149 gl_api
*apiOut
, GLuint
*versionOut
)
152 bool fwd_context
, compat_context
;
154 get_gl_override(*apiOut
, &version
, &fwd_context
, &compat_context
);
157 *versionOut
= version
;
159 /* If the API is a desktop API, adjust the context flags. We may also
160 * need to modify the API depending on the version. For example, Mesa
161 * does not support a GL 3.3 compatibility profile.
163 if (*apiOut
== API_OPENGL_CORE
|| *apiOut
== API_OPENGL_COMPAT
) {
164 if (version
>= 30 && fwd_context
) {
165 *apiOut
= API_OPENGL_CORE
;
166 consts
->ContextFlags
|= GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT
;
167 } else if (version
>= 31 && !compat_context
) {
168 *apiOut
= API_OPENGL_CORE
;
170 *apiOut
= API_OPENGL_COMPAT
;
180 _mesa_override_gl_version(struct gl_context
*ctx
)
182 if (_mesa_override_gl_version_contextless(&ctx
->Const
, &ctx
->API
,
184 create_version_string(ctx
, "");
189 * Override the context's GLSL version if the environment variable
190 * MESA_GLSL_VERSION_OVERRIDE is set. Valid values for
191 * MESA_GLSL_VERSION_OVERRIDE are integers, such as "130".
194 _mesa_override_glsl_version(struct gl_constants
*consts
)
196 const char *env_var
= "MESA_GLSL_VERSION_OVERRIDE";
200 version
= getenv(env_var
);
205 n
= sscanf(version
, "%u", &consts
->GLSLVersion
);
207 fprintf(stderr
, "error: invalid value for %s: %s\n", env_var
, version
);
213 * Examine enabled GL extensions to determine GL version.
216 compute_version(const struct gl_extensions
*extensions
,
217 const struct gl_constants
*consts
, gl_api api
)
219 GLuint major
, minor
, version
;
221 const bool ver_1_3
= (extensions
->ARB_texture_border_clamp
&&
222 extensions
->ARB_texture_cube_map
&&
223 extensions
->ARB_texture_env_combine
&&
224 extensions
->ARB_texture_env_dot3
);
225 const bool ver_1_4
= (ver_1_3
&&
226 extensions
->ARB_depth_texture
&&
227 extensions
->ARB_shadow
&&
228 extensions
->ARB_texture_env_crossbar
&&
229 extensions
->EXT_blend_color
&&
230 extensions
->EXT_blend_func_separate
&&
231 extensions
->EXT_blend_minmax
&&
232 extensions
->EXT_point_parameters
);
233 const bool ver_1_5
= (ver_1_4
&&
234 extensions
->ARB_occlusion_query
);
235 const bool ver_2_0
= (ver_1_5
&&
236 extensions
->ARB_point_sprite
&&
237 extensions
->ARB_vertex_shader
&&
238 extensions
->ARB_fragment_shader
&&
239 extensions
->ARB_texture_non_power_of_two
&&
240 extensions
->EXT_blend_equation_separate
&&
242 /* Technically, 2.0 requires the functionality of the
243 * EXT version. Enable 2.0 if either extension is
244 * available, and assume that a driver that only
245 * exposes the ATI extension will fallback to
246 * software when necessary.
248 (extensions
->EXT_stencil_two_side
249 || extensions
->ATI_separate_stencil
));
250 const bool ver_2_1
= (ver_2_0
&&
251 extensions
->EXT_pixel_buffer_object
&&
252 extensions
->EXT_texture_sRGB
);
253 const bool ver_3_0
= (ver_2_1
&&
254 consts
->GLSLVersion
>= 130 &&
255 (consts
->MaxSamples
>= 4 || consts
->FakeSWMSAA
) &&
256 (api
== API_OPENGL_CORE
||
257 extensions
->ARB_color_buffer_float
) &&
258 extensions
->ARB_depth_buffer_float
&&
259 extensions
->ARB_half_float_vertex
&&
260 extensions
->ARB_map_buffer_range
&&
261 extensions
->ARB_shader_texture_lod
&&
262 extensions
->ARB_texture_float
&&
263 extensions
->ARB_texture_rg
&&
264 extensions
->ARB_texture_compression_rgtc
&&
265 extensions
->EXT_draw_buffers2
&&
266 extensions
->ARB_framebuffer_object
&&
267 extensions
->EXT_framebuffer_sRGB
&&
268 extensions
->EXT_packed_float
&&
269 extensions
->EXT_texture_array
&&
270 extensions
->EXT_texture_shared_exponent
&&
271 extensions
->EXT_transform_feedback
&&
272 extensions
->NV_conditional_render
);
273 const bool ver_3_1
= (ver_3_0
&&
274 consts
->GLSLVersion
>= 140 &&
275 extensions
->ARB_draw_instanced
&&
276 extensions
->ARB_texture_buffer_object
&&
277 extensions
->ARB_uniform_buffer_object
&&
278 extensions
->EXT_texture_snorm
&&
279 extensions
->NV_primitive_restart
&&
280 extensions
->NV_texture_rectangle
&&
281 consts
->Program
[MESA_SHADER_VERTEX
].MaxTextureImageUnits
>= 16);
282 const bool ver_3_2
= (ver_3_1
&&
283 consts
->GLSLVersion
>= 150 &&
284 extensions
->ARB_depth_clamp
&&
285 extensions
->ARB_draw_elements_base_vertex
&&
286 extensions
->ARB_fragment_coord_conventions
&&
287 extensions
->EXT_provoking_vertex
&&
288 extensions
->ARB_seamless_cube_map
&&
289 extensions
->ARB_sync
&&
290 extensions
->ARB_texture_multisample
&&
291 extensions
->EXT_vertex_array_bgra
);
292 const bool ver_3_3
= (ver_3_2
&&
293 consts
->GLSLVersion
>= 330 &&
294 extensions
->ARB_blend_func_extended
&&
295 extensions
->ARB_explicit_attrib_location
&&
296 extensions
->ARB_instanced_arrays
&&
297 extensions
->ARB_occlusion_query2
&&
298 extensions
->ARB_shader_bit_encoding
&&
299 extensions
->ARB_texture_rgb10_a2ui
&&
300 extensions
->ARB_timer_query
&&
301 extensions
->ARB_vertex_type_2_10_10_10_rev
&&
302 extensions
->EXT_texture_swizzle
);
303 /* ARB_sampler_objects is always enabled in mesa */
305 const bool ver_4_0
= (ver_3_3
&&
306 consts
->GLSLVersion
>= 400 &&
307 extensions
->ARB_draw_buffers_blend
&&
308 extensions
->ARB_draw_indirect
&&
309 extensions
->ARB_gpu_shader5
&&
310 extensions
->ARB_gpu_shader_fp64
&&
311 extensions
->ARB_sample_shading
&&
312 extensions
->ARB_shader_subroutine
&&
313 extensions
->ARB_tessellation_shader
&&
314 extensions
->ARB_texture_buffer_object_rgb32
&&
315 extensions
->ARB_texture_cube_map_array
&&
316 extensions
->ARB_texture_query_lod
&&
317 extensions
->ARB_transform_feedback2
&&
318 extensions
->ARB_transform_feedback3
);
319 const bool ver_4_1
= (ver_4_0
&&
320 consts
->GLSLVersion
>= 410 &&
321 extensions
->ARB_ES2_compatibility
&&
322 extensions
->ARB_shader_precision
&&
323 extensions
->ARB_vertex_attrib_64bit
&&
324 extensions
->ARB_viewport_array
);
325 const bool ver_4_2
= (ver_4_1
&&
326 consts
->GLSLVersion
>= 420 &&
327 extensions
->ARB_base_instance
&&
328 extensions
->ARB_conservative_depth
&&
329 extensions
->ARB_internalformat_query
&&
330 extensions
->ARB_shader_atomic_counters
&&
331 extensions
->ARB_shader_image_load_store
&&
332 extensions
->ARB_shading_language_420pack
&&
333 extensions
->ARB_shading_language_packing
&&
334 extensions
->ARB_texture_compression_bptc
&&
335 extensions
->ARB_transform_feedback_instanced
);
390 version
= major
* 10 + minor
;
392 if (api
== API_OPENGL_CORE
&& version
< 31)
399 compute_version_es1(const struct gl_extensions
*extensions
)
401 /* OpenGL ES 1.0 is derived from OpenGL 1.3 */
402 const bool ver_1_0
= (extensions
->ARB_texture_env_combine
&&
403 extensions
->ARB_texture_env_dot3
);
404 /* OpenGL ES 1.1 is derived from OpenGL 1.5 */
405 const bool ver_1_1
= (ver_1_0
&&
406 extensions
->EXT_point_parameters
);
410 } else if (ver_1_0
) {
418 compute_version_es2(const struct gl_extensions
*extensions
)
420 /* OpenGL ES 2.0 is derived from OpenGL 2.0 */
421 const bool ver_2_0
= (extensions
->ARB_texture_cube_map
&&
422 extensions
->EXT_blend_color
&&
423 extensions
->EXT_blend_func_separate
&&
424 extensions
->EXT_blend_minmax
&&
425 extensions
->ARB_vertex_shader
&&
426 extensions
->ARB_fragment_shader
&&
427 extensions
->ARB_texture_non_power_of_two
&&
428 extensions
->EXT_blend_equation_separate
);
429 /* FINISHME: This list isn't quite right. */
430 const bool ver_3_0
= (extensions
->ARB_half_float_vertex
&&
431 extensions
->ARB_internalformat_query
&&
432 extensions
->ARB_map_buffer_range
&&
433 extensions
->ARB_shader_texture_lod
&&
434 extensions
->ARB_texture_float
&&
435 extensions
->ARB_texture_rg
&&
436 extensions
->ARB_depth_buffer_float
&&
437 extensions
->EXT_draw_buffers2
&&
438 /* extensions->ARB_framebuffer_object && */
439 extensions
->EXT_framebuffer_sRGB
&&
440 extensions
->EXT_packed_float
&&
441 extensions
->EXT_texture_array
&&
442 extensions
->EXT_texture_shared_exponent
&&
443 extensions
->EXT_transform_feedback
&&
444 extensions
->ARB_draw_instanced
&&
445 extensions
->ARB_uniform_buffer_object
&&
446 extensions
->EXT_texture_snorm
&&
447 extensions
->NV_primitive_restart
&&
448 extensions
->OES_depth_texture_cube_map
);
449 const bool ver_3_1
= (ver_3_0
&&
450 extensions
->ARB_arrays_of_arrays
&&
451 extensions
->ARB_compute_shader
&&
452 extensions
->ARB_draw_indirect
&&
453 false /*extensions->ARB_framebuffer_no_attachments*/ &&
454 extensions
->ARB_shader_atomic_counters
&&
455 extensions
->ARB_shader_image_load_store
&&
456 false /*extensions->ARB_shader_image_size*/ &&
457 false /*extensions->ARB_shader_storage_buffer_object*/ &&
458 extensions
->ARB_shading_language_packing
&&
459 extensions
->ARB_stencil_texturing
&&
460 extensions
->ARB_gpu_shader5
&&
461 extensions
->EXT_shader_integer_mix
);
465 } else if (ver_3_0
) {
467 } else if (ver_2_0
) {
475 _mesa_get_version(const struct gl_extensions
*extensions
,
476 struct gl_constants
*consts
, gl_api api
)
479 case API_OPENGL_COMPAT
:
480 /* Disable GLSL 1.40 and later for legacy contexts.
481 * This disallows creation of the GL 3.1 compatibility context. */
482 if (consts
->GLSLVersion
> 130) {
483 consts
->GLSLVersion
= 130;
486 case API_OPENGL_CORE
:
487 return compute_version(extensions
, consts
, api
);
489 return compute_version_es1(extensions
);
491 return compute_version_es2(extensions
);
497 * Set the context's Version and VersionString fields.
498 * This should only be called once as part of context initialization
499 * or to perform version check for GLX_ARB_create_context_profile.
502 _mesa_compute_version(struct gl_context
*ctx
)
507 ctx
->Version
= _mesa_get_version(&ctx
->Extensions
, &ctx
->Const
, ctx
->API
);
509 /* Make sure that the GLSL version lines up with the GL version. In some
510 * cases it can be too high, e.g. if an extension is missing.
512 if (ctx
->API
== API_OPENGL_CORE
) {
513 switch (ctx
->Version
) {
515 ctx
->Const
.GLSLVersion
= 140;
518 ctx
->Const
.GLSLVersion
= 150;
521 ctx
->Const
.GLSLVersion
= ctx
->Version
* 10;
527 case API_OPENGL_COMPAT
:
528 case API_OPENGL_CORE
:
529 create_version_string(ctx
, "");
534 _mesa_problem(ctx
, "Incomplete OpenGL ES 1.0 support.");
537 create_version_string(ctx
, "OpenGL ES-CM ");
542 _mesa_problem(ctx
, "Incomplete OpenGL ES 2.0 support.");
545 create_version_string(ctx
, "OpenGL ES ");