added extra check to optimized GL_RETURN case
[mesa.git] / src / mesa / main / accum.c
1 /* $Id: accum.c,v 1.22 2000/07/14 04:15:37 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 if (value != 0.0F) {
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 if (value != 1.0F) {
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 if (value == 0.0F)
227 return;
228
229 (*ctx->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
230 ctx->Pixel.DriverReadBuffer );
231
232 /* May have to leave optimized accum buffer mode */
233 if (ctx->IntegerAccumScaler == 0.0 && value > 0.0 && value <= 1.0)
234 ctx->IntegerAccumScaler = value;
235 if (ctx->IntegerAccumMode && value != ctx->IntegerAccumScaler)
236 rescale_accum(ctx);
237
238 if (ctx->IntegerAccumMode) {
239 /* simply add integer color values into accum buffer */
240 GLuint j;
241 GLaccum *acc = ctx->DrawBuffer->Accum + ypos * width4 + xpos * 4;
242 assert(ctx->IntegerAccumScaler > 0.0);
243 assert(ctx->IntegerAccumScaler <= 1.0);
244 for (j = 0; j < height; j++) {
245
246 GLuint i, i4;
247 gl_read_rgba_span(ctx, ctx->DrawBuffer, width, xpos, ypos, rgba);
248 for (i = i4 = 0; i < width; i++, i4+=4) {
249 acc[i4+0] += rgba[i][RCOMP];
250 acc[i4+1] += rgba[i][GCOMP];
251 acc[i4+2] += rgba[i][BCOMP];
252 acc[i4+3] += rgba[i][ACOMP];
253 }
254 acc += width4;
255 ypos++;
256 }
257 }
258 else {
259 /* scaled integer accum buffer */
260 const GLfloat rscale = value * acc_scale / fChanMax;
261 const GLfloat gscale = value * acc_scale / fChanMax;
262 const GLfloat bscale = value * acc_scale / fChanMax;
263 const GLfloat ascale = value * acc_scale / fChanMax;
264 GLuint j;
265 for (j=0;j<height;j++) {
266 GLaccum *acc = ctx->DrawBuffer->Accum + ypos * width4 + xpos * 4;
267 GLuint i;
268 gl_read_rgba_span(ctx, ctx->DrawBuffer, width, xpos, ypos, rgba);
269 for (i=0;i<width;i++) {
270 *acc += (GLaccum) ( (GLfloat) rgba[i][RCOMP] * rscale ); acc++;
271 *acc += (GLaccum) ( (GLfloat) rgba[i][GCOMP] * gscale ); acc++;
272 *acc += (GLaccum) ( (GLfloat) rgba[i][BCOMP] * bscale ); acc++;
273 *acc += (GLaccum) ( (GLfloat) rgba[i][ACOMP] * ascale ); acc++;
274 }
275 ypos++;
276 }
277 }
278 /* restore read buffer = draw buffer (the default) */
279 (*ctx->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
280 ctx->Color.DriverDrawBuffer );
281 break;
282
283 case GL_LOAD:
284 (*ctx->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
285 ctx->Pixel.DriverReadBuffer );
286
287 /* This is a change to go into optimized accum buffer mode */
288 if (value > 0.0 && value <= 1.0) {
289 #ifdef USE_OPTIMIZED_ACCUM
290 ctx->IntegerAccumMode = GL_TRUE;
291 #else
292 ctx->IntegerAccumMode = GL_FALSE;
293 #endif
294 ctx->IntegerAccumScaler = value;
295 }
296 else {
297 ctx->IntegerAccumMode = GL_FALSE;
298 ctx->IntegerAccumScaler = 0.0;
299 }
300
301 if (ctx->IntegerAccumMode) {
302 /* just copy values into accum buffer */
303 GLuint j;
304 GLaccum *acc = ctx->DrawBuffer->Accum + ypos * width4 + xpos * 4;
305 assert(ctx->IntegerAccumScaler > 0.0);
306 assert(ctx->IntegerAccumScaler <= 1.0);
307 for (j = 0; j < height; j++) {
308 GLuint i, i4;
309 gl_read_rgba_span(ctx, ctx->DrawBuffer, width, xpos, ypos, rgba);
310 for (i = i4 = 0; i < width; i++, i4 += 4) {
311 acc[i4+0] = rgba[i][RCOMP];
312 acc[i4+1] = rgba[i][GCOMP];
313 acc[i4+2] = rgba[i][BCOMP];
314 acc[i4+3] = rgba[i][ACOMP];
315 }
316 acc += width4;
317 ypos++;
318 }
319 }
320 else {
321 /* scaled integer accum buffer */
322 const GLfloat rscale = value * acc_scale / fChanMax;
323 const GLfloat gscale = value * acc_scale / fChanMax;
324 const GLfloat bscale = value * acc_scale / fChanMax;
325 const GLfloat ascale = value * acc_scale / fChanMax;
326 const GLfloat d = 3.0 / acc_scale;
327 GLuint i, j;
328 for (j = 0; j < height; j++) {
329 GLaccum *acc = ctx->DrawBuffer->Accum + ypos * width4 + xpos * 4;
330 gl_read_rgba_span(ctx, ctx->DrawBuffer, width, xpos, ypos, rgba);
331 for (i=0;i<width;i++) {
332 *acc++ = (GLaccum) ((GLfloat) rgba[i][RCOMP] * rscale + d);
333 *acc++ = (GLaccum) ((GLfloat) rgba[i][GCOMP] * gscale + d);
334 *acc++ = (GLaccum) ((GLfloat) rgba[i][BCOMP] * bscale + d);
335 *acc++ = (GLaccum) ((GLfloat) rgba[i][ACOMP] * ascale + d);
336 }
337 ypos++;
338 }
339 }
340
341 /* restore read buffer = draw buffer (the default) */
342 (*ctx->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
343 ctx->Color.DriverDrawBuffer );
344 break;
345
346 case GL_RETURN:
347 /* May have to leave optimized accum buffer mode */
348 if (ctx->IntegerAccumMode && value != 1.0)
349 rescale_accum(ctx);
350
351 if (ctx->IntegerAccumMode && ctx->IntegerAccumScaler > 0
352 && (GLint) (256 / ctx->IntegerAccumScaler) <= 32768) {
353 /* build lookup table to avoid many floating point multiplies */
354 const GLfloat mult = ctx->IntegerAccumScaler;
355 static GLchan multTable[32768];
356 static GLfloat prevMult = 0.0;
357 GLuint j;
358 const GLint max = (GLint) (256 / mult);
359 if (mult != prevMult) {
360 assert(max <= 32768);
361 for (j = 0; j < max; j++)
362 multTable[j] = (GLint) ((GLfloat) j * mult + 0.5F);
363 prevMult = mult;
364 }
365
366 assert(ctx->IntegerAccumScaler > 0.0);
367 assert(ctx->IntegerAccumScaler <= 1.0);
368 for (j = 0; j < height; j++) {
369 const GLaccum *acc = ctx->DrawBuffer->Accum + ypos * width4 + xpos*4;
370 GLuint i, i4;
371 for (i = i4 = 0; i < width; i++, i4 += 4) {
372 ASSERT(acc[i4+0] < max);
373 ASSERT(acc[i4+1] < max);
374 ASSERT(acc[i4+2] < max);
375 ASSERT(acc[i4+3] < max);
376 rgba[i][RCOMP] = multTable[acc[i4+0]];
377 rgba[i][GCOMP] = multTable[acc[i4+1]];
378 rgba[i][BCOMP] = multTable[acc[i4+2]];
379 rgba[i][ACOMP] = multTable[acc[i4+3]];
380 }
381 if (ctx->Color.SWmasking) {
382 _mesa_mask_rgba_span( ctx, width, xpos, ypos, rgba );
383 }
384 (*ctx->Driver.WriteRGBASpan)( ctx, width, xpos, ypos,
385 (const GLubyte (*)[4])rgba, NULL );
386 ypos++;
387 }
388 }
389 else {
390 const GLfloat rscale = value / acc_scale * fChanMax;
391 const GLfloat gscale = value / acc_scale * fChanMax;
392 const GLfloat bscale = value / acc_scale * fChanMax;
393 const GLfloat ascale = value / acc_scale * fChanMax;
394 GLuint i, j;
395 for (j=0;j<height;j++) {
396 const GLaccum *acc = ctx->DrawBuffer->Accum + ypos * width4 + xpos*4;
397 for (i=0;i<width;i++) {
398 GLint r, g, b, a;
399 r = (GLint) ( (GLfloat) (*acc++) * rscale + 0.5F );
400 g = (GLint) ( (GLfloat) (*acc++) * gscale + 0.5F );
401 b = (GLint) ( (GLfloat) (*acc++) * bscale + 0.5F );
402 a = (GLint) ( (GLfloat) (*acc++) * ascale + 0.5F );
403 rgba[i][RCOMP] = CLAMP( r, 0, iChanMax );
404 rgba[i][GCOMP] = CLAMP( g, 0, iChanMax );
405 rgba[i][BCOMP] = CLAMP( b, 0, iChanMax );
406 rgba[i][ACOMP] = CLAMP( a, 0, iChanMax );
407 }
408 if (ctx->Color.SWmasking) {
409 _mesa_mask_rgba_span( ctx, width, xpos, ypos, rgba );
410 }
411 (*ctx->Driver.WriteRGBASpan)( ctx, width, xpos, ypos,
412 (const GLubyte (*)[4])rgba, NULL );
413 ypos++;
414 }
415 }
416 break;
417
418 default:
419 gl_error( ctx, GL_INVALID_ENUM, "glAccum" );
420 }
421 }
422
423
424
425 /*
426 * Clear the accumulation Buffer.
427 */
428 void
429 _mesa_clear_accum_buffer( GLcontext *ctx )
430 {
431 GLuint buffersize;
432 GLfloat acc_scale;
433
434 if (ctx->Visual->AccumRedBits==0) {
435 /* No accumulation buffer! */
436 return;
437 }
438
439 if (sizeof(GLaccum)==1) {
440 acc_scale = 127.0;
441 }
442 else if (sizeof(GLaccum)==2) {
443 acc_scale = 32767.0;
444 }
445 else {
446 /* sizeof(GLaccum) > 2 (Cray) */
447 acc_scale = (float) SHRT_MAX;
448 }
449
450 /* number of pixels */
451 buffersize = ctx->DrawBuffer->Width * ctx->DrawBuffer->Height;
452
453 if (!ctx->DrawBuffer->Accum) {
454 /* try to alloc accumulation buffer */
455 ctx->DrawBuffer->Accum = (GLaccum *)
456 MALLOC( buffersize * 4 * sizeof(GLaccum) );
457 }
458
459 if (ctx->DrawBuffer->Accum) {
460 if (ctx->Scissor.Enabled) {
461 /* Limit clear to scissor box */
462 GLaccum r, g, b, a;
463 GLint i, j;
464 GLint width, height;
465 GLaccum *row;
466 r = (GLaccum) (ctx->Accum.ClearColor[0] * acc_scale);
467 g = (GLaccum) (ctx->Accum.ClearColor[1] * acc_scale);
468 b = (GLaccum) (ctx->Accum.ClearColor[2] * acc_scale);
469 a = (GLaccum) (ctx->Accum.ClearColor[3] * acc_scale);
470 /* size of region to clear */
471 width = 4 * (ctx->DrawBuffer->Xmax - ctx->DrawBuffer->Xmin + 1);
472 height = ctx->DrawBuffer->Ymax - ctx->DrawBuffer->Ymin + 1;
473 /* ptr to first element to clear */
474 row = ctx->DrawBuffer->Accum
475 + 4 * (ctx->DrawBuffer->Ymin * ctx->DrawBuffer->Width
476 + ctx->DrawBuffer->Xmin);
477 for (j=0;j<height;j++) {
478 for (i=0;i<width;i+=4) {
479 row[i+0] = r;
480 row[i+1] = g;
481 row[i+2] = b;
482 row[i+3] = a;
483 }
484 row += 4 * ctx->DrawBuffer->Width;
485 }
486 }
487 else {
488 /* clear whole buffer */
489 if (ctx->Accum.ClearColor[0]==0.0 &&
490 ctx->Accum.ClearColor[1]==0.0 &&
491 ctx->Accum.ClearColor[2]==0.0 &&
492 ctx->Accum.ClearColor[3]==0.0) {
493 /* Black */
494 BZERO( ctx->DrawBuffer->Accum, buffersize * 4 * sizeof(GLaccum) );
495 }
496 else {
497 /* Not black */
498 GLaccum *acc, r, g, b, a;
499 GLuint i;
500
501 acc = ctx->DrawBuffer->Accum;
502 r = (GLaccum) (ctx->Accum.ClearColor[0] * acc_scale);
503 g = (GLaccum) (ctx->Accum.ClearColor[1] * acc_scale);
504 b = (GLaccum) (ctx->Accum.ClearColor[2] * acc_scale);
505 a = (GLaccum) (ctx->Accum.ClearColor[3] * acc_scale);
506 for (i=0;i<buffersize;i++) {
507 *acc++ = r;
508 *acc++ = g;
509 *acc++ = b;
510 *acc++ = a;
511 }
512 }
513 }
514
515 /* update optimized accum state vars */
516 if (ctx->Accum.ClearColor[0] == 0.0 && ctx->Accum.ClearColor[1] == 0.0 &&
517 ctx->Accum.ClearColor[2] == 0.0 && ctx->Accum.ClearColor[3] == 0.0) {
518 #ifdef USE_OPTIMIZED_ACCUM
519 ctx->IntegerAccumMode = GL_TRUE;
520 #else
521 ctx->IntegerAccumMode = GL_FALSE;
522 #endif
523 ctx->IntegerAccumScaler = 0.0; /* denotes empty accum buffer */
524 }
525 else {
526 ctx->IntegerAccumMode = GL_FALSE;
527 }
528 }
529 }