Merge git://proxy01.pd.intel.com:9419/git/mesa/mesa into crestline
[mesa.git] / src / mesa / swrast / s_buffers.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.2
4 *
5 * Copyright (C) 1999-2006 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 /** XXX This file should be named s_clear.c */
26
27 #include "glheader.h"
28 #include "colormac.h"
29 #include "macros.h"
30 #include "imports.h"
31 #include "mtypes.h"
32
33 #include "s_accum.h"
34 #include "s_context.h"
35 #include "s_depth.h"
36 #include "s_masking.h"
37 #include "s_stencil.h"
38
39
40 /**
41 * Clear the color buffer when glColorMask is in effect.
42 */
43 static void
44 clear_rgba_buffer_with_masking(GLcontext *ctx, struct gl_renderbuffer *rb)
45 {
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;
50 SWspan span;
51 GLint i;
52
53 ASSERT(ctx->Visual.rgbMode);
54 ASSERT(rb->PutRow);
55
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);
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, width, 0, SPAN_RGBA);
123 for (i = 0; i < width;i++) {
124 span.array->index[i] = ctx->Color.ClearIndex;
125 }
126
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.
130 */
131 for (i = 0; i < height;i++) {
132 span.x = x;
133 span.y = y + 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);
137 }
138 }
139
140
141 /**
142 * Clear an rgba color buffer without channel masking.
143 */
144 static void
145 clear_rgba_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
146 {
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;
151 GLubyte clear8[4];
152 GLushort clear16[4];
153 GLvoid *clearVal;
154 GLint i;
155
156 ASSERT(ctx->Visual.rgbMode);
157
158 ASSERT(ctx->Color.ColorMask[0] &&
159 ctx->Color.ColorMask[1] &&
160 ctx->Color.ColorMask[2] &&
161 ctx->Color.ColorMask[3]);
162
163 ASSERT(rb->PutMonoRow);
164
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]);
171 clearVal = clear8;
172 break;
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]);
178 clearVal = clear16;
179 break;
180 case GL_FLOAT:
181 clearVal = ctx->Color.ClearColor;
182 break;
183 default:
184 _mesa_problem(ctx, "Bad rb DataType in clear_color_buffer");
185 return;
186 }
187
188 for (i = 0; i < height; i++) {
189 rb->PutMonoRow(ctx, rb, width, x, y + i, clearVal, NULL);
190 }
191 }
192
193
194 /**
195 * Clear color index buffer without masking.
196 */
197 static void
198 clear_ci_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
199 {
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;
204 GLubyte clear8;
205 GLushort clear16;
206 GLuint clear32;
207 GLvoid *clearVal;
208 GLint i;
209
210 ASSERT(!ctx->Visual.rgbMode);
211
212 ASSERT((ctx->Color.IndexMask & ((1 << rb->IndexBits) - 1))
213 == (GLuint) ((1 << rb->IndexBits) - 1));
214
215 ASSERT(rb->PutMonoRow);
216
217 /* setup clear value */
218 switch (rb->DataType) {
219 case GL_UNSIGNED_BYTE:
220 clear8 = (GLubyte) ctx->Color.ClearIndex;
221 clearVal = &clear8;
222 break;
223 case GL_UNSIGNED_SHORT:
224 clear16 = (GLushort) ctx->Color.ClearIndex;
225 clearVal = &clear16;
226 break;
227 case GL_UNSIGNED_INT:
228 clear32 = ctx->Color.ClearIndex;
229 clearVal = &clear32;
230 break;
231 default:
232 _mesa_problem(ctx, "Bad rb DataType in clear_color_buffer");
233 return;
234 }
235
236 for (i = 0; i < height; i++)
237 rb->PutMonoRow(ctx, rb, width, x, y + i, clearVal, NULL);
238 }
239
240
241 /**
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).
245 */
246 static void
247 clear_color_buffers(GLcontext *ctx)
248 {
249 GLboolean masking;
250 GLuint i;
251
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]) {
257 masking = GL_FALSE;
258 }
259 else {
260 masking = GL_TRUE;
261 }
262 }
263 else {
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) {
267 masking = GL_FALSE;
268 }
269 else {
270 masking = GL_TRUE;
271 }
272 }
273
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) {
277 if (masking) {
278 clear_rgba_buffer_with_masking(ctx, rb);
279 }
280 else {
281 clear_rgba_buffer(ctx, rb);
282 }
283 }
284 else {
285 if (masking) {
286 clear_ci_buffer_with_masking(ctx, rb);
287 }
288 else {
289 clear_ci_buffer(ctx, rb);
290 }
291 }
292 }
293 }
294
295
296 /**
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.
302 */
303 void
304 _swrast_Clear(GLcontext *ctx, GLbitfield buffers)
305 {
306 SWcontext *swrast = SWRAST_CONTEXT(ctx);
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 BUFFER_BIT_AUX1 |
320 BUFFER_BIT_AUX2 |
321 BUFFER_BIT_AUX3;
322 assert((buffers & (~legalBits)) == 0);
323 }
324 #endif
325
326 RENDER_START(swrast,ctx);
327
328 /* do software clearing here */
329 if (buffers) {
330 if (buffers & ctx->DrawBuffer->_ColorDrawBufferMask[0]) {
331 clear_color_buffers(ctx);
332 }
333 if (buffers & BUFFER_BIT_DEPTH) {
334 _swrast_clear_depth_buffer(ctx, ctx->DrawBuffer->_DepthBuffer);
335 }
336 if (buffers & BUFFER_BIT_ACCUM) {
337 _swrast_clear_accum_buffer(ctx,
338 ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer);
339 }
340 if (buffers & BUFFER_BIT_STENCIL) {
341 _swrast_clear_stencil_buffer(ctx, ctx->DrawBuffer->_StencilBuffer);
342 }
343 }
344
345 RENDER_FINISH(swrast,ctx);
346 }