[MESA]
[reactos.git] / reactos / dll / opengl / mesa / src / mesa / main / extensions.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.6
4 *
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27 /**
28 * \file
29 * \brief Extension handling
30 */
31
32
33 #include "glheader.h"
34 #include "imports.h"
35 #include "context.h"
36 #include "extensions.h"
37 #include "mfeatures.h"
38 #include "mtypes.h"
39
40 #define ALIGN(value, alignment) (((value) + alignment - 1) & ~(alignment - 1))
41
42 /**
43 * \brief An element of the \c extension_table.
44 */
45 struct extension {
46 /** Name of extension, such as "GL_ARB_depth_clamp". */
47 const char *name;
48
49 /** Offset (in bytes) of the corresponding member in struct gl_extensions. */
50 size_t offset;
51
52 /** Year the extension was proposed or approved. Used to sort the
53 * extension string chronologically. */
54 uint16_t year;
55 };
56
57
58 /**
59 * Given a member \c x of struct gl_extensions, return offset of
60 * \c x in bytes.
61 */
62 #define o(x) offsetof(struct gl_extensions, x)
63
64
65 /**
66 * \brief Table of supported OpenGL extensions for all API's.
67 */
68 static const struct extension extension_table[] = {
69 /* ARB Extensions */
70 { "GL_ARB_half_float_pixel", o(ARB_half_float_pixel), 2003 },
71 { "GL_ARB_half_float_vertex", o(ARB_half_float_vertex), 2008 },
72 { "GL_ARB_map_buffer_range", o(ARB_map_buffer_range), 2008 },
73 { "GL_ARB_multisample", o(dummy_true), 1994 },
74 { "GL_ARB_multitexture", o(dummy_true), 1998 },
75 { "GL_ARB_pixel_buffer_object", o(EXT_pixel_buffer_object), 2004 },
76 { "GL_ARB_point_parameters", o(EXT_point_parameters), 1997 },
77 { "GL_ARB_point_sprite", o(ARB_point_sprite), 2003 },
78 { "GL_ARB_robustness", o(dummy_true), 2010 },
79 { "GL_ARB_texture_border_clamp", o(ARB_texture_border_clamp), 2000 },
80 { "GL_ARB_texture_cube_map", o(ARB_texture_cube_map), 1999 },
81 { "GL_ARB_texture_env_add", o(dummy_true), 1999 },
82 { "GL_ARB_texture_env_combine", o(ARB_texture_env_combine), 2001 },
83 { "GL_ARB_texture_env_crossbar", o(ARB_texture_env_crossbar), 2001 },
84 { "GL_ARB_texture_env_dot3", o(ARB_texture_env_dot3), 2001 },
85 { "GL_ARB_texture_float", o(ARB_texture_float), 2004 },
86 { "GL_ARB_texture_mirrored_repeat", o(dummy_true), 2001 },
87 { "GL_ARB_texture_non_power_of_two", o(ARB_texture_non_power_of_two), 2003 },
88 { "GL_ARB_texture_storage", o(ARB_texture_storage), 2011 },
89 { "GL_ARB_transpose_matrix", o(ARB_transpose_matrix), 1999 },
90 { "GL_ARB_vertex_array_object", o(ARB_vertex_array_object), 2006 },
91 { "GL_ARB_vertex_buffer_object", o(dummy_true), 2003 },
92 { "GL_ARB_window_pos", o(ARB_window_pos), 2001 },
93 /* EXT extensions */
94 { "GL_EXT_abgr", o(dummy_true), 1995 },
95 { "GL_EXT_bgra", o(dummy_true), 1995 },
96 { "GL_EXT_blend_color", o(EXT_blend_color), 1995 },
97 { "GL_EXT_blend_equation_separate", o(EXT_blend_equation_separate), 2003 },
98 { "GL_EXT_blend_func_separate", o(EXT_blend_func_separate), 1999 },
99 { "GL_EXT_blend_minmax", o(EXT_blend_minmax), 1995 },
100 { "GL_EXT_blend_subtract", o(dummy_true), 1995 },
101 { "GL_EXT_clip_volume_hint", o(EXT_clip_volume_hint), 1996 },
102 { "GL_EXT_compiled_vertex_array", o(EXT_compiled_vertex_array), 1996 },
103 { "GL_EXT_copy_texture", o(dummy_true), 1995 },
104 { "GL_EXT_depth_bounds_test", o(EXT_depth_bounds_test), 2002 },
105 { "GL_EXT_draw_range_elements", o(EXT_draw_range_elements), 1997 },
106 { "GL_EXT_fog_coord", o(EXT_fog_coord), 1999 },
107 { "GL_EXT_multi_draw_arrays", o(dummy_true), 1999 },
108 { "GL_EXT_packed_pixels", o(EXT_packed_pixels), 1997 },
109 { "GL_EXT_pixel_buffer_object", o(EXT_pixel_buffer_object), 2004 },
110 { "GL_EXT_point_parameters", o(EXT_point_parameters), 1997 },
111 { "GL_EXT_polygon_offset", o(dummy_true), 1995 },
112 { "GL_EXT_rescale_normal", o(EXT_rescale_normal), 1997 },
113 { "GL_EXT_secondary_color", o(EXT_secondary_color), 1999 },
114 { "GL_EXT_separate_shader_objects", o(EXT_separate_shader_objects), 2008 },
115 { "GL_EXT_separate_specular_color", o(EXT_separate_specular_color), 1997 },
116 { "GL_EXT_shadow_funcs", o(EXT_shadow_funcs), 2002 },
117 { "GL_EXT_stencil_two_side", o(EXT_stencil_two_side), 2001 },
118 { "GL_EXT_stencil_wrap", o(dummy_true), 2002 },
119 { "GL_EXT_subtexture", o(dummy_true), 1995 },
120 { "GL_EXT_texture3D", o(EXT_texture3D), 1996 },
121 { "GL_EXT_texture_cube_map", o(ARB_texture_cube_map), 2001 },
122 { "GL_EXT_texture_edge_clamp", o(dummy_true), 1997 },
123 { "GL_EXT_texture_env_add", o(dummy_true), 1999 },
124 { "GL_EXT_texture_env_combine", o(dummy_true), 2000 },
125 { "GL_EXT_texture_env_dot3", o(EXT_texture_env_dot3), 2000 },
126 { "GL_EXT_texture_filter_anisotropic", o(EXT_texture_filter_anisotropic), 1999 },
127 { "GL_EXT_texture_integer", o(EXT_texture_integer), 2006 },
128 { "GL_EXT_texture_mirror_clamp", o(EXT_texture_mirror_clamp), 2004 },
129 { "GL_EXT_texture_object", o(dummy_true), 1995 },
130 { "GL_EXT_texture", o(dummy_true), 1996 },
131 { "GL_EXT_vertex_array", o(dummy_true), 1995 },
132
133 /* Vendor extensions */
134 { "GL_APPLE_object_purgeable", o(APPLE_object_purgeable), 2006 },
135 { "GL_APPLE_packed_pixels", o(APPLE_packed_pixels), 2002 },
136 { "GL_APPLE_vertex_array_object", o(APPLE_vertex_array_object), 2002 },
137 { "GL_ATI_blend_equation_separate", o(EXT_blend_equation_separate), 2003 },
138 { "GL_ATI_separate_stencil", o(ATI_separate_stencil), 2006 },
139 { "GL_ATI_texture_env_combine3", o(ATI_texture_env_combine3), 2002 },
140 { "GL_ATI_texture_float", o(ARB_texture_float), 2002 },
141 { "GL_ATI_texture_mirror_once", o(ATI_texture_mirror_once), 2006 },
142 { "GL_IBM_multimode_draw_arrays", o(IBM_multimode_draw_arrays), 1998 },
143 { "GL_IBM_rasterpos_clip", o(IBM_rasterpos_clip), 1996 },
144 { "GL_IBM_texture_mirrored_repeat", o(dummy_true), 1998 },
145 { "GL_INGR_blend_func_separate", o(EXT_blend_func_separate), 1999 },
146 { "GL_MESA_pack_invert", o(MESA_pack_invert), 2002 },
147 { "GL_MESA_resize_buffers", o(MESA_resize_buffers), 1999 },
148 { "GL_MESA_window_pos", o(ARB_window_pos), 2000 },
149 { "GL_MESA_ycbcr_texture", o(MESA_ycbcr_texture), 2002 },
150 { "GL_NV_blend_square", o(NV_blend_square), 1999 },
151 { "GL_NV_fog_distance", o(NV_fog_distance), 2001 },
152 { "GL_NV_light_max_exponent", o(NV_light_max_exponent), 1999 },
153 { "GL_NV_point_sprite", o(NV_point_sprite), 2001 },
154 { "GL_NV_texgen_reflection", o(NV_texgen_reflection), 1999 },
155 { "GL_NV_texture_barrier", o(NV_texture_barrier), 2009 },
156 { "GL_NV_texture_env_combine4", o(NV_texture_env_combine4), 1999 },
157 { "GL_SGIS_texture_border_clamp", o(ARB_texture_border_clamp), 1997 },
158 { "GL_SGIS_texture_edge_clamp", o(dummy_true), 1997 },
159 { "GL_SUN_multi_draw_arrays", o(dummy_true), 1999 },
160
161 { 0, 0, 0},
162 };
163
164
165 /**
166 * Given an extension name, lookup up the corresponding member of struct
167 * gl_extensions and return that member's offset (in bytes). If the name is
168 * not found in the \c extension_table, return 0.
169 *
170 * \param name Name of extension.
171 * \return Offset of member in struct gl_extensions.
172 */
173 static size_t
174 name_to_offset(const char* name)
175 {
176 const struct extension *i;
177
178 if (name == 0)
179 return 0;
180
181 for (i = extension_table; i->name != 0; ++i) {
182 if (strcmp(name, i->name) == 0)
183 return i->offset;
184 }
185
186 return 0;
187 }
188
189
190 /**
191 * \brief Extensions enabled by default.
192 *
193 * These extensions are enabled by _mesa_init_extensions().
194 *
195 * XXX: Should these defaults also apply to GLES?
196 */
197 static const size_t default_extensions[] = {
198 o(ARB_transpose_matrix),
199 o(ARB_window_pos),
200
201 o(EXT_compiled_vertex_array),
202 o(EXT_draw_range_elements),
203 o(EXT_packed_pixels),
204 o(EXT_rescale_normal),
205 o(EXT_separate_specular_color),
206 o(EXT_texture3D),
207
208 /* Vendor Extensions */
209 o(APPLE_packed_pixels),
210 o(IBM_multimode_draw_arrays),
211 o(IBM_rasterpos_clip),
212 o(NV_light_max_exponent),
213 o(NV_texgen_reflection),
214
215 0,
216 };
217
218
219 /**
220 * Enable all extensions suitable for a software-only renderer.
221 * This is a convenience function used by the XMesa, OSMesa, GGI drivers, etc.
222 */
223 void
224 _mesa_enable_sw_extensions(struct gl_context *ctx)
225 {
226 ctx->Extensions.ARB_half_float_pixel = GL_TRUE;
227 ctx->Extensions.ARB_half_float_vertex = GL_TRUE;
228 ctx->Extensions.ARB_map_buffer_range = GL_TRUE;
229 ctx->Extensions.ARB_point_sprite = GL_TRUE;
230 ctx->Extensions.ARB_texture_border_clamp = GL_TRUE;
231 ctx->Extensions.ARB_texture_cube_map = GL_TRUE;
232 ctx->Extensions.ARB_texture_env_combine = GL_TRUE;
233 ctx->Extensions.ARB_texture_env_crossbar = GL_TRUE;
234 ctx->Extensions.ARB_texture_env_dot3 = GL_TRUE;
235 /*ctx->Extensions.ARB_texture_float = GL_TRUE;*/
236 ctx->Extensions.ARB_texture_non_power_of_two = GL_TRUE;
237 ctx->Extensions.ARB_texture_storage = GL_TRUE;
238 ctx->Extensions.ARB_vertex_array_object = GL_TRUE;
239 ctx->Extensions.APPLE_vertex_array_object = GL_TRUE;
240 #if FEATURE_APPLE_object_purgeable
241 ctx->Extensions.APPLE_object_purgeable = GL_TRUE;
242 #endif
243 ctx->Extensions.ATI_texture_env_combine3 = GL_TRUE;
244 ctx->Extensions.ATI_texture_mirror_once = GL_TRUE;
245 ctx->Extensions.ATI_separate_stencil = GL_TRUE;
246 ctx->Extensions.EXT_blend_color = GL_TRUE;
247 ctx->Extensions.EXT_blend_equation_separate = GL_TRUE;
248 ctx->Extensions.EXT_blend_func_separate = GL_TRUE;
249 ctx->Extensions.EXT_blend_minmax = GL_TRUE;
250 ctx->Extensions.EXT_depth_bounds_test = GL_TRUE;
251 ctx->Extensions.EXT_fog_coord = GL_TRUE;
252 #if FEATURE_EXT_pixel_buffer_object
253 ctx->Extensions.EXT_pixel_buffer_object = GL_TRUE;
254 #endif
255 ctx->Extensions.EXT_point_parameters = GL_TRUE;
256 ctx->Extensions.EXT_shadow_funcs = GL_TRUE;
257 ctx->Extensions.EXT_secondary_color = GL_TRUE;
258 ctx->Extensions.EXT_stencil_two_side = GL_TRUE;
259 ctx->Extensions.EXT_texture_env_dot3 = GL_TRUE;
260 ctx->Extensions.EXT_texture_filter_anisotropic = GL_TRUE;
261 ctx->Extensions.EXT_texture_mirror_clamp = GL_TRUE;
262 /*ctx->Extensions.IBM_multimode_draw_arrays = GL_TRUE;*/
263 ctx->Extensions.MESA_pack_invert = GL_TRUE;
264 ctx->Extensions.MESA_resize_buffers = GL_TRUE;
265 ctx->Extensions.MESA_ycbcr_texture = GL_TRUE;
266 ctx->Extensions.NV_blend_square = GL_TRUE;
267 /*ctx->Extensions.NV_light_max_exponent = GL_TRUE;*/
268 ctx->Extensions.NV_point_sprite = GL_TRUE;
269 ctx->Extensions.NV_texture_env_combine4 = GL_TRUE;
270 /*ctx->Extensions.NV_texgen_reflection = GL_TRUE;*/
271 #if FEATURE_texture_fxt1
272 _mesa_enable_extension(ctx, "GL_3DFX_texture_compression_FXT1");
273 #endif
274 }
275
276
277 /**
278 * Enable all OpenGL 1.3 features and extensions.
279 * A convenience function to be called by drivers.
280 */
281 void
282 _mesa_enable_1_3_extensions(struct gl_context *ctx)
283 {
284 ctx->Extensions.ARB_texture_border_clamp = GL_TRUE;
285 ctx->Extensions.ARB_texture_cube_map = GL_TRUE;
286 ctx->Extensions.ARB_texture_env_combine = GL_TRUE;
287 ctx->Extensions.ARB_texture_env_dot3 = GL_TRUE;
288 /*ctx->Extensions.ARB_transpose_matrix = GL_TRUE;*/
289 }
290
291
292
293 /**
294 * Enable all OpenGL 1.4 features and extensions.
295 * A convenience function to be called by drivers.
296 */
297 void
298 _mesa_enable_1_4_extensions(struct gl_context *ctx)
299 {
300 ctx->Extensions.ARB_texture_env_crossbar = GL_TRUE;
301 ctx->Extensions.ARB_window_pos = GL_TRUE;
302 ctx->Extensions.EXT_blend_color = GL_TRUE;
303 ctx->Extensions.EXT_blend_func_separate = GL_TRUE;
304 ctx->Extensions.EXT_blend_minmax = GL_TRUE;
305 ctx->Extensions.EXT_fog_coord = GL_TRUE;
306 ctx->Extensions.EXT_point_parameters = GL_TRUE;
307 ctx->Extensions.EXT_secondary_color = GL_TRUE;
308 }
309
310
311 /**
312 * Enable all OpenGL 1.5 features and extensions.
313 * A convenience function to be called by drivers.
314 */
315 void
316 _mesa_enable_1_5_extensions(struct gl_context *ctx)
317 {
318 ctx->Extensions.EXT_shadow_funcs = GL_TRUE;
319 }
320
321
322 /**
323 * Enable all OpenGL 2.0 features and extensions.
324 * A convenience function to be called by drivers.
325 */
326 void
327 _mesa_enable_2_0_extensions(struct gl_context *ctx)
328 {
329 ctx->Extensions.ARB_point_sprite = GL_TRUE;
330 ctx->Extensions.EXT_blend_equation_separate = GL_TRUE;
331 ctx->Extensions.ARB_texture_non_power_of_two = GL_TRUE;
332 ctx->Extensions.EXT_stencil_two_side = GL_TRUE;
333 }
334
335
336 /**
337 * Enable all OpenGL 2.1 features and extensions.
338 * A convenience function to be called by drivers.
339 */
340 void
341 _mesa_enable_2_1_extensions(struct gl_context *ctx)
342 {
343 #if FEATURE_EXT_pixel_buffer_object
344 ctx->Extensions.EXT_pixel_buffer_object = GL_TRUE;
345 #endif
346 }
347
348
349 /**
350 * Either enable or disable the named extension.
351 * \return GL_TRUE for success, GL_FALSE if invalid extension name
352 */
353 static GLboolean
354 set_extension( struct gl_context *ctx, const char *name, GLboolean state )
355 {
356 size_t offset;
357
358 if (ctx->Extensions.String) {
359 /* The string was already queried - can't change it now! */
360 _mesa_problem(ctx, "Trying to enable/disable extension after glGetString(GL_EXTENSIONS): %s", name);
361 return GL_FALSE;
362 }
363
364 offset = name_to_offset(name);
365 if (offset == 0) {
366 _mesa_problem(ctx, "Trying to enable/disable unknown extension %s",
367 name);
368 return GL_FALSE;
369 } else if (offset == o(dummy_true) && state == GL_FALSE) {
370 _mesa_problem(ctx, "Trying to disable a permanently enabled extension: "
371 "%s", name);
372 return GL_FALSE;
373 } else {
374 GLboolean *base = (GLboolean *) &ctx->Extensions;
375 base[offset] = state;
376 return GL_TRUE;
377 }
378 }
379
380
381 /**
382 * Enable the named extension.
383 * Typically called by drivers.
384 */
385 void
386 _mesa_enable_extension( struct gl_context *ctx, const char *name )
387 {
388 if (!set_extension(ctx, name, GL_TRUE))
389 _mesa_problem(ctx, "Trying to enable unknown extension: %s", name);
390 }
391
392
393 /**
394 * Disable the named extension.
395 * XXX is this really needed???
396 */
397 void
398 _mesa_disable_extension( struct gl_context *ctx, const char *name )
399 {
400 if (!set_extension(ctx, name, GL_FALSE))
401 _mesa_problem(ctx, "Trying to disable unknown extension: %s", name);
402 }
403
404
405 /**
406 * Test if the named extension is enabled in this context.
407 */
408 GLboolean
409 _mesa_extension_is_enabled( struct gl_context *ctx, const char *name )
410 {
411 size_t offset;
412 GLboolean *base;
413
414 if (name == 0)
415 return GL_FALSE;
416
417 offset = name_to_offset(name);
418 if (offset == 0)
419 return GL_FALSE;
420 base = (GLboolean *) &ctx->Extensions;
421 return base[offset];
422 }
423
424
425 /**
426 * \brief Apply the \c MESA_EXTENSION_OVERRIDE environment variable.
427 *
428 * \c MESA_EXTENSION_OVERRIDE is a space-separated list of extensions to
429 * enable or disable. The list is processed thus:
430 * - Enable recognized extension names that are prefixed with '+'.
431 * - Disable recognized extension names that are prefixed with '-'.
432 * - Enable recognized extension names that are not prefixed.
433 * - Collect unrecognized extension names in a new string.
434 *
435 * \return Space-separated list of unrecognized extension names (which must
436 * be freed). Does not return \c NULL.
437 */
438 static char *
439 get_extension_override( struct gl_context *ctx )
440 {
441 const char *env_const = _mesa_getenv("MESA_EXTENSION_OVERRIDE");
442 char *env;
443 char *ext;
444 char *extra_exts;
445 int len;
446
447 if (env_const == NULL) {
448 /* Return the empty string rather than NULL. This simplifies the logic
449 * of client functions. */
450 return calloc(4, sizeof(char));
451 }
452
453 /* extra_exts: List of unrecognized extensions. */
454 extra_exts = calloc(ALIGN(strlen(env_const) + 2, 4), sizeof(char));
455
456 /* Copy env_const because strtok() is destructive. */
457 env = _strdup(env_const);
458 for (ext = strtok(env, " "); ext != NULL; ext = strtok(NULL, " ")) {
459 int enable;
460 int recognized;
461 switch (ext[0]) {
462 case '+':
463 enable = 1;
464 ++ext;
465 break;
466 case '-':
467 enable = 0;
468 ++ext;
469 break;
470 default:
471 enable = 1;
472 break;
473 }
474 recognized = set_extension(ctx, ext, enable);
475 if (!recognized) {
476 strcat(extra_exts, ext);
477 strcat(extra_exts, " ");
478 }
479 }
480
481 free(env);
482
483 /* Remove trailing space. */
484 len = strlen(extra_exts);
485 if (extra_exts[len - 1] == ' ')
486 extra_exts[len - 1] = '\0';
487
488 return extra_exts;
489 }
490
491
492 /**
493 * \brief Initialize extension tables and enable default extensions.
494 *
495 * This should be called during context initialization.
496 * Note: Sets gl_extensions.dummy_true to true.
497 */
498 void
499 _mesa_init_extensions( struct gl_context *ctx )
500 {
501 GLboolean *base = (GLboolean *) &ctx->Extensions;
502 GLboolean *sentinel = base + o(extension_sentinel);
503 GLboolean *i;
504 const size_t *j;
505
506 /* First, turn all extensions off. */
507 for (i = base; i != sentinel; ++i)
508 *i = GL_FALSE;
509
510 /* Then, selectively turn default extensions on. */
511 ctx->Extensions.dummy_true = GL_TRUE;
512 for (j = default_extensions; *j != 0; ++j)
513 base[*j] = GL_TRUE;
514 }
515
516
517 typedef unsigned short extension_index;
518
519
520 /**
521 * Compare two entries of the extensions table. Sorts first by year,
522 * then by name.
523 *
524 * Arguments are indices into extension_table.
525 */
526 static int
527 extension_compare(const void *p1, const void *p2)
528 {
529 extension_index i1 = * (const extension_index *) p1;
530 extension_index i2 = * (const extension_index *) p2;
531 const struct extension *e1 = &extension_table[i1];
532 const struct extension *e2 = &extension_table[i2];
533 int res;
534
535 res = (int)e1->year - (int)e2->year;
536
537 if (res == 0) {
538 res = strcmp(e1->name, e2->name);
539 }
540
541 return res;
542 }
543
544
545 /**
546 * Construct the GL_EXTENSIONS string. Called the first time that
547 * glGetString(GL_EXTENSIONS) is called.
548 */
549 GLubyte*
550 _mesa_make_extension_string(struct gl_context *ctx)
551 {
552 /* The extension string. */
553 char *exts = 0;
554 /* Length of extension string. */
555 size_t length = 0;
556 /* Number of extensions */
557 unsigned count;
558 /* Indices of the extensions sorted by year */
559 extension_index *extension_indices;
560 /* String of extra extensions. */
561 char *extra_extensions = get_extension_override(ctx);
562 GLboolean *base = (GLboolean *) &ctx->Extensions;
563 const struct extension *i;
564 unsigned j;
565 unsigned maxYear = ~0;
566
567 /* Check if the MESA_EXTENSION_MAX_YEAR env var is set */
568 {
569 const char *env = getenv("MESA_EXTENSION_MAX_YEAR");
570 if (env) {
571 maxYear = atoi(env);
572 _mesa_debug(ctx, "Note: limiting GL extensions to %u or earlier\n",
573 maxYear);
574 }
575 }
576
577 /* Compute length of the extension string. */
578 count = 0;
579 for (i = extension_table; i->name != 0; ++i) {
580 if (base[i->offset] &&
581 i->year <= maxYear) {
582 length += strlen(i->name) + 1; /* +1 for space */
583 ++count;
584 }
585 }
586 if (extra_extensions != NULL)
587 length += 1 + strlen(extra_extensions); /* +1 for space */
588
589 exts = (char *) calloc(ALIGN(length + 1, 4), sizeof(char));
590 if (exts == NULL) {
591 free(extra_extensions);
592 return NULL;
593 }
594
595 extension_indices = malloc(count * sizeof(extension_index));
596 if (extension_indices == NULL) {
597 free(exts);
598 free(extra_extensions);
599 return NULL;
600 }
601
602 /* Sort extensions in chronological order because certain old applications (e.g.,
603 * Quake3 demo) store the extension list in a static size buffer so chronologically
604 * order ensure that the extensions that such applications expect will fit into
605 * that buffer.
606 */
607 j = 0;
608 for (i = extension_table; i->name != 0; ++i) {
609 if (base[i->offset] &&
610 i->year <= maxYear) {
611 extension_indices[j++] = i - extension_table;
612 }
613 }
614 assert(j == count);
615 qsort(extension_indices, count, sizeof *extension_indices, extension_compare);
616
617 /* Build the extension string.*/
618 for (j = 0; j < count; ++j) {
619 i = &extension_table[extension_indices[j]];
620 assert(base[i->offset]);
621 strcat(exts, i->name);
622 strcat(exts, " ");
623 }
624 free(extension_indices);
625 if (extra_extensions != 0) {
626 strcat(exts, extra_extensions);
627 free(extra_extensions);
628 }
629
630 return (GLubyte *) exts;
631 }
632
633 /**
634 * Return number of enabled extensions.
635 */
636 GLuint
637 _mesa_get_extension_count(struct gl_context *ctx)
638 {
639 GLboolean *base;
640 const struct extension *i;
641
642 /* only count once */
643 if (ctx->Extensions.Count != 0)
644 return ctx->Extensions.Count;
645
646 base = (GLboolean *) &ctx->Extensions;
647 for (i = extension_table; i->name != 0; ++i) {
648 if (base[i->offset]) {
649 ctx->Extensions.Count++;
650 }
651 }
652 return ctx->Extensions.Count;
653 }
654
655 /**
656 * Return name of i-th enabled extension
657 */
658 const GLubyte *
659 _mesa_get_enabled_extension(struct gl_context *ctx, GLuint index)
660 {
661 const GLboolean *base;
662 size_t n;
663 const struct extension *i;
664
665 if (index < 0)
666 return NULL;
667
668 base = (GLboolean*) &ctx->Extensions;
669 n = 0;
670 for (i = extension_table; i->name != 0; ++i) {
671 if (n == index && base[i->offset]) {
672 return (GLubyte*) i->name;
673 } else if (base[i->offset]) {
674 ++n;
675 }
676 }
677
678 return NULL;
679 }