Get rid of _swrast_mask_rgba_array() and _swrast_mask_index_array().
[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 GLchan clearColor[4];
51 GLint i;
52
53 ASSERT(ctx->Visual.rgbMode);
54 ASSERT(rb->PutRow);
55
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]);
60
61 for (i = 0; i < height; i++) {
62 struct sw_span span;
63 GLchan rgba[MAX_WIDTH][4];
64 GLint j;
65 for (j = 0; j < width; j++) {
66 COPY_CHAN4(rgba[j], clearColor);
67 }
68 /* setup span struct for masking */
69 INIT_SPAN(span, GL_BITMAP, width, 0, SPAN_RGBA);
70 span.x = x;
71 span.y = y + i;
72 _swrast_mask_rgba_span(ctx, rb, &span, rgba);
73 /* write masked row */
74 rb->PutRow(ctx, rb, width, x, y + i, rgba, NULL);
75 }
76 }
77
78
79 /**
80 * Clear color index buffer with masking.
81 */
82 static void
83 clear_ci_buffer_with_masking(GLcontext *ctx, struct gl_renderbuffer *rb)
84 {
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;
89 GLint i;
90
91 ASSERT(!ctx->Visual.rgbMode);
92 ASSERT(rb->PutRow);
93 ASSERT(rb->DataType == GL_UNSIGNED_INT);
94
95 for (i = 0; i < height;i++) {
96 struct sw_span span;
97 GLuint indexes[MAX_WIDTH];
98 GLint j;
99 for (j = 0; j < width;j++) {
100 indexes[j] = ctx->Color.ClearIndex;
101 }
102 /* setup span struct for masking */
103 INIT_SPAN(span, GL_BITMAP, width, 0, SPAN_RGBA);
104 span.x = x;
105 span.y = y + i;
106 _swrast_mask_ci_span(ctx, rb, &span, indexes);
107 /* write masked row */
108 rb->PutRow(ctx, rb, width, x, y + i, indexes, NULL);
109 }
110 }
111
112
113 /**
114 * Clear an rgba color buffer without channel masking.
115 */
116 static void
117 clear_rgba_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
118 {
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;
123 GLubyte clear8[4];
124 GLushort clear16[4];
125 GLvoid *clearVal;
126 GLint i;
127
128 ASSERT(ctx->Visual.rgbMode);
129
130 ASSERT(ctx->Color.ColorMask[0] &&
131 ctx->Color.ColorMask[1] &&
132 ctx->Color.ColorMask[2] &&
133 ctx->Color.ColorMask[3]);
134
135 ASSERT(rb->PutMonoRow);
136
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]);
143 clearVal = clear8;
144 break;
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]);
150 clearVal = clear16;
151 break;
152 case GL_FLOAT:
153 clearVal = ctx->Color.ClearColor;
154 break;
155 default:
156 _mesa_problem(ctx, "Bad rb DataType in clear_color_buffer");
157 return;
158 }
159
160 for (i = 0; i < height; i++) {
161 rb->PutMonoRow(ctx, rb, width, x, y + i, clearVal, NULL);
162 }
163 }
164
165
166 /**
167 * Clear color index buffer without masking.
168 */
169 static void
170 clear_ci_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
171 {
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;
176 GLubyte clear8;
177 GLushort clear16;
178 GLuint clear32;
179 GLvoid *clearVal;
180 GLint i;
181
182 ASSERT(!ctx->Visual.rgbMode);
183
184 ASSERT((ctx->Color.IndexMask & ((1 << rb->IndexBits) - 1))
185 == (GLuint) ((1 << rb->IndexBits) - 1));
186
187 ASSERT(rb->PutMonoRow);
188
189 /* setup clear value */
190 switch (rb->DataType) {
191 case GL_UNSIGNED_BYTE:
192 clear8 = (GLubyte) ctx->Color.ClearIndex;
193 clearVal = &clear8;
194 break;
195 case GL_UNSIGNED_SHORT:
196 clear16 = (GLushort) ctx->Color.ClearIndex;
197 clearVal = &clear16;
198 break;
199 case GL_UNSIGNED_INT:
200 clear32 = ctx->Color.ClearIndex;
201 clearVal = &clear32;
202 break;
203 default:
204 _mesa_problem(ctx, "Bad rb DataType in clear_color_buffer");
205 return;
206 }
207
208 for (i = 0; i < height; i++)
209 rb->PutMonoRow(ctx, rb, width, x, y + i, clearVal, NULL);
210 }
211
212
213 /**
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).
217 */
218 static void
219 clear_color_buffers(GLcontext *ctx)
220 {
221 GLboolean masking;
222 GLuint i;
223
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]) {
229 masking = GL_FALSE;
230 }
231 else {
232 masking = GL_TRUE;
233 }
234 }
235 else {
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) {
239 masking = GL_FALSE;
240 }
241 else {
242 masking = GL_TRUE;
243 }
244 }
245
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) {
249 if (masking) {
250 clear_rgba_buffer_with_masking(ctx, rb);
251 }
252 else {
253 clear_rgba_buffer(ctx, rb);
254 }
255 }
256 else {
257 if (masking) {
258 clear_ci_buffer_with_masking(ctx, rb);
259 }
260 else {
261 clear_ci_buffer(ctx, rb);
262 }
263 }
264 }
265 }
266
267
268 /**
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
272 * are to be cleared.
273 * \param all if GL_TRUE, clear whole buffer, else clear specified region.
274 */
275 void
276 _swrast_Clear(GLcontext *ctx, GLbitfield mask,
277 GLboolean all, GLint x, GLint y, GLint width, GLint height)
278 {
279 SWcontext *swrast = SWRAST_CONTEXT(ctx);
280
281 (void) all; (void) x; (void) y; (void) width; (void) height;
282
283 #ifdef DEBUG_FOO
284 {
285 const GLbitfield legalBits =
286 BUFFER_BIT_FRONT_LEFT |
287 BUFFER_BIT_FRONT_RIGHT |
288 BUFFER_BIT_BACK_LEFT |
289 BUFFER_BIT_BACK_RIGHT |
290 BUFFER_BIT_DEPTH |
291 BUFFER_BIT_STENCIL |
292 BUFFER_BIT_ACCUM |
293 BUFFER_BIT_AUX0 |
294 BUFFER_BIT_AUX1 |
295 BUFFER_BIT_AUX2 |
296 BUFFER_BIT_AUX3;
297 assert((mask & (~legalBits)) == 0);
298 }
299 #endif
300
301 RENDER_START(swrast,ctx);
302
303 /* do software clearing here */
304 if (mask) {
305 if (mask & ctx->DrawBuffer->_ColorDrawBufferMask[0]) {
306 clear_color_buffers(ctx);
307 }
308 if (mask & BUFFER_BIT_DEPTH) {
309 _swrast_clear_depth_buffer(ctx, ctx->DrawBuffer->_DepthBuffer);
310 }
311 if (mask & BUFFER_BIT_ACCUM) {
312 _swrast_clear_accum_buffer(ctx,
313 ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer);
314 }
315 if (mask & BUFFER_BIT_STENCIL) {
316 _swrast_clear_stencil_buffer(ctx, ctx->DrawBuffer->_StencilBuffer);
317 }
318 }
319
320 RENDER_FINISH(swrast,ctx);
321 }