-/* $Id: accum.c,v 1.30 2000/10/30 13:31:59 keithw Exp $ */
-
/*
* Mesa 3-D graphics library
- * Version: 3.5
- *
- * Copyright (C) 1999-2000 Brian Paul All Rights Reserved.
- *
+ *
+ * Copyright (C) 1999-2005 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"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
- *
+ *
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
- *
+ *
* 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.
*/
-
-#ifdef PC_HEADER
-#include "all.h"
-#else
#include "glheader.h"
#include "accum.h"
+#include "condrender.h"
#include "context.h"
+#include "format_unpack.h"
+#include "format_pack.h"
+#include "framebuffer.h"
+#include "imports.h"
#include "macros.h"
-#include "mem.h"
-#include "masking.h"
-#include "span.h"
#include "state.h"
-#include "types.h"
-#endif
+#include "mtypes.h"
-/*
- * Accumulation buffer notes
- *
- * Normally, accumulation buffer values are GLshorts with values in
- * [-32767, 32767] which represent floating point colors in [-1, 1],
- * as suggested by the OpenGL specification.
- *
- * We optimize for the common case used for full-scene antialiasing:
- * // start with accum buffer cleared to zero
- * glAccum(GL_LOAD, w); // or GL_ACCUM the first image
- * glAccum(GL_ACCUM, w);
- * ...
- * glAccum(GL_ACCUM, w);
- * glAccum(GL_RETURN, 1.0);
- * That is, we start with an empty accumulation buffer and accumulate
- * n images, each with weight w = 1/n.
- * In this scenario, we can simply store unscaled integer values in
- * the accum buffer instead of scaled integers. We'll also keep track
- * of the w value so when we do GL_RETURN we simply divide the accumulated
- * values by n (=1/w).
- * This lets us avoid _many_ int->float->int conversions.
- */
+void GLAPIENTRY
+_mesa_ClearAccum( GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha )
+{
+ GLfloat tmp[4];
+ GET_CURRENT_CONTEXT(ctx);
+ tmp[0] = CLAMP( red, -1.0F, 1.0F );
+ tmp[1] = CLAMP( green, -1.0F, 1.0F );
+ tmp[2] = CLAMP( blue, -1.0F, 1.0F );
+ tmp[3] = CLAMP( alpha, -1.0F, 1.0F );
-#if CHAN_BITS == 8
-#define USE_OPTIMIZED_ACCUM /* enable the optimization */
-#endif
+ if (TEST_EQ_4V(tmp, ctx->Accum.ClearColor))
+ return;
+ COPY_4FV( ctx->Accum.ClearColor, tmp );
+}
+/**
+ * Clear the accumulation buffer by mapping the renderbuffer and
+ * writing the clear color to it. Called by the driver's implementation
+ * of the glClear function.
+ */
void
-_mesa_alloc_accum_buffer( GLcontext *ctx )
+_mesa_clear_accum_buffer(struct gl_context *ctx)
{
- GLint n;
+ GLuint x, y, width, height;
+ GLubyte *accMap;
+ GLint accRowStride;
+ struct gl_renderbuffer *accRb;
- if (ctx->DrawBuffer->Accum) {
- FREE( ctx->DrawBuffer->Accum );
- ctx->DrawBuffer->Accum = NULL;
- }
+ if (!ctx->DrawBuffer)
+ return;
- /* allocate accumulation buffer if not already present */
- n = ctx->DrawBuffer->Width * ctx->DrawBuffer->Height * 4 * sizeof(GLaccum);
- ctx->DrawBuffer->Accum = (GLaccum *) MALLOC( n );
- if (!ctx->DrawBuffer->Accum) {
- /* unable to setup accumulation buffer */
- gl_error( ctx, GL_OUT_OF_MEMORY, "glAccum" );
- }
-#ifdef USE_OPTIMIZED_ACCUM
- ctx->IntegerAccumMode = GL_TRUE;
-#else
- ctx->IntegerAccumMode = GL_FALSE;
-#endif
- ctx->IntegerAccumScaler = 0.0;
-}
+ accRb = ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer;
+ if (!accRb)
+ return; /* missing accum buffer, not an error */
+ _mesa_update_draw_buffer_bounds(ctx, ctx->DrawBuffer);
+ /* bounds, with scissor */
+ x = ctx->DrawBuffer->_Xmin;
+ y = ctx->DrawBuffer->_Ymin;
+ width = ctx->DrawBuffer->_Xmax - ctx->DrawBuffer->_Xmin;
+ height = ctx->DrawBuffer->_Ymax - ctx->DrawBuffer->_Ymin;
-void
-_mesa_ClearAccum( GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha )
-{
- GET_CURRENT_CONTEXT(ctx);
- ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glAccum");
+ ctx->Driver.MapRenderbuffer(ctx, accRb, x, y, width, height,
+ GL_MAP_WRITE_BIT, &accMap, &accRowStride,
+ ctx->DrawBuffer->FlipY);
- ctx->Accum.ClearColor[0] = CLAMP( red, -1.0, 1.0 );
- ctx->Accum.ClearColor[1] = CLAMP( green, -1.0, 1.0 );
- ctx->Accum.ClearColor[2] = CLAMP( blue, -1.0, 1.0 );
- ctx->Accum.ClearColor[3] = CLAMP( alpha, -1.0, 1.0 );
- ctx->NewState |= _NEW_ACCUM;
-}
+ if (!accMap) {
+ _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum");
+ return;
+ }
+
+ if (accRb->Format == MESA_FORMAT_RGBA_SNORM16) {
+ const GLshort clearR = FLOAT_TO_SHORT(ctx->Accum.ClearColor[0]);
+ const GLshort clearG = FLOAT_TO_SHORT(ctx->Accum.ClearColor[1]);
+ const GLshort clearB = FLOAT_TO_SHORT(ctx->Accum.ClearColor[2]);
+ const GLshort clearA = FLOAT_TO_SHORT(ctx->Accum.ClearColor[3]);
+ GLuint i, j;
+
+ for (j = 0; j < height; j++) {
+ GLshort *row = (GLshort *) accMap;
+
+ for (i = 0; i < width; i++) {
+ row[i * 4 + 0] = clearR;
+ row[i * 4 + 1] = clearG;
+ row[i * 4 + 2] = clearB;
+ row[i * 4 + 3] = clearA;
+ }
+ accMap += accRowStride;
+ }
+ }
+ else {
+ /* other types someday? */
+ _mesa_warning(ctx, "unexpected accum buffer type");
+ }
+ ctx->Driver.UnmapRenderbuffer(ctx, accRb);
+}
-/*
- * This is called when we fall out of optimized/unscaled accum buffer mode.
- * That is, we convert each unscaled accum buffer value into a scaled value
- * representing the range[-1, 1].
+/**
+ * if (bias)
+ * Accum += value
+ * else
+ * Accum *= value
*/
-static void rescale_accum( GLcontext *ctx )
+static void
+accum_scale_or_bias(struct gl_context *ctx, GLfloat value,
+ GLint xpos, GLint ypos, GLint width, GLint height,
+ GLboolean bias)
{
- const GLuint n = ctx->DrawBuffer->Width * ctx->DrawBuffer->Height * 4;
- const GLfloat fChanMax = (1 << (sizeof(GLchan) * 8)) - 1;
- const GLfloat s = ctx->IntegerAccumScaler * (32767.0 / fChanMax);
- GLaccum *accum = ctx->DrawBuffer->Accum;
- GLuint i;
+ struct gl_renderbuffer *accRb =
+ ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer;
+ GLubyte *accMap;
+ GLint accRowStride;
+
+ assert(accRb);
+
+ ctx->Driver.MapRenderbuffer(ctx, accRb, xpos, ypos, width, height,
+ GL_MAP_READ_BIT | GL_MAP_WRITE_BIT,
+ &accMap, &accRowStride,
+ ctx->DrawBuffer->FlipY);
- assert(ctx->IntegerAccumMode);
- assert(accum);
+ if (!accMap) {
+ _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum");
+ return;
+ }
- for (i = 0; i < n; i++) {
- accum[i] = (GLaccum) (accum[i] * s);
+ if (accRb->Format == MESA_FORMAT_RGBA_SNORM16) {
+ const GLshort incr = (GLshort) (value * 32767.0f);
+ GLint i, j;
+ if (bias) {
+ for (j = 0; j < height; j++) {
+ GLshort *acc = (GLshort *) accMap;
+ for (i = 0; i < 4 * width; i++) {
+ acc[i] += incr;
+ }
+ accMap += accRowStride;
+ }
+ }
+ else {
+ /* scale */
+ for (j = 0; j < height; j++) {
+ GLshort *acc = (GLshort *) accMap;
+ for (i = 0; i < 4 * width; i++) {
+ acc[i] = (GLshort) (acc[i] * value);
+ }
+ accMap += accRowStride;
+ }
+ }
+ }
+ else {
+ /* other types someday? */
}
- ctx->IntegerAccumMode = GL_FALSE;
+ ctx->Driver.UnmapRenderbuffer(ctx, accRb);
}
-
-void
-_mesa_Accum( GLenum op, GLfloat value )
+/**
+ * if (load)
+ * Accum = ColorBuf * value
+ * else
+ * Accum += ColorBuf * value
+ */
+static void
+accum_or_load(struct gl_context *ctx, GLfloat value,
+ GLint xpos, GLint ypos, GLint width, GLint height,
+ GLboolean load)
{
- GET_CURRENT_CONTEXT(ctx);
- GLuint xpos, ypos, width, height, width4;
- GLfloat acc_scale;
- GLchan rgba[MAX_WIDTH][4];
- const GLuint colorMask = *((GLuint *) &ctx->Color.ColorMask);
- const GLint iChanMax = (1 << (sizeof(GLchan) * 8)) - 1;
- const GLfloat fChanMax = (1 << (sizeof(GLchan) * 8)) - 1;
-
- ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glAccum");
-
- if (ctx->Visual.AccumRedBits == 0 || ctx->DrawBuffer != ctx->ReadBuffer) {
- gl_error(ctx, GL_INVALID_OPERATION, "glAccum");
+ struct gl_renderbuffer *accRb =
+ ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer;
+ struct gl_renderbuffer *colorRb = ctx->ReadBuffer->_ColorReadBuffer;
+ GLubyte *accMap, *colorMap;
+ GLint accRowStride, colorRowStride;
+ GLbitfield mappingFlags;
+
+ if (!colorRb) {
+ /* no read buffer - OK */
return;
}
- if (!ctx->DrawBuffer->Accum) {
- _mesa_warning(ctx, "Calling glAccum() without an accumulation buffer (low memory?)");
+ assert(accRb);
+
+ mappingFlags = GL_MAP_WRITE_BIT;
+ if (!load) /* if we're accumulating */
+ mappingFlags |= GL_MAP_READ_BIT;
+
+ /* Map accum buffer */
+ ctx->Driver.MapRenderbuffer(ctx, accRb, xpos, ypos, width, height,
+ mappingFlags, &accMap, &accRowStride,
+ ctx->DrawBuffer->FlipY);
+ if (!accMap) {
+ _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum");
return;
}
- if (sizeof(GLaccum)==1) {
- acc_scale = 127.0;
+ /* Map color buffer */
+ ctx->Driver.MapRenderbuffer(ctx, colorRb, xpos, ypos, width, height,
+ GL_MAP_READ_BIT,
+ &colorMap, &colorRowStride,
+ ctx->DrawBuffer->FlipY);
+ if (!colorMap) {
+ ctx->Driver.UnmapRenderbuffer(ctx, accRb);
+ _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum");
+ return;
}
- else if (sizeof(GLaccum)==2) {
- acc_scale = 32767.0;
+
+ if (accRb->Format == MESA_FORMAT_RGBA_SNORM16) {
+ const GLfloat scale = value * 32767.0f;
+ GLint i, j;
+ GLfloat (*rgba)[4];
+
+ rgba = malloc(width * 4 * sizeof(GLfloat));
+ if (rgba) {
+ for (j = 0; j < height; j++) {
+ GLshort *acc = (GLshort *) accMap;
+
+ /* read colors from source color buffer */
+ _mesa_unpack_rgba_row(colorRb->Format, width, colorMap, rgba);
+
+ if (load) {
+ for (i = 0; i < width; i++) {
+ acc[i * 4 + 0] = (GLshort) (rgba[i][RCOMP] * scale);
+ acc[i * 4 + 1] = (GLshort) (rgba[i][GCOMP] * scale);
+ acc[i * 4 + 2] = (GLshort) (rgba[i][BCOMP] * scale);
+ acc[i * 4 + 3] = (GLshort) (rgba[i][ACOMP] * scale);
+ }
+ }
+ else {
+ /* accumulate */
+ for (i = 0; i < width; i++) {
+ acc[i * 4 + 0] += (GLshort) (rgba[i][RCOMP] * scale);
+ acc[i * 4 + 1] += (GLshort) (rgba[i][GCOMP] * scale);
+ acc[i * 4 + 2] += (GLshort) (rgba[i][BCOMP] * scale);
+ acc[i * 4 + 3] += (GLshort) (rgba[i][ACOMP] * scale);
+ }
+ }
+
+ colorMap += colorRowStride;
+ accMap += accRowStride;
+ }
+
+ free(rgba);
+ }
+ else {
+ _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum");
+ }
}
else {
- /* sizeof(GLaccum) > 2 (Cray) */
- acc_scale = (float) SHRT_MAX;
+ /* other types someday? */
}
- if (ctx->NewState)
- gl_update_state( ctx );
-
- /* Determine region to operate upon. */
- if (ctx->Scissor.Enabled) {
- xpos = ctx->Scissor.X;
- ypos = ctx->Scissor.Y;
- width = ctx->Scissor.Width;
- height = ctx->Scissor.Height;
- }
- else {
- /* whole window */
- xpos = 0;
- ypos = 0;
- width = ctx->DrawBuffer->Width;
- height = ctx->DrawBuffer->Height;
+ ctx->Driver.UnmapRenderbuffer(ctx, accRb);
+ ctx->Driver.UnmapRenderbuffer(ctx, colorRb);
+}
+
+
+/**
+ * ColorBuffer = Accum * value
+ */
+static void
+accum_return(struct gl_context *ctx, GLfloat value,
+ GLint xpos, GLint ypos, GLint width, GLint height)
+{
+ struct gl_framebuffer *fb = ctx->DrawBuffer;
+ struct gl_renderbuffer *accRb = fb->Attachment[BUFFER_ACCUM].Renderbuffer;
+ GLubyte *accMap, *colorMap;
+ GLint accRowStride, colorRowStride;
+ GLuint buffer;
+
+ /* Map accum buffer */
+ ctx->Driver.MapRenderbuffer(ctx, accRb, xpos, ypos, width, height,
+ GL_MAP_READ_BIT,
+ &accMap, &accRowStride, fb->FlipY);
+ if (!accMap) {
+ _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum");
+ return;
}
- width4 = 4 * width;
+ /* Loop over destination buffers */
+ for (buffer = 0; buffer < fb->_NumColorDrawBuffers; buffer++) {
+ struct gl_renderbuffer *colorRb = fb->_ColorDrawBuffers[buffer];
+ const GLboolean masking = (!GET_COLORMASK_BIT(ctx->Color.ColorMask, buffer, 0) ||
+ !GET_COLORMASK_BIT(ctx->Color.ColorMask, buffer, 1) ||
+ !GET_COLORMASK_BIT(ctx->Color.ColorMask, buffer, 2) ||
+ !GET_COLORMASK_BIT(ctx->Color.ColorMask, buffer, 3));
+ GLbitfield mappingFlags = GL_MAP_WRITE_BIT;
+
+ if (masking)
+ mappingFlags |= GL_MAP_READ_BIT;
+
+ /* Map color buffer */
+ ctx->Driver.MapRenderbuffer(ctx, colorRb, xpos, ypos, width, height,
+ mappingFlags, &colorMap, &colorRowStride,
+ fb->FlipY);
+ if (!colorMap) {
+ _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum");
+ continue;
+ }
- switch (op) {
- case GL_ADD:
- if (value != 0.0F) {
- const GLaccum intVal = (GLaccum) (value * acc_scale);
- GLuint j;
- /* Leave optimized accum buffer mode */
- if (ctx->IntegerAccumMode)
- rescale_accum(ctx);
- for (j = 0; j < height; j++) {
- GLaccum * acc = ctx->DrawBuffer->Accum + ypos * width4 + 4 * xpos;
- GLuint i;
- for (i = 0; i < width4; i++) {
- acc[i] += intVal;
- }
- ypos++;
- }
- }
- break;
-
- case GL_MULT:
- if (value != 1.0F) {
- GLuint j;
- /* Leave optimized accum buffer mode */
- if (ctx->IntegerAccumMode)
- rescale_accum(ctx);
- for (j = 0; j < height; j++) {
- GLaccum *acc = ctx->DrawBuffer->Accum + ypos * width4 + 4 * xpos;
- GLuint i;
- for (i = 0; i < width4; i++) {
- acc[i] = (GLaccum) ( (GLfloat) acc[i] * value );
- }
- ypos++;
- }
- }
- break;
-
- case GL_ACCUM:
- if (value == 0.0F)
- return;
-
- (*ctx->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
- ctx->Pixel.DriverReadBuffer );
-
- /* May have to leave optimized accum buffer mode */
- if (ctx->IntegerAccumScaler == 0.0 && value > 0.0 && value <= 1.0)
- ctx->IntegerAccumScaler = value;
- if (ctx->IntegerAccumMode && value != ctx->IntegerAccumScaler)
- rescale_accum(ctx);
-
- RENDER_START(ctx);
-
- if (ctx->IntegerAccumMode) {
- /* simply add integer color values into accum buffer */
- GLuint j;
- GLaccum *acc = ctx->DrawBuffer->Accum + ypos * width4 + xpos * 4;
- assert(ctx->IntegerAccumScaler > 0.0);
- assert(ctx->IntegerAccumScaler <= 1.0);
- for (j = 0; j < height; j++) {
-
- GLuint i, i4;
- gl_read_rgba_span(ctx, ctx->DrawBuffer, width, xpos, ypos, rgba);
- for (i = i4 = 0; i < width; i++, i4+=4) {
- acc[i4+0] += rgba[i][RCOMP];
- acc[i4+1] += rgba[i][GCOMP];
- acc[i4+2] += rgba[i][BCOMP];
- acc[i4+3] += rgba[i][ACOMP];
- }
- acc += width4;
- ypos++;
- }
- }
- else {
- /* scaled integer accum buffer */
- const GLfloat rscale = value * acc_scale / fChanMax;
- const GLfloat gscale = value * acc_scale / fChanMax;
- const GLfloat bscale = value * acc_scale / fChanMax;
- const GLfloat ascale = value * acc_scale / fChanMax;
- GLuint j;
- for (j=0;j<height;j++) {
- GLaccum *acc = ctx->DrawBuffer->Accum + ypos * width4 + xpos * 4;
- GLuint i;
- gl_read_rgba_span(ctx, ctx->DrawBuffer, width, xpos, ypos, rgba);
- for (i=0;i<width;i++) {
- *acc += (GLaccum) ( (GLfloat) rgba[i][RCOMP] * rscale ); acc++;
- *acc += (GLaccum) ( (GLfloat) rgba[i][GCOMP] * gscale ); acc++;
- *acc += (GLaccum) ( (GLfloat) rgba[i][BCOMP] * bscale ); acc++;
- *acc += (GLaccum) ( (GLfloat) rgba[i][ACOMP] * ascale ); acc++;
- }
- ypos++;
- }
- }
- /* restore read buffer = draw buffer (the default) */
- (*ctx->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
- ctx->Color.DriverDrawBuffer );
- RENDER_FINISH(ctx);
- break;
-
- case GL_LOAD:
- (*ctx->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
- ctx->Pixel.DriverReadBuffer );
-
- /* This is a change to go into optimized accum buffer mode */
- if (value > 0.0 && value <= 1.0) {
-#ifdef USE_OPTIMIZED_ACCUM
- ctx->IntegerAccumMode = GL_TRUE;
-#else
- ctx->IntegerAccumMode = GL_FALSE;
-#endif
- ctx->IntegerAccumScaler = value;
- }
- else {
- ctx->IntegerAccumMode = GL_FALSE;
- ctx->IntegerAccumScaler = 0.0;
- }
+ if (accRb->Format == MESA_FORMAT_RGBA_SNORM16) {
+ const GLfloat scale = value / 32767.0f;
+ GLint i, j;
+ GLfloat (*rgba)[4], (*dest)[4];
- RENDER_START(ctx);
- if (ctx->IntegerAccumMode) {
- /* just copy values into accum buffer */
- GLuint j;
- GLaccum *acc = ctx->DrawBuffer->Accum + ypos * width4 + xpos * 4;
- assert(ctx->IntegerAccumScaler > 0.0);
- assert(ctx->IntegerAccumScaler <= 1.0);
+ rgba = malloc(width * 4 * sizeof(GLfloat));
+ dest = malloc(width * 4 * sizeof(GLfloat));
+
+ if (rgba && dest) {
for (j = 0; j < height; j++) {
- GLuint i, i4;
- gl_read_rgba_span(ctx, ctx->DrawBuffer, width, xpos, ypos, rgba);
- for (i = i4 = 0; i < width; i++, i4 += 4) {
- acc[i4+0] = rgba[i][RCOMP];
- acc[i4+1] = rgba[i][GCOMP];
- acc[i4+2] = rgba[i][BCOMP];
- acc[i4+3] = rgba[i][ACOMP];
+ GLshort *acc = (GLshort *) accMap;
+
+ for (i = 0; i < width; i++) {
+ rgba[i][0] = acc[i * 4 + 0] * scale;
+ rgba[i][1] = acc[i * 4 + 1] * scale;
+ rgba[i][2] = acc[i * 4 + 2] * scale;
+ rgba[i][3] = acc[i * 4 + 3] * scale;
}
- acc += width4;
- ypos++;
- }
- }
- else {
- /* scaled integer accum buffer */
- const GLfloat rscale = value * acc_scale / fChanMax;
- const GLfloat gscale = value * acc_scale / fChanMax;
- const GLfloat bscale = value * acc_scale / fChanMax;
- const GLfloat ascale = value * acc_scale / fChanMax;
- const GLfloat d = 3.0 / acc_scale;
- GLuint i, j;
- for (j = 0; j < height; j++) {
- GLaccum *acc = ctx->DrawBuffer->Accum + ypos * width4 + xpos * 4;
- gl_read_rgba_span(ctx, ctx->DrawBuffer, width, xpos, ypos, rgba);
- for (i=0;i<width;i++) {
- *acc++ = (GLaccum) ((GLfloat) rgba[i][RCOMP] * rscale + d);
- *acc++ = (GLaccum) ((GLfloat) rgba[i][GCOMP] * gscale + d);
- *acc++ = (GLaccum) ((GLfloat) rgba[i][BCOMP] * bscale + d);
- *acc++ = (GLaccum) ((GLfloat) rgba[i][ACOMP] * ascale + d);
+
+ if (masking) {
+
+ /* get existing colors from dest buffer */
+ _mesa_unpack_rgba_row(colorRb->Format, width, colorMap, dest);
+
+ /* use the dest colors where mask[channel] = 0 */
+ if (!GET_COLORMASK_BIT(ctx->Color.ColorMask, buffer, 0)) {
+ for (i = 0; i < width; i++)
+ rgba[i][RCOMP] = dest[i][RCOMP];
+ }
+ if (!GET_COLORMASK_BIT(ctx->Color.ColorMask, buffer, 1)) {
+ for (i = 0; i < width; i++)
+ rgba[i][GCOMP] = dest[i][GCOMP];
+ }
+ if (!GET_COLORMASK_BIT(ctx->Color.ColorMask, buffer, 2)) {
+ for (i = 0; i < width; i++)
+ rgba[i][BCOMP] = dest[i][BCOMP];
+ }
+ if (!GET_COLORMASK_BIT(ctx->Color.ColorMask, buffer, 3)) {
+ for (i = 0; i < width; i++)
+ rgba[i][ACOMP] = dest[i][ACOMP];
+ }
}
- ypos++;
- }
- }
- /* restore read buffer = draw buffer (the default) */
- (*ctx->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
- ctx->Color.DriverDrawBuffer );
- RENDER_FINISH(ctx);
- break;
-
- case GL_RETURN:
- /* May have to leave optimized accum buffer mode */
- if (ctx->IntegerAccumMode && value != 1.0)
- rescale_accum(ctx);
-
- RENDER_START(ctx);
- if (ctx->IntegerAccumMode && ctx->IntegerAccumScaler > 0) {
- /* build lookup table to avoid many floating point multiplies */
- static GLchan multTable[32768];
- static GLfloat prevMult = 0.0;
- const GLfloat mult = ctx->IntegerAccumScaler;
- const GLint max = MIN2((GLint) (256 / mult), 32767);
- GLuint j;
- if (mult != prevMult) {
- for (j = 0; j < max; j++)
- multTable[j] = (GLint) ((GLfloat) j * mult + 0.5F);
- prevMult = mult;
- }
+ _mesa_pack_float_rgba_row(colorRb->Format, width,
+ (const GLfloat (*)[4]) rgba, colorMap);
- assert(ctx->IntegerAccumScaler > 0.0);
- assert(ctx->IntegerAccumScaler <= 1.0);
- for (j = 0; j < height; j++) {
- const GLaccum *acc = ctx->DrawBuffer->Accum + ypos * width4 + xpos*4;
- GLuint i, i4;
- for (i = i4 = 0; i < width; i++, i4 += 4) {
- ASSERT(acc[i4+0] < max);
- ASSERT(acc[i4+1] < max);
- ASSERT(acc[i4+2] < max);
- ASSERT(acc[i4+3] < max);
- rgba[i][RCOMP] = multTable[acc[i4+0]];
- rgba[i][GCOMP] = multTable[acc[i4+1]];
- rgba[i][BCOMP] = multTable[acc[i4+2]];
- rgba[i][ACOMP] = multTable[acc[i4+3]];
- }
- if (colorMask != 0xffffffff) {
- _mesa_mask_rgba_span( ctx, width, xpos, ypos, rgba );
- }
- (*ctx->Driver.WriteRGBASpan)( ctx, width, xpos, ypos,
- (const GLchan (*)[4])rgba, NULL );
- ypos++;
+ accMap += accRowStride;
+ colorMap += colorRowStride;
}
}
else {
- const GLfloat rscale = value / acc_scale * fChanMax;
- const GLfloat gscale = value / acc_scale * fChanMax;
- const GLfloat bscale = value / acc_scale * fChanMax;
- const GLfloat ascale = value / acc_scale * fChanMax;
- GLuint i, j;
- for (j=0;j<height;j++) {
- const GLaccum *acc = ctx->DrawBuffer->Accum + ypos * width4 + xpos*4;
- for (i=0;i<width;i++) {
- GLint r, g, b, a;
- r = (GLint) ( (GLfloat) (*acc++) * rscale + 0.5F );
- g = (GLint) ( (GLfloat) (*acc++) * gscale + 0.5F );
- b = (GLint) ( (GLfloat) (*acc++) * bscale + 0.5F );
- a = (GLint) ( (GLfloat) (*acc++) * ascale + 0.5F );
- rgba[i][RCOMP] = CLAMP( r, 0, iChanMax );
- rgba[i][GCOMP] = CLAMP( g, 0, iChanMax );
- rgba[i][BCOMP] = CLAMP( b, 0, iChanMax );
- rgba[i][ACOMP] = CLAMP( a, 0, iChanMax );
- }
- if (colorMask != 0xffffffff) {
- _mesa_mask_rgba_span( ctx, width, xpos, ypos, rgba );
- }
- (*ctx->Driver.WriteRGBASpan)( ctx, width, xpos, ypos,
- (const GLchan (*)[4])rgba, NULL );
- ypos++;
- }
- }
- RENDER_FINISH(ctx);
- break;
+ _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum");
+ }
+ free(rgba);
+ free(dest);
+ }
+ else {
+ /* other types someday? */
+ }
- default:
- gl_error( ctx, GL_INVALID_ENUM, "glAccum" );
+ ctx->Driver.UnmapRenderbuffer(ctx, colorRb);
}
+
+ ctx->Driver.UnmapRenderbuffer(ctx, accRb);
}
-/*
- * Clear the accumulation Buffer.
+/**
+ * Software fallback for glAccum. A hardware driver that supports
+ * signed 16-bit color channels could implement hardware accumulation
+ * operations, but no driver does so at this time.
*/
-void
-_mesa_clear_accum_buffer( GLcontext *ctx )
+static void
+accum(struct gl_context *ctx, GLenum op, GLfloat value)
{
- GLuint buffersize;
- GLfloat acc_scale;
+ GLint xpos, ypos, width, height;
- if (ctx->Visual.AccumRedBits==0) {
- /* No accumulation buffer! */
+ if (!ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer) {
+ _mesa_warning(ctx, "Calling glAccum() without an accumulation buffer");
return;
}
- if (sizeof(GLaccum)==1) {
- acc_scale = 127.0;
+ if (!_mesa_check_conditional_render(ctx))
+ return;
+
+ _mesa_update_draw_buffer_bounds(ctx, ctx->DrawBuffer);
+
+ xpos = ctx->DrawBuffer->_Xmin;
+ ypos = ctx->DrawBuffer->_Ymin;
+ width = ctx->DrawBuffer->_Xmax - ctx->DrawBuffer->_Xmin;
+ height = ctx->DrawBuffer->_Ymax - ctx->DrawBuffer->_Ymin;
+
+ switch (op) {
+ case GL_ADD:
+ if (value != 0.0F) {
+ accum_scale_or_bias(ctx, value, xpos, ypos, width, height, GL_TRUE);
+ }
+ break;
+ case GL_MULT:
+ if (value != 1.0F) {
+ accum_scale_or_bias(ctx, value, xpos, ypos, width, height, GL_FALSE);
+ }
+ break;
+ case GL_ACCUM:
+ if (value != 0.0F) {
+ accum_or_load(ctx, value, xpos, ypos, width, height, GL_FALSE);
+ }
+ break;
+ case GL_LOAD:
+ accum_or_load(ctx, value, xpos, ypos, width, height, GL_TRUE);
+ break;
+ case GL_RETURN:
+ accum_return(ctx, value, xpos, ypos, width, height);
+ break;
+ default:
+ unreachable("invalid mode in _mesa_Accum()");
+ break;
}
- else if (sizeof(GLaccum)==2) {
- acc_scale = 32767.0;
+}
+
+
+void
+_mesa_init_accum( struct gl_context *ctx )
+{
+ /* Accumulate buffer group */
+ ASSIGN_4V( ctx->Accum.ClearColor, 0.0, 0.0, 0.0, 0.0 );
+}
+
+
+void GLAPIENTRY
+_mesa_Accum( GLenum op, GLfloat value )
+{
+ GET_CURRENT_CONTEXT(ctx);
+ FLUSH_VERTICES(ctx, 0);
+
+ switch (op) {
+ case GL_ADD:
+ case GL_MULT:
+ case GL_ACCUM:
+ case GL_LOAD:
+ case GL_RETURN:
+ /* OK */
+ break;
+ default:
+ _mesa_error(ctx, GL_INVALID_ENUM, "glAccum(op)");
+ return;
}
- else {
- /* sizeof(GLaccum) > 2 (Cray) */
- acc_scale = (float) SHRT_MAX;
+
+ if (ctx->DrawBuffer->Visual.accumRedBits == 0) {
+ _mesa_error(ctx, GL_INVALID_OPERATION, "glAccum(no accum buffer)");
+ return;
+ }
+
+ if (ctx->DrawBuffer != ctx->ReadBuffer) {
+ /* See GLX_SGI_make_current_read or WGL_ARB_make_current_read,
+ * or GL_EXT_framebuffer_blit.
+ */
+ _mesa_error(ctx, GL_INVALID_OPERATION,
+ "glAccum(different read/draw buffers)");
+ return;
}
- /* number of pixels */
- buffersize = ctx->DrawBuffer->Width * ctx->DrawBuffer->Height;
+ if (ctx->NewState)
+ _mesa_update_state(ctx);
- if (!ctx->DrawBuffer->Accum) {
- /* try to alloc accumulation buffer */
- ctx->DrawBuffer->Accum = (GLaccum *)
- MALLOC( buffersize * 4 * sizeof(GLaccum) );
+ if (ctx->DrawBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
+ _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
+ "glAccum(incomplete framebuffer)");
+ return;
}
- if (ctx->DrawBuffer->Accum) {
- if (ctx->Scissor.Enabled) {
- /* Limit clear to scissor box */
- GLaccum r, g, b, a;
- GLint i, j;
- GLint width, height;
- GLaccum *row;
- r = (GLaccum) (ctx->Accum.ClearColor[0] * acc_scale);
- g = (GLaccum) (ctx->Accum.ClearColor[1] * acc_scale);
- b = (GLaccum) (ctx->Accum.ClearColor[2] * acc_scale);
- a = (GLaccum) (ctx->Accum.ClearColor[3] * acc_scale);
- /* size of region to clear */
- width = 4 * (ctx->DrawBuffer->Xmax - ctx->DrawBuffer->Xmin);
- height = ctx->DrawBuffer->Ymax - ctx->DrawBuffer->Ymin;
- /* ptr to first element to clear */
- row = ctx->DrawBuffer->Accum
- + 4 * (ctx->DrawBuffer->Ymin * ctx->DrawBuffer->Width
- + ctx->DrawBuffer->Xmin);
- for (j=0;j<height;j++) {
- for (i=0;i<width;i+=4) {
- row[i+0] = r;
- row[i+1] = g;
- row[i+2] = b;
- row[i+3] = a;
- }
- row += 4 * ctx->DrawBuffer->Width;
- }
- }
- else {
- /* clear whole buffer */
- if (ctx->Accum.ClearColor[0]==0.0 &&
- ctx->Accum.ClearColor[1]==0.0 &&
- ctx->Accum.ClearColor[2]==0.0 &&
- ctx->Accum.ClearColor[3]==0.0) {
- /* Black */
- BZERO( ctx->DrawBuffer->Accum, buffersize * 4 * sizeof(GLaccum) );
- }
- else {
- /* Not black */
- GLaccum *acc, r, g, b, a;
- GLuint i;
-
- acc = ctx->DrawBuffer->Accum;
- r = (GLaccum) (ctx->Accum.ClearColor[0] * acc_scale);
- g = (GLaccum) (ctx->Accum.ClearColor[1] * acc_scale);
- b = (GLaccum) (ctx->Accum.ClearColor[2] * acc_scale);
- a = (GLaccum) (ctx->Accum.ClearColor[3] * acc_scale);
- for (i=0;i<buffersize;i++) {
- *acc++ = r;
- *acc++ = g;
- *acc++ = b;
- *acc++ = a;
- }
- }
- }
+ if (ctx->RasterDiscard)
+ return;
- /* update optimized accum state vars */
- if (ctx->Accum.ClearColor[0] == 0.0 && ctx->Accum.ClearColor[1] == 0.0 &&
- ctx->Accum.ClearColor[2] == 0.0 && ctx->Accum.ClearColor[3] == 0.0) {
-#ifdef USE_OPTIMIZED_ACCUM
- ctx->IntegerAccumMode = GL_TRUE;
-#else
- ctx->IntegerAccumMode = GL_FALSE;
-#endif
- ctx->IntegerAccumScaler = 0.0; /* denotes empty accum buffer */
- }
- else {
- ctx->IntegerAccumMode = GL_FALSE;
- }
+ if (ctx->RenderMode == GL_RENDER) {
+ accum(ctx, op, value);
}
}