debug.c revision 02f73c43b4060b58fa0d9b3da4753cbbccde3c84
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 "colormac.h"
28#include "context.h"
29#include "hash.h"
30#include "imports.h"
31#include "debug.h"
32#include "get.h"
33#include "texobj.h"
34#include "texformat.h"
35
36
37/**
38 * Primitive names
39 */
40const char *_mesa_prim_name[GL_POLYGON+4] = {
41   "GL_POINTS",
42   "GL_LINES",
43   "GL_LINE_LOOP",
44   "GL_LINE_STRIP",
45   "GL_TRIANGLES",
46   "GL_TRIANGLE_STRIP",
47   "GL_TRIANGLE_FAN",
48   "GL_QUADS",
49   "GL_QUAD_STRIP",
50   "GL_POLYGON",
51   "outside begin/end",
52   "inside unkown primitive",
53   "unknown state"
54};
55
56void
57_mesa_print_state( const char *msg, GLuint state )
58{
59   _mesa_debug(NULL,
60	   "%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",
61	   msg,
62	   state,
63	   (state & _NEW_MODELVIEW)       ? "ctx->ModelView, " : "",
64	   (state & _NEW_PROJECTION)      ? "ctx->Projection, " : "",
65	   (state & _NEW_TEXTURE_MATRIX)  ? "ctx->TextureMatrix, " : "",
66	   (state & _NEW_COLOR_MATRIX)    ? "ctx->ColorMatrix, " : "",
67	   (state & _NEW_ACCUM)           ? "ctx->Accum, " : "",
68	   (state & _NEW_COLOR)           ? "ctx->Color, " : "",
69	   (state & _NEW_DEPTH)           ? "ctx->Depth, " : "",
70	   (state & _NEW_EVAL)            ? "ctx->Eval/EvalMap, " : "",
71	   (state & _NEW_FOG)             ? "ctx->Fog, " : "",
72	   (state & _NEW_HINT)            ? "ctx->Hint, " : "",
73	   (state & _NEW_LIGHT)           ? "ctx->Light, " : "",
74	   (state & _NEW_LINE)            ? "ctx->Line, " : "",
75	   (state & _NEW_PIXEL)           ? "ctx->Pixel, " : "",
76	   (state & _NEW_POINT)           ? "ctx->Point, " : "",
77	   (state & _NEW_POLYGON)         ? "ctx->Polygon, " : "",
78	   (state & _NEW_POLYGONSTIPPLE)  ? "ctx->PolygonStipple, " : "",
79	   (state & _NEW_SCISSOR)         ? "ctx->Scissor, " : "",
80	   (state & _NEW_TEXTURE)         ? "ctx->Texture, " : "",
81	   (state & _NEW_TRANSFORM)       ? "ctx->Transform, " : "",
82	   (state & _NEW_VIEWPORT)        ? "ctx->Viewport, " : "",
83	   (state & _NEW_PACKUNPACK)      ? "ctx->Pack/Unpack, " : "",
84	   (state & _NEW_ARRAY)           ? "ctx->Array, " : "",
85	   (state & _NEW_RENDERMODE)      ? "ctx->RenderMode, " : "",
86	   (state & _NEW_BUFFERS)         ? "ctx->Visual, ctx->DrawBuffer,, " : "");
87}
88
89
90
91void
92_mesa_print_tri_caps( const char *name, GLuint flags )
93{
94   _mesa_debug(NULL,
95	   "%s: (0x%x) %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
96	   name,
97	   flags,
98	   (flags & DD_FLATSHADE)           ? "flat-shade, " : "",
99	   (flags & DD_SEPARATE_SPECULAR)   ? "separate-specular, " : "",
100	   (flags & DD_TRI_LIGHT_TWOSIDE)   ? "tri-light-twoside, " : "",
101	   (flags & DD_TRI_TWOSTENCIL)      ? "tri-twostencil, " : "",
102	   (flags & DD_TRI_UNFILLED)        ? "tri-unfilled, " : "",
103	   (flags & DD_TRI_STIPPLE)         ? "tri-stipple, " : "",
104	   (flags & DD_TRI_OFFSET)          ? "tri-offset, " : "",
105	   (flags & DD_TRI_SMOOTH)          ? "tri-smooth, " : "",
106	   (flags & DD_LINE_SMOOTH)         ? "line-smooth, " : "",
107	   (flags & DD_LINE_STIPPLE)        ? "line-stipple, " : "",
108	   (flags & DD_LINE_WIDTH)          ? "line-wide, " : "",
109	   (flags & DD_POINT_SMOOTH)        ? "point-smooth, " : "",
110	   (flags & DD_POINT_SIZE)          ? "point-size, " : "",
111	   (flags & DD_POINT_ATTEN)         ? "point-atten, " : "",
112	   (flags & DD_TRI_CULL_FRONT_BACK) ? "cull-all, " : ""
113      );
114}
115
116
117/**
118 * Print information about this Mesa version and build options.
119 */
120void _mesa_print_info( void )
121{
122   _mesa_debug(NULL, "Mesa GL_VERSION = %s\n",
123	   (char *) _mesa_GetString(GL_VERSION));
124   _mesa_debug(NULL, "Mesa GL_RENDERER = %s\n",
125	   (char *) _mesa_GetString(GL_RENDERER));
126   _mesa_debug(NULL, "Mesa GL_VENDOR = %s\n",
127	   (char *) _mesa_GetString(GL_VENDOR));
128   _mesa_debug(NULL, "Mesa GL_EXTENSIONS = %s\n",
129	   (char *) _mesa_GetString(GL_EXTENSIONS));
130#if defined(THREADS)
131   _mesa_debug(NULL, "Mesa thread-safe: YES\n");
132#else
133   _mesa_debug(NULL, "Mesa thread-safe: NO\n");
134#endif
135#if defined(USE_X86_ASM)
136   _mesa_debug(NULL, "Mesa x86-optimized: YES\n");
137#else
138   _mesa_debug(NULL, "Mesa x86-optimized: NO\n");
139#endif
140#if defined(USE_SPARC_ASM)
141   _mesa_debug(NULL, "Mesa sparc-optimized: YES\n");
142#else
143   _mesa_debug(NULL, "Mesa sparc-optimized: NO\n");
144#endif
145}
146
147
148/**
149 * Set the debugging flags.
150 *
151 * \param debug debug string
152 *
153 * If compiled with debugging support then search for keywords in \p debug and
154 * enables the verbose debug output of the respective feature.
155 */
156static void add_debug_flags( const char *debug )
157{
158#ifdef DEBUG
159   struct debug_option {
160      const char *name;
161      GLbitfield flag;
162   };
163   static const struct debug_option debug_opt[] = {
164      { "varray",    VERBOSE_VARRAY },
165      { "tex",       VERBOSE_TEXTURE },
166      { "imm",       VERBOSE_IMMEDIATE },
167      { "pipe",      VERBOSE_PIPELINE },
168      { "driver",    VERBOSE_DRIVER },
169      { "state",     VERBOSE_STATE },
170      { "api",       VERBOSE_API },
171      { "list",      VERBOSE_DISPLAY_LIST },
172      { "lighting",  VERBOSE_LIGHTING },
173      { "disassem",  VERBOSE_DISASSEM }
174   };
175   GLuint i;
176
177   MESA_VERBOSE = 0x0;
178   for (i = 0; i < Elements(debug_opt); i++) {
179      if (_mesa_strstr(debug, debug_opt[i].name))
180         MESA_VERBOSE |= debug_opt[i].flag;
181   }
182
183   /* Debug flag:
184    */
185   if (_mesa_strstr(debug, "flush"))
186      MESA_DEBUG_FLAGS |= DEBUG_ALWAYS_FLUSH;
187
188#if defined(_FPU_GETCW) && defined(_FPU_SETCW)
189   if (_mesa_strstr(debug, "fpexceptions")) {
190      /* raise FP exceptions */
191      fpu_control_t mask;
192      _FPU_GETCW(mask);
193      mask &= ~(_FPU_MASK_IM | _FPU_MASK_DM | _FPU_MASK_ZM
194                | _FPU_MASK_OM | _FPU_MASK_UM);
195      _FPU_SETCW(mask);
196   }
197#endif
198
199#else
200   (void) debug;
201#endif
202}
203
204
205void
206_mesa_init_debug( GLcontext *ctx )
207{
208   char *c;
209
210   /* Dither disable */
211   ctx->NoDither = _mesa_getenv("MESA_NO_DITHER") ? GL_TRUE : GL_FALSE;
212   if (ctx->NoDither) {
213      if (_mesa_getenv("MESA_DEBUG")) {
214         _mesa_debug(ctx, "MESA_NO_DITHER set - dithering disabled\n");
215      }
216      ctx->Color.DitherFlag = GL_FALSE;
217   }
218
219   c = _mesa_getenv("MESA_DEBUG");
220   if (c)
221      add_debug_flags(c);
222
223   c = _mesa_getenv("MESA_VERBOSE");
224   if (c)
225      add_debug_flags(c);
226}
227
228
229/*
230 * Write ppm file
231 */
232static void
233write_ppm(const char *filename, const GLubyte *buffer, int width, int height,
234          int comps, int rcomp, int gcomp, int bcomp)
235{
236   FILE *f = fopen( filename, "w" );
237   if (f) {
238      int i, x, y;
239      const GLubyte *ptr = buffer;
240      fprintf(f,"P6\n");
241      fprintf(f,"# ppm-file created by osdemo.c\n");
242      fprintf(f,"%i %i\n", width,height);
243      fprintf(f,"255\n");
244      fclose(f);
245      f = fopen( filename, "ab" );  /* reopen in binary append mode */
246      for (y=height-1; y>=0; y--) {
247         for (x=0; x<width; x++) {
248            i = (y*width + x) * comps;
249            fputc(ptr[i+rcomp], f);   /* write red */
250            fputc(ptr[i+gcomp], f); /* write green */
251            fputc(ptr[i+bcomp], f); /* write blue */
252         }
253      }
254      fclose(f);
255   }
256}
257
258
259/**
260 * Write level[0] image to a ppm file.
261 */
262static void
263write_texture_image(struct gl_texture_object *texObj)
264{
265   const struct gl_texture_image *img = texObj->Image[0][0];
266   if (img && img->Data) {
267      char s[100];
268
269      /* make filename */
270      sprintf(s, "/tmp/teximage%u.ppm", texObj->Name);
271
272      switch (img->TexFormat->MesaFormat) {
273      case MESA_FORMAT_RGBA8888:
274         write_ppm(s, img->Data, img->Width, img->Height, 4, 3, 2, 1);
275         break;
276      case MESA_FORMAT_ARGB8888:
277         write_ppm(s, img->Data, img->Width, img->Height, 4, 2, 1, 0);
278         break;
279      case MESA_FORMAT_RGB888:
280         write_ppm(s, img->Data, img->Width, img->Height, 3, 2, 1, 0);
281         break;
282      case MESA_FORMAT_RGB565:
283         {
284            GLubyte *buf2 = (GLubyte *) _mesa_malloc(img->Width * img->Height * 3);
285            GLint i;
286            for (i = 0; i < img->Width * img->Height; i++) {
287               GLint r, g, b;
288               GLushort s = ((GLushort *) img->Data)[i];
289               r = UBYTE_TO_CHAN( ((s >> 8) & 0xf8) | ((s >> 13) & 0x7) );
290               g = UBYTE_TO_CHAN( ((s >> 3) & 0xfc) | ((s >>  9) & 0x3) );
291               b = UBYTE_TO_CHAN( ((s << 3) & 0xf8) | ((s >>  2) & 0x7) );
292               buf2[i*3+1] = r;
293               buf2[i*3+2] = g;
294               buf2[i*3+3] = b;
295            }
296            write_ppm(s, buf2, img->Width, img->Height, 3, 2, 1, 0);
297            _mesa_free(buf2);
298         }
299         break;
300      default:
301         printf("XXXX unsupported mesa tex format %d in %s\n",
302                img->TexFormat->MesaFormat, __FUNCTION__);
303      }
304   }
305}
306
307
308static GLboolean DumpImages;
309
310
311static void
312dump_texture_cb(GLuint id, void *data, void *userData)
313{
314   struct gl_texture_object *texObj = (struct gl_texture_object *) data;
315   int i;
316   (void) userData;
317
318   printf("Texture %u\n", texObj->Name);
319   printf("  Target 0x%x\n", texObj->Target);
320   for (i = 0; i < MAX_TEXTURE_LEVELS; i++) {
321      struct gl_texture_image *texImg = texObj->Image[0][i];
322      if (texImg) {
323         printf("  Image %u: %d x %d x %d at %p\n", i,
324                texImg->Width, texImg->Height, texImg->Depth, texImg->Data);
325         if (DumpImages && i == 0) {
326            write_texture_image(texObj);
327         }
328      }
329   }
330}
331
332
333/**
334 * Print basic info about all texture objext to stdout.
335 * If dumpImages is true, write PPM of level[0] image to a file.
336 */
337void
338_mesa_dump_textures(GLboolean dumpImages)
339{
340   GET_CURRENT_CONTEXT(ctx);
341   DumpImages = dumpImages;
342   _mesa_HashWalk(ctx->Shared->TexObjects, dump_texture_cb, ctx);
343}
344
345
346void
347_mesa_dump_color_buffer(const char *filename)
348{
349   GET_CURRENT_CONTEXT(ctx);
350   const GLuint w = ctx->DrawBuffer->Width;
351   const GLuint h = ctx->DrawBuffer->Height;
352   GLubyte *buf;
353
354   buf = (GLubyte *) _mesa_malloc(w * h * 4);
355
356   glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);
357   glPixelStorei(GL_PACK_ALIGNMENT, 1);
358   glPixelStorei(GL_PACK_INVERT_MESA, GL_TRUE);
359
360   glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, buf);
361
362   _mesa_printf("ReadBuffer %p 0x%x  DrawBuffer %p 0x%x\n",
363                ctx->ReadBuffer->_ColorReadBuffer,
364                ctx->ReadBuffer->ColorReadBuffer,
365                ctx->DrawBuffer->_ColorDrawBuffers[0],
366                ctx->DrawBuffer->ColorDrawBuffer[0]);
367   _mesa_printf("Writing %d x %d color buffer to %s\n", w, h, filename);
368   write_ppm(filename, buf, w, h, 4, 0, 1, 2);
369
370   glPopClientAttrib();
371
372   _mesa_free(buf);
373}
374
375
376void
377_mesa_dump_depth_buffer(const char *filename)
378{
379   GET_CURRENT_CONTEXT(ctx);
380   const GLuint w = ctx->DrawBuffer->Width;
381   const GLuint h = ctx->DrawBuffer->Height;
382   GLuint *buf;
383   GLubyte *buf2;
384   GLuint i;
385
386   buf = (GLuint *) _mesa_malloc(w * h * 4);  /* 4 bpp */
387   buf2 = (GLubyte *) _mesa_malloc(w * h * 3); /* 3 bpp */
388
389   glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);
390   glPixelStorei(GL_PACK_ALIGNMENT, 1);
391   glPixelStorei(GL_PACK_INVERT_MESA, GL_TRUE);
392
393   glReadPixels(0, 0, w, h, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, buf);
394
395   /* spread 24 bits of Z across R, G, B */
396   for (i = 0; i < w * h; i++) {
397      buf2[i*3+0] = (buf[i] >> 24) & 0xff;
398      buf2[i*3+1] = (buf[i] >> 16) & 0xff;
399      buf2[i*3+2] = (buf[i] >>  8) & 0xff;
400   }
401
402   _mesa_printf("Writing %d x %d depth buffer to %s\n", w, h, filename);
403   write_ppm(filename, buf2, w, h, 3, 0, 1, 2);
404
405   glPopClientAttrib();
406
407   _mesa_free(buf);
408   _mesa_free(buf2);
409}
410
411
412void
413_mesa_dump_stencil_buffer(const char *filename)
414{
415   GET_CURRENT_CONTEXT(ctx);
416   const GLuint w = ctx->DrawBuffer->Width;
417   const GLuint h = ctx->DrawBuffer->Height;
418   GLubyte *buf;
419   GLubyte *buf2;
420   GLuint i;
421
422   buf = (GLubyte *) _mesa_malloc(w * h);  /* 1 bpp */
423   buf2 = (GLubyte *) _mesa_malloc(w * h * 3); /* 3 bpp */
424
425   glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);
426   glPixelStorei(GL_PACK_ALIGNMENT, 1);
427   glPixelStorei(GL_PACK_INVERT_MESA, GL_TRUE);
428
429   glReadPixels(0, 0, w, h, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, buf);
430
431   for (i = 0; i < w * h; i++) {
432      buf2[i*3+0] = buf[i];
433      buf2[i*3+1] = (buf[i] & 127) * 2;
434      buf2[i*3+2] = (buf[i] - 128) * 2;
435   }
436
437   _mesa_printf("Writing %d x %d stencil buffer to %s\n", w, h, filename);
438   write_ppm(filename, buf2, w, h, 3, 0, 1, 2);
439
440   glPopClientAttrib();
441
442   _mesa_free(buf);
443   _mesa_free(buf2);
444}
445