gallium: Use MALLOC().
[mesa.git] / src / mesa / state_tracker / st_cb_accum.c
index 03c65db70ba9c40ada5fa5239dd1125419997df8..3a3bf9016dd61e20ae30262e9e38326871aa4bff 100644 (file)
 #include "st_format.h"
 #include "pipe/p_context.h"
 #include "pipe/p_defines.h"
+#include "pipe/p_inlines.h"
+#include "pipe/util/p_tile.h"
+
+
+#define UNCLAMPED_FLOAT_TO_SHORT(us, f)  \
+   us = ( (short) ( CLAMP((f), -1.0, 1.0) * 32767.0F) )
 
 
 /**
  */
 
 
+void
+st_clear_accum_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
+{
+   struct pipe_context *pipe = ctx->st->pipe;
+   struct st_renderbuffer *acc_strb = st_renderbuffer(rb);
+   struct pipe_surface *acc_ps = acc_strb->surface;
+   const GLint xpos = ctx->DrawBuffer->_Xmin;
+   const GLint ypos = ctx->DrawBuffer->_Ymin;
+   const GLint width = ctx->DrawBuffer->_Xmax - xpos;
+   const GLint height = ctx->DrawBuffer->_Ymax - ypos;
+   const GLfloat r = ctx->Accum.ClearColor[0];
+   const GLfloat g = ctx->Accum.ClearColor[1];
+   const GLfloat b = ctx->Accum.ClearColor[2];
+   const GLfloat a = ctx->Accum.ClearColor[3];
+   GLfloat *accBuf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
+   int i;
+
+   for (i = 0; i < width * height; i++) {
+      accBuf[i*4+0] = r;
+      accBuf[i*4+1] = g;
+      accBuf[i*4+2] = b;
+      accBuf[i*4+3] = a;
+   }
+
+   pipe_put_tile_rgba(pipe, acc_ps, xpos, ypos, width, height, accBuf);
+}
+
+
 /** For ADD/MULT */
 static void
 accum_mad(struct pipe_context *pipe, GLfloat scale, GLfloat bias,
@@ -62,20 +96,15 @@ accum_mad(struct pipe_context *pipe, GLfloat scale, GLfloat bias,
 
    accBuf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
 
-   (void) pipe->region_map(pipe, acc_ps->region);
-
-   acc_ps->get_tile(acc_ps, xpos, ypos, width, height, accBuf);
+   pipe_get_tile_rgba(pipe, acc_ps, xpos, ypos, width, height, accBuf);
 
    for (i = 0; i < 4 * width * height; i++) {
-      GLfloat val = accBuf[i] * scale + bias;
-      accBuf[i] = CLAMP(val, 0.0, 1.0);
+      accBuf[i] = accBuf[i] * scale + bias;
    }
 
-   acc_ps->put_tile(acc_ps, xpos, ypos, width, height, accBuf);
-
-   _mesa_free(accBuf);
+   pipe_put_tile_rgba(pipe, acc_ps, xpos, ypos, width, height, accBuf);
 
-   pipe->region_unmap(pipe, acc_ps->region);
+   free(accBuf);
 }
 
 
@@ -85,31 +114,23 @@ accum_accum(struct pipe_context *pipe, GLfloat value,
             struct pipe_surface *acc_ps,
             struct pipe_surface *color_ps)
 {
-   ubyte *colorMap, *accMap;
    GLfloat *colorBuf, *accBuf;
    GLint i;
 
    colorBuf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
    accBuf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
 
-   colorMap = pipe->region_map(pipe, color_ps->region);
-   accMap = pipe->region_map(pipe, acc_ps->region);
-
-   color_ps->get_tile(color_ps, xpos, ypos, width, height, colorBuf);
-   acc_ps->get_tile(acc_ps, xpos, ypos, width, height, accBuf);
+   pipe_get_tile_rgba(pipe, color_ps, xpos, ypos, width, height, colorBuf);
+   pipe_get_tile_rgba(pipe, acc_ps, xpos, ypos, width, height, accBuf);
 
    for (i = 0; i < 4 * width * height; i++) {
-      GLfloat val = accBuf[i] + colorBuf[i] * value;
-      accBuf[i] = CLAMP(val, 0.0, 1.0);
+      accBuf[i] = accBuf[i] + colorBuf[i] * value;
    }
 
-   acc_ps->put_tile(acc_ps, xpos, ypos, width, height, accBuf);
-
-   _mesa_free(colorBuf);
-   _mesa_free(accBuf);
+   pipe_put_tile_rgba(pipe, acc_ps, xpos, ypos, width, height, accBuf);
 
-   pipe->region_unmap(pipe, color_ps->region);
-   pipe->region_unmap(pipe, acc_ps->region);
+   free(colorBuf);
+   free(accBuf);
 }
 
 
@@ -124,22 +145,15 @@ accum_load(struct pipe_context *pipe, GLfloat value,
 
    buf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
 
-   (void) pipe->region_map(pipe, color_ps->region);
-   (void) pipe->region_map(pipe, acc_ps->region);
-
-   color_ps->get_tile(color_ps, xpos, ypos, width, height, buf);
+   pipe_get_tile_rgba(pipe, color_ps, xpos, ypos, width, height, buf);
 
    for (i = 0; i < 4 * width * height; i++) {
-      GLfloat val = buf[i] * value;
-      buf[i] = CLAMP(val, 0.0, 1.0);
+      buf[i] = buf[i] * value;
    }
 
-   acc_ps->put_tile(acc_ps, xpos, ypos, width, height, buf);
-
-   _mesa_free(buf);
+   pipe_put_tile_rgba(pipe, acc_ps, xpos, ypos, width, height, buf);
 
-   pipe->region_unmap(pipe, color_ps->region);
-   pipe->region_unmap(pipe, acc_ps->region);
+   free(buf);
 }
 
 
@@ -150,45 +164,36 @@ accum_return(GLcontext *ctx, GLfloat value,
              struct pipe_surface *color_ps)
 {
    struct pipe_context *pipe = ctx->st->pipe;
-   const GLboolean writeR = ctx->Color.ColorMask[0];
-   const GLboolean writeG = ctx->Color.ColorMask[1];
-   const GLboolean writeB = ctx->Color.ColorMask[2];
-   const GLboolean writeA = ctx->Color.ColorMask[3];
-   GLfloat *buf;
-   GLint i;
+   const GLubyte *colormask = ctx->Color.ColorMask;
+   GLfloat *abuf, *cbuf = NULL;
+   GLint i, ch;
 
-   buf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
+   abuf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
 
-   (void) pipe->region_map(pipe, color_ps->region);
-   (void) pipe->region_map(pipe, acc_ps->region);
+   pipe_get_tile_rgba(pipe, acc_ps, xpos, ypos, width, height, abuf);
 
-   acc_ps->get_tile(acc_ps, xpos, ypos, width, height, buf);
+   if (!colormask[0] || !colormask[1] || !colormask[2] || !colormask[3]) {
+      cbuf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
+      pipe_get_tile_rgba(pipe, color_ps, xpos, ypos, width, height, cbuf);
+   }
 
    for (i = 0; i < width * height; i++) {
-      if (writeR) {
-         GLfloat r = buf[i * 4 + 0] * value;
-         buf[i * 4 + 0] = CLAMP(r, 0.0, 1.0);
-      }
-      if (writeG) {
-         GLfloat g = buf[i * 4 + 1] * value;
-         buf[i * 4 + 1] = CLAMP(g, 0.0, 1.0);
-      }
-      if (writeB) {
-         GLfloat b = buf[i * 4 + 2] * value;
-         buf[i * 4 + 2] = CLAMP(b, 0.0, 1.0);
-      }
-      if (writeA) {
-         GLfloat a = buf[i * 4 + 3] * value;
-         buf[i * 4 + 3] = CLAMP(a, 0.0, 1.0);
+      for (ch = 0; ch < 4; ch++) {
+         if (colormask[ch]) {
+            GLfloat val = abuf[i * 4 + ch] * value;
+            abuf[i * 4 + ch] = CLAMP(val, 0.0, 1.0);
+         }
+         else {
+            abuf[i * 4 + ch] = cbuf[i * 4 + ch];
+         }
       }
    }
 
-   color_ps->put_tile(color_ps, xpos, ypos, width, height, buf);
-
-   _mesa_free(buf);
+   pipe_put_tile_rgba(pipe, color_ps, xpos, ypos, width, height, abuf);
 
-   pipe->region_unmap(pipe, color_ps->region);
-   pipe->region_unmap(pipe, acc_ps->region);
+   free(abuf);
+   if (cbuf)
+      free(cbuf);
 }
 
 
@@ -209,6 +214,9 @@ st_Accum(GLcontext *ctx, GLenum op, GLfloat value)
    const GLint width = ctx->DrawBuffer->_Xmax - xpos;
    const GLint height = ctx->DrawBuffer->_Ymax - ypos;
 
+   /* make sure color bufs aren't cached */
+   pipe->flush(pipe, PIPE_FLUSH_RENDER_CACHE);
+
    switch (op) {
    case GL_ADD:
       if (value != 0.0F) {