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