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"
43 _mesa_ClearAccum( GLfloat red
, GLfloat green
, GLfloat blue
, GLfloat alpha
)
46 GET_CURRENT_CONTEXT(ctx
);
47 ASSERT_OUTSIDE_BEGIN_END(ctx
);
49 tmp
[0] = CLAMP( red
, -1.0F
, 1.0F
);
50 tmp
[1] = CLAMP( green
, -1.0F
, 1.0F
);
51 tmp
[2] = CLAMP( blue
, -1.0F
, 1.0F
);
52 tmp
[3] = CLAMP( alpha
, -1.0F
, 1.0F
);
54 if (TEST_EQ_4V(tmp
, ctx
->Accum
.ClearColor
))
57 COPY_4FV( ctx
->Accum
.ClearColor
, tmp
);
61 static void GLAPIENTRY
62 _mesa_Accum( GLenum op
, GLfloat value
)
64 GET_CURRENT_CONTEXT(ctx
);
65 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
76 _mesa_error(ctx
, GL_INVALID_ENUM
, "glAccum(op)");
80 if (ctx
->DrawBuffer
->Visual
.haveAccumBuffer
== 0) {
81 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glAccum(no accum buffer)");
85 if (ctx
->DrawBuffer
!= ctx
->ReadBuffer
) {
86 /* See GLX_SGI_make_current_read or WGL_ARB_make_current_read,
87 * or GL_EXT_framebuffer_blit.
89 _mesa_error(ctx
, GL_INVALID_OPERATION
,
90 "glAccum(different read/draw buffers)");
95 _mesa_update_state(ctx
);
97 if (ctx
->DrawBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
98 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
99 "glAccum(incomplete framebuffer)");
103 if (ctx
->RasterDiscard
)
106 if (ctx
->RenderMode
== GL_RENDER
) {
107 _mesa_accum(ctx
, op
, value
);
113 _mesa_init_accum_dispatch(struct _glapi_table
*disp
)
115 SET_Accum(disp
, _mesa_Accum
);
116 SET_ClearAccum(disp
, _mesa_ClearAccum
);
121 * Clear the accumulation buffer by mapping the renderbuffer and
122 * writing the clear color to it. Called by the driver's implementation
123 * of the glClear function.
126 _mesa_clear_accum_buffer(struct gl_context
*ctx
)
128 GLuint x
, y
, width
, height
;
131 struct gl_renderbuffer
*accRb
;
133 if (!ctx
->DrawBuffer
)
136 accRb
= ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
;
138 return; /* missing accum buffer, not an error */
140 /* bounds, with scissor */
141 x
= ctx
->DrawBuffer
->_Xmin
;
142 y
= ctx
->DrawBuffer
->_Ymin
;
143 width
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
144 height
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
146 ctx
->Driver
.MapRenderbuffer(ctx
, accRb
, x
, y
, width
, height
,
147 GL_MAP_WRITE_BIT
, &accMap
, &accRowStride
);
150 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
154 if (accRb
->Format
== MESA_FORMAT_SIGNED_RGBA_16
) {
155 const GLshort clearR
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[0]);
156 const GLshort clearG
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[1]);
157 const GLshort clearB
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[2]);
158 const GLshort clearA
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[3]);
161 for (j
= 0; j
< height
; j
++) {
162 GLshort
*row
= (GLshort
*) accMap
;
164 for (i
= 0; i
< width
; i
++) {
165 row
[i
* 4 + 0] = clearR
;
166 row
[i
* 4 + 1] = clearG
;
167 row
[i
* 4 + 2] = clearB
;
168 row
[i
* 4 + 3] = clearA
;
170 accMap
+= accRowStride
;
174 /* other types someday? */
175 _mesa_warning(ctx
, "unexpected accum buffer type");
178 ctx
->Driver
.UnmapRenderbuffer(ctx
, accRb
);
189 accum_scale_or_bias(struct gl_context
*ctx
, GLfloat value
,
190 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
193 struct gl_renderbuffer
*accRb
=
194 ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
;
200 ctx
->Driver
.MapRenderbuffer(ctx
, accRb
, xpos
, ypos
, width
, height
,
201 GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
,
202 &accMap
, &accRowStride
);
205 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
209 if (accRb
->Format
== MESA_FORMAT_SIGNED_RGBA_16
) {
210 const GLshort incr
= (GLshort
) (value
* 32767.0f
);
213 for (j
= 0; j
< height
; j
++) {
214 GLshort
*acc
= (GLshort
*) accMap
;
215 for (i
= 0; i
< 4 * width
; i
++) {
218 accMap
+= accRowStride
;
223 for (j
= 0; j
< height
; j
++) {
224 GLshort
*acc
= (GLshort
*) accMap
;
225 for (i
= 0; i
< 4 * width
; i
++) {
226 acc
[i
] = (GLshort
) (acc
[i
] * value
);
228 accMap
+= accRowStride
;
233 /* other types someday? */
236 ctx
->Driver
.UnmapRenderbuffer(ctx
, accRb
);
242 * Accum = ColorBuf * value
244 * Accum += ColorBuf * value
247 accum_or_load(struct gl_context
*ctx
, GLfloat value
,
248 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
251 struct gl_renderbuffer
*accRb
=
252 ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
;
253 struct gl_renderbuffer
*colorRb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
254 GLubyte
*accMap
, *colorMap
;
255 GLint accRowStride
, colorRowStride
;
256 GLbitfield mappingFlags
;
259 /* no read buffer - OK */
265 mappingFlags
= GL_MAP_WRITE_BIT
;
266 if (!load
) /* if we're accumulating */
267 mappingFlags
|= GL_MAP_READ_BIT
;
269 /* Map accum buffer */
270 ctx
->Driver
.MapRenderbuffer(ctx
, accRb
, xpos
, ypos
, width
, height
,
271 mappingFlags
, &accMap
, &accRowStride
);
273 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
277 /* Map color buffer */
278 ctx
->Driver
.MapRenderbuffer(ctx
, colorRb
, xpos
, ypos
, width
, height
,
280 &colorMap
, &colorRowStride
);
282 ctx
->Driver
.UnmapRenderbuffer(ctx
, accRb
);
283 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
287 if (accRb
->Format
== MESA_FORMAT_SIGNED_RGBA_16
) {
288 const GLfloat scale
= value
* 32767.0f
;
292 rgba
= (GLfloat (*)[4]) malloc(width
* 4 * sizeof(GLfloat
));
294 for (j
= 0; j
< height
; j
++) {
295 GLshort
*acc
= (GLshort
*) accMap
;
297 /* read colors from source color buffer */
298 _mesa_unpack_rgba_row(colorRb
->Format
, width
, colorMap
, rgba
);
301 for (i
= 0; i
< width
; i
++) {
302 acc
[i
* 4 + 0] = (GLshort
) (rgba
[i
][RCOMP
] * scale
);
303 acc
[i
* 4 + 1] = (GLshort
) (rgba
[i
][GCOMP
] * scale
);
304 acc
[i
* 4 + 2] = (GLshort
) (rgba
[i
][BCOMP
] * scale
);
305 acc
[i
* 4 + 3] = (GLshort
) (rgba
[i
][ACOMP
] * scale
);
310 for (i
= 0; i
< width
; i
++) {
311 acc
[i
* 4 + 0] += (GLshort
) (rgba
[i
][RCOMP
] * scale
);
312 acc
[i
* 4 + 1] += (GLshort
) (rgba
[i
][GCOMP
] * scale
);
313 acc
[i
* 4 + 2] += (GLshort
) (rgba
[i
][BCOMP
] * scale
);
314 acc
[i
* 4 + 3] += (GLshort
) (rgba
[i
][ACOMP
] * scale
);
318 colorMap
+= colorRowStride
;
319 accMap
+= accRowStride
;
325 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
329 /* other types someday? */
332 ctx
->Driver
.UnmapRenderbuffer(ctx
, accRb
);
333 ctx
->Driver
.UnmapRenderbuffer(ctx
, colorRb
);
338 * ColorBuffer = Accum * value
341 accum_return(struct gl_context
*ctx
, GLfloat value
,
342 GLint xpos
, GLint ypos
, GLint width
, GLint height
)
344 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
345 struct gl_renderbuffer
*accRb
= fb
->Attachment
[BUFFER_ACCUM
].Renderbuffer
;
346 GLubyte
*accMap
, *colorMap
;
347 GLint accRowStride
, colorRowStride
;
350 /* Map accum buffer */
351 ctx
->Driver
.MapRenderbuffer(ctx
, accRb
, xpos
, ypos
, width
, height
,
353 &accMap
, &accRowStride
);
355 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
359 /* Loop over destination buffers */
360 for (buffer
= 0; buffer
< fb
->_NumColorDrawBuffers
; buffer
++) {
361 struct gl_renderbuffer
*colorRb
= fb
->_ColorDrawBuffers
[buffer
];
362 const GLboolean masking
= (!ctx
->Color
.ColorMask
[buffer
][RCOMP
] ||
363 !ctx
->Color
.ColorMask
[buffer
][GCOMP
] ||
364 !ctx
->Color
.ColorMask
[buffer
][BCOMP
] ||
365 !ctx
->Color
.ColorMask
[buffer
][ACOMP
]);
366 GLbitfield mappingFlags
= GL_MAP_WRITE_BIT
;
369 mappingFlags
|= GL_MAP_READ_BIT
;
371 /* Map color buffer */
372 ctx
->Driver
.MapRenderbuffer(ctx
, colorRb
, xpos
, ypos
, width
, height
,
373 mappingFlags
, &colorMap
, &colorRowStride
);
375 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
379 if (accRb
->Format
== MESA_FORMAT_SIGNED_RGBA_16
) {
380 const GLfloat scale
= value
/ 32767.0f
;
382 GLfloat (*rgba
)[4], (*dest
)[4];
384 rgba
= (GLfloat (*)[4]) malloc(width
* 4 * sizeof(GLfloat
));
385 dest
= (GLfloat (*)[4]) malloc(width
* 4 * sizeof(GLfloat
));
388 for (j
= 0; j
< height
; j
++) {
389 GLshort
*acc
= (GLshort
*) accMap
;
391 for (i
= 0; i
< width
; i
++) {
392 rgba
[i
][0] = acc
[i
* 4 + 0] * scale
;
393 rgba
[i
][1] = acc
[i
* 4 + 1] * scale
;
394 rgba
[i
][2] = acc
[i
* 4 + 2] * scale
;
395 rgba
[i
][3] = acc
[i
* 4 + 3] * scale
;
400 /* get existing colors from dest buffer */
401 _mesa_unpack_rgba_row(colorRb
->Format
, width
, colorMap
, dest
);
403 /* use the dest colors where mask[channel] = 0 */
404 if (ctx
->Color
.ColorMask
[buffer
][RCOMP
] == 0) {
405 for (i
= 0; i
< width
; i
++)
406 rgba
[i
][RCOMP
] = dest
[i
][RCOMP
];
408 if (ctx
->Color
.ColorMask
[buffer
][GCOMP
] == 0) {
409 for (i
= 0; i
< width
; i
++)
410 rgba
[i
][GCOMP
] = dest
[i
][GCOMP
];
412 if (ctx
->Color
.ColorMask
[buffer
][BCOMP
] == 0) {
413 for (i
= 0; i
< width
; i
++)
414 rgba
[i
][BCOMP
] = dest
[i
][BCOMP
];
416 if (ctx
->Color
.ColorMask
[buffer
][ACOMP
] == 0) {
417 for (i
= 0; i
< width
; i
++)
418 rgba
[i
][ACOMP
] = dest
[i
][ACOMP
];
422 _mesa_pack_float_rgba_row(colorRb
->Format
, width
,
423 (const GLfloat (*)[4]) rgba
, colorMap
);
425 accMap
+= accRowStride
;
426 colorMap
+= colorRowStride
;
430 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
436 /* other types someday? */
439 ctx
->Driver
.UnmapRenderbuffer(ctx
, colorRb
);
442 ctx
->Driver
.UnmapRenderbuffer(ctx
, accRb
);
448 * Software fallback for glAccum. A hardware driver that supports
449 * signed 16-bit color channels could implement hardware accumulation
450 * operations, but no driver does so at this time.
453 _mesa_accum(struct gl_context
*ctx
, GLenum op
, GLfloat value
)
455 GLint xpos
, ypos
, width
, height
;
457 if (!ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
) {
458 _mesa_warning(ctx
, "Calling glAccum() without an accumulation buffer");
462 if (!_mesa_check_conditional_render(ctx
))
465 xpos
= ctx
->DrawBuffer
->_Xmin
;
466 ypos
= ctx
->DrawBuffer
->_Ymin
;
467 width
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
468 height
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
473 accum_scale_or_bias(ctx
, value
, xpos
, ypos
, width
, height
, GL_TRUE
);
478 accum_scale_or_bias(ctx
, value
, xpos
, ypos
, width
, height
, GL_FALSE
);
483 accum_or_load(ctx
, value
, xpos
, ypos
, width
, height
, GL_FALSE
);
487 accum_or_load(ctx
, value
, xpos
, ypos
, width
, height
, GL_TRUE
);
490 accum_return(ctx
, value
, xpos
, ypos
, width
, height
);
493 _mesa_problem(ctx
, "invalid mode in _mesa_accum()");
499 #endif /* FEATURE_accum */
503 _mesa_init_accum( struct gl_context
*ctx
)
505 /* Accumulate buffer group */
506 ASSIGN_4V( ctx
->Accum
.ClearColor
, 0.0, 0.0, 0.0, 0.0 );