1 /* $Id: accum.c,v 1.20 2000/04/05 14:40:04 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 GLint iChanMax
= (1 << (sizeof(GLchan
) * 8)) - 1;
144 const GLfloat fChanMax
= (1 << (sizeof(GLchan
) * 8)) - 1;
146 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glAccum");
148 if (ctx
->Visual
->AccumRedBits
== 0 || ctx
->DrawBuffer
!= ctx
->ReadBuffer
) {
149 gl_error(ctx
, GL_INVALID_OPERATION
, "glAccum");
153 if (!ctx
->DrawBuffer
->Accum
) {
154 gl_warning(ctx
, "Calling glAccum() without an accumulation buffer (low memory?)");
158 if (sizeof(GLaccum
)==1) {
161 else if (sizeof(GLaccum
)==2) {
165 /* sizeof(GLaccum) > 2 (Cray) */
166 acc_scale
= (float) SHRT_MAX
;
170 gl_update_state( ctx
);
172 /* Determine region to operate upon. */
173 if (ctx
->Scissor
.Enabled
) {
174 xpos
= ctx
->Scissor
.X
;
175 ypos
= ctx
->Scissor
.Y
;
176 width
= ctx
->Scissor
.Width
;
177 height
= ctx
->Scissor
.Height
;
183 width
= ctx
->DrawBuffer
->Width
;
184 height
= ctx
->DrawBuffer
->Height
;
192 const GLaccum intVal
= (GLaccum
) (value
* acc_scale
);
194 /* Leave optimized accum buffer mode */
195 if (ctx
->IntegerAccumMode
)
197 for (j
= 0; j
< height
; j
++) {
198 GLaccum
* acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ 4 * xpos
;
200 for (i
= 0; i
< width4
; i
++) {
211 /* Leave optimized accum buffer mode */
212 if (ctx
->IntegerAccumMode
)
214 for (j
= 0; j
< height
; j
++) {
215 GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ 4 * xpos
;
217 for (i
= 0; i
< width4
; i
++) {
218 acc
[i
] = (GLaccum
) ( (GLfloat
) acc
[i
] * value
);
226 (*ctx
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
227 ctx
->Pixel
.DriverReadBuffer
);
229 /* May have to leave optimized accum buffer mode */
230 if (ctx
->IntegerAccumScaler
== 0.0 && value
> 0.0 && value
<= 1.0)
231 ctx
->IntegerAccumScaler
= value
;
232 if (ctx
->IntegerAccumMode
&& value
!= ctx
->IntegerAccumScaler
)
235 if (ctx
->IntegerAccumMode
) {
236 /* simply add integer color values into accum buffer */
238 GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ xpos
* 4;
239 assert(ctx
->IntegerAccumScaler
> 0.0);
240 assert(ctx
->IntegerAccumScaler
<= 1.0);
241 for (j
= 0; j
< height
; j
++) {
244 gl_read_rgba_span(ctx
, ctx
->DrawBuffer
, width
, xpos
, ypos
, rgba
);
245 for (i
= i4
= 0; i
< width
; i
++, i4
+=4) {
246 acc
[i4
+0] += rgba
[i
][RCOMP
];
247 acc
[i4
+1] += rgba
[i
][GCOMP
];
248 acc
[i4
+2] += rgba
[i
][BCOMP
];
249 acc
[i4
+3] += rgba
[i
][ACOMP
];
256 /* scaled integer accum buffer */
257 const GLfloat rscale
= value
* acc_scale
/ fChanMax
;
258 const GLfloat gscale
= value
* acc_scale
/ fChanMax
;
259 const GLfloat bscale
= value
* acc_scale
/ fChanMax
;
260 const GLfloat ascale
= value
* acc_scale
/ fChanMax
;
262 for (j
=0;j
<height
;j
++) {
263 GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ xpos
* 4;
265 gl_read_rgba_span(ctx
, ctx
->DrawBuffer
, width
, xpos
, ypos
, rgba
);
266 for (i
=0;i
<width
;i
++) {
267 *acc
+= (GLaccum
) ( (GLfloat
) rgba
[i
][RCOMP
] * rscale
); acc
++;
268 *acc
+= (GLaccum
) ( (GLfloat
) rgba
[i
][GCOMP
] * gscale
); acc
++;
269 *acc
+= (GLaccum
) ( (GLfloat
) rgba
[i
][BCOMP
] * bscale
); acc
++;
270 *acc
+= (GLaccum
) ( (GLfloat
) rgba
[i
][ACOMP
] * ascale
); acc
++;
275 /* restore read buffer = draw buffer (the default) */
276 (*ctx
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
277 ctx
->Color
.DriverDrawBuffer
);
281 (*ctx
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
282 ctx
->Pixel
.DriverReadBuffer
);
284 /* This is a change to go into optimized accum buffer mode */
285 if (value
> 0.0 && value
<= 1.0) {
286 #ifdef USE_OPTIMIZED_ACCUM
287 ctx
->IntegerAccumMode
= GL_TRUE
;
289 ctx
->IntegerAccumMode
= GL_FALSE
;
291 ctx
->IntegerAccumScaler
= value
;
294 ctx
->IntegerAccumMode
= GL_FALSE
;
295 ctx
->IntegerAccumScaler
= 0.0;
298 if (ctx
->IntegerAccumMode
) {
299 /* just copy values into accum buffer */
301 GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ xpos
* 4;
302 assert(ctx
->IntegerAccumScaler
> 0.0);
303 assert(ctx
->IntegerAccumScaler
<= 1.0);
304 for (j
= 0; j
< height
; j
++) {
306 gl_read_rgba_span(ctx
, ctx
->DrawBuffer
, width
, xpos
, ypos
, rgba
);
307 for (i
= i4
= 0; i
< width
; i
++, i4
+= 4) {
308 acc
[i4
+0] = rgba
[i
][RCOMP
];
309 acc
[i4
+1] = rgba
[i
][GCOMP
];
310 acc
[i4
+2] = rgba
[i
][BCOMP
];
311 acc
[i4
+3] = rgba
[i
][ACOMP
];
318 /* scaled integer accum buffer */
319 const GLfloat rscale
= value
* acc_scale
/ fChanMax
;
320 const GLfloat gscale
= value
* acc_scale
/ fChanMax
;
321 const GLfloat bscale
= value
* acc_scale
/ fChanMax
;
322 const GLfloat ascale
= value
* acc_scale
/ fChanMax
;
323 const GLfloat d
= 3.0 / acc_scale
;
325 for (j
= 0; j
< height
; j
++) {
326 GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ xpos
* 4;
327 gl_read_rgba_span(ctx
, ctx
->DrawBuffer
, width
, xpos
, ypos
, rgba
);
328 for (i
=0;i
<width
;i
++) {
329 *acc
++ = (GLaccum
) ((GLfloat
) rgba
[i
][RCOMP
] * rscale
+ d
);
330 *acc
++ = (GLaccum
) ((GLfloat
) rgba
[i
][GCOMP
] * gscale
+ d
);
331 *acc
++ = (GLaccum
) ((GLfloat
) rgba
[i
][BCOMP
] * bscale
+ d
);
332 *acc
++ = (GLaccum
) ((GLfloat
) rgba
[i
][ACOMP
] * ascale
+ d
);
338 /* restore read buffer = draw buffer (the default) */
339 (*ctx
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
340 ctx
->Color
.DriverDrawBuffer
);
344 /* May have to leave optimized accum buffer mode */
345 if (ctx
->IntegerAccumMode
&& value
!= 1.0)
348 if (ctx
->IntegerAccumMode
&& ctx
->IntegerAccumScaler
> 0) {
349 /* build lookup table to avoid many floating point multiplies */
350 const GLfloat mult
= ctx
->IntegerAccumScaler
;
351 static GLchan multTable
[32768];
352 static GLfloat prevMult
= 0.0;
354 const GLint max
= (GLint
) (256 / mult
);
355 if (mult
!= prevMult
) {
356 assert(max
<= 32768);
357 for (j
= 0; j
< max
; j
++)
358 multTable
[j
] = (GLint
) ((GLfloat
) j
* mult
+ 0.5F
);
362 assert(ctx
->IntegerAccumScaler
> 0.0);
363 assert(ctx
->IntegerAccumScaler
<= 1.0);
364 for (j
= 0; j
< height
; j
++) {
365 const GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ xpos
*4;
367 for (i
= i4
= 0; i
< width
; i
++, i4
+= 4) {
368 ASSERT(acc
[i4
+0] < max
);
369 ASSERT(acc
[i4
+1] < max
);
370 ASSERT(acc
[i4
+2] < max
);
371 ASSERT(acc
[i4
+3] < max
);
372 rgba
[i
][RCOMP
] = multTable
[acc
[i4
+0]];
373 rgba
[i
][GCOMP
] = multTable
[acc
[i4
+1]];
374 rgba
[i
][BCOMP
] = multTable
[acc
[i4
+2]];
375 rgba
[i
][ACOMP
] = multTable
[acc
[i4
+3]];
377 if (ctx
->Color
.SWmasking
) {
378 _mesa_mask_rgba_span( ctx
, width
, xpos
, ypos
, rgba
);
380 (*ctx
->Driver
.WriteRGBASpan
)( ctx
, width
, xpos
, ypos
,
381 (const GLubyte (*)[4])rgba
, NULL
);
386 const GLfloat rscale
= value
/ acc_scale
* fChanMax
;
387 const GLfloat gscale
= value
/ acc_scale
* fChanMax
;
388 const GLfloat bscale
= value
/ acc_scale
* fChanMax
;
389 const GLfloat ascale
= value
/ acc_scale
* fChanMax
;
391 for (j
=0;j
<height
;j
++) {
392 const GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ xpos
*4;
393 for (i
=0;i
<width
;i
++) {
395 r
= (GLint
) ( (GLfloat
) (*acc
++) * rscale
+ 0.5F
);
396 g
= (GLint
) ( (GLfloat
) (*acc
++) * gscale
+ 0.5F
);
397 b
= (GLint
) ( (GLfloat
) (*acc
++) * bscale
+ 0.5F
);
398 a
= (GLint
) ( (GLfloat
) (*acc
++) * ascale
+ 0.5F
);
399 rgba
[i
][RCOMP
] = CLAMP( r
, 0, iChanMax
);
400 rgba
[i
][GCOMP
] = CLAMP( g
, 0, iChanMax
);
401 rgba
[i
][BCOMP
] = CLAMP( b
, 0, iChanMax
);
402 rgba
[i
][ACOMP
] = CLAMP( a
, 0, iChanMax
);
404 if (ctx
->Color
.SWmasking
) {
405 _mesa_mask_rgba_span( ctx
, width
, xpos
, ypos
, rgba
);
407 (*ctx
->Driver
.WriteRGBASpan
)( ctx
, width
, xpos
, ypos
,
408 (const GLubyte (*)[4])rgba
, NULL
);
415 gl_error( ctx
, GL_INVALID_ENUM
, "glAccum" );
422 * Clear the accumulation Buffer.
425 _mesa_clear_accum_buffer( GLcontext
*ctx
)
430 if (ctx
->Visual
->AccumRedBits
==0) {
431 /* No accumulation buffer! */
435 if (sizeof(GLaccum
)==1) {
438 else if (sizeof(GLaccum
)==2) {
442 /* sizeof(GLaccum) > 2 (Cray) */
443 acc_scale
= (float) SHRT_MAX
;
446 /* number of pixels */
447 buffersize
= ctx
->DrawBuffer
->Width
* ctx
->DrawBuffer
->Height
;
449 if (!ctx
->DrawBuffer
->Accum
) {
450 /* try to alloc accumulation buffer */
451 ctx
->DrawBuffer
->Accum
= (GLaccum
*)
452 MALLOC( buffersize
* 4 * sizeof(GLaccum
) );
455 if (ctx
->DrawBuffer
->Accum
) {
456 if (ctx
->Scissor
.Enabled
) {
457 /* Limit clear to scissor box */
462 r
= (GLaccum
) (ctx
->Accum
.ClearColor
[0] * acc_scale
);
463 g
= (GLaccum
) (ctx
->Accum
.ClearColor
[1] * acc_scale
);
464 b
= (GLaccum
) (ctx
->Accum
.ClearColor
[2] * acc_scale
);
465 a
= (GLaccum
) (ctx
->Accum
.ClearColor
[3] * acc_scale
);
466 /* size of region to clear */
467 width
= 4 * (ctx
->DrawBuffer
->Xmax
- ctx
->DrawBuffer
->Xmin
+ 1);
468 height
= ctx
->DrawBuffer
->Ymax
- ctx
->DrawBuffer
->Ymin
+ 1;
469 /* ptr to first element to clear */
470 row
= ctx
->DrawBuffer
->Accum
471 + 4 * (ctx
->DrawBuffer
->Ymin
* ctx
->DrawBuffer
->Width
472 + ctx
->DrawBuffer
->Xmin
);
473 for (j
=0;j
<height
;j
++) {
474 for (i
=0;i
<width
;i
+=4) {
480 row
+= 4 * ctx
->DrawBuffer
->Width
;
484 /* clear whole buffer */
485 if (ctx
->Accum
.ClearColor
[0]==0.0 &&
486 ctx
->Accum
.ClearColor
[1]==0.0 &&
487 ctx
->Accum
.ClearColor
[2]==0.0 &&
488 ctx
->Accum
.ClearColor
[3]==0.0) {
490 BZERO( ctx
->DrawBuffer
->Accum
, buffersize
* 4 * sizeof(GLaccum
) );
494 GLaccum
*acc
, r
, g
, b
, a
;
497 acc
= ctx
->DrawBuffer
->Accum
;
498 r
= (GLaccum
) (ctx
->Accum
.ClearColor
[0] * acc_scale
);
499 g
= (GLaccum
) (ctx
->Accum
.ClearColor
[1] * acc_scale
);
500 b
= (GLaccum
) (ctx
->Accum
.ClearColor
[2] * acc_scale
);
501 a
= (GLaccum
) (ctx
->Accum
.ClearColor
[3] * acc_scale
);
502 for (i
=0;i
<buffersize
;i
++) {
511 /* update optimized accum state vars */
512 if (ctx
->Accum
.ClearColor
[0] == 0.0 && ctx
->Accum
.ClearColor
[1] == 0.0 &&
513 ctx
->Accum
.ClearColor
[2] == 0.0 && ctx
->Accum
.ClearColor
[3] == 0.0) {
514 #ifdef USE_OPTIMIZED_ACCUM
515 ctx
->IntegerAccumMode
= GL_TRUE
;
517 ctx
->IntegerAccumMode
= GL_FALSE
;
519 ctx
->IntegerAccumScaler
= 0.0; /* denotes empty accum buffer */
522 ctx
->IntegerAccumMode
= GL_FALSE
;