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 */
57 _debug_vprintf(const char *format
, va_list ap
)
59 static char buf
[4096] = {'\0'};
60 #if defined(PIPE_OS_WINDOWS) || defined(PIPE_SUBSYSTEM_EMBEDDED)
61 /* We buffer until we find a newline. */
62 size_t len
= strlen(buf
);
63 int ret
= util_vsnprintf(buf
+ len
, sizeof(buf
) - len
, format
, ap
);
64 if (ret
> (int)(sizeof(buf
) - len
- 1) || util_strchr(buf
+ len
, '\n')) {
69 util_vsnprintf(buf
, sizeof(buf
), format
, ap
);
76 _pipe_debug_message(struct pipe_debug_callback
*cb
,
78 enum pipe_debug_type type
,
83 if (cb
&& cb
->debug_message
)
84 cb
->debug_message(cb
->data
, id
, type
, fmt
, args
);
90 debug_disable_error_message_boxes(void)
93 /* When Windows' error message boxes are disabled for this process (as is
94 * typically the case when running tests in an automated fashion) we disable
95 * CRT message boxes too.
97 UINT uMode
= SetErrorMode(0);
99 if (uMode
& SEM_FAILCRITICALERRORS
) {
100 /* Disable assertion failure message box.
101 * http://msdn.microsoft.com/en-us/library/sas1dkb2.aspx
103 _set_error_mode(_OUT_TO_STDERR
);
105 /* Disable abort message box.
106 * http://msdn.microsoft.com/en-us/library/e631wekh.aspx
108 _set_abort_behavior(0, _WRITE_ABORT_MSG
| _CALL_REPORTFAULT
);
117 debug_print_blob(const char *name
, const void *blob
, unsigned size
)
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 */
152 debug_get_option(const char *name
, const char *dfault
)
156 result
= os_get_option(name
);
160 if (debug_get_option_should_print())
161 debug_printf("%s: %s = %s\n", __FUNCTION__
, name
,
162 result
? result
: "(null)");
169 debug_get_bool_option(const char *name
, boolean dfault
)
171 const char *str
= os_get_option(name
);
176 else if (!util_strcmp(str
, "n"))
178 else if (!util_strcmp(str
, "no"))
180 else if (!util_strcmp(str
, "0"))
182 else if (!util_strcmp(str
, "f"))
184 else if (!util_strcmp(str
, "F"))
186 else if (!util_strcmp(str
, "false"))
188 else if (!util_strcmp(str
, "FALSE"))
193 if (debug_get_option_should_print())
194 debug_printf("%s: %s = %s\n", __FUNCTION__
, name
,
195 result
? "TRUE" : "FALSE");
202 debug_get_num_option(const char *name
, long dfault
)
207 str
= os_get_option(name
);
222 while ('0' <= c
&& c
<= '9') {
223 result
= result
*10 + (c
- '0');
229 if (debug_get_option_should_print())
230 debug_printf("%s: %s = %li\n", __FUNCTION__
, name
, result
);
237 str_has_option(const char *str
, const char *name
)
245 if (!util_strcmp(str
, "all")) {
249 /* Find 'name' in 'str' surrounded by non-alphanumeric characters. */
251 const char *start
= str
;
252 unsigned name_len
= strlen(name
);
254 /* 'start' is the beginning of the currently-parsed word,
255 * we increment 'str' each iteration.
256 * if we find either the end of string or a non-alphanumeric character,
257 * we compare 'start' up to 'str-1' with 'name'. */
260 if (!*str
|| !(isalnum(*str
) || *str
== '_')) {
261 if (str
-start
== name_len
&&
262 !memcmp(start
, name
, name_len
)) {
282 debug_get_flags_option(const char *name
,
283 const struct debug_named_value
*flags
,
288 const struct debug_named_value
*orig
= flags
;
289 unsigned namealign
= 0;
291 str
= os_get_option(name
);
294 else if (!util_strcmp(str
, "help")) {
296 _debug_printf("%s: help for %s:\n", __FUNCTION__
, name
);
297 for (; flags
->name
; ++flags
)
298 namealign
= MAX2(namealign
, strlen(flags
->name
));
299 for (flags
= orig
; flags
->name
; ++flags
)
300 _debug_printf("| %*s [0x%0*"PRIx64
"]%s%s\n", namealign
, flags
->name
,
301 (int)sizeof(uint64_t)*CHAR_BIT
/4, flags
->value
,
302 flags
->desc
? " " : "", flags
->desc
? flags
->desc
: "");
306 while (flags
->name
) {
307 if (str_has_option(str
, flags
->name
))
308 result
|= flags
->value
;
313 if (debug_get_option_should_print()) {
315 debug_printf("%s: %s = 0x%"PRIx64
" (%s)\n",
316 __FUNCTION__
, name
, result
, str
);
318 debug_printf("%s: %s = 0x%"PRIx64
"\n", __FUNCTION__
, name
, result
);
327 _debug_assert_fail(const char *expr
, const char *file
, unsigned line
,
328 const char *function
)
330 _debug_printf("%s:%u:%s: Assertion `%s' failed.\n",
331 file
, line
, function
, expr
);
337 debug_dump_enum(const struct debug_named_value
*names
,
340 static char rest
[64];
342 while (names
->name
) {
343 if (names
->value
== value
)
348 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
354 debug_dump_enum_noprefix(const struct debug_named_value
*names
,
358 static char rest
[64];
360 while (names
->name
) {
361 if (names
->value
== value
) {
362 const char *name
= names
->name
;
363 while (*name
== *prefix
) {
372 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
378 debug_dump_flags(const struct debug_named_value
*names
, unsigned long value
)
380 static char output
[4096];
381 static char rest
[256];
386 while (names
->name
) {
387 if ((names
->value
& value
) == names
->value
) {
389 util_strncat(output
, "|", sizeof(output
) - strlen(output
) - 1);
392 util_strncat(output
, names
->name
, sizeof(output
) - strlen(output
) - 1);
393 output
[sizeof(output
) - 1] = '\0';
394 value
&= ~names
->value
;
401 util_strncat(output
, "|", sizeof(output
) - strlen(output
) - 1);
405 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
406 util_strncat(output
, rest
, sizeof(output
) - strlen(output
) - 1);
407 output
[sizeof(output
) - 1] = '\0';
419 debug_print_format(const char *msg
, unsigned fmt
)
421 debug_printf("%s: %s\n", msg
, util_format_name(fmt
));
426 /** Return string name of given primitive type */
428 u_prim_name(unsigned prim
)
430 static const struct debug_named_value names
[] = {
431 DEBUG_NAMED_VALUE(PIPE_PRIM_POINTS
),
432 DEBUG_NAMED_VALUE(PIPE_PRIM_LINES
),
433 DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_LOOP
),
434 DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_STRIP
),
435 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLES
),
436 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_STRIP
),
437 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_FAN
),
438 DEBUG_NAMED_VALUE(PIPE_PRIM_QUADS
),
439 DEBUG_NAMED_VALUE(PIPE_PRIM_QUAD_STRIP
),
440 DEBUG_NAMED_VALUE(PIPE_PRIM_POLYGON
),
441 DEBUG_NAMED_VALUE(PIPE_PRIM_LINES_ADJACENCY
),
442 DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_STRIP_ADJACENCY
),
443 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLES_ADJACENCY
),
444 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY
),
445 DEBUG_NAMED_VALUE_END
447 return debug_dump_enum(names
, prim
);
454 const char* fl_function
[1024];
457 debug_funclog_enter(const char* f
, const int line
, const char* file
)
461 for (i
= 0; i
< fl_indent
; i
++)
463 debug_printf("%s\n", f
);
465 assert(fl_indent
< 1023);
466 fl_function
[fl_indent
++] = f
;
472 debug_funclog_exit(const char* f
, const int line
, const char* file
)
475 assert(fl_indent
>= 0);
476 assert(fl_function
[fl_indent
] == f
);
480 debug_funclog_enter_exit(const char* f
, const int line
, const char* file
)
483 for (i
= 0; i
< fl_indent
; i
++)
485 debug_printf("%s\n", f
);
493 * Dump an image to .ppm file.
494 * \param format PIPE_FORMAT_x
495 * \param cpp bytes per pixel
496 * \param width width in pixels
497 * \param height height in pixels
498 * \param stride row stride in bytes
501 debug_dump_image(const char *prefix
,
502 enum pipe_format format
, unsigned cpp
,
503 unsigned width
, unsigned height
,
507 /* write a ppm file */
512 util_snprintf(filename
, sizeof(filename
), "%s.ppm", prefix
);
514 rgb8
= MALLOC(height
* width
* 3);
519 util_format_translate(
520 PIPE_FORMAT_R8G8B8_UNORM
,
525 0, 0, width
, height
);
527 /* Must be opened in binary mode or DOS line ending causes data
528 * to be read with one byte offset.
530 f
= fopen(filename
, "wb");
533 fprintf(f
, "# ppm-file created by gallium\n");
534 fprintf(f
, "%i %i\n", width
, height
);
536 fwrite(rgb8
, 1, height
* width
* 3, f
);
540 fprintf(stderr
, "Can't open %s for writing\n", filename
);
547 /* FIXME: dump resources, not surfaces... */
549 debug_dump_surface(struct pipe_context
*pipe
,
551 struct pipe_surface
*surface
)
553 struct pipe_resource
*texture
;
554 struct pipe_transfer
*transfer
;
560 /* XXX: this doesn't necessarily work, as the driver may be using
561 * temporary storage for the surface which hasn't been propagated
562 * back into the texture. Need to nail down the semantics of views
563 * and transfers a bit better before we can say if extra work needs
566 texture
= surface
->texture
;
568 data
= pipe_transfer_map(pipe
, texture
, surface
->u
.tex
.level
,
569 surface
->u
.tex
.first_layer
,
571 0, 0, surface
->width
, surface
->height
, &transfer
);
575 debug_dump_image(prefix
,
577 util_format_get_blocksize(texture
->format
),
578 util_format_get_nblocksx(texture
->format
, surface
->width
),
579 util_format_get_nblocksy(texture
->format
, surface
->height
),
583 pipe
->transfer_unmap(pipe
, transfer
);
588 debug_dump_texture(struct pipe_context
*pipe
,
590 struct pipe_resource
*texture
)
592 struct pipe_surface
*surface
, surf_tmpl
;
597 /* XXX for now, just dump image for layer=0, level=0 */
598 u_surface_default_template(&surf_tmpl
, texture
);
599 surface
= pipe
->create_surface(pipe
, texture
, &surf_tmpl
);
601 debug_dump_surface(pipe
, prefix
, surface
);
602 pipe
->surface_destroy(pipe
, surface
);
608 struct bmp_file_header
{
611 uint16_t bfReserved1
;
612 uint16_t bfReserved2
;
617 struct bmp_info_header
{
623 uint32_t biCompression
;
624 uint32_t biSizeImage
;
625 int32_t biXPelsPerMeter
;
626 int32_t biYPelsPerMeter
;
628 uint32_t biClrImportant
;
631 struct bmp_rgb_quad
{
639 debug_dump_surface_bmp(struct pipe_context
*pipe
,
640 const char *filename
,
641 struct pipe_surface
*surface
)
643 struct pipe_transfer
*transfer
;
644 struct pipe_resource
*texture
= surface
->texture
;
647 ptr
= pipe_transfer_map(pipe
, texture
, surface
->u
.tex
.level
,
648 surface
->u
.tex
.first_layer
, PIPE_TRANSFER_READ
,
649 0, 0, surface
->width
, surface
->height
, &transfer
);
651 debug_dump_transfer_bmp(pipe
, filename
, transfer
, ptr
);
653 pipe
->transfer_unmap(pipe
, transfer
);
657 debug_dump_transfer_bmp(struct pipe_context
*pipe
,
658 const char *filename
,
659 struct pipe_transfer
*transfer
, void *ptr
)
666 rgba
= MALLOC(transfer
->box
.width
*
667 transfer
->box
.height
*
668 transfer
->box
.depth
*
673 pipe_get_tile_rgba(transfer
, ptr
, 0, 0,
674 transfer
->box
.width
, transfer
->box
.height
,
677 debug_dump_float_rgba_bmp(filename
,
678 transfer
->box
.width
, transfer
->box
.height
,
679 rgba
, transfer
->box
.width
);
687 debug_dump_float_rgba_bmp(const char *filename
,
688 unsigned width
, unsigned height
,
689 float *rgba
, unsigned stride
)
692 struct bmp_file_header bmfh
;
693 struct bmp_info_header bmih
;
699 bmfh
.bfType
= 0x4d42;
700 bmfh
.bfSize
= 14 + 40 + height
*width
*4;
701 bmfh
.bfReserved1
= 0;
702 bmfh
.bfReserved2
= 0;
703 bmfh
.bfOffBits
= 14 + 40;
706 bmih
.biWidth
= width
;
707 bmih
.biHeight
= height
;
709 bmih
.biBitCount
= 32;
710 bmih
.biCompression
= 0;
711 bmih
.biSizeImage
= height
*width
*4;
712 bmih
.biXPelsPerMeter
= 0;
713 bmih
.biYPelsPerMeter
= 0;
715 bmih
.biClrImportant
= 0;
717 stream
= fopen(filename
, "wb");
721 fwrite(&bmfh
, 14, 1, stream
);
722 fwrite(&bmih
, 40, 1, stream
);
726 float *ptr
= rgba
+ (stride
* y
* 4);
727 for (x
= 0; x
< width
; ++x
) {
728 struct bmp_rgb_quad pixel
;
729 pixel
.rgbRed
= float_to_ubyte(ptr
[x
*4 + 0]);
730 pixel
.rgbGreen
= float_to_ubyte(ptr
[x
*4 + 1]);
731 pixel
.rgbBlue
= float_to_ubyte(ptr
[x
*4 + 2]);
732 pixel
.rgbAlpha
= float_to_ubyte(ptr
[x
*4 + 3]);
733 fwrite(&pixel
, 1, 4, stream
);
743 debug_dump_ubyte_rgba_bmp(const char *filename
,
744 unsigned width
, unsigned height
,
745 const ubyte
*rgba
, unsigned stride
)
748 struct bmp_file_header bmfh
;
749 struct bmp_info_header bmih
;
756 bmfh
.bfType
= 0x4d42;
757 bmfh
.bfSize
= 14 + 40 + height
*width
*4;
758 bmfh
.bfReserved1
= 0;
759 bmfh
.bfReserved2
= 0;
760 bmfh
.bfOffBits
= 14 + 40;
763 bmih
.biWidth
= width
;
764 bmih
.biHeight
= height
;
766 bmih
.biBitCount
= 32;
767 bmih
.biCompression
= 0;
768 bmih
.biSizeImage
= height
*width
*4;
769 bmih
.biXPelsPerMeter
= 0;
770 bmih
.biYPelsPerMeter
= 0;
772 bmih
.biClrImportant
= 0;
774 stream
= fopen(filename
, "wb");
779 fwrite(&bmfh
, 14, 1, stream
);
780 fwrite(&bmih
, 40, 1, stream
);
784 const ubyte
*ptr
= rgba
+ (stride
* y
* 4);
785 for (x
= 0; x
< width
; ++x
) {
786 struct bmp_rgb_quad pixel
;
787 pixel
.rgbRed
= ptr
[x
*4 + 0];
788 pixel
.rgbGreen
= ptr
[x
*4 + 1];
789 pixel
.rgbBlue
= ptr
[x
*4 + 2];
790 pixel
.rgbAlpha
= ptr
[x
*4 + 3];
791 fwrite(&pixel
, 1, 4, stream
);
802 * Print PIPE_TRANSFER_x flags with a message.
805 debug_print_transfer_flags(const char *msg
, unsigned usage
)
807 static const struct debug_named_value names
[] = {
808 DEBUG_NAMED_VALUE(PIPE_TRANSFER_READ
),
809 DEBUG_NAMED_VALUE(PIPE_TRANSFER_WRITE
),
810 DEBUG_NAMED_VALUE(PIPE_TRANSFER_MAP_DIRECTLY
),
811 DEBUG_NAMED_VALUE(PIPE_TRANSFER_DISCARD_RANGE
),
812 DEBUG_NAMED_VALUE(PIPE_TRANSFER_DONTBLOCK
),
813 DEBUG_NAMED_VALUE(PIPE_TRANSFER_UNSYNCHRONIZED
),
814 DEBUG_NAMED_VALUE(PIPE_TRANSFER_FLUSH_EXPLICIT
),
815 DEBUG_NAMED_VALUE(PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
),
816 DEBUG_NAMED_VALUE(PIPE_TRANSFER_PERSISTENT
),
817 DEBUG_NAMED_VALUE(PIPE_TRANSFER_COHERENT
),
818 DEBUG_NAMED_VALUE_END
821 debug_printf("%s: %s\n", msg
, debug_dump_flags(names
, usage
));
826 * Print PIPE_BIND_x flags with a message.
829 debug_print_bind_flags(const char *msg
, unsigned usage
)
831 static const struct debug_named_value names
[] = {
832 DEBUG_NAMED_VALUE(PIPE_BIND_DEPTH_STENCIL
),
833 DEBUG_NAMED_VALUE(PIPE_BIND_RENDER_TARGET
),
834 DEBUG_NAMED_VALUE(PIPE_BIND_BLENDABLE
),
835 DEBUG_NAMED_VALUE(PIPE_BIND_SAMPLER_VIEW
),
836 DEBUG_NAMED_VALUE(PIPE_BIND_VERTEX_BUFFER
),
837 DEBUG_NAMED_VALUE(PIPE_BIND_INDEX_BUFFER
),
838 DEBUG_NAMED_VALUE(PIPE_BIND_CONSTANT_BUFFER
),
839 DEBUG_NAMED_VALUE(PIPE_BIND_DISPLAY_TARGET
),
840 DEBUG_NAMED_VALUE(PIPE_BIND_TRANSFER_WRITE
),
841 DEBUG_NAMED_VALUE(PIPE_BIND_TRANSFER_READ
),
842 DEBUG_NAMED_VALUE(PIPE_BIND_STREAM_OUTPUT
),
843 DEBUG_NAMED_VALUE(PIPE_BIND_CURSOR
),
844 DEBUG_NAMED_VALUE(PIPE_BIND_CUSTOM
),
845 DEBUG_NAMED_VALUE(PIPE_BIND_GLOBAL
),
846 DEBUG_NAMED_VALUE(PIPE_BIND_SHADER_BUFFER
),
847 DEBUG_NAMED_VALUE(PIPE_BIND_SHADER_IMAGE
),
848 DEBUG_NAMED_VALUE(PIPE_BIND_COMPUTE_RESOURCE
),
849 DEBUG_NAMED_VALUE(PIPE_BIND_COMMAND_ARGS_BUFFER
),
850 DEBUG_NAMED_VALUE(PIPE_BIND_SCANOUT
),
851 DEBUG_NAMED_VALUE(PIPE_BIND_SHARED
),
852 DEBUG_NAMED_VALUE(PIPE_BIND_LINEAR
),
853 DEBUG_NAMED_VALUE_END
856 debug_printf("%s: %s\n", msg
, debug_dump_flags(names
, usage
));
861 * Print PIPE_USAGE_x enum values with a message.
864 debug_print_usage_enum(const char *msg
, unsigned usage
)
866 static const struct debug_named_value names
[] = {
867 DEBUG_NAMED_VALUE(PIPE_USAGE_DEFAULT
),
868 DEBUG_NAMED_VALUE(PIPE_USAGE_IMMUTABLE
),
869 DEBUG_NAMED_VALUE(PIPE_USAGE_DYNAMIC
),
870 DEBUG_NAMED_VALUE(PIPE_USAGE_STREAM
),
871 DEBUG_NAMED_VALUE(PIPE_USAGE_STAGING
),
872 DEBUG_NAMED_VALUE_END
875 debug_printf("%s: %s\n", msg
, debug_dump_enum(names
, usage
));