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"
45 #include <limits.h> /* CHAR_BIT */
47 void _debug_vprintf(const char *format
, va_list ap
)
49 /* We buffer until we find a newline. */
50 static char buf
[4096] = {'\0'};
51 size_t len
= strlen(buf
);
52 int ret
= util_vsnprintf(buf
+ len
, sizeof(buf
) - len
, format
, ap
);
53 if(ret
> (int)(sizeof(buf
) - len
- 1) || util_strchr(buf
+ len
, '\n')) {
61 void debug_print_blob( const char *name
,
65 const unsigned *ublob
= (const unsigned *)blob
;
68 debug_printf("%s (%d dwords%s)\n", name
, size
/4,
69 size
%4 ? "... plus a few bytes" : "");
71 for (i
= 0; i
< size
/4; i
++) {
72 debug_printf("%d:\t%08x\n", i
, ublob
[i
]);
79 debug_get_option_should_print(void)
81 static boolean first
= TRUE
;
82 static boolean value
= FALSE
;
87 /* Oh hey this will call into this function,
88 * but its cool since we set first to false
91 value
= debug_get_bool_option("GALLIUM_PRINT_OPTIONS", TRUE
);
92 /* XXX should we print this option? Currently it wont */
97 debug_get_option(const char *name
, const char *dfault
)
101 result
= os_get_option(name
);
105 if (debug_get_option_should_print())
106 debug_printf("%s: %s = %s\n", __FUNCTION__
, name
, result
? result
: "(null)");
112 debug_get_bool_option(const char *name
, boolean dfault
)
114 const char *str
= os_get_option(name
);
119 else if(!util_strcmp(str
, "n"))
121 else if(!util_strcmp(str
, "no"))
123 else if(!util_strcmp(str
, "0"))
125 else if(!util_strcmp(str
, "f"))
127 else if(!util_strcmp(str
, "F"))
129 else if(!util_strcmp(str
, "false"))
131 else if(!util_strcmp(str
, "FALSE"))
136 if (debug_get_option_should_print())
137 debug_printf("%s: %s = %s\n", __FUNCTION__
, name
, result
? "TRUE" : "FALSE");
144 debug_get_num_option(const char *name
, long dfault
)
149 str
= os_get_option(name
);
164 while('0' <= c
&& c
<= '9') {
165 result
= result
*10 + (c
- '0');
171 if (debug_get_option_should_print())
172 debug_printf("%s: %s = %li\n", __FUNCTION__
, name
, result
);
179 debug_get_flags_option(const char *name
,
180 const struct debug_named_value
*flags
,
181 unsigned long dfault
)
183 unsigned long result
;
185 const struct debug_named_value
*orig
= flags
;
188 str
= os_get_option(name
);
191 else if (!util_strcmp(str
, "help")) {
193 debug_printf("%s: help for %s:\n", __FUNCTION__
, name
);
194 for (; flags
->name
; ++flags
)
195 namealign
= MAX2(namealign
, strlen(flags
->name
));
196 for (flags
= orig
; flags
->name
; ++flags
)
197 debug_printf("| %*s [0x%0*lx]%s%s\n", namealign
, flags
->name
,
198 sizeof(unsigned long)*CHAR_BIT
/4, flags
->value
,
199 flags
->desc
? " " : "", flags
->desc
? flags
->desc
: "");
203 while( flags
->name
) {
204 if (!util_strcmp(str
, "all") || util_strstr(str
, flags
->name
))
205 result
|= flags
->value
;
210 if (debug_get_option_should_print()) {
212 debug_printf("%s: %s = 0x%lx (%s)\n", __FUNCTION__
, name
, result
, str
);
214 debug_printf("%s: %s = 0x%lx\n", __FUNCTION__
, name
, result
);
222 void _debug_assert_fail(const char *expr
,
225 const char *function
)
227 _debug_printf("%s:%u:%s: Assertion `%s' failed.\n", file
, line
, function
, expr
);
228 #if defined(PIPE_OS_WINDOWS) && !defined(PIPE_SUBSYSTEM_WINDOWS_USER)
229 if (debug_get_bool_option("GALLIUM_ABORT_ON_ASSERT", FALSE
))
231 if (debug_get_bool_option("GALLIUM_ABORT_ON_ASSERT", TRUE
))
235 _debug_printf("continuing...\n");
240 debug_dump_enum(const struct debug_named_value
*names
,
243 static char rest
[64];
246 if(names
->value
== value
)
251 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
257 debug_dump_enum_noprefix(const struct debug_named_value
*names
,
261 static char rest
[64];
264 if(names
->value
== value
) {
265 const char *name
= names
->name
;
266 while (*name
== *prefix
) {
277 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
283 debug_dump_flags(const struct debug_named_value
*names
,
286 static char output
[4096];
287 static char rest
[256];
293 if((names
->value
& value
) == names
->value
) {
295 util_strncat(output
, "|", sizeof(output
));
298 util_strncat(output
, names
->name
, sizeof(output
) - 1);
299 output
[sizeof(output
) - 1] = '\0';
300 value
&= ~names
->value
;
307 util_strncat(output
, "|", sizeof(output
));
311 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
312 util_strncat(output
, rest
, sizeof(output
) - 1);
313 output
[sizeof(output
) - 1] = '\0';
324 void debug_print_format(const char *msg
, unsigned fmt
)
326 debug_printf("%s: %s\n", msg
, util_format_name(fmt
));
332 static const struct debug_named_value pipe_prim_names
[] = {
334 DEBUG_NAMED_VALUE(PIPE_PRIM_POINTS
),
335 DEBUG_NAMED_VALUE(PIPE_PRIM_LINES
),
336 DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_LOOP
),
337 DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_STRIP
),
338 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLES
),
339 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_STRIP
),
340 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_FAN
),
341 DEBUG_NAMED_VALUE(PIPE_PRIM_QUADS
),
342 DEBUG_NAMED_VALUE(PIPE_PRIM_QUAD_STRIP
),
343 DEBUG_NAMED_VALUE(PIPE_PRIM_POLYGON
),
345 DEBUG_NAMED_VALUE_END
349 const char *u_prim_name( unsigned prim
)
351 return debug_dump_enum(pipe_prim_names
, prim
);
359 * Dump an image to a .raw or .ppm file (depends on OS).
360 * \param format PIPE_FORMAT_x
361 * \param cpp bytes per pixel
362 * \param width width in pixels
363 * \param height height in pixels
364 * \param stride row stride in bytes
366 void debug_dump_image(const char *prefix
,
367 unsigned format
, unsigned cpp
,
368 unsigned width
, unsigned height
,
372 #ifdef PIPE_SUBSYSTEM_WINDOWS_DISPLAY
373 static unsigned no
= 0;
375 WCHAR wfilename
[sizeof(filename
)];
383 unsigned char *pMap
= NULL
;
386 util_snprintf(filename
, sizeof(filename
), "\\??\\c:\\%03u%s.raw", ++no
, prefix
);
387 for(i
= 0; i
< sizeof(filename
); ++i
)
388 wfilename
[i
] = (WCHAR
)filename
[i
];
390 pMap
= (unsigned char *)EngMapFile(wfilename
, sizeof(header
) + height
*width
*cpp
, &iFile
);
394 header
.format
= format
;
396 header
.width
= width
;
397 header
.height
= height
;
398 memcpy(pMap
, &header
, sizeof(header
));
399 pMap
+= sizeof(header
);
401 for(i
= 0; i
< height
; ++i
) {
402 memcpy(pMap
, (unsigned char *)data
+ stride
*i
, cpp
*width
);
407 #elif defined(PIPE_OS_UNIX)
408 /* write a ppm file */
412 util_snprintf(filename
, sizeof(filename
), "%s.ppm", prefix
);
414 f
= fopen(filename
, "w");
418 const uint8_t *ptr
= (uint8_t *) data
;
420 /* XXX this is a hack */
422 case PIPE_FORMAT_B8G8R8A8_UNORM
:
434 fprintf(f
, "# ppm-file created by osdemo.c\n");
435 fprintf(f
, "%i %i\n", width
, height
);
439 f
= fopen(filename
, "ab"); /* reopen in binary append mode */
440 for (y
= 0; y
< height
; y
++) {
441 for (x
= 0; x
< width
; x
++) {
442 i
= y
* stride
+ x
* cpp
;
443 fputc(ptr
[i
+ r
], f
); /* write red */
444 fputc(ptr
[i
+ g
], f
); /* write green */
445 fputc(ptr
[i
+ b
], f
); /* write blue */
451 fprintf(stderr
, "Can't open %s for writing\n", filename
);
456 void debug_dump_surface(struct pipe_context
*pipe
,
458 struct pipe_surface
*surface
)
460 struct pipe_resource
*texture
;
461 struct pipe_transfer
*transfer
;
467 /* XXX: this doesn't necessarily work, as the driver may be using
468 * temporary storage for the surface which hasn't been propagated
469 * back into the texture. Need to nail down the semantics of views
470 * and transfers a bit better before we can say if extra work needs
473 texture
= surface
->texture
;
475 transfer
= pipe_get_transfer(pipe
, texture
, surface
->face
,
476 surface
->level
, surface
->zslice
,
477 PIPE_TRANSFER_READ
, 0, 0, surface
->width
,
480 data
= pipe
->transfer_map(pipe
, transfer
);
484 debug_dump_image(prefix
,
486 util_format_get_blocksize(texture
->format
),
487 util_format_get_nblocksx(texture
->format
, surface
->width
),
488 util_format_get_nblocksy(texture
->format
, surface
->height
),
492 pipe
->transfer_unmap(pipe
, transfer
);
494 pipe
->transfer_destroy(pipe
, transfer
);
498 void debug_dump_texture(struct pipe_context
*pipe
,
500 struct pipe_resource
*texture
)
502 struct pipe_surface
*surface
;
503 struct pipe_screen
*screen
;
508 screen
= texture
->screen
;
510 /* XXX for now, just dump image for face=0, level=0 */
511 surface
= screen
->get_tex_surface(screen
, texture
, 0, 0, 0,
512 PIPE_BIND_SAMPLER_VIEW
);
514 debug_dump_surface(pipe
, prefix
, surface
);
515 screen
->tex_surface_destroy(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
->face
,
561 surface
->level
, surface
->zslice
,
562 PIPE_TRANSFER_READ
, 0, 0, surface
->width
,
565 debug_dump_transfer_bmp(pipe
, filename
, transfer
);
567 pipe
->transfer_destroy(pipe
, transfer
);
572 debug_dump_transfer_bmp(struct pipe_context
*pipe
,
573 const char *filename
,
574 struct pipe_transfer
*transfer
)
576 #ifndef PIPE_SUBSYSTEM_WINDOWS_MINIPORT
582 rgba
= MALLOC(transfer
->box
.width
*
583 transfer
->box
.height
*
584 transfer
->box
.depth
*
589 pipe_get_tile_rgba(pipe
, transfer
, 0, 0,
590 transfer
->box
.width
, transfer
->box
.height
,
593 debug_dump_float_rgba_bmp(filename
,
594 transfer
->box
.width
, transfer
->box
.height
,
595 rgba
, transfer
->box
.width
);
604 debug_dump_float_rgba_bmp(const char *filename
,
605 unsigned width
, unsigned height
,
606 float *rgba
, unsigned stride
)
608 #ifndef PIPE_SUBSYSTEM_WINDOWS_MINIPORT
609 struct os_stream
*stream
;
610 struct bmp_file_header bmfh
;
611 struct bmp_info_header bmih
;
617 bmfh
.bfType
= 0x4d42;
618 bmfh
.bfSize
= 14 + 40 + height
*width
*4;
619 bmfh
.bfReserved1
= 0;
620 bmfh
.bfReserved2
= 0;
621 bmfh
.bfOffBits
= 14 + 40;
624 bmih
.biWidth
= width
;
625 bmih
.biHeight
= height
;
627 bmih
.biBitCount
= 32;
628 bmih
.biCompression
= 0;
629 bmih
.biSizeImage
= height
*width
*4;
630 bmih
.biXPelsPerMeter
= 0;
631 bmih
.biYPelsPerMeter
= 0;
633 bmih
.biClrImportant
= 0;
635 stream
= os_file_stream_create(filename
);
639 os_stream_write(stream
, &bmfh
, 14);
640 os_stream_write(stream
, &bmih
, 40);
644 float *ptr
= rgba
+ (stride
* y
* 4);
645 for(x
= 0; x
< width
; ++x
)
647 struct bmp_rgb_quad pixel
;
648 pixel
.rgbRed
= float_to_ubyte(ptr
[x
*4 + 0]);
649 pixel
.rgbGreen
= float_to_ubyte(ptr
[x
*4 + 1]);
650 pixel
.rgbBlue
= float_to_ubyte(ptr
[x
*4 + 2]);
651 pixel
.rgbAlpha
= 255;
652 os_stream_write(stream
, &pixel
, 4);
656 os_stream_close(stream
);