fixup vertex building code ..
[mesa.git] / src / mesa / swrast / s_accum.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.0.1
4 *
5 * Copyright (C) 1999-2004 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 #include "glheader.h"
27 #include "context.h"
28 #include "macros.h"
29 #include "imports.h"
30
31 #include "s_accum.h"
32 #include "s_alphabuf.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 && ACCUM_BITS < 32
63 #define USE_OPTIMIZED_ACCUM /* enable the optimization */
64 #endif
65
66
67 void
68 _swrast_alloc_accum_buffer( GLframebuffer *buffer )
69 {
70 GET_CURRENT_CONTEXT(ctx);
71 GLint n;
72
73 if (buffer->Accum) {
74 MESA_PBUFFER_FREE( buffer->Accum );
75 buffer->Accum = NULL;
76 }
77
78 /* allocate accumulation buffer if not already present */
79 n = buffer->Width * buffer->Height * 4 * sizeof(GLaccum);
80 buffer->Accum = (GLaccum *) MESA_PBUFFER_ALLOC( n );
81 if (!buffer->Accum) {
82 /* unable to setup accumulation buffer */
83 _mesa_error( NULL, GL_OUT_OF_MEMORY, "glAccum" );
84 }
85
86 if (ctx) {
87 SWcontext *swrast = SWRAST_CONTEXT(ctx);
88 /* XXX these fields should probably be in the GLframebuffer */
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 * This is called when we fall out of optimized/unscaled accum buffer mode.
101 * That is, we convert each unscaled accum buffer value into a scaled value
102 * representing the range[-1, 1].
103 */
104 static void rescale_accum( GLcontext *ctx )
105 {
106 SWcontext *swrast = SWRAST_CONTEXT(ctx);
107 const GLuint n = ctx->DrawBuffer->Width * ctx->DrawBuffer->Height * 4;
108 const GLfloat s = swrast->_IntegerAccumScaler * (32767.0F / CHAN_MAXF);
109 GLaccum *accum = ctx->DrawBuffer->Accum;
110 GLuint i;
111
112 assert(swrast->_IntegerAccumMode);
113 assert(accum);
114
115 for (i = 0; i < n; i++) {
116 accum[i] = (GLaccum) (accum[i] * s);
117 }
118
119 swrast->_IntegerAccumMode = GL_FALSE;
120 }
121
122
123
124
125
126
127 /*
128 * Clear the accumulation Buffer.
129 */
130 void
131 _swrast_clear_accum_buffer( GLcontext *ctx )
132 {
133 SWcontext *swrast = SWRAST_CONTEXT(ctx);
134 GLuint buffersize;
135 GLfloat acc_scale;
136
137 if (ctx->Visual.accumRedBits==0) {
138 /* No accumulation buffer! */
139 return;
140 }
141
142 if (sizeof(GLaccum)==1) {
143 acc_scale = 127.0;
144 }
145 else if (sizeof(GLaccum)==2) {
146 acc_scale = 32767.0;
147 }
148 else {
149 acc_scale = 1.0F;
150 }
151
152 /* number of pixels */
153 buffersize = ctx->DrawBuffer->Width * ctx->DrawBuffer->Height;
154
155 if (!ctx->DrawBuffer->Accum) {
156 /* try to alloc accumulation buffer */
157 ctx->DrawBuffer->Accum = (GLaccum *)
158 MALLOC( buffersize * 4 * sizeof(GLaccum) );
159 }
160
161 if (ctx->DrawBuffer->Accum) {
162 if (ctx->Scissor.Enabled) {
163 /* Limit clear to scissor box */
164 const GLaccum r = (GLaccum) (ctx->Accum.ClearColor[0] * acc_scale);
165 const GLaccum g = (GLaccum) (ctx->Accum.ClearColor[1] * acc_scale);
166 const GLaccum b = (GLaccum) (ctx->Accum.ClearColor[2] * acc_scale);
167 const GLaccum a = (GLaccum) (ctx->Accum.ClearColor[3] * acc_scale);
168 GLint i, j;
169 GLint width, height;
170 GLaccum *row;
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 _mesa_bzero( ctx->DrawBuffer->Accum,
196 buffersize * 4 * sizeof(GLaccum) );
197 }
198 else {
199 /* Not black */
200 const GLaccum r = (GLaccum) (ctx->Accum.ClearColor[0] * acc_scale);
201 const GLaccum g = (GLaccum) (ctx->Accum.ClearColor[1] * acc_scale);
202 const GLaccum b = (GLaccum) (ctx->Accum.ClearColor[2] * acc_scale);
203 const GLaccum a = (GLaccum) (ctx->Accum.ClearColor[3] * acc_scale);
204 GLaccum *acc = ctx->DrawBuffer->Accum;
205 GLuint i;
206 for (i=0;i<buffersize;i++) {
207 *acc++ = r;
208 *acc++ = g;
209 *acc++ = b;
210 *acc++ = a;
211 }
212 }
213 }
214
215 /* update optimized accum state vars */
216 if (ctx->Accum.ClearColor[0] == 0.0 && ctx->Accum.ClearColor[1] == 0.0 &&
217 ctx->Accum.ClearColor[2] == 0.0 && ctx->Accum.ClearColor[3] == 0.0) {
218 #ifdef USE_OPTIMIZED_ACCUM
219 swrast->_IntegerAccumMode = GL_TRUE;
220 #else
221 swrast->_IntegerAccumMode = GL_FALSE;
222 #endif
223 swrast->_IntegerAccumScaler = 0.0; /* denotes empty accum buffer */
224 }
225 else {
226 swrast->_IntegerAccumMode = GL_FALSE;
227 }
228 }
229 }
230
231
232 void
233 _swrast_Accum( GLcontext *ctx, GLenum op, GLfloat value,
234 GLint xpos, GLint ypos,
235 GLint width, GLint height )
236
237 {
238 SWcontext *swrast = SWRAST_CONTEXT(ctx);
239 GLuint width4;
240 GLfloat acc_scale;
241 GLchan rgba[MAX_WIDTH][4];
242 const GLuint colorMask = *((GLuint *) &ctx->Color.ColorMask);
243
244
245 if (SWRAST_CONTEXT(ctx)->NewState)
246 _swrast_validate_derived( ctx );
247
248 if (!ctx->DrawBuffer->Accum) {
249 _mesa_warning(ctx,
250 "Calling glAccum() without an accumulation "
251 "buffer (low memory?)");
252 return;
253 }
254
255 if (sizeof(GLaccum)==1) {
256 acc_scale = 127.0;
257 }
258 else if (sizeof(GLaccum)==2) {
259 acc_scale = 32767.0;
260 }
261 else {
262 acc_scale = 1.0F;
263 }
264
265 width4 = 4 * width;
266
267 switch (op) {
268 case GL_ADD:
269 if (value != 0.0F) {
270 const GLaccum val = (GLaccum) (value * acc_scale);
271 GLint j;
272 /* Leave optimized accum buffer mode */
273 if (swrast->_IntegerAccumMode)
274 rescale_accum(ctx);
275 for (j = 0; j < height; j++) {
276 GLaccum *acc = ctx->DrawBuffer->Accum + ypos * width4 + 4*xpos;
277 GLuint i;
278 for (i = 0; i < width4; i++) {
279 acc[i] += val;
280 }
281 ypos++;
282 }
283 }
284 break;
285
286 case GL_MULT:
287 if (value != 1.0F) {
288 GLint j;
289 /* Leave optimized accum buffer mode */
290 if (swrast->_IntegerAccumMode)
291 rescale_accum(ctx);
292 for (j = 0; j < height; j++) {
293 GLaccum *acc = ctx->DrawBuffer->Accum + ypos * width4 + 4 * xpos;
294 GLuint i;
295 for (i = 0; i < width4; i++) {
296 acc[i] = (GLaccum) ( (GLfloat) acc[i] * value );
297 }
298 ypos++;
299 }
300 }
301 break;
302
303 case GL_ACCUM:
304 if (value == 0.0F)
305 return;
306
307 _swrast_use_read_buffer(ctx);
308
309 /* May have to leave optimized accum buffer mode */
310 if (swrast->_IntegerAccumScaler == 0.0 && value > 0.0 && value <= 1.0)
311 swrast->_IntegerAccumScaler = value;
312 if (swrast->_IntegerAccumMode && value != swrast->_IntegerAccumScaler)
313 rescale_accum(ctx);
314
315 RENDER_START(swrast,ctx);
316
317 if (swrast->_IntegerAccumMode) {
318 /* simply add integer color values into accum buffer */
319 GLint j;
320 GLaccum *acc = ctx->DrawBuffer->Accum + ypos * width4 + xpos * 4;
321 assert(swrast->_IntegerAccumScaler > 0.0);
322 assert(swrast->_IntegerAccumScaler <= 1.0);
323 for (j = 0; j < height; j++) {
324
325 GLint i, i4;
326 _swrast_read_rgba_span(ctx, ctx->DrawBuffer, width, xpos, ypos, rgba);
327 for (i = i4 = 0; i < width; i++, i4+=4) {
328 acc[i4+0] += rgba[i][RCOMP];
329 acc[i4+1] += rgba[i][GCOMP];
330 acc[i4+2] += rgba[i][BCOMP];
331 acc[i4+3] += rgba[i][ACOMP];
332 }
333 acc += width4;
334 ypos++;
335 }
336 }
337 else {
338 /* scaled integer (or float) accum buffer */
339 const GLfloat rscale = value * acc_scale / CHAN_MAXF;
340 const GLfloat gscale = value * acc_scale / CHAN_MAXF;
341 const GLfloat bscale = value * acc_scale / CHAN_MAXF;
342 const GLfloat ascale = value * acc_scale / CHAN_MAXF;
343 GLint j;
344 for (j=0;j<height;j++) {
345 GLaccum *acc = ctx->DrawBuffer->Accum + ypos * width4 + xpos * 4;
346 GLint i;
347 _swrast_read_rgba_span(ctx, ctx->DrawBuffer, width, xpos, ypos, rgba);
348 for (i=0;i<width;i++) {
349 acc[0] += (GLaccum) ( (GLfloat) rgba[i][RCOMP] * rscale );
350 acc[1] += (GLaccum) ( (GLfloat) rgba[i][GCOMP] * gscale );
351 acc[2] += (GLaccum) ( (GLfloat) rgba[i][BCOMP] * bscale );
352 acc[3] += (GLaccum) ( (GLfloat) rgba[i][ACOMP] * ascale );
353 acc += 4;
354 }
355 ypos++;
356 }
357 }
358 /* restore read buffer = draw buffer (the default) */
359 _swrast_use_draw_buffer(ctx);
360
361 RENDER_FINISH(swrast,ctx);
362 break;
363
364 case GL_LOAD:
365 _swrast_use_read_buffer(ctx);
366
367 /* This is a change to go into optimized accum buffer mode */
368 if (value > 0.0 && value <= 1.0) {
369 #ifdef USE_OPTIMIZED_ACCUM
370 swrast->_IntegerAccumMode = GL_TRUE;
371 #else
372 swrast->_IntegerAccumMode = GL_FALSE;
373 #endif
374 swrast->_IntegerAccumScaler = value;
375 }
376 else {
377 swrast->_IntegerAccumMode = GL_FALSE;
378 swrast->_IntegerAccumScaler = 0.0;
379 }
380
381 RENDER_START(swrast,ctx);
382 if (swrast->_IntegerAccumMode) {
383 /* just copy values into accum buffer */
384 GLint j;
385 GLaccum *acc = ctx->DrawBuffer->Accum + ypos * width4 + xpos * 4;
386 assert(swrast->_IntegerAccumScaler > 0.0);
387 assert(swrast->_IntegerAccumScaler <= 1.0);
388 for (j = 0; j < height; j++) {
389 GLint i, i4;
390 _swrast_read_rgba_span(ctx, ctx->DrawBuffer, width, xpos, ypos, rgba);
391 for (i = i4 = 0; i < width; i++, i4 += 4) {
392 acc[i4+0] = rgba[i][RCOMP];
393 acc[i4+1] = rgba[i][GCOMP];
394 acc[i4+2] = rgba[i][BCOMP];
395 acc[i4+3] = rgba[i][ACOMP];
396 }
397 acc += width4;
398 ypos++;
399 }
400 }
401 else {
402 /* scaled integer (or float) accum buffer */
403 const GLfloat rscale = value * acc_scale / CHAN_MAXF;
404 const GLfloat gscale = value * acc_scale / CHAN_MAXF;
405 const GLfloat bscale = value * acc_scale / CHAN_MAXF;
406 const GLfloat ascale = value * acc_scale / CHAN_MAXF;
407 #if 0
408 const GLfloat d = 3.0 / acc_scale; /* XXX what's this? */
409 #endif
410 GLint i, j;
411 for (j = 0; j < height; j++) {
412 GLaccum *acc = ctx->DrawBuffer->Accum + ypos * width4 + xpos * 4;
413 _swrast_read_rgba_span(ctx, ctx->DrawBuffer, width, xpos, ypos, rgba);
414 for (i=0;i<width;i++) {
415 #if 0
416 *acc++ = (GLaccum) ((GLfloat) rgba[i][RCOMP] * rscale + d);
417 *acc++ = (GLaccum) ((GLfloat) rgba[i][GCOMP] * gscale + d);
418 *acc++ = (GLaccum) ((GLfloat) rgba[i][BCOMP] * bscale + d);
419 *acc++ = (GLaccum) ((GLfloat) rgba[i][ACOMP] * ascale + d);
420 #else
421 *acc++ = (GLaccum) ((GLfloat) rgba[i][RCOMP] * rscale);
422 *acc++ = (GLaccum) ((GLfloat) rgba[i][GCOMP] * gscale);
423 *acc++ = (GLaccum) ((GLfloat) rgba[i][BCOMP] * bscale);
424 *acc++ = (GLaccum) ((GLfloat) rgba[i][ACOMP] * ascale);
425 #endif
426 }
427 ypos++;
428 }
429 }
430
431 /* restore read buffer = draw buffer (the default) */
432 _swrast_use_draw_buffer(ctx);
433
434 RENDER_FINISH(swrast,ctx);
435 break;
436
437 case GL_RETURN:
438 /* May have to leave optimized accum buffer mode */
439 if (swrast->_IntegerAccumMode && value != 1.0)
440 rescale_accum(ctx);
441
442 RENDER_START(swrast,ctx);
443 #ifdef USE_OPTIMIZED_ACCUM
444 if (swrast->_IntegerAccumMode && swrast->_IntegerAccumScaler > 0) {
445 /* build lookup table to avoid many floating point multiplies */
446 static GLchan multTable[32768];
447 static GLfloat prevMult = 0.0;
448 const GLfloat mult = swrast->_IntegerAccumScaler;
449 const GLint max = MIN2((GLint) (256 / mult), 32767);
450 GLint j;
451 if (mult != prevMult) {
452 for (j = 0; j < max; j++)
453 multTable[j] = IROUND((GLfloat) j * mult);
454 prevMult = mult;
455 }
456
457 assert(swrast->_IntegerAccumScaler > 0.0);
458 assert(swrast->_IntegerAccumScaler <= 1.0);
459 for (j = 0; j < height; j++) {
460 const GLaccum *acc = ctx->DrawBuffer->Accum + ypos * width4 + xpos*4;
461 GLint i, i4;
462 for (i = i4 = 0; i < width; i++, i4 += 4) {
463 ASSERT(acc[i4+0] < max);
464 ASSERT(acc[i4+1] < max);
465 ASSERT(acc[i4+2] < max);
466 ASSERT(acc[i4+3] < max);
467 rgba[i][RCOMP] = multTable[acc[i4+0]];
468 rgba[i][GCOMP] = multTable[acc[i4+1]];
469 rgba[i][BCOMP] = multTable[acc[i4+2]];
470 rgba[i][ACOMP] = multTable[acc[i4+3]];
471 }
472 if (colorMask != 0xffffffff) {
473 _swrast_mask_rgba_array( ctx, width, xpos, ypos, rgba );
474 }
475 (*swrast->Driver.WriteRGBASpan)( ctx, width, xpos, ypos,
476 (const GLchan (*)[4])rgba, NULL );
477 if (ctx->DrawBuffer->UseSoftwareAlphaBuffers
478 && ctx->Color.ColorMask[ACOMP]) {
479 _swrast_write_alpha_span(ctx, width, xpos, ypos,
480 (CONST GLchan (*)[4]) rgba, NULL);
481 }
482 ypos++;
483 }
484 }
485 else
486 #endif /* USE_OPTIMIZED_ACCUM */
487 {
488 /* scaled integer (or float) accum buffer */
489 const GLfloat rscale = value / acc_scale * CHAN_MAXF;
490 const GLfloat gscale = value / acc_scale * CHAN_MAXF;
491 const GLfloat bscale = value / acc_scale * CHAN_MAXF;
492 const GLfloat ascale = value / acc_scale * CHAN_MAXF;
493 GLint i, j;
494 for (j=0;j<height;j++) {
495 const GLaccum *acc = ctx->DrawBuffer->Accum + ypos * width4 + xpos*4;
496 for (i=0;i<width;i++) {
497 GLint r = IROUND( (GLfloat) (acc[0]) * rscale );
498 GLint g = IROUND( (GLfloat) (acc[1]) * gscale );
499 GLint b = IROUND( (GLfloat) (acc[2]) * bscale );
500 GLint a = IROUND( (GLfloat) (acc[3]) * ascale );
501 acc += 4;
502 rgba[i][RCOMP] = CLAMP( r, 0, CHAN_MAX );
503 rgba[i][GCOMP] = CLAMP( g, 0, CHAN_MAX );
504 rgba[i][BCOMP] = CLAMP( b, 0, CHAN_MAX );
505 rgba[i][ACOMP] = CLAMP( a, 0, CHAN_MAX );
506 }
507 if (colorMask != 0xffffffff) {
508 _swrast_mask_rgba_array( ctx, width, xpos, ypos, rgba );
509 }
510 (*swrast->Driver.WriteRGBASpan)( ctx, width, xpos, ypos,
511 (const GLchan (*)[4])rgba, NULL );
512 if (ctx->DrawBuffer->UseSoftwareAlphaBuffers
513 && ctx->Color.ColorMask[ACOMP]) {
514 _swrast_write_alpha_span(ctx, width, xpos, ypos,
515 (CONST GLchan (*)[4]) rgba, NULL);
516 }
517 ypos++;
518 }
519 }
520 RENDER_FINISH(swrast,ctx);
521 break;
522
523 default:
524 _mesa_error( ctx, GL_INVALID_ENUM, "glAccum" );
525 }
526 }