mesa: more info in _mesa_problem() call
[mesa.git] / src / mesa / main / debug.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5
4 *
5 * Copyright (C) 1999-2005 Brian Paul 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 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 #include "mtypes.h"
26 #include "colormac.h"
27 #include "context.h"
28 #include "hash.h"
29 #include "imports.h"
30 #include "debug.h"
31 #include "get.h"
32 #include "texobj.h"
33 #include "texformat.h"
34
35
36 /**
37 * Primitive names
38 */
39 const char *_mesa_prim_name[GL_POLYGON+4] = {
40 "GL_POINTS",
41 "GL_LINES",
42 "GL_LINE_LOOP",
43 "GL_LINE_STRIP",
44 "GL_TRIANGLES",
45 "GL_TRIANGLE_STRIP",
46 "GL_TRIANGLE_FAN",
47 "GL_QUADS",
48 "GL_QUAD_STRIP",
49 "GL_POLYGON",
50 "outside begin/end",
51 "inside unkown primitive",
52 "unknown state"
53 };
54
55 void
56 _mesa_print_state( const char *msg, GLuint state )
57 {
58 _mesa_debug(NULL,
59 "%s: (0x%x) %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
60 msg,
61 state,
62 (state & _NEW_MODELVIEW) ? "ctx->ModelView, " : "",
63 (state & _NEW_PROJECTION) ? "ctx->Projection, " : "",
64 (state & _NEW_TEXTURE_MATRIX) ? "ctx->TextureMatrix, " : "",
65 (state & _NEW_COLOR_MATRIX) ? "ctx->ColorMatrix, " : "",
66 (state & _NEW_ACCUM) ? "ctx->Accum, " : "",
67 (state & _NEW_COLOR) ? "ctx->Color, " : "",
68 (state & _NEW_DEPTH) ? "ctx->Depth, " : "",
69 (state & _NEW_EVAL) ? "ctx->Eval/EvalMap, " : "",
70 (state & _NEW_FOG) ? "ctx->Fog, " : "",
71 (state & _NEW_HINT) ? "ctx->Hint, " : "",
72 (state & _NEW_LIGHT) ? "ctx->Light, " : "",
73 (state & _NEW_LINE) ? "ctx->Line, " : "",
74 (state & _NEW_PIXEL) ? "ctx->Pixel, " : "",
75 (state & _NEW_POINT) ? "ctx->Point, " : "",
76 (state & _NEW_POLYGON) ? "ctx->Polygon, " : "",
77 (state & _NEW_POLYGONSTIPPLE) ? "ctx->PolygonStipple, " : "",
78 (state & _NEW_SCISSOR) ? "ctx->Scissor, " : "",
79 (state & _NEW_TEXTURE) ? "ctx->Texture, " : "",
80 (state & _NEW_TRANSFORM) ? "ctx->Transform, " : "",
81 (state & _NEW_VIEWPORT) ? "ctx->Viewport, " : "",
82 (state & _NEW_PACKUNPACK) ? "ctx->Pack/Unpack, " : "",
83 (state & _NEW_ARRAY) ? "ctx->Array, " : "",
84 (state & _NEW_RENDERMODE) ? "ctx->RenderMode, " : "",
85 (state & _NEW_BUFFERS) ? "ctx->Visual, ctx->DrawBuffer,, " : "");
86 }
87
88
89
90 void
91 _mesa_print_tri_caps( const char *name, GLuint flags )
92 {
93 _mesa_debug(NULL,
94 "%s: (0x%x) %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
95 name,
96 flags,
97 (flags & DD_FLATSHADE) ? "flat-shade, " : "",
98 (flags & DD_SEPARATE_SPECULAR) ? "separate-specular, " : "",
99 (flags & DD_TRI_LIGHT_TWOSIDE) ? "tri-light-twoside, " : "",
100 (flags & DD_TRI_TWOSTENCIL) ? "tri-twostencil, " : "",
101 (flags & DD_TRI_UNFILLED) ? "tri-unfilled, " : "",
102 (flags & DD_TRI_STIPPLE) ? "tri-stipple, " : "",
103 (flags & DD_TRI_OFFSET) ? "tri-offset, " : "",
104 (flags & DD_TRI_SMOOTH) ? "tri-smooth, " : "",
105 (flags & DD_LINE_SMOOTH) ? "line-smooth, " : "",
106 (flags & DD_LINE_STIPPLE) ? "line-stipple, " : "",
107 (flags & DD_LINE_WIDTH) ? "line-wide, " : "",
108 (flags & DD_POINT_SMOOTH) ? "point-smooth, " : "",
109 (flags & DD_POINT_SIZE) ? "point-size, " : "",
110 (flags & DD_POINT_ATTEN) ? "point-atten, " : "",
111 (flags & DD_TRI_CULL_FRONT_BACK) ? "cull-all, " : ""
112 );
113 }
114
115
116 /**
117 * Print information about this Mesa version and build options.
118 */
119 void _mesa_print_info( void )
120 {
121 _mesa_debug(NULL, "Mesa GL_VERSION = %s\n",
122 (char *) _mesa_GetString(GL_VERSION));
123 _mesa_debug(NULL, "Mesa GL_RENDERER = %s\n",
124 (char *) _mesa_GetString(GL_RENDERER));
125 _mesa_debug(NULL, "Mesa GL_VENDOR = %s\n",
126 (char *) _mesa_GetString(GL_VENDOR));
127 _mesa_debug(NULL, "Mesa GL_EXTENSIONS = %s\n",
128 (char *) _mesa_GetString(GL_EXTENSIONS));
129 #if defined(THREADS)
130 _mesa_debug(NULL, "Mesa thread-safe: YES\n");
131 #else
132 _mesa_debug(NULL, "Mesa thread-safe: NO\n");
133 #endif
134 #if defined(USE_X86_ASM)
135 _mesa_debug(NULL, "Mesa x86-optimized: YES\n");
136 #else
137 _mesa_debug(NULL, "Mesa x86-optimized: NO\n");
138 #endif
139 #if defined(USE_SPARC_ASM)
140 _mesa_debug(NULL, "Mesa sparc-optimized: YES\n");
141 #else
142 _mesa_debug(NULL, "Mesa sparc-optimized: NO\n");
143 #endif
144 }
145
146
147 /**
148 * Set the debugging flags.
149 *
150 * \param debug debug string
151 *
152 * If compiled with debugging support then search for keywords in \p debug and
153 * enables the verbose debug output of the respective feature.
154 */
155 static void add_debug_flags( const char *debug )
156 {
157 #ifdef DEBUG
158 struct debug_option {
159 const char *name;
160 GLbitfield flag;
161 };
162 static const struct debug_option debug_opt[] = {
163 { "varray", VERBOSE_VARRAY },
164 { "tex", VERBOSE_TEXTURE },
165 { "imm", VERBOSE_IMMEDIATE },
166 { "pipe", VERBOSE_PIPELINE },
167 { "driver", VERBOSE_DRIVER },
168 { "state", VERBOSE_STATE },
169 { "api", VERBOSE_API },
170 { "list", VERBOSE_DISPLAY_LIST },
171 { "lighting", VERBOSE_LIGHTING },
172 { "disassem", VERBOSE_DISASSEM }
173 };
174 GLuint i;
175
176 MESA_VERBOSE = 0x0;
177 for (i = 0; i < Elements(debug_opt); i++) {
178 if (_mesa_strstr(debug, debug_opt[i].name))
179 MESA_VERBOSE |= debug_opt[i].flag;
180 }
181
182 /* Debug flag:
183 */
184 if (_mesa_strstr(debug, "flush"))
185 MESA_DEBUG_FLAGS |= DEBUG_ALWAYS_FLUSH;
186
187 #if defined(_FPU_GETCW) && defined(_FPU_SETCW)
188 if (_mesa_strstr(debug, "fpexceptions")) {
189 /* raise FP exceptions */
190 fpu_control_t mask;
191 _FPU_GETCW(mask);
192 mask &= ~(_FPU_MASK_IM | _FPU_MASK_DM | _FPU_MASK_ZM
193 | _FPU_MASK_OM | _FPU_MASK_UM);
194 _FPU_SETCW(mask);
195 }
196 #endif
197
198 #else
199 (void) debug;
200 #endif
201 }
202
203
204 void
205 _mesa_init_debug( GLcontext *ctx )
206 {
207 char *c;
208
209 /* Dither disable */
210 ctx->NoDither = _mesa_getenv("MESA_NO_DITHER") ? GL_TRUE : GL_FALSE;
211 if (ctx->NoDither) {
212 if (_mesa_getenv("MESA_DEBUG")) {
213 _mesa_debug(ctx, "MESA_NO_DITHER set - dithering disabled\n");
214 }
215 ctx->Color.DitherFlag = GL_FALSE;
216 }
217
218 c = _mesa_getenv("MESA_DEBUG");
219 if (c)
220 add_debug_flags(c);
221
222 c = _mesa_getenv("MESA_VERBOSE");
223 if (c)
224 add_debug_flags(c);
225 }
226
227
228 /*
229 * Write ppm file
230 */
231 static void
232 write_ppm(const char *filename, const GLubyte *buffer, int width, int height,
233 int comps, int rcomp, int gcomp, int bcomp)
234 {
235 FILE *f = fopen( filename, "w" );
236 if (f) {
237 int i, x, y;
238 const GLubyte *ptr = buffer;
239 fprintf(f,"P6\n");
240 fprintf(f,"# ppm-file created by osdemo.c\n");
241 fprintf(f,"%i %i\n", width,height);
242 fprintf(f,"255\n");
243 fclose(f);
244 f = fopen( filename, "ab" ); /* reopen in binary append mode */
245 for (y=height-1; y>=0; y--) {
246 for (x=0; x<width; x++) {
247 i = (y*width + x) * comps;
248 fputc(ptr[i+rcomp], f); /* write red */
249 fputc(ptr[i+gcomp], f); /* write green */
250 fputc(ptr[i+bcomp], f); /* write blue */
251 }
252 }
253 fclose(f);
254 }
255 }
256
257
258 /**
259 * Write level[0] image to a ppm file.
260 */
261 static void
262 write_texture_image(struct gl_texture_object *texObj)
263 {
264 const struct gl_texture_image *img = texObj->Image[0][0];
265 if (img) {
266 char s[100];
267
268 /* make filename */
269 sprintf(s, "/tmp/teximage%u.ppm", texObj->Name);
270
271 switch (img->TexFormat->MesaFormat) {
272 case MESA_FORMAT_RGBA8888:
273 write_ppm(s, img->Data, img->Width, img->Height, 4, 3, 2, 1);
274 break;
275 case MESA_FORMAT_ARGB8888:
276 write_ppm(s, img->Data, img->Width, img->Height, 4, 2, 1, 0);
277 break;
278 case MESA_FORMAT_RGB888:
279 write_ppm(s, img->Data, img->Width, img->Height, 3, 2, 1, 0);
280 break;
281 case MESA_FORMAT_RGB565:
282 {
283 GLubyte *buf2 = (GLubyte *) _mesa_malloc(img->Width * img->Height * 3);
284 GLint i;
285 for (i = 0; i < img->Width * img->Height; i++) {
286 GLint r, g, b;
287 GLushort s = ((GLushort *) img->Data)[i];
288 r = UBYTE_TO_CHAN( ((s >> 8) & 0xf8) | ((s >> 13) & 0x7) );
289 g = UBYTE_TO_CHAN( ((s >> 3) & 0xfc) | ((s >> 9) & 0x3) );
290 b = UBYTE_TO_CHAN( ((s << 3) & 0xf8) | ((s >> 2) & 0x7) );
291 buf2[i*3+1] = r;
292 buf2[i*3+2] = g;
293 buf2[i*3+3] = b;
294 }
295 write_ppm(s, buf2, img->Width, img->Height, 3, 2, 1, 0);
296 _mesa_free(buf2);
297 }
298 break;
299 default:
300 printf("XXXX unsupported mesa tex format %d in %s\n",
301 img->TexFormat->MesaFormat, __FUNCTION__);
302 }
303 }
304 }
305
306
307 static GLboolean DumpImages;
308
309
310 static void
311 dump_texture_cb(GLuint id, void *data, void *userData)
312 {
313 struct gl_texture_object *texObj = (struct gl_texture_object *) data;
314 int i;
315 (void) userData;
316
317 printf("Texture %u\n", texObj->Name);
318 printf(" Target 0x%x\n", texObj->Target);
319 for (i = 0; i < MAX_TEXTURE_LEVELS; i++) {
320 struct gl_texture_image *texImg = texObj->Image[0][i];
321 if (texImg) {
322 printf(" Image %u: %d x %d x %d at %p\n", i,
323 texImg->Width, texImg->Height, texImg->Depth, texImg->Data);
324 if (DumpImages && i == 0) {
325 write_texture_image(texObj);
326 }
327 }
328 }
329 }
330
331
332 /**
333 * Print basic info about all texture objext to stdout.
334 * If dumpImages is true, write PPM of level[0] image to a file.
335 */
336 void
337 _mesa_dump_textures(GLboolean dumpImages)
338 {
339 GET_CURRENT_CONTEXT(ctx);
340 DumpImages = dumpImages;
341 _mesa_HashDeleteAll(ctx->Shared->TexObjects, dump_texture_cb, ctx);
342 }