1 /**************************************************************************
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
29 #include "pipe/p_config.h"
34 #ifdef PIPE_SUBSYSTEM_WINDOWS_DISPLAY
39 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
41 #ifndef WIN32_LEAN_AND_MEAN
42 #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
53 #include "pipe/p_compiler.h"
54 #include "pipe/p_debug.h"
55 #include "pipe/p_format.h"
56 #include "pipe/p_state.h"
57 #include "pipe/p_inlines.h"
58 #include "util/u_memory.h"
59 #include "util/u_string.h"
60 #include "util/u_stream.h"
61 #include "util/u_math.h"
62 #include "util/u_tile.h"
65 #ifdef PIPE_SUBSYSTEM_WINDOWS_DISPLAY
67 _EngDebugPrint(const char *format
, ...)
71 EngDebugPrint("", (PCHAR
)format
, ap
);
77 void _debug_vprintf(const char *format
, va_list ap
)
79 #if defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
80 /* EngDebugPrint does not handle float point arguments, so we need to use
81 * our own vsnprintf implementation. It is also very slow, so buffer until
82 * we find a newline. */
83 static char buf
[512] = {'\0'};
84 size_t len
= strlen(buf
);
85 int ret
= util_vsnprintf(buf
+ len
, sizeof(buf
) - len
, format
, ap
);
86 if(ret
> (int)(sizeof(buf
) - len
- 1) || util_strchr(buf
+ len
, '\n')) {
87 _EngDebugPrint("%s", buf
);
90 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
91 /* EngDebugPrint does not handle float point arguments, so we need to use
92 * our own vsnprintf implementation. It is also very slow, so buffer until
93 * we find a newline. */
94 static char buf
[512 + 1] = {'\0'};
95 size_t len
= strlen(buf
);
96 int ret
= util_vsnprintf(buf
+ len
, sizeof(buf
) - len
, format
, ap
);
97 if(ret
> (int)(sizeof(buf
) - len
- 1) || util_strchr(buf
+ len
, '\n')) {
98 OutputDebugStringA(buf
);
101 #elif defined(PIPE_SUBSYSTEM_WINDOWS_CE) || defined(PIPE_SUBSYSTEM_WINDOWS_MINIPORT)
103 #else /* !PIPE_SUBSYSTEM_WINDOWS */
104 vfprintf(stderr
, format
, ap
);
110 void debug_print_blob( const char *name
,
114 const unsigned *ublob
= (const unsigned *)blob
;
117 debug_printf("%s (%d dwords%s)\n", name
, size
/4,
118 size
%4 ? "... plus a few bytes" : "");
120 for (i
= 0; i
< size
/4; i
++) {
121 debug_printf("%d:\t%08x\n", i
, ublob
[i
]);
127 void _debug_break(void)
129 #if defined(PIPE_ARCH_X86) && defined(PIPE_CC_GCC)
131 #elif defined(PIPE_ARCH_X86) && defined(PIPE_CC_MSVC)
133 #elif defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
141 #ifdef PIPE_SUBSYSTEM_WINDOWS_DISPLAY
143 find(const char *start
, const char *end
, char c
)
146 for(p
= start
; !end
|| p
!= end
; ++p
) {
156 compare(const char *start
, const char *end
, const char *s
)
159 for(p
= start
, q
= s
; p
!= end
&& *q
!= '\0'; ++p
, ++q
) {
163 return p
== end
&& *q
== '\0';
167 copy(char *dst
, const char *start
, const char *end
, size_t n
)
171 for(p
= start
, q
= dst
, n
= n
- 1; p
!= end
&& n
; ++p
, ++q
, --n
)
178 static INLINE
const char *
179 _debug_get_option(const char *name
)
181 #if defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
182 /* EngMapFile creates the file if it does not exists, so it must either be
183 * disabled on release versions (or put in a less conspicuous place). */
185 const char *result
= NULL
;
187 const void *pMap
= NULL
;
188 const char *sol
, *eol
, *sep
;
189 static char output
[1024];
191 pMap
= EngMapFile(L
"\\??\\c:\\gallium.cfg", 0, &iFile
);
193 sol
= (const char *)pMap
;
195 /* TODO: handle LF line endings */
196 eol
= find(sol
, NULL
, '\r');
197 if(!eol
|| eol
== sol
)
199 sep
= find(sol
, eol
, '=');
202 if(compare(sol
, sep
, name
)) {
203 copy(output
, sep
+ 1, eol
, sizeof(output
));
215 #elif defined(PIPE_SUBSYSTEM_WINDOWS_CE) || defined(PIPE_SUBSYSTEM_WINDOWS_MINIPORT)
216 /* TODO: implement */
224 debug_get_option(const char *name
, const char *dfault
)
228 result
= _debug_get_option(name
);
232 debug_printf("%s: %s = %s\n", __FUNCTION__
, name
, result
? result
: "(null)");
238 debug_get_bool_option(const char *name
, boolean dfault
)
240 const char *str
= _debug_get_option(name
);
245 else if(!util_strcmp(str
, "n"))
247 else if(!util_strcmp(str
, "no"))
249 else if(!util_strcmp(str
, "0"))
251 else if(!util_strcmp(str
, "f"))
253 else if(!util_strcmp(str
, "false"))
258 debug_printf("%s: %s = %s\n", __FUNCTION__
, name
, result
? "TRUE" : "FALSE");
265 debug_get_num_option(const char *name
, long dfault
)
270 str
= _debug_get_option(name
);
285 while('0' <= c
&& c
<= '9') {
286 result
= result
*10 + (c
- '0');
292 debug_printf("%s: %s = %li\n", __FUNCTION__
, name
, result
);
299 debug_get_flags_option(const char *name
,
300 const struct debug_named_value
*flags
,
301 unsigned long dfault
)
303 unsigned long result
;
306 str
= _debug_get_option(name
);
311 while( flags
->name
) {
312 if (!util_strcmp(str
, "all") || util_strstr(str
, flags
->name
))
313 result
|= flags
->value
;
318 debug_printf("%s: %s = 0x%lx\n", __FUNCTION__
, name
, result
);
324 void _debug_assert_fail(const char *expr
,
327 const char *function
)
329 _debug_printf("%s:%u:%s: Assertion `%s' failed.\n", file
, line
, function
, expr
);
330 #if defined(PIPE_OS_WINDOWS)
331 if (debug_get_bool_option("GALLIUM_ABORT_ON_ASSERT", FALSE
))
333 if (debug_get_bool_option("GALLIUM_ABORT_ON_ASSERT", TRUE
))
337 _debug_printf("continuing...\n");
342 debug_dump_enum(const struct debug_named_value
*names
,
345 static char rest
[64];
348 if(names
->value
== value
)
353 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
359 debug_dump_flags(const struct debug_named_value
*names
,
362 static char output
[4096];
363 static char rest
[256];
369 if((names
->value
& value
) == names
->value
) {
371 util_strncat(output
, "|", sizeof(output
));
374 util_strncat(output
, names
->name
, sizeof(output
));
375 value
&= ~names
->value
;
382 util_strncat(output
, "|", sizeof(output
));
386 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
387 util_strncat(output
, rest
, sizeof(output
));
397 static const struct debug_named_value pipe_format_names
[] = {
399 DEBUG_NAMED_VALUE(PIPE_FORMAT_NONE
),
400 DEBUG_NAMED_VALUE(PIPE_FORMAT_A8R8G8B8_UNORM
),
401 DEBUG_NAMED_VALUE(PIPE_FORMAT_X8R8G8B8_UNORM
),
402 DEBUG_NAMED_VALUE(PIPE_FORMAT_B8G8R8A8_UNORM
),
403 DEBUG_NAMED_VALUE(PIPE_FORMAT_B8G8R8X8_UNORM
),
404 DEBUG_NAMED_VALUE(PIPE_FORMAT_A1R5G5B5_UNORM
),
405 DEBUG_NAMED_VALUE(PIPE_FORMAT_A4R4G4B4_UNORM
),
406 DEBUG_NAMED_VALUE(PIPE_FORMAT_R5G6B5_UNORM
),
407 DEBUG_NAMED_VALUE(PIPE_FORMAT_A2B10G10R10_UNORM
),
408 DEBUG_NAMED_VALUE(PIPE_FORMAT_L8_UNORM
),
409 DEBUG_NAMED_VALUE(PIPE_FORMAT_A8_UNORM
),
410 DEBUG_NAMED_VALUE(PIPE_FORMAT_I8_UNORM
),
411 DEBUG_NAMED_VALUE(PIPE_FORMAT_A8L8_UNORM
),
412 DEBUG_NAMED_VALUE(PIPE_FORMAT_L16_UNORM
),
413 DEBUG_NAMED_VALUE(PIPE_FORMAT_YCBCR
),
414 DEBUG_NAMED_VALUE(PIPE_FORMAT_YCBCR_REV
),
415 DEBUG_NAMED_VALUE(PIPE_FORMAT_Z16_UNORM
),
416 DEBUG_NAMED_VALUE(PIPE_FORMAT_Z32_UNORM
),
417 DEBUG_NAMED_VALUE(PIPE_FORMAT_Z32_FLOAT
),
418 DEBUG_NAMED_VALUE(PIPE_FORMAT_S8Z24_UNORM
),
419 DEBUG_NAMED_VALUE(PIPE_FORMAT_Z24S8_UNORM
),
420 DEBUG_NAMED_VALUE(PIPE_FORMAT_X8Z24_UNORM
),
421 DEBUG_NAMED_VALUE(PIPE_FORMAT_Z24X8_UNORM
),
422 DEBUG_NAMED_VALUE(PIPE_FORMAT_S8_UNORM
),
423 DEBUG_NAMED_VALUE(PIPE_FORMAT_R64_FLOAT
),
424 DEBUG_NAMED_VALUE(PIPE_FORMAT_R64G64_FLOAT
),
425 DEBUG_NAMED_VALUE(PIPE_FORMAT_R64G64B64_FLOAT
),
426 DEBUG_NAMED_VALUE(PIPE_FORMAT_R64G64B64A64_FLOAT
),
427 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32_FLOAT
),
428 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32_FLOAT
),
429 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32B32_FLOAT
),
430 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32B32A32_FLOAT
),
431 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32_UNORM
),
432 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32_UNORM
),
433 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32B32_UNORM
),
434 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32B32A32_UNORM
),
435 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32_USCALED
),
436 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32_USCALED
),
437 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32B32_USCALED
),
438 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32B32A32_USCALED
),
439 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32_SNORM
),
440 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32_SNORM
),
441 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32B32_SNORM
),
442 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32B32A32_SNORM
),
443 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32_SSCALED
),
444 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32_SSCALED
),
445 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32B32_SSCALED
),
446 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32B32A32_SSCALED
),
447 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16_UNORM
),
448 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16_UNORM
),
449 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16B16_UNORM
),
450 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16B16A16_UNORM
),
451 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16_USCALED
),
452 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16_USCALED
),
453 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16B16_USCALED
),
454 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16B16A16_USCALED
),
455 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16_SNORM
),
456 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16_SNORM
),
457 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16B16_SNORM
),
458 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16B16A16_SNORM
),
459 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16_SSCALED
),
460 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16_SSCALED
),
461 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16B16_SSCALED
),
462 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16B16A16_SSCALED
),
463 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8_UNORM
),
464 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8_UNORM
),
465 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8_UNORM
),
466 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8A8_UNORM
),
467 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8X8_UNORM
),
468 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8_USCALED
),
469 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8_USCALED
),
470 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8_USCALED
),
471 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8A8_USCALED
),
472 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8X8_USCALED
),
473 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8_SNORM
),
474 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8_SNORM
),
475 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8_SNORM
),
476 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8A8_SNORM
),
477 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8X8_SNORM
),
478 DEBUG_NAMED_VALUE(PIPE_FORMAT_B6G5R5_SNORM
),
479 DEBUG_NAMED_VALUE(PIPE_FORMAT_A8B8G8R8_SNORM
),
480 DEBUG_NAMED_VALUE(PIPE_FORMAT_X8B8G8R8_SNORM
),
481 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8_SSCALED
),
482 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8_SSCALED
),
483 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8_SSCALED
),
484 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8A8_SSCALED
),
485 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8X8_SSCALED
),
486 DEBUG_NAMED_VALUE(PIPE_FORMAT_L8_SRGB
),
487 DEBUG_NAMED_VALUE(PIPE_FORMAT_A8_L8_SRGB
),
488 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8_SRGB
),
489 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8A8_SRGB
),
490 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8X8_SRGB
),
491 DEBUG_NAMED_VALUE(PIPE_FORMAT_X8UB8UG8SR8S_NORM
),
492 DEBUG_NAMED_VALUE(PIPE_FORMAT_B6UG5SR5S_NORM
),
493 DEBUG_NAMED_VALUE(PIPE_FORMAT_DXT1_RGB
),
494 DEBUG_NAMED_VALUE(PIPE_FORMAT_DXT1_RGBA
),
495 DEBUG_NAMED_VALUE(PIPE_FORMAT_DXT3_RGBA
),
496 DEBUG_NAMED_VALUE(PIPE_FORMAT_DXT5_RGBA
),
498 DEBUG_NAMED_VALUE_END
502 void debug_print_format(const char *msg
, unsigned fmt
)
504 debug_printf("%s: %s\n", msg
, debug_dump_enum(pipe_format_names
, fmt
));
508 const char *pf_name( enum pipe_format format
)
510 return debug_dump_enum(pipe_format_names
, format
);
515 void debug_dump_image(const char *prefix
,
516 unsigned format
, unsigned cpp
,
517 unsigned width
, unsigned height
,
521 #ifdef PIPE_SUBSYSTEM_WINDOWS_DISPLAY
522 static unsigned no
= 0;
524 WCHAR wfilename
[sizeof(filename
)];
532 unsigned char *pMap
= NULL
;
535 util_snprintf(filename
, sizeof(filename
), "\\??\\c:\\%03u%s.raw", ++no
, prefix
);
536 for(i
= 0; i
< sizeof(filename
); ++i
)
537 wfilename
[i
] = (WCHAR
)filename
[i
];
539 pMap
= (unsigned char *)EngMapFile(wfilename
, sizeof(header
) + height
*width
*cpp
, &iFile
);
543 header
.format
= format
;
545 header
.width
= width
;
546 header
.height
= height
;
547 memcpy(pMap
, &header
, sizeof(header
));
548 pMap
+= sizeof(header
);
550 for(i
= 0; i
< height
; ++i
) {
551 memcpy(pMap
, (unsigned char *)data
+ stride
*i
, cpp
*width
);
559 void debug_dump_surface(const char *prefix
,
560 struct pipe_surface
*surface
)
562 unsigned surface_usage
;
568 /* XXX: force mappable surface */
569 surface_usage
= surface
->usage
;
570 surface
->usage
|= PIPE_BUFFER_USAGE_CPU_READ
;
572 data
= pipe_surface_map(surface
,
573 PIPE_BUFFER_USAGE_CPU_READ
);
577 debug_dump_image(prefix
,
585 pipe_surface_unmap(surface
);
587 surface
->usage
= surface_usage
;
594 struct bmp_file_header
{
597 uint16_t bfReserved1
;
598 uint16_t bfReserved2
;
603 struct bmp_info_header
{
609 uint32_t biCompression
;
610 uint32_t biSizeImage
;
611 int32_t biXPelsPerMeter
;
612 int32_t biYPelsPerMeter
;
614 uint32_t biClrImportant
;
617 struct bmp_rgb_quad
{
625 debug_dump_surface_bmp(const char *filename
,
626 struct pipe_surface
*surface
)
628 struct util_stream
*stream
;
629 unsigned surface_usage
;
630 struct bmp_file_header bmfh
;
631 struct bmp_info_header bmih
;
638 rgba
= MALLOC(surface
->width
*4*sizeof(float));
642 bmfh
.bfType
= 0x4d42;
643 bmfh
.bfSize
= 14 + 40 + surface
->height
*surface
->width
*4;
644 bmfh
.bfReserved1
= 0;
645 bmfh
.bfReserved2
= 0;
646 bmfh
.bfOffBits
= 14 + 40;
649 bmih
.biWidth
= surface
->width
;
650 bmih
.biHeight
= surface
->height
;
652 bmih
.biBitCount
= 32;
653 bmih
.biCompression
= 0;
654 bmih
.biSizeImage
= surface
->height
*surface
->width
*4;
655 bmih
.biXPelsPerMeter
= 0;
656 bmih
.biYPelsPerMeter
= 0;
658 bmih
.biClrImportant
= 0;
660 stream
= util_stream_create(filename
, bmfh
.bfSize
);
664 util_stream_write(stream
, &bmfh
, 14);
665 util_stream_write(stream
, &bmih
, 40);
667 /* XXX: force mappable surface */
668 surface_usage
= surface
->usage
;
669 surface
->usage
|= PIPE_BUFFER_USAGE_CPU_READ
;
673 pipe_get_tile_rgba(surface
,
674 0, y
, surface
->width
, 1,
676 for(x
= 0; x
< surface
->width
; ++x
)
678 struct bmp_rgb_quad pixel
;
679 pixel
.rgbRed
= float_to_ubyte(rgba
[x
*4 + 0]);
680 pixel
.rgbGreen
= float_to_ubyte(rgba
[x
*4 + 1]);
681 pixel
.rgbBlue
= float_to_ubyte(rgba
[x
*4 + 2]);
682 pixel
.rgbAlpha
= float_to_ubyte(rgba
[x
*4 + 3]);
683 util_stream_write(stream
, &pixel
, 4);
687 surface
->usage
= surface_usage
;
689 util_stream_close(stream
);