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
->TransformFeedback
.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
);
120 #endif /* FEATURE_accum */
124 _mesa_init_accum( struct gl_context
*ctx
)
126 /* Accumulate buffer group */
127 ASSIGN_4V( ctx
->Accum
.ClearColor
, 0.0, 0.0, 0.0, 0.0 );
134 * Clear the accumulation buffer by mapping the renderbuffer and
135 * writing the clear color to it. Called by the driver's implementation
136 * of the glClear function.
139 _mesa_clear_accum_buffer(struct gl_context
*ctx
)
141 GLuint x
, y
, width
, height
;
144 struct gl_renderbuffer
*accRb
;
146 if (!ctx
->DrawBuffer
)
149 accRb
= ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
;
151 return; /* missing accum buffer, not an error */
153 /* bounds, with scissor */
154 x
= ctx
->DrawBuffer
->_Xmin
;
155 y
= ctx
->DrawBuffer
->_Ymin
;
156 width
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
157 height
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
159 ctx
->Driver
.MapRenderbuffer(ctx
, accRb
, x
, y
, width
, height
,
160 GL_MAP_WRITE_BIT
, &accMap
, &accRowStride
);
163 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
167 if (accRb
->Format
== MESA_FORMAT_SIGNED_RGBA_16
) {
168 const GLshort clearR
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[0]);
169 const GLshort clearG
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[1]);
170 const GLshort clearB
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[2]);
171 const GLshort clearA
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[3]);
174 for (j
= 0; j
< height
; j
++) {
175 GLshort
*row
= (GLshort
*) accMap
;
177 for (i
= 0; i
< width
; i
++) {
178 row
[i
* 4 + 0] = clearR
;
179 row
[i
* 4 + 1] = clearG
;
180 row
[i
* 4 + 2] = clearB
;
181 row
[i
* 4 + 3] = clearA
;
183 accMap
+= accRowStride
;
187 /* other types someday? */
188 _mesa_warning(ctx
, "unexpected accum buffer type");
191 ctx
->Driver
.UnmapRenderbuffer(ctx
, accRb
);
202 accum_scale_or_bias(struct gl_context
*ctx
, GLfloat value
,
203 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
206 struct gl_renderbuffer
*accRb
=
207 ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
;
213 ctx
->Driver
.MapRenderbuffer(ctx
, accRb
, xpos
, ypos
, width
, height
,
214 GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
,
215 &accMap
, &accRowStride
);
218 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
222 if (accRb
->Format
== MESA_FORMAT_SIGNED_RGBA_16
) {
223 const GLshort incr
= (GLshort
) (value
* 32767.0f
);
226 for (j
= 0; j
< height
; j
++) {
227 GLshort
*acc
= (GLshort
*) accMap
;
228 for (i
= 0; i
< 4 * width
; i
++) {
231 accMap
+= accRowStride
;
236 for (j
= 0; j
< height
; j
++) {
237 GLshort
*acc
= (GLshort
*) accMap
;
238 for (i
= 0; i
< 4 * width
; i
++) {
239 acc
[i
] = (GLshort
) (acc
[i
] * value
);
241 accMap
+= accRowStride
;
246 /* other types someday? */
249 ctx
->Driver
.UnmapRenderbuffer(ctx
, accRb
);
255 * Accum = ColorBuf * value
257 * Accum += ColorBuf * value
260 accum_or_load(struct gl_context
*ctx
, GLfloat value
,
261 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
264 struct gl_renderbuffer
*accRb
=
265 ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
;
266 struct gl_renderbuffer
*colorRb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
267 GLubyte
*accMap
, *colorMap
;
268 GLint accRowStride
, colorRowStride
;
269 GLbitfield mappingFlags
;
272 /* no read buffer - OK */
278 mappingFlags
= GL_MAP_WRITE_BIT
;
279 if (!load
) /* if we're accumulating */
280 mappingFlags
|= GL_MAP_READ_BIT
;
282 /* Map accum buffer */
283 ctx
->Driver
.MapRenderbuffer(ctx
, accRb
, xpos
, ypos
, width
, height
,
284 mappingFlags
, &accMap
, &accRowStride
);
286 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
290 /* Map color buffer */
291 ctx
->Driver
.MapRenderbuffer(ctx
, colorRb
, xpos
, ypos
, width
, height
,
293 &colorMap
, &colorRowStride
);
295 ctx
->Driver
.UnmapRenderbuffer(ctx
, accRb
);
296 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
300 if (accRb
->Format
== MESA_FORMAT_SIGNED_RGBA_16
) {
301 const GLfloat scale
= value
* 32767.0f
;
305 rgba
= (GLfloat (*)[4]) malloc(width
* 4 * sizeof(GLfloat
));
307 for (j
= 0; j
< height
; j
++) {
308 GLshort
*acc
= (GLshort
*) accMap
;
310 /* read colors from source color buffer */
311 _mesa_unpack_rgba_row(colorRb
->Format
, width
, colorMap
, rgba
);
314 for (i
= 0; i
< width
; i
++) {
315 acc
[i
* 4 + 0] = (GLshort
) (rgba
[i
][RCOMP
] * scale
);
316 acc
[i
* 4 + 1] = (GLshort
) (rgba
[i
][GCOMP
] * scale
);
317 acc
[i
* 4 + 2] = (GLshort
) (rgba
[i
][BCOMP
] * scale
);
318 acc
[i
* 4 + 3] = (GLshort
) (rgba
[i
][ACOMP
] * scale
);
323 for (i
= 0; i
< width
; i
++) {
324 acc
[i
* 4 + 0] += (GLshort
) (rgba
[i
][RCOMP
] * scale
);
325 acc
[i
* 4 + 1] += (GLshort
) (rgba
[i
][GCOMP
] * scale
);
326 acc
[i
* 4 + 2] += (GLshort
) (rgba
[i
][BCOMP
] * scale
);
327 acc
[i
* 4 + 3] += (GLshort
) (rgba
[i
][ACOMP
] * scale
);
331 colorMap
+= colorRowStride
;
332 accMap
+= accRowStride
;
338 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
342 /* other types someday? */
345 ctx
->Driver
.UnmapRenderbuffer(ctx
, accRb
);
346 ctx
->Driver
.UnmapRenderbuffer(ctx
, colorRb
);
351 * ColorBuffer = Accum * value
354 accum_return(struct gl_context
*ctx
, GLfloat value
,
355 GLint xpos
, GLint ypos
, GLint width
, GLint height
)
357 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
358 struct gl_renderbuffer
*accRb
= fb
->Attachment
[BUFFER_ACCUM
].Renderbuffer
;
359 GLubyte
*accMap
, *colorMap
;
360 GLint accRowStride
, colorRowStride
;
363 /* Map accum buffer */
364 ctx
->Driver
.MapRenderbuffer(ctx
, accRb
, xpos
, ypos
, width
, height
,
366 &accMap
, &accRowStride
);
368 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
372 /* Loop over destination buffers */
373 for (buffer
= 0; buffer
< fb
->_NumColorDrawBuffers
; buffer
++) {
374 struct gl_renderbuffer
*colorRb
= fb
->_ColorDrawBuffers
[buffer
];
375 const GLboolean masking
= (!ctx
->Color
.ColorMask
[buffer
][RCOMP
] ||
376 !ctx
->Color
.ColorMask
[buffer
][GCOMP
] ||
377 !ctx
->Color
.ColorMask
[buffer
][BCOMP
] ||
378 !ctx
->Color
.ColorMask
[buffer
][ACOMP
]);
379 GLbitfield mappingFlags
= GL_MAP_WRITE_BIT
;
382 mappingFlags
|= GL_MAP_READ_BIT
;
384 /* Map color buffer */
385 ctx
->Driver
.MapRenderbuffer(ctx
, colorRb
, xpos
, ypos
, width
, height
,
386 mappingFlags
, &colorMap
, &colorRowStride
);
388 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
392 if (accRb
->Format
== MESA_FORMAT_SIGNED_RGBA_16
) {
393 const GLfloat scale
= value
/ 32767.0f
;
395 GLfloat (*rgba
)[4], (*dest
)[4];
397 rgba
= (GLfloat (*)[4]) malloc(width
* 4 * sizeof(GLfloat
));
398 dest
= (GLfloat (*)[4]) malloc(width
* 4 * sizeof(GLfloat
));
401 for (j
= 0; j
< height
; j
++) {
402 GLshort
*acc
= (GLshort
*) accMap
;
404 for (i
= 0; i
< width
; i
++) {
405 rgba
[i
][0] = acc
[i
* 4 + 0] * scale
;
406 rgba
[i
][1] = acc
[i
* 4 + 1] * scale
;
407 rgba
[i
][2] = acc
[i
* 4 + 2] * scale
;
408 rgba
[i
][3] = acc
[i
* 4 + 3] * scale
;
413 /* get existing colors from dest buffer */
414 _mesa_unpack_rgba_row(colorRb
->Format
, width
, colorMap
, dest
);
416 /* use the dest colors where mask[channel] = 0 */
417 if (ctx
->Color
.ColorMask
[buffer
][RCOMP
] == 0) {
418 for (i
= 0; i
< width
; i
++)
419 rgba
[i
][RCOMP
] = dest
[i
][RCOMP
];
421 if (ctx
->Color
.ColorMask
[buffer
][GCOMP
] == 0) {
422 for (i
= 0; i
< width
; i
++)
423 rgba
[i
][GCOMP
] = dest
[i
][GCOMP
];
425 if (ctx
->Color
.ColorMask
[buffer
][BCOMP
] == 0) {
426 for (i
= 0; i
< width
; i
++)
427 rgba
[i
][BCOMP
] = dest
[i
][BCOMP
];
429 if (ctx
->Color
.ColorMask
[buffer
][ACOMP
] == 0) {
430 for (i
= 0; i
< width
; i
++)
431 rgba
[i
][ACOMP
] = dest
[i
][ACOMP
];
435 _mesa_pack_float_rgba_row(colorRb
->Format
, width
,
436 (const GLfloat (*)[4]) rgba
, colorMap
);
438 accMap
+= accRowStride
;
439 colorMap
+= colorRowStride
;
443 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
449 /* other types someday? */
452 ctx
->Driver
.UnmapRenderbuffer(ctx
, colorRb
);
455 ctx
->Driver
.UnmapRenderbuffer(ctx
, accRb
);
461 * Software fallback for glAccum. A hardware driver that supports
462 * signed 16-bit color channels could implement hardware accumulation
463 * operations, but no driver does so at this time.
466 _mesa_accum(struct gl_context
*ctx
, GLenum op
, GLfloat value
)
468 GLint xpos
, ypos
, width
, height
;
470 if (!ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
) {
471 _mesa_warning(ctx
, "Calling glAccum() without an accumulation buffer");
475 if (!_mesa_check_conditional_render(ctx
))
478 xpos
= ctx
->DrawBuffer
->_Xmin
;
479 ypos
= ctx
->DrawBuffer
->_Ymin
;
480 width
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
481 height
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
486 accum_scale_or_bias(ctx
, value
, xpos
, ypos
, width
, height
, GL_TRUE
);
491 accum_scale_or_bias(ctx
, value
, xpos
, ypos
, width
, height
, GL_FALSE
);
496 accum_or_load(ctx
, value
, xpos
, ypos
, width
, height
, GL_FALSE
);
500 accum_or_load(ctx
, value
, xpos
, ypos
, width
, height
, GL_TRUE
);
503 accum_return(ctx
, value
, xpos
, ypos
, width
, height
);
506 _mesa_problem(ctx
, "invalid mode in _mesa_accum()");