2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2004 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.
32 #include "s_alphabuf.h"
33 #include "s_context.h"
34 #include "s_masking.h"
39 * Accumulation buffer notes
41 * Normally, accumulation buffer values are GLshorts with values in
42 * [-32767, 32767] which represent floating point colors in [-1, 1],
43 * as suggested by the OpenGL specification.
45 * We optimize for the common case used for full-scene antialiasing:
46 * // start with accum buffer cleared to zero
47 * glAccum(GL_LOAD, w); // or GL_ACCUM the first image
48 * glAccum(GL_ACCUM, w);
50 * glAccum(GL_ACCUM, w);
51 * glAccum(GL_RETURN, 1.0);
52 * That is, we start with an empty accumulation buffer and accumulate
53 * n images, each with weight w = 1/n.
54 * In this scenario, we can simply store unscaled integer values in
55 * the accum buffer instead of scaled integers. We'll also keep track
56 * of the w value so when we do GL_RETURN we simply divide the accumulated
58 * This lets us avoid _many_ int->float->int conversions.
62 #if CHAN_BITS == 8 && ACCUM_BITS < 32
63 #define USE_OPTIMIZED_ACCUM /* enable the optimization */
68 _swrast_alloc_accum_buffer( GLframebuffer
*buffer
)
70 GET_CURRENT_CONTEXT(ctx
);
74 MESA_PBUFFER_FREE( buffer
->Accum
);
78 /* allocate accumulation buffer if not already present */
79 n
= buffer
->Width
* buffer
->Height
* 4 * sizeof(GLaccum
);
80 buffer
->Accum
= (GLaccum
*) MESA_PBUFFER_ALLOC( n
);
82 /* unable to setup accumulation buffer */
83 _mesa_error( NULL
, GL_OUT_OF_MEMORY
, "glAccum" );
87 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
88 /* XXX these fields should probably be in the GLframebuffer */
89 #ifdef USE_OPTIMIZED_ACCUM
90 swrast
->_IntegerAccumMode
= GL_TRUE
;
92 swrast
->_IntegerAccumMode
= GL_FALSE
;
94 swrast
->_IntegerAccumScaler
= 0.0;
100 * This is called when we fall out of optimized/unscaled accum buffer mode.
101 * That is, we convert each unscaled accum buffer value into a scaled value
102 * representing the range[-1, 1].
104 static void rescale_accum( GLcontext
*ctx
)
106 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
107 const GLuint n
= ctx
->DrawBuffer
->Width
* ctx
->DrawBuffer
->Height
* 4;
108 const GLfloat s
= swrast
->_IntegerAccumScaler
* (32767.0F
/ CHAN_MAXF
);
109 GLaccum
*accum
= ctx
->DrawBuffer
->Accum
;
112 assert(swrast
->_IntegerAccumMode
);
115 for (i
= 0; i
< n
; i
++) {
116 accum
[i
] = (GLaccum
) (accum
[i
] * s
);
119 swrast
->_IntegerAccumMode
= GL_FALSE
;
128 * Clear the accumulation Buffer.
131 _swrast_clear_accum_buffer( GLcontext
*ctx
)
133 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
137 if (ctx
->Visual
.accumRedBits
==0) {
138 /* No accumulation buffer! */
142 if (sizeof(GLaccum
)==1) {
145 else if (sizeof(GLaccum
)==2) {
152 /* number of pixels */
153 buffersize
= ctx
->DrawBuffer
->Width
* ctx
->DrawBuffer
->Height
;
155 if (!ctx
->DrawBuffer
->Accum
) {
156 /* try to alloc accumulation buffer */
157 ctx
->DrawBuffer
->Accum
= (GLaccum
*)
158 MALLOC( buffersize
* 4 * sizeof(GLaccum
) );
161 if (ctx
->DrawBuffer
->Accum
) {
162 if (ctx
->Scissor
.Enabled
) {
163 /* Limit clear to scissor box */
164 const GLaccum r
= (GLaccum
) (ctx
->Accum
.ClearColor
[0] * acc_scale
);
165 const GLaccum g
= (GLaccum
) (ctx
->Accum
.ClearColor
[1] * acc_scale
);
166 const GLaccum b
= (GLaccum
) (ctx
->Accum
.ClearColor
[2] * acc_scale
);
167 const GLaccum a
= (GLaccum
) (ctx
->Accum
.ClearColor
[3] * acc_scale
);
171 /* size of region to clear */
172 width
= 4 * (ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
);
173 height
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
174 /* ptr to first element to clear */
175 row
= ctx
->DrawBuffer
->Accum
176 + 4 * (ctx
->DrawBuffer
->_Ymin
* ctx
->DrawBuffer
->Width
177 + ctx
->DrawBuffer
->_Xmin
);
178 for (j
=0;j
<height
;j
++) {
179 for (i
=0;i
<width
;i
+=4) {
185 row
+= 4 * ctx
->DrawBuffer
->Width
;
189 /* clear whole buffer */
190 if (ctx
->Accum
.ClearColor
[0]==0.0 &&
191 ctx
->Accum
.ClearColor
[1]==0.0 &&
192 ctx
->Accum
.ClearColor
[2]==0.0 &&
193 ctx
->Accum
.ClearColor
[3]==0.0) {
195 _mesa_bzero( ctx
->DrawBuffer
->Accum
,
196 buffersize
* 4 * sizeof(GLaccum
) );
200 const GLaccum r
= (GLaccum
) (ctx
->Accum
.ClearColor
[0] * acc_scale
);
201 const GLaccum g
= (GLaccum
) (ctx
->Accum
.ClearColor
[1] * acc_scale
);
202 const GLaccum b
= (GLaccum
) (ctx
->Accum
.ClearColor
[2] * acc_scale
);
203 const GLaccum a
= (GLaccum
) (ctx
->Accum
.ClearColor
[3] * acc_scale
);
204 GLaccum
*acc
= ctx
->DrawBuffer
->Accum
;
206 for (i
=0;i
<buffersize
;i
++) {
215 /* update optimized accum state vars */
216 if (ctx
->Accum
.ClearColor
[0] == 0.0 && ctx
->Accum
.ClearColor
[1] == 0.0 &&
217 ctx
->Accum
.ClearColor
[2] == 0.0 && ctx
->Accum
.ClearColor
[3] == 0.0) {
218 #ifdef USE_OPTIMIZED_ACCUM
219 swrast
->_IntegerAccumMode
= GL_TRUE
;
221 swrast
->_IntegerAccumMode
= GL_FALSE
;
223 swrast
->_IntegerAccumScaler
= 0.0; /* denotes empty accum buffer */
226 swrast
->_IntegerAccumMode
= GL_FALSE
;
233 _swrast_Accum( GLcontext
*ctx
, GLenum op
, GLfloat value
,
234 GLint xpos
, GLint ypos
,
235 GLint width
, GLint height
)
238 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
241 GLchan rgba
[MAX_WIDTH
][4];
242 const GLuint colorMask
= *((GLuint
*) &ctx
->Color
.ColorMask
);
245 if (SWRAST_CONTEXT(ctx
)->NewState
)
246 _swrast_validate_derived( ctx
);
248 if (!ctx
->DrawBuffer
->Accum
) {
250 "Calling glAccum() without an accumulation "
251 "buffer (low memory?)");
255 if (sizeof(GLaccum
)==1) {
258 else if (sizeof(GLaccum
)==2) {
270 const GLaccum val
= (GLaccum
) (value
* acc_scale
);
272 /* Leave optimized accum buffer mode */
273 if (swrast
->_IntegerAccumMode
)
275 for (j
= 0; j
< height
; j
++) {
276 GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ 4*xpos
;
278 for (i
= 0; i
< width4
; i
++) {
289 /* Leave optimized accum buffer mode */
290 if (swrast
->_IntegerAccumMode
)
292 for (j
= 0; j
< height
; j
++) {
293 GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ 4 * xpos
;
295 for (i
= 0; i
< width4
; i
++) {
296 acc
[i
] = (GLaccum
) ( (GLfloat
) acc
[i
] * value
);
307 _swrast_use_read_buffer(ctx
);
309 /* May have to leave optimized accum buffer mode */
310 if (swrast
->_IntegerAccumScaler
== 0.0 && value
> 0.0 && value
<= 1.0)
311 swrast
->_IntegerAccumScaler
= value
;
312 if (swrast
->_IntegerAccumMode
&& value
!= swrast
->_IntegerAccumScaler
)
315 RENDER_START(swrast
,ctx
);
317 if (swrast
->_IntegerAccumMode
) {
318 /* simply add integer color values into accum buffer */
320 GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ xpos
* 4;
321 assert(swrast
->_IntegerAccumScaler
> 0.0);
322 assert(swrast
->_IntegerAccumScaler
<= 1.0);
323 for (j
= 0; j
< height
; j
++) {
326 _swrast_read_rgba_span(ctx
, ctx
->DrawBuffer
, width
, xpos
, ypos
, rgba
);
327 for (i
= i4
= 0; i
< width
; i
++, i4
+=4) {
328 acc
[i4
+0] += rgba
[i
][RCOMP
];
329 acc
[i4
+1] += rgba
[i
][GCOMP
];
330 acc
[i4
+2] += rgba
[i
][BCOMP
];
331 acc
[i4
+3] += rgba
[i
][ACOMP
];
338 /* scaled integer (or float) accum buffer */
339 const GLfloat rscale
= value
* acc_scale
/ CHAN_MAXF
;
340 const GLfloat gscale
= value
* acc_scale
/ CHAN_MAXF
;
341 const GLfloat bscale
= value
* acc_scale
/ CHAN_MAXF
;
342 const GLfloat ascale
= value
* acc_scale
/ CHAN_MAXF
;
344 for (j
=0;j
<height
;j
++) {
345 GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ xpos
* 4;
347 _swrast_read_rgba_span(ctx
, ctx
->DrawBuffer
, width
, xpos
, ypos
, rgba
);
348 for (i
=0;i
<width
;i
++) {
349 acc
[0] += (GLaccum
) ( (GLfloat
) rgba
[i
][RCOMP
] * rscale
);
350 acc
[1] += (GLaccum
) ( (GLfloat
) rgba
[i
][GCOMP
] * gscale
);
351 acc
[2] += (GLaccum
) ( (GLfloat
) rgba
[i
][BCOMP
] * bscale
);
352 acc
[3] += (GLaccum
) ( (GLfloat
) rgba
[i
][ACOMP
] * ascale
);
358 /* restore read buffer = draw buffer (the default) */
359 _swrast_use_draw_buffer(ctx
);
361 RENDER_FINISH(swrast
,ctx
);
365 _swrast_use_read_buffer(ctx
);
367 /* This is a change to go into optimized accum buffer mode */
368 if (value
> 0.0 && value
<= 1.0) {
369 #ifdef 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 RENDER_START(swrast
,ctx
);
382 if (swrast
->_IntegerAccumMode
) {
383 /* just copy values into accum buffer */
385 GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ xpos
* 4;
386 assert(swrast
->_IntegerAccumScaler
> 0.0);
387 assert(swrast
->_IntegerAccumScaler
<= 1.0);
388 for (j
= 0; j
< height
; j
++) {
390 _swrast_read_rgba_span(ctx
, ctx
->DrawBuffer
, width
, xpos
, ypos
, rgba
);
391 for (i
= i4
= 0; i
< width
; i
++, i4
+= 4) {
392 acc
[i4
+0] = rgba
[i
][RCOMP
];
393 acc
[i4
+1] = rgba
[i
][GCOMP
];
394 acc
[i4
+2] = rgba
[i
][BCOMP
];
395 acc
[i4
+3] = rgba
[i
][ACOMP
];
402 /* scaled integer (or float) accum buffer */
403 const GLfloat rscale
= value
* acc_scale
/ CHAN_MAXF
;
404 const GLfloat gscale
= value
* acc_scale
/ CHAN_MAXF
;
405 const GLfloat bscale
= value
* acc_scale
/ CHAN_MAXF
;
406 const GLfloat ascale
= value
* acc_scale
/ CHAN_MAXF
;
408 const GLfloat d
= 3.0 / acc_scale
; /* XXX what's this? */
411 for (j
= 0; j
< height
; j
++) {
412 GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ xpos
* 4;
413 _swrast_read_rgba_span(ctx
, ctx
->DrawBuffer
, width
, xpos
, ypos
, rgba
);
414 for (i
=0;i
<width
;i
++) {
416 *acc
++ = (GLaccum
) ((GLfloat
) rgba
[i
][RCOMP
] * rscale
+ d
);
417 *acc
++ = (GLaccum
) ((GLfloat
) rgba
[i
][GCOMP
] * gscale
+ d
);
418 *acc
++ = (GLaccum
) ((GLfloat
) rgba
[i
][BCOMP
] * bscale
+ d
);
419 *acc
++ = (GLaccum
) ((GLfloat
) rgba
[i
][ACOMP
] * ascale
+ d
);
421 *acc
++ = (GLaccum
) ((GLfloat
) rgba
[i
][RCOMP
] * rscale
);
422 *acc
++ = (GLaccum
) ((GLfloat
) rgba
[i
][GCOMP
] * gscale
);
423 *acc
++ = (GLaccum
) ((GLfloat
) rgba
[i
][BCOMP
] * bscale
);
424 *acc
++ = (GLaccum
) ((GLfloat
) rgba
[i
][ACOMP
] * ascale
);
431 /* restore read buffer = draw buffer (the default) */
432 _swrast_use_draw_buffer(ctx
);
434 RENDER_FINISH(swrast
,ctx
);
438 /* May have to leave optimized accum buffer mode */
439 if (swrast
->_IntegerAccumMode
&& value
!= 1.0)
442 RENDER_START(swrast
,ctx
);
443 #ifdef USE_OPTIMIZED_ACCUM
444 if (swrast
->_IntegerAccumMode
&& swrast
->_IntegerAccumScaler
> 0) {
445 /* build lookup table to avoid many floating point multiplies */
446 static GLchan multTable
[32768];
447 static GLfloat prevMult
= 0.0;
448 const GLfloat mult
= swrast
->_IntegerAccumScaler
;
449 const GLint max
= MIN2((GLint
) (256 / mult
), 32767);
451 if (mult
!= prevMult
) {
452 for (j
= 0; j
< max
; j
++)
453 multTable
[j
] = IROUND((GLfloat
) j
* mult
);
457 assert(swrast
->_IntegerAccumScaler
> 0.0);
458 assert(swrast
->_IntegerAccumScaler
<= 1.0);
459 for (j
= 0; j
< height
; j
++) {
460 const GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ xpos
*4;
462 for (i
= i4
= 0; i
< width
; i
++, i4
+= 4) {
463 ASSERT(acc
[i4
+0] < max
);
464 ASSERT(acc
[i4
+1] < max
);
465 ASSERT(acc
[i4
+2] < max
);
466 ASSERT(acc
[i4
+3] < max
);
467 rgba
[i
][RCOMP
] = multTable
[acc
[i4
+0]];
468 rgba
[i
][GCOMP
] = multTable
[acc
[i4
+1]];
469 rgba
[i
][BCOMP
] = multTable
[acc
[i4
+2]];
470 rgba
[i
][ACOMP
] = multTable
[acc
[i4
+3]];
472 if (colorMask
!= 0xffffffff) {
473 _swrast_mask_rgba_array( ctx
, width
, xpos
, ypos
, rgba
);
475 (*swrast
->Driver
.WriteRGBASpan
)( ctx
, width
, xpos
, ypos
,
476 (const GLchan (*)[4])rgba
, NULL
);
477 if (ctx
->DrawBuffer
->UseSoftwareAlphaBuffers
478 && ctx
->Color
.ColorMask
[ACOMP
]) {
479 _swrast_write_alpha_span(ctx
, width
, xpos
, ypos
,
480 (CONST
GLchan (*)[4]) rgba
, NULL
);
486 #endif /* USE_OPTIMIZED_ACCUM */
488 /* scaled integer (or float) accum buffer */
489 const GLfloat rscale
= value
/ acc_scale
* CHAN_MAXF
;
490 const GLfloat gscale
= value
/ acc_scale
* CHAN_MAXF
;
491 const GLfloat bscale
= value
/ acc_scale
* CHAN_MAXF
;
492 const GLfloat ascale
= value
/ acc_scale
* CHAN_MAXF
;
494 for (j
=0;j
<height
;j
++) {
495 const GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ xpos
*4;
496 for (i
=0;i
<width
;i
++) {
497 GLint r
= IROUND( (GLfloat
) (acc
[0]) * rscale
);
498 GLint g
= IROUND( (GLfloat
) (acc
[1]) * gscale
);
499 GLint b
= IROUND( (GLfloat
) (acc
[2]) * bscale
);
500 GLint a
= IROUND( (GLfloat
) (acc
[3]) * ascale
);
502 rgba
[i
][RCOMP
] = CLAMP( r
, 0, CHAN_MAX
);
503 rgba
[i
][GCOMP
] = CLAMP( g
, 0, CHAN_MAX
);
504 rgba
[i
][BCOMP
] = CLAMP( b
, 0, CHAN_MAX
);
505 rgba
[i
][ACOMP
] = CLAMP( a
, 0, CHAN_MAX
);
507 if (colorMask
!= 0xffffffff) {
508 _swrast_mask_rgba_array( ctx
, width
, xpos
, ypos
, rgba
);
510 (*swrast
->Driver
.WriteRGBASpan
)( ctx
, width
, xpos
, ypos
,
511 (const GLchan (*)[4])rgba
, NULL
);
512 if (ctx
->DrawBuffer
->UseSoftwareAlphaBuffers
513 && ctx
->Color
.ColorMask
[ACOMP
]) {
514 _swrast_write_alpha_span(ctx
, width
, xpos
, ypos
,
515 (CONST
GLchan (*)[4]) rgba
, NULL
);
520 RENDER_FINISH(swrast
,ctx
);
524 _mesa_error( ctx
, GL_INVALID_ENUM
, "glAccum" );