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"
35 #include "main/dispatch.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 /* bounds, with scissor */
77 x
= ctx
->DrawBuffer
->_Xmin
;
78 y
= ctx
->DrawBuffer
->_Ymin
;
79 width
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
80 height
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
82 ctx
->Driver
.MapRenderbuffer(ctx
, accRb
, x
, y
, width
, height
,
83 GL_MAP_WRITE_BIT
, &accMap
, &accRowStride
);
86 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
90 if (accRb
->Format
== MESA_FORMAT_RGBA_SNORM16
) {
91 const GLshort clearR
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[0]);
92 const GLshort clearG
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[1]);
93 const GLshort clearB
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[2]);
94 const GLshort clearA
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[3]);
97 for (j
= 0; j
< height
; j
++) {
98 GLshort
*row
= (GLshort
*) accMap
;
100 for (i
= 0; i
< width
; i
++) {
101 row
[i
* 4 + 0] = clearR
;
102 row
[i
* 4 + 1] = clearG
;
103 row
[i
* 4 + 2] = clearB
;
104 row
[i
* 4 + 3] = clearA
;
106 accMap
+= accRowStride
;
110 /* other types someday? */
111 _mesa_warning(ctx
, "unexpected accum buffer type");
114 ctx
->Driver
.UnmapRenderbuffer(ctx
, accRb
);
125 accum_scale_or_bias(struct gl_context
*ctx
, GLfloat value
,
126 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
129 struct gl_renderbuffer
*accRb
=
130 ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
;
136 ctx
->Driver
.MapRenderbuffer(ctx
, accRb
, xpos
, ypos
, width
, height
,
137 GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
,
138 &accMap
, &accRowStride
);
141 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
145 if (accRb
->Format
== MESA_FORMAT_RGBA_SNORM16
) {
146 const GLshort incr
= (GLshort
) (value
* 32767.0f
);
149 for (j
= 0; j
< height
; j
++) {
150 GLshort
*acc
= (GLshort
*) accMap
;
151 for (i
= 0; i
< 4 * width
; i
++) {
154 accMap
+= accRowStride
;
159 for (j
= 0; j
< height
; j
++) {
160 GLshort
*acc
= (GLshort
*) accMap
;
161 for (i
= 0; i
< 4 * width
; i
++) {
162 acc
[i
] = (GLshort
) (acc
[i
] * value
);
164 accMap
+= accRowStride
;
169 /* other types someday? */
172 ctx
->Driver
.UnmapRenderbuffer(ctx
, accRb
);
178 * Accum = ColorBuf * value
180 * Accum += ColorBuf * value
183 accum_or_load(struct gl_context
*ctx
, GLfloat value
,
184 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
187 struct gl_renderbuffer
*accRb
=
188 ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
;
189 struct gl_renderbuffer
*colorRb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
190 GLubyte
*accMap
, *colorMap
;
191 GLint accRowStride
, colorRowStride
;
192 GLbitfield mappingFlags
;
195 /* no read buffer - OK */
201 mappingFlags
= GL_MAP_WRITE_BIT
;
202 if (!load
) /* if we're accumulating */
203 mappingFlags
|= GL_MAP_READ_BIT
;
205 /* Map accum buffer */
206 ctx
->Driver
.MapRenderbuffer(ctx
, accRb
, xpos
, ypos
, width
, height
,
207 mappingFlags
, &accMap
, &accRowStride
);
209 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
213 /* Map color buffer */
214 ctx
->Driver
.MapRenderbuffer(ctx
, colorRb
, xpos
, ypos
, width
, height
,
216 &colorMap
, &colorRowStride
);
218 ctx
->Driver
.UnmapRenderbuffer(ctx
, accRb
);
219 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
223 if (accRb
->Format
== MESA_FORMAT_RGBA_SNORM16
) {
224 const GLfloat scale
= value
* 32767.0f
;
228 rgba
= malloc(width
* 4 * sizeof(GLfloat
));
230 for (j
= 0; j
< height
; j
++) {
231 GLshort
*acc
= (GLshort
*) accMap
;
233 /* read colors from source color buffer */
234 _mesa_unpack_rgba_row(colorRb
->Format
, width
, colorMap
, rgba
);
237 for (i
= 0; i
< width
; i
++) {
238 acc
[i
* 4 + 0] = (GLshort
) (rgba
[i
][RCOMP
] * scale
);
239 acc
[i
* 4 + 1] = (GLshort
) (rgba
[i
][GCOMP
] * scale
);
240 acc
[i
* 4 + 2] = (GLshort
) (rgba
[i
][BCOMP
] * scale
);
241 acc
[i
* 4 + 3] = (GLshort
) (rgba
[i
][ACOMP
] * scale
);
246 for (i
= 0; i
< width
; i
++) {
247 acc
[i
* 4 + 0] += (GLshort
) (rgba
[i
][RCOMP
] * scale
);
248 acc
[i
* 4 + 1] += (GLshort
) (rgba
[i
][GCOMP
] * scale
);
249 acc
[i
* 4 + 2] += (GLshort
) (rgba
[i
][BCOMP
] * scale
);
250 acc
[i
* 4 + 3] += (GLshort
) (rgba
[i
][ACOMP
] * scale
);
254 colorMap
+= colorRowStride
;
255 accMap
+= accRowStride
;
261 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
265 /* other types someday? */
268 ctx
->Driver
.UnmapRenderbuffer(ctx
, accRb
);
269 ctx
->Driver
.UnmapRenderbuffer(ctx
, colorRb
);
274 * ColorBuffer = Accum * value
277 accum_return(struct gl_context
*ctx
, GLfloat value
,
278 GLint xpos
, GLint ypos
, GLint width
, GLint height
)
280 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
281 struct gl_renderbuffer
*accRb
= fb
->Attachment
[BUFFER_ACCUM
].Renderbuffer
;
282 GLubyte
*accMap
, *colorMap
;
283 GLint accRowStride
, colorRowStride
;
286 /* Map accum buffer */
287 ctx
->Driver
.MapRenderbuffer(ctx
, accRb
, xpos
, ypos
, width
, height
,
289 &accMap
, &accRowStride
);
291 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
295 /* Loop over destination buffers */
296 for (buffer
= 0; buffer
< fb
->_NumColorDrawBuffers
; buffer
++) {
297 struct gl_renderbuffer
*colorRb
= fb
->_ColorDrawBuffers
[buffer
];
298 const GLboolean masking
= (!ctx
->Color
.ColorMask
[buffer
][RCOMP
] ||
299 !ctx
->Color
.ColorMask
[buffer
][GCOMP
] ||
300 !ctx
->Color
.ColorMask
[buffer
][BCOMP
] ||
301 !ctx
->Color
.ColorMask
[buffer
][ACOMP
]);
302 GLbitfield mappingFlags
= GL_MAP_WRITE_BIT
;
305 mappingFlags
|= GL_MAP_READ_BIT
;
307 /* Map color buffer */
308 ctx
->Driver
.MapRenderbuffer(ctx
, colorRb
, xpos
, ypos
, width
, height
,
309 mappingFlags
, &colorMap
, &colorRowStride
);
311 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
315 if (accRb
->Format
== MESA_FORMAT_RGBA_SNORM16
) {
316 const GLfloat scale
= value
/ 32767.0f
;
318 GLfloat (*rgba
)[4], (*dest
)[4];
320 rgba
= malloc(width
* 4 * sizeof(GLfloat
));
321 dest
= malloc(width
* 4 * sizeof(GLfloat
));
324 for (j
= 0; j
< height
; j
++) {
325 GLshort
*acc
= (GLshort
*) accMap
;
327 for (i
= 0; i
< width
; i
++) {
328 rgba
[i
][0] = acc
[i
* 4 + 0] * scale
;
329 rgba
[i
][1] = acc
[i
* 4 + 1] * scale
;
330 rgba
[i
][2] = acc
[i
* 4 + 2] * scale
;
331 rgba
[i
][3] = acc
[i
* 4 + 3] * scale
;
336 /* get existing colors from dest buffer */
337 _mesa_unpack_rgba_row(colorRb
->Format
, width
, colorMap
, dest
);
339 /* use the dest colors where mask[channel] = 0 */
340 if (ctx
->Color
.ColorMask
[buffer
][RCOMP
] == 0) {
341 for (i
= 0; i
< width
; i
++)
342 rgba
[i
][RCOMP
] = dest
[i
][RCOMP
];
344 if (ctx
->Color
.ColorMask
[buffer
][GCOMP
] == 0) {
345 for (i
= 0; i
< width
; i
++)
346 rgba
[i
][GCOMP
] = dest
[i
][GCOMP
];
348 if (ctx
->Color
.ColorMask
[buffer
][BCOMP
] == 0) {
349 for (i
= 0; i
< width
; i
++)
350 rgba
[i
][BCOMP
] = dest
[i
][BCOMP
];
352 if (ctx
->Color
.ColorMask
[buffer
][ACOMP
] == 0) {
353 for (i
= 0; i
< width
; i
++)
354 rgba
[i
][ACOMP
] = dest
[i
][ACOMP
];
358 _mesa_pack_float_rgba_row(colorRb
->Format
, width
,
359 (const GLfloat (*)[4]) rgba
, colorMap
);
361 accMap
+= accRowStride
;
362 colorMap
+= colorRowStride
;
366 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glAccum");
372 /* other types someday? */
375 ctx
->Driver
.UnmapRenderbuffer(ctx
, colorRb
);
378 ctx
->Driver
.UnmapRenderbuffer(ctx
, accRb
);
384 * Software fallback for glAccum. A hardware driver that supports
385 * signed 16-bit color channels could implement hardware accumulation
386 * operations, but no driver does so at this time.
389 accum(struct gl_context
*ctx
, GLenum op
, GLfloat value
)
391 GLint xpos
, ypos
, width
, height
;
393 if (!ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
) {
394 _mesa_warning(ctx
, "Calling glAccum() without an accumulation buffer");
398 if (!_mesa_check_conditional_render(ctx
))
401 xpos
= ctx
->DrawBuffer
->_Xmin
;
402 ypos
= ctx
->DrawBuffer
->_Ymin
;
403 width
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
404 height
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
409 accum_scale_or_bias(ctx
, value
, xpos
, ypos
, width
, height
, GL_TRUE
);
414 accum_scale_or_bias(ctx
, value
, xpos
, ypos
, width
, height
, GL_FALSE
);
419 accum_or_load(ctx
, value
, xpos
, ypos
, width
, height
, GL_FALSE
);
423 accum_or_load(ctx
, value
, xpos
, ypos
, width
, height
, GL_TRUE
);
426 accum_return(ctx
, value
, xpos
, ypos
, width
, height
);
429 unreachable("invalid mode in _mesa_Accum()");
436 _mesa_init_accum( struct gl_context
*ctx
)
438 /* Accumulate buffer group */
439 ASSIGN_4V( ctx
->Accum
.ClearColor
, 0.0, 0.0, 0.0, 0.0 );
444 _mesa_Accum( GLenum op
, GLfloat value
)
446 GET_CURRENT_CONTEXT(ctx
);
447 FLUSH_VERTICES(ctx
, 0);
458 _mesa_error(ctx
, GL_INVALID_ENUM
, "glAccum(op)");
462 if (ctx
->DrawBuffer
->Visual
.haveAccumBuffer
== 0) {
463 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glAccum(no accum buffer)");
467 if (ctx
->DrawBuffer
!= ctx
->ReadBuffer
) {
468 /* See GLX_SGI_make_current_read or WGL_ARB_make_current_read,
469 * or GL_EXT_framebuffer_blit.
471 _mesa_error(ctx
, GL_INVALID_OPERATION
,
472 "glAccum(different read/draw buffers)");
477 _mesa_update_state(ctx
);
479 if (ctx
->DrawBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
480 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
481 "glAccum(incomplete framebuffer)");
485 if (ctx
->RasterDiscard
)
488 if (ctx
->RenderMode
== GL_RENDER
) {
489 accum(ctx
, op
, value
);