+ if (ctx->RasterDiscard)
+ return;
+
+ if (ctx->RenderMode == GL_RENDER) {
+ _mesa_accum(ctx, op, value);
+ }
+}
+
+
+/**
+ * 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_clear_accum_buffer(struct gl_context *ctx)
+{
+ GLuint x, y, width, height;
+ GLubyte *accMap;
+ GLint accRowStride;
+ struct gl_renderbuffer *accRb;
+
+ if (!ctx->DrawBuffer)
+ return;
+
+ accRb = ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer;
+ if (!accRb)
+ return; /* missing accum buffer, not an error */
+
+ /* 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;
+
+ ctx->Driver.MapRenderbuffer(ctx, accRb, x, y, width, height,
+ GL_MAP_WRITE_BIT, &accMap, &accRowStride);
+
+ 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);
+}
+
+
+/**
+ * if (bias)
+ * Accum += value
+ * else
+ * Accum *= value
+ */
+static void
+accum_scale_or_bias(struct gl_context *ctx, GLfloat value,
+ GLint xpos, GLint ypos, GLint width, GLint height,
+ GLboolean bias)
+{
+ 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);
+
+ if (!accMap) {
+ _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum");
+ return;
+ }
+
+ 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->Driver.UnmapRenderbuffer(ctx, accRb);
+}
+
+
+/**
+ * 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)
+{
+ 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;
+ }
+
+ 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);
+ if (!accMap) {
+ _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum");
+ return;
+ }
+
+ /* Map color buffer */
+ ctx->Driver.MapRenderbuffer(ctx, colorRb, xpos, ypos, width, height,
+ GL_MAP_READ_BIT,
+ &colorMap, &colorRowStride);
+ if (!colorMap) {
+ ctx->Driver.UnmapRenderbuffer(ctx, accRb);
+ _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum");
+ return;
+ }
+
+ 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");
+ }