2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 #include "main/glheader.h"
27 #include "main/context.h"
28 #include "main/macros.h"
29 #include "main/imports.h"
30 #include "main/fbobject.h"
33 #include "s_context.h"
34 #include "s_masking.h"
38 /* XXX this would have to change for accum buffers with more or less
39 * than 16 bits per color channel.
41 #define ACCUM_SCALE16 32767.0
45 * Accumulation buffer notes
47 * Normally, accumulation buffer values are GLshorts with values in
48 * [-32767, 32767] which represent floating point colors in [-1, 1],
49 * as defined by the OpenGL specification.
51 * We optimize for the common case used for full-scene antialiasing:
52 * // start with accum buffer cleared to zero
53 * glAccum(GL_LOAD, w); // or GL_ACCUM the first image
54 * glAccum(GL_ACCUM, w);
56 * glAccum(GL_ACCUM, w);
57 * glAccum(GL_RETURN, 1.0);
58 * That is, we start with an empty accumulation buffer and accumulate
59 * n images, each with weight w = 1/n.
60 * In this scenario, we can simply store unscaled integer values in
61 * the accum buffer instead of scaled integers. We'll also keep track
62 * of the w value so when we do GL_RETURN we simply divide the accumulated
63 * values by n (n=1/w).
64 * This lets us avoid _many_ int->float->int conversions.
69 /* enable the optimization */
70 #define USE_OPTIMIZED_ACCUM 1
72 #define USE_OPTIMIZED_ACCUM 0
80 * This is called when we fall out of optimized/unscaled accum buffer mode.
81 * That is, we convert each unscaled accum buffer value into a scaled value
82 * representing the range[-1, 1].
85 rescale_accum( GLcontext *ctx )
87 SWcontext *swrast = SWRAST_CONTEXT(ctx);
88 struct gl_renderbuffer *rb
89 = ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer;
90 const GLfloat s = swrast->_IntegerAccumScaler * (32767.0F / CHAN_MAXF);
93 assert(rb->_BaseFormat == GL_RGBA);
94 /* add other types in future? */
95 assert(rb->DataType == GL_SHORT || rb->DataType == GL_UNSIGNED_SHORT);
96 assert(swrast->_IntegerAccumMode);
98 if (rb->GetPointer(ctx, rb, 0, 0)) {
99 /* directly-addressable memory */
101 for (y = 0; y < rb->Height; y++) {
103 GLshort *acc = (GLshort *) rb->GetPointer(ctx, rb, 0, y);
104 for (i = 0; i < 4 * rb->Width; i++) {
105 acc[i] = (GLshort) (acc[i] * s);
110 /* use get/put row funcs */
112 for (y = 0; y < rb->Height; y++) {
113 GLshort accRow[MAX_WIDTH * 4];
115 rb->GetRow(ctx, rb, rb->Width, 0, y, accRow);
116 for (i = 0; i < 4 * rb->Width; i++) {
117 accRow[i] = (GLshort) (accRow[i] * s);
119 rb->PutRow(ctx, rb, rb->Width, 0, y, accRow, NULL);
123 swrast->_IntegerAccumMode = GL_FALSE;
129 * Clear the accumulation Buffer.
132 _swrast_clear_accum_buffer( GLcontext *ctx, struct gl_renderbuffer *rb )
134 SWcontext *swrast = SWRAST_CONTEXT(ctx);
135 GLuint x, y, width, height;
137 if (ctx->Visual.accumRedBits == 0) {
138 /* No accumulation buffer! Not an error. */
142 if (!rb || !rb->Data)
145 assert(rb->_BaseFormat == GL_RGBA);
146 /* add other types in future? */
147 assert(rb->DataType == GL_SHORT || rb->DataType == GL_UNSIGNED_SHORT);
149 /* bounds, with scissor */
150 x = ctx->DrawBuffer->_Xmin;
151 y = ctx->DrawBuffer->_Ymin;
152 width = ctx->DrawBuffer->_Xmax - ctx->DrawBuffer->_Xmin;
153 height = ctx->DrawBuffer->_Ymax - ctx->DrawBuffer->_Ymin;
155 if (rb->DataType == GL_SHORT || rb->DataType == GL_UNSIGNED_SHORT) {
156 const GLfloat accScale = 32767.0;
160 clearVal[0] = (GLshort) (ctx->Accum.ClearColor[0] * accScale);
161 clearVal[1] = (GLshort) (ctx->Accum.ClearColor[1] * accScale);
162 clearVal[2] = (GLshort) (ctx->Accum.ClearColor[2] * accScale);
163 clearVal[3] = (GLshort) (ctx->Accum.ClearColor[3] * accScale);
165 for (i = 0; i < height; i++) {
166 rb->PutMonoRow(ctx, rb, width, x, y + i, clearVal, NULL);
170 /* someday support other sizes */
173 /* update optimized accum state vars */
174 if (ctx->Accum.ClearColor[0] == 0.0 && ctx->Accum.ClearColor[1] == 0.0 &&
175 ctx->Accum.ClearColor[2] == 0.0 && ctx->Accum.ClearColor[3] == 0.0) {
176 #if USE_OPTIMIZED_ACCUM
177 swrast->_IntegerAccumMode = GL_TRUE;
179 swrast->_IntegerAccumMode = GL_FALSE;
181 swrast->_IntegerAccumScaler = 0.0; /* denotes empty accum buffer */
184 swrast->_IntegerAccumMode = GL_FALSE;
190 accum_add(GLcontext *ctx, GLfloat value,
191 GLint xpos, GLint ypos, GLint width, GLint height )
193 SWcontext *swrast = SWRAST_CONTEXT(ctx);
194 struct gl_renderbuffer *rb
195 = ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer;
199 /* Leave optimized accum buffer mode */
200 if (swrast->_IntegerAccumMode)
203 if (rb->DataType == GL_SHORT || rb->DataType == GL_UNSIGNED_SHORT) {
204 const GLshort incr = (GLshort) (value * ACCUM_SCALE16);
205 if (rb->GetPointer(ctx, rb, 0, 0)) {
207 for (i = 0; i < height; i++) {
208 GLshort *acc = (GLshort *) rb->GetPointer(ctx, rb, xpos, ypos + i);
209 for (j = 0; j < 4 * width; j++) {
216 for (i = 0; i < height; i++) {
217 GLshort accRow[4 * MAX_WIDTH];
218 rb->GetRow(ctx, rb, width, xpos, ypos + i, accRow);
219 for (j = 0; j < 4 * width; j++) {
222 rb->PutRow(ctx, rb, width, xpos, ypos + i, accRow, NULL);
227 /* other types someday */
233 accum_mult(GLcontext *ctx, GLfloat mult,
234 GLint xpos, GLint ypos, GLint width, GLint height )
236 SWcontext *swrast = SWRAST_CONTEXT(ctx);
237 struct gl_renderbuffer *rb
238 = ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer;
242 /* Leave optimized accum buffer mode */
243 if (swrast->_IntegerAccumMode)
246 if (rb->DataType == GL_SHORT || rb->DataType == GL_UNSIGNED_SHORT) {
247 if (rb->GetPointer(ctx, rb, 0, 0)) {
249 for (i = 0; i < height; i++) {
250 GLshort *acc = (GLshort *) rb->GetPointer(ctx, rb, xpos, ypos + i);
251 for (j = 0; j < 4 * width; j++) {
252 acc[j] = (GLshort) (acc[j] * mult);
258 for (i = 0; i < height; i++) {
259 GLshort accRow[4 * MAX_WIDTH];
260 rb->GetRow(ctx, rb, width, xpos, ypos + i, accRow);
261 for (j = 0; j < 4 * width; j++) {
262 accRow[j] = (GLshort) (accRow[j] * mult);
264 rb->PutRow(ctx, rb, width, xpos, ypos + i, accRow, NULL);
269 /* other types someday */
276 accum_accum(GLcontext *ctx, GLfloat value,
277 GLint xpos, GLint ypos, GLint width, GLint height )
279 SWcontext *swrast = SWRAST_CONTEXT(ctx);
280 struct gl_renderbuffer *rb
281 = ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer;
282 const GLboolean directAccess = (rb->GetPointer(ctx, rb, 0, 0) != NULL);
286 if (!ctx->ReadBuffer->_ColorReadBuffer) {
287 /* no read buffer - OK */
291 /* May have to leave optimized accum buffer mode */
292 if (swrast->_IntegerAccumScaler == 0.0 && value > 0.0 && value <= 1.0)
293 swrast->_IntegerAccumScaler = value;
294 if (swrast->_IntegerAccumMode && value != swrast->_IntegerAccumScaler)
297 if (rb->DataType == GL_SHORT || rb->DataType == GL_UNSIGNED_SHORT) {
298 const GLfloat scale = value * ACCUM_SCALE16 / CHAN_MAXF;
299 GLshort accumRow[4 * MAX_WIDTH];
300 GLchan rgba[MAX_WIDTH][4];
303 for (i = 0; i < height; i++) {
306 acc = (GLshort *) rb->GetPointer(ctx, rb, xpos, ypos + i);
309 rb->GetRow(ctx, rb, width, xpos, ypos + i, accumRow);
313 /* read colors from color buffer */
314 _swrast_read_rgba_span(ctx, ctx->ReadBuffer->_ColorReadBuffer, width,
315 xpos, ypos + i, CHAN_TYPE, rgba);
317 /* do accumulation */
318 if (swrast->_IntegerAccumMode) {
319 /* simply add integer color values into accum buffer */
321 for (j = 0; j < width; j++) {
322 acc[j * 4 + 0] += rgba[j][RCOMP];
323 acc[j * 4 + 1] += rgba[j][GCOMP];
324 acc[j * 4 + 2] += rgba[j][BCOMP];
325 acc[j * 4 + 3] += rgba[j][ACOMP];
329 /* scaled integer (or float) accum buffer */
331 for (j = 0; j < width; j++) {
332 acc[j * 4 + 0] += (GLshort) ((GLfloat) rgba[j][RCOMP] * scale);
333 acc[j * 4 + 1] += (GLshort) ((GLfloat) rgba[j][GCOMP] * scale);
334 acc[j * 4 + 2] += (GLshort) ((GLfloat) rgba[j][BCOMP] * scale);
335 acc[j * 4 + 3] += (GLshort) ((GLfloat) rgba[j][ACOMP] * scale);
340 rb->PutRow(ctx, rb, width, xpos, ypos + i, accumRow, NULL);
345 /* other types someday */
352 accum_load(GLcontext *ctx, GLfloat value,
353 GLint xpos, GLint ypos, GLint width, GLint height )
355 SWcontext *swrast = SWRAST_CONTEXT(ctx);
356 struct gl_renderbuffer *rb
357 = ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer;
358 const GLboolean directAccess = (rb->GetPointer(ctx, rb, 0, 0) != NULL);
362 if (!ctx->ReadBuffer->_ColorReadBuffer) {
363 /* no read buffer - OK */
367 /* This is a change to go into optimized accum buffer mode */
368 if (value > 0.0 && value <= 1.0) {
369 #if USE_OPTIMIZED_ACCUM
370 swrast->_IntegerAccumMode = GL_TRUE;
372 swrast->_IntegerAccumMode = GL_FALSE;
374 swrast->_IntegerAccumScaler = value;
377 swrast->_IntegerAccumMode = GL_FALSE;
378 swrast->_IntegerAccumScaler = 0.0;
381 if (rb->DataType == GL_SHORT || rb->DataType == GL_UNSIGNED_SHORT) {
382 const GLfloat scale = value * ACCUM_SCALE16 / CHAN_MAXF;
383 GLshort accumRow[4 * MAX_WIDTH];
384 GLchan rgba[MAX_WIDTH][4];
387 for (i = 0; i < height; i++) {
390 acc = (GLshort *) rb->GetPointer(ctx, rb, xpos, ypos + i);
393 rb->GetRow(ctx, rb, width, xpos, ypos + i, accumRow);
397 /* read colors from color buffer */
398 _swrast_read_rgba_span(ctx, ctx->ReadBuffer->_ColorReadBuffer, width,
399 xpos, ypos + i, CHAN_TYPE, rgba);
402 if (swrast->_IntegerAccumMode) {
403 /* just copy values in */
405 assert(swrast->_IntegerAccumScaler > 0.0);
406 assert(swrast->_IntegerAccumScaler <= 1.0);
407 for (j = 0; j < width; j++) {
408 acc[j * 4 + 0] = rgba[j][RCOMP];
409 acc[j * 4 + 1] = rgba[j][GCOMP];
410 acc[j * 4 + 2] = rgba[j][BCOMP];
411 acc[j * 4 + 3] = rgba[j][ACOMP];
415 /* scaled integer (or float) accum buffer */
417 for (j = 0; j < width; j++) {
418 acc[j * 4 + 0] = (GLshort) ((GLfloat) rgba[j][RCOMP] * scale);
419 acc[j * 4 + 1] = (GLshort) ((GLfloat) rgba[j][GCOMP] * scale);
420 acc[j * 4 + 2] = (GLshort) ((GLfloat) rgba[j][BCOMP] * scale);
421 acc[j * 4 + 3] = (GLshort) ((GLfloat) rgba[j][ACOMP] * scale);
426 rb->PutRow(ctx, rb, width, xpos, ypos + i, accumRow, NULL);
434 accum_return(GLcontext *ctx, GLfloat value,
435 GLint xpos, GLint ypos, GLint width, GLint height )
437 SWcontext *swrast = SWRAST_CONTEXT(ctx);
438 struct gl_framebuffer *fb = ctx->DrawBuffer;
439 struct gl_renderbuffer *accumRb = fb->Attachment[BUFFER_ACCUM].Renderbuffer;
440 const GLboolean directAccess
441 = (accumRb->GetPointer(ctx, accumRb, 0, 0) != NULL);
442 const GLboolean masking = (!ctx->Color.ColorMask[RCOMP] ||
443 !ctx->Color.ColorMask[GCOMP] ||
444 !ctx->Color.ColorMask[BCOMP] ||
445 !ctx->Color.ColorMask[ACOMP]);
447 static GLchan multTable[32768];
448 static GLfloat prevMult = 0.0;
449 const GLfloat mult = swrast->_IntegerAccumScaler;
450 const GLint max = MIN2((GLint) (256 / mult), 32767);
452 /* May have to leave optimized accum buffer mode */
453 if (swrast->_IntegerAccumMode && value != 1.0)
456 if (swrast->_IntegerAccumMode && swrast->_IntegerAccumScaler > 0) {
457 /* build lookup table to avoid many floating point multiplies */
459 assert(swrast->_IntegerAccumScaler <= 1.0);
460 if (mult != prevMult) {
461 for (j = 0; j < max; j++)
462 multTable[j] = IROUND((GLfloat) j * mult);
467 if (accumRb->DataType == GL_SHORT ||
468 accumRb->DataType == GL_UNSIGNED_SHORT) {
469 const GLfloat scale = value * CHAN_MAXF / ACCUM_SCALE16;
473 /* XXX maybe transpose the 'i' and 'buffer' loops??? */
474 for (i = 0; i < height; i++) {
475 GLshort accumRow[4 * MAX_WIDTH];
479 /* init color span */
480 INIT_SPAN(span, GL_BITMAP);
482 span.arrayMask = SPAN_RGBA;
487 acc = (GLshort *) accumRb->GetPointer(ctx, accumRb, xpos, ypos +i);
490 accumRb->GetRow(ctx, accumRb, width, xpos, ypos + i, accumRow);
494 /* get the colors to return */
495 if (swrast->_IntegerAccumMode) {
497 for (j = 0; j < width; j++) {
498 ASSERT(acc[j * 4 + 0] < max);
499 ASSERT(acc[j * 4 + 1] < max);
500 ASSERT(acc[j * 4 + 2] < max);
501 ASSERT(acc[j * 4 + 3] < max);
502 span.array->rgba[j][RCOMP] = multTable[acc[j * 4 + 0]];
503 span.array->rgba[j][GCOMP] = multTable[acc[j * 4 + 1]];
504 span.array->rgba[j][BCOMP] = multTable[acc[j * 4 + 2]];
505 span.array->rgba[j][ACOMP] = multTable[acc[j * 4 + 3]];
509 /* scaled integer (or float) accum buffer */
511 for (j = 0; j < width; j++) {
513 GLchan r = acc[j * 4 + 0] * scale;
514 GLchan g = acc[j * 4 + 1] * scale;
515 GLchan b = acc[j * 4 + 2] * scale;
516 GLchan a = acc[j * 4 + 3] * scale;
518 GLint r = IROUND( (GLfloat) (acc[j * 4 + 0]) * scale );
519 GLint g = IROUND( (GLfloat) (acc[j * 4 + 1]) * scale );
520 GLint b = IROUND( (GLfloat) (acc[j * 4 + 2]) * scale );
521 GLint a = IROUND( (GLfloat) (acc[j * 4 + 3]) * scale );
523 span.array->rgba[j][RCOMP] = CLAMP( r, 0, CHAN_MAX );
524 span.array->rgba[j][GCOMP] = CLAMP( g, 0, CHAN_MAX );
525 span.array->rgba[j][BCOMP] = CLAMP( b, 0, CHAN_MAX );
526 span.array->rgba[j][ACOMP] = CLAMP( a, 0, CHAN_MAX );
531 for (buffer = 0; buffer < fb->_NumColorDrawBuffers; buffer++) {
532 struct gl_renderbuffer *rb = fb->_ColorDrawBuffers[buffer];
534 _swrast_mask_rgba_span(ctx, rb, &span);
536 rb->PutRow(ctx, rb, width, xpos, ypos + i, span.array->rgba, NULL);
541 /* other types someday */
548 * Software fallback for glAccum.
551 _swrast_Accum(GLcontext *ctx, GLenum op, GLfloat value)
553 SWcontext *swrast = SWRAST_CONTEXT(ctx);
554 GLint xpos, ypos, width, height;
556 if (swrast->NewState)
557 _swrast_validate_derived( ctx );
559 if (!ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer) {
560 _mesa_warning(ctx, "Calling glAccum() without an accumulation buffer");
564 swrast_render_start(ctx);
566 /* Compute region after calling swrast_render_start() so that we know the
567 * drawbuffer's size/bounds are up to date.
569 xpos = ctx->DrawBuffer->_Xmin;
570 ypos = ctx->DrawBuffer->_Ymin;
571 width = ctx->DrawBuffer->_Xmax - ctx->DrawBuffer->_Xmin;
572 height = ctx->DrawBuffer->_Ymax - ctx->DrawBuffer->_Ymin;
577 accum_add(ctx, value, xpos, ypos, width, height);
582 accum_mult(ctx, value, xpos, ypos, width, height);
587 accum_accum(ctx, value, xpos, ypos, width, height);
591 accum_load(ctx, value, xpos, ypos, width, height);
594 accum_return(ctx, value, xpos, ypos, width, height);
597 _mesa_problem(ctx, "invalid mode in _swrast_Accum()");
601 swrast_render_finish(ctx);
605 #endif /* FEATURE_accum */