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