more minor header file re-org (moved CONST, ASSERT, INLINE to config.h)
[mesa.git] / src / mesa / main / accum.c
1 /* $Id: accum.c,v 1.29 2000/10/29 18:23:16 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999-2000 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 #ifdef PC_HEADER
29 #include "all.h"
30 #else
31 #include "glheader.h"
32 #include "accum.h"
33 #include "context.h"
34 #include "macros.h"
35 #include "mem.h"
36 #include "masking.h"
37 #include "span.h"
38 #include "state.h"
39 #include "types.h"
40 #endif
41
42
43 /*
44 * Accumulation buffer notes
45 *
46 * Normally, accumulation buffer values are GLshorts with values in
47 * [-32767, 32767] which represent floating point colors in [-1, 1],
48 * as suggested by the OpenGL specification.
49 *
50 * We optimize for the common case used for full-scene antialiasing:
51 * // start with accum buffer cleared to zero
52 * glAccum(GL_LOAD, w); // or GL_ACCUM the first image
53 * glAccum(GL_ACCUM, w);
54 * ...
55 * glAccum(GL_ACCUM, w);
56 * glAccum(GL_RETURN, 1.0);
57 * That is, we start with an empty accumulation buffer and accumulate
58 * n images, each with weight w = 1/n.
59 * In this scenario, we can simply store unscaled integer values in
60 * the accum buffer instead of scaled integers. We'll also keep track
61 * of the w value so when we do GL_RETURN we simply divide the accumulated
62 * values by n (=1/w).
63 * This lets us avoid _many_ int->float->int conversions.
64 */
65
66
67 #if CHAN_BITS == 8
68 #define USE_OPTIMIZED_ACCUM /* enable the optimization */
69 #endif
70
71
72
73 void
74 _mesa_alloc_accum_buffer( GLcontext *ctx )
75 {
76 GLint n;
77
78 if (ctx->DrawBuffer->Accum) {
79 FREE( ctx->DrawBuffer->Accum );
80 ctx->DrawBuffer->Accum = NULL;
81 }
82
83 /* allocate accumulation buffer if not already present */
84 n = ctx->DrawBuffer->Width * ctx->DrawBuffer->Height * 4 * sizeof(GLaccum);
85 ctx->DrawBuffer->Accum = (GLaccum *) MALLOC( n );
86 if (!ctx->DrawBuffer->Accum) {
87 /* unable to setup accumulation buffer */
88 gl_error( ctx, GL_OUT_OF_MEMORY, "glAccum" );
89 }
90 #ifdef USE_OPTIMIZED_ACCUM
91 ctx->IntegerAccumMode = GL_TRUE;
92 #else
93 ctx->IntegerAccumMode = GL_FALSE;
94 #endif
95 ctx->IntegerAccumScaler = 0.0;
96 }
97
98
99
100 void
101 _mesa_ClearAccum( GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha )
102 {
103 GET_CURRENT_CONTEXT(ctx);
104 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glAccum");
105
106 ctx->Accum.ClearColor[0] = CLAMP( red, -1.0, 1.0 );
107 ctx->Accum.ClearColor[1] = CLAMP( green, -1.0, 1.0 );
108 ctx->Accum.ClearColor[2] = CLAMP( blue, -1.0, 1.0 );
109 ctx->Accum.ClearColor[3] = CLAMP( alpha, -1.0, 1.0 );
110 }
111
112
113
114 /*
115 * This is called when we fall out of optimized/unscaled accum buffer mode.
116 * That is, we convert each unscaled accum buffer value into a scaled value
117 * representing the range[-1, 1].
118 */
119 static void rescale_accum( GLcontext *ctx )
120 {
121 const GLuint n = ctx->DrawBuffer->Width * ctx->DrawBuffer->Height * 4;
122 const GLfloat fChanMax = (1 << (sizeof(GLchan) * 8)) - 1;
123 const GLfloat s = ctx->IntegerAccumScaler * (32767.0 / fChanMax);
124 GLaccum *accum = ctx->DrawBuffer->Accum;
125 GLuint i;
126
127 assert(ctx->IntegerAccumMode);
128 assert(accum);
129
130 for (i = 0; i < n; i++) {
131 accum[i] = (GLaccum) (accum[i] * s);
132 }
133
134 ctx->IntegerAccumMode = GL_FALSE;
135 }
136
137
138
139 void
140 _mesa_Accum( GLenum op, GLfloat value )
141 {
142 GET_CURRENT_CONTEXT(ctx);
143 GLuint xpos, ypos, width, height, width4;
144 GLfloat acc_scale;
145 GLchan rgba[MAX_WIDTH][4];
146 const GLuint colorMask = *((GLuint *) &ctx->Color.ColorMask);
147 const GLint iChanMax = (1 << (sizeof(GLchan) * 8)) - 1;
148 const GLfloat fChanMax = (1 << (sizeof(GLchan) * 8)) - 1;
149
150 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glAccum");
151
152 if (ctx->Visual.AccumRedBits == 0 || ctx->DrawBuffer != ctx->ReadBuffer) {
153 gl_error(ctx, GL_INVALID_OPERATION, "glAccum");
154 return;
155 }
156
157 if (!ctx->DrawBuffer->Accum) {
158 _mesa_warning(ctx, "Calling glAccum() without an accumulation buffer (low memory?)");
159 return;
160 }
161
162 if (sizeof(GLaccum)==1) {
163 acc_scale = 127.0;
164 }
165 else if (sizeof(GLaccum)==2) {
166 acc_scale = 32767.0;
167 }
168 else {
169 /* sizeof(GLaccum) > 2 (Cray) */
170 acc_scale = (float) SHRT_MAX;
171 }
172
173 if (ctx->NewState)
174 gl_update_state( ctx );
175
176 /* Determine region to operate upon. */
177 if (ctx->Scissor.Enabled) {
178 xpos = ctx->Scissor.X;
179 ypos = ctx->Scissor.Y;
180 width = ctx->Scissor.Width;
181 height = ctx->Scissor.Height;
182 }
183 else {
184 /* whole window */
185 xpos = 0;
186 ypos = 0;
187 width = ctx->DrawBuffer->Width;
188 height = ctx->DrawBuffer->Height;
189 }
190
191 width4 = 4 * width;
192
193 switch (op) {
194 case GL_ADD:
195 if (value != 0.0F) {
196 const GLaccum intVal = (GLaccum) (value * acc_scale);
197 GLuint j;
198 /* Leave optimized accum buffer mode */
199 if (ctx->IntegerAccumMode)
200 rescale_accum(ctx);
201 for (j = 0; j < height; j++) {
202 GLaccum * acc = ctx->DrawBuffer->Accum + ypos * width4 + 4 * xpos;
203 GLuint i;
204 for (i = 0; i < width4; i++) {
205 acc[i] += intVal;
206 }
207 ypos++;
208 }
209 }
210 break;
211
212 case GL_MULT:
213 if (value != 1.0F) {
214 GLuint j;
215 /* Leave optimized accum buffer mode */
216 if (ctx->IntegerAccumMode)
217 rescale_accum(ctx);
218 for (j = 0; j < height; j++) {
219 GLaccum *acc = ctx->DrawBuffer->Accum + ypos * width4 + 4 * xpos;
220 GLuint i;
221 for (i = 0; i < width4; i++) {
222 acc[i] = (GLaccum) ( (GLfloat) acc[i] * value );
223 }
224 ypos++;
225 }
226 }
227 break;
228
229 case GL_ACCUM:
230 if (value == 0.0F)
231 return;
232
233 (*ctx->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
234 ctx->Pixel.DriverReadBuffer );
235
236 /* May have to leave optimized accum buffer mode */
237 if (ctx->IntegerAccumScaler == 0.0 && value > 0.0 && value <= 1.0)
238 ctx->IntegerAccumScaler = value;
239 if (ctx->IntegerAccumMode && value != ctx->IntegerAccumScaler)
240 rescale_accum(ctx);
241
242 RENDER_START(ctx);
243
244 if (ctx->IntegerAccumMode) {
245 /* simply add integer color values into accum buffer */
246 GLuint j;
247 GLaccum *acc = ctx->DrawBuffer->Accum + ypos * width4 + xpos * 4;
248 assert(ctx->IntegerAccumScaler > 0.0);
249 assert(ctx->IntegerAccumScaler <= 1.0);
250 for (j = 0; j < height; j++) {
251
252 GLuint i, i4;
253 gl_read_rgba_span(ctx, ctx->DrawBuffer, width, xpos, ypos, rgba);
254 for (i = i4 = 0; i < width; i++, i4+=4) {
255 acc[i4+0] += rgba[i][RCOMP];
256 acc[i4+1] += rgba[i][GCOMP];
257 acc[i4+2] += rgba[i][BCOMP];
258 acc[i4+3] += rgba[i][ACOMP];
259 }
260 acc += width4;
261 ypos++;
262 }
263 }
264 else {
265 /* scaled integer accum buffer */
266 const GLfloat rscale = value * acc_scale / fChanMax;
267 const GLfloat gscale = value * acc_scale / fChanMax;
268 const GLfloat bscale = value * acc_scale / fChanMax;
269 const GLfloat ascale = value * acc_scale / fChanMax;
270 GLuint j;
271 for (j=0;j<height;j++) {
272 GLaccum *acc = ctx->DrawBuffer->Accum + ypos * width4 + xpos * 4;
273 GLuint i;
274 gl_read_rgba_span(ctx, ctx->DrawBuffer, width, xpos, ypos, rgba);
275 for (i=0;i<width;i++) {
276 *acc += (GLaccum) ( (GLfloat) rgba[i][RCOMP] * rscale ); acc++;
277 *acc += (GLaccum) ( (GLfloat) rgba[i][GCOMP] * gscale ); acc++;
278 *acc += (GLaccum) ( (GLfloat) rgba[i][BCOMP] * bscale ); acc++;
279 *acc += (GLaccum) ( (GLfloat) rgba[i][ACOMP] * ascale ); acc++;
280 }
281 ypos++;
282 }
283 }
284 /* restore read buffer = draw buffer (the default) */
285 (*ctx->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
286 ctx->Color.DriverDrawBuffer );
287 RENDER_FINISH(ctx);
288 break;
289
290 case GL_LOAD:
291 (*ctx->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
292 ctx->Pixel.DriverReadBuffer );
293
294 /* This is a change to go into optimized accum buffer mode */
295 if (value > 0.0 && value <= 1.0) {
296 #ifdef USE_OPTIMIZED_ACCUM
297 ctx->IntegerAccumMode = GL_TRUE;
298 #else
299 ctx->IntegerAccumMode = GL_FALSE;
300 #endif
301 ctx->IntegerAccumScaler = value;
302 }
303 else {
304 ctx->IntegerAccumMode = GL_FALSE;
305 ctx->IntegerAccumScaler = 0.0;
306 }
307
308 RENDER_START(ctx);
309 if (ctx->IntegerAccumMode) {
310 /* just copy values into accum buffer */
311 GLuint j;
312 GLaccum *acc = ctx->DrawBuffer->Accum + ypos * width4 + xpos * 4;
313 assert(ctx->IntegerAccumScaler > 0.0);
314 assert(ctx->IntegerAccumScaler <= 1.0);
315 for (j = 0; j < height; j++) {
316 GLuint i, i4;
317 gl_read_rgba_span(ctx, ctx->DrawBuffer, width, xpos, ypos, rgba);
318 for (i = i4 = 0; i < width; i++, i4 += 4) {
319 acc[i4+0] = rgba[i][RCOMP];
320 acc[i4+1] = rgba[i][GCOMP];
321 acc[i4+2] = rgba[i][BCOMP];
322 acc[i4+3] = rgba[i][ACOMP];
323 }
324 acc += width4;
325 ypos++;
326 }
327 }
328 else {
329 /* scaled integer accum buffer */
330 const GLfloat rscale = value * acc_scale / fChanMax;
331 const GLfloat gscale = value * acc_scale / fChanMax;
332 const GLfloat bscale = value * acc_scale / fChanMax;
333 const GLfloat ascale = value * acc_scale / fChanMax;
334 const GLfloat d = 3.0 / acc_scale;
335 GLuint i, j;
336 for (j = 0; j < height; j++) {
337 GLaccum *acc = ctx->DrawBuffer->Accum + ypos * width4 + xpos * 4;
338 gl_read_rgba_span(ctx, ctx->DrawBuffer, width, xpos, ypos, rgba);
339 for (i=0;i<width;i++) {
340 *acc++ = (GLaccum) ((GLfloat) rgba[i][RCOMP] * rscale + d);
341 *acc++ = (GLaccum) ((GLfloat) rgba[i][GCOMP] * gscale + d);
342 *acc++ = (GLaccum) ((GLfloat) rgba[i][BCOMP] * bscale + d);
343 *acc++ = (GLaccum) ((GLfloat) rgba[i][ACOMP] * ascale + d);
344 }
345 ypos++;
346 }
347 }
348
349 /* restore read buffer = draw buffer (the default) */
350 (*ctx->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
351 ctx->Color.DriverDrawBuffer );
352 RENDER_FINISH(ctx);
353 break;
354
355 case GL_RETURN:
356 /* May have to leave optimized accum buffer mode */
357 if (ctx->IntegerAccumMode && value != 1.0)
358 rescale_accum(ctx);
359
360 RENDER_START(ctx);
361 if (ctx->IntegerAccumMode && ctx->IntegerAccumScaler > 0) {
362 /* build lookup table to avoid many floating point multiplies */
363 static GLchan multTable[32768];
364 static GLfloat prevMult = 0.0;
365 const GLfloat mult = ctx->IntegerAccumScaler;
366 const GLint max = MIN2((GLint) (256 / mult), 32767);
367 GLuint j;
368 if (mult != prevMult) {
369 for (j = 0; j < max; j++)
370 multTable[j] = (GLint) ((GLfloat) j * mult + 0.5F);
371 prevMult = mult;
372 }
373
374 assert(ctx->IntegerAccumScaler > 0.0);
375 assert(ctx->IntegerAccumScaler <= 1.0);
376 for (j = 0; j < height; j++) {
377 const GLaccum *acc = ctx->DrawBuffer->Accum + ypos * width4 + xpos*4;
378 GLuint i, i4;
379 for (i = i4 = 0; i < width; i++, i4 += 4) {
380 ASSERT(acc[i4+0] < max);
381 ASSERT(acc[i4+1] < max);
382 ASSERT(acc[i4+2] < max);
383 ASSERT(acc[i4+3] < max);
384 rgba[i][RCOMP] = multTable[acc[i4+0]];
385 rgba[i][GCOMP] = multTable[acc[i4+1]];
386 rgba[i][BCOMP] = multTable[acc[i4+2]];
387 rgba[i][ACOMP] = multTable[acc[i4+3]];
388 }
389 if (colorMask != 0xffffffff) {
390 _mesa_mask_rgba_span( ctx, width, xpos, ypos, rgba );
391 }
392 (*ctx->Driver.WriteRGBASpan)( ctx, width, xpos, ypos,
393 (const GLchan (*)[4])rgba, NULL );
394 ypos++;
395 }
396 }
397 else {
398 const GLfloat rscale = value / acc_scale * fChanMax;
399 const GLfloat gscale = value / acc_scale * fChanMax;
400 const GLfloat bscale = value / acc_scale * fChanMax;
401 const GLfloat ascale = value / acc_scale * fChanMax;
402 GLuint i, j;
403 for (j=0;j<height;j++) {
404 const GLaccum *acc = ctx->DrawBuffer->Accum + ypos * width4 + xpos*4;
405 for (i=0;i<width;i++) {
406 GLint r, g, b, a;
407 r = (GLint) ( (GLfloat) (*acc++) * rscale + 0.5F );
408 g = (GLint) ( (GLfloat) (*acc++) * gscale + 0.5F );
409 b = (GLint) ( (GLfloat) (*acc++) * bscale + 0.5F );
410 a = (GLint) ( (GLfloat) (*acc++) * ascale + 0.5F );
411 rgba[i][RCOMP] = CLAMP( r, 0, iChanMax );
412 rgba[i][GCOMP] = CLAMP( g, 0, iChanMax );
413 rgba[i][BCOMP] = CLAMP( b, 0, iChanMax );
414 rgba[i][ACOMP] = CLAMP( a, 0, iChanMax );
415 }
416 if (colorMask != 0xffffffff) {
417 _mesa_mask_rgba_span( ctx, width, xpos, ypos, rgba );
418 }
419 (*ctx->Driver.WriteRGBASpan)( ctx, width, xpos, ypos,
420 (const GLchan (*)[4])rgba, NULL );
421 ypos++;
422 }
423 }
424 RENDER_FINISH(ctx);
425 break;
426
427 default:
428 gl_error( ctx, GL_INVALID_ENUM, "glAccum" );
429 }
430 }
431
432
433
434 /*
435 * Clear the accumulation Buffer.
436 */
437 void
438 _mesa_clear_accum_buffer( GLcontext *ctx )
439 {
440 GLuint buffersize;
441 GLfloat acc_scale;
442
443 if (ctx->Visual.AccumRedBits==0) {
444 /* No accumulation buffer! */
445 return;
446 }
447
448 if (sizeof(GLaccum)==1) {
449 acc_scale = 127.0;
450 }
451 else if (sizeof(GLaccum)==2) {
452 acc_scale = 32767.0;
453 }
454 else {
455 /* sizeof(GLaccum) > 2 (Cray) */
456 acc_scale = (float) SHRT_MAX;
457 }
458
459 /* number of pixels */
460 buffersize = ctx->DrawBuffer->Width * ctx->DrawBuffer->Height;
461
462 if (!ctx->DrawBuffer->Accum) {
463 /* try to alloc accumulation buffer */
464 ctx->DrawBuffer->Accum = (GLaccum *)
465 MALLOC( buffersize * 4 * sizeof(GLaccum) );
466 }
467
468 if (ctx->DrawBuffer->Accum) {
469 if (ctx->Scissor.Enabled) {
470 /* Limit clear to scissor box */
471 GLaccum r, g, b, a;
472 GLint i, j;
473 GLint width, height;
474 GLaccum *row;
475 r = (GLaccum) (ctx->Accum.ClearColor[0] * acc_scale);
476 g = (GLaccum) (ctx->Accum.ClearColor[1] * acc_scale);
477 b = (GLaccum) (ctx->Accum.ClearColor[2] * acc_scale);
478 a = (GLaccum) (ctx->Accum.ClearColor[3] * acc_scale);
479 /* size of region to clear */
480 width = 4 * (ctx->DrawBuffer->Xmax - ctx->DrawBuffer->Xmin);
481 height = ctx->DrawBuffer->Ymax - ctx->DrawBuffer->Ymin;
482 /* ptr to first element to clear */
483 row = ctx->DrawBuffer->Accum
484 + 4 * (ctx->DrawBuffer->Ymin * ctx->DrawBuffer->Width
485 + ctx->DrawBuffer->Xmin);
486 for (j=0;j<height;j++) {
487 for (i=0;i<width;i+=4) {
488 row[i+0] = r;
489 row[i+1] = g;
490 row[i+2] = b;
491 row[i+3] = a;
492 }
493 row += 4 * ctx->DrawBuffer->Width;
494 }
495 }
496 else {
497 /* clear whole buffer */
498 if (ctx->Accum.ClearColor[0]==0.0 &&
499 ctx->Accum.ClearColor[1]==0.0 &&
500 ctx->Accum.ClearColor[2]==0.0 &&
501 ctx->Accum.ClearColor[3]==0.0) {
502 /* Black */
503 BZERO( ctx->DrawBuffer->Accum, buffersize * 4 * sizeof(GLaccum) );
504 }
505 else {
506 /* Not black */
507 GLaccum *acc, r, g, b, a;
508 GLuint i;
509
510 acc = ctx->DrawBuffer->Accum;
511 r = (GLaccum) (ctx->Accum.ClearColor[0] * acc_scale);
512 g = (GLaccum) (ctx->Accum.ClearColor[1] * acc_scale);
513 b = (GLaccum) (ctx->Accum.ClearColor[2] * acc_scale);
514 a = (GLaccum) (ctx->Accum.ClearColor[3] * acc_scale);
515 for (i=0;i<buffersize;i++) {
516 *acc++ = r;
517 *acc++ = g;
518 *acc++ = b;
519 *acc++ = a;
520 }
521 }
522 }
523
524 /* update optimized accum state vars */
525 if (ctx->Accum.ClearColor[0] == 0.0 && ctx->Accum.ClearColor[1] == 0.0 &&
526 ctx->Accum.ClearColor[2] == 0.0 && ctx->Accum.ClearColor[3] == 0.0) {
527 #ifdef USE_OPTIMIZED_ACCUM
528 ctx->IntegerAccumMode = GL_TRUE;
529 #else
530 ctx->IntegerAccumMode = GL_FALSE;
531 #endif
532 ctx->IntegerAccumScaler = 0.0; /* denotes empty accum buffer */
533 }
534 else {
535 ctx->IntegerAccumMode = GL_FALSE;
536 }
537 }
538 }