2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
27 #include "condrender.h"
29 #include "format_unpack.h"
30 #include "format_pack.h"
33 #include "mfeatures.h"
36 #include "main/dispatch.h"
40 _mesa_ClearAccum( GLfloat red
, GLfloat green
, GLfloat blue
, GLfloat alpha
)
43 GET_CURRENT_CONTEXT(ctx
);
45 tmp
[0] = CLAMP( red
, -1.0F
, 1.0F
);
46 tmp
[1] = CLAMP( green
, -1.0F
, 1.0F
);
47 tmp
[2] = CLAMP( blue
, -1.0F
, 1.0F
);
48 tmp
[3] = CLAMP( alpha
, -1.0F
, 1.0F
);
50 if (TEST_EQ_4V(tmp
, ctx
->Accum
.ClearColor
))
53 COPY_4FV( ctx
->Accum
.ClearColor
, tmp
);
58 _mesa_Accum( GLenum op
, GLfloat value
)
60 GET_CURRENT_CONTEXT(ctx
);
61 FLUSH_VERTICES(ctx
, 0);
72 _mesa_error(ctx
, GL_INVALID_ENUM
, "glAccum(op)");
76 if (ctx
->DrawBuffer
->Visual
.haveAccumBuffer
== 0) {
77 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glAccum(no accum buffer)");
81 if (ctx
->DrawBuffer
!= ctx
->ReadBuffer
) {
82 /* See GLX_SGI_make_current_read or WGL_ARB_make_current_read,
83 * or GL_EXT_framebuffer_blit.
85 _mesa_error(ctx
, GL_INVALID_OPERATION
,
86 "glAccum(different read/draw buffers)");
91 _mesa_update_state(ctx
);
93 if (ctx
->DrawBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
94 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
95 "glAccum(incomplete framebuffer)");
99 if (ctx
->RasterDiscard
)
102 if (ctx
->RenderMode
== GL_RENDER
) {
103 _mesa_accum(ctx
, op
, value
);
109 * Clear the accumulation buffer by mapping the renderbuffer and
110 * writing the clear color to it. Called by the driver's implementation
111 * of the glClear function.
114 _mesa_clear_accum_buffer(struct gl_context
*ctx
)
116 GLuint x
, y
, width
, height
;
119 struct gl_renderbuffer
*accRb
;
121 if (!ctx
->DrawBuffer
)
124 accRb
= ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
;
126 return; /* missing accum buffer, not an error */
128 /* bounds, with scissor */
129 x
= ctx
->DrawBuffer
->_Xmin
;
130 y
= ctx
->DrawBuffer
->_Ymin
;
131 width
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
132 height
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
134 ctx
->Driver
.MapRenderbuffer(ctx
, accRb
, x
, y
, width
, height
,
135 GL_MAP_WRITE_BIT
, &accMap
, &accRowStride
);
138 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
142 if (accRb
->Format
== MESA_FORMAT_SIGNED_RGBA_16
) {
143 const GLshort clearR
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[0]);
144 const GLshort clearG
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[1]);
145 const GLshort clearB
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[2]);
146 const GLshort clearA
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[3]);
149 for (j
= 0; j
< height
; j
++) {
150 GLshort
*row
= (GLshort
*) accMap
;
152 for (i
= 0; i
< width
; i
++) {
153 row
[i
* 4 + 0] = clearR
;
154 row
[i
* 4 + 1] = clearG
;
155 row
[i
* 4 + 2] = clearB
;
156 row
[i
* 4 + 3] = clearA
;
158 accMap
+= accRowStride
;
162 /* other types someday? */
163 _mesa_warning(ctx
, "unexpected accum buffer type");
166 ctx
->Driver
.UnmapRenderbuffer(ctx
, accRb
);
177 accum_scale_or_bias(struct gl_context
*ctx
, GLfloat value
,
178 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
181 struct gl_renderbuffer
*accRb
=
182 ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
;
188 ctx
->Driver
.MapRenderbuffer(ctx
, accRb
, xpos
, ypos
, width
, height
,
189 GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
,
190 &accMap
, &accRowStride
);
193 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
197 if (accRb
->Format
== MESA_FORMAT_SIGNED_RGBA_16
) {
198 const GLshort incr
= (GLshort
) (value
* 32767.0f
);
201 for (j
= 0; j
< height
; j
++) {
202 GLshort
*acc
= (GLshort
*) accMap
;
203 for (i
= 0; i
< 4 * width
; i
++) {
206 accMap
+= accRowStride
;
211 for (j
= 0; j
< height
; j
++) {
212 GLshort
*acc
= (GLshort
*) accMap
;
213 for (i
= 0; i
< 4 * width
; i
++) {
214 acc
[i
] = (GLshort
) (acc
[i
] * value
);
216 accMap
+= accRowStride
;
221 /* other types someday? */
224 ctx
->Driver
.UnmapRenderbuffer(ctx
, accRb
);
230 * Accum = ColorBuf * value
232 * Accum += ColorBuf * value
235 accum_or_load(struct gl_context
*ctx
, GLfloat value
,
236 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
239 struct gl_renderbuffer
*accRb
=
240 ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
;
241 struct gl_renderbuffer
*colorRb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
242 GLubyte
*accMap
, *colorMap
;
243 GLint accRowStride
, colorRowStride
;
244 GLbitfield mappingFlags
;
247 /* no read buffer - OK */
253 mappingFlags
= GL_MAP_WRITE_BIT
;
254 if (!load
) /* if we're accumulating */
255 mappingFlags
|= GL_MAP_READ_BIT
;
257 /* Map accum buffer */
258 ctx
->Driver
.MapRenderbuffer(ctx
, accRb
, xpos
, ypos
, width
, height
,
259 mappingFlags
, &accMap
, &accRowStride
);
261 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
265 /* Map color buffer */
266 ctx
->Driver
.MapRenderbuffer(ctx
, colorRb
, xpos
, ypos
, width
, height
,
268 &colorMap
, &colorRowStride
);
270 ctx
->Driver
.UnmapRenderbuffer(ctx
, accRb
);
271 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
275 if (accRb
->Format
== MESA_FORMAT_SIGNED_RGBA_16
) {
276 const GLfloat scale
= value
* 32767.0f
;
280 rgba
= malloc(width
* 4 * sizeof(GLfloat
));
282 for (j
= 0; j
< height
; j
++) {
283 GLshort
*acc
= (GLshort
*) accMap
;
285 /* read colors from source color buffer */
286 _mesa_unpack_rgba_row(colorRb
->Format
, width
, colorMap
, rgba
);
289 for (i
= 0; i
< width
; i
++) {
290 acc
[i
* 4 + 0] = (GLshort
) (rgba
[i
][RCOMP
] * scale
);
291 acc
[i
* 4 + 1] = (GLshort
) (rgba
[i
][GCOMP
] * scale
);
292 acc
[i
* 4 + 2] = (GLshort
) (rgba
[i
][BCOMP
] * scale
);
293 acc
[i
* 4 + 3] = (GLshort
) (rgba
[i
][ACOMP
] * scale
);
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
);
306 colorMap
+= colorRowStride
;
307 accMap
+= accRowStride
;
313 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
317 /* other types someday? */
320 ctx
->Driver
.UnmapRenderbuffer(ctx
, accRb
);
321 ctx
->Driver
.UnmapRenderbuffer(ctx
, colorRb
);
326 * ColorBuffer = Accum * value
329 accum_return(struct gl_context
*ctx
, GLfloat value
,
330 GLint xpos
, GLint ypos
, GLint width
, GLint height
)
332 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
333 struct gl_renderbuffer
*accRb
= fb
->Attachment
[BUFFER_ACCUM
].Renderbuffer
;
334 GLubyte
*accMap
, *colorMap
;
335 GLint accRowStride
, colorRowStride
;
338 /* Map accum buffer */
339 ctx
->Driver
.MapRenderbuffer(ctx
, accRb
, xpos
, ypos
, width
, height
,
341 &accMap
, &accRowStride
);
343 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
347 /* Loop over destination buffers */
348 for (buffer
= 0; buffer
< fb
->_NumColorDrawBuffers
; buffer
++) {
349 struct gl_renderbuffer
*colorRb
= fb
->_ColorDrawBuffers
[buffer
];
350 const GLboolean masking
= (!ctx
->Color
.ColorMask
[buffer
][RCOMP
] ||
351 !ctx
->Color
.ColorMask
[buffer
][GCOMP
] ||
352 !ctx
->Color
.ColorMask
[buffer
][BCOMP
] ||
353 !ctx
->Color
.ColorMask
[buffer
][ACOMP
]);
354 GLbitfield mappingFlags
= GL_MAP_WRITE_BIT
;
357 mappingFlags
|= GL_MAP_READ_BIT
;
359 /* Map color buffer */
360 ctx
->Driver
.MapRenderbuffer(ctx
, colorRb
, xpos
, ypos
, width
, height
,
361 mappingFlags
, &colorMap
, &colorRowStride
);
363 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
367 if (accRb
->Format
== MESA_FORMAT_SIGNED_RGBA_16
) {
368 const GLfloat scale
= value
/ 32767.0f
;
370 GLfloat (*rgba
)[4], (*dest
)[4];
372 rgba
= malloc(width
* 4 * sizeof(GLfloat
));
373 dest
= malloc(width
* 4 * sizeof(GLfloat
));
376 for (j
= 0; j
< height
; j
++) {
377 GLshort
*acc
= (GLshort
*) accMap
;
379 for (i
= 0; i
< width
; i
++) {
380 rgba
[i
][0] = acc
[i
* 4 + 0] * scale
;
381 rgba
[i
][1] = acc
[i
* 4 + 1] * scale
;
382 rgba
[i
][2] = acc
[i
* 4 + 2] * scale
;
383 rgba
[i
][3] = acc
[i
* 4 + 3] * scale
;
388 /* get existing colors from dest buffer */
389 _mesa_unpack_rgba_row(colorRb
->Format
, width
, colorMap
, dest
);
391 /* use the dest colors where mask[channel] = 0 */
392 if (ctx
->Color
.ColorMask
[buffer
][RCOMP
] == 0) {
393 for (i
= 0; i
< width
; i
++)
394 rgba
[i
][RCOMP
] = dest
[i
][RCOMP
];
396 if (ctx
->Color
.ColorMask
[buffer
][GCOMP
] == 0) {
397 for (i
= 0; i
< width
; i
++)
398 rgba
[i
][GCOMP
] = dest
[i
][GCOMP
];
400 if (ctx
->Color
.ColorMask
[buffer
][BCOMP
] == 0) {
401 for (i
= 0; i
< width
; i
++)
402 rgba
[i
][BCOMP
] = dest
[i
][BCOMP
];
404 if (ctx
->Color
.ColorMask
[buffer
][ACOMP
] == 0) {
405 for (i
= 0; i
< width
; i
++)
406 rgba
[i
][ACOMP
] = dest
[i
][ACOMP
];
410 _mesa_pack_float_rgba_row(colorRb
->Format
, width
,
411 (const GLfloat (*)[4]) rgba
, colorMap
);
413 accMap
+= accRowStride
;
414 colorMap
+= colorRowStride
;
418 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
424 /* other types someday? */
427 ctx
->Driver
.UnmapRenderbuffer(ctx
, colorRb
);
430 ctx
->Driver
.UnmapRenderbuffer(ctx
, accRb
);
436 * Software fallback for glAccum. A hardware driver that supports
437 * signed 16-bit color channels could implement hardware accumulation
438 * operations, but no driver does so at this time.
441 _mesa_accum(struct gl_context
*ctx
, GLenum op
, GLfloat value
)
443 GLint xpos
, ypos
, width
, height
;
445 if (!ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
) {
446 _mesa_warning(ctx
, "Calling glAccum() without an accumulation buffer");
450 if (!_mesa_check_conditional_render(ctx
))
453 xpos
= ctx
->DrawBuffer
->_Xmin
;
454 ypos
= ctx
->DrawBuffer
->_Ymin
;
455 width
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
456 height
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
461 accum_scale_or_bias(ctx
, value
, xpos
, ypos
, width
, height
, GL_TRUE
);
466 accum_scale_or_bias(ctx
, value
, xpos
, ypos
, width
, height
, GL_FALSE
);
471 accum_or_load(ctx
, value
, xpos
, ypos
, width
, height
, GL_FALSE
);
475 accum_or_load(ctx
, value
, xpos
, ypos
, width
, height
, GL_TRUE
);
478 accum_return(ctx
, value
, xpos
, ypos
, width
, height
);
481 _mesa_problem(ctx
, "invalid mode in _mesa_accum()");
488 _mesa_init_accum( struct gl_context
*ctx
)
490 /* Accumulate buffer group */
491 ASSIGN_4V( ctx
->Accum
.ClearColor
, 0.0, 0.0, 0.0, 0.0 );