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