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 struct pipe_debug_callback
*cb
,
77 enum pipe_debug_type type
,
82 if (cb
&& cb
->debug_message
)
83 cb
->debug_message(cb
->data
, id
, type
, fmt
, args
);
89 debug_disable_error_message_boxes(void)
92 /* When Windows' error message boxes are disabled for this process (as is
93 * typically the case when running tests in an automated fashion) we disable
94 * CRT message boxes too.
96 UINT uMode
= SetErrorMode(0);
98 if (uMode
& SEM_FAILCRITICALERRORS
) {
99 /* Disable assertion failure message box.
100 * http://msdn.microsoft.com/en-us/library/sas1dkb2.aspx
102 _set_error_mode(_OUT_TO_STDERR
);
104 /* Disable abort message box.
105 * http://msdn.microsoft.com/en-us/library/e631wekh.aspx
107 _set_abort_behavior(0, _WRITE_ABORT_MSG
| _CALL_REPORTFAULT
);
115 void debug_print_blob( const char *name
,
119 const unsigned *ublob
= (const unsigned *)blob
;
122 debug_printf("%s (%d dwords%s)\n", name
, size
/4,
123 size
%4 ? "... plus a few bytes" : "");
125 for (i
= 0; i
< size
/4; i
++) {
126 debug_printf("%d:\t%08x\n", i
, ublob
[i
]);
133 debug_get_option_should_print(void)
135 static boolean first
= TRUE
;
136 static boolean value
= FALSE
;
141 /* Oh hey this will call into this function,
142 * but its cool since we set first to false
145 value
= debug_get_bool_option("GALLIUM_PRINT_OPTIONS", FALSE
);
146 /* XXX should we print this option? Currently it wont */
151 debug_get_option(const char *name
, const char *dfault
)
155 result
= os_get_option(name
);
159 if (debug_get_option_should_print())
160 debug_printf("%s: %s = %s\n", __FUNCTION__
, name
, result
? result
: "(null)");
166 debug_get_bool_option(const char *name
, boolean dfault
)
168 const char *str
= os_get_option(name
);
173 else if(!util_strcmp(str
, "n"))
175 else if(!util_strcmp(str
, "no"))
177 else if(!util_strcmp(str
, "0"))
179 else if(!util_strcmp(str
, "f"))
181 else if(!util_strcmp(str
, "F"))
183 else if(!util_strcmp(str
, "false"))
185 else if(!util_strcmp(str
, "FALSE"))
190 if (debug_get_option_should_print())
191 debug_printf("%s: %s = %s\n", __FUNCTION__
, name
, result
? "TRUE" : "FALSE");
198 debug_get_num_option(const char *name
, long dfault
)
203 str
= os_get_option(name
);
218 while('0' <= c
&& c
<= '9') {
219 result
= result
*10 + (c
- '0');
225 if (debug_get_option_should_print())
226 debug_printf("%s: %s = %li\n", __FUNCTION__
, name
, result
);
231 static boolean
str_has_option(const char *str
, const char *name
)
239 if (!util_strcmp(str
, "all")) {
243 /* Find 'name' in 'str' surrounded by non-alphanumeric characters. */
245 const char *start
= str
;
246 unsigned name_len
= strlen(name
);
248 /* 'start' is the beginning of the currently-parsed word,
249 * we increment 'str' each iteration.
250 * if we find either the end of string or a non-alphanumeric character,
251 * we compare 'start' up to 'str-1' with 'name'. */
254 if (!*str
|| !(isalnum(*str
) || *str
== '_')) {
255 if (str
-start
== name_len
&&
256 !memcmp(start
, name
, name_len
)) {
275 debug_get_flags_option(const char *name
,
276 const struct debug_named_value
*flags
,
281 const struct debug_named_value
*orig
= flags
;
282 unsigned namealign
= 0;
284 str
= os_get_option(name
);
287 else if (!util_strcmp(str
, "help")) {
289 _debug_printf("%s: help for %s:\n", __FUNCTION__
, name
);
290 for (; flags
->name
; ++flags
)
291 namealign
= MAX2(namealign
, strlen(flags
->name
));
292 for (flags
= orig
; flags
->name
; ++flags
)
293 _debug_printf("| %*s [0x%0*"PRIx64
"]%s%s\n", namealign
, flags
->name
,
294 (int)sizeof(uint64_t)*CHAR_BIT
/4, flags
->value
,
295 flags
->desc
? " " : "", flags
->desc
? flags
->desc
: "");
299 while( flags
->name
) {
300 if (str_has_option(str
, flags
->name
))
301 result
|= flags
->value
;
306 if (debug_get_option_should_print()) {
308 debug_printf("%s: %s = 0x%"PRIx64
" (%s)\n", __FUNCTION__
, name
, result
, str
);
310 debug_printf("%s: %s = 0x%"PRIx64
"\n", __FUNCTION__
, name
, result
);
318 void _debug_assert_fail(const char *expr
,
321 const char *function
)
323 _debug_printf("%s:%u:%s: Assertion `%s' failed.\n", file
, line
, function
, expr
);
329 debug_dump_enum(const struct debug_named_value
*names
,
332 static char rest
[64];
335 if(names
->value
== value
)
340 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
346 debug_dump_enum_noprefix(const struct debug_named_value
*names
,
350 static char rest
[64];
353 if(names
->value
== value
) {
354 const char *name
= names
->name
;
355 while (*name
== *prefix
) {
366 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
372 debug_dump_flags(const struct debug_named_value
*names
,
375 static char output
[4096];
376 static char rest
[256];
382 if((names
->value
& value
) == names
->value
) {
384 util_strncat(output
, "|", sizeof(output
) - strlen(output
) - 1);
387 util_strncat(output
, names
->name
, sizeof(output
) - strlen(output
) - 1);
388 output
[sizeof(output
) - 1] = '\0';
389 value
&= ~names
->value
;
396 util_strncat(output
, "|", sizeof(output
) - strlen(output
) - 1);
400 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
401 util_strncat(output
, rest
, sizeof(output
) - strlen(output
) - 1);
402 output
[sizeof(output
) - 1] = '\0';
413 void debug_print_format(const char *msg
, unsigned fmt
)
415 debug_printf("%s: %s\n", msg
, util_format_name(fmt
));
420 /** Return string name of given primitive type */
422 u_prim_name(unsigned prim
)
424 static const struct debug_named_value names
[] = {
425 DEBUG_NAMED_VALUE(PIPE_PRIM_POINTS
),
426 DEBUG_NAMED_VALUE(PIPE_PRIM_LINES
),
427 DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_LOOP
),
428 DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_STRIP
),
429 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLES
),
430 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_STRIP
),
431 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_FAN
),
432 DEBUG_NAMED_VALUE(PIPE_PRIM_QUADS
),
433 DEBUG_NAMED_VALUE(PIPE_PRIM_QUAD_STRIP
),
434 DEBUG_NAMED_VALUE(PIPE_PRIM_POLYGON
),
435 DEBUG_NAMED_VALUE(PIPE_PRIM_LINES_ADJACENCY
),
436 DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_STRIP_ADJACENCY
),
437 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLES_ADJACENCY
),
438 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY
),
439 DEBUG_NAMED_VALUE_END
441 return debug_dump_enum(names
, prim
);
448 const char* fl_function
[1024];
450 int debug_funclog_enter(const char* f
, const int line
, const char* file
)
454 for (i
= 0; i
< fl_indent
; i
++)
456 debug_printf("%s\n", f
);
458 assert(fl_indent
< 1023);
459 fl_function
[fl_indent
++] = f
;
464 void debug_funclog_exit(const char* f
, const int line
, const char* file
)
467 assert(fl_indent
>= 0);
468 assert(fl_function
[fl_indent
] == f
);
471 void debug_funclog_enter_exit(const char* f
, const int line
, const char* file
)
474 for (i
= 0; i
< fl_indent
; i
++)
476 debug_printf("%s\n", f
);
484 * Dump an image to .ppm file.
485 * \param format PIPE_FORMAT_x
486 * \param cpp bytes per pixel
487 * \param width width in pixels
488 * \param height height in pixels
489 * \param stride row stride in bytes
491 void debug_dump_image(const char *prefix
,
492 enum pipe_format format
, unsigned cpp
,
493 unsigned width
, unsigned height
,
497 /* write a ppm file */
502 util_snprintf(filename
, sizeof(filename
), "%s.ppm", prefix
);
504 rgb8
= MALLOC(height
* width
* 3);
509 util_format_translate(
510 PIPE_FORMAT_R8G8B8_UNORM
,
515 0, 0, width
, height
);
517 /* Must be opened in binary mode or DOS line ending causes data
518 * to be read with one byte offset.
520 f
= fopen(filename
, "wb");
523 fprintf(f
, "# ppm-file created by gallium\n");
524 fprintf(f
, "%i %i\n", width
, height
);
526 fwrite(rgb8
, 1, height
* width
* 3, f
);
530 fprintf(stderr
, "Can't open %s for writing\n", filename
);
536 /* FIXME: dump resources, not surfaces... */
537 void debug_dump_surface(struct pipe_context
*pipe
,
539 struct pipe_surface
*surface
)
541 struct pipe_resource
*texture
;
542 struct pipe_transfer
*transfer
;
548 /* XXX: this doesn't necessarily work, as the driver may be using
549 * temporary storage for the surface which hasn't been propagated
550 * back into the texture. Need to nail down the semantics of views
551 * and transfers a bit better before we can say if extra work needs
554 texture
= surface
->texture
;
556 data
= pipe_transfer_map(pipe
, texture
, surface
->u
.tex
.level
,
557 surface
->u
.tex
.first_layer
,
559 0, 0, surface
->width
, surface
->height
, &transfer
);
563 debug_dump_image(prefix
,
565 util_format_get_blocksize(texture
->format
),
566 util_format_get_nblocksx(texture
->format
, surface
->width
),
567 util_format_get_nblocksy(texture
->format
, surface
->height
),
571 pipe
->transfer_unmap(pipe
, transfer
);
575 void debug_dump_texture(struct pipe_context
*pipe
,
577 struct pipe_resource
*texture
)
579 struct pipe_surface
*surface
, surf_tmpl
;
584 /* XXX for now, just dump image for layer=0, level=0 */
585 u_surface_default_template(&surf_tmpl
, texture
);
586 surface
= pipe
->create_surface(pipe
, texture
, &surf_tmpl
);
588 debug_dump_surface(pipe
, prefix
, surface
);
589 pipe
->surface_destroy(pipe
, surface
);
595 struct bmp_file_header
{
598 uint16_t bfReserved1
;
599 uint16_t bfReserved2
;
604 struct bmp_info_header
{
610 uint32_t biCompression
;
611 uint32_t biSizeImage
;
612 int32_t biXPelsPerMeter
;
613 int32_t biYPelsPerMeter
;
615 uint32_t biClrImportant
;
618 struct bmp_rgb_quad
{
626 debug_dump_surface_bmp(struct pipe_context
*pipe
,
627 const char *filename
,
628 struct pipe_surface
*surface
)
630 struct pipe_transfer
*transfer
;
631 struct pipe_resource
*texture
= surface
->texture
;
634 ptr
= pipe_transfer_map(pipe
, texture
, surface
->u
.tex
.level
,
635 surface
->u
.tex
.first_layer
, PIPE_TRANSFER_READ
,
636 0, 0, surface
->width
, surface
->height
, &transfer
);
638 debug_dump_transfer_bmp(pipe
, filename
, transfer
, ptr
);
640 pipe
->transfer_unmap(pipe
, transfer
);
644 debug_dump_transfer_bmp(struct pipe_context
*pipe
,
645 const char *filename
,
646 struct pipe_transfer
*transfer
, void *ptr
)
653 rgba
= MALLOC(transfer
->box
.width
*
654 transfer
->box
.height
*
655 transfer
->box
.depth
*
660 pipe_get_tile_rgba(transfer
, ptr
, 0, 0,
661 transfer
->box
.width
, transfer
->box
.height
,
664 debug_dump_float_rgba_bmp(filename
,
665 transfer
->box
.width
, transfer
->box
.height
,
666 rgba
, transfer
->box
.width
);
674 debug_dump_float_rgba_bmp(const char *filename
,
675 unsigned width
, unsigned height
,
676 float *rgba
, unsigned stride
)
679 struct bmp_file_header bmfh
;
680 struct bmp_info_header bmih
;
686 bmfh
.bfType
= 0x4d42;
687 bmfh
.bfSize
= 14 + 40 + height
*width
*4;
688 bmfh
.bfReserved1
= 0;
689 bmfh
.bfReserved2
= 0;
690 bmfh
.bfOffBits
= 14 + 40;
693 bmih
.biWidth
= width
;
694 bmih
.biHeight
= height
;
696 bmih
.biBitCount
= 32;
697 bmih
.biCompression
= 0;
698 bmih
.biSizeImage
= height
*width
*4;
699 bmih
.biXPelsPerMeter
= 0;
700 bmih
.biYPelsPerMeter
= 0;
702 bmih
.biClrImportant
= 0;
704 stream
= fopen(filename
, "wb");
708 fwrite(&bmfh
, 14, 1, stream
);
709 fwrite(&bmih
, 40, 1, stream
);
713 float *ptr
= rgba
+ (stride
* y
* 4);
714 for(x
= 0; x
< width
; ++x
)
716 struct bmp_rgb_quad pixel
;
717 pixel
.rgbRed
= float_to_ubyte(ptr
[x
*4 + 0]);
718 pixel
.rgbGreen
= float_to_ubyte(ptr
[x
*4 + 1]);
719 pixel
.rgbBlue
= float_to_ubyte(ptr
[x
*4 + 2]);
720 pixel
.rgbAlpha
= float_to_ubyte(ptr
[x
*4 + 3]);
721 fwrite(&pixel
, 1, 4, stream
);
731 debug_dump_ubyte_rgba_bmp(const char *filename
,
732 unsigned width
, unsigned height
,
733 const ubyte
*rgba
, unsigned stride
)
736 struct bmp_file_header bmfh
;
737 struct bmp_info_header bmih
;
744 bmfh
.bfType
= 0x4d42;
745 bmfh
.bfSize
= 14 + 40 + height
*width
*4;
746 bmfh
.bfReserved1
= 0;
747 bmfh
.bfReserved2
= 0;
748 bmfh
.bfOffBits
= 14 + 40;
751 bmih
.biWidth
= width
;
752 bmih
.biHeight
= height
;
754 bmih
.biBitCount
= 32;
755 bmih
.biCompression
= 0;
756 bmih
.biSizeImage
= height
*width
*4;
757 bmih
.biXPelsPerMeter
= 0;
758 bmih
.biYPelsPerMeter
= 0;
760 bmih
.biClrImportant
= 0;
762 stream
= fopen(filename
, "wb");
767 fwrite(&bmfh
, 14, 1, stream
);
768 fwrite(&bmih
, 40, 1, stream
);
772 const ubyte
*ptr
= rgba
+ (stride
* y
* 4);
773 for(x
= 0; x
< width
; ++x
)
775 struct bmp_rgb_quad pixel
;
776 pixel
.rgbRed
= ptr
[x
*4 + 0];
777 pixel
.rgbGreen
= ptr
[x
*4 + 1];
778 pixel
.rgbBlue
= ptr
[x
*4 + 2];
779 pixel
.rgbAlpha
= ptr
[x
*4 + 3];
780 fwrite(&pixel
, 1, 4, stream
);
791 * Print PIPE_TRANSFER_x flags with a message.
794 debug_print_transfer_flags(const char *msg
, unsigned usage
)
796 static const struct debug_named_value names
[] = {
797 DEBUG_NAMED_VALUE(PIPE_TRANSFER_READ
),
798 DEBUG_NAMED_VALUE(PIPE_TRANSFER_WRITE
),
799 DEBUG_NAMED_VALUE(PIPE_TRANSFER_MAP_DIRECTLY
),
800 DEBUG_NAMED_VALUE(PIPE_TRANSFER_DISCARD_RANGE
),
801 DEBUG_NAMED_VALUE(PIPE_TRANSFER_DONTBLOCK
),
802 DEBUG_NAMED_VALUE(PIPE_TRANSFER_UNSYNCHRONIZED
),
803 DEBUG_NAMED_VALUE(PIPE_TRANSFER_FLUSH_EXPLICIT
),
804 DEBUG_NAMED_VALUE(PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
),
805 DEBUG_NAMED_VALUE(PIPE_TRANSFER_PERSISTENT
),
806 DEBUG_NAMED_VALUE(PIPE_TRANSFER_COHERENT
),
807 DEBUG_NAMED_VALUE_END
810 debug_printf("%s: %s\n", msg
, debug_dump_flags(names
, usage
));
815 * Print PIPE_BIND_x flags with a message.
818 debug_print_bind_flags(const char *msg
, unsigned usage
)
820 static const struct debug_named_value names
[] = {
821 DEBUG_NAMED_VALUE(PIPE_BIND_DEPTH_STENCIL
),
822 DEBUG_NAMED_VALUE(PIPE_BIND_RENDER_TARGET
),
823 DEBUG_NAMED_VALUE(PIPE_BIND_BLENDABLE
),
824 DEBUG_NAMED_VALUE(PIPE_BIND_SAMPLER_VIEW
),
825 DEBUG_NAMED_VALUE(PIPE_BIND_VERTEX_BUFFER
),
826 DEBUG_NAMED_VALUE(PIPE_BIND_INDEX_BUFFER
),
827 DEBUG_NAMED_VALUE(PIPE_BIND_CONSTANT_BUFFER
),
828 DEBUG_NAMED_VALUE(PIPE_BIND_DISPLAY_TARGET
),
829 DEBUG_NAMED_VALUE(PIPE_BIND_TRANSFER_WRITE
),
830 DEBUG_NAMED_VALUE(PIPE_BIND_TRANSFER_READ
),
831 DEBUG_NAMED_VALUE(PIPE_BIND_STREAM_OUTPUT
),
832 DEBUG_NAMED_VALUE(PIPE_BIND_CURSOR
),
833 DEBUG_NAMED_VALUE(PIPE_BIND_CUSTOM
),
834 DEBUG_NAMED_VALUE(PIPE_BIND_GLOBAL
),
835 DEBUG_NAMED_VALUE(PIPE_BIND_SHADER_BUFFER
),
836 DEBUG_NAMED_VALUE(PIPE_BIND_SHADER_IMAGE
),
837 DEBUG_NAMED_VALUE(PIPE_BIND_COMPUTE_RESOURCE
),
838 DEBUG_NAMED_VALUE(PIPE_BIND_COMMAND_ARGS_BUFFER
),
839 DEBUG_NAMED_VALUE(PIPE_BIND_SCANOUT
),
840 DEBUG_NAMED_VALUE(PIPE_BIND_SHARED
),
841 DEBUG_NAMED_VALUE(PIPE_BIND_LINEAR
),
842 DEBUG_NAMED_VALUE_END
845 debug_printf("%s: %s\n", msg
, debug_dump_flags(names
, usage
));
850 * Print PIPE_USAGE_x enum values with a message.
853 debug_print_usage_enum(const char *msg
, unsigned usage
)
855 static const struct debug_named_value names
[] = {
856 DEBUG_NAMED_VALUE(PIPE_USAGE_DEFAULT
),
857 DEBUG_NAMED_VALUE(PIPE_USAGE_IMMUTABLE
),
858 DEBUG_NAMED_VALUE(PIPE_USAGE_DYNAMIC
),
859 DEBUG_NAMED_VALUE(PIPE_USAGE_STREAM
),
860 DEBUG_NAMED_VALUE(PIPE_USAGE_STAGING
),
861 DEBUG_NAMED_VALUE_END
864 debug_printf("%s: %s\n", msg
, debug_dump_enum(names
, usage
));