1 /* $Id: accum.c,v 1.13 1999/11/25 17:36:48 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999 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.
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.
65 #define USE_OPTIMIZED_ACCUM /* enable the optimization */
69 void gl_alloc_accum_buffer( GLcontext
*ctx
)
73 if (ctx
->DrawBuffer
->Accum
) {
74 FREE( ctx
->DrawBuffer
->Accum
);
75 ctx
->DrawBuffer
->Accum
= NULL
;
78 /* allocate accumulation buffer if not already present */
79 n
= ctx
->DrawBuffer
->Width
* ctx
->DrawBuffer
->Height
* 4 * sizeof(GLaccum
);
80 ctx
->DrawBuffer
->Accum
= (GLaccum
*) MALLOC( n
);
81 if (!ctx
->DrawBuffer
->Accum
) {
82 /* unable to setup accumulation buffer */
83 gl_error( ctx
, GL_OUT_OF_MEMORY
, "glAccum" );
85 #ifdef USE_OPTIMIZED_ACCUM
86 ctx
->IntegerAccumMode
= GL_TRUE
;
88 ctx
->IntegerAccumMode
= GL_FALSE
;
90 ctx
->IntegerAccumScaler
= 0.0;
96 _mesa_ClearAccum( GLfloat red
, GLfloat green
, GLfloat blue
, GLfloat alpha
)
98 GET_CURRENT_CONTEXT(ctx
);
99 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glAccum");
101 ctx
->Accum
.ClearColor
[0] = CLAMP( red
, -1.0, 1.0 );
102 ctx
->Accum
.ClearColor
[1] = CLAMP( green
, -1.0, 1.0 );
103 ctx
->Accum
.ClearColor
[2] = CLAMP( blue
, -1.0, 1.0 );
104 ctx
->Accum
.ClearColor
[3] = CLAMP( alpha
, -1.0, 1.0 );
110 * This is called when we fall out of optimized/unscaled accum buffer mode.
111 * That is, we convert each unscaled accum buffer value into a scaled value
112 * representing the range[-1, 1].
114 static void rescale_accum( GLcontext
*ctx
)
116 const GLuint n
= ctx
->DrawBuffer
->Width
* ctx
->DrawBuffer
->Height
* 4;
117 const GLfloat fChanMax
= (1 << (sizeof(GLchan
) * 8)) - 1;
118 const GLfloat s
= ctx
->IntegerAccumScaler
* (32767.0 / fChanMax
);
119 GLaccum
*accum
= ctx
->DrawBuffer
->Accum
;
122 assert(ctx
->IntegerAccumMode
);
125 for (i
= 0; i
< n
; i
++) {
126 accum
[i
] = (GLaccum
) (accum
[i
] * s
);
129 ctx
->IntegerAccumMode
= GL_FALSE
;
135 _mesa_Accum( GLenum op
, GLfloat value
)
137 GET_CURRENT_CONTEXT(ctx
);
138 GLuint xpos
, ypos
, width
, height
, width4
;
140 GLubyte rgba
[MAX_WIDTH
][4];
141 const GLint iChanMax
= (1 << (sizeof(GLchan
) * 8)) - 1;
142 const GLfloat fChanMax
= (1 << (sizeof(GLchan
) * 8)) - 1;
144 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glAccum");
146 if (ctx
->Visual
->AccumBits
== 0 || ctx
->DrawBuffer
!= ctx
->ReadBuffer
) {
147 gl_error(ctx
, GL_INVALID_OPERATION
, "glAccum");
151 if (!ctx
->DrawBuffer
->Accum
) {
152 gl_warning(ctx
, "Calling glAccum() without an accumulation buffer (low memory?)");
156 if (sizeof(GLaccum
)==1) {
159 else if (sizeof(GLaccum
)==2) {
163 /* sizeof(GLaccum) > 2 (Cray) */
164 acc_scale
= (float) SHRT_MAX
;
168 gl_update_state( ctx
);
170 /* Determine region to operate upon. */
171 if (ctx
->Scissor
.Enabled
) {
172 xpos
= ctx
->Scissor
.X
;
173 ypos
= ctx
->Scissor
.Y
;
174 width
= ctx
->Scissor
.Width
;
175 height
= ctx
->Scissor
.Height
;
181 width
= ctx
->DrawBuffer
->Width
;
182 height
= ctx
->DrawBuffer
->Height
;
190 const GLaccum intVal
= (GLaccum
) (value
* acc_scale
);
192 /* Leave optimized accum buffer mode */
193 if (ctx
->IntegerAccumMode
)
195 for (j
= 0; j
< height
; j
++) {
196 GLaccum
* acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ 4 * xpos
;
198 for (i
= 0; i
< width4
; i
++) {
209 /* Leave optimized accum buffer mode */
210 if (ctx
->IntegerAccumMode
)
212 for (j
= 0; j
< height
; j
++) {
213 GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ 4 * xpos
;
215 for (i
= 0; i
< width4
; i
++) {
216 acc
[i
] = (GLaccum
) ( (GLfloat
) acc
[i
] * value
);
224 (*ctx
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
225 ctx
->Pixel
.DriverReadBuffer
);
227 /* May have to leave optimized accum buffer mode */
228 if (ctx
->IntegerAccumScaler
== 0.0 && value
> 0.0 && value
<= 1.0)
229 ctx
->IntegerAccumScaler
= value
;
230 if (ctx
->IntegerAccumMode
&& value
!= ctx
->IntegerAccumScaler
)
233 if (ctx
->IntegerAccumMode
) {
234 /* simply add integer color values into accum buffer */
236 GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ xpos
* 4;
237 assert(ctx
->IntegerAccumScaler
> 0.0);
238 assert(ctx
->IntegerAccumScaler
<= 1.0);
239 for (j
= 0; j
< height
; j
++) {
242 gl_read_rgba_span(ctx
, ctx
->DrawBuffer
, width
, xpos
, ypos
, rgba
);
243 for (i
= i4
= 0; i
< width
; i
++, i4
+=4) {
244 acc
[i4
+0] += rgba
[i
][RCOMP
];
245 acc
[i4
+1] += rgba
[i
][GCOMP
];
246 acc
[i4
+2] += rgba
[i
][BCOMP
];
247 acc
[i4
+3] += rgba
[i
][ACOMP
];
254 /* scaled integer accum buffer */
255 const GLfloat rscale
= value
* acc_scale
/ fChanMax
;
256 const GLfloat gscale
= value
* acc_scale
/ fChanMax
;
257 const GLfloat bscale
= value
* acc_scale
/ fChanMax
;
258 const GLfloat ascale
= value
* acc_scale
/ fChanMax
;
260 for (j
=0;j
<height
;j
++) {
261 GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ xpos
* 4;
263 gl_read_rgba_span(ctx
, ctx
->DrawBuffer
, width
, xpos
, ypos
, rgba
);
264 for (i
=0;i
<width
;i
++) {
265 *acc
+= (GLaccum
) ( (GLfloat
) rgba
[i
][RCOMP
] * rscale
); acc
++;
266 *acc
+= (GLaccum
) ( (GLfloat
) rgba
[i
][GCOMP
] * gscale
); acc
++;
267 *acc
+= (GLaccum
) ( (GLfloat
) rgba
[i
][BCOMP
] * bscale
); acc
++;
268 *acc
+= (GLaccum
) ( (GLfloat
) rgba
[i
][ACOMP
] * ascale
); acc
++;
273 /* restore read buffer = draw buffer (the default) */
274 (*ctx
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
275 ctx
->Color
.DriverDrawBuffer
);
279 (*ctx
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
280 ctx
->Pixel
.DriverReadBuffer
);
282 /* This is a change to go into optimized accum buffer mode */
283 if (value
> 0.0 && value
<= 1.0) {
284 #ifdef USE_OPTIMIZED_ACCUM
285 ctx
->IntegerAccumMode
= GL_TRUE
;
287 ctx
->IntegerAccumMode
= GL_FALSE
;
289 ctx
->IntegerAccumScaler
= value
;
292 ctx
->IntegerAccumMode
= GL_FALSE
;
293 ctx
->IntegerAccumScaler
= 0.0;
296 if (ctx
->IntegerAccumMode
) {
297 /* just copy values into accum buffer */
299 GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ xpos
* 4;
300 assert(ctx
->IntegerAccumScaler
> 0.0);
301 assert(ctx
->IntegerAccumScaler
<= 1.0);
302 for (j
= 0; j
< height
; j
++) {
304 gl_read_rgba_span(ctx
, ctx
->DrawBuffer
, width
, xpos
, ypos
, rgba
);
305 for (i
= i4
= 0; i
< width
; i
++, i4
+= 4) {
306 acc
[i4
+0] = rgba
[i
][RCOMP
];
307 acc
[i4
+1] = rgba
[i
][GCOMP
];
308 acc
[i4
+2] = rgba
[i
][BCOMP
];
309 acc
[i4
+3] = rgba
[i
][ACOMP
];
316 /* scaled integer accum buffer */
317 const GLfloat rscale
= value
* acc_scale
/ fChanMax
;
318 const GLfloat gscale
= value
* acc_scale
/ fChanMax
;
319 const GLfloat bscale
= value
* acc_scale
/ fChanMax
;
320 const GLfloat ascale
= value
* acc_scale
/ fChanMax
;
321 const GLfloat d
= 3.0 / acc_scale
;
323 for (j
= 0; j
< height
; j
++) {
324 GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ xpos
* 4;
325 gl_read_rgba_span(ctx
, ctx
->DrawBuffer
, width
, xpos
, ypos
, rgba
);
326 for (i
=0;i
<width
;i
++) {
327 *acc
++ = (GLaccum
) ((GLfloat
) rgba
[i
][RCOMP
] * rscale
+ d
);
328 *acc
++ = (GLaccum
) ((GLfloat
) rgba
[i
][GCOMP
] * gscale
+ d
);
329 *acc
++ = (GLaccum
) ((GLfloat
) rgba
[i
][BCOMP
] * bscale
+ d
);
330 *acc
++ = (GLaccum
) ((GLfloat
) rgba
[i
][ACOMP
] * ascale
+ d
);
336 /* restore read buffer = draw buffer (the default) */
337 (*ctx
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
338 ctx
->Color
.DriverDrawBuffer
);
342 /* May have to leave optimized accum buffer mode */
343 if (ctx
->IntegerAccumMode
&& value
!= 1.0)
346 if (ctx
->IntegerAccumMode
) {
347 /* build lookup table to avoid many floating point multiplies */
348 const GLfloat mult
= ctx
->IntegerAccumScaler
;
349 static GLchan multTable
[32768];
350 static GLfloat prevMult
= 0.0;
352 const GLint max
= 256 / mult
;
353 if (mult
!= prevMult
) {
354 assert(max
<= 32768);
355 for (j
= 0; j
< max
; j
++)
356 multTable
[j
] = (GLint
) ((GLfloat
) j
* mult
+ 0.5F
);
360 assert(ctx
->IntegerAccumScaler
> 0.0);
361 assert(ctx
->IntegerAccumScaler
<= 1.0);
362 for (j
= 0; j
< height
; j
++) {
363 const GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ xpos
*4;
365 for (i
= i4
= 0; i
< width
; i
++, i4
+= 4) {
366 ASSERT(acc
[i4
+0] < max
);
367 ASSERT(acc
[i4
+1] < max
);
368 ASSERT(acc
[i4
+2] < max
);
369 ASSERT(acc
[i4
+3] < max
);
370 rgba
[i
][RCOMP
] = multTable
[acc
[i4
+0]];
371 rgba
[i
][GCOMP
] = multTable
[acc
[i4
+1]];
372 rgba
[i
][BCOMP
] = multTable
[acc
[i4
+2]];
373 rgba
[i
][ACOMP
] = multTable
[acc
[i4
+3]];
375 if (ctx
->Color
.SWmasking
) {
376 gl_mask_rgba_span( ctx
, width
, xpos
, ypos
, rgba
);
378 (*ctx
->Driver
.WriteRGBASpan
)( ctx
, width
, xpos
, ypos
,
379 (const GLubyte (*)[4])rgba
, NULL
);
384 const GLfloat rscale
= value
/ acc_scale
* fChanMax
;
385 const GLfloat gscale
= value
/ acc_scale
* fChanMax
;
386 const GLfloat bscale
= value
/ acc_scale
* fChanMax
;
387 const GLfloat ascale
= value
/ acc_scale
* fChanMax
;
389 for (j
=0;j
<height
;j
++) {
390 const GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ xpos
*4;
391 for (i
=0;i
<width
;i
++) {
393 r
= (GLint
) ( (GLfloat
) (*acc
++) * rscale
+ 0.5F
);
394 g
= (GLint
) ( (GLfloat
) (*acc
++) * gscale
+ 0.5F
);
395 b
= (GLint
) ( (GLfloat
) (*acc
++) * bscale
+ 0.5F
);
396 a
= (GLint
) ( (GLfloat
) (*acc
++) * ascale
+ 0.5F
);
397 rgba
[i
][RCOMP
] = CLAMP( r
, 0, iChanMax
);
398 rgba
[i
][GCOMP
] = CLAMP( g
, 0, iChanMax
);
399 rgba
[i
][BCOMP
] = CLAMP( b
, 0, iChanMax
);
400 rgba
[i
][ACOMP
] = CLAMP( a
, 0, iChanMax
);
402 if (ctx
->Color
.SWmasking
) {
403 gl_mask_rgba_span( ctx
, width
, xpos
, ypos
, rgba
);
405 (*ctx
->Driver
.WriteRGBASpan
)( ctx
, width
, xpos
, ypos
,
406 (const GLubyte (*)[4])rgba
, NULL
);
413 gl_error( ctx
, GL_INVALID_ENUM
, "glAccum" );
420 * Clear the accumulation Buffer.
422 void gl_clear_accum_buffer( GLcontext
*ctx
)
427 if (ctx
->Visual
->AccumBits
==0) {
428 /* No accumulation buffer! */
432 if (sizeof(GLaccum
)==1) {
435 else if (sizeof(GLaccum
)==2) {
439 /* sizeof(GLaccum) > 2 (Cray) */
440 acc_scale
= (float) SHRT_MAX
;
443 /* number of pixels */
444 buffersize
= ctx
->DrawBuffer
->Width
* ctx
->DrawBuffer
->Height
;
446 if (!ctx
->DrawBuffer
->Accum
) {
447 /* try to alloc accumulation buffer */
448 ctx
->DrawBuffer
->Accum
= (GLaccum
*)
449 MALLOC( buffersize
* 4 * sizeof(GLaccum
) );
452 if (ctx
->DrawBuffer
->Accum
) {
453 if (ctx
->Scissor
.Enabled
) {
454 /* Limit clear to scissor box */
459 r
= (GLaccum
) (ctx
->Accum
.ClearColor
[0] * acc_scale
);
460 g
= (GLaccum
) (ctx
->Accum
.ClearColor
[1] * acc_scale
);
461 b
= (GLaccum
) (ctx
->Accum
.ClearColor
[2] * acc_scale
);
462 a
= (GLaccum
) (ctx
->Accum
.ClearColor
[3] * acc_scale
);
463 /* size of region to clear */
464 width
= 4 * (ctx
->DrawBuffer
->Xmax
- ctx
->DrawBuffer
->Xmin
+ 1);
465 height
= ctx
->DrawBuffer
->Ymax
- ctx
->DrawBuffer
->Ymin
+ 1;
466 /* ptr to first element to clear */
467 row
= ctx
->DrawBuffer
->Accum
468 + 4 * (ctx
->DrawBuffer
->Ymin
* ctx
->DrawBuffer
->Width
469 + ctx
->DrawBuffer
->Xmin
);
470 for (j
=0;j
<height
;j
++) {
471 for (i
=0;i
<width
;i
+=4) {
477 row
+= 4 * ctx
->DrawBuffer
->Width
;
481 /* clear whole buffer */
482 if (ctx
->Accum
.ClearColor
[0]==0.0 &&
483 ctx
->Accum
.ClearColor
[1]==0.0 &&
484 ctx
->Accum
.ClearColor
[2]==0.0 &&
485 ctx
->Accum
.ClearColor
[3]==0.0) {
487 MEMSET( ctx
->DrawBuffer
->Accum
, 0, buffersize
* 4 * sizeof(GLaccum
) );
491 GLaccum
*acc
, r
, g
, b
, a
;
494 acc
= ctx
->DrawBuffer
->Accum
;
495 r
= (GLaccum
) (ctx
->Accum
.ClearColor
[0] * acc_scale
);
496 g
= (GLaccum
) (ctx
->Accum
.ClearColor
[1] * acc_scale
);
497 b
= (GLaccum
) (ctx
->Accum
.ClearColor
[2] * acc_scale
);
498 a
= (GLaccum
) (ctx
->Accum
.ClearColor
[3] * acc_scale
);
499 for (i
=0;i
<buffersize
;i
++) {
508 /* update optimized accum state vars */
509 if (ctx
->Accum
.ClearColor
[0] == 0.0 && ctx
->Accum
.ClearColor
[1] == 0.0 &&
510 ctx
->Accum
.ClearColor
[2] == 0.0 && ctx
->Accum
.ClearColor
[3] == 0.0) {
511 #ifdef USE_OPTIMIZED_ACCUM
512 ctx
->IntegerAccumMode
= GL_TRUE
;
514 ctx
->IntegerAccumMode
= GL_FALSE
;
516 ctx
->IntegerAccumScaler
= 0.0; /* denotes empty accum buffer */
519 ctx
->IntegerAccumMode
= GL_FALSE
;