1 /* $Id: s_accum.c,v 1.1 2000/10/31 18:00:04 keithw 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.
33 #include "s_masking.h"
38 * Accumulation buffer notes
40 * Normally, accumulation buffer values are GLshorts with values in
41 * [-32767, 32767] which represent floating point colors in [-1, 1],
42 * as suggested by the OpenGL specification.
44 * We optimize for the common case used for full-scene antialiasing:
45 * // start with accum buffer cleared to zero
46 * glAccum(GL_LOAD, w); // or GL_ACCUM the first image
47 * glAccum(GL_ACCUM, w);
49 * glAccum(GL_ACCUM, w);
50 * glAccum(GL_RETURN, 1.0);
51 * That is, we start with an empty accumulation buffer and accumulate
52 * n images, each with weight w = 1/n.
53 * In this scenario, we can simply store unscaled integer values in
54 * the accum buffer instead of scaled integers. We'll also keep track
55 * of the w value so when we do GL_RETURN we simply divide the accumulated
57 * This lets us avoid _many_ int->float->int conversions.
62 #define USE_OPTIMIZED_ACCUM /* enable the optimization */
68 _mesa_alloc_accum_buffer( GLcontext
*ctx
)
72 if (ctx
->DrawBuffer
->Accum
) {
73 FREE( ctx
->DrawBuffer
->Accum
);
74 ctx
->DrawBuffer
->Accum
= NULL
;
77 /* allocate accumulation buffer if not already present */
78 n
= ctx
->DrawBuffer
->Width
* ctx
->DrawBuffer
->Height
* 4 * sizeof(GLaccum
);
79 ctx
->DrawBuffer
->Accum
= (GLaccum
*) MALLOC( n
);
80 if (!ctx
->DrawBuffer
->Accum
) {
81 /* unable to setup accumulation buffer */
82 gl_error( ctx
, GL_OUT_OF_MEMORY
, "glAccum" );
84 #ifdef USE_OPTIMIZED_ACCUM
85 ctx
->IntegerAccumMode
= GL_TRUE
;
87 ctx
->IntegerAccumMode
= GL_FALSE
;
89 ctx
->IntegerAccumScaler
= 0.0;
98 * This is called when we fall out of optimized/unscaled accum buffer mode.
99 * That is, we convert each unscaled accum buffer value into a scaled value
100 * representing the range[-1, 1].
102 static void rescale_accum( GLcontext
*ctx
)
104 const GLuint n
= ctx
->DrawBuffer
->Width
* ctx
->DrawBuffer
->Height
* 4;
105 const GLfloat fChanMax
= (1 << (sizeof(GLchan
) * 8)) - 1;
106 const GLfloat s
= ctx
->IntegerAccumScaler
* (32767.0 / fChanMax
);
107 GLaccum
*accum
= ctx
->DrawBuffer
->Accum
;
110 assert(ctx
->IntegerAccumMode
);
113 for (i
= 0; i
< n
; i
++) {
114 accum
[i
] = (GLaccum
) (accum
[i
] * s
);
117 ctx
->IntegerAccumMode
= GL_FALSE
;
123 _swrast_Accum( GLcontext
*ctx
, GLenum op
, GLfloat value
,
124 GLint xpos
, GLint ypos
,
125 GLint width
, GLint height
)
130 GLchan rgba
[MAX_WIDTH
][4];
131 const GLuint colorMask
= *((GLuint
*) &ctx
->Color
.ColorMask
);
132 const GLint iChanMax
= (1 << (sizeof(GLchan
) * 8)) - 1;
133 const GLfloat fChanMax
= (1 << (sizeof(GLchan
) * 8)) - 1;
136 if (!ctx
->DrawBuffer
->Accum
) {
137 _mesa_warning(ctx
, "Calling glAccum() without an accumulation buffer (low memory?)");
141 if (sizeof(GLaccum
)==1) {
144 else if (sizeof(GLaccum
)==2) {
148 /* sizeof(GLaccum) > 2 (Cray) */
149 acc_scale
= (float) SHRT_MAX
;
157 const GLaccum intVal
= (GLaccum
) (value
* acc_scale
);
159 /* Leave optimized accum buffer mode */
160 if (ctx
->IntegerAccumMode
)
162 for (j
= 0; j
< height
; j
++) {
163 GLaccum
* acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ 4 * xpos
;
165 for (i
= 0; i
< width4
; i
++) {
176 /* Leave optimized accum buffer mode */
177 if (ctx
->IntegerAccumMode
)
179 for (j
= 0; j
< height
; j
++) {
180 GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ 4 * xpos
;
182 for (i
= 0; i
< width4
; i
++) {
183 acc
[i
] = (GLaccum
) ( (GLfloat
) acc
[i
] * value
);
194 (*ctx
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
195 ctx
->Pixel
.DriverReadBuffer
);
197 /* May have to leave optimized accum buffer mode */
198 if (ctx
->IntegerAccumScaler
== 0.0 && value
> 0.0 && value
<= 1.0)
199 ctx
->IntegerAccumScaler
= value
;
200 if (ctx
->IntegerAccumMode
&& value
!= ctx
->IntegerAccumScaler
)
205 if (ctx
->IntegerAccumMode
) {
206 /* simply add integer color values into accum buffer */
208 GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ xpos
* 4;
209 assert(ctx
->IntegerAccumScaler
> 0.0);
210 assert(ctx
->IntegerAccumScaler
<= 1.0);
211 for (j
= 0; j
< height
; j
++) {
214 gl_read_rgba_span(ctx
, ctx
->DrawBuffer
, width
, xpos
, ypos
, rgba
);
215 for (i
= i4
= 0; i
< width
; i
++, i4
+=4) {
216 acc
[i4
+0] += rgba
[i
][RCOMP
];
217 acc
[i4
+1] += rgba
[i
][GCOMP
];
218 acc
[i4
+2] += rgba
[i
][BCOMP
];
219 acc
[i4
+3] += rgba
[i
][ACOMP
];
226 /* scaled integer accum buffer */
227 const GLfloat rscale
= value
* acc_scale
/ fChanMax
;
228 const GLfloat gscale
= value
* acc_scale
/ fChanMax
;
229 const GLfloat bscale
= value
* acc_scale
/ fChanMax
;
230 const GLfloat ascale
= value
* acc_scale
/ fChanMax
;
232 for (j
=0;j
<height
;j
++) {
233 GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ xpos
* 4;
235 gl_read_rgba_span(ctx
, ctx
->DrawBuffer
, width
, xpos
, ypos
, rgba
);
236 for (i
=0;i
<width
;i
++) {
237 *acc
+= (GLaccum
) ( (GLfloat
) rgba
[i
][RCOMP
] * rscale
); acc
++;
238 *acc
+= (GLaccum
) ( (GLfloat
) rgba
[i
][GCOMP
] * gscale
); acc
++;
239 *acc
+= (GLaccum
) ( (GLfloat
) rgba
[i
][BCOMP
] * bscale
); acc
++;
240 *acc
+= (GLaccum
) ( (GLfloat
) rgba
[i
][ACOMP
] * ascale
); acc
++;
245 /* restore read buffer = draw buffer (the default) */
246 (*ctx
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
247 ctx
->Color
.DriverDrawBuffer
);
252 (*ctx
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
253 ctx
->Pixel
.DriverReadBuffer
);
255 /* This is a change to go into optimized accum buffer mode */
256 if (value
> 0.0 && value
<= 1.0) {
257 #ifdef USE_OPTIMIZED_ACCUM
258 ctx
->IntegerAccumMode
= GL_TRUE
;
260 ctx
->IntegerAccumMode
= GL_FALSE
;
262 ctx
->IntegerAccumScaler
= value
;
265 ctx
->IntegerAccumMode
= GL_FALSE
;
266 ctx
->IntegerAccumScaler
= 0.0;
270 if (ctx
->IntegerAccumMode
) {
271 /* just copy values into accum buffer */
273 GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ xpos
* 4;
274 assert(ctx
->IntegerAccumScaler
> 0.0);
275 assert(ctx
->IntegerAccumScaler
<= 1.0);
276 for (j
= 0; j
< height
; j
++) {
278 gl_read_rgba_span(ctx
, ctx
->DrawBuffer
, width
, xpos
, ypos
, rgba
);
279 for (i
= i4
= 0; i
< width
; i
++, i4
+= 4) {
280 acc
[i4
+0] = rgba
[i
][RCOMP
];
281 acc
[i4
+1] = rgba
[i
][GCOMP
];
282 acc
[i4
+2] = rgba
[i
][BCOMP
];
283 acc
[i4
+3] = rgba
[i
][ACOMP
];
290 /* scaled integer accum buffer */
291 const GLfloat rscale
= value
* acc_scale
/ fChanMax
;
292 const GLfloat gscale
= value
* acc_scale
/ fChanMax
;
293 const GLfloat bscale
= value
* acc_scale
/ fChanMax
;
294 const GLfloat ascale
= value
* acc_scale
/ fChanMax
;
295 const GLfloat d
= 3.0 / acc_scale
;
297 for (j
= 0; j
< height
; j
++) {
298 GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ xpos
* 4;
299 gl_read_rgba_span(ctx
, ctx
->DrawBuffer
, width
, xpos
, ypos
, rgba
);
300 for (i
=0;i
<width
;i
++) {
301 *acc
++ = (GLaccum
) ((GLfloat
) rgba
[i
][RCOMP
] * rscale
+ d
);
302 *acc
++ = (GLaccum
) ((GLfloat
) rgba
[i
][GCOMP
] * gscale
+ d
);
303 *acc
++ = (GLaccum
) ((GLfloat
) rgba
[i
][BCOMP
] * bscale
+ d
);
304 *acc
++ = (GLaccum
) ((GLfloat
) rgba
[i
][ACOMP
] * ascale
+ d
);
310 /* restore read buffer = draw buffer (the default) */
311 (*ctx
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
312 ctx
->Color
.DriverDrawBuffer
);
317 /* May have to leave optimized accum buffer mode */
318 if (ctx
->IntegerAccumMode
&& value
!= 1.0)
322 if (ctx
->IntegerAccumMode
&& ctx
->IntegerAccumScaler
> 0) {
323 /* build lookup table to avoid many floating point multiplies */
324 static GLchan multTable
[32768];
325 static GLfloat prevMult
= 0.0;
326 const GLfloat mult
= ctx
->IntegerAccumScaler
;
327 const GLint max
= MIN2((GLint
) (256 / mult
), 32767);
329 if (mult
!= prevMult
) {
330 for (j
= 0; j
< max
; j
++)
331 multTable
[j
] = (GLint
) ((GLfloat
) j
* mult
+ 0.5F
);
335 assert(ctx
->IntegerAccumScaler
> 0.0);
336 assert(ctx
->IntegerAccumScaler
<= 1.0);
337 for (j
= 0; j
< height
; j
++) {
338 const GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ xpos
*4;
340 for (i
= i4
= 0; i
< width
; i
++, i4
+= 4) {
341 ASSERT(acc
[i4
+0] < max
);
342 ASSERT(acc
[i4
+1] < max
);
343 ASSERT(acc
[i4
+2] < max
);
344 ASSERT(acc
[i4
+3] < max
);
345 rgba
[i
][RCOMP
] = multTable
[acc
[i4
+0]];
346 rgba
[i
][GCOMP
] = multTable
[acc
[i4
+1]];
347 rgba
[i
][BCOMP
] = multTable
[acc
[i4
+2]];
348 rgba
[i
][ACOMP
] = multTable
[acc
[i4
+3]];
350 if (colorMask
!= 0xffffffff) {
351 _mesa_mask_rgba_span( ctx
, width
, xpos
, ypos
, rgba
);
353 (*ctx
->Driver
.WriteRGBASpan
)( ctx
, width
, xpos
, ypos
,
354 (const GLchan (*)[4])rgba
, NULL
);
359 const GLfloat rscale
= value
/ acc_scale
* fChanMax
;
360 const GLfloat gscale
= value
/ acc_scale
* fChanMax
;
361 const GLfloat bscale
= value
/ acc_scale
* fChanMax
;
362 const GLfloat ascale
= value
/ acc_scale
* fChanMax
;
364 for (j
=0;j
<height
;j
++) {
365 const GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ xpos
*4;
366 for (i
=0;i
<width
;i
++) {
368 r
= (GLint
) ( (GLfloat
) (*acc
++) * rscale
+ 0.5F
);
369 g
= (GLint
) ( (GLfloat
) (*acc
++) * gscale
+ 0.5F
);
370 b
= (GLint
) ( (GLfloat
) (*acc
++) * bscale
+ 0.5F
);
371 a
= (GLint
) ( (GLfloat
) (*acc
++) * ascale
+ 0.5F
);
372 rgba
[i
][RCOMP
] = CLAMP( r
, 0, iChanMax
);
373 rgba
[i
][GCOMP
] = CLAMP( g
, 0, iChanMax
);
374 rgba
[i
][BCOMP
] = CLAMP( b
, 0, iChanMax
);
375 rgba
[i
][ACOMP
] = CLAMP( a
, 0, iChanMax
);
377 if (colorMask
!= 0xffffffff) {
378 _mesa_mask_rgba_span( ctx
, width
, xpos
, ypos
, rgba
);
380 (*ctx
->Driver
.WriteRGBASpan
)( ctx
, width
, xpos
, ypos
,
381 (const GLchan (*)[4])rgba
, NULL
);
389 gl_error( ctx
, GL_INVALID_ENUM
, "glAccum" );
396 * Clear the accumulation Buffer.
399 _mesa_clear_accum_buffer( GLcontext
*ctx
)
404 if (ctx
->Visual
.AccumRedBits
==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
->DrawBuffer
->Width
* ctx
->DrawBuffer
->Height
;
423 if (!ctx
->DrawBuffer
->Accum
) {
424 /* try to alloc accumulation buffer */
425 ctx
->DrawBuffer
->Accum
= (GLaccum
*)
426 MALLOC( buffersize
* 4 * sizeof(GLaccum
) );
429 if (ctx
->DrawBuffer
->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
->DrawBuffer
->Xmax
- ctx
->DrawBuffer
->Xmin
);
442 height
= ctx
->DrawBuffer
->Ymax
- ctx
->DrawBuffer
->Ymin
;
443 /* ptr to first element to clear */
444 row
= ctx
->DrawBuffer
->Accum
445 + 4 * (ctx
->DrawBuffer
->Ymin
* ctx
->DrawBuffer
->Width
446 + ctx
->DrawBuffer
->Xmin
);
447 for (j
=0;j
<height
;j
++) {
448 for (i
=0;i
<width
;i
+=4) {
454 row
+= 4 * ctx
->DrawBuffer
->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 BZERO( ctx
->DrawBuffer
->Accum
, buffersize
* 4 * sizeof(GLaccum
) );
468 GLaccum
*acc
, r
, g
, b
, a
;
471 acc
= ctx
->DrawBuffer
->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 #ifdef USE_OPTIMIZED_ACCUM
489 ctx
->IntegerAccumMode
= GL_TRUE
;
491 ctx
->IntegerAccumMode
= GL_FALSE
;
493 ctx
->IntegerAccumScaler
= 0.0; /* denotes empty accum buffer */
496 ctx
->IntegerAccumMode
= GL_FALSE
;