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