3 * Standard C library function wrappers.
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,
10 * Some drivers will want to override/replace this file with something
11 * specialized, but that'll be rare.
13 * Eventually, I want to move roll the glheader.h file into this.
15 * \todo Functions still needed:
22 * Mesa 3-D graphics library
25 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
27 * Permission is hereby granted, free of charge, to any person obtaining a
28 * copy of this software and associated documentation files (the "Software"),
29 * to deal in the Software without restriction, including without limitation
30 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
31 * and/or sell copies of the Software, and to permit persons to whom the
32 * Software is furnished to do so, subject to the following conditions:
34 * The above copyright notice and this permission notice shall be included
35 * in all copies or substantial portions of the Software.
37 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
38 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
39 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
40 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
41 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
42 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
52 #define MAXSTRING 4000 /* for vsnprintf() */
55 #define vsnprintf _vsnprintf
56 #elif defined(__IBMC__) || defined(__IBMCPP__) || ( defined(__VMS) && __CRTL_VER < 70312000 )
57 extern int vsnprintf(char *str
, size_t count
, const char *fmt
, va_list arg
);
59 #include "vsnprintf.c"
63 /**********************************************************************/
67 /** Wrapper around malloc() */
69 _mesa_malloc(size_t bytes
)
74 /** Wrapper around calloc() */
76 _mesa_calloc(size_t bytes
)
78 return calloc(1, bytes
);
81 /** Wrapper around free() */
89 * Allocate aligned memory.
91 * \param bytes number of bytes to allocate.
92 * \param alignment alignment (must be greater than zero).
94 * Allocates extra memory to accommodate rounding up the address for
95 * alignment and to record the real malloc address.
97 * \sa _mesa_align_free().
100 _mesa_align_malloc(size_t bytes
, unsigned long alignment
)
102 #if defined(HAVE_POSIX_MEMALIGN)
105 (void) posix_memalign(& mem
, alignment
, bytes
);
107 #elif defined(_WIN32) && defined(_MSC_VER)
108 return _aligned_malloc(bytes
, alignment
);
112 ASSERT( alignment
> 0 );
114 ptr
= (uintptr_t) _mesa_malloc(bytes
+ alignment
+ sizeof(void *));
118 buf
= (ptr
+ alignment
+ sizeof(void *)) & ~(uintptr_t)(alignment
- 1);
119 *(uintptr_t *)(buf
- sizeof(void *)) = ptr
;
122 /* mark the non-aligned area */
123 while ( ptr
< buf
- sizeof(void *) ) {
124 *(unsigned long *)ptr
= 0xcdcdcdcd;
125 ptr
+= sizeof(unsigned long);
130 #endif /* defined(HAVE_POSIX_MEMALIGN) */
134 * Same as _mesa_align_malloc(), but using _mesa_calloc() instead of
138 _mesa_align_calloc(size_t bytes
, unsigned long alignment
)
140 #if defined(HAVE_POSIX_MEMALIGN)
143 mem
= _mesa_align_malloc(bytes
, alignment
);
145 (void) memset(mem
, 0, bytes
);
149 #elif defined(_WIN32) && defined(_MSC_VER)
152 mem
= _aligned_malloc(bytes
, alignment
);
154 (void) memset(mem
, 0, bytes
);
161 ASSERT( alignment
> 0 );
163 ptr
= (uintptr_t) _mesa_calloc(bytes
+ alignment
+ sizeof(void *));
167 buf
= (ptr
+ alignment
+ sizeof(void *)) & ~(uintptr_t)(alignment
- 1);
168 *(uintptr_t *)(buf
- sizeof(void *)) = ptr
;
171 /* mark the non-aligned area */
172 while ( ptr
< buf
- sizeof(void *) ) {
173 *(unsigned long *)ptr
= 0xcdcdcdcd;
174 ptr
+= sizeof(unsigned long);
179 #endif /* defined(HAVE_POSIX_MEMALIGN) */
183 * Free memory which was allocated with either _mesa_align_malloc()
184 * or _mesa_align_calloc().
185 * \param ptr pointer to the memory to be freed.
186 * The actual address to free is stored in the word immediately before the
187 * address the client sees.
190 _mesa_align_free(void *ptr
)
192 #if defined(HAVE_POSIX_MEMALIGN)
194 #elif defined(_WIN32) && defined(_MSC_VER)
197 void **cubbyHole
= (void **) ((char *) ptr
- sizeof(void *));
198 void *realAddr
= *cubbyHole
;
199 _mesa_free(realAddr
);
200 #endif /* defined(HAVE_POSIX_MEMALIGN) */
204 * Reallocate memory, with alignment.
207 _mesa_align_realloc(void *oldBuffer
, size_t oldSize
, size_t newSize
,
208 unsigned long alignment
)
210 #if defined(_WIN32) && defined(_MSC_VER)
212 return _aligned_realloc(oldBuffer
, newSize
, alignment
);
214 const size_t copySize
= (oldSize
< newSize
) ? oldSize
: newSize
;
215 void *newBuf
= _mesa_align_malloc(newSize
, alignment
);
216 if (newBuf
&& oldBuffer
&& copySize
> 0) {
217 _mesa_memcpy(newBuf
, oldBuffer
, copySize
);
220 _mesa_align_free(oldBuffer
);
227 /** Reallocate memory */
229 _mesa_realloc(void *oldBuffer
, size_t oldSize
, size_t newSize
)
231 const size_t copySize
= (oldSize
< newSize
) ? oldSize
: newSize
;
232 void *newBuffer
= _mesa_malloc(newSize
);
233 if (newBuffer
&& oldBuffer
&& copySize
> 0)
234 _mesa_memcpy(newBuffer
, oldBuffer
, copySize
);
236 _mesa_free(oldBuffer
);
240 /** memcpy wrapper */
242 _mesa_memcpy(void *dest
, const void *src
, size_t n
)
245 return memcpy((char *) dest
, (char *) src
, (int) n
);
247 return memcpy(dest
, src
, n
);
251 /** Wrapper around memset() */
253 _mesa_memset( void *dst
, int val
, size_t n
)
256 memset( (char *) dst
, (int) val
, (int) n
);
263 * Fill memory with a constant 16bit word.
264 * \param dst destination pointer.
266 * \param n number of words.
269 _mesa_memset16( unsigned short *dst
, unsigned short val
, size_t n
)
275 /** Wrapper around either memset() or bzero() */
277 _mesa_bzero( void *dst
, size_t n
)
279 #if defined(__FreeBSD__)
286 /** Wrapper around memcmp() */
288 _mesa_memcmp( const void *s1
, const void *s2
, size_t n
)
291 return memcmp( (char *) s1
, (char *) s2
, (int) n
);
293 return memcmp(s1
, s2
, n
);
300 /**********************************************************************/
304 /** Wrapper around sin() */
311 /** Single precision wrapper around sin() */
315 return (float) sin((double) a
);
318 /** Wrapper around cos() */
325 /** Single precision wrapper around asin() */
329 return (float) asin((double) x
);
332 /** Single precision wrapper around atan() */
336 return (float) atan((double) x
);
339 /** Wrapper around sqrt() */
341 _mesa_sqrtd(double x
)
348 * A High Speed, Low Precision Square Root
349 * by Paul Lalonde and Robert Dawson
350 * from "Graphics Gems", Academic Press, 1990
352 * SPARC implementation of a fast square root by table
354 * SPARC floating point format is as follows:
357 * sign exponent mantissa
359 static short sqrttab
[0x100]; /* declare table of square roots */
362 _mesa_init_sqrt_table(void)
364 #if defined(USE_IEEE) && !defined(DEBUG)
366 fi_type fi
; /* to access the bits of a float in C quickly */
367 /* we use a union defined in glheader.h */
369 for(i
=0; i
<= 0x7f; i
++) {
373 * Build a float with the bit pattern i as mantissa
374 * and an exponent of 0, stored as 127
377 fi
.i
= (i
<< 16) | (127 << 23);
378 fi
.f
= _mesa_sqrtd(fi
.f
);
381 * Take the square root then strip the first 7 bits of
382 * the mantissa into the table
385 sqrttab
[i
] = (fi
.i
& 0x7fffff) >> 16;
388 * Repeat the process, this time with an exponent of
393 fi
.i
= (i
<< 16) | (128 << 23);
395 sqrttab
[i
+0x80] = (fi
.i
& 0x7fffff) >> 16;
398 (void) sqrttab
; /* silence compiler warnings */
399 #endif /*HAVE_FAST_MATH*/
404 * Single precision square root.
407 _mesa_sqrtf( float x
)
409 #if defined(USE_IEEE) && !defined(DEBUG)
411 /* to access the bits of a float in C
412 * we use a union from glheader.h */
414 short e
; /* the exponent */
415 if (x
== 0.0F
) return 0.0F
; /* check for square root of 0 */
417 e
= (num
.i
>> 23) - 127; /* get the exponent - on a SPARC the */
418 /* exponent is stored with 127 added */
419 num
.i
&= 0x7fffff; /* leave only the mantissa */
420 if (e
& 0x01) num
.i
|= 0x800000;
421 /* the exponent is odd so we have to */
422 /* look it up in the second half of */
423 /* the lookup table, so we set the */
425 e
>>= 1; /* divide the exponent by two */
426 /* note that in C the shift */
427 /* operators are sign preserving */
428 /* for signed operands */
429 /* Do the table lookup, based on the quaternary mantissa,
430 * then reconstruct the result back into a float
432 num
.i
= ((sqrttab
[num
.i
>> 16]) << 16) | ((e
+ 127) << 23);
436 return (float) _mesa_sqrtd((double) x
);
442 inv_sqrt - A single precision 1/sqrt routine for IEEE format floats.
443 written by Josh Vanderhoof, based on newsgroup posts by James Van Buskirk
447 _mesa_inv_sqrtf(float n
)
449 #if defined(USE_IEEE) && !defined(DEBUG)
453 #if 0 /* not used, see below -BP */
456 union { float f
; unsigned int i
; } u
;
460 Exponent part of the magic number -
463 1. subtract the bias from the exponent,
465 3. divide by two (rounding towards -inf)
468 Which is the same as subtracting the exponent from 381 and dividing
471 floor(-(x - 127) / 2) + 127 = floor((381 - x) / 2)
477 Significand part of magic number -
479 With the current magic number, "(magic - u.i) >> 1" will give you:
481 for 1 <= u.f <= 2: 1.25 - u.f / 4
482 for 2 <= u.f <= 4: 1.00 - u.f / 8
484 This isn't a bad approximation of 1/sqrt. The maximum difference from
485 1/sqrt will be around .06. After three Newton-Raphson iterations, the
486 maximum difference is less than 4.5e-8. (Which is actually close
487 enough to make the following bias academic...)
489 To get a better approximation you can add a bias to the magic
490 number. For example, if you subtract 1/2 of the maximum difference in
491 the first approximation (.03), you will get the following function:
493 for 1 <= u.f <= 2: 1.22 - u.f / 4
494 for 2 <= u.f <= 3.76: 0.97 - u.f / 8
495 for 3.76 <= u.f <= 4: 0.72 - u.f / 16
496 (The 3.76 to 4 range is where the result is < .5.)
498 This is the closest possible initial approximation, but with a maximum
499 error of 8e-11 after three NR iterations, it is still not perfect. If
500 you subtract 0.0332281 instead of .03, the maximum error will be
501 2.5e-11 after three NR iterations, which should be about as close as
504 for 1 <= u.f <= 2: 1.2167719 - u.f / 4
505 for 2 <= u.f <= 3.73: 0.9667719 - u.f / 8
506 for 3.73 <= u.f <= 4: 0.7167719 - u.f / 16
510 magic
-= (int)(0.0332281 * (1 << 25));
513 u
.i
= (magic
- u
.i
) >> 1;
516 Instead of Newton-Raphson, we use Goldschmidt's algorithm, which
517 allows more parallelism. From what I understand, the parallelism
518 comes at the cost of less precision, because it lets error
519 accumulate across iterations.
534 return x2
* r2
; /* we can stop here, and be conformant -BP */
543 return (float) (1.0 / sqrt(n
));
548 /** Wrapper around pow() */
550 _mesa_pow(double x
, double y
)
557 * Find the first bit set in a word.
562 #if (defined(_WIN32) ) || defined(__IBMC__) || defined(__IBMCPP__)
563 register int bit
= 0;
565 if ((i
& 0xffff) == 0) {
569 if ((i
& 0xff) == 0) {
573 if ((i
& 0xf) == 0) {
577 while ((i
& 1) == 0) {
591 * Find position of first bit set in given value.
592 * XXX Warning: this function can only be used on 64-bit systems!
593 * \return position of least-significant bit set, starting at 1, return zero
597 _mesa_ffsll(int64_t val
)
604 assert(sizeof(val
) == 8);
606 bit
= _mesa_ffs((int32_t)val
);
610 bit
= _mesa_ffs((int32_t)(val
>> 32));
620 * Return number of bits set in given GLuint.
623 _mesa_bitcount(unsigned int n
)
626 for (bits
= 0; n
> 0; n
= n
>> 1) {
634 * Convert a 4-byte float to a 2-byte half float.
635 * Based on code from:
636 * http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/008786.html
639 _mesa_float_to_half(float val
)
641 const int flt
= *((int *) (void *) &val
);
642 const int flt_m
= flt
& 0x7fffff;
643 const int flt_e
= (flt
>> 23) & 0xff;
644 const int flt_s
= (flt
>> 31) & 0x1;
651 /* handle special cases */
652 if ((flt_e
== 0) && (flt_m
== 0)) {
654 /* m = 0; - already set */
657 else if ((flt_e
== 0) && (flt_m
!= 0)) {
658 /* denorm -- denorm float maps to 0 half */
659 /* m = 0; - already set */
662 else if ((flt_e
== 0xff) && (flt_m
== 0)) {
664 /* m = 0; - already set */
667 else if ((flt_e
== 0xff) && (flt_m
!= 0)) {
674 const int new_exp
= flt_e
- 127;
677 /* m = 0; - already set */
680 else if (new_exp
< -14) {
681 /* this maps to a denorm */
682 unsigned int exp_val
= (unsigned int) (-14 - new_exp
); /* 2^-exp_val*/
687 "float_to_half: logical error in denorm creation!\n");
688 /* m = 0; - already set */
690 case 1: m
= 512 + (flt_m
>> 14); break;
691 case 2: m
= 256 + (flt_m
>> 15); break;
692 case 3: m
= 128 + (flt_m
>> 16); break;
693 case 4: m
= 64 + (flt_m
>> 17); break;
694 case 5: m
= 32 + (flt_m
>> 18); break;
695 case 6: m
= 16 + (flt_m
>> 19); break;
696 case 7: m
= 8 + (flt_m
>> 20); break;
697 case 8: m
= 4 + (flt_m
>> 21); break;
698 case 9: m
= 2 + (flt_m
>> 22); break;
699 case 10: m
= 1; break;
702 else if (new_exp
> 15) {
703 /* map this value to infinity */
704 /* m = 0; - already set */
714 result
= (s
<< 15) | (e
<< 10) | m
;
720 * Convert a 2-byte half float to a 4-byte float.
721 * Based on code from:
722 * http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/008786.html
725 _mesa_half_to_float(GLhalfARB val
)
727 /* XXX could also use a 64K-entry lookup table */
728 const int m
= val
& 0x3ff;
729 const int e
= (val
>> 10) & 0x1f;
730 const int s
= (val
>> 15) & 0x1;
731 int flt_m
, flt_e
, flt_s
, flt
;
737 /* handle special cases */
738 if ((e
== 0) && (m
== 0)) {
743 else if ((e
== 0) && (m
!= 0)) {
744 /* denorm -- denorm half will fit in non-denorm single */
745 const float half_denorm
= 1.0f
/ 16384.0f
; /* 2^-14 */
746 float mantissa
= ((float) (m
)) / 1024.0f
;
747 float sign
= s
? -1.0f
: 1.0f
;
748 return sign
* mantissa
* half_denorm
;
750 else if ((e
== 31) && (m
== 0)) {
755 else if ((e
== 31) && (m
!= 0)) {
766 flt
= (flt_s
<< 31) | (flt_e
<< 23) | flt_m
;
767 result
= *((float *) (void *) &flt
);
774 /**********************************************************************/
775 /** \name Sort & Search */
779 * Wrapper for bsearch().
782 _mesa_bsearch( const void *key
, const void *base
, size_t nmemb
, size_t size
,
783 int (*compar
)(const void *, const void *) )
785 #if defined(_WIN32_WCE)
790 mid
= (char *)base
+ nmemb
* size
;
791 cmp
= (*compar
)(key
, mid
);
795 base
= (char *)mid
+ size
;
801 return bsearch(key
, base
, nmemb
, size
, compar
);
808 /**********************************************************************/
809 /** \name Environment vars */
813 * Wrapper for getenv().
816 _mesa_getenv( const char *var
)
818 #if defined(_XBOX) || defined(_WIN32_WCE)
828 /**********************************************************************/
832 /** Wrapper around strstr() */
834 _mesa_strstr( const char *haystack
, const char *needle
)
836 return strstr(haystack
, needle
);
839 /** Wrapper around strncat() */
841 _mesa_strncat( char *dest
, const char *src
, size_t n
)
843 return strncat(dest
, src
, n
);
846 /** Wrapper around strcpy() */
848 _mesa_strcpy( char *dest
, const char *src
)
850 return strcpy(dest
, src
);
853 /** Wrapper around strncpy() */
855 _mesa_strncpy( char *dest
, const char *src
, size_t n
)
857 return strncpy(dest
, src
, n
);
860 /** Wrapper around strlen() */
862 _mesa_strlen( const char *s
)
867 /** Wrapper around strcmp() */
869 _mesa_strcmp( const char *s1
, const char *s2
)
871 return strcmp(s1
, s2
);
874 /** Wrapper around strncmp() */
876 _mesa_strncmp( const char *s1
, const char *s2
, size_t n
)
878 return strncmp(s1
, s2
, n
);
882 * Implemented using _mesa_malloc() and _mesa_strcpy.
883 * Note that NULL is handled accordingly.
886 _mesa_strdup( const char *s
)
889 size_t l
= _mesa_strlen(s
);
890 char *s2
= (char *) _mesa_malloc(l
+ 1);
900 /** Wrapper around atoi() */
902 _mesa_atoi(const char *s
)
907 /** Wrapper around strtod() */
909 _mesa_strtod( const char *s
, char **end
)
911 return strtod(s
, end
);
914 /** Compute simple checksum/hash for a string */
916 _mesa_str_checksum(const char *str
)
918 /* This could probably be much better */
922 for (c
= str
; *c
; c
++)
923 sum
+= *c
* (i
% 100);
931 /**********************************************************************/
935 /** Wrapper around vsprintf() */
937 _mesa_sprintf( char *str
, const char *fmt
, ... )
941 va_start( args
, fmt
);
942 r
= vsprintf( str
, fmt
, args
);
947 /** Wrapper around vsnprintf() */
949 _mesa_snprintf( char *str
, size_t size
, const char *fmt
, ... )
953 va_start( args
, fmt
);
954 r
= vsnprintf( str
, size
, fmt
, args
);
959 /** Wrapper around printf(), using vsprintf() for the formatting. */
961 _mesa_printf( const char *fmtString
, ... )
964 va_start( args
, fmtString
);
965 vfprintf(stderr
, fmtString
, args
);
969 /** Wrapper around fprintf(), using vsprintf() for the formatting. */
971 _mesa_fprintf( FILE *f
, const char *fmtString
, ... )
975 va_start( args
, fmtString
);
976 vsnprintf(s
, MAXSTRING
, fmtString
, args
);
982 /** Wrapper around vsprintf() */
984 _mesa_vsprintf( char *str
, const char *fmt
, va_list args
)
986 return vsprintf( str
, fmt
, args
);
992 /**********************************************************************/
993 /** \name Diagnostics */
997 output_if_debug(const char *prefixString
, const char *outputString
,
1000 static int debug
= -1;
1002 /* Check the MESA_DEBUG environment variable if it hasn't
1003 * been checked yet. We only have to check it once...
1006 char *env
= _mesa_getenv("MESA_DEBUG");
1008 /* In a debug build, we print warning messages *unless*
1009 * MESA_DEBUG is 0. In a non-debug build, we don't
1010 * print warning messages *unless* MESA_DEBUG is
1011 * set *to any value*.
1014 debug
= (env
!= NULL
&& _mesa_atoi(env
) == 0) ? 0 : 1;
1016 debug
= (env
!= NULL
) ? 1 : 0;
1020 /* Now only print the string if we're required to do so. */
1022 fprintf(stderr
, "%s: %s", prefixString
, outputString
);
1024 fprintf(stderr
, "\n");
1026 #if defined(_WIN32) && !defined(_WIN32_WCE)
1027 /* stderr from windows applications without console is not usually
1028 * visible, so communicate with the debugger instead */
1031 _mesa_snprintf(buf
, sizeof(buf
), "%s: %s%s", prefixString
, outputString
, newline
? "\n" : "");
1032 OutputDebugStringA(buf
);
1040 * Return string version of GL error code.
1043 error_string( GLenum error
)
1047 return "GL_NO_ERROR";
1048 case GL_INVALID_VALUE
:
1049 return "GL_INVALID_VALUE";
1050 case GL_INVALID_ENUM
:
1051 return "GL_INVALID_ENUM";
1052 case GL_INVALID_OPERATION
:
1053 return "GL_INVALID_OPERATION";
1054 case GL_STACK_OVERFLOW
:
1055 return "GL_STACK_OVERFLOW";
1056 case GL_STACK_UNDERFLOW
:
1057 return "GL_STACK_UNDERFLOW";
1058 case GL_OUT_OF_MEMORY
:
1059 return "GL_OUT_OF_MEMORY";
1060 case GL_TABLE_TOO_LARGE
:
1061 return "GL_TABLE_TOO_LARGE";
1062 case GL_INVALID_FRAMEBUFFER_OPERATION_EXT
:
1063 return "GL_INVALID_FRAMEBUFFER_OPERATION";
1071 * When a new type of error is recorded, print a message describing
1072 * previous errors which were accumulated.
1075 flush_delayed_errors( GLcontext
*ctx
)
1079 if (ctx
->ErrorDebugCount
) {
1080 _mesa_snprintf(s
, MAXSTRING
, "%d similar %s errors",
1081 ctx
->ErrorDebugCount
,
1082 error_string(ctx
->ErrorValue
));
1084 output_if_debug("Mesa", s
, GL_TRUE
);
1086 ctx
->ErrorDebugCount
= 0;
1092 * Report a warning (a recoverable error condition) to stderr if
1093 * either DEBUG is defined or the MESA_DEBUG env var is set.
1095 * \param ctx GL context.
1096 * \param fmtString printf()-like format string.
1099 _mesa_warning( GLcontext
*ctx
, const char *fmtString
, ... )
1101 char str
[MAXSTRING
];
1103 va_start( args
, fmtString
);
1104 (void) vsnprintf( str
, MAXSTRING
, fmtString
, args
);
1108 flush_delayed_errors( ctx
);
1110 output_if_debug("Mesa warning", str
, GL_TRUE
);
1115 * Report an internal implementation problem.
1116 * Prints the message to stderr via fprintf().
1118 * \param ctx GL context.
1119 * \param fmtString problem description string.
1122 _mesa_problem( const GLcontext
*ctx
, const char *fmtString
, ... )
1125 char str
[MAXSTRING
];
1128 va_start( args
, fmtString
);
1129 vsnprintf( str
, MAXSTRING
, fmtString
, args
);
1132 fprintf(stderr
, "Mesa %s implementation error: %s\n", MESA_VERSION_STRING
, str
);
1133 fprintf(stderr
, "Please report at bugzilla.freedesktop.org\n");
1138 * Record an OpenGL state error. These usually occur when the user
1139 * passes invalid parameters to a GL function.
1141 * If debugging is enabled (either at compile-time via the DEBUG macro, or
1142 * run-time via the MESA_DEBUG environment variable), report the error with
1145 * \param ctx the GL context.
1146 * \param error the error value.
1147 * \param fmtString printf() style format string, followed by optional args
1150 _mesa_error( GLcontext
*ctx
, GLenum error
, const char *fmtString
, ... )
1152 static GLint debug
= -1;
1154 /* Check debug environment variable only once:
1157 const char *debugEnv
= _mesa_getenv("MESA_DEBUG");
1160 if (debugEnv
&& _mesa_strstr(debugEnv
, "silent"))
1173 if (ctx
->ErrorValue
== error
&&
1174 ctx
->ErrorDebugFmtString
== fmtString
) {
1175 ctx
->ErrorDebugCount
++;
1178 char s
[MAXSTRING
], s2
[MAXSTRING
];
1181 flush_delayed_errors( ctx
);
1183 va_start(args
, fmtString
);
1184 vsnprintf(s
, MAXSTRING
, fmtString
, args
);
1187 _mesa_snprintf(s2
, MAXSTRING
, "%s in %s", error_string(error
), s
);
1188 output_if_debug("Mesa: User error", s2
, GL_TRUE
);
1190 ctx
->ErrorDebugFmtString
= fmtString
;
1191 ctx
->ErrorDebugCount
= 0;
1195 _mesa_record_error(ctx
, error
);
1200 * Report debug information. Print error message to stderr via fprintf().
1201 * No-op if DEBUG mode not enabled.
1203 * \param ctx GL context.
1204 * \param fmtString printf()-style format string, followed by optional args.
1207 _mesa_debug( const GLcontext
*ctx
, const char *fmtString
, ... )
1212 va_start(args
, fmtString
);
1213 vsnprintf(s
, MAXSTRING
, fmtString
, args
);
1215 output_if_debug("Mesa", s
, GL_FALSE
);
1225 * Wrapper for exit().
1228 _mesa_exit( int status
)