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