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