68e567bdf1ebdf5a4f3b6cf13ec303e9bda21ea9
1 /* $Id: accum.c,v 1.10 1999/11/08 07:36:43 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.
28 /* $XFree86: xc/lib/GL/mesa/src/accum.c,v 1.3 1999/04/04 00:20:17 dawes Exp $ */
39 #include "GL/xf86glx.h"
51 * Accumulation buffer notes
53 * Normally, accumulation buffer values are GLshorts with values in
54 * [-32767, 32767] which represent floating point colors in [-1, 1],
55 * as suggested by the OpenGL specification.
57 * We optimize for the common case used for full-scene antialiasing:
58 * // start with accum buffer cleared to zero
59 * glAccum(GL_LOAD, w); // or GL_ACCUM the first image
60 * glAccum(GL_ACCUM, w);
62 * glAccum(GL_ACCUM, w);
63 * glAccum(GL_RETURN, 1.0);
64 * That is, we start with an empty accumulation buffer and accumulate
65 * n images, each with weight w = 1/n.
66 * In this scenario, we can simply store unscaled integer values in
67 * the accum buffer instead of scaled integers. We'll also keep track
68 * of the w value so when we do GL_RETURN we simply divide the accumulated
70 * This lets us avoid _many_ int->float->int conversions.
74 #define USE_OPTIMIZED_ACCUM /* enable the optimization */
78 void gl_alloc_accum_buffer( GLcontext
*ctx
)
82 if (ctx
->Buffer
->Accum
) {
83 FREE( ctx
->Buffer
->Accum
);
84 ctx
->Buffer
->Accum
= NULL
;
87 /* allocate accumulation buffer if not already present */
88 n
= ctx
->Buffer
->Width
* ctx
->Buffer
->Height
* 4 * sizeof(GLaccum
);
89 ctx
->Buffer
->Accum
= (GLaccum
*) MALLOC( n
);
90 if (!ctx
->Buffer
->Accum
) {
91 /* unable to setup accumulation buffer */
92 gl_error( ctx
, GL_OUT_OF_MEMORY
, "glAccum" );
94 #ifdef USE_OPTIMIZED_ACCUM
95 ctx
->IntegerAccumMode
= GL_TRUE
;
97 ctx
->IntegerAccumMode
= GL_FALSE
;
99 ctx
->IntegerAccumScaler
= 0.0;
104 void gl_ClearAccum( GLcontext
*ctx
,
105 GLfloat red
, GLfloat green
, GLfloat blue
, GLfloat alpha
)
107 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glAccum");
109 ctx
->Accum
.ClearColor
[0] = CLAMP( red
, -1.0, 1.0 );
110 ctx
->Accum
.ClearColor
[1] = CLAMP( green
, -1.0, 1.0 );
111 ctx
->Accum
.ClearColor
[2] = CLAMP( blue
, -1.0, 1.0 );
112 ctx
->Accum
.ClearColor
[3] = CLAMP( alpha
, -1.0, 1.0 );
118 * This is called when we fall out of optimized/unscaled accum buffer mode.
119 * That is, we convert each unscaled accum buffer value into a scaled value
120 * representing the range[-1, 1].
122 static void rescale_accum( GLcontext
*ctx
)
124 const GLuint n
= ctx
->Buffer
->Width
* ctx
->Buffer
->Height
* 4;
125 const GLfloat fChanMax
= (1 << (sizeof(GLchan
) * 8)) - 1;
126 const GLfloat s
= ctx
->IntegerAccumScaler
* (32767.0 / fChanMax
);
127 GLaccum
*accum
= ctx
->Buffer
->Accum
;
130 assert(ctx
->IntegerAccumMode
);
133 for (i
= 0; i
< n
; i
++) {
134 accum
[i
] = (GLaccum
) (accum
[i
] * s
);
137 ctx
->IntegerAccumMode
= GL_FALSE
;
142 void gl_Accum( GLcontext
*ctx
, GLenum op
, GLfloat value
)
144 GLuint xpos
, ypos
, width
, height
, width4
;
146 GLubyte rgba
[MAX_WIDTH
][4];
147 const GLint iChanMax
= (1 << (sizeof(GLchan
) * 8)) - 1;
148 const GLfloat fChanMax
= (1 << (sizeof(GLchan
) * 8)) - 1;
150 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glAccum");
152 if (ctx
->Visual
->AccumBits
==0 || !ctx
->Buffer
->Accum
) {
153 /* No accumulation buffer! */
154 gl_warning(ctx
, "Calling glAccum() without an accumulation buffer");
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
->Buffer
->Width
;
184 height
= ctx
->Buffer
->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
->Buffer
->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
->Buffer
->Accum
+ ypos
* width4
+ 4 * xpos
;
217 for (i
= 0; i
< width4
; i
++) {
218 acc
[i
] = (GLaccum
) ( (GLfloat
) acc
[i
] * value
);
226 (void) (*ctx
->Driver
.SetBuffer
)( ctx
, ctx
->Pixel
.DriverReadBuffer
);
228 /* May have to leave optimized accum buffer mode */
229 if (ctx
->IntegerAccumScaler
== 0.0 && value
> 0.0 && value
<= 1.0)
230 ctx
->IntegerAccumScaler
= value
;
231 if (ctx
->IntegerAccumMode
&& value
!= ctx
->IntegerAccumScaler
)
234 if (ctx
->IntegerAccumMode
) {
235 /* simply add integer color values into accum buffer */
237 GLaccum
*acc
= ctx
->Buffer
->Accum
+ ypos
* width4
+ xpos
* 4;
238 assert(ctx
->IntegerAccumScaler
> 0.0);
239 assert(ctx
->IntegerAccumScaler
<= 1.0);
240 for (j
= 0; j
< height
; j
++) {
243 gl_read_rgba_span(ctx
, width
, xpos
, ypos
, rgba
);
244 for (i
= i4
= 0; i
< width
; i
++, i4
+=4) {
245 acc
[i4
+0] += rgba
[i
][RCOMP
];
246 acc
[i4
+1] += rgba
[i
][GCOMP
];
247 acc
[i4
+2] += rgba
[i
][BCOMP
];
248 acc
[i4
+3] += rgba
[i
][ACOMP
];
255 /* scaled integer accum buffer */
256 const GLfloat rscale
= value
* acc_scale
/ fChanMax
;
257 const GLfloat gscale
= value
* acc_scale
/ fChanMax
;
258 const GLfloat bscale
= value
* acc_scale
/ fChanMax
;
259 const GLfloat ascale
= value
* acc_scale
/ fChanMax
;
261 for (j
=0;j
<height
;j
++) {
262 GLaccum
*acc
= ctx
->Buffer
->Accum
+ ypos
* width4
+ xpos
* 4;
264 gl_read_rgba_span(ctx
, width
, xpos
, ypos
, rgba
);
265 for (i
=0;i
<width
;i
++) {
266 *acc
+= (GLaccum
) ( (GLfloat
) rgba
[i
][RCOMP
] * rscale
); acc
++;
267 *acc
+= (GLaccum
) ( (GLfloat
) rgba
[i
][GCOMP
] * gscale
); acc
++;
268 *acc
+= (GLaccum
) ( (GLfloat
) rgba
[i
][BCOMP
] * bscale
); acc
++;
269 *acc
+= (GLaccum
) ( (GLfloat
) rgba
[i
][ACOMP
] * ascale
); acc
++;
274 (void) (*ctx
->Driver
.SetBuffer
)( ctx
, ctx
->Color
.DriverDrawBuffer
);
278 (void) (*ctx
->Driver
.SetBuffer
)( ctx
, ctx
->Pixel
.DriverReadBuffer
);
280 /* This is a change to go into optimized accum buffer mode */
281 if (value
> 0.0 && value
<= 1.0) {
282 #ifdef USE_OPTIMIZED_ACCUM
283 ctx
->IntegerAccumMode
= GL_TRUE
;
285 ctx
->IntegerAccumMode
= GL_FALSE
;
287 ctx
->IntegerAccumScaler
= value
;
290 ctx
->IntegerAccumMode
= GL_FALSE
;
291 ctx
->IntegerAccumScaler
= 0.0;
294 if (ctx
->IntegerAccumMode
) {
295 /* just copy values into accum buffer */
297 GLaccum
*acc
= ctx
->Buffer
->Accum
+ ypos
* width4
+ xpos
* 4;
298 assert(ctx
->IntegerAccumScaler
> 0.0);
299 assert(ctx
->IntegerAccumScaler
<= 1.0);
300 for (j
= 0; j
< height
; j
++) {
302 gl_read_rgba_span(ctx
, width
, xpos
, ypos
, rgba
);
303 for (i
= i4
= 0; i
< width
; i
++, i4
+= 4) {
304 acc
[i4
+0] = rgba
[i
][RCOMP
];
305 acc
[i4
+1] = rgba
[i
][GCOMP
];
306 acc
[i4
+2] = rgba
[i
][BCOMP
];
307 acc
[i4
+3] = rgba
[i
][ACOMP
];
314 /* scaled integer accum buffer */
315 const GLfloat rscale
= value
* acc_scale
/ fChanMax
;
316 const GLfloat gscale
= value
* acc_scale
/ fChanMax
;
317 const GLfloat bscale
= value
* acc_scale
/ fChanMax
;
318 const GLfloat ascale
= value
* acc_scale
/ fChanMax
;
319 const GLfloat d
= 3.0 / acc_scale
;
321 for (j
= 0; j
< height
; j
++) {
322 GLaccum
*acc
= ctx
->Buffer
->Accum
+ ypos
* width4
+ xpos
* 4;
323 gl_read_rgba_span(ctx
, width
, xpos
, ypos
, rgba
);
324 for (i
=0;i
<width
;i
++) {
325 *acc
++ = (GLaccum
) ((GLfloat
) rgba
[i
][RCOMP
] * rscale
+ d
);
326 *acc
++ = (GLaccum
) ((GLfloat
) rgba
[i
][GCOMP
] * gscale
+ d
);
327 *acc
++ = (GLaccum
) ((GLfloat
) rgba
[i
][BCOMP
] * bscale
+ d
);
328 *acc
++ = (GLaccum
) ((GLfloat
) rgba
[i
][ACOMP
] * ascale
+ d
);
333 (void) (*ctx
->Driver
.SetBuffer
)( ctx
, ctx
->Color
.DriverDrawBuffer
);
337 /* May have to leave optimized accum buffer mode */
338 if (ctx
->IntegerAccumMode
&& value
!= 1.0)
341 if (ctx
->IntegerAccumMode
) {
342 /* build lookup table to avoid many floating point multiplies */
343 const GLfloat mult
= ctx
->IntegerAccumScaler
;
344 static GLchan multTable
[32768];
345 static GLfloat prevMult
= 0.0;
347 const GLint max
= 256 / mult
;
348 if (mult
!= prevMult
) {
349 assert(max
<= 32768);
350 for (j
= 0; j
< max
; j
++)
351 multTable
[j
] = (GLint
) ((GLfloat
) j
* mult
+ 0.5F
);
355 assert(ctx
->IntegerAccumScaler
> 0.0);
356 assert(ctx
->IntegerAccumScaler
<= 1.0);
357 for (j
= 0; j
< height
; j
++) {
358 const GLaccum
*acc
= ctx
->Buffer
->Accum
+ ypos
* width4
+ xpos
*4;
360 for (i
= i4
= 0; i
< width
; i
++, i4
+= 4) {
361 ASSERT(acc
[i4
+0] < max
);
362 ASSERT(acc
[i4
+1] < max
);
363 ASSERT(acc
[i4
+2] < max
);
364 ASSERT(acc
[i4
+3] < max
);
365 rgba
[i
][RCOMP
] = multTable
[acc
[i4
+0]];
366 rgba
[i
][GCOMP
] = multTable
[acc
[i4
+1]];
367 rgba
[i
][BCOMP
] = multTable
[acc
[i4
+2]];
368 rgba
[i
][ACOMP
] = multTable
[acc
[i4
+3]];
370 if (ctx
->Color
.SWmasking
) {
371 gl_mask_rgba_span( ctx
, width
, xpos
, ypos
, rgba
);
373 (*ctx
->Driver
.WriteRGBASpan
)( ctx
, width
, xpos
, ypos
,
374 (const GLubyte (*)[4])rgba
, NULL
);
379 const GLfloat rscale
= value
/ acc_scale
* fChanMax
;
380 const GLfloat gscale
= value
/ acc_scale
* fChanMax
;
381 const GLfloat bscale
= value
/ acc_scale
* fChanMax
;
382 const GLfloat ascale
= value
/ acc_scale
* fChanMax
;
384 for (j
=0;j
<height
;j
++) {
385 const GLaccum
*acc
= ctx
->Buffer
->Accum
+ ypos
* width4
+ xpos
*4;
386 for (i
=0;i
<width
;i
++) {
388 r
= (GLint
) ( (GLfloat
) (*acc
++) * rscale
+ 0.5F
);
389 g
= (GLint
) ( (GLfloat
) (*acc
++) * gscale
+ 0.5F
);
390 b
= (GLint
) ( (GLfloat
) (*acc
++) * bscale
+ 0.5F
);
391 a
= (GLint
) ( (GLfloat
) (*acc
++) * ascale
+ 0.5F
);
392 rgba
[i
][RCOMP
] = CLAMP( r
, 0, iChanMax
);
393 rgba
[i
][GCOMP
] = CLAMP( g
, 0, iChanMax
);
394 rgba
[i
][BCOMP
] = CLAMP( b
, 0, iChanMax
);
395 rgba
[i
][ACOMP
] = CLAMP( a
, 0, iChanMax
);
397 if (ctx
->Color
.SWmasking
) {
398 gl_mask_rgba_span( ctx
, width
, xpos
, ypos
, rgba
);
400 (*ctx
->Driver
.WriteRGBASpan
)( ctx
, width
, xpos
, ypos
,
401 (const GLubyte (*)[4])rgba
, NULL
);
408 gl_error( ctx
, GL_INVALID_ENUM
, "glAccum" );
415 * Clear the accumulation Buffer.
417 void gl_clear_accum_buffer( GLcontext
*ctx
)
422 if (ctx
->Visual
->AccumBits
==0) {
423 /* No accumulation buffer! */
427 if (sizeof(GLaccum
)==1) {
430 else if (sizeof(GLaccum
)==2) {
434 /* sizeof(GLaccum) > 2 (Cray) */
435 acc_scale
= (float) SHRT_MAX
;
438 /* number of pixels */
439 buffersize
= ctx
->Buffer
->Width
* ctx
->Buffer
->Height
;
441 if (!ctx
->Buffer
->Accum
) {
442 /* try to alloc accumulation buffer */
443 ctx
->Buffer
->Accum
= (GLaccum
*)
444 MALLOC( buffersize
* 4 * sizeof(GLaccum
) );
447 if (ctx
->Buffer
->Accum
) {
448 if (ctx
->Scissor
.Enabled
) {
449 /* Limit clear to scissor box */
454 r
= (GLaccum
) (ctx
->Accum
.ClearColor
[0] * acc_scale
);
455 g
= (GLaccum
) (ctx
->Accum
.ClearColor
[1] * acc_scale
);
456 b
= (GLaccum
) (ctx
->Accum
.ClearColor
[2] * acc_scale
);
457 a
= (GLaccum
) (ctx
->Accum
.ClearColor
[3] * acc_scale
);
458 /* size of region to clear */
459 width
= 4 * (ctx
->Buffer
->Xmax
- ctx
->Buffer
->Xmin
+ 1);
460 height
= ctx
->Buffer
->Ymax
- ctx
->Buffer
->Ymin
+ 1;
461 /* ptr to first element to clear */
462 row
= ctx
->Buffer
->Accum
463 + 4 * (ctx
->Buffer
->Ymin
* ctx
->Buffer
->Width
464 + ctx
->Buffer
->Xmin
);
465 for (j
=0;j
<height
;j
++) {
466 for (i
=0;i
<width
;i
+=4) {
472 row
+= 4 * ctx
->Buffer
->Width
;
476 /* clear whole buffer */
477 if (ctx
->Accum
.ClearColor
[0]==0.0 &&
478 ctx
->Accum
.ClearColor
[1]==0.0 &&
479 ctx
->Accum
.ClearColor
[2]==0.0 &&
480 ctx
->Accum
.ClearColor
[3]==0.0) {
482 MEMSET( ctx
->Buffer
->Accum
, 0, buffersize
* 4 * sizeof(GLaccum
) );
486 GLaccum
*acc
, r
, g
, b
, a
;
489 acc
= ctx
->Buffer
->Accum
;
490 r
= (GLaccum
) (ctx
->Accum
.ClearColor
[0] * acc_scale
);
491 g
= (GLaccum
) (ctx
->Accum
.ClearColor
[1] * acc_scale
);
492 b
= (GLaccum
) (ctx
->Accum
.ClearColor
[2] * acc_scale
);
493 a
= (GLaccum
) (ctx
->Accum
.ClearColor
[3] * acc_scale
);
494 for (i
=0;i
<buffersize
;i
++) {
503 /* update optimized accum state vars */
504 if (ctx
->Accum
.ClearColor
[0] == 0.0 && ctx
->Accum
.ClearColor
[1] == 0.0 &&
505 ctx
->Accum
.ClearColor
[2] == 0.0 && ctx
->Accum
.ClearColor
[3] == 0.0) {
506 #ifdef USE_OPTIMIZED_ACCUM
507 ctx
->IntegerAccumMode
= GL_TRUE
;
509 ctx
->IntegerAccumMode
= GL_FALSE
;
511 ctx
->IntegerAccumScaler
= 0.0; /* denotes empty accum buffer */
514 ctx
->IntegerAccumMode
= GL_FALSE
;