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