a1fcec46dcd0e9405c57d1beefe423017e03d924
[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 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24
25 #include "imports.h"
26 #include "mtypes.h"
27 #include "version.h"
28 #include "git_sha1.h"
29
30 /**
31 * Scans 'string' to see if it ends with 'ending'.
32 */
33 static GLboolean
34 check_for_ending(char *string, const char *ending)
35 {
36 int len1, len2;
37
38 len1 = strlen(string);
39 len2 = strlen(ending);
40
41 if (len2 > len1) {
42 return GL_FALSE;
43 }
44
45 if (strcmp(string + (len1 - len2), ending) == 0) {
46 return GL_TRUE;
47 } else {
48 return GL_FALSE;
49 }
50 }
51
52 /**
53 * Builds the MESA version string.
54 */
55 static void
56 create_version_string(struct gl_context *ctx, const char *prefix)
57 {
58 static const int max = 100;
59
60 ctx->VersionString = malloc(max);
61 if (ctx->VersionString) {
62 _mesa_snprintf(ctx->VersionString, max,
63 "%s%u.%u%s Mesa " MESA_VERSION_STRING
64 #ifdef MESA_GIT_SHA1
65 " (" MESA_GIT_SHA1 ")"
66 #endif
67 ,
68 prefix,
69 ctx->Version / 10, ctx->Version % 10,
70 (ctx->API == API_OPENGL_CORE) ? " (Core Profile)" : ""
71 );
72 }
73 }
74
75 /**
76 * Override the context's version and/or API type if the
77 * environment variable MESA_GL_VERSION_OVERRIDE is set.
78 *
79 * Example uses of MESA_GL_VERSION_OVERRIDE:
80 *
81 * 2.1: select a compatibility (non-Core) profile with GL version 2.1
82 * 3.0: select a compatibility (non-Core) profile with GL version 3.0
83 * 3.0FC: select a Core+Forward Compatible profile with GL version 3.0
84 * 3.1: select a Core profile with GL version 3.1
85 * 3.1FC: select a Core+Forward Compatible profile with GL version 3.1
86 */
87 void
88 _mesa_override_gl_version(struct gl_context *ctx)
89 {
90 const char *env_var = "MESA_GL_VERSION_OVERRIDE";
91 const char *version;
92 int n;
93 int major, minor;
94 GLboolean fc_suffix;
95
96 version = getenv(env_var);
97 if (!version) {
98 return;
99 }
100
101 fc_suffix = check_for_ending(version, "FC");
102
103 n = sscanf(version, "%u.%u", &major, &minor);
104 if (n != 2) {
105 fprintf(stderr, "error: invalid value for %s: %s\n", env_var, version);
106 } else {
107 ctx->Version = major * 10 + minor;
108 if (ctx->Version < 30 && fc_suffix) {
109 fprintf(stderr, "error: invalid value for %s: %s\n", env_var, version);
110 } else {
111 if (ctx->Version >= 30 && fc_suffix) {
112 ctx->API = API_OPENGL_CORE;
113 ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT;
114 } else if (ctx->Version >= 31) {
115 ctx->API = API_OPENGL_CORE;
116 } else {
117 ctx->API = API_OPENGL;
118 }
119 create_version_string(ctx, "");
120 }
121 }
122 }
123
124 /**
125 * Override the context's GLSL version if the environment variable
126 * MESA_GLSL_VERSION_OVERRIDE is set. Valid values for
127 * MESA_GLSL_VERSION_OVERRIDE are integers, such as "130".
128 */
129 void
130 _mesa_override_glsl_version(struct gl_context *ctx)
131 {
132 const char *env_var = "MESA_GLSL_VERSION_OVERRIDE";
133 const char *version;
134 int n;
135
136 version = getenv(env_var);
137 if (!version) {
138 return;
139 }
140
141 n = sscanf(version, "%u", &ctx->Const.GLSLVersion);
142 if (n != 1) {
143 fprintf(stderr, "error: invalid value for %s: %s\n", env_var, version);
144 return;
145 }
146 }
147
148 /**
149 * Examine enabled GL extensions to determine GL version.
150 */
151 static void
152 compute_version(struct gl_context *ctx)
153 {
154 GLuint major, minor;
155
156 const GLboolean ver_1_3 = (ctx->Extensions.ARB_texture_border_clamp &&
157 ctx->Extensions.ARB_texture_cube_map &&
158 ctx->Extensions.ARB_texture_env_combine &&
159 ctx->Extensions.ARB_texture_env_dot3);
160 const GLboolean ver_1_4 = (ver_1_3 &&
161 ctx->Extensions.ARB_depth_texture &&
162 ctx->Extensions.ARB_shadow &&
163 ctx->Extensions.ARB_texture_env_crossbar &&
164 ctx->Extensions.ARB_window_pos &&
165 ctx->Extensions.EXT_blend_color &&
166 ctx->Extensions.EXT_blend_func_separate &&
167 ctx->Extensions.EXT_blend_minmax &&
168 ctx->Extensions.EXT_fog_coord &&
169 ctx->Extensions.EXT_point_parameters &&
170 ctx->Extensions.EXT_secondary_color);
171 const GLboolean ver_1_5 = (ver_1_4 &&
172 ctx->Extensions.ARB_occlusion_query &&
173 ctx->Extensions.EXT_shadow_funcs);
174 const GLboolean ver_2_0 = (ver_1_5 &&
175 ctx->Extensions.ARB_point_sprite &&
176 ctx->Extensions.ARB_shader_objects &&
177 ctx->Extensions.ARB_vertex_shader &&
178 ctx->Extensions.ARB_fragment_shader &&
179 ctx->Extensions.ARB_texture_non_power_of_two &&
180 ctx->Extensions.EXT_blend_equation_separate &&
181
182 /* Technically, 2.0 requires the functionality
183 * of the EXT version. Enable 2.0 if either
184 * extension is available, and assume that a
185 * driver that only exposes the ATI extension
186 * will fallback to software when necessary.
187 */
188 (ctx->Extensions.EXT_stencil_two_side
189 || ctx->Extensions.ATI_separate_stencil));
190 const GLboolean ver_2_1 = (ver_2_0 &&
191 ctx->Const.GLSLVersion >= 120 &&
192 ctx->Extensions.EXT_pixel_buffer_object &&
193 ctx->Extensions.EXT_texture_sRGB);
194 const GLboolean ver_3_0 = (ver_2_1 &&
195 ctx->Const.GLSLVersion >= 130 &&
196 ctx->Const.MaxSamples >= 4 &&
197 ctx->Extensions.ARB_color_buffer_float &&
198 ctx->Extensions.ARB_depth_buffer_float &&
199 ctx->Extensions.ARB_half_float_pixel &&
200 ctx->Extensions.ARB_half_float_vertex &&
201 ctx->Extensions.ARB_map_buffer_range &&
202 ctx->Extensions.ARB_shader_texture_lod &&
203 ctx->Extensions.ARB_texture_float &&
204 ctx->Extensions.ARB_texture_rg &&
205 ctx->Extensions.ARB_texture_compression_rgtc &&
206 ctx->Extensions.EXT_draw_buffers2 &&
207 ctx->Extensions.ARB_framebuffer_object &&
208 ctx->Extensions.EXT_framebuffer_sRGB &&
209 ctx->Extensions.EXT_packed_float &&
210 ctx->Extensions.EXT_texture_array &&
211 ctx->Extensions.EXT_texture_shared_exponent &&
212 ctx->Extensions.EXT_transform_feedback &&
213 ctx->Extensions.NV_conditional_render);
214 const GLboolean ver_3_1 = (ver_3_0 &&
215 ctx->Const.GLSLVersion >= 140 &&
216 ctx->Extensions.ARB_copy_buffer &&
217 ctx->Extensions.ARB_draw_instanced &&
218 ctx->Extensions.ARB_texture_buffer_object &&
219 ctx->Extensions.ARB_uniform_buffer_object &&
220 ctx->Extensions.EXT_texture_snorm &&
221 ctx->Extensions.NV_primitive_restart &&
222 ctx->Extensions.NV_texture_rectangle &&
223 ctx->Const.MaxVertexTextureImageUnits >= 16);
224 const GLboolean ver_3_2 = (ver_3_1 &&
225 ctx->Const.GLSLVersion >= 150 &&
226 ctx->Extensions.ARB_depth_clamp &&
227 ctx->Extensions.ARB_draw_elements_base_vertex &&
228 ctx->Extensions.ARB_fragment_coord_conventions &&
229 ctx->Extensions.ARB_geometry_shader4 &&
230 ctx->Extensions.EXT_provoking_vertex &&
231 ctx->Extensions.ARB_seamless_cube_map &&
232 ctx->Extensions.ARB_sync &&
233 ctx->Extensions.ARB_texture_multisample &&
234 ctx->Extensions.EXT_vertex_array_bgra);
235 const GLboolean ver_3_3 = (ver_3_2 &&
236 ctx->Const.GLSLVersion >= 330 &&
237 ctx->Extensions.ARB_blend_func_extended &&
238 ctx->Extensions.ARB_explicit_attrib_location &&
239 ctx->Extensions.ARB_instanced_arrays &&
240 ctx->Extensions.ARB_occlusion_query2 &&
241 ctx->Extensions.ARB_shader_bit_encoding &&
242 ctx->Extensions.ARB_texture_rgb10_a2ui &&
243 ctx->Extensions.ARB_timer_query &&
244 ctx->Extensions.ARB_vertex_type_2_10_10_10_rev &&
245 ctx->Extensions.EXT_texture_swizzle);
246 /* ARB_sampler_objects is always enabled in mesa */
247
248 if (ver_3_3) {
249 major = 3;
250 minor = 3;
251 }
252 else if (ver_3_2) {
253 major = 3;
254 minor = 2;
255 }
256 else if (ver_3_1) {
257 major = 3;
258 minor = 1;
259 }
260 else if (ver_3_0) {
261 major = 3;
262 minor = 0;
263 }
264 else if (ver_2_1) {
265 major = 2;
266 minor = 1;
267 }
268 else if (ver_2_0) {
269 major = 2;
270 minor = 0;
271 }
272 else if (ver_1_5) {
273 major = 1;
274 minor = 5;
275 }
276 else if (ver_1_4) {
277 major = 1;
278 minor = 4;
279 }
280 else if (ver_1_3) {
281 major = 1;
282 minor = 3;
283 }
284 else {
285 major = 1;
286 minor = 2;
287 }
288
289 ctx->Version = major * 10 + minor;
290
291 create_version_string(ctx, "");
292 }
293
294 static void
295 compute_version_es1(struct gl_context *ctx)
296 {
297 /* OpenGL ES 1.0 is derived from OpenGL 1.3 */
298 const GLboolean ver_1_0 = (ctx->Extensions.ARB_texture_env_combine &&
299 ctx->Extensions.ARB_texture_env_dot3);
300 /* OpenGL ES 1.1 is derived from OpenGL 1.5 */
301 const GLboolean ver_1_1 = (ver_1_0 &&
302 ctx->Extensions.EXT_point_parameters);
303
304 if (ver_1_1) {
305 ctx->Version = 11;
306 } else if (ver_1_0) {
307 ctx->Version = 10;
308 } else {
309 _mesa_problem(ctx, "Incomplete OpenGL ES 1.0 support.");
310 }
311
312 create_version_string(ctx, "OpenGL ES-CM ");
313 }
314
315 static void
316 compute_version_es2(struct gl_context *ctx)
317 {
318 /* OpenGL ES 2.0 is derived from OpenGL 2.0 */
319 const GLboolean ver_2_0 = (ctx->Extensions.ARB_texture_cube_map &&
320 ctx->Extensions.EXT_blend_color &&
321 ctx->Extensions.EXT_blend_func_separate &&
322 ctx->Extensions.EXT_blend_minmax &&
323 ctx->Extensions.ARB_shader_objects &&
324 ctx->Extensions.ARB_vertex_shader &&
325 ctx->Extensions.ARB_fragment_shader &&
326 ctx->Extensions.ARB_texture_non_power_of_two &&
327 ctx->Extensions.EXT_blend_equation_separate);
328 if (ver_2_0) {
329 ctx->Version = 20;
330 } else {
331 _mesa_problem(ctx, "Incomplete OpenGL ES 2.0 support.");
332 }
333
334 create_version_string(ctx, "OpenGL ES ");
335 }
336
337 /**
338 * Set the context's Version and VersionString fields.
339 * This should only be called once as part of context initialization
340 * or to perform version check for GLX_ARB_create_context_profile.
341 */
342 void
343 _mesa_compute_version(struct gl_context *ctx)
344 {
345 if (ctx->Version)
346 return;
347
348 switch (ctx->API) {
349 case API_OPENGL:
350 case API_OPENGL_CORE:
351 compute_version(ctx);
352 break;
353 case API_OPENGLES:
354 compute_version_es1(ctx);
355 break;
356 case API_OPENGLES2:
357 compute_version_es2(ctx);
358 break;
359 }
360
361 }