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
);
44 ASSERT_OUTSIDE_BEGIN_END(ctx
);
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
);
51 if (TEST_EQ_4V(tmp
, ctx
->Accum
.ClearColor
))
54 COPY_4FV( ctx
->Accum
.ClearColor
, tmp
);
59 _mesa_Accum( GLenum op
, GLfloat value
)
61 GET_CURRENT_CONTEXT(ctx
);
62 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
73 _mesa_error(ctx
, GL_INVALID_ENUM
, "glAccum(op)");
77 if (ctx
->DrawBuffer
->Visual
.haveAccumBuffer
== 0) {
78 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glAccum(no accum buffer)");
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.
86 _mesa_error(ctx
, GL_INVALID_OPERATION
,
87 "glAccum(different read/draw buffers)");
92 _mesa_update_state(ctx
);
94 if (ctx
->DrawBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
95 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
96 "glAccum(incomplete framebuffer)");
100 if (ctx
->RasterDiscard
)
103 if (ctx
->RenderMode
== GL_RENDER
) {
104 _mesa_accum(ctx
, op
, value
);
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.
115 _mesa_clear_accum_buffer(struct gl_context
*ctx
)
117 GLuint x
, y
, width
, height
;
120 struct gl_renderbuffer
*accRb
;
122 if (!ctx
->DrawBuffer
)
125 accRb
= ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
;
127 return; /* missing accum buffer, not an error */
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
;
135 ctx
->Driver
.MapRenderbuffer(ctx
, accRb
, x
, y
, width
, height
,
136 GL_MAP_WRITE_BIT
, &accMap
, &accRowStride
);
139 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
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]);
150 for (j
= 0; j
< height
; j
++) {
151 GLshort
*row
= (GLshort
*) accMap
;
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
;
159 accMap
+= accRowStride
;
163 /* other types someday? */
164 _mesa_warning(ctx
, "unexpected accum buffer type");
167 ctx
->Driver
.UnmapRenderbuffer(ctx
, accRb
);
178 accum_scale_or_bias(struct gl_context
*ctx
, GLfloat value
,
179 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
182 struct gl_renderbuffer
*accRb
=
183 ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
;
189 ctx
->Driver
.MapRenderbuffer(ctx
, accRb
, xpos
, ypos
, width
, height
,
190 GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
,
191 &accMap
, &accRowStride
);
194 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
198 if (accRb
->Format
== MESA_FORMAT_SIGNED_RGBA_16
) {
199 const GLshort incr
= (GLshort
) (value
* 32767.0f
);
202 for (j
= 0; j
< height
; j
++) {
203 GLshort
*acc
= (GLshort
*) accMap
;
204 for (i
= 0; i
< 4 * width
; i
++) {
207 accMap
+= accRowStride
;
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
);
217 accMap
+= accRowStride
;
222 /* other types someday? */
225 ctx
->Driver
.UnmapRenderbuffer(ctx
, accRb
);
231 * Accum = ColorBuf * value
233 * Accum += ColorBuf * value
236 accum_or_load(struct gl_context
*ctx
, GLfloat value
,
237 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
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
;
248 /* no read buffer - OK */
254 mappingFlags
= GL_MAP_WRITE_BIT
;
255 if (!load
) /* if we're accumulating */
256 mappingFlags
|= GL_MAP_READ_BIT
;
258 /* Map accum buffer */
259 ctx
->Driver
.MapRenderbuffer(ctx
, accRb
, xpos
, ypos
, width
, height
,
260 mappingFlags
, &accMap
, &accRowStride
);
262 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
266 /* Map color buffer */
267 ctx
->Driver
.MapRenderbuffer(ctx
, colorRb
, xpos
, ypos
, width
, height
,
269 &colorMap
, &colorRowStride
);
271 ctx
->Driver
.UnmapRenderbuffer(ctx
, accRb
);
272 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
276 if (accRb
->Format
== MESA_FORMAT_SIGNED_RGBA_16
) {
277 const GLfloat scale
= value
* 32767.0f
;
281 rgba
= malloc(width
* 4 * sizeof(GLfloat
));
283 for (j
= 0; j
< height
; j
++) {
284 GLshort
*acc
= (GLshort
*) accMap
;
286 /* read colors from source color buffer */
287 _mesa_unpack_rgba_row(colorRb
->Format
, width
, colorMap
, rgba
);
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
);
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
);
307 colorMap
+= colorRowStride
;
308 accMap
+= accRowStride
;
314 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
318 /* other types someday? */
321 ctx
->Driver
.UnmapRenderbuffer(ctx
, accRb
);
322 ctx
->Driver
.UnmapRenderbuffer(ctx
, colorRb
);
327 * ColorBuffer = Accum * value
330 accum_return(struct gl_context
*ctx
, GLfloat value
,
331 GLint xpos
, GLint ypos
, GLint width
, GLint height
)
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
;
339 /* Map accum buffer */
340 ctx
->Driver
.MapRenderbuffer(ctx
, accRb
, xpos
, ypos
, width
, height
,
342 &accMap
, &accRowStride
);
344 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
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
;
358 mappingFlags
|= GL_MAP_READ_BIT
;
360 /* Map color buffer */
361 ctx
->Driver
.MapRenderbuffer(ctx
, colorRb
, xpos
, ypos
, width
, height
,
362 mappingFlags
, &colorMap
, &colorRowStride
);
364 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
368 if (accRb
->Format
== MESA_FORMAT_SIGNED_RGBA_16
) {
369 const GLfloat scale
= value
/ 32767.0f
;
371 GLfloat (*rgba
)[4], (*dest
)[4];
373 rgba
= malloc(width
* 4 * sizeof(GLfloat
));
374 dest
= malloc(width
* 4 * sizeof(GLfloat
));
377 for (j
= 0; j
< height
; j
++) {
378 GLshort
*acc
= (GLshort
*) accMap
;
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
;
389 /* get existing colors from dest buffer */
390 _mesa_unpack_rgba_row(colorRb
->Format
, width
, colorMap
, dest
);
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
];
397 if (ctx
->Color
.ColorMask
[buffer
][GCOMP
] == 0) {
398 for (i
= 0; i
< width
; i
++)
399 rgba
[i
][GCOMP
] = dest
[i
][GCOMP
];
401 if (ctx
->Color
.ColorMask
[buffer
][BCOMP
] == 0) {
402 for (i
= 0; i
< width
; i
++)
403 rgba
[i
][BCOMP
] = dest
[i
][BCOMP
];
405 if (ctx
->Color
.ColorMask
[buffer
][ACOMP
] == 0) {
406 for (i
= 0; i
< width
; i
++)
407 rgba
[i
][ACOMP
] = dest
[i
][ACOMP
];
411 _mesa_pack_float_rgba_row(colorRb
->Format
, width
,
412 (const GLfloat (*)[4]) rgba
, colorMap
);
414 accMap
+= accRowStride
;
415 colorMap
+= colorRowStride
;
419 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
425 /* other types someday? */
428 ctx
->Driver
.UnmapRenderbuffer(ctx
, colorRb
);
431 ctx
->Driver
.UnmapRenderbuffer(ctx
, accRb
);
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.
442 _mesa_accum(struct gl_context
*ctx
, GLenum op
, GLfloat value
)
444 GLint xpos
, ypos
, width
, height
;
446 if (!ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
) {
447 _mesa_warning(ctx
, "Calling glAccum() without an accumulation buffer");
451 if (!_mesa_check_conditional_render(ctx
))
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
;
462 accum_scale_or_bias(ctx
, value
, xpos
, ypos
, width
, height
, GL_TRUE
);
467 accum_scale_or_bias(ctx
, value
, xpos
, ypos
, width
, height
, GL_FALSE
);
472 accum_or_load(ctx
, value
, xpos
, ypos
, width
, height
, GL_FALSE
);
476 accum_or_load(ctx
, value
, xpos
, ypos
, width
, height
, GL_TRUE
);
479 accum_return(ctx
, value
, xpos
, ypos
, width
, height
);
482 _mesa_problem(ctx
, "invalid mode in _mesa_accum()");
489 _mesa_init_accum( struct gl_context
*ctx
)
491 /* Accumulate buffer group */
492 ASSIGN_4V( ctx
->Accum
.ClearColor
, 0.0, 0.0, 0.0, 0.0 );