add FreeTexImageData hook to help single-copy texturing in drivers
[mesa.git] / src / mesa / main / colortab.c
index d0fa323c39b624c79e1cb9d89f4f8c27ab59e75e..4eafe3e899fcfecba3f804169d06e2fca5809984 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Mesa 3-D graphics library
- * Version:  6.1
+ * Version:  6.3
  *
  * Copyright (C) 1999-2004  Brian Paul   All Rights Reserved.
  *
@@ -24,7 +24,7 @@
 
 
 #include "glheader.h"
-#include "imports.h"
+#include "bufferobj.h"
 #include "colortab.h"
 #include "context.h"
 #include "image.h"
@@ -171,6 +171,135 @@ set_component_sizes( struct gl_color_table *table )
 
 
 
+/**
+ * Update/replace all or part of a color table.  Helper function
+ * used by _mesa_ColorTable() and _mesa_ColorSubTable().
+ * The table->Table buffer should already be allocated.
+ * \param start first entry to update
+ * \param count number of entries to update
+ * \param format format of user-provided table data
+ * \param type datatype of user-provided table data
+ * \param data user-provided table data
+ * \param [rgba]Scale - RGBA scale factors
+ * \param [rgba]Bias - RGBA bias factors
+ */
+static void
+store_colortable_entries(GLcontext *ctx, struct gl_color_table *table,
+                        GLsizei start, GLsizei count,
+                        GLenum format, GLenum type, const GLvoid *data,
+                        GLfloat rScale, GLfloat rBias,
+                        GLfloat gScale, GLfloat gBias,
+                        GLfloat bScale, GLfloat bBias,
+                        GLfloat aScale, GLfloat aBias)
+{
+   if (ctx->Unpack.BufferObj->Name) {
+      /* Get/unpack the color table data from a PBO */
+      GLubyte *buf;
+      if (!_mesa_validate_pbo_access(1, &ctx->Unpack, count, 1, 1,
+                                     format, type, data)) {
+         _mesa_error(ctx, GL_INVALID_OPERATION,
+                     "glColor[Sub]Table(bad PBO access)");
+         return;
+      }
+      buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
+                                              GL_READ_ONLY_ARB,
+                                              ctx->Unpack.BufferObj);
+      if (!buf) {
+         _mesa_error(ctx, GL_INVALID_OPERATION,
+                     "glColor[Sub]Table(PBO mapped)");
+         return;
+      }
+      data = ADD_POINTERS(buf, data);
+   }
+
+
+   if (table->Type == GL_FLOAT) {
+      /* convert user-provided data to GLfloat values */
+      GLfloat tempTab[MAX_COLOR_TABLE_SIZE * 4];
+      GLfloat *tableF;
+      GLint i;
+
+      _mesa_unpack_color_span_float(ctx,
+                                    count,         /* number of pixels */
+                                    table->Format, /* dest format */
+                                    tempTab,       /* dest address */
+                                    format, type,  /* src format/type */
+                                    data,          /* src data */
+                                    &ctx->Unpack,
+                                    IMAGE_CLAMP_BIT); /* transfer ops */
+
+      /* the destination */
+      tableF = (GLfloat *) table->Table;
+
+      /* Apply scale & bias & clamp now */
+      switch (table->Format) {
+         case GL_INTENSITY:
+            for (i = 0; i < count; i++) {
+               GLuint j = start + i;
+               tableF[j] = CLAMP(tempTab[i] * rScale + rBias, 0.0F, 1.0F);
+            }
+            break;
+         case GL_LUMINANCE:
+            for (i = 0; i < count; i++) {
+               GLuint j = start + i;
+               tableF[j] = CLAMP(tempTab[i] * rScale + rBias, 0.0F, 1.0F);
+            }
+            break;
+         case GL_ALPHA:
+            for (i = 0; i < count; i++) {
+               GLuint j = start + i;
+               tableF[j] = CLAMP(tempTab[i] * aScale + aBias, 0.0F, 1.0F);
+            }
+            break;
+         case GL_LUMINANCE_ALPHA:
+            for (i = 0; i < count; i++) {
+               GLuint j = start + i;
+               tableF[j*2+0] = CLAMP(tempTab[i*2+0] * rScale + rBias, 0.0F, 1.0F);
+               tableF[j*2+1] = CLAMP(tempTab[i*2+1] * aScale + aBias, 0.0F, 1.0F);
+            }
+            break;
+         case GL_RGB:
+            for (i = 0; i < count; i++) {
+               GLuint j = start + i;
+               tableF[j*3+0] = CLAMP(tempTab[i*3+0] * rScale + rBias, 0.0F, 1.0F);
+               tableF[j*3+1] = CLAMP(tempTab[i*3+1] * gScale + gBias, 0.0F, 1.0F);
+               tableF[j*3+2] = CLAMP(tempTab[i*3+2] * bScale + bBias, 0.0F, 1.0F);
+            }
+            break;
+         case GL_RGBA:
+            for (i = 0; i < count; i++) {
+               GLuint j = start + i;
+               tableF[j*4+0] = CLAMP(tempTab[i*4+0] * rScale + rBias, 0.0F, 1.0F);
+               tableF[j*4+1] = CLAMP(tempTab[i*4+1] * gScale + gBias, 0.0F, 1.0F);
+               tableF[j*4+2] = CLAMP(tempTab[i*4+2] * bScale + bBias, 0.0F, 1.0F);
+               tableF[j*4+3] = CLAMP(tempTab[i*4+3] * aScale + aBias, 0.0F, 1.0F);
+            }
+            break;
+         default:
+            _mesa_problem(ctx, "Bad format in store_colortable_entries");
+            return;
+         }
+   }
+   else {
+      /* non-float (GLchan) */
+      const GLint comps = _mesa_components_in_format(table->Format);
+      GLchan *dest = (GLchan *) table->Table + start * comps;
+      _mesa_unpack_color_span_chan(ctx, count,         /* number of entries */
+                                  table->Format,      /* dest format */
+                                  dest,               /* dest address */
+                                   format, type, data, /* src data */
+                                  &ctx->Unpack,
+                                  0);                 /* transfer ops */
+   }
+
+   if (ctx->Unpack.BufferObj->Name) {
+      ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
+                              ctx->Unpack.BufferObj);
+   }
+}
+
+
+
 void GLAPIENTRY
 _mesa_ColorTable( GLenum target, GLenum internalFormat,
                   GLsizei width, GLenum format, GLenum type,
@@ -314,7 +443,7 @@ _mesa_ColorTable( GLenum target, GLenum internalFormat,
 
    assert(table);
 
-   if (!_mesa_is_legal_format_and_type(format, type) ||
+   if (!_mesa_is_legal_format_and_type(ctx, format, type) ||
        format == GL_INTENSITY) {
       _mesa_error(ctx, GL_INVALID_OPERATION, "glColorTable(format or type)");
       return;
@@ -365,82 +494,30 @@ _mesa_ColorTable( GLenum target, GLenum internalFormat,
          FREE(table->Table);
          table->Table = NULL;
       }
+
       if (width > 0) {
          if (tableType == GL_FLOAT) {
-            GLfloat tempTab[MAX_COLOR_TABLE_SIZE * 4];
-            GLfloat *tableF;
-            GLint i;
-
-            _mesa_unpack_float_color_span(ctx, width, table->Format,
-                                          tempTab,  /* dest */
-                                          format, type, data, &ctx->Unpack,
-                                          0, GL_FALSE);
-
            table->Type = GL_FLOAT;
-            table->Table = MALLOC(comps * width * sizeof(GLfloat));
-            if (!table->Table) {
-               _mesa_error(ctx, GL_OUT_OF_MEMORY, "glColorTable");
-               return;
-            }
-
-            tableF = (GLfloat *) table->Table;
-
-            switch (table->Format) {
-               case GL_INTENSITY:
-                  for (i = 0; i < width; i++) {
-                     tableF[i] = CLAMP(tempTab[i] * rScale + rBias, 0.0F, 1.0F);
-                  }
-                  break;
-               case GL_LUMINANCE:
-                  for (i = 0; i < width; i++) {
-                     tableF[i] = CLAMP(tempTab[i] * rScale + rBias, 0.0F, 1.0F);
-                  }
-                  break;
-               case GL_ALPHA:
-                  for (i = 0; i < width; i++) {
-                     tableF[i] = CLAMP(tempTab[i] * aScale + aBias, 0.0F, 1.0F);
-                  }
-                  break;
-               case GL_LUMINANCE_ALPHA:
-                  for (i = 0; i < width; i++) {
-                     tableF[i*2+0] = CLAMP(tempTab[i*2+0] * rScale + rBias, 0.0F, 1.0F);
-                     tableF[i*2+1] = CLAMP(tempTab[i*2+1] * aScale + aBias, 0.0F, 1.0F);
-                  }
-                  break;
-               case GL_RGB:
-                  for (i = 0; i < width; i++) {
-                     tableF[i*3+0] = CLAMP(tempTab[i*3+0] * rScale + rBias, 0.0F, 1.0F);
-                     tableF[i*3+1] = CLAMP(tempTab[i*3+1] * gScale + gBias, 0.0F, 1.0F);
-                     tableF[i*3+2] = CLAMP(tempTab[i*3+2] * bScale + bBias, 0.0F, 1.0F);
-                  }
-                  break;
-               case GL_RGBA:
-                  for (i = 0; i < width; i++) {
-                     tableF[i*4+0] = CLAMP(tempTab[i*4+0] * rScale + rBias, 0.0F, 1.0F);
-                     tableF[i*4+1] = CLAMP(tempTab[i*4+1] * gScale + gBias, 0.0F, 1.0F);
-                     tableF[i*4+2] = CLAMP(tempTab[i*4+2] * bScale + bBias, 0.0F, 1.0F);
-                     tableF[i*4+3] = CLAMP(tempTab[i*4+3] * aScale + aBias, 0.0F, 1.0F);
-                  }
-                  break;
-               default:
-                  _mesa_problem(ctx, "Bad format in _mesa_ColorTable");
-                  return;
-            }
-         }
-         else {
-            /* store GLchan table */
+           table->Table = MALLOC(comps * width * sizeof(GLfloat));
+        }
+        else {
            table->Type = CHAN_TYPE;
             table->Table = MALLOC(comps * width * sizeof(GLchan));
-            if (!table->Table) {
-               _mesa_error(ctx, GL_OUT_OF_MEMORY, "glColorTable");
-               return;
-            }
-            _mesa_unpack_chan_color_span(ctx, width, table->Format,
-                                         (GLchan *) table->Table,  /* dest */
-                                         format, type, data,
-                                         &ctx->Unpack, 0);
-         } /* type==GL_FLOAT */
-      } /* width > 0 */
+        }
+
+        if (!table->Table) {
+           _mesa_error(ctx, GL_OUT_OF_MEMORY, "glColorTable");
+           return;
+        }
+
+        store_colortable_entries(ctx, table,
+                                 0, width,  /* start, count */
+                                 format, type, data,
+                                 rScale, rBias,
+                                 gScale, gBias,
+                                 bScale, bBias,
+                                 aScale, aBias);
+      }
    } /* proxy */
 
    if (texObj || target == GL_SHARED_TEXTURE_PALETTE_EXT) {
@@ -466,7 +543,6 @@ _mesa_ColorSubTable( GLenum target, GLsizei start,
    struct gl_color_table *table = NULL;
    GLfloat rScale = 1.0, gScale = 1.0, bScale = 1.0, aScale = 1.0;
    GLfloat rBias  = 0.0, gBias  = 0.0, bBias  = 0.0, aBias  = 0.0;
-   GLint comps;
    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
 
    switch (target) {
@@ -548,7 +624,7 @@ _mesa_ColorSubTable( GLenum target, GLsizei start,
 
    assert(table);
 
-   if (!_mesa_is_legal_format_and_type(format, type) ||
+   if (!_mesa_is_legal_format_and_type(ctx, format, type) ||
        format == GL_INTENSITY) {
       _mesa_error(ctx, GL_INVALID_OPERATION, "glColorSubTable(format or type)");
       return;
@@ -559,8 +635,8 @@ _mesa_ColorSubTable( GLenum target, GLsizei start,
       return;
    }
 
-   comps = _mesa_components_in_format(table->Format);
-   assert(comps > 0);  /* error should have been caught sooner */
+   /* error should have been caught sooner */
+   assert(_mesa_components_in_format(table->Format) > 0);
 
    if (start + count > (GLint) table->Size) {
       _mesa_error(ctx, GL_INVALID_VALUE, "glColorSubTable(count)");
@@ -572,73 +648,12 @@ _mesa_ColorSubTable( GLenum target, GLsizei start,
       return;
    }
 
-   if (table->Type != GL_FLOAT) {
-      GLchan *dest = (GLchan *) table->Table + start * comps * sizeof(GLchan);
-      _mesa_unpack_chan_color_span(ctx, count, table->Format, dest,
-                                   format, type, data, &ctx->Unpack, 0);
-   }
-   else {
-      GLfloat tempTab[MAX_COLOR_TABLE_SIZE * 4];
-      GLfloat *tableF;
-      GLint i;
-
-      ASSERT(table->Type == GL_FLOAT);
-
-      _mesa_unpack_float_color_span(ctx, count, table->Format,
-                                    tempTab,  /* dest */
-                                    format, type, data, &ctx->Unpack,
-                                    0, GL_FALSE);
-
-      tableF = (GLfloat *) table->Table;
-
-      switch (table->Format) {
-         case GL_INTENSITY:
-            for (i = 0; i < count; i++) {
-               GLuint j = start + i;
-               tableF[j] = CLAMP(tempTab[i] * rScale + rBias, 0.0F, 1.0F);
-            }
-            break;
-         case GL_LUMINANCE:
-            for (i = 0; i < count; i++) {
-               GLuint j = start + i;
-               tableF[j] = CLAMP(tempTab[i] * rScale + rBias, 0.0F, 1.0F);
-            }
-            break;
-         case GL_ALPHA:
-            for (i = 0; i < count; i++) {
-               GLuint j = start + i;
-               tableF[j] = CLAMP(tempTab[i] * aScale + aBias, 0.0F, 1.0F);
-            }
-            break;
-         case GL_LUMINANCE_ALPHA:
-            for (i = 0; i < count; i++) {
-               GLuint j = start + i;
-               tableF[j*2+0] = CLAMP(tempTab[i*2+0] * rScale + rBias, 0.0F, 1.0F);
-               tableF[j*2+1] = CLAMP(tempTab[i*2+1] * aScale + aBias, 0.0F, 1.0F);
-            }
-            break;
-         case GL_RGB:
-            for (i = 0; i < count; i++) {
-               GLuint j = start + i;
-               tableF[j*3+0] = CLAMP(tempTab[i*3+0] * rScale + rBias, 0.0F, 1.0F);
-               tableF[j*3+1] = CLAMP(tempTab[i*3+1] * gScale + gBias, 0.0F, 1.0F);
-               tableF[j*3+2] = CLAMP(tempTab[i*3+2] * bScale + bBias, 0.0F, 1.0F);
-            }
-            break;
-         case GL_RGBA:
-            for (i = 0; i < count; i++) {
-               GLuint j = start + i;
-               tableF[j*4+0] = CLAMP(tempTab[i*4+0] * rScale + rBias, 0.0F, 1.0F);
-               tableF[j*4+1] = CLAMP(tempTab[i*4+1] * gScale + gBias, 0.0F, 1.0F);
-               tableF[j*4+2] = CLAMP(tempTab[i*4+2] * bScale + bBias, 0.0F, 1.0F);
-               tableF[j*4+3] = CLAMP(tempTab[i*4+3] * aScale + aBias, 0.0F, 1.0F);
-            }
-            break;
-         default:
-            _mesa_problem(ctx, "Bad format in _mesa_ColorSubTable");
-            return;
-         }
-   }
+   store_colortable_entries(ctx, table, start, count,
+                           format, type, data,
+                           rScale, rBias,
+                           gScale, gBias,
+                           bScale, bBias,
+                           aScale, aBias);
 
    if (texObj || target == GL_SHARED_TEXTURE_PALETTE_EXT) {
       /* per-texture object palette */
@@ -652,7 +667,6 @@ _mesa_ColorSubTable( GLenum target, GLsizei start,
 
 
 
-/* XXX not tested */
 void GLAPIENTRY
 _mesa_CopyColorTable(GLenum target, GLenum internalformat,
                      GLint x, GLint y, GLsizei width)
@@ -666,7 +680,6 @@ _mesa_CopyColorTable(GLenum target, GLenum internalformat,
 
 
 
-/* XXX not tested */
 void GLAPIENTRY
 _mesa_CopyColorSubTable(GLenum target, GLsizei start,
                         GLint x, GLint y, GLsizei width)
@@ -874,8 +887,34 @@ _mesa_GetColorTable( GLenum target, GLenum format,
          return;
    }
 
-   _mesa_pack_rgba_span(ctx, table->Size, (const GLchan (*)[4]) rgba,
+   if (ctx->Pack.BufferObj->Name) {
+      /* pack color table into PBO */
+      GLubyte *buf;
+      if (!_mesa_validate_pbo_access(1, &ctx->Pack, table->Size, 1, 1,
+                                     format, type, data)) {
+         _mesa_error(ctx, GL_INVALID_OPERATION,
+                     "glGetColorTable(invalid PBO access)");
+         return;
+      }
+      buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
+                                              GL_WRITE_ONLY_ARB,
+                                              ctx->Pack.BufferObj);
+      if (!buf) {
+         /* buffer is already mapped - that's an error */
+         _mesa_error(ctx, GL_INVALID_OPERATION,
+                     "glGetColorTable(PBO is mapped)");
+         return;
+      }
+      data = ADD_POINTERS(buf, data);
+   }
+
+   _mesa_pack_rgba_span_chan(ctx, table->Size, (const GLchan (*)[4]) rgba,
                         format, type, data, &ctx->Pack, GL_FALSE);
+
+   if (ctx->Pack.BufferObj->Name) {
+      ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
+                              ctx->Pack.BufferObj);
+   }
 }