2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 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.
25 #include "main/glheader.h"
26 #include "main/colormac.h"
27 #include "main/macros.h"
28 #include "main/imports.h"
29 #include "main/mtypes.h"
32 #include "s_context.h"
34 #include "s_masking.h"
35 #include "s_stencil.h"
39 * Clear the color buffer when glColorMask is in effect.
42 clear_rgba_buffer_with_masking(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
44 const GLint x
= ctx
->DrawBuffer
->_Xmin
;
45 const GLint y
= ctx
->DrawBuffer
->_Ymin
;
46 const GLint height
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
47 const GLint width
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
51 ASSERT(ctx
->Visual
.rgbMode
);
54 /* Initialize color span with clear color */
55 /* XXX optimize for clearcolor == black/zero (bzero) */
56 INIT_SPAN(span
, GL_BITMAP
);
58 span
.arrayMask
= SPAN_RGBA
;
59 span
.array
->ChanType
= rb
->DataType
;
60 if (span
.array
->ChanType
== GL_UNSIGNED_BYTE
) {
61 GLubyte clearColor
[4];
62 UNCLAMPED_FLOAT_TO_UBYTE(clearColor
[RCOMP
], ctx
->Color
.ClearColor
[0]);
63 UNCLAMPED_FLOAT_TO_UBYTE(clearColor
[GCOMP
], ctx
->Color
.ClearColor
[1]);
64 UNCLAMPED_FLOAT_TO_UBYTE(clearColor
[BCOMP
], ctx
->Color
.ClearColor
[2]);
65 UNCLAMPED_FLOAT_TO_UBYTE(clearColor
[ACOMP
], ctx
->Color
.ClearColor
[3]);
66 for (i
= 0; i
< width
; i
++) {
67 COPY_4UBV(span
.array
->rgba
[i
], clearColor
);
70 else if (span
.array
->ChanType
== GL_UNSIGNED_SHORT
) {
71 GLushort clearColor
[4];
72 UNCLAMPED_FLOAT_TO_USHORT(clearColor
[RCOMP
], ctx
->Color
.ClearColor
[0]);
73 UNCLAMPED_FLOAT_TO_USHORT(clearColor
[GCOMP
], ctx
->Color
.ClearColor
[1]);
74 UNCLAMPED_FLOAT_TO_USHORT(clearColor
[BCOMP
], ctx
->Color
.ClearColor
[2]);
75 UNCLAMPED_FLOAT_TO_USHORT(clearColor
[ACOMP
], ctx
->Color
.ClearColor
[3]);
76 for (i
= 0; i
< width
; i
++) {
77 COPY_4V(span
.array
->rgba
[i
], clearColor
);
81 ASSERT(span
.array
->ChanType
== GL_FLOAT
);
82 for (i
= 0; i
< width
; i
++) {
83 CLAMPED_FLOAT_TO_CHAN(span
.array
->rgba
[i
][0], ctx
->Color
.ClearColor
[0]);
84 CLAMPED_FLOAT_TO_CHAN(span
.array
->rgba
[i
][1], ctx
->Color
.ClearColor
[1]);
85 CLAMPED_FLOAT_TO_CHAN(span
.array
->rgba
[i
][2], ctx
->Color
.ClearColor
[2]);
86 CLAMPED_FLOAT_TO_CHAN(span
.array
->rgba
[i
][3], ctx
->Color
.ClearColor
[3]);
90 /* Note that masking will change the color values, but only the
91 * channels for which the write mask is GL_FALSE. The channels
92 * which which are write-enabled won't get modified.
94 for (i
= 0; i
< height
; i
++) {
97 _swrast_mask_rgba_span(ctx
, rb
, &span
);
98 /* write masked row */
99 rb
->PutRow(ctx
, rb
, width
, x
, y
+ i
, span
.array
->rgba
, NULL
);
105 * Clear color index buffer with masking.
108 clear_ci_buffer_with_masking(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
110 const GLint x
= ctx
->DrawBuffer
->_Xmin
;
111 const GLint y
= ctx
->DrawBuffer
->_Ymin
;
112 const GLint height
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
113 const GLint width
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
117 ASSERT(!ctx
->Visual
.rgbMode
);
119 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
121 /* Initialize index span with clear index */
122 INIT_SPAN(span
, GL_BITMAP
);
124 span
.arrayMask
= SPAN_INDEX
;
125 for (i
= 0; i
< width
;i
++) {
126 span
.array
->index
[i
] = ctx
->Color
.ClearIndex
;
129 /* Note that masking will change the color indexes, but only the
130 * bits for which the write mask is GL_FALSE. The bits
131 * which are write-enabled won't get modified.
133 for (i
= 0; i
< height
;i
++) {
136 _swrast_mask_ci_span(ctx
, rb
, &span
);
137 /* write masked row */
138 rb
->PutRow(ctx
, rb
, width
, x
, y
+ i
, span
.array
->index
, NULL
);
144 * Clear an rgba color buffer without channel masking.
147 clear_rgba_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
149 const GLint x
= ctx
->DrawBuffer
->_Xmin
;
150 const GLint y
= ctx
->DrawBuffer
->_Ymin
;
151 const GLint height
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
152 const GLint width
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
158 ASSERT(ctx
->Visual
.rgbMode
);
160 ASSERT(ctx
->Color
.ColorMask
[0] &&
161 ctx
->Color
.ColorMask
[1] &&
162 ctx
->Color
.ColorMask
[2] &&
163 ctx
->Color
.ColorMask
[3]);
165 ASSERT(rb
->PutMonoRow
);
167 switch (rb
->DataType
) {
168 case GL_UNSIGNED_BYTE
:
169 UNCLAMPED_FLOAT_TO_UBYTE(clear8
[0], ctx
->Color
.ClearColor
[0]);
170 UNCLAMPED_FLOAT_TO_UBYTE(clear8
[1], ctx
->Color
.ClearColor
[1]);
171 UNCLAMPED_FLOAT_TO_UBYTE(clear8
[2], ctx
->Color
.ClearColor
[2]);
172 UNCLAMPED_FLOAT_TO_UBYTE(clear8
[3], ctx
->Color
.ClearColor
[3]);
175 case GL_UNSIGNED_SHORT
:
176 UNCLAMPED_FLOAT_TO_USHORT(clear16
[0], ctx
->Color
.ClearColor
[0]);
177 UNCLAMPED_FLOAT_TO_USHORT(clear16
[1], ctx
->Color
.ClearColor
[1]);
178 UNCLAMPED_FLOAT_TO_USHORT(clear16
[2], ctx
->Color
.ClearColor
[2]);
179 UNCLAMPED_FLOAT_TO_USHORT(clear16
[3], ctx
->Color
.ClearColor
[3]);
183 clearVal
= ctx
->Color
.ClearColor
;
186 _mesa_problem(ctx
, "Bad rb DataType in clear_color_buffer");
190 for (i
= 0; i
< height
; i
++) {
191 rb
->PutMonoRow(ctx
, rb
, width
, x
, y
+ i
, clearVal
, NULL
);
197 * Clear color index buffer without masking.
200 clear_ci_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
202 const GLint x
= ctx
->DrawBuffer
->_Xmin
;
203 const GLint y
= ctx
->DrawBuffer
->_Ymin
;
204 const GLint height
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
205 const GLint width
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
212 ASSERT(!ctx
->Visual
.rgbMode
);
214 ASSERT((ctx
->Color
.IndexMask
& ((1 << rb
->IndexBits
) - 1))
215 == (GLuint
) ((1 << rb
->IndexBits
) - 1));
217 ASSERT(rb
->PutMonoRow
);
219 /* setup clear value */
220 switch (rb
->DataType
) {
221 case GL_UNSIGNED_BYTE
:
222 clear8
= (GLubyte
) ctx
->Color
.ClearIndex
;
225 case GL_UNSIGNED_SHORT
:
226 clear16
= (GLushort
) ctx
->Color
.ClearIndex
;
229 case GL_UNSIGNED_INT
:
230 clear32
= ctx
->Color
.ClearIndex
;
234 _mesa_problem(ctx
, "Bad rb DataType in clear_color_buffer");
238 for (i
= 0; i
< height
; i
++)
239 rb
->PutMonoRow(ctx
, rb
, width
, x
, y
+ i
, clearVal
, NULL
);
244 * Clear the front/back/left/right/aux color buffers.
245 * This function is usually only called if the device driver can't
246 * clear its own color buffers for some reason (such as with masking).
249 clear_color_buffers(GLcontext
*ctx
)
254 if (ctx
->Visual
.rgbMode
) {
255 if (ctx
->Color
.ColorMask
[0] &&
256 ctx
->Color
.ColorMask
[1] &&
257 ctx
->Color
.ColorMask
[2] &&
258 ctx
->Color
.ColorMask
[3]) {
266 struct gl_renderbuffer
*rb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[0];
267 const GLuint indexBits
= (1 << rb
->IndexBits
) - 1;
268 if ((ctx
->Color
.IndexMask
& indexBits
) == indexBits
) {
276 for (buf
= 0; buf
< ctx
->DrawBuffer
->_NumColorDrawBuffers
; buf
++) {
277 struct gl_renderbuffer
*rb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[buf
];
278 if (ctx
->Visual
.rgbMode
) {
280 clear_rgba_buffer_with_masking(ctx
, rb
);
283 clear_rgba_buffer(ctx
, rb
);
288 clear_ci_buffer_with_masking(ctx
, rb
);
291 clear_ci_buffer(ctx
, rb
);
299 * Called via the device driver's ctx->Driver.Clear() function if the
300 * device driver can't clear one or more of the buffers itself.
301 * \param buffers bitfield of BUFFER_BIT_* values indicating which
302 * renderbuffers are to be cleared.
303 * \param all if GL_TRUE, clear whole buffer, else clear specified region.
306 _swrast_Clear(GLcontext
*ctx
, GLbitfield buffers
)
310 const GLbitfield legalBits
=
311 BUFFER_BIT_FRONT_LEFT
|
312 BUFFER_BIT_FRONT_RIGHT
|
313 BUFFER_BIT_BACK_LEFT
|
314 BUFFER_BIT_BACK_RIGHT
|
319 assert((buffers
& (~legalBits
)) == 0);
323 swrast_render_start(ctx
);
325 /* do software clearing here */
327 if ((buffers
& BUFFER_BITS_COLOR
)
328 && (ctx
->DrawBuffer
->_NumColorDrawBuffers
> 0)) {
329 clear_color_buffers(ctx
);
331 if (buffers
& BUFFER_BIT_DEPTH
) {
332 _swrast_clear_depth_buffer(ctx
, ctx
->DrawBuffer
->_DepthBuffer
);
334 if (buffers
& BUFFER_BIT_ACCUM
) {
335 _swrast_clear_accum_buffer(ctx
,
336 ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
);
338 if (buffers
& BUFFER_BIT_STENCIL
) {
339 _swrast_clear_stencil_buffer(ctx
, ctx
->DrawBuffer
->_StencilBuffer
);
343 swrast_render_finish(ctx
);