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