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