add FreeTexImageData hook to help single-copy texturing in drivers
[mesa.git] / src / mesa / main / imports.c
index 7597979e4a781efcd622e810c494e7bb6792beaf..cee54e526b4693b4707c8530a315793565d18dd2 100644 (file)
@@ -32,9 +32,9 @@
 
 /*
  * Mesa 3-D graphics library
- * Version:  5.1
+ * Version:  6.3
  *
- * Copyright (C) 1999-2003  Brian Paul   All Rights Reserved.
+ * Copyright (C) 1999-2004  Brian Paul   All Rights Reserved.
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
 
 #include "imports.h"
 #include "context.h"
+#include "version.h"
 
 
 #define MAXSTRING 4000  /* for vsnprintf() */
 
 #ifdef WIN32
 #define vsnprintf _vsnprintf
-#elif defined(__IBMC__) || defined(__IBMCPP__) || defined(VMS)
+#elif defined(__IBMC__) || defined(__IBMCPP__) || ( defined(__VMS) && __CRTL_VER < 70312000 )
 extern int vsnprintf(char *str, size_t count, const char *fmt, va_list arg);
+#ifdef __VMS
+#include "vsnprintf.c"
+#endif
 #endif
 
 
@@ -350,6 +354,9 @@ static void init_sqrt_table(void)
 }
 
 
+/**
+ * Single precision square root.
+ */
 float
 _mesa_sqrtf( float x )
 {
@@ -489,12 +496,14 @@ _mesa_inv_sqrtf(float n)
 #elif defined(XFree86LOADER) && defined(IN_MODULE)
         return 1.0F / xf86sqrt(n);
 #else
-        return 1.0F / sqrt(n);
+        return (float) (1.0 / sqrt(n));
 #endif
 }
 
 
-/** Wrapper around either pow() or xf86pow() */
+/**
+ * Wrapper around either pow() or xf86pow().
+ */
 double
 _mesa_pow(double x, double y)
 {
@@ -506,7 +515,7 @@ _mesa_pow(double x, double y)
 }
 
 
-/*
+/**
  * Return number of bits set in given GLuint.
  */
 unsigned int
@@ -519,6 +528,145 @@ _mesa_bitcount(unsigned int n)
    return bits;
 }
 
+
+/**
+ * Convert a 4-byte float to a 2-byte half float.
+ * Based on code from:
+ * http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/008786.html
+ */
+GLhalfARB
+_mesa_float_to_half(float val)
+{
+   const int flt = *((int *) (void *) &val);
+   const int flt_m = flt & 0x7fffff;
+   const int flt_e = (flt >> 23) & 0xff;
+   const int flt_s = (flt >> 31) & 0x1;
+   int s, e, m = 0;
+   GLhalfARB result;
+   
+   /* sign bit */
+   s = flt_s;
+
+   /* handle special cases */
+   if ((flt_e == 0) && (flt_m == 0)) {
+      /* zero */
+      /* m = 0; - already set */
+      e = 0;
+   }
+   else if ((flt_e == 0) && (flt_m != 0)) {
+      /* denorm -- denorm float maps to 0 half */
+      /* m = 0; - already set */
+      e = 0;
+   }
+   else if ((flt_e == 0xff) && (flt_m == 0)) {
+      /* infinity */
+      /* m = 0; - already set */
+      e = 31;
+   }
+   else if ((flt_e == 0xff) && (flt_m != 0)) {
+      /* NaN */
+      m = 1;
+      e = 31;
+   }
+   else {
+      /* regular number */
+      const int new_exp = flt_e - 127;
+      if (new_exp < -24) {
+         /* this maps to 0 */
+         /* m = 0; - already set */
+         e = 0;
+      }
+      else if (new_exp < -14) {
+         /* this maps to a denorm */
+         unsigned int exp_val = (unsigned int) (-14 - new_exp); /* 2^-exp_val*/
+         e = 0;
+         switch (exp_val) {
+            case 0:
+               _mesa_warning(NULL,
+                   "float_to_half: logical error in denorm creation!\n");
+               /* m = 0; - already set */
+               break;
+            case 1: m = 512 + (flt_m >> 14); break;
+            case 2: m = 256 + (flt_m >> 15); break;
+            case 3: m = 128 + (flt_m >> 16); break;
+            case 4: m = 64 + (flt_m >> 17); break;
+            case 5: m = 32 + (flt_m >> 18); break;
+            case 6: m = 16 + (flt_m >> 19); break;
+            case 7: m = 8 + (flt_m >> 20); break;
+            case 8: m = 4 + (flt_m >> 21); break;
+            case 9: m = 2 + (flt_m >> 22); break;
+            case 10: m = 1; break;
+         }
+      }
+      else if (new_exp > 15) {
+         /* map this value to infinity */
+         /* m = 0; - already set */
+         e = 31;
+      }
+      else {
+         /* regular */
+         e = new_exp + 15;
+         m = flt_m >> 13;
+      }
+   }
+
+   result = (s << 15) | (e << 10) | m;
+   return result;
+}
+
+
+/**
+ * Convert a 2-byte half float to a 4-byte float.
+ * Based on code from:
+ * http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/008786.html
+ */
+float
+_mesa_half_to_float(GLhalfARB val)
+{
+   /* XXX could also use a 64K-entry lookup table */
+   const int m = val & 0x3ff;
+   const int e = (val >> 10) & 0x1f;
+   const int s = (val >> 15) & 0x1;
+   int flt_m, flt_e, flt_s, flt;
+   float result;
+
+   /* sign bit */
+   flt_s = s;
+
+   /* handle special cases */
+   if ((e == 0) && (m == 0)) {
+      /* zero */
+      flt_m = 0;
+      flt_e = 0;
+   }
+   else if ((e == 0) && (m != 0)) {
+      /* denorm -- denorm half will fit in non-denorm single */
+      const float half_denorm = 1.0f / 16384.0f; /* 2^-14 */
+      float mantissa = ((float) (m)) / 1024.0f;
+      float sign = s ? -1.0f : 1.0f;
+      return sign * mantissa * half_denorm;
+   }
+   else if ((e == 31) && (m == 0)) {
+      /* infinity */
+      flt_e = 0xff;
+      flt_m = 0;
+   }
+   else if ((e == 31) && (m != 0)) {
+      /* NaN */
+      flt_e = 0xff;
+      flt_m = 1;
+   }
+   else {
+      /* regular */
+      flt_e = e + 112;
+      flt_m = m << 13;
+   }
+
+   flt = (flt_s << 31) | (flt_e << 23) | flt_m;
+   result = *((float *) (void *) &flt);
+   return result;
+}
+
 /*@}*/
 
 
@@ -526,7 +674,9 @@ _mesa_bitcount(unsigned int n)
 /** \name Environment vars */
 /*@{*/
 
-/** Wrapper around either () or xf86() */
+/**
+ * Wrapper for getenv().
+ */
 char *
 _mesa_getenv( const char *var )
 {
@@ -756,11 +906,11 @@ _mesa_problem( const GLcontext *ctx, const char *fmtString, ... )
    va_end( args );
 
 #if defined(XFree86LOADER) && defined(IN_MODULE)
-   xf86fprintf(stderr, "Mesa implementation error: %s\n", str);
-   xf86fprintf(stderr, "Please report to the DRI project at dri.sourceforge.net\n");
+   xf86fprintf(stderr, "Mesa %s implementation error: %s\n", MESA_VERSION_STRING, str);
+   xf86fprintf(stderr, "Please report at bugzilla.freedesktop.org\n");
 #else
-   fprintf(stderr, "Mesa implementation error: %s\n", str);
-   fprintf(stderr, "Please report to the Mesa bug database at www.mesa3d.org\n" );
+   fprintf(stderr, "Mesa %s implementation error: %s\n", MESA_VERSION_STRING, str);
+   fprintf(stderr, "Please report at bugzilla.freedesktop.org\n");
 #endif
 }
 
@@ -836,7 +986,7 @@ _mesa_error( GLcontext *ctx, GLenum error, const char *fmtString, ... )
            errstr = "unknown";
            break;
       }
-      _mesa_debug(ctx, "Mesa user error: %s in %s\n", errstr, where);
+      _mesa_debug(ctx, "User error: %s in %s\n", errstr, where);
    }
 
    _mesa_record_error(ctx, error);
@@ -855,6 +1005,7 @@ _mesa_debug( const GLcontext *ctx, const char *fmtString, ... )
 {
    char s[MAXSTRING];
    va_list args;
+   (void) ctx;
    va_start(args, fmtString);
    vsnprintf(s, MAXSTRING, fmtString, args);
    va_end(args);
@@ -945,6 +1096,7 @@ default_sprintf(__GLcontext *gc, char *str, const char *fmt, ...)
 {
    int r;
    va_list args;
+   (void) gc;
    va_start( args, fmt );  
    r = vsprintf( str, fmt, args );
    va_end( args );
@@ -955,6 +1107,7 @@ default_sprintf(__GLcontext *gc, char *str, const char *fmt, ...)
 static void * CAPI
 default_fopen(__GLcontext *gc, const char *path, const char *mode)
 {
+   (void) gc;
    return fopen(path, mode);
 }
 
@@ -962,6 +1115,7 @@ default_fopen(__GLcontext *gc, const char *path, const char *mode)
 static int CAPI
 default_fclose(__GLcontext *gc, void *stream)
 {
+   (void) gc;
    return fclose((FILE *) stream);
 }
 
@@ -971,6 +1125,7 @@ default_fprintf(__GLcontext *gc, void *stream, const char *fmt, ...)
 {
    int r;
    va_list args;
+   (void) gc;
    va_start( args, fmt );  
    r = vfprintf( (FILE *) stream, fmt, args );
    va_end( args );
@@ -983,6 +1138,7 @@ default_fprintf(__GLcontext *gc, void *stream, const char *fmt, ...)
 static __GLdrawablePrivate *
 default_GetDrawablePrivate(__GLcontext *gc)
 {
+   (void) gc;
    return NULL;
 }