2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
27 #include "condrender.h"
29 #include "format_unpack.h"
30 #include "format_pack.h"
31 #include "framebuffer.h"
39 _mesa_ClearAccum( GLfloat red
, GLfloat green
, GLfloat blue
, GLfloat alpha
)
42 GET_CURRENT_CONTEXT(ctx
);
44 tmp
[0] = CLAMP( red
, -1.0F
, 1.0F
);
45 tmp
[1] = CLAMP( green
, -1.0F
, 1.0F
);
46 tmp
[2] = CLAMP( blue
, -1.0F
, 1.0F
);
47 tmp
[3] = CLAMP( alpha
, -1.0F
, 1.0F
);
49 if (TEST_EQ_4V(tmp
, ctx
->Accum
.ClearColor
))
52 COPY_4FV( ctx
->Accum
.ClearColor
, tmp
);
57 * Clear the accumulation buffer by mapping the renderbuffer and
58 * writing the clear color to it. Called by the driver's implementation
59 * of the glClear function.
62 _mesa_clear_accum_buffer(struct gl_context
*ctx
)
64 GLuint x
, y
, width
, height
;
67 struct gl_renderbuffer
*accRb
;
72 accRb
= ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
;
74 return; /* missing accum buffer, not an error */
76 _mesa_update_draw_buffer_bounds(ctx
, ctx
->DrawBuffer
);
78 /* bounds, with scissor */
79 x
= ctx
->DrawBuffer
->_Xmin
;
80 y
= ctx
->DrawBuffer
->_Ymin
;
81 width
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
82 height
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
84 ctx
->Driver
.MapRenderbuffer(ctx
, accRb
, x
, y
, width
, height
,
85 GL_MAP_WRITE_BIT
, &accMap
, &accRowStride
,
86 ctx
->DrawBuffer
->FlipY
);
89 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
93 if (accRb
->Format
== MESA_FORMAT_RGBA_SNORM16
) {
94 const GLshort clearR
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[0]);
95 const GLshort clearG
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[1]);
96 const GLshort clearB
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[2]);
97 const GLshort clearA
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[3]);
100 for (j
= 0; j
< height
; j
++) {
101 GLshort
*row
= (GLshort
*) accMap
;
103 for (i
= 0; i
< width
; i
++) {
104 row
[i
* 4 + 0] = clearR
;
105 row
[i
* 4 + 1] = clearG
;
106 row
[i
* 4 + 2] = clearB
;
107 row
[i
* 4 + 3] = clearA
;
109 accMap
+= accRowStride
;
113 /* other types someday? */
114 _mesa_warning(ctx
, "unexpected accum buffer type");
117 ctx
->Driver
.UnmapRenderbuffer(ctx
, accRb
);
128 accum_scale_or_bias(struct gl_context
*ctx
, GLfloat value
,
129 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
132 struct gl_renderbuffer
*accRb
=
133 ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
;
139 ctx
->Driver
.MapRenderbuffer(ctx
, accRb
, xpos
, ypos
, width
, height
,
140 GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
,
141 &accMap
, &accRowStride
,
142 ctx
->DrawBuffer
->FlipY
);
145 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
149 if (accRb
->Format
== MESA_FORMAT_RGBA_SNORM16
) {
150 const GLshort incr
= (GLshort
) (value
* 32767.0f
);
153 for (j
= 0; j
< height
; j
++) {
154 GLshort
*acc
= (GLshort
*) accMap
;
155 for (i
= 0; i
< 4 * width
; i
++) {
158 accMap
+= accRowStride
;
163 for (j
= 0; j
< height
; j
++) {
164 GLshort
*acc
= (GLshort
*) accMap
;
165 for (i
= 0; i
< 4 * width
; i
++) {
166 acc
[i
] = (GLshort
) (acc
[i
] * value
);
168 accMap
+= accRowStride
;
173 /* other types someday? */
176 ctx
->Driver
.UnmapRenderbuffer(ctx
, accRb
);
182 * Accum = ColorBuf * value
184 * Accum += ColorBuf * value
187 accum_or_load(struct gl_context
*ctx
, GLfloat value
,
188 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
191 struct gl_renderbuffer
*accRb
=
192 ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
;
193 struct gl_renderbuffer
*colorRb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
194 GLubyte
*accMap
, *colorMap
;
195 GLint accRowStride
, colorRowStride
;
196 GLbitfield mappingFlags
;
199 /* no read buffer - OK */
205 mappingFlags
= GL_MAP_WRITE_BIT
;
206 if (!load
) /* if we're accumulating */
207 mappingFlags
|= GL_MAP_READ_BIT
;
209 /* Map accum buffer */
210 ctx
->Driver
.MapRenderbuffer(ctx
, accRb
, xpos
, ypos
, width
, height
,
211 mappingFlags
, &accMap
, &accRowStride
,
212 ctx
->DrawBuffer
->FlipY
);
214 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
218 /* Map color buffer */
219 ctx
->Driver
.MapRenderbuffer(ctx
, colorRb
, xpos
, ypos
, width
, height
,
221 &colorMap
, &colorRowStride
,
222 ctx
->DrawBuffer
->FlipY
);
224 ctx
->Driver
.UnmapRenderbuffer(ctx
, accRb
);
225 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
229 if (accRb
->Format
== MESA_FORMAT_RGBA_SNORM16
) {
230 const GLfloat scale
= value
* 32767.0f
;
234 rgba
= malloc(width
* 4 * sizeof(GLfloat
));
236 for (j
= 0; j
< height
; j
++) {
237 GLshort
*acc
= (GLshort
*) accMap
;
239 /* read colors from source color buffer */
240 _mesa_unpack_rgba_row(colorRb
->Format
, width
, colorMap
, rgba
);
243 for (i
= 0; i
< width
; i
++) {
244 acc
[i
* 4 + 0] = (GLshort
) (rgba
[i
][RCOMP
] * scale
);
245 acc
[i
* 4 + 1] = (GLshort
) (rgba
[i
][GCOMP
] * scale
);
246 acc
[i
* 4 + 2] = (GLshort
) (rgba
[i
][BCOMP
] * scale
);
247 acc
[i
* 4 + 3] = (GLshort
) (rgba
[i
][ACOMP
] * scale
);
252 for (i
= 0; i
< width
; i
++) {
253 acc
[i
* 4 + 0] += (GLshort
) (rgba
[i
][RCOMP
] * scale
);
254 acc
[i
* 4 + 1] += (GLshort
) (rgba
[i
][GCOMP
] * scale
);
255 acc
[i
* 4 + 2] += (GLshort
) (rgba
[i
][BCOMP
] * scale
);
256 acc
[i
* 4 + 3] += (GLshort
) (rgba
[i
][ACOMP
] * scale
);
260 colorMap
+= colorRowStride
;
261 accMap
+= accRowStride
;
267 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
271 /* other types someday? */
274 ctx
->Driver
.UnmapRenderbuffer(ctx
, accRb
);
275 ctx
->Driver
.UnmapRenderbuffer(ctx
, colorRb
);
280 * ColorBuffer = Accum * value
283 accum_return(struct gl_context
*ctx
, GLfloat value
,
284 GLint xpos
, GLint ypos
, GLint width
, GLint height
)
286 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
287 struct gl_renderbuffer
*accRb
= fb
->Attachment
[BUFFER_ACCUM
].Renderbuffer
;
288 GLubyte
*accMap
, *colorMap
;
289 GLint accRowStride
, colorRowStride
;
292 /* Map accum buffer */
293 ctx
->Driver
.MapRenderbuffer(ctx
, accRb
, xpos
, ypos
, width
, height
,
295 &accMap
, &accRowStride
, fb
->FlipY
);
297 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
301 /* Loop over destination buffers */
302 for (buffer
= 0; buffer
< fb
->_NumColorDrawBuffers
; buffer
++) {
303 struct gl_renderbuffer
*colorRb
= fb
->_ColorDrawBuffers
[buffer
];
304 const GLboolean masking
= (!GET_COLORMASK_BIT(ctx
->Color
.ColorMask
, buffer
, 0) ||
305 !GET_COLORMASK_BIT(ctx
->Color
.ColorMask
, buffer
, 1) ||
306 !GET_COLORMASK_BIT(ctx
->Color
.ColorMask
, buffer
, 2) ||
307 !GET_COLORMASK_BIT(ctx
->Color
.ColorMask
, buffer
, 3));
308 GLbitfield mappingFlags
= GL_MAP_WRITE_BIT
;
311 mappingFlags
|= GL_MAP_READ_BIT
;
313 /* Map color buffer */
314 ctx
->Driver
.MapRenderbuffer(ctx
, colorRb
, xpos
, ypos
, width
, height
,
315 mappingFlags
, &colorMap
, &colorRowStride
,
318 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
322 if (accRb
->Format
== MESA_FORMAT_RGBA_SNORM16
) {
323 const GLfloat scale
= value
/ 32767.0f
;
325 GLfloat (*rgba
)[4], (*dest
)[4];
327 rgba
= malloc(width
* 4 * sizeof(GLfloat
));
328 dest
= malloc(width
* 4 * sizeof(GLfloat
));
331 for (j
= 0; j
< height
; j
++) {
332 GLshort
*acc
= (GLshort
*) accMap
;
334 for (i
= 0; i
< width
; i
++) {
335 rgba
[i
][0] = acc
[i
* 4 + 0] * scale
;
336 rgba
[i
][1] = acc
[i
* 4 + 1] * scale
;
337 rgba
[i
][2] = acc
[i
* 4 + 2] * scale
;
338 rgba
[i
][3] = acc
[i
* 4 + 3] * scale
;
343 /* get existing colors from dest buffer */
344 _mesa_unpack_rgba_row(colorRb
->Format
, width
, colorMap
, dest
);
346 /* use the dest colors where mask[channel] = 0 */
347 if (!GET_COLORMASK_BIT(ctx
->Color
.ColorMask
, buffer
, 0)) {
348 for (i
= 0; i
< width
; i
++)
349 rgba
[i
][RCOMP
] = dest
[i
][RCOMP
];
351 if (!GET_COLORMASK_BIT(ctx
->Color
.ColorMask
, buffer
, 1)) {
352 for (i
= 0; i
< width
; i
++)
353 rgba
[i
][GCOMP
] = dest
[i
][GCOMP
];
355 if (!GET_COLORMASK_BIT(ctx
->Color
.ColorMask
, buffer
, 2)) {
356 for (i
= 0; i
< width
; i
++)
357 rgba
[i
][BCOMP
] = dest
[i
][BCOMP
];
359 if (!GET_COLORMASK_BIT(ctx
->Color
.ColorMask
, buffer
, 3)) {
360 for (i
= 0; i
< width
; i
++)
361 rgba
[i
][ACOMP
] = dest
[i
][ACOMP
];
365 _mesa_pack_float_rgba_row(colorRb
->Format
, width
,
366 (const GLfloat (*)[4]) rgba
, colorMap
);
368 accMap
+= accRowStride
;
369 colorMap
+= colorRowStride
;
373 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
379 /* other types someday? */
382 ctx
->Driver
.UnmapRenderbuffer(ctx
, colorRb
);
385 ctx
->Driver
.UnmapRenderbuffer(ctx
, accRb
);
391 * Software fallback for glAccum. A hardware driver that supports
392 * signed 16-bit color channels could implement hardware accumulation
393 * operations, but no driver does so at this time.
396 accum(struct gl_context
*ctx
, GLenum op
, GLfloat value
)
398 GLint xpos
, ypos
, width
, height
;
400 if (!ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
) {
401 _mesa_warning(ctx
, "Calling glAccum() without an accumulation buffer");
405 if (!_mesa_check_conditional_render(ctx
))
408 _mesa_update_draw_buffer_bounds(ctx
, ctx
->DrawBuffer
);
410 xpos
= ctx
->DrawBuffer
->_Xmin
;
411 ypos
= ctx
->DrawBuffer
->_Ymin
;
412 width
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
413 height
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
418 accum_scale_or_bias(ctx
, value
, xpos
, ypos
, width
, height
, GL_TRUE
);
423 accum_scale_or_bias(ctx
, value
, xpos
, ypos
, width
, height
, GL_FALSE
);
428 accum_or_load(ctx
, value
, xpos
, ypos
, width
, height
, GL_FALSE
);
432 accum_or_load(ctx
, value
, xpos
, ypos
, width
, height
, GL_TRUE
);
435 accum_return(ctx
, value
, xpos
, ypos
, width
, height
);
438 unreachable("invalid mode in _mesa_Accum()");
445 _mesa_init_accum( struct gl_context
*ctx
)
447 /* Accumulate buffer group */
448 ASSIGN_4V( ctx
->Accum
.ClearColor
, 0.0, 0.0, 0.0, 0.0 );
453 _mesa_Accum( GLenum op
, GLfloat value
)
455 GET_CURRENT_CONTEXT(ctx
);
456 FLUSH_VERTICES(ctx
, 0);
467 _mesa_error(ctx
, GL_INVALID_ENUM
, "glAccum(op)");
471 if (ctx
->DrawBuffer
->Visual
.haveAccumBuffer
== 0) {
472 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glAccum(no accum buffer)");
476 if (ctx
->DrawBuffer
!= ctx
->ReadBuffer
) {
477 /* See GLX_SGI_make_current_read or WGL_ARB_make_current_read,
478 * or GL_EXT_framebuffer_blit.
480 _mesa_error(ctx
, GL_INVALID_OPERATION
,
481 "glAccum(different read/draw buffers)");
486 _mesa_update_state(ctx
);
488 if (ctx
->DrawBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
489 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
490 "glAccum(incomplete framebuffer)");
494 if (ctx
->RasterDiscard
)
497 if (ctx
->RenderMode
== GL_RENDER
) {
498 accum(ctx
, op
, value
);