mesa: optimize initialization of new VAOs
[mesa.git] / src / mesa / main / clear.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * 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 "enums.h"
37 #include "fbobject.h"
38 #include "get.h"
39 #include "macros.h"
40 #include "mtypes.h"
41 #include "state.h"
42
43
44
45 void GLAPIENTRY
46 _mesa_ClearIndex( GLfloat c )
47 {
48 GET_CURRENT_CONTEXT(ctx);
49
50 ctx->Color.ClearIndex = (GLuint) c;
51 }
52
53
54 /**
55 * Specify the clear values for the color buffers.
56 *
57 * \param red red color component.
58 * \param green green color component.
59 * \param blue blue color component.
60 * \param alpha alpha component.
61 *
62 * \sa glClearColor().
63 */
64 void GLAPIENTRY
65 _mesa_ClearColor( GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha )
66 {
67 GET_CURRENT_CONTEXT(ctx);
68
69 ctx->Color.ClearColor.f[0] = red;
70 ctx->Color.ClearColor.f[1] = green;
71 ctx->Color.ClearColor.f[2] = blue;
72 ctx->Color.ClearColor.f[3] = alpha;
73 }
74
75
76 /**
77 * GL_EXT_texture_integer
78 */
79 void GLAPIENTRY
80 _mesa_ClearColorIiEXT(GLint r, GLint g, GLint b, GLint a)
81 {
82 GET_CURRENT_CONTEXT(ctx);
83
84 ctx->Color.ClearColor.i[0] = r;
85 ctx->Color.ClearColor.i[1] = g;
86 ctx->Color.ClearColor.i[2] = b;
87 ctx->Color.ClearColor.i[3] = a;
88 }
89
90
91 /**
92 * GL_EXT_texture_integer
93 */
94 void GLAPIENTRY
95 _mesa_ClearColorIuiEXT(GLuint r, GLuint g, GLuint b, GLuint a)
96 {
97 GET_CURRENT_CONTEXT(ctx);
98
99 ctx->Color.ClearColor.ui[0] = r;
100 ctx->Color.ClearColor.ui[1] = g;
101 ctx->Color.ClearColor.ui[2] = b;
102 ctx->Color.ClearColor.ui[3] = a;
103 }
104
105
106 /**
107 * Returns true if color writes are enabled for the given color attachment.
108 *
109 * Beyond checking ColorMask, this uses _mesa_format_has_color_component to
110 * ignore components that don't actually exist in the format (such as X in
111 * XRGB).
112 */
113 static bool
114 color_buffer_writes_enabled(const struct gl_context *ctx, unsigned idx)
115 {
116 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[idx];
117 GLuint c;
118
119 if (rb) {
120 for (c = 0; c < 4; c++) {
121 if (GET_COLORMASK_BIT(ctx->Color.ColorMask, idx, c) &&
122 _mesa_format_has_color_component(rb->Format, c)) {
123 return true;
124 }
125 }
126 }
127
128 return false;
129 }
130
131
132 /**
133 * Clear buffers.
134 *
135 * \param mask bit-mask indicating the buffers to be cleared.
136 *
137 * Flushes the vertices and verifies the parameter.
138 * If __struct gl_contextRec::NewState is set then calls _mesa_update_state()
139 * to update gl_frame_buffer::_Xmin, etc. If the rasterization mode is set to
140 * GL_RENDER then requests the driver to clear the buffers, via the
141 * dd_function_table::Clear callback.
142 */
143 static ALWAYS_INLINE void
144 clear(struct gl_context *ctx, GLbitfield mask, bool no_error)
145 {
146 FLUSH_VERTICES(ctx, 0);
147
148 if (!no_error) {
149 if (mask & ~(GL_COLOR_BUFFER_BIT |
150 GL_DEPTH_BUFFER_BIT |
151 GL_STENCIL_BUFFER_BIT |
152 GL_ACCUM_BUFFER_BIT)) {
153 _mesa_error( ctx, GL_INVALID_VALUE, "glClear(0x%x)", mask);
154 return;
155 }
156
157 /* Accumulation buffers were removed in core contexts, and they never
158 * existed in OpenGL ES.
159 */
160 if ((mask & GL_ACCUM_BUFFER_BIT) != 0
161 && (ctx->API == API_OPENGL_CORE || _mesa_is_gles(ctx))) {
162 _mesa_error( ctx, GL_INVALID_VALUE, "glClear(GL_ACCUM_BUFFER_BIT)");
163 return;
164 }
165 }
166
167 if (ctx->NewState) {
168 _mesa_update_state( ctx ); /* update _Xmin, etc */
169 }
170
171 if (!no_error && ctx->DrawBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
172 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
173 "glClear(incomplete framebuffer)");
174 return;
175 }
176
177 if (ctx->RasterDiscard)
178 return;
179
180 if (ctx->RenderMode == GL_RENDER) {
181 GLbitfield bufferMask;
182
183 /* don't clear depth buffer if depth writing disabled */
184 if (!ctx->Depth.Mask)
185 mask &= ~GL_DEPTH_BUFFER_BIT;
186
187 /* Build the bitmask to send to device driver's Clear function.
188 * Note that the GL_COLOR_BUFFER_BIT flag will expand to 0, 1, 2 or 4
189 * of the BUFFER_BIT_FRONT/BACK_LEFT/RIGHT flags, or one of the
190 * BUFFER_BIT_COLORn flags.
191 */
192 bufferMask = 0;
193 if (mask & GL_COLOR_BUFFER_BIT) {
194 GLuint i;
195 for (i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; i++) {
196 gl_buffer_index buf = ctx->DrawBuffer->_ColorDrawBufferIndexes[i];
197
198 if (buf != BUFFER_NONE && color_buffer_writes_enabled(ctx, i)) {
199 bufferMask |= 1 << buf;
200 }
201 }
202 }
203
204 if ((mask & GL_DEPTH_BUFFER_BIT)
205 && ctx->DrawBuffer->Visual.depthBits > 0) {
206 bufferMask |= BUFFER_BIT_DEPTH;
207 }
208
209 if ((mask & GL_STENCIL_BUFFER_BIT)
210 && ctx->DrawBuffer->Visual.stencilBits > 0) {
211 bufferMask |= BUFFER_BIT_STENCIL;
212 }
213
214 if ((mask & GL_ACCUM_BUFFER_BIT)
215 && ctx->DrawBuffer->Visual.accumRedBits > 0) {
216 bufferMask |= BUFFER_BIT_ACCUM;
217 }
218
219 assert(ctx->Driver.Clear);
220 ctx->Driver.Clear(ctx, bufferMask);
221 }
222 }
223
224
225 void GLAPIENTRY
226 _mesa_Clear_no_error(GLbitfield mask)
227 {
228 GET_CURRENT_CONTEXT(ctx);
229 clear(ctx, mask, true);
230 }
231
232
233 void GLAPIENTRY
234 _mesa_Clear(GLbitfield mask)
235 {
236 GET_CURRENT_CONTEXT(ctx);
237
238 if (MESA_VERBOSE & VERBOSE_API)
239 _mesa_debug(ctx, "glClear 0x%x\n", mask);
240
241 clear(ctx, mask, false);
242 }
243
244
245 /** Returned by make_color_buffer_mask() for errors */
246 #define INVALID_MASK ~0x0U
247
248
249 /**
250 * Convert the glClearBuffer 'drawbuffer' parameter into a bitmask of
251 * BUFFER_BIT_x values.
252 * Return INVALID_MASK if the drawbuffer value is invalid.
253 */
254 static GLbitfield
255 make_color_buffer_mask(struct gl_context *ctx, GLint drawbuffer)
256 {
257 const struct gl_renderbuffer_attachment *att = ctx->DrawBuffer->Attachment;
258 GLbitfield mask = 0x0;
259
260 /* From the GL 4.0 specification:
261 * If buffer is COLOR, a particular draw buffer DRAW_BUFFERi is
262 * specified by passing i as the parameter drawbuffer, and value
263 * points to a four-element vector specifying the R, G, B, and A
264 * color to clear that draw buffer to. If the draw buffer is one
265 * of FRONT, BACK, LEFT, RIGHT, or FRONT_AND_BACK, identifying
266 * multiple buffers, each selected buffer is cleared to the same
267 * value.
268 *
269 * Note that "drawbuffer" and "draw buffer" have different meaning.
270 * "drawbuffer" specifies DRAW_BUFFERi, while "draw buffer" is what's
271 * assigned to DRAW_BUFFERi. It could be COLOR_ATTACHMENT0, FRONT, BACK,
272 * etc.
273 */
274 if (drawbuffer < 0 || drawbuffer >= (GLint)ctx->Const.MaxDrawBuffers) {
275 return INVALID_MASK;
276 }
277
278 switch (ctx->DrawBuffer->ColorDrawBuffer[drawbuffer]) {
279 case GL_FRONT:
280 if (att[BUFFER_FRONT_LEFT].Renderbuffer)
281 mask |= BUFFER_BIT_FRONT_LEFT;
282 if (att[BUFFER_FRONT_RIGHT].Renderbuffer)
283 mask |= BUFFER_BIT_FRONT_RIGHT;
284 break;
285 case GL_BACK:
286 /* For GLES contexts with a single buffered configuration, we actually
287 * only have a front renderbuffer, so any clear calls to GL_BACK should
288 * affect that buffer. See draw_buffer_enum_to_bitmask for details.
289 */
290 if (_mesa_is_gles(ctx))
291 if (!ctx->DrawBuffer->Visual.doubleBufferMode)
292 if (att[BUFFER_FRONT_LEFT].Renderbuffer)
293 mask |= BUFFER_BIT_FRONT_LEFT;
294 if (att[BUFFER_BACK_LEFT].Renderbuffer)
295 mask |= BUFFER_BIT_BACK_LEFT;
296 if (att[BUFFER_BACK_RIGHT].Renderbuffer)
297 mask |= BUFFER_BIT_BACK_RIGHT;
298 break;
299 case GL_LEFT:
300 if (att[BUFFER_FRONT_LEFT].Renderbuffer)
301 mask |= BUFFER_BIT_FRONT_LEFT;
302 if (att[BUFFER_BACK_LEFT].Renderbuffer)
303 mask |= BUFFER_BIT_BACK_LEFT;
304 break;
305 case GL_RIGHT:
306 if (att[BUFFER_FRONT_RIGHT].Renderbuffer)
307 mask |= BUFFER_BIT_FRONT_RIGHT;
308 if (att[BUFFER_BACK_RIGHT].Renderbuffer)
309 mask |= BUFFER_BIT_BACK_RIGHT;
310 break;
311 case GL_FRONT_AND_BACK:
312 if (att[BUFFER_FRONT_LEFT].Renderbuffer)
313 mask |= BUFFER_BIT_FRONT_LEFT;
314 if (att[BUFFER_BACK_LEFT].Renderbuffer)
315 mask |= BUFFER_BIT_BACK_LEFT;
316 if (att[BUFFER_FRONT_RIGHT].Renderbuffer)
317 mask |= BUFFER_BIT_FRONT_RIGHT;
318 if (att[BUFFER_BACK_RIGHT].Renderbuffer)
319 mask |= BUFFER_BIT_BACK_RIGHT;
320 break;
321 default:
322 {
323 gl_buffer_index buf =
324 ctx->DrawBuffer->_ColorDrawBufferIndexes[drawbuffer];
325
326 if (buf != BUFFER_NONE && att[buf].Renderbuffer) {
327 mask |= 1 << buf;
328 }
329 }
330 }
331
332 return mask;
333 }
334
335
336
337 /**
338 * New in GL 3.0
339 * Clear signed integer color buffer or stencil buffer (not depth).
340 */
341 static ALWAYS_INLINE void
342 clear_bufferiv(struct gl_context *ctx, GLenum buffer, GLint drawbuffer,
343 const GLint *value, bool no_error)
344 {
345 FLUSH_VERTICES(ctx, 0);
346
347 if (ctx->NewState) {
348 _mesa_update_state( ctx );
349 }
350
351 if (!no_error && ctx->DrawBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
352 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
353 "glClearBufferiv(incomplete framebuffer)");
354 return;
355 }
356
357 switch (buffer) {
358 case GL_STENCIL:
359 /* Page 264 (page 280 of the PDF) of the OpenGL 3.0 spec says:
360 *
361 * "ClearBuffer generates an INVALID VALUE error if buffer is
362 * COLOR and drawbuffer is less than zero, or greater than the
363 * value of MAX DRAW BUFFERS minus one; or if buffer is DEPTH,
364 * STENCIL, or DEPTH STENCIL and drawbuffer is not zero."
365 */
366 if (!no_error && drawbuffer != 0) {
367 _mesa_error(ctx, GL_INVALID_VALUE, "glClearBufferiv(drawbuffer=%d)",
368 drawbuffer);
369 return;
370 }
371 else if (ctx->DrawBuffer->Attachment[BUFFER_STENCIL].Renderbuffer
372 && !ctx->RasterDiscard) {
373 /* Save current stencil clear value, set to 'value', do the
374 * stencil clear and restore the clear value.
375 * XXX in the future we may have a new ctx->Driver.ClearBuffer()
376 * hook instead.
377 */
378 const GLuint clearSave = ctx->Stencil.Clear;
379 ctx->Stencil.Clear = *value;
380 ctx->Driver.Clear(ctx, BUFFER_BIT_STENCIL);
381 ctx->Stencil.Clear = clearSave;
382 }
383 break;
384 case GL_COLOR:
385 {
386 const GLbitfield mask = make_color_buffer_mask(ctx, drawbuffer);
387 if (!no_error && mask == INVALID_MASK) {
388 _mesa_error(ctx, GL_INVALID_VALUE, "glClearBufferiv(drawbuffer=%d)",
389 drawbuffer);
390 return;
391 }
392 else if (mask && !ctx->RasterDiscard) {
393 union gl_color_union clearSave;
394
395 /* save color */
396 clearSave = ctx->Color.ClearColor;
397 /* set color */
398 COPY_4V(ctx->Color.ClearColor.i, value);
399 /* clear buffer(s) */
400 ctx->Driver.Clear(ctx, mask);
401 /* restore color */
402 ctx->Color.ClearColor = clearSave;
403 }
404 }
405 break;
406 default:
407 if (!no_error) {
408 /* Page 498 of the PDF, section '17.4.3.1 Clearing Individual Buffers'
409 * of the OpenGL 4.5 spec states:
410 *
411 * "An INVALID_ENUM error is generated by ClearBufferiv and
412 * ClearNamedFramebufferiv if buffer is not COLOR or STENCIL."
413 */
414 _mesa_error(ctx, GL_INVALID_ENUM, "glClearBufferiv(buffer=%s)",
415 _mesa_enum_to_string(buffer));
416 }
417 return;
418 }
419 }
420
421
422 void GLAPIENTRY
423 _mesa_ClearBufferiv_no_error(GLenum buffer, GLint drawbuffer, const GLint *value)
424 {
425 GET_CURRENT_CONTEXT(ctx);
426 clear_bufferiv(ctx, buffer, drawbuffer, value, true);
427 }
428
429
430 void GLAPIENTRY
431 _mesa_ClearBufferiv(GLenum buffer, GLint drawbuffer, const GLint *value)
432 {
433 GET_CURRENT_CONTEXT(ctx);
434 clear_bufferiv(ctx, buffer, drawbuffer, value, false);
435 }
436
437
438 /**
439 * The ClearBuffer framework is so complicated and so riddled with the
440 * assumption that the framebuffer is bound that, for now, we will just fake
441 * direct state access clearing for the user.
442 */
443 void GLAPIENTRY
444 _mesa_ClearNamedFramebufferiv(GLuint framebuffer, GLenum buffer,
445 GLint drawbuffer, const GLint *value)
446 {
447 GLint oldfb;
448
449 _mesa_GetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &oldfb);
450 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffer);
451 _mesa_ClearBufferiv(buffer, drawbuffer, value);
452 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, (GLuint) oldfb);
453 }
454
455
456 /**
457 * New in GL 3.0
458 * Clear unsigned integer color buffer (not depth, not stencil).
459 */
460 static ALWAYS_INLINE void
461 clear_bufferuiv(struct gl_context *ctx, GLenum buffer, GLint drawbuffer,
462 const GLuint *value, bool no_error)
463 {
464 FLUSH_VERTICES(ctx, 0);
465
466 if (ctx->NewState) {
467 _mesa_update_state( ctx );
468 }
469
470 switch (buffer) {
471 case GL_COLOR:
472 {
473 const GLbitfield mask = make_color_buffer_mask(ctx, drawbuffer);
474 if (!no_error && mask == INVALID_MASK) {
475 _mesa_error(ctx, GL_INVALID_VALUE, "glClearBufferuiv(drawbuffer=%d)",
476 drawbuffer);
477 return;
478 }
479 else if (mask && !ctx->RasterDiscard) {
480 union gl_color_union clearSave;
481
482 /* save color */
483 clearSave = ctx->Color.ClearColor;
484 /* set color */
485 COPY_4V(ctx->Color.ClearColor.ui, value);
486 /* clear buffer(s) */
487 ctx->Driver.Clear(ctx, mask);
488 /* restore color */
489 ctx->Color.ClearColor = clearSave;
490 }
491 }
492 break;
493 default:
494 if (!no_error) {
495 /* Page 498 of the PDF, section '17.4.3.1 Clearing Individual Buffers'
496 * of the OpenGL 4.5 spec states:
497 *
498 * "An INVALID_ENUM error is generated by ClearBufferuiv and
499 * ClearNamedFramebufferuiv if buffer is not COLOR."
500 */
501 _mesa_error(ctx, GL_INVALID_ENUM, "glClearBufferuiv(buffer=%s)",
502 _mesa_enum_to_string(buffer));
503 }
504 return;
505 }
506 }
507
508
509 void GLAPIENTRY
510 _mesa_ClearBufferuiv_no_error(GLenum buffer, GLint drawbuffer,
511 const GLuint *value)
512 {
513 GET_CURRENT_CONTEXT(ctx);
514 clear_bufferuiv(ctx, buffer, drawbuffer, value, true);
515 }
516
517
518 void GLAPIENTRY
519 _mesa_ClearBufferuiv(GLenum buffer, GLint drawbuffer, const GLuint *value)
520 {
521 GET_CURRENT_CONTEXT(ctx);
522 clear_bufferuiv(ctx, buffer, drawbuffer, value, false);
523 }
524
525
526 /**
527 * The ClearBuffer framework is so complicated and so riddled with the
528 * assumption that the framebuffer is bound that, for now, we will just fake
529 * direct state access clearing for the user.
530 */
531 void GLAPIENTRY
532 _mesa_ClearNamedFramebufferuiv(GLuint framebuffer, GLenum buffer,
533 GLint drawbuffer, const GLuint *value)
534 {
535 GLint oldfb;
536
537 _mesa_GetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &oldfb);
538 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffer);
539 _mesa_ClearBufferuiv(buffer, drawbuffer, value);
540 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, (GLuint) oldfb);
541 }
542
543
544 /**
545 * New in GL 3.0
546 * Clear fixed-pt or float color buffer or depth buffer (not stencil).
547 */
548 static ALWAYS_INLINE void
549 clear_bufferfv(struct gl_context *ctx, GLenum buffer, GLint drawbuffer,
550 const GLfloat *value, bool no_error)
551 {
552 FLUSH_VERTICES(ctx, 0);
553
554 if (ctx->NewState) {
555 _mesa_update_state( ctx );
556 }
557
558 switch (buffer) {
559 case GL_DEPTH:
560 /* Page 264 (page 280 of the PDF) of the OpenGL 3.0 spec says:
561 *
562 * "ClearBuffer generates an INVALID VALUE error if buffer is
563 * COLOR and drawbuffer is less than zero, or greater than the
564 * value of MAX DRAW BUFFERS minus one; or if buffer is DEPTH,
565 * STENCIL, or DEPTH STENCIL and drawbuffer is not zero."
566 */
567 if (!no_error && drawbuffer != 0) {
568 _mesa_error(ctx, GL_INVALID_VALUE, "glClearBufferfv(drawbuffer=%d)",
569 drawbuffer);
570 return;
571 }
572 else if (ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer
573 && !ctx->RasterDiscard) {
574 /* Save current depth clear value, set to 'value', do the
575 * depth clear and restore the clear value.
576 * XXX in the future we may have a new ctx->Driver.ClearBuffer()
577 * hook instead.
578 */
579 const GLclampd clearSave = ctx->Depth.Clear;
580 ctx->Depth.Clear = *value;
581 ctx->Driver.Clear(ctx, BUFFER_BIT_DEPTH);
582 ctx->Depth.Clear = clearSave;
583 }
584 /* clear depth buffer to value */
585 break;
586 case GL_COLOR:
587 {
588 const GLbitfield mask = make_color_buffer_mask(ctx, drawbuffer);
589 if (!no_error && mask == INVALID_MASK) {
590 _mesa_error(ctx, GL_INVALID_VALUE, "glClearBufferfv(drawbuffer=%d)",
591 drawbuffer);
592 return;
593 }
594 else if (mask && !ctx->RasterDiscard) {
595 union gl_color_union clearSave;
596
597 /* save color */
598 clearSave = ctx->Color.ClearColor;
599 /* set color */
600 COPY_4V(ctx->Color.ClearColor.f, value);
601 /* clear buffer(s) */
602 ctx->Driver.Clear(ctx, mask);
603 /* restore color */
604 ctx->Color.ClearColor = clearSave;
605 }
606 }
607 break;
608 default:
609 if (!no_error) {
610 /* Page 498 of the PDF, section '17.4.3.1 Clearing Individual Buffers'
611 * of the OpenGL 4.5 spec states:
612 *
613 * "An INVALID_ENUM error is generated by ClearBufferfv and
614 * ClearNamedFramebufferfv if buffer is not COLOR or DEPTH."
615 */
616 _mesa_error(ctx, GL_INVALID_ENUM, "glClearBufferfv(buffer=%s)",
617 _mesa_enum_to_string(buffer));
618 }
619 return;
620 }
621 }
622
623
624 void GLAPIENTRY
625 _mesa_ClearBufferfv_no_error(GLenum buffer, GLint drawbuffer,
626 const GLfloat *value)
627 {
628 GET_CURRENT_CONTEXT(ctx);
629 clear_bufferfv(ctx, buffer, drawbuffer, value, true);
630 }
631
632
633 void GLAPIENTRY
634 _mesa_ClearBufferfv(GLenum buffer, GLint drawbuffer, const GLfloat *value)
635 {
636 GET_CURRENT_CONTEXT(ctx);
637 clear_bufferfv(ctx, buffer, drawbuffer, value, false);
638 }
639
640
641 /**
642 * The ClearBuffer framework is so complicated and so riddled with the
643 * assumption that the framebuffer is bound that, for now, we will just fake
644 * direct state access clearing for the user.
645 */
646 void GLAPIENTRY
647 _mesa_ClearNamedFramebufferfv(GLuint framebuffer, GLenum buffer,
648 GLint drawbuffer, const GLfloat *value)
649 {
650 GLint oldfb;
651
652 _mesa_GetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &oldfb);
653 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffer);
654 _mesa_ClearBufferfv(buffer, drawbuffer, value);
655 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, (GLuint) oldfb);
656 }
657
658
659 /**
660 * New in GL 3.0
661 * Clear depth/stencil buffer only.
662 */
663 static ALWAYS_INLINE void
664 clear_bufferfi(struct gl_context *ctx, GLenum buffer, GLint drawbuffer,
665 GLfloat depth, GLint stencil, bool no_error)
666 {
667 GLbitfield mask = 0;
668
669 FLUSH_VERTICES(ctx, 0);
670
671 if (!no_error) {
672 if (buffer != GL_DEPTH_STENCIL) {
673 _mesa_error(ctx, GL_INVALID_ENUM, "glClearBufferfi(buffer=%s)",
674 _mesa_enum_to_string(buffer));
675 return;
676 }
677
678 /* Page 264 (page 280 of the PDF) of the OpenGL 3.0 spec says:
679 *
680 * "ClearBuffer generates an INVALID VALUE error if buffer is
681 * COLOR and drawbuffer is less than zero, or greater than the
682 * value of MAX DRAW BUFFERS minus one; or if buffer is DEPTH,
683 * STENCIL, or DEPTH STENCIL and drawbuffer is not zero."
684 */
685 if (drawbuffer != 0) {
686 _mesa_error(ctx, GL_INVALID_VALUE, "glClearBufferfi(drawbuffer=%d)",
687 drawbuffer);
688 return;
689 }
690 }
691
692 if (ctx->RasterDiscard)
693 return;
694
695 if (ctx->NewState) {
696 _mesa_update_state( ctx );
697 }
698
699 if (!no_error && ctx->DrawBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
700 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
701 "glClearBufferfi(incomplete framebuffer)");
702 return;
703 }
704
705 if (ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer)
706 mask |= BUFFER_BIT_DEPTH;
707 if (ctx->DrawBuffer->Attachment[BUFFER_STENCIL].Renderbuffer)
708 mask |= BUFFER_BIT_STENCIL;
709
710 if (mask) {
711 /* save current clear values */
712 const GLclampd clearDepthSave = ctx->Depth.Clear;
713 const GLuint clearStencilSave = ctx->Stencil.Clear;
714
715 /* set new clear values */
716 ctx->Depth.Clear = depth;
717 ctx->Stencil.Clear = stencil;
718
719 /* clear buffers */
720 ctx->Driver.Clear(ctx, mask);
721
722 /* restore */
723 ctx->Depth.Clear = clearDepthSave;
724 ctx->Stencil.Clear = clearStencilSave;
725 }
726 }
727
728
729 void GLAPIENTRY
730 _mesa_ClearBufferfi_no_error(GLenum buffer, GLint drawbuffer,
731 GLfloat depth, GLint stencil)
732 {
733 GET_CURRENT_CONTEXT(ctx);
734 clear_bufferfi(ctx, buffer, drawbuffer, depth, stencil, true);
735 }
736
737
738 void GLAPIENTRY
739 _mesa_ClearBufferfi(GLenum buffer, GLint drawbuffer,
740 GLfloat depth, GLint stencil)
741 {
742 GET_CURRENT_CONTEXT(ctx);
743 clear_bufferfi(ctx, buffer, drawbuffer, depth, stencil, false);
744 }
745
746
747 /**
748 * The ClearBuffer framework is so complicated and so riddled with the
749 * assumption that the framebuffer is bound that, for now, we will just fake
750 * direct state access clearing for the user.
751 */
752 void GLAPIENTRY
753 _mesa_ClearNamedFramebufferfi(GLuint framebuffer, GLenum buffer,
754 GLint drawbuffer, GLfloat depth, GLint stencil)
755 {
756 GLint oldfb;
757
758 _mesa_GetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &oldfb);
759 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffer);
760 _mesa_ClearBufferfi(buffer, drawbuffer, depth, stencil);
761 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, (GLuint) oldfb);
762 }