u_debug.c revision 4c7001462607e6e99e474d6271dd481d3f8f201c
178c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson/**************************************************************************
278c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson *
378c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
478c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson * Copyright (c) 2008 VMware, Inc.
578c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson * All Rights Reserved.
678c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson *
778c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson * Permission is hereby granted, free of charge, to any person obtaining a
878c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson * copy of this software and associated documentation files (the
978c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson * "Software"), to deal in the Software without restriction, including
1078c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson * without limitation the rights to use, copy, modify, merge, publish,
1178c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson * distribute, sub license, and/or sell copies of the Software, and to
1278c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson * permit persons to whom the Software is furnished to do so, subject to
1378c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson * the following conditions:
1478c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson *
1578c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson * The above copyright notice and this permission notice (including the
1678c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson * next paragraph) shall be included in all copies or substantial portions
1778c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson * of the Software.
1878c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson *
1978c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
2078c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2178c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
2278c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
2378c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
2478c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
2578c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2678c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson *
2778c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson **************************************************************************/
2878c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson
2978c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson
3078c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson#include "pipe/p_config.h"
3178c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson
3278c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson#include "pipe/p_compiler.h"
3378c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson#include "os/os_stream.h"
3478c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson#include "util/u_debug.h"
3578c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson#include "pipe/p_format.h"
3678c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson#include "pipe/p_state.h"
3778c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson#include "util/u_inlines.h"
3878c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson#include "util/u_format.h"
3978c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson#include "util/u_memory.h"
4078c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson#include "util/u_string.h"
4178c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson#include "util/u_math.h"
4278c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson#include "util/u_tile.h"
4378c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson#include "util/u_prim.h"
4478c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson#include "util/u_surface.h"
4578c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson
4678c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson#include <limits.h> /* CHAR_BIT */
4778c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson
4878c496fe0fac4c89993109340aec80d1afa3141fIan Parkinsonvoid _debug_vprintf(const char *format, va_list ap)
4978c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson{
5078c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson   /* We buffer until we find a newline. */
5178c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson   static char buf[4096] = {'\0'};
5278c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson   size_t len = strlen(buf);
5378c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson   int ret = util_vsnprintf(buf + len, sizeof(buf) - len, format, ap);
5478c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson   if(ret > (int)(sizeof(buf) - len - 1) || util_strchr(buf + len, '\n')) {
5578c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson      os_log_message(buf);
5678c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson      buf[0] = '\0';
5778c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson   }
5878c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson}
5978c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson
6078c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson
6178c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson#ifdef DEBUG
6278c496fe0fac4c89993109340aec80d1afa3141fIan Parkinsonvoid debug_print_blob( const char *name,
6378c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson                       const void *blob,
6478c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson                       unsigned size )
6578c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson{
6678c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson   const unsigned *ublob = (const unsigned *)blob;
6778c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson   unsigned i;
6878c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson
6978c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson   debug_printf("%s (%d dwords%s)\n", name, size/4,
7078c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson                size%4 ? "... plus a few bytes" : "");
7178c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson
7278c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson   for (i = 0; i < size/4; i++) {
7378c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson      debug_printf("%d:\t%08x\n", i, ublob[i]);
7478c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson   }
7578c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson}
7678c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson#endif
7778c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson
7878c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson
7978c496fe0fac4c89993109340aec80d1afa3141fIan Parkinsonstatic boolean
8078c496fe0fac4c89993109340aec80d1afa3141fIan Parkinsondebug_get_option_should_print(void)
8178c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson{
8278c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson   static boolean first = TRUE;
8378c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson   static boolean value = FALSE;
8478c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson
8578c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson   if (!first)
8678c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson      return value;
8778c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson
88   /* Oh hey this will call into this function,
89    * but its cool since we set first to false
90    */
91   first = FALSE;
92   value = debug_get_bool_option("GALLIUM_PRINT_OPTIONS", FALSE);
93   /* XXX should we print this option? Currently it wont */
94   return value;
95}
96
97const char *
98debug_get_option(const char *name, const char *dfault)
99{
100   const char *result;
101
102   result = os_get_option(name);
103   if(!result)
104      result = dfault;
105
106   if (debug_get_option_should_print())
107      debug_printf("%s: %s = %s\n", __FUNCTION__, name, result ? result : "(null)");
108
109   return result;
110}
111
112boolean
113debug_get_bool_option(const char *name, boolean dfault)
114{
115   const char *str = os_get_option(name);
116   boolean result;
117
118   if(str == NULL)
119      result = dfault;
120   else if(!util_strcmp(str, "n"))
121      result = FALSE;
122   else if(!util_strcmp(str, "no"))
123      result = FALSE;
124   else if(!util_strcmp(str, "0"))
125      result = FALSE;
126   else if(!util_strcmp(str, "f"))
127      result = FALSE;
128   else if(!util_strcmp(str, "F"))
129      result = FALSE;
130   else if(!util_strcmp(str, "false"))
131      result = FALSE;
132   else if(!util_strcmp(str, "FALSE"))
133      result = FALSE;
134   else
135      result = TRUE;
136
137   if (debug_get_option_should_print())
138      debug_printf("%s: %s = %s\n", __FUNCTION__, name, result ? "TRUE" : "FALSE");
139
140   return result;
141}
142
143
144long
145debug_get_num_option(const char *name, long dfault)
146{
147   long result;
148   const char *str;
149
150   str = os_get_option(name);
151   if(!str)
152      result = dfault;
153   else {
154      long sign;
155      char c;
156      c = *str++;
157      if(c == '-') {
158	 sign = -1;
159	 c = *str++;
160      }
161      else {
162	 sign = 1;
163      }
164      result = 0;
165      while('0' <= c && c <= '9') {
166	 result = result*10 + (c - '0');
167	 c = *str++;
168      }
169      result *= sign;
170   }
171
172   if (debug_get_option_should_print())
173      debug_printf("%s: %s = %li\n", __FUNCTION__, name, result);
174
175   return result;
176}
177
178
179unsigned long
180debug_get_flags_option(const char *name,
181                       const struct debug_named_value *flags,
182                       unsigned long dfault)
183{
184   unsigned long result;
185   const char *str;
186   const struct debug_named_value *orig = flags;
187   int namealign = 0;
188
189   str = os_get_option(name);
190   if(!str)
191      result = dfault;
192   else if (!util_strcmp(str, "help")) {
193      result = dfault;
194      _debug_printf("%s: help for %s:\n", __FUNCTION__, name);
195      for (; flags->name; ++flags)
196         namealign = MAX2(namealign, strlen(flags->name));
197      for (flags = orig; flags->name; ++flags)
198         _debug_printf("| %*s [0x%0*lx]%s%s\n", namealign, flags->name,
199                      (int)sizeof(unsigned long)*CHAR_BIT/4, flags->value,
200                      flags->desc ? " " : "", flags->desc ? flags->desc : "");
201   }
202   else {
203      result = 0;
204      while( flags->name ) {
205	 if (!util_strcmp(str, "all") || util_strstr(str, flags->name ))
206	    result |= flags->value;
207	 ++flags;
208      }
209   }
210
211   if (debug_get_option_should_print()) {
212      if (str) {
213         debug_printf("%s: %s = 0x%lx (%s)\n", __FUNCTION__, name, result, str);
214      } else {
215         debug_printf("%s: %s = 0x%lx\n", __FUNCTION__, name, result);
216      }
217   }
218
219   return result;
220}
221
222
223void _debug_assert_fail(const char *expr,
224                        const char *file,
225                        unsigned line,
226                        const char *function)
227{
228   _debug_printf("%s:%u:%s: Assertion `%s' failed.\n", file, line, function, expr);
229#if defined(PIPE_OS_WINDOWS) && !defined(PIPE_SUBSYSTEM_WINDOWS_USER)
230   if (debug_get_bool_option("GALLIUM_ABORT_ON_ASSERT", FALSE))
231#else
232   if (debug_get_bool_option("GALLIUM_ABORT_ON_ASSERT", TRUE))
233#endif
234      os_abort();
235   else
236      _debug_printf("continuing...\n");
237}
238
239
240const char *
241debug_dump_enum(const struct debug_named_value *names,
242                unsigned long value)
243{
244   static char rest[64];
245
246   while(names->name) {
247      if(names->value == value)
248	 return names->name;
249      ++names;
250   }
251
252   util_snprintf(rest, sizeof(rest), "0x%08lx", value);
253   return rest;
254}
255
256
257const char *
258debug_dump_enum_noprefix(const struct debug_named_value *names,
259                         const char *prefix,
260                         unsigned long value)
261{
262   static char rest[64];
263
264   while(names->name) {
265      if(names->value == value) {
266         const char *name = names->name;
267         while (*name == *prefix) {
268            name++;
269            prefix++;
270         }
271         return name;
272      }
273      ++names;
274   }
275
276
277
278   util_snprintf(rest, sizeof(rest), "0x%08lx", value);
279   return rest;
280}
281
282
283const char *
284debug_dump_flags(const struct debug_named_value *names,
285                 unsigned long value)
286{
287   static char output[4096];
288   static char rest[256];
289   int first = 1;
290
291   output[0] = '\0';
292
293   while(names->name) {
294      if((names->value & value) == names->value) {
295	 if (!first)
296	    util_strncat(output, "|", sizeof(output));
297	 else
298	    first = 0;
299	 util_strncat(output, names->name, sizeof(output) - 1);
300	 output[sizeof(output) - 1] = '\0';
301	 value &= ~names->value;
302      }
303      ++names;
304   }
305
306   if (value) {
307      if (!first)
308	 util_strncat(output, "|", sizeof(output));
309      else
310	 first = 0;
311
312      util_snprintf(rest, sizeof(rest), "0x%08lx", value);
313      util_strncat(output, rest, sizeof(output) - 1);
314      output[sizeof(output) - 1] = '\0';
315   }
316
317   if(first)
318      return "0";
319
320   return output;
321}
322
323
324#ifdef DEBUG
325void debug_print_format(const char *msg, unsigned fmt )
326{
327   debug_printf("%s: %s\n", msg, util_format_name(fmt));
328}
329#endif
330
331
332
333static const struct debug_named_value pipe_prim_names[] = {
334#ifdef DEBUG
335   DEBUG_NAMED_VALUE(PIPE_PRIM_POINTS),
336   DEBUG_NAMED_VALUE(PIPE_PRIM_LINES),
337   DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_LOOP),
338   DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_STRIP),
339   DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLES),
340   DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_STRIP),
341   DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_FAN),
342   DEBUG_NAMED_VALUE(PIPE_PRIM_QUADS),
343   DEBUG_NAMED_VALUE(PIPE_PRIM_QUAD_STRIP),
344   DEBUG_NAMED_VALUE(PIPE_PRIM_POLYGON),
345#endif
346   DEBUG_NAMED_VALUE_END
347};
348
349
350const char *u_prim_name( unsigned prim )
351{
352   return debug_dump_enum(pipe_prim_names, prim);
353}
354
355
356
357
358#ifdef DEBUG
359/**
360 * Dump an image to a .raw or .ppm file (depends on OS).
361 * \param format  PIPE_FORMAT_x
362 * \param cpp  bytes per pixel
363 * \param width  width in pixels
364 * \param height height in pixels
365 * \param stride  row stride in bytes
366 */
367void debug_dump_image(const char *prefix,
368                      unsigned format, unsigned cpp,
369                      unsigned width, unsigned height,
370                      unsigned stride,
371                      const void *data)
372{
373#ifdef PIPE_SUBSYSTEM_WINDOWS_DISPLAY
374   static unsigned no = 0;
375   char filename[256];
376   WCHAR wfilename[sizeof(filename)];
377   ULONG_PTR iFile = 0;
378   struct {
379      unsigned format;
380      unsigned cpp;
381      unsigned width;
382      unsigned height;
383   } header;
384   unsigned char *pMap = NULL;
385   unsigned i;
386
387   util_snprintf(filename, sizeof(filename), "\\??\\c:\\%03u%s.raw", ++no, prefix);
388   for(i = 0; i < sizeof(filename); ++i)
389      wfilename[i] = (WCHAR)filename[i];
390
391   pMap = (unsigned char *)EngMapFile(wfilename, sizeof(header) + height*width*cpp, &iFile);
392   if(!pMap)
393      return;
394
395   header.format = format;
396   header.cpp = cpp;
397   header.width = width;
398   header.height = height;
399   memcpy(pMap, &header, sizeof(header));
400   pMap += sizeof(header);
401
402   for(i = 0; i < height; ++i) {
403      memcpy(pMap, (unsigned char *)data + stride*i, cpp*width);
404      pMap += cpp*width;
405   }
406
407   EngUnmapFile(iFile);
408#elif defined(PIPE_OS_UNIX)
409   /* write a ppm file */
410   char filename[256];
411   FILE *f;
412
413   util_snprintf(filename, sizeof(filename), "%s.ppm", prefix);
414
415   f = fopen(filename, "w");
416   if (f) {
417      int i, x, y;
418      int r, g, b;
419      const uint8_t *ptr = (uint8_t *) data;
420
421      /* XXX this is a hack */
422      switch (format) {
423      case PIPE_FORMAT_B8G8R8A8_UNORM:
424         r = 2;
425         g = 1;
426         b = 0;
427         break;
428      default:
429         r = 0;
430         g = 1;
431         b = 1;
432      }
433
434      fprintf(f, "P6\n");
435      fprintf(f, "# ppm-file created by osdemo.c\n");
436      fprintf(f, "%i %i\n", width, height);
437      fprintf(f, "255\n");
438      fclose(f);
439
440      f = fopen(filename, "ab");  /* reopen in binary append mode */
441      for (y = 0; y < height; y++) {
442         for (x = 0; x < width; x++) {
443            i = y * stride + x * cpp;
444            fputc(ptr[i + r], f); /* write red */
445            fputc(ptr[i + g], f); /* write green */
446            fputc(ptr[i + b], f); /* write blue */
447         }
448      }
449      fclose(f);
450   }
451   else {
452      fprintf(stderr, "Can't open %s for writing\n", filename);
453   }
454#endif
455}
456
457/* FIXME: dump resources, not surfaces... */
458void debug_dump_surface(struct pipe_context *pipe,
459                        const char *prefix,
460                        struct pipe_surface *surface)
461{
462   struct pipe_resource *texture;
463   struct pipe_transfer *transfer;
464   void *data;
465
466   if (!surface)
467      return;
468
469   /* XXX: this doesn't necessarily work, as the driver may be using
470    * temporary storage for the surface which hasn't been propagated
471    * back into the texture.  Need to nail down the semantics of views
472    * and transfers a bit better before we can say if extra work needs
473    * to be done here:
474    */
475   texture = surface->texture;
476
477   transfer = pipe_get_transfer(pipe, texture, surface->u.tex.level,
478                                surface->u.tex.first_layer,
479                                PIPE_TRANSFER_READ,
480                                0, 0, surface->width, surface->height);
481
482   data = pipe->transfer_map(pipe, transfer);
483   if(!data)
484      goto error;
485
486   debug_dump_image(prefix,
487                    texture->format,
488                    util_format_get_blocksize(texture->format),
489                    util_format_get_nblocksx(texture->format, surface->width),
490                    util_format_get_nblocksy(texture->format, surface->height),
491                    transfer->stride,
492                    data);
493
494   pipe->transfer_unmap(pipe, transfer);
495error:
496   pipe->transfer_destroy(pipe, transfer);
497}
498
499
500void debug_dump_texture(struct pipe_context *pipe,
501                        const char *prefix,
502                        struct pipe_resource *texture)
503{
504   struct pipe_surface *surface, surf_tmpl;
505
506   if (!texture)
507      return;
508
509   /* XXX for now, just dump image for layer=0, level=0 */
510   memset(&surf_tmpl, 0, sizeof(surf_tmpl));
511   u_surface_default_template(&surf_tmpl, texture, 0 /* no bind flag - not a surface */);
512   surface = pipe->create_surface(pipe, texture, &surf_tmpl);
513   if (surface) {
514      debug_dump_surface(pipe, prefix, surface);
515      pipe->surface_destroy(pipe, surface);
516   }
517}
518
519
520#pragma pack(push,2)
521struct bmp_file_header {
522   uint16_t bfType;
523   uint32_t bfSize;
524   uint16_t bfReserved1;
525   uint16_t bfReserved2;
526   uint32_t bfOffBits;
527};
528#pragma pack(pop)
529
530struct bmp_info_header {
531   uint32_t biSize;
532   int32_t biWidth;
533   int32_t biHeight;
534   uint16_t biPlanes;
535   uint16_t biBitCount;
536   uint32_t biCompression;
537   uint32_t biSizeImage;
538   int32_t biXPelsPerMeter;
539   int32_t biYPelsPerMeter;
540   uint32_t biClrUsed;
541   uint32_t biClrImportant;
542};
543
544struct bmp_rgb_quad {
545   uint8_t rgbBlue;
546   uint8_t rgbGreen;
547   uint8_t rgbRed;
548   uint8_t rgbAlpha;
549};
550
551void
552debug_dump_surface_bmp(struct pipe_context *pipe,
553                       const char *filename,
554                       struct pipe_surface *surface)
555{
556#ifndef PIPE_SUBSYSTEM_WINDOWS_MINIPORT
557   struct pipe_transfer *transfer;
558   struct pipe_resource *texture = surface->texture;
559
560   transfer = pipe_get_transfer(pipe, texture, surface->u.tex.level,
561                                surface->u.tex.first_layer, PIPE_TRANSFER_READ,
562                                0, 0, surface->width, surface->height);
563
564   debug_dump_transfer_bmp(pipe, filename, transfer);
565
566   pipe->transfer_destroy(pipe, transfer);
567#endif
568}
569
570void
571debug_dump_transfer_bmp(struct pipe_context *pipe,
572                        const char *filename,
573                        struct pipe_transfer *transfer)
574{
575#ifndef PIPE_SUBSYSTEM_WINDOWS_MINIPORT
576   float *rgba;
577
578   if (!transfer)
579      goto error1;
580
581   rgba = MALLOC(transfer->box.width *
582		 transfer->box.height *
583		 transfer->box.depth *
584		 4*sizeof(float));
585   if(!rgba)
586      goto error1;
587
588   pipe_get_tile_rgba(pipe, transfer, 0, 0,
589                      transfer->box.width, transfer->box.height,
590                      rgba);
591
592   debug_dump_float_rgba_bmp(filename,
593                             transfer->box.width, transfer->box.height,
594                             rgba, transfer->box.width);
595
596   FREE(rgba);
597error1:
598   ;
599#endif
600}
601
602void
603debug_dump_float_rgba_bmp(const char *filename,
604                          unsigned width, unsigned height,
605                          float *rgba, unsigned stride)
606{
607#ifndef PIPE_SUBSYSTEM_WINDOWS_MINIPORT
608   struct os_stream *stream;
609   struct bmp_file_header bmfh;
610   struct bmp_info_header bmih;
611   unsigned x, y;
612
613   if(!rgba)
614      goto error1;
615
616   bmfh.bfType = 0x4d42;
617   bmfh.bfSize = 14 + 40 + height*width*4;
618   bmfh.bfReserved1 = 0;
619   bmfh.bfReserved2 = 0;
620   bmfh.bfOffBits = 14 + 40;
621
622   bmih.biSize = 40;
623   bmih.biWidth = width;
624   bmih.biHeight = height;
625   bmih.biPlanes = 1;
626   bmih.biBitCount = 32;
627   bmih.biCompression = 0;
628   bmih.biSizeImage = height*width*4;
629   bmih.biXPelsPerMeter = 0;
630   bmih.biYPelsPerMeter = 0;
631   bmih.biClrUsed = 0;
632   bmih.biClrImportant = 0;
633
634   stream = os_file_stream_create(filename);
635   if(!stream)
636      goto error1;
637
638   os_stream_write(stream, &bmfh, 14);
639   os_stream_write(stream, &bmih, 40);
640
641   y = height;
642   while(y--) {
643      float *ptr = rgba + (stride * y * 4);
644      for(x = 0; x < width; ++x)
645      {
646         struct bmp_rgb_quad pixel;
647         pixel.rgbRed   = float_to_ubyte(ptr[x*4 + 0]);
648         pixel.rgbGreen = float_to_ubyte(ptr[x*4 + 1]);
649         pixel.rgbBlue  = float_to_ubyte(ptr[x*4 + 2]);
650         pixel.rgbAlpha = 255;
651         os_stream_write(stream, &pixel, 4);
652      }
653   }
654
655   os_stream_close(stream);
656error1:
657   ;
658#endif
659}
660
661#endif
662