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