mesa: Implement GL_ARB_texture_filter_anisotropic
[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 #include "imports.h"
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 static char *extra_extensions = NULL;
43
44
45 /**
46 * Given a member \c x of struct gl_extensions, return offset of
47 * \c x in bytes.
48 */
49 #define o(x) offsetof(struct gl_extensions, x)
50
51 static bool disabled_extensions[MESA_EXTENSION_COUNT];
52
53 /**
54 * Given an extension name, lookup up the corresponding member of struct
55 * gl_extensions and return that member's index. If the name is
56 * not found in the \c _mesa_extension_table, return -1.
57 *
58 * \param name Name of extension.
59 * \return Index of member in struct gl_extensions.
60 */
61 static int
62 name_to_index(const char* name)
63 {
64 unsigned i;
65
66 if (name == 0)
67 return -1;
68
69 for (i = 0; i < MESA_EXTENSION_COUNT; ++i) {
70 if (strcmp(name, _mesa_extension_table[i].name) == 0)
71 return i;
72 }
73
74 return -1;
75 }
76
77 /**
78 * Overrides extensions in \c ctx based on the values in
79 * _mesa_extension_override_enables and _mesa_extension_override_disables.
80 */
81 static void
82 override_extensions_in_context(struct gl_context *ctx)
83 {
84 unsigned i;
85 const GLboolean *enables =
86 (GLboolean*) &_mesa_extension_override_enables;
87 const GLboolean *disables =
88 (GLboolean*) &_mesa_extension_override_disables;
89 GLboolean *ctx_ext = (GLboolean*)&ctx->Extensions;
90
91 for (i = 0; i < MESA_EXTENSION_COUNT; ++i) {
92 size_t offset = _mesa_extension_table[i].offset;
93
94 assert(!enables[offset] || !disables[offset]);
95 if (enables[offset]) {
96 ctx_ext[offset] = 1;
97 } else if (disables[offset]) {
98 ctx_ext[offset] = 0;
99 }
100 }
101 }
102
103
104 /**
105 * Enable all extensions suitable for a software-only renderer.
106 * This is a convenience function used by the XMesa, OSMesa, GGI drivers, etc.
107 */
108 void
109 _mesa_enable_sw_extensions(struct gl_context *ctx)
110 {
111 ctx->Extensions.ARB_depth_clamp = GL_TRUE;
112 ctx->Extensions.ARB_depth_texture = GL_TRUE;
113 ctx->Extensions.ARB_draw_elements_base_vertex = GL_TRUE;
114 ctx->Extensions.ARB_draw_instanced = GL_TRUE;
115 ctx->Extensions.ARB_explicit_attrib_location = GL_TRUE;
116 ctx->Extensions.ARB_fragment_coord_conventions = GL_TRUE;
117 ctx->Extensions.ARB_fragment_program = GL_TRUE;
118 ctx->Extensions.ARB_fragment_program_shadow = GL_TRUE;
119 ctx->Extensions.ARB_fragment_shader = GL_TRUE;
120 ctx->Extensions.ARB_framebuffer_object = GL_TRUE;
121 ctx->Extensions.ARB_half_float_vertex = GL_TRUE;
122 ctx->Extensions.ARB_map_buffer_range = GL_TRUE;
123 ctx->Extensions.ARB_occlusion_query = GL_TRUE;
124 ctx->Extensions.ARB_occlusion_query2 = GL_TRUE;
125 ctx->Extensions.ARB_point_sprite = GL_TRUE;
126 ctx->Extensions.ARB_shadow = GL_TRUE;
127 ctx->Extensions.ARB_texture_border_clamp = GL_TRUE;
128 ctx->Extensions.ARB_texture_compression_bptc = GL_TRUE;
129 ctx->Extensions.ARB_texture_cube_map = GL_TRUE;
130 ctx->Extensions.ARB_texture_env_combine = GL_TRUE;
131 ctx->Extensions.ARB_texture_env_crossbar = GL_TRUE;
132 ctx->Extensions.ARB_texture_env_dot3 = GL_TRUE;
133 ctx->Extensions.ARB_texture_filter_anisotropic = GL_TRUE;
134 #ifdef TEXTURE_FLOAT_ENABLED
135 ctx->Extensions.ARB_texture_float = GL_TRUE;
136 #endif
137 ctx->Extensions.ARB_texture_mirror_clamp_to_edge = GL_TRUE;
138 ctx->Extensions.ARB_texture_non_power_of_two = GL_TRUE;
139 ctx->Extensions.ARB_texture_rg = GL_TRUE;
140 ctx->Extensions.ARB_texture_compression_rgtc = GL_TRUE;
141 ctx->Extensions.ARB_vertex_program = GL_TRUE;
142 ctx->Extensions.ARB_vertex_shader = GL_TRUE;
143 ctx->Extensions.ARB_sync = GL_TRUE;
144 ctx->Extensions.APPLE_object_purgeable = GL_TRUE;
145 ctx->Extensions.ATI_fragment_shader = GL_TRUE;
146 ctx->Extensions.ATI_texture_compression_3dc = GL_TRUE;
147 ctx->Extensions.ATI_texture_env_combine3 = GL_TRUE;
148 ctx->Extensions.ATI_texture_mirror_once = GL_TRUE;
149 ctx->Extensions.ATI_separate_stencil = GL_TRUE;
150 ctx->Extensions.EXT_blend_color = GL_TRUE;
151 ctx->Extensions.EXT_blend_equation_separate = GL_TRUE;
152 ctx->Extensions.EXT_blend_func_separate = GL_TRUE;
153 ctx->Extensions.EXT_blend_minmax = GL_TRUE;
154 ctx->Extensions.EXT_depth_bounds_test = GL_TRUE;
155 ctx->Extensions.EXT_draw_buffers2 = GL_TRUE;
156 ctx->Extensions.EXT_pixel_buffer_object = GL_TRUE;
157 ctx->Extensions.EXT_point_parameters = GL_TRUE;
158 ctx->Extensions.EXT_provoking_vertex = GL_TRUE;
159 ctx->Extensions.EXT_stencil_two_side = GL_TRUE;
160 ctx->Extensions.EXT_texture_array = GL_TRUE;
161 ctx->Extensions.EXT_texture_compression_latc = GL_TRUE;
162 ctx->Extensions.EXT_texture_env_dot3 = GL_TRUE;
163 ctx->Extensions.EXT_texture_filter_anisotropic = GL_TRUE;
164 ctx->Extensions.EXT_texture_mirror_clamp = GL_TRUE;
165 ctx->Extensions.EXT_texture_shared_exponent = GL_TRUE;
166 ctx->Extensions.EXT_texture_sRGB = GL_TRUE;
167 ctx->Extensions.EXT_texture_sRGB_decode = GL_TRUE;
168 ctx->Extensions.EXT_texture_swizzle = GL_TRUE;
169 /*ctx->Extensions.EXT_transform_feedback = GL_TRUE;*/
170 ctx->Extensions.EXT_vertex_array_bgra = GL_TRUE;
171 ctx->Extensions.MESA_pack_invert = GL_TRUE;
172 ctx->Extensions.MESA_ycbcr_texture = GL_TRUE;
173 ctx->Extensions.NV_conditional_render = GL_TRUE;
174 ctx->Extensions.NV_point_sprite = GL_TRUE;
175 ctx->Extensions.NV_texture_env_combine4 = GL_TRUE;
176 ctx->Extensions.NV_texture_rectangle = GL_TRUE;
177 ctx->Extensions.EXT_gpu_program_parameters = GL_TRUE;
178 ctx->Extensions.OES_standard_derivatives = GL_TRUE;
179 ctx->Extensions.TDFX_texture_compression_FXT1 = GL_TRUE;
180 if (ctx->Mesa_DXTn) {
181 ctx->Extensions.ANGLE_texture_compression_dxt = GL_TRUE;
182 ctx->Extensions.EXT_texture_compression_s3tc = GL_TRUE;
183 }
184 }
185
186 /**
187 * Either enable or disable the named extension.
188 * \return offset of extensions withint `ext' or 0 if extension is not known
189 */
190 static size_t
191 set_extension(struct gl_extensions *ext, int i, GLboolean state)
192 {
193 size_t offset;
194
195 offset = i < 0 ? 0 : _mesa_extension_table[i].offset;
196 if (offset != 0 && (offset != o(dummy_true) || state != GL_FALSE)) {
197 ((GLboolean *) ext)[offset] = state;
198 }
199
200 return offset;
201 }
202
203 /**
204 * \brief Apply the \c MESA_EXTENSION_OVERRIDE environment variable.
205 *
206 * \c MESA_EXTENSION_OVERRIDE is a space-separated list of extensions to
207 * enable or disable. The list is processed thus:
208 * - Enable recognized extension names that are prefixed with '+'.
209 * - Disable recognized extension names that are prefixed with '-'.
210 * - Enable recognized extension names that are not prefixed.
211 * - Collect unrecognized extension names in a new string.
212 *
213 * \c MESA_EXTENSION_OVERRIDE was previously parsed during
214 * _mesa_one_time_init_extension_overrides. We just use the results of that
215 * parsing in this function.
216 *
217 * \return Space-separated list of unrecognized extension names (which must
218 * be freed). Does not return \c NULL.
219 */
220 static char *
221 get_extension_override( struct gl_context *ctx )
222 {
223 override_extensions_in_context(ctx);
224
225 if (extra_extensions == NULL) {
226 return calloc(1, sizeof(char));
227 } else {
228 _mesa_problem(ctx, "Trying to enable unknown extensions: %s",
229 extra_extensions);
230 return strdup(extra_extensions);
231 }
232 }
233
234
235 /**
236 * \brief Free extra_extensions string
237 *
238 * These strings are allocated early during the first context creation by
239 * _mesa_one_time_init_extension_overrides.
240 */
241 static void
242 free_unknown_extensions_strings(void)
243 {
244 free(extra_extensions);
245 }
246
247
248 /**
249 * \brief Initialize extension override tables.
250 *
251 * This should be called one time early during first context initialization.
252 */
253 void
254 _mesa_one_time_init_extension_overrides(void)
255 {
256 const char *env_const = getenv("MESA_EXTENSION_OVERRIDE");
257 char *env;
258 char *ext;
259 int len;
260 size_t offset;
261
262 atexit(free_unknown_extensions_strings);
263
264 memset(&_mesa_extension_override_enables, 0, sizeof(struct gl_extensions));
265 memset(&_mesa_extension_override_disables, 0, sizeof(struct gl_extensions));
266
267 if (env_const == NULL) {
268 return;
269 }
270
271 /* extra_exts: List of unrecognized extensions. */
272 extra_extensions = calloc(ALIGN(strlen(env_const) + 2, 4), sizeof(char));
273
274 /* Copy env_const because strtok() is destructive. */
275 env = strdup(env_const);
276
277 if (env == NULL ||
278 extra_extensions == NULL) {
279 free(env);
280 free(extra_extensions);
281 return;
282 }
283
284 for (ext = strtok(env, " "); ext != NULL; ext = strtok(NULL, " ")) {
285 int enable;
286 int i;
287 bool recognized;
288 switch (ext[0]) {
289 case '+':
290 enable = 1;
291 ++ext;
292 break;
293 case '-':
294 enable = 0;
295 ++ext;
296 break;
297 default:
298 enable = 1;
299 break;
300 }
301
302 i = name_to_index(ext);
303 offset = set_extension(&_mesa_extension_override_enables, i, enable);
304 if (offset != 0 && (offset != o(dummy_true) || enable != GL_FALSE)) {
305 ((GLboolean *) &_mesa_extension_override_disables)[offset] = !enable;
306 recognized = true;
307 } else {
308 recognized = false;
309 }
310
311 if (i >= 0)
312 disabled_extensions[i] = !enable;
313
314 if (!recognized && enable) {
315 strcat(extra_extensions, ext);
316 strcat(extra_extensions, " ");
317 }
318 }
319
320 free(env);
321
322 /* Remove trailing space, and free if unused. */
323 len = strlen(extra_extensions);
324 if (len == 0) {
325 free(extra_extensions);
326 extra_extensions = NULL;
327 } else if (extra_extensions[len - 1] == ' ') {
328 extra_extensions[len - 1] = '\0';
329 }
330 }
331
332
333 /**
334 * \brief Initialize extension tables and enable default extensions.
335 *
336 * This should be called during context initialization.
337 * Note: Sets gl_extensions.dummy_true to true.
338 */
339 void
340 _mesa_init_extensions(struct gl_extensions *extensions)
341 {
342 GLboolean *base = (GLboolean *) extensions;
343 GLboolean *sentinel = base + o(extension_sentinel);
344 GLboolean *i;
345
346 /* First, turn all extensions off. */
347 for (i = base; i != sentinel; ++i)
348 *i = GL_FALSE;
349
350 /* Then, selectively turn default extensions on. */
351 extensions->dummy_true = GL_TRUE;
352 }
353
354
355 typedef unsigned short extension_index;
356
357
358 /**
359 * Given an extension enum, return whether or not the extension is supported
360 * dependent on the following factors:
361 * There's driver support and the OpenGL/ES version is at least that
362 * specified in the _mesa_extension_table.
363 */
364 static inline bool
365 _mesa_extension_supported(const struct gl_context *ctx, extension_index i)
366 {
367 const bool *base = (bool *) &ctx->Extensions;
368 const struct mesa_extension *ext = _mesa_extension_table + i;
369
370 return !disabled_extensions[i] &&
371 (ctx->Version >= ext->version[ctx->API]) && base[ext->offset];
372 }
373
374 /**
375 * Compare two entries of the extensions table. Sorts first by year,
376 * then by name.
377 *
378 * Arguments are indices into _mesa_extension_table.
379 */
380 static int
381 extension_compare(const void *p1, const void *p2)
382 {
383 extension_index i1 = * (const extension_index *) p1;
384 extension_index i2 = * (const extension_index *) p2;
385 const struct mesa_extension *e1 = &_mesa_extension_table[i1];
386 const struct mesa_extension *e2 = &_mesa_extension_table[i2];
387 int res;
388
389 res = (int)e1->year - (int)e2->year;
390
391 if (res == 0) {
392 res = strcmp(e1->name, e2->name);
393 }
394
395 return res;
396 }
397
398
399 /**
400 * Construct the GL_EXTENSIONS string. Called the first time that
401 * glGetString(GL_EXTENSIONS) is called.
402 */
403 GLubyte*
404 _mesa_make_extension_string(struct gl_context *ctx)
405 {
406 /* The extension string. */
407 char *exts = 0;
408 /* Length of extension string. */
409 size_t length = 0;
410 /* Number of extensions */
411 unsigned count;
412 /* Indices of the extensions sorted by year */
413 extension_index *extension_indices;
414 /* String of extra extensions. */
415 char *extra_extensions = get_extension_override(ctx);
416 unsigned k;
417 unsigned j;
418 unsigned maxYear = ~0;
419
420 /* Check if the MESA_EXTENSION_MAX_YEAR env var is set */
421 {
422 const char *env = getenv("MESA_EXTENSION_MAX_YEAR");
423 if (env) {
424 maxYear = atoi(env);
425 _mesa_debug(ctx, "Note: limiting GL extensions to %u or earlier\n",
426 maxYear);
427 }
428 }
429
430 /* Compute length of the extension string. */
431 count = 0;
432 for (k = 0; k < MESA_EXTENSION_COUNT; ++k) {
433 const struct mesa_extension *i = _mesa_extension_table + k;
434
435 if (i->year <= maxYear &&
436 _mesa_extension_supported(ctx, k)) {
437 length += strlen(i->name) + 1; /* +1 for space */
438 ++count;
439 }
440 }
441 if (extra_extensions != NULL)
442 length += 1 + strlen(extra_extensions); /* +1 for space */
443
444 exts = calloc(ALIGN(length + 1, 4), sizeof(char));
445 if (exts == NULL) {
446 free(extra_extensions);
447 return NULL;
448 }
449
450 extension_indices = malloc(count * sizeof(extension_index));
451 if (extension_indices == NULL) {
452 free(exts);
453 free(extra_extensions);
454 return NULL;
455 }
456
457 /* Sort extensions in chronological order because certain old applications
458 * (e.g., Quake3 demo) store the extension list in a static size buffer so
459 * chronologically order ensure that the extensions that such applications
460 * expect will fit into that buffer.
461 */
462 j = 0;
463 for (k = 0; k < MESA_EXTENSION_COUNT; ++k) {
464 if (_mesa_extension_table[k].year <= maxYear &&
465 _mesa_extension_supported(ctx, k)) {
466 extension_indices[j++] = k;
467 }
468 }
469 assert(j == count);
470 qsort(extension_indices, count,
471 sizeof *extension_indices, extension_compare);
472
473 /* Build the extension string.*/
474 for (j = 0; j < count; ++j) {
475 const struct mesa_extension *i = &_mesa_extension_table[extension_indices[j]];
476 assert(_mesa_extension_supported(ctx, extension_indices[j]));
477 strcat(exts, i->name);
478 strcat(exts, " ");
479 }
480 free(extension_indices);
481 if (extra_extensions != 0) {
482 strcat(exts, extra_extensions);
483 free(extra_extensions);
484 }
485
486 return (GLubyte *) exts;
487 }
488
489 /**
490 * Return number of enabled extensions.
491 */
492 GLuint
493 _mesa_get_extension_count(struct gl_context *ctx)
494 {
495 unsigned k;
496
497 /* only count once */
498 if (ctx->Extensions.Count != 0)
499 return ctx->Extensions.Count;
500
501 for (k = 0; k < MESA_EXTENSION_COUNT; ++k) {
502 if (_mesa_extension_supported(ctx, k))
503 ctx->Extensions.Count++;
504 }
505 return ctx->Extensions.Count;
506 }
507
508 /**
509 * Return name of i-th enabled extension
510 */
511 const GLubyte *
512 _mesa_get_enabled_extension(struct gl_context *ctx, GLuint index)
513 {
514 size_t n = 0;
515 unsigned i;
516
517 for (i = 0; i < MESA_EXTENSION_COUNT; ++i) {
518 if (_mesa_extension_supported(ctx, i)) {
519 if (n == index)
520 return (const GLubyte*) _mesa_extension_table[i].name;
521 else
522 ++n;
523 }
524 }
525
526 return NULL;
527 }