Merge remote branch 'origin/master' into lp-binning
[mesa.git] / src / gallium / auxiliary / util / u_debug.c
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * Copyright (c) 2008 VMware, Inc.
5 * All Rights Reserved.
6 *
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:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
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.
26 *
27 **************************************************************************/
28
29
30 #include "pipe/p_config.h"
31
32 #include <stdarg.h>
33
34
35 #ifdef PIPE_SUBSYSTEM_WINDOWS_DISPLAY
36
37 #include <windows.h>
38 #include <winddi.h>
39
40 #elif defined(PIPE_SUBSYSTEM_WINDOWS_CE)
41
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <windows.h>
45 #include <types.h>
46
47 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
48
49 #ifndef WIN32_LEAN_AND_MEAN
50 #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
51 #endif
52 #include <windows.h>
53 #include <stdio.h>
54
55 #else
56
57 #include <stdio.h>
58 #include <stdlib.h>
59
60 #endif
61
62 #include "pipe/p_compiler.h"
63 #include "util/u_debug.h"
64 #include "pipe/p_format.h"
65 #include "pipe/p_state.h"
66 #include "pipe/p_inlines.h"
67 #include "util/u_format.h"
68 #include "util/u_memory.h"
69 #include "util/u_string.h"
70 #include "util/u_stream.h"
71 #include "util/u_math.h"
72 #include "util/u_tile.h"
73 #include "util/u_prim.h"
74
75
76 #ifdef PIPE_SUBSYSTEM_WINDOWS_DISPLAY
77 static INLINE void
78 _EngDebugPrint(const char *format, ...)
79 {
80 va_list ap;
81 va_start(ap, format);
82 EngDebugPrint("", (PCHAR)format, ap);
83 va_end(ap);
84 }
85 #endif
86
87
88 void _debug_vprintf(const char *format, va_list ap)
89 {
90 #if defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
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] = {'\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 _EngDebugPrint("%s", buf);
99 buf[0] = '\0';
100 }
101 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
102 /* OutputDebugStringA can be very slow, so buffer until we find a newline. */
103 static char buf[4096] = {'\0'};
104 size_t len = strlen(buf);
105 int ret = util_vsnprintf(buf + len, sizeof(buf) - len, format, ap);
106 if(ret > (int)(sizeof(buf) - len - 1) || util_strchr(buf + len, '\n')) {
107 OutputDebugStringA(buf);
108 buf[0] = '\0';
109 }
110
111 if(GetConsoleWindow() && !IsDebuggerPresent()) {
112 fflush(stdout);
113 vfprintf(stderr, format, ap);
114 fflush(stderr);
115 }
116
117 #elif defined(PIPE_SUBSYSTEM_WINDOWS_CE)
118 wchar_t *wide_format;
119 long wide_str_len;
120 char buf[512];
121 int ret;
122 #if (_WIN32_WCE < 600)
123 ret = vsprintf(buf, format, ap);
124 if(ret < 0){
125 sprintf(buf, "Cant handle debug print!");
126 ret = 25;
127 }
128 #else
129 ret = vsprintf_s(buf, 512, format, ap);
130 if(ret < 0){
131 sprintf_s(buf, 512, "Cant handle debug print!");
132 ret = 25;
133 }
134 #endif
135 buf[ret] = '\0';
136 /* Format is ascii - needs to be converted to wchar_t for printing */
137 wide_str_len = MultiByteToWideChar(CP_ACP, 0, (const char *) buf, -1, NULL, 0);
138 wide_format = (wchar_t *) malloc((wide_str_len+1) * sizeof(wchar_t));
139 if (wide_format) {
140 MultiByteToWideChar(CP_ACP, 0, (const char *) buf, -1,
141 wide_format, wide_str_len);
142 NKDbgPrintfW(wide_format, wide_format);
143 free(wide_format);
144 }
145 #elif defined(PIPE_SUBSYSTEM_WINDOWS_MINIPORT)
146 /* TODO */
147 #else /* !PIPE_SUBSYSTEM_WINDOWS */
148 fflush(stdout);
149 vfprintf(stderr, format, ap);
150 #endif
151 }
152
153
154 #ifdef DEBUG
155 void debug_print_blob( const char *name,
156 const void *blob,
157 unsigned size )
158 {
159 const unsigned *ublob = (const unsigned *)blob;
160 unsigned i;
161
162 debug_printf("%s (%d dwords%s)\n", name, size/4,
163 size%4 ? "... plus a few bytes" : "");
164
165 for (i = 0; i < size/4; i++) {
166 debug_printf("%d:\t%08x\n", i, ublob[i]);
167 }
168 }
169 #endif
170
171
172 #ifndef debug_break
173 void debug_break(void)
174 {
175 #if defined(PIPE_SUBSYSTEM_WINDOWS_USER)
176 DebugBreak();
177 #elif defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
178 EngDebugBreak();
179 #else
180 abort();
181 #endif
182 }
183 #endif
184
185
186 #ifdef PIPE_SUBSYSTEM_WINDOWS_DISPLAY
187 static const char *
188 find(const char *start, const char *end, char c)
189 {
190 const char *p;
191 for(p = start; !end || p != end; ++p) {
192 if(*p == c)
193 return p;
194 if(*p < 32)
195 break;
196 }
197 return NULL;
198 }
199
200 static int
201 compare(const char *start, const char *end, const char *s)
202 {
203 const char *p, *q;
204 for(p = start, q = s; p != end && *q != '\0'; ++p, ++q) {
205 if(*p != *q)
206 return 0;
207 }
208 return p == end && *q == '\0';
209 }
210
211 static void
212 copy(char *dst, const char *start, const char *end, size_t n)
213 {
214 const char *p;
215 char *q;
216 for(p = start, q = dst, n = n - 1; p != end && n; ++p, ++q, --n)
217 *q = *p;
218 *q = '\0';
219 }
220 #endif
221
222
223 static INLINE const char *
224 _debug_get_option(const char *name)
225 {
226 #if defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
227 /* EngMapFile creates the file if it does not exists, so it must either be
228 * disabled on release versions (or put in a less conspicuous place). */
229 #ifdef DEBUG
230 const char *result = NULL;
231 ULONG_PTR iFile = 0;
232 const void *pMap = NULL;
233 const char *sol, *eol, *sep;
234 static char output[1024];
235
236 pMap = EngMapFile(L"\\??\\c:\\gallium.cfg", 0, &iFile);
237 if(pMap) {
238 sol = (const char *)pMap;
239 while(1) {
240 /* TODO: handle LF line endings */
241 eol = find(sol, NULL, '\r');
242 if(!eol || eol == sol)
243 break;
244 sep = find(sol, eol, '=');
245 if(!sep)
246 break;
247 if(compare(sol, sep, name)) {
248 copy(output, sep + 1, eol, sizeof(output));
249 result = output;
250 break;
251 }
252 sol = eol + 2;
253 }
254 EngUnmapFile(iFile);
255 }
256 return result;
257 #else
258 return NULL;
259 #endif
260 #elif defined(PIPE_SUBSYSTEM_WINDOWS_CE) || defined(PIPE_SUBSYSTEM_WINDOWS_MINIPORT)
261 /* TODO: implement */
262 return NULL;
263 #else
264 return getenv(name);
265 #endif
266 }
267
268 const char *
269 debug_get_option(const char *name, const char *dfault)
270 {
271 const char *result;
272
273 result = _debug_get_option(name);
274 if(!result)
275 result = dfault;
276
277 debug_printf("%s: %s = %s\n", __FUNCTION__, name, result ? result : "(null)");
278
279 return result;
280 }
281
282 boolean
283 debug_get_bool_option(const char *name, boolean dfault)
284 {
285 const char *str = _debug_get_option(name);
286 boolean result;
287
288 if(str == NULL)
289 result = dfault;
290 else if(!util_strcmp(str, "n"))
291 result = FALSE;
292 else if(!util_strcmp(str, "no"))
293 result = FALSE;
294 else if(!util_strcmp(str, "0"))
295 result = FALSE;
296 else if(!util_strcmp(str, "f"))
297 result = FALSE;
298 else if(!util_strcmp(str, "false"))
299 result = FALSE;
300 else
301 result = TRUE;
302
303 debug_printf("%s: %s = %s\n", __FUNCTION__, name, result ? "TRUE" : "FALSE");
304
305 return result;
306 }
307
308
309 long
310 debug_get_num_option(const char *name, long dfault)
311 {
312 long result;
313 const char *str;
314
315 str = _debug_get_option(name);
316 if(!str)
317 result = dfault;
318 else {
319 long sign;
320 char c;
321 c = *str++;
322 if(c == '-') {
323 sign = -1;
324 c = *str++;
325 }
326 else {
327 sign = 1;
328 }
329 result = 0;
330 while('0' <= c && c <= '9') {
331 result = result*10 + (c - '0');
332 c = *str++;
333 }
334 result *= sign;
335 }
336
337 debug_printf("%s: %s = %li\n", __FUNCTION__, name, result);
338
339 return result;
340 }
341
342
343 unsigned long
344 debug_get_flags_option(const char *name,
345 const struct debug_named_value *flags,
346 unsigned long dfault)
347 {
348 unsigned long result;
349 const char *str;
350
351 str = _debug_get_option(name);
352 if(!str)
353 result = dfault;
354 else if (!util_strcmp(str, "help")) {
355 result = dfault;
356 while (flags->name) {
357 debug_printf("%s: help for %s: %s [0x%lx]\n", __FUNCTION__, name, flags->name, flags->value);
358 flags++;
359 }
360 }
361 else {
362 result = 0;
363 while( flags->name ) {
364 if (!util_strcmp(str, "all") || util_strstr(str, flags->name ))
365 result |= flags->value;
366 ++flags;
367 }
368 }
369
370 if (str) {
371 debug_printf("%s: %s = 0x%lx (%s)\n", __FUNCTION__, name, result, str);
372 }
373 else {
374 debug_printf("%s: %s = 0x%lx\n", __FUNCTION__, name, result);
375 }
376
377 return result;
378 }
379
380
381 void _debug_assert_fail(const char *expr,
382 const char *file,
383 unsigned line,
384 const char *function)
385 {
386 _debug_printf("%s:%u:%s: Assertion `%s' failed.\n", file, line, function, expr);
387 #if defined(PIPE_OS_WINDOWS) && !defined(PIPE_SUBSYSTEM_WINDOWS_USER)
388 if (debug_get_bool_option("GALLIUM_ABORT_ON_ASSERT", FALSE))
389 #else
390 if (debug_get_bool_option("GALLIUM_ABORT_ON_ASSERT", TRUE))
391 #endif
392 debug_break();
393 else
394 _debug_printf("continuing...\n");
395 }
396
397
398 const char *
399 debug_dump_enum(const struct debug_named_value *names,
400 unsigned long value)
401 {
402 static char rest[64];
403
404 while(names->name) {
405 if(names->value == value)
406 return names->name;
407 ++names;
408 }
409
410 util_snprintf(rest, sizeof(rest), "0x%08lx", value);
411 return rest;
412 }
413
414
415 const char *
416 debug_dump_enum_noprefix(const struct debug_named_value *names,
417 const char *prefix,
418 unsigned long value)
419 {
420 static char rest[64];
421
422 while(names->name) {
423 if(names->value == value) {
424 const char *name = names->name;
425 while (*name == *prefix) {
426 name++;
427 prefix++;
428 }
429 return name;
430 }
431 ++names;
432 }
433
434
435
436 util_snprintf(rest, sizeof(rest), "0x%08lx", value);
437 return rest;
438 }
439
440
441 const char *
442 debug_dump_flags(const struct debug_named_value *names,
443 unsigned long value)
444 {
445 static char output[4096];
446 static char rest[256];
447 int first = 1;
448
449 output[0] = '\0';
450
451 while(names->name) {
452 if((names->value & value) == names->value) {
453 if (!first)
454 util_strncat(output, "|", sizeof(output));
455 else
456 first = 0;
457 util_strncat(output, names->name, sizeof(output) - 1);
458 output[sizeof(output) - 1] = '\0';
459 value &= ~names->value;
460 }
461 ++names;
462 }
463
464 if (value) {
465 if (!first)
466 util_strncat(output, "|", sizeof(output));
467 else
468 first = 0;
469
470 util_snprintf(rest, sizeof(rest), "0x%08lx", value);
471 util_strncat(output, rest, sizeof(output) - 1);
472 output[sizeof(output) - 1] = '\0';
473 }
474
475 if(first)
476 return "0";
477
478 return output;
479 }
480
481
482 static const struct debug_named_value pipe_format_names[] = {
483 #ifdef DEBUG
484 DEBUG_NAMED_VALUE(PIPE_FORMAT_NONE),
485 DEBUG_NAMED_VALUE(PIPE_FORMAT_A8R8G8B8_UNORM),
486 DEBUG_NAMED_VALUE(PIPE_FORMAT_X8R8G8B8_UNORM),
487 DEBUG_NAMED_VALUE(PIPE_FORMAT_B8G8R8A8_UNORM),
488 DEBUG_NAMED_VALUE(PIPE_FORMAT_B8G8R8X8_UNORM),
489 DEBUG_NAMED_VALUE(PIPE_FORMAT_A1R5G5B5_UNORM),
490 DEBUG_NAMED_VALUE(PIPE_FORMAT_A4R4G4B4_UNORM),
491 DEBUG_NAMED_VALUE(PIPE_FORMAT_R5G6B5_UNORM),
492 DEBUG_NAMED_VALUE(PIPE_FORMAT_A2B10G10R10_UNORM),
493 DEBUG_NAMED_VALUE(PIPE_FORMAT_L8_UNORM),
494 DEBUG_NAMED_VALUE(PIPE_FORMAT_A8_UNORM),
495 DEBUG_NAMED_VALUE(PIPE_FORMAT_I8_UNORM),
496 DEBUG_NAMED_VALUE(PIPE_FORMAT_A8L8_UNORM),
497 DEBUG_NAMED_VALUE(PIPE_FORMAT_L16_UNORM),
498 DEBUG_NAMED_VALUE(PIPE_FORMAT_YCBCR),
499 DEBUG_NAMED_VALUE(PIPE_FORMAT_YCBCR_REV),
500 DEBUG_NAMED_VALUE(PIPE_FORMAT_Z16_UNORM),
501 DEBUG_NAMED_VALUE(PIPE_FORMAT_Z32_UNORM),
502 DEBUG_NAMED_VALUE(PIPE_FORMAT_Z32_FLOAT),
503 DEBUG_NAMED_VALUE(PIPE_FORMAT_S8Z24_UNORM),
504 DEBUG_NAMED_VALUE(PIPE_FORMAT_Z24S8_UNORM),
505 DEBUG_NAMED_VALUE(PIPE_FORMAT_X8Z24_UNORM),
506 DEBUG_NAMED_VALUE(PIPE_FORMAT_Z24X8_UNORM),
507 DEBUG_NAMED_VALUE(PIPE_FORMAT_S8_UNORM),
508 DEBUG_NAMED_VALUE(PIPE_FORMAT_R64_FLOAT),
509 DEBUG_NAMED_VALUE(PIPE_FORMAT_R64G64_FLOAT),
510 DEBUG_NAMED_VALUE(PIPE_FORMAT_R64G64B64_FLOAT),
511 DEBUG_NAMED_VALUE(PIPE_FORMAT_R64G64B64A64_FLOAT),
512 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32_FLOAT),
513 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32_FLOAT),
514 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32B32_FLOAT),
515 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32B32A32_FLOAT),
516 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32_UNORM),
517 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32_UNORM),
518 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32B32_UNORM),
519 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32B32A32_UNORM),
520 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32_USCALED),
521 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32_USCALED),
522 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32B32_USCALED),
523 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32B32A32_USCALED),
524 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32_SNORM),
525 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32_SNORM),
526 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32B32_SNORM),
527 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32B32A32_SNORM),
528 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32_SSCALED),
529 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32_SSCALED),
530 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32B32_SSCALED),
531 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32B32A32_SSCALED),
532 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16_UNORM),
533 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16_UNORM),
534 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16B16_UNORM),
535 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16B16A16_UNORM),
536 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16_USCALED),
537 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16_USCALED),
538 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16B16_USCALED),
539 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16B16A16_USCALED),
540 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16_SNORM),
541 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16_SNORM),
542 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16B16_SNORM),
543 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16B16A16_SNORM),
544 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16_SSCALED),
545 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16_SSCALED),
546 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16B16_SSCALED),
547 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16B16A16_SSCALED),
548 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8_UNORM),
549 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8_UNORM),
550 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8_UNORM),
551 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8A8_UNORM),
552 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8X8_UNORM),
553 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8_USCALED),
554 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8_USCALED),
555 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8_USCALED),
556 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8A8_USCALED),
557 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8X8_USCALED),
558 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8_SNORM),
559 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8_SNORM),
560 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8_SNORM),
561 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8A8_SNORM),
562 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8X8_SNORM),
563 DEBUG_NAMED_VALUE(PIPE_FORMAT_B6G5R5_SNORM),
564 DEBUG_NAMED_VALUE(PIPE_FORMAT_A8B8G8R8_SNORM),
565 DEBUG_NAMED_VALUE(PIPE_FORMAT_X8B8G8R8_SNORM),
566 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8_SSCALED),
567 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8_SSCALED),
568 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8_SSCALED),
569 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8A8_SSCALED),
570 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8X8_SSCALED),
571 DEBUG_NAMED_VALUE(PIPE_FORMAT_L8_SRGB),
572 DEBUG_NAMED_VALUE(PIPE_FORMAT_A8L8_SRGB),
573 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8_SRGB),
574 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8A8_SRGB),
575 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8X8_SRGB),
576 DEBUG_NAMED_VALUE(PIPE_FORMAT_A8R8G8B8_SRGB),
577 DEBUG_NAMED_VALUE(PIPE_FORMAT_X8R8G8B8_SRGB),
578 DEBUG_NAMED_VALUE(PIPE_FORMAT_B8G8R8A8_SRGB),
579 DEBUG_NAMED_VALUE(PIPE_FORMAT_B8G8R8X8_SRGB),
580 DEBUG_NAMED_VALUE(PIPE_FORMAT_X8UB8UG8SR8S_NORM),
581 DEBUG_NAMED_VALUE(PIPE_FORMAT_B6UG5SR5S_NORM),
582 DEBUG_NAMED_VALUE(PIPE_FORMAT_DXT1_RGB),
583 DEBUG_NAMED_VALUE(PIPE_FORMAT_DXT1_RGBA),
584 DEBUG_NAMED_VALUE(PIPE_FORMAT_DXT3_RGBA),
585 DEBUG_NAMED_VALUE(PIPE_FORMAT_DXT5_RGBA),
586 DEBUG_NAMED_VALUE(PIPE_FORMAT_DXT1_SRGB),
587 DEBUG_NAMED_VALUE(PIPE_FORMAT_DXT1_SRGBA),
588 DEBUG_NAMED_VALUE(PIPE_FORMAT_DXT3_SRGBA),
589 DEBUG_NAMED_VALUE(PIPE_FORMAT_DXT5_SRGBA),
590 #endif
591 DEBUG_NAMED_VALUE_END
592 };
593
594 #ifdef DEBUG
595 void debug_print_format(const char *msg, unsigned fmt )
596 {
597 debug_printf("%s: %s\n", msg, debug_dump_enum(pipe_format_names, fmt));
598 }
599 #endif
600
601 const char *pf_name( enum pipe_format format )
602 {
603 return debug_dump_enum(pipe_format_names, format);
604 }
605
606
607
608 static const struct debug_named_value pipe_prim_names[] = {
609 #ifdef DEBUG
610 DEBUG_NAMED_VALUE(PIPE_PRIM_POINTS),
611 DEBUG_NAMED_VALUE(PIPE_PRIM_LINES),
612 DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_LOOP),
613 DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_STRIP),
614 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLES),
615 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_STRIP),
616 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_FAN),
617 DEBUG_NAMED_VALUE(PIPE_PRIM_QUADS),
618 DEBUG_NAMED_VALUE(PIPE_PRIM_QUAD_STRIP),
619 DEBUG_NAMED_VALUE(PIPE_PRIM_POLYGON),
620 #endif
621 DEBUG_NAMED_VALUE_END
622 };
623
624
625 const char *u_prim_name( unsigned prim )
626 {
627 return debug_dump_enum(pipe_prim_names, prim);
628 }
629
630
631
632
633 #ifdef DEBUG
634 /**
635 * Dump an image to a .raw or .ppm file (depends on OS).
636 * \param format PIPE_FORMAT_x
637 * \param cpp bytes per pixel
638 * \param width width in pixels
639 * \param height height in pixels
640 * \param stride row stride in bytes
641 */
642 void debug_dump_image(const char *prefix,
643 unsigned format, unsigned cpp,
644 unsigned width, unsigned height,
645 unsigned stride,
646 const void *data)
647 {
648 #ifdef PIPE_SUBSYSTEM_WINDOWS_DISPLAY
649 static unsigned no = 0;
650 char filename[256];
651 WCHAR wfilename[sizeof(filename)];
652 ULONG_PTR iFile = 0;
653 struct {
654 unsigned format;
655 unsigned cpp;
656 unsigned width;
657 unsigned height;
658 } header;
659 unsigned char *pMap = NULL;
660 unsigned i;
661
662 util_snprintf(filename, sizeof(filename), "\\??\\c:\\%03u%s.raw", ++no, prefix);
663 for(i = 0; i < sizeof(filename); ++i)
664 wfilename[i] = (WCHAR)filename[i];
665
666 pMap = (unsigned char *)EngMapFile(wfilename, sizeof(header) + height*width*cpp, &iFile);
667 if(!pMap)
668 return;
669
670 header.format = format;
671 header.cpp = cpp;
672 header.width = width;
673 header.height = height;
674 memcpy(pMap, &header, sizeof(header));
675 pMap += sizeof(header);
676
677 for(i = 0; i < height; ++i) {
678 memcpy(pMap, (unsigned char *)data + stride*i, cpp*width);
679 pMap += cpp*width;
680 }
681
682 EngUnmapFile(iFile);
683 #elif defined(PIPE_OS_UNIX)
684 /* write a ppm file */
685 char filename[256];
686 FILE *f;
687
688 util_snprintf(filename, sizeof(filename), "%s.ppm", prefix);
689
690 f = fopen(filename, "w");
691 if (f) {
692 int i, x, y;
693 int r, g, b;
694 const uint8_t *ptr = (uint8_t *) data;
695
696 /* XXX this is a hack */
697 switch (format) {
698 case PIPE_FORMAT_A8R8G8B8_UNORM:
699 r = 2;
700 g = 1;
701 b = 0;
702 break;
703 default:
704 r = 0;
705 g = 1;
706 b = 1;
707 }
708
709 fprintf(f, "P6\n");
710 fprintf(f, "# ppm-file created by osdemo.c\n");
711 fprintf(f, "%i %i\n", width, height);
712 fprintf(f, "255\n");
713 fclose(f);
714
715 f = fopen(filename, "ab"); /* reopen in binary append mode */
716 for (y = 0; y < height; y++) {
717 for (x = 0; x < width; x++) {
718 i = y * stride + x * cpp;
719 fputc(ptr[i + r], f); /* write red */
720 fputc(ptr[i + g], f); /* write green */
721 fputc(ptr[i + b], f); /* write blue */
722 }
723 }
724 fclose(f);
725 }
726 else {
727 fprintf(stderr, "Can't open %s for writing\n", filename);
728 }
729 #endif
730 }
731
732 void debug_dump_surface(const char *prefix,
733 struct pipe_surface *surface)
734 {
735 struct pipe_texture *texture;
736 struct pipe_screen *screen;
737 struct pipe_transfer *transfer;
738 void *data;
739
740 if (!surface)
741 return;
742
743 texture = surface->texture;
744 screen = texture->screen;
745
746 transfer = screen->get_tex_transfer(screen, texture, surface->face,
747 surface->level, surface->zslice,
748 PIPE_TRANSFER_READ, 0, 0, surface->width,
749 surface->height);
750
751 data = screen->transfer_map(screen, transfer);
752 if(!data)
753 goto error;
754
755 debug_dump_image(prefix,
756 texture->format,
757 util_format_get_blocksize(texture->format),
758 util_format_get_nblocksx(texture->format, transfer->width),
759 util_format_get_nblocksy(texture->format, transfer->height),
760 transfer->stride,
761 data);
762
763 screen->transfer_unmap(screen, transfer);
764 error:
765 screen->tex_transfer_destroy(transfer);
766 }
767
768
769 void debug_dump_texture(const char *prefix,
770 struct pipe_texture *texture)
771 {
772 struct pipe_surface *surface;
773 struct pipe_screen *screen;
774
775 if (!texture)
776 return;
777
778 screen = texture->screen;
779
780 /* XXX for now, just dump image for face=0, level=0 */
781 surface = screen->get_tex_surface(screen, texture, 0, 0, 0,
782 PIPE_TEXTURE_USAGE_SAMPLER);
783 if (surface) {
784 debug_dump_surface(prefix, surface);
785 screen->tex_surface_destroy(surface);
786 }
787 }
788
789
790 #pragma pack(push,2)
791 struct bmp_file_header {
792 uint16_t bfType;
793 uint32_t bfSize;
794 uint16_t bfReserved1;
795 uint16_t bfReserved2;
796 uint32_t bfOffBits;
797 };
798 #pragma pack(pop)
799
800 struct bmp_info_header {
801 uint32_t biSize;
802 int32_t biWidth;
803 int32_t biHeight;
804 uint16_t biPlanes;
805 uint16_t biBitCount;
806 uint32_t biCompression;
807 uint32_t biSizeImage;
808 int32_t biXPelsPerMeter;
809 int32_t biYPelsPerMeter;
810 uint32_t biClrUsed;
811 uint32_t biClrImportant;
812 };
813
814 struct bmp_rgb_quad {
815 uint8_t rgbBlue;
816 uint8_t rgbGreen;
817 uint8_t rgbRed;
818 uint8_t rgbAlpha;
819 };
820
821 void
822 debug_dump_surface_bmp(const char *filename,
823 struct pipe_surface *surface)
824 {
825 #ifndef PIPE_SUBSYSTEM_WINDOWS_MINIPORT
826 struct pipe_transfer *transfer;
827 struct pipe_texture *texture = surface->texture;
828 struct pipe_screen *screen = texture->screen;
829
830 transfer = screen->get_tex_transfer(screen, texture, surface->face,
831 surface->level, surface->zslice,
832 PIPE_TRANSFER_READ, 0, 0, surface->width,
833 surface->height);
834
835 debug_dump_transfer_bmp(filename, transfer);
836
837 screen->tex_transfer_destroy(transfer);
838 #endif
839 }
840
841 void
842 debug_dump_transfer_bmp(const char *filename,
843 struct pipe_transfer *transfer)
844 {
845 #ifndef PIPE_SUBSYSTEM_WINDOWS_MINIPORT
846 float *rgba;
847
848 if (!transfer)
849 goto error1;
850
851 rgba = MALLOC(transfer->width*transfer->height*4*sizeof(float));
852 if(!rgba)
853 goto error1;
854
855 pipe_get_tile_rgba(transfer, 0, 0,
856 transfer->width, transfer->height,
857 rgba);
858
859 debug_dump_float_rgba_bmp(filename,
860 transfer->width, transfer->height,
861 rgba, transfer->width);
862
863 FREE(rgba);
864 error1:
865 ;
866 #endif
867 }
868
869 void
870 debug_dump_float_rgba_bmp(const char *filename,
871 unsigned width, unsigned height,
872 float *rgba, unsigned stride)
873 {
874 #ifndef PIPE_SUBSYSTEM_WINDOWS_MINIPORT
875 struct util_stream *stream;
876 struct bmp_file_header bmfh;
877 struct bmp_info_header bmih;
878 unsigned x, y;
879
880 if(!rgba)
881 goto error1;
882
883 bmfh.bfType = 0x4d42;
884 bmfh.bfSize = 14 + 40 + height*width*4;
885 bmfh.bfReserved1 = 0;
886 bmfh.bfReserved2 = 0;
887 bmfh.bfOffBits = 14 + 40;
888
889 bmih.biSize = 40;
890 bmih.biWidth = width;
891 bmih.biHeight = height;
892 bmih.biPlanes = 1;
893 bmih.biBitCount = 32;
894 bmih.biCompression = 0;
895 bmih.biSizeImage = height*width*4;
896 bmih.biXPelsPerMeter = 0;
897 bmih.biYPelsPerMeter = 0;
898 bmih.biClrUsed = 0;
899 bmih.biClrImportant = 0;
900
901 stream = util_stream_create(filename, bmfh.bfSize);
902 if(!stream)
903 goto error1;
904
905 util_stream_write(stream, &bmfh, 14);
906 util_stream_write(stream, &bmih, 40);
907
908 y = height;
909 while(y--) {
910 float *ptr = rgba + (stride * y * 4);
911 for(x = 0; x < width; ++x)
912 {
913 struct bmp_rgb_quad pixel;
914 pixel.rgbRed = float_to_ubyte(ptr[x*4 + 0]);
915 pixel.rgbGreen = float_to_ubyte(ptr[x*4 + 1]);
916 pixel.rgbBlue = float_to_ubyte(ptr[x*4 + 2]);
917 pixel.rgbAlpha = 255;
918 util_stream_write(stream, &pixel, 4);
919 }
920 }
921
922 util_stream_close(stream);
923 error1:
924 ;
925 #endif
926 }
927
928 #endif