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