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(const char *name
, const char *dfault
)
82 result
= os_get_option(name
);
86 debug_printf("%s: %s = %s\n", __FUNCTION__
, name
, result
? result
: "(null)");
92 debug_get_bool_option(const char *name
, boolean dfault
)
94 const char *str
= os_get_option(name
);
99 else if(!util_strcmp(str
, "n"))
101 else if(!util_strcmp(str
, "no"))
103 else if(!util_strcmp(str
, "0"))
105 else if(!util_strcmp(str
, "f"))
107 else if(!util_strcmp(str
, "false"))
112 debug_printf("%s: %s = %s\n", __FUNCTION__
, name
, result
? "TRUE" : "FALSE");
119 debug_get_num_option(const char *name
, long dfault
)
124 str
= os_get_option(name
);
139 while('0' <= c
&& c
<= '9') {
140 result
= result
*10 + (c
- '0');
146 debug_printf("%s: %s = %li\n", __FUNCTION__
, name
, result
);
153 debug_get_flags_option(const char *name
,
154 const struct debug_named_value
*flags
,
155 unsigned long dfault
)
157 unsigned long result
;
160 str
= os_get_option(name
);
163 else if (!util_strcmp(str
, "help")) {
165 while (flags
->name
) {
166 debug_printf("%s: help for %s: %s [0x%lx]\n", __FUNCTION__
, name
, flags
->name
, flags
->value
);
172 while( flags
->name
) {
173 if (!util_strcmp(str
, "all") || util_strstr(str
, flags
->name
))
174 result
|= flags
->value
;
180 debug_printf("%s: %s = 0x%lx (%s)\n", __FUNCTION__
, name
, result
, str
);
183 debug_printf("%s: %s = 0x%lx\n", __FUNCTION__
, name
, result
);
190 void _debug_assert_fail(const char *expr
,
193 const char *function
)
195 _debug_printf("%s:%u:%s: Assertion `%s' failed.\n", file
, line
, function
, expr
);
196 #if defined(PIPE_OS_WINDOWS) && !defined(PIPE_SUBSYSTEM_WINDOWS_USER)
197 if (debug_get_bool_option("GALLIUM_ABORT_ON_ASSERT", FALSE
))
199 if (debug_get_bool_option("GALLIUM_ABORT_ON_ASSERT", TRUE
))
203 _debug_printf("continuing...\n");
208 debug_dump_enum(const struct debug_named_value
*names
,
211 static char rest
[64];
214 if(names
->value
== value
)
219 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
225 debug_dump_enum_noprefix(const struct debug_named_value
*names
,
229 static char rest
[64];
232 if(names
->value
== value
) {
233 const char *name
= names
->name
;
234 while (*name
== *prefix
) {
245 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
251 debug_dump_flags(const struct debug_named_value
*names
,
254 static char output
[4096];
255 static char rest
[256];
261 if((names
->value
& value
) == names
->value
) {
263 util_strncat(output
, "|", sizeof(output
));
266 util_strncat(output
, names
->name
, sizeof(output
) - 1);
267 output
[sizeof(output
) - 1] = '\0';
268 value
&= ~names
->value
;
275 util_strncat(output
, "|", sizeof(output
));
279 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
280 util_strncat(output
, rest
, sizeof(output
) - 1);
281 output
[sizeof(output
) - 1] = '\0';
292 void debug_print_format(const char *msg
, unsigned fmt
)
294 debug_printf("%s: %s\n", msg
, util_format_name(fmt
));
300 static const struct debug_named_value pipe_prim_names
[] = {
302 DEBUG_NAMED_VALUE(PIPE_PRIM_POINTS
),
303 DEBUG_NAMED_VALUE(PIPE_PRIM_LINES
),
304 DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_LOOP
),
305 DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_STRIP
),
306 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLES
),
307 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_STRIP
),
308 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_FAN
),
309 DEBUG_NAMED_VALUE(PIPE_PRIM_QUADS
),
310 DEBUG_NAMED_VALUE(PIPE_PRIM_QUAD_STRIP
),
311 DEBUG_NAMED_VALUE(PIPE_PRIM_POLYGON
),
313 DEBUG_NAMED_VALUE_END
317 const char *u_prim_name( unsigned prim
)
319 return debug_dump_enum(pipe_prim_names
, prim
);
327 * Dump an image to a .raw or .ppm file (depends on OS).
328 * \param format PIPE_FORMAT_x
329 * \param cpp bytes per pixel
330 * \param width width in pixels
331 * \param height height in pixels
332 * \param stride row stride in bytes
334 void debug_dump_image(const char *prefix
,
335 unsigned format
, unsigned cpp
,
336 unsigned width
, unsigned height
,
340 #ifdef PIPE_SUBSYSTEM_WINDOWS_DISPLAY
341 static unsigned no
= 0;
343 WCHAR wfilename
[sizeof(filename
)];
351 unsigned char *pMap
= NULL
;
354 util_snprintf(filename
, sizeof(filename
), "\\??\\c:\\%03u%s.raw", ++no
, prefix
);
355 for(i
= 0; i
< sizeof(filename
); ++i
)
356 wfilename
[i
] = (WCHAR
)filename
[i
];
358 pMap
= (unsigned char *)EngMapFile(wfilename
, sizeof(header
) + height
*width
*cpp
, &iFile
);
362 header
.format
= format
;
364 header
.width
= width
;
365 header
.height
= height
;
366 memcpy(pMap
, &header
, sizeof(header
));
367 pMap
+= sizeof(header
);
369 for(i
= 0; i
< height
; ++i
) {
370 memcpy(pMap
, (unsigned char *)data
+ stride
*i
, cpp
*width
);
375 #elif defined(PIPE_OS_UNIX)
376 /* write a ppm file */
380 util_snprintf(filename
, sizeof(filename
), "%s.ppm", prefix
);
382 f
= fopen(filename
, "w");
386 const uint8_t *ptr
= (uint8_t *) data
;
388 /* XXX this is a hack */
390 case PIPE_FORMAT_B8G8R8A8_UNORM
:
402 fprintf(f
, "# ppm-file created by osdemo.c\n");
403 fprintf(f
, "%i %i\n", width
, height
);
407 f
= fopen(filename
, "ab"); /* reopen in binary append mode */
408 for (y
= 0; y
< height
; y
++) {
409 for (x
= 0; x
< width
; x
++) {
410 i
= y
* stride
+ x
* cpp
;
411 fputc(ptr
[i
+ r
], f
); /* write red */
412 fputc(ptr
[i
+ g
], f
); /* write green */
413 fputc(ptr
[i
+ b
], f
); /* write blue */
419 fprintf(stderr
, "Can't open %s for writing\n", filename
);
424 void debug_dump_surface(const char *prefix
,
425 struct pipe_surface
*surface
)
427 struct pipe_texture
*texture
;
428 struct pipe_screen
*screen
;
429 struct pipe_transfer
*transfer
;
435 texture
= surface
->texture
;
436 screen
= texture
->screen
;
438 transfer
= screen
->get_tex_transfer(screen
, texture
, surface
->face
,
439 surface
->level
, surface
->zslice
,
440 PIPE_TRANSFER_READ
, 0, 0, surface
->width
,
443 data
= screen
->transfer_map(screen
, transfer
);
447 debug_dump_image(prefix
,
449 util_format_get_blocksize(texture
->format
),
450 util_format_get_nblocksx(texture
->format
, transfer
->width
),
451 util_format_get_nblocksy(texture
->format
, transfer
->height
),
455 screen
->transfer_unmap(screen
, transfer
);
457 screen
->tex_transfer_destroy(transfer
);
461 void debug_dump_texture(const char *prefix
,
462 struct pipe_texture
*texture
)
464 struct pipe_surface
*surface
;
465 struct pipe_screen
*screen
;
470 screen
= texture
->screen
;
472 /* XXX for now, just dump image for face=0, level=0 */
473 surface
= screen
->get_tex_surface(screen
, texture
, 0, 0, 0,
474 PIPE_TEXTURE_USAGE_SAMPLER
);
476 debug_dump_surface(prefix
, surface
);
477 screen
->tex_surface_destroy(surface
);
483 struct bmp_file_header
{
486 uint16_t bfReserved1
;
487 uint16_t bfReserved2
;
492 struct bmp_info_header
{
498 uint32_t biCompression
;
499 uint32_t biSizeImage
;
500 int32_t biXPelsPerMeter
;
501 int32_t biYPelsPerMeter
;
503 uint32_t biClrImportant
;
506 struct bmp_rgb_quad
{
514 debug_dump_surface_bmp(const char *filename
,
515 struct pipe_surface
*surface
)
517 #ifndef PIPE_SUBSYSTEM_WINDOWS_MINIPORT
518 struct pipe_transfer
*transfer
;
519 struct pipe_texture
*texture
= surface
->texture
;
520 struct pipe_screen
*screen
= texture
->screen
;
522 transfer
= screen
->get_tex_transfer(screen
, texture
, surface
->face
,
523 surface
->level
, surface
->zslice
,
524 PIPE_TRANSFER_READ
, 0, 0, surface
->width
,
527 debug_dump_transfer_bmp(filename
, transfer
);
529 screen
->tex_transfer_destroy(transfer
);
534 debug_dump_transfer_bmp(const char *filename
,
535 struct pipe_transfer
*transfer
)
537 #ifndef PIPE_SUBSYSTEM_WINDOWS_MINIPORT
543 rgba
= MALLOC(transfer
->width
*transfer
->height
*4*sizeof(float));
547 pipe_get_tile_rgba(transfer
, 0, 0,
548 transfer
->width
, transfer
->height
,
551 debug_dump_float_rgba_bmp(filename
,
552 transfer
->width
, transfer
->height
,
553 rgba
, transfer
->width
);
562 debug_dump_float_rgba_bmp(const char *filename
,
563 unsigned width
, unsigned height
,
564 float *rgba
, unsigned stride
)
566 #ifndef PIPE_SUBSYSTEM_WINDOWS_MINIPORT
567 struct os_stream
*stream
;
568 struct bmp_file_header bmfh
;
569 struct bmp_info_header bmih
;
575 bmfh
.bfType
= 0x4d42;
576 bmfh
.bfSize
= 14 + 40 + height
*width
*4;
577 bmfh
.bfReserved1
= 0;
578 bmfh
.bfReserved2
= 0;
579 bmfh
.bfOffBits
= 14 + 40;
582 bmih
.biWidth
= width
;
583 bmih
.biHeight
= height
;
585 bmih
.biBitCount
= 32;
586 bmih
.biCompression
= 0;
587 bmih
.biSizeImage
= height
*width
*4;
588 bmih
.biXPelsPerMeter
= 0;
589 bmih
.biYPelsPerMeter
= 0;
591 bmih
.biClrImportant
= 0;
593 stream
= os_file_stream_create(filename
);
597 os_stream_write(stream
, &bmfh
, 14);
598 os_stream_write(stream
, &bmih
, 40);
602 float *ptr
= rgba
+ (stride
* y
* 4);
603 for(x
= 0; x
< width
; ++x
)
605 struct bmp_rgb_quad pixel
;
606 pixel
.rgbRed
= float_to_ubyte(ptr
[x
*4 + 0]);
607 pixel
.rgbGreen
= float_to_ubyte(ptr
[x
*4 + 1]);
608 pixel
.rgbBlue
= float_to_ubyte(ptr
[x
*4 + 2]);
609 pixel
.rgbAlpha
= 255;
610 os_stream_write(stream
, &pixel
, 4);
614 os_stream_close(stream
);