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
);
58 static void GLAPIENTRY
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 _mesa_init_accum_dispatch(struct _glapi_table
*disp
)
112 SET_Accum(disp
, _mesa_Accum
);
113 SET_ClearAccum(disp
, _mesa_ClearAccum
);
118 * Clear the accumulation buffer by mapping the renderbuffer and
119 * writing the clear color to it. Called by the driver's implementation
120 * of the glClear function.
123 _mesa_clear_accum_buffer(struct gl_context
*ctx
)
125 GLuint x
, y
, width
, height
;
128 struct gl_renderbuffer
*accRb
;
130 if (!ctx
->DrawBuffer
)
133 accRb
= ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
;
135 return; /* missing accum buffer, not an error */
137 /* bounds, with scissor */
138 x
= ctx
->DrawBuffer
->_Xmin
;
139 y
= ctx
->DrawBuffer
->_Ymin
;
140 width
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
141 height
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
143 ctx
->Driver
.MapRenderbuffer(ctx
, accRb
, x
, y
, width
, height
,
144 GL_MAP_WRITE_BIT
, &accMap
, &accRowStride
);
147 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
151 if (accRb
->Format
== MESA_FORMAT_SIGNED_RGBA_16
) {
152 const GLshort clearR
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[0]);
153 const GLshort clearG
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[1]);
154 const GLshort clearB
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[2]);
155 const GLshort clearA
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[3]);
158 for (j
= 0; j
< height
; j
++) {
159 GLshort
*row
= (GLshort
*) accMap
;
161 for (i
= 0; i
< width
; i
++) {
162 row
[i
* 4 + 0] = clearR
;
163 row
[i
* 4 + 1] = clearG
;
164 row
[i
* 4 + 2] = clearB
;
165 row
[i
* 4 + 3] = clearA
;
167 accMap
+= accRowStride
;
171 /* other types someday? */
172 _mesa_warning(ctx
, "unexpected accum buffer type");
175 ctx
->Driver
.UnmapRenderbuffer(ctx
, accRb
);
186 accum_scale_or_bias(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
;
197 ctx
->Driver
.MapRenderbuffer(ctx
, accRb
, xpos
, ypos
, width
, height
,
198 GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
,
199 &accMap
, &accRowStride
);
202 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
206 if (accRb
->Format
== MESA_FORMAT_SIGNED_RGBA_16
) {
207 const GLshort incr
= (GLshort
) (value
* 32767.0f
);
210 for (j
= 0; j
< height
; j
++) {
211 GLshort
*acc
= (GLshort
*) accMap
;
212 for (i
= 0; i
< 4 * width
; i
++) {
215 accMap
+= accRowStride
;
220 for (j
= 0; j
< height
; j
++) {
221 GLshort
*acc
= (GLshort
*) accMap
;
222 for (i
= 0; i
< 4 * width
; i
++) {
223 acc
[i
] = (GLshort
) (acc
[i
] * value
);
225 accMap
+= accRowStride
;
230 /* other types someday? */
233 ctx
->Driver
.UnmapRenderbuffer(ctx
, accRb
);
239 * Accum = ColorBuf * value
241 * Accum += ColorBuf * value
244 accum_or_load(struct gl_context
*ctx
, GLfloat value
,
245 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
248 struct gl_renderbuffer
*accRb
=
249 ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
;
250 struct gl_renderbuffer
*colorRb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
251 GLubyte
*accMap
, *colorMap
;
252 GLint accRowStride
, colorRowStride
;
253 GLbitfield mappingFlags
;
256 /* no read buffer - OK */
262 mappingFlags
= GL_MAP_WRITE_BIT
;
263 if (!load
) /* if we're accumulating */
264 mappingFlags
|= GL_MAP_READ_BIT
;
266 /* Map accum buffer */
267 ctx
->Driver
.MapRenderbuffer(ctx
, accRb
, xpos
, ypos
, width
, height
,
268 mappingFlags
, &accMap
, &accRowStride
);
270 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
274 /* Map color buffer */
275 ctx
->Driver
.MapRenderbuffer(ctx
, colorRb
, xpos
, ypos
, width
, height
,
277 &colorMap
, &colorRowStride
);
279 ctx
->Driver
.UnmapRenderbuffer(ctx
, accRb
);
280 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
284 if (accRb
->Format
== MESA_FORMAT_SIGNED_RGBA_16
) {
285 const GLfloat scale
= value
* 32767.0f
;
289 rgba
= malloc(width
* 4 * sizeof(GLfloat
));
291 for (j
= 0; j
< height
; j
++) {
292 GLshort
*acc
= (GLshort
*) accMap
;
294 /* read colors from source color buffer */
295 _mesa_unpack_rgba_row(colorRb
->Format
, width
, colorMap
, rgba
);
298 for (i
= 0; i
< width
; i
++) {
299 acc
[i
* 4 + 0] = (GLshort
) (rgba
[i
][RCOMP
] * scale
);
300 acc
[i
* 4 + 1] = (GLshort
) (rgba
[i
][GCOMP
] * scale
);
301 acc
[i
* 4 + 2] = (GLshort
) (rgba
[i
][BCOMP
] * scale
);
302 acc
[i
* 4 + 3] = (GLshort
) (rgba
[i
][ACOMP
] * scale
);
307 for (i
= 0; i
< width
; i
++) {
308 acc
[i
* 4 + 0] += (GLshort
) (rgba
[i
][RCOMP
] * scale
);
309 acc
[i
* 4 + 1] += (GLshort
) (rgba
[i
][GCOMP
] * scale
);
310 acc
[i
* 4 + 2] += (GLshort
) (rgba
[i
][BCOMP
] * scale
);
311 acc
[i
* 4 + 3] += (GLshort
) (rgba
[i
][ACOMP
] * scale
);
315 colorMap
+= colorRowStride
;
316 accMap
+= accRowStride
;
322 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
326 /* other types someday? */
329 ctx
->Driver
.UnmapRenderbuffer(ctx
, accRb
);
330 ctx
->Driver
.UnmapRenderbuffer(ctx
, colorRb
);
335 * ColorBuffer = Accum * value
338 accum_return(struct gl_context
*ctx
, GLfloat value
,
339 GLint xpos
, GLint ypos
, GLint width
, GLint height
)
341 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
342 struct gl_renderbuffer
*accRb
= fb
->Attachment
[BUFFER_ACCUM
].Renderbuffer
;
343 GLubyte
*accMap
, *colorMap
;
344 GLint accRowStride
, colorRowStride
;
347 /* Map accum buffer */
348 ctx
->Driver
.MapRenderbuffer(ctx
, accRb
, xpos
, ypos
, width
, height
,
350 &accMap
, &accRowStride
);
352 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
356 /* Loop over destination buffers */
357 for (buffer
= 0; buffer
< fb
->_NumColorDrawBuffers
; buffer
++) {
358 struct gl_renderbuffer
*colorRb
= fb
->_ColorDrawBuffers
[buffer
];
359 const GLboolean masking
= (!ctx
->Color
.ColorMask
[buffer
][RCOMP
] ||
360 !ctx
->Color
.ColorMask
[buffer
][GCOMP
] ||
361 !ctx
->Color
.ColorMask
[buffer
][BCOMP
] ||
362 !ctx
->Color
.ColorMask
[buffer
][ACOMP
]);
363 GLbitfield mappingFlags
= GL_MAP_WRITE_BIT
;
366 mappingFlags
|= GL_MAP_READ_BIT
;
368 /* Map color buffer */
369 ctx
->Driver
.MapRenderbuffer(ctx
, colorRb
, xpos
, ypos
, width
, height
,
370 mappingFlags
, &colorMap
, &colorRowStride
);
372 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
376 if (accRb
->Format
== MESA_FORMAT_SIGNED_RGBA_16
) {
377 const GLfloat scale
= value
/ 32767.0f
;
379 GLfloat (*rgba
)[4], (*dest
)[4];
381 rgba
= malloc(width
* 4 * sizeof(GLfloat
));
382 dest
= malloc(width
* 4 * sizeof(GLfloat
));
385 for (j
= 0; j
< height
; j
++) {
386 GLshort
*acc
= (GLshort
*) accMap
;
388 for (i
= 0; i
< width
; i
++) {
389 rgba
[i
][0] = acc
[i
* 4 + 0] * scale
;
390 rgba
[i
][1] = acc
[i
* 4 + 1] * scale
;
391 rgba
[i
][2] = acc
[i
* 4 + 2] * scale
;
392 rgba
[i
][3] = acc
[i
* 4 + 3] * scale
;
397 /* get existing colors from dest buffer */
398 _mesa_unpack_rgba_row(colorRb
->Format
, width
, colorMap
, dest
);
400 /* use the dest colors where mask[channel] = 0 */
401 if (ctx
->Color
.ColorMask
[buffer
][RCOMP
] == 0) {
402 for (i
= 0; i
< width
; i
++)
403 rgba
[i
][RCOMP
] = dest
[i
][RCOMP
];
405 if (ctx
->Color
.ColorMask
[buffer
][GCOMP
] == 0) {
406 for (i
= 0; i
< width
; i
++)
407 rgba
[i
][GCOMP
] = dest
[i
][GCOMP
];
409 if (ctx
->Color
.ColorMask
[buffer
][BCOMP
] == 0) {
410 for (i
= 0; i
< width
; i
++)
411 rgba
[i
][BCOMP
] = dest
[i
][BCOMP
];
413 if (ctx
->Color
.ColorMask
[buffer
][ACOMP
] == 0) {
414 for (i
= 0; i
< width
; i
++)
415 rgba
[i
][ACOMP
] = dest
[i
][ACOMP
];
419 _mesa_pack_float_rgba_row(colorRb
->Format
, width
,
420 (const GLfloat (*)[4]) rgba
, colorMap
);
422 accMap
+= accRowStride
;
423 colorMap
+= colorRowStride
;
427 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
433 /* other types someday? */
436 ctx
->Driver
.UnmapRenderbuffer(ctx
, colorRb
);
439 ctx
->Driver
.UnmapRenderbuffer(ctx
, accRb
);
445 * Software fallback for glAccum. A hardware driver that supports
446 * signed 16-bit color channels could implement hardware accumulation
447 * operations, but no driver does so at this time.
450 _mesa_accum(struct gl_context
*ctx
, GLenum op
, GLfloat value
)
452 GLint xpos
, ypos
, width
, height
;
454 if (!ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
) {
455 _mesa_warning(ctx
, "Calling glAccum() without an accumulation buffer");
459 if (!_mesa_check_conditional_render(ctx
))
462 xpos
= ctx
->DrawBuffer
->_Xmin
;
463 ypos
= ctx
->DrawBuffer
->_Ymin
;
464 width
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
465 height
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
470 accum_scale_or_bias(ctx
, value
, xpos
, ypos
, width
, height
, GL_TRUE
);
475 accum_scale_or_bias(ctx
, value
, xpos
, ypos
, width
, height
, GL_FALSE
);
480 accum_or_load(ctx
, value
, xpos
, ypos
, width
, height
, GL_FALSE
);
484 accum_or_load(ctx
, value
, xpos
, ypos
, width
, height
, GL_TRUE
);
487 accum_return(ctx
, value
, xpos
, ypos
, width
, height
);
490 _mesa_problem(ctx
, "invalid mode in _mesa_accum()");
497 _mesa_init_accum( struct gl_context
*ctx
)
499 /* Accumulate buffer group */
500 ASSIGN_4V( ctx
->Accum
.ClearColor
, 0.0, 0.0, 0.0, 0.0 );