gallium/util: added debug_dump_texture() and ppm output
authorBrian Paul <brianp@vmware.com>
Fri, 15 Jan 2010 02:04:40 +0000 (19:04 -0700)
committerBrian Paul <brianp@vmware.com>
Fri, 15 Jan 2010 02:04:42 +0000 (19:04 -0700)
Now we can get dump debug images on Linux too.

src/gallium/auxiliary/util/u_debug.c
src/gallium/auxiliary/util/u_debug.h

index 9b4e6ca2a73693e86d600964d89fd481f34e2c8f..7ee0864d292d3bc0bd4a4a9930d73d061f969337 100644 (file)
@@ -631,6 +631,14 @@ const char *u_prim_name( unsigned prim )
 
 
 #ifdef DEBUG
+/**
+ * Dump an image to a .raw or .ppm file (depends on OS).
+ * \param format  PIPE_FORMAT_x
+ * \param cpp  bytes per pixel
+ * \param width  width in pixels
+ * \param height height in pixels
+ * \param stride  row stride in bytes
+ */
 void debug_dump_image(const char *prefix,
                       unsigned format, unsigned cpp,
                       unsigned width, unsigned height,
@@ -672,6 +680,52 @@ void debug_dump_image(const char *prefix,
    }
       
    EngUnmapFile(iFile);
+#elif defined(PIPE_OS_UNIX)
+   /* write a ppm file */
+   char filename[256];
+   FILE *f;
+
+   util_snprintf(filename, sizeof(filename), "%s.ppm", prefix);
+
+   f = fopen(filename, "w");
+   if (f) {
+      int i, x, y;
+      int r, g, b;
+      const uint8_t *ptr = (uint8_t *) data;
+
+      /* XXX this is a hack */
+      switch (format) {
+      case PIPE_FORMAT_A8R8G8B8_UNORM:
+         r = 2;
+         g = 1;
+         b = 0;
+         break;
+      default:
+         r = 0;
+         g = 1;
+         b = 1;
+      }
+
+      fprintf(f, "P6\n");
+      fprintf(f, "# ppm-file created by osdemo.c\n");
+      fprintf(f, "%i %i\n", width, height);
+      fprintf(f, "255\n");
+      fclose(f);
+
+      f = fopen(filename, "ab");  /* reopen in binary append mode */
+      for (y = 0; y < height; y++) {
+         for (x = 0; x < width; x++) {
+            i = y * stride + x * cpp;
+            fputc(ptr[i + r], f); /* write red */
+            fputc(ptr[i + g], f); /* write green */
+            fputc(ptr[i + b], f); /* write blue */
+         }
+      }
+      fclose(f);
+   }
+   else {
+      fprintf(stderr, "Can't open %s for writing\n", filename);
+   }
 #endif
 }
 
@@ -712,6 +766,27 @@ error:
 }
 
 
+void debug_dump_texture(const char *prefix,
+                        struct pipe_texture *texture)
+{
+   struct pipe_surface *surface;
+   struct pipe_screen *screen;
+
+   if (!texture)
+      return;
+
+   screen = texture->screen;
+
+   /* XXX for now, just dump image for face=0, level=0 */
+   surface = screen->get_tex_surface(screen, texture, 0, 0, 0,
+                                     PIPE_TEXTURE_USAGE_SAMPLER);
+   if (surface) {
+      debug_dump_surface(prefix, surface);
+      screen->tex_surface_destroy(surface);
+   }
+}
+
+
 #pragma pack(push,2)
 struct bmp_file_header {
    uint16_t bfType;
index facc30a5534ca8f744a9fd7180caa2d7f1f8d1e9..131c99153919ad3da5985e181f3b99a3b36c2081 100644 (file)
@@ -354,6 +354,8 @@ debug_memory_end(unsigned long beginning);
 #ifdef DEBUG
 struct pipe_surface;
 struct pipe_transfer;
+struct pipe_texture;
+
 void debug_dump_image(const char *prefix,
                       unsigned format, unsigned cpp,
                       unsigned width, unsigned height,
@@ -361,6 +363,8 @@ void debug_dump_image(const char *prefix,
                       const void *data);
 void debug_dump_surface(const char *prefix,
                         struct pipe_surface *surface);   
+void debug_dump_texture(const char *prefix,
+                        struct pipe_texture *texture);
 void debug_dump_surface_bmp(const char *filename,
                             struct pipe_surface *surface);
 void debug_dump_transfer_bmp(const char *filename,