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.
28 #include "format_unpack.h"
29 #include "format_pack.h"
32 #include "mfeatures.h"
35 #include "main/dispatch.h"
42 _mesa_ClearAccum( GLfloat red
, GLfloat green
, GLfloat blue
, GLfloat alpha
)
45 GET_CURRENT_CONTEXT(ctx
);
46 ASSERT_OUTSIDE_BEGIN_END(ctx
);
48 tmp
[0] = CLAMP( red
, -1.0F
, 1.0F
);
49 tmp
[1] = CLAMP( green
, -1.0F
, 1.0F
);
50 tmp
[2] = CLAMP( blue
, -1.0F
, 1.0F
);
51 tmp
[3] = CLAMP( alpha
, -1.0F
, 1.0F
);
53 if (TEST_EQ_4V(tmp
, ctx
->Accum
.ClearColor
))
56 COPY_4FV( ctx
->Accum
.ClearColor
, tmp
);
60 static void GLAPIENTRY
61 _mesa_Accum( GLenum op
, GLfloat value
)
63 GET_CURRENT_CONTEXT(ctx
);
64 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
75 _mesa_error(ctx
, GL_INVALID_ENUM
, "glAccum(op)");
79 if (ctx
->DrawBuffer
->Visual
.haveAccumBuffer
== 0) {
80 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glAccum(no accum buffer)");
84 if (ctx
->DrawBuffer
!= ctx
->ReadBuffer
) {
85 /* See GLX_SGI_make_current_read or WGL_ARB_make_current_read,
86 * or GL_EXT_framebuffer_blit.
88 _mesa_error(ctx
, GL_INVALID_OPERATION
,
89 "glAccum(different read/draw buffers)");
94 _mesa_update_state(ctx
);
96 if (ctx
->DrawBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
97 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
98 "glAccum(incomplete framebuffer)");
102 if (ctx
->RasterDiscard
)
105 if (ctx
->RenderMode
== GL_RENDER
) {
106 _mesa_accum(ctx
, op
, value
);
112 _mesa_init_accum_dispatch(struct _glapi_table
*disp
)
114 SET_Accum(disp
, _mesa_Accum
);
115 SET_ClearAccum(disp
, _mesa_ClearAccum
);
120 * Clear the accumulation buffer by mapping the renderbuffer and
121 * writing the clear color to it. Called by the driver's implementation
122 * of the glClear function.
125 _mesa_clear_accum_buffer(struct gl_context
*ctx
)
127 GLuint x
, y
, width
, height
;
130 struct gl_renderbuffer
*accRb
;
132 if (!ctx
->DrawBuffer
)
135 accRb
= ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
;
137 return; /* missing accum buffer, not an error */
139 /* bounds, with scissor */
140 x
= ctx
->DrawBuffer
->_Xmin
;
141 y
= ctx
->DrawBuffer
->_Ymin
;
142 width
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
143 height
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
145 ctx
->Driver
.MapRenderbuffer(ctx
, accRb
, x
, y
, width
, height
,
146 GL_MAP_WRITE_BIT
, &accMap
, &accRowStride
);
149 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
153 if (accRb
->Format
== MESA_FORMAT_SIGNED_RGBA_16
) {
154 const GLshort clearR
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[0]);
155 const GLshort clearG
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[1]);
156 const GLshort clearB
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[2]);
157 const GLshort clearA
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[3]);
160 for (j
= 0; j
< height
; j
++) {
161 GLshort
*row
= (GLshort
*) accMap
;
163 for (i
= 0; i
< width
; i
++) {
164 row
[i
* 4 + 0] = clearR
;
165 row
[i
* 4 + 1] = clearG
;
166 row
[i
* 4 + 2] = clearB
;
167 row
[i
* 4 + 3] = clearA
;
169 accMap
+= accRowStride
;
173 /* other types someday? */
174 _mesa_warning(ctx
, "unexpected accum buffer type");
177 ctx
->Driver
.UnmapRenderbuffer(ctx
, accRb
);
188 accum_scale_or_bias(struct gl_context
*ctx
, GLfloat value
,
189 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
192 struct gl_renderbuffer
*accRb
=
193 ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
;
199 ctx
->Driver
.MapRenderbuffer(ctx
, accRb
, xpos
, ypos
, width
, height
,
200 GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
,
201 &accMap
, &accRowStride
);
204 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
208 if (accRb
->Format
== MESA_FORMAT_SIGNED_RGBA_16
) {
209 const GLshort incr
= (GLshort
) (value
* 32767.0f
);
212 for (j
= 0; j
< height
; j
++) {
213 GLshort
*acc
= (GLshort
*) accMap
;
214 for (i
= 0; i
< 4 * width
; i
++) {
217 accMap
+= accRowStride
;
222 for (j
= 0; j
< height
; j
++) {
223 GLshort
*acc
= (GLshort
*) accMap
;
224 for (i
= 0; i
< 4 * width
; i
++) {
225 acc
[i
] = (GLshort
) (acc
[i
] * value
);
227 accMap
+= accRowStride
;
232 /* other types someday? */
235 ctx
->Driver
.UnmapRenderbuffer(ctx
, accRb
);
241 * Accum = ColorBuf * value
243 * Accum += ColorBuf * value
246 accum_or_load(struct gl_context
*ctx
, GLfloat value
,
247 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
250 struct gl_renderbuffer
*accRb
=
251 ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
;
252 struct gl_renderbuffer
*colorRb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
253 GLubyte
*accMap
, *colorMap
;
254 GLint accRowStride
, colorRowStride
;
255 GLbitfield mappingFlags
;
258 /* no read buffer - OK */
264 mappingFlags
= GL_MAP_WRITE_BIT
;
265 if (!load
) /* if we're accumulating */
266 mappingFlags
|= GL_MAP_READ_BIT
;
268 /* Map accum buffer */
269 ctx
->Driver
.MapRenderbuffer(ctx
, accRb
, xpos
, ypos
, width
, height
,
270 mappingFlags
, &accMap
, &accRowStride
);
272 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
276 /* Map color buffer */
277 ctx
->Driver
.MapRenderbuffer(ctx
, colorRb
, xpos
, ypos
, width
, height
,
279 &colorMap
, &colorRowStride
);
281 ctx
->Driver
.UnmapRenderbuffer(ctx
, accRb
);
282 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
286 if (accRb
->Format
== MESA_FORMAT_SIGNED_RGBA_16
) {
287 const GLfloat scale
= value
* 32767.0f
;
291 rgba
= (GLfloat (*)[4]) malloc(width
* 4 * sizeof(GLfloat
));
293 for (j
= 0; j
< height
; j
++) {
294 GLshort
*acc
= (GLshort
*) accMap
;
296 /* read colors from source color buffer */
297 _mesa_unpack_rgba_row(colorRb
->Format
, width
, colorMap
, rgba
);
300 for (i
= 0; i
< width
; i
++) {
301 acc
[i
* 4 + 0] = (GLshort
) (rgba
[i
][RCOMP
] * scale
);
302 acc
[i
* 4 + 1] = (GLshort
) (rgba
[i
][GCOMP
] * scale
);
303 acc
[i
* 4 + 2] = (GLshort
) (rgba
[i
][BCOMP
] * scale
);
304 acc
[i
* 4 + 3] = (GLshort
) (rgba
[i
][ACOMP
] * scale
);
309 for (i
= 0; i
< width
; i
++) {
310 acc
[i
* 4 + 0] += (GLshort
) (rgba
[i
][RCOMP
] * scale
);
311 acc
[i
* 4 + 1] += (GLshort
) (rgba
[i
][GCOMP
] * scale
);
312 acc
[i
* 4 + 2] += (GLshort
) (rgba
[i
][BCOMP
] * scale
);
313 acc
[i
* 4 + 3] += (GLshort
) (rgba
[i
][ACOMP
] * scale
);
317 colorMap
+= colorRowStride
;
318 accMap
+= accRowStride
;
324 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
328 /* other types someday? */
331 ctx
->Driver
.UnmapRenderbuffer(ctx
, accRb
);
332 ctx
->Driver
.UnmapRenderbuffer(ctx
, colorRb
);
337 * ColorBuffer = Accum * value
340 accum_return(struct gl_context
*ctx
, GLfloat value
,
341 GLint xpos
, GLint ypos
, GLint width
, GLint height
)
343 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
344 struct gl_renderbuffer
*accRb
= fb
->Attachment
[BUFFER_ACCUM
].Renderbuffer
;
345 GLubyte
*accMap
, *colorMap
;
346 GLint accRowStride
, colorRowStride
;
349 /* Map accum buffer */
350 ctx
->Driver
.MapRenderbuffer(ctx
, accRb
, xpos
, ypos
, width
, height
,
352 &accMap
, &accRowStride
);
354 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
358 /* Loop over destination buffers */
359 for (buffer
= 0; buffer
< fb
->_NumColorDrawBuffers
; buffer
++) {
360 struct gl_renderbuffer
*colorRb
= fb
->_ColorDrawBuffers
[buffer
];
361 const GLboolean masking
= (!ctx
->Color
.ColorMask
[buffer
][RCOMP
] ||
362 !ctx
->Color
.ColorMask
[buffer
][GCOMP
] ||
363 !ctx
->Color
.ColorMask
[buffer
][BCOMP
] ||
364 !ctx
->Color
.ColorMask
[buffer
][ACOMP
]);
365 GLbitfield mappingFlags
= GL_MAP_WRITE_BIT
;
368 mappingFlags
|= GL_MAP_READ_BIT
;
370 /* Map color buffer */
371 ctx
->Driver
.MapRenderbuffer(ctx
, colorRb
, xpos
, ypos
, width
, height
,
372 mappingFlags
, &colorMap
, &colorRowStride
);
374 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
378 if (accRb
->Format
== MESA_FORMAT_SIGNED_RGBA_16
) {
379 const GLfloat scale
= value
/ 32767.0f
;
381 GLfloat (*rgba
)[4], (*dest
)[4];
383 rgba
= (GLfloat (*)[4]) malloc(width
* 4 * sizeof(GLfloat
));
384 dest
= (GLfloat (*)[4]) malloc(width
* 4 * sizeof(GLfloat
));
387 for (j
= 0; j
< height
; j
++) {
388 GLshort
*acc
= (GLshort
*) accMap
;
390 for (i
= 0; i
< width
; i
++) {
391 rgba
[i
][0] = acc
[i
* 4 + 0] * scale
;
392 rgba
[i
][1] = acc
[i
* 4 + 1] * scale
;
393 rgba
[i
][2] = acc
[i
* 4 + 2] * scale
;
394 rgba
[i
][3] = acc
[i
* 4 + 3] * scale
;
399 /* get existing colors from dest buffer */
400 _mesa_unpack_rgba_row(colorRb
->Format
, width
, colorMap
, dest
);
402 /* use the dest colors where mask[channel] = 0 */
403 if (ctx
->Color
.ColorMask
[buffer
][RCOMP
] == 0) {
404 for (i
= 0; i
< width
; i
++)
405 rgba
[i
][RCOMP
] = dest
[i
][RCOMP
];
407 if (ctx
->Color
.ColorMask
[buffer
][GCOMP
] == 0) {
408 for (i
= 0; i
< width
; i
++)
409 rgba
[i
][GCOMP
] = dest
[i
][GCOMP
];
411 if (ctx
->Color
.ColorMask
[buffer
][BCOMP
] == 0) {
412 for (i
= 0; i
< width
; i
++)
413 rgba
[i
][BCOMP
] = dest
[i
][BCOMP
];
415 if (ctx
->Color
.ColorMask
[buffer
][ACOMP
] == 0) {
416 for (i
= 0; i
< width
; i
++)
417 rgba
[i
][ACOMP
] = dest
[i
][ACOMP
];
421 _mesa_pack_float_rgba_row(colorRb
->Format
, width
,
422 (const GLfloat (*)[4]) rgba
, colorMap
);
424 accMap
+= accRowStride
;
425 colorMap
+= colorRowStride
;
429 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
435 /* other types someday? */
438 ctx
->Driver
.UnmapRenderbuffer(ctx
, colorRb
);
441 ctx
->Driver
.UnmapRenderbuffer(ctx
, accRb
);
447 * Software fallback for glAccum. A hardware driver that supports
448 * signed 16-bit color channels could implement hardware accumulation
449 * operations, but no driver does so at this time.
452 _mesa_accum(struct gl_context
*ctx
, GLenum op
, GLfloat value
)
454 GLint xpos
, ypos
, width
, height
;
456 if (!ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
) {
457 _mesa_warning(ctx
, "Calling glAccum() without an accumulation buffer");
461 xpos
= ctx
->DrawBuffer
->_Xmin
;
462 ypos
= ctx
->DrawBuffer
->_Ymin
;
463 width
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
464 height
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
469 accum_scale_or_bias(ctx
, value
, xpos
, ypos
, width
, height
, GL_TRUE
);
474 accum_scale_or_bias(ctx
, value
, xpos
, ypos
, width
, height
, GL_FALSE
);
479 accum_or_load(ctx
, value
, xpos
, ypos
, width
, height
, GL_FALSE
);
483 accum_or_load(ctx
, value
, xpos
, ypos
, width
, height
, GL_TRUE
);
486 accum_return(ctx
, value
, xpos
, ypos
, width
, height
);
489 _mesa_problem(ctx
, "invalid mode in _mesa_accum()");
495 #endif /* FEATURE_accum */
499 _mesa_init_accum( struct gl_context
*ctx
)
501 /* Accumulate buffer group */
502 ASSIGN_4V( ctx
->Accum
.ClearColor
, 0.0, 0.0, 0.0, 0.0 );