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 "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 #include "util/u_surface.h"
46 #include <limits.h> /* CHAR_BIT */
48 void _debug_vprintf(const char *format
, va_list ap
)
50 #if defined(PIPE_OS_WINDOWS) || defined(PIPE_OS_EMBEDDED)
51 /* We buffer until we find a newline. */
52 static char buf
[4096] = {'\0'};
53 size_t len
= strlen(buf
);
54 int ret
= util_vsnprintf(buf
+ len
, sizeof(buf
) - len
, format
, ap
);
55 if(ret
> (int)(sizeof(buf
) - len
- 1) || util_strchr(buf
+ len
, '\n')) {
56 os_log_message(buf
* h
);
60 /* Just print as-is to stderr */
61 vfprintf(stderr
, format
, ap
);
67 void debug_print_blob( const char *name
,
71 const unsigned *ublob
= (const unsigned *)blob
;
74 debug_printf("%s (%d dwords%s)\n", name
, size
/4,
75 size
%4 ? "... plus a few bytes" : "");
77 for (i
= 0; i
< size
/4; i
++) {
78 debug_printf("%d:\t%08x\n", i
, ublob
[i
]);
85 debug_get_option_should_print(void)
87 static boolean first
= TRUE
;
88 static boolean value
= FALSE
;
93 /* Oh hey this will call into this function,
94 * but its cool since we set first to false
97 value
= debug_get_bool_option("GALLIUM_PRINT_OPTIONS", FALSE
);
98 /* XXX should we print this option? Currently it wont */
103 debug_get_option(const char *name
, const char *dfault
)
107 result
= os_get_option(name
);
111 if (debug_get_option_should_print())
112 debug_printf("%s: %s = %s\n", __FUNCTION__
, name
, result
? result
: "(null)");
118 debug_get_bool_option(const char *name
, boolean dfault
)
120 const char *str
= os_get_option(name
);
125 else if(!util_strcmp(str
, "n"))
127 else if(!util_strcmp(str
, "no"))
129 else if(!util_strcmp(str
, "0"))
131 else if(!util_strcmp(str
, "f"))
133 else if(!util_strcmp(str
, "F"))
135 else if(!util_strcmp(str
, "false"))
137 else if(!util_strcmp(str
, "FALSE"))
142 if (debug_get_option_should_print())
143 debug_printf("%s: %s = %s\n", __FUNCTION__
, name
, result
? "TRUE" : "FALSE");
150 debug_get_num_option(const char *name
, long dfault
)
155 str
= os_get_option(name
);
170 while('0' <= c
&& c
<= '9') {
171 result
= result
*10 + (c
- '0');
177 if (debug_get_option_should_print())
178 debug_printf("%s: %s = %li\n", __FUNCTION__
, name
, result
);
185 debug_get_flags_option(const char *name
,
186 const struct debug_named_value
*flags
,
187 unsigned long dfault
)
189 unsigned long result
;
191 const struct debug_named_value
*orig
= flags
;
194 str
= os_get_option(name
);
197 else if (!util_strcmp(str
, "help")) {
199 _debug_printf("%s: help for %s:\n", __FUNCTION__
, name
);
200 for (; flags
->name
; ++flags
)
201 namealign
= MAX2(namealign
, strlen(flags
->name
));
202 for (flags
= orig
; flags
->name
; ++flags
)
203 _debug_printf("| %*s [0x%0*lx]%s%s\n", namealign
, flags
->name
,
204 (int)sizeof(unsigned long)*CHAR_BIT
/4, flags
->value
,
205 flags
->desc
? " " : "", flags
->desc
? flags
->desc
: "");
209 while( flags
->name
) {
210 if (!util_strcmp(str
, "all") || util_strstr(str
, flags
->name
))
211 result
|= flags
->value
;
216 if (debug_get_option_should_print()) {
218 debug_printf("%s: %s = 0x%lx (%s)\n", __FUNCTION__
, name
, result
, str
);
220 debug_printf("%s: %s = 0x%lx\n", __FUNCTION__
, name
, result
);
228 void _debug_assert_fail(const char *expr
,
231 const char *function
)
233 _debug_printf("%s:%u:%s: Assertion `%s' failed.\n", file
, line
, function
, expr
);
234 #if defined(PIPE_OS_WINDOWS) && !defined(PIPE_SUBSYSTEM_WINDOWS_USER)
235 if (debug_get_bool_option("GALLIUM_ABORT_ON_ASSERT", FALSE
))
237 if (debug_get_bool_option("GALLIUM_ABORT_ON_ASSERT", TRUE
))
241 _debug_printf("continuing...\n");
246 debug_dump_enum(const struct debug_named_value
*names
,
249 static char rest
[64];
252 if(names
->value
== value
)
257 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
263 debug_dump_enum_noprefix(const struct debug_named_value
*names
,
267 static char rest
[64];
270 if(names
->value
== value
) {
271 const char *name
= names
->name
;
272 while (*name
== *prefix
) {
283 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
289 debug_dump_flags(const struct debug_named_value
*names
,
292 static char output
[4096];
293 static char rest
[256];
299 if((names
->value
& value
) == names
->value
) {
301 util_strncat(output
, "|", sizeof(output
));
304 util_strncat(output
, names
->name
, sizeof(output
) - 1);
305 output
[sizeof(output
) - 1] = '\0';
306 value
&= ~names
->value
;
313 util_strncat(output
, "|", sizeof(output
));
317 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
318 util_strncat(output
, rest
, sizeof(output
) - 1);
319 output
[sizeof(output
) - 1] = '\0';
330 void debug_print_format(const char *msg
, unsigned fmt
)
332 debug_printf("%s: %s\n", msg
, util_format_name(fmt
));
338 static const struct debug_named_value pipe_prim_names
[] = {
340 DEBUG_NAMED_VALUE(PIPE_PRIM_POINTS
),
341 DEBUG_NAMED_VALUE(PIPE_PRIM_LINES
),
342 DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_LOOP
),
343 DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_STRIP
),
344 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLES
),
345 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_STRIP
),
346 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_FAN
),
347 DEBUG_NAMED_VALUE(PIPE_PRIM_QUADS
),
348 DEBUG_NAMED_VALUE(PIPE_PRIM_QUAD_STRIP
),
349 DEBUG_NAMED_VALUE(PIPE_PRIM_POLYGON
),
351 DEBUG_NAMED_VALUE_END
355 const char *u_prim_name( unsigned prim
)
357 return debug_dump_enum(pipe_prim_names
, prim
);
365 * Dump an image to a .raw or .ppm file (depends on OS).
366 * \param format PIPE_FORMAT_x
367 * \param cpp bytes per pixel
368 * \param width width in pixels
369 * \param height height in pixels
370 * \param stride row stride in bytes
372 void debug_dump_image(const char *prefix
,
373 unsigned format
, unsigned cpp
,
374 unsigned width
, unsigned height
,
378 #ifdef PIPE_SUBSYSTEM_WINDOWS_DISPLAY
379 static unsigned no
= 0;
381 WCHAR wfilename
[sizeof(filename
)];
389 unsigned char *pMap
= NULL
;
392 util_snprintf(filename
, sizeof(filename
), "\\??\\c:\\%03u%s.raw", ++no
, prefix
);
393 for(i
= 0; i
< sizeof(filename
); ++i
)
394 wfilename
[i
] = (WCHAR
)filename
[i
];
396 pMap
= (unsigned char *)EngMapFile(wfilename
, sizeof(header
) + height
*width
*cpp
, &iFile
);
400 header
.format
= format
;
402 header
.width
= width
;
403 header
.height
= height
;
404 memcpy(pMap
, &header
, sizeof(header
));
405 pMap
+= sizeof(header
);
407 for(i
= 0; i
< height
; ++i
) {
408 memcpy(pMap
, (unsigned char *)data
+ stride
*i
, cpp
*width
);
413 #elif defined(PIPE_OS_UNIX)
414 /* write a ppm file */
418 util_snprintf(filename
, sizeof(filename
), "%s.ppm", prefix
);
420 f
= fopen(filename
, "w");
424 const uint8_t *ptr
= (uint8_t *) data
;
426 /* XXX this is a hack */
428 case PIPE_FORMAT_B8G8R8A8_UNORM
:
440 fprintf(f
, "# ppm-file created by osdemo.c\n");
441 fprintf(f
, "%i %i\n", width
, height
);
445 f
= fopen(filename
, "ab"); /* reopen in binary append mode */
446 for (y
= 0; y
< height
; y
++) {
447 for (x
= 0; x
< width
; x
++) {
448 i
= y
* stride
+ x
* cpp
;
449 fputc(ptr
[i
+ r
], f
); /* write red */
450 fputc(ptr
[i
+ g
], f
); /* write green */
451 fputc(ptr
[i
+ b
], f
); /* write blue */
457 fprintf(stderr
, "Can't open %s for writing\n", filename
);
462 /* FIXME: dump resources, not surfaces... */
463 void debug_dump_surface(struct pipe_context
*pipe
,
465 struct pipe_surface
*surface
)
467 struct pipe_resource
*texture
;
468 struct pipe_transfer
*transfer
;
474 /* XXX: this doesn't necessarily work, as the driver may be using
475 * temporary storage for the surface which hasn't been propagated
476 * back into the texture. Need to nail down the semantics of views
477 * and transfers a bit better before we can say if extra work needs
480 texture
= surface
->texture
;
482 transfer
= pipe_get_transfer(pipe
, texture
, surface
->u
.tex
.level
,
483 surface
->u
.tex
.first_layer
,
485 0, 0, surface
->width
, surface
->height
);
487 data
= pipe
->transfer_map(pipe
, transfer
);
491 debug_dump_image(prefix
,
493 util_format_get_blocksize(texture
->format
),
494 util_format_get_nblocksx(texture
->format
, surface
->width
),
495 util_format_get_nblocksy(texture
->format
, surface
->height
),
499 pipe
->transfer_unmap(pipe
, transfer
);
501 pipe
->transfer_destroy(pipe
, transfer
);
505 void debug_dump_texture(struct pipe_context
*pipe
,
507 struct pipe_resource
*texture
)
509 struct pipe_surface
*surface
, surf_tmpl
;
514 /* XXX for now, just dump image for layer=0, level=0 */
515 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
516 u_surface_default_template(&surf_tmpl
, texture
, 0 /* no bind flag - not a surface */);
517 surface
= pipe
->create_surface(pipe
, texture
, &surf_tmpl
);
519 debug_dump_surface(pipe
, prefix
, surface
);
520 pipe
->surface_destroy(pipe
, surface
);
526 struct bmp_file_header
{
529 uint16_t bfReserved1
;
530 uint16_t bfReserved2
;
535 struct bmp_info_header
{
541 uint32_t biCompression
;
542 uint32_t biSizeImage
;
543 int32_t biXPelsPerMeter
;
544 int32_t biYPelsPerMeter
;
546 uint32_t biClrImportant
;
549 struct bmp_rgb_quad
{
557 debug_dump_surface_bmp(struct pipe_context
*pipe
,
558 const char *filename
,
559 struct pipe_surface
*surface
)
561 #ifndef PIPE_SUBSYSTEM_WINDOWS_MINIPORT
562 struct pipe_transfer
*transfer
;
563 struct pipe_resource
*texture
= surface
->texture
;
565 transfer
= pipe_get_transfer(pipe
, texture
, surface
->u
.tex
.level
,
566 surface
->u
.tex
.first_layer
, PIPE_TRANSFER_READ
,
567 0, 0, surface
->width
, surface
->height
);
569 debug_dump_transfer_bmp(pipe
, filename
, transfer
);
571 pipe
->transfer_destroy(pipe
, transfer
);
576 debug_dump_transfer_bmp(struct pipe_context
*pipe
,
577 const char *filename
,
578 struct pipe_transfer
*transfer
)
580 #ifndef PIPE_SUBSYSTEM_WINDOWS_MINIPORT
586 rgba
= MALLOC(transfer
->box
.width
*
587 transfer
->box
.height
*
588 transfer
->box
.depth
*
593 pipe_get_tile_rgba(pipe
, transfer
, 0, 0,
594 transfer
->box
.width
, transfer
->box
.height
,
597 debug_dump_float_rgba_bmp(filename
,
598 transfer
->box
.width
, transfer
->box
.height
,
599 rgba
, transfer
->box
.width
);
608 debug_dump_float_rgba_bmp(const char *filename
,
609 unsigned width
, unsigned height
,
610 float *rgba
, unsigned stride
)
612 #ifndef PIPE_SUBSYSTEM_WINDOWS_MINIPORT
613 struct os_stream
*stream
;
614 struct bmp_file_header bmfh
;
615 struct bmp_info_header bmih
;
621 bmfh
.bfType
= 0x4d42;
622 bmfh
.bfSize
= 14 + 40 + height
*width
*4;
623 bmfh
.bfReserved1
= 0;
624 bmfh
.bfReserved2
= 0;
625 bmfh
.bfOffBits
= 14 + 40;
628 bmih
.biWidth
= width
;
629 bmih
.biHeight
= height
;
631 bmih
.biBitCount
= 32;
632 bmih
.biCompression
= 0;
633 bmih
.biSizeImage
= height
*width
*4;
634 bmih
.biXPelsPerMeter
= 0;
635 bmih
.biYPelsPerMeter
= 0;
637 bmih
.biClrImportant
= 0;
639 stream
= os_file_stream_create(filename
);
643 os_stream_write(stream
, &bmfh
, 14);
644 os_stream_write(stream
, &bmih
, 40);
648 float *ptr
= rgba
+ (stride
* y
* 4);
649 for(x
= 0; x
< width
; ++x
)
651 struct bmp_rgb_quad pixel
;
652 pixel
.rgbRed
= float_to_ubyte(ptr
[x
*4 + 0]);
653 pixel
.rgbGreen
= float_to_ubyte(ptr
[x
*4 + 1]);
654 pixel
.rgbBlue
= float_to_ubyte(ptr
[x
*4 + 2]);
655 pixel
.rgbAlpha
= 255;
656 os_stream_write(stream
, &pixel
, 4);
660 os_stream_close(stream
);