i965/vs: Store texturing results into a vec4 temporary.
[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 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 /**
110 * Clear the accumulation buffer by mapping the renderbuffer and
111 * writing the clear color to it. Called by the driver's implementation
112 * of the glClear function.
113 */
114 void
115 _mesa_clear_accum_buffer(struct gl_context *ctx)
116 {
117 GLuint x, y, width, height;
118 GLubyte *accMap;
119 GLint accRowStride;
120 struct gl_renderbuffer *accRb;
121
122 if (!ctx->DrawBuffer)
123 return;
124
125 accRb = ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer;
126 if (!accRb)
127 return; /* missing accum buffer, not an error */
128
129 /* bounds, with scissor */
130 x = ctx->DrawBuffer->_Xmin;
131 y = ctx->DrawBuffer->_Ymin;
132 width = ctx->DrawBuffer->_Xmax - ctx->DrawBuffer->_Xmin;
133 height = ctx->DrawBuffer->_Ymax - ctx->DrawBuffer->_Ymin;
134
135 ctx->Driver.MapRenderbuffer(ctx, accRb, x, y, width, height,
136 GL_MAP_WRITE_BIT, &accMap, &accRowStride);
137
138 if (!accMap) {
139 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum");
140 return;
141 }
142
143 if (accRb->Format == MESA_FORMAT_SIGNED_RGBA_16) {
144 const GLshort clearR = FLOAT_TO_SHORT(ctx->Accum.ClearColor[0]);
145 const GLshort clearG = FLOAT_TO_SHORT(ctx->Accum.ClearColor[1]);
146 const GLshort clearB = FLOAT_TO_SHORT(ctx->Accum.ClearColor[2]);
147 const GLshort clearA = FLOAT_TO_SHORT(ctx->Accum.ClearColor[3]);
148 GLuint i, j;
149
150 for (j = 0; j < height; j++) {
151 GLshort *row = (GLshort *) accMap;
152
153 for (i = 0; i < width; i++) {
154 row[i * 4 + 0] = clearR;
155 row[i * 4 + 1] = clearG;
156 row[i * 4 + 2] = clearB;
157 row[i * 4 + 3] = clearA;
158 }
159 accMap += accRowStride;
160 }
161 }
162 else {
163 /* other types someday? */
164 _mesa_warning(ctx, "unexpected accum buffer type");
165 }
166
167 ctx->Driver.UnmapRenderbuffer(ctx, accRb);
168 }
169
170
171 /**
172 * if (bias)
173 * Accum += value
174 * else
175 * Accum *= value
176 */
177 static void
178 accum_scale_or_bias(struct gl_context *ctx, GLfloat value,
179 GLint xpos, GLint ypos, GLint width, GLint height,
180 GLboolean bias)
181 {
182 struct gl_renderbuffer *accRb =
183 ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer;
184 GLubyte *accMap;
185 GLint accRowStride;
186
187 assert(accRb);
188
189 ctx->Driver.MapRenderbuffer(ctx, accRb, xpos, ypos, width, height,
190 GL_MAP_READ_BIT | GL_MAP_WRITE_BIT,
191 &accMap, &accRowStride);
192
193 if (!accMap) {
194 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum");
195 return;
196 }
197
198 if (accRb->Format == MESA_FORMAT_SIGNED_RGBA_16) {
199 const GLshort incr = (GLshort) (value * 32767.0f);
200 GLint i, j;
201 if (bias) {
202 for (j = 0; j < height; j++) {
203 GLshort *acc = (GLshort *) accMap;
204 for (i = 0; i < 4 * width; i++) {
205 acc[i] += incr;
206 }
207 accMap += accRowStride;
208 }
209 }
210 else {
211 /* scale */
212 for (j = 0; j < height; j++) {
213 GLshort *acc = (GLshort *) accMap;
214 for (i = 0; i < 4 * width; i++) {
215 acc[i] = (GLshort) (acc[i] * value);
216 }
217 accMap += accRowStride;
218 }
219 }
220 }
221 else {
222 /* other types someday? */
223 }
224
225 ctx->Driver.UnmapRenderbuffer(ctx, accRb);
226 }
227
228
229 /**
230 * if (load)
231 * Accum = ColorBuf * value
232 * else
233 * Accum += ColorBuf * value
234 */
235 static void
236 accum_or_load(struct gl_context *ctx, GLfloat value,
237 GLint xpos, GLint ypos, GLint width, GLint height,
238 GLboolean load)
239 {
240 struct gl_renderbuffer *accRb =
241 ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer;
242 struct gl_renderbuffer *colorRb = ctx->ReadBuffer->_ColorReadBuffer;
243 GLubyte *accMap, *colorMap;
244 GLint accRowStride, colorRowStride;
245 GLbitfield mappingFlags;
246
247 if (!colorRb) {
248 /* no read buffer - OK */
249 return;
250 }
251
252 assert(accRb);
253
254 mappingFlags = GL_MAP_WRITE_BIT;
255 if (!load) /* if we're accumulating */
256 mappingFlags |= GL_MAP_READ_BIT;
257
258 /* Map accum buffer */
259 ctx->Driver.MapRenderbuffer(ctx, accRb, xpos, ypos, width, height,
260 mappingFlags, &accMap, &accRowStride);
261 if (!accMap) {
262 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum");
263 return;
264 }
265
266 /* Map color buffer */
267 ctx->Driver.MapRenderbuffer(ctx, colorRb, xpos, ypos, width, height,
268 GL_MAP_READ_BIT,
269 &colorMap, &colorRowStride);
270 if (!colorMap) {
271 ctx->Driver.UnmapRenderbuffer(ctx, accRb);
272 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum");
273 return;
274 }
275
276 if (accRb->Format == MESA_FORMAT_SIGNED_RGBA_16) {
277 const GLfloat scale = value * 32767.0f;
278 GLint i, j;
279 GLfloat (*rgba)[4];
280
281 rgba = malloc(width * 4 * sizeof(GLfloat));
282 if (rgba) {
283 for (j = 0; j < height; j++) {
284 GLshort *acc = (GLshort *) accMap;
285
286 /* read colors from source color buffer */
287 _mesa_unpack_rgba_row(colorRb->Format, width, colorMap, rgba);
288
289 if (load) {
290 for (i = 0; i < width; i++) {
291 acc[i * 4 + 0] = (GLshort) (rgba[i][RCOMP] * scale);
292 acc[i * 4 + 1] = (GLshort) (rgba[i][GCOMP] * scale);
293 acc[i * 4 + 2] = (GLshort) (rgba[i][BCOMP] * scale);
294 acc[i * 4 + 3] = (GLshort) (rgba[i][ACOMP] * scale);
295 }
296 }
297 else {
298 /* accumulate */
299 for (i = 0; i < width; i++) {
300 acc[i * 4 + 0] += (GLshort) (rgba[i][RCOMP] * scale);
301 acc[i * 4 + 1] += (GLshort) (rgba[i][GCOMP] * scale);
302 acc[i * 4 + 2] += (GLshort) (rgba[i][BCOMP] * scale);
303 acc[i * 4 + 3] += (GLshort) (rgba[i][ACOMP] * scale);
304 }
305 }
306
307 colorMap += colorRowStride;
308 accMap += accRowStride;
309 }
310
311 free(rgba);
312 }
313 else {
314 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum");
315 }
316 }
317 else {
318 /* other types someday? */
319 }
320
321 ctx->Driver.UnmapRenderbuffer(ctx, accRb);
322 ctx->Driver.UnmapRenderbuffer(ctx, colorRb);
323 }
324
325
326 /**
327 * ColorBuffer = Accum * value
328 */
329 static void
330 accum_return(struct gl_context *ctx, GLfloat value,
331 GLint xpos, GLint ypos, GLint width, GLint height)
332 {
333 struct gl_framebuffer *fb = ctx->DrawBuffer;
334 struct gl_renderbuffer *accRb = fb->Attachment[BUFFER_ACCUM].Renderbuffer;
335 GLubyte *accMap, *colorMap;
336 GLint accRowStride, colorRowStride;
337 GLuint buffer;
338
339 /* Map accum buffer */
340 ctx->Driver.MapRenderbuffer(ctx, accRb, xpos, ypos, width, height,
341 GL_MAP_READ_BIT,
342 &accMap, &accRowStride);
343 if (!accMap) {
344 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum");
345 return;
346 }
347
348 /* Loop over destination buffers */
349 for (buffer = 0; buffer < fb->_NumColorDrawBuffers; buffer++) {
350 struct gl_renderbuffer *colorRb = fb->_ColorDrawBuffers[buffer];
351 const GLboolean masking = (!ctx->Color.ColorMask[buffer][RCOMP] ||
352 !ctx->Color.ColorMask[buffer][GCOMP] ||
353 !ctx->Color.ColorMask[buffer][BCOMP] ||
354 !ctx->Color.ColorMask[buffer][ACOMP]);
355 GLbitfield mappingFlags = GL_MAP_WRITE_BIT;
356
357 if (masking)
358 mappingFlags |= GL_MAP_READ_BIT;
359
360 /* Map color buffer */
361 ctx->Driver.MapRenderbuffer(ctx, colorRb, xpos, ypos, width, height,
362 mappingFlags, &colorMap, &colorRowStride);
363 if (!colorMap) {
364 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum");
365 continue;
366 }
367
368 if (accRb->Format == MESA_FORMAT_SIGNED_RGBA_16) {
369 const GLfloat scale = value / 32767.0f;
370 GLint i, j;
371 GLfloat (*rgba)[4], (*dest)[4];
372
373 rgba = malloc(width * 4 * sizeof(GLfloat));
374 dest = malloc(width * 4 * sizeof(GLfloat));
375
376 if (rgba && dest) {
377 for (j = 0; j < height; j++) {
378 GLshort *acc = (GLshort *) accMap;
379
380 for (i = 0; i < width; i++) {
381 rgba[i][0] = acc[i * 4 + 0] * scale;
382 rgba[i][1] = acc[i * 4 + 1] * scale;
383 rgba[i][2] = acc[i * 4 + 2] * scale;
384 rgba[i][3] = acc[i * 4 + 3] * scale;
385 }
386
387 if (masking) {
388
389 /* get existing colors from dest buffer */
390 _mesa_unpack_rgba_row(colorRb->Format, width, colorMap, dest);
391
392 /* use the dest colors where mask[channel] = 0 */
393 if (ctx->Color.ColorMask[buffer][RCOMP] == 0) {
394 for (i = 0; i < width; i++)
395 rgba[i][RCOMP] = dest[i][RCOMP];
396 }
397 if (ctx->Color.ColorMask[buffer][GCOMP] == 0) {
398 for (i = 0; i < width; i++)
399 rgba[i][GCOMP] = dest[i][GCOMP];
400 }
401 if (ctx->Color.ColorMask[buffer][BCOMP] == 0) {
402 for (i = 0; i < width; i++)
403 rgba[i][BCOMP] = dest[i][BCOMP];
404 }
405 if (ctx->Color.ColorMask[buffer][ACOMP] == 0) {
406 for (i = 0; i < width; i++)
407 rgba[i][ACOMP] = dest[i][ACOMP];
408 }
409 }
410
411 _mesa_pack_float_rgba_row(colorRb->Format, width,
412 (const GLfloat (*)[4]) rgba, colorMap);
413
414 accMap += accRowStride;
415 colorMap += colorRowStride;
416 }
417 }
418 else {
419 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum");
420 }
421 free(rgba);
422 free(dest);
423 }
424 else {
425 /* other types someday? */
426 }
427
428 ctx->Driver.UnmapRenderbuffer(ctx, colorRb);
429 }
430
431 ctx->Driver.UnmapRenderbuffer(ctx, accRb);
432 }
433
434
435
436 /**
437 * Software fallback for glAccum. A hardware driver that supports
438 * signed 16-bit color channels could implement hardware accumulation
439 * operations, but no driver does so at this time.
440 */
441 void
442 _mesa_accum(struct gl_context *ctx, GLenum op, GLfloat value)
443 {
444 GLint xpos, ypos, width, height;
445
446 if (!ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer) {
447 _mesa_warning(ctx, "Calling glAccum() without an accumulation buffer");
448 return;
449 }
450
451 if (!_mesa_check_conditional_render(ctx))
452 return;
453
454 xpos = ctx->DrawBuffer->_Xmin;
455 ypos = ctx->DrawBuffer->_Ymin;
456 width = ctx->DrawBuffer->_Xmax - ctx->DrawBuffer->_Xmin;
457 height = ctx->DrawBuffer->_Ymax - ctx->DrawBuffer->_Ymin;
458
459 switch (op) {
460 case GL_ADD:
461 if (value != 0.0F) {
462 accum_scale_or_bias(ctx, value, xpos, ypos, width, height, GL_TRUE);
463 }
464 break;
465 case GL_MULT:
466 if (value != 1.0F) {
467 accum_scale_or_bias(ctx, value, xpos, ypos, width, height, GL_FALSE);
468 }
469 break;
470 case GL_ACCUM:
471 if (value != 0.0F) {
472 accum_or_load(ctx, value, xpos, ypos, width, height, GL_FALSE);
473 }
474 break;
475 case GL_LOAD:
476 accum_or_load(ctx, value, xpos, ypos, width, height, GL_TRUE);
477 break;
478 case GL_RETURN:
479 accum_return(ctx, value, xpos, ypos, width, height);
480 break;
481 default:
482 _mesa_problem(ctx, "invalid mode in _mesa_accum()");
483 break;
484 }
485 }
486
487
488 void
489 _mesa_init_accum( struct gl_context *ctx )
490 {
491 /* Accumulate buffer group */
492 ASSIGN_4V( ctx->Accum.ClearColor, 0.0, 0.0, 0.0, 0.0 );
493 }