draw: corrections to allow for different cliptest cases
[mesa.git] / src / mesa / main / clear.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.1
4 *
5 * Copyright (C) 1999-2007 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
26 /**
27 * \file clear.c
28 * glClearColor, glClearIndex, glClear() functions.
29 */
30
31
32
33 #include "glheader.h"
34 #include "clear.h"
35 #include "context.h"
36 #include "colormac.h"
37 #include "enums.h"
38 #include "macros.h"
39 #include "state.h"
40
41
42
43 #if _HAVE_FULL_GL
44 void GLAPIENTRY
45 _mesa_ClearIndex( GLfloat c )
46 {
47 GET_CURRENT_CONTEXT(ctx);
48 ASSERT_OUTSIDE_BEGIN_END(ctx);
49
50 if (ctx->Color.ClearIndex == (GLuint) c)
51 return;
52
53 FLUSH_VERTICES(ctx, _NEW_COLOR);
54 ctx->Color.ClearIndex = (GLuint) c;
55 }
56 #endif
57
58
59 /**
60 * Specify the clear values for the color buffers.
61 *
62 * \param red red color component.
63 * \param green green color component.
64 * \param blue blue color component.
65 * \param alpha alpha component.
66 *
67 * \sa glClearColor().
68 *
69 * Clamps the parameters and updates gl_colorbuffer_attrib::ClearColor. On a
70 * change, flushes the vertices and notifies the driver via the
71 * dd_function_table::ClearColor callback.
72 */
73 void GLAPIENTRY
74 _mesa_ClearColor( GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha )
75 {
76 GLfloat tmp[4];
77 GET_CURRENT_CONTEXT(ctx);
78 ASSERT_OUTSIDE_BEGIN_END(ctx);
79
80 tmp[0] = CLAMP(red, 0.0F, 1.0F);
81 tmp[1] = CLAMP(green, 0.0F, 1.0F);
82 tmp[2] = CLAMP(blue, 0.0F, 1.0F);
83 tmp[3] = CLAMP(alpha, 0.0F, 1.0F);
84
85 if (TEST_EQ_4V(tmp, ctx->Color.ClearColor))
86 return; /* no change */
87
88 FLUSH_VERTICES(ctx, _NEW_COLOR);
89 COPY_4V(ctx->Color.ClearColor, tmp);
90
91 if (ctx->Driver.ClearColor) {
92 /* it's OK to call glClearColor in CI mode but it should be a NOP */
93 (*ctx->Driver.ClearColor)(ctx, ctx->Color.ClearColor);
94 }
95 }
96
97
98 /**
99 * Clear buffers.
100 *
101 * \param mask bit-mask indicating the buffers to be cleared.
102 *
103 * Flushes the vertices and verifies the parameter. If __GLcontextRec::NewState
104 * is set then calls _mesa_update_state() to update gl_frame_buffer::_Xmin,
105 * etc. If the rasterization mode is set to GL_RENDER then requests the driver
106 * to clear the buffers, via the dd_function_table::Clear callback.
107 */
108 void GLAPIENTRY
109 _mesa_Clear( GLbitfield mask )
110 {
111 GET_CURRENT_CONTEXT(ctx);
112 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
113
114 FLUSH_CURRENT(ctx, 0);
115
116 if (MESA_VERBOSE & VERBOSE_API)
117 _mesa_debug(ctx, "glClear 0x%x\n", mask);
118
119 if (mask & ~(GL_COLOR_BUFFER_BIT |
120 GL_DEPTH_BUFFER_BIT |
121 GL_STENCIL_BUFFER_BIT |
122 GL_ACCUM_BUFFER_BIT)) {
123 /* invalid bit set */
124 _mesa_error( ctx, GL_INVALID_VALUE, "glClear(0x%x)", mask);
125 return;
126 }
127
128 if (ctx->NewState) {
129 _mesa_update_state( ctx ); /* update _Xmin, etc */
130 }
131
132 if (ctx->DrawBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
133 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
134 "glClear(incomplete framebuffer)");
135 return;
136 }
137
138 if (ctx->DrawBuffer->Width == 0 || ctx->DrawBuffer->Height == 0 ||
139 ctx->DrawBuffer->_Xmin >= ctx->DrawBuffer->_Xmax ||
140 ctx->DrawBuffer->_Ymin >= ctx->DrawBuffer->_Ymax)
141 return;
142
143 if (ctx->RenderMode == GL_RENDER) {
144 GLbitfield bufferMask;
145
146 /* don't clear depth buffer if depth writing disabled */
147 if (!ctx->Depth.Mask)
148 mask &= ~GL_DEPTH_BUFFER_BIT;
149
150 /* Build the bitmask to send to device driver's Clear function.
151 * Note that the GL_COLOR_BUFFER_BIT flag will expand to 0, 1, 2 or 4
152 * of the BUFFER_BIT_FRONT/BACK_LEFT/RIGHT flags, or one of the
153 * BUFFER_BIT_COLORn flags.
154 */
155 bufferMask = 0;
156 if (mask & GL_COLOR_BUFFER_BIT) {
157 GLuint i;
158 for (i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; i++) {
159 bufferMask |= (1 << ctx->DrawBuffer->_ColorDrawBufferIndexes[i]);
160 }
161 }
162
163 if ((mask & GL_DEPTH_BUFFER_BIT)
164 && ctx->DrawBuffer->Visual.haveDepthBuffer) {
165 bufferMask |= BUFFER_BIT_DEPTH;
166 }
167
168 if ((mask & GL_STENCIL_BUFFER_BIT)
169 && ctx->DrawBuffer->Visual.haveStencilBuffer) {
170 bufferMask |= BUFFER_BIT_STENCIL;
171 }
172
173 if ((mask & GL_ACCUM_BUFFER_BIT)
174 && ctx->DrawBuffer->Visual.haveAccumBuffer) {
175 bufferMask |= BUFFER_BIT_ACCUM;
176 }
177
178 ASSERT(ctx->Driver.Clear);
179 ctx->Driver.Clear(ctx, bufferMask);
180 }
181 }
182
183
184 /** Returned by make_color_buffer_mask() for errors */
185 #define INVALID_MASK ~0x0
186
187
188 /**
189 * Convert the glClearBuffer 'drawbuffer' parameter into a bitmask of
190 * BUFFER_BIT_x values.
191 * Return INVALID_MASK if the drawbuffer value is invalid.
192 */
193 static GLbitfield
194 make_color_buffer_mask(GLcontext *ctx, GLint drawbuffer)
195 {
196 const struct gl_renderbuffer_attachment *att = ctx->DrawBuffer->Attachment;
197 GLbitfield mask = 0x0;
198
199 switch (drawbuffer) {
200 case GL_FRONT:
201 if (att[BUFFER_FRONT_LEFT].Renderbuffer)
202 mask |= BUFFER_BIT_FRONT_LEFT;
203 if (att[BUFFER_FRONT_RIGHT].Renderbuffer)
204 mask |= BUFFER_BIT_FRONT_RIGHT;
205 break;
206 case GL_BACK:
207 if (att[BUFFER_BACK_LEFT].Renderbuffer)
208 mask |= BUFFER_BIT_BACK_LEFT;
209 if (att[BUFFER_BACK_RIGHT].Renderbuffer)
210 mask |= BUFFER_BIT_BACK_RIGHT;
211 break;
212 case GL_LEFT:
213 if (att[BUFFER_FRONT_LEFT].Renderbuffer)
214 mask |= BUFFER_BIT_FRONT_LEFT;
215 if (att[BUFFER_BACK_LEFT].Renderbuffer)
216 mask |= BUFFER_BIT_BACK_LEFT;
217 break;
218 case GL_RIGHT:
219 if (att[BUFFER_FRONT_RIGHT].Renderbuffer)
220 mask |= BUFFER_BIT_FRONT_RIGHT;
221 if (att[BUFFER_BACK_RIGHT].Renderbuffer)
222 mask |= BUFFER_BIT_BACK_RIGHT;
223 break;
224 case GL_FRONT_AND_BACK:
225 if (att[BUFFER_FRONT_LEFT].Renderbuffer)
226 mask |= BUFFER_BIT_FRONT_LEFT;
227 if (att[BUFFER_BACK_LEFT].Renderbuffer)
228 mask |= BUFFER_BIT_BACK_LEFT;
229 if (att[BUFFER_FRONT_RIGHT].Renderbuffer)
230 mask |= BUFFER_BIT_FRONT_RIGHT;
231 if (att[BUFFER_BACK_RIGHT].Renderbuffer)
232 mask |= BUFFER_BIT_BACK_RIGHT;
233 break;
234 default:
235 if (drawbuffer < 0 || drawbuffer >= (GLint)ctx->Const.MaxDrawBuffers) {
236 mask = INVALID_MASK;
237 }
238 else if (att[BUFFER_COLOR0 + drawbuffer].Renderbuffer) {
239 mask |= (BUFFER_BIT_COLOR0 << drawbuffer);
240 }
241 }
242
243 return mask;
244 }
245
246
247
248 /**
249 * New in GL 3.0
250 * Clear signed integer color buffer or stencil buffer (not depth).
251 */
252 void GLAPIENTRY
253 _mesa_ClearBufferiv(GLenum buffer, GLint drawbuffer, const GLint *value)
254 {
255 GET_CURRENT_CONTEXT(ctx);
256 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
257
258 FLUSH_CURRENT(ctx, 0);
259
260 if (ctx->NewState) {
261 _mesa_update_state( ctx );
262 }
263
264 switch (buffer) {
265 case GL_STENCIL:
266 if (drawbuffer != 0) {
267 _mesa_error(ctx, GL_INVALID_VALUE, "glClearBufferiv(drawbuffer=%d)",
268 drawbuffer);
269 return;
270 }
271 else {
272 /* Save current stencil clear value, set to 'value', do the
273 * stencil clear and restore the clear value.
274 * XXX in the future we may have a new ctx->Driver.ClearBuffer()
275 * hook instead.
276 */
277 const GLuint clearSave = ctx->Stencil.Clear;
278 ctx->Stencil.Clear = *value;
279 if (ctx->Driver.ClearStencil)
280 ctx->Driver.ClearStencil(ctx, *value);
281 ctx->Driver.Clear(ctx, BUFFER_BIT_STENCIL);
282 ctx->Stencil.Clear = clearSave;
283 if (ctx->Driver.ClearStencil)
284 ctx->Driver.ClearStencil(ctx, clearSave);
285 }
286 break;
287 case GL_COLOR:
288 {
289 const GLbitfield mask = make_color_buffer_mask(ctx, drawbuffer);
290 if (mask == INVALID_MASK) {
291 _mesa_error(ctx, GL_INVALID_VALUE, "glClearBufferiv(drawbuffer=%d)",
292 drawbuffer);
293 return;
294 }
295 else if (mask) {
296 /* XXX note: we're putting the integer clear values into the
297 * floating point state var. This will not always work. We'll
298 * need a new ctx->Driver.ClearBuffer() hook....
299 */
300 GLclampf clearSave[4];
301 /* save color */
302 COPY_4V(clearSave, ctx->Color.ClearColor);
303 /* set color */
304 COPY_4V_CAST(ctx->Color.ClearColor, value, GLclampf);
305 if (ctx->Driver.ClearColor)
306 ctx->Driver.ClearColor(ctx, ctx->Color.ClearColor);
307 /* clear buffer(s) */
308 ctx->Driver.Clear(ctx, mask);
309 /* restore color */
310 COPY_4V(ctx->Color.ClearColor, clearSave);
311 if (ctx->Driver.ClearColor)
312 ctx->Driver.ClearColor(ctx, clearSave);
313 }
314 }
315 break;
316 default:
317 _mesa_error(ctx, GL_INVALID_ENUM, "glClearBufferiv(buffer=%s)",
318 _mesa_lookup_enum_by_nr(buffer));
319 return;
320 }
321 }
322
323
324 /**
325 * New in GL 3.0
326 * Clear unsigned integer color buffer (not depth, not stencil).
327 */
328 void GLAPIENTRY
329 _mesa_ClearBufferuiv(GLenum buffer, GLint drawbuffer, const GLuint *value)
330 {
331 GET_CURRENT_CONTEXT(ctx);
332 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
333
334 FLUSH_CURRENT(ctx, 0);
335
336 if (ctx->NewState) {
337 _mesa_update_state( ctx );
338 }
339
340 switch (buffer) {
341 case GL_COLOR:
342 {
343 const GLbitfield mask = make_color_buffer_mask(ctx, drawbuffer);
344 if (mask == INVALID_MASK) {
345 _mesa_error(ctx, GL_INVALID_VALUE, "glClearBufferiv(drawbuffer=%d)",
346 drawbuffer);
347 return;
348 }
349 else if (mask) {
350 /* XXX note: we're putting the uint clear values into the
351 * floating point state var. This will not always work. We'll
352 * need a new ctx->Driver.ClearBuffer() hook....
353 */
354 GLclampf clearSave[4];
355 /* save color */
356 COPY_4V(clearSave, ctx->Color.ClearColor);
357 /* set color */
358 COPY_4V_CAST(ctx->Color.ClearColor, value, GLclampf);
359 if (ctx->Driver.ClearColor)
360 ctx->Driver.ClearColor(ctx, ctx->Color.ClearColor);
361 /* clear buffer(s) */
362 ctx->Driver.Clear(ctx, mask);
363 /* restore color */
364 COPY_4V(ctx->Color.ClearColor, clearSave);
365 if (ctx->Driver.ClearColor)
366 ctx->Driver.ClearColor(ctx, clearSave);
367 }
368 }
369 break;
370 default:
371 _mesa_error(ctx, GL_INVALID_ENUM, "glClearBufferuiv(buffer=%s)",
372 _mesa_lookup_enum_by_nr(buffer));
373 return;
374 }
375 }
376
377
378 /**
379 * New in GL 3.0
380 * Clear fixed-pt or float color buffer or depth buffer (not stencil).
381 */
382 void GLAPIENTRY
383 _mesa_ClearBufferfv(GLenum buffer, GLint drawbuffer, const GLfloat *value)
384 {
385 GET_CURRENT_CONTEXT(ctx);
386 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
387
388 FLUSH_CURRENT(ctx, 0);
389
390 if (ctx->NewState) {
391 _mesa_update_state( ctx );
392 }
393
394 switch (buffer) {
395 case GL_DEPTH:
396 if (drawbuffer != 0) {
397 _mesa_error(ctx, GL_INVALID_VALUE, "glClearBufferfv(drawbuffer=%d)",
398 drawbuffer);
399 return;
400 }
401 else {
402 /* Save current depth clear value, set to 'value', do the
403 * depth clear and restore the clear value.
404 * XXX in the future we may have a new ctx->Driver.ClearBuffer()
405 * hook instead.
406 */
407 const GLclampd clearSave = ctx->Depth.Clear;
408 ctx->Depth.Clear = *value;
409 if (ctx->Driver.ClearDepth)
410 ctx->Driver.ClearDepth(ctx, *value);
411 ctx->Driver.Clear(ctx, BUFFER_BIT_DEPTH);
412 ctx->Depth.Clear = clearSave;
413 if (ctx->Driver.ClearDepth)
414 ctx->Driver.ClearDepth(ctx, clearSave);
415 }
416 /* clear depth buffer to value */
417 break;
418 case GL_COLOR:
419 {
420 const GLbitfield mask = make_color_buffer_mask(ctx, drawbuffer);
421 if (mask == INVALID_MASK) {
422 _mesa_error(ctx, GL_INVALID_VALUE, "glClearBufferfv(drawbuffer=%d)",
423 drawbuffer);
424 return;
425 }
426 else if (mask) {
427 GLclampf clearSave[4];
428 /* save color */
429 COPY_4V(clearSave, ctx->Color.ClearColor);
430 /* set color */
431 COPY_4V_CAST(ctx->Color.ClearColor, value, GLclampf);
432 if (ctx->Driver.ClearColor)
433 ctx->Driver.ClearColor(ctx, ctx->Color.ClearColor);
434 /* clear buffer(s) */
435 ctx->Driver.Clear(ctx, mask);
436 /* restore color */
437 COPY_4V(ctx->Color.ClearColor, clearSave);
438 if (ctx->Driver.ClearColor)
439 ctx->Driver.ClearColor(ctx, clearSave);
440 }
441 }
442 break;
443 default:
444 _mesa_error(ctx, GL_INVALID_ENUM, "glClearBufferfv(buffer=%s)",
445 _mesa_lookup_enum_by_nr(buffer));
446 return;
447 }
448 }
449
450
451 /**
452 * New in GL 3.0
453 * Clear depth/stencil buffer only.
454 */
455 void GLAPIENTRY
456 _mesa_ClearBufferfi(GLenum buffer, GLint drawbuffer,
457 GLfloat depth, GLint stencil)
458 {
459 GET_CURRENT_CONTEXT(ctx);
460 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
461
462 FLUSH_CURRENT(ctx, 0);
463
464 if (buffer != GL_DEPTH_STENCIL) {
465 _mesa_error(ctx, GL_INVALID_ENUM, "glClearBufferfi(buffer=%s)",
466 _mesa_lookup_enum_by_nr(buffer));
467 return;
468 }
469
470 if (drawbuffer != 0) {
471 _mesa_error(ctx, GL_INVALID_VALUE, "glClearBufferfi(drawbuffer=%d)",
472 drawbuffer);
473 return;
474 }
475
476 if (ctx->NewState) {
477 _mesa_update_state( ctx );
478 }
479
480 {
481 /* save current clear values */
482 const GLclampd clearDepthSave = ctx->Depth.Clear;
483 const GLuint clearStencilSave = ctx->Stencil.Clear;
484
485 /* set new clear values */
486 ctx->Depth.Clear = depth;
487 ctx->Stencil.Clear = stencil;
488 if (ctx->Driver.ClearDepth)
489 ctx->Driver.ClearDepth(ctx, depth);
490 if (ctx->Driver.ClearStencil)
491 ctx->Driver.ClearStencil(ctx, stencil);
492
493 /* clear buffers */
494 ctx->Driver.Clear(ctx, BUFFER_BIT_DEPTH | BUFFER_BIT_STENCIL);
495
496 /* restore */
497 ctx->Depth.Clear = clearDepthSave;
498 ctx->Stencil.Clear = clearStencilSave;
499 if (ctx->Driver.ClearDepth)
500 ctx->Driver.ClearDepth(ctx, clearDepthSave);
501 if (ctx->Driver.ClearStencil)
502 ctx->Driver.ClearStencil(ctx, clearStencilSave);
503 }
504 }