Minor fixes for Win32 (Karl Schultz).
[mesa.git] / src / mesa / swrast / s_accum.c
1 /* $Id: s_accum.c,v 1.11 2001/04/20 19:21:41 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999-2001 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 "glheader.h"
29 #include "context.h"
30 #include "macros.h"
31 #include "mmath.h"
32 #include "mem.h"
33
34 #include "s_accum.h"
35 #include "s_alphabuf.h"
36 #include "s_context.h"
37 #include "s_masking.h"
38 #include "s_span.h"
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 #if CHAN_BITS == 8
66 #define USE_OPTIMIZED_ACCUM /* enable the optimization */
67 #endif
68
69
70
71 void
72 _mesa_alloc_accum_buffer( GLcontext *ctx )
73 {
74 SWcontext *swrast = SWRAST_CONTEXT(ctx);
75 GLint n;
76
77 if (ctx->DrawBuffer->Accum) {
78 FREE( ctx->DrawBuffer->Accum );
79 ctx->DrawBuffer->Accum = NULL;
80 }
81
82 /* allocate accumulation buffer if not already present */
83 n = ctx->DrawBuffer->Width * ctx->DrawBuffer->Height * 4 * sizeof(GLaccum);
84 ctx->DrawBuffer->Accum = (GLaccum *) MALLOC( n );
85 if (!ctx->DrawBuffer->Accum) {
86 /* unable to setup accumulation buffer */
87 _mesa_error( ctx, GL_OUT_OF_MEMORY, "glAccum" );
88 }
89 #ifdef USE_OPTIMIZED_ACCUM
90 swrast->_IntegerAccumMode = GL_TRUE;
91 #else
92 swrast->_IntegerAccumMode = GL_FALSE;
93 #endif
94 swrast->_IntegerAccumScaler = 0.0;
95 }
96
97
98
99
100
101
102 /*
103 * This is called when we fall out of optimized/unscaled accum buffer mode.
104 * That is, we convert each unscaled accum buffer value into a scaled value
105 * representing the range[-1, 1].
106 */
107 static void rescale_accum( GLcontext *ctx )
108 {
109 SWcontext *swrast = SWRAST_CONTEXT(ctx);
110 const GLuint n = ctx->DrawBuffer->Width * ctx->DrawBuffer->Height * 4;
111 const GLfloat fChanMax = (1 << (sizeof(GLchan) * 8)) - 1;
112 const GLfloat s = swrast->_IntegerAccumScaler * (32767.0 / fChanMax);
113 GLaccum *accum = ctx->DrawBuffer->Accum;
114 GLuint i;
115
116 assert(swrast->_IntegerAccumMode);
117 assert(accum);
118
119 for (i = 0; i < n; i++) {
120 accum[i] = (GLaccum) (accum[i] * s);
121 }
122
123 swrast->_IntegerAccumMode = GL_FALSE;
124 }
125
126
127
128
129
130
131 /*
132 * Clear the accumulation Buffer.
133 */
134 void
135 _mesa_clear_accum_buffer( GLcontext *ctx )
136 {
137 SWcontext *swrast = SWRAST_CONTEXT(ctx);
138 GLuint buffersize;
139 GLfloat acc_scale;
140
141 if (ctx->Visual.accumRedBits==0) {
142 /* No 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 /* number of pixels */
158 buffersize = ctx->DrawBuffer->Width * ctx->DrawBuffer->Height;
159
160 if (!ctx->DrawBuffer->Accum) {
161 /* try to alloc accumulation buffer */
162 ctx->DrawBuffer->Accum = (GLaccum *)
163 MALLOC( buffersize * 4 * sizeof(GLaccum) );
164 }
165
166 if (ctx->DrawBuffer->Accum) {
167 if (ctx->Scissor.Enabled) {
168 /* Limit clear to scissor box */
169 GLaccum r, g, b, a;
170 GLint i, j;
171 GLint width, height;
172 GLaccum *row;
173 r = (GLaccum) (ctx->Accum.ClearColor[0] * acc_scale);
174 g = (GLaccum) (ctx->Accum.ClearColor[1] * acc_scale);
175 b = (GLaccum) (ctx->Accum.ClearColor[2] * acc_scale);
176 a = (GLaccum) (ctx->Accum.ClearColor[3] * acc_scale);
177 /* size of region to clear */
178 width = 4 * (ctx->DrawBuffer->_Xmax - ctx->DrawBuffer->_Xmin);
179 height = ctx->DrawBuffer->_Ymax - ctx->DrawBuffer->_Ymin;
180 /* ptr to first element to clear */
181 row = ctx->DrawBuffer->Accum
182 + 4 * (ctx->DrawBuffer->_Ymin * ctx->DrawBuffer->Width
183 + ctx->DrawBuffer->_Xmin);
184 for (j=0;j<height;j++) {
185 for (i=0;i<width;i+=4) {
186 row[i+0] = r;
187 row[i+1] = g;
188 row[i+2] = b;
189 row[i+3] = a;
190 }
191 row += 4 * ctx->DrawBuffer->Width;
192 }
193 }
194 else {
195 /* clear whole buffer */
196 if (ctx->Accum.ClearColor[0]==0.0 &&
197 ctx->Accum.ClearColor[1]==0.0 &&
198 ctx->Accum.ClearColor[2]==0.0 &&
199 ctx->Accum.ClearColor[3]==0.0) {
200 /* Black */
201 BZERO( ctx->DrawBuffer->Accum, buffersize * 4 * sizeof(GLaccum) );
202 }
203 else {
204 /* Not black */
205 GLaccum *acc, r, g, b, a;
206 GLuint i;
207
208 acc = ctx->DrawBuffer->Accum;
209 r = (GLaccum) (ctx->Accum.ClearColor[0] * acc_scale);
210 g = (GLaccum) (ctx->Accum.ClearColor[1] * acc_scale);
211 b = (GLaccum) (ctx->Accum.ClearColor[2] * acc_scale);
212 a = (GLaccum) (ctx->Accum.ClearColor[3] * acc_scale);
213 for (i=0;i<buffersize;i++) {
214 *acc++ = r;
215 *acc++ = g;
216 *acc++ = b;
217 *acc++ = a;
218 }
219 }
220 }
221
222 /* update optimized accum state vars */
223 if (ctx->Accum.ClearColor[0] == 0.0 && ctx->Accum.ClearColor[1] == 0.0 &&
224 ctx->Accum.ClearColor[2] == 0.0 && ctx->Accum.ClearColor[3] == 0.0) {
225 #ifdef USE_OPTIMIZED_ACCUM
226 swrast->_IntegerAccumMode = GL_TRUE;
227 #else
228 swrast->_IntegerAccumMode = GL_FALSE;
229 #endif
230 swrast->_IntegerAccumScaler = 0.0; /* denotes empty accum buffer */
231 }
232 else {
233 swrast->_IntegerAccumMode = GL_FALSE;
234 }
235 }
236 }
237
238
239 void
240 _swrast_Accum( GLcontext *ctx, GLenum op, GLfloat value,
241 GLint xpos, GLint ypos,
242 GLint width, GLint height )
243
244 {
245 SWcontext *swrast = SWRAST_CONTEXT(ctx);
246 GLuint width4;
247 GLfloat acc_scale;
248 GLchan rgba[MAX_WIDTH][4];
249 const GLuint colorMask = *((GLuint *) &ctx->Color.ColorMask);
250 const GLint iChanMax = (1 << (sizeof(GLchan) * 8)) - 1;
251 const GLfloat fChanMax = (1 << (sizeof(GLchan) * 8)) - 1;
252
253
254 if (SWRAST_CONTEXT(ctx)->NewState)
255 _swrast_validate_derived( ctx );
256
257 if (!ctx->DrawBuffer->Accum) {
258 _mesa_warning(ctx,
259 "Calling glAccum() without an accumulation "
260 "buffer (low memory?)");
261 return;
262 }
263
264 if (sizeof(GLaccum)==1) {
265 acc_scale = 127.0;
266 }
267 else if (sizeof(GLaccum)==2) {
268 acc_scale = 32767.0;
269 }
270 else {
271 /* sizeof(GLaccum) > 2 (Cray) */
272 acc_scale = (float) SHRT_MAX;
273 }
274
275 width4 = 4 * width;
276
277 switch (op) {
278 case GL_ADD:
279 if (value != 0.0F) {
280 const GLaccum intVal = (GLaccum) (value * acc_scale);
281 GLint j;
282 /* Leave optimized accum buffer mode */
283 if (swrast->_IntegerAccumMode)
284 rescale_accum(ctx);
285 for (j = 0; j < height; j++) {
286 GLaccum * acc = ctx->DrawBuffer->Accum + ypos * width4 + 4 * xpos;
287 GLuint i;
288 for (i = 0; i < width4; i++) {
289 acc[i] += intVal;
290 }
291 ypos++;
292 }
293 }
294 break;
295
296 case GL_MULT:
297 if (value != 1.0F) {
298 GLint j;
299 /* Leave optimized accum buffer mode */
300 if (swrast->_IntegerAccumMode)
301 rescale_accum(ctx);
302 for (j = 0; j < height; j++) {
303 GLaccum *acc = ctx->DrawBuffer->Accum + ypos * width4 + 4 * xpos;
304 GLuint i;
305 for (i = 0; i < width4; i++) {
306 acc[i] = (GLaccum) ( (GLfloat) acc[i] * value );
307 }
308 ypos++;
309 }
310 }
311 break;
312
313 case GL_ACCUM:
314 if (value == 0.0F)
315 return;
316
317 (*swrast->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
318 ctx->Pixel.DriverReadBuffer );
319
320 /* May have to leave optimized accum buffer mode */
321 if (swrast->_IntegerAccumScaler == 0.0 && value > 0.0 && value <= 1.0)
322 swrast->_IntegerAccumScaler = value;
323 if (swrast->_IntegerAccumMode && value != swrast->_IntegerAccumScaler)
324 rescale_accum(ctx);
325
326 RENDER_START(swrast,ctx);
327
328 if (swrast->_IntegerAccumMode) {
329 /* simply add integer color values into accum buffer */
330 GLint j;
331 GLaccum *acc = ctx->DrawBuffer->Accum + ypos * width4 + xpos * 4;
332 assert(swrast->_IntegerAccumScaler > 0.0);
333 assert(swrast->_IntegerAccumScaler <= 1.0);
334 for (j = 0; j < height; j++) {
335
336 GLint i, i4;
337 _mesa_read_rgba_span(ctx, ctx->DrawBuffer, width, xpos, ypos, rgba);
338 for (i = i4 = 0; i < width; i++, i4+=4) {
339 acc[i4+0] += rgba[i][RCOMP];
340 acc[i4+1] += rgba[i][GCOMP];
341 acc[i4+2] += rgba[i][BCOMP];
342 acc[i4+3] += rgba[i][ACOMP];
343 }
344 acc += width4;
345 ypos++;
346 }
347 }
348 else {
349 /* scaled integer accum buffer */
350 const GLfloat rscale = value * acc_scale / fChanMax;
351 const GLfloat gscale = value * acc_scale / fChanMax;
352 const GLfloat bscale = value * acc_scale / fChanMax;
353 const GLfloat ascale = value * acc_scale / fChanMax;
354 GLint j;
355 for (j=0;j<height;j++) {
356 GLaccum *acc = ctx->DrawBuffer->Accum + ypos * width4 + xpos * 4;
357 GLint i;
358 _mesa_read_rgba_span(ctx, ctx->DrawBuffer, width, xpos, ypos, rgba);
359 for (i=0;i<width;i++) {
360 *acc += (GLaccum) ( (GLfloat) rgba[i][RCOMP] * rscale ); acc++;
361 *acc += (GLaccum) ( (GLfloat) rgba[i][GCOMP] * gscale ); acc++;
362 *acc += (GLaccum) ( (GLfloat) rgba[i][BCOMP] * bscale ); acc++;
363 *acc += (GLaccum) ( (GLfloat) rgba[i][ACOMP] * ascale ); acc++;
364 }
365 ypos++;
366 }
367 }
368 /* restore read buffer = draw buffer (the default) */
369 (*swrast->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
370 ctx->Color.DriverDrawBuffer );
371 RENDER_FINISH(swrast,ctx);
372 break;
373
374 case GL_LOAD:
375 (*swrast->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
376 ctx->Pixel.DriverReadBuffer );
377
378 /* This is a change to go into optimized accum buffer mode */
379 if (value > 0.0 && value <= 1.0) {
380 #ifdef USE_OPTIMIZED_ACCUM
381 swrast->_IntegerAccumMode = GL_TRUE;
382 #else
383 swrast->_IntegerAccumMode = GL_FALSE;
384 #endif
385 swrast->_IntegerAccumScaler = value;
386 }
387 else {
388 swrast->_IntegerAccumMode = GL_FALSE;
389 swrast->_IntegerAccumScaler = 0.0;
390 }
391
392 RENDER_START(swrast,ctx);
393 if (swrast->_IntegerAccumMode) {
394 /* just copy values into accum buffer */
395 GLint j;
396 GLaccum *acc = ctx->DrawBuffer->Accum + ypos * width4 + xpos * 4;
397 assert(swrast->_IntegerAccumScaler > 0.0);
398 assert(swrast->_IntegerAccumScaler <= 1.0);
399 for (j = 0; j < height; j++) {
400 GLint i, i4;
401 _mesa_read_rgba_span(ctx, ctx->DrawBuffer, width, xpos, ypos, rgba);
402 for (i = i4 = 0; i < width; i++, i4 += 4) {
403 acc[i4+0] = rgba[i][RCOMP];
404 acc[i4+1] = rgba[i][GCOMP];
405 acc[i4+2] = rgba[i][BCOMP];
406 acc[i4+3] = rgba[i][ACOMP];
407 }
408 acc += width4;
409 ypos++;
410 }
411 }
412 else {
413 /* scaled integer accum buffer */
414 const GLfloat rscale = value * acc_scale / fChanMax;
415 const GLfloat gscale = value * acc_scale / fChanMax;
416 const GLfloat bscale = value * acc_scale / fChanMax;
417 const GLfloat ascale = value * acc_scale / fChanMax;
418 const GLfloat d = 3.0 / acc_scale;
419 GLint i, j;
420 for (j = 0; j < height; j++) {
421 GLaccum *acc = ctx->DrawBuffer->Accum + ypos * width4 + xpos * 4;
422 _mesa_read_rgba_span(ctx, ctx->DrawBuffer, width, xpos, ypos, rgba);
423 for (i=0;i<width;i++) {
424 *acc++ = (GLaccum) ((GLfloat) rgba[i][RCOMP] * rscale + d);
425 *acc++ = (GLaccum) ((GLfloat) rgba[i][GCOMP] * gscale + d);
426 *acc++ = (GLaccum) ((GLfloat) rgba[i][BCOMP] * bscale + d);
427 *acc++ = (GLaccum) ((GLfloat) rgba[i][ACOMP] * ascale + d);
428 }
429 ypos++;
430 }
431 }
432
433 /* restore read buffer = draw buffer (the default) */
434 (*swrast->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
435 ctx->Color.DriverDrawBuffer );
436 RENDER_FINISH(swrast,ctx);
437 break;
438
439 case GL_RETURN:
440 /* May have to leave optimized accum buffer mode */
441 if (swrast->_IntegerAccumMode && value != 1.0)
442 rescale_accum(ctx);
443
444 RENDER_START(swrast,ctx);
445 if (swrast->_IntegerAccumMode && swrast->_IntegerAccumScaler > 0) {
446 /* build lookup table to avoid many floating point multiplies */
447 static GLchan multTable[32768];
448 static GLfloat prevMult = 0.0;
449 const GLfloat mult = swrast->_IntegerAccumScaler;
450 const GLint max = MIN2((GLint) (256 / mult), 32767);
451 GLint j;
452 if (mult != prevMult) {
453 for (j = 0; j < max; j++)
454 multTable[j] = IROUND((GLfloat) j * mult);
455 prevMult = mult;
456 }
457
458 assert(swrast->_IntegerAccumScaler > 0.0);
459 assert(swrast->_IntegerAccumScaler <= 1.0);
460 for (j = 0; j < height; j++) {
461 const GLaccum *acc = ctx->DrawBuffer->Accum + ypos * width4 + xpos*4;
462 GLint i, i4;
463 for (i = i4 = 0; i < width; i++, i4 += 4) {
464 ASSERT(acc[i4+0] < max);
465 ASSERT(acc[i4+1] < max);
466 ASSERT(acc[i4+2] < max);
467 ASSERT(acc[i4+3] < max);
468 rgba[i][RCOMP] = multTable[acc[i4+0]];
469 rgba[i][GCOMP] = multTable[acc[i4+1]];
470 rgba[i][BCOMP] = multTable[acc[i4+2]];
471 rgba[i][ACOMP] = multTable[acc[i4+3]];
472 }
473 if (colorMask != 0xffffffff) {
474 _mesa_mask_rgba_span( ctx, width, xpos, ypos, rgba );
475 }
476 (*swrast->Driver.WriteRGBASpan)( ctx, width, xpos, ypos,
477 (const GLchan (*)[4])rgba, NULL );
478 if (ctx->DrawBuffer->UseSoftwareAlphaBuffers
479 && ctx->Color.ColorMask[ACOMP]) {
480 _mesa_write_alpha_span(ctx, width, xpos, ypos,
481 (CONST GLchan (*)[4]) rgba, NULL);
482 }
483 ypos++;
484 }
485 }
486 else {
487 const GLfloat rscale = value / acc_scale * fChanMax;
488 const GLfloat gscale = value / acc_scale * fChanMax;
489 const GLfloat bscale = value / acc_scale * fChanMax;
490 const GLfloat ascale = value / acc_scale * fChanMax;
491 GLint i, j;
492 for (j=0;j<height;j++) {
493 const GLaccum *acc = ctx->DrawBuffer->Accum + ypos * width4 + xpos*4;
494 for (i=0;i<width;i++) {
495 GLint r = IROUND( (GLfloat) (acc[0]) * rscale );
496 GLint g = IROUND( (GLfloat) (acc[1]) * gscale );
497 GLint b = IROUND( (GLfloat) (acc[2]) * bscale );
498 GLint a = IROUND( (GLfloat) (acc[3]) * ascale );
499 acc += 4;
500 rgba[i][RCOMP] = CLAMP( r, 0, iChanMax );
501 rgba[i][GCOMP] = CLAMP( g, 0, iChanMax );
502 rgba[i][BCOMP] = CLAMP( b, 0, iChanMax );
503 rgba[i][ACOMP] = CLAMP( a, 0, iChanMax );
504 }
505 if (colorMask != 0xffffffff) {
506 _mesa_mask_rgba_span( ctx, width, xpos, ypos, rgba );
507 }
508 (*swrast->Driver.WriteRGBASpan)( ctx, width, xpos, ypos,
509 (const GLchan (*)[4])rgba, NULL );
510 if (ctx->DrawBuffer->UseSoftwareAlphaBuffers
511 && ctx->Color.ColorMask[ACOMP]) {
512 _mesa_write_alpha_span(ctx, width, xpos, ypos,
513 (CONST GLchan (*)[4]) rgba, NULL);
514 }
515 ypos++;
516 }
517 }
518 RENDER_FINISH(swrast,ctx);
519 break;
520
521 default:
522 _mesa_error( ctx, GL_INVALID_ENUM, "glAccum" );
523 }
524 }