clean-up to reduce MSVC warnings
[mesa.git] / src / mesa / main / accum.c
1 /* $Id: accum.c,v 1.9 1999/11/03 19:27:41 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 <assert.h>
35 #include <limits.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #else
39 #include "GL/xf86glx.h"
40 #endif
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 many floating point multiplies */
346 const GLfloat mult = ctx->IntegerAccumScaler;
347 static GLchan multTable[32768];
348 static GLfloat prevMult = 0.0;
349 GLuint j;
350 const GLint max = 256 / mult;
351 if (mult != prevMult) {
352 assert(max <= 32768);
353 for (j = 0; j < max; j++)
354 multTable[j] = (GLint) ((GLfloat) j * mult + 0.5F);
355 prevMult = mult;
356 }
357
358 assert(ctx->IntegerAccumScaler > 0.0);
359 assert(ctx->IntegerAccumScaler <= 1.0);
360 for (j = 0; j < height; j++) {
361 const GLaccum *acc = ctx->Buffer->Accum + ypos * width4 + xpos*4;
362 GLuint i, i4;
363 for (i = i4 = 0; i < width; i++, i4 += 4) {
364 ASSERT(acc[i4+0] < max);
365 ASSERT(acc[i4+1] < max);
366 ASSERT(acc[i4+2] < max);
367 ASSERT(acc[i4+3] < max);
368 rgba[i][RCOMP] = multTable[acc[i4+0]];
369 rgba[i][GCOMP] = multTable[acc[i4+1]];
370 rgba[i][BCOMP] = multTable[acc[i4+2]];
371 rgba[i][ACOMP] = multTable[acc[i4+3]];
372 }
373 if (ctx->Color.SWmasking) {
374 gl_mask_rgba_span( ctx, width, xpos, ypos, rgba );
375 }
376 (*ctx->Driver.WriteRGBASpan)( ctx, width, xpos, ypos,
377 (const GLubyte (*)[4])rgba, NULL );
378 ypos++;
379 }
380 }
381 else {
382 const GLfloat rscale = value / acc_scale * fChanMax;
383 const GLfloat gscale = value / acc_scale * fChanMax;
384 const GLfloat bscale = value / acc_scale * fChanMax;
385 const GLfloat ascale = value / acc_scale * fChanMax;
386 GLuint i, j;
387 for (j=0;j<height;j++) {
388 const GLaccum *acc = ctx->Buffer->Accum + ypos * width4 + xpos*4;
389 for (i=0;i<width;i++) {
390 GLint r, g, b, a;
391 r = (GLint) ( (GLfloat) (*acc++) * rscale + 0.5F );
392 g = (GLint) ( (GLfloat) (*acc++) * gscale + 0.5F );
393 b = (GLint) ( (GLfloat) (*acc++) * bscale + 0.5F );
394 a = (GLint) ( (GLfloat) (*acc++) * ascale + 0.5F );
395 rgba[i][RCOMP] = CLAMP( r, 0, iChanMax );
396 rgba[i][GCOMP] = CLAMP( g, 0, iChanMax );
397 rgba[i][BCOMP] = CLAMP( b, 0, iChanMax );
398 rgba[i][ACOMP] = CLAMP( a, 0, iChanMax );
399 }
400 if (ctx->Color.SWmasking) {
401 gl_mask_rgba_span( ctx, width, xpos, ypos, rgba );
402 }
403 (*ctx->Driver.WriteRGBASpan)( ctx, width, xpos, ypos,
404 (const GLubyte (*)[4])rgba, NULL );
405 ypos++;
406 }
407 }
408 break;
409
410 default:
411 gl_error( ctx, GL_INVALID_ENUM, "glAccum" );
412 }
413 }
414
415
416
417 /*
418 * Clear the accumulation Buffer.
419 */
420 void gl_clear_accum_buffer( GLcontext *ctx )
421 {
422 GLuint buffersize;
423 GLfloat acc_scale;
424
425 if (ctx->Visual->AccumBits==0) {
426 /* No accumulation buffer! */
427 return;
428 }
429
430 if (sizeof(GLaccum)==1) {
431 acc_scale = 127.0;
432 }
433 else if (sizeof(GLaccum)==2) {
434 acc_scale = 32767.0;
435 }
436 else {
437 /* sizeof(GLaccum) > 2 (Cray) */
438 acc_scale = (float) SHRT_MAX;
439 }
440
441 /* number of pixels */
442 buffersize = ctx->Buffer->Width * ctx->Buffer->Height;
443
444 if (!ctx->Buffer->Accum) {
445 /* try to alloc accumulation buffer */
446 ctx->Buffer->Accum = (GLaccum *)
447 MALLOC( buffersize * 4 * sizeof(GLaccum) );
448 }
449
450 if (ctx->Buffer->Accum) {
451 if (ctx->Scissor.Enabled) {
452 /* Limit clear to scissor box */
453 GLaccum r, g, b, a;
454 GLint i, j;
455 GLint width, height;
456 GLaccum *row;
457 r = (GLaccum) (ctx->Accum.ClearColor[0] * acc_scale);
458 g = (GLaccum) (ctx->Accum.ClearColor[1] * acc_scale);
459 b = (GLaccum) (ctx->Accum.ClearColor[2] * acc_scale);
460 a = (GLaccum) (ctx->Accum.ClearColor[3] * acc_scale);
461 /* size of region to clear */
462 width = 4 * (ctx->Buffer->Xmax - ctx->Buffer->Xmin + 1);
463 height = ctx->Buffer->Ymax - ctx->Buffer->Ymin + 1;
464 /* ptr to first element to clear */
465 row = ctx->Buffer->Accum
466 + 4 * (ctx->Buffer->Ymin * ctx->Buffer->Width
467 + ctx->Buffer->Xmin);
468 for (j=0;j<height;j++) {
469 for (i=0;i<width;i+=4) {
470 row[i+0] = r;
471 row[i+1] = g;
472 row[i+2] = b;
473 row[i+3] = a;
474 }
475 row += 4 * ctx->Buffer->Width;
476 }
477 }
478 else {
479 /* clear whole buffer */
480 if (ctx->Accum.ClearColor[0]==0.0 &&
481 ctx->Accum.ClearColor[1]==0.0 &&
482 ctx->Accum.ClearColor[2]==0.0 &&
483 ctx->Accum.ClearColor[3]==0.0) {
484 /* Black */
485 MEMSET( ctx->Buffer->Accum, 0, buffersize * 4 * sizeof(GLaccum) );
486 }
487 else {
488 /* Not black */
489 GLaccum *acc, r, g, b, a;
490 GLuint i;
491
492 acc = ctx->Buffer->Accum;
493 r = (GLaccum) (ctx->Accum.ClearColor[0] * acc_scale);
494 g = (GLaccum) (ctx->Accum.ClearColor[1] * acc_scale);
495 b = (GLaccum) (ctx->Accum.ClearColor[2] * acc_scale);
496 a = (GLaccum) (ctx->Accum.ClearColor[3] * acc_scale);
497 for (i=0;i<buffersize;i++) {
498 *acc++ = r;
499 *acc++ = g;
500 *acc++ = b;
501 *acc++ = a;
502 }
503 }
504 }
505
506 /* update optimized accum state vars */
507 if (ctx->Accum.ClearColor[0] == 0.0 && ctx->Accum.ClearColor[1] == 0.0 &&
508 ctx->Accum.ClearColor[2] == 0.0 && ctx->Accum.ClearColor[3] == 0.0) {
509 #ifdef USE_OPTIMIZED_ACCUM
510 ctx->IntegerAccumMode = GL_TRUE;
511 #else
512 ctx->IntegerAccumMode = GL_FALSE;
513 #endif
514 ctx->IntegerAccumScaler = 0.0; /* denotes empty accum buffer */
515 }
516 else {
517 ctx->IntegerAccumMode = GL_FALSE;
518 }
519 }
520 }