Merge branch 'mesa_7_5_branch' into mesa_7_6_branch
[mesa.git] / src / mesa / swrast / s_clear.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.1
4 *
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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.
23 */
24
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"
30
31 #include "s_accum.h"
32 #include "s_context.h"
33 #include "s_depth.h"
34 #include "s_masking.h"
35 #include "s_stencil.h"
36
37
38 /**
39 * Clear the color buffer when glColorMask is in effect.
40 */
41 static void
42 clear_rgba_buffer_with_masking(GLcontext *ctx, struct gl_renderbuffer *rb)
43 {
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;
48 SWspan span;
49 GLint i;
50
51 ASSERT(ctx->Visual.rgbMode);
52 ASSERT(rb->PutRow);
53
54 /* Initialize color span with clear color */
55 /* XXX optimize for clearcolor == black/zero (bzero) */
56 INIT_SPAN(span, GL_BITMAP);
57 span.end = width;
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);
68 }
69 }
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);
78 }
79 }
80 else {
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]);
87 }
88 }
89
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.
93 */
94 for (i = 0; i < height; i++) {
95 span.x = x;
96 span.y = y + 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);
100 }
101 }
102
103
104 /**
105 * Clear color index buffer with masking.
106 */
107 static void
108 clear_ci_buffer_with_masking(GLcontext *ctx, struct gl_renderbuffer *rb)
109 {
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;
114 SWspan span;
115 GLint i;
116
117 ASSERT(!ctx->Visual.rgbMode);
118 ASSERT(rb->PutRow);
119 ASSERT(rb->DataType == GL_UNSIGNED_INT);
120
121 /* Initialize index span with clear index */
122 INIT_SPAN(span, GL_BITMAP);
123 span.end = width;
124 span.arrayMask = SPAN_INDEX;
125 for (i = 0; i < width;i++) {
126 span.array->index[i] = ctx->Color.ClearIndex;
127 }
128
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.
132 */
133 for (i = 0; i < height;i++) {
134 span.x = x;
135 span.y = y + 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);
139 }
140 }
141
142
143 /**
144 * Clear an rgba color buffer without channel masking.
145 */
146 static void
147 clear_rgba_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
148 {
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;
153 GLubyte clear8[4];
154 GLushort clear16[4];
155 GLvoid *clearVal;
156 GLint i;
157
158 ASSERT(ctx->Visual.rgbMode);
159
160 ASSERT(ctx->Color.ColorMask[0] &&
161 ctx->Color.ColorMask[1] &&
162 ctx->Color.ColorMask[2] &&
163 ctx->Color.ColorMask[3]);
164
165 ASSERT(rb->PutMonoRow);
166
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]);
173 clearVal = clear8;
174 break;
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]);
180 clearVal = clear16;
181 break;
182 case GL_FLOAT:
183 clearVal = ctx->Color.ClearColor;
184 break;
185 default:
186 _mesa_problem(ctx, "Bad rb DataType in clear_color_buffer");
187 return;
188 }
189
190 for (i = 0; i < height; i++) {
191 rb->PutMonoRow(ctx, rb, width, x, y + i, clearVal, NULL);
192 }
193 }
194
195
196 /**
197 * Clear color index buffer without masking.
198 */
199 static void
200 clear_ci_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
201 {
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;
206 GLubyte clear8;
207 GLushort clear16;
208 GLuint clear32;
209 GLvoid *clearVal;
210 GLint i;
211
212 ASSERT(!ctx->Visual.rgbMode);
213
214 ASSERT((ctx->Color.IndexMask & ((1 << rb->IndexBits) - 1))
215 == (GLuint) ((1 << rb->IndexBits) - 1));
216
217 ASSERT(rb->PutMonoRow);
218
219 /* setup clear value */
220 switch (rb->DataType) {
221 case GL_UNSIGNED_BYTE:
222 clear8 = (GLubyte) ctx->Color.ClearIndex;
223 clearVal = &clear8;
224 break;
225 case GL_UNSIGNED_SHORT:
226 clear16 = (GLushort) ctx->Color.ClearIndex;
227 clearVal = &clear16;
228 break;
229 case GL_UNSIGNED_INT:
230 clear32 = ctx->Color.ClearIndex;
231 clearVal = &clear32;
232 break;
233 default:
234 _mesa_problem(ctx, "Bad rb DataType in clear_color_buffer");
235 return;
236 }
237
238 for (i = 0; i < height; i++)
239 rb->PutMonoRow(ctx, rb, width, x, y + i, clearVal, NULL);
240 }
241
242
243 /**
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).
247 */
248 static void
249 clear_color_buffers(GLcontext *ctx)
250 {
251 GLboolean masking;
252 GLuint buf;
253
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]) {
259 masking = GL_FALSE;
260 }
261 else {
262 masking = GL_TRUE;
263 }
264 }
265 else {
266 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0];
267 const GLuint indexBits = (1 << rb->IndexBits) - 1;
268 if ((ctx->Color.IndexMask & indexBits) == indexBits) {
269 masking = GL_FALSE;
270 }
271 else {
272 masking = GL_TRUE;
273 }
274 }
275
276 for (buf = 0; buf < ctx->DrawBuffer->_NumColorDrawBuffers; buf++) {
277 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[buf];
278 if (ctx->Visual.rgbMode) {
279 if (masking) {
280 clear_rgba_buffer_with_masking(ctx, rb);
281 }
282 else {
283 clear_rgba_buffer(ctx, rb);
284 }
285 }
286 else {
287 if (masking) {
288 clear_ci_buffer_with_masking(ctx, rb);
289 }
290 else {
291 clear_ci_buffer(ctx, rb);
292 }
293 }
294 }
295 }
296
297
298 /**
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.
304 */
305 void
306 _swrast_Clear(GLcontext *ctx, GLbitfield buffers)
307 {
308 #ifdef DEBUG_FOO
309 {
310 const GLbitfield legalBits =
311 BUFFER_BIT_FRONT_LEFT |
312 BUFFER_BIT_FRONT_RIGHT |
313 BUFFER_BIT_BACK_LEFT |
314 BUFFER_BIT_BACK_RIGHT |
315 BUFFER_BIT_DEPTH |
316 BUFFER_BIT_STENCIL |
317 BUFFER_BIT_ACCUM |
318 BUFFER_BIT_AUX0;
319 assert((buffers & (~legalBits)) == 0);
320 }
321 #endif
322
323 swrast_render_start(ctx);
324
325 /* do software clearing here */
326 if (buffers) {
327 if ((buffers & BUFFER_BITS_COLOR)
328 && (ctx->DrawBuffer->_NumColorDrawBuffers > 0)) {
329 clear_color_buffers(ctx);
330 }
331 if (buffers & BUFFER_BIT_DEPTH) {
332 _swrast_clear_depth_buffer(ctx, ctx->DrawBuffer->_DepthBuffer);
333 }
334 if (buffers & BUFFER_BIT_ACCUM) {
335 _swrast_clear_accum_buffer(ctx,
336 ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer);
337 }
338 if (buffers & BUFFER_BIT_STENCIL) {
339 _swrast_clear_stencil_buffer(ctx, ctx->DrawBuffer->_StencilBuffer);
340 }
341 }
342
343 swrast_render_finish(ctx);
344 }