mesa: clean-up _mesa_lookup_prim_by_nr()
[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 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 #include "mtypes.h"
27 #include "attrib.h"
28 #include "colormac.h"
29 #include "enums.h"
30 #include "formats.h"
31 #include "hash.h"
32 #include "imports.h"
33 #include "debug.h"
34 #include "get.h"
35 #include "pixelstore.h"
36 #include "readpix.h"
37 #include "texobj.h"
38
39
40 static const char *
41 tex_target_name(GLenum tgt)
42 {
43 static const struct {
44 GLenum target;
45 const char *name;
46 } tex_targets[] = {
47 { GL_TEXTURE_1D, "GL_TEXTURE_1D" },
48 { GL_TEXTURE_2D, "GL_TEXTURE_2D" },
49 { GL_TEXTURE_3D, "GL_TEXTURE_3D" },
50 { GL_TEXTURE_CUBE_MAP, "GL_TEXTURE_CUBE_MAP" },
51 { GL_TEXTURE_RECTANGLE, "GL_TEXTURE_RECTANGLE" },
52 { GL_TEXTURE_1D_ARRAY_EXT, "GL_TEXTURE_1D_ARRAY" },
53 { GL_TEXTURE_2D_ARRAY_EXT, "GL_TEXTURE_2D_ARRAY" }
54 };
55 GLuint i;
56 for (i = 0; i < Elements(tex_targets); i++) {
57 if (tex_targets[i].target == tgt)
58 return tex_targets[i].name;
59 }
60 return "UNKNOWN TEX TARGET";
61 }
62
63
64 void
65 _mesa_print_state( const char *msg, GLuint state )
66 {
67 _mesa_debug(NULL,
68 "%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",
69 msg,
70 state,
71 (state & _NEW_MODELVIEW) ? "ctx->ModelView, " : "",
72 (state & _NEW_PROJECTION) ? "ctx->Projection, " : "",
73 (state & _NEW_TEXTURE_MATRIX) ? "ctx->TextureMatrix, " : "",
74 (state & _NEW_ACCUM) ? "ctx->Accum, " : "",
75 (state & _NEW_COLOR) ? "ctx->Color, " : "",
76 (state & _NEW_DEPTH) ? "ctx->Depth, " : "",
77 (state & _NEW_EVAL) ? "ctx->Eval/EvalMap, " : "",
78 (state & _NEW_FOG) ? "ctx->Fog, " : "",
79 (state & _NEW_HINT) ? "ctx->Hint, " : "",
80 (state & _NEW_LIGHT) ? "ctx->Light, " : "",
81 (state & _NEW_LINE) ? "ctx->Line, " : "",
82 (state & _NEW_PIXEL) ? "ctx->Pixel, " : "",
83 (state & _NEW_POINT) ? "ctx->Point, " : "",
84 (state & _NEW_POLYGON) ? "ctx->Polygon, " : "",
85 (state & _NEW_POLYGONSTIPPLE) ? "ctx->PolygonStipple, " : "",
86 (state & _NEW_SCISSOR) ? "ctx->Scissor, " : "",
87 (state & _NEW_STENCIL) ? "ctx->Stencil, " : "",
88 (state & _NEW_TEXTURE) ? "ctx->Texture, " : "",
89 (state & _NEW_TRANSFORM) ? "ctx->Transform, " : "",
90 (state & _NEW_VIEWPORT) ? "ctx->Viewport, " : "",
91 (state & _NEW_PACKUNPACK) ? "ctx->Pack/Unpack, " : "",
92 (state & _NEW_ARRAY) ? "ctx->Array, " : "",
93 (state & _NEW_RENDERMODE) ? "ctx->RenderMode, " : "",
94 (state & _NEW_BUFFERS) ? "ctx->Visual, ctx->DrawBuffer,, " : "");
95 }
96
97
98
99 void
100 _mesa_print_tri_caps( const char *name, GLuint flags )
101 {
102 _mesa_debug(NULL,
103 "%s: (0x%x) %s%s%s%s%s%s%s%s%s%s%s%s%s\n",
104 name,
105 flags,
106 (flags & DD_FLATSHADE) ? "flat-shade, " : "",
107 (flags & DD_SEPARATE_SPECULAR) ? "separate-specular, " : "",
108 (flags & DD_TRI_LIGHT_TWOSIDE) ? "tri-light-twoside, " : "",
109 (flags & DD_TRI_TWOSTENCIL) ? "tri-twostencil, " : "",
110 (flags & DD_TRI_UNFILLED) ? "tri-unfilled, " : "",
111 (flags & DD_TRI_STIPPLE) ? "tri-stipple, " : "",
112 (flags & DD_TRI_OFFSET) ? "tri-offset, " : "",
113 (flags & DD_TRI_SMOOTH) ? "tri-smooth, " : "",
114 (flags & DD_LINE_SMOOTH) ? "line-smooth, " : "",
115 (flags & DD_LINE_STIPPLE) ? "line-stipple, " : "",
116 (flags & DD_POINT_SMOOTH) ? "point-smooth, " : "",
117 (flags & DD_POINT_ATTEN) ? "point-atten, " : "",
118 (flags & DD_TRI_CULL_FRONT_BACK) ? "cull-all, " : ""
119 );
120 }
121
122
123 /**
124 * Print information about this Mesa version and build options.
125 */
126 void _mesa_print_info( void )
127 {
128 _mesa_debug(NULL, "Mesa GL_VERSION = %s\n",
129 (char *) _mesa_GetString(GL_VERSION));
130 _mesa_debug(NULL, "Mesa GL_RENDERER = %s\n",
131 (char *) _mesa_GetString(GL_RENDERER));
132 _mesa_debug(NULL, "Mesa GL_VENDOR = %s\n",
133 (char *) _mesa_GetString(GL_VENDOR));
134 _mesa_debug(NULL, "Mesa GL_EXTENSIONS = %s\n",
135 (char *) _mesa_GetString(GL_EXTENSIONS));
136 #if defined(THREADS)
137 _mesa_debug(NULL, "Mesa thread-safe: YES\n");
138 #else
139 _mesa_debug(NULL, "Mesa thread-safe: NO\n");
140 #endif
141 #if defined(USE_X86_ASM)
142 _mesa_debug(NULL, "Mesa x86-optimized: YES\n");
143 #else
144 _mesa_debug(NULL, "Mesa x86-optimized: NO\n");
145 #endif
146 #if defined(USE_SPARC_ASM)
147 _mesa_debug(NULL, "Mesa sparc-optimized: YES\n");
148 #else
149 _mesa_debug(NULL, "Mesa sparc-optimized: NO\n");
150 #endif
151 }
152
153
154 /**
155 * Set the debugging flags.
156 *
157 * \param debug debug string
158 *
159 * If compiled with debugging support then search for keywords in \p debug and
160 * enables the verbose debug output of the respective feature.
161 */
162 static void add_debug_flags( const char *debug )
163 {
164 #ifdef DEBUG
165 struct debug_option {
166 const char *name;
167 GLbitfield flag;
168 };
169 static const struct debug_option debug_opt[] = {
170 { "varray", VERBOSE_VARRAY },
171 { "tex", VERBOSE_TEXTURE },
172 { "mat", VERBOSE_MATERIAL },
173 { "pipe", VERBOSE_PIPELINE },
174 { "driver", VERBOSE_DRIVER },
175 { "state", VERBOSE_STATE },
176 { "api", VERBOSE_API },
177 { "list", VERBOSE_DISPLAY_LIST },
178 { "lighting", VERBOSE_LIGHTING },
179 { "disassem", VERBOSE_DISASSEM },
180 { "draw", VERBOSE_DRAW },
181 { "swap", VERBOSE_SWAPBUFFERS }
182 };
183 GLuint i;
184
185 MESA_VERBOSE = 0x0;
186 for (i = 0; i < Elements(debug_opt); i++) {
187 if (strstr(debug, debug_opt[i].name))
188 MESA_VERBOSE |= debug_opt[i].flag;
189 }
190
191 /* Debug flag:
192 */
193 if (strstr(debug, "flush"))
194 MESA_DEBUG_FLAGS |= DEBUG_ALWAYS_FLUSH;
195
196 #if defined(_FPU_GETCW) && defined(_FPU_SETCW)
197 if (strstr(debug, "fpexceptions")) {
198 /* raise FP exceptions */
199 fpu_control_t mask;
200 _FPU_GETCW(mask);
201 mask &= ~(_FPU_MASK_IM | _FPU_MASK_DM | _FPU_MASK_ZM
202 | _FPU_MASK_OM | _FPU_MASK_UM);
203 _FPU_SETCW(mask);
204 }
205 #endif
206
207 #else
208 (void) debug;
209 #endif
210 }
211
212
213 void
214 _mesa_init_debug( struct gl_context *ctx )
215 {
216 char *c;
217
218 /* Dither disable */
219 ctx->NoDither = _mesa_getenv("MESA_NO_DITHER") ? GL_TRUE : GL_FALSE;
220 if (ctx->NoDither) {
221 if (_mesa_getenv("MESA_DEBUG")) {
222 _mesa_debug(ctx, "MESA_NO_DITHER set - dithering disabled\n");
223 }
224 ctx->Color.DitherFlag = GL_FALSE;
225 }
226
227 c = _mesa_getenv("MESA_DEBUG");
228 if (c)
229 add_debug_flags(c);
230
231 c = _mesa_getenv("MESA_VERBOSE");
232 if (c)
233 add_debug_flags(c);
234 }
235
236
237 /*
238 * Write ppm file
239 */
240 static void
241 write_ppm(const char *filename, const GLubyte *buffer, int width, int height,
242 int comps, int rcomp, int gcomp, int bcomp, GLboolean invert)
243 {
244 FILE *f = fopen( filename, "w" );
245 if (f) {
246 int x, y;
247 const GLubyte *ptr = buffer;
248 fprintf(f,"P6\n");
249 fprintf(f,"# ppm-file created by osdemo.c\n");
250 fprintf(f,"%i %i\n", width,height);
251 fprintf(f,"255\n");
252 fclose(f);
253 f = fopen( filename, "ab" ); /* reopen in binary append mode */
254 for (y=0; y < height; y++) {
255 for (x = 0; x < width; x++) {
256 int yy = invert ? (height - 1 - y) : y;
257 int i = (yy * width + x) * comps;
258 fputc(ptr[i+rcomp], f); /* write red */
259 fputc(ptr[i+gcomp], f); /* write green */
260 fputc(ptr[i+bcomp], f); /* write blue */
261 }
262 }
263 fclose(f);
264 }
265 }
266
267
268 /**
269 * Write a texture image to a ppm file.
270 * \param face cube face in [0,5]
271 * \param level mipmap level
272 */
273 static void
274 write_texture_image(struct gl_texture_object *texObj,
275 GLuint face, GLuint level)
276 {
277 struct gl_texture_image *img = texObj->Image[face][level];
278 if (img) {
279 GET_CURRENT_CONTEXT(ctx);
280 struct gl_pixelstore_attrib store;
281 GLubyte *buffer;
282 char s[100];
283
284 buffer = (GLubyte *) malloc(img->Width * img->Height
285 * img->Depth * 4);
286
287 store = ctx->Pack; /* save */
288 ctx->Pack = ctx->DefaultPacking;
289
290 ctx->Driver.GetTexImage(ctx, texObj->Target, level,
291 GL_RGBA, GL_UNSIGNED_BYTE,
292 buffer, texObj, img);
293
294 /* make filename */
295 _mesa_snprintf(s, sizeof(s), "/tmp/tex%u.l%u.f%u.ppm", texObj->Name, level, face);
296
297 printf(" Writing image level %u to %s\n", level, s);
298 write_ppm(s, buffer, img->Width, img->Height, 4, 0, 1, 2, GL_FALSE);
299
300 ctx->Pack = store; /* restore */
301
302 free(buffer);
303 }
304 }
305
306
307 /**
308 * Write renderbuffer image to a ppm file.
309 */
310 static void
311 write_renderbuffer_image(const struct gl_renderbuffer *rb)
312 {
313 GET_CURRENT_CONTEXT(ctx);
314 GLubyte *buffer;
315 char s[100];
316 GLenum format, type;
317
318 if (rb->_BaseFormat == GL_RGB ||
319 rb->_BaseFormat == GL_RGBA) {
320 format = GL_RGBA;
321 type = GL_UNSIGNED_BYTE;
322 }
323 else if (rb->_BaseFormat == GL_DEPTH_STENCIL) {
324 format = GL_DEPTH_STENCIL;
325 type = GL_UNSIGNED_INT_24_8;
326 }
327 else {
328 return;
329 }
330
331 buffer = (GLubyte *) malloc(rb->Width * rb->Height * 4);
332
333 ctx->Driver.ReadPixels(ctx, 0, 0, rb->Width, rb->Height,
334 format, type, &ctx->DefaultPacking, buffer);
335
336 /* make filename */
337 _mesa_snprintf(s, sizeof(s), "/tmp/renderbuffer%u.ppm", rb->Name);
338
339 printf(" Writing renderbuffer image to %s\n", s);
340 write_ppm(s, buffer, rb->Width, rb->Height, 4, 0, 1, 2, GL_TRUE);
341
342 free(buffer);
343 }
344
345
346 /** How many texture images (mipmap levels, faces) to write to files */
347 #define WRITE_NONE 0
348 #define WRITE_ONE 1
349 #define WRITE_ALL 2
350
351 static GLuint WriteImages;
352
353
354 static void
355 dump_texture(struct gl_texture_object *texObj, GLuint writeImages)
356 {
357 const GLuint numFaces = texObj->Target == GL_TEXTURE_CUBE_MAP ? 6 : 1;
358 GLboolean written = GL_FALSE;
359 GLuint i, j;
360
361 printf("Texture %u\n", texObj->Name);
362 printf(" Target %s\n", tex_target_name(texObj->Target));
363 for (i = 0; i < MAX_TEXTURE_LEVELS; i++) {
364 for (j = 0; j < numFaces; j++) {
365 struct gl_texture_image *texImg = texObj->Image[j][i];
366 if (texImg) {
367 printf(" Face %u level %u: %d x %d x %d, format %s at %p\n",
368 j, i,
369 texImg->Width, texImg->Height, texImg->Depth,
370 _mesa_get_format_name(texImg->TexFormat),
371 texImg->Data);
372 if (writeImages == WRITE_ALL ||
373 (writeImages == WRITE_ONE && !written)) {
374 write_texture_image(texObj, j, i);
375 written = GL_TRUE;
376 }
377 }
378 }
379 }
380 }
381
382
383 /**
384 * Dump a single texture.
385 */
386 void
387 _mesa_dump_texture(GLuint texture, GLuint writeImages)
388 {
389 GET_CURRENT_CONTEXT(ctx);
390 struct gl_texture_object *texObj = _mesa_lookup_texture(ctx, texture);
391 if (texObj) {
392 dump_texture(texObj, writeImages);
393 }
394 }
395
396
397 static void
398 dump_texture_cb(GLuint id, void *data, void *userData)
399 {
400 struct gl_texture_object *texObj = (struct gl_texture_object *) data;
401 (void) userData;
402 dump_texture(texObj, WriteImages);
403 }
404
405
406 /**
407 * Print basic info about all texture objext to stdout.
408 * If dumpImages is true, write PPM of level[0] image to a file.
409 */
410 void
411 _mesa_dump_textures(GLuint writeImages)
412 {
413 GET_CURRENT_CONTEXT(ctx);
414 WriteImages = writeImages;
415 _mesa_HashWalk(ctx->Shared->TexObjects, dump_texture_cb, ctx);
416 }
417
418
419 static void
420 dump_renderbuffer(const struct gl_renderbuffer *rb, GLboolean writeImage)
421 {
422 printf("Renderbuffer %u: %u x %u IntFormat = %s\n",
423 rb->Name, rb->Width, rb->Height,
424 _mesa_lookup_enum_by_nr(rb->InternalFormat));
425 if (writeImage) {
426 write_renderbuffer_image(rb);
427 }
428 }
429
430
431 static void
432 dump_renderbuffer_cb(GLuint id, void *data, void *userData)
433 {
434 const struct gl_renderbuffer *rb = (const struct gl_renderbuffer *) data;
435 (void) userData;
436 dump_renderbuffer(rb, WriteImages);
437 }
438
439
440 /**
441 * Print basic info about all renderbuffers to stdout.
442 * If dumpImages is true, write PPM of level[0] image to a file.
443 */
444 void
445 _mesa_dump_renderbuffers(GLboolean writeImages)
446 {
447 GET_CURRENT_CONTEXT(ctx);
448 WriteImages = writeImages;
449 _mesa_HashWalk(ctx->Shared->RenderBuffers, dump_renderbuffer_cb, ctx);
450 }
451
452
453
454 void
455 _mesa_dump_color_buffer(const char *filename)
456 {
457 GET_CURRENT_CONTEXT(ctx);
458 const GLuint w = ctx->DrawBuffer->Width;
459 const GLuint h = ctx->DrawBuffer->Height;
460 GLubyte *buf;
461
462 buf = (GLubyte *) malloc(w * h * 4);
463
464 _mesa_PushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);
465 _mesa_PixelStorei(GL_PACK_ALIGNMENT, 1);
466 _mesa_PixelStorei(GL_PACK_INVERT_MESA, GL_TRUE);
467
468 _mesa_ReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, buf);
469
470 printf("ReadBuffer %p 0x%x DrawBuffer %p 0x%x\n",
471 (void *) ctx->ReadBuffer->_ColorReadBuffer,
472 ctx->ReadBuffer->ColorReadBuffer,
473 (void *) ctx->DrawBuffer->_ColorDrawBuffers[0],
474 ctx->DrawBuffer->ColorDrawBuffer[0]);
475 printf("Writing %d x %d color buffer to %s\n", w, h, filename);
476 write_ppm(filename, buf, w, h, 4, 0, 1, 2, GL_TRUE);
477
478 _mesa_PopClientAttrib();
479
480 free(buf);
481 }
482
483
484 void
485 _mesa_dump_depth_buffer(const char *filename)
486 {
487 GET_CURRENT_CONTEXT(ctx);
488 const GLuint w = ctx->DrawBuffer->Width;
489 const GLuint h = ctx->DrawBuffer->Height;
490 GLuint *buf;
491 GLubyte *buf2;
492 GLuint i;
493
494 buf = (GLuint *) malloc(w * h * 4); /* 4 bpp */
495 buf2 = (GLubyte *) malloc(w * h * 3); /* 3 bpp */
496
497 _mesa_PushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);
498 _mesa_PixelStorei(GL_PACK_ALIGNMENT, 1);
499 _mesa_PixelStorei(GL_PACK_INVERT_MESA, GL_TRUE);
500
501 _mesa_ReadPixels(0, 0, w, h, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, buf);
502
503 /* spread 24 bits of Z across R, G, B */
504 for (i = 0; i < w * h; i++) {
505 buf2[i*3+0] = (buf[i] >> 24) & 0xff;
506 buf2[i*3+1] = (buf[i] >> 16) & 0xff;
507 buf2[i*3+2] = (buf[i] >> 8) & 0xff;
508 }
509
510 printf("Writing %d x %d depth buffer to %s\n", w, h, filename);
511 write_ppm(filename, buf2, w, h, 3, 0, 1, 2, GL_TRUE);
512
513 _mesa_PopClientAttrib();
514
515 free(buf);
516 free(buf2);
517 }
518
519
520 void
521 _mesa_dump_stencil_buffer(const char *filename)
522 {
523 GET_CURRENT_CONTEXT(ctx);
524 const GLuint w = ctx->DrawBuffer->Width;
525 const GLuint h = ctx->DrawBuffer->Height;
526 GLubyte *buf;
527 GLubyte *buf2;
528 GLuint i;
529
530 buf = (GLubyte *) malloc(w * h); /* 1 bpp */
531 buf2 = (GLubyte *) malloc(w * h * 3); /* 3 bpp */
532
533 _mesa_PushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);
534 _mesa_PixelStorei(GL_PACK_ALIGNMENT, 1);
535 _mesa_PixelStorei(GL_PACK_INVERT_MESA, GL_TRUE);
536
537 _mesa_ReadPixels(0, 0, w, h, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, buf);
538
539 for (i = 0; i < w * h; i++) {
540 buf2[i*3+0] = buf[i];
541 buf2[i*3+1] = (buf[i] & 127) * 2;
542 buf2[i*3+2] = (buf[i] - 128) * 2;
543 }
544
545 printf("Writing %d x %d stencil buffer to %s\n", w, h, filename);
546 write_ppm(filename, buf2, w, h, 3, 0, 1, 2, GL_TRUE);
547
548 _mesa_PopClientAttrib();
549
550 free(buf);
551 free(buf2);
552 }
553
554
555 /**
556 * Quick and dirty function to "print" a texture to stdout.
557 */
558 void
559 _mesa_print_texture(struct gl_context *ctx, const struct gl_texture_image *img)
560 {
561 #if CHAN_TYPE != GL_UNSIGNED_BYTE
562 _mesa_problem(NULL, "PrintTexture not supported");
563 #else
564 GLuint i, j, c;
565 const GLubyte *data = (const GLubyte *) img->Data;
566
567 if (!data) {
568 printf("No texture data\n");
569 return;
570 }
571
572 /* XXX add more formats or make into a new format utility function */
573 switch (img->TexFormat) {
574 case MESA_FORMAT_A8:
575 case MESA_FORMAT_L8:
576 case MESA_FORMAT_I8:
577 case MESA_FORMAT_CI8:
578 c = 1;
579 break;
580 case MESA_FORMAT_AL88:
581 case MESA_FORMAT_AL88_REV:
582 c = 2;
583 break;
584 case MESA_FORMAT_RGB888:
585 case MESA_FORMAT_BGR888:
586 c = 3;
587 break;
588 case MESA_FORMAT_RGBA8888:
589 case MESA_FORMAT_ARGB8888:
590 c = 4;
591 break;
592 default:
593 _mesa_problem(NULL, "error in PrintTexture\n");
594 return;
595 }
596
597 for (i = 0; i < img->Height; i++) {
598 for (j = 0; j < img->Width; j++) {
599 if (c==1)
600 printf("%02x ", data[0]);
601 else if (c==2)
602 printf("%02x%02x ", data[0], data[1]);
603 else if (c==3)
604 printf("%02x%02x%02x ", data[0], data[1], data[2]);
605 else if (c==4)
606 printf("%02x%02x%02x%02x ", data[0], data[1], data[2], data[3]);
607 data += (img->RowStride - img->Width) * c;
608 }
609 /* XXX use img->ImageStride here */
610 printf("\n");
611 }
612 #endif
613 }