2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2006 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 /** XXX This file should be named s_clear.c */
34 #include "s_context.h"
36 #include "s_masking.h"
37 #include "s_stencil.h"
41 * Clear the color buffer when glColorMask is in effect.
44 clear_rgba_buffer_with_masking(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
46 const GLint x
= ctx
->DrawBuffer
->_Xmin
;
47 const GLint y
= ctx
->DrawBuffer
->_Ymin
;
48 const GLint height
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
49 const GLint width
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
53 ASSERT(ctx
->Visual
.rgbMode
);
56 /* Initialize color span with clear color */
57 /* XXX optimize for clearcolor == black/zero (bzero) */
58 INIT_SPAN(span
, GL_BITMAP
, width
, 0, 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
, width
, 0, SPAN_RGBA
);
123 for (i
= 0; i
< width
;i
++) {
124 span
.array
->index
[i
] = ctx
->Color
.ClearIndex
;
127 /* Note that masking will change the color indexes, but only the
128 * bits for which the write mask is GL_FALSE. The bits
129 * which are write-enabled won't get modified.
131 for (i
= 0; i
< height
;i
++) {
134 _swrast_mask_ci_span(ctx
, rb
, &span
);
135 /* write masked row */
136 rb
->PutRow(ctx
, rb
, width
, x
, y
+ i
, span
.array
->index
, NULL
);
142 * Clear an rgba color buffer without channel masking.
145 clear_rgba_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
147 const GLint x
= ctx
->DrawBuffer
->_Xmin
;
148 const GLint y
= ctx
->DrawBuffer
->_Ymin
;
149 const GLint height
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
150 const GLint width
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
156 ASSERT(ctx
->Visual
.rgbMode
);
158 ASSERT(ctx
->Color
.ColorMask
[0] &&
159 ctx
->Color
.ColorMask
[1] &&
160 ctx
->Color
.ColorMask
[2] &&
161 ctx
->Color
.ColorMask
[3]);
163 ASSERT(rb
->PutMonoRow
);
165 switch (rb
->DataType
) {
166 case GL_UNSIGNED_BYTE
:
167 UNCLAMPED_FLOAT_TO_UBYTE(clear8
[0], ctx
->Color
.ClearColor
[0]);
168 UNCLAMPED_FLOAT_TO_UBYTE(clear8
[1], ctx
->Color
.ClearColor
[1]);
169 UNCLAMPED_FLOAT_TO_UBYTE(clear8
[2], ctx
->Color
.ClearColor
[2]);
170 UNCLAMPED_FLOAT_TO_UBYTE(clear8
[3], ctx
->Color
.ClearColor
[3]);
173 case GL_UNSIGNED_SHORT
:
174 UNCLAMPED_FLOAT_TO_USHORT(clear16
[0], ctx
->Color
.ClearColor
[0]);
175 UNCLAMPED_FLOAT_TO_USHORT(clear16
[1], ctx
->Color
.ClearColor
[1]);
176 UNCLAMPED_FLOAT_TO_USHORT(clear16
[2], ctx
->Color
.ClearColor
[2]);
177 UNCLAMPED_FLOAT_TO_USHORT(clear16
[3], ctx
->Color
.ClearColor
[3]);
181 clearVal
= ctx
->Color
.ClearColor
;
184 _mesa_problem(ctx
, "Bad rb DataType in clear_color_buffer");
188 for (i
= 0; i
< height
; i
++) {
189 rb
->PutMonoRow(ctx
, rb
, width
, x
, y
+ i
, clearVal
, NULL
);
195 * Clear color index buffer without masking.
198 clear_ci_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
200 const GLint x
= ctx
->DrawBuffer
->_Xmin
;
201 const GLint y
= ctx
->DrawBuffer
->_Ymin
;
202 const GLint height
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
203 const GLint width
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
210 ASSERT(!ctx
->Visual
.rgbMode
);
212 ASSERT((ctx
->Color
.IndexMask
& ((1 << rb
->IndexBits
) - 1))
213 == (GLuint
) ((1 << rb
->IndexBits
) - 1));
215 ASSERT(rb
->PutMonoRow
);
217 /* setup clear value */
218 switch (rb
->DataType
) {
219 case GL_UNSIGNED_BYTE
:
220 clear8
= (GLubyte
) ctx
->Color
.ClearIndex
;
223 case GL_UNSIGNED_SHORT
:
224 clear16
= (GLushort
) ctx
->Color
.ClearIndex
;
227 case GL_UNSIGNED_INT
:
228 clear32
= ctx
->Color
.ClearIndex
;
232 _mesa_problem(ctx
, "Bad rb DataType in clear_color_buffer");
236 for (i
= 0; i
< height
; i
++)
237 rb
->PutMonoRow(ctx
, rb
, width
, x
, y
+ i
, clearVal
, NULL
);
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(GLcontext
*ctx
)
252 if (ctx
->Visual
.rgbMode
) {
253 if (ctx
->Color
.ColorMask
[0] &&
254 ctx
->Color
.ColorMask
[1] &&
255 ctx
->Color
.ColorMask
[2] &&
256 ctx
->Color
.ColorMask
[3]) {
264 struct gl_renderbuffer
*rb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[0][0];
265 const GLuint indexBits
= (1 << rb
->IndexBits
) - 1;
266 if ((ctx
->Color
.IndexMask
& indexBits
) == indexBits
) {
274 for (i
= 0; i
< ctx
->DrawBuffer
->_NumColorDrawBuffers
[0]; i
++) {
275 struct gl_renderbuffer
*rb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[0][i
];
276 if (ctx
->Visual
.rgbMode
) {
278 clear_rgba_buffer_with_masking(ctx
, rb
);
281 clear_rgba_buffer(ctx
, rb
);
286 clear_ci_buffer_with_masking(ctx
, rb
);
289 clear_ci_buffer(ctx
, rb
);
297 * Called via the device driver's ctx->Driver.Clear() function if the
298 * device driver can't clear one or more of the buffers itself.
299 * \param buffers bitfield of BUFFER_BIT_* values indicating which
300 * renderbuffers are to be cleared.
301 * \param all if GL_TRUE, clear whole buffer, else clear specified region.
304 _swrast_Clear(GLcontext
*ctx
, GLbitfield buffers
)
306 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
310 const GLbitfield legalBits
=
311 BUFFER_BIT_FRONT_LEFT
|
312 BUFFER_BIT_FRONT_RIGHT
|
313 BUFFER_BIT_BACK_LEFT
|
314 BUFFER_BIT_BACK_RIGHT
|
322 assert((buffers
& (~legalBits
)) == 0);
326 RENDER_START(swrast
,ctx
);
328 /* do software clearing here */
330 if (buffers
& ctx
->DrawBuffer
->_ColorDrawBufferMask
[0]) {
331 clear_color_buffers(ctx
);
333 if (buffers
& BUFFER_BIT_DEPTH
) {
334 _swrast_clear_depth_buffer(ctx
, ctx
->DrawBuffer
->_DepthBuffer
);
336 if (buffers
& BUFFER_BIT_ACCUM
) {
337 _swrast_clear_accum_buffer(ctx
,
338 ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
);
340 if (buffers
& BUFFER_BIT_STENCIL
) {
341 _swrast_clear_stencil_buffer(ctx
, ctx
->DrawBuffer
->_StencilBuffer
);
345 RENDER_FINISH(swrast
,ctx
);