1 /* $Id: accum.c,v 1.1 1999/08/19 00:55:41 jtg 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.
42 #include "GL/xf86glx.h"
48 * Accumulation buffer notes
50 * Normally, accumulation buffer values are GLshorts with values in
51 * [-32767, 32767] which represent floating point colors in [-1, 1],
52 * as suggested by the OpenGL specification.
54 * We optimize for the common case used for full-scene antialiasing:
55 * // start with accum buffer cleared to zero
56 * glAccum(GL_LOAD, w); // or GL_ACCUM the first image
57 * glAccum(GL_ACCUM, w);
59 * glAccum(GL_ACCUM, w);
60 * glAccum(GL_RETURN, 1.0);
61 * That is, we start with an empty accumulation buffer and accumulate
62 * n images, each with weight w = 1/n.
63 * In this scenario, we can simply store unscaled integer values in
64 * the accum buffer instead of scaled integers. We'll also keep track
65 * of the w value so when we do GL_RETURN we simply divide the accumulated
67 * This lets us avoid _many_ int->float->int conversions.
72 void gl_alloc_accum_buffer( GLcontext
*ctx
)
76 if (ctx
->Buffer
->Accum
) {
77 free( ctx
->Buffer
->Accum
);
78 ctx
->Buffer
->Accum
= NULL
;
81 /* allocate accumulation buffer if not already present */
82 n
= ctx
->Buffer
->Width
* ctx
->Buffer
->Height
* 4 * sizeof(GLaccum
);
83 ctx
->Buffer
->Accum
= (GLaccum
*) malloc( n
);
84 if (!ctx
->Buffer
->Accum
) {
85 /* unable to setup accumulation buffer */
86 gl_error( ctx
, GL_OUT_OF_MEMORY
, "glAccum" );
88 ctx
->IntegerAccumMode
= GL_TRUE
;
89 ctx
->IntegerAccumScaler
= 0.0;
94 void gl_ClearAccum( GLcontext
*ctx
,
95 GLfloat red
, GLfloat green
, GLfloat blue
, GLfloat alpha
)
97 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glAccum");
99 ctx
->Accum
.ClearColor
[0] = CLAMP( red
, -1.0, 1.0 );
100 ctx
->Accum
.ClearColor
[1] = CLAMP( green
, -1.0, 1.0 );
101 ctx
->Accum
.ClearColor
[2] = CLAMP( blue
, -1.0, 1.0 );
102 ctx
->Accum
.ClearColor
[3] = CLAMP( alpha
, -1.0, 1.0 );
108 * This is called when we fall out of optimized/unscaled accum buffer mode.
109 * That is, we convert each unscaled accum buffer value into a scaled value
110 * representing the range[-1, 1].
112 static void rescale_accum( GLcontext
*ctx
)
114 const GLuint n
= ctx
->Buffer
->Width
* ctx
->Buffer
->Height
* 4;
115 const GLfloat s
= ctx
->IntegerAccumScaler
* (32767.0 / 255.0);
116 GLaccum
*accum
= ctx
->Buffer
->Accum
;
119 assert(ctx
->IntegerAccumMode
);
120 assert(sizeof(GLchan
) == 1); /* if not true, 255.0 above must be fixed */
123 for (i
= 0; i
< n
; i
++) {
124 accum
[i
] = (GLaccum
) (accum
[i
] * s
);
127 ctx
->IntegerAccumMode
= GL_FALSE
;
132 void gl_Accum( GLcontext
*ctx
, GLenum op
, GLfloat value
)
134 GLuint xpos
, ypos
, width
, height
, width4
;
136 GLubyte rgba
[MAX_WIDTH
][4];
138 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glAccum");
140 if (ctx
->Visual
->AccumBits
==0 || !ctx
->Buffer
->Accum
) {
141 /* No accumulation buffer! */
142 gl_warning(ctx
, "Calling glAccum() without an accumulation buffer");
146 if (sizeof(GLaccum
)==1) {
149 else if (sizeof(GLaccum
)==2) {
153 /* sizeof(GLaccum) > 2 (Cray) */
154 acc_scale
= (float) SHRT_MAX
;
158 gl_update_state( ctx
);
160 /* Determine region to operate upon. */
161 if (ctx
->Scissor
.Enabled
) {
162 xpos
= ctx
->Scissor
.X
;
163 ypos
= ctx
->Scissor
.Y
;
164 width
= ctx
->Scissor
.Width
;
165 height
= ctx
->Scissor
.Height
;
171 width
= ctx
->Buffer
->Width
;
172 height
= ctx
->Buffer
->Height
;
180 const GLaccum intVal
= (GLaccum
) (value
* acc_scale
);
182 /* May have to leave optimized accum buffer mode */
183 if (ctx
->IntegerAccumMode
)
185 for (j
= 0; j
< height
; j
++) {
186 GLaccum
* acc
= ctx
->Buffer
->Accum
+ ypos
* width4
+ 4 * xpos
;
188 for (i
= 0; i
< width4
; i
++) {
199 /* May have to leave optimized accum buffer mode */
200 if (ctx
->IntegerAccumMode
)
202 for (j
= 0; j
< height
; j
++) {
203 GLaccum
*acc
= ctx
->Buffer
->Accum
+ ypos
* width4
+ 4 * xpos
;
205 for (i
= 0; i
< width4
; i
++) {
206 acc
[i
] = (GLaccum
) ( (GLfloat
) acc
[i
] * value
);
214 (void) (*ctx
->Driver
.SetBuffer
)( ctx
, ctx
->Pixel
.DriverReadBuffer
);
216 /* May have to leave optimized accum buffer mode */
217 if (ctx
->IntegerAccumScaler
== 0.0 && value
> 0.0 && value
<= 1.0)
218 ctx
->IntegerAccumScaler
= value
;
219 if (ctx
->IntegerAccumMode
&& value
!= ctx
->IntegerAccumScaler
)
222 if (ctx
->IntegerAccumMode
) {
223 /* simply add integer color values into accum buffer */
225 GLaccum
*acc
= ctx
->Buffer
->Accum
+ ypos
* width4
+ xpos
* 4;
226 assert(ctx
->IntegerAccumScaler
> 0.0);
227 assert(ctx
->IntegerAccumScaler
<= 1.0);
228 for (j
= 0; j
< height
; j
++) {
231 gl_read_rgba_span(ctx
, width
, xpos
, ypos
, rgba
);
232 for (i
= i4
= 0; i
< width
; i
++, i4
+=4) {
233 acc
[i4
+0] += rgba
[i
][RCOMP
];
234 acc
[i4
+1] += rgba
[i
][GCOMP
];
235 acc
[i4
+2] += rgba
[i
][BCOMP
];
236 acc
[i4
+3] += rgba
[i
][ACOMP
];
243 /* scaled integer accum buffer */
244 const GLfloat rscale
= value
* acc_scale
/ 255.0;
245 const GLfloat gscale
= value
* acc_scale
/ 255.0;
246 const GLfloat bscale
= value
* acc_scale
/ 255.0;
247 const GLfloat ascale
= value
* acc_scale
/ 255.0;
249 for (j
=0;j
<height
;j
++) {
250 GLaccum
*acc
= ctx
->Buffer
->Accum
+ ypos
* width4
+ xpos
* 4;
252 gl_read_rgba_span(ctx
, width
, xpos
, ypos
, rgba
);
253 for (i
=0;i
<width
;i
++) {
254 *acc
+= (GLaccum
) ( (GLfloat
) rgba
[i
][RCOMP
] * rscale
); acc
++;
255 *acc
+= (GLaccum
) ( (GLfloat
) rgba
[i
][GCOMP
] * gscale
); acc
++;
256 *acc
+= (GLaccum
) ( (GLfloat
) rgba
[i
][BCOMP
] * bscale
); acc
++;
257 *acc
+= (GLaccum
) ( (GLfloat
) rgba
[i
][ACOMP
] * ascale
); acc
++;
262 (void) (*ctx
->Driver
.SetBuffer
)( ctx
, ctx
->Color
.DriverDrawBuffer
);
266 (void) (*ctx
->Driver
.SetBuffer
)( ctx
, ctx
->Pixel
.DriverReadBuffer
);
268 /* This is a change to go into optimized accum buffer mode */
269 if (value
> 0.0 && value
<= 1.0) {
270 ctx
->IntegerAccumMode
= GL_TRUE
;
271 ctx
->IntegerAccumScaler
= value
;
274 ctx
->IntegerAccumMode
= GL_FALSE
;
275 ctx
->IntegerAccumScaler
= 0.0;
278 if (ctx
->IntegerAccumMode
) {
279 /* just copy values into accum buffer */
281 GLaccum
*acc
= ctx
->Buffer
->Accum
+ ypos
* width4
+ xpos
* 4;
282 assert(ctx
->IntegerAccumScaler
> 0.0);
283 assert(ctx
->IntegerAccumScaler
<= 1.0);
284 for (j
= 0; j
< height
; j
++) {
286 gl_read_rgba_span(ctx
, width
, xpos
, ypos
, rgba
);
287 for (i
= i4
= 0; i
< width
; i
++, i4
+= 4) {
288 acc
[i4
+0] = rgba
[i
][RCOMP
];
289 acc
[i4
+1] = rgba
[i
][GCOMP
];
290 acc
[i4
+2] = rgba
[i
][BCOMP
];
291 acc
[i4
+3] = rgba
[i
][ACOMP
];
298 /* scaled integer accum buffer */
299 const GLfloat rscale
= value
* acc_scale
/ 255.0;
300 const GLfloat gscale
= value
* acc_scale
/ 255.0;
301 const GLfloat bscale
= value
* acc_scale
/ 255.0;
302 const GLfloat ascale
= value
* acc_scale
/ 255.0;
304 for (j
= 0; j
< height
; j
++) {
305 GLaccum
*acc
= ctx
->Buffer
->Accum
+ ypos
* width4
+ xpos
* 4;
306 gl_read_rgba_span(ctx
, width
, xpos
, ypos
, rgba
);
307 for (i
=0;i
<width
;i
++) {
308 *acc
++ = (GLaccum
) ( (GLfloat
) rgba
[i
][RCOMP
] * rscale
);
309 *acc
++ = (GLaccum
) ( (GLfloat
) rgba
[i
][GCOMP
] * gscale
);
310 *acc
++ = (GLaccum
) ( (GLfloat
) rgba
[i
][BCOMP
] * bscale
);
311 *acc
++ = (GLaccum
) ( (GLfloat
) rgba
[i
][ACOMP
] * ascale
);
316 (void) (*ctx
->Driver
.SetBuffer
)( ctx
, ctx
->Color
.DriverDrawBuffer
);
320 /* May have to leave optimized accum buffer mode */
321 if (ctx
->IntegerAccumMode
&& value
!= 1.0)
324 if (ctx
->IntegerAccumMode
) {
325 /* build lookup table to avoid integer divides */
326 GLint divisor
= (GLint
) ((1.0F
/ ctx
->IntegerAccumScaler
) + 0.5F
);
327 static GLubyte divTable
[32768];
328 static GLint prevDivisor
= 0.0;
330 if (divisor
!= prevDivisor
) {
331 assert(divisor
* 256 <= 32768);
332 for (j
= 0; j
< divisor
* 256; j
++)
333 divTable
[j
] = j
/ divisor
;
334 prevDivisor
= divisor
;
337 assert(ctx
->IntegerAccumScaler
> 0.0);
338 assert(ctx
->IntegerAccumScaler
<= 1.0);
339 for (j
= 0; j
< height
; j
++) {
340 const GLaccum
*acc
= ctx
->Buffer
->Accum
+ ypos
* width4
+ xpos
*4;
342 for (i
= i4
= 0; i
< width
; i
++, i4
+= 4) {
343 ASSERT(acc
[i4
+0] < divisor
* 256);
344 ASSERT(acc
[i4
+1] < divisor
* 256);
345 ASSERT(acc
[i4
+2] < divisor
* 256);
346 ASSERT(acc
[i4
+3] < divisor
* 256);
347 rgba
[i
][RCOMP
] = divTable
[acc
[i4
+0]];
348 rgba
[i
][GCOMP
] = divTable
[acc
[i4
+1]];
349 rgba
[i
][BCOMP
] = divTable
[acc
[i4
+2]];
350 rgba
[i
][ACOMP
] = divTable
[acc
[i4
+3]];
352 if (ctx
->Color
.SWmasking
) {
353 gl_mask_rgba_span( ctx
, width
, xpos
, ypos
, rgba
);
355 (*ctx
->Driver
.WriteRGBASpan
)( ctx
, width
, xpos
, ypos
,
356 (const GLubyte (*)[4])rgba
, NULL
);
361 const GLfloat rscale
= value
/ acc_scale
* 255.0F
;
362 const GLfloat gscale
= value
/ acc_scale
* 255.0F
;
363 const GLfloat bscale
= value
/ acc_scale
* 255.0F
;
364 const GLfloat ascale
= value
/ acc_scale
* 255.0F
;
366 for (j
=0;j
<height
;j
++) {
367 const GLaccum
*acc
= ctx
->Buffer
->Accum
+ ypos
* width4
+ xpos
*4;
368 for (i
=0;i
<width
;i
++) {
370 r
= (GLint
) ( (GLfloat
) (*acc
++) * rscale
+ 0.5F
);
371 g
= (GLint
) ( (GLfloat
) (*acc
++) * gscale
+ 0.5F
);
372 b
= (GLint
) ( (GLfloat
) (*acc
++) * bscale
+ 0.5F
);
373 a
= (GLint
) ( (GLfloat
) (*acc
++) * ascale
+ 0.5F
);
374 rgba
[i
][RCOMP
] = CLAMP( r
, 0, 255 );
375 rgba
[i
][GCOMP
] = CLAMP( g
, 0, 255 );
376 rgba
[i
][BCOMP
] = CLAMP( b
, 0, 255 );
377 rgba
[i
][ACOMP
] = CLAMP( a
, 0, 255 );
379 if (ctx
->Color
.SWmasking
) {
380 gl_mask_rgba_span( ctx
, width
, xpos
, ypos
, rgba
);
382 (*ctx
->Driver
.WriteRGBASpan
)( ctx
, width
, xpos
, ypos
,
383 (const GLubyte (*)[4])rgba
, NULL
);
390 gl_error( ctx
, GL_INVALID_ENUM
, "glAccum" );
397 * Clear the accumulation Buffer.
399 void gl_clear_accum_buffer( GLcontext
*ctx
)
404 if (ctx
->Visual
->AccumBits
==0) {
405 /* No accumulation buffer! */
409 if (sizeof(GLaccum
)==1) {
412 else if (sizeof(GLaccum
)==2) {
416 /* sizeof(GLaccum) > 2 (Cray) */
417 acc_scale
= (float) SHRT_MAX
;
420 /* number of pixels */
421 buffersize
= ctx
->Buffer
->Width
* ctx
->Buffer
->Height
;
423 if (!ctx
->Buffer
->Accum
) {
424 /* try to alloc accumulation buffer */
425 ctx
->Buffer
->Accum
= (GLaccum
*)
426 malloc( buffersize
* 4 * sizeof(GLaccum
) );
429 if (ctx
->Buffer
->Accum
) {
430 if (ctx
->Scissor
.Enabled
) {
431 /* Limit clear to scissor box */
436 r
= (GLaccum
) (ctx
->Accum
.ClearColor
[0] * acc_scale
);
437 g
= (GLaccum
) (ctx
->Accum
.ClearColor
[1] * acc_scale
);
438 b
= (GLaccum
) (ctx
->Accum
.ClearColor
[2] * acc_scale
);
439 a
= (GLaccum
) (ctx
->Accum
.ClearColor
[3] * acc_scale
);
440 /* size of region to clear */
441 width
= 4 * (ctx
->Buffer
->Xmax
- ctx
->Buffer
->Xmin
+ 1);
442 height
= ctx
->Buffer
->Ymax
- ctx
->Buffer
->Ymin
+ 1;
443 /* ptr to first element to clear */
444 row
= ctx
->Buffer
->Accum
445 + 4 * (ctx
->Buffer
->Ymin
* ctx
->Buffer
->Width
446 + ctx
->Buffer
->Xmin
);
447 for (j
=0;j
<height
;j
++) {
448 for (i
=0;i
<width
;i
+=4) {
454 row
+= 4 * ctx
->Buffer
->Width
;
458 /* clear whole buffer */
459 if (ctx
->Accum
.ClearColor
[0]==0.0 &&
460 ctx
->Accum
.ClearColor
[1]==0.0 &&
461 ctx
->Accum
.ClearColor
[2]==0.0 &&
462 ctx
->Accum
.ClearColor
[3]==0.0) {
464 MEMSET( ctx
->Buffer
->Accum
, 0, buffersize
* 4 * sizeof(GLaccum
) );
468 GLaccum
*acc
, r
, g
, b
, a
;
471 acc
= ctx
->Buffer
->Accum
;
472 r
= (GLaccum
) (ctx
->Accum
.ClearColor
[0] * acc_scale
);
473 g
= (GLaccum
) (ctx
->Accum
.ClearColor
[1] * acc_scale
);
474 b
= (GLaccum
) (ctx
->Accum
.ClearColor
[2] * acc_scale
);
475 a
= (GLaccum
) (ctx
->Accum
.ClearColor
[3] * acc_scale
);
476 for (i
=0;i
<buffersize
;i
++) {
485 /* update optimized accum state vars */
486 if (ctx
->Accum
.ClearColor
[0] == 0.0 && ctx
->Accum
.ClearColor
[1] == 0.0 &&
487 ctx
->Accum
.ClearColor
[2] == 0.0 && ctx
->Accum
.ClearColor
[3] == 0.0) {
488 ctx
->IntegerAccumMode
= GL_TRUE
;
489 ctx
->IntegerAccumScaler
= 0.0; /* denotes empty accum buffer */
492 ctx
->IntegerAccumMode
= GL_FALSE
;