1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2010 VMware, Inc. All Rights Reserved.
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
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.
23 */
24
25
26 #include <stdio.h>
27 #include "context.h"
28 #include "imports.h"
29 #include "mtypes.h"
30 #include "version.h"
31 #include "git_sha1.h"
32
33 /**
34 * Scans 'string' to see if it ends with 'ending'.
35 */
36 static bool
check_for_ending(const char * string,const char * ending)37 check_for_ending(const char *string, const char *ending)
38 {
39 const size_t len1 = strlen(string);
40 const size_t len2 = strlen(ending);
41
42 if (len2 > len1)
43 return false;
44
45 return strcmp(string + (len1 - len2), ending) == 0;
46 }
47
48 /**
49 * Returns the gl override data
50 *
51 * version > 0 indicates there is an override requested
52 * fwd_context is only valid if version > 0
53 */
54 static void
get_gl_override(gl_api api,int * version,bool * fwd_context,bool * compat_context)55 get_gl_override(gl_api api, int *version, bool *fwd_context,
56 bool *compat_context)
57 {
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;
61 int major, minor, n;
62 static struct override_info {
63 int version;
64 bool fc_suffix;
65 bool compat_suffix;
66 } override[] = {
67 { -1, false, false},
68 { -1, false, false},
69 { -1, false, false},
70 { -1, false, false},
71 };
72
73 STATIC_ASSERT(ARRAY_SIZE(override) == API_OPENGL_LAST + 1);
74
75 if (api == API_OPENGLES)
76 goto exit;
77
78 if (override[api].version < 0) {
79 override[api].version = 0;
80
81 version_str = getenv(env_var);
82 if (version_str) {
83 override[api].fc_suffix = check_for_ending(version_str, "FC");
84 override[api].compat_suffix = check_for_ending(version_str, "COMPAT");
85
86 n = sscanf(version_str, "%u.%u", &major, &minor);
87 if (n != 2) {
88 fprintf(stderr, "error: invalid value for %s: %s\n",
89 env_var, version_str);
90 override[api].version = 0;
91 } else {
92 override[api].version = major * 10 + minor;
93
94 /* There is no such thing as compatibility or forward-compatible for
95 * OpenGL ES 2.0 or 3.x APIs.
96 */
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);
102 }
103 }
104 }
105 }
106
107 exit:
108 *version = override[api].version;
109 *fwd_context = override[api].fc_suffix;
110 *compat_context = override[api].compat_suffix;
111 }
112
113 /**
114 * Builds the Mesa version string.
115 */
116 static void
create_version_string(struct gl_context * ctx,const char * prefix)117 create_version_string(struct gl_context *ctx, const char *prefix)
118 {
119 static const int max = 100;
120
121 ctx->VersionString = malloc(max);
122 if (ctx->VersionString) {
123 _mesa_snprintf(ctx->VersionString, max,
124 "%s%u.%u%s Mesa " PACKAGE_VERSION
125 #ifdef MESA_GIT_SHA1
126 " (" MESA_GIT_SHA1 ")"
127 #endif
128 ,
129 prefix,
130 ctx->Version / 10, ctx->Version % 10,
131 (ctx->API == API_OPENGL_CORE) ? " (Core Profile)" : ""
132 );
133 }
134 }
135
136 /**
137 * Override the context's version and/or API type if the
138 * environment variable MESA_GL_VERSION_OVERRIDE is set.
139 *
140 * Example uses of MESA_GL_VERSION_OVERRIDE:
141 *
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
147 */
148 bool
_mesa_override_gl_version_contextless(struct gl_constants * consts,gl_api * apiOut,GLuint * versionOut)149 _mesa_override_gl_version_contextless(struct gl_constants *consts,
150 gl_api *apiOut, GLuint *versionOut)
151 {
152 int version;
153 bool fwd_context, compat_context;
154
155 get_gl_override(*apiOut, &version, &fwd_context, &compat_context);
156
157 if (version > 0) {
158 *versionOut = version;
159
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.
163 */
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;
170 } else {
171 *apiOut = API_OPENGL_COMPAT;
172 }
173 }
174
175 return true;
176 }
177 return false;
178 }
179
180 void
_mesa_override_gl_version(struct gl_context * ctx)181 _mesa_override_gl_version(struct gl_context *ctx)
182 {
183 if (_mesa_override_gl_version_contextless(&ctx->Const, &ctx->API,
184 &ctx->Version)) {
185 /* We need to include API in version string for OpenGL ES, otherwise
186 * application can not detect GLES via glGetString(GL_VERSION) query.
187 *
188 * From OpenGL ES 3.2 spec, Page 436:
189 *
190 * "The VERSION string is laid out as follows:
191 *
192 * OpenGL ES N.M vendor-specific information"
193 *
194 * From OpenGL 4.5 spec, Page 538:
195 *
196 * "The VERSION and SHADING_LANGUAGE_VERSION strings are laid out as
197 * follows:
198 *
199 * <version number><space><vendor-specific information>"
200 */
201 create_version_string(ctx, _mesa_is_gles(ctx) ? "OpenGL ES " : "");
202 ctx->Extensions.Version = ctx->Version;
203 }
204 }
205
206 /**
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".
210 */
211 void
_mesa_override_glsl_version(struct gl_constants * consts)212 _mesa_override_glsl_version(struct gl_constants *consts)
213 {
214 const char *env_var = "MESA_GLSL_VERSION_OVERRIDE";
215 const char *version;
216 int n;
217
218 version = getenv(env_var);
219 if (!version) {
220 return;
221 }
222
223 n = sscanf(version, "%u", &consts->GLSLVersion);
224 if (n != 1) {
225 fprintf(stderr, "error: invalid value for %s: %s\n", env_var, version);
226 return;
227 }
228 }
229
230 /**
231 * Examine enabled GL extensions to determine GL version.
232 */
233 static GLuint
compute_version(const struct gl_extensions * extensions,const struct gl_constants * consts,gl_api api)234 compute_version(const struct gl_extensions *extensions,
235 const struct gl_constants *consts, gl_api api)
236 {
237 GLuint major, minor, version;
238
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 &&
259
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.
265 */
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 */
322
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);
354 const bool ver_4_3 = (ver_4_2 &&
355 consts->GLSLVersion >= 430 &&
356 extensions->ARB_ES3_compatibility &&
357 extensions->ARB_arrays_of_arrays &&
358 extensions->ARB_compute_shader &&
359 extensions->ARB_copy_image &&
360 extensions->ARB_explicit_uniform_location &&
361 extensions->ARB_fragment_layer_viewport &&
362 extensions->ARB_framebuffer_no_attachments &&
363 extensions->ARB_internalformat_query2 &&
364 extensions->ARB_robust_buffer_access_behavior &&
365 extensions->ARB_shader_image_size &&
366 extensions->ARB_shader_storage_buffer_object &&
367 extensions->ARB_stencil_texturing &&
368 extensions->ARB_texture_buffer_range &&
369 extensions->ARB_texture_query_levels &&
370 extensions->ARB_texture_view);
371 const bool ver_4_4 = (ver_4_3 &&
372 consts->GLSLVersion >= 440 &&
373 extensions->ARB_buffer_storage &&
374 extensions->ARB_clear_texture &&
375 extensions->ARB_enhanced_layouts &&
376 extensions->ARB_query_buffer_object &&
377 extensions->ARB_texture_mirror_clamp_to_edge &&
378 extensions->ARB_texture_stencil8 &&
379 extensions->ARB_vertex_type_10f_11f_11f_rev);
380 const bool ver_4_5 = (ver_4_4 &&
381 consts->GLSLVersion >= 450 &&
382 extensions->ARB_ES3_1_compatibility &&
383 extensions->ARB_clip_control &&
384 extensions->ARB_conditional_render_inverted &&
385 extensions->ARB_cull_distance &&
386 extensions->ARB_derivative_control &&
387 extensions->ARB_shader_texture_image_samples &&
388 extensions->NV_texture_barrier);
389
390 if (ver_4_5) {
391 major = 4;
392 minor = 5;
393 }
394 else if (ver_4_4) {
395 major = 4;
396 minor = 4;
397 }
398 else if (ver_4_3) {
399 major = 4;
400 minor = 3;
401 }
402 else if (ver_4_2) {
403 major = 4;
404 minor = 2;
405 }
406 else if (ver_4_1) {
407 major = 4;
408 minor = 1;
409 }
410 else if (ver_4_0) {
411 major = 4;
412 minor = 0;
413 }
414 else if (ver_3_3) {
415 major = 3;
416 minor = 3;
417 }
418 else if (ver_3_2) {
419 major = 3;
420 minor = 2;
421 }
422 else if (ver_3_1) {
423 major = 3;
424 minor = 1;
425 }
426 else if (ver_3_0) {
427 major = 3;
428 minor = 0;
429 }
430 else if (ver_2_1) {
431 major = 2;
432 minor = 1;
433 }
434 else if (ver_2_0) {
435 major = 2;
436 minor = 0;
437 }
438 else if (ver_1_5) {
439 major = 1;
440 minor = 5;
441 }
442 else if (ver_1_4) {
443 major = 1;
444 minor = 4;
445 }
446 else if (ver_1_3) {
447 major = 1;
448 minor = 3;
449 }
450 else {
451 major = 1;
452 minor = 2;
453 }
454
455 version = major * 10 + minor;
456
457 if (api == API_OPENGL_CORE && version < 31)
458 return 0;
459
460 return version;
461 }
462
463 static GLuint
compute_version_es1(const struct gl_extensions * extensions)464 compute_version_es1(const struct gl_extensions *extensions)
465 {
466 /* OpenGL ES 1.0 is derived from OpenGL 1.3 */
467 const bool ver_1_0 = (extensions->ARB_texture_env_combine &&
468 extensions->ARB_texture_env_dot3);
469 /* OpenGL ES 1.1 is derived from OpenGL 1.5 */
470 const bool ver_1_1 = (ver_1_0 &&
471 extensions->EXT_point_parameters);
472
473 if (ver_1_1) {
474 return 11;
475 } else if (ver_1_0) {
476 return 10;
477 } else {
478 return 0;
479 }
480 }
481
482 static GLuint
compute_version_es2(const struct gl_extensions * extensions,const struct gl_constants * consts)483 compute_version_es2(const struct gl_extensions *extensions,
484 const struct gl_constants *consts)
485 {
486 /* OpenGL ES 2.0 is derived from OpenGL 2.0 */
487 const bool ver_2_0 = (extensions->ARB_texture_cube_map &&
488 extensions->EXT_blend_color &&
489 extensions->EXT_blend_func_separate &&
490 extensions->EXT_blend_minmax &&
491 extensions->ARB_vertex_shader &&
492 extensions->ARB_fragment_shader &&
493 extensions->ARB_texture_non_power_of_two &&
494 extensions->EXT_blend_equation_separate);
495 /* FINISHME: This list isn't quite right. */
496 const bool ver_3_0 = (extensions->ARB_half_float_vertex &&
497 extensions->ARB_internalformat_query &&
498 extensions->ARB_map_buffer_range &&
499 extensions->ARB_shader_texture_lod &&
500 extensions->ARB_texture_float &&
501 extensions->ARB_texture_rg &&
502 extensions->ARB_depth_buffer_float &&
503 extensions->EXT_draw_buffers2 &&
504 /* extensions->ARB_framebuffer_object && */
505 extensions->EXT_framebuffer_sRGB &&
506 extensions->EXT_packed_float &&
507 extensions->EXT_texture_array &&
508 extensions->EXT_texture_shared_exponent &&
509 extensions->EXT_transform_feedback &&
510 extensions->ARB_draw_instanced &&
511 extensions->ARB_uniform_buffer_object &&
512 extensions->EXT_texture_snorm &&
513 extensions->NV_primitive_restart &&
514 extensions->OES_depth_texture_cube_map);
515 const bool es31_compute_shader =
516 consts->MaxComputeWorkGroupInvocations >= 128;
517 const bool ver_3_1 = (ver_3_0 &&
518 extensions->ARB_arrays_of_arrays &&
519 es31_compute_shader &&
520 extensions->ARB_draw_indirect &&
521 extensions->ARB_explicit_uniform_location &&
522 extensions->ARB_framebuffer_no_attachments &&
523 extensions->ARB_shader_atomic_counters &&
524 extensions->ARB_shader_image_load_store &&
525 extensions->ARB_shader_image_size &&
526 extensions->ARB_shader_storage_buffer_object &&
527 extensions->ARB_shading_language_packing &&
528 extensions->ARB_stencil_texturing &&
529 extensions->ARB_texture_multisample &&
530 extensions->ARB_gpu_shader5 &&
531 extensions->EXT_shader_integer_mix);
532 const bool ver_3_2 = (ver_3_1 &&
533 extensions->KHR_blend_equation_advanced &&
534 extensions->KHR_robustness &&
535 extensions->KHR_texture_compression_astc_ldr &&
536 extensions->OES_copy_image &&
537 extensions->ARB_draw_buffers_blend &&
538 extensions->ARB_draw_elements_base_vertex &&
539 extensions->OES_geometry_shader &&
540 extensions->OES_primitive_bounding_box &&
541 extensions->OES_sample_variables &&
542 extensions->ARB_tessellation_shader &&
543 extensions->ARB_texture_border_clamp &&
544 extensions->OES_texture_buffer &&
545 extensions->OES_texture_cube_map_array &&
546 extensions->ARB_texture_stencil8);
547
548 if (ver_3_2) {
549 return 32;
550 } else if (ver_3_1) {
551 return 31;
552 } else if (ver_3_0) {
553 return 30;
554 } else if (ver_2_0) {
555 return 20;
556 } else {
557 return 0;
558 }
559 }
560
561 GLuint
_mesa_get_version(const struct gl_extensions * extensions,struct gl_constants * consts,gl_api api)562 _mesa_get_version(const struct gl_extensions *extensions,
563 struct gl_constants *consts, gl_api api)
564 {
565 switch (api) {
566 case API_OPENGL_COMPAT:
567 /* Disable GLSL 1.40 and later for legacy contexts.
568 * This disallows creation of the GL 3.1 compatibility context. */
569 if (consts->GLSLVersion > 130) {
570 consts->GLSLVersion = 130;
571 }
572 /* fall through */
573 case API_OPENGL_CORE:
574 return compute_version(extensions, consts, api);
575 case API_OPENGLES:
576 return compute_version_es1(extensions);
577 case API_OPENGLES2:
578 return compute_version_es2(extensions, consts);
579 }
580 return 0;
581 }
582
583 /**
584 * Set the context's Version and VersionString fields.
585 * This should only be called once as part of context initialization
586 * or to perform version check for GLX_ARB_create_context_profile.
587 */
588 void
_mesa_compute_version(struct gl_context * ctx)589 _mesa_compute_version(struct gl_context *ctx)
590 {
591 if (ctx->Version)
592 return;
593
594 ctx->Version = _mesa_get_version(&ctx->Extensions, &ctx->Const, ctx->API);
595 ctx->Extensions.Version = ctx->Version;
596
597 /* Make sure that the GLSL version lines up with the GL version. In some
598 * cases it can be too high, e.g. if an extension is missing.
599 */
600 if (ctx->API == API_OPENGL_CORE) {
601 switch (ctx->Version) {
602 case 31:
603 ctx->Const.GLSLVersion = 140;
604 break;
605 case 32:
606 ctx->Const.GLSLVersion = 150;
607 break;
608 default:
609 ctx->Const.GLSLVersion = ctx->Version * 10;
610 break;
611 }
612 }
613
614 switch (ctx->API) {
615 case API_OPENGL_COMPAT:
616 case API_OPENGL_CORE:
617 create_version_string(ctx, "");
618 break;
619
620 case API_OPENGLES:
621 if (!ctx->Version) {
622 _mesa_problem(ctx, "Incomplete OpenGL ES 1.0 support.");
623 return;
624 }
625 create_version_string(ctx, "OpenGL ES-CM ");
626 break;
627
628 case API_OPENGLES2:
629 if (!ctx->Version) {
630 _mesa_problem(ctx, "Incomplete OpenGL ES 2.0 support.");
631 return;
632 }
633 create_version_string(ctx, "OpenGL ES ");
634 break;
635 }
636 }
637