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