2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 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.
25 #include "main/glheader.h"
26 #include "main/accum.h"
27 #include "main/condrender.h"
28 #include "main/format_pack.h"
29 #include "main/macros.h"
30 #include "main/imports.h"
31 #include "main/mtypes.h"
33 #include "s_context.h"
35 #include "s_stencil.h"
39 * Convert a boolean color mask to a packed color where each channel of
40 * the packed value at dst will be 0 or ~0 depending on the colorMask.
43 _pack_colormask(mesa_format format
, const uint8_t colorMask
[4], void *dst
)
47 switch (_mesa_get_format_datatype(format
)) {
48 case GL_UNSIGNED_NORMALIZED
:
49 /* simple: 1.0 will convert to ~0 in the right bit positions */
50 maskColor
[0] = colorMask
[0] ? 1.0f
: 0.0f
;
51 maskColor
[1] = colorMask
[1] ? 1.0f
: 0.0f
;
52 maskColor
[2] = colorMask
[2] ? 1.0f
: 0.0f
;
53 maskColor
[3] = colorMask
[3] ? 1.0f
: 0.0f
;
54 _mesa_pack_float_rgba_row(format
, 1,
55 (const float (*)[4]) maskColor
, dst
);
57 case GL_SIGNED_NORMALIZED
:
59 /* These formats are harder because it's hard to know the floating
60 * point values that will convert to ~0 for each color channel's bits.
61 * This solution just generates a non-zero value for each color channel
62 * then fixes up the non-zero values to be ~0.
63 * Note: we'll need to add special case code if we ever have to deal
64 * with formats with unequal color channel sizes, like R11_G11_B10.
65 * We issue a warning below for channel sizes other than 8,16,32.
68 uint32_t bits
= _mesa_get_format_max_bits(format
); /* bits per chan */
69 uint32_t bytes
= _mesa_get_format_bytes(format
);
72 /* this should put non-zero values into the channels of dst */
73 maskColor
[0] = colorMask
[0] ? -1.0f
: 0.0f
;
74 maskColor
[1] = colorMask
[1] ? -1.0f
: 0.0f
;
75 maskColor
[2] = colorMask
[2] ? -1.0f
: 0.0f
;
76 maskColor
[3] = colorMask
[3] ? -1.0f
: 0.0f
;
77 _mesa_pack_float_rgba_row(format
, 1,
78 (const float (*)[4]) maskColor
, dst
);
80 /* fix-up the dst channels by converting non-zero values to ~0 */
82 uint8_t *d
= (uint8_t *) dst
;
83 for (i
= 0; i
< bytes
; i
++) {
84 d
[i
] = d
[i
] ? 0xff : 0x0;
87 else if (bits
== 16) {
88 uint16_t *d
= (uint16_t *) dst
;
89 for (i
= 0; i
< bytes
/ 2; i
++) {
90 d
[i
] = d
[i
] ? 0xffff : 0x0;
93 else if (bits
== 32) {
94 uint32_t *d
= (uint32_t *) dst
;
95 for (i
= 0; i
< bytes
/ 4; i
++) {
96 d
[i
] = d
[i
] ? 0xffffffffU
: 0x0;
100 unreachable("unexpected size in _mesa_pack_colormask()");
105 unreachable("unexpected format data type in gen_color_mask()");
110 * Clear an rgba color buffer with masking if needed.
113 clear_rgba_buffer(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
114 const GLubyte colorMask
[4])
116 const GLint x
= ctx
->DrawBuffer
->_Xmin
;
117 const GLint y
= ctx
->DrawBuffer
->_Ymin
;
118 const GLint height
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
119 const GLint width
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
120 const GLuint pixelSize
= _mesa_get_format_bytes(rb
->Format
);
121 const GLboolean doMasking
= (colorMask
[0] == 0 ||
125 const GLfloat (*clearColor
)[4] =
126 (const GLfloat (*)[4]) ctx
->Color
.ClearColor
.f
;
127 GLbitfield mapMode
= GL_MAP_WRITE_BIT
;
133 /* we'll need to read buffer values too */
134 mapMode
|= GL_MAP_READ_BIT
;
137 /* map dest buffer */
138 ctx
->Driver
.MapRenderbuffer(ctx
, rb
, x
, y
, width
, height
,
139 mapMode
, &map
, &rowStride
,
140 ctx
->DrawBuffer
->FlipY
);
142 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glClear(color)");
146 /* for 1, 2, 4-byte clearing */
147 #define SIMPLE_TYPE_CLEAR(TYPE) \
149 TYPE pixel, pixelMask; \
150 _mesa_pack_float_rgba_row(rb->Format, 1, clearColor, &pixel); \
152 _pack_colormask(rb->Format, colorMask, &pixelMask); \
153 pixel &= pixelMask; \
154 pixelMask = ~pixelMask; \
156 for (i = 0; i < height; i++) { \
157 TYPE *row = (TYPE *) map; \
159 for (j = 0; j < width; j++) { \
160 row[j] = (row[j] & pixelMask) | pixel; \
164 for (j = 0; j < width; j++) { \
173 /* for 3, 6, 8, 12, 16-byte clearing */
174 #define MULTI_WORD_CLEAR(TYPE, N) \
176 TYPE pixel[N], pixelMask[N]; \
178 _mesa_pack_float_rgba_row(rb->Format, 1, clearColor, pixel); \
180 _pack_colormask(rb->Format, colorMask, pixelMask); \
181 for (k = 0; k < N; k++) { \
182 pixel[k] &= pixelMask[k]; \
183 pixelMask[k] = ~pixelMask[k]; \
186 for (i = 0; i < height; i++) { \
187 TYPE *row = (TYPE *) map; \
189 for (j = 0; j < width; j++) { \
190 for (k = 0; k < N; k++) { \
192 (row[j * N + k] & pixelMask[k]) | pixel[k]; \
197 for (j = 0; j < width; j++) { \
198 for (k = 0; k < N; k++) { \
199 row[j * N + k] = pixel[k]; \
209 SIMPLE_TYPE_CLEAR(GLubyte
);
212 SIMPLE_TYPE_CLEAR(GLushort
);
215 MULTI_WORD_CLEAR(GLubyte
, 3);
218 SIMPLE_TYPE_CLEAR(GLuint
);
221 MULTI_WORD_CLEAR(GLushort
, 3);
224 MULTI_WORD_CLEAR(GLuint
, 2);
227 MULTI_WORD_CLEAR(GLuint
, 3);
230 MULTI_WORD_CLEAR(GLuint
, 4);
233 _mesa_problem(ctx
, "bad pixel size in clear_rgba_buffer()");
237 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);
242 * Clear the front/back/left/right/aux color buffers.
243 * This function is usually only called if the device driver can't
244 * clear its own color buffers for some reason (such as with masking).
247 clear_color_buffers(struct gl_context
*ctx
)
251 for (buf
= 0; buf
< ctx
->DrawBuffer
->_NumColorDrawBuffers
; buf
++) {
252 struct gl_renderbuffer
*rb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[buf
];
254 /* If this is an ES2 context or GL_ARB_ES2_compatibility is supported,
255 * the framebuffer can be complete with some attachments be missing. In
256 * this case the _ColorDrawBuffers pointer will be NULL.
261 const GLubyte colormask
[4] = {
262 GET_COLORMASK_BIT(ctx
->Color
.ColorMask
, buf
, 0) ? 0xff : 0,
263 GET_COLORMASK_BIT(ctx
->Color
.ColorMask
, buf
, 1) ? 0xff : 0,
264 GET_COLORMASK_BIT(ctx
->Color
.ColorMask
, buf
, 2) ? 0xff : 0,
265 GET_COLORMASK_BIT(ctx
->Color
.ColorMask
, buf
, 3) ? 0xff : 0,
267 clear_rgba_buffer(ctx
, rb
, colormask
);
273 * Called via the device driver's ctx->Driver.Clear() function if the
274 * device driver can't clear one or more of the buffers itself.
275 * \param buffers bitfield of BUFFER_BIT_* values indicating which
276 * renderbuffers are to be cleared.
277 * \param all if GL_TRUE, clear whole buffer, else clear specified region.
280 _swrast_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
282 const GLbitfield BUFFER_DS
= BUFFER_BIT_DEPTH
| BUFFER_BIT_STENCIL
;
286 const GLbitfield legalBits
=
287 BUFFER_BIT_FRONT_LEFT
|
288 BUFFER_BIT_FRONT_RIGHT
|
289 BUFFER_BIT_BACK_LEFT
|
290 BUFFER_BIT_BACK_RIGHT
|
295 assert((buffers
& (~legalBits
)) == 0);
299 if (!_mesa_check_conditional_render(ctx
))
300 return; /* don't clear */
302 if (SWRAST_CONTEXT(ctx
)->NewState
)
303 _swrast_validate_derived(ctx
);
305 if ((buffers
& BUFFER_BITS_COLOR
)
306 && (ctx
->DrawBuffer
->_NumColorDrawBuffers
> 0)) {
307 clear_color_buffers(ctx
);
310 if (buffers
& BUFFER_BIT_ACCUM
) {
311 _mesa_clear_accum_buffer(ctx
);
314 if (buffers
& BUFFER_DS
) {
315 struct gl_renderbuffer
*depthRb
=
316 ctx
->DrawBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
317 struct gl_renderbuffer
*stencilRb
=
318 ctx
->DrawBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
320 if ((buffers
& BUFFER_DS
) == BUFFER_DS
&& depthRb
== stencilRb
) {
321 /* clear depth and stencil together */
322 _swrast_clear_depth_stencil_buffer(ctx
);
325 /* clear depth, stencil separately */
326 if (buffers
& BUFFER_BIT_DEPTH
) {
327 _swrast_clear_depth_buffer(ctx
);
329 if (buffers
& BUFFER_BIT_STENCIL
) {
330 _swrast_clear_stencil_buffer(ctx
);