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/condrender.h"
28 #include "main/context.h"
29 #include "main/macros.h"
30 #include "main/imports.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.0F
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
77 * This is called when we fall out of optimized/unscaled accum buffer mode.
78 * That is, we convert each unscaled accum buffer value into a scaled value
79 * representing the range[-1, 1].
82 rescale_accum( struct gl_context *ctx )
84 SWcontext *swrast = SWRAST_CONTEXT(ctx);
85 struct gl_renderbuffer *rb
86 = ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer;
87 const GLfloat s = swrast->_IntegerAccumScaler * (32767.0F / CHAN_MAXF);
90 assert(rb->_BaseFormat == GL_RGBA);
91 /* add other types in future? */
92 assert(rb->DataType == GL_SHORT || rb->DataType == GL_UNSIGNED_SHORT);
93 assert(swrast->_IntegerAccumMode);
95 if (rb->GetPointer(ctx, rb, 0, 0)) {
96 /* directly-addressable memory */
98 for (y = 0; y < rb->Height; y++) {
100 GLshort *acc = (GLshort *) rb->GetPointer(ctx, rb, 0, y);
101 for (i = 0; i < 4 * rb->Width; i++) {
102 acc[i] = (GLshort) (acc[i] * s);
107 /* use get/put row funcs */
109 for (y = 0; y < rb->Height; y++) {
110 GLshort accRow[MAX_WIDTH * 4];
112 rb->GetRow(ctx, rb, rb->Width, 0, y, accRow);
113 for (i = 0; i < 4 * rb->Width; i++) {
114 accRow[i] = (GLshort) (accRow[i] * s);
116 rb->PutRow(ctx, rb, rb->Width, 0, y, accRow, NULL);
120 swrast->_IntegerAccumMode = GL_FALSE;
126 * Clear the accumulation Buffer.
129 _swrast_clear_accum_buffer( struct gl_context *ctx, struct gl_renderbuffer *rb )
131 SWcontext *swrast = SWRAST_CONTEXT(ctx);
132 GLuint x, y, width, height;
134 /* No accumulation buffer! Not an error. */
135 if (!rb || !rb->Data)
138 assert(rb->_BaseFormat == GL_RGBA);
139 /* add other types in future? */
140 assert(rb->DataType == GL_SHORT || rb->DataType == GL_UNSIGNED_SHORT);
142 /* bounds, with scissor */
143 x = ctx->DrawBuffer->_Xmin;
144 y = ctx->DrawBuffer->_Ymin;
145 width = ctx->DrawBuffer->_Xmax - ctx->DrawBuffer->_Xmin;
146 height = ctx->DrawBuffer->_Ymax - ctx->DrawBuffer->_Ymin;
148 if (rb->DataType == GL_SHORT || rb->DataType == GL_UNSIGNED_SHORT) {
149 const GLfloat accScale = 32767.0;
153 clearVal[0] = (GLshort) (ctx->Accum.ClearColor[0] * accScale);
154 clearVal[1] = (GLshort) (ctx->Accum.ClearColor[1] * accScale);
155 clearVal[2] = (GLshort) (ctx->Accum.ClearColor[2] * accScale);
156 clearVal[3] = (GLshort) (ctx->Accum.ClearColor[3] * accScale);
158 for (i = 0; i < height; i++) {
159 rb->PutMonoRow(ctx, rb, width, x, y + i, clearVal, NULL);
163 /* someday support other sizes */
166 /* update optimized accum state vars */
167 if (ctx->Accum.ClearColor[0] == 0.0 && ctx->Accum.ClearColor[1] == 0.0 &&
168 ctx->Accum.ClearColor[2] == 0.0 && ctx->Accum.ClearColor[3] == 0.0) {
169 #if USE_OPTIMIZED_ACCUM
170 swrast->_IntegerAccumMode = GL_TRUE;
172 swrast->_IntegerAccumMode = GL_FALSE;
174 swrast->_IntegerAccumScaler = 0.0; /* denotes empty accum buffer */
177 swrast->_IntegerAccumMode = GL_FALSE;
183 accum_add(struct gl_context *ctx, GLfloat value,
184 GLint xpos, GLint ypos, GLint width, GLint height )
186 SWcontext *swrast = SWRAST_CONTEXT(ctx);
187 struct gl_renderbuffer *rb
188 = ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer;
192 /* Leave optimized accum buffer mode */
193 if (swrast->_IntegerAccumMode)
196 if (rb->DataType == GL_SHORT || rb->DataType == GL_UNSIGNED_SHORT) {
197 const GLshort incr = (GLshort) (value * ACCUM_SCALE16);
198 if (rb->GetPointer(ctx, rb, 0, 0)) {
200 for (i = 0; i < height; i++) {
201 GLshort *acc = (GLshort *) rb->GetPointer(ctx, rb, xpos, ypos + i);
202 for (j = 0; j < 4 * width; j++) {
209 for (i = 0; i < height; i++) {
210 GLshort accRow[4 * MAX_WIDTH];
211 rb->GetRow(ctx, rb, width, xpos, ypos + i, accRow);
212 for (j = 0; j < 4 * width; j++) {
215 rb->PutRow(ctx, rb, width, xpos, ypos + i, accRow, NULL);
220 /* other types someday */
226 accum_mult(struct gl_context *ctx, GLfloat mult,
227 GLint xpos, GLint ypos, GLint width, GLint height )
229 SWcontext *swrast = SWRAST_CONTEXT(ctx);
230 struct gl_renderbuffer *rb
231 = ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer;
235 /* Leave optimized accum buffer mode */
236 if (swrast->_IntegerAccumMode)
239 if (rb->DataType == GL_SHORT || rb->DataType == GL_UNSIGNED_SHORT) {
240 if (rb->GetPointer(ctx, rb, 0, 0)) {
242 for (i = 0; i < height; i++) {
243 GLshort *acc = (GLshort *) rb->GetPointer(ctx, rb, xpos, ypos + i);
244 for (j = 0; j < 4 * width; j++) {
245 acc[j] = (GLshort) (acc[j] * mult);
251 for (i = 0; i < height; i++) {
252 GLshort accRow[4 * MAX_WIDTH];
253 rb->GetRow(ctx, rb, width, xpos, ypos + i, accRow);
254 for (j = 0; j < 4 * width; j++) {
255 accRow[j] = (GLshort) (accRow[j] * mult);
257 rb->PutRow(ctx, rb, width, xpos, ypos + i, accRow, NULL);
262 /* other types someday */
269 accum_accum(struct gl_context *ctx, GLfloat value,
270 GLint xpos, GLint ypos, GLint width, GLint height )
272 SWcontext *swrast = SWRAST_CONTEXT(ctx);
273 struct gl_renderbuffer *rb
274 = ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer;
275 const GLboolean directAccess = (rb->GetPointer(ctx, rb, 0, 0) != NULL);
279 if (!ctx->ReadBuffer->_ColorReadBuffer) {
280 /* no read buffer - OK */
284 /* May have to leave optimized accum buffer mode */
285 if (swrast->_IntegerAccumScaler == 0.0 && value > 0.0 && value <= 1.0)
286 swrast->_IntegerAccumScaler = value;
287 if (swrast->_IntegerAccumMode && value != swrast->_IntegerAccumScaler)
290 if (rb->DataType == GL_SHORT || rb->DataType == GL_UNSIGNED_SHORT) {
291 const GLfloat scale = value * ACCUM_SCALE16 / CHAN_MAXF;
292 GLshort accumRow[4 * MAX_WIDTH];
293 GLchan rgba[MAX_WIDTH][4];
296 for (i = 0; i < height; i++) {
299 acc = (GLshort *) rb->GetPointer(ctx, rb, xpos, ypos + i);
302 rb->GetRow(ctx, rb, width, xpos, ypos + i, accumRow);
306 /* read colors from color buffer */
307 _swrast_read_rgba_span(ctx, ctx->ReadBuffer->_ColorReadBuffer, width,
308 xpos, ypos + i, CHAN_TYPE, rgba);
310 /* do accumulation */
311 if (swrast->_IntegerAccumMode) {
312 /* simply add integer color values into accum buffer */
314 for (j = 0; j < width; j++) {
315 acc[j * 4 + 0] += rgba[j][RCOMP];
316 acc[j * 4 + 1] += rgba[j][GCOMP];
317 acc[j * 4 + 2] += rgba[j][BCOMP];
318 acc[j * 4 + 3] += rgba[j][ACOMP];
322 /* scaled integer (or float) accum buffer */
324 for (j = 0; j < width; j++) {
325 acc[j * 4 + 0] += (GLshort) ((GLfloat) rgba[j][RCOMP] * scale);
326 acc[j * 4 + 1] += (GLshort) ((GLfloat) rgba[j][GCOMP] * scale);
327 acc[j * 4 + 2] += (GLshort) ((GLfloat) rgba[j][BCOMP] * scale);
328 acc[j * 4 + 3] += (GLshort) ((GLfloat) rgba[j][ACOMP] * scale);
333 rb->PutRow(ctx, rb, width, xpos, ypos + i, accumRow, NULL);
338 /* other types someday */
345 accum_load(struct gl_context *ctx, GLfloat value,
346 GLint xpos, GLint ypos, GLint width, GLint height )
348 SWcontext *swrast = SWRAST_CONTEXT(ctx);
349 struct gl_renderbuffer *rb
350 = ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer;
351 const GLboolean directAccess = (rb->GetPointer(ctx, rb, 0, 0) != NULL);
355 if (!ctx->ReadBuffer->_ColorReadBuffer) {
356 /* no read buffer - OK */
360 /* This is a change to go into optimized accum buffer mode */
361 if (value > 0.0 && value <= 1.0) {
362 #if USE_OPTIMIZED_ACCUM
363 swrast->_IntegerAccumMode = GL_TRUE;
365 swrast->_IntegerAccumMode = GL_FALSE;
367 swrast->_IntegerAccumScaler = value;
370 swrast->_IntegerAccumMode = GL_FALSE;
371 swrast->_IntegerAccumScaler = 0.0;
374 if (rb->DataType == GL_SHORT || rb->DataType == GL_UNSIGNED_SHORT) {
375 const GLfloat scale = value * ACCUM_SCALE16 / CHAN_MAXF;
376 GLshort accumRow[4 * MAX_WIDTH];
377 GLchan rgba[MAX_WIDTH][4];
380 for (i = 0; i < height; i++) {
383 acc = (GLshort *) rb->GetPointer(ctx, rb, xpos, ypos + i);
386 rb->GetRow(ctx, rb, width, xpos, ypos + i, accumRow);
390 /* read colors from color buffer */
391 _swrast_read_rgba_span(ctx, ctx->ReadBuffer->_ColorReadBuffer, width,
392 xpos, ypos + i, CHAN_TYPE, rgba);
395 if (swrast->_IntegerAccumMode) {
396 /* just copy values in */
398 assert(swrast->_IntegerAccumScaler > 0.0);
399 assert(swrast->_IntegerAccumScaler <= 1.0);
400 for (j = 0; j < width; j++) {
401 acc[j * 4 + 0] = rgba[j][RCOMP];
402 acc[j * 4 + 1] = rgba[j][GCOMP];
403 acc[j * 4 + 2] = rgba[j][BCOMP];
404 acc[j * 4 + 3] = rgba[j][ACOMP];
408 /* scaled integer (or float) accum buffer */
410 for (j = 0; j < width; j++) {
411 acc[j * 4 + 0] = (GLshort) ((GLfloat) rgba[j][RCOMP] * scale);
412 acc[j * 4 + 1] = (GLshort) ((GLfloat) rgba[j][GCOMP] * scale);
413 acc[j * 4 + 2] = (GLshort) ((GLfloat) rgba[j][BCOMP] * scale);
414 acc[j * 4 + 3] = (GLshort) ((GLfloat) rgba[j][ACOMP] * scale);
419 rb->PutRow(ctx, rb, width, xpos, ypos + i, accumRow, NULL);
427 accum_return(struct gl_context *ctx, GLfloat value,
428 GLint xpos, GLint ypos, GLint width, GLint height )
430 SWcontext *swrast = SWRAST_CONTEXT(ctx);
431 struct gl_framebuffer *fb = ctx->DrawBuffer;
432 struct gl_renderbuffer *accumRb = fb->Attachment[BUFFER_ACCUM].Renderbuffer;
433 const GLboolean directAccess
434 = (accumRb->GetPointer(ctx, accumRb, 0, 0) != NULL);
436 static GLchan multTable[32768];
437 static GLfloat prevMult = 0.0;
438 const GLfloat mult = swrast->_IntegerAccumScaler;
439 const GLint max = MIN2((GLint) (256 / mult), 32767);
441 /* May have to leave optimized accum buffer mode */
442 if (swrast->_IntegerAccumMode && value != 1.0)
445 if (swrast->_IntegerAccumMode && swrast->_IntegerAccumScaler > 0) {
446 /* build lookup table to avoid many floating point multiplies */
448 assert(swrast->_IntegerAccumScaler <= 1.0);
449 if (mult != prevMult) {
450 for (j = 0; j < max; j++)
451 multTable[j] = IROUND((GLfloat) j * mult);
456 if (accumRb->DataType == GL_SHORT ||
457 accumRb->DataType == GL_UNSIGNED_SHORT) {
458 const GLfloat scale = value * CHAN_MAXF / ACCUM_SCALE16;
462 /* XXX maybe transpose the 'i' and 'buffer' loops??? */
463 for (i = 0; i < height; i++) {
464 GLshort accumRow[4 * MAX_WIDTH];
468 /* init color span */
469 INIT_SPAN(span, GL_BITMAP);
471 span.arrayMask = SPAN_RGBA;
476 acc = (GLshort *) accumRb->GetPointer(ctx, accumRb, xpos, ypos +i);
479 accumRb->GetRow(ctx, accumRb, width, xpos, ypos + i, accumRow);
483 /* get the colors to return */
484 if (swrast->_IntegerAccumMode) {
486 for (j = 0; j < width; j++) {
487 ASSERT(acc[j * 4 + 0] < max);
488 ASSERT(acc[j * 4 + 1] < max);
489 ASSERT(acc[j * 4 + 2] < max);
490 ASSERT(acc[j * 4 + 3] < max);
491 span.array->rgba[j][RCOMP] = multTable[acc[j * 4 + 0]];
492 span.array->rgba[j][GCOMP] = multTable[acc[j * 4 + 1]];
493 span.array->rgba[j][BCOMP] = multTable[acc[j * 4 + 2]];
494 span.array->rgba[j][ACOMP] = multTable[acc[j * 4 + 3]];
498 /* scaled integer (or float) accum buffer */
500 for (j = 0; j < width; j++) {
502 GLchan r = acc[j * 4 + 0] * scale;
503 GLchan g = acc[j * 4 + 1] * scale;
504 GLchan b = acc[j * 4 + 2] * scale;
505 GLchan a = acc[j * 4 + 3] * scale;
507 GLint r = IROUND( (GLfloat) (acc[j * 4 + 0]) * scale );
508 GLint g = IROUND( (GLfloat) (acc[j * 4 + 1]) * scale );
509 GLint b = IROUND( (GLfloat) (acc[j * 4 + 2]) * scale );
510 GLint a = IROUND( (GLfloat) (acc[j * 4 + 3]) * scale );
512 span.array->rgba[j][RCOMP] = CLAMP( r, 0, CHAN_MAX );
513 span.array->rgba[j][GCOMP] = CLAMP( g, 0, CHAN_MAX );
514 span.array->rgba[j][BCOMP] = CLAMP( b, 0, CHAN_MAX );
515 span.array->rgba[j][ACOMP] = CLAMP( a, 0, CHAN_MAX );
520 for (buffer = 0; buffer < fb->_NumColorDrawBuffers; buffer++) {
521 struct gl_renderbuffer *rb = fb->_ColorDrawBuffers[buffer];
522 const GLboolean masking = (!ctx->Color.ColorMask[buffer][RCOMP] ||
523 !ctx->Color.ColorMask[buffer][GCOMP] ||
524 !ctx->Color.ColorMask[buffer][BCOMP] ||
525 !ctx->Color.ColorMask[buffer][ACOMP]);
527 _swrast_mask_rgba_span(ctx, rb, &span, buffer);
529 rb->PutRow(ctx, rb, width, xpos, ypos + i, span.array->rgba, NULL);
534 /* other types someday */
541 * Software fallback for glAccum.
544 _swrast_Accum(struct gl_context *ctx, GLenum op, GLfloat value)
546 SWcontext *swrast = SWRAST_CONTEXT(ctx);
547 GLint xpos, ypos, width, height;
549 if (swrast->NewState)
550 _swrast_validate_derived( ctx );
552 if (!ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer) {
553 _mesa_warning(ctx, "Calling glAccum() without an accumulation buffer");
557 if (!_mesa_check_conditional_render(ctx))
560 swrast_render_start(ctx);
562 /* Compute region after calling swrast_render_start() so that we know the
563 * drawbuffer's size/bounds are up to date.
565 xpos = ctx->DrawBuffer->_Xmin;
566 ypos = ctx->DrawBuffer->_Ymin;
567 width = ctx->DrawBuffer->_Xmax - ctx->DrawBuffer->_Xmin;
568 height = ctx->DrawBuffer->_Ymax - ctx->DrawBuffer->_Ymin;
573 accum_add(ctx, value, xpos, ypos, width, height);
578 accum_mult(ctx, value, xpos, ypos, width, height);
583 accum_accum(ctx, value, xpos, ypos, width, height);
587 accum_load(ctx, value, xpos, ypos, width, height);
590 accum_return(ctx, value, xpos, ypos, width, height);
593 _mesa_problem(ctx, "invalid mode in _swrast_Accum()");
597 swrast_render_finish(ctx);