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(struct pipe_context
*pipe
,
426 struct pipe_surface
*surface
)
428 struct pipe_resource
*texture
;
429 struct pipe_transfer
*transfer
;
435 /* XXX: this doesn't necessarily work, as the driver may be using
436 * temporary storage for the surface which hasn't been propagated
437 * back into the texture. Need to nail down the semantics of views
438 * and transfers a bit better before we can say if extra work needs
441 texture
= surface
->texture
;
443 transfer
= pipe_get_transfer(pipe
, texture
, surface
->face
,
444 surface
->level
, surface
->zslice
,
445 PIPE_TRANSFER_READ
, 0, 0, surface
->width
,
448 data
= pipe
->transfer_map(pipe
, transfer
);
452 debug_dump_image(prefix
,
454 util_format_get_blocksize(texture
->format
),
455 util_format_get_nblocksx(texture
->format
, surface
->width
),
456 util_format_get_nblocksy(texture
->format
, surface
->height
),
460 pipe
->transfer_unmap(pipe
, transfer
);
462 pipe
->transfer_destroy(pipe
, transfer
);
466 void debug_dump_texture(struct pipe_context
*pipe
,
468 struct pipe_resource
*texture
)
470 struct pipe_surface
*surface
;
471 struct pipe_screen
*screen
;
476 screen
= texture
->screen
;
478 /* XXX for now, just dump image for face=0, level=0 */
479 surface
= screen
->get_tex_surface(screen
, texture
, 0, 0, 0,
480 PIPE_BIND_SAMPLER_VIEW
);
482 debug_dump_surface(pipe
, prefix
, surface
);
483 screen
->tex_surface_destroy(surface
);
489 struct bmp_file_header
{
492 uint16_t bfReserved1
;
493 uint16_t bfReserved2
;
498 struct bmp_info_header
{
504 uint32_t biCompression
;
505 uint32_t biSizeImage
;
506 int32_t biXPelsPerMeter
;
507 int32_t biYPelsPerMeter
;
509 uint32_t biClrImportant
;
512 struct bmp_rgb_quad
{
520 debug_dump_surface_bmp(struct pipe_context
*pipe
,
521 const char *filename
,
522 struct pipe_surface
*surface
)
524 #ifndef PIPE_SUBSYSTEM_WINDOWS_MINIPORT
525 struct pipe_transfer
*transfer
;
526 struct pipe_resource
*texture
= surface
->texture
;
528 transfer
= pipe_get_transfer(pipe
, texture
, surface
->face
,
529 surface
->level
, surface
->zslice
,
530 PIPE_TRANSFER_READ
, 0, 0, surface
->width
,
533 debug_dump_transfer_bmp(pipe
, filename
, transfer
);
535 pipe
->transfer_destroy(pipe
, transfer
);
540 debug_dump_transfer_bmp(struct pipe_context
*pipe
,
541 const char *filename
,
542 struct pipe_transfer
*transfer
)
544 #ifndef PIPE_SUBSYSTEM_WINDOWS_MINIPORT
550 rgba
= MALLOC(transfer
->box
.width
*
551 transfer
->box
.height
*
552 transfer
->box
.depth
*
557 pipe_get_tile_rgba(pipe
, transfer
, 0, 0,
558 transfer
->box
.width
, transfer
->box
.height
,
561 debug_dump_float_rgba_bmp(filename
,
562 transfer
->box
.width
, transfer
->box
.height
,
563 rgba
, transfer
->box
.width
);
572 debug_dump_float_rgba_bmp(const char *filename
,
573 unsigned width
, unsigned height
,
574 float *rgba
, unsigned stride
)
576 #ifndef PIPE_SUBSYSTEM_WINDOWS_MINIPORT
577 struct os_stream
*stream
;
578 struct bmp_file_header bmfh
;
579 struct bmp_info_header bmih
;
585 bmfh
.bfType
= 0x4d42;
586 bmfh
.bfSize
= 14 + 40 + height
*width
*4;
587 bmfh
.bfReserved1
= 0;
588 bmfh
.bfReserved2
= 0;
589 bmfh
.bfOffBits
= 14 + 40;
592 bmih
.biWidth
= width
;
593 bmih
.biHeight
= height
;
595 bmih
.biBitCount
= 32;
596 bmih
.biCompression
= 0;
597 bmih
.biSizeImage
= height
*width
*4;
598 bmih
.biXPelsPerMeter
= 0;
599 bmih
.biYPelsPerMeter
= 0;
601 bmih
.biClrImportant
= 0;
603 stream
= os_file_stream_create(filename
);
607 os_stream_write(stream
, &bmfh
, 14);
608 os_stream_write(stream
, &bmih
, 40);
612 float *ptr
= rgba
+ (stride
* y
* 4);
613 for(x
= 0; x
< width
; ++x
)
615 struct bmp_rgb_quad pixel
;
616 pixel
.rgbRed
= float_to_ubyte(ptr
[x
*4 + 0]);
617 pixel
.rgbGreen
= float_to_ubyte(ptr
[x
*4 + 1]);
618 pixel
.rgbBlue
= float_to_ubyte(ptr
[x
*4 + 2]);
619 pixel
.rgbAlpha
= 255;
620 os_stream_write(stream
, &pixel
, 4);
624 os_stream_close(stream
);