de660a84a6a05b0ba93b6a000d18c44ccc059222
[mesa.git] / src / mesa / main / accum.c
1 /* $Id: accum.c,v 1.7 1999/10/20 22:32:02 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.1
6 *
7 * Copyright (C) 1999 Brian Paul All Rights Reserved.
8 *
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:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
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.
25 */
26
27
28 /* $XFree86: xc/lib/GL/mesa/src/accum.c,v 1.3 1999/04/04 00:20:17 dawes Exp $ */
29
30 #ifdef PC_HEADER
31 #include "all.h"
32 #else
33 #ifndef XFree86Server
34 #include <limits.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #else
38 #include "GL/xf86glx.h"
39 #endif
40 #include <assert.h>
41 #include "accum.h"
42 #include "context.h"
43 #include "macros.h"
44 #include "masking.h"
45 #include "span.h"
46 #include "types.h"
47 #ifdef XFree86Server
48 #include "GL/xf86glx.h"
49 #endif
50 #endif
51
52
53 /*
54 * Accumulation buffer notes
55 *
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.
59 *
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);
64 * ...
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
72 * values by n (=1/w).
73 * This lets us avoid _many_ int->float->int conversions.
74 */
75
76
77 #define USE_OPTIMIZED_ACCUM /* enable the optimization */
78
79
80
81 void gl_alloc_accum_buffer( GLcontext *ctx )
82 {
83 GLint n;
84
85 if (ctx->Buffer->Accum) {
86 FREE( ctx->Buffer->Accum );
87 ctx->Buffer->Accum = NULL;
88 }
89
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" );
96 }
97 #ifdef USE_OPTIMIZED_ACCUM
98 ctx->IntegerAccumMode = GL_TRUE;
99 #else
100 ctx->IntegerAccumMode = GL_FALSE;
101 #endif
102 ctx->IntegerAccumScaler = 0.0;
103 }
104
105
106
107 void gl_ClearAccum( GLcontext *ctx,
108 GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha )
109 {
110 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glAccum");
111
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 );
116 }
117
118
119
120 /*
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].
124 */
125 static void rescale_accum( GLcontext *ctx )
126 {
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;
131 GLuint i;
132
133 assert(ctx->IntegerAccumMode);
134 assert(accum);
135
136 for (i = 0; i < n; i++) {
137 accum[i] = (GLaccum) (accum[i] * s);
138 }
139
140 ctx->IntegerAccumMode = GL_FALSE;
141 }
142
143
144
145 void gl_Accum( GLcontext *ctx, GLenum op, GLfloat value )
146 {
147 GLuint xpos, ypos, width, height, width4;
148 GLfloat acc_scale;
149 GLubyte rgba[MAX_WIDTH][4];
150 const GLint iChanMax = (1 << (sizeof(GLchan) * 8)) - 1;
151 const GLfloat fChanMax = (1 << (sizeof(GLchan) * 8)) - 1;
152
153 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glAccum");
154
155 if (ctx->Visual->AccumBits==0 || !ctx->Buffer->Accum) {
156 /* No accumulation buffer! */
157 gl_warning(ctx, "Calling glAccum() without an accumulation buffer");
158 return;
159 }
160
161 if (sizeof(GLaccum)==1) {
162 acc_scale = 127.0;
163 }
164 else if (sizeof(GLaccum)==2) {
165 acc_scale = 32767.0;
166 }
167 else {
168 /* sizeof(GLaccum) > 2 (Cray) */
169 acc_scale = (float) SHRT_MAX;
170 }
171
172 if (ctx->NewState)
173 gl_update_state( ctx );
174
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;
181 }
182 else {
183 /* whole window */
184 xpos = 0;
185 ypos = 0;
186 width = ctx->Buffer->Width;
187 height = ctx->Buffer->Height;
188 }
189
190 width4 = 4 * width;
191
192 switch (op) {
193 case GL_ADD:
194 {
195 const GLaccum intVal = (GLaccum) (value * acc_scale);
196 GLuint j;
197 /* Leave optimized accum buffer mode */
198 if (ctx->IntegerAccumMode)
199 rescale_accum(ctx);
200 for (j = 0; j < height; j++) {
201 GLaccum * acc = ctx->Buffer->Accum + ypos * width4 + 4 * xpos;
202 GLuint i;
203 for (i = 0; i < width4; i++) {
204 acc[i] += intVal;
205 }
206 ypos++;
207 }
208 }
209 break;
210
211 case GL_MULT:
212 {
213 GLuint j;
214 /* Leave optimized accum buffer mode */
215 if (ctx->IntegerAccumMode)
216 rescale_accum(ctx);
217 for (j = 0; j < height; j++) {
218 GLaccum *acc = ctx->Buffer->Accum + ypos * width4 + 4 * xpos;
219 GLuint i;
220 for (i = 0; i < width4; i++) {
221 acc[i] = (GLaccum) ( (GLfloat) acc[i] * value );
222 }
223 ypos++;
224 }
225 }
226 break;
227
228 case GL_ACCUM:
229 (void) (*ctx->Driver.SetBuffer)( ctx, ctx->Pixel.DriverReadBuffer );
230
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)
235 rescale_accum(ctx);
236
237 if (ctx->IntegerAccumMode) {
238 /* simply add integer color values into accum buffer */
239 GLuint j;
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++) {
244
245 GLuint i, i4;
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];
252 }
253 acc += width4;
254 ypos++;
255 }
256 }
257 else {
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;
263 GLuint j;
264 for (j=0;j<height;j++) {
265 GLaccum *acc = ctx->Buffer->Accum + ypos * width4 + xpos * 4;
266 GLuint i;
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++;
273 }
274 ypos++;
275 }
276 }
277 (void) (*ctx->Driver.SetBuffer)( ctx, ctx->Color.DriverDrawBuffer );
278 break;
279
280 case GL_LOAD:
281 (void) (*ctx->Driver.SetBuffer)( ctx, ctx->Pixel.DriverReadBuffer );
282
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;
287 #else
288 ctx->IntegerAccumMode = GL_FALSE;
289 #endif
290 ctx->IntegerAccumScaler = value;
291 }
292 else {
293 ctx->IntegerAccumMode = GL_FALSE;
294 ctx->IntegerAccumScaler = 0.0;
295 }
296
297 if (ctx->IntegerAccumMode) {
298 /* just copy values into accum buffer */
299 GLuint j;
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++) {
304 GLuint i, i4;
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];
311 }
312 acc += width4;
313 ypos++;
314 }
315 }
316 else {
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;
323 GLuint i, j;
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);
332 }
333 ypos++;
334 }
335 }
336 (void) (*ctx->Driver.SetBuffer)( ctx, ctx->Color.DriverDrawBuffer );
337 break;
338
339 case GL_RETURN:
340 /* May have to leave optimized accum buffer mode */
341 if (ctx->IntegerAccumMode && value != 1.0)
342 rescale_accum(ctx);
343
344 if (ctx->IntegerAccumMode) {
345 /* build lookup table to avoid integer divides */
346 GLfloat divisor = 1.0F / ctx->IntegerAccumScaler;
347 static GLubyte divTable[32768];
348 static GLfloat prevDivisor = 0.0;
349 GLuint j;
350 if (divisor != prevDivisor) {
351 assert(divisor * 256 <= 32768);
352 for (j = 0; j < divisor * 256; j++)
353 divTable[j] = (GLint) ((GLfloat) j / divisor + 0.5F);
354 prevDivisor = divisor;
355 }
356
357 assert(ctx->IntegerAccumScaler > 0.0);
358 assert(ctx->IntegerAccumScaler <= 1.0);
359 for (j = 0; j < height; j++) {
360 const GLaccum *acc = ctx->Buffer->Accum + ypos * width4 + xpos*4;
361 GLuint i, i4;
362 for (i = i4 = 0; i < width; i++, i4 += 4) {
363 ASSERT(acc[i4+0] < divisor * 256);
364 ASSERT(acc[i4+1] < divisor * 256);
365 ASSERT(acc[i4+2] < divisor * 256);
366 ASSERT(acc[i4+3] < divisor * 256);
367 rgba[i][RCOMP] = divTable[acc[i4+0]];
368 rgba[i][GCOMP] = divTable[acc[i4+1]];
369 rgba[i][BCOMP] = divTable[acc[i4+2]];
370 rgba[i][ACOMP] = divTable[acc[i4+3]];
371 }
372 if (ctx->Color.SWmasking) {
373 gl_mask_rgba_span( ctx, width, xpos, ypos, rgba );
374 }
375 (*ctx->Driver.WriteRGBASpan)( ctx, width, xpos, ypos,
376 (const GLubyte (*)[4])rgba, NULL );
377 ypos++;
378 }
379 }
380 else {
381 const GLfloat rscale = value / acc_scale * fChanMax;
382 const GLfloat gscale = value / acc_scale * fChanMax;
383 const GLfloat bscale = value / acc_scale * fChanMax;
384 const GLfloat ascale = value / acc_scale * fChanMax;
385 GLuint i, j;
386 for (j=0;j<height;j++) {
387 const GLaccum *acc = ctx->Buffer->Accum + ypos * width4 + xpos*4;
388 for (i=0;i<width;i++) {
389 GLint r, g, b, a;
390 r = (GLint) ( (GLfloat) (*acc++) * rscale + 0.5F );
391 g = (GLint) ( (GLfloat) (*acc++) * gscale + 0.5F );
392 b = (GLint) ( (GLfloat) (*acc++) * bscale + 0.5F );
393 a = (GLint) ( (GLfloat) (*acc++) * ascale + 0.5F );
394 rgba[i][RCOMP] = CLAMP( r, 0, iChanMax );
395 rgba[i][GCOMP] = CLAMP( g, 0, iChanMax );
396 rgba[i][BCOMP] = CLAMP( b, 0, iChanMax );
397 rgba[i][ACOMP] = CLAMP( a, 0, iChanMax );
398 }
399 if (ctx->Color.SWmasking) {
400 gl_mask_rgba_span( ctx, width, xpos, ypos, rgba );
401 }
402 (*ctx->Driver.WriteRGBASpan)( ctx, width, xpos, ypos,
403 (const GLubyte (*)[4])rgba, NULL );
404 ypos++;
405 }
406 }
407 break;
408
409 default:
410 gl_error( ctx, GL_INVALID_ENUM, "glAccum" );
411 }
412 }
413
414
415
416 /*
417 * Clear the accumulation Buffer.
418 */
419 void gl_clear_accum_buffer( GLcontext *ctx )
420 {
421 GLuint buffersize;
422 GLfloat acc_scale;
423
424 if (ctx->Visual->AccumBits==0) {
425 /* No accumulation buffer! */
426 return;
427 }
428
429 if (sizeof(GLaccum)==1) {
430 acc_scale = 127.0;
431 }
432 else if (sizeof(GLaccum)==2) {
433 acc_scale = 32767.0;
434 }
435 else {
436 /* sizeof(GLaccum) > 2 (Cray) */
437 acc_scale = (float) SHRT_MAX;
438 }
439
440 /* number of pixels */
441 buffersize = ctx->Buffer->Width * ctx->Buffer->Height;
442
443 if (!ctx->Buffer->Accum) {
444 /* try to alloc accumulation buffer */
445 ctx->Buffer->Accum = (GLaccum *)
446 MALLOC( buffersize * 4 * sizeof(GLaccum) );
447 }
448
449 if (ctx->Buffer->Accum) {
450 if (ctx->Scissor.Enabled) {
451 /* Limit clear to scissor box */
452 GLaccum r, g, b, a;
453 GLint i, j;
454 GLint width, height;
455 GLaccum *row;
456 r = (GLaccum) (ctx->Accum.ClearColor[0] * acc_scale);
457 g = (GLaccum) (ctx->Accum.ClearColor[1] * acc_scale);
458 b = (GLaccum) (ctx->Accum.ClearColor[2] * acc_scale);
459 a = (GLaccum) (ctx->Accum.ClearColor[3] * acc_scale);
460 /* size of region to clear */
461 width = 4 * (ctx->Buffer->Xmax - ctx->Buffer->Xmin + 1);
462 height = ctx->Buffer->Ymax - ctx->Buffer->Ymin + 1;
463 /* ptr to first element to clear */
464 row = ctx->Buffer->Accum
465 + 4 * (ctx->Buffer->Ymin * ctx->Buffer->Width
466 + ctx->Buffer->Xmin);
467 for (j=0;j<height;j++) {
468 for (i=0;i<width;i+=4) {
469 row[i+0] = r;
470 row[i+1] = g;
471 row[i+2] = b;
472 row[i+3] = a;
473 }
474 row += 4 * ctx->Buffer->Width;
475 }
476 }
477 else {
478 /* clear whole buffer */
479 if (ctx->Accum.ClearColor[0]==0.0 &&
480 ctx->Accum.ClearColor[1]==0.0 &&
481 ctx->Accum.ClearColor[2]==0.0 &&
482 ctx->Accum.ClearColor[3]==0.0) {
483 /* Black */
484 MEMSET( ctx->Buffer->Accum, 0, buffersize * 4 * sizeof(GLaccum) );
485 }
486 else {
487 /* Not black */
488 GLaccum *acc, r, g, b, a;
489 GLuint i;
490
491 acc = ctx->Buffer->Accum;
492 r = (GLaccum) (ctx->Accum.ClearColor[0] * acc_scale);
493 g = (GLaccum) (ctx->Accum.ClearColor[1] * acc_scale);
494 b = (GLaccum) (ctx->Accum.ClearColor[2] * acc_scale);
495 a = (GLaccum) (ctx->Accum.ClearColor[3] * acc_scale);
496 for (i=0;i<buffersize;i++) {
497 *acc++ = r;
498 *acc++ = g;
499 *acc++ = b;
500 *acc++ = a;
501 }
502 }
503 }
504
505 /* update optimized accum state vars */
506 if (ctx->Accum.ClearColor[0] == 0.0 && ctx->Accum.ClearColor[1] == 0.0 &&
507 ctx->Accum.ClearColor[2] == 0.0 && ctx->Accum.ClearColor[3] == 0.0) {
508 #ifdef USE_OPTIMIZED_ACCUM
509 ctx->IntegerAccumMode = GL_TRUE;
510 #else
511 ctx->IntegerAccumMode = GL_FALSE;
512 #endif
513 ctx->IntegerAccumScaler = 0.0; /* denotes empty accum buffer */
514 }
515 else {
516 ctx->IntegerAccumMode = GL_FALSE;
517 }
518 }
519 }