Merge branch 'gallium-msaa'
[mesa.git] / src / gallium / auxiliary / util / u_debug.c
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * Copyright (c) 2008 VMware, Inc.
5 * 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
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 **************************************************************************/
28
29
30 #include "pipe/p_config.h"
31
32 #include "pipe/p_compiler.h"
33 #include "os/os_stream.h"
34 #include "util/u_debug.h"
35 #include "pipe/p_format.h"
36 #include "pipe/p_state.h"
37 #include "util/u_inlines.h"
38 #include "util/u_format.h"
39 #include "util/u_memory.h"
40 #include "util/u_string.h"
41 #include "util/u_math.h"
42 #include "util/u_tile.h"
43 #include "util/u_prim.h"
44
45
46 void _debug_vprintf(const char *format, va_list ap)
47 {
48 /* We buffer until we find a newline. */
49 static char buf[4096] = {'\0'};
50 size_t len = strlen(buf);
51 int ret = util_vsnprintf(buf + len, sizeof(buf) - len, format, ap);
52 if(ret > (int)(sizeof(buf) - len - 1) || util_strchr(buf + len, '\n')) {
53 os_log_message(buf);
54 buf[0] = '\0';
55 }
56 }
57
58
59 #ifdef DEBUG
60 void debug_print_blob( const char *name,
61 const void *blob,
62 unsigned size )
63 {
64 const unsigned *ublob = (const unsigned *)blob;
65 unsigned i;
66
67 debug_printf("%s (%d dwords%s)\n", name, size/4,
68 size%4 ? "... plus a few bytes" : "");
69
70 for (i = 0; i < size/4; i++) {
71 debug_printf("%d:\t%08x\n", i, ublob[i]);
72 }
73 }
74 #endif
75
76
77 static boolean
78 debug_get_option_should_print(void)
79 {
80 static boolean first = TRUE;
81 static boolean value = FALSE;
82
83 if (!first)
84 return value;
85
86 /* Oh hey this will call into this function,
87 * but its cool since we set first to false
88 */
89 first = FALSE;
90 value = debug_get_bool_option("GALLIUM_PRINT_OPTIONS", TRUE);
91 /* XXX should we print this option? Currently it wont */
92 return value;
93 }
94
95 const char *
96 debug_get_option(const char *name, const char *dfault)
97 {
98 const char *result;
99
100 result = os_get_option(name);
101 if(!result)
102 result = dfault;
103
104 if (debug_get_option_should_print())
105 debug_printf("%s: %s = %s\n", __FUNCTION__, name, result ? result : "(null)");
106
107 return result;
108 }
109
110 boolean
111 debug_get_bool_option(const char *name, boolean dfault)
112 {
113 const char *str = os_get_option(name);
114 boolean result;
115
116 if(str == NULL)
117 result = dfault;
118 else if(!util_strcmp(str, "n"))
119 result = FALSE;
120 else if(!util_strcmp(str, "no"))
121 result = FALSE;
122 else if(!util_strcmp(str, "0"))
123 result = FALSE;
124 else if(!util_strcmp(str, "f"))
125 result = FALSE;
126 else if(!util_strcmp(str, "F"))
127 result = FALSE;
128 else if(!util_strcmp(str, "false"))
129 result = FALSE;
130 else if(!util_strcmp(str, "FALSE"))
131 result = FALSE;
132 else
133 result = TRUE;
134
135 if (debug_get_option_should_print())
136 debug_printf("%s: %s = %s\n", __FUNCTION__, name, result ? "TRUE" : "FALSE");
137
138 return result;
139 }
140
141
142 long
143 debug_get_num_option(const char *name, long dfault)
144 {
145 long result;
146 const char *str;
147
148 str = os_get_option(name);
149 if(!str)
150 result = dfault;
151 else {
152 long sign;
153 char c;
154 c = *str++;
155 if(c == '-') {
156 sign = -1;
157 c = *str++;
158 }
159 else {
160 sign = 1;
161 }
162 result = 0;
163 while('0' <= c && c <= '9') {
164 result = result*10 + (c - '0');
165 c = *str++;
166 }
167 result *= sign;
168 }
169
170 if (debug_get_option_should_print())
171 debug_printf("%s: %s = %li\n", __FUNCTION__, name, result);
172
173 return result;
174 }
175
176
177 unsigned long
178 debug_get_flags_option(const char *name,
179 const struct debug_named_value *flags,
180 unsigned long dfault)
181 {
182 unsigned long result;
183 const char *str;
184
185 str = os_get_option(name);
186 if(!str)
187 result = dfault;
188 else if (!util_strcmp(str, "help")) {
189 result = dfault;
190 while (flags->name) {
191 debug_printf("%s: help for %s: %s [0x%lx]\n", __FUNCTION__, name, flags->name, flags->value);
192 flags++;
193 }
194 }
195 else {
196 result = 0;
197 while( flags->name ) {
198 if (!util_strcmp(str, "all") || util_strstr(str, flags->name ))
199 result |= flags->value;
200 ++flags;
201 }
202 }
203
204 if (debug_get_option_should_print()) {
205 if (str) {
206 debug_printf("%s: %s = 0x%lx (%s)\n", __FUNCTION__, name, result, str);
207 } else {
208 debug_printf("%s: %s = 0x%lx\n", __FUNCTION__, name, result);
209 }
210 }
211
212 return result;
213 }
214
215
216 void _debug_assert_fail(const char *expr,
217 const char *file,
218 unsigned line,
219 const char *function)
220 {
221 _debug_printf("%s:%u:%s: Assertion `%s' failed.\n", file, line, function, expr);
222 #if defined(PIPE_OS_WINDOWS) && !defined(PIPE_SUBSYSTEM_WINDOWS_USER)
223 if (debug_get_bool_option("GALLIUM_ABORT_ON_ASSERT", FALSE))
224 #else
225 if (debug_get_bool_option("GALLIUM_ABORT_ON_ASSERT", TRUE))
226 #endif
227 os_abort();
228 else
229 _debug_printf("continuing...\n");
230 }
231
232
233 const char *
234 debug_dump_enum(const struct debug_named_value *names,
235 unsigned long value)
236 {
237 static char rest[64];
238
239 while(names->name) {
240 if(names->value == value)
241 return names->name;
242 ++names;
243 }
244
245 util_snprintf(rest, sizeof(rest), "0x%08lx", value);
246 return rest;
247 }
248
249
250 const char *
251 debug_dump_enum_noprefix(const struct debug_named_value *names,
252 const char *prefix,
253 unsigned long value)
254 {
255 static char rest[64];
256
257 while(names->name) {
258 if(names->value == value) {
259 const char *name = names->name;
260 while (*name == *prefix) {
261 name++;
262 prefix++;
263 }
264 return name;
265 }
266 ++names;
267 }
268
269
270
271 util_snprintf(rest, sizeof(rest), "0x%08lx", value);
272 return rest;
273 }
274
275
276 const char *
277 debug_dump_flags(const struct debug_named_value *names,
278 unsigned long value)
279 {
280 static char output[4096];
281 static char rest[256];
282 int first = 1;
283
284 output[0] = '\0';
285
286 while(names->name) {
287 if((names->value & value) == names->value) {
288 if (!first)
289 util_strncat(output, "|", sizeof(output));
290 else
291 first = 0;
292 util_strncat(output, names->name, sizeof(output) - 1);
293 output[sizeof(output) - 1] = '\0';
294 value &= ~names->value;
295 }
296 ++names;
297 }
298
299 if (value) {
300 if (!first)
301 util_strncat(output, "|", sizeof(output));
302 else
303 first = 0;
304
305 util_snprintf(rest, sizeof(rest), "0x%08lx", value);
306 util_strncat(output, rest, sizeof(output) - 1);
307 output[sizeof(output) - 1] = '\0';
308 }
309
310 if(first)
311 return "0";
312
313 return output;
314 }
315
316
317 #ifdef DEBUG
318 void debug_print_format(const char *msg, unsigned fmt )
319 {
320 debug_printf("%s: %s\n", msg, util_format_name(fmt));
321 }
322 #endif
323
324
325
326 static const struct debug_named_value pipe_prim_names[] = {
327 #ifdef DEBUG
328 DEBUG_NAMED_VALUE(PIPE_PRIM_POINTS),
329 DEBUG_NAMED_VALUE(PIPE_PRIM_LINES),
330 DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_LOOP),
331 DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_STRIP),
332 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLES),
333 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_STRIP),
334 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_FAN),
335 DEBUG_NAMED_VALUE(PIPE_PRIM_QUADS),
336 DEBUG_NAMED_VALUE(PIPE_PRIM_QUAD_STRIP),
337 DEBUG_NAMED_VALUE(PIPE_PRIM_POLYGON),
338 #endif
339 DEBUG_NAMED_VALUE_END
340 };
341
342
343 const char *u_prim_name( unsigned prim )
344 {
345 return debug_dump_enum(pipe_prim_names, prim);
346 }
347
348
349
350
351 #ifdef DEBUG
352 /**
353 * Dump an image to a .raw or .ppm file (depends on OS).
354 * \param format PIPE_FORMAT_x
355 * \param cpp bytes per pixel
356 * \param width width in pixels
357 * \param height height in pixels
358 * \param stride row stride in bytes
359 */
360 void debug_dump_image(const char *prefix,
361 unsigned format, unsigned cpp,
362 unsigned width, unsigned height,
363 unsigned stride,
364 const void *data)
365 {
366 #ifdef PIPE_SUBSYSTEM_WINDOWS_DISPLAY
367 static unsigned no = 0;
368 char filename[256];
369 WCHAR wfilename[sizeof(filename)];
370 ULONG_PTR iFile = 0;
371 struct {
372 unsigned format;
373 unsigned cpp;
374 unsigned width;
375 unsigned height;
376 } header;
377 unsigned char *pMap = NULL;
378 unsigned i;
379
380 util_snprintf(filename, sizeof(filename), "\\??\\c:\\%03u%s.raw", ++no, prefix);
381 for(i = 0; i < sizeof(filename); ++i)
382 wfilename[i] = (WCHAR)filename[i];
383
384 pMap = (unsigned char *)EngMapFile(wfilename, sizeof(header) + height*width*cpp, &iFile);
385 if(!pMap)
386 return;
387
388 header.format = format;
389 header.cpp = cpp;
390 header.width = width;
391 header.height = height;
392 memcpy(pMap, &header, sizeof(header));
393 pMap += sizeof(header);
394
395 for(i = 0; i < height; ++i) {
396 memcpy(pMap, (unsigned char *)data + stride*i, cpp*width);
397 pMap += cpp*width;
398 }
399
400 EngUnmapFile(iFile);
401 #elif defined(PIPE_OS_UNIX)
402 /* write a ppm file */
403 char filename[256];
404 FILE *f;
405
406 util_snprintf(filename, sizeof(filename), "%s.ppm", prefix);
407
408 f = fopen(filename, "w");
409 if (f) {
410 int i, x, y;
411 int r, g, b;
412 const uint8_t *ptr = (uint8_t *) data;
413
414 /* XXX this is a hack */
415 switch (format) {
416 case PIPE_FORMAT_B8G8R8A8_UNORM:
417 r = 2;
418 g = 1;
419 b = 0;
420 break;
421 default:
422 r = 0;
423 g = 1;
424 b = 1;
425 }
426
427 fprintf(f, "P6\n");
428 fprintf(f, "# ppm-file created by osdemo.c\n");
429 fprintf(f, "%i %i\n", width, height);
430 fprintf(f, "255\n");
431 fclose(f);
432
433 f = fopen(filename, "ab"); /* reopen in binary append mode */
434 for (y = 0; y < height; y++) {
435 for (x = 0; x < width; x++) {
436 i = y * stride + x * cpp;
437 fputc(ptr[i + r], f); /* write red */
438 fputc(ptr[i + g], f); /* write green */
439 fputc(ptr[i + b], f); /* write blue */
440 }
441 }
442 fclose(f);
443 }
444 else {
445 fprintf(stderr, "Can't open %s for writing\n", filename);
446 }
447 #endif
448 }
449
450 void debug_dump_surface(struct pipe_context *pipe,
451 const char *prefix,
452 struct pipe_surface *surface)
453 {
454 struct pipe_resource *texture;
455 struct pipe_transfer *transfer;
456 void *data;
457
458 if (!surface)
459 return;
460
461 /* XXX: this doesn't necessarily work, as the driver may be using
462 * temporary storage for the surface which hasn't been propagated
463 * back into the texture. Need to nail down the semantics of views
464 * and transfers a bit better before we can say if extra work needs
465 * to be done here:
466 */
467 texture = surface->texture;
468
469 transfer = pipe_get_transfer(pipe, texture, surface->face,
470 surface->level, surface->zslice,
471 PIPE_TRANSFER_READ, 0, 0, surface->width,
472 surface->height);
473
474 data = pipe->transfer_map(pipe, transfer);
475 if(!data)
476 goto error;
477
478 debug_dump_image(prefix,
479 texture->format,
480 util_format_get_blocksize(texture->format),
481 util_format_get_nblocksx(texture->format, surface->width),
482 util_format_get_nblocksy(texture->format, surface->height),
483 transfer->stride,
484 data);
485
486 pipe->transfer_unmap(pipe, transfer);
487 error:
488 pipe->transfer_destroy(pipe, transfer);
489 }
490
491
492 void debug_dump_texture(struct pipe_context *pipe,
493 const char *prefix,
494 struct pipe_resource *texture)
495 {
496 struct pipe_surface *surface;
497 struct pipe_screen *screen;
498
499 if (!texture)
500 return;
501
502 screen = texture->screen;
503
504 /* XXX for now, just dump image for face=0, level=0 */
505 surface = screen->get_tex_surface(screen, texture, 0, 0, 0,
506 PIPE_BIND_SAMPLER_VIEW);
507 if (surface) {
508 debug_dump_surface(pipe, prefix, surface);
509 screen->tex_surface_destroy(surface);
510 }
511 }
512
513
514 #pragma pack(push,2)
515 struct bmp_file_header {
516 uint16_t bfType;
517 uint32_t bfSize;
518 uint16_t bfReserved1;
519 uint16_t bfReserved2;
520 uint32_t bfOffBits;
521 };
522 #pragma pack(pop)
523
524 struct bmp_info_header {
525 uint32_t biSize;
526 int32_t biWidth;
527 int32_t biHeight;
528 uint16_t biPlanes;
529 uint16_t biBitCount;
530 uint32_t biCompression;
531 uint32_t biSizeImage;
532 int32_t biXPelsPerMeter;
533 int32_t biYPelsPerMeter;
534 uint32_t biClrUsed;
535 uint32_t biClrImportant;
536 };
537
538 struct bmp_rgb_quad {
539 uint8_t rgbBlue;
540 uint8_t rgbGreen;
541 uint8_t rgbRed;
542 uint8_t rgbAlpha;
543 };
544
545 void
546 debug_dump_surface_bmp(struct pipe_context *pipe,
547 const char *filename,
548 struct pipe_surface *surface)
549 {
550 #ifndef PIPE_SUBSYSTEM_WINDOWS_MINIPORT
551 struct pipe_transfer *transfer;
552 struct pipe_resource *texture = surface->texture;
553
554 transfer = pipe_get_transfer(pipe, texture, surface->face,
555 surface->level, surface->zslice,
556 PIPE_TRANSFER_READ, 0, 0, surface->width,
557 surface->height);
558
559 debug_dump_transfer_bmp(pipe, filename, transfer);
560
561 pipe->transfer_destroy(pipe, transfer);
562 #endif
563 }
564
565 void
566 debug_dump_transfer_bmp(struct pipe_context *pipe,
567 const char *filename,
568 struct pipe_transfer *transfer)
569 {
570 #ifndef PIPE_SUBSYSTEM_WINDOWS_MINIPORT
571 float *rgba;
572
573 if (!transfer)
574 goto error1;
575
576 rgba = MALLOC(transfer->box.width *
577 transfer->box.height *
578 transfer->box.depth *
579 4*sizeof(float));
580 if(!rgba)
581 goto error1;
582
583 pipe_get_tile_rgba(pipe, transfer, 0, 0,
584 transfer->box.width, transfer->box.height,
585 rgba);
586
587 debug_dump_float_rgba_bmp(filename,
588 transfer->box.width, transfer->box.height,
589 rgba, transfer->box.width);
590
591 FREE(rgba);
592 error1:
593 ;
594 #endif
595 }
596
597 void
598 debug_dump_float_rgba_bmp(const char *filename,
599 unsigned width, unsigned height,
600 float *rgba, unsigned stride)
601 {
602 #ifndef PIPE_SUBSYSTEM_WINDOWS_MINIPORT
603 struct os_stream *stream;
604 struct bmp_file_header bmfh;
605 struct bmp_info_header bmih;
606 unsigned x, y;
607
608 if(!rgba)
609 goto error1;
610
611 bmfh.bfType = 0x4d42;
612 bmfh.bfSize = 14 + 40 + height*width*4;
613 bmfh.bfReserved1 = 0;
614 bmfh.bfReserved2 = 0;
615 bmfh.bfOffBits = 14 + 40;
616
617 bmih.biSize = 40;
618 bmih.biWidth = width;
619 bmih.biHeight = height;
620 bmih.biPlanes = 1;
621 bmih.biBitCount = 32;
622 bmih.biCompression = 0;
623 bmih.biSizeImage = height*width*4;
624 bmih.biXPelsPerMeter = 0;
625 bmih.biYPelsPerMeter = 0;
626 bmih.biClrUsed = 0;
627 bmih.biClrImportant = 0;
628
629 stream = os_file_stream_create(filename);
630 if(!stream)
631 goto error1;
632
633 os_stream_write(stream, &bmfh, 14);
634 os_stream_write(stream, &bmih, 40);
635
636 y = height;
637 while(y--) {
638 float *ptr = rgba + (stride * y * 4);
639 for(x = 0; x < width; ++x)
640 {
641 struct bmp_rgb_quad pixel;
642 pixel.rgbRed = float_to_ubyte(ptr[x*4 + 0]);
643 pixel.rgbGreen = float_to_ubyte(ptr[x*4 + 1]);
644 pixel.rgbBlue = float_to_ubyte(ptr[x*4 + 2]);
645 pixel.rgbAlpha = 255;
646 os_stream_write(stream, &pixel, 4);
647 }
648 }
649
650 os_stream_close(stream);
651 error1:
652 ;
653 #endif
654 }
655
656 #endif