Remove x/y/width/height parameters from Clear functions.
[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 COPY_4V(span.array->rgba[i], ctx->Color.ClearColor);
84 }
85 }
86
87 /* Note that masking will change the color values, but only the
88 * channels for which the write mask is GL_FALSE. The channels
89 * which which are write-enabled won't get modified.
90 */
91 for (i = 0; i < height; i++) {
92 span.x = x;
93 span.y = y + i;
94 _swrast_mask_rgba_span(ctx, rb, &span);
95 /* write masked row */
96 rb->PutRow(ctx, rb, width, x, y + i, span.array->rgba, NULL);
97 }
98 }
99
100
101 /**
102 * Clear color index buffer with masking.
103 */
104 static void
105 clear_ci_buffer_with_masking(GLcontext *ctx, struct gl_renderbuffer *rb)
106 {
107 const GLint x = ctx->DrawBuffer->_Xmin;
108 const GLint y = ctx->DrawBuffer->_Ymin;
109 const GLint height = ctx->DrawBuffer->_Ymax - ctx->DrawBuffer->_Ymin;
110 const GLint width = ctx->DrawBuffer->_Xmax - ctx->DrawBuffer->_Xmin;
111 SWspan span;
112 GLint i;
113
114 ASSERT(!ctx->Visual.rgbMode);
115 ASSERT(rb->PutRow);
116 ASSERT(rb->DataType == GL_UNSIGNED_INT);
117
118 /* Initialize index span with clear index */
119 INIT_SPAN(span, GL_BITMAP, width, 0, SPAN_RGBA);
120 for (i = 0; i < width;i++) {
121 span.array->index[i] = ctx->Color.ClearIndex;
122 }
123
124 /* Note that masking will change the color indexes, but only the
125 * bits for which the write mask is GL_FALSE. The bits
126 * which are write-enabled won't get modified.
127 */
128 for (i = 0; i < height;i++) {
129 span.x = x;
130 span.y = y + i;
131 _swrast_mask_ci_span(ctx, rb, &span);
132 /* write masked row */
133 rb->PutRow(ctx, rb, width, x, y + i, span.array->index, NULL);
134 }
135 }
136
137
138 /**
139 * Clear an rgba color buffer without channel masking.
140 */
141 static void
142 clear_rgba_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
143 {
144 const GLint x = ctx->DrawBuffer->_Xmin;
145 const GLint y = ctx->DrawBuffer->_Ymin;
146 const GLint height = ctx->DrawBuffer->_Ymax - ctx->DrawBuffer->_Ymin;
147 const GLint width = ctx->DrawBuffer->_Xmax - ctx->DrawBuffer->_Xmin;
148 GLubyte clear8[4];
149 GLushort clear16[4];
150 GLvoid *clearVal;
151 GLint i;
152
153 ASSERT(ctx->Visual.rgbMode);
154
155 ASSERT(ctx->Color.ColorMask[0] &&
156 ctx->Color.ColorMask[1] &&
157 ctx->Color.ColorMask[2] &&
158 ctx->Color.ColorMask[3]);
159
160 ASSERT(rb->PutMonoRow);
161
162 switch (rb->DataType) {
163 case GL_UNSIGNED_BYTE:
164 UNCLAMPED_FLOAT_TO_UBYTE(clear8[0], ctx->Color.ClearColor[0]);
165 UNCLAMPED_FLOAT_TO_UBYTE(clear8[1], ctx->Color.ClearColor[1]);
166 UNCLAMPED_FLOAT_TO_UBYTE(clear8[2], ctx->Color.ClearColor[2]);
167 UNCLAMPED_FLOAT_TO_UBYTE(clear8[3], ctx->Color.ClearColor[3]);
168 clearVal = clear8;
169 break;
170 case GL_UNSIGNED_SHORT:
171 UNCLAMPED_FLOAT_TO_USHORT(clear16[0], ctx->Color.ClearColor[0]);
172 UNCLAMPED_FLOAT_TO_USHORT(clear16[1], ctx->Color.ClearColor[1]);
173 UNCLAMPED_FLOAT_TO_USHORT(clear16[2], ctx->Color.ClearColor[2]);
174 UNCLAMPED_FLOAT_TO_USHORT(clear16[3], ctx->Color.ClearColor[3]);
175 clearVal = clear16;
176 break;
177 case GL_FLOAT:
178 clearVal = ctx->Color.ClearColor;
179 break;
180 default:
181 _mesa_problem(ctx, "Bad rb DataType in clear_color_buffer");
182 return;
183 }
184
185 for (i = 0; i < height; i++) {
186 rb->PutMonoRow(ctx, rb, width, x, y + i, clearVal, NULL);
187 }
188 }
189
190
191 /**
192 * Clear color index buffer without masking.
193 */
194 static void
195 clear_ci_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
196 {
197 const GLint x = ctx->DrawBuffer->_Xmin;
198 const GLint y = ctx->DrawBuffer->_Ymin;
199 const GLint height = ctx->DrawBuffer->_Ymax - ctx->DrawBuffer->_Ymin;
200 const GLint width = ctx->DrawBuffer->_Xmax - ctx->DrawBuffer->_Xmin;
201 GLubyte clear8;
202 GLushort clear16;
203 GLuint clear32;
204 GLvoid *clearVal;
205 GLint i;
206
207 ASSERT(!ctx->Visual.rgbMode);
208
209 ASSERT((ctx->Color.IndexMask & ((1 << rb->IndexBits) - 1))
210 == (GLuint) ((1 << rb->IndexBits) - 1));
211
212 ASSERT(rb->PutMonoRow);
213
214 /* setup clear value */
215 switch (rb->DataType) {
216 case GL_UNSIGNED_BYTE:
217 clear8 = (GLubyte) ctx->Color.ClearIndex;
218 clearVal = &clear8;
219 break;
220 case GL_UNSIGNED_SHORT:
221 clear16 = (GLushort) ctx->Color.ClearIndex;
222 clearVal = &clear16;
223 break;
224 case GL_UNSIGNED_INT:
225 clear32 = ctx->Color.ClearIndex;
226 clearVal = &clear32;
227 break;
228 default:
229 _mesa_problem(ctx, "Bad rb DataType in clear_color_buffer");
230 return;
231 }
232
233 for (i = 0; i < height; i++)
234 rb->PutMonoRow(ctx, rb, width, x, y + i, clearVal, NULL);
235 }
236
237
238 /**
239 * Clear the front/back/left/right/aux color buffers.
240 * This function is usually only called if the device driver can't
241 * clear its own color buffers for some reason (such as with masking).
242 */
243 static void
244 clear_color_buffers(GLcontext *ctx)
245 {
246 GLboolean masking;
247 GLuint i;
248
249 if (ctx->Visual.rgbMode) {
250 if (ctx->Color.ColorMask[0] &&
251 ctx->Color.ColorMask[1] &&
252 ctx->Color.ColorMask[2] &&
253 ctx->Color.ColorMask[3]) {
254 masking = GL_FALSE;
255 }
256 else {
257 masking = GL_TRUE;
258 }
259 }
260 else {
261 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0][0];
262 const GLuint indexBits = (1 << rb->IndexBits) - 1;
263 if ((ctx->Color.IndexMask & indexBits) == indexBits) {
264 masking = GL_FALSE;
265 }
266 else {
267 masking = GL_TRUE;
268 }
269 }
270
271 for (i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers[0]; i++) {
272 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0][i];
273 if (ctx->Visual.rgbMode) {
274 if (masking) {
275 clear_rgba_buffer_with_masking(ctx, rb);
276 }
277 else {
278 clear_rgba_buffer(ctx, rb);
279 }
280 }
281 else {
282 if (masking) {
283 clear_ci_buffer_with_masking(ctx, rb);
284 }
285 else {
286 clear_ci_buffer(ctx, rb);
287 }
288 }
289 }
290 }
291
292
293 /**
294 * Called via the device driver's ctx->Driver.Clear() function if the
295 * device driver can't clear one or more of the buffers itself.
296 * \param buffers bitfield of BUFFER_BIT_* values indicating which
297 * renderbuffers are to be cleared.
298 * \param all if GL_TRUE, clear whole buffer, else clear specified region.
299 */
300 void
301 _swrast_Clear(GLcontext *ctx, GLbitfield buffers)
302 {
303 SWcontext *swrast = SWRAST_CONTEXT(ctx);
304
305 #ifdef DEBUG_FOO
306 {
307 const GLbitfield legalBits =
308 BUFFER_BIT_FRONT_LEFT |
309 BUFFER_BIT_FRONT_RIGHT |
310 BUFFER_BIT_BACK_LEFT |
311 BUFFER_BIT_BACK_RIGHT |
312 BUFFER_BIT_DEPTH |
313 BUFFER_BIT_STENCIL |
314 BUFFER_BIT_ACCUM |
315 BUFFER_BIT_AUX0 |
316 BUFFER_BIT_AUX1 |
317 BUFFER_BIT_AUX2 |
318 BUFFER_BIT_AUX3;
319 assert((buffers & (~legalBits)) == 0);
320 }
321 #endif
322
323 RENDER_START(swrast,ctx);
324
325 /* do software clearing here */
326 if (buffers) {
327 if (buffers & ctx->DrawBuffer->_ColorDrawBufferMask[0]) {
328 clear_color_buffers(ctx);
329 }
330 if (buffers & BUFFER_BIT_DEPTH) {
331 _swrast_clear_depth_buffer(ctx, ctx->DrawBuffer->_DepthBuffer);
332 }
333 if (buffers & BUFFER_BIT_ACCUM) {
334 _swrast_clear_accum_buffer(ctx,
335 ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer);
336 }
337 if (buffers & BUFFER_BIT_STENCIL) {
338 _swrast_clear_stencil_buffer(ctx, ctx->DrawBuffer->_StencilBuffer);
339 }
340 }
341
342 RENDER_FINISH(swrast,ctx);
343 }