1 /**************************************************************************
3 * Copyright 2008 VMware, Inc.
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 VMWARE 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"
47 #include <limits.h> /* CHAR_BIT */
48 #include <ctype.h> /* isalnum */
56 void _debug_vprintf(const char *format
, va_list ap
)
58 static char buf
[4096] = {'\0'};
59 #if defined(PIPE_OS_WINDOWS) || defined(PIPE_SUBSYSTEM_EMBEDDED)
60 /* We buffer until we find a newline. */
61 size_t len
= strlen(buf
);
62 int ret
= util_vsnprintf(buf
+ len
, sizeof(buf
) - len
, format
, ap
);
63 if(ret
> (int)(sizeof(buf
) - len
- 1) || util_strchr(buf
+ len
, '\n')) {
68 util_vsnprintf(buf
, sizeof(buf
), format
, ap
);
75 debug_disable_error_message_boxes(void)
78 /* When Windows' error message boxes are disabled for this process (as is
79 * typically the case when running tests in an automated fashion) we disable
80 * CRT message boxes too.
82 UINT uMode
= SetErrorMode(0);
84 if (uMode
& SEM_FAILCRITICALERRORS
) {
85 /* Disable assertion failure message box.
86 * http://msdn.microsoft.com/en-us/library/sas1dkb2.aspx
88 _set_error_mode(_OUT_TO_STDERR
);
90 /* Disable abort message box.
91 * http://msdn.microsoft.com/en-us/library/e631wekh.aspx
93 _set_abort_behavior(0, _WRITE_ABORT_MSG
| _CALL_REPORTFAULT
);
101 void debug_print_blob( const char *name
,
105 const unsigned *ublob
= (const unsigned *)blob
;
108 debug_printf("%s (%d dwords%s)\n", name
, size
/4,
109 size
%4 ? "... plus a few bytes" : "");
111 for (i
= 0; i
< size
/4; i
++) {
112 debug_printf("%d:\t%08x\n", i
, ublob
[i
]);
119 debug_get_option_should_print(void)
121 static boolean first
= TRUE
;
122 static boolean value
= FALSE
;
127 /* Oh hey this will call into this function,
128 * but its cool since we set first to false
131 value
= debug_get_bool_option("GALLIUM_PRINT_OPTIONS", FALSE
);
132 /* XXX should we print this option? Currently it wont */
137 debug_get_option(const char *name
, const char *dfault
)
141 result
= os_get_option(name
);
145 if (debug_get_option_should_print())
146 debug_printf("%s: %s = %s\n", __FUNCTION__
, name
, result
? result
: "(null)");
152 debug_get_bool_option(const char *name
, boolean dfault
)
154 const char *str
= os_get_option(name
);
159 else if(!util_strcmp(str
, "n"))
161 else if(!util_strcmp(str
, "no"))
163 else if(!util_strcmp(str
, "0"))
165 else if(!util_strcmp(str
, "f"))
167 else if(!util_strcmp(str
, "F"))
169 else if(!util_strcmp(str
, "false"))
171 else if(!util_strcmp(str
, "FALSE"))
176 if (debug_get_option_should_print())
177 debug_printf("%s: %s = %s\n", __FUNCTION__
, name
, result
? "TRUE" : "FALSE");
184 debug_get_num_option(const char *name
, long dfault
)
189 str
= os_get_option(name
);
204 while('0' <= c
&& c
<= '9') {
205 result
= result
*10 + (c
- '0');
211 if (debug_get_option_should_print())
212 debug_printf("%s: %s = %li\n", __FUNCTION__
, name
, result
);
217 static boolean
str_has_option(const char *str
, const char *name
)
225 if (!util_strcmp(str
, "all")) {
229 /* Find 'name' in 'str' surrounded by non-alphanumeric characters. */
231 const char *start
= str
;
232 unsigned name_len
= strlen(name
);
234 /* 'start' is the beginning of the currently-parsed word,
235 * we increment 'str' each iteration.
236 * if we find either the end of string or a non-alphanumeric character,
237 * we compare 'start' up to 'str-1' with 'name'. */
240 if (!*str
|| !(isalnum(*str
) || *str
== '_')) {
241 if (str
-start
== name_len
&&
242 !memcmp(start
, name
, name_len
)) {
261 debug_get_flags_option(const char *name
,
262 const struct debug_named_value
*flags
,
267 const struct debug_named_value
*orig
= flags
;
268 unsigned namealign
= 0;
270 str
= os_get_option(name
);
273 else if (!util_strcmp(str
, "help")) {
275 _debug_printf("%s: help for %s:\n", __FUNCTION__
, name
);
276 for (; flags
->name
; ++flags
)
277 namealign
= MAX2(namealign
, strlen(flags
->name
));
278 for (flags
= orig
; flags
->name
; ++flags
)
279 _debug_printf("| %*s [0x%0*"PRIx64
"]%s%s\n", namealign
, flags
->name
,
280 (int)sizeof(uint64_t)*CHAR_BIT
/4, flags
->value
,
281 flags
->desc
? " " : "", flags
->desc
? flags
->desc
: "");
285 while( flags
->name
) {
286 if (str_has_option(str
, flags
->name
))
287 result
|= flags
->value
;
292 if (debug_get_option_should_print()) {
294 debug_printf("%s: %s = 0x%"PRIx64
" (%s)\n", __FUNCTION__
, name
, result
, str
);
296 debug_printf("%s: %s = 0x%"PRIx64
"\n", __FUNCTION__
, name
, result
);
304 void _debug_assert_fail(const char *expr
,
307 const char *function
)
309 _debug_printf("%s:%u:%s: Assertion `%s' failed.\n", file
, line
, function
, expr
);
315 debug_dump_enum(const struct debug_named_value
*names
,
318 static char rest
[64];
321 if(names
->value
== value
)
326 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
332 debug_dump_enum_noprefix(const struct debug_named_value
*names
,
336 static char rest
[64];
339 if(names
->value
== value
) {
340 const char *name
= names
->name
;
341 while (*name
== *prefix
) {
352 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
358 debug_dump_flags(const struct debug_named_value
*names
,
361 static char output
[4096];
362 static char rest
[256];
368 if((names
->value
& value
) == names
->value
) {
370 util_strncat(output
, "|", sizeof(output
) - strlen(output
) - 1);
373 util_strncat(output
, names
->name
, sizeof(output
) - strlen(output
) - 1);
374 output
[sizeof(output
) - 1] = '\0';
375 value
&= ~names
->value
;
382 util_strncat(output
, "|", sizeof(output
) - strlen(output
) - 1);
386 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
387 util_strncat(output
, rest
, sizeof(output
) - strlen(output
) - 1);
388 output
[sizeof(output
) - 1] = '\0';
399 void debug_print_format(const char *msg
, unsigned fmt
)
401 debug_printf("%s: %s\n", msg
, util_format_name(fmt
));
406 /** Return string name of given primitive type */
408 u_prim_name(unsigned prim
)
410 static const struct debug_named_value names
[] = {
411 DEBUG_NAMED_VALUE(PIPE_PRIM_POINTS
),
412 DEBUG_NAMED_VALUE(PIPE_PRIM_LINES
),
413 DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_LOOP
),
414 DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_STRIP
),
415 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLES
),
416 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_STRIP
),
417 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_FAN
),
418 DEBUG_NAMED_VALUE(PIPE_PRIM_QUADS
),
419 DEBUG_NAMED_VALUE(PIPE_PRIM_QUAD_STRIP
),
420 DEBUG_NAMED_VALUE(PIPE_PRIM_POLYGON
),
421 DEBUG_NAMED_VALUE(PIPE_PRIM_LINES_ADJACENCY
),
422 DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_STRIP_ADJACENCY
),
423 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLES_ADJACENCY
),
424 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY
),
425 DEBUG_NAMED_VALUE_END
427 return debug_dump_enum(names
, prim
);
434 const char* fl_function
[1024];
436 int debug_funclog_enter(const char* f
, const int line
, const char* file
)
440 for (i
= 0; i
< fl_indent
; i
++)
442 debug_printf("%s\n", f
);
444 assert(fl_indent
< 1023);
445 fl_function
[fl_indent
++] = f
;
450 void debug_funclog_exit(const char* f
, const int line
, const char* file
)
453 assert(fl_indent
>= 0);
454 assert(fl_function
[fl_indent
] == f
);
457 void debug_funclog_enter_exit(const char* f
, const int line
, const char* file
)
460 for (i
= 0; i
< fl_indent
; i
++)
462 debug_printf("%s\n", f
);
470 * Dump an image to .ppm file.
471 * \param format PIPE_FORMAT_x
472 * \param cpp bytes per pixel
473 * \param width width in pixels
474 * \param height height in pixels
475 * \param stride row stride in bytes
477 void debug_dump_image(const char *prefix
,
478 enum pipe_format format
, unsigned cpp
,
479 unsigned width
, unsigned height
,
483 /* write a ppm file */
488 util_snprintf(filename
, sizeof(filename
), "%s.ppm", prefix
);
490 rgb8
= MALLOC(height
* width
* 3);
495 util_format_translate(
496 PIPE_FORMAT_R8G8B8_UNORM
,
501 0, 0, width
, height
);
503 /* Must be opened in binary mode or DOS line ending causes data
504 * to be read with one byte offset.
506 f
= fopen(filename
, "wb");
509 fprintf(f
, "# ppm-file created by gallium\n");
510 fprintf(f
, "%i %i\n", width
, height
);
512 fwrite(rgb8
, 1, height
* width
* 3, f
);
516 fprintf(stderr
, "Can't open %s for writing\n", filename
);
522 /* FIXME: dump resources, not surfaces... */
523 void debug_dump_surface(struct pipe_context
*pipe
,
525 struct pipe_surface
*surface
)
527 struct pipe_resource
*texture
;
528 struct pipe_transfer
*transfer
;
534 /* XXX: this doesn't necessarily work, as the driver may be using
535 * temporary storage for the surface which hasn't been propagated
536 * back into the texture. Need to nail down the semantics of views
537 * and transfers a bit better before we can say if extra work needs
540 texture
= surface
->texture
;
542 data
= pipe_transfer_map(pipe
, texture
, surface
->u
.tex
.level
,
543 surface
->u
.tex
.first_layer
,
545 0, 0, surface
->width
, surface
->height
, &transfer
);
549 debug_dump_image(prefix
,
551 util_format_get_blocksize(texture
->format
),
552 util_format_get_nblocksx(texture
->format
, surface
->width
),
553 util_format_get_nblocksy(texture
->format
, surface
->height
),
557 pipe
->transfer_unmap(pipe
, transfer
);
561 void debug_dump_texture(struct pipe_context
*pipe
,
563 struct pipe_resource
*texture
)
565 struct pipe_surface
*surface
, surf_tmpl
;
570 /* XXX for now, just dump image for layer=0, level=0 */
571 u_surface_default_template(&surf_tmpl
, texture
);
572 surface
= pipe
->create_surface(pipe
, texture
, &surf_tmpl
);
574 debug_dump_surface(pipe
, prefix
, surface
);
575 pipe
->surface_destroy(pipe
, surface
);
581 struct bmp_file_header
{
584 uint16_t bfReserved1
;
585 uint16_t bfReserved2
;
590 struct bmp_info_header
{
596 uint32_t biCompression
;
597 uint32_t biSizeImage
;
598 int32_t biXPelsPerMeter
;
599 int32_t biYPelsPerMeter
;
601 uint32_t biClrImportant
;
604 struct bmp_rgb_quad
{
612 debug_dump_surface_bmp(struct pipe_context
*pipe
,
613 const char *filename
,
614 struct pipe_surface
*surface
)
616 struct pipe_transfer
*transfer
;
617 struct pipe_resource
*texture
= surface
->texture
;
620 ptr
= pipe_transfer_map(pipe
, texture
, surface
->u
.tex
.level
,
621 surface
->u
.tex
.first_layer
, PIPE_TRANSFER_READ
,
622 0, 0, surface
->width
, surface
->height
, &transfer
);
624 debug_dump_transfer_bmp(pipe
, filename
, transfer
, ptr
);
626 pipe
->transfer_unmap(pipe
, transfer
);
630 debug_dump_transfer_bmp(struct pipe_context
*pipe
,
631 const char *filename
,
632 struct pipe_transfer
*transfer
, void *ptr
)
639 rgba
= MALLOC(transfer
->box
.width
*
640 transfer
->box
.height
*
641 transfer
->box
.depth
*
646 pipe_get_tile_rgba(transfer
, ptr
, 0, 0,
647 transfer
->box
.width
, transfer
->box
.height
,
650 debug_dump_float_rgba_bmp(filename
,
651 transfer
->box
.width
, transfer
->box
.height
,
652 rgba
, transfer
->box
.width
);
660 debug_dump_float_rgba_bmp(const char *filename
,
661 unsigned width
, unsigned height
,
662 float *rgba
, unsigned stride
)
665 struct bmp_file_header bmfh
;
666 struct bmp_info_header bmih
;
672 bmfh
.bfType
= 0x4d42;
673 bmfh
.bfSize
= 14 + 40 + height
*width
*4;
674 bmfh
.bfReserved1
= 0;
675 bmfh
.bfReserved2
= 0;
676 bmfh
.bfOffBits
= 14 + 40;
679 bmih
.biWidth
= width
;
680 bmih
.biHeight
= height
;
682 bmih
.biBitCount
= 32;
683 bmih
.biCompression
= 0;
684 bmih
.biSizeImage
= height
*width
*4;
685 bmih
.biXPelsPerMeter
= 0;
686 bmih
.biYPelsPerMeter
= 0;
688 bmih
.biClrImportant
= 0;
690 stream
= fopen(filename
, "wb");
694 fwrite(&bmfh
, 14, 1, stream
);
695 fwrite(&bmih
, 40, 1, stream
);
699 float *ptr
= rgba
+ (stride
* y
* 4);
700 for(x
= 0; x
< width
; ++x
)
702 struct bmp_rgb_quad pixel
;
703 pixel
.rgbRed
= float_to_ubyte(ptr
[x
*4 + 0]);
704 pixel
.rgbGreen
= float_to_ubyte(ptr
[x
*4 + 1]);
705 pixel
.rgbBlue
= float_to_ubyte(ptr
[x
*4 + 2]);
706 pixel
.rgbAlpha
= float_to_ubyte(ptr
[x
*4 + 3]);
707 fwrite(&pixel
, 1, 4, stream
);
718 * Print PIPE_TRANSFER_x flags with a message.
721 debug_print_transfer_flags(const char *msg
, unsigned usage
)
723 static const struct debug_named_value names
[] = {
724 DEBUG_NAMED_VALUE(PIPE_TRANSFER_READ
),
725 DEBUG_NAMED_VALUE(PIPE_TRANSFER_WRITE
),
726 DEBUG_NAMED_VALUE(PIPE_TRANSFER_MAP_DIRECTLY
),
727 DEBUG_NAMED_VALUE(PIPE_TRANSFER_DISCARD_RANGE
),
728 DEBUG_NAMED_VALUE(PIPE_TRANSFER_DONTBLOCK
),
729 DEBUG_NAMED_VALUE(PIPE_TRANSFER_UNSYNCHRONIZED
),
730 DEBUG_NAMED_VALUE(PIPE_TRANSFER_FLUSH_EXPLICIT
),
731 DEBUG_NAMED_VALUE(PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
),
732 DEBUG_NAMED_VALUE(PIPE_TRANSFER_PERSISTENT
),
733 DEBUG_NAMED_VALUE(PIPE_TRANSFER_COHERENT
),
734 DEBUG_NAMED_VALUE_END
737 debug_printf("%s: %s\n", msg
, debug_dump_flags(names
, usage
));
742 * Print PIPE_BIND_x flags with a message.
745 debug_print_bind_flags(const char *msg
, unsigned usage
)
747 static const struct debug_named_value names
[] = {
748 DEBUG_NAMED_VALUE(PIPE_BIND_DEPTH_STENCIL
),
749 DEBUG_NAMED_VALUE(PIPE_BIND_RENDER_TARGET
),
750 DEBUG_NAMED_VALUE(PIPE_BIND_BLENDABLE
),
751 DEBUG_NAMED_VALUE(PIPE_BIND_SAMPLER_VIEW
),
752 DEBUG_NAMED_VALUE(PIPE_BIND_VERTEX_BUFFER
),
753 DEBUG_NAMED_VALUE(PIPE_BIND_INDEX_BUFFER
),
754 DEBUG_NAMED_VALUE(PIPE_BIND_CONSTANT_BUFFER
),
755 DEBUG_NAMED_VALUE(PIPE_BIND_DISPLAY_TARGET
),
756 DEBUG_NAMED_VALUE(PIPE_BIND_TRANSFER_WRITE
),
757 DEBUG_NAMED_VALUE(PIPE_BIND_TRANSFER_READ
),
758 DEBUG_NAMED_VALUE(PIPE_BIND_STREAM_OUTPUT
),
759 DEBUG_NAMED_VALUE(PIPE_BIND_CURSOR
),
760 DEBUG_NAMED_VALUE(PIPE_BIND_CUSTOM
),
761 DEBUG_NAMED_VALUE(PIPE_BIND_GLOBAL
),
762 DEBUG_NAMED_VALUE(PIPE_BIND_SHADER_BUFFER
),
763 DEBUG_NAMED_VALUE(PIPE_BIND_SHADER_IMAGE
),
764 DEBUG_NAMED_VALUE(PIPE_BIND_COMPUTE_RESOURCE
),
765 DEBUG_NAMED_VALUE(PIPE_BIND_COMMAND_ARGS_BUFFER
),
766 DEBUG_NAMED_VALUE(PIPE_BIND_SCANOUT
),
767 DEBUG_NAMED_VALUE(PIPE_BIND_SHARED
),
768 DEBUG_NAMED_VALUE(PIPE_BIND_LINEAR
),
769 DEBUG_NAMED_VALUE_END
772 debug_printf("%s: %s\n", msg
, debug_dump_flags(names
, usage
));
777 * Print PIPE_USAGE_x enum values with a message.
780 debug_print_usage_enum(const char *msg
, unsigned usage
)
782 static const struct debug_named_value names
[] = {
783 DEBUG_NAMED_VALUE(PIPE_USAGE_DEFAULT
),
784 DEBUG_NAMED_VALUE(PIPE_USAGE_IMMUTABLE
),
785 DEBUG_NAMED_VALUE(PIPE_USAGE_DYNAMIC
),
786 DEBUG_NAMED_VALUE(PIPE_USAGE_STREAM
),
787 DEBUG_NAMED_VALUE(PIPE_USAGE_STAGING
),
788 DEBUG_NAMED_VALUE_END
791 debug_printf("%s: %s\n", msg
, debug_dump_enum(names
, usage
));