mesa: remove FEATURE_accum define.
[mesa.git] / src / mesa / main / accum.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5
4 *
5 * Copyright (C) 1999-2005 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 #include "glheader.h"
26 #include "accum.h"
27 #include "condrender.h"
28 #include "context.h"
29 #include "format_unpack.h"
30 #include "format_pack.h"
31 #include "imports.h"
32 #include "macros.h"
33 #include "mfeatures.h"
34 #include "state.h"
35 #include "mtypes.h"
36 #include "main/dispatch.h"
37
38
39 void GLAPIENTRY
40 _mesa_ClearAccum( GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha )
41 {
42 GLfloat tmp[4];
43 GET_CURRENT_CONTEXT(ctx);
44 ASSERT_OUTSIDE_BEGIN_END(ctx);
45
46 tmp[0] = CLAMP( red, -1.0F, 1.0F );
47 tmp[1] = CLAMP( green, -1.0F, 1.0F );
48 tmp[2] = CLAMP( blue, -1.0F, 1.0F );
49 tmp[3] = CLAMP( alpha, -1.0F, 1.0F );
50
51 if (TEST_EQ_4V(tmp, ctx->Accum.ClearColor))
52 return;
53
54 COPY_4FV( ctx->Accum.ClearColor, tmp );
55 }
56
57
58 static void GLAPIENTRY
59 _mesa_Accum( GLenum op, GLfloat value )
60 {
61 GET_CURRENT_CONTEXT(ctx);
62 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
63
64 switch (op) {
65 case GL_ADD:
66 case GL_MULT:
67 case GL_ACCUM:
68 case GL_LOAD:
69 case GL_RETURN:
70 /* OK */
71 break;
72 default:
73 _mesa_error(ctx, GL_INVALID_ENUM, "glAccum(op)");
74 return;
75 }
76
77 if (ctx->DrawBuffer->Visual.haveAccumBuffer == 0) {
78 _mesa_error(ctx, GL_INVALID_OPERATION, "glAccum(no accum buffer)");
79 return;
80 }
81
82 if (ctx->DrawBuffer != ctx->ReadBuffer) {
83 /* See GLX_SGI_make_current_read or WGL_ARB_make_current_read,
84 * or GL_EXT_framebuffer_blit.
85 */
86 _mesa_error(ctx, GL_INVALID_OPERATION,
87 "glAccum(different read/draw buffers)");
88 return;
89 }
90
91 if (ctx->NewState)
92 _mesa_update_state(ctx);
93
94 if (ctx->DrawBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
95 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
96 "glAccum(incomplete framebuffer)");
97 return;
98 }
99
100 if (ctx->RasterDiscard)
101 return;
102
103 if (ctx->RenderMode == GL_RENDER) {
104 _mesa_accum(ctx, op, value);
105 }
106 }
107
108
109 void
110 _mesa_init_accum_dispatch(struct _glapi_table *disp)
111 {
112 SET_Accum(disp, _mesa_Accum);
113 SET_ClearAccum(disp, _mesa_ClearAccum);
114 }
115
116
117 /**
118 * Clear the accumulation buffer by mapping the renderbuffer and
119 * writing the clear color to it. Called by the driver's implementation
120 * of the glClear function.
121 */
122 void
123 _mesa_clear_accum_buffer(struct gl_context *ctx)
124 {
125 GLuint x, y, width, height;
126 GLubyte *accMap;
127 GLint accRowStride;
128 struct gl_renderbuffer *accRb;
129
130 if (!ctx->DrawBuffer)
131 return;
132
133 accRb = ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer;
134 if (!accRb)
135 return; /* missing accum buffer, not an error */
136
137 /* bounds, with scissor */
138 x = ctx->DrawBuffer->_Xmin;
139 y = ctx->DrawBuffer->_Ymin;
140 width = ctx->DrawBuffer->_Xmax - ctx->DrawBuffer->_Xmin;
141 height = ctx->DrawBuffer->_Ymax - ctx->DrawBuffer->_Ymin;
142
143 ctx->Driver.MapRenderbuffer(ctx, accRb, x, y, width, height,
144 GL_MAP_WRITE_BIT, &accMap, &accRowStride);
145
146 if (!accMap) {
147 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum");
148 return;
149 }
150
151 if (accRb->Format == MESA_FORMAT_SIGNED_RGBA_16) {
152 const GLshort clearR = FLOAT_TO_SHORT(ctx->Accum.ClearColor[0]);
153 const GLshort clearG = FLOAT_TO_SHORT(ctx->Accum.ClearColor[1]);
154 const GLshort clearB = FLOAT_TO_SHORT(ctx->Accum.ClearColor[2]);
155 const GLshort clearA = FLOAT_TO_SHORT(ctx->Accum.ClearColor[3]);
156 GLuint i, j;
157
158 for (j = 0; j < height; j++) {
159 GLshort *row = (GLshort *) accMap;
160
161 for (i = 0; i < width; i++) {
162 row[i * 4 + 0] = clearR;
163 row[i * 4 + 1] = clearG;
164 row[i * 4 + 2] = clearB;
165 row[i * 4 + 3] = clearA;
166 }
167 accMap += accRowStride;
168 }
169 }
170 else {
171 /* other types someday? */
172 _mesa_warning(ctx, "unexpected accum buffer type");
173 }
174
175 ctx->Driver.UnmapRenderbuffer(ctx, accRb);
176 }
177
178
179 /**
180 * if (bias)
181 * Accum += value
182 * else
183 * Accum *= value
184 */
185 static void
186 accum_scale_or_bias(struct gl_context *ctx, GLfloat value,
187 GLint xpos, GLint ypos, GLint width, GLint height,
188 GLboolean bias)
189 {
190 struct gl_renderbuffer *accRb =
191 ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer;
192 GLubyte *accMap;
193 GLint accRowStride;
194
195 assert(accRb);
196
197 ctx->Driver.MapRenderbuffer(ctx, accRb, xpos, ypos, width, height,
198 GL_MAP_READ_BIT | GL_MAP_WRITE_BIT,
199 &accMap, &accRowStride);
200
201 if (!accMap) {
202 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum");
203 return;
204 }
205
206 if (accRb->Format == MESA_FORMAT_SIGNED_RGBA_16) {
207 const GLshort incr = (GLshort) (value * 32767.0f);
208 GLuint i, j;
209 if (bias) {
210 for (j = 0; j < height; j++) {
211 GLshort *acc = (GLshort *) accMap;
212 for (i = 0; i < 4 * width; i++) {
213 acc[i] += incr;
214 }
215 accMap += accRowStride;
216 }
217 }
218 else {
219 /* scale */
220 for (j = 0; j < height; j++) {
221 GLshort *acc = (GLshort *) accMap;
222 for (i = 0; i < 4 * width; i++) {
223 acc[i] = (GLshort) (acc[i] * value);
224 }
225 accMap += accRowStride;
226 }
227 }
228 }
229 else {
230 /* other types someday? */
231 }
232
233 ctx->Driver.UnmapRenderbuffer(ctx, accRb);
234 }
235
236
237 /**
238 * if (load)
239 * Accum = ColorBuf * value
240 * else
241 * Accum += ColorBuf * value
242 */
243 static void
244 accum_or_load(struct gl_context *ctx, GLfloat value,
245 GLint xpos, GLint ypos, GLint width, GLint height,
246 GLboolean load)
247 {
248 struct gl_renderbuffer *accRb =
249 ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer;
250 struct gl_renderbuffer *colorRb = ctx->ReadBuffer->_ColorReadBuffer;
251 GLubyte *accMap, *colorMap;
252 GLint accRowStride, colorRowStride;
253 GLbitfield mappingFlags;
254
255 if (!colorRb) {
256 /* no read buffer - OK */
257 return;
258 }
259
260 assert(accRb);
261
262 mappingFlags = GL_MAP_WRITE_BIT;
263 if (!load) /* if we're accumulating */
264 mappingFlags |= GL_MAP_READ_BIT;
265
266 /* Map accum buffer */
267 ctx->Driver.MapRenderbuffer(ctx, accRb, xpos, ypos, width, height,
268 mappingFlags, &accMap, &accRowStride);
269 if (!accMap) {
270 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum");
271 return;
272 }
273
274 /* Map color buffer */
275 ctx->Driver.MapRenderbuffer(ctx, colorRb, xpos, ypos, width, height,
276 GL_MAP_READ_BIT,
277 &colorMap, &colorRowStride);
278 if (!colorMap) {
279 ctx->Driver.UnmapRenderbuffer(ctx, accRb);
280 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum");
281 return;
282 }
283
284 if (accRb->Format == MESA_FORMAT_SIGNED_RGBA_16) {
285 const GLfloat scale = value * 32767.0f;
286 GLuint i, j;
287 GLfloat (*rgba)[4];
288
289 rgba = malloc(width * 4 * sizeof(GLfloat));
290 if (rgba) {
291 for (j = 0; j < height; j++) {
292 GLshort *acc = (GLshort *) accMap;
293
294 /* read colors from source color buffer */
295 _mesa_unpack_rgba_row(colorRb->Format, width, colorMap, rgba);
296
297 if (load) {
298 for (i = 0; i < width; i++) {
299 acc[i * 4 + 0] = (GLshort) (rgba[i][RCOMP] * scale);
300 acc[i * 4 + 1] = (GLshort) (rgba[i][GCOMP] * scale);
301 acc[i * 4 + 2] = (GLshort) (rgba[i][BCOMP] * scale);
302 acc[i * 4 + 3] = (GLshort) (rgba[i][ACOMP] * scale);
303 }
304 }
305 else {
306 /* accumulate */
307 for (i = 0; i < width; i++) {
308 acc[i * 4 + 0] += (GLshort) (rgba[i][RCOMP] * scale);
309 acc[i * 4 + 1] += (GLshort) (rgba[i][GCOMP] * scale);
310 acc[i * 4 + 2] += (GLshort) (rgba[i][BCOMP] * scale);
311 acc[i * 4 + 3] += (GLshort) (rgba[i][ACOMP] * scale);
312 }
313 }
314
315 colorMap += colorRowStride;
316 accMap += accRowStride;
317 }
318
319 free(rgba);
320 }
321 else {
322 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum");
323 }
324 }
325 else {
326 /* other types someday? */
327 }
328
329 ctx->Driver.UnmapRenderbuffer(ctx, accRb);
330 ctx->Driver.UnmapRenderbuffer(ctx, colorRb);
331 }
332
333
334 /**
335 * ColorBuffer = Accum * value
336 */
337 static void
338 accum_return(struct gl_context *ctx, GLfloat value,
339 GLint xpos, GLint ypos, GLint width, GLint height)
340 {
341 struct gl_framebuffer *fb = ctx->DrawBuffer;
342 struct gl_renderbuffer *accRb = fb->Attachment[BUFFER_ACCUM].Renderbuffer;
343 GLubyte *accMap, *colorMap;
344 GLint accRowStride, colorRowStride;
345 GLuint buffer;
346
347 /* Map accum buffer */
348 ctx->Driver.MapRenderbuffer(ctx, accRb, xpos, ypos, width, height,
349 GL_MAP_READ_BIT,
350 &accMap, &accRowStride);
351 if (!accMap) {
352 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum");
353 return;
354 }
355
356 /* Loop over destination buffers */
357 for (buffer = 0; buffer < fb->_NumColorDrawBuffers; buffer++) {
358 struct gl_renderbuffer *colorRb = fb->_ColorDrawBuffers[buffer];
359 const GLboolean masking = (!ctx->Color.ColorMask[buffer][RCOMP] ||
360 !ctx->Color.ColorMask[buffer][GCOMP] ||
361 !ctx->Color.ColorMask[buffer][BCOMP] ||
362 !ctx->Color.ColorMask[buffer][ACOMP]);
363 GLbitfield mappingFlags = GL_MAP_WRITE_BIT;
364
365 if (masking)
366 mappingFlags |= GL_MAP_READ_BIT;
367
368 /* Map color buffer */
369 ctx->Driver.MapRenderbuffer(ctx, colorRb, xpos, ypos, width, height,
370 mappingFlags, &colorMap, &colorRowStride);
371 if (!colorMap) {
372 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum");
373 continue;
374 }
375
376 if (accRb->Format == MESA_FORMAT_SIGNED_RGBA_16) {
377 const GLfloat scale = value / 32767.0f;
378 GLint i, j;
379 GLfloat (*rgba)[4], (*dest)[4];
380
381 rgba = malloc(width * 4 * sizeof(GLfloat));
382 dest = malloc(width * 4 * sizeof(GLfloat));
383
384 if (rgba && dest) {
385 for (j = 0; j < height; j++) {
386 GLshort *acc = (GLshort *) accMap;
387
388 for (i = 0; i < width; i++) {
389 rgba[i][0] = acc[i * 4 + 0] * scale;
390 rgba[i][1] = acc[i * 4 + 1] * scale;
391 rgba[i][2] = acc[i * 4 + 2] * scale;
392 rgba[i][3] = acc[i * 4 + 3] * scale;
393 }
394
395 if (masking) {
396
397 /* get existing colors from dest buffer */
398 _mesa_unpack_rgba_row(colorRb->Format, width, colorMap, dest);
399
400 /* use the dest colors where mask[channel] = 0 */
401 if (ctx->Color.ColorMask[buffer][RCOMP] == 0) {
402 for (i = 0; i < width; i++)
403 rgba[i][RCOMP] = dest[i][RCOMP];
404 }
405 if (ctx->Color.ColorMask[buffer][GCOMP] == 0) {
406 for (i = 0; i < width; i++)
407 rgba[i][GCOMP] = dest[i][GCOMP];
408 }
409 if (ctx->Color.ColorMask[buffer][BCOMP] == 0) {
410 for (i = 0; i < width; i++)
411 rgba[i][BCOMP] = dest[i][BCOMP];
412 }
413 if (ctx->Color.ColorMask[buffer][ACOMP] == 0) {
414 for (i = 0; i < width; i++)
415 rgba[i][ACOMP] = dest[i][ACOMP];
416 }
417 }
418
419 _mesa_pack_float_rgba_row(colorRb->Format, width,
420 (const GLfloat (*)[4]) rgba, colorMap);
421
422 accMap += accRowStride;
423 colorMap += colorRowStride;
424 }
425 }
426 else {
427 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum");
428 }
429 free(rgba);
430 free(dest);
431 }
432 else {
433 /* other types someday? */
434 }
435
436 ctx->Driver.UnmapRenderbuffer(ctx, colorRb);
437 }
438
439 ctx->Driver.UnmapRenderbuffer(ctx, accRb);
440 }
441
442
443
444 /**
445 * Software fallback for glAccum. A hardware driver that supports
446 * signed 16-bit color channels could implement hardware accumulation
447 * operations, but no driver does so at this time.
448 */
449 void
450 _mesa_accum(struct gl_context *ctx, GLenum op, GLfloat value)
451 {
452 GLint xpos, ypos, width, height;
453
454 if (!ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer) {
455 _mesa_warning(ctx, "Calling glAccum() without an accumulation buffer");
456 return;
457 }
458
459 if (!_mesa_check_conditional_render(ctx))
460 return;
461
462 xpos = ctx->DrawBuffer->_Xmin;
463 ypos = ctx->DrawBuffer->_Ymin;
464 width = ctx->DrawBuffer->_Xmax - ctx->DrawBuffer->_Xmin;
465 height = ctx->DrawBuffer->_Ymax - ctx->DrawBuffer->_Ymin;
466
467 switch (op) {
468 case GL_ADD:
469 if (value != 0.0F) {
470 accum_scale_or_bias(ctx, value, xpos, ypos, width, height, GL_TRUE);
471 }
472 break;
473 case GL_MULT:
474 if (value != 1.0F) {
475 accum_scale_or_bias(ctx, value, xpos, ypos, width, height, GL_FALSE);
476 }
477 break;
478 case GL_ACCUM:
479 if (value != 0.0F) {
480 accum_or_load(ctx, value, xpos, ypos, width, height, GL_FALSE);
481 }
482 break;
483 case GL_LOAD:
484 accum_or_load(ctx, value, xpos, ypos, width, height, GL_TRUE);
485 break;
486 case GL_RETURN:
487 accum_return(ctx, value, xpos, ypos, width, height);
488 break;
489 default:
490 _mesa_problem(ctx, "invalid mode in _mesa_accum()");
491 break;
492 }
493 }
494
495
496 void
497 _mesa_init_accum( struct gl_context *ctx )
498 {
499 /* Accumulate buffer group */
500 ASSIGN_4V( ctx->Accum.ClearColor, 0.0, 0.0, 0.0, 0.0 );
501 }