[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_color_buffer_float", o(ARB_color_buffer_float), 2004 },
71 { "GL_ARB_conservative_depth", o(ARB_conservative_depth), 2011 },
72 { "GL_ARB_fragment_program", o(ARB_fragment_program), 2002 },
73 { "GL_ARB_fragment_program_shadow", o(ARB_fragment_program_shadow), 2003 },
74 { "GL_ARB_fragment_shader", o(ARB_fragment_shader), 2002 },
75 { "GL_ARB_framebuffer_object", o(ARB_framebuffer_object), 2005 },
76 { "GL_ARB_half_float_pixel", o(ARB_half_float_pixel), 2003 },
77 { "GL_ARB_half_float_vertex", o(ARB_half_float_vertex), 2008 },
78 { "GL_ARB_map_buffer_range", o(ARB_map_buffer_range), 2008 },
79 { "GL_ARB_multisample", o(dummy_true), 1994 },
80 { "GL_ARB_multitexture", o(dummy_true), 1998 },
81 { "GL_ARB_pixel_buffer_object", o(EXT_pixel_buffer_object), 2004 },
82 { "GL_ARB_point_parameters", o(EXT_point_parameters), 1997 },
83 { "GL_ARB_point_sprite", o(ARB_point_sprite), 2003 },
84 { "GL_ARB_robustness", o(dummy_true), 2010 },
85 { "GL_ARB_shader_objects", o(ARB_shader_objects), 2002 },
86 { "GL_ARB_shader_stencil_export", o(ARB_shader_stencil_export), 2009 },
87 { "GL_ARB_shader_texture_lod", o(ARB_shader_texture_lod), 2009 },
88 { "GL_ARB_shading_language_100", o(ARB_shading_language_100), 2003 },
89 { "GL_ARB_texture_border_clamp", o(ARB_texture_border_clamp), 2000 },
90 { "GL_ARB_texture_cube_map", o(ARB_texture_cube_map), 1999 },
91 { "GL_ARB_texture_env_add", o(dummy_true), 1999 },
92 { "GL_ARB_texture_env_combine", o(ARB_texture_env_combine), 2001 },
93 { "GL_ARB_texture_env_crossbar", o(ARB_texture_env_crossbar), 2001 },
94 { "GL_ARB_texture_env_dot3", o(ARB_texture_env_dot3), 2001 },
95 { "GL_ARB_texture_float", o(ARB_texture_float), 2004 },
96 { "GL_ARB_texture_mirrored_repeat", o(dummy_true), 2001 },
97 { "GL_ARB_texture_non_power_of_two", o(ARB_texture_non_power_of_two), 2003 },
98 { "GL_ARB_texture_storage", o(ARB_texture_storage), 2011 },
99 { "GL_ARB_transpose_matrix", o(ARB_transpose_matrix), 1999 },
100 { "GL_ARB_vertex_array_object", o(ARB_vertex_array_object), 2006 },
101 { "GL_ARB_vertex_buffer_object", o(dummy_true), 2003 },
102 { "GL_ARB_vertex_program", o(ARB_vertex_program), 2002 },
103 { "GL_ARB_vertex_shader", o(ARB_vertex_shader), 2002 },
104 { "GL_ARB_window_pos", o(ARB_window_pos), 2001 },
105 /* EXT extensions */
106 { "GL_EXT_abgr", o(dummy_true), 1995 },
107 { "GL_EXT_bgra", o(dummy_true), 1995 },
108 { "GL_EXT_blend_color", o(EXT_blend_color), 1995 },
109 { "GL_EXT_blend_equation_separate", o(EXT_blend_equation_separate), 2003 },
110 { "GL_EXT_blend_func_separate", o(EXT_blend_func_separate), 1999 },
111 { "GL_EXT_blend_minmax", o(EXT_blend_minmax), 1995 },
112 { "GL_EXT_blend_subtract", o(dummy_true), 1995 },
113 { "GL_EXT_clip_volume_hint", o(EXT_clip_volume_hint), 1996 },
114 { "GL_EXT_compiled_vertex_array", o(EXT_compiled_vertex_array), 1996 },
115 { "GL_EXT_copy_texture", o(dummy_true), 1995 },
116 { "GL_EXT_depth_bounds_test", o(EXT_depth_bounds_test), 2002 },
117 { "GL_EXT_draw_range_elements", o(EXT_draw_range_elements), 1997 },
118 { "GL_EXT_fog_coord", o(EXT_fog_coord), 1999 },
119 { "GL_EXT_framebuffer_blit", o(EXT_framebuffer_blit), 2005 },
120 { "GL_EXT_framebuffer_multisample", o(EXT_framebuffer_multisample), 2005 },
121 { "GL_EXT_framebuffer_object", o(EXT_framebuffer_object), 2000 },
122 { "GL_EXT_gpu_program_parameters", o(EXT_gpu_program_parameters), 2006 },
123 { "GL_EXT_gpu_shader4", o(EXT_gpu_shader4), 2006 },
124 { "GL_EXT_multi_draw_arrays", o(dummy_true), 1999 },
125 { "GL_EXT_packed_pixels", o(EXT_packed_pixels), 1997 },
126 { "GL_EXT_pixel_buffer_object", o(EXT_pixel_buffer_object), 2004 },
127 { "GL_EXT_point_parameters", o(EXT_point_parameters), 1997 },
128 { "GL_EXT_polygon_offset", o(dummy_true), 1995 },
129 { "GL_EXT_rescale_normal", o(EXT_rescale_normal), 1997 },
130 { "GL_EXT_secondary_color", o(EXT_secondary_color), 1999 },
131 { "GL_EXT_separate_shader_objects", o(EXT_separate_shader_objects), 2008 },
132 { "GL_EXT_separate_specular_color", o(EXT_separate_specular_color), 1997 },
133 { "GL_EXT_shadow_funcs", o(EXT_shadow_funcs), 2002 },
134 { "GL_EXT_stencil_two_side", o(EXT_stencil_two_side), 2001 },
135 { "GL_EXT_stencil_wrap", o(dummy_true), 2002 },
136 { "GL_EXT_subtexture", o(dummy_true), 1995 },
137 { "GL_EXT_texture3D", o(EXT_texture3D), 1996 },
138 { "GL_EXT_texture_cube_map", o(ARB_texture_cube_map), 2001 },
139 { "GL_EXT_texture_edge_clamp", o(dummy_true), 1997 },
140 { "GL_EXT_texture_env_add", o(dummy_true), 1999 },
141 { "GL_EXT_texture_env_combine", o(dummy_true), 2000 },
142 { "GL_EXT_texture_env_dot3", o(EXT_texture_env_dot3), 2000 },
143 { "GL_EXT_texture_filter_anisotropic", o(EXT_texture_filter_anisotropic), 1999 },
144 { "GL_EXT_texture_integer", o(EXT_texture_integer), 2006 },
145 { "GL_EXT_texture_lod_bias", o(dummy_true), 1999 },
146 { "GL_EXT_texture_mirror_clamp", o(EXT_texture_mirror_clamp), 2004 },
147 { "GL_EXT_texture_object", o(dummy_true), 1995 },
148 { "GL_EXT_texture", o(dummy_true), 1996 },
149 { "GL_EXT_vertex_array", o(dummy_true), 1995 },
150
151 /* Vendor extensions */
152 { "GL_AMD_conservative_depth", o(ARB_conservative_depth), 2009 },
153 { "GL_AMD_shader_stencil_export", o(ARB_shader_stencil_export), 2009 },
154 { "GL_APPLE_object_purgeable", o(APPLE_object_purgeable), 2006 },
155 { "GL_APPLE_packed_pixels", o(APPLE_packed_pixels), 2002 },
156 { "GL_APPLE_vertex_array_object", o(APPLE_vertex_array_object), 2002 },
157 { "GL_ATI_blend_equation_separate", o(EXT_blend_equation_separate), 2003 },
158 { "GL_ATI_draw_buffers", o(dummy_true), 2002 },
159 { "GL_ATI_envmap_bumpmap", o(ATI_envmap_bumpmap), 2001 },
160 { "GL_ATI_separate_stencil", o(ATI_separate_stencil), 2006 },
161 { "GL_ATI_texture_env_combine3", o(ATI_texture_env_combine3), 2002 },
162 { "GL_ATI_texture_float", o(ARB_texture_float), 2002 },
163 { "GL_ATI_texture_mirror_once", o(ATI_texture_mirror_once), 2006 },
164 { "GL_IBM_multimode_draw_arrays", o(IBM_multimode_draw_arrays), 1998 },
165 { "GL_IBM_rasterpos_clip", o(IBM_rasterpos_clip), 1996 },
166 { "GL_IBM_texture_mirrored_repeat", o(dummy_true), 1998 },
167 { "GL_INGR_blend_func_separate", o(EXT_blend_func_separate), 1999 },
168 { "GL_MESA_pack_invert", o(MESA_pack_invert), 2002 },
169 { "GL_MESA_resize_buffers", o(MESA_resize_buffers), 1999 },
170 { "GL_MESA_window_pos", o(ARB_window_pos), 2000 },
171 { "GL_MESA_ycbcr_texture", o(MESA_ycbcr_texture), 2002 },
172 { "GL_NV_blend_square", o(NV_blend_square), 1999 },
173 { "GL_NV_fog_distance", o(NV_fog_distance), 2001 },
174 { "GL_NV_fragment_program", o(NV_fragment_program), 2001 },
175 { "GL_NV_fragment_program_option", o(NV_fragment_program_option), 2005 },
176 { "GL_NV_light_max_exponent", o(NV_light_max_exponent), 1999 },
177 { "GL_NV_point_sprite", o(NV_point_sprite), 2001 },
178 { "GL_NV_texgen_reflection", o(NV_texgen_reflection), 1999 },
179 { "GL_NV_texture_barrier", o(NV_texture_barrier), 2009 },
180 { "GL_NV_texture_env_combine4", o(NV_texture_env_combine4), 1999 },
181 { "GL_NV_vertex_program1_1", o(NV_vertex_program1_1), 2001 },
182 { "GL_NV_vertex_program", o(NV_vertex_program), 2000 },
183 { "GL_SGIS_generate_mipmap", o(dummy_true), 1997 },
184 { "GL_SGIS_texture_border_clamp", o(ARB_texture_border_clamp), 1997 },
185 { "GL_SGIS_texture_edge_clamp", o(dummy_true), 1997 },
186 { "GL_SGIS_texture_lod", o(SGIS_texture_lod), 1997 },
187 { "GL_SUN_multi_draw_arrays", o(dummy_true), 1999 },
188
189 { 0, 0, 0},
190 };
191
192
193 /**
194 * Given an extension name, lookup up the corresponding member of struct
195 * gl_extensions and return that member's offset (in bytes). If the name is
196 * not found in the \c extension_table, return 0.
197 *
198 * \param name Name of extension.
199 * \return Offset of member in struct gl_extensions.
200 */
201 static size_t
202 name_to_offset(const char* name)
203 {
204 const struct extension *i;
205
206 if (name == 0)
207 return 0;
208
209 for (i = extension_table; i->name != 0; ++i) {
210 if (strcmp(name, i->name) == 0)
211 return i->offset;
212 }
213
214 return 0;
215 }
216
217
218 /**
219 * \brief Extensions enabled by default.
220 *
221 * These extensions are enabled by _mesa_init_extensions().
222 *
223 * XXX: Should these defaults also apply to GLES?
224 */
225 static const size_t default_extensions[] = {
226 o(ARB_transpose_matrix),
227 o(ARB_window_pos),
228
229 o(EXT_compiled_vertex_array),
230 o(EXT_draw_range_elements),
231 o(EXT_packed_pixels),
232 o(EXT_rescale_normal),
233 o(EXT_separate_specular_color),
234 o(EXT_texture3D),
235
236 /* Vendor Extensions */
237 o(APPLE_packed_pixels),
238 o(IBM_multimode_draw_arrays),
239 o(IBM_rasterpos_clip),
240 o(NV_light_max_exponent),
241 o(NV_texgen_reflection),
242 o(SGIS_texture_lod),
243
244 0,
245 };
246
247
248 /**
249 * Enable all extensions suitable for a software-only renderer.
250 * This is a convenience function used by the XMesa, OSMesa, GGI drivers, etc.
251 */
252 void
253 _mesa_enable_sw_extensions(struct gl_context *ctx)
254 {
255 #if FEATURE_ARB_fragment_program
256 ctx->Extensions.ARB_fragment_program = GL_TRUE;
257 ctx->Extensions.ARB_fragment_program_shadow = GL_TRUE;
258 #endif
259 #if FEATURE_ARB_fragment_shader
260 ctx->Extensions.ARB_fragment_shader = GL_TRUE;
261 #endif
262 #if FEATURE_ARB_framebuffer_object
263 ctx->Extensions.ARB_framebuffer_object = GL_TRUE;
264 #endif
265 ctx->Extensions.ARB_half_float_pixel = GL_TRUE;
266 ctx->Extensions.ARB_half_float_vertex = GL_TRUE;
267 ctx->Extensions.ARB_map_buffer_range = GL_TRUE;
268 ctx->Extensions.ARB_point_sprite = GL_TRUE;
269 #if FEATURE_ARB_shader_objects
270 ctx->Extensions.ARB_shader_objects = GL_TRUE;
271 ctx->Extensions.EXT_separate_shader_objects = GL_TRUE;
272 #endif
273 #if FEATURE_ARB_shading_language_100
274 ctx->Extensions.ARB_shading_language_100 = GL_TRUE;
275 #endif
276 ctx->Extensions.ARB_texture_border_clamp = GL_TRUE;
277 ctx->Extensions.ARB_texture_cube_map = GL_TRUE;
278 ctx->Extensions.ARB_texture_env_combine = GL_TRUE;
279 ctx->Extensions.ARB_texture_env_crossbar = GL_TRUE;
280 ctx->Extensions.ARB_texture_env_dot3 = GL_TRUE;
281 /*ctx->Extensions.ARB_texture_float = GL_TRUE;*/
282 ctx->Extensions.ARB_texture_non_power_of_two = GL_TRUE;
283 ctx->Extensions.ARB_texture_storage = GL_TRUE;
284 ctx->Extensions.ARB_vertex_array_object = GL_TRUE;
285 #if FEATURE_ARB_vertex_program
286 ctx->Extensions.ARB_vertex_program = GL_TRUE;
287 #endif
288 #if FEATURE_ARB_vertex_shader
289 ctx->Extensions.ARB_vertex_shader = GL_TRUE;
290 #endif
291 ctx->Extensions.APPLE_vertex_array_object = GL_TRUE;
292 #if FEATURE_APPLE_object_purgeable
293 ctx->Extensions.APPLE_object_purgeable = GL_TRUE;
294 #endif
295 ctx->Extensions.ATI_envmap_bumpmap = GL_TRUE;
296 ctx->Extensions.ATI_texture_env_combine3 = GL_TRUE;
297 ctx->Extensions.ATI_texture_mirror_once = GL_TRUE;
298 ctx->Extensions.ATI_separate_stencil = GL_TRUE;
299 ctx->Extensions.EXT_blend_color = GL_TRUE;
300 ctx->Extensions.EXT_blend_equation_separate = GL_TRUE;
301 ctx->Extensions.EXT_blend_func_separate = GL_TRUE;
302 ctx->Extensions.EXT_blend_minmax = GL_TRUE;
303 ctx->Extensions.EXT_depth_bounds_test = GL_TRUE;
304 ctx->Extensions.EXT_fog_coord = GL_TRUE;
305 #if FEATURE_EXT_framebuffer_object
306 ctx->Extensions.EXT_framebuffer_object = GL_TRUE;
307 #endif
308 #if FEATURE_EXT_framebuffer_blit
309 ctx->Extensions.EXT_framebuffer_blit = GL_TRUE;
310 #endif
311 #if FEATURE_ARB_framebuffer_object
312 ctx->Extensions.EXT_framebuffer_multisample = GL_TRUE;
313 #endif
314 #if FEATURE_EXT_pixel_buffer_object
315 ctx->Extensions.EXT_pixel_buffer_object = GL_TRUE;
316 #endif
317 ctx->Extensions.EXT_point_parameters = GL_TRUE;
318 ctx->Extensions.EXT_shadow_funcs = GL_TRUE;
319 ctx->Extensions.EXT_secondary_color = GL_TRUE;
320 ctx->Extensions.EXT_stencil_two_side = GL_TRUE;
321 ctx->Extensions.EXT_texture_env_dot3 = GL_TRUE;
322 ctx->Extensions.EXT_texture_filter_anisotropic = GL_TRUE;
323 ctx->Extensions.EXT_texture_mirror_clamp = GL_TRUE;
324 /*ctx->Extensions.IBM_multimode_draw_arrays = GL_TRUE;*/
325 ctx->Extensions.MESA_pack_invert = GL_TRUE;
326 ctx->Extensions.MESA_resize_buffers = GL_TRUE;
327 ctx->Extensions.MESA_ycbcr_texture = GL_TRUE;
328 ctx->Extensions.NV_blend_square = GL_TRUE;
329 /*ctx->Extensions.NV_light_max_exponent = GL_TRUE;*/
330 ctx->Extensions.NV_point_sprite = GL_TRUE;
331 ctx->Extensions.NV_texture_env_combine4 = GL_TRUE;
332 /*ctx->Extensions.NV_texgen_reflection = GL_TRUE;*/
333 #if FEATURE_NV_vertex_program
334 ctx->Extensions.NV_vertex_program = GL_TRUE;
335 ctx->Extensions.NV_vertex_program1_1 = GL_TRUE;
336 #endif
337 #if FEATURE_NV_fragment_program
338 ctx->Extensions.NV_fragment_program = GL_TRUE;
339 #endif
340 #if FEATURE_NV_fragment_program && FEATURE_ARB_fragment_program
341 ctx->Extensions.NV_fragment_program_option = GL_TRUE;
342 #endif
343 #if FEATURE_ARB_vertex_program || FEATURE_ARB_fragment_program
344 ctx->Extensions.EXT_gpu_program_parameters = GL_TRUE;
345 #endif
346 #if FEATURE_texture_fxt1
347 _mesa_enable_extension(ctx, "GL_3DFX_texture_compression_FXT1");
348 #endif
349 #if FEATURE_texture_s3tc
350 if (ctx->Mesa_DXTn) {
351 _mesa_enable_extension(ctx, "GL_EXT_texture_compression_s3tc");
352 _mesa_enable_extension(ctx, "GL_S3_s3tc");
353 }
354 #endif
355 }
356
357
358 /**
359 * Enable all OpenGL 1.3 features and extensions.
360 * A convenience function to be called by drivers.
361 */
362 void
363 _mesa_enable_1_3_extensions(struct gl_context *ctx)
364 {
365 ctx->Extensions.ARB_texture_border_clamp = GL_TRUE;
366 ctx->Extensions.ARB_texture_cube_map = GL_TRUE;
367 ctx->Extensions.ARB_texture_env_combine = GL_TRUE;
368 ctx->Extensions.ARB_texture_env_dot3 = GL_TRUE;
369 /*ctx->Extensions.ARB_transpose_matrix = GL_TRUE;*/
370 }
371
372
373
374 /**
375 * Enable all OpenGL 1.4 features and extensions.
376 * A convenience function to be called by drivers.
377 */
378 void
379 _mesa_enable_1_4_extensions(struct gl_context *ctx)
380 {
381 ctx->Extensions.ARB_texture_env_crossbar = GL_TRUE;
382 ctx->Extensions.ARB_window_pos = GL_TRUE;
383 ctx->Extensions.EXT_blend_color = GL_TRUE;
384 ctx->Extensions.EXT_blend_func_separate = GL_TRUE;
385 ctx->Extensions.EXT_blend_minmax = GL_TRUE;
386 ctx->Extensions.EXT_fog_coord = GL_TRUE;
387 ctx->Extensions.EXT_point_parameters = GL_TRUE;
388 ctx->Extensions.EXT_secondary_color = GL_TRUE;
389 }
390
391
392 /**
393 * Enable all OpenGL 1.5 features and extensions.
394 * A convenience function to be called by drivers.
395 */
396 void
397 _mesa_enable_1_5_extensions(struct gl_context *ctx)
398 {
399 ctx->Extensions.EXT_shadow_funcs = GL_TRUE;
400 }
401
402
403 /**
404 * Enable all OpenGL 2.0 features and extensions.
405 * A convenience function to be called by drivers.
406 */
407 void
408 _mesa_enable_2_0_extensions(struct gl_context *ctx)
409 {
410 #if FEATURE_ARB_fragment_shader
411 ctx->Extensions.ARB_fragment_shader = GL_TRUE;
412 #endif
413 ctx->Extensions.ARB_point_sprite = GL_TRUE;
414 ctx->Extensions.EXT_blend_equation_separate = GL_TRUE;
415 ctx->Extensions.ARB_texture_non_power_of_two = GL_TRUE;
416 #if FEATURE_ARB_shader_objects
417 ctx->Extensions.ARB_shader_objects = GL_TRUE;
418 #endif
419 #if FEATURE_ARB_shading_language_100
420 ctx->Extensions.ARB_shading_language_100 = GL_TRUE;
421 #endif
422 ctx->Extensions.EXT_stencil_two_side = GL_TRUE;
423 #if FEATURE_ARB_vertex_shader
424 ctx->Extensions.ARB_vertex_shader = GL_TRUE;
425 #endif
426 }
427
428
429 /**
430 * Enable all OpenGL 2.1 features and extensions.
431 * A convenience function to be called by drivers.
432 */
433 void
434 _mesa_enable_2_1_extensions(struct gl_context *ctx)
435 {
436 #if FEATURE_EXT_pixel_buffer_object
437 ctx->Extensions.EXT_pixel_buffer_object = GL_TRUE;
438 #endif
439 }
440
441
442 /**
443 * Either enable or disable the named extension.
444 * \return GL_TRUE for success, GL_FALSE if invalid extension name
445 */
446 static GLboolean
447 set_extension( struct gl_context *ctx, const char *name, GLboolean state )
448 {
449 size_t offset;
450
451 if (ctx->Extensions.String) {
452 /* The string was already queried - can't change it now! */
453 _mesa_problem(ctx, "Trying to enable/disable extension after glGetString(GL_EXTENSIONS): %s", name);
454 return GL_FALSE;
455 }
456
457 offset = name_to_offset(name);
458 if (offset == 0) {
459 _mesa_problem(ctx, "Trying to enable/disable unknown extension %s",
460 name);
461 return GL_FALSE;
462 } else if (offset == o(dummy_true) && state == GL_FALSE) {
463 _mesa_problem(ctx, "Trying to disable a permanently enabled extension: "
464 "%s", name);
465 return GL_FALSE;
466 } else {
467 GLboolean *base = (GLboolean *) &ctx->Extensions;
468 base[offset] = state;
469 return GL_TRUE;
470 }
471 }
472
473
474 /**
475 * Enable the named extension.
476 * Typically called by drivers.
477 */
478 void
479 _mesa_enable_extension( struct gl_context *ctx, const char *name )
480 {
481 if (!set_extension(ctx, name, GL_TRUE))
482 _mesa_problem(ctx, "Trying to enable unknown extension: %s", name);
483 }
484
485
486 /**
487 * Disable the named extension.
488 * XXX is this really needed???
489 */
490 void
491 _mesa_disable_extension( struct gl_context *ctx, const char *name )
492 {
493 if (!set_extension(ctx, name, GL_FALSE))
494 _mesa_problem(ctx, "Trying to disable unknown extension: %s", name);
495 }
496
497
498 /**
499 * Test if the named extension is enabled in this context.
500 */
501 GLboolean
502 _mesa_extension_is_enabled( struct gl_context *ctx, const char *name )
503 {
504 size_t offset;
505 GLboolean *base;
506
507 if (name == 0)
508 return GL_FALSE;
509
510 offset = name_to_offset(name);
511 if (offset == 0)
512 return GL_FALSE;
513 base = (GLboolean *) &ctx->Extensions;
514 return base[offset];
515 }
516
517
518 /**
519 * \brief Apply the \c MESA_EXTENSION_OVERRIDE environment variable.
520 *
521 * \c MESA_EXTENSION_OVERRIDE is a space-separated list of extensions to
522 * enable or disable. The list is processed thus:
523 * - Enable recognized extension names that are prefixed with '+'.
524 * - Disable recognized extension names that are prefixed with '-'.
525 * - Enable recognized extension names that are not prefixed.
526 * - Collect unrecognized extension names in a new string.
527 *
528 * \return Space-separated list of unrecognized extension names (which must
529 * be freed). Does not return \c NULL.
530 */
531 static char *
532 get_extension_override( struct gl_context *ctx )
533 {
534 const char *env_const = _mesa_getenv("MESA_EXTENSION_OVERRIDE");
535 char *env;
536 char *ext;
537 char *extra_exts;
538 int len;
539
540 if (env_const == NULL) {
541 /* Return the empty string rather than NULL. This simplifies the logic
542 * of client functions. */
543 return calloc(4, sizeof(char));
544 }
545
546 /* extra_exts: List of unrecognized extensions. */
547 extra_exts = calloc(ALIGN(strlen(env_const) + 2, 4), sizeof(char));
548
549 /* Copy env_const because strtok() is destructive. */
550 env = strdup(env_const);
551 for (ext = strtok(env, " "); ext != NULL; ext = strtok(NULL, " ")) {
552 int enable;
553 int recognized;
554 switch (ext[0]) {
555 case '+':
556 enable = 1;
557 ++ext;
558 break;
559 case '-':
560 enable = 0;
561 ++ext;
562 break;
563 default:
564 enable = 1;
565 break;
566 }
567 recognized = set_extension(ctx, ext, enable);
568 if (!recognized) {
569 strcat(extra_exts, ext);
570 strcat(extra_exts, " ");
571 }
572 }
573
574 free(env);
575
576 /* Remove trailing space. */
577 len = strlen(extra_exts);
578 if (extra_exts[len - 1] == ' ')
579 extra_exts[len - 1] = '\0';
580
581 return extra_exts;
582 }
583
584
585 /**
586 * \brief Initialize extension tables and enable default extensions.
587 *
588 * This should be called during context initialization.
589 * Note: Sets gl_extensions.dummy_true to true.
590 */
591 void
592 _mesa_init_extensions( struct gl_context *ctx )
593 {
594 GLboolean *base = (GLboolean *) &ctx->Extensions;
595 GLboolean *sentinel = base + o(extension_sentinel);
596 GLboolean *i;
597 const size_t *j;
598
599 /* First, turn all extensions off. */
600 for (i = base; i != sentinel; ++i)
601 *i = GL_FALSE;
602
603 /* Then, selectively turn default extensions on. */
604 ctx->Extensions.dummy_true = GL_TRUE;
605 for (j = default_extensions; *j != 0; ++j)
606 base[*j] = GL_TRUE;
607 }
608
609
610 typedef unsigned short extension_index;
611
612
613 /**
614 * Compare two entries of the extensions table. Sorts first by year,
615 * then by name.
616 *
617 * Arguments are indices into extension_table.
618 */
619 static int
620 extension_compare(const void *p1, const void *p2)
621 {
622 extension_index i1 = * (const extension_index *) p1;
623 extension_index i2 = * (const extension_index *) p2;
624 const struct extension *e1 = &extension_table[i1];
625 const struct extension *e2 = &extension_table[i2];
626 int res;
627
628 res = (int)e1->year - (int)e2->year;
629
630 if (res == 0) {
631 res = strcmp(e1->name, e2->name);
632 }
633
634 return res;
635 }
636
637
638 /**
639 * Construct the GL_EXTENSIONS string. Called the first time that
640 * glGetString(GL_EXTENSIONS) is called.
641 */
642 GLubyte*
643 _mesa_make_extension_string(struct gl_context *ctx)
644 {
645 /* The extension string. */
646 char *exts = 0;
647 /* Length of extension string. */
648 size_t length = 0;
649 /* Number of extensions */
650 unsigned count;
651 /* Indices of the extensions sorted by year */
652 extension_index *extension_indices;
653 /* String of extra extensions. */
654 char *extra_extensions = get_extension_override(ctx);
655 GLboolean *base = (GLboolean *) &ctx->Extensions;
656 const struct extension *i;
657 unsigned j;
658 unsigned maxYear = ~0;
659
660 /* Check if the MESA_EXTENSION_MAX_YEAR env var is set */
661 {
662 const char *env = getenv("MESA_EXTENSION_MAX_YEAR");
663 if (env) {
664 maxYear = atoi(env);
665 _mesa_debug(ctx, "Note: limiting GL extensions to %u or earlier\n",
666 maxYear);
667 }
668 }
669
670 /* Compute length of the extension string. */
671 count = 0;
672 for (i = extension_table; i->name != 0; ++i) {
673 if (base[i->offset] &&
674 i->year <= maxYear) {
675 length += strlen(i->name) + 1; /* +1 for space */
676 ++count;
677 }
678 }
679 if (extra_extensions != NULL)
680 length += 1 + strlen(extra_extensions); /* +1 for space */
681
682 exts = (char *) calloc(ALIGN(length + 1, 4), sizeof(char));
683 if (exts == NULL) {
684 free(extra_extensions);
685 return NULL;
686 }
687
688 extension_indices = malloc(count * sizeof(extension_index));
689 if (extension_indices == NULL) {
690 free(exts);
691 free(extra_extensions);
692 return NULL;
693 }
694
695 /* Sort extensions in chronological order because certain old applications (e.g.,
696 * Quake3 demo) store the extension list in a static size buffer so chronologically
697 * order ensure that the extensions that such applications expect will fit into
698 * that buffer.
699 */
700 j = 0;
701 for (i = extension_table; i->name != 0; ++i) {
702 if (base[i->offset] &&
703 i->year <= maxYear) {
704 extension_indices[j++] = i - extension_table;
705 }
706 }
707 assert(j == count);
708 qsort(extension_indices, count, sizeof *extension_indices, extension_compare);
709
710 /* Build the extension string.*/
711 for (j = 0; j < count; ++j) {
712 i = &extension_table[extension_indices[j]];
713 assert(base[i->offset]);
714 strcat(exts, i->name);
715 strcat(exts, " ");
716 }
717 free(extension_indices);
718 if (extra_extensions != 0) {
719 strcat(exts, extra_extensions);
720 free(extra_extensions);
721 }
722
723 return (GLubyte *) exts;
724 }
725
726 /**
727 * Return number of enabled extensions.
728 */
729 GLuint
730 _mesa_get_extension_count(struct gl_context *ctx)
731 {
732 GLboolean *base;
733 const struct extension *i;
734
735 /* only count once */
736 if (ctx->Extensions.Count != 0)
737 return ctx->Extensions.Count;
738
739 base = (GLboolean *) &ctx->Extensions;
740 for (i = extension_table; i->name != 0; ++i) {
741 if (base[i->offset]) {
742 ctx->Extensions.Count++;
743 }
744 }
745 return ctx->Extensions.Count;
746 }
747
748 /**
749 * Return name of i-th enabled extension
750 */
751 const GLubyte *
752 _mesa_get_enabled_extension(struct gl_context *ctx, GLuint index)
753 {
754 const GLboolean *base;
755 size_t n;
756 const struct extension *i;
757
758 if (index < 0)
759 return NULL;
760
761 base = (GLboolean*) &ctx->Extensions;
762 n = 0;
763 for (i = extension_table; i->name != 0; ++i) {
764 if (n == index && base[i->offset]) {
765 return (GLubyte*) i->name;
766 } else if (base[i->offset]) {
767 ++n;
768 }
769 }
770
771 return NULL;
772 }