34152dfdb6f1534b453c22845b34f282e483f899
[mesa.git] / src / mesa / main / version.c
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 "imports.h"
27 #include "mtypes.h"
28 #include "version.h"
29 #include "git_sha1.h"
30
31 /**
32 * Scans 'string' to see if it ends with 'ending'.
33 */
34 static GLboolean
35 check_for_ending(const char *string, const char *ending)
36 {
37 int len1, len2;
38
39 len1 = strlen(string);
40 len2 = strlen(ending);
41
42 if (len2 > len1) {
43 return GL_FALSE;
44 }
45
46 if (strcmp(string + (len1 - len2), ending) == 0) {
47 return GL_TRUE;
48 } else {
49 return GL_FALSE;
50 }
51 }
52
53 /**
54 * Returns the gl override data
55 *
56 * version > 0 indicates there is an override requested
57 * fwd_context is only valid if version > 0
58 */
59 static void
60 get_gl_override(int *version, GLboolean *fwd_context)
61 {
62 const char *env_var = "MESA_GL_VERSION_OVERRIDE";
63 const char *version_str;
64 int major, minor, n;
65 static int override_version = -1;
66 static GLboolean fc_suffix = GL_FALSE;
67
68 if (override_version < 0) {
69 override_version = 0;
70
71 version_str = getenv(env_var);
72 if (version_str) {
73 fc_suffix = check_for_ending(version_str, "FC");
74
75 n = sscanf(version_str, "%u.%u", &major, &minor);
76 if (n != 2) {
77 fprintf(stderr, "error: invalid value for %s: %s\n", env_var, version_str);
78 override_version = 0;
79 } else {
80 override_version = major * 10 + minor;
81 if (override_version < 30 && fc_suffix) {
82 fprintf(stderr, "error: invalid value for %s: %s\n", env_var, version_str);
83 }
84 }
85 }
86 }
87
88 *version = override_version;
89 *fwd_context = fc_suffix;
90 }
91
92 /**
93 * Builds the MESA version string.
94 */
95 static void
96 create_version_string(struct gl_context *ctx, const char *prefix)
97 {
98 static const int max = 100;
99
100 ctx->VersionString = malloc(max);
101 if (ctx->VersionString) {
102 _mesa_snprintf(ctx->VersionString, max,
103 "%s%u.%u%s Mesa " PACKAGE_VERSION
104 #ifdef MESA_GIT_SHA1
105 " (" MESA_GIT_SHA1 ")"
106 #endif
107 ,
108 prefix,
109 ctx->Version / 10, ctx->Version % 10,
110 (ctx->API == API_OPENGL_CORE) ? " (Core Profile)" : ""
111 );
112 }
113 }
114
115 /**
116 * Override the context's version and/or API type if the
117 * environment variable MESA_GL_VERSION_OVERRIDE is set.
118 *
119 * Example uses of MESA_GL_VERSION_OVERRIDE:
120 *
121 * 2.1: select a compatibility (non-Core) profile with GL version 2.1
122 * 3.0: select a compatibility (non-Core) profile with GL version 3.0
123 * 3.0FC: select a Core+Forward Compatible profile with GL version 3.0
124 * 3.1: select a Core profile with GL version 3.1
125 * 3.1FC: select a Core+Forward Compatible profile with GL version 3.1
126 */
127 bool
128 _mesa_override_gl_version_contextless(struct gl_constants *consts,
129 gl_api *apiOut, GLuint *versionOut)
130 {
131 int version;
132 GLboolean fwd_context;
133
134 get_gl_override(&version, &fwd_context);
135
136 if (version > 0) {
137 *versionOut = version;
138 if (version >= 30 && fwd_context) {
139 *apiOut = API_OPENGL_CORE;
140 consts->ContextFlags |= GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT;
141 } else if (version >= 31) {
142 *apiOut = API_OPENGL_CORE;
143 } else {
144 *apiOut = API_OPENGL_COMPAT;
145 }
146 return GL_TRUE;
147 }
148 return GL_FALSE;
149 }
150
151 void
152 _mesa_override_gl_version(struct gl_context *ctx)
153 {
154 if (_mesa_override_gl_version_contextless(&ctx->Const, &ctx->API,
155 &ctx->Version)) {
156 create_version_string(ctx, "");
157 }
158 }
159
160 /**
161 * Returns the gl override value
162 *
163 * version > 0 indicates there is an override requested
164 */
165 int
166 _mesa_get_gl_version_override(void)
167 {
168 int version;
169 GLboolean fwd_context;
170
171 get_gl_override(&version, &fwd_context);
172
173 return version;
174 }
175
176 /**
177 * Override the context's GLSL version if the environment variable
178 * MESA_GLSL_VERSION_OVERRIDE is set. Valid values for
179 * MESA_GLSL_VERSION_OVERRIDE are integers, such as "130".
180 */
181 void
182 _mesa_override_glsl_version(struct gl_constants *consts)
183 {
184 const char *env_var = "MESA_GLSL_VERSION_OVERRIDE";
185 const char *version;
186 int n;
187
188 version = getenv(env_var);
189 if (!version) {
190 return;
191 }
192
193 n = sscanf(version, "%u", &consts->GLSLVersion);
194 if (n != 1) {
195 fprintf(stderr, "error: invalid value for %s: %s\n", env_var, version);
196 return;
197 }
198 }
199
200 /**
201 * Examine enabled GL extensions to determine GL version.
202 */
203 static void
204 compute_version(struct gl_context *ctx)
205 {
206 GLuint major, minor;
207
208 const GLboolean ver_1_3 = (ctx->Extensions.ARB_texture_border_clamp &&
209 ctx->Extensions.ARB_texture_cube_map &&
210 ctx->Extensions.ARB_texture_env_combine &&
211 ctx->Extensions.ARB_texture_env_dot3);
212 const GLboolean ver_1_4 = (ver_1_3 &&
213 ctx->Extensions.ARB_depth_texture &&
214 ctx->Extensions.ARB_shadow &&
215 ctx->Extensions.ARB_texture_env_crossbar &&
216 ctx->Extensions.EXT_blend_color &&
217 ctx->Extensions.EXT_blend_func_separate &&
218 ctx->Extensions.EXT_blend_minmax &&
219 ctx->Extensions.EXT_point_parameters);
220 const GLboolean ver_1_5 = (ver_1_4 &&
221 ctx->Extensions.ARB_occlusion_query);
222 const GLboolean ver_2_0 = (ver_1_5 &&
223 ctx->Extensions.ARB_point_sprite &&
224 ctx->Extensions.ARB_vertex_shader &&
225 ctx->Extensions.ARB_fragment_shader &&
226 ctx->Extensions.ARB_texture_non_power_of_two &&
227 ctx->Extensions.EXT_blend_equation_separate &&
228
229 /* Technically, 2.0 requires the functionality
230 * of the EXT version. Enable 2.0 if either
231 * extension is available, and assume that a
232 * driver that only exposes the ATI extension
233 * will fallback to software when necessary.
234 */
235 (ctx->Extensions.EXT_stencil_two_side
236 || ctx->Extensions.ATI_separate_stencil));
237 const GLboolean ver_2_1 = (ver_2_0 &&
238 ctx->Extensions.EXT_pixel_buffer_object &&
239 ctx->Extensions.EXT_texture_sRGB);
240 const GLboolean ver_3_0 = (ver_2_1 &&
241 ctx->Const.GLSLVersion >= 130 &&
242 (ctx->Const.MaxSamples >= 4 || ctx->Const.FakeSWMSAA) &&
243 (ctx->API == API_OPENGL_CORE ||
244 ctx->Extensions.ARB_color_buffer_float) &&
245 ctx->Extensions.ARB_depth_buffer_float &&
246 ctx->Extensions.ARB_half_float_vertex &&
247 ctx->Extensions.ARB_map_buffer_range &&
248 ctx->Extensions.ARB_shader_texture_lod &&
249 ctx->Extensions.ARB_texture_float &&
250 ctx->Extensions.ARB_texture_rg &&
251 ctx->Extensions.ARB_texture_compression_rgtc &&
252 ctx->Extensions.EXT_draw_buffers2 &&
253 ctx->Extensions.ARB_framebuffer_object &&
254 ctx->Extensions.EXT_framebuffer_sRGB &&
255 ctx->Extensions.EXT_packed_float &&
256 ctx->Extensions.EXT_texture_array &&
257 ctx->Extensions.EXT_texture_shared_exponent &&
258 ctx->Extensions.EXT_transform_feedback &&
259 ctx->Extensions.NV_conditional_render);
260 const GLboolean ver_3_1 = (ver_3_0 &&
261 ctx->Const.GLSLVersion >= 140 &&
262 ctx->Extensions.ARB_draw_instanced &&
263 ctx->Extensions.ARB_texture_buffer_object &&
264 ctx->Extensions.ARB_uniform_buffer_object &&
265 ctx->Extensions.EXT_texture_snorm &&
266 ctx->Extensions.NV_primitive_restart &&
267 ctx->Extensions.NV_texture_rectangle &&
268 ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits >= 16);
269 const GLboolean ver_3_2 = (ver_3_1 &&
270 ctx->Const.GLSLVersion >= 150 &&
271 ctx->Extensions.ARB_depth_clamp &&
272 ctx->Extensions.ARB_draw_elements_base_vertex &&
273 ctx->Extensions.ARB_fragment_coord_conventions &&
274 ctx->Extensions.EXT_provoking_vertex &&
275 ctx->Extensions.ARB_seamless_cube_map &&
276 ctx->Extensions.ARB_sync &&
277 ctx->Extensions.ARB_texture_multisample &&
278 ctx->Extensions.EXT_vertex_array_bgra);
279 const GLboolean ver_3_3 = (ver_3_2 &&
280 ctx->Const.GLSLVersion >= 330 &&
281 ctx->Extensions.ARB_blend_func_extended &&
282 ctx->Extensions.ARB_explicit_attrib_location &&
283 ctx->Extensions.ARB_instanced_arrays &&
284 ctx->Extensions.ARB_occlusion_query2 &&
285 ctx->Extensions.ARB_shader_bit_encoding &&
286 ctx->Extensions.ARB_texture_rgb10_a2ui &&
287 ctx->Extensions.ARB_timer_query &&
288 ctx->Extensions.ARB_vertex_type_2_10_10_10_rev &&
289 ctx->Extensions.EXT_texture_swizzle);
290 /* ARB_sampler_objects is always enabled in mesa */
291
292 if (ver_3_3) {
293 major = 3;
294 minor = 3;
295 }
296 else if (ver_3_2) {
297 major = 3;
298 minor = 2;
299 }
300 else if (ver_3_1) {
301 major = 3;
302 minor = 1;
303 }
304 else if (ver_3_0) {
305 major = 3;
306 minor = 0;
307 }
308 else if (ver_2_1) {
309 major = 2;
310 minor = 1;
311 }
312 else if (ver_2_0) {
313 major = 2;
314 minor = 0;
315 }
316 else if (ver_1_5) {
317 major = 1;
318 minor = 5;
319 }
320 else if (ver_1_4) {
321 major = 1;
322 minor = 4;
323 }
324 else if (ver_1_3) {
325 major = 1;
326 minor = 3;
327 }
328 else {
329 major = 1;
330 minor = 2;
331 }
332
333 ctx->Version = major * 10 + minor;
334
335 create_version_string(ctx, "");
336 }
337
338 static void
339 compute_version_es1(struct gl_context *ctx)
340 {
341 /* OpenGL ES 1.0 is derived from OpenGL 1.3 */
342 const GLboolean ver_1_0 = (ctx->Extensions.ARB_texture_env_combine &&
343 ctx->Extensions.ARB_texture_env_dot3);
344 /* OpenGL ES 1.1 is derived from OpenGL 1.5 */
345 const GLboolean ver_1_1 = (ver_1_0 &&
346 ctx->Extensions.EXT_point_parameters);
347
348 if (ver_1_1) {
349 ctx->Version = 11;
350 } else if (ver_1_0) {
351 ctx->Version = 10;
352 } else {
353 _mesa_problem(ctx, "Incomplete OpenGL ES 1.0 support.");
354 }
355
356 create_version_string(ctx, "OpenGL ES-CM ");
357 }
358
359 static void
360 compute_version_es2(struct gl_context *ctx)
361 {
362 /* OpenGL ES 2.0 is derived from OpenGL 2.0 */
363 const GLboolean ver_2_0 = (ctx->Extensions.ARB_texture_cube_map &&
364 ctx->Extensions.EXT_blend_color &&
365 ctx->Extensions.EXT_blend_func_separate &&
366 ctx->Extensions.EXT_blend_minmax &&
367 ctx->Extensions.ARB_vertex_shader &&
368 ctx->Extensions.ARB_fragment_shader &&
369 ctx->Extensions.ARB_texture_non_power_of_two &&
370 ctx->Extensions.EXT_blend_equation_separate);
371 /* FINISHME: This list isn't quite right. */
372 const GLboolean ver_3_0 = (ctx->Extensions.ARB_half_float_vertex &&
373 ctx->Extensions.ARB_internalformat_query &&
374 ctx->Extensions.ARB_map_buffer_range &&
375 ctx->Extensions.ARB_shader_texture_lod &&
376 ctx->Extensions.ARB_texture_float &&
377 ctx->Extensions.ARB_texture_rg &&
378 ctx->Extensions.ARB_texture_compression_rgtc &&
379 ctx->Extensions.EXT_draw_buffers2 &&
380 /* ctx->Extensions.ARB_framebuffer_object && */
381 ctx->Extensions.EXT_framebuffer_sRGB &&
382 ctx->Extensions.EXT_packed_float &&
383 ctx->Extensions.EXT_texture_array &&
384 ctx->Extensions.EXT_texture_shared_exponent &&
385 ctx->Extensions.EXT_transform_feedback &&
386 ctx->Extensions.NV_conditional_render &&
387 ctx->Extensions.ARB_draw_instanced &&
388 ctx->Extensions.ARB_uniform_buffer_object &&
389 ctx->Extensions.EXT_texture_snorm &&
390 ctx->Extensions.NV_primitive_restart &&
391 ctx->Extensions.OES_depth_texture_cube_map);
392 if (ver_3_0) {
393 ctx->Version = 30;
394 } else if (ver_2_0) {
395 ctx->Version = 20;
396 } else {
397 _mesa_problem(ctx, "Incomplete OpenGL ES 2.0 support.");
398 }
399
400 create_version_string(ctx, "OpenGL ES ");
401 }
402
403 /**
404 * Set the context's Version and VersionString fields.
405 * This should only be called once as part of context initialization
406 * or to perform version check for GLX_ARB_create_context_profile.
407 */
408 void
409 _mesa_compute_version(struct gl_context *ctx)
410 {
411 if (ctx->Version)
412 return;
413
414 switch (ctx->API) {
415 case API_OPENGL_COMPAT:
416 /* Disable GLSL 1.40 and later for legacy contexts.
417 * This disallows creation of the GL 3.1 compatibility context. */
418 if (ctx->Const.GLSLVersion > 130) {
419 ctx->Const.GLSLVersion = 130;
420 }
421 /* fall through */
422 case API_OPENGL_CORE:
423 compute_version(ctx);
424 break;
425 case API_OPENGLES:
426 compute_version_es1(ctx);
427 break;
428 case API_OPENGLES2:
429 compute_version_es2(ctx);
430 break;
431 }
432
433 }