mesa|mapi: replace _mesa_[v]snprintf with [v]snprintf
[mesa.git] / src / mesa / main / debug.c
index 7dd108c25d5a8ccf41fc49d98089897a2b34859c..a646d4426efc9a5df8c6e67304d99db829255577 100644 (file)
@@ -1,6 +1,5 @@
 /*
  * Mesa 3-D graphics library
- * Version:  6.5
  *
  * Copyright (C) 1999-2005  Brian Paul   All Rights Reserved.
  * Copyright (C) 2009  VMware, Inc.  All Rights Reserved.
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
- * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
- * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
  */
 
+#include <stdio.h>
+#include "errors.h"
 #include "mtypes.h"
 #include "attrib.h"
-#include "colormac.h"
 #include "enums.h"
 #include "formats.h"
 #include "hash.h"
-#include "imports.h"
+#include "util/imports.h"
+#include "macros.h"
 #include "debug.h"
 #include "get.h"
 #include "pixelstore.h"
@@ -51,10 +53,15 @@ tex_target_name(GLenum tgt)
       { GL_TEXTURE_RECTANGLE, "GL_TEXTURE_RECTANGLE" },
       { GL_TEXTURE_1D_ARRAY_EXT, "GL_TEXTURE_1D_ARRAY" },
       { GL_TEXTURE_2D_ARRAY_EXT, "GL_TEXTURE_2D_ARRAY" },
+      { GL_TEXTURE_CUBE_MAP_ARRAY, "GL_TEXTURE_CUBE_MAP_ARRAY" },
+      { GL_TEXTURE_BUFFER, "GL_TEXTURE_BUFFER" },
+      { GL_TEXTURE_2D_MULTISAMPLE, "GL_TEXTURE_2D_MULTISAMPLE" },
+      { GL_TEXTURE_2D_MULTISAMPLE_ARRAY, "GL_TEXTURE_2D_MULTISAMPLE_ARRAY" },
       { GL_TEXTURE_EXTERNAL_OES, "GL_TEXTURE_EXTERNAL_OES" }
    };
    GLuint i;
-   for (i = 0; i < Elements(tex_targets); i++) {
+   STATIC_ASSERT(ARRAY_SIZE(tex_targets) == NUM_TEXTURE_TARGETS);
+   for (i = 0; i < ARRAY_SIZE(tex_targets); i++) {
       if (tex_targets[i].target == tgt)
          return tex_targets[i].name;
    }
@@ -66,7 +73,7 @@ void
 _mesa_print_state( const char *msg, GLuint state )
 {
    _mesa_debug(NULL,
-          "%s: (0x%x) %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
+          "%s: (0x%x) %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
           msg,
           state,
           (state & _NEW_MODELVIEW)       ? "ctx->ModelView, " : "",
@@ -85,40 +92,20 @@ _mesa_print_state( const char *msg, GLuint state )
           (state & _NEW_POLYGONSTIPPLE)  ? "ctx->PolygonStipple, " : "",
           (state & _NEW_SCISSOR)         ? "ctx->Scissor, " : "",
           (state & _NEW_STENCIL)         ? "ctx->Stencil, " : "",
-          (state & _NEW_TEXTURE)         ? "ctx->Texture, " : "",
+          (state & _NEW_TEXTURE_OBJECT)  ? "ctx->Texture(Object), " : "",
           (state & _NEW_TRANSFORM)       ? "ctx->Transform, " : "",
           (state & _NEW_VIEWPORT)        ? "ctx->Viewport, " : "",
-          (state & _NEW_PACKUNPACK)      ? "ctx->Pack/Unpack, " : "",
-          (state & _NEW_ARRAY)           ? "ctx->Array, " : "",
+           (state & _NEW_TEXTURE_STATE)   ? "ctx->Texture(State), " : "",
           (state & _NEW_RENDERMODE)      ? "ctx->RenderMode, " : "",
           (state & _NEW_BUFFERS)         ? "ctx->Visual, ctx->DrawBuffer,, " : "");
 }
 
 
 
-void
-_mesa_print_tri_caps( const char *name, GLuint flags )
-{
-   _mesa_debug(NULL,
-          "%s: (0x%x) %s%s%s%s%s%s%s%s\n",
-          name,
-          flags,
-          (flags & DD_TRI_LIGHT_TWOSIDE)   ? "tri-light-twoside, " : "",
-          (flags & DD_TRI_UNFILLED)        ? "tri-unfilled, " : "",
-          (flags & DD_TRI_STIPPLE)         ? "tri-stipple, " : "",
-          (flags & DD_TRI_OFFSET)          ? "tri-offset, " : "",
-          (flags & DD_TRI_SMOOTH)          ? "tri-smooth, " : "",
-          (flags & DD_LINE_STIPPLE)        ? "line-stipple, " : "",
-          (flags & DD_POINT_SMOOTH)        ? "point-smooth, " : "",
-          (flags & DD_POINT_ATTEN)         ? "point-atten, " : ""
-      );
-}
-
-
 /**
  * Print information about this Mesa version and build options.
  */
-void _mesa_print_info( void )
+void _mesa_print_info( struct gl_context *ctx )
 {
    _mesa_debug(NULL, "Mesa GL_VERSION = %s\n",
           (char *) _mesa_GetString(GL_VERSION));
@@ -126,13 +113,12 @@ void _mesa_print_info( void )
           (char *) _mesa_GetString(GL_RENDERER));
    _mesa_debug(NULL, "Mesa GL_VENDOR = %s\n",
           (char *) _mesa_GetString(GL_VENDOR));
-   _mesa_debug(NULL, "Mesa GL_EXTENSIONS = %s\n",
-          (char *) _mesa_GetString(GL_EXTENSIONS));
-#if defined(THREADS)
-   _mesa_debug(NULL, "Mesa thread-safe: YES\n");
-#else
-   _mesa_debug(NULL, "Mesa thread-safe: NO\n");
-#endif
+
+   /* use ctx as GL_EXTENSIONS will not work on 3.0 or higher
+    * core contexts.
+    */
+   _mesa_debug(NULL, "Mesa GL_EXTENSIONS = %s\n", ctx->Extensions.String);
+
 #if defined(USE_X86_ASM)
    _mesa_debug(NULL, "Mesa x86-optimized: YES\n");
 #else
@@ -154,7 +140,7 @@ void _mesa_print_info( void )
 static void
 set_verbose_flags(const char *str)
 {
-#ifdef DEBUG
+#ifndef NDEBUG
    struct option {
       const char *name;
       GLbitfield flag;
@@ -179,7 +165,7 @@ set_verbose_flags(const char *str)
       return;
 
    MESA_VERBOSE = 0x0;
-   for (i = 0; i < Elements(opts); i++) {
+   for (i = 0; i < ARRAY_SIZE(opts); i++) {
       if (strstr(str, opts[i].name) || strcmp(str, "all") == 0)
          MESA_VERBOSE |= opts[i].flag;
    }
@@ -195,7 +181,7 @@ set_verbose_flags(const char *str)
 static void
 set_debug_flags(const char *str)
 {
-#ifdef DEBUG
+#ifndef NDEBUG
    struct option {
       const char *name;
       GLbitfield flag;
@@ -204,7 +190,8 @@ set_debug_flags(const char *str)
       { "silent", DEBUG_SILENT }, /* turn off debug messages */
       { "flush", DEBUG_ALWAYS_FLUSH }, /* flush after each drawing command */
       { "incomplete_tex", DEBUG_INCOMPLETE_TEXTURE },
-      { "incomplete_fbo", DEBUG_INCOMPLETE_FBO }
+      { "incomplete_fbo", DEBUG_INCOMPLETE_FBO },
+      { "context", DEBUG_CONTEXT } /* force set GL_CONTEXT_FLAG_DEBUG_BIT flag */
    };
    GLuint i;
 
@@ -212,7 +199,7 @@ set_debug_flags(const char *str)
       return;
 
    MESA_DEBUG_FLAGS = 0x0;
-   for (i = 0; i < Elements(opts); i++) {
+   for (i = 0; i < ARRAY_SIZE(opts); i++) {
       if (strstr(str, opts[i].name))
          MESA_DEBUG_FLAGS |= opts[i].flag;
    }
@@ -226,8 +213,8 @@ set_debug_flags(const char *str)
 void 
 _mesa_init_debug( struct gl_context *ctx )
 {
-   set_debug_flags(_mesa_getenv("MESA_DEBUG"));
-   set_verbose_flags(_mesa_getenv("MESA_VERBOSE"));
+   set_debug_flags(getenv("MESA_DEBUG"));
+   set_verbose_flags(getenv("MESA_VERBOSE"));
 }
 
 
@@ -248,6 +235,11 @@ write_ppm(const char *filename, const GLubyte *buffer, int width, int height,
       fprintf(f,"255\n");
       fclose(f);
       f = fopen( filename, "ab" );  /* reopen in binary append mode */
+      if (!f) {
+         fprintf(stderr, "Error while reopening %s in write_ppm()\n",
+                 filename);
+         return;
+      }
       for (y=0; y < height; y++) {
          for (x = 0; x < width; x++) {
             int yy = invert ? (height - 1 - y) : y;
@@ -287,10 +279,12 @@ write_texture_image(struct gl_texture_object *texObj,
       store = ctx->Pack; /* save */
       ctx->Pack = ctx->DefaultPacking;
 
-      ctx->Driver.GetTexImage(ctx, GL_RGBA, GL_UNSIGNED_BYTE, buffer, img);
+      ctx->Driver.GetTexSubImage(ctx,
+                                 0, 0, 0, img->Width, img->Height, img->Depth,
+                                 GL_RGBA, GL_UNSIGNED_BYTE, buffer, img);
 
       /* make filename */
-      _mesa_snprintf(s, sizeof(s), "/tmp/tex%u.l%u.f%u.ppm", texObj->Name, level, face);
+      snprintf(s, sizeof(s), "/tmp/tex%u.l%u.f%u.ppm", texObj->Name, level, face);
 
       printf("  Writing image level %u to %s\n", level, s);
       write_ppm(s, buffer, img->Width, img->Height, 4, 0, 1, 2, GL_FALSE);
@@ -336,8 +330,8 @@ _mesa_write_renderbuffer_image(const struct gl_renderbuffer *rb)
                           format, type, &ctx->DefaultPacking, buffer);
 
    /* make filename */
-   _mesa_snprintf(s, sizeof(s), "/tmp/renderbuffer%u.ppm", rb->Name);
-   _mesa_snprintf(s, sizeof(s), "C:\\renderbuffer%u.ppm", rb->Name);
+   snprintf(s, sizeof(s), "/tmp/renderbuffer%u.ppm", rb->Name);
+   snprintf(s, sizeof(s), "C:\\renderbuffer%u.ppm", rb->Name);
 
    printf("  Writing renderbuffer image to %s\n", s);
 
@@ -426,7 +420,7 @@ dump_renderbuffer(const struct gl_renderbuffer *rb, GLboolean writeImage)
 {
    printf("Renderbuffer %u: %u x %u  IntFormat = %s\n",
          rb->Name, rb->Width, rb->Height,
-         _mesa_lookup_enum_by_nr(rb->InternalFormat));
+         _mesa_enum_to_string(rb->InternalFormat));
    if (writeImage) {
       _mesa_write_renderbuffer_image(rb);
    }
@@ -626,21 +620,20 @@ _mesa_print_texture(struct gl_context *ctx, struct gl_texture_image *img)
    else {
       /* XXX add more formats or make into a new format utility function */
       switch (img->TexFormat) {
-         case MESA_FORMAT_A8:
-         case MESA_FORMAT_L8:
-         case MESA_FORMAT_I8:
+         case MESA_FORMAT_A_UNORM8:
+         case MESA_FORMAT_L_UNORM8:
+         case MESA_FORMAT_I_UNORM8:
             c = 1;
             break;
-         case MESA_FORMAT_AL88:
-         case MESA_FORMAT_AL88_REV:
+         case MESA_FORMAT_LA_UNORM8:
             c = 2;
             break;
-         case MESA_FORMAT_RGB888:
-         case MESA_FORMAT_BGR888:
+         case MESA_FORMAT_BGR_UNORM8:
+         case MESA_FORMAT_RGB_UNORM8:
             c = 3;
             break;
-         case MESA_FORMAT_RGBA8888:
-         case MESA_FORMAT_ARGB8888:
+         case MESA_FORMAT_A8B8G8R8_UNORM:
+         case MESA_FORMAT_B8G8R8A8_UNORM:
             c = 4;
             break;
          default: