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"
46 void _debug_vprintf(const char *format
, va_list ap
)
48 /* We buffer until we find a newline. */
49 static char buf
[4096] = {'\0'};
50 size_t len
= strlen(buf
);
51 int ret
= util_vsnprintf(buf
+ len
, sizeof(buf
) - len
, format
, ap
);
52 if(ret
> (int)(sizeof(buf
) - len
- 1) || util_strchr(buf
+ len
, '\n')) {
60 void debug_print_blob( const char *name
,
64 const unsigned *ublob
= (const unsigned *)blob
;
67 debug_printf("%s (%d dwords%s)\n", name
, size
/4,
68 size
%4 ? "... plus a few bytes" : "");
70 for (i
= 0; i
< size
/4; i
++) {
71 debug_printf("%d:\t%08x\n", i
, ublob
[i
]);
78 debug_get_option_should_print(void)
80 static boolean first
= TRUE
;
81 static boolean value
= FALSE
;
86 /* Oh hey this will call into this function,
87 * but its cool since we set first to false
90 value
= debug_get_bool_option("GALLIUM_PRINT_OPTIONS", TRUE
);
91 /* XXX should we print this option? Currently it wont */
96 debug_get_option(const char *name
, const char *dfault
)
100 result
= os_get_option(name
);
104 if (debug_get_option_should_print())
105 debug_printf("%s: %s = %s\n", __FUNCTION__
, name
, result
? result
: "(null)");
111 debug_get_bool_option(const char *name
, boolean dfault
)
113 const char *str
= os_get_option(name
);
118 else if(!util_strcmp(str
, "n"))
120 else if(!util_strcmp(str
, "no"))
122 else if(!util_strcmp(str
, "0"))
124 else if(!util_strcmp(str
, "f"))
126 else if(!util_strcmp(str
, "false"))
131 if (debug_get_option_should_print())
132 debug_printf("%s: %s = %s\n", __FUNCTION__
, name
, result
? "TRUE" : "FALSE");
139 debug_get_num_option(const char *name
, long dfault
)
144 str
= os_get_option(name
);
159 while('0' <= c
&& c
<= '9') {
160 result
= result
*10 + (c
- '0');
166 if (debug_get_option_should_print())
167 debug_printf("%s: %s = %li\n", __FUNCTION__
, name
, result
);
174 debug_get_flags_option(const char *name
,
175 const struct debug_named_value
*flags
,
176 unsigned long dfault
)
178 unsigned long result
;
181 str
= os_get_option(name
);
184 else if (!util_strcmp(str
, "help")) {
186 while (flags
->name
) {
187 debug_printf("%s: help for %s: %s [0x%lx]\n", __FUNCTION__
, name
, flags
->name
, flags
->value
);
193 while( flags
->name
) {
194 if (!util_strcmp(str
, "all") || util_strstr(str
, flags
->name
))
195 result
|= flags
->value
;
200 if (debug_get_option_should_print()) {
202 debug_printf("%s: %s = 0x%lx (%s)\n", __FUNCTION__
, name
, result
, str
);
204 debug_printf("%s: %s = 0x%lx\n", __FUNCTION__
, name
, result
);
212 void _debug_assert_fail(const char *expr
,
215 const char *function
)
217 _debug_printf("%s:%u:%s: Assertion `%s' failed.\n", file
, line
, function
, expr
);
218 #if defined(PIPE_OS_WINDOWS) && !defined(PIPE_SUBSYSTEM_WINDOWS_USER)
219 if (debug_get_bool_option("GALLIUM_ABORT_ON_ASSERT", FALSE
))
221 if (debug_get_bool_option("GALLIUM_ABORT_ON_ASSERT", TRUE
))
225 _debug_printf("continuing...\n");
230 debug_dump_enum(const struct debug_named_value
*names
,
233 static char rest
[64];
236 if(names
->value
== value
)
241 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
247 debug_dump_enum_noprefix(const struct debug_named_value
*names
,
251 static char rest
[64];
254 if(names
->value
== value
) {
255 const char *name
= names
->name
;
256 while (*name
== *prefix
) {
267 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
273 debug_dump_flags(const struct debug_named_value
*names
,
276 static char output
[4096];
277 static char rest
[256];
283 if((names
->value
& value
) == names
->value
) {
285 util_strncat(output
, "|", sizeof(output
));
288 util_strncat(output
, names
->name
, sizeof(output
) - 1);
289 output
[sizeof(output
) - 1] = '\0';
290 value
&= ~names
->value
;
297 util_strncat(output
, "|", sizeof(output
));
301 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
302 util_strncat(output
, rest
, sizeof(output
) - 1);
303 output
[sizeof(output
) - 1] = '\0';
314 void debug_print_format(const char *msg
, unsigned fmt
)
316 debug_printf("%s: %s\n", msg
, util_format_name(fmt
));
322 static const struct debug_named_value pipe_prim_names
[] = {
324 DEBUG_NAMED_VALUE(PIPE_PRIM_POINTS
),
325 DEBUG_NAMED_VALUE(PIPE_PRIM_LINES
),
326 DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_LOOP
),
327 DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_STRIP
),
328 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLES
),
329 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_STRIP
),
330 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_FAN
),
331 DEBUG_NAMED_VALUE(PIPE_PRIM_QUADS
),
332 DEBUG_NAMED_VALUE(PIPE_PRIM_QUAD_STRIP
),
333 DEBUG_NAMED_VALUE(PIPE_PRIM_POLYGON
),
335 DEBUG_NAMED_VALUE_END
339 const char *u_prim_name( unsigned prim
)
341 return debug_dump_enum(pipe_prim_names
, prim
);
349 * Dump an image to a .raw or .ppm file (depends on OS).
350 * \param format PIPE_FORMAT_x
351 * \param cpp bytes per pixel
352 * \param width width in pixels
353 * \param height height in pixels
354 * \param stride row stride in bytes
356 void debug_dump_image(const char *prefix
,
357 unsigned format
, unsigned cpp
,
358 unsigned width
, unsigned height
,
362 #ifdef PIPE_SUBSYSTEM_WINDOWS_DISPLAY
363 static unsigned no
= 0;
365 WCHAR wfilename
[sizeof(filename
)];
373 unsigned char *pMap
= NULL
;
376 util_snprintf(filename
, sizeof(filename
), "\\??\\c:\\%03u%s.raw", ++no
, prefix
);
377 for(i
= 0; i
< sizeof(filename
); ++i
)
378 wfilename
[i
] = (WCHAR
)filename
[i
];
380 pMap
= (unsigned char *)EngMapFile(wfilename
, sizeof(header
) + height
*width
*cpp
, &iFile
);
384 header
.format
= format
;
386 header
.width
= width
;
387 header
.height
= height
;
388 memcpy(pMap
, &header
, sizeof(header
));
389 pMap
+= sizeof(header
);
391 for(i
= 0; i
< height
; ++i
) {
392 memcpy(pMap
, (unsigned char *)data
+ stride
*i
, cpp
*width
);
397 #elif defined(PIPE_OS_UNIX)
398 /* write a ppm file */
402 util_snprintf(filename
, sizeof(filename
), "%s.ppm", prefix
);
404 f
= fopen(filename
, "w");
408 const uint8_t *ptr
= (uint8_t *) data
;
410 /* XXX this is a hack */
412 case PIPE_FORMAT_B8G8R8A8_UNORM
:
424 fprintf(f
, "# ppm-file created by osdemo.c\n");
425 fprintf(f
, "%i %i\n", width
, height
);
429 f
= fopen(filename
, "ab"); /* reopen in binary append mode */
430 for (y
= 0; y
< height
; y
++) {
431 for (x
= 0; x
< width
; x
++) {
432 i
= y
* stride
+ x
* cpp
;
433 fputc(ptr
[i
+ r
], f
); /* write red */
434 fputc(ptr
[i
+ g
], f
); /* write green */
435 fputc(ptr
[i
+ b
], f
); /* write blue */
441 fprintf(stderr
, "Can't open %s for writing\n", filename
);
446 void debug_dump_surface(struct pipe_context
*pipe
,
448 struct pipe_surface
*surface
)
450 struct pipe_resource
*texture
;
451 struct pipe_transfer
*transfer
;
457 /* XXX: this doesn't necessarily work, as the driver may be using
458 * temporary storage for the surface which hasn't been propagated
459 * back into the texture. Need to nail down the semantics of views
460 * and transfers a bit better before we can say if extra work needs
463 texture
= surface
->texture
;
465 transfer
= pipe_get_transfer(pipe
, texture
, surface
->face
,
466 surface
->level
, surface
->zslice
,
467 PIPE_TRANSFER_READ
, 0, 0, surface
->width
,
470 data
= pipe
->transfer_map(pipe
, transfer
);
474 debug_dump_image(prefix
,
476 util_format_get_blocksize(texture
->format
),
477 util_format_get_nblocksx(texture
->format
, surface
->width
),
478 util_format_get_nblocksy(texture
->format
, surface
->height
),
482 pipe
->transfer_unmap(pipe
, transfer
);
484 pipe
->transfer_destroy(pipe
, transfer
);
488 void debug_dump_texture(struct pipe_context
*pipe
,
490 struct pipe_resource
*texture
)
492 struct pipe_surface
*surface
;
493 struct pipe_screen
*screen
;
498 screen
= texture
->screen
;
500 /* XXX for now, just dump image for face=0, level=0 */
501 surface
= screen
->get_tex_surface(screen
, texture
, 0, 0, 0,
502 PIPE_BIND_SAMPLER_VIEW
);
504 debug_dump_surface(pipe
, prefix
, surface
);
505 screen
->tex_surface_destroy(surface
);
511 struct bmp_file_header
{
514 uint16_t bfReserved1
;
515 uint16_t bfReserved2
;
520 struct bmp_info_header
{
526 uint32_t biCompression
;
527 uint32_t biSizeImage
;
528 int32_t biXPelsPerMeter
;
529 int32_t biYPelsPerMeter
;
531 uint32_t biClrImportant
;
534 struct bmp_rgb_quad
{
542 debug_dump_surface_bmp(struct pipe_context
*pipe
,
543 const char *filename
,
544 struct pipe_surface
*surface
)
546 #ifndef PIPE_SUBSYSTEM_WINDOWS_MINIPORT
547 struct pipe_transfer
*transfer
;
548 struct pipe_resource
*texture
= surface
->texture
;
550 transfer
= pipe_get_transfer(pipe
, texture
, surface
->face
,
551 surface
->level
, surface
->zslice
,
552 PIPE_TRANSFER_READ
, 0, 0, surface
->width
,
555 debug_dump_transfer_bmp(pipe
, filename
, transfer
);
557 pipe
->transfer_destroy(pipe
, transfer
);
562 debug_dump_transfer_bmp(struct pipe_context
*pipe
,
563 const char *filename
,
564 struct pipe_transfer
*transfer
)
566 #ifndef PIPE_SUBSYSTEM_WINDOWS_MINIPORT
572 rgba
= MALLOC(transfer
->box
.width
*
573 transfer
->box
.height
*
574 transfer
->box
.depth
*
579 pipe_get_tile_rgba(pipe
, transfer
, 0, 0,
580 transfer
->box
.width
, transfer
->box
.height
,
583 debug_dump_float_rgba_bmp(filename
,
584 transfer
->box
.width
, transfer
->box
.height
,
585 rgba
, transfer
->box
.width
);
594 debug_dump_float_rgba_bmp(const char *filename
,
595 unsigned width
, unsigned height
,
596 float *rgba
, unsigned stride
)
598 #ifndef PIPE_SUBSYSTEM_WINDOWS_MINIPORT
599 struct os_stream
*stream
;
600 struct bmp_file_header bmfh
;
601 struct bmp_info_header bmih
;
607 bmfh
.bfType
= 0x4d42;
608 bmfh
.bfSize
= 14 + 40 + height
*width
*4;
609 bmfh
.bfReserved1
= 0;
610 bmfh
.bfReserved2
= 0;
611 bmfh
.bfOffBits
= 14 + 40;
614 bmih
.biWidth
= width
;
615 bmih
.biHeight
= height
;
617 bmih
.biBitCount
= 32;
618 bmih
.biCompression
= 0;
619 bmih
.biSizeImage
= height
*width
*4;
620 bmih
.biXPelsPerMeter
= 0;
621 bmih
.biYPelsPerMeter
= 0;
623 bmih
.biClrImportant
= 0;
625 stream
= os_file_stream_create(filename
);
629 os_stream_write(stream
, &bmfh
, 14);
630 os_stream_write(stream
, &bmih
, 40);
634 float *ptr
= rgba
+ (stride
* y
* 4);
635 for(x
= 0; x
< width
; ++x
)
637 struct bmp_rgb_quad pixel
;
638 pixel
.rgbRed
= float_to_ubyte(ptr
[x
*4 + 0]);
639 pixel
.rgbGreen
= float_to_ubyte(ptr
[x
*4 + 1]);
640 pixel
.rgbBlue
= float_to_ubyte(ptr
[x
*4 + 2]);
641 pixel
.rgbAlpha
= 255;
642 os_stream_write(stream
, &pixel
, 4);
646 os_stream_close(stream
);