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