remove ^M chars, disable shading language extensions
[mesa.git] / src / mesa / main / imports.c
1 /**
2 * \file imports.c
3 * Standard C library function wrappers.
4 *
5 * Imports are services which the device driver or window system or
6 * operating system provides to the core renderer. The core renderer (Mesa)
7 * will call these functions in order to do memory allocation, simple I/O,
8 * etc.
9 *
10 * Some drivers will want to override/replace this file with something
11 * specialized, but that'll be rare.
12 *
13 * Eventually, I want to move roll the glheader.h file into this.
14 *
15 * The OpenGL SI's __GLimports structure allows per-context specification of
16 * replacements for the standard C lib functions. In practice that's probably
17 * never needed; compile-time replacements are far more likely.
18 *
19 * The _mesa_*() functions defined here don't in general take a context
20 * parameter. I guess we can change that someday, if need be.
21 * So for now, the __GLimports stuff really isn't used.
22 *
23 * \todo Functions still needed:
24 * - scanf
25 * - qsort
26 * - bsearch
27 * - rand and RAND_MAX
28 *
29 * \note When compiled into a XFree86 module these functions wrap around
30 * XFree86 own wrappers.
31 */
32
33 /*
34 * Mesa 3-D graphics library
35 * Version: 6.3
36 *
37 * Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
38 *
39 * Permission is hereby granted, free of charge, to any person obtaining a
40 * copy of this software and associated documentation files (the "Software"),
41 * to deal in the Software without restriction, including without limitation
42 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
43 * and/or sell copies of the Software, and to permit persons to whom the
44 * Software is furnished to do so, subject to the following conditions:
45 *
46 * The above copyright notice and this permission notice shall be included
47 * in all copies or substantial portions of the Software.
48 *
49 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
50 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
51 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
52 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
53 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
54 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
55 */
56
57
58
59 #include "imports.h"
60 #include "context.h"
61 #include "version.h"
62
63
64 #define MAXSTRING 4000 /* for vsnprintf() */
65
66 #ifdef WIN32
67 #define vsnprintf _vsnprintf
68 #elif defined(__IBMC__) || defined(__IBMCPP__) || ( defined(__VMS) && __CRTL_VER < 70312000 )
69 extern int vsnprintf(char *str, size_t count, const char *fmt, va_list arg);
70 #ifdef __VMS
71 #include "vsnprintf.c"
72 #endif
73 #endif
74
75
76 /**********************************************************************/
77 /** \name Memory */
78 /*@{*/
79
80 /** Wrapper around either malloc() or xf86malloc() */
81 void *
82 _mesa_malloc(size_t bytes)
83 {
84 #if defined(XFree86LOADER) && defined(IN_MODULE)
85 return xf86malloc(bytes);
86 #else
87 return malloc(bytes);
88 #endif
89 }
90
91 /** Wrapper around either calloc() or xf86calloc() */
92 void *
93 _mesa_calloc(size_t bytes)
94 {
95 #if defined(XFree86LOADER) && defined(IN_MODULE)
96 return xf86calloc(1, bytes);
97 #else
98 return calloc(1, bytes);
99 #endif
100 }
101
102 /** Wrapper around either free() or xf86free() */
103 void
104 _mesa_free(void *ptr)
105 {
106 #if defined(XFree86LOADER) && defined(IN_MODULE)
107 xf86free(ptr);
108 #else
109 free(ptr);
110 #endif
111 }
112
113 /**
114 * Allocate aligned memory.
115 *
116 * \param bytes number of bytes to allocate.
117 * \param alignment alignment (must be greater than zero).
118 *
119 * Allocates extra memory to accommodate rounding up the address for
120 * alignment and to record the real malloc address.
121 *
122 * \sa _mesa_align_free().
123 */
124 void *
125 _mesa_align_malloc(size_t bytes, unsigned long alignment)
126 {
127 uintptr_t ptr, buf;
128
129 ASSERT( alignment > 0 );
130
131 ptr = (uintptr_t) _mesa_malloc(bytes + alignment + sizeof(void *));
132 if (!ptr)
133 return NULL;
134
135 buf = (ptr + alignment + sizeof(void *)) & ~(uintptr_t)(alignment - 1);
136 *(uintptr_t *)(buf - sizeof(void *)) = ptr;
137
138 #ifdef DEBUG
139 /* mark the non-aligned area */
140 while ( ptr < buf - sizeof(void *) ) {
141 *(unsigned long *)ptr = 0xcdcdcdcd;
142 ptr += sizeof(unsigned long);
143 }
144 #endif
145
146 return (void *) buf;
147 }
148
149 /**
150 * Same as _mesa_align_malloc(), but using _mesa_calloc() instead of
151 * _mesa_malloc()
152 */
153 void *
154 _mesa_align_calloc(size_t bytes, unsigned long alignment)
155 {
156 uintptr_t ptr, buf;
157
158 ASSERT( alignment > 0 );
159
160 ptr = (uintptr_t) _mesa_calloc(bytes + alignment + sizeof(void *));
161 if (!ptr)
162 return NULL;
163
164 buf = (ptr + alignment + sizeof(void *)) & ~(uintptr_t)(alignment - 1);
165 *(uintptr_t *)(buf - sizeof(void *)) = ptr;
166
167 #ifdef DEBUG
168 /* mark the non-aligned area */
169 while ( ptr < buf - sizeof(void *) ) {
170 *(unsigned long *)ptr = 0xcdcdcdcd;
171 ptr += sizeof(unsigned long);
172 }
173 #endif
174
175 return (void *)buf;
176 }
177
178 /**
179 * Free memory which was allocated with either _mesa_align_malloc()
180 * or _mesa_align_calloc().
181 * \param ptr pointer to the memory to be freed.
182 * The actual address to free is stored in the word immediately before the
183 * address the client sees.
184 */
185 void
186 _mesa_align_free(void *ptr)
187 {
188 #if 0
189 _mesa_free( (void *)(*(unsigned long *)((unsigned long)ptr - sizeof(void *))) );
190 #else
191 void **cubbyHole = (void **) ((char *) ptr - sizeof(void *));
192 void *realAddr = *cubbyHole;
193 _mesa_free(realAddr);
194 #endif
195 }
196
197 /** Reallocate memory */
198 void *
199 _mesa_realloc(void *oldBuffer, size_t oldSize, size_t newSize)
200 {
201 const size_t copySize = (oldSize < newSize) ? oldSize : newSize;
202 void *newBuffer = _mesa_malloc(newSize);
203 if (newBuffer && oldBuffer && copySize > 0)
204 _mesa_memcpy(newBuffer, oldBuffer, copySize);
205 if (oldBuffer)
206 _mesa_free(oldBuffer);
207 return newBuffer;
208 }
209
210 /** memcpy wrapper */
211 void *
212 _mesa_memcpy(void *dest, const void *src, size_t n)
213 {
214 #if defined(XFree86LOADER) && defined(IN_MODULE)
215 return xf86memcpy(dest, src, n);
216 #elif defined(SUNOS4)
217 return memcpy((char *) dest, (char *) src, (int) n);
218 #else
219 return memcpy(dest, src, n);
220 #endif
221 }
222
223 /** Wrapper around either memset() or xf86memset() */
224 void
225 _mesa_memset( void *dst, int val, size_t n )
226 {
227 #if defined(XFree86LOADER) && defined(IN_MODULE)
228 xf86memset( dst, val, n );
229 #elif defined(SUNOS4)
230 memset( (char *) dst, (int) val, (int) n );
231 #else
232 memset(dst, val, n);
233 #endif
234 }
235
236 /**
237 * Fill memory with a constant 16bit word.
238 * \param dst destination pointer.
239 * \param val value.
240 * \param n number of words.
241 */
242 void
243 _mesa_memset16( unsigned short *dst, unsigned short val, size_t n )
244 {
245 while (n-- > 0)
246 *dst++ = val;
247 }
248
249 /** Wrapper around either memcpy() or xf86memcpy() or bzero() */
250 void
251 _mesa_bzero( void *dst, size_t n )
252 {
253 #if defined(XFree86LOADER) && defined(IN_MODULE)
254 xf86memset( dst, 0, n );
255 #elif defined(__FreeBSD__)
256 bzero( dst, n );
257 #else
258 memset( dst, 0, n );
259 #endif
260 }
261
262 /*@}*/
263
264
265 /**********************************************************************/
266 /** \name Math */
267 /*@{*/
268
269 /** Wrapper around either sin() or xf86sin() */
270 double
271 _mesa_sin(double a)
272 {
273 #if defined(XFree86LOADER) && defined(IN_MODULE)
274 return xf86sin(a);
275 #else
276 return sin(a);
277 #endif
278 }
279
280 /** Wrapper around either cos() or xf86cos() */
281 double
282 _mesa_cos(double a)
283 {
284 #if defined(XFree86LOADER) && defined(IN_MODULE)
285 return xf86cos(a);
286 #else
287 return cos(a);
288 #endif
289 }
290
291 /** Wrapper around either sqrt() or xf86sqrt() */
292 double
293 _mesa_sqrtd(double x)
294 {
295 #if defined(XFree86LOADER) && defined(IN_MODULE)
296 return xf86sqrt(x);
297 #else
298 return sqrt(x);
299 #endif
300 }
301
302
303 /*
304 * A High Speed, Low Precision Square Root
305 * by Paul Lalonde and Robert Dawson
306 * from "Graphics Gems", Academic Press, 1990
307 *
308 * SPARC implementation of a fast square root by table
309 * lookup.
310 * SPARC floating point format is as follows:
311 *
312 * BIT 31 30 23 22 0
313 * sign exponent mantissa
314 */
315 static short sqrttab[0x100]; /* declare table of square roots */
316
317 static void init_sqrt_table(void)
318 {
319 #if defined(USE_IEEE) && !defined(DEBUG)
320 unsigned short i;
321 fi_type fi; /* to access the bits of a float in C quickly */
322 /* we use a union defined in glheader.h */
323
324 for(i=0; i<= 0x7f; i++) {
325 fi.i = 0;
326
327 /*
328 * Build a float with the bit pattern i as mantissa
329 * and an exponent of 0, stored as 127
330 */
331
332 fi.i = (i << 16) | (127 << 23);
333 fi.f = _mesa_sqrtd(fi.f);
334
335 /*
336 * Take the square root then strip the first 7 bits of
337 * the mantissa into the table
338 */
339
340 sqrttab[i] = (fi.i & 0x7fffff) >> 16;
341
342 /*
343 * Repeat the process, this time with an exponent of
344 * 1, stored as 128
345 */
346
347 fi.i = 0;
348 fi.i = (i << 16) | (128 << 23);
349 fi.f = sqrt(fi.f);
350 sqrttab[i+0x80] = (fi.i & 0x7fffff) >> 16;
351 }
352 #else
353 (void) sqrttab; /* silence compiler warnings */
354 #endif /*HAVE_FAST_MATH*/
355 }
356
357
358 /**
359 * Single precision square root.
360 */
361 float
362 _mesa_sqrtf( float x )
363 {
364 #if defined(USE_IEEE) && !defined(DEBUG)
365 fi_type num;
366 /* to access the bits of a float in C
367 * we use a union from glheader.h */
368
369 short e; /* the exponent */
370 if (x == 0.0F) return 0.0F; /* check for square root of 0 */
371 num.f = x;
372 e = (num.i >> 23) - 127; /* get the exponent - on a SPARC the */
373 /* exponent is stored with 127 added */
374 num.i &= 0x7fffff; /* leave only the mantissa */
375 if (e & 0x01) num.i |= 0x800000;
376 /* the exponent is odd so we have to */
377 /* look it up in the second half of */
378 /* the lookup table, so we set the */
379 /* high bit */
380 e >>= 1; /* divide the exponent by two */
381 /* note that in C the shift */
382 /* operators are sign preserving */
383 /* for signed operands */
384 /* Do the table lookup, based on the quaternary mantissa,
385 * then reconstruct the result back into a float
386 */
387 num.i = ((sqrttab[num.i >> 16]) << 16) | ((e + 127) << 23);
388
389 return num.f;
390 #else
391 return (float) _mesa_sqrtd((double) x);
392 #endif
393 }
394
395
396 /**
397 inv_sqrt - A single precision 1/sqrt routine for IEEE format floats.
398 written by Josh Vanderhoof, based on newsgroup posts by James Van Buskirk
399 and Vesa Karvonen.
400 */
401 float
402 _mesa_inv_sqrtf(float n)
403 {
404 #if defined(USE_IEEE) && !defined(DEBUG)
405 float r0, x0, y0;
406 float r1, x1, y1;
407 float r2, x2, y2;
408 #if 0 /* not used, see below -BP */
409 float r3, x3, y3;
410 #endif
411 union { float f; unsigned int i; } u;
412 unsigned int magic;
413
414 /*
415 Exponent part of the magic number -
416
417 We want to:
418 1. subtract the bias from the exponent,
419 2. negate it
420 3. divide by two (rounding towards -inf)
421 4. add the bias back
422
423 Which is the same as subtracting the exponent from 381 and dividing
424 by 2.
425
426 floor(-(x - 127) / 2) + 127 = floor((381 - x) / 2)
427 */
428
429 magic = 381 << 23;
430
431 /*
432 Significand part of magic number -
433
434 With the current magic number, "(magic - u.i) >> 1" will give you:
435
436 for 1 <= u.f <= 2: 1.25 - u.f / 4
437 for 2 <= u.f <= 4: 1.00 - u.f / 8
438
439 This isn't a bad approximation of 1/sqrt. The maximum difference from
440 1/sqrt will be around .06. After three Newton-Raphson iterations, the
441 maximum difference is less than 4.5e-8. (Which is actually close
442 enough to make the following bias academic...)
443
444 To get a better approximation you can add a bias to the magic
445 number. For example, if you subtract 1/2 of the maximum difference in
446 the first approximation (.03), you will get the following function:
447
448 for 1 <= u.f <= 2: 1.22 - u.f / 4
449 for 2 <= u.f <= 3.76: 0.97 - u.f / 8
450 for 3.76 <= u.f <= 4: 0.72 - u.f / 16
451 (The 3.76 to 4 range is where the result is < .5.)
452
453 This is the closest possible initial approximation, but with a maximum
454 error of 8e-11 after three NR iterations, it is still not perfect. If
455 you subtract 0.0332281 instead of .03, the maximum error will be
456 2.5e-11 after three NR iterations, which should be about as close as
457 is possible.
458
459 for 1 <= u.f <= 2: 1.2167719 - u.f / 4
460 for 2 <= u.f <= 3.73: 0.9667719 - u.f / 8
461 for 3.73 <= u.f <= 4: 0.7167719 - u.f / 16
462
463 */
464
465 magic -= (int)(0.0332281 * (1 << 25));
466
467 u.f = n;
468 u.i = (magic - u.i) >> 1;
469
470 /*
471 Instead of Newton-Raphson, we use Goldschmidt's algorithm, which
472 allows more parallelism. From what I understand, the parallelism
473 comes at the cost of less precision, because it lets error
474 accumulate across iterations.
475 */
476 x0 = 1.0f;
477 y0 = 0.5f * n;
478 r0 = u.f;
479
480 x1 = x0 * r0;
481 y1 = y0 * r0 * r0;
482 r1 = 1.5f - y1;
483
484 x2 = x1 * r1;
485 y2 = y1 * r1 * r1;
486 r2 = 1.5f - y2;
487
488 #if 1
489 return x2 * r2; /* we can stop here, and be conformant -BP */
490 #else
491 x3 = x2 * r2;
492 y3 = y2 * r2 * r2;
493 r3 = 1.5f - y3;
494
495 return x3 * r3;
496 #endif
497 #elif defined(XFree86LOADER) && defined(IN_MODULE)
498 return 1.0F / xf86sqrt(n);
499 #else
500 return (float) (1.0 / sqrt(n));
501 #endif
502 }
503
504
505 /**
506 * Wrapper around either pow() or xf86pow().
507 */
508 double
509 _mesa_pow(double x, double y)
510 {
511 #if defined(XFree86LOADER) && defined(IN_MODULE)
512 return xf86pow(x, y);
513 #else
514 return pow(x, y);
515 #endif
516 }
517
518
519 /**
520 * Return number of bits set in given GLuint.
521 */
522 unsigned int
523 _mesa_bitcount(unsigned int n)
524 {
525 unsigned int bits;
526 for (bits = 0; n > 0; n = n >> 1) {
527 bits += (n & 1);
528 }
529 return bits;
530 }
531
532
533 /**
534 * Convert a 4-byte float to a 2-byte half float.
535 * Based on code from:
536 * http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/008786.html
537 */
538 GLhalfARB
539 _mesa_float_to_half(float val)
540 {
541 const int flt = *((int *) (void *) &val);
542 const int flt_m = flt & 0x7fffff;
543 const int flt_e = (flt >> 23) & 0xff;
544 const int flt_s = (flt >> 31) & 0x1;
545 int s, e, m = 0;
546 GLhalfARB result;
547
548 /* sign bit */
549 s = flt_s;
550
551 /* handle special cases */
552 if ((flt_e == 0) && (flt_m == 0)) {
553 /* zero */
554 /* m = 0; - already set */
555 e = 0;
556 }
557 else if ((flt_e == 0) && (flt_m != 0)) {
558 /* denorm -- denorm float maps to 0 half */
559 /* m = 0; - already set */
560 e = 0;
561 }
562 else if ((flt_e == 0xff) && (flt_m == 0)) {
563 /* infinity */
564 /* m = 0; - already set */
565 e = 31;
566 }
567 else if ((flt_e == 0xff) && (flt_m != 0)) {
568 /* NaN */
569 m = 1;
570 e = 31;
571 }
572 else {
573 /* regular number */
574 const int new_exp = flt_e - 127;
575 if (new_exp < -24) {
576 /* this maps to 0 */
577 /* m = 0; - already set */
578 e = 0;
579 }
580 else if (new_exp < -14) {
581 /* this maps to a denorm */
582 unsigned int exp_val = (unsigned int) (-14 - new_exp); /* 2^-exp_val*/
583 e = 0;
584 switch (exp_val) {
585 case 0:
586 _mesa_warning(NULL,
587 "float_to_half: logical error in denorm creation!\n");
588 /* m = 0; - already set */
589 break;
590 case 1: m = 512 + (flt_m >> 14); break;
591 case 2: m = 256 + (flt_m >> 15); break;
592 case 3: m = 128 + (flt_m >> 16); break;
593 case 4: m = 64 + (flt_m >> 17); break;
594 case 5: m = 32 + (flt_m >> 18); break;
595 case 6: m = 16 + (flt_m >> 19); break;
596 case 7: m = 8 + (flt_m >> 20); break;
597 case 8: m = 4 + (flt_m >> 21); break;
598 case 9: m = 2 + (flt_m >> 22); break;
599 case 10: m = 1; break;
600 }
601 }
602 else if (new_exp > 15) {
603 /* map this value to infinity */
604 /* m = 0; - already set */
605 e = 31;
606 }
607 else {
608 /* regular */
609 e = new_exp + 15;
610 m = flt_m >> 13;
611 }
612 }
613
614 result = (s << 15) | (e << 10) | m;
615 return result;
616 }
617
618
619 /**
620 * Convert a 2-byte half float to a 4-byte float.
621 * Based on code from:
622 * http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/008786.html
623 */
624 float
625 _mesa_half_to_float(GLhalfARB val)
626 {
627 /* XXX could also use a 64K-entry lookup table */
628 const int m = val & 0x3ff;
629 const int e = (val >> 10) & 0x1f;
630 const int s = (val >> 15) & 0x1;
631 int flt_m, flt_e, flt_s, flt;
632 float result;
633
634 /* sign bit */
635 flt_s = s;
636
637 /* handle special cases */
638 if ((e == 0) && (m == 0)) {
639 /* zero */
640 flt_m = 0;
641 flt_e = 0;
642 }
643 else if ((e == 0) && (m != 0)) {
644 /* denorm -- denorm half will fit in non-denorm single */
645 const float half_denorm = 1.0f / 16384.0f; /* 2^-14 */
646 float mantissa = ((float) (m)) / 1024.0f;
647 float sign = s ? -1.0f : 1.0f;
648 return sign * mantissa * half_denorm;
649 }
650 else if ((e == 31) && (m == 0)) {
651 /* infinity */
652 flt_e = 0xff;
653 flt_m = 0;
654 }
655 else if ((e == 31) && (m != 0)) {
656 /* NaN */
657 flt_e = 0xff;
658 flt_m = 1;
659 }
660 else {
661 /* regular */
662 flt_e = e + 112;
663 flt_m = m << 13;
664 }
665
666 flt = (flt_s << 31) | (flt_e << 23) | flt_m;
667 result = *((float *) (void *) &flt);
668 return result;
669 }
670
671 /*@}*/
672
673
674 /**********************************************************************/
675 /** \name Environment vars */
676 /*@{*/
677
678 /**
679 * Wrapper for getenv().
680 */
681 char *
682 _mesa_getenv( const char *var )
683 {
684 #if defined(XFree86LOADER) && defined(IN_MODULE)
685 return xf86getenv(var);
686 #elif defined(_XBOX)
687 return NULL;
688 #else
689 return getenv(var);
690 #endif
691 }
692
693 /*@}*/
694
695
696 /**********************************************************************/
697 /** \name String */
698 /*@{*/
699
700 /** Wrapper around either strstr() or xf86strstr() */
701 char *
702 _mesa_strstr( const char *haystack, const char *needle )
703 {
704 #if defined(XFree86LOADER) && defined(IN_MODULE)
705 return xf86strstr(haystack, needle);
706 #else
707 return strstr(haystack, needle);
708 #endif
709 }
710
711 /** Wrapper around either strncat() or xf86strncat() */
712 char *
713 _mesa_strncat( char *dest, const char *src, size_t n )
714 {
715 #if defined(XFree86LOADER) && defined(IN_MODULE)
716 return xf86strncat(dest, src, n);
717 #else
718 return strncat(dest, src, n);
719 #endif
720 }
721
722 /** Wrapper around either strcpy() or xf86strcpy() */
723 char *
724 _mesa_strcpy( char *dest, const char *src )
725 {
726 #if defined(XFree86LOADER) && defined(IN_MODULE)
727 return xf86strcpy(dest, src);
728 #else
729 return strcpy(dest, src);
730 #endif
731 }
732
733 /** Wrapper around either strncpy() or xf86strncpy() */
734 char *
735 _mesa_strncpy( char *dest, const char *src, size_t n )
736 {
737 #if defined(XFree86LOADER) && defined(IN_MODULE)
738 return xf86strncpy(dest, src, n);
739 #else
740 return strncpy(dest, src, n);
741 #endif
742 }
743
744 /** Wrapper around either strlen() or xf86strlen() */
745 size_t
746 _mesa_strlen( const char *s )
747 {
748 #if defined(XFree86LOADER) && defined(IN_MODULE)
749 return xf86strlen(s);
750 #else
751 return strlen(s);
752 #endif
753 }
754
755 /** Wrapper around either strcmp() or xf86strcmp() */
756 int
757 _mesa_strcmp( const char *s1, const char *s2 )
758 {
759 #if defined(XFree86LOADER) && defined(IN_MODULE)
760 return xf86strcmp(s1, s2);
761 #else
762 return strcmp(s1, s2);
763 #endif
764 }
765
766 /** Wrapper around either strncmp() or xf86strncmp() */
767 int
768 _mesa_strncmp( const char *s1, const char *s2, size_t n )
769 {
770 #if defined(XFree86LOADER) && defined(IN_MODULE)
771 return xf86strncmp(s1, s2, n);
772 #else
773 return strncmp(s1, s2, n);
774 #endif
775 }
776
777 /** Implemented using _mesa_malloc() and _mesa_strcpy */
778 char *
779 _mesa_strdup( const char *s )
780 {
781 size_t l = _mesa_strlen(s);
782 char *s2 = (char *) _mesa_malloc(l + 1);
783 if (s2)
784 _mesa_strcpy(s2, s);
785 return s2;
786 }
787
788 /** Wrapper around either atoi() or xf86atoi() */
789 int
790 _mesa_atoi(const char *s)
791 {
792 #if defined(XFree86LOADER) && defined(IN_MODULE)
793 return xf86atoi(s);
794 #else
795 return atoi(s);
796 #endif
797 }
798
799 /** Wrapper around either strtod() or xf86strtod() */
800 double
801 _mesa_strtod( const char *s, char **end )
802 {
803 #if defined(XFree86LOADER) && defined(IN_MODULE)
804 return xf86strtod(s, end);
805 #else
806 return strtod(s, end);
807 #endif
808 }
809
810 /*@}*/
811
812
813 /**********************************************************************/
814 /** \name I/O */
815 /*@{*/
816
817 /** Wrapper around either vsprintf() or xf86vsprintf() */
818 int
819 _mesa_sprintf( char *str, const char *fmt, ... )
820 {
821 int r;
822 va_list args;
823 va_start( args, fmt );
824 va_end( args );
825 #if defined(XFree86LOADER) && defined(IN_MODULE)
826 r = xf86vsprintf( str, fmt, args );
827 #else
828 r = vsprintf( str, fmt, args );
829 #endif
830 return r;
831 }
832
833 /** Wrapper around either printf() or xf86printf(), using vsprintf() for
834 * the formatting. */
835 void
836 _mesa_printf( const char *fmtString, ... )
837 {
838 char s[MAXSTRING];
839 va_list args;
840 va_start( args, fmtString );
841 vsnprintf(s, MAXSTRING, fmtString, args);
842 va_end( args );
843 #if defined(XFree86LOADER) && defined(IN_MODULE)
844 xf86printf("%s", s);
845 #else
846 fprintf(stderr,"%s", s);
847 #endif
848 }
849
850 /*@}*/
851
852
853 /**********************************************************************/
854 /** \name Diagnostics */
855 /*@{*/
856
857 /**
858 * Display a warning.
859 *
860 * \param ctx GL context.
861 * \param fmtString printf() alike format string.
862 *
863 * If debugging is enabled (either at compile-time via the DEBUG macro, or
864 * run-time via the MESA_DEBUG environment variable), prints the warning to
865 * stderr, either via fprintf() or xf86printf().
866 */
867 void
868 _mesa_warning( GLcontext *ctx, const char *fmtString, ... )
869 {
870 GLboolean debug;
871 char str[MAXSTRING];
872 va_list args;
873 (void) ctx;
874 va_start( args, fmtString );
875 (void) vsnprintf( str, MAXSTRING, fmtString, args );
876 va_end( args );
877 #ifdef DEBUG
878 debug = GL_TRUE; /* always print warning */
879 #else
880 debug = _mesa_getenv("MESA_DEBUG") ? GL_TRUE : GL_FALSE;
881 #endif
882 if (debug) {
883 #if defined(XFree86LOADER) && defined(IN_MODULE)
884 xf86fprintf(stderr, "Mesa warning: %s", str);
885 #else
886 fprintf(stderr, "Mesa warning: %s", str);
887 #endif
888 }
889 }
890
891 /**
892 * This function is called when the Mesa user has stumbled into a code
893 * path which may not be implemented fully or correctly.
894 *
895 * \param ctx GL context.
896 * \param s problem description string.
897 *
898 * Prints the message to stderr, either via fprintf() or xf86fprintf().
899 */
900 void
901 _mesa_problem( const GLcontext *ctx, const char *fmtString, ... )
902 {
903 va_list args;
904 char str[MAXSTRING];
905 (void) ctx;
906
907 va_start( args, fmtString );
908 vsnprintf( str, MAXSTRING, fmtString, args );
909 va_end( args );
910
911 #if defined(XFree86LOADER) && defined(IN_MODULE)
912 xf86fprintf(stderr, "Mesa %s implementation error: %s\n", MESA_VERSION_STRING, str);
913 xf86fprintf(stderr, "Please report at bugzilla.freedesktop.org\n");
914 #else
915 fprintf(stderr, "Mesa %s implementation error: %s\n", MESA_VERSION_STRING, str);
916 fprintf(stderr, "Please report at bugzilla.freedesktop.org\n");
917 #endif
918 }
919
920 /**
921 * Display an error message.
922 *
923 * If in debug mode, print error message.
924 * Also, record the error code by calling _mesa_record_error().
925 *
926 * \param ctx the GL context.
927 * \param error the error value.
928 * \param fmtString printf() style format string, followed by optional args
929 *
930 * If debugging is enabled (either at compile-time via the DEBUG macro, or
931 * run-time via the MESA_DEBUG environment variable), interperts the error code and
932 * prints the error message via _mesa_debug().
933 */
934 void
935 _mesa_error( GLcontext *ctx, GLenum error, const char *fmtString, ... )
936 {
937 const char *debugEnv;
938 GLboolean debug;
939
940 debugEnv = _mesa_getenv("MESA_DEBUG");
941
942 #ifdef DEBUG
943 if (debugEnv && _mesa_strstr(debugEnv, "silent"))
944 debug = GL_FALSE;
945 else
946 debug = GL_TRUE;
947 #else
948 if (debugEnv)
949 debug = GL_TRUE;
950 else
951 debug = GL_FALSE;
952 #endif
953
954 if (debug) {
955 va_list args;
956 char where[MAXSTRING];
957 const char *errstr;
958
959 va_start( args, fmtString );
960 vsnprintf( where, MAXSTRING, fmtString, args );
961 va_end( args );
962
963 switch (error) {
964 case GL_NO_ERROR:
965 errstr = "GL_NO_ERROR";
966 break;
967 case GL_INVALID_VALUE:
968 errstr = "GL_INVALID_VALUE";
969 break;
970 case GL_INVALID_ENUM:
971 errstr = "GL_INVALID_ENUM";
972 break;
973 case GL_INVALID_OPERATION:
974 errstr = "GL_INVALID_OPERATION";
975 break;
976 case GL_STACK_OVERFLOW:
977 errstr = "GL_STACK_OVERFLOW";
978 break;
979 case GL_STACK_UNDERFLOW:
980 errstr = "GL_STACK_UNDERFLOW";
981 break;
982 case GL_OUT_OF_MEMORY:
983 errstr = "GL_OUT_OF_MEMORY";
984 break;
985 case GL_TABLE_TOO_LARGE:
986 errstr = "GL_TABLE_TOO_LARGE";
987 break;
988 default:
989 errstr = "unknown";
990 break;
991 }
992 _mesa_debug(ctx, "User error: %s in %s\n", errstr, where);
993 }
994
995 _mesa_record_error(ctx, error);
996 }
997
998 /**
999 * Report debug information.
1000 *
1001 * \param ctx GL context.
1002 * \param fmtString printf() alike format string.
1003 *
1004 * Prints the message to stderr, either via fprintf() or xf86printf().
1005 */
1006 void
1007 _mesa_debug( const GLcontext *ctx, const char *fmtString, ... )
1008 {
1009 char s[MAXSTRING];
1010 va_list args;
1011 (void) ctx;
1012 va_start(args, fmtString);
1013 vsnprintf(s, MAXSTRING, fmtString, args);
1014 va_end(args);
1015 #if defined(XFree86LOADER) && defined(IN_MODULE)
1016 xf86fprintf(stderr, "Mesa: %s", s);
1017 #else
1018 fprintf(stderr, "Mesa: %s", s);
1019 #endif
1020 }
1021
1022 /*@}*/
1023
1024
1025 /**********************************************************************/
1026 /** \name Default Imports Wrapper */
1027 /*@{*/
1028
1029 /** Wrapper around _mesa_malloc() */
1030 static void *
1031 default_malloc(__GLcontext *gc, size_t size)
1032 {
1033 (void) gc;
1034 return _mesa_malloc(size);
1035 }
1036
1037 /** Wrapper around _mesa_malloc() */
1038 static void *
1039 default_calloc(__GLcontext *gc, size_t numElem, size_t elemSize)
1040 {
1041 (void) gc;
1042 return _mesa_calloc(numElem * elemSize);
1043 }
1044
1045 /** Wrapper around either realloc() or xf86realloc() */
1046 static void *
1047 default_realloc(__GLcontext *gc, void *oldAddr, size_t newSize)
1048 {
1049 (void) gc;
1050 #if defined(XFree86LOADER) && defined(IN_MODULE)
1051 return xf86realloc(oldAddr, newSize);
1052 #else
1053 return realloc(oldAddr, newSize);
1054 #endif
1055 }
1056
1057 /** Wrapper around _mesa_free() */
1058 static void
1059 default_free(__GLcontext *gc, void *addr)
1060 {
1061 (void) gc;
1062 _mesa_free(addr);
1063 }
1064
1065 /** Wrapper around _mesa_getenv() */
1066 static char * CAPI
1067 default_getenv( __GLcontext *gc, const char *var )
1068 {
1069 (void) gc;
1070 return _mesa_getenv(var);
1071 }
1072
1073 /** Wrapper around _mesa_warning() */
1074 static void
1075 default_warning(__GLcontext *gc, char *str)
1076 {
1077 _mesa_warning(gc, str);
1078 }
1079
1080 /** Wrapper around _mesa_problem() */
1081 static void
1082 default_fatal(__GLcontext *gc, char *str)
1083 {
1084 _mesa_problem(gc, str);
1085 abort();
1086 }
1087
1088 /** Wrapper around atoi() */
1089 static int CAPI
1090 default_atoi(__GLcontext *gc, const char *str)
1091 {
1092 (void) gc;
1093 return atoi(str);
1094 }
1095
1096 /** Wrapper around vsprintf() */
1097 static int CAPI
1098 default_sprintf(__GLcontext *gc, char *str, const char *fmt, ...)
1099 {
1100 int r;
1101 va_list args;
1102 (void) gc;
1103 va_start( args, fmt );
1104 r = vsprintf( str, fmt, args );
1105 va_end( args );
1106 return r;
1107 }
1108
1109 /** Wrapper around fopen() */
1110 static void * CAPI
1111 default_fopen(__GLcontext *gc, const char *path, const char *mode)
1112 {
1113 (void) gc;
1114 return fopen(path, mode);
1115 }
1116
1117 /** Wrapper around fclose() */
1118 static int CAPI
1119 default_fclose(__GLcontext *gc, void *stream)
1120 {
1121 (void) gc;
1122 return fclose((FILE *) stream);
1123 }
1124
1125 /** Wrapper around vfprintf() */
1126 static int CAPI
1127 default_fprintf(__GLcontext *gc, void *stream, const char *fmt, ...)
1128 {
1129 int r;
1130 va_list args;
1131 (void) gc;
1132 va_start( args, fmt );
1133 r = vfprintf( (FILE *) stream, fmt, args );
1134 va_end( args );
1135 return r;
1136 }
1137
1138 /**
1139 * \todo this really is driver-specific and can't be here
1140 */
1141 static __GLdrawablePrivate *
1142 default_GetDrawablePrivate(__GLcontext *gc)
1143 {
1144 (void) gc;
1145 return NULL;
1146 }
1147
1148 /*@}*/
1149
1150
1151 /**
1152 * Initialize a __GLimports object to point to the functions in this
1153 * file.
1154 *
1155 * This is to be called from device drivers.
1156 *
1157 * Also, do some one-time initializations.
1158 *
1159 * \param imports the object to initialize.
1160 * \param driverCtx pointer to device driver-specific data.
1161 */
1162 void
1163 _mesa_init_default_imports(__GLimports *imports, void *driverCtx)
1164 {
1165 /* XXX maybe move this one-time init stuff into context.c */
1166 static GLboolean initialized = GL_FALSE;
1167 if (!initialized) {
1168 init_sqrt_table();
1169
1170 #if defined(_FPU_GETCW) && defined(_FPU_SETCW)
1171 {
1172 const char *debug = _mesa_getenv("MESA_DEBUG");
1173 if (debug && _mesa_strcmp(debug, "FP")==0) {
1174 /* die on FP exceptions */
1175 fpu_control_t mask;
1176 _FPU_GETCW(mask);
1177 mask &= ~(_FPU_MASK_IM | _FPU_MASK_DM | _FPU_MASK_ZM
1178 | _FPU_MASK_OM | _FPU_MASK_UM);
1179 _FPU_SETCW(mask);
1180 }
1181 }
1182 #endif
1183 initialized = GL_TRUE;
1184 }
1185
1186 imports->malloc = default_malloc;
1187 imports->calloc = default_calloc;
1188 imports->realloc = default_realloc;
1189 imports->free = default_free;
1190 imports->warning = default_warning;
1191 imports->fatal = default_fatal;
1192 imports->getenv = default_getenv; /* not used for now */
1193 imports->atoi = default_atoi;
1194 imports->sprintf = default_sprintf;
1195 imports->fopen = default_fopen;
1196 imports->fclose = default_fclose;
1197 imports->fprintf = default_fprintf;
1198 imports->getDrawablePrivate = default_GetDrawablePrivate;
1199 imports->other = driverCtx;
1200 }