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 "draw_validate.h"
29 
30 #include "util/os_misc.h"
31 #include "util/simple_mtx.h"
32 
33 #include "mtypes.h"
34 #include "version.h"
35 #include "git_sha1.h"
36 
37 static simple_mtx_t override_lock = _SIMPLE_MTX_INITIALIZER_NP;
38 
39 /**
40  * Scans 'string' to see if it ends with 'ending'.
41  */
42 static bool
check_for_ending(const char * string,const char * ending)43 check_for_ending(const char *string, const char *ending)
44 {
45    const size_t len1 = strlen(string);
46    const size_t len2 = strlen(ending);
47 
48    if (len2 > len1)
49       return false;
50 
51    return strcmp(string + (len1 - len2), ending) == 0;
52 }
53 
54 /**
55  * Returns the gl override data
56  *
57  * version > 0 indicates there is an override requested
58  * fwd_context is only valid if version > 0
59  */
60 static void
get_gl_override(gl_api api,int * version,bool * fwd_context,bool * compat_context)61 get_gl_override(gl_api api, int *version, bool *fwd_context,
62                 bool *compat_context)
63 {
64    const char *env_var = (api == API_OPENGL_CORE || api == API_OPENGL_COMPAT)
65       ? "MESA_GL_VERSION_OVERRIDE" : "MESA_GLES_VERSION_OVERRIDE";
66    const char *version_str;
67    int major, minor, n;
68    static struct override_info {
69       int version;
70       bool fc_suffix;
71       bool compat_suffix;
72    } override[] = {
73       [API_OPENGL_COMPAT] = { -1, false, false},
74       [API_OPENGLES]      = { -1, false, false},
75       [API_OPENGLES2]     = { -1, false, false},
76       [API_OPENGL_CORE]   = { -1, false, false},
77    };
78 
79    STATIC_ASSERT(ARRAY_SIZE(override) == API_OPENGL_LAST + 1);
80 
81    simple_mtx_lock(&override_lock);
82 
83    if (api == API_OPENGLES)
84       goto exit;
85 
86    if (override[api].version < 0) {
87       override[api].version = 0;
88 
89       version_str = os_get_option(env_var);
90       if (version_str) {
91          override[api].fc_suffix = check_for_ending(version_str, "FC");
92          override[api].compat_suffix = check_for_ending(version_str, "COMPAT");
93 
94          n = sscanf(version_str, "%u.%u", &major, &minor);
95          if (n != 2) {
96             fprintf(stderr, "error: invalid value for %s: %s\n",
97                     env_var, version_str);
98             override[api].version = 0;
99          } else {
100             override[api].version = major * 10 + minor;
101 
102             /* There is no such thing as compatibility or forward-compatible for
103              * OpenGL ES 2.0 or 3.x APIs.
104              */
105             if ((override[api].version < 30 && override[api].fc_suffix) ||
106                 (api == API_OPENGLES2 && (override[api].fc_suffix ||
107                                           override[api].compat_suffix))) {
108                fprintf(stderr, "error: invalid value for %s: %s\n",
109                        env_var, version_str);
110             }
111          }
112       }
113    }
114 
115 exit:
116    *version = override[api].version;
117    *fwd_context = override[api].fc_suffix;
118    *compat_context = override[api].compat_suffix;
119 
120    simple_mtx_unlock(&override_lock);
121 }
122 
123 /**
124  * Builds the Mesa version string.
125  */
126 static void
create_version_string(struct gl_context * ctx,const char * prefix)127 create_version_string(struct gl_context *ctx, const char *prefix)
128 {
129    static const int max = 100;
130 
131    ctx->VersionString = malloc(max);
132    if (ctx->VersionString) {
133       snprintf(ctx->VersionString, max,
134 		     "%s%u.%u%s Mesa " PACKAGE_VERSION MESA_GIT_SHA1,
135 		     prefix,
136 		     ctx->Version / 10, ctx->Version % 10,
137 		     (ctx->API == API_OPENGL_CORE) ? " (Core Profile)" :
138                      (ctx->API == API_OPENGL_COMPAT && ctx->Version >= 32) ?
139                         " (Compatibility Profile)" : ""
140 		     );
141    }
142 }
143 
144 /**
145  * Override the context's version and/or API type if the environment variables
146  * MESA_GL_VERSION_OVERRIDE or MESA_GLES_VERSION_OVERRIDE are set.
147  *
148  * Example uses of MESA_GL_VERSION_OVERRIDE:
149  *
150  * 2.1: select a compatibility (non-Core) profile with GL version 2.1.
151  * 3.0: select a compatibility (non-Core) profile with GL version 3.0.
152  * 3.0FC: select a Core+Forward Compatible profile with GL version 3.0.
153  * 3.1: select GL version 3.1 with GL_ARB_compatibility enabled per the driver default.
154  * 3.1FC: select GL version 3.1 with forward compatibility and GL_ARB_compatibility disabled.
155  * 3.1COMPAT: select GL version 3.1 with GL_ARB_compatibility enabled.
156  * X.Y: override GL version to X.Y without changing the profile.
157  * X.YFC: select a Core+Forward Compatible profile with GL version X.Y.
158  * X.YCOMPAT: select a Compatibility profile with GL version X.Y.
159  *
160  * Example uses of MESA_GLES_VERSION_OVERRIDE:
161  *
162  * 2.0: select GLES version 2.0.
163  * 3.0: select GLES version 3.0.
164  * 3.1: select GLES version 3.1.
165  */
166 bool
_mesa_override_gl_version_contextless(struct gl_constants * consts,gl_api * apiOut,GLuint * versionOut)167 _mesa_override_gl_version_contextless(struct gl_constants *consts,
168                                       gl_api *apiOut, GLuint *versionOut)
169 {
170    int version;
171    bool fwd_context, compat_context;
172 
173    get_gl_override(*apiOut, &version, &fwd_context, &compat_context);
174 
175    if (version > 0) {
176       *versionOut = version;
177 
178       /* Modify the API and context flags as needed. */
179       if (*apiOut == API_OPENGL_CORE || *apiOut == API_OPENGL_COMPAT) {
180          if (version >= 30 && fwd_context) {
181             *apiOut = API_OPENGL_CORE;
182             consts->ContextFlags |= GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT;
183          } else if (compat_context) {
184             *apiOut = API_OPENGL_COMPAT;
185          }
186       }
187 
188       return true;
189    }
190    return false;
191 }
192 
193 void
_mesa_override_gl_version(struct gl_context * ctx)194 _mesa_override_gl_version(struct gl_context *ctx)
195 {
196    if (_mesa_override_gl_version_contextless(&ctx->Const, &ctx->API,
197                                              &ctx->Version)) {
198       /* We need to include API in version string for OpenGL ES, otherwise
199        * application can not detect GLES via glGetString(GL_VERSION) query.
200        *
201        * From OpenGL ES 3.2 spec, Page 436:
202        *
203        *     "The VERSION string is laid out as follows:
204        *
205        *     OpenGL ES N.M vendor-specific information"
206        *
207        * From OpenGL 4.5 spec, Page 538:
208        *
209        *     "The VERSION and SHADING_LANGUAGE_VERSION strings are laid out as
210        *     follows:
211        *
212        *     <version number><space><vendor-specific information>"
213        */
214       create_version_string(ctx, _mesa_is_gles(ctx) ? "OpenGL ES " : "");
215       ctx->Extensions.Version = ctx->Version;
216    }
217 }
218 
219 /**
220  * Override the context's GLSL version if the environment variable
221  * MESA_GLSL_VERSION_OVERRIDE is set. Valid values for
222  * MESA_GLSL_VERSION_OVERRIDE are integers, such as "130".
223  */
224 void
_mesa_override_glsl_version(struct gl_constants * consts)225 _mesa_override_glsl_version(struct gl_constants *consts)
226 {
227    const char *env_var = "MESA_GLSL_VERSION_OVERRIDE";
228    const char *version;
229    int n;
230 
231    version = getenv(env_var);
232    if (!version) {
233       return;
234    }
235 
236    n = sscanf(version, "%u", &consts->GLSLVersion);
237    if (n != 1) {
238       fprintf(stderr, "error: invalid value for %s: %s\n", env_var, version);
239       return;
240    }
241 }
242 
243 /**
244  * Examine enabled GL extensions to determine GL version.
245  */
246 static GLuint
compute_version(const struct gl_extensions * extensions,const struct gl_constants * consts,gl_api api)247 compute_version(const struct gl_extensions *extensions,
248                 const struct gl_constants *consts, gl_api api)
249 {
250    GLuint major, minor, version;
251 
252    const bool ver_1_3 = (extensions->ARB_texture_border_clamp &&
253                          extensions->ARB_texture_cube_map &&
254                          extensions->ARB_texture_env_combine &&
255                          extensions->ARB_texture_env_dot3);
256    const bool ver_1_4 = (ver_1_3 &&
257                          extensions->ARB_depth_texture &&
258                          extensions->ARB_shadow &&
259                          extensions->ARB_texture_env_crossbar &&
260                          extensions->EXT_blend_color &&
261                          extensions->EXT_blend_func_separate &&
262                          extensions->EXT_blend_minmax &&
263                          extensions->EXT_point_parameters);
264    const bool ver_1_5 = (ver_1_4 &&
265                          extensions->ARB_occlusion_query);
266    const bool ver_2_0 = (ver_1_5 &&
267                          extensions->ARB_point_sprite &&
268                          extensions->ARB_vertex_shader &&
269                          extensions->ARB_fragment_shader &&
270                          extensions->ARB_texture_non_power_of_two &&
271                          extensions->EXT_blend_equation_separate &&
272                          extensions->EXT_stencil_two_side);
273    const bool ver_2_1 = (ver_2_0 &&
274                          extensions->EXT_pixel_buffer_object &&
275                          extensions->EXT_texture_sRGB);
276    /* We lie about the minimum number of color attachments. Strictly, OpenGL
277     * 3.0 requires 8, whereas OpenGL ES requires 4. OpenGL ES 3.0 class
278     * hardware may only support 4 render targets. Advertise non-conformant
279     * OpenGL 3.0 anyway. Affects freedreno on a3xx
280     */
281    const bool ver_3_0 = (ver_2_1 &&
282                          consts->GLSLVersion >= 130 &&
283                          consts->MaxColorAttachments >= 4 &&
284                          (consts->MaxSamples >= 4 || consts->FakeSWMSAA) &&
285                          (api == API_OPENGL_CORE ||
286                           extensions->ARB_color_buffer_float) &&
287                          extensions->ARB_depth_buffer_float &&
288                          extensions->ARB_half_float_vertex &&
289                          extensions->ARB_map_buffer_range &&
290                          extensions->ARB_shader_texture_lod &&
291                          extensions->ARB_texture_float &&
292                          extensions->ARB_texture_rg &&
293                          extensions->ARB_texture_compression_rgtc &&
294                          extensions->EXT_draw_buffers2 &&
295                          extensions->ARB_framebuffer_object &&
296                          extensions->EXT_framebuffer_sRGB &&
297                          extensions->EXT_packed_float &&
298                          extensions->EXT_texture_array &&
299                          extensions->EXT_texture_shared_exponent &&
300                          extensions->EXT_transform_feedback &&
301                          extensions->NV_conditional_render);
302    const bool ver_3_1 = (ver_3_0 &&
303                          consts->GLSLVersion >= 140 &&
304                          extensions->ARB_draw_instanced &&
305                          extensions->ARB_texture_buffer_object &&
306                          extensions->ARB_uniform_buffer_object &&
307                          extensions->EXT_texture_snorm &&
308                          extensions->NV_primitive_restart &&
309                          extensions->NV_texture_rectangle &&
310                          consts->Program[MESA_SHADER_VERTEX].MaxTextureImageUnits >= 16);
311    const bool ver_3_2 = (ver_3_1 &&
312                          consts->GLSLVersion >= 150 &&
313                          extensions->ARB_depth_clamp &&
314                          extensions->ARB_draw_elements_base_vertex &&
315                          extensions->ARB_fragment_coord_conventions &&
316                          extensions->EXT_provoking_vertex &&
317                          extensions->ARB_seamless_cube_map &&
318                          extensions->ARB_sync &&
319                          extensions->ARB_texture_multisample &&
320                          extensions->EXT_vertex_array_bgra);
321    const bool ver_3_3 = (ver_3_2 &&
322                          consts->GLSLVersion >= 330 &&
323                          extensions->ARB_blend_func_extended &&
324                          extensions->ARB_explicit_attrib_location &&
325                          extensions->ARB_instanced_arrays &&
326                          extensions->ARB_occlusion_query2 &&
327                          extensions->ARB_shader_bit_encoding &&
328                          extensions->ARB_texture_rgb10_a2ui &&
329                          extensions->ARB_timer_query &&
330                          extensions->ARB_vertex_type_2_10_10_10_rev &&
331                          extensions->EXT_texture_swizzle);
332    /* ARB_sampler_objects is always enabled in mesa */
333 
334    const bool ver_4_0 = (ver_3_3 &&
335                          consts->GLSLVersion >= 400 &&
336                          extensions->ARB_draw_buffers_blend &&
337                          extensions->ARB_draw_indirect &&
338                          extensions->ARB_gpu_shader5 &&
339                          extensions->ARB_gpu_shader_fp64 &&
340                          extensions->ARB_sample_shading &&
341                          extensions->ARB_tessellation_shader &&
342                          extensions->ARB_texture_buffer_object_rgb32 &&
343                          extensions->ARB_texture_cube_map_array &&
344                          extensions->ARB_texture_query_lod &&
345                          extensions->ARB_transform_feedback2 &&
346                          extensions->ARB_transform_feedback3);
347    const bool ver_4_1 = (ver_4_0 &&
348                          consts->GLSLVersion >= 410 &&
349                          consts->MaxTextureSize >= 16384 &&
350                          consts->MaxRenderbufferSize >= 16384 &&
351                          extensions->ARB_ES2_compatibility &&
352                          extensions->ARB_shader_precision &&
353                          extensions->ARB_vertex_attrib_64bit &&
354                          extensions->ARB_viewport_array);
355    const bool ver_4_2 = (ver_4_1 &&
356                          consts->GLSLVersion >= 420 &&
357                          extensions->ARB_base_instance &&
358                          extensions->ARB_conservative_depth &&
359                          extensions->ARB_internalformat_query &&
360                          extensions->ARB_shader_atomic_counters &&
361                          extensions->ARB_shader_image_load_store &&
362                          extensions->ARB_shading_language_420pack &&
363                          extensions->ARB_shading_language_packing &&
364                          extensions->ARB_texture_compression_bptc &&
365                          extensions->ARB_transform_feedback_instanced);
366    const bool ver_4_3 = (ver_4_2 &&
367                          consts->GLSLVersion >= 430 &&
368                          consts->Program[MESA_SHADER_VERTEX].MaxUniformBlocks >= 14 &&
369                          extensions->ARB_ES3_compatibility &&
370                          extensions->ARB_arrays_of_arrays &&
371                          extensions->ARB_compute_shader &&
372                          extensions->ARB_copy_image &&
373                          extensions->ARB_explicit_uniform_location &&
374                          extensions->ARB_fragment_layer_viewport &&
375                          extensions->ARB_framebuffer_no_attachments &&
376                          extensions->ARB_internalformat_query2 &&
377                          extensions->ARB_robust_buffer_access_behavior &&
378                          extensions->ARB_shader_image_size &&
379                          extensions->ARB_shader_storage_buffer_object &&
380                          extensions->ARB_stencil_texturing &&
381                          extensions->ARB_texture_buffer_range &&
382                          extensions->ARB_texture_query_levels &&
383                          extensions->ARB_texture_view);
384    const bool ver_4_4 = (ver_4_3 &&
385                          consts->GLSLVersion >= 440 &&
386                          consts->MaxVertexAttribStride >= 2048 &&
387                          extensions->ARB_buffer_storage &&
388                          extensions->ARB_clear_texture &&
389                          extensions->ARB_enhanced_layouts &&
390                          extensions->ARB_query_buffer_object &&
391                          extensions->ARB_texture_mirror_clamp_to_edge &&
392                          extensions->ARB_texture_stencil8 &&
393                          extensions->ARB_vertex_type_10f_11f_11f_rev);
394    const bool ver_4_5 = (ver_4_4 &&
395                          consts->GLSLVersion >= 450 &&
396                          extensions->ARB_ES3_1_compatibility &&
397                          extensions->ARB_clip_control &&
398                          extensions->ARB_conditional_render_inverted &&
399                          extensions->ARB_cull_distance &&
400                          extensions->ARB_derivative_control &&
401                          extensions->ARB_shader_texture_image_samples &&
402                          extensions->NV_texture_barrier);
403    const bool ver_4_6 = (ver_4_5 &&
404                          consts->GLSLVersion >= 460 &&
405                          extensions->ARB_gl_spirv &&
406                          extensions->ARB_spirv_extensions &&
407                          extensions->ARB_indirect_parameters &&
408                          extensions->ARB_pipeline_statistics_query &&
409                          extensions->ARB_polygon_offset_clamp &&
410                          extensions->ARB_shader_atomic_counter_ops &&
411                          extensions->ARB_shader_draw_parameters &&
412                          extensions->ARB_shader_group_vote &&
413                          extensions->ARB_texture_filter_anisotropic &&
414                          extensions->ARB_transform_feedback_overflow_query);
415 
416    if (ver_4_6) {
417       major = 4;
418       minor = 6;
419    }
420    else if (ver_4_5) {
421       major = 4;
422       minor = 5;
423    }
424    else if (ver_4_4) {
425       major = 4;
426       minor = 4;
427    }
428    else if (ver_4_3) {
429       major = 4;
430       minor = 3;
431    }
432    else if (ver_4_2) {
433       major = 4;
434       minor = 2;
435    }
436    else if (ver_4_1) {
437       major = 4;
438       minor = 1;
439    }
440    else if (ver_4_0) {
441       major = 4;
442       minor = 0;
443    }
444    else if (ver_3_3) {
445       major = 3;
446       minor = 3;
447    }
448    else if (ver_3_2) {
449       major = 3;
450       minor = 2;
451    }
452    else if (ver_3_1) {
453       major = 3;
454       minor = 1;
455    }
456    else if (ver_3_0) {
457       major = 3;
458       minor = 0;
459    }
460    else if (ver_2_1) {
461       major = 2;
462       minor = 1;
463    }
464    else if (ver_2_0) {
465       major = 2;
466       minor = 0;
467    }
468    else if (ver_1_5) {
469       major = 1;
470       minor = 5;
471    }
472    else if (ver_1_4) {
473       major = 1;
474       minor = 4;
475    }
476    else if (ver_1_3) {
477       major = 1;
478       minor = 3;
479    }
480    else {
481       major = 1;
482       minor = 2;
483    }
484 
485    version = major * 10 + minor;
486 
487    if (api == API_OPENGL_CORE && version < 31)
488       return 0;
489 
490    return version;
491 }
492 
493 static GLuint
compute_version_es1(const struct gl_extensions * extensions)494 compute_version_es1(const struct gl_extensions *extensions)
495 {
496    /* OpenGL ES 1.0 is derived from OpenGL 1.3 */
497    const bool ver_1_0 = (extensions->ARB_texture_env_combine &&
498                          extensions->ARB_texture_env_dot3);
499    /* OpenGL ES 1.1 is derived from OpenGL 1.5 */
500    const bool ver_1_1 = (ver_1_0 &&
501                          extensions->EXT_point_parameters);
502 
503    if (ver_1_1) {
504       return 11;
505    } else if (ver_1_0) {
506       return 10;
507    } else {
508       return 0;
509    }
510 }
511 
512 static GLuint
compute_version_es2(const struct gl_extensions * extensions,const struct gl_constants * consts)513 compute_version_es2(const struct gl_extensions *extensions,
514                     const struct gl_constants *consts)
515 {
516    /* OpenGL ES 2.0 is derived from OpenGL 2.0 */
517    const bool ver_2_0 = (extensions->ARB_texture_cube_map &&
518                          extensions->EXT_blend_color &&
519                          extensions->EXT_blend_func_separate &&
520                          extensions->EXT_blend_minmax &&
521                          extensions->ARB_vertex_shader &&
522                          extensions->ARB_fragment_shader &&
523                          extensions->ARB_texture_non_power_of_two &&
524                          extensions->EXT_blend_equation_separate);
525    /* FINISHME: This list isn't quite right. */
526    const bool ver_3_0 = (extensions->ARB_half_float_vertex &&
527                          extensions->ARB_internalformat_query &&
528                          extensions->ARB_map_buffer_range &&
529                          extensions->ARB_shader_texture_lod &&
530                          extensions->OES_texture_float &&
531                          extensions->OES_texture_half_float &&
532                          extensions->OES_texture_half_float_linear &&
533                          extensions->ARB_texture_rg &&
534                          extensions->ARB_depth_buffer_float &&
535                          extensions->ARB_framebuffer_object &&
536                          extensions->EXT_sRGB &&
537                          extensions->EXT_packed_float &&
538                          extensions->EXT_texture_array &&
539                          extensions->EXT_texture_shared_exponent &&
540                          extensions->EXT_texture_sRGB &&
541                          extensions->EXT_transform_feedback &&
542                          extensions->ARB_draw_instanced &&
543                          extensions->ARB_uniform_buffer_object &&
544                          extensions->EXT_texture_snorm &&
545                          (extensions->NV_primitive_restart ||
546                           consts->PrimitiveRestartFixedIndex) &&
547                          extensions->OES_depth_texture_cube_map &&
548                          extensions->EXT_texture_type_2_10_10_10_REV &&
549                          consts->MaxColorAttachments >= 4);
550    const bool es31_compute_shader =
551       consts->MaxComputeWorkGroupInvocations >= 128 &&
552       consts->Program[MESA_SHADER_COMPUTE].MaxShaderStorageBlocks &&
553       consts->Program[MESA_SHADER_COMPUTE].MaxAtomicBuffers &&
554       consts->Program[MESA_SHADER_COMPUTE].MaxImageUniforms;
555    const bool ver_3_1 = (ver_3_0 &&
556                          consts->MaxVertexAttribStride >= 2048 &&
557                          extensions->ARB_arrays_of_arrays &&
558                          es31_compute_shader &&
559                          extensions->ARB_draw_indirect &&
560                          extensions->ARB_explicit_uniform_location &&
561                          extensions->ARB_framebuffer_no_attachments &&
562                          extensions->ARB_shading_language_packing &&
563                          extensions->ARB_stencil_texturing &&
564                          extensions->ARB_texture_multisample &&
565                          extensions->ARB_texture_gather &&
566                          extensions->MESA_shader_integer_functions &&
567                          extensions->EXT_shader_integer_mix);
568    const bool ver_3_2 = (ver_3_1 &&
569                          /* ES 3.2 requires that images/buffers be accessible
570                           * from fragment shaders as well
571                           */
572                          extensions->ARB_shader_atomic_counters &&
573                          extensions->ARB_shader_image_load_store &&
574                          extensions->ARB_shader_image_size &&
575                          extensions->ARB_shader_storage_buffer_object &&
576 
577                          extensions->EXT_draw_buffers2 &&
578                          extensions->KHR_blend_equation_advanced &&
579                          extensions->KHR_robustness &&
580                          extensions->KHR_texture_compression_astc_ldr &&
581                          extensions->OES_copy_image &&
582                          extensions->ARB_draw_buffers_blend &&
583                          extensions->ARB_draw_elements_base_vertex &&
584                          extensions->OES_geometry_shader &&
585                          extensions->OES_primitive_bounding_box &&
586                          extensions->OES_sample_variables &&
587                          extensions->ARB_tessellation_shader &&
588                          extensions->ARB_texture_border_clamp &&
589                          extensions->OES_texture_buffer &&
590                          extensions->OES_texture_cube_map_array &&
591                          extensions->ARB_texture_stencil8);
592 
593    if (ver_3_2) {
594       return 32;
595    } else if (ver_3_1) {
596       return 31;
597    } else if (ver_3_0) {
598       return 30;
599    } else if (ver_2_0) {
600       return 20;
601    } else {
602       return 0;
603    }
604 }
605 
606 GLuint
_mesa_get_version(const struct gl_extensions * extensions,struct gl_constants * consts,gl_api api)607 _mesa_get_version(const struct gl_extensions *extensions,
608                   struct gl_constants *consts, gl_api api)
609 {
610    switch (api) {
611    case API_OPENGL_COMPAT:
612       /* Disable higher GLSL versions for legacy contexts.
613        * This disallows creation of higher compatibility contexts. */
614       if (!consts->AllowHigherCompatVersion) {
615          consts->GLSLVersion = consts->GLSLVersionCompat;
616       }
617       FALLTHROUGH;
618    case API_OPENGL_CORE:
619       return compute_version(extensions, consts, api);
620    case API_OPENGLES:
621       return compute_version_es1(extensions);
622    case API_OPENGLES2:
623       return compute_version_es2(extensions, consts);
624    }
625    return 0;
626 }
627 
628 /**
629  * Set the context's Version and VersionString fields.
630  * This should only be called once as part of context initialization
631  * or to perform version check for GLX_ARB_create_context_profile.
632  */
633 void
_mesa_compute_version(struct gl_context * ctx)634 _mesa_compute_version(struct gl_context *ctx)
635 {
636    if (ctx->Version)
637       goto done;
638 
639    ctx->Version = _mesa_get_version(&ctx->Extensions, &ctx->Const, ctx->API);
640    ctx->Extensions.Version = ctx->Version;
641 
642    /* Make sure that the GLSL version lines up with the GL version. In some
643     * cases it can be too high, e.g. if an extension is missing.
644     */
645    if (_mesa_is_desktop_gl(ctx)) {
646       switch (ctx->Version) {
647       case 20:
648          FALLTHROUGH; /* GLSL 1.20 is the minimum we support */
649       case 21:
650          ctx->Const.GLSLVersion = 120;
651          break;
652       case 30:
653          ctx->Const.GLSLVersion = 130;
654          break;
655       case 31:
656          ctx->Const.GLSLVersion = 140;
657          break;
658       case 32:
659          ctx->Const.GLSLVersion = 150;
660          break;
661       default:
662          if (ctx->Version >= 33)
663             ctx->Const.GLSLVersion = ctx->Version * 10;
664          break;
665       }
666    }
667 
668    switch (ctx->API) {
669    case API_OPENGL_COMPAT:
670    case API_OPENGL_CORE:
671       create_version_string(ctx, "");
672       break;
673 
674    case API_OPENGLES:
675       if (!ctx->Version) {
676          _mesa_problem(ctx, "Incomplete OpenGL ES 1.0 support.");
677          return;
678       }
679       create_version_string(ctx, "OpenGL ES-CM ");
680       break;
681 
682    case API_OPENGLES2:
683       if (!ctx->Version) {
684          _mesa_problem(ctx, "Incomplete OpenGL ES 2.0 support.");
685          return;
686       }
687       create_version_string(ctx, "OpenGL ES ");
688       break;
689    }
690 
691 done:
692    if (ctx->API == API_OPENGL_COMPAT && ctx->Version >= 31)
693       ctx->Extensions.ARB_compatibility = GL_TRUE;
694 
695    /* Precompute valid primitive types for faster draw time validation. */
696    /* All primitive type enums are less than 32, so we can use the shift. */
697    ctx->SupportedPrimMask = (1 << GL_POINTS) |
698                            (1 << GL_LINES) |
699                            (1 << GL_LINE_LOOP) |
700                            (1 << GL_LINE_STRIP) |
701                            (1 << GL_TRIANGLES) |
702                            (1 << GL_TRIANGLE_STRIP) |
703                            (1 << GL_TRIANGLE_FAN);
704 
705    if (ctx->API == API_OPENGL_COMPAT) {
706       ctx->SupportedPrimMask |= (1 << GL_QUADS) |
707                                (1 << GL_QUAD_STRIP) |
708                                (1 << GL_POLYGON);
709    }
710 
711    if (_mesa_has_geometry_shaders(ctx)) {
712       ctx->SupportedPrimMask |= (1 << GL_LINES_ADJACENCY) |
713                                (1 << GL_LINE_STRIP_ADJACENCY) |
714                                (1 << GL_TRIANGLES_ADJACENCY) |
715                                (1 << GL_TRIANGLE_STRIP_ADJACENCY);
716    }
717 
718    if (_mesa_has_tessellation(ctx))
719       ctx->SupportedPrimMask |= 1 << GL_PATCHES;
720 
721    /* First time initialization. */
722    _mesa_update_valid_to_render_state(ctx);
723 }
724 
725 
726 void
_mesa_get_driver_uuid(struct gl_context * ctx,GLint * uuid)727 _mesa_get_driver_uuid(struct gl_context *ctx, GLint *uuid)
728 {
729    ctx->Driver.GetDriverUuid(ctx, (char*) uuid);
730 }
731 
732 void
_mesa_get_device_uuid(struct gl_context * ctx,GLint * uuid)733 _mesa_get_device_uuid(struct gl_context *ctx, GLint *uuid)
734 {
735    ctx->Driver.GetDeviceUuid(ctx, (char*) uuid);
736 }
737 
738 /**
739  * Get the i-th GLSL version string.  If index=0, return the most recent
740  * supported version.
741  * \param ctx context to query
742  * \param index  which version string to return, or -1 if none
743  * \param versionOut returns the vesrion string
744  * \return total number of shading language versions.
745  */
746 int
_mesa_get_shading_language_version(const struct gl_context * ctx,int index,char ** versionOut)747 _mesa_get_shading_language_version(const struct gl_context *ctx,
748                                    int index,
749                                    char **versionOut)
750 {
751    int n = 0;
752 
753 #define GLSL_VERSION(S) \
754    if (n++ == index) \
755       *versionOut = S
756 
757    /* GLSL core */
758    if (ctx->Const.GLSLVersion >= 460)
759       GLSL_VERSION("460");
760    if (ctx->Const.GLSLVersion >= 450)
761       GLSL_VERSION("450");
762    if (ctx->Const.GLSLVersion >= 440)
763       GLSL_VERSION("440");
764    if (ctx->Const.GLSLVersion >= 430)
765       GLSL_VERSION("430");
766    if (ctx->Const.GLSLVersion >= 420)
767       GLSL_VERSION("420");
768    if (ctx->Const.GLSLVersion >= 410)
769       GLSL_VERSION("410");
770    if (ctx->Const.GLSLVersion >= 400)
771       GLSL_VERSION("400");
772    if (ctx->Const.GLSLVersion >= 330)
773       GLSL_VERSION("330");
774    if (ctx->Const.GLSLVersion >= 150)
775       GLSL_VERSION("150");
776    if (ctx->Const.GLSLVersion >= 140)
777       GLSL_VERSION("140");
778    if (ctx->Const.GLSLVersion >= 130)
779       GLSL_VERSION("130");
780    if (ctx->Const.GLSLVersion >= 120)
781       GLSL_VERSION("120");
782    /* The GL spec says to return the empty string for GLSL 1.10 */
783    if (ctx->Const.GLSLVersion >= 110)
784       GLSL_VERSION("");
785 
786    /* GLSL es */
787    if ((ctx->API == API_OPENGLES2 && ctx->Version >= 32) ||
788         ctx->Extensions.ARB_ES3_2_compatibility)
789       GLSL_VERSION("320 es");
790    if (_mesa_is_gles31(ctx) || ctx->Extensions.ARB_ES3_1_compatibility)
791       GLSL_VERSION("310 es");
792    if (_mesa_is_gles3(ctx) || ctx->Extensions.ARB_ES3_compatibility)
793       GLSL_VERSION("300 es");
794    if (ctx->API == API_OPENGLES2 || ctx->Extensions.ARB_ES2_compatibility)
795       GLSL_VERSION("100");
796 
797 #undef GLSL_VERSION
798 
799    return n;
800 }
801