1 /* $Id: accum.c,v 1.12 1999/11/24 18:48:30 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.
42 * Accumulation buffer notes
44 * Normally, accumulation buffer values are GLshorts with values in
45 * [-32767, 32767] which represent floating point colors in [-1, 1],
46 * as suggested by the OpenGL specification.
48 * We optimize for the common case used for full-scene antialiasing:
49 * // start with accum buffer cleared to zero
50 * glAccum(GL_LOAD, w); // or GL_ACCUM the first image
51 * glAccum(GL_ACCUM, w);
53 * glAccum(GL_ACCUM, w);
54 * glAccum(GL_RETURN, 1.0);
55 * That is, we start with an empty accumulation buffer and accumulate
56 * n images, each with weight w = 1/n.
57 * In this scenario, we can simply store unscaled integer values in
58 * the accum buffer instead of scaled integers. We'll also keep track
59 * of the w value so when we do GL_RETURN we simply divide the accumulated
61 * This lets us avoid _many_ int->float->int conversions.
65 #define USE_OPTIMIZED_ACCUM /* enable the optimization */
69 void gl_alloc_accum_buffer( GLcontext
*ctx
)
73 if (ctx
->DrawBuffer
->Accum
) {
74 FREE( ctx
->DrawBuffer
->Accum
);
75 ctx
->DrawBuffer
->Accum
= NULL
;
78 /* allocate accumulation buffer if not already present */
79 n
= ctx
->DrawBuffer
->Width
* ctx
->DrawBuffer
->Height
* 4 * sizeof(GLaccum
);
80 ctx
->DrawBuffer
->Accum
= (GLaccum
*) MALLOC( n
);
81 if (!ctx
->DrawBuffer
->Accum
) {
82 /* unable to setup accumulation buffer */
83 gl_error( ctx
, GL_OUT_OF_MEMORY
, "glAccum" );
85 #ifdef USE_OPTIMIZED_ACCUM
86 ctx
->IntegerAccumMode
= GL_TRUE
;
88 ctx
->IntegerAccumMode
= GL_FALSE
;
90 ctx
->IntegerAccumScaler
= 0.0;
96 _mesa_ClearAccum( GLfloat red
, GLfloat green
, GLfloat blue
, GLfloat alpha
)
98 GET_CURRENT_CONTEXT(ctx
);
99 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glAccum");
101 ctx
->Accum
.ClearColor
[0] = CLAMP( red
, -1.0, 1.0 );
102 ctx
->Accum
.ClearColor
[1] = CLAMP( green
, -1.0, 1.0 );
103 ctx
->Accum
.ClearColor
[2] = CLAMP( blue
, -1.0, 1.0 );
104 ctx
->Accum
.ClearColor
[3] = CLAMP( alpha
, -1.0, 1.0 );
110 * This is called when we fall out of optimized/unscaled accum buffer mode.
111 * That is, we convert each unscaled accum buffer value into a scaled value
112 * representing the range[-1, 1].
114 static void rescale_accum( GLcontext
*ctx
)
116 const GLuint n
= ctx
->DrawBuffer
->Width
* ctx
->DrawBuffer
->Height
* 4;
117 const GLfloat fChanMax
= (1 << (sizeof(GLchan
) * 8)) - 1;
118 const GLfloat s
= ctx
->IntegerAccumScaler
* (32767.0 / fChanMax
);
119 GLaccum
*accum
= ctx
->DrawBuffer
->Accum
;
122 assert(ctx
->IntegerAccumMode
);
125 for (i
= 0; i
< n
; i
++) {
126 accum
[i
] = (GLaccum
) (accum
[i
] * s
);
129 ctx
->IntegerAccumMode
= GL_FALSE
;
135 _mesa_Accum( GLenum op
, GLfloat value
)
137 GET_CURRENT_CONTEXT(ctx
);
138 GLuint xpos
, ypos
, width
, height
, width4
;
140 GLubyte rgba
[MAX_WIDTH
][4];
141 const GLint iChanMax
= (1 << (sizeof(GLchan
) * 8)) - 1;
142 const GLfloat fChanMax
= (1 << (sizeof(GLchan
) * 8)) - 1;
144 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glAccum");
146 if (ctx
->Visual
->AccumBits
== 0 || ctx
->DrawBuffer
!= ctx
->ReadBuffer
) {
147 gl_error(ctx
, GL_INVALID_OPERATION
, "glAccum");
151 if (!ctx
->DrawBuffer
->Accum
) {
152 gl_warning(ctx
, "Calling glAccum() without an accumulation buffer (low memory?)");
156 if (sizeof(GLaccum
)==1) {
159 else if (sizeof(GLaccum
)==2) {
163 /* sizeof(GLaccum) > 2 (Cray) */
164 acc_scale
= (float) SHRT_MAX
;
168 gl_update_state( ctx
);
170 /* Determine region to operate upon. */
171 if (ctx
->Scissor
.Enabled
) {
172 xpos
= ctx
->Scissor
.X
;
173 ypos
= ctx
->Scissor
.Y
;
174 width
= ctx
->Scissor
.Width
;
175 height
= ctx
->Scissor
.Height
;
181 width
= ctx
->DrawBuffer
->Width
;
182 height
= ctx
->DrawBuffer
->Height
;
190 const GLaccum intVal
= (GLaccum
) (value
* acc_scale
);
192 /* Leave optimized accum buffer mode */
193 if (ctx
->IntegerAccumMode
)
195 for (j
= 0; j
< height
; j
++) {
196 GLaccum
* acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ 4 * xpos
;
198 for (i
= 0; i
< width4
; i
++) {
209 /* Leave optimized accum buffer mode */
210 if (ctx
->IntegerAccumMode
)
212 for (j
= 0; j
< height
; j
++) {
213 GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ 4 * xpos
;
215 for (i
= 0; i
< width4
; i
++) {
216 acc
[i
] = (GLaccum
) ( (GLfloat
) acc
[i
] * value
);
224 (void) (*ctx
->Driver
.SetBuffer
)( ctx
, ctx
->Pixel
.DriverReadBuffer
);
226 /* May have to leave optimized accum buffer mode */
227 if (ctx
->IntegerAccumScaler
== 0.0 && value
> 0.0 && value
<= 1.0)
228 ctx
->IntegerAccumScaler
= value
;
229 if (ctx
->IntegerAccumMode
&& value
!= ctx
->IntegerAccumScaler
)
232 if (ctx
->IntegerAccumMode
) {
233 /* simply add integer color values into accum buffer */
235 GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ xpos
* 4;
236 assert(ctx
->IntegerAccumScaler
> 0.0);
237 assert(ctx
->IntegerAccumScaler
<= 1.0);
238 for (j
= 0; j
< height
; j
++) {
241 gl_read_rgba_span(ctx
, ctx
->DrawBuffer
, width
, xpos
, ypos
, rgba
);
242 for (i
= i4
= 0; i
< width
; i
++, i4
+=4) {
243 acc
[i4
+0] += rgba
[i
][RCOMP
];
244 acc
[i4
+1] += rgba
[i
][GCOMP
];
245 acc
[i4
+2] += rgba
[i
][BCOMP
];
246 acc
[i4
+3] += rgba
[i
][ACOMP
];
253 /* scaled integer accum buffer */
254 const GLfloat rscale
= value
* acc_scale
/ fChanMax
;
255 const GLfloat gscale
= value
* acc_scale
/ fChanMax
;
256 const GLfloat bscale
= value
* acc_scale
/ fChanMax
;
257 const GLfloat ascale
= value
* acc_scale
/ fChanMax
;
259 for (j
=0;j
<height
;j
++) {
260 GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ xpos
* 4;
262 gl_read_rgba_span(ctx
, ctx
->DrawBuffer
, width
, xpos
, ypos
, rgba
);
263 for (i
=0;i
<width
;i
++) {
264 *acc
+= (GLaccum
) ( (GLfloat
) rgba
[i
][RCOMP
] * rscale
); acc
++;
265 *acc
+= (GLaccum
) ( (GLfloat
) rgba
[i
][GCOMP
] * gscale
); acc
++;
266 *acc
+= (GLaccum
) ( (GLfloat
) rgba
[i
][BCOMP
] * bscale
); acc
++;
267 *acc
+= (GLaccum
) ( (GLfloat
) rgba
[i
][ACOMP
] * ascale
); acc
++;
272 (void) (*ctx
->Driver
.SetBuffer
)( ctx
, ctx
->Color
.DriverDrawBuffer
);
276 (void) (*ctx
->Driver
.SetBuffer
)( ctx
, ctx
->Pixel
.DriverReadBuffer
);
278 /* This is a change to go into optimized accum buffer mode */
279 if (value
> 0.0 && value
<= 1.0) {
280 #ifdef USE_OPTIMIZED_ACCUM
281 ctx
->IntegerAccumMode
= GL_TRUE
;
283 ctx
->IntegerAccumMode
= GL_FALSE
;
285 ctx
->IntegerAccumScaler
= value
;
288 ctx
->IntegerAccumMode
= GL_FALSE
;
289 ctx
->IntegerAccumScaler
= 0.0;
292 if (ctx
->IntegerAccumMode
) {
293 /* just copy values into accum buffer */
295 GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ xpos
* 4;
296 assert(ctx
->IntegerAccumScaler
> 0.0);
297 assert(ctx
->IntegerAccumScaler
<= 1.0);
298 for (j
= 0; j
< height
; j
++) {
300 gl_read_rgba_span(ctx
, ctx
->DrawBuffer
, width
, xpos
, ypos
, rgba
);
301 for (i
= i4
= 0; i
< width
; i
++, i4
+= 4) {
302 acc
[i4
+0] = rgba
[i
][RCOMP
];
303 acc
[i4
+1] = rgba
[i
][GCOMP
];
304 acc
[i4
+2] = rgba
[i
][BCOMP
];
305 acc
[i4
+3] = rgba
[i
][ACOMP
];
312 /* scaled integer accum buffer */
313 const GLfloat rscale
= value
* acc_scale
/ fChanMax
;
314 const GLfloat gscale
= value
* acc_scale
/ fChanMax
;
315 const GLfloat bscale
= value
* acc_scale
/ fChanMax
;
316 const GLfloat ascale
= value
* acc_scale
/ fChanMax
;
317 const GLfloat d
= 3.0 / acc_scale
;
319 for (j
= 0; j
< height
; j
++) {
320 GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ xpos
* 4;
321 gl_read_rgba_span(ctx
, ctx
->DrawBuffer
, width
, xpos
, ypos
, rgba
);
322 for (i
=0;i
<width
;i
++) {
323 *acc
++ = (GLaccum
) ((GLfloat
) rgba
[i
][RCOMP
] * rscale
+ d
);
324 *acc
++ = (GLaccum
) ((GLfloat
) rgba
[i
][GCOMP
] * gscale
+ d
);
325 *acc
++ = (GLaccum
) ((GLfloat
) rgba
[i
][BCOMP
] * bscale
+ d
);
326 *acc
++ = (GLaccum
) ((GLfloat
) rgba
[i
][ACOMP
] * ascale
+ d
);
331 (void) (*ctx
->Driver
.SetBuffer
)( ctx
, ctx
->Color
.DriverDrawBuffer
);
335 /* May have to leave optimized accum buffer mode */
336 if (ctx
->IntegerAccumMode
&& value
!= 1.0)
339 if (ctx
->IntegerAccumMode
) {
340 /* build lookup table to avoid many floating point multiplies */
341 const GLfloat mult
= ctx
->IntegerAccumScaler
;
342 static GLchan multTable
[32768];
343 static GLfloat prevMult
= 0.0;
345 const GLint max
= 256 / mult
;
346 if (mult
!= prevMult
) {
347 assert(max
<= 32768);
348 for (j
= 0; j
< max
; j
++)
349 multTable
[j
] = (GLint
) ((GLfloat
) j
* mult
+ 0.5F
);
353 assert(ctx
->IntegerAccumScaler
> 0.0);
354 assert(ctx
->IntegerAccumScaler
<= 1.0);
355 for (j
= 0; j
< height
; j
++) {
356 const GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ xpos
*4;
358 for (i
= i4
= 0; i
< width
; i
++, i4
+= 4) {
359 ASSERT(acc
[i4
+0] < max
);
360 ASSERT(acc
[i4
+1] < max
);
361 ASSERT(acc
[i4
+2] < max
);
362 ASSERT(acc
[i4
+3] < max
);
363 rgba
[i
][RCOMP
] = multTable
[acc
[i4
+0]];
364 rgba
[i
][GCOMP
] = multTable
[acc
[i4
+1]];
365 rgba
[i
][BCOMP
] = multTable
[acc
[i4
+2]];
366 rgba
[i
][ACOMP
] = multTable
[acc
[i4
+3]];
368 if (ctx
->Color
.SWmasking
) {
369 gl_mask_rgba_span( ctx
, width
, xpos
, ypos
, rgba
);
371 (*ctx
->Driver
.WriteRGBASpan
)( ctx
, width
, xpos
, ypos
,
372 (const GLubyte (*)[4])rgba
, NULL
);
377 const GLfloat rscale
= value
/ acc_scale
* fChanMax
;
378 const GLfloat gscale
= value
/ acc_scale
* fChanMax
;
379 const GLfloat bscale
= value
/ acc_scale
* fChanMax
;
380 const GLfloat ascale
= value
/ acc_scale
* fChanMax
;
382 for (j
=0;j
<height
;j
++) {
383 const GLaccum
*acc
= ctx
->DrawBuffer
->Accum
+ ypos
* width4
+ xpos
*4;
384 for (i
=0;i
<width
;i
++) {
386 r
= (GLint
) ( (GLfloat
) (*acc
++) * rscale
+ 0.5F
);
387 g
= (GLint
) ( (GLfloat
) (*acc
++) * gscale
+ 0.5F
);
388 b
= (GLint
) ( (GLfloat
) (*acc
++) * bscale
+ 0.5F
);
389 a
= (GLint
) ( (GLfloat
) (*acc
++) * ascale
+ 0.5F
);
390 rgba
[i
][RCOMP
] = CLAMP( r
, 0, iChanMax
);
391 rgba
[i
][GCOMP
] = CLAMP( g
, 0, iChanMax
);
392 rgba
[i
][BCOMP
] = CLAMP( b
, 0, iChanMax
);
393 rgba
[i
][ACOMP
] = CLAMP( a
, 0, iChanMax
);
395 if (ctx
->Color
.SWmasking
) {
396 gl_mask_rgba_span( ctx
, width
, xpos
, ypos
, rgba
);
398 (*ctx
->Driver
.WriteRGBASpan
)( ctx
, width
, xpos
, ypos
,
399 (const GLubyte (*)[4])rgba
, NULL
);
406 gl_error( ctx
, GL_INVALID_ENUM
, "glAccum" );
413 * Clear the accumulation Buffer.
415 void gl_clear_accum_buffer( GLcontext
*ctx
)
420 if (ctx
->Visual
->AccumBits
==0) {
421 /* No accumulation buffer! */
425 if (sizeof(GLaccum
)==1) {
428 else if (sizeof(GLaccum
)==2) {
432 /* sizeof(GLaccum) > 2 (Cray) */
433 acc_scale
= (float) SHRT_MAX
;
436 /* number of pixels */
437 buffersize
= ctx
->DrawBuffer
->Width
* ctx
->DrawBuffer
->Height
;
439 if (!ctx
->DrawBuffer
->Accum
) {
440 /* try to alloc accumulation buffer */
441 ctx
->DrawBuffer
->Accum
= (GLaccum
*)
442 MALLOC( buffersize
* 4 * sizeof(GLaccum
) );
445 if (ctx
->DrawBuffer
->Accum
) {
446 if (ctx
->Scissor
.Enabled
) {
447 /* Limit clear to scissor box */
452 r
= (GLaccum
) (ctx
->Accum
.ClearColor
[0] * acc_scale
);
453 g
= (GLaccum
) (ctx
->Accum
.ClearColor
[1] * acc_scale
);
454 b
= (GLaccum
) (ctx
->Accum
.ClearColor
[2] * acc_scale
);
455 a
= (GLaccum
) (ctx
->Accum
.ClearColor
[3] * acc_scale
);
456 /* size of region to clear */
457 width
= 4 * (ctx
->DrawBuffer
->Xmax
- ctx
->DrawBuffer
->Xmin
+ 1);
458 height
= ctx
->DrawBuffer
->Ymax
- ctx
->DrawBuffer
->Ymin
+ 1;
459 /* ptr to first element to clear */
460 row
= ctx
->DrawBuffer
->Accum
461 + 4 * (ctx
->DrawBuffer
->Ymin
* ctx
->DrawBuffer
->Width
462 + ctx
->DrawBuffer
->Xmin
);
463 for (j
=0;j
<height
;j
++) {
464 for (i
=0;i
<width
;i
+=4) {
470 row
+= 4 * ctx
->DrawBuffer
->Width
;
474 /* clear whole buffer */
475 if (ctx
->Accum
.ClearColor
[0]==0.0 &&
476 ctx
->Accum
.ClearColor
[1]==0.0 &&
477 ctx
->Accum
.ClearColor
[2]==0.0 &&
478 ctx
->Accum
.ClearColor
[3]==0.0) {
480 MEMSET( ctx
->DrawBuffer
->Accum
, 0, buffersize
* 4 * sizeof(GLaccum
) );
484 GLaccum
*acc
, r
, g
, b
, a
;
487 acc
= ctx
->DrawBuffer
->Accum
;
488 r
= (GLaccum
) (ctx
->Accum
.ClearColor
[0] * acc_scale
);
489 g
= (GLaccum
) (ctx
->Accum
.ClearColor
[1] * acc_scale
);
490 b
= (GLaccum
) (ctx
->Accum
.ClearColor
[2] * acc_scale
);
491 a
= (GLaccum
) (ctx
->Accum
.ClearColor
[3] * acc_scale
);
492 for (i
=0;i
<buffersize
;i
++) {
501 /* update optimized accum state vars */
502 if (ctx
->Accum
.ClearColor
[0] == 0.0 && ctx
->Accum
.ClearColor
[1] == 0.0 &&
503 ctx
->Accum
.ClearColor
[2] == 0.0 && ctx
->Accum
.ClearColor
[3] == 0.0) {
504 #ifdef USE_OPTIMIZED_ACCUM
505 ctx
->IntegerAccumMode
= GL_TRUE
;
507 ctx
->IntegerAccumMode
= GL_FALSE
;
509 ctx
->IntegerAccumScaler
= 0.0; /* denotes empty accum buffer */
512 ctx
->IntegerAccumMode
= GL_FALSE
;