Merge branch 'mesa_7_5_branch'
[mesa.git] / src / mesa / main / histogram.c
index e96c990f584938989d71b9c63c40fa5c767a710c..5fee4fd0e34520219d6989b5d9daf1fc07a87b33 100644 (file)
@@ -1,10 +1,8 @@
-/* $Id: histogram.c,v 1.13 2003/03/25 02:26:29 brianp Exp $ */
-
 /*
  * 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 "glheader.h"
+#include "bufferobj.h"
 #include "colormac.h"
 #include "context.h"
 #include "image.h"
 #include "histogram.h"
 
 
-/**********************************************************************
- * Internal functions
- */
-
-
-/*
- * Update the min/max values from an array of fragment colors.
- */
-void
-_mesa_update_minmax(GLcontext *ctx, GLuint n, const GLfloat rgba[][4])
-{
-   GLuint i;
-   for (i = 0; i < n; i++) {
-      /* update mins */
-      if (rgba[i][RCOMP] < ctx->MinMax.Min[RCOMP])
-         ctx->MinMax.Min[RCOMP] = rgba[i][RCOMP];
-      if (rgba[i][GCOMP] < ctx->MinMax.Min[GCOMP])
-         ctx->MinMax.Min[GCOMP] = rgba[i][GCOMP];
-      if (rgba[i][BCOMP] < ctx->MinMax.Min[BCOMP])
-         ctx->MinMax.Min[BCOMP] = rgba[i][BCOMP];
-      if (rgba[i][ACOMP] < ctx->MinMax.Min[ACOMP])
-         ctx->MinMax.Min[ACOMP] = rgba[i][ACOMP];
-
-      /* update maxs */
-      if (rgba[i][RCOMP] > ctx->MinMax.Max[RCOMP])
-         ctx->MinMax.Max[RCOMP] = rgba[i][RCOMP];
-      if (rgba[i][GCOMP] > ctx->MinMax.Max[GCOMP])
-         ctx->MinMax.Max[GCOMP] = rgba[i][GCOMP];
-      if (rgba[i][BCOMP] > ctx->MinMax.Max[BCOMP])
-         ctx->MinMax.Max[BCOMP] = rgba[i][BCOMP];
-      if (rgba[i][ACOMP] > ctx->MinMax.Max[ACOMP])
-         ctx->MinMax.Max[ACOMP] = rgba[i][ACOMP];
-   }
-}
-
-
-/*
- * Update the histogram values from an array of fragment colors.
- */
-void
-_mesa_update_histogram(GLcontext *ctx, GLuint n, const GLfloat rgba[][4])
-{
-   const GLint max = ctx->Histogram.Width - 1;
-   GLfloat w = (GLfloat) max;
-   GLuint i;
-
-   if (ctx->Histogram.Width == 0)
-      return;
-
-   for (i = 0; i < n; i++) {
-      GLint ri = IROUND(rgba[i][RCOMP] * w);
-      GLint gi = IROUND(rgba[i][GCOMP] * w);
-      GLint bi = IROUND(rgba[i][BCOMP] * w);
-      GLint ai = IROUND(rgba[i][ACOMP] * w);
-      ri = CLAMP(ri, 0, max);
-      gi = CLAMP(gi, 0, max);
-      bi = CLAMP(bi, 0, max);
-      ai = CLAMP(ai, 0, max);
-      ctx->Histogram.Count[ri][RCOMP]++;
-      ctx->Histogram.Count[gi][GCOMP]++;
-      ctx->Histogram.Count[bi][BCOMP]++;
-      ctx->Histogram.Count[ai][ACOMP]++;
-   }
-}
-
 
 /*
  * XXX the packed pixel formats haven't been tested.
@@ -248,6 +182,23 @@ pack_histogram( GLcontext *ctx,
             }
          }
          break;
+      case GL_HALF_FLOAT_ARB:
+         {
+            /* temporarily store as GLuints */
+            GLuint temp[4*HISTOGRAM_TABLE_SIZE];
+            GLhalfARB *dst = (GLhalfARB *) destination;
+            GLuint i;
+            /* get GLuint values */
+            PACK_MACRO(GLuint);
+            /* convert to GLhalf */
+            for (i = 0; i < n * comps; i++) {
+               dst[i] = _mesa_float_to_half((GLfloat) temp[i]);
+            }
+            if (packing->SwapBytes) {
+               _mesa_swap2((GLushort *) dst, n * comps);
+            }
+         }
+         break;
       case GL_UNSIGNED_BYTE_3_3_2:
          if (format == GL_RGB) {
             GLubyte *dst = (GLubyte *) destination;
@@ -663,7 +614,7 @@ base_histogram_format( GLenum format )
  */
 
 
-void
+void GLAPIENTRY
 _mesa_GetMinmax(GLenum target, GLboolean reset, GLenum format, GLenum type, GLvoid *values)
 {
    GET_CURRENT_CONTEXT(ctx);
@@ -679,36 +630,48 @@ _mesa_GetMinmax(GLenum target, GLboolean reset, GLenum format, GLenum type, GLvo
       return;
    }
 
-   if (!_mesa_is_legal_format_and_type(format, type)) {
-      _mesa_error(ctx, GL_INVALID_OPERATION, "glGetMinmax(format or type)");
-      return;
+   if (format != GL_RED &&
+       format != GL_GREEN &&
+       format != GL_BLUE &&
+       format != GL_ALPHA &&
+       format != GL_RGB &&
+       format != GL_BGR &&
+       format != GL_RGBA &&
+       format != GL_BGRA &&
+       format != GL_ABGR_EXT &&
+       format != GL_LUMINANCE &&
+       format != GL_LUMINANCE_ALPHA) {
+      _mesa_error(ctx, GL_INVALID_ENUM, "glGetMinMax(format)");
    }
 
-   if (type != GL_UNSIGNED_BYTE &&
-       type != GL_BYTE &&
-       type != GL_UNSIGNED_SHORT &&
-       type != GL_SHORT &&
-       type != GL_UNSIGNED_INT &&
-       type != GL_INT &&
-       type != GL_FLOAT &&
-       type != GL_UNSIGNED_BYTE_3_3_2 &&
-       type != GL_UNSIGNED_BYTE_2_3_3_REV &&
-       type != GL_UNSIGNED_SHORT_5_6_5 &&
-       type != GL_UNSIGNED_SHORT_5_6_5_REV &&
-       type != GL_UNSIGNED_SHORT_4_4_4_4 &&
-       type != GL_UNSIGNED_SHORT_4_4_4_4_REV &&
-       type != GL_UNSIGNED_SHORT_5_5_5_1 &&
-       type != GL_UNSIGNED_SHORT_1_5_5_5_REV &&
-       type != GL_UNSIGNED_INT_8_8_8_8 &&
-       type != GL_UNSIGNED_INT_8_8_8_8_REV &&
-       type != GL_UNSIGNED_INT_10_10_10_2 &&
-       type != GL_UNSIGNED_INT_2_10_10_10_REV) {
-      _mesa_error(ctx, GL_INVALID_ENUM, "glGetMinmax(type)");
+   if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
+      _mesa_error(ctx, GL_INVALID_OPERATION, "glGetMinmax(format or type)");
       return;
    }
 
-   if (!values)
+   if (ctx->Pack.BufferObj->Name) {
+      /* pack min/max values into a PBO */
+      GLubyte *buf;
+      if (!_mesa_validate_pbo_access(1, &ctx->Pack, 2, 1, 1,
+                                     format, type, values)) {
+         _mesa_error(ctx, GL_INVALID_OPERATION,
+                     "glGetMinMax(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,"glGetMinMax(PBO is mapped)");
+         return;
+      }
+      values = ADD_POINTERS(buf, values);
+   }
+   else if (!values) {
+      /* not an error */
       return;
+   }
 
    {
       GLfloat minmax[2][4];
@@ -720,8 +683,13 @@ _mesa_GetMinmax(GLenum target, GLboolean reset, GLenum format, GLenum type, GLvo
       minmax[1][GCOMP] = CLAMP(ctx->MinMax.Max[GCOMP], 0.0F, 1.0F);
       minmax[1][BCOMP] = CLAMP(ctx->MinMax.Max[BCOMP], 0.0F, 1.0F);
       minmax[1][ACOMP] = CLAMP(ctx->MinMax.Max[ACOMP], 0.0F, 1.0F);
-      _mesa_pack_float_rgba_span(ctx, 2, (CONST GLfloat (*)[4]) minmax,
-                                 format, type, values, &ctx->Pack, 0);
+      _mesa_pack_rgba_span_float(ctx, 2, minmax,
+                                 format, type, values, &ctx->Pack, 0x0);
+   }
+
+   if (ctx->Pack.BufferObj->Name) {
+      ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
+                              ctx->Pack.BufferObj);
    }
 
    if (reset) {
@@ -730,7 +698,7 @@ _mesa_GetMinmax(GLenum target, GLboolean reset, GLenum format, GLenum type, GLvo
 }
 
 
-void
+void GLAPIENTRY
 _mesa_GetHistogram(GLenum target, GLboolean reset, GLenum format, GLenum type, GLvoid *values)
 {
    GET_CURRENT_CONTEXT(ctx);
@@ -746,41 +714,58 @@ _mesa_GetHistogram(GLenum target, GLboolean reset, GLenum format, GLenum type, G
       return;
    }
 
-   if (!_mesa_is_legal_format_and_type(format, type)) {
-      _mesa_error(ctx, GL_INVALID_OPERATION, "glGetHistogram(format or type)");
-      return;
+   if (format != GL_RED &&
+       format != GL_GREEN &&
+       format != GL_BLUE &&
+       format != GL_ALPHA &&
+       format != GL_RGB &&
+       format != GL_BGR &&
+       format != GL_RGBA &&
+       format != GL_BGRA &&
+       format != GL_ABGR_EXT &&
+       format != GL_LUMINANCE &&
+       format != GL_LUMINANCE_ALPHA) {
+      _mesa_error(ctx, GL_INVALID_ENUM, "glGetHistogram(format)");
    }
 
-   if (type != GL_UNSIGNED_BYTE &&
-       type != GL_BYTE &&
-       type != GL_UNSIGNED_SHORT &&
-       type != GL_SHORT &&
-       type != GL_UNSIGNED_INT &&
-       type != GL_INT &&
-       type != GL_FLOAT &&
-       type != GL_UNSIGNED_BYTE_3_3_2 &&
-       type != GL_UNSIGNED_BYTE_2_3_3_REV &&
-       type != GL_UNSIGNED_SHORT_5_6_5 &&
-       type != GL_UNSIGNED_SHORT_5_6_5_REV &&
-       type != GL_UNSIGNED_SHORT_4_4_4_4 &&
-       type != GL_UNSIGNED_SHORT_4_4_4_4_REV &&
-       type != GL_UNSIGNED_SHORT_5_5_5_1 &&
-       type != GL_UNSIGNED_SHORT_1_5_5_5_REV &&
-       type != GL_UNSIGNED_INT_8_8_8_8 &&
-       type != GL_UNSIGNED_INT_8_8_8_8_REV &&
-       type != GL_UNSIGNED_INT_10_10_10_2 &&
-       type != GL_UNSIGNED_INT_2_10_10_10_REV) {
-      _mesa_error(ctx, GL_INVALID_ENUM, "glGetHistogram(type)");
+   if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
+      _mesa_error(ctx, GL_INVALID_OPERATION, "glGetHistogram(format or type)");
       return;
    }
 
-   if (!values)
+   if (ctx->Pack.BufferObj->Name) {
+      /* pack min/max values into a PBO */
+      GLubyte *buf;
+      if (!_mesa_validate_pbo_access(1, &ctx->Pack, ctx->Histogram.Width, 1, 1,
+                                     format, type, values)) {
+         _mesa_error(ctx, GL_INVALID_OPERATION,
+                     "glGetHistogram(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,"glGetHistogram(PBO is mapped)");
+         return;
+      }
+      values = ADD_POINTERS(buf, values);
+   }
+   else if (!values) {
+      /* not an error */
       return;
+   }
 
    pack_histogram(ctx, ctx->Histogram.Width,
                   (CONST GLuint (*)[4]) ctx->Histogram.Count,
                   format, type, values, &ctx->Pack);
 
+   if (ctx->Pack.BufferObj->Name) {
+      ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
+                              ctx->Pack.BufferObj);
+   }
+
    if (reset) {
       GLuint i;
       for (i = 0; i < HISTOGRAM_TABLE_SIZE; i++) {
@@ -793,7 +778,7 @@ _mesa_GetHistogram(GLenum target, GLboolean reset, GLenum format, GLenum type, G
 }
 
 
-void
+void GLAPIENTRY
 _mesa_GetHistogramParameterfv(GLenum target, GLenum pname, GLfloat *params)
 {
    GET_CURRENT_CONTEXT(ctx);
@@ -840,7 +825,7 @@ _mesa_GetHistogramParameterfv(GLenum target, GLenum pname, GLfloat *params)
 }
 
 
-void
+void GLAPIENTRY
 _mesa_GetHistogramParameteriv(GLenum target, GLenum pname, GLint *params)
 {
    GET_CURRENT_CONTEXT(ctx);
@@ -887,7 +872,7 @@ _mesa_GetHistogramParameteriv(GLenum target, GLenum pname, GLint *params)
 }
 
 
-void
+void GLAPIENTRY
 _mesa_GetMinmaxParameterfv(GLenum target, GLenum pname, GLfloat *params)
 {
    GET_CURRENT_CONTEXT(ctx);
@@ -913,7 +898,7 @@ _mesa_GetMinmaxParameterfv(GLenum target, GLenum pname, GLfloat *params)
 }
 
 
-void
+void GLAPIENTRY
 _mesa_GetMinmaxParameteriv(GLenum target, GLenum pname, GLint *params)
 {
    GET_CURRENT_CONTEXT(ctx);
@@ -939,7 +924,7 @@ _mesa_GetMinmaxParameteriv(GLenum target, GLenum pname, GLint *params)
 }
 
 
-void
+void GLAPIENTRY
 _mesa_Histogram(GLenum target, GLsizei width, GLenum internalFormat, GLboolean sink)
 {
    GLuint i;
@@ -970,7 +955,7 @@ _mesa_Histogram(GLenum target, GLsizei width, GLenum internalFormat, GLboolean s
       }
    }
 
-   if (width != 0 && _mesa_bitcount(width) != 1) {
+   if (width != 0 && !_mesa_is_pow_two(width)) {
       if (target == GL_PROXY_HISTOGRAM) {
          error = GL_TRUE;
       }
@@ -990,6 +975,8 @@ _mesa_Histogram(GLenum target, GLsizei width, GLenum internalFormat, GLboolean s
       }
    }
 
+   FLUSH_VERTICES(ctx, _NEW_PIXEL);
+
    /* reset histograms */
    for (i = 0; i < HISTOGRAM_TABLE_SIZE; i++) {
       ctx->Histogram.Count[i][0] = 0;
@@ -1017,12 +1004,10 @@ _mesa_Histogram(GLenum target, GLsizei width, GLenum internalFormat, GLboolean s
       ctx->Histogram.AlphaSize     = 8 * sizeof(GLuint);
       ctx->Histogram.LuminanceSize = 8 * sizeof(GLuint);
    }
-
-   ctx->NewState |= _NEW_PIXEL;
 }
 
 
-void
+void GLAPIENTRY
 _mesa_Minmax(GLenum target, GLenum internalFormat, GLboolean sink)
 {
    GET_CURRENT_CONTEXT(ctx);
@@ -1050,7 +1035,7 @@ _mesa_Minmax(GLenum target, GLenum internalFormat, GLboolean sink)
 }
 
 
-void
+void GLAPIENTRY
 _mesa_ResetHistogram(GLenum target)
 {
    GLuint i;
@@ -1073,12 +1058,10 @@ _mesa_ResetHistogram(GLenum target)
       ctx->Histogram.Count[i][2] = 0;
       ctx->Histogram.Count[i][3] = 0;
    }
-
-   ctx->NewState |= _NEW_PIXEL;
 }
 
 
-void
+void GLAPIENTRY
 _mesa_ResetMinmax(GLenum target)
 {
    GET_CURRENT_CONTEXT(ctx);
@@ -1098,5 +1081,39 @@ _mesa_ResetMinmax(GLenum target)
    ctx->MinMax.Min[GCOMP] = 1000;    ctx->MinMax.Max[GCOMP] = -1000;
    ctx->MinMax.Min[BCOMP] = 1000;    ctx->MinMax.Max[BCOMP] = -1000;
    ctx->MinMax.Min[ACOMP] = 1000;    ctx->MinMax.Max[ACOMP] = -1000;
-   ctx->NewState |= _NEW_PIXEL;
+}
+
+
+
+/**********************************************************************/
+/*****                      Initialization                        *****/
+/**********************************************************************/
+
+void _mesa_init_histogram( GLcontext * ctx )
+{
+   int i;
+
+   /* Histogram group */
+   ctx->Histogram.Width = 0;
+   ctx->Histogram.Format = GL_RGBA;
+   ctx->Histogram.Sink = GL_FALSE;
+   ctx->Histogram.RedSize       = 0;
+   ctx->Histogram.GreenSize     = 0;
+   ctx->Histogram.BlueSize      = 0;
+   ctx->Histogram.AlphaSize     = 0;
+   ctx->Histogram.LuminanceSize = 0;
+   for (i = 0; i < HISTOGRAM_TABLE_SIZE; i++) {
+      ctx->Histogram.Count[i][0] = 0;
+      ctx->Histogram.Count[i][1] = 0;
+      ctx->Histogram.Count[i][2] = 0;
+      ctx->Histogram.Count[i][3] = 0;
+   }
+
+   /* Min/Max group */
+   ctx->MinMax.Format = GL_RGBA;
+   ctx->MinMax.Sink = GL_FALSE;
+   ctx->MinMax.Min[RCOMP] = 1000;    ctx->MinMax.Max[RCOMP] = -1000;
+   ctx->MinMax.Min[GCOMP] = 1000;    ctx->MinMax.Max[GCOMP] = -1000;
+   ctx->MinMax.Min[BCOMP] = 1000;    ctx->MinMax.Max[BCOMP] = -1000;
+   ctx->MinMax.Min[ACOMP] = 1000;    ctx->MinMax.Max[ACOMP] = -1000;
 }