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