1 /* $Id: accum.c,v 1.6 1999/10/13 18:42:49 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 $ */
38 #include "GL/xf86glx.h"
48 #include "GL/xf86glx.h"
54 * Accumulation buffer notes
56 * Normally, accumulation buffer values are GLshorts with values in
57 * [-32767, 32767] which represent floating point colors in [-1, 1],
58 * as suggested by the OpenGL specification.
60 * We optimize for the common case used for full-scene antialiasing:
61 * // start with accum buffer cleared to zero
62 * glAccum(GL_LOAD, w); // or GL_ACCUM the first image
63 * glAccum(GL_ACCUM, w);
65 * glAccum(GL_ACCUM, w);
66 * glAccum(GL_RETURN, 1.0);
67 * That is, we start with an empty accumulation buffer and accumulate
68 * n images, each with weight w = 1/n.
69 * In this scenario, we can simply store unscaled integer values in
70 * the accum buffer instead of scaled integers. We'll also keep track
71 * of the w value so when we do GL_RETURN we simply divide the accumulated
73 * This lets us avoid _many_ int->float->int conversions.
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 ctx
->IntegerAccumMode
= GL_TRUE
;
95 ctx
->IntegerAccumScaler
= 0.0;
100 void gl_ClearAccum( GLcontext
*ctx
,
101 GLfloat red
, GLfloat green
, GLfloat blue
, GLfloat alpha
)
103 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glAccum");
105 ctx
->Accum
.ClearColor
[0] = CLAMP( red
, -1.0, 1.0 );
106 ctx
->Accum
.ClearColor
[1] = CLAMP( green
, -1.0, 1.0 );
107 ctx
->Accum
.ClearColor
[2] = CLAMP( blue
, -1.0, 1.0 );
108 ctx
->Accum
.ClearColor
[3] = CLAMP( alpha
, -1.0, 1.0 );
114 * This is called when we fall out of optimized/unscaled accum buffer mode.
115 * That is, we convert each unscaled accum buffer value into a scaled value
116 * representing the range[-1, 1].
118 static void rescale_accum( GLcontext
*ctx
)
120 const GLuint n
= ctx
->Buffer
->Width
* ctx
->Buffer
->Height
* 4;
121 const GLfloat s
= ctx
->IntegerAccumScaler
* (32767.0 / 255.0);
122 GLaccum
*accum
= ctx
->Buffer
->Accum
;
125 assert(ctx
->IntegerAccumMode
);
126 assert(sizeof(GLchan
) == 1); /* if not true, 255.0 above must be fixed */
129 for (i
= 0; i
< n
; i
++) {
130 accum
[i
] = (GLaccum
) (accum
[i
] * s
);
133 ctx
->IntegerAccumMode
= GL_FALSE
;
138 void gl_Accum( GLcontext
*ctx
, GLenum op
, GLfloat value
)
140 GLuint xpos
, ypos
, width
, height
, width4
;
142 GLubyte rgba
[MAX_WIDTH
][4];
144 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glAccum");
146 if (ctx
->Visual
->AccumBits
==0 || !ctx
->Buffer
->Accum
) {
147 /* No accumulation buffer! */
148 gl_warning(ctx
, "Calling glAccum() without an accumulation buffer");
152 if (sizeof(GLaccum
)==1) {
155 else if (sizeof(GLaccum
)==2) {
159 /* sizeof(GLaccum) > 2 (Cray) */
160 acc_scale
= (float) SHRT_MAX
;
164 gl_update_state( ctx
);
166 /* Determine region to operate upon. */
167 if (ctx
->Scissor
.Enabled
) {
168 xpos
= ctx
->Scissor
.X
;
169 ypos
= ctx
->Scissor
.Y
;
170 width
= ctx
->Scissor
.Width
;
171 height
= ctx
->Scissor
.Height
;
177 width
= ctx
->Buffer
->Width
;
178 height
= ctx
->Buffer
->Height
;
186 const GLaccum intVal
= (GLaccum
) (value
* acc_scale
);
188 /* May have to leave optimized accum buffer mode */
189 if (ctx
->IntegerAccumMode
)
191 for (j
= 0; j
< height
; j
++) {
192 GLaccum
* acc
= ctx
->Buffer
->Accum
+ ypos
* width4
+ 4 * xpos
;
194 for (i
= 0; i
< width4
; i
++) {
205 /* May have to leave optimized accum buffer mode */
206 if (ctx
->IntegerAccumMode
)
208 for (j
= 0; j
< height
; j
++) {
209 GLaccum
*acc
= ctx
->Buffer
->Accum
+ ypos
* width4
+ 4 * xpos
;
211 for (i
= 0; i
< width4
; i
++) {
212 acc
[i
] = (GLaccum
) ( (GLfloat
) acc
[i
] * value
);
220 (void) (*ctx
->Driver
.SetBuffer
)( ctx
, ctx
->Pixel
.DriverReadBuffer
);
222 /* May have to leave optimized accum buffer mode */
223 if (ctx
->IntegerAccumScaler
== 0.0 && value
> 0.0 && value
<= 1.0)
224 ctx
->IntegerAccumScaler
= value
;
225 if (ctx
->IntegerAccumMode
&& value
!= ctx
->IntegerAccumScaler
)
228 if (ctx
->IntegerAccumMode
) {
229 /* simply add integer color values into accum buffer */
231 GLaccum
*acc
= ctx
->Buffer
->Accum
+ ypos
* width4
+ xpos
* 4;
232 assert(ctx
->IntegerAccumScaler
> 0.0);
233 assert(ctx
->IntegerAccumScaler
<= 1.0);
234 for (j
= 0; j
< height
; j
++) {
237 gl_read_rgba_span(ctx
, width
, xpos
, ypos
, rgba
);
238 for (i
= i4
= 0; i
< width
; i
++, i4
+=4) {
239 acc
[i4
+0] += rgba
[i
][RCOMP
];
240 acc
[i4
+1] += rgba
[i
][GCOMP
];
241 acc
[i4
+2] += rgba
[i
][BCOMP
];
242 acc
[i4
+3] += rgba
[i
][ACOMP
];
249 /* scaled integer accum buffer */
250 const GLfloat rscale
= value
* acc_scale
/ 255.0;
251 const GLfloat gscale
= value
* acc_scale
/ 255.0;
252 const GLfloat bscale
= value
* acc_scale
/ 255.0;
253 const GLfloat ascale
= value
* acc_scale
/ 255.0;
255 for (j
=0;j
<height
;j
++) {
256 GLaccum
*acc
= ctx
->Buffer
->Accum
+ ypos
* width4
+ xpos
* 4;
258 gl_read_rgba_span(ctx
, width
, xpos
, ypos
, rgba
);
259 for (i
=0;i
<width
;i
++) {
260 *acc
+= (GLaccum
) ( (GLfloat
) rgba
[i
][RCOMP
] * rscale
); acc
++;
261 *acc
+= (GLaccum
) ( (GLfloat
) rgba
[i
][GCOMP
] * gscale
); acc
++;
262 *acc
+= (GLaccum
) ( (GLfloat
) rgba
[i
][BCOMP
] * bscale
); acc
++;
263 *acc
+= (GLaccum
) ( (GLfloat
) rgba
[i
][ACOMP
] * ascale
); acc
++;
268 (void) (*ctx
->Driver
.SetBuffer
)( ctx
, ctx
->Color
.DriverDrawBuffer
);
272 (void) (*ctx
->Driver
.SetBuffer
)( ctx
, ctx
->Pixel
.DriverReadBuffer
);
274 /* This is a change to go into optimized accum buffer mode */
275 if (value
> 0.0 && value
<= 1.0) {
276 ctx
->IntegerAccumMode
= GL_TRUE
;
277 ctx
->IntegerAccumScaler
= value
;
280 ctx
->IntegerAccumMode
= GL_FALSE
;
281 ctx
->IntegerAccumScaler
= 0.0;
284 if (ctx
->IntegerAccumMode
) {
285 /* just copy values into accum buffer */
287 GLaccum
*acc
= ctx
->Buffer
->Accum
+ ypos
* width4
+ xpos
* 4;
288 assert(ctx
->IntegerAccumScaler
> 0.0);
289 assert(ctx
->IntegerAccumScaler
<= 1.0);
290 for (j
= 0; j
< height
; j
++) {
292 gl_read_rgba_span(ctx
, width
, xpos
, ypos
, rgba
);
293 for (i
= i4
= 0; i
< width
; i
++, i4
+= 4) {
294 acc
[i4
+0] = rgba
[i
][RCOMP
];
295 acc
[i4
+1] = rgba
[i
][GCOMP
];
296 acc
[i4
+2] = rgba
[i
][BCOMP
];
297 acc
[i4
+3] = rgba
[i
][ACOMP
];
304 /* scaled integer accum buffer */
305 const GLfloat rscale
= value
* acc_scale
/ 255.0;
306 const GLfloat gscale
= value
* acc_scale
/ 255.0;
307 const GLfloat bscale
= value
* acc_scale
/ 255.0;
308 const GLfloat ascale
= value
* acc_scale
/ 255.0;
310 for (j
= 0; j
< height
; j
++) {
311 GLaccum
*acc
= ctx
->Buffer
->Accum
+ ypos
* width4
+ xpos
* 4;
312 gl_read_rgba_span(ctx
, width
, xpos
, ypos
, rgba
);
313 for (i
=0;i
<width
;i
++) {
314 *acc
++ = (GLaccum
) ( (GLfloat
) rgba
[i
][RCOMP
] * rscale
);
315 *acc
++ = (GLaccum
) ( (GLfloat
) rgba
[i
][GCOMP
] * gscale
);
316 *acc
++ = (GLaccum
) ( (GLfloat
) rgba
[i
][BCOMP
] * bscale
);
317 *acc
++ = (GLaccum
) ( (GLfloat
) rgba
[i
][ACOMP
] * ascale
);
322 (void) (*ctx
->Driver
.SetBuffer
)( ctx
, ctx
->Color
.DriverDrawBuffer
);
326 /* May have to leave optimized accum buffer mode */
327 if (ctx
->IntegerAccumMode
&& value
!= 1.0)
330 if (ctx
->IntegerAccumMode
) {
331 /* build lookup table to avoid integer divides */
332 GLint divisor
= (GLint
) ((1.0F
/ ctx
->IntegerAccumScaler
) + 0.5F
);
333 static GLubyte divTable
[32768];
334 static GLint prevDivisor
= 0.0;
336 if (divisor
!= prevDivisor
) {
337 assert(divisor
* 256 <= 32768);
338 for (j
= 0; j
< divisor
* 256; j
++)
339 divTable
[j
] = j
/ divisor
;
340 prevDivisor
= divisor
;
343 assert(ctx
->IntegerAccumScaler
> 0.0);
344 assert(ctx
->IntegerAccumScaler
<= 1.0);
345 for (j
= 0; j
< height
; j
++) {
346 const GLaccum
*acc
= ctx
->Buffer
->Accum
+ ypos
* width4
+ xpos
*4;
348 for (i
= i4
= 0; i
< width
; i
++, i4
+= 4) {
349 ASSERT(acc
[i4
+0] < divisor
* 256);
350 ASSERT(acc
[i4
+1] < divisor
* 256);
351 ASSERT(acc
[i4
+2] < divisor
* 256);
352 ASSERT(acc
[i4
+3] < divisor
* 256);
353 rgba
[i
][RCOMP
] = divTable
[acc
[i4
+0]];
354 rgba
[i
][GCOMP
] = divTable
[acc
[i4
+1]];
355 rgba
[i
][BCOMP
] = divTable
[acc
[i4
+2]];
356 rgba
[i
][ACOMP
] = divTable
[acc
[i4
+3]];
358 if (ctx
->Color
.SWmasking
) {
359 gl_mask_rgba_span( ctx
, width
, xpos
, ypos
, rgba
);
361 (*ctx
->Driver
.WriteRGBASpan
)( ctx
, width
, xpos
, ypos
,
362 (const GLubyte (*)[4])rgba
, NULL
);
367 const GLfloat rscale
= value
/ acc_scale
* 255.0F
;
368 const GLfloat gscale
= value
/ acc_scale
* 255.0F
;
369 const GLfloat bscale
= value
/ acc_scale
* 255.0F
;
370 const GLfloat ascale
= value
/ acc_scale
* 255.0F
;
372 for (j
=0;j
<height
;j
++) {
373 const GLaccum
*acc
= ctx
->Buffer
->Accum
+ ypos
* width4
+ xpos
*4;
374 for (i
=0;i
<width
;i
++) {
376 r
= (GLint
) ( (GLfloat
) (*acc
++) * rscale
+ 0.5F
);
377 g
= (GLint
) ( (GLfloat
) (*acc
++) * gscale
+ 0.5F
);
378 b
= (GLint
) ( (GLfloat
) (*acc
++) * bscale
+ 0.5F
);
379 a
= (GLint
) ( (GLfloat
) (*acc
++) * ascale
+ 0.5F
);
380 rgba
[i
][RCOMP
] = CLAMP( r
, 0, 255 );
381 rgba
[i
][GCOMP
] = CLAMP( g
, 0, 255 );
382 rgba
[i
][BCOMP
] = CLAMP( b
, 0, 255 );
383 rgba
[i
][ACOMP
] = CLAMP( a
, 0, 255 );
385 if (ctx
->Color
.SWmasking
) {
386 gl_mask_rgba_span( ctx
, width
, xpos
, ypos
, rgba
);
388 (*ctx
->Driver
.WriteRGBASpan
)( ctx
, width
, xpos
, ypos
,
389 (const GLubyte (*)[4])rgba
, NULL
);
396 gl_error( ctx
, GL_INVALID_ENUM
, "glAccum" );
403 * Clear the accumulation Buffer.
405 void gl_clear_accum_buffer( GLcontext
*ctx
)
410 if (ctx
->Visual
->AccumBits
==0) {
411 /* No accumulation buffer! */
415 if (sizeof(GLaccum
)==1) {
418 else if (sizeof(GLaccum
)==2) {
422 /* sizeof(GLaccum) > 2 (Cray) */
423 acc_scale
= (float) SHRT_MAX
;
426 /* number of pixels */
427 buffersize
= ctx
->Buffer
->Width
* ctx
->Buffer
->Height
;
429 if (!ctx
->Buffer
->Accum
) {
430 /* try to alloc accumulation buffer */
431 ctx
->Buffer
->Accum
= (GLaccum
*)
432 MALLOC( buffersize
* 4 * sizeof(GLaccum
) );
435 if (ctx
->Buffer
->Accum
) {
436 if (ctx
->Scissor
.Enabled
) {
437 /* Limit clear to scissor box */
442 r
= (GLaccum
) (ctx
->Accum
.ClearColor
[0] * acc_scale
);
443 g
= (GLaccum
) (ctx
->Accum
.ClearColor
[1] * acc_scale
);
444 b
= (GLaccum
) (ctx
->Accum
.ClearColor
[2] * acc_scale
);
445 a
= (GLaccum
) (ctx
->Accum
.ClearColor
[3] * acc_scale
);
446 /* size of region to clear */
447 width
= 4 * (ctx
->Buffer
->Xmax
- ctx
->Buffer
->Xmin
+ 1);
448 height
= ctx
->Buffer
->Ymax
- ctx
->Buffer
->Ymin
+ 1;
449 /* ptr to first element to clear */
450 row
= ctx
->Buffer
->Accum
451 + 4 * (ctx
->Buffer
->Ymin
* ctx
->Buffer
->Width
452 + ctx
->Buffer
->Xmin
);
453 for (j
=0;j
<height
;j
++) {
454 for (i
=0;i
<width
;i
+=4) {
460 row
+= 4 * ctx
->Buffer
->Width
;
464 /* clear whole buffer */
465 if (ctx
->Accum
.ClearColor
[0]==0.0 &&
466 ctx
->Accum
.ClearColor
[1]==0.0 &&
467 ctx
->Accum
.ClearColor
[2]==0.0 &&
468 ctx
->Accum
.ClearColor
[3]==0.0) {
470 MEMSET( ctx
->Buffer
->Accum
, 0, buffersize
* 4 * sizeof(GLaccum
) );
474 GLaccum
*acc
, r
, g
, b
, a
;
477 acc
= ctx
->Buffer
->Accum
;
478 r
= (GLaccum
) (ctx
->Accum
.ClearColor
[0] * acc_scale
);
479 g
= (GLaccum
) (ctx
->Accum
.ClearColor
[1] * acc_scale
);
480 b
= (GLaccum
) (ctx
->Accum
.ClearColor
[2] * acc_scale
);
481 a
= (GLaccum
) (ctx
->Accum
.ClearColor
[3] * acc_scale
);
482 for (i
=0;i
<buffersize
;i
++) {
491 /* update optimized accum state vars */
492 if (ctx
->Accum
.ClearColor
[0] == 0.0 && ctx
->Accum
.ClearColor
[1] == 0.0 &&
493 ctx
->Accum
.ClearColor
[2] == 0.0 && ctx
->Accum
.ClearColor
[3] == 0.0) {
494 ctx
->IntegerAccumMode
= GL_TRUE
;
495 ctx
->IntegerAccumScaler
= 0.0; /* denotes empty accum buffer */
498 ctx
->IntegerAccumMode
= GL_FALSE
;