1 /* $Id: accum.c,v 1.26 2000/09/26 20:53:53 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2000 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.
43 * Accumulation buffer notes
45 * Normally, accumulation buffer values are GLshorts with values in
46 * [-32767, 32767] which represent floating point colors in [-1, 1],
47 * as suggested by the OpenGL specification.
49 * We optimize for the common case used for full-scene antialiasing:
50 * // start with accum buffer cleared to zero
51 * glAccum(GL_LOAD, w); // or GL_ACCUM the first image
52 * glAccum(GL_ACCUM, w);
54 * glAccum(GL_ACCUM, w);
55 * glAccum(GL_RETURN, 1.0);
56 * That is, we start with an empty accumulation buffer and accumulate
57 * n images, each with weight w = 1/n.
58 * In this scenario, we can simply store unscaled integer values in
59 * the accum buffer instead of scaled integers. We'll also keep track
60 * of the w value so when we do GL_RETURN we simply divide the accumulated
62 * This lets us avoid _many_ int->float->int conversions.
66 #define USE_OPTIMIZED_ACCUM /* enable the optimization */
71 _mesa_alloc_accum_buffer( GLcontext
*ctx
)
75 if (ctx
->DrawBuffer
->Accum
) {
76 FREE( ctx
->DrawBuffer
->Accum
);
77 ctx
->DrawBuffer
->Accum
= NULL
;
80 /* allocate accumulation buffer if not already present */
81 n
= ctx
->DrawBuffer
->Width
* ctx
->DrawBuffer
->Height
* 4 * sizeof(GLaccum
);
82 ctx
->DrawBuffer
->Accum
= (GLaccum
*) MALLOC( n
);
83 if (!ctx
->DrawBuffer
->Accum
) {
84 /* unable to setup accumulation buffer */
85 gl_error( ctx
, GL_OUT_OF_MEMORY
, "glAccum" );
87 #ifdef USE_OPTIMIZED_ACCUM
88 ctx
->IntegerAccumMode
= GL_TRUE
;
90 ctx
->IntegerAccumMode
= GL_FALSE
;
92 ctx
->IntegerAccumScaler
= 0.0;
98 _mesa_ClearAccum( GLfloat red
, GLfloat green
, GLfloat blue
, GLfloat alpha
)
100 GET_CURRENT_CONTEXT(ctx
);
101 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glAccum");
103 ctx
->Accum
.ClearColor
[0] = CLAMP( red
, -1.0, 1.0 );
104 ctx
->Accum
.ClearColor
[1] = CLAMP( green
, -1.0, 1.0 );
105 ctx
->Accum
.ClearColor
[2] = CLAMP( blue
, -1.0, 1.0 );
106 ctx
->Accum
.ClearColor
[3] = CLAMP( alpha
, -1.0, 1.0 );
112 * This is called when we fall out of optimized/unscaled accum buffer mode.
113 * That is, we convert each unscaled accum buffer value into a scaled value
114 * representing the range[-1, 1].
116 static void rescale_accum( GLcontext
*ctx
)
118 const GLuint n
= ctx
->DrawBuffer
->Width
* ctx
->DrawBuffer
->Height
* 4;
119 const GLfloat fChanMax
= (1 << (sizeof(GLchan
) * 8)) - 1;
120 const GLfloat s
= ctx
->IntegerAccumScaler
* (32767.0 / fChanMax
);
121 GLaccum
*accum
= ctx
->DrawBuffer
->Accum
;
124 assert(ctx
->IntegerAccumMode
);
127 for (i
= 0; i
< n
; i
++) {
128 accum
[i
] = (GLaccum
) (accum
[i
] * s
);
131 ctx
->IntegerAccumMode
= GL_FALSE
;
137 _mesa_Accum( GLenum op
, GLfloat value
)
139 GET_CURRENT_CONTEXT(ctx
);
140 GLuint xpos
, ypos
, width
, height
, width4
;
142 GLubyte rgba
[MAX_WIDTH
][4];
143 const GLuint colorMask
= *((GLuint
*) &ctx
->Color
.ColorMask
);
144 const GLint iChanMax
= (1 << (sizeof(GLchan
) * 8)) - 1;
145 const GLfloat fChanMax
= (1 << (sizeof(GLchan
) * 8)) - 1;
147 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glAccum");
149 if (ctx
->Visual
.AccumRedBits
== 0 || ctx
->DrawBuffer
!= ctx
->ReadBuffer
) {
150 gl_error(ctx
, GL_INVALID_OPERATION
, "glAccum");
154 if (!ctx
->DrawBuffer
->Accum
) {
155 _mesa_warning(ctx
, "Calling glAccum() without an accumulation buffer (low memory?)");
159 if (sizeof(GLaccum
)==1) {
162 else if (sizeof(GLaccum
)==2) {
166 /* sizeof(GLaccum) > 2 (Cray) */
167 acc_scale
= (float) SHRT_MAX
;
171 gl_update_state( ctx
);
173 /* Determine region to operate upon. */
174 if (ctx
->Scissor
.Enabled
) {
175 xpos
= ctx
->Scissor
.X
;
176 ypos
= ctx
->Scissor
.Y
;
177 width
= ctx
->Scissor
.Width
;
178 height
= ctx
->Scissor
.Height
;
184 width
= ctx
->DrawBuffer
->Width
;
185 height
= ctx
->DrawBuffer
->Height
;
193 const GLaccum intVal
= (GLaccum
) (value
* acc_scale
);
195 /* Leave optimized accum buffer mode */
196 if (ctx
->IntegerAccumMode
)
198 for (j
= 0; j
< height
; j
++) {
199 GLaccum
* acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ 4 * xpos
;
201 for (i
= 0; i
< width4
; i
++) {
212 /* Leave optimized accum buffer mode */
213 if (ctx
->IntegerAccumMode
)
215 for (j
= 0; j
< height
; j
++) {
216 GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ 4 * xpos
;
218 for (i
= 0; i
< width4
; i
++) {
219 acc
[i
] = (GLaccum
) ( (GLfloat
) acc
[i
] * value
);
230 (*ctx
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
231 ctx
->Pixel
.DriverReadBuffer
);
233 /* May have to leave optimized accum buffer mode */
234 if (ctx
->IntegerAccumScaler
== 0.0 && value
> 0.0 && value
<= 1.0)
235 ctx
->IntegerAccumScaler
= value
;
236 if (ctx
->IntegerAccumMode
&& value
!= ctx
->IntegerAccumScaler
)
239 if (ctx
->IntegerAccumMode
) {
240 /* simply add integer color values into accum buffer */
242 GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ xpos
* 4;
243 assert(ctx
->IntegerAccumScaler
> 0.0);
244 assert(ctx
->IntegerAccumScaler
<= 1.0);
245 for (j
= 0; j
< height
; j
++) {
248 gl_read_rgba_span(ctx
, ctx
->DrawBuffer
, width
, xpos
, ypos
, rgba
);
249 for (i
= i4
= 0; i
< width
; i
++, i4
+=4) {
250 acc
[i4
+0] += rgba
[i
][RCOMP
];
251 acc
[i4
+1] += rgba
[i
][GCOMP
];
252 acc
[i4
+2] += rgba
[i
][BCOMP
];
253 acc
[i4
+3] += rgba
[i
][ACOMP
];
260 /* scaled integer accum buffer */
261 const GLfloat rscale
= value
* acc_scale
/ fChanMax
;
262 const GLfloat gscale
= value
* acc_scale
/ fChanMax
;
263 const GLfloat bscale
= value
* acc_scale
/ fChanMax
;
264 const GLfloat ascale
= value
* acc_scale
/ fChanMax
;
266 for (j
=0;j
<height
;j
++) {
267 GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ xpos
* 4;
269 gl_read_rgba_span(ctx
, ctx
->DrawBuffer
, width
, xpos
, ypos
, rgba
);
270 for (i
=0;i
<width
;i
++) {
271 *acc
+= (GLaccum
) ( (GLfloat
) rgba
[i
][RCOMP
] * rscale
); acc
++;
272 *acc
+= (GLaccum
) ( (GLfloat
) rgba
[i
][GCOMP
] * gscale
); acc
++;
273 *acc
+= (GLaccum
) ( (GLfloat
) rgba
[i
][BCOMP
] * bscale
); acc
++;
274 *acc
+= (GLaccum
) ( (GLfloat
) rgba
[i
][ACOMP
] * ascale
); acc
++;
279 /* restore read buffer = draw buffer (the default) */
280 (*ctx
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
281 ctx
->Color
.DriverDrawBuffer
);
285 (*ctx
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
286 ctx
->Pixel
.DriverReadBuffer
);
288 /* This is a change to go into optimized accum buffer mode */
289 if (value
> 0.0 && value
<= 1.0) {
290 #ifdef USE_OPTIMIZED_ACCUM
291 ctx
->IntegerAccumMode
= GL_TRUE
;
293 ctx
->IntegerAccumMode
= GL_FALSE
;
295 ctx
->IntegerAccumScaler
= value
;
298 ctx
->IntegerAccumMode
= GL_FALSE
;
299 ctx
->IntegerAccumScaler
= 0.0;
302 if (ctx
->IntegerAccumMode
) {
303 /* just copy values into accum buffer */
305 GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ xpos
* 4;
306 assert(ctx
->IntegerAccumScaler
> 0.0);
307 assert(ctx
->IntegerAccumScaler
<= 1.0);
308 for (j
= 0; j
< height
; j
++) {
310 gl_read_rgba_span(ctx
, ctx
->DrawBuffer
, width
, xpos
, ypos
, rgba
);
311 for (i
= i4
= 0; i
< width
; i
++, i4
+= 4) {
312 acc
[i4
+0] = rgba
[i
][RCOMP
];
313 acc
[i4
+1] = rgba
[i
][GCOMP
];
314 acc
[i4
+2] = rgba
[i
][BCOMP
];
315 acc
[i4
+3] = rgba
[i
][ACOMP
];
322 /* scaled integer accum buffer */
323 const GLfloat rscale
= value
* acc_scale
/ fChanMax
;
324 const GLfloat gscale
= value
* acc_scale
/ fChanMax
;
325 const GLfloat bscale
= value
* acc_scale
/ fChanMax
;
326 const GLfloat ascale
= value
* acc_scale
/ fChanMax
;
327 const GLfloat d
= 3.0 / acc_scale
;
329 for (j
= 0; j
< height
; j
++) {
330 GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ xpos
* 4;
331 gl_read_rgba_span(ctx
, ctx
->DrawBuffer
, width
, xpos
, ypos
, rgba
);
332 for (i
=0;i
<width
;i
++) {
333 *acc
++ = (GLaccum
) ((GLfloat
) rgba
[i
][RCOMP
] * rscale
+ d
);
334 *acc
++ = (GLaccum
) ((GLfloat
) rgba
[i
][GCOMP
] * gscale
+ d
);
335 *acc
++ = (GLaccum
) ((GLfloat
) rgba
[i
][BCOMP
] * bscale
+ d
);
336 *acc
++ = (GLaccum
) ((GLfloat
) rgba
[i
][ACOMP
] * ascale
+ d
);
342 /* restore read buffer = draw buffer (the default) */
343 (*ctx
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
344 ctx
->Color
.DriverDrawBuffer
);
348 /* May have to leave optimized accum buffer mode */
349 if (ctx
->IntegerAccumMode
&& value
!= 1.0)
352 if (ctx
->IntegerAccumMode
&& ctx
->IntegerAccumScaler
> 0) {
353 /* build lookup table to avoid many floating point multiplies */
354 static GLchan multTable
[32768];
355 static GLfloat prevMult
= 0.0;
356 const GLfloat mult
= ctx
->IntegerAccumScaler
;
357 const GLint max
= MIN2((GLint
) (256 / mult
), 32767);
359 if (mult
!= prevMult
) {
360 for (j
= 0; j
< max
; j
++)
361 multTable
[j
] = (GLint
) ((GLfloat
) j
* mult
+ 0.5F
);
365 assert(ctx
->IntegerAccumScaler
> 0.0);
366 assert(ctx
->IntegerAccumScaler
<= 1.0);
367 for (j
= 0; j
< height
; j
++) {
368 const GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ xpos
*4;
370 for (i
= i4
= 0; i
< width
; i
++, i4
+= 4) {
371 ASSERT(acc
[i4
+0] < max
);
372 ASSERT(acc
[i4
+1] < max
);
373 ASSERT(acc
[i4
+2] < max
);
374 ASSERT(acc
[i4
+3] < max
);
375 rgba
[i
][RCOMP
] = multTable
[acc
[i4
+0]];
376 rgba
[i
][GCOMP
] = multTable
[acc
[i4
+1]];
377 rgba
[i
][BCOMP
] = multTable
[acc
[i4
+2]];
378 rgba
[i
][ACOMP
] = multTable
[acc
[i4
+3]];
380 if (colorMask
!= 0xffffffff) {
381 _mesa_mask_rgba_span( ctx
, width
, xpos
, ypos
, rgba
);
383 (*ctx
->Driver
.WriteRGBASpan
)( ctx
, width
, xpos
, ypos
,
384 (const GLubyte (*)[4])rgba
, NULL
);
389 const GLfloat rscale
= value
/ acc_scale
* fChanMax
;
390 const GLfloat gscale
= value
/ acc_scale
* fChanMax
;
391 const GLfloat bscale
= value
/ acc_scale
* fChanMax
;
392 const GLfloat ascale
= value
/ acc_scale
* fChanMax
;
394 for (j
=0;j
<height
;j
++) {
395 const GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ xpos
*4;
396 for (i
=0;i
<width
;i
++) {
398 r
= (GLint
) ( (GLfloat
) (*acc
++) * rscale
+ 0.5F
);
399 g
= (GLint
) ( (GLfloat
) (*acc
++) * gscale
+ 0.5F
);
400 b
= (GLint
) ( (GLfloat
) (*acc
++) * bscale
+ 0.5F
);
401 a
= (GLint
) ( (GLfloat
) (*acc
++) * ascale
+ 0.5F
);
402 rgba
[i
][RCOMP
] = CLAMP( r
, 0, iChanMax
);
403 rgba
[i
][GCOMP
] = CLAMP( g
, 0, iChanMax
);
404 rgba
[i
][BCOMP
] = CLAMP( b
, 0, iChanMax
);
405 rgba
[i
][ACOMP
] = CLAMP( a
, 0, iChanMax
);
407 if (colorMask
!= 0xffffffff) {
408 _mesa_mask_rgba_span( ctx
, width
, xpos
, ypos
, rgba
);
410 (*ctx
->Driver
.WriteRGBASpan
)( ctx
, width
, xpos
, ypos
,
411 (const GLubyte (*)[4])rgba
, NULL
);
418 gl_error( ctx
, GL_INVALID_ENUM
, "glAccum" );
425 * Clear the accumulation Buffer.
428 _mesa_clear_accum_buffer( GLcontext
*ctx
)
433 if (ctx
->Visual
.AccumRedBits
==0) {
434 /* No accumulation buffer! */
438 if (sizeof(GLaccum
)==1) {
441 else if (sizeof(GLaccum
)==2) {
445 /* sizeof(GLaccum) > 2 (Cray) */
446 acc_scale
= (float) SHRT_MAX
;
449 /* number of pixels */
450 buffersize
= ctx
->DrawBuffer
->Width
* ctx
->DrawBuffer
->Height
;
452 if (!ctx
->DrawBuffer
->Accum
) {
453 /* try to alloc accumulation buffer */
454 ctx
->DrawBuffer
->Accum
= (GLaccum
*)
455 MALLOC( buffersize
* 4 * sizeof(GLaccum
) );
458 if (ctx
->DrawBuffer
->Accum
) {
459 if (ctx
->Scissor
.Enabled
) {
460 /* Limit clear to scissor box */
465 r
= (GLaccum
) (ctx
->Accum
.ClearColor
[0] * acc_scale
);
466 g
= (GLaccum
) (ctx
->Accum
.ClearColor
[1] * acc_scale
);
467 b
= (GLaccum
) (ctx
->Accum
.ClearColor
[2] * acc_scale
);
468 a
= (GLaccum
) (ctx
->Accum
.ClearColor
[3] * acc_scale
);
469 /* size of region to clear */
470 width
= 4 * (ctx
->DrawBuffer
->Xmax
- ctx
->DrawBuffer
->Xmin
);
471 height
= ctx
->DrawBuffer
->Ymax
- ctx
->DrawBuffer
->Ymin
;
472 /* ptr to first element to clear */
473 row
= ctx
->DrawBuffer
->Accum
474 + 4 * (ctx
->DrawBuffer
->Ymin
* ctx
->DrawBuffer
->Width
475 + ctx
->DrawBuffer
->Xmin
);
476 for (j
=0;j
<height
;j
++) {
477 for (i
=0;i
<width
;i
+=4) {
483 row
+= 4 * ctx
->DrawBuffer
->Width
;
487 /* clear whole buffer */
488 if (ctx
->Accum
.ClearColor
[0]==0.0 &&
489 ctx
->Accum
.ClearColor
[1]==0.0 &&
490 ctx
->Accum
.ClearColor
[2]==0.0 &&
491 ctx
->Accum
.ClearColor
[3]==0.0) {
493 BZERO( ctx
->DrawBuffer
->Accum
, buffersize
* 4 * sizeof(GLaccum
) );
497 GLaccum
*acc
, r
, g
, b
, a
;
500 acc
= ctx
->DrawBuffer
->Accum
;
501 r
= (GLaccum
) (ctx
->Accum
.ClearColor
[0] * acc_scale
);
502 g
= (GLaccum
) (ctx
->Accum
.ClearColor
[1] * acc_scale
);
503 b
= (GLaccum
) (ctx
->Accum
.ClearColor
[2] * acc_scale
);
504 a
= (GLaccum
) (ctx
->Accum
.ClearColor
[3] * acc_scale
);
505 for (i
=0;i
<buffersize
;i
++) {
514 /* update optimized accum state vars */
515 if (ctx
->Accum
.ClearColor
[0] == 0.0 && ctx
->Accum
.ClearColor
[1] == 0.0 &&
516 ctx
->Accum
.ClearColor
[2] == 0.0 && ctx
->Accum
.ClearColor
[3] == 0.0) {
517 #ifdef USE_OPTIMIZED_ACCUM
518 ctx
->IntegerAccumMode
= GL_TRUE
;
520 ctx
->IntegerAccumMode
= GL_FALSE
;
522 ctx
->IntegerAccumScaler
= 0.0; /* denotes empty accum buffer */
525 ctx
->IntegerAccumMode
= GL_FALSE
;