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 /* We buffer until we find a newline. */
51 static char buf
[4096] = {'\0'};
52 size_t len
= strlen(buf
);
53 int ret
= util_vsnprintf(buf
+ len
, sizeof(buf
) - len
, format
, ap
);
54 if(ret
> (int)(sizeof(buf
) - len
- 1) || util_strchr(buf
+ len
, '\n')) {
62 void debug_print_blob( const char *name
,
66 const unsigned *ublob
= (const unsigned *)blob
;
69 debug_printf("%s (%d dwords%s)\n", name
, size
/4,
70 size
%4 ? "... plus a few bytes" : "");
72 for (i
= 0; i
< size
/4; i
++) {
73 debug_printf("%d:\t%08x\n", i
, ublob
[i
]);
80 debug_get_option_should_print(void)
82 static boolean first
= TRUE
;
83 static boolean value
= FALSE
;
88 /* Oh hey this will call into this function,
89 * but its cool since we set first to false
92 value
= debug_get_bool_option("GALLIUM_PRINT_OPTIONS", FALSE
);
93 /* XXX should we print this option? Currently it wont */
98 debug_get_option(const char *name
, const char *dfault
)
102 result
= os_get_option(name
);
106 if (debug_get_option_should_print())
107 debug_printf("%s: %s = %s\n", __FUNCTION__
, name
, result
? result
: "(null)");
113 debug_get_bool_option(const char *name
, boolean dfault
)
115 const char *str
= os_get_option(name
);
120 else if(!util_strcmp(str
, "n"))
122 else if(!util_strcmp(str
, "no"))
124 else if(!util_strcmp(str
, "0"))
126 else if(!util_strcmp(str
, "f"))
128 else if(!util_strcmp(str
, "F"))
130 else if(!util_strcmp(str
, "false"))
132 else if(!util_strcmp(str
, "FALSE"))
137 if (debug_get_option_should_print())
138 debug_printf("%s: %s = %s\n", __FUNCTION__
, name
, result
? "TRUE" : "FALSE");
145 debug_get_num_option(const char *name
, long dfault
)
150 str
= os_get_option(name
);
165 while('0' <= c
&& c
<= '9') {
166 result
= result
*10 + (c
- '0');
172 if (debug_get_option_should_print())
173 debug_printf("%s: %s = %li\n", __FUNCTION__
, name
, result
);
180 debug_get_flags_option(const char *name
,
181 const struct debug_named_value
*flags
,
182 unsigned long dfault
)
184 unsigned long result
;
186 const struct debug_named_value
*orig
= flags
;
189 str
= os_get_option(name
);
192 else if (!util_strcmp(str
, "help")) {
194 _debug_printf("%s: help for %s:\n", __FUNCTION__
, name
);
195 for (; flags
->name
; ++flags
)
196 namealign
= MAX2(namealign
, strlen(flags
->name
));
197 for (flags
= orig
; flags
->name
; ++flags
)
198 _debug_printf("| %*s [0x%0*lx]%s%s\n", namealign
, flags
->name
,
199 (int)sizeof(unsigned long)*CHAR_BIT
/4, flags
->value
,
200 flags
->desc
? " " : "", flags
->desc
? flags
->desc
: "");
204 while( flags
->name
) {
205 if (!util_strcmp(str
, "all") || util_strstr(str
, flags
->name
))
206 result
|= flags
->value
;
211 if (debug_get_option_should_print()) {
213 debug_printf("%s: %s = 0x%lx (%s)\n", __FUNCTION__
, name
, result
, str
);
215 debug_printf("%s: %s = 0x%lx\n", __FUNCTION__
, name
, result
);
223 void _debug_assert_fail(const char *expr
,
226 const char *function
)
228 _debug_printf("%s:%u:%s: Assertion `%s' failed.\n", file
, line
, function
, expr
);
229 #if defined(PIPE_OS_WINDOWS) && !defined(PIPE_SUBSYSTEM_WINDOWS_USER)
230 if (debug_get_bool_option("GALLIUM_ABORT_ON_ASSERT", FALSE
))
232 if (debug_get_bool_option("GALLIUM_ABORT_ON_ASSERT", TRUE
))
236 _debug_printf("continuing...\n");
241 debug_dump_enum(const struct debug_named_value
*names
,
244 static char rest
[64];
247 if(names
->value
== value
)
252 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
258 debug_dump_enum_noprefix(const struct debug_named_value
*names
,
262 static char rest
[64];
265 if(names
->value
== value
) {
266 const char *name
= names
->name
;
267 while (*name
== *prefix
) {
278 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
284 debug_dump_flags(const struct debug_named_value
*names
,
287 static char output
[4096];
288 static char rest
[256];
294 if((names
->value
& value
) == names
->value
) {
296 util_strncat(output
, "|", sizeof(output
));
299 util_strncat(output
, names
->name
, sizeof(output
) - 1);
300 output
[sizeof(output
) - 1] = '\0';
301 value
&= ~names
->value
;
308 util_strncat(output
, "|", sizeof(output
));
312 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
313 util_strncat(output
, rest
, sizeof(output
) - 1);
314 output
[sizeof(output
) - 1] = '\0';
325 void debug_print_format(const char *msg
, unsigned fmt
)
327 debug_printf("%s: %s\n", msg
, util_format_name(fmt
));
333 static const struct debug_named_value pipe_prim_names
[] = {
335 DEBUG_NAMED_VALUE(PIPE_PRIM_POINTS
),
336 DEBUG_NAMED_VALUE(PIPE_PRIM_LINES
),
337 DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_LOOP
),
338 DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_STRIP
),
339 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLES
),
340 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_STRIP
),
341 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_FAN
),
342 DEBUG_NAMED_VALUE(PIPE_PRIM_QUADS
),
343 DEBUG_NAMED_VALUE(PIPE_PRIM_QUAD_STRIP
),
344 DEBUG_NAMED_VALUE(PIPE_PRIM_POLYGON
),
346 DEBUG_NAMED_VALUE_END
350 const char *u_prim_name( unsigned prim
)
352 return debug_dump_enum(pipe_prim_names
, prim
);
360 * Dump an image to a .raw or .ppm file (depends on OS).
361 * \param format PIPE_FORMAT_x
362 * \param cpp bytes per pixel
363 * \param width width in pixels
364 * \param height height in pixels
365 * \param stride row stride in bytes
367 void debug_dump_image(const char *prefix
,
368 unsigned format
, unsigned cpp
,
369 unsigned width
, unsigned height
,
373 #ifdef PIPE_SUBSYSTEM_WINDOWS_DISPLAY
374 static unsigned no
= 0;
376 WCHAR wfilename
[sizeof(filename
)];
384 unsigned char *pMap
= NULL
;
387 util_snprintf(filename
, sizeof(filename
), "\\??\\c:\\%03u%s.raw", ++no
, prefix
);
388 for(i
= 0; i
< sizeof(filename
); ++i
)
389 wfilename
[i
] = (WCHAR
)filename
[i
];
391 pMap
= (unsigned char *)EngMapFile(wfilename
, sizeof(header
) + height
*width
*cpp
, &iFile
);
395 header
.format
= format
;
397 header
.width
= width
;
398 header
.height
= height
;
399 memcpy(pMap
, &header
, sizeof(header
));
400 pMap
+= sizeof(header
);
402 for(i
= 0; i
< height
; ++i
) {
403 memcpy(pMap
, (unsigned char *)data
+ stride
*i
, cpp
*width
);
408 #elif defined(PIPE_OS_UNIX)
409 /* write a ppm file */
413 util_snprintf(filename
, sizeof(filename
), "%s.ppm", prefix
);
415 f
= fopen(filename
, "w");
419 const uint8_t *ptr
= (uint8_t *) data
;
421 /* XXX this is a hack */
423 case PIPE_FORMAT_B8G8R8A8_UNORM
:
435 fprintf(f
, "# ppm-file created by osdemo.c\n");
436 fprintf(f
, "%i %i\n", width
, height
);
440 f
= fopen(filename
, "ab"); /* reopen in binary append mode */
441 for (y
= 0; y
< height
; y
++) {
442 for (x
= 0; x
< width
; x
++) {
443 i
= y
* stride
+ x
* cpp
;
444 fputc(ptr
[i
+ r
], f
); /* write red */
445 fputc(ptr
[i
+ g
], f
); /* write green */
446 fputc(ptr
[i
+ b
], f
); /* write blue */
452 fprintf(stderr
, "Can't open %s for writing\n", filename
);
457 /* FIXME: dump resources, not surfaces... */
458 void debug_dump_surface(struct pipe_context
*pipe
,
460 struct pipe_surface
*surface
)
462 struct pipe_resource
*texture
;
463 struct pipe_transfer
*transfer
;
469 /* XXX: this doesn't necessarily work, as the driver may be using
470 * temporary storage for the surface which hasn't been propagated
471 * back into the texture. Need to nail down the semantics of views
472 * and transfers a bit better before we can say if extra work needs
475 texture
= surface
->texture
;
477 transfer
= pipe_get_transfer(pipe
, texture
, surface
->u
.tex
.level
,
478 surface
->u
.tex
.first_layer
,
480 0, 0, surface
->width
, surface
->height
);
482 data
= pipe
->transfer_map(pipe
, transfer
);
486 debug_dump_image(prefix
,
488 util_format_get_blocksize(texture
->format
),
489 util_format_get_nblocksx(texture
->format
, surface
->width
),
490 util_format_get_nblocksy(texture
->format
, surface
->height
),
494 pipe
->transfer_unmap(pipe
, transfer
);
496 pipe
->transfer_destroy(pipe
, transfer
);
500 void debug_dump_texture(struct pipe_context
*pipe
,
502 struct pipe_resource
*texture
)
504 struct pipe_surface
*surface
, surf_tmpl
;
509 /* XXX for now, just dump image for layer=0, level=0 */
510 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
511 u_surface_default_template(&surf_tmpl
, texture
, 0 /* no bind flag - not a surface */);
512 surface
= pipe
->create_surface(pipe
, texture
, &surf_tmpl
);
514 debug_dump_surface(pipe
, prefix
, surface
);
515 pipe
->surface_destroy(pipe
, surface
);
521 struct bmp_file_header
{
524 uint16_t bfReserved1
;
525 uint16_t bfReserved2
;
530 struct bmp_info_header
{
536 uint32_t biCompression
;
537 uint32_t biSizeImage
;
538 int32_t biXPelsPerMeter
;
539 int32_t biYPelsPerMeter
;
541 uint32_t biClrImportant
;
544 struct bmp_rgb_quad
{
552 debug_dump_surface_bmp(struct pipe_context
*pipe
,
553 const char *filename
,
554 struct pipe_surface
*surface
)
556 #ifndef PIPE_SUBSYSTEM_WINDOWS_MINIPORT
557 struct pipe_transfer
*transfer
;
558 struct pipe_resource
*texture
= surface
->texture
;
560 transfer
= pipe_get_transfer(pipe
, texture
, surface
->u
.tex
.level
,
561 surface
->u
.tex
.first_layer
, PIPE_TRANSFER_READ
,
562 0, 0, surface
->width
, surface
->height
);
564 debug_dump_transfer_bmp(pipe
, filename
, transfer
);
566 pipe
->transfer_destroy(pipe
, transfer
);
571 debug_dump_transfer_bmp(struct pipe_context
*pipe
,
572 const char *filename
,
573 struct pipe_transfer
*transfer
)
575 #ifndef PIPE_SUBSYSTEM_WINDOWS_MINIPORT
581 rgba
= MALLOC(transfer
->box
.width
*
582 transfer
->box
.height
*
583 transfer
->box
.depth
*
588 pipe_get_tile_rgba(pipe
, transfer
, 0, 0,
589 transfer
->box
.width
, transfer
->box
.height
,
592 debug_dump_float_rgba_bmp(filename
,
593 transfer
->box
.width
, transfer
->box
.height
,
594 rgba
, transfer
->box
.width
);
603 debug_dump_float_rgba_bmp(const char *filename
,
604 unsigned width
, unsigned height
,
605 float *rgba
, unsigned stride
)
607 #ifndef PIPE_SUBSYSTEM_WINDOWS_MINIPORT
608 struct os_stream
*stream
;
609 struct bmp_file_header bmfh
;
610 struct bmp_info_header bmih
;
616 bmfh
.bfType
= 0x4d42;
617 bmfh
.bfSize
= 14 + 40 + height
*width
*4;
618 bmfh
.bfReserved1
= 0;
619 bmfh
.bfReserved2
= 0;
620 bmfh
.bfOffBits
= 14 + 40;
623 bmih
.biWidth
= width
;
624 bmih
.biHeight
= height
;
626 bmih
.biBitCount
= 32;
627 bmih
.biCompression
= 0;
628 bmih
.biSizeImage
= height
*width
*4;
629 bmih
.biXPelsPerMeter
= 0;
630 bmih
.biYPelsPerMeter
= 0;
632 bmih
.biClrImportant
= 0;
634 stream
= os_file_stream_create(filename
);
638 os_stream_write(stream
, &bmfh
, 14);
639 os_stream_write(stream
, &bmih
, 40);
643 float *ptr
= rgba
+ (stride
* y
* 4);
644 for(x
= 0; x
< width
; ++x
)
646 struct bmp_rgb_quad pixel
;
647 pixel
.rgbRed
= float_to_ubyte(ptr
[x
*4 + 0]);
648 pixel
.rgbGreen
= float_to_ubyte(ptr
[x
*4 + 1]);
649 pixel
.rgbBlue
= float_to_ubyte(ptr
[x
*4 + 2]);
650 pixel
.rgbAlpha
= 255;
651 os_stream_write(stream
, &pixel
, 4);
655 os_stream_close(stream
);