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 CLAMPED_FLOAT_TO_CHAN(clearColor
[RCOMP
], ctx
->Color
.ClearColor
[0]);
57 CLAMPED_FLOAT_TO_CHAN(clearColor
[GCOMP
], ctx
->Color
.ClearColor
[1]);
58 CLAMPED_FLOAT_TO_CHAN(clearColor
[BCOMP
], ctx
->Color
.ClearColor
[2]);
59 CLAMPED_FLOAT_TO_CHAN(clearColor
[ACOMP
], ctx
->Color
.ClearColor
[3]);
61 for (i
= 0; i
< height
; i
++) {
63 GLchan rgba
[MAX_WIDTH
][4];
65 for (j
= 0; j
< width
; j
++) {
66 COPY_CHAN4(rgba
[j
], clearColor
);
68 /* setup span struct for masking */
69 INIT_SPAN(span
, GL_BITMAP
, width
, 0, SPAN_RGBA
);
72 _swrast_mask_rgba_span(ctx
, rb
, &span
, rgba
);
73 /* write masked row */
74 rb
->PutRow(ctx
, rb
, width
, x
, y
+ i
, rgba
, NULL
);
80 * Clear color index buffer with masking.
83 clear_ci_buffer_with_masking(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
85 const GLint x
= ctx
->DrawBuffer
->_Xmin
;
86 const GLint y
= ctx
->DrawBuffer
->_Ymin
;
87 const GLint height
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
88 const GLint width
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
91 ASSERT(!ctx
->Visual
.rgbMode
);
93 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
95 for (i
= 0; i
< height
;i
++) {
97 GLuint indexes
[MAX_WIDTH
];
99 for (j
= 0; j
< width
;j
++) {
100 indexes
[j
] = ctx
->Color
.ClearIndex
;
102 /* setup span struct for masking */
103 INIT_SPAN(span
, GL_BITMAP
, width
, 0, SPAN_RGBA
);
106 _swrast_mask_ci_span(ctx
, rb
, &span
, indexes
);
107 /* write masked row */
108 rb
->PutRow(ctx
, rb
, width
, x
, y
+ i
, indexes
, NULL
);
114 * Clear an rgba color buffer without channel masking.
117 clear_rgba_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
119 const GLint x
= ctx
->DrawBuffer
->_Xmin
;
120 const GLint y
= ctx
->DrawBuffer
->_Ymin
;
121 const GLint height
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
122 const GLint width
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
128 ASSERT(ctx
->Visual
.rgbMode
);
130 ASSERT(ctx
->Color
.ColorMask
[0] &&
131 ctx
->Color
.ColorMask
[1] &&
132 ctx
->Color
.ColorMask
[2] &&
133 ctx
->Color
.ColorMask
[3]);
135 ASSERT(rb
->PutMonoRow
);
137 switch (rb
->DataType
) {
138 case GL_UNSIGNED_BYTE
:
139 UNCLAMPED_FLOAT_TO_UBYTE(clear8
[0], ctx
->Color
.ClearColor
[0]);
140 UNCLAMPED_FLOAT_TO_UBYTE(clear8
[1], ctx
->Color
.ClearColor
[1]);
141 UNCLAMPED_FLOAT_TO_UBYTE(clear8
[2], ctx
->Color
.ClearColor
[2]);
142 UNCLAMPED_FLOAT_TO_UBYTE(clear8
[3], ctx
->Color
.ClearColor
[3]);
145 case GL_UNSIGNED_SHORT
:
146 UNCLAMPED_FLOAT_TO_USHORT(clear16
[0], ctx
->Color
.ClearColor
[0]);
147 UNCLAMPED_FLOAT_TO_USHORT(clear16
[1], ctx
->Color
.ClearColor
[1]);
148 UNCLAMPED_FLOAT_TO_USHORT(clear16
[2], ctx
->Color
.ClearColor
[2]);
149 UNCLAMPED_FLOAT_TO_USHORT(clear16
[3], ctx
->Color
.ClearColor
[3]);
153 clearVal
= ctx
->Color
.ClearColor
;
156 _mesa_problem(ctx
, "Bad rb DataType in clear_color_buffer");
160 for (i
= 0; i
< height
; i
++) {
161 rb
->PutMonoRow(ctx
, rb
, width
, x
, y
+ i
, clearVal
, NULL
);
167 * Clear color index buffer without masking.
170 clear_ci_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
172 const GLint x
= ctx
->DrawBuffer
->_Xmin
;
173 const GLint y
= ctx
->DrawBuffer
->_Ymin
;
174 const GLint height
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
175 const GLint width
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
182 ASSERT(!ctx
->Visual
.rgbMode
);
184 ASSERT((ctx
->Color
.IndexMask
& ((1 << rb
->IndexBits
) - 1))
185 == (GLuint
) ((1 << rb
->IndexBits
) - 1));
187 ASSERT(rb
->PutMonoRow
);
189 /* setup clear value */
190 switch (rb
->DataType
) {
191 case GL_UNSIGNED_BYTE
:
192 clear8
= (GLubyte
) ctx
->Color
.ClearIndex
;
195 case GL_UNSIGNED_SHORT
:
196 clear16
= (GLushort
) ctx
->Color
.ClearIndex
;
199 case GL_UNSIGNED_INT
:
200 clear32
= ctx
->Color
.ClearIndex
;
204 _mesa_problem(ctx
, "Bad rb DataType in clear_color_buffer");
208 for (i
= 0; i
< height
; i
++)
209 rb
->PutMonoRow(ctx
, rb
, width
, x
, y
+ i
, clearVal
, NULL
);
214 * Clear the front/back/left/right/aux color buffers.
215 * This function is usually only called if the device driver can't
216 * clear its own color buffers for some reason (such as with masking).
219 clear_color_buffers(GLcontext
*ctx
)
224 if (ctx
->Visual
.rgbMode
) {
225 if (ctx
->Color
.ColorMask
[0] &&
226 ctx
->Color
.ColorMask
[1] &&
227 ctx
->Color
.ColorMask
[2] &&
228 ctx
->Color
.ColorMask
[3]) {
236 struct gl_renderbuffer
*rb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[0][0];
237 const GLuint indexBits
= (1 << rb
->IndexBits
) - 1;
238 if ((ctx
->Color
.IndexMask
& indexBits
) == indexBits
) {
246 for (i
= 0; i
< ctx
->DrawBuffer
->_NumColorDrawBuffers
[0]; i
++) {
247 struct gl_renderbuffer
*rb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[0][i
];
248 if (ctx
->Visual
.rgbMode
) {
250 clear_rgba_buffer_with_masking(ctx
, rb
);
253 clear_rgba_buffer(ctx
, rb
);
258 clear_ci_buffer_with_masking(ctx
, rb
);
261 clear_ci_buffer(ctx
, rb
);
269 * Called via the device driver's ctx->Driver.Clear() function if the
270 * device driver can't clear one or more of the buffers itself.
271 * \param mask bitfield of BUFER_BIT_* values indicating which renderbuffers
273 * \param all if GL_TRUE, clear whole buffer, else clear specified region.
276 _swrast_Clear(GLcontext
*ctx
, GLbitfield mask
,
277 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
279 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
281 (void) all
; (void) x
; (void) y
; (void) width
; (void) height
;
285 const GLbitfield legalBits
=
286 BUFFER_BIT_FRONT_LEFT
|
287 BUFFER_BIT_FRONT_RIGHT
|
288 BUFFER_BIT_BACK_LEFT
|
289 BUFFER_BIT_BACK_RIGHT
|
297 assert((mask
& (~legalBits
)) == 0);
301 RENDER_START(swrast
,ctx
);
303 /* do software clearing here */
305 if (mask
& ctx
->DrawBuffer
->_ColorDrawBufferMask
[0]) {
306 clear_color_buffers(ctx
);
308 if (mask
& BUFFER_BIT_DEPTH
) {
309 _swrast_clear_depth_buffer(ctx
, ctx
->DrawBuffer
->_DepthBuffer
);
311 if (mask
& BUFFER_BIT_ACCUM
) {
312 _swrast_clear_accum_buffer(ctx
,
313 ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
);
315 if (mask
& BUFFER_BIT_STENCIL
) {
316 _swrast_clear_stencil_buffer(ctx
, ctx
->DrawBuffer
->_StencilBuffer
);
320 RENDER_FINISH(swrast
,ctx
);