1 /* $Id: accum.c,v 1.9 1999/11/03 19:27:41 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"
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.
77 #define USE_OPTIMIZED_ACCUM /* enable the optimization */
81 void gl_alloc_accum_buffer( GLcontext
*ctx
)
85 if (ctx
->Buffer
->Accum
) {
86 FREE( ctx
->Buffer
->Accum
);
87 ctx
->Buffer
->Accum
= NULL
;
90 /* allocate accumulation buffer if not already present */
91 n
= ctx
->Buffer
->Width
* ctx
->Buffer
->Height
* 4 * sizeof(GLaccum
);
92 ctx
->Buffer
->Accum
= (GLaccum
*) MALLOC( n
);
93 if (!ctx
->Buffer
->Accum
) {
94 /* unable to setup accumulation buffer */
95 gl_error( ctx
, GL_OUT_OF_MEMORY
, "glAccum" );
97 #ifdef USE_OPTIMIZED_ACCUM
98 ctx
->IntegerAccumMode
= GL_TRUE
;
100 ctx
->IntegerAccumMode
= GL_FALSE
;
102 ctx
->IntegerAccumScaler
= 0.0;
107 void gl_ClearAccum( GLcontext
*ctx
,
108 GLfloat red
, GLfloat green
, GLfloat blue
, GLfloat alpha
)
110 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glAccum");
112 ctx
->Accum
.ClearColor
[0] = CLAMP( red
, -1.0, 1.0 );
113 ctx
->Accum
.ClearColor
[1] = CLAMP( green
, -1.0, 1.0 );
114 ctx
->Accum
.ClearColor
[2] = CLAMP( blue
, -1.0, 1.0 );
115 ctx
->Accum
.ClearColor
[3] = CLAMP( alpha
, -1.0, 1.0 );
121 * This is called when we fall out of optimized/unscaled accum buffer mode.
122 * That is, we convert each unscaled accum buffer value into a scaled value
123 * representing the range[-1, 1].
125 static void rescale_accum( GLcontext
*ctx
)
127 const GLuint n
= ctx
->Buffer
->Width
* ctx
->Buffer
->Height
* 4;
128 const GLfloat fChanMax
= (1 << (sizeof(GLchan
) * 8)) - 1;
129 const GLfloat s
= ctx
->IntegerAccumScaler
* (32767.0 / fChanMax
);
130 GLaccum
*accum
= ctx
->Buffer
->Accum
;
133 assert(ctx
->IntegerAccumMode
);
136 for (i
= 0; i
< n
; i
++) {
137 accum
[i
] = (GLaccum
) (accum
[i
] * s
);
140 ctx
->IntegerAccumMode
= GL_FALSE
;
145 void gl_Accum( GLcontext
*ctx
, GLenum op
, GLfloat value
)
147 GLuint xpos
, ypos
, width
, height
, width4
;
149 GLubyte rgba
[MAX_WIDTH
][4];
150 const GLint iChanMax
= (1 << (sizeof(GLchan
) * 8)) - 1;
151 const GLfloat fChanMax
= (1 << (sizeof(GLchan
) * 8)) - 1;
153 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glAccum");
155 if (ctx
->Visual
->AccumBits
==0 || !ctx
->Buffer
->Accum
) {
156 /* No accumulation buffer! */
157 gl_warning(ctx
, "Calling glAccum() without an accumulation buffer");
161 if (sizeof(GLaccum
)==1) {
164 else if (sizeof(GLaccum
)==2) {
168 /* sizeof(GLaccum) > 2 (Cray) */
169 acc_scale
= (float) SHRT_MAX
;
173 gl_update_state( ctx
);
175 /* Determine region to operate upon. */
176 if (ctx
->Scissor
.Enabled
) {
177 xpos
= ctx
->Scissor
.X
;
178 ypos
= ctx
->Scissor
.Y
;
179 width
= ctx
->Scissor
.Width
;
180 height
= ctx
->Scissor
.Height
;
186 width
= ctx
->Buffer
->Width
;
187 height
= ctx
->Buffer
->Height
;
195 const GLaccum intVal
= (GLaccum
) (value
* acc_scale
);
197 /* Leave optimized accum buffer mode */
198 if (ctx
->IntegerAccumMode
)
200 for (j
= 0; j
< height
; j
++) {
201 GLaccum
* acc
= ctx
->Buffer
->Accum
+ ypos
* width4
+ 4 * xpos
;
203 for (i
= 0; i
< width4
; i
++) {
214 /* Leave optimized accum buffer mode */
215 if (ctx
->IntegerAccumMode
)
217 for (j
= 0; j
< height
; j
++) {
218 GLaccum
*acc
= ctx
->Buffer
->Accum
+ ypos
* width4
+ 4 * xpos
;
220 for (i
= 0; i
< width4
; i
++) {
221 acc
[i
] = (GLaccum
) ( (GLfloat
) acc
[i
] * value
);
229 (void) (*ctx
->Driver
.SetBuffer
)( ctx
, ctx
->Pixel
.DriverReadBuffer
);
231 /* May have to leave optimized accum buffer mode */
232 if (ctx
->IntegerAccumScaler
== 0.0 && value
> 0.0 && value
<= 1.0)
233 ctx
->IntegerAccumScaler
= value
;
234 if (ctx
->IntegerAccumMode
&& value
!= ctx
->IntegerAccumScaler
)
237 if (ctx
->IntegerAccumMode
) {
238 /* simply add integer color values into accum buffer */
240 GLaccum
*acc
= ctx
->Buffer
->Accum
+ ypos
* width4
+ xpos
* 4;
241 assert(ctx
->IntegerAccumScaler
> 0.0);
242 assert(ctx
->IntegerAccumScaler
<= 1.0);
243 for (j
= 0; j
< height
; j
++) {
246 gl_read_rgba_span(ctx
, width
, xpos
, ypos
, rgba
);
247 for (i
= i4
= 0; i
< width
; i
++, i4
+=4) {
248 acc
[i4
+0] += rgba
[i
][RCOMP
];
249 acc
[i4
+1] += rgba
[i
][GCOMP
];
250 acc
[i4
+2] += rgba
[i
][BCOMP
];
251 acc
[i4
+3] += rgba
[i
][ACOMP
];
258 /* scaled integer accum buffer */
259 const GLfloat rscale
= value
* acc_scale
/ fChanMax
;
260 const GLfloat gscale
= value
* acc_scale
/ fChanMax
;
261 const GLfloat bscale
= value
* acc_scale
/ fChanMax
;
262 const GLfloat ascale
= value
* acc_scale
/ fChanMax
;
264 for (j
=0;j
<height
;j
++) {
265 GLaccum
*acc
= ctx
->Buffer
->Accum
+ ypos
* width4
+ xpos
* 4;
267 gl_read_rgba_span(ctx
, width
, xpos
, ypos
, rgba
);
268 for (i
=0;i
<width
;i
++) {
269 *acc
+= (GLaccum
) ( (GLfloat
) rgba
[i
][RCOMP
] * rscale
); acc
++;
270 *acc
+= (GLaccum
) ( (GLfloat
) rgba
[i
][GCOMP
] * gscale
); acc
++;
271 *acc
+= (GLaccum
) ( (GLfloat
) rgba
[i
][BCOMP
] * bscale
); acc
++;
272 *acc
+= (GLaccum
) ( (GLfloat
) rgba
[i
][ACOMP
] * ascale
); acc
++;
277 (void) (*ctx
->Driver
.SetBuffer
)( ctx
, ctx
->Color
.DriverDrawBuffer
);
281 (void) (*ctx
->Driver
.SetBuffer
)( ctx
, ctx
->Pixel
.DriverReadBuffer
);
283 /* This is a change to go into optimized accum buffer mode */
284 if (value
> 0.0 && value
<= 1.0) {
285 #ifdef USE_OPTIMIZED_ACCUM
286 ctx
->IntegerAccumMode
= GL_TRUE
;
288 ctx
->IntegerAccumMode
= GL_FALSE
;
290 ctx
->IntegerAccumScaler
= value
;
293 ctx
->IntegerAccumMode
= GL_FALSE
;
294 ctx
->IntegerAccumScaler
= 0.0;
297 if (ctx
->IntegerAccumMode
) {
298 /* just copy values into accum buffer */
300 GLaccum
*acc
= ctx
->Buffer
->Accum
+ ypos
* width4
+ xpos
* 4;
301 assert(ctx
->IntegerAccumScaler
> 0.0);
302 assert(ctx
->IntegerAccumScaler
<= 1.0);
303 for (j
= 0; j
< height
; j
++) {
305 gl_read_rgba_span(ctx
, width
, xpos
, ypos
, rgba
);
306 for (i
= i4
= 0; i
< width
; i
++, i4
+= 4) {
307 acc
[i4
+0] = rgba
[i
][RCOMP
];
308 acc
[i4
+1] = rgba
[i
][GCOMP
];
309 acc
[i4
+2] = rgba
[i
][BCOMP
];
310 acc
[i4
+3] = rgba
[i
][ACOMP
];
317 /* scaled integer accum buffer */
318 const GLfloat rscale
= value
* acc_scale
/ fChanMax
;
319 const GLfloat gscale
= value
* acc_scale
/ fChanMax
;
320 const GLfloat bscale
= value
* acc_scale
/ fChanMax
;
321 const GLfloat ascale
= value
* acc_scale
/ fChanMax
;
322 const GLfloat d
= 3.0 / acc_scale
;
324 for (j
= 0; j
< height
; j
++) {
325 GLaccum
*acc
= ctx
->Buffer
->Accum
+ ypos
* width4
+ xpos
* 4;
326 gl_read_rgba_span(ctx
, width
, xpos
, ypos
, rgba
);
327 for (i
=0;i
<width
;i
++) {
328 *acc
++ = (GLaccum
) ((GLfloat
) rgba
[i
][RCOMP
] * rscale
+ d
);
329 *acc
++ = (GLaccum
) ((GLfloat
) rgba
[i
][GCOMP
] * gscale
+ d
);
330 *acc
++ = (GLaccum
) ((GLfloat
) rgba
[i
][BCOMP
] * bscale
+ d
);
331 *acc
++ = (GLaccum
) ((GLfloat
) rgba
[i
][ACOMP
] * ascale
+ d
);
336 (void) (*ctx
->Driver
.SetBuffer
)( ctx
, ctx
->Color
.DriverDrawBuffer
);
340 /* May have to leave optimized accum buffer mode */
341 if (ctx
->IntegerAccumMode
&& value
!= 1.0)
344 if (ctx
->IntegerAccumMode
) {
345 /* build lookup table to avoid many floating point multiplies */
346 const GLfloat mult
= ctx
->IntegerAccumScaler
;
347 static GLchan multTable
[32768];
348 static GLfloat prevMult
= 0.0;
350 const GLint max
= 256 / mult
;
351 if (mult
!= prevMult
) {
352 assert(max
<= 32768);
353 for (j
= 0; j
< max
; j
++)
354 multTable
[j
] = (GLint
) ((GLfloat
) j
* mult
+ 0.5F
);
358 assert(ctx
->IntegerAccumScaler
> 0.0);
359 assert(ctx
->IntegerAccumScaler
<= 1.0);
360 for (j
= 0; j
< height
; j
++) {
361 const GLaccum
*acc
= ctx
->Buffer
->Accum
+ ypos
* width4
+ xpos
*4;
363 for (i
= i4
= 0; i
< width
; i
++, i4
+= 4) {
364 ASSERT(acc
[i4
+0] < max
);
365 ASSERT(acc
[i4
+1] < max
);
366 ASSERT(acc
[i4
+2] < max
);
367 ASSERT(acc
[i4
+3] < max
);
368 rgba
[i
][RCOMP
] = multTable
[acc
[i4
+0]];
369 rgba
[i
][GCOMP
] = multTable
[acc
[i4
+1]];
370 rgba
[i
][BCOMP
] = multTable
[acc
[i4
+2]];
371 rgba
[i
][ACOMP
] = multTable
[acc
[i4
+3]];
373 if (ctx
->Color
.SWmasking
) {
374 gl_mask_rgba_span( ctx
, width
, xpos
, ypos
, rgba
);
376 (*ctx
->Driver
.WriteRGBASpan
)( ctx
, width
, xpos
, ypos
,
377 (const GLubyte (*)[4])rgba
, NULL
);
382 const GLfloat rscale
= value
/ acc_scale
* fChanMax
;
383 const GLfloat gscale
= value
/ acc_scale
* fChanMax
;
384 const GLfloat bscale
= value
/ acc_scale
* fChanMax
;
385 const GLfloat ascale
= value
/ acc_scale
* fChanMax
;
387 for (j
=0;j
<height
;j
++) {
388 const GLaccum
*acc
= ctx
->Buffer
->Accum
+ ypos
* width4
+ xpos
*4;
389 for (i
=0;i
<width
;i
++) {
391 r
= (GLint
) ( (GLfloat
) (*acc
++) * rscale
+ 0.5F
);
392 g
= (GLint
) ( (GLfloat
) (*acc
++) * gscale
+ 0.5F
);
393 b
= (GLint
) ( (GLfloat
) (*acc
++) * bscale
+ 0.5F
);
394 a
= (GLint
) ( (GLfloat
) (*acc
++) * ascale
+ 0.5F
);
395 rgba
[i
][RCOMP
] = CLAMP( r
, 0, iChanMax
);
396 rgba
[i
][GCOMP
] = CLAMP( g
, 0, iChanMax
);
397 rgba
[i
][BCOMP
] = CLAMP( b
, 0, iChanMax
);
398 rgba
[i
][ACOMP
] = CLAMP( a
, 0, iChanMax
);
400 if (ctx
->Color
.SWmasking
) {
401 gl_mask_rgba_span( ctx
, width
, xpos
, ypos
, rgba
);
403 (*ctx
->Driver
.WriteRGBASpan
)( ctx
, width
, xpos
, ypos
,
404 (const GLubyte (*)[4])rgba
, NULL
);
411 gl_error( ctx
, GL_INVALID_ENUM
, "glAccum" );
418 * Clear the accumulation Buffer.
420 void gl_clear_accum_buffer( GLcontext
*ctx
)
425 if (ctx
->Visual
->AccumBits
==0) {
426 /* No accumulation buffer! */
430 if (sizeof(GLaccum
)==1) {
433 else if (sizeof(GLaccum
)==2) {
437 /* sizeof(GLaccum) > 2 (Cray) */
438 acc_scale
= (float) SHRT_MAX
;
441 /* number of pixels */
442 buffersize
= ctx
->Buffer
->Width
* ctx
->Buffer
->Height
;
444 if (!ctx
->Buffer
->Accum
) {
445 /* try to alloc accumulation buffer */
446 ctx
->Buffer
->Accum
= (GLaccum
*)
447 MALLOC( buffersize
* 4 * sizeof(GLaccum
) );
450 if (ctx
->Buffer
->Accum
) {
451 if (ctx
->Scissor
.Enabled
) {
452 /* Limit clear to scissor box */
457 r
= (GLaccum
) (ctx
->Accum
.ClearColor
[0] * acc_scale
);
458 g
= (GLaccum
) (ctx
->Accum
.ClearColor
[1] * acc_scale
);
459 b
= (GLaccum
) (ctx
->Accum
.ClearColor
[2] * acc_scale
);
460 a
= (GLaccum
) (ctx
->Accum
.ClearColor
[3] * acc_scale
);
461 /* size of region to clear */
462 width
= 4 * (ctx
->Buffer
->Xmax
- ctx
->Buffer
->Xmin
+ 1);
463 height
= ctx
->Buffer
->Ymax
- ctx
->Buffer
->Ymin
+ 1;
464 /* ptr to first element to clear */
465 row
= ctx
->Buffer
->Accum
466 + 4 * (ctx
->Buffer
->Ymin
* ctx
->Buffer
->Width
467 + ctx
->Buffer
->Xmin
);
468 for (j
=0;j
<height
;j
++) {
469 for (i
=0;i
<width
;i
+=4) {
475 row
+= 4 * ctx
->Buffer
->Width
;
479 /* clear whole buffer */
480 if (ctx
->Accum
.ClearColor
[0]==0.0 &&
481 ctx
->Accum
.ClearColor
[1]==0.0 &&
482 ctx
->Accum
.ClearColor
[2]==0.0 &&
483 ctx
->Accum
.ClearColor
[3]==0.0) {
485 MEMSET( ctx
->Buffer
->Accum
, 0, buffersize
* 4 * sizeof(GLaccum
) );
489 GLaccum
*acc
, r
, g
, b
, a
;
492 acc
= ctx
->Buffer
->Accum
;
493 r
= (GLaccum
) (ctx
->Accum
.ClearColor
[0] * acc_scale
);
494 g
= (GLaccum
) (ctx
->Accum
.ClearColor
[1] * acc_scale
);
495 b
= (GLaccum
) (ctx
->Accum
.ClearColor
[2] * acc_scale
);
496 a
= (GLaccum
) (ctx
->Accum
.ClearColor
[3] * acc_scale
);
497 for (i
=0;i
<buffersize
;i
++) {
506 /* update optimized accum state vars */
507 if (ctx
->Accum
.ClearColor
[0] == 0.0 && ctx
->Accum
.ClearColor
[1] == 0.0 &&
508 ctx
->Accum
.ClearColor
[2] == 0.0 && ctx
->Accum
.ClearColor
[3] == 0.0) {
509 #ifdef USE_OPTIMIZED_ACCUM
510 ctx
->IntegerAccumMode
= GL_TRUE
;
512 ctx
->IntegerAccumMode
= GL_FALSE
;
514 ctx
->IntegerAccumScaler
= 0.0; /* denotes empty accum buffer */
517 ctx
->IntegerAccumMode
= GL_FALSE
;