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"
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 * Clear the accumulation buffer by mapping the renderbuffer and
59 * writing the clear color to it. Called by the driver's implementation
60 * of the glClear function.
63 _mesa_clear_accum_buffer(struct gl_context
*ctx
)
65 GLuint x
, y
, width
, height
;
68 struct gl_renderbuffer
*accRb
;
73 accRb
= ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
;
75 return; /* missing accum buffer, not an error */
77 _mesa_update_draw_buffer_bounds(ctx
, ctx
->DrawBuffer
);
79 /* bounds, with scissor */
80 x
= ctx
->DrawBuffer
->_Xmin
;
81 y
= ctx
->DrawBuffer
->_Ymin
;
82 width
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
83 height
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
85 ctx
->Driver
.MapRenderbuffer(ctx
, accRb
, x
, y
, width
, height
,
86 GL_MAP_WRITE_BIT
, &accMap
, &accRowStride
);
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
);
144 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
148 if (accRb
->Format
== MESA_FORMAT_RGBA_SNORM16
) {
149 const GLshort incr
= (GLshort
) (value
* 32767.0f
);
152 for (j
= 0; j
< height
; j
++) {
153 GLshort
*acc
= (GLshort
*) accMap
;
154 for (i
= 0; i
< 4 * width
; i
++) {
157 accMap
+= accRowStride
;
162 for (j
= 0; j
< height
; j
++) {
163 GLshort
*acc
= (GLshort
*) accMap
;
164 for (i
= 0; i
< 4 * width
; i
++) {
165 acc
[i
] = (GLshort
) (acc
[i
] * value
);
167 accMap
+= accRowStride
;
172 /* other types someday? */
175 ctx
->Driver
.UnmapRenderbuffer(ctx
, accRb
);
181 * Accum = ColorBuf * value
183 * Accum += ColorBuf * value
186 accum_or_load(struct gl_context
*ctx
, GLfloat value
,
187 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
190 struct gl_renderbuffer
*accRb
=
191 ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
;
192 struct gl_renderbuffer
*colorRb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
193 GLubyte
*accMap
, *colorMap
;
194 GLint accRowStride
, colorRowStride
;
195 GLbitfield mappingFlags
;
198 /* no read buffer - OK */
204 mappingFlags
= GL_MAP_WRITE_BIT
;
205 if (!load
) /* if we're accumulating */
206 mappingFlags
|= GL_MAP_READ_BIT
;
208 /* Map accum buffer */
209 ctx
->Driver
.MapRenderbuffer(ctx
, accRb
, xpos
, ypos
, width
, height
,
210 mappingFlags
, &accMap
, &accRowStride
);
212 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
216 /* Map color buffer */
217 ctx
->Driver
.MapRenderbuffer(ctx
, colorRb
, xpos
, ypos
, width
, height
,
219 &colorMap
, &colorRowStride
);
221 ctx
->Driver
.UnmapRenderbuffer(ctx
, accRb
);
222 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
226 if (accRb
->Format
== MESA_FORMAT_RGBA_SNORM16
) {
227 const GLfloat scale
= value
* 32767.0f
;
231 rgba
= malloc(width
* 4 * sizeof(GLfloat
));
233 for (j
= 0; j
< height
; j
++) {
234 GLshort
*acc
= (GLshort
*) accMap
;
236 /* read colors from source color buffer */
237 _mesa_unpack_rgba_row(colorRb
->Format
, width
, colorMap
, rgba
);
240 for (i
= 0; i
< width
; i
++) {
241 acc
[i
* 4 + 0] = (GLshort
) (rgba
[i
][RCOMP
] * scale
);
242 acc
[i
* 4 + 1] = (GLshort
) (rgba
[i
][GCOMP
] * scale
);
243 acc
[i
* 4 + 2] = (GLshort
) (rgba
[i
][BCOMP
] * scale
);
244 acc
[i
* 4 + 3] = (GLshort
) (rgba
[i
][ACOMP
] * scale
);
249 for (i
= 0; i
< width
; i
++) {
250 acc
[i
* 4 + 0] += (GLshort
) (rgba
[i
][RCOMP
] * scale
);
251 acc
[i
* 4 + 1] += (GLshort
) (rgba
[i
][GCOMP
] * scale
);
252 acc
[i
* 4 + 2] += (GLshort
) (rgba
[i
][BCOMP
] * scale
);
253 acc
[i
* 4 + 3] += (GLshort
) (rgba
[i
][ACOMP
] * scale
);
257 colorMap
+= colorRowStride
;
258 accMap
+= accRowStride
;
264 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
268 /* other types someday? */
271 ctx
->Driver
.UnmapRenderbuffer(ctx
, accRb
);
272 ctx
->Driver
.UnmapRenderbuffer(ctx
, colorRb
);
277 * ColorBuffer = Accum * value
280 accum_return(struct gl_context
*ctx
, GLfloat value
,
281 GLint xpos
, GLint ypos
, GLint width
, GLint height
)
283 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
284 struct gl_renderbuffer
*accRb
= fb
->Attachment
[BUFFER_ACCUM
].Renderbuffer
;
285 GLubyte
*accMap
, *colorMap
;
286 GLint accRowStride
, colorRowStride
;
289 /* Map accum buffer */
290 ctx
->Driver
.MapRenderbuffer(ctx
, accRb
, xpos
, ypos
, width
, height
,
292 &accMap
, &accRowStride
);
294 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
298 /* Loop over destination buffers */
299 for (buffer
= 0; buffer
< fb
->_NumColorDrawBuffers
; buffer
++) {
300 struct gl_renderbuffer
*colorRb
= fb
->_ColorDrawBuffers
[buffer
];
301 const GLboolean masking
= (!ctx
->Color
.ColorMask
[buffer
][RCOMP
] ||
302 !ctx
->Color
.ColorMask
[buffer
][GCOMP
] ||
303 !ctx
->Color
.ColorMask
[buffer
][BCOMP
] ||
304 !ctx
->Color
.ColorMask
[buffer
][ACOMP
]);
305 GLbitfield mappingFlags
= GL_MAP_WRITE_BIT
;
308 mappingFlags
|= GL_MAP_READ_BIT
;
310 /* Map color buffer */
311 ctx
->Driver
.MapRenderbuffer(ctx
, colorRb
, xpos
, ypos
, width
, height
,
312 mappingFlags
, &colorMap
, &colorRowStride
);
314 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
318 if (accRb
->Format
== MESA_FORMAT_RGBA_SNORM16
) {
319 const GLfloat scale
= value
/ 32767.0f
;
321 GLfloat (*rgba
)[4], (*dest
)[4];
323 rgba
= malloc(width
* 4 * sizeof(GLfloat
));
324 dest
= malloc(width
* 4 * sizeof(GLfloat
));
327 for (j
= 0; j
< height
; j
++) {
328 GLshort
*acc
= (GLshort
*) accMap
;
330 for (i
= 0; i
< width
; i
++) {
331 rgba
[i
][0] = acc
[i
* 4 + 0] * scale
;
332 rgba
[i
][1] = acc
[i
* 4 + 1] * scale
;
333 rgba
[i
][2] = acc
[i
* 4 + 2] * scale
;
334 rgba
[i
][3] = acc
[i
* 4 + 3] * scale
;
339 /* get existing colors from dest buffer */
340 _mesa_unpack_rgba_row(colorRb
->Format
, width
, colorMap
, dest
);
342 /* use the dest colors where mask[channel] = 0 */
343 if (ctx
->Color
.ColorMask
[buffer
][RCOMP
] == 0) {
344 for (i
= 0; i
< width
; i
++)
345 rgba
[i
][RCOMP
] = dest
[i
][RCOMP
];
347 if (ctx
->Color
.ColorMask
[buffer
][GCOMP
] == 0) {
348 for (i
= 0; i
< width
; i
++)
349 rgba
[i
][GCOMP
] = dest
[i
][GCOMP
];
351 if (ctx
->Color
.ColorMask
[buffer
][BCOMP
] == 0) {
352 for (i
= 0; i
< width
; i
++)
353 rgba
[i
][BCOMP
] = dest
[i
][BCOMP
];
355 if (ctx
->Color
.ColorMask
[buffer
][ACOMP
] == 0) {
356 for (i
= 0; i
< width
; i
++)
357 rgba
[i
][ACOMP
] = dest
[i
][ACOMP
];
361 _mesa_pack_float_rgba_row(colorRb
->Format
, width
,
362 (const GLfloat (*)[4]) rgba
, colorMap
);
364 accMap
+= accRowStride
;
365 colorMap
+= colorRowStride
;
369 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
375 /* other types someday? */
378 ctx
->Driver
.UnmapRenderbuffer(ctx
, colorRb
);
381 ctx
->Driver
.UnmapRenderbuffer(ctx
, accRb
);
387 * Software fallback for glAccum. A hardware driver that supports
388 * signed 16-bit color channels could implement hardware accumulation
389 * operations, but no driver does so at this time.
392 accum(struct gl_context
*ctx
, GLenum op
, GLfloat value
)
394 GLint xpos
, ypos
, width
, height
;
396 if (!ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
) {
397 _mesa_warning(ctx
, "Calling glAccum() without an accumulation buffer");
401 if (!_mesa_check_conditional_render(ctx
))
404 _mesa_update_draw_buffer_bounds(ctx
, ctx
->DrawBuffer
);
406 xpos
= ctx
->DrawBuffer
->_Xmin
;
407 ypos
= ctx
->DrawBuffer
->_Ymin
;
408 width
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
409 height
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
414 accum_scale_or_bias(ctx
, value
, xpos
, ypos
, width
, height
, GL_TRUE
);
419 accum_scale_or_bias(ctx
, value
, xpos
, ypos
, width
, height
, GL_FALSE
);
424 accum_or_load(ctx
, value
, xpos
, ypos
, width
, height
, GL_FALSE
);
428 accum_or_load(ctx
, value
, xpos
, ypos
, width
, height
, GL_TRUE
);
431 accum_return(ctx
, value
, xpos
, ypos
, width
, height
);
434 unreachable("invalid mode in _mesa_Accum()");
441 _mesa_init_accum( struct gl_context
*ctx
)
443 /* Accumulate buffer group */
444 ASSIGN_4V( ctx
->Accum
.ClearColor
, 0.0, 0.0, 0.0, 0.0 );
449 _mesa_Accum( GLenum op
, GLfloat value
)
451 GET_CURRENT_CONTEXT(ctx
);
452 FLUSH_VERTICES(ctx
, 0);
463 _mesa_error(ctx
, GL_INVALID_ENUM
, "glAccum(op)");
467 if (ctx
->DrawBuffer
->Visual
.haveAccumBuffer
== 0) {
468 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glAccum(no accum buffer)");
472 if (ctx
->DrawBuffer
!= ctx
->ReadBuffer
) {
473 /* See GLX_SGI_make_current_read or WGL_ARB_make_current_read,
474 * or GL_EXT_framebuffer_blit.
476 _mesa_error(ctx
, GL_INVALID_OPERATION
,
477 "glAccum(different read/draw buffers)");
482 _mesa_update_state(ctx
);
484 if (ctx
->DrawBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
485 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
486 "glAccum(incomplete framebuffer)");
490 if (ctx
->RasterDiscard
)
493 if (ctx
->RenderMode
== GL_RENDER
) {
494 accum(ctx
, op
, value
);