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