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