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