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