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
, "F"))
128 else if(!util_strcmp(str
, "false"))
130 else if(!util_strcmp(str
, "FALSE"))
135 if (debug_get_option_should_print())
136 debug_printf("%s: %s = %s\n", __FUNCTION__
, name
, result
? "TRUE" : "FALSE");
143 debug_get_num_option(const char *name
, long dfault
)
148 str
= os_get_option(name
);
163 while('0' <= c
&& c
<= '9') {
164 result
= result
*10 + (c
- '0');
170 if (debug_get_option_should_print())
171 debug_printf("%s: %s = %li\n", __FUNCTION__
, name
, result
);
178 debug_get_flags_option(const char *name
,
179 const struct debug_named_value
*flags
,
180 unsigned long dfault
)
182 unsigned long result
;
185 str
= os_get_option(name
);
188 else if (!util_strcmp(str
, "help")) {
190 while (flags
->name
) {
191 debug_printf("%s: help for %s: %s [0x%lx]\n", __FUNCTION__
, name
, flags
->name
, flags
->value
);
197 while( flags
->name
) {
198 if (!util_strcmp(str
, "all") || util_strstr(str
, flags
->name
))
199 result
|= flags
->value
;
204 if (debug_get_option_should_print()) {
206 debug_printf("%s: %s = 0x%lx (%s)\n", __FUNCTION__
, name
, result
, str
);
208 debug_printf("%s: %s = 0x%lx\n", __FUNCTION__
, name
, result
);
216 void _debug_assert_fail(const char *expr
,
219 const char *function
)
221 _debug_printf("%s:%u:%s: Assertion `%s' failed.\n", file
, line
, function
, expr
);
222 #if defined(PIPE_OS_WINDOWS) && !defined(PIPE_SUBSYSTEM_WINDOWS_USER)
223 if (debug_get_bool_option("GALLIUM_ABORT_ON_ASSERT", FALSE
))
225 if (debug_get_bool_option("GALLIUM_ABORT_ON_ASSERT", TRUE
))
229 _debug_printf("continuing...\n");
234 debug_dump_enum(const struct debug_named_value
*names
,
237 static char rest
[64];
240 if(names
->value
== value
)
245 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
251 debug_dump_enum_noprefix(const struct debug_named_value
*names
,
255 static char rest
[64];
258 if(names
->value
== value
) {
259 const char *name
= names
->name
;
260 while (*name
== *prefix
) {
271 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
277 debug_dump_flags(const struct debug_named_value
*names
,
280 static char output
[4096];
281 static char rest
[256];
287 if((names
->value
& value
) == names
->value
) {
289 util_strncat(output
, "|", sizeof(output
));
292 util_strncat(output
, names
->name
, sizeof(output
) - 1);
293 output
[sizeof(output
) - 1] = '\0';
294 value
&= ~names
->value
;
301 util_strncat(output
, "|", sizeof(output
));
305 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
306 util_strncat(output
, rest
, sizeof(output
) - 1);
307 output
[sizeof(output
) - 1] = '\0';
318 void debug_print_format(const char *msg
, unsigned fmt
)
320 debug_printf("%s: %s\n", msg
, util_format_name(fmt
));
326 static const struct debug_named_value pipe_prim_names
[] = {
328 DEBUG_NAMED_VALUE(PIPE_PRIM_POINTS
),
329 DEBUG_NAMED_VALUE(PIPE_PRIM_LINES
),
330 DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_LOOP
),
331 DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_STRIP
),
332 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLES
),
333 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_STRIP
),
334 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_FAN
),
335 DEBUG_NAMED_VALUE(PIPE_PRIM_QUADS
),
336 DEBUG_NAMED_VALUE(PIPE_PRIM_QUAD_STRIP
),
337 DEBUG_NAMED_VALUE(PIPE_PRIM_POLYGON
),
339 DEBUG_NAMED_VALUE_END
343 const char *u_prim_name( unsigned prim
)
345 return debug_dump_enum(pipe_prim_names
, prim
);
353 * Dump an image to a .raw or .ppm file (depends on OS).
354 * \param format PIPE_FORMAT_x
355 * \param cpp bytes per pixel
356 * \param width width in pixels
357 * \param height height in pixels
358 * \param stride row stride in bytes
360 void debug_dump_image(const char *prefix
,
361 unsigned format
, unsigned cpp
,
362 unsigned width
, unsigned height
,
366 #ifdef PIPE_SUBSYSTEM_WINDOWS_DISPLAY
367 static unsigned no
= 0;
369 WCHAR wfilename
[sizeof(filename
)];
377 unsigned char *pMap
= NULL
;
380 util_snprintf(filename
, sizeof(filename
), "\\??\\c:\\%03u%s.raw", ++no
, prefix
);
381 for(i
= 0; i
< sizeof(filename
); ++i
)
382 wfilename
[i
] = (WCHAR
)filename
[i
];
384 pMap
= (unsigned char *)EngMapFile(wfilename
, sizeof(header
) + height
*width
*cpp
, &iFile
);
388 header
.format
= format
;
390 header
.width
= width
;
391 header
.height
= height
;
392 memcpy(pMap
, &header
, sizeof(header
));
393 pMap
+= sizeof(header
);
395 for(i
= 0; i
< height
; ++i
) {
396 memcpy(pMap
, (unsigned char *)data
+ stride
*i
, cpp
*width
);
401 #elif defined(PIPE_OS_UNIX)
402 /* write a ppm file */
406 util_snprintf(filename
, sizeof(filename
), "%s.ppm", prefix
);
408 f
= fopen(filename
, "w");
412 const uint8_t *ptr
= (uint8_t *) data
;
414 /* XXX this is a hack */
416 case PIPE_FORMAT_B8G8R8A8_UNORM
:
428 fprintf(f
, "# ppm-file created by osdemo.c\n");
429 fprintf(f
, "%i %i\n", width
, height
);
433 f
= fopen(filename
, "ab"); /* reopen in binary append mode */
434 for (y
= 0; y
< height
; y
++) {
435 for (x
= 0; x
< width
; x
++) {
436 i
= y
* stride
+ x
* cpp
;
437 fputc(ptr
[i
+ r
], f
); /* write red */
438 fputc(ptr
[i
+ g
], f
); /* write green */
439 fputc(ptr
[i
+ b
], f
); /* write blue */
445 fprintf(stderr
, "Can't open %s for writing\n", filename
);
450 void debug_dump_surface(struct pipe_context
*pipe
,
452 struct pipe_surface
*surface
)
454 struct pipe_resource
*texture
;
455 struct pipe_transfer
*transfer
;
461 /* XXX: this doesn't necessarily work, as the driver may be using
462 * temporary storage for the surface which hasn't been propagated
463 * back into the texture. Need to nail down the semantics of views
464 * and transfers a bit better before we can say if extra work needs
467 texture
= surface
->texture
;
469 transfer
= pipe_get_transfer(pipe
, texture
, surface
->face
,
470 surface
->level
, surface
->zslice
,
471 PIPE_TRANSFER_READ
, 0, 0, surface
->width
,
474 data
= pipe
->transfer_map(pipe
, transfer
);
478 debug_dump_image(prefix
,
480 util_format_get_blocksize(texture
->format
),
481 util_format_get_nblocksx(texture
->format
, surface
->width
),
482 util_format_get_nblocksy(texture
->format
, surface
->height
),
486 pipe
->transfer_unmap(pipe
, transfer
);
488 pipe
->transfer_destroy(pipe
, transfer
);
492 void debug_dump_texture(struct pipe_context
*pipe
,
494 struct pipe_resource
*texture
)
496 struct pipe_surface
*surface
;
497 struct pipe_screen
*screen
;
502 screen
= texture
->screen
;
504 /* XXX for now, just dump image for face=0, level=0 */
505 surface
= screen
->get_tex_surface(screen
, texture
, 0, 0, 0,
506 PIPE_BIND_SAMPLER_VIEW
);
508 debug_dump_surface(pipe
, prefix
, surface
);
509 screen
->tex_surface_destroy(surface
);
515 struct bmp_file_header
{
518 uint16_t bfReserved1
;
519 uint16_t bfReserved2
;
524 struct bmp_info_header
{
530 uint32_t biCompression
;
531 uint32_t biSizeImage
;
532 int32_t biXPelsPerMeter
;
533 int32_t biYPelsPerMeter
;
535 uint32_t biClrImportant
;
538 struct bmp_rgb_quad
{
546 debug_dump_surface_bmp(struct pipe_context
*pipe
,
547 const char *filename
,
548 struct pipe_surface
*surface
)
550 #ifndef PIPE_SUBSYSTEM_WINDOWS_MINIPORT
551 struct pipe_transfer
*transfer
;
552 struct pipe_resource
*texture
= surface
->texture
;
554 transfer
= pipe_get_transfer(pipe
, texture
, surface
->face
,
555 surface
->level
, surface
->zslice
,
556 PIPE_TRANSFER_READ
, 0, 0, surface
->width
,
559 debug_dump_transfer_bmp(pipe
, filename
, transfer
);
561 pipe
->transfer_destroy(pipe
, transfer
);
566 debug_dump_transfer_bmp(struct pipe_context
*pipe
,
567 const char *filename
,
568 struct pipe_transfer
*transfer
)
570 #ifndef PIPE_SUBSYSTEM_WINDOWS_MINIPORT
576 rgba
= MALLOC(transfer
->box
.width
*
577 transfer
->box
.height
*
578 transfer
->box
.depth
*
583 pipe_get_tile_rgba(pipe
, transfer
, 0, 0,
584 transfer
->box
.width
, transfer
->box
.height
,
587 debug_dump_float_rgba_bmp(filename
,
588 transfer
->box
.width
, transfer
->box
.height
,
589 rgba
, transfer
->box
.width
);
598 debug_dump_float_rgba_bmp(const char *filename
,
599 unsigned width
, unsigned height
,
600 float *rgba
, unsigned stride
)
602 #ifndef PIPE_SUBSYSTEM_WINDOWS_MINIPORT
603 struct os_stream
*stream
;
604 struct bmp_file_header bmfh
;
605 struct bmp_info_header bmih
;
611 bmfh
.bfType
= 0x4d42;
612 bmfh
.bfSize
= 14 + 40 + height
*width
*4;
613 bmfh
.bfReserved1
= 0;
614 bmfh
.bfReserved2
= 0;
615 bmfh
.bfOffBits
= 14 + 40;
618 bmih
.biWidth
= width
;
619 bmih
.biHeight
= height
;
621 bmih
.biBitCount
= 32;
622 bmih
.biCompression
= 0;
623 bmih
.biSizeImage
= height
*width
*4;
624 bmih
.biXPelsPerMeter
= 0;
625 bmih
.biYPelsPerMeter
= 0;
627 bmih
.biClrImportant
= 0;
629 stream
= os_file_stream_create(filename
);
633 os_stream_write(stream
, &bmfh
, 14);
634 os_stream_write(stream
, &bmih
, 40);
638 float *ptr
= rgba
+ (stride
* y
* 4);
639 for(x
= 0; x
< width
; ++x
)
641 struct bmp_rgb_quad pixel
;
642 pixel
.rgbRed
= float_to_ubyte(ptr
[x
*4 + 0]);
643 pixel
.rgbGreen
= float_to_ubyte(ptr
[x
*4 + 1]);
644 pixel
.rgbBlue
= float_to_ubyte(ptr
[x
*4 + 2]);
645 pixel
.rgbAlpha
= 255;
646 os_stream_write(stream
, &pixel
, 4);
650 os_stream_close(stream
);