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.
59 #define MAXSTRING 4000 /* for vsnprintf() */
62 #define vsnprintf _vsnprintf
63 #elif defined(__IBMC__) || defined(__IBMCPP__) || ( defined(__VMS) && __CRTL_VER < 70312000 )
64 extern int vsnprintf(char *str
, size_t count
, const char *fmt
, va_list arg
);
66 #include "vsnprintf.c"
70 /**********************************************************************/
74 /** Wrapper around malloc() */
76 _mesa_malloc(size_t bytes
)
81 /** Wrapper around calloc() */
83 _mesa_calloc(size_t bytes
)
85 return calloc(1, bytes
);
88 /** Wrapper around free() */
96 * Allocate aligned memory.
98 * \param bytes number of bytes to allocate.
99 * \param alignment alignment (must be greater than zero).
101 * Allocates extra memory to accommodate rounding up the address for
102 * alignment and to record the real malloc address.
104 * \sa _mesa_align_free().
107 _mesa_align_malloc(size_t bytes
, unsigned long alignment
)
109 #if defined(HAVE_POSIX_MEMALIGN)
111 int err
= posix_memalign(& mem
, alignment
, bytes
);
114 #elif defined(_WIN32) && defined(_MSC_VER)
115 return _aligned_malloc(bytes
, alignment
);
119 ASSERT( alignment
> 0 );
121 ptr
= (uintptr_t) _mesa_malloc(bytes
+ alignment
+ sizeof(void *));
125 buf
= (ptr
+ alignment
+ sizeof(void *)) & ~(uintptr_t)(alignment
- 1);
126 *(uintptr_t *)(buf
- sizeof(void *)) = ptr
;
129 /* mark the non-aligned area */
130 while ( ptr
< buf
- sizeof(void *) ) {
131 *(unsigned long *)ptr
= 0xcdcdcdcd;
132 ptr
+= sizeof(unsigned long);
137 #endif /* defined(HAVE_POSIX_MEMALIGN) */
141 * Same as _mesa_align_malloc(), but using _mesa_calloc() instead of
145 _mesa_align_calloc(size_t bytes
, unsigned long alignment
)
147 #if defined(HAVE_POSIX_MEMALIGN)
150 mem
= _mesa_align_malloc(bytes
, alignment
);
152 (void) memset(mem
, 0, bytes
);
156 #elif defined(_WIN32) && defined(_MSC_VER)
159 mem
= _aligned_malloc(bytes
, alignment
);
161 (void) memset(mem
, 0, bytes
);
168 ASSERT( alignment
> 0 );
170 ptr
= (uintptr_t) _mesa_calloc(bytes
+ alignment
+ sizeof(void *));
174 buf
= (ptr
+ alignment
+ sizeof(void *)) & ~(uintptr_t)(alignment
- 1);
175 *(uintptr_t *)(buf
- sizeof(void *)) = ptr
;
178 /* mark the non-aligned area */
179 while ( ptr
< buf
- sizeof(void *) ) {
180 *(unsigned long *)ptr
= 0xcdcdcdcd;
181 ptr
+= sizeof(unsigned long);
186 #endif /* defined(HAVE_POSIX_MEMALIGN) */
190 * Free memory which was allocated with either _mesa_align_malloc()
191 * or _mesa_align_calloc().
192 * \param ptr pointer to the memory to be freed.
193 * The actual address to free is stored in the word immediately before the
194 * address the client sees.
197 _mesa_align_free(void *ptr
)
199 #if defined(HAVE_POSIX_MEMALIGN)
201 #elif defined(_WIN32) && defined(_MSC_VER)
204 void **cubbyHole
= (void **) ((char *) ptr
- sizeof(void *));
205 void *realAddr
= *cubbyHole
;
206 _mesa_free(realAddr
);
207 #endif /* defined(HAVE_POSIX_MEMALIGN) */
211 * Reallocate memory, with alignment.
214 _mesa_align_realloc(void *oldBuffer
, size_t oldSize
, size_t newSize
,
215 unsigned long alignment
)
217 #if defined(_WIN32) && defined(_MSC_VER)
219 return _aligned_realloc(oldBuffer
, newSize
, alignment
);
221 const size_t copySize
= (oldSize
< newSize
) ? oldSize
: newSize
;
222 void *newBuf
= _mesa_align_malloc(newSize
, alignment
);
223 if (newBuf
&& oldBuffer
&& copySize
> 0) {
224 _mesa_memcpy(newBuf
, oldBuffer
, copySize
);
227 _mesa_align_free(oldBuffer
);
234 /** Reallocate memory */
236 _mesa_realloc(void *oldBuffer
, size_t oldSize
, size_t newSize
)
238 const size_t copySize
= (oldSize
< newSize
) ? oldSize
: newSize
;
239 void *newBuffer
= _mesa_malloc(newSize
);
240 if (newBuffer
&& oldBuffer
&& copySize
> 0)
241 _mesa_memcpy(newBuffer
, oldBuffer
, copySize
);
243 _mesa_free(oldBuffer
);
247 /** memcpy wrapper */
249 _mesa_memcpy(void *dest
, const void *src
, size_t n
)
252 return memcpy((char *) dest
, (char *) src
, (int) n
);
254 return memcpy(dest
, src
, n
);
258 /** Wrapper around memset() */
260 _mesa_memset( void *dst
, int val
, size_t n
)
263 memset( (char *) dst
, (int) val
, (int) n
);
270 * Fill memory with a constant 16bit word.
271 * \param dst destination pointer.
273 * \param n number of words.
276 _mesa_memset16( unsigned short *dst
, unsigned short val
, size_t n
)
282 /** Wrapper around either memset() or bzero() */
284 _mesa_bzero( void *dst
, size_t n
)
286 #if defined(__FreeBSD__)
293 /** Wrapper around memcmp() */
295 _mesa_memcmp( const void *s1
, const void *s2
, size_t n
)
298 return memcmp( (char *) s1
, (char *) s2
, (int) n
);
300 return memcmp(s1
, s2
, n
);
307 /**********************************************************************/
311 /** Wrapper around sin() */
318 /** Single precision wrapper around sin() */
322 return (float) sin((double) a
);
325 /** Wrapper around cos() */
332 /** Single precision wrapper around asin() */
336 return (float) asin((double) x
);
339 /** Single precision wrapper around atan() */
343 return (float) atan((double) x
);
346 /** Wrapper around sqrt() */
348 _mesa_sqrtd(double x
)
355 * A High Speed, Low Precision Square Root
356 * by Paul Lalonde and Robert Dawson
357 * from "Graphics Gems", Academic Press, 1990
359 * SPARC implementation of a fast square root by table
361 * SPARC floating point format is as follows:
364 * sign exponent mantissa
366 static short sqrttab
[0x100]; /* declare table of square roots */
369 _mesa_init_sqrt_table(void)
371 #if defined(USE_IEEE) && !defined(DEBUG)
373 fi_type fi
; /* to access the bits of a float in C quickly */
374 /* we use a union defined in glheader.h */
376 for(i
=0; i
<= 0x7f; i
++) {
380 * Build a float with the bit pattern i as mantissa
381 * and an exponent of 0, stored as 127
384 fi
.i
= (i
<< 16) | (127 << 23);
385 fi
.f
= _mesa_sqrtd(fi
.f
);
388 * Take the square root then strip the first 7 bits of
389 * the mantissa into the table
392 sqrttab
[i
] = (fi
.i
& 0x7fffff) >> 16;
395 * Repeat the process, this time with an exponent of
400 fi
.i
= (i
<< 16) | (128 << 23);
402 sqrttab
[i
+0x80] = (fi
.i
& 0x7fffff) >> 16;
405 (void) sqrttab
; /* silence compiler warnings */
406 #endif /*HAVE_FAST_MATH*/
411 * Single precision square root.
414 _mesa_sqrtf( float x
)
416 #if defined(USE_IEEE) && !defined(DEBUG)
418 /* to access the bits of a float in C
419 * we use a union from glheader.h */
421 short e
; /* the exponent */
422 if (x
== 0.0F
) return 0.0F
; /* check for square root of 0 */
424 e
= (num
.i
>> 23) - 127; /* get the exponent - on a SPARC the */
425 /* exponent is stored with 127 added */
426 num
.i
&= 0x7fffff; /* leave only the mantissa */
427 if (e
& 0x01) num
.i
|= 0x800000;
428 /* the exponent is odd so we have to */
429 /* look it up in the second half of */
430 /* the lookup table, so we set the */
432 e
>>= 1; /* divide the exponent by two */
433 /* note that in C the shift */
434 /* operators are sign preserving */
435 /* for signed operands */
436 /* Do the table lookup, based on the quaternary mantissa,
437 * then reconstruct the result back into a float
439 num
.i
= ((sqrttab
[num
.i
>> 16]) << 16) | ((e
+ 127) << 23);
443 return (float) _mesa_sqrtd((double) x
);
449 inv_sqrt - A single precision 1/sqrt routine for IEEE format floats.
450 written by Josh Vanderhoof, based on newsgroup posts by James Van Buskirk
454 _mesa_inv_sqrtf(float n
)
456 #if defined(USE_IEEE) && !defined(DEBUG)
460 #if 0 /* not used, see below -BP */
467 Exponent part of the magic number -
470 1. subtract the bias from the exponent,
472 3. divide by two (rounding towards -inf)
475 Which is the same as subtracting the exponent from 381 and dividing
478 floor(-(x - 127) / 2) + 127 = floor((381 - x) / 2)
484 Significand part of magic number -
486 With the current magic number, "(magic - u.i) >> 1" will give you:
488 for 1 <= u.f <= 2: 1.25 - u.f / 4
489 for 2 <= u.f <= 4: 1.00 - u.f / 8
491 This isn't a bad approximation of 1/sqrt. The maximum difference from
492 1/sqrt will be around .06. After three Newton-Raphson iterations, the
493 maximum difference is less than 4.5e-8. (Which is actually close
494 enough to make the following bias academic...)
496 To get a better approximation you can add a bias to the magic
497 number. For example, if you subtract 1/2 of the maximum difference in
498 the first approximation (.03), you will get the following function:
500 for 1 <= u.f <= 2: 1.22 - u.f / 4
501 for 2 <= u.f <= 3.76: 0.97 - u.f / 8
502 for 3.76 <= u.f <= 4: 0.72 - u.f / 16
503 (The 3.76 to 4 range is where the result is < .5.)
505 This is the closest possible initial approximation, but with a maximum
506 error of 8e-11 after three NR iterations, it is still not perfect. If
507 you subtract 0.0332281 instead of .03, the maximum error will be
508 2.5e-11 after three NR iterations, which should be about as close as
511 for 1 <= u.f <= 2: 1.2167719 - u.f / 4
512 for 2 <= u.f <= 3.73: 0.9667719 - u.f / 8
513 for 3.73 <= u.f <= 4: 0.7167719 - u.f / 16
517 magic
-= (int)(0.0332281 * (1 << 25));
520 u
.i
= (magic
- u
.i
) >> 1;
523 Instead of Newton-Raphson, we use Goldschmidt's algorithm, which
524 allows more parallelism. From what I understand, the parallelism
525 comes at the cost of less precision, because it lets error
526 accumulate across iterations.
541 return x2
* r2
; /* we can stop here, and be conformant -BP */
550 return (float) (1.0 / sqrt(n
));
555 /** Wrapper around pow() */
557 _mesa_pow(double x
, double y
)
564 * Find the first bit set in a word.
569 #if (defined(_WIN32) ) || defined(__IBMC__) || defined(__IBMCPP__)
570 register int bit
= 0;
572 if ((i
& 0xffff) == 0) {
576 if ((i
& 0xff) == 0) {
580 if ((i
& 0xf) == 0) {
584 while ((i
& 1) == 0) {
598 * Find position of first bit set in given value.
599 * XXX Warning: this function can only be used on 64-bit systems!
600 * \return position of least-significant bit set, starting at 1, return zero
604 _mesa_ffsll(int64_t val
)
611 assert(sizeof(val
) == 8);
613 bit
= _mesa_ffs((int32_t)val
);
617 bit
= _mesa_ffs((int32_t)(val
>> 32));
627 * Return number of bits set in given GLuint.
630 _mesa_bitcount(unsigned int n
)
632 #if defined(__GNUC__)
633 return __builtin_popcount(n
);
636 for (bits
= 0; n
> 0; n
= n
>> 1) {
645 * Convert a 4-byte float to a 2-byte half float.
646 * Based on code from:
647 * http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/008786.html
650 _mesa_float_to_half(float val
)
652 const fi_type fi
= {val
};
653 const int flt_m
= fi
.i
& 0x7fffff;
654 const int flt_e
= (fi
.i
>> 23) & 0xff;
655 const int flt_s
= (fi
.i
>> 31) & 0x1;
662 /* handle special cases */
663 if ((flt_e
== 0) && (flt_m
== 0)) {
665 /* m = 0; - already set */
668 else if ((flt_e
== 0) && (flt_m
!= 0)) {
669 /* denorm -- denorm float maps to 0 half */
670 /* m = 0; - already set */
673 else if ((flt_e
== 0xff) && (flt_m
== 0)) {
675 /* m = 0; - already set */
678 else if ((flt_e
== 0xff) && (flt_m
!= 0)) {
685 const int new_exp
= flt_e
- 127;
688 /* m = 0; - already set */
691 else if (new_exp
< -14) {
692 /* this maps to a denorm */
693 unsigned int exp_val
= (unsigned int) (-14 - new_exp
); /* 2^-exp_val*/
698 "float_to_half: logical error in denorm creation!\n");
699 /* m = 0; - already set */
701 case 1: m
= 512 + (flt_m
>> 14); break;
702 case 2: m
= 256 + (flt_m
>> 15); break;
703 case 3: m
= 128 + (flt_m
>> 16); break;
704 case 4: m
= 64 + (flt_m
>> 17); break;
705 case 5: m
= 32 + (flt_m
>> 18); break;
706 case 6: m
= 16 + (flt_m
>> 19); break;
707 case 7: m
= 8 + (flt_m
>> 20); break;
708 case 8: m
= 4 + (flt_m
>> 21); break;
709 case 9: m
= 2 + (flt_m
>> 22); break;
710 case 10: m
= 1; break;
713 else if (new_exp
> 15) {
714 /* map this value to infinity */
715 /* m = 0; - already set */
725 result
= (s
<< 15) | (e
<< 10) | m
;
731 * Convert a 2-byte half float to a 4-byte float.
732 * Based on code from:
733 * http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/008786.html
736 _mesa_half_to_float(GLhalfARB val
)
738 /* XXX could also use a 64K-entry lookup table */
739 const int m
= val
& 0x3ff;
740 const int e
= (val
>> 10) & 0x1f;
741 const int s
= (val
>> 15) & 0x1;
742 int flt_m
, flt_e
, flt_s
;
749 /* handle special cases */
750 if ((e
== 0) && (m
== 0)) {
755 else if ((e
== 0) && (m
!= 0)) {
756 /* denorm -- denorm half will fit in non-denorm single */
757 const float half_denorm
= 1.0f
/ 16384.0f
; /* 2^-14 */
758 float mantissa
= ((float) (m
)) / 1024.0f
;
759 float sign
= s
? -1.0f
: 1.0f
;
760 return sign
* mantissa
* half_denorm
;
762 else if ((e
== 31) && (m
== 0)) {
767 else if ((e
== 31) && (m
!= 0)) {
778 fi
.i
= (flt_s
<< 31) | (flt_e
<< 23) | flt_m
;
786 /**********************************************************************/
787 /** \name Sort & Search */
791 * Wrapper for bsearch().
794 _mesa_bsearch( const void *key
, const void *base
, size_t nmemb
, size_t size
,
795 int (*compar
)(const void *, const void *) )
797 #if defined(_WIN32_WCE)
802 mid
= (char *)base
+ nmemb
* size
;
803 cmp
= (*compar
)(key
, mid
);
807 base
= (char *)mid
+ size
;
813 return bsearch(key
, base
, nmemb
, size
, compar
);
820 /**********************************************************************/
821 /** \name Environment vars */
825 * Wrapper for getenv().
828 _mesa_getenv( const char *var
)
830 #if defined(_XBOX) || defined(_WIN32_WCE)
840 /**********************************************************************/
844 /** Wrapper around strstr() */
846 _mesa_strstr( const char *haystack
, const char *needle
)
848 return strstr(haystack
, needle
);
851 /** Wrapper around strncat() */
853 _mesa_strncat( char *dest
, const char *src
, size_t n
)
855 return strncat(dest
, src
, n
);
858 /** Wrapper around strcpy() */
860 _mesa_strcpy( char *dest
, const char *src
)
862 return strcpy(dest
, src
);
865 /** Wrapper around strncpy() */
867 _mesa_strncpy( char *dest
, const char *src
, size_t n
)
869 return strncpy(dest
, src
, n
);
872 /** Wrapper around strlen() */
874 _mesa_strlen( const char *s
)
879 /** Wrapper around strcmp() */
881 _mesa_strcmp( const char *s1
, const char *s2
)
883 return strcmp(s1
, s2
);
886 /** Wrapper around strncmp() */
888 _mesa_strncmp( const char *s1
, const char *s2
, size_t n
)
890 return strncmp(s1
, s2
, n
);
894 * Implemented using _mesa_malloc() and _mesa_strcpy.
895 * Note that NULL is handled accordingly.
898 _mesa_strdup( const char *s
)
901 size_t l
= _mesa_strlen(s
);
902 char *s2
= (char *) _mesa_malloc(l
+ 1);
912 /** Wrapper around atoi() */
914 _mesa_atoi(const char *s
)
919 /** Wrapper around strtod() */
921 _mesa_strtod( const char *s
, char **end
)
924 static locale_t loc
= NULL
;
926 loc
= newlocale(LC_CTYPE_MASK
, "C", NULL
);
928 return strtod_l(s
, end
, loc
);
930 return strtod(s
, end
);
934 /** Compute simple checksum/hash for a string */
936 _mesa_str_checksum(const char *str
)
938 /* This could probably be much better */
942 for (c
= str
; *c
; c
++, i
++)
943 sum
+= *c
* (i
% 100);
951 /**********************************************************************/
955 /** Wrapper around vsprintf() */
957 _mesa_sprintf( char *str
, const char *fmt
, ... )
961 va_start( args
, fmt
);
962 r
= vsprintf( str
, fmt
, args
);
967 /** Wrapper around vsnprintf() */
969 _mesa_snprintf( char *str
, size_t size
, const char *fmt
, ... )
973 va_start( args
, fmt
);
974 r
= vsnprintf( str
, size
, fmt
, args
);
979 /** Wrapper around printf(), using vsprintf() for the formatting. */
981 _mesa_printf( const char *fmtString
, ... )
984 va_start( args
, fmtString
);
985 vfprintf(stderr
, fmtString
, args
);
989 /** Wrapper around fprintf(), using vsprintf() for the formatting. */
991 _mesa_fprintf( FILE *f
, const char *fmtString
, ... )
995 va_start( args
, fmtString
);
996 vsnprintf(s
, MAXSTRING
, fmtString
, args
);
1002 /** Wrapper around vsprintf() */
1004 _mesa_vsprintf( char *str
, const char *fmt
, va_list args
)
1006 return vsprintf( str
, fmt
, args
);
1012 /**********************************************************************/
1013 /** \name Diagnostics */
1017 output_if_debug(const char *prefixString
, const char *outputString
,
1020 static int debug
= -1;
1022 /* Check the MESA_DEBUG environment variable if it hasn't
1023 * been checked yet. We only have to check it once...
1026 char *env
= _mesa_getenv("MESA_DEBUG");
1028 /* In a debug build, we print warning messages *unless*
1029 * MESA_DEBUG is 0. In a non-debug build, we don't
1030 * print warning messages *unless* MESA_DEBUG is
1031 * set *to any value*.
1034 debug
= (env
!= NULL
&& _mesa_atoi(env
) == 0) ? 0 : 1;
1036 debug
= (env
!= NULL
) ? 1 : 0;
1040 /* Now only print the string if we're required to do so. */
1042 fprintf(stderr
, "%s: %s", prefixString
, outputString
);
1044 fprintf(stderr
, "\n");
1046 #if defined(_WIN32) && !defined(_WIN32_WCE)
1047 /* stderr from windows applications without console is not usually
1048 * visible, so communicate with the debugger instead */
1051 _mesa_snprintf(buf
, sizeof(buf
), "%s: %s%s", prefixString
, outputString
, newline
? "\n" : "");
1052 OutputDebugStringA(buf
);
1060 * Return string version of GL error code.
1063 error_string( GLenum error
)
1067 return "GL_NO_ERROR";
1068 case GL_INVALID_VALUE
:
1069 return "GL_INVALID_VALUE";
1070 case GL_INVALID_ENUM
:
1071 return "GL_INVALID_ENUM";
1072 case GL_INVALID_OPERATION
:
1073 return "GL_INVALID_OPERATION";
1074 case GL_STACK_OVERFLOW
:
1075 return "GL_STACK_OVERFLOW";
1076 case GL_STACK_UNDERFLOW
:
1077 return "GL_STACK_UNDERFLOW";
1078 case GL_OUT_OF_MEMORY
:
1079 return "GL_OUT_OF_MEMORY";
1080 case GL_TABLE_TOO_LARGE
:
1081 return "GL_TABLE_TOO_LARGE";
1082 case GL_INVALID_FRAMEBUFFER_OPERATION_EXT
:
1083 return "GL_INVALID_FRAMEBUFFER_OPERATION";
1091 * When a new type of error is recorded, print a message describing
1092 * previous errors which were accumulated.
1095 flush_delayed_errors( GLcontext
*ctx
)
1099 if (ctx
->ErrorDebugCount
) {
1100 _mesa_snprintf(s
, MAXSTRING
, "%d similar %s errors",
1101 ctx
->ErrorDebugCount
,
1102 error_string(ctx
->ErrorValue
));
1104 output_if_debug("Mesa", s
, GL_TRUE
);
1106 ctx
->ErrorDebugCount
= 0;
1112 * Report a warning (a recoverable error condition) to stderr if
1113 * either DEBUG is defined or the MESA_DEBUG env var is set.
1115 * \param ctx GL context.
1116 * \param fmtString printf()-like format string.
1119 _mesa_warning( GLcontext
*ctx
, const char *fmtString
, ... )
1121 char str
[MAXSTRING
];
1123 va_start( args
, fmtString
);
1124 (void) vsnprintf( str
, MAXSTRING
, fmtString
, args
);
1128 flush_delayed_errors( ctx
);
1130 output_if_debug("Mesa warning", str
, GL_TRUE
);
1135 * Report an internal implementation problem.
1136 * Prints the message to stderr via fprintf().
1138 * \param ctx GL context.
1139 * \param fmtString problem description string.
1142 _mesa_problem( const GLcontext
*ctx
, const char *fmtString
, ... )
1145 char str
[MAXSTRING
];
1148 va_start( args
, fmtString
);
1149 vsnprintf( str
, MAXSTRING
, fmtString
, args
);
1152 fprintf(stderr
, "Mesa %s implementation error: %s\n", MESA_VERSION_STRING
, str
);
1153 fprintf(stderr
, "Please report at bugzilla.freedesktop.org\n");
1158 * Record an OpenGL state error. These usually occur when the user
1159 * passes invalid parameters to a GL function.
1161 * If debugging is enabled (either at compile-time via the DEBUG macro, or
1162 * run-time via the MESA_DEBUG environment variable), report the error with
1165 * \param ctx the GL context.
1166 * \param error the error value.
1167 * \param fmtString printf() style format string, followed by optional args
1170 _mesa_error( GLcontext
*ctx
, GLenum error
, const char *fmtString
, ... )
1172 static GLint debug
= -1;
1174 /* Check debug environment variable only once:
1177 const char *debugEnv
= _mesa_getenv("MESA_DEBUG");
1180 if (debugEnv
&& _mesa_strstr(debugEnv
, "silent"))
1193 if (ctx
->ErrorValue
== error
&&
1194 ctx
->ErrorDebugFmtString
== fmtString
) {
1195 ctx
->ErrorDebugCount
++;
1198 char s
[MAXSTRING
], s2
[MAXSTRING
];
1201 flush_delayed_errors( ctx
);
1203 va_start(args
, fmtString
);
1204 vsnprintf(s
, MAXSTRING
, fmtString
, args
);
1207 _mesa_snprintf(s2
, MAXSTRING
, "%s in %s", error_string(error
), s
);
1208 output_if_debug("Mesa: User error", s2
, GL_TRUE
);
1210 ctx
->ErrorDebugFmtString
= fmtString
;
1211 ctx
->ErrorDebugCount
= 0;
1215 _mesa_record_error(ctx
, error
);
1220 * Report debug information. Print error message to stderr via fprintf().
1221 * No-op if DEBUG mode not enabled.
1223 * \param ctx GL context.
1224 * \param fmtString printf()-style format string, followed by optional args.
1227 _mesa_debug( const GLcontext
*ctx
, const char *fmtString
, ... )
1232 va_start(args
, fmtString
);
1233 vsnprintf(s
, MAXSTRING
, fmtString
, args
);
1235 output_if_debug("Mesa", s
, GL_FALSE
);