7a10b4b4b987b9fe4ab4272c8ef8a5fcded5403d
1 /* $Id: s_accum.c,v 1.11 2001/04/20 19:21:41 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 #include "s_alphabuf.h"
36 #include "s_context.h"
37 #include "s_masking.h"
42 * Accumulation buffer notes
44 * Normally, accumulation buffer values are GLshorts with values in
45 * [-32767, 32767] which represent floating point colors in [-1, 1],
46 * as suggested by the OpenGL specification.
48 * We optimize for the common case used for full-scene antialiasing:
49 * // start with accum buffer cleared to zero
50 * glAccum(GL_LOAD, w); // or GL_ACCUM the first image
51 * glAccum(GL_ACCUM, w);
53 * glAccum(GL_ACCUM, w);
54 * glAccum(GL_RETURN, 1.0);
55 * That is, we start with an empty accumulation buffer and accumulate
56 * n images, each with weight w = 1/n.
57 * In this scenario, we can simply store unscaled integer values in
58 * the accum buffer instead of scaled integers. We'll also keep track
59 * of the w value so when we do GL_RETURN we simply divide the accumulated
61 * This lets us avoid _many_ int->float->int conversions.
66 #define USE_OPTIMIZED_ACCUM /* enable the optimization */
72 _mesa_alloc_accum_buffer( GLcontext
*ctx
)
74 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
77 if (ctx
->DrawBuffer
->Accum
) {
78 FREE( ctx
->DrawBuffer
->Accum
);
79 ctx
->DrawBuffer
->Accum
= NULL
;
82 /* allocate accumulation buffer if not already present */
83 n
= ctx
->DrawBuffer
->Width
* ctx
->DrawBuffer
->Height
* 4 * sizeof(GLaccum
);
84 ctx
->DrawBuffer
->Accum
= (GLaccum
*) MALLOC( n
);
85 if (!ctx
->DrawBuffer
->Accum
) {
86 /* unable to setup accumulation buffer */
87 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "glAccum" );
89 #ifdef USE_OPTIMIZED_ACCUM
90 swrast
->_IntegerAccumMode
= GL_TRUE
;
92 swrast
->_IntegerAccumMode
= GL_FALSE
;
94 swrast
->_IntegerAccumScaler
= 0.0;
103 * This is called when we fall out of optimized/unscaled accum buffer mode.
104 * That is, we convert each unscaled accum buffer value into a scaled value
105 * representing the range[-1, 1].
107 static void rescale_accum( GLcontext
*ctx
)
109 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
110 const GLuint n
= ctx
->DrawBuffer
->Width
* ctx
->DrawBuffer
->Height
* 4;
111 const GLfloat fChanMax
= (1 << (sizeof(GLchan
) * 8)) - 1;
112 const GLfloat s
= swrast
->_IntegerAccumScaler
* (32767.0 / fChanMax
);
113 GLaccum
*accum
= ctx
->DrawBuffer
->Accum
;
116 assert(swrast
->_IntegerAccumMode
);
119 for (i
= 0; i
< n
; i
++) {
120 accum
[i
] = (GLaccum
) (accum
[i
] * s
);
123 swrast
->_IntegerAccumMode
= GL_FALSE
;
132 * Clear the accumulation Buffer.
135 _mesa_clear_accum_buffer( GLcontext
*ctx
)
137 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
141 if (ctx
->Visual
.accumRedBits
==0) {
142 /* No accumulation buffer! */
146 if (sizeof(GLaccum
)==1) {
149 else if (sizeof(GLaccum
)==2) {
153 /* sizeof(GLaccum) > 2 (Cray) */
154 acc_scale
= (float) SHRT_MAX
;
157 /* number of pixels */
158 buffersize
= ctx
->DrawBuffer
->Width
* ctx
->DrawBuffer
->Height
;
160 if (!ctx
->DrawBuffer
->Accum
) {
161 /* try to alloc accumulation buffer */
162 ctx
->DrawBuffer
->Accum
= (GLaccum
*)
163 MALLOC( buffersize
* 4 * sizeof(GLaccum
) );
166 if (ctx
->DrawBuffer
->Accum
) {
167 if (ctx
->Scissor
.Enabled
) {
168 /* Limit clear to scissor box */
173 r
= (GLaccum
) (ctx
->Accum
.ClearColor
[0] * acc_scale
);
174 g
= (GLaccum
) (ctx
->Accum
.ClearColor
[1] * acc_scale
);
175 b
= (GLaccum
) (ctx
->Accum
.ClearColor
[2] * acc_scale
);
176 a
= (GLaccum
) (ctx
->Accum
.ClearColor
[3] * acc_scale
);
177 /* size of region to clear */
178 width
= 4 * (ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
);
179 height
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
180 /* ptr to first element to clear */
181 row
= ctx
->DrawBuffer
->Accum
182 + 4 * (ctx
->DrawBuffer
->_Ymin
* ctx
->DrawBuffer
->Width
183 + ctx
->DrawBuffer
->_Xmin
);
184 for (j
=0;j
<height
;j
++) {
185 for (i
=0;i
<width
;i
+=4) {
191 row
+= 4 * ctx
->DrawBuffer
->Width
;
195 /* clear whole buffer */
196 if (ctx
->Accum
.ClearColor
[0]==0.0 &&
197 ctx
->Accum
.ClearColor
[1]==0.0 &&
198 ctx
->Accum
.ClearColor
[2]==0.0 &&
199 ctx
->Accum
.ClearColor
[3]==0.0) {
201 BZERO( ctx
->DrawBuffer
->Accum
, buffersize
* 4 * sizeof(GLaccum
) );
205 GLaccum
*acc
, r
, g
, b
, a
;
208 acc
= ctx
->DrawBuffer
->Accum
;
209 r
= (GLaccum
) (ctx
->Accum
.ClearColor
[0] * acc_scale
);
210 g
= (GLaccum
) (ctx
->Accum
.ClearColor
[1] * acc_scale
);
211 b
= (GLaccum
) (ctx
->Accum
.ClearColor
[2] * acc_scale
);
212 a
= (GLaccum
) (ctx
->Accum
.ClearColor
[3] * acc_scale
);
213 for (i
=0;i
<buffersize
;i
++) {
222 /* update optimized accum state vars */
223 if (ctx
->Accum
.ClearColor
[0] == 0.0 && ctx
->Accum
.ClearColor
[1] == 0.0 &&
224 ctx
->Accum
.ClearColor
[2] == 0.0 && ctx
->Accum
.ClearColor
[3] == 0.0) {
225 #ifdef USE_OPTIMIZED_ACCUM
226 swrast
->_IntegerAccumMode
= GL_TRUE
;
228 swrast
->_IntegerAccumMode
= GL_FALSE
;
230 swrast
->_IntegerAccumScaler
= 0.0; /* denotes empty accum buffer */
233 swrast
->_IntegerAccumMode
= GL_FALSE
;
240 _swrast_Accum( GLcontext
*ctx
, GLenum op
, GLfloat value
,
241 GLint xpos
, GLint ypos
,
242 GLint width
, GLint height
)
245 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
248 GLchan rgba
[MAX_WIDTH
][4];
249 const GLuint colorMask
= *((GLuint
*) &ctx
->Color
.ColorMask
);
250 const GLint iChanMax
= (1 << (sizeof(GLchan
) * 8)) - 1;
251 const GLfloat fChanMax
= (1 << (sizeof(GLchan
) * 8)) - 1;
254 if (SWRAST_CONTEXT(ctx
)->NewState
)
255 _swrast_validate_derived( ctx
);
257 if (!ctx
->DrawBuffer
->Accum
) {
259 "Calling glAccum() without an accumulation "
260 "buffer (low memory?)");
264 if (sizeof(GLaccum
)==1) {
267 else if (sizeof(GLaccum
)==2) {
271 /* sizeof(GLaccum) > 2 (Cray) */
272 acc_scale
= (float) SHRT_MAX
;
280 const GLaccum intVal
= (GLaccum
) (value
* acc_scale
);
282 /* Leave optimized accum buffer mode */
283 if (swrast
->_IntegerAccumMode
)
285 for (j
= 0; j
< height
; j
++) {
286 GLaccum
* acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ 4 * xpos
;
288 for (i
= 0; i
< width4
; i
++) {
299 /* Leave optimized accum buffer mode */
300 if (swrast
->_IntegerAccumMode
)
302 for (j
= 0; j
< height
; j
++) {
303 GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ 4 * xpos
;
305 for (i
= 0; i
< width4
; i
++) {
306 acc
[i
] = (GLaccum
) ( (GLfloat
) acc
[i
] * value
);
317 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
318 ctx
->Pixel
.DriverReadBuffer
);
320 /* May have to leave optimized accum buffer mode */
321 if (swrast
->_IntegerAccumScaler
== 0.0 && value
> 0.0 && value
<= 1.0)
322 swrast
->_IntegerAccumScaler
= value
;
323 if (swrast
->_IntegerAccumMode
&& value
!= swrast
->_IntegerAccumScaler
)
326 RENDER_START(swrast
,ctx
);
328 if (swrast
->_IntegerAccumMode
) {
329 /* simply add integer color values into accum buffer */
331 GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ xpos
* 4;
332 assert(swrast
->_IntegerAccumScaler
> 0.0);
333 assert(swrast
->_IntegerAccumScaler
<= 1.0);
334 for (j
= 0; j
< height
; j
++) {
337 _mesa_read_rgba_span(ctx
, ctx
->DrawBuffer
, width
, xpos
, ypos
, rgba
);
338 for (i
= i4
= 0; i
< width
; i
++, i4
+=4) {
339 acc
[i4
+0] += rgba
[i
][RCOMP
];
340 acc
[i4
+1] += rgba
[i
][GCOMP
];
341 acc
[i4
+2] += rgba
[i
][BCOMP
];
342 acc
[i4
+3] += rgba
[i
][ACOMP
];
349 /* scaled integer accum buffer */
350 const GLfloat rscale
= value
* acc_scale
/ fChanMax
;
351 const GLfloat gscale
= value
* acc_scale
/ fChanMax
;
352 const GLfloat bscale
= value
* acc_scale
/ fChanMax
;
353 const GLfloat ascale
= value
* acc_scale
/ fChanMax
;
355 for (j
=0;j
<height
;j
++) {
356 GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ xpos
* 4;
358 _mesa_read_rgba_span(ctx
, ctx
->DrawBuffer
, width
, xpos
, ypos
, rgba
);
359 for (i
=0;i
<width
;i
++) {
360 *acc
+= (GLaccum
) ( (GLfloat
) rgba
[i
][RCOMP
] * rscale
); acc
++;
361 *acc
+= (GLaccum
) ( (GLfloat
) rgba
[i
][GCOMP
] * gscale
); acc
++;
362 *acc
+= (GLaccum
) ( (GLfloat
) rgba
[i
][BCOMP
] * bscale
); acc
++;
363 *acc
+= (GLaccum
) ( (GLfloat
) rgba
[i
][ACOMP
] * ascale
); acc
++;
368 /* restore read buffer = draw buffer (the default) */
369 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
370 ctx
->Color
.DriverDrawBuffer
);
371 RENDER_FINISH(swrast
,ctx
);
375 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
376 ctx
->Pixel
.DriverReadBuffer
);
378 /* This is a change to go into optimized accum buffer mode */
379 if (value
> 0.0 && value
<= 1.0) {
380 #ifdef USE_OPTIMIZED_ACCUM
381 swrast
->_IntegerAccumMode
= GL_TRUE
;
383 swrast
->_IntegerAccumMode
= GL_FALSE
;
385 swrast
->_IntegerAccumScaler
= value
;
388 swrast
->_IntegerAccumMode
= GL_FALSE
;
389 swrast
->_IntegerAccumScaler
= 0.0;
392 RENDER_START(swrast
,ctx
);
393 if (swrast
->_IntegerAccumMode
) {
394 /* just copy values into accum buffer */
396 GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ xpos
* 4;
397 assert(swrast
->_IntegerAccumScaler
> 0.0);
398 assert(swrast
->_IntegerAccumScaler
<= 1.0);
399 for (j
= 0; j
< height
; j
++) {
401 _mesa_read_rgba_span(ctx
, ctx
->DrawBuffer
, width
, xpos
, ypos
, rgba
);
402 for (i
= i4
= 0; i
< width
; i
++, i4
+= 4) {
403 acc
[i4
+0] = rgba
[i
][RCOMP
];
404 acc
[i4
+1] = rgba
[i
][GCOMP
];
405 acc
[i4
+2] = rgba
[i
][BCOMP
];
406 acc
[i4
+3] = rgba
[i
][ACOMP
];
413 /* scaled integer accum buffer */
414 const GLfloat rscale
= value
* acc_scale
/ fChanMax
;
415 const GLfloat gscale
= value
* acc_scale
/ fChanMax
;
416 const GLfloat bscale
= value
* acc_scale
/ fChanMax
;
417 const GLfloat ascale
= value
* acc_scale
/ fChanMax
;
418 const GLfloat d
= 3.0 / acc_scale
;
420 for (j
= 0; j
< height
; j
++) {
421 GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ xpos
* 4;
422 _mesa_read_rgba_span(ctx
, ctx
->DrawBuffer
, width
, xpos
, ypos
, rgba
);
423 for (i
=0;i
<width
;i
++) {
424 *acc
++ = (GLaccum
) ((GLfloat
) rgba
[i
][RCOMP
] * rscale
+ d
);
425 *acc
++ = (GLaccum
) ((GLfloat
) rgba
[i
][GCOMP
] * gscale
+ d
);
426 *acc
++ = (GLaccum
) ((GLfloat
) rgba
[i
][BCOMP
] * bscale
+ d
);
427 *acc
++ = (GLaccum
) ((GLfloat
) rgba
[i
][ACOMP
] * ascale
+ d
);
433 /* restore read buffer = draw buffer (the default) */
434 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
435 ctx
->Color
.DriverDrawBuffer
);
436 RENDER_FINISH(swrast
,ctx
);
440 /* May have to leave optimized accum buffer mode */
441 if (swrast
->_IntegerAccumMode
&& value
!= 1.0)
444 RENDER_START(swrast
,ctx
);
445 if (swrast
->_IntegerAccumMode
&& swrast
->_IntegerAccumScaler
> 0) {
446 /* build lookup table to avoid many floating point multiplies */
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 if (mult
!= prevMult
) {
453 for (j
= 0; j
< max
; j
++)
454 multTable
[j
] = IROUND((GLfloat
) j
* mult
);
458 assert(swrast
->_IntegerAccumScaler
> 0.0);
459 assert(swrast
->_IntegerAccumScaler
<= 1.0);
460 for (j
= 0; j
< height
; j
++) {
461 const GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ xpos
*4;
463 for (i
= i4
= 0; i
< width
; i
++, i4
+= 4) {
464 ASSERT(acc
[i4
+0] < max
);
465 ASSERT(acc
[i4
+1] < max
);
466 ASSERT(acc
[i4
+2] < max
);
467 ASSERT(acc
[i4
+3] < max
);
468 rgba
[i
][RCOMP
] = multTable
[acc
[i4
+0]];
469 rgba
[i
][GCOMP
] = multTable
[acc
[i4
+1]];
470 rgba
[i
][BCOMP
] = multTable
[acc
[i4
+2]];
471 rgba
[i
][ACOMP
] = multTable
[acc
[i4
+3]];
473 if (colorMask
!= 0xffffffff) {
474 _mesa_mask_rgba_span( ctx
, width
, xpos
, ypos
, rgba
);
476 (*swrast
->Driver
.WriteRGBASpan
)( ctx
, width
, xpos
, ypos
,
477 (const GLchan (*)[4])rgba
, NULL
);
478 if (ctx
->DrawBuffer
->UseSoftwareAlphaBuffers
479 && ctx
->Color
.ColorMask
[ACOMP
]) {
480 _mesa_write_alpha_span(ctx
, width
, xpos
, ypos
,
481 (CONST
GLchan (*)[4]) rgba
, NULL
);
487 const GLfloat rscale
= value
/ acc_scale
* fChanMax
;
488 const GLfloat gscale
= value
/ acc_scale
* fChanMax
;
489 const GLfloat bscale
= value
/ acc_scale
* fChanMax
;
490 const GLfloat ascale
= value
/ acc_scale
* fChanMax
;
492 for (j
=0;j
<height
;j
++) {
493 const GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ xpos
*4;
494 for (i
=0;i
<width
;i
++) {
495 GLint r
= IROUND( (GLfloat
) (acc
[0]) * rscale
);
496 GLint g
= IROUND( (GLfloat
) (acc
[1]) * gscale
);
497 GLint b
= IROUND( (GLfloat
) (acc
[2]) * bscale
);
498 GLint a
= IROUND( (GLfloat
) (acc
[3]) * ascale
);
500 rgba
[i
][RCOMP
] = CLAMP( r
, 0, iChanMax
);
501 rgba
[i
][GCOMP
] = CLAMP( g
, 0, iChanMax
);
502 rgba
[i
][BCOMP
] = CLAMP( b
, 0, iChanMax
);
503 rgba
[i
][ACOMP
] = CLAMP( a
, 0, iChanMax
);
505 if (colorMask
!= 0xffffffff) {
506 _mesa_mask_rgba_span( ctx
, width
, xpos
, ypos
, rgba
);
508 (*swrast
->Driver
.WriteRGBASpan
)( ctx
, width
, xpos
, ypos
,
509 (const GLchan (*)[4])rgba
, NULL
);
510 if (ctx
->DrawBuffer
->UseSoftwareAlphaBuffers
511 && ctx
->Color
.ColorMask
[ACOMP
]) {
512 _mesa_write_alpha_span(ctx
, width
, xpos
, ypos
,
513 (CONST
GLchan (*)[4]) rgba
, NULL
);
518 RENDER_FINISH(swrast
,ctx
);
522 _mesa_error( ctx
, GL_INVALID_ENUM
, "glAccum" );