mesa/version: only enable GL4.1 with correct limits.
[mesa.git] / src / mesa / main / extensions.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27 /**
28 * \file
29 * \brief Extension handling
30 */
31
32
33 #include "glheader.h"
34
35 #include "context.h"
36 #include "extensions.h"
37 #include "macros.h"
38 #include "mtypes.h"
39
40 struct gl_extensions _mesa_extension_override_enables;
41 struct gl_extensions _mesa_extension_override_disables;
42
43 #define MAX_UNRECOGNIZED_EXTENSIONS 16
44 static struct {
45 char *env;
46 const char *names[MAX_UNRECOGNIZED_EXTENSIONS];
47 } unrecognized_extensions;
48
49 /**
50 * Given a member \c x of struct gl_extensions, return offset of
51 * \c x in bytes.
52 */
53 #define o(x) offsetof(struct gl_extensions, x)
54
55 static int
56 extension_name_compare(const void *name, const void *elem)
57 {
58 const struct mesa_extension *entry = elem;
59 return strcmp(name, entry->name);
60 }
61
62 /**
63 * Given an extension name, lookup up the corresponding member of struct
64 * gl_extensions and return that member's index. If the name is
65 * not found in the \c _mesa_extension_table, return -1.
66 *
67 * \param name Name of extension.
68 * \return Index of member in struct gl_extensions.
69 */
70 static int
71 name_to_index(const char* name)
72 {
73 const struct mesa_extension *entry;
74
75 if (!name)
76 return -1;
77
78 entry = bsearch(name,
79 _mesa_extension_table, MESA_EXTENSION_COUNT,
80 sizeof(_mesa_extension_table[0]),
81 extension_name_compare);
82
83 if (entry)
84 return entry - _mesa_extension_table;
85
86 return -1;
87 }
88
89 /**
90 * Overrides extensions in \c ctx based on the values in
91 * _mesa_extension_override_enables and _mesa_extension_override_disables.
92 */
93 void
94 _mesa_override_extensions(struct gl_context *ctx)
95 {
96 unsigned i;
97 const GLboolean *enables =
98 (GLboolean*) &_mesa_extension_override_enables;
99 const GLboolean *disables =
100 (GLboolean*) &_mesa_extension_override_disables;
101 GLboolean *ctx_ext = (GLboolean*)&ctx->Extensions;
102
103 for (i = 0; i < MESA_EXTENSION_COUNT; ++i) {
104 size_t offset = _mesa_extension_table[i].offset;
105
106 assert(!enables[offset] || !disables[offset]);
107 if (enables[offset]) {
108 ctx_ext[offset] = 1;
109 } else if (disables[offset]) {
110 ctx_ext[offset] = 0;
111 }
112 }
113 }
114
115
116 /**
117 * Enable all extensions suitable for a software-only renderer.
118 * This is a convenience function used by the XMesa, OSMesa, GGI drivers, etc.
119 */
120 void
121 _mesa_enable_sw_extensions(struct gl_context *ctx)
122 {
123 ctx->Extensions.ARB_depth_clamp = GL_TRUE;
124 ctx->Extensions.ARB_depth_texture = GL_TRUE;
125 ctx->Extensions.ARB_draw_elements_base_vertex = GL_TRUE;
126 ctx->Extensions.ARB_draw_instanced = GL_TRUE;
127 ctx->Extensions.ARB_explicit_attrib_location = GL_TRUE;
128 ctx->Extensions.ARB_fragment_coord_conventions = GL_TRUE;
129 ctx->Extensions.ARB_fragment_program = GL_TRUE;
130 ctx->Extensions.ARB_fragment_program_shadow = GL_TRUE;
131 ctx->Extensions.ARB_fragment_shader = GL_TRUE;
132 ctx->Extensions.ARB_framebuffer_object = GL_TRUE;
133 ctx->Extensions.ARB_half_float_vertex = GL_TRUE;
134 ctx->Extensions.ARB_map_buffer_range = GL_TRUE;
135 ctx->Extensions.ARB_occlusion_query = GL_TRUE;
136 ctx->Extensions.ARB_occlusion_query2 = GL_TRUE;
137 ctx->Extensions.ARB_point_sprite = GL_TRUE;
138 ctx->Extensions.ARB_shadow = GL_TRUE;
139 ctx->Extensions.ARB_texture_border_clamp = GL_TRUE;
140 ctx->Extensions.ARB_texture_compression_bptc = GL_TRUE;
141 ctx->Extensions.ARB_texture_cube_map = GL_TRUE;
142 ctx->Extensions.ARB_texture_env_combine = GL_TRUE;
143 ctx->Extensions.ARB_texture_env_crossbar = GL_TRUE;
144 ctx->Extensions.ARB_texture_env_dot3 = GL_TRUE;
145 ctx->Extensions.ARB_texture_filter_anisotropic = GL_TRUE;
146 ctx->Extensions.ARB_texture_float = GL_TRUE;
147 ctx->Extensions.ARB_texture_mirror_clamp_to_edge = GL_TRUE;
148 ctx->Extensions.ARB_texture_non_power_of_two = GL_TRUE;
149 ctx->Extensions.ARB_texture_rg = GL_TRUE;
150 ctx->Extensions.ARB_texture_compression_rgtc = GL_TRUE;
151 ctx->Extensions.ARB_vertex_program = GL_TRUE;
152 ctx->Extensions.ARB_vertex_shader = GL_TRUE;
153 ctx->Extensions.ARB_sync = GL_TRUE;
154 ctx->Extensions.APPLE_object_purgeable = GL_TRUE;
155 ctx->Extensions.ATI_fragment_shader = GL_TRUE;
156 ctx->Extensions.ATI_texture_compression_3dc = GL_TRUE;
157 ctx->Extensions.ATI_texture_env_combine3 = GL_TRUE;
158 ctx->Extensions.ATI_texture_mirror_once = GL_TRUE;
159 ctx->Extensions.EXT_blend_color = GL_TRUE;
160 ctx->Extensions.EXT_blend_equation_separate = GL_TRUE;
161 ctx->Extensions.EXT_blend_func_separate = GL_TRUE;
162 ctx->Extensions.EXT_blend_minmax = GL_TRUE;
163 ctx->Extensions.EXT_depth_bounds_test = GL_TRUE;
164 ctx->Extensions.EXT_draw_buffers2 = GL_TRUE;
165 ctx->Extensions.EXT_pixel_buffer_object = GL_TRUE;
166 ctx->Extensions.EXT_point_parameters = GL_TRUE;
167 ctx->Extensions.EXT_provoking_vertex = GL_TRUE;
168 ctx->Extensions.EXT_stencil_two_side = GL_TRUE;
169 ctx->Extensions.EXT_texture_array = GL_TRUE;
170 ctx->Extensions.EXT_texture_compression_latc = GL_TRUE;
171 ctx->Extensions.EXT_texture_env_dot3 = GL_TRUE;
172 ctx->Extensions.EXT_texture_filter_anisotropic = GL_TRUE;
173 ctx->Extensions.EXT_texture_mirror_clamp = GL_TRUE;
174 ctx->Extensions.EXT_texture_shared_exponent = GL_TRUE;
175 ctx->Extensions.EXT_texture_sRGB = GL_TRUE;
176 ctx->Extensions.EXT_texture_sRGB_decode = GL_TRUE;
177 ctx->Extensions.EXT_texture_swizzle = GL_TRUE;
178 /*ctx->Extensions.EXT_transform_feedback = GL_TRUE;*/
179 ctx->Extensions.EXT_vertex_array_bgra = GL_TRUE;
180 ctx->Extensions.MESA_pack_invert = GL_TRUE;
181 ctx->Extensions.MESA_ycbcr_texture = GL_TRUE;
182 ctx->Extensions.NV_conditional_render = GL_TRUE;
183 ctx->Extensions.NV_point_sprite = GL_TRUE;
184 ctx->Extensions.NV_texture_env_combine4 = GL_TRUE;
185 ctx->Extensions.NV_texture_rectangle = GL_TRUE;
186 ctx->Extensions.EXT_gpu_program_parameters = GL_TRUE;
187 ctx->Extensions.OES_standard_derivatives = GL_TRUE;
188 ctx->Extensions.TDFX_texture_compression_FXT1 = GL_TRUE;
189 ctx->Extensions.ANGLE_texture_compression_dxt = GL_TRUE;
190 ctx->Extensions.EXT_texture_compression_s3tc = GL_TRUE;
191 }
192
193 /**
194 * Either enable or disable the named extension.
195 * \return offset of extensions withint `ext' or 0 if extension is not known
196 */
197 static size_t
198 set_extension(struct gl_extensions *ext, int i, GLboolean state)
199 {
200 size_t offset;
201
202 offset = i < 0 ? 0 : _mesa_extension_table[i].offset;
203 if (offset != 0 && (offset != o(dummy_true) || state != GL_FALSE)) {
204 ((GLboolean *) ext)[offset] = state;
205 }
206
207 return offset;
208 }
209
210
211 /**
212 * \brief Free string pointed by unrecognized_extensions
213 *
214 * This string is allocated early during the first context creation by
215 * _mesa_one_time_init_extension_overrides.
216 */
217 static void
218 free_unknown_extensions_strings(void)
219 {
220 free(unrecognized_extensions.env);
221 for (int i = 0; i < MAX_UNRECOGNIZED_EXTENSIONS; ++i)
222 unrecognized_extensions.names[i] = NULL;
223 }
224
225
226 /**
227 * \brief Initialize extension override tables based on \c MESA_EXTENSION_OVERRIDE
228 *
229 * This should be called one time early during first context initialization.
230
231 * \c MESA_EXTENSION_OVERRIDE is a space-separated list of extensions to
232 * enable or disable. The list is processed thus:
233 * - Enable recognized extension names that are prefixed with '+'.
234 * - Disable recognized extension names that are prefixed with '-'.
235 * - Enable recognized extension names that are not prefixed.
236 * - Collect unrecognized extension names in a new string.
237 */
238 void
239 _mesa_one_time_init_extension_overrides(void)
240 {
241 const char *env_const = getenv("MESA_EXTENSION_OVERRIDE");
242 char *env;
243 char *ext;
244 size_t offset;
245 unsigned unknown_ext = 0;
246
247 memset(&_mesa_extension_override_enables, 0, sizeof(struct gl_extensions));
248 memset(&_mesa_extension_override_disables, 0, sizeof(struct gl_extensions));
249
250 if (env_const == NULL) {
251 return;
252 }
253
254 /* Copy env_const because strtok() is destructive. */
255 env = strdup(env_const);
256
257 if (env == NULL)
258 return;
259
260 for (ext = strtok(env, " "); ext != NULL; ext = strtok(NULL, " ")) {
261 int enable;
262 int i;
263 bool recognized;
264 switch (ext[0]) {
265 case '+':
266 enable = 1;
267 ++ext;
268 break;
269 case '-':
270 enable = 0;
271 ++ext;
272 break;
273 default:
274 enable = 1;
275 break;
276 }
277
278 i = name_to_index(ext);
279 offset = set_extension(&_mesa_extension_override_enables, i, enable);
280 offset = set_extension(&_mesa_extension_override_disables, i, !enable);
281 if (offset != 0)
282 recognized = true;
283 else
284 recognized = false;
285
286 if (!recognized && enable) {
287 if (unknown_ext >= MAX_UNRECOGNIZED_EXTENSIONS) {
288 static bool warned;
289
290 if (!warned) {
291 warned = true;
292 _mesa_problem(NULL, "Trying to enable too many unknown extension. "
293 "Only the first %d will be honoured",
294 MAX_UNRECOGNIZED_EXTENSIONS);
295 }
296 } else {
297 unrecognized_extensions.names[unknown_ext] = ext;
298 unknown_ext++;
299 _mesa_problem(NULL, "Trying to enable unknown extension: %s", ext);
300 }
301 }
302 }
303
304 if (!unknown_ext) {
305 free(env);
306 } else {
307 unrecognized_extensions.env = env;
308 atexit(free_unknown_extensions_strings);
309 }
310 }
311
312
313 /**
314 * \brief Initialize extension tables and enable default extensions.
315 *
316 * This should be called during context initialization.
317 * Note: Sets gl_extensions.dummy_true to true.
318 */
319 void
320 _mesa_init_extensions(struct gl_extensions *extensions)
321 {
322 GLboolean *base = (GLboolean *) extensions;
323 GLboolean *sentinel = base + o(extension_sentinel);
324 GLboolean *i;
325
326 /* First, turn all extensions off. */
327 for (i = base; i != sentinel; ++i)
328 *i = GL_FALSE;
329
330 /* Then, selectively turn default extensions on. */
331 extensions->dummy_true = GL_TRUE;
332 }
333
334
335 typedef unsigned short extension_index;
336
337
338 /**
339 * Given an extension enum, return whether or not the extension is supported
340 * dependent on the following factors:
341 * There's driver support and the OpenGL/ES version is at least that
342 * specified in the _mesa_extension_table.
343 */
344 static inline bool
345 _mesa_extension_supported(const struct gl_context *ctx, extension_index i)
346 {
347 const bool *base = (bool *) &ctx->Extensions;
348 const struct mesa_extension *ext = _mesa_extension_table + i;
349
350 return (ctx->Version >= ext->version[ctx->API]) && base[ext->offset];
351 }
352
353 /**
354 * Compare two entries of the extensions table. Sorts first by year,
355 * then by name.
356 *
357 * Arguments are indices into _mesa_extension_table.
358 */
359 static int
360 extension_compare(const void *p1, const void *p2)
361 {
362 extension_index i1 = * (const extension_index *) p1;
363 extension_index i2 = * (const extension_index *) p2;
364 const struct mesa_extension *e1 = &_mesa_extension_table[i1];
365 const struct mesa_extension *e2 = &_mesa_extension_table[i2];
366 int res;
367
368 res = (int)e1->year - (int)e2->year;
369
370 if (res == 0) {
371 res = strcmp(e1->name, e2->name);
372 }
373
374 return res;
375 }
376
377
378 /**
379 * Construct the GL_EXTENSIONS string. Called the first time that
380 * glGetString(GL_EXTENSIONS) is called.
381 */
382 GLubyte*
383 _mesa_make_extension_string(struct gl_context *ctx)
384 {
385 /* The extension string. */
386 char *exts = 0;
387 /* Length of extension string. */
388 size_t length = 0;
389 /* Number of extensions */
390 unsigned count;
391 /* Indices of the extensions sorted by year */
392 extension_index extension_indices[MESA_EXTENSION_COUNT];
393 unsigned k;
394 unsigned j;
395 unsigned maxYear = ~0;
396
397 /* Check if the MESA_EXTENSION_MAX_YEAR env var is set */
398 {
399 const char *env = getenv("MESA_EXTENSION_MAX_YEAR");
400 if (env) {
401 maxYear = atoi(env);
402 _mesa_debug(ctx, "Note: limiting GL extensions to %u or earlier\n",
403 maxYear);
404 }
405 }
406
407 /* Compute length of the extension string. */
408 count = 0;
409 for (k = 0; k < MESA_EXTENSION_COUNT; ++k) {
410 const struct mesa_extension *i = _mesa_extension_table + k;
411
412 if (i->year <= maxYear &&
413 _mesa_extension_supported(ctx, k)) {
414 length += strlen(i->name) + 1; /* +1 for space */
415 ++count;
416 }
417 }
418 for (k = 0; k < MAX_UNRECOGNIZED_EXTENSIONS; k++)
419 if (unrecognized_extensions.names[k])
420 length += 1 + strlen(unrecognized_extensions.names[k]); /* +1 for space */
421
422 exts = calloc(ALIGN(length + 1, 4), sizeof(char));
423 if (exts == NULL) {
424 return NULL;
425 }
426
427 /* Sort extensions in chronological order because idTech 2/3 games
428 * (e.g., Quake3 demo) store the extension list in a fixed size buffer.
429 * Some cases truncate, while others overflow the buffer. Resulting in
430 * misrendering and crashes, respectively.
431 * Address the former here, while the latter will be addressed by setting
432 * the MESA_EXTENSION_MAX_YEAR environment variable.
433 */
434 j = 0;
435 for (k = 0; k < MESA_EXTENSION_COUNT; ++k) {
436 if (_mesa_extension_table[k].year <= maxYear &&
437 _mesa_extension_supported(ctx, k)) {
438 extension_indices[j++] = k;
439 }
440 }
441 assert(j == count);
442 qsort(extension_indices, count,
443 sizeof *extension_indices, extension_compare);
444
445 /* Build the extension string.*/
446 for (j = 0; j < count; ++j) {
447 const struct mesa_extension *i = &_mesa_extension_table[extension_indices[j]];
448 assert(_mesa_extension_supported(ctx, extension_indices[j]));
449 strcat(exts, i->name);
450 strcat(exts, " ");
451 }
452 for (j = 0; j < MAX_UNRECOGNIZED_EXTENSIONS; j++) {
453 if (unrecognized_extensions.names[j]) {
454 strcat(exts, unrecognized_extensions.names[j]);
455 strcat(exts, " ");
456 }
457 }
458
459 return (GLubyte *) exts;
460 }
461
462 /**
463 * Return number of enabled extensions.
464 */
465 GLuint
466 _mesa_get_extension_count(struct gl_context *ctx)
467 {
468 unsigned k;
469
470 /* only count once */
471 if (ctx->Extensions.Count != 0)
472 return ctx->Extensions.Count;
473
474 for (k = 0; k < MESA_EXTENSION_COUNT; ++k) {
475 if (_mesa_extension_supported(ctx, k))
476 ctx->Extensions.Count++;
477 }
478
479 for (k = 0; k < MAX_UNRECOGNIZED_EXTENSIONS; ++k) {
480 if (unrecognized_extensions.names[k])
481 ctx->Extensions.Count++;
482 }
483 return ctx->Extensions.Count;
484 }
485
486 /**
487 * Return name of i-th enabled extension
488 */
489 const GLubyte *
490 _mesa_get_enabled_extension(struct gl_context *ctx, GLuint index)
491 {
492 size_t n = 0;
493 unsigned i;
494
495 for (i = 0; i < MESA_EXTENSION_COUNT; ++i) {
496 if (_mesa_extension_supported(ctx, i)) {
497 if (n == index)
498 return (const GLubyte*) _mesa_extension_table[i].name;
499 else
500 ++n;
501 }
502 }
503
504 for (i = 0; i < MAX_UNRECOGNIZED_EXTENSIONS; ++i) {
505 if (unrecognized_extensions.names[i]) {
506 if (n == index)
507 return (const GLubyte*) unrecognized_extensions.names[i];
508 else
509 ++n;
510 }
511 }
512 return NULL;
513 }