27e0b0d1595cbd1d670469c9a7eef9f599123115
[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
74
75 #ifdef PIPE_SUBSYSTEM_WINDOWS_DISPLAY
76 static INLINE void
77 _EngDebugPrint(const char *format, ...)
78 {
79 va_list ap;
80 va_start(ap, format);
81 EngDebugPrint("", (PCHAR)format, ap);
82 va_end(ap);
83 }
84 #endif
85
86
87 void _debug_vprintf(const char *format, va_list ap)
88 {
89 #if defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
90 /* EngDebugPrint does not handle float point arguments, so we need to use
91 * our own vsnprintf implementation. It is also very slow, so buffer until
92 * we find a newline. */
93 static char buf[512] = {'\0'};
94 size_t len = strlen(buf);
95 int ret = util_vsnprintf(buf + len, sizeof(buf) - len, format, ap);
96 if(ret > (int)(sizeof(buf) - len - 1) || util_strchr(buf + len, '\n')) {
97 _EngDebugPrint("%s", buf);
98 buf[0] = '\0';
99 }
100 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
101 /* OutputDebugStringA can be very slow, so buffer until we find a newline. */
102 static char buf[4096] = {'\0'};
103 size_t len = strlen(buf);
104 int ret = util_vsnprintf(buf + len, sizeof(buf) - len, format, ap);
105 if(ret > (int)(sizeof(buf) - len - 1) || util_strchr(buf + len, '\n')) {
106 OutputDebugStringA(buf);
107 buf[0] = '\0';
108 }
109
110 if(GetConsoleWindow() && !IsDebuggerPresent()) {
111 fflush(stdout);
112 vfprintf(stderr, format, ap);
113 fflush(stderr);
114 }
115
116 #elif defined(PIPE_SUBSYSTEM_WINDOWS_CE)
117 wchar_t *wide_format;
118 long wide_str_len;
119 char buf[512];
120 int ret;
121 #if (_WIN32_WCE < 600)
122 ret = vsprintf(buf, format, ap);
123 if(ret < 0){
124 sprintf(buf, "Cant handle debug print!");
125 ret = 25;
126 }
127 #else
128 ret = vsprintf_s(buf, 512, format, ap);
129 if(ret < 0){
130 sprintf_s(buf, 512, "Cant handle debug print!");
131 ret = 25;
132 }
133 #endif
134 buf[ret] = '\0';
135 /* Format is ascii - needs to be converted to wchar_t for printing */
136 wide_str_len = MultiByteToWideChar(CP_ACP, 0, (const char *) buf, -1, NULL, 0);
137 wide_format = (wchar_t *) malloc((wide_str_len+1) * sizeof(wchar_t));
138 if (wide_format) {
139 MultiByteToWideChar(CP_ACP, 0, (const char *) buf, -1,
140 wide_format, wide_str_len);
141 NKDbgPrintfW(wide_format, wide_format);
142 free(wide_format);
143 }
144 #elif defined(PIPE_SUBSYSTEM_WINDOWS_MINIPORT)
145 /* TODO */
146 #else /* !PIPE_SUBSYSTEM_WINDOWS */
147 fflush(stdout);
148 vfprintf(stderr, format, ap);
149 #endif
150 }
151
152
153 #ifdef DEBUG
154 void debug_print_blob( const char *name,
155 const void *blob,
156 unsigned size )
157 {
158 const unsigned *ublob = (const unsigned *)blob;
159 unsigned i;
160
161 debug_printf("%s (%d dwords%s)\n", name, size/4,
162 size%4 ? "... plus a few bytes" : "");
163
164 for (i = 0; i < size/4; i++) {
165 debug_printf("%d:\t%08x\n", i, ublob[i]);
166 }
167 }
168 #endif
169
170
171 #ifndef debug_break
172 void debug_break(void)
173 {
174 #if defined(PIPE_SUBSYSTEM_WINDOWS_USER)
175 DebugBreak();
176 #elif defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
177 EngDebugBreak();
178 #else
179 abort();
180 #endif
181 }
182 #endif
183
184
185 #ifdef PIPE_SUBSYSTEM_WINDOWS_DISPLAY
186 static const char *
187 find(const char *start, const char *end, char c)
188 {
189 const char *p;
190 for(p = start; !end || p != end; ++p) {
191 if(*p == c)
192 return p;
193 if(*p < 32)
194 break;
195 }
196 return NULL;
197 }
198
199 static int
200 compare(const char *start, const char *end, const char *s)
201 {
202 const char *p, *q;
203 for(p = start, q = s; p != end && *q != '\0'; ++p, ++q) {
204 if(*p != *q)
205 return 0;
206 }
207 return p == end && *q == '\0';
208 }
209
210 static void
211 copy(char *dst, const char *start, const char *end, size_t n)
212 {
213 const char *p;
214 char *q;
215 for(p = start, q = dst, n = n - 1; p != end && n; ++p, ++q, --n)
216 *q = *p;
217 *q = '\0';
218 }
219 #endif
220
221
222 static INLINE const char *
223 _debug_get_option(const char *name)
224 {
225 #if defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
226 /* EngMapFile creates the file if it does not exists, so it must either be
227 * disabled on release versions (or put in a less conspicuous place). */
228 #ifdef DEBUG
229 const char *result = NULL;
230 ULONG_PTR iFile = 0;
231 const void *pMap = NULL;
232 const char *sol, *eol, *sep;
233 static char output[1024];
234
235 pMap = EngMapFile(L"\\??\\c:\\gallium.cfg", 0, &iFile);
236 if(pMap) {
237 sol = (const char *)pMap;
238 while(1) {
239 /* TODO: handle LF line endings */
240 eol = find(sol, NULL, '\r');
241 if(!eol || eol == sol)
242 break;
243 sep = find(sol, eol, '=');
244 if(!sep)
245 break;
246 if(compare(sol, sep, name)) {
247 copy(output, sep + 1, eol, sizeof(output));
248 result = output;
249 break;
250 }
251 sol = eol + 2;
252 }
253 EngUnmapFile(iFile);
254 }
255 return result;
256 #else
257 return NULL;
258 #endif
259 #elif defined(PIPE_SUBSYSTEM_WINDOWS_CE) || defined(PIPE_SUBSYSTEM_WINDOWS_MINIPORT)
260 /* TODO: implement */
261 return NULL;
262 #else
263 return getenv(name);
264 #endif
265 }
266
267 const char *
268 debug_get_option(const char *name, const char *dfault)
269 {
270 const char *result;
271
272 result = _debug_get_option(name);
273 if(!result)
274 result = dfault;
275
276 debug_printf("%s: %s = %s\n", __FUNCTION__, name, result ? result : "(null)");
277
278 return result;
279 }
280
281 boolean
282 debug_get_bool_option(const char *name, boolean dfault)
283 {
284 const char *str = _debug_get_option(name);
285 boolean result;
286
287 if(str == NULL)
288 result = dfault;
289 else if(!util_strcmp(str, "n"))
290 result = FALSE;
291 else if(!util_strcmp(str, "no"))
292 result = FALSE;
293 else if(!util_strcmp(str, "0"))
294 result = FALSE;
295 else if(!util_strcmp(str, "f"))
296 result = FALSE;
297 else if(!util_strcmp(str, "false"))
298 result = FALSE;
299 else
300 result = TRUE;
301
302 debug_printf("%s: %s = %s\n", __FUNCTION__, name, result ? "TRUE" : "FALSE");
303
304 return result;
305 }
306
307
308 long
309 debug_get_num_option(const char *name, long dfault)
310 {
311 long result;
312 const char *str;
313
314 str = _debug_get_option(name);
315 if(!str)
316 result = dfault;
317 else {
318 long sign;
319 char c;
320 c = *str++;
321 if(c == '-') {
322 sign = -1;
323 c = *str++;
324 }
325 else {
326 sign = 1;
327 }
328 result = 0;
329 while('0' <= c && c <= '9') {
330 result = result*10 + (c - '0');
331 c = *str++;
332 }
333 result *= sign;
334 }
335
336 debug_printf("%s: %s = %li\n", __FUNCTION__, name, result);
337
338 return result;
339 }
340
341
342 unsigned long
343 debug_get_flags_option(const char *name,
344 const struct debug_named_value *flags,
345 unsigned long dfault)
346 {
347 unsigned long result;
348 const char *str;
349
350 str = _debug_get_option(name);
351 if(!str)
352 result = dfault;
353 else if (!util_strcmp(str, "help")) {
354 result = dfault;
355 while (flags->name) {
356 debug_printf("%s: help for %s: %s [0x%lx]\n", __FUNCTION__, name, flags->name, flags->value);
357 flags++;
358 }
359 }
360 else {
361 result = 0;
362 while( flags->name ) {
363 if (!util_strcmp(str, "all") || util_strstr(str, flags->name ))
364 result |= flags->value;
365 ++flags;
366 }
367 }
368
369 if (str) {
370 debug_printf("%s: %s = 0x%lx (%s)\n", __FUNCTION__, name, result, str);
371 }
372 else {
373 debug_printf("%s: %s = 0x%lx\n", __FUNCTION__, name, result);
374 }
375
376 return result;
377 }
378
379
380 void _debug_assert_fail(const char *expr,
381 const char *file,
382 unsigned line,
383 const char *function)
384 {
385 _debug_printf("%s:%u:%s: Assertion `%s' failed.\n", file, line, function, expr);
386 #if defined(PIPE_OS_WINDOWS) && !defined(PIPE_SUBSYSTEM_WINDOWS_USER)
387 if (debug_get_bool_option("GALLIUM_ABORT_ON_ASSERT", FALSE))
388 #else
389 if (debug_get_bool_option("GALLIUM_ABORT_ON_ASSERT", TRUE))
390 #endif
391 debug_break();
392 else
393 _debug_printf("continuing...\n");
394 }
395
396
397 const char *
398 debug_dump_enum(const struct debug_named_value *names,
399 unsigned long value)
400 {
401 static char rest[64];
402
403 while(names->name) {
404 if(names->value == value)
405 return names->name;
406 ++names;
407 }
408
409 util_snprintf(rest, sizeof(rest), "0x%08lx", value);
410 return rest;
411 }
412
413
414 const char *
415 debug_dump_enum_noprefix(const struct debug_named_value *names,
416 const char *prefix,
417 unsigned long value)
418 {
419 static char rest[64];
420
421 while(names->name) {
422 if(names->value == value) {
423 const char *name = names->name;
424 while (*name == *prefix) {
425 name++;
426 prefix++;
427 }
428 return name;
429 }
430 ++names;
431 }
432
433
434
435 util_snprintf(rest, sizeof(rest), "0x%08lx", value);
436 return rest;
437 }
438
439
440 const char *
441 debug_dump_flags(const struct debug_named_value *names,
442 unsigned long value)
443 {
444 static char output[4096];
445 static char rest[256];
446 int first = 1;
447
448 output[0] = '\0';
449
450 while(names->name) {
451 if((names->value & value) == names->value) {
452 if (!first)
453 util_strncat(output, "|", sizeof(output));
454 else
455 first = 0;
456 util_strncat(output, names->name, sizeof(output));
457 value &= ~names->value;
458 }
459 ++names;
460 }
461
462 if (value) {
463 if (!first)
464 util_strncat(output, "|", sizeof(output));
465 else
466 first = 0;
467
468 util_snprintf(rest, sizeof(rest), "0x%08lx", value);
469 util_strncat(output, rest, sizeof(output));
470 }
471
472 if(first)
473 return "0";
474
475 return output;
476 }
477
478
479 static const struct debug_named_value pipe_format_names[] = {
480 #ifdef DEBUG
481 DEBUG_NAMED_VALUE(PIPE_FORMAT_NONE),
482 DEBUG_NAMED_VALUE(PIPE_FORMAT_A8R8G8B8_UNORM),
483 DEBUG_NAMED_VALUE(PIPE_FORMAT_X8R8G8B8_UNORM),
484 DEBUG_NAMED_VALUE(PIPE_FORMAT_B8G8R8A8_UNORM),
485 DEBUG_NAMED_VALUE(PIPE_FORMAT_B8G8R8X8_UNORM),
486 DEBUG_NAMED_VALUE(PIPE_FORMAT_A1R5G5B5_UNORM),
487 DEBUG_NAMED_VALUE(PIPE_FORMAT_A4R4G4B4_UNORM),
488 DEBUG_NAMED_VALUE(PIPE_FORMAT_R5G6B5_UNORM),
489 DEBUG_NAMED_VALUE(PIPE_FORMAT_A2B10G10R10_UNORM),
490 DEBUG_NAMED_VALUE(PIPE_FORMAT_L8_UNORM),
491 DEBUG_NAMED_VALUE(PIPE_FORMAT_A8_UNORM),
492 DEBUG_NAMED_VALUE(PIPE_FORMAT_I8_UNORM),
493 DEBUG_NAMED_VALUE(PIPE_FORMAT_A8L8_UNORM),
494 DEBUG_NAMED_VALUE(PIPE_FORMAT_L16_UNORM),
495 DEBUG_NAMED_VALUE(PIPE_FORMAT_YCBCR),
496 DEBUG_NAMED_VALUE(PIPE_FORMAT_YCBCR_REV),
497 DEBUG_NAMED_VALUE(PIPE_FORMAT_Z16_UNORM),
498 DEBUG_NAMED_VALUE(PIPE_FORMAT_Z32_UNORM),
499 DEBUG_NAMED_VALUE(PIPE_FORMAT_Z32_FLOAT),
500 DEBUG_NAMED_VALUE(PIPE_FORMAT_S8Z24_UNORM),
501 DEBUG_NAMED_VALUE(PIPE_FORMAT_Z24S8_UNORM),
502 DEBUG_NAMED_VALUE(PIPE_FORMAT_X8Z24_UNORM),
503 DEBUG_NAMED_VALUE(PIPE_FORMAT_Z24X8_UNORM),
504 DEBUG_NAMED_VALUE(PIPE_FORMAT_S8_UNORM),
505 DEBUG_NAMED_VALUE(PIPE_FORMAT_R64_FLOAT),
506 DEBUG_NAMED_VALUE(PIPE_FORMAT_R64G64_FLOAT),
507 DEBUG_NAMED_VALUE(PIPE_FORMAT_R64G64B64_FLOAT),
508 DEBUG_NAMED_VALUE(PIPE_FORMAT_R64G64B64A64_FLOAT),
509 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32_FLOAT),
510 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32_FLOAT),
511 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32B32_FLOAT),
512 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32B32A32_FLOAT),
513 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32_UNORM),
514 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32_UNORM),
515 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32B32_UNORM),
516 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32B32A32_UNORM),
517 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32_USCALED),
518 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32_USCALED),
519 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32B32_USCALED),
520 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32B32A32_USCALED),
521 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32_SNORM),
522 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32_SNORM),
523 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32B32_SNORM),
524 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32B32A32_SNORM),
525 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32_SSCALED),
526 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32_SSCALED),
527 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32B32_SSCALED),
528 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32B32A32_SSCALED),
529 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16_UNORM),
530 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16_UNORM),
531 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16B16_UNORM),
532 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16B16A16_UNORM),
533 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16_USCALED),
534 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16_USCALED),
535 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16B16_USCALED),
536 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16B16A16_USCALED),
537 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16_SNORM),
538 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16_SNORM),
539 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16B16_SNORM),
540 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16B16A16_SNORM),
541 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16_SSCALED),
542 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16_SSCALED),
543 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16B16_SSCALED),
544 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16B16A16_SSCALED),
545 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8_UNORM),
546 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8_UNORM),
547 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8_UNORM),
548 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8A8_UNORM),
549 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8X8_UNORM),
550 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8_USCALED),
551 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8_USCALED),
552 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8_USCALED),
553 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8A8_USCALED),
554 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8X8_USCALED),
555 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8_SNORM),
556 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8_SNORM),
557 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8_SNORM),
558 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8A8_SNORM),
559 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8X8_SNORM),
560 DEBUG_NAMED_VALUE(PIPE_FORMAT_B6G5R5_SNORM),
561 DEBUG_NAMED_VALUE(PIPE_FORMAT_A8B8G8R8_SNORM),
562 DEBUG_NAMED_VALUE(PIPE_FORMAT_X8B8G8R8_SNORM),
563 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8_SSCALED),
564 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8_SSCALED),
565 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8_SSCALED),
566 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8A8_SSCALED),
567 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8X8_SSCALED),
568 DEBUG_NAMED_VALUE(PIPE_FORMAT_L8_SRGB),
569 DEBUG_NAMED_VALUE(PIPE_FORMAT_A8L8_SRGB),
570 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8_SRGB),
571 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8A8_SRGB),
572 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8X8_SRGB),
573 DEBUG_NAMED_VALUE(PIPE_FORMAT_A8R8G8B8_SRGB),
574 DEBUG_NAMED_VALUE(PIPE_FORMAT_X8R8G8B8_SRGB),
575 DEBUG_NAMED_VALUE(PIPE_FORMAT_B8G8R8A8_SRGB),
576 DEBUG_NAMED_VALUE(PIPE_FORMAT_B8G8R8X8_SRGB),
577 DEBUG_NAMED_VALUE(PIPE_FORMAT_X8UB8UG8SR8S_NORM),
578 DEBUG_NAMED_VALUE(PIPE_FORMAT_B6UG5SR5S_NORM),
579 DEBUG_NAMED_VALUE(PIPE_FORMAT_DXT1_RGB),
580 DEBUG_NAMED_VALUE(PIPE_FORMAT_DXT1_RGBA),
581 DEBUG_NAMED_VALUE(PIPE_FORMAT_DXT3_RGBA),
582 DEBUG_NAMED_VALUE(PIPE_FORMAT_DXT5_RGBA),
583 DEBUG_NAMED_VALUE(PIPE_FORMAT_DXT1_SRGB),
584 DEBUG_NAMED_VALUE(PIPE_FORMAT_DXT1_SRGBA),
585 DEBUG_NAMED_VALUE(PIPE_FORMAT_DXT3_SRGBA),
586 DEBUG_NAMED_VALUE(PIPE_FORMAT_DXT5_SRGBA),
587 #endif
588 DEBUG_NAMED_VALUE_END
589 };
590
591 #ifdef DEBUG
592 void debug_print_format(const char *msg, unsigned fmt )
593 {
594 debug_printf("%s: %s\n", msg, debug_dump_enum(pipe_format_names, fmt));
595 }
596 #endif
597
598 const char *pf_name( enum pipe_format format )
599 {
600 return debug_dump_enum(pipe_format_names, format);
601 }
602
603
604 #ifdef DEBUG
605 void debug_dump_image(const char *prefix,
606 unsigned format, unsigned cpp,
607 unsigned width, unsigned height,
608 unsigned stride,
609 const void *data)
610 {
611 #ifdef PIPE_SUBSYSTEM_WINDOWS_DISPLAY
612 static unsigned no = 0;
613 char filename[256];
614 WCHAR wfilename[sizeof(filename)];
615 ULONG_PTR iFile = 0;
616 struct {
617 unsigned format;
618 unsigned cpp;
619 unsigned width;
620 unsigned height;
621 } header;
622 unsigned char *pMap = NULL;
623 unsigned i;
624
625 util_snprintf(filename, sizeof(filename), "\\??\\c:\\%03u%s.raw", ++no, prefix);
626 for(i = 0; i < sizeof(filename); ++i)
627 wfilename[i] = (WCHAR)filename[i];
628
629 pMap = (unsigned char *)EngMapFile(wfilename, sizeof(header) + height*width*cpp, &iFile);
630 if(!pMap)
631 return;
632
633 header.format = format;
634 header.cpp = cpp;
635 header.width = width;
636 header.height = height;
637 memcpy(pMap, &header, sizeof(header));
638 pMap += sizeof(header);
639
640 for(i = 0; i < height; ++i) {
641 memcpy(pMap, (unsigned char *)data + stride*i, cpp*width);
642 pMap += cpp*width;
643 }
644
645 EngUnmapFile(iFile);
646 #endif
647 }
648
649 void debug_dump_surface(const char *prefix,
650 struct pipe_surface *surface)
651 {
652 struct pipe_texture *texture;
653 struct pipe_screen *screen;
654 struct pipe_transfer *transfer;
655 void *data;
656
657 if (!surface)
658 return;
659
660 texture = surface->texture;
661 screen = texture->screen;
662
663 transfer = screen->get_tex_transfer(screen, texture, surface->face,
664 surface->level, surface->zslice,
665 PIPE_TRANSFER_READ, 0, 0, surface->width,
666 surface->height);
667
668 data = screen->transfer_map(screen, transfer);
669 if(!data)
670 goto error;
671
672 debug_dump_image(prefix,
673 texture->format,
674 util_format_get_blocksize(texture->format),
675 util_format_get_nblocksx(texture->format, transfer->width),
676 util_format_get_nblocksy(texture->format, transfer->height),
677 transfer->stride,
678 data);
679
680 screen->transfer_unmap(screen, transfer);
681 error:
682 screen->tex_transfer_destroy(transfer);
683 }
684
685
686 #pragma pack(push,2)
687 struct bmp_file_header {
688 uint16_t bfType;
689 uint32_t bfSize;
690 uint16_t bfReserved1;
691 uint16_t bfReserved2;
692 uint32_t bfOffBits;
693 };
694 #pragma pack(pop)
695
696 struct bmp_info_header {
697 uint32_t biSize;
698 int32_t biWidth;
699 int32_t biHeight;
700 uint16_t biPlanes;
701 uint16_t biBitCount;
702 uint32_t biCompression;
703 uint32_t biSizeImage;
704 int32_t biXPelsPerMeter;
705 int32_t biYPelsPerMeter;
706 uint32_t biClrUsed;
707 uint32_t biClrImportant;
708 };
709
710 struct bmp_rgb_quad {
711 uint8_t rgbBlue;
712 uint8_t rgbGreen;
713 uint8_t rgbRed;
714 uint8_t rgbAlpha;
715 };
716
717 void
718 debug_dump_surface_bmp(const char *filename,
719 struct pipe_surface *surface)
720 {
721 #ifndef PIPE_SUBSYSTEM_WINDOWS_MINIPORT
722 struct pipe_transfer *transfer;
723 struct pipe_texture *texture = surface->texture;
724 struct pipe_screen *screen = texture->screen;
725
726 transfer = screen->get_tex_transfer(screen, texture, surface->face,
727 surface->level, surface->zslice,
728 PIPE_TRANSFER_READ, 0, 0, surface->width,
729 surface->height);
730
731 debug_dump_transfer_bmp(filename, transfer);
732
733 screen->tex_transfer_destroy(transfer);
734 #endif
735 }
736
737 void
738 debug_dump_transfer_bmp(const char *filename,
739 struct pipe_transfer *transfer)
740 {
741 #ifndef PIPE_SUBSYSTEM_WINDOWS_MINIPORT
742 float *rgba;
743
744 if (!transfer)
745 goto error1;
746
747 rgba = MALLOC(transfer->width*transfer->height*4*sizeof(float));
748 if(!rgba)
749 goto error1;
750
751 pipe_get_tile_rgba(transfer, 0, 0,
752 transfer->width, transfer->height,
753 rgba);
754
755 debug_dump_float_rgba_bmp(filename,
756 transfer->width, transfer->height,
757 rgba, transfer->width);
758
759 FREE(rgba);
760 error1:
761 ;
762 #endif
763 }
764
765 void
766 debug_dump_float_rgba_bmp(const char *filename,
767 unsigned width, unsigned height,
768 float *rgba, unsigned stride)
769 {
770 #ifndef PIPE_SUBSYSTEM_WINDOWS_MINIPORT
771 struct util_stream *stream;
772 struct bmp_file_header bmfh;
773 struct bmp_info_header bmih;
774 unsigned x, y;
775
776 if(!rgba)
777 goto error1;
778
779 bmfh.bfType = 0x4d42;
780 bmfh.bfSize = 14 + 40 + height*width*4;
781 bmfh.bfReserved1 = 0;
782 bmfh.bfReserved2 = 0;
783 bmfh.bfOffBits = 14 + 40;
784
785 bmih.biSize = 40;
786 bmih.biWidth = width;
787 bmih.biHeight = height;
788 bmih.biPlanes = 1;
789 bmih.biBitCount = 32;
790 bmih.biCompression = 0;
791 bmih.biSizeImage = height*width*4;
792 bmih.biXPelsPerMeter = 0;
793 bmih.biYPelsPerMeter = 0;
794 bmih.biClrUsed = 0;
795 bmih.biClrImportant = 0;
796
797 stream = util_stream_create(filename, bmfh.bfSize);
798 if(!stream)
799 goto error1;
800
801 util_stream_write(stream, &bmfh, 14);
802 util_stream_write(stream, &bmih, 40);
803
804 y = height;
805 while(y--) {
806 float *ptr = rgba + (stride * y * 4);
807 for(x = 0; x < width; ++x)
808 {
809 struct bmp_rgb_quad pixel;
810 pixel.rgbRed = float_to_ubyte(ptr[x*4 + 0]);
811 pixel.rgbGreen = float_to_ubyte(ptr[x*4 + 1]);
812 pixel.rgbBlue = float_to_ubyte(ptr[x*4 + 2]);
813 pixel.rgbAlpha = 255;
814 util_stream_write(stream, &pixel, 4);
815 }
816 }
817
818 util_stream_close(stream);
819 error1:
820 ;
821 #endif
822 }
823
824 #endif