1 /**************************************************************************
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * Copyright (c) 2008 VMware, Inc.
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:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
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.
27 **************************************************************************/
30 #include "pipe/p_config.h"
32 #include "pipe/p_compiler.h"
33 #include "util/u_debug.h"
34 #include "pipe/p_format.h"
35 #include "pipe/p_state.h"
36 #include "util/u_inlines.h"
37 #include "util/u_format.h"
38 #include "util/u_memory.h"
39 #include "util/u_string.h"
40 #include "util/u_math.h"
41 #include "util/u_tile.h"
42 #include "util/u_prim.h"
43 #include "util/u_surface.h"
46 #include <limits.h> /* CHAR_BIT */
47 #include <ctype.h> /* isalnum */
49 void _debug_vprintf(const char *format
, va_list ap
)
51 static char buf
[4096] = {'\0'};
52 #if defined(PIPE_OS_WINDOWS) || defined(PIPE_SUBSYSTEM_EMBEDDED)
53 /* We buffer until we find a newline. */
54 size_t len
= strlen(buf
);
55 int ret
= util_vsnprintf(buf
+ len
, sizeof(buf
) - len
, format
, ap
);
56 if(ret
> (int)(sizeof(buf
) - len
- 1) || util_strchr(buf
+ len
, '\n')) {
61 util_vsnprintf(buf
, sizeof(buf
), format
, ap
);
68 void debug_print_blob( const char *name
,
72 const unsigned *ublob
= (const unsigned *)blob
;
75 debug_printf("%s (%d dwords%s)\n", name
, size
/4,
76 size
%4 ? "... plus a few bytes" : "");
78 for (i
= 0; i
< size
/4; i
++) {
79 debug_printf("%d:\t%08x\n", i
, ublob
[i
]);
86 debug_get_option_should_print(void)
88 static boolean first
= TRUE
;
89 static boolean value
= FALSE
;
94 /* Oh hey this will call into this function,
95 * but its cool since we set first to false
98 value
= debug_get_bool_option("GALLIUM_PRINT_OPTIONS", FALSE
);
99 /* XXX should we print this option? Currently it wont */
104 debug_get_option(const char *name
, const char *dfault
)
108 result
= os_get_option(name
);
112 if (debug_get_option_should_print())
113 debug_printf("%s: %s = %s\n", __FUNCTION__
, name
, result
? result
: "(null)");
119 debug_get_bool_option(const char *name
, boolean dfault
)
121 const char *str
= os_get_option(name
);
126 else if(!util_strcmp(str
, "n"))
128 else if(!util_strcmp(str
, "no"))
130 else if(!util_strcmp(str
, "0"))
132 else if(!util_strcmp(str
, "f"))
134 else if(!util_strcmp(str
, "F"))
136 else if(!util_strcmp(str
, "false"))
138 else if(!util_strcmp(str
, "FALSE"))
143 if (debug_get_option_should_print())
144 debug_printf("%s: %s = %s\n", __FUNCTION__
, name
, result
? "TRUE" : "FALSE");
151 debug_get_num_option(const char *name
, long dfault
)
156 str
= os_get_option(name
);
171 while('0' <= c
&& c
<= '9') {
172 result
= result
*10 + (c
- '0');
178 if (debug_get_option_should_print())
179 debug_printf("%s: %s = %li\n", __FUNCTION__
, name
, result
);
184 static boolean
str_has_option(const char *str
, const char *name
)
192 if (!util_strcmp(str
, "all")) {
196 /* Find 'name' in 'str' surrounded by non-alphanumeric characters. */
198 const char *start
= str
;
199 unsigned name_len
= strlen(name
);
201 /* 'start' is the beginning of the currently-parsed word,
202 * we increment 'str' each iteration.
203 * if we find either the end of string or a non-alphanumeric character,
204 * we compare 'start' up to 'str-1' with 'name'. */
207 if (!*str
|| !(isalnum(*str
) || *str
== '_')) {
208 if (str
-start
== name_len
&&
209 !memcmp(start
, name
, name_len
)) {
228 debug_get_flags_option(const char *name
,
229 const struct debug_named_value
*flags
,
230 unsigned long dfault
)
232 unsigned long result
;
234 const struct debug_named_value
*orig
= flags
;
235 unsigned namealign
= 0;
237 str
= os_get_option(name
);
240 else if (!util_strcmp(str
, "help")) {
242 _debug_printf("%s: help for %s:\n", __FUNCTION__
, name
);
243 for (; flags
->name
; ++flags
)
244 namealign
= MAX2(namealign
, strlen(flags
->name
));
245 for (flags
= orig
; flags
->name
; ++flags
)
246 _debug_printf("| %*s [0x%0*lx]%s%s\n", namealign
, flags
->name
,
247 (int)sizeof(unsigned long)*CHAR_BIT
/4, flags
->value
,
248 flags
->desc
? " " : "", flags
->desc
? flags
->desc
: "");
252 while( flags
->name
) {
253 if (str_has_option(str
, flags
->name
))
254 result
|= flags
->value
;
259 if (debug_get_option_should_print()) {
261 debug_printf("%s: %s = 0x%lx (%s)\n", __FUNCTION__
, name
, result
, str
);
263 debug_printf("%s: %s = 0x%lx\n", __FUNCTION__
, name
, result
);
271 void _debug_assert_fail(const char *expr
,
274 const char *function
)
276 _debug_printf("%s:%u:%s: Assertion `%s' failed.\n", file
, line
, function
, expr
);
277 if (debug_get_bool_option("GALLIUM_ABORT_ON_ASSERT", TRUE
))
280 _debug_printf("continuing...\n");
285 debug_dump_enum(const struct debug_named_value
*names
,
288 static char rest
[64];
291 if(names
->value
== value
)
296 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
302 debug_dump_enum_noprefix(const struct debug_named_value
*names
,
306 static char rest
[64];
309 if(names
->value
== value
) {
310 const char *name
= names
->name
;
311 while (*name
== *prefix
) {
322 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
328 debug_dump_flags(const struct debug_named_value
*names
,
331 static char output
[4096];
332 static char rest
[256];
338 if((names
->value
& value
) == names
->value
) {
340 util_strncat(output
, "|", sizeof(output
));
343 util_strncat(output
, names
->name
, sizeof(output
) - 1);
344 output
[sizeof(output
) - 1] = '\0';
345 value
&= ~names
->value
;
352 util_strncat(output
, "|", sizeof(output
));
356 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
357 util_strncat(output
, rest
, sizeof(output
) - 1);
358 output
[sizeof(output
) - 1] = '\0';
369 void debug_print_format(const char *msg
, unsigned fmt
)
371 debug_printf("%s: %s\n", msg
, util_format_name(fmt
));
377 static const struct debug_named_value pipe_prim_names
[] = {
379 DEBUG_NAMED_VALUE(PIPE_PRIM_POINTS
),
380 DEBUG_NAMED_VALUE(PIPE_PRIM_LINES
),
381 DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_LOOP
),
382 DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_STRIP
),
383 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLES
),
384 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_STRIP
),
385 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_FAN
),
386 DEBUG_NAMED_VALUE(PIPE_PRIM_QUADS
),
387 DEBUG_NAMED_VALUE(PIPE_PRIM_QUAD_STRIP
),
388 DEBUG_NAMED_VALUE(PIPE_PRIM_POLYGON
),
389 DEBUG_NAMED_VALUE(PIPE_PRIM_LINES_ADJACENCY
),
390 DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_STRIP_ADJACENCY
),
391 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLES_ADJACENCY
),
392 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY
),
394 DEBUG_NAMED_VALUE_END
398 const char *u_prim_name( unsigned prim
)
400 return debug_dump_enum(pipe_prim_names
, prim
);
407 const char* fl_function
[1024];
409 int debug_funclog_enter(const char* f
, const int line
, const char* file
)
413 for (i
= 0; i
< fl_indent
; i
++)
415 debug_printf("%s\n", f
);
417 assert(fl_indent
< 1023);
418 fl_function
[fl_indent
++] = f
;
423 void debug_funclog_exit(const char* f
, const int line
, const char* file
)
426 assert(fl_indent
>= 0);
427 assert(fl_function
[fl_indent
] == f
);
430 void debug_funclog_enter_exit(const char* f
, const int line
, const char* file
)
433 for (i
= 0; i
< fl_indent
; i
++)
435 debug_printf("%s\n", f
);
443 * Dump an image to .ppm file.
444 * \param format PIPE_FORMAT_x
445 * \param cpp bytes per pixel
446 * \param width width in pixels
447 * \param height height in pixels
448 * \param stride row stride in bytes
450 void debug_dump_image(const char *prefix
,
451 enum pipe_format format
, unsigned cpp
,
452 unsigned width
, unsigned height
,
456 /* write a ppm file */
461 util_snprintf(filename
, sizeof(filename
), "%s.ppm", prefix
);
463 rgb8
= MALLOC(height
* width
* 3);
468 util_format_translate(
469 PIPE_FORMAT_R8G8B8_UNORM
,
474 0, 0, width
, height
);
476 /* Must be opened in binary mode or DOS line ending causes data
477 * to be read with one byte offset.
479 f
= fopen(filename
, "wb");
482 fprintf(f
, "# ppm-file created by gallium\n");
483 fprintf(f
, "%i %i\n", width
, height
);
485 fwrite(rgb8
, 1, height
* width
* 3, f
);
489 fprintf(stderr
, "Can't open %s for writing\n", filename
);
495 /* FIXME: dump resources, not surfaces... */
496 void debug_dump_surface(struct pipe_context
*pipe
,
498 struct pipe_surface
*surface
)
500 struct pipe_resource
*texture
;
501 struct pipe_transfer
*transfer
;
507 /* XXX: this doesn't necessarily work, as the driver may be using
508 * temporary storage for the surface which hasn't been propagated
509 * back into the texture. Need to nail down the semantics of views
510 * and transfers a bit better before we can say if extra work needs
513 texture
= surface
->texture
;
515 data
= pipe_transfer_map(pipe
, texture
, surface
->u
.tex
.level
,
516 surface
->u
.tex
.first_layer
,
518 0, 0, surface
->width
, surface
->height
, &transfer
);
522 debug_dump_image(prefix
,
524 util_format_get_blocksize(texture
->format
),
525 util_format_get_nblocksx(texture
->format
, surface
->width
),
526 util_format_get_nblocksy(texture
->format
, surface
->height
),
530 pipe
->transfer_unmap(pipe
, transfer
);
534 void debug_dump_texture(struct pipe_context
*pipe
,
536 struct pipe_resource
*texture
)
538 struct pipe_surface
*surface
, surf_tmpl
;
543 /* XXX for now, just dump image for layer=0, level=0 */
544 u_surface_default_template(&surf_tmpl
, texture
);
545 surface
= pipe
->create_surface(pipe
, texture
, &surf_tmpl
);
547 debug_dump_surface(pipe
, prefix
, surface
);
548 pipe
->surface_destroy(pipe
, surface
);
554 struct bmp_file_header
{
557 uint16_t bfReserved1
;
558 uint16_t bfReserved2
;
563 struct bmp_info_header
{
569 uint32_t biCompression
;
570 uint32_t biSizeImage
;
571 int32_t biXPelsPerMeter
;
572 int32_t biYPelsPerMeter
;
574 uint32_t biClrImportant
;
577 struct bmp_rgb_quad
{
585 debug_dump_surface_bmp(struct pipe_context
*pipe
,
586 const char *filename
,
587 struct pipe_surface
*surface
)
589 struct pipe_transfer
*transfer
;
590 struct pipe_resource
*texture
= surface
->texture
;
593 ptr
= pipe_transfer_map(pipe
, texture
, surface
->u
.tex
.level
,
594 surface
->u
.tex
.first_layer
, PIPE_TRANSFER_READ
,
595 0, 0, surface
->width
, surface
->height
, &transfer
);
597 debug_dump_transfer_bmp(pipe
, filename
, transfer
, ptr
);
599 pipe
->transfer_unmap(pipe
, transfer
);
603 debug_dump_transfer_bmp(struct pipe_context
*pipe
,
604 const char *filename
,
605 struct pipe_transfer
*transfer
, void *ptr
)
612 rgba
= MALLOC(transfer
->box
.width
*
613 transfer
->box
.height
*
614 transfer
->box
.depth
*
619 pipe_get_tile_rgba(transfer
, ptr
, 0, 0,
620 transfer
->box
.width
, transfer
->box
.height
,
623 debug_dump_float_rgba_bmp(filename
,
624 transfer
->box
.width
, transfer
->box
.height
,
625 rgba
, transfer
->box
.width
);
633 debug_dump_float_rgba_bmp(const char *filename
,
634 unsigned width
, unsigned height
,
635 float *rgba
, unsigned stride
)
638 struct bmp_file_header bmfh
;
639 struct bmp_info_header bmih
;
645 bmfh
.bfType
= 0x4d42;
646 bmfh
.bfSize
= 14 + 40 + height
*width
*4;
647 bmfh
.bfReserved1
= 0;
648 bmfh
.bfReserved2
= 0;
649 bmfh
.bfOffBits
= 14 + 40;
652 bmih
.biWidth
= width
;
653 bmih
.biHeight
= height
;
655 bmih
.biBitCount
= 32;
656 bmih
.biCompression
= 0;
657 bmih
.biSizeImage
= height
*width
*4;
658 bmih
.biXPelsPerMeter
= 0;
659 bmih
.biYPelsPerMeter
= 0;
661 bmih
.biClrImportant
= 0;
663 stream
= fopen(filename
, "wb");
667 fwrite(&bmfh
, 14, 1, stream
);
668 fwrite(&bmih
, 40, 1, stream
);
672 float *ptr
= rgba
+ (stride
* y
* 4);
673 for(x
= 0; x
< width
; ++x
)
675 struct bmp_rgb_quad pixel
;
676 pixel
.rgbRed
= float_to_ubyte(ptr
[x
*4 + 0]);
677 pixel
.rgbGreen
= float_to_ubyte(ptr
[x
*4 + 1]);
678 pixel
.rgbBlue
= float_to_ubyte(ptr
[x
*4 + 2]);
679 pixel
.rgbAlpha
= float_to_ubyte(ptr
[x
*4 + 3]);
680 fwrite(&pixel
, 1, 4, stream
);
691 * Print PIPE_TRANSFER_x flags with a message.
694 debug_print_transfer_flags(const char *msg
, unsigned usage
)
696 #define FLAG(x) { x, #x }
697 static const struct {
701 FLAG(PIPE_TRANSFER_READ
),
702 FLAG(PIPE_TRANSFER_WRITE
),
703 FLAG(PIPE_TRANSFER_MAP_DIRECTLY
),
704 FLAG(PIPE_TRANSFER_DISCARD_RANGE
),
705 FLAG(PIPE_TRANSFER_DONTBLOCK
),
706 FLAG(PIPE_TRANSFER_UNSYNCHRONIZED
),
707 FLAG(PIPE_TRANSFER_FLUSH_EXPLICIT
),
708 FLAG(PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
)
712 debug_printf("%s ", msg
);
714 for (i
= 0; i
< Elements(flags
); i
++) {
715 if (usage
& flags
[i
].bit
) {
716 debug_printf("%s", flags
[i
].name
);
717 usage
&= ~flags
[i
].bit
;