2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
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:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
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.
28 * glClearColor, glClearIndex, glClear() functions.
46 _mesa_ClearIndex( GLfloat c
)
48 GET_CURRENT_CONTEXT(ctx
);
50 ctx
->Color
.ClearIndex
= (GLuint
) c
;
55 * Specify the clear values for the color buffers.
57 * \param red red color component.
58 * \param green green color component.
59 * \param blue blue color component.
60 * \param alpha alpha component.
65 _mesa_ClearColor( GLclampf red
, GLclampf green
, GLclampf blue
, GLclampf alpha
)
67 GET_CURRENT_CONTEXT(ctx
);
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
;
77 * GL_EXT_texture_integer
80 _mesa_ClearColorIiEXT(GLint r
, GLint g
, GLint b
, GLint a
)
82 GET_CURRENT_CONTEXT(ctx
);
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
;
92 * GL_EXT_texture_integer
95 _mesa_ClearColorIuiEXT(GLuint r
, GLuint g
, GLuint b
, GLuint a
)
97 GET_CURRENT_CONTEXT(ctx
);
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
;
107 * Returns true if color writes are enabled for the given color attachment.
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
114 color_buffer_writes_enabled(const struct gl_context
*ctx
, unsigned idx
)
116 struct gl_renderbuffer
*rb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[idx
];
118 GLubyte colorMask
= 0;
121 for (c
= 0; c
< 4; c
++) {
122 if (_mesa_format_has_color_component(rb
->Format
, c
))
123 colorMask
|= ctx
->Color
.ColorMask
[idx
][c
];
127 return colorMask
!= 0;
134 * \param mask bit-mask indicating the buffers to be cleared.
136 * Flushes the vertices and verifies the parameter.
137 * If __struct gl_contextRec::NewState is set then calls _mesa_update_state()
138 * to update gl_frame_buffer::_Xmin, etc. If the rasterization mode is set to
139 * GL_RENDER then requests the driver to clear the buffers, via the
140 * dd_function_table::Clear callback.
143 _mesa_Clear( GLbitfield mask
)
145 GET_CURRENT_CONTEXT(ctx
);
146 FLUSH_VERTICES(ctx
, 0);
148 FLUSH_CURRENT(ctx
, 0);
150 if (MESA_VERBOSE
& VERBOSE_API
)
151 _mesa_debug(ctx
, "glClear 0x%x\n", mask
);
153 if (mask
& ~(GL_COLOR_BUFFER_BIT
|
154 GL_DEPTH_BUFFER_BIT
|
155 GL_STENCIL_BUFFER_BIT
|
156 GL_ACCUM_BUFFER_BIT
)) {
157 /* invalid bit set */
158 _mesa_error( ctx
, GL_INVALID_VALUE
, "glClear(0x%x)", mask
);
162 /* Accumulation buffers were removed in core contexts, and they never
163 * existed in OpenGL ES.
165 if ((mask
& GL_ACCUM_BUFFER_BIT
) != 0
166 && (ctx
->API
== API_OPENGL_CORE
|| _mesa_is_gles(ctx
))) {
167 _mesa_error( ctx
, GL_INVALID_VALUE
, "glClear(GL_ACCUM_BUFFER_BIT)");
172 _mesa_update_state( ctx
); /* update _Xmin, etc */
175 if (ctx
->DrawBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
176 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
177 "glClear(incomplete framebuffer)");
181 if (ctx
->RasterDiscard
)
184 if (ctx
->RenderMode
== GL_RENDER
) {
185 GLbitfield bufferMask
;
187 /* don't clear depth buffer if depth writing disabled */
188 if (!ctx
->Depth
.Mask
)
189 mask
&= ~GL_DEPTH_BUFFER_BIT
;
191 /* Build the bitmask to send to device driver's Clear function.
192 * Note that the GL_COLOR_BUFFER_BIT flag will expand to 0, 1, 2 or 4
193 * of the BUFFER_BIT_FRONT/BACK_LEFT/RIGHT flags, or one of the
194 * BUFFER_BIT_COLORn flags.
197 if (mask
& GL_COLOR_BUFFER_BIT
) {
199 for (i
= 0; i
< ctx
->DrawBuffer
->_NumColorDrawBuffers
; i
++) {
200 GLint buf
= ctx
->DrawBuffer
->_ColorDrawBufferIndexes
[i
];
202 if (buf
>= 0 && color_buffer_writes_enabled(ctx
, i
)) {
203 bufferMask
|= 1 << buf
;
208 if ((mask
& GL_DEPTH_BUFFER_BIT
)
209 && ctx
->DrawBuffer
->Visual
.haveDepthBuffer
) {
210 bufferMask
|= BUFFER_BIT_DEPTH
;
213 if ((mask
& GL_STENCIL_BUFFER_BIT
)
214 && ctx
->DrawBuffer
->Visual
.haveStencilBuffer
) {
215 bufferMask
|= BUFFER_BIT_STENCIL
;
218 if ((mask
& GL_ACCUM_BUFFER_BIT
)
219 && ctx
->DrawBuffer
->Visual
.haveAccumBuffer
) {
220 bufferMask
|= BUFFER_BIT_ACCUM
;
223 assert(ctx
->Driver
.Clear
);
224 ctx
->Driver
.Clear(ctx
, bufferMask
);
229 /** Returned by make_color_buffer_mask() for errors */
230 #define INVALID_MASK ~0x0U
234 * Convert the glClearBuffer 'drawbuffer' parameter into a bitmask of
235 * BUFFER_BIT_x values.
236 * Return INVALID_MASK if the drawbuffer value is invalid.
239 make_color_buffer_mask(struct gl_context
*ctx
, GLint drawbuffer
)
241 const struct gl_renderbuffer_attachment
*att
= ctx
->DrawBuffer
->Attachment
;
242 GLbitfield mask
= 0x0;
244 /* From the GL 4.0 specification:
245 * If buffer is COLOR, a particular draw buffer DRAW_BUFFERi is
246 * specified by passing i as the parameter drawbuffer, and value
247 * points to a four-element vector specifying the R, G, B, and A
248 * color to clear that draw buffer to. If the draw buffer is one
249 * of FRONT, BACK, LEFT, RIGHT, or FRONT_AND_BACK, identifying
250 * multiple buffers, each selected buffer is cleared to the same
253 * Note that "drawbuffer" and "draw buffer" have different meaning.
254 * "drawbuffer" specifies DRAW_BUFFERi, while "draw buffer" is what's
255 * assigned to DRAW_BUFFERi. It could be COLOR_ATTACHMENT0, FRONT, BACK,
258 if (drawbuffer
< 0 || drawbuffer
>= (GLint
)ctx
->Const
.MaxDrawBuffers
) {
262 switch (ctx
->DrawBuffer
->ColorDrawBuffer
[drawbuffer
]) {
264 if (att
[BUFFER_FRONT_LEFT
].Renderbuffer
)
265 mask
|= BUFFER_BIT_FRONT_LEFT
;
266 if (att
[BUFFER_FRONT_RIGHT
].Renderbuffer
)
267 mask
|= BUFFER_BIT_FRONT_RIGHT
;
270 if (att
[BUFFER_BACK_LEFT
].Renderbuffer
)
271 mask
|= BUFFER_BIT_BACK_LEFT
;
272 if (att
[BUFFER_BACK_RIGHT
].Renderbuffer
)
273 mask
|= BUFFER_BIT_BACK_RIGHT
;
276 if (att
[BUFFER_FRONT_LEFT
].Renderbuffer
)
277 mask
|= BUFFER_BIT_FRONT_LEFT
;
278 if (att
[BUFFER_BACK_LEFT
].Renderbuffer
)
279 mask
|= BUFFER_BIT_BACK_LEFT
;
282 if (att
[BUFFER_FRONT_RIGHT
].Renderbuffer
)
283 mask
|= BUFFER_BIT_FRONT_RIGHT
;
284 if (att
[BUFFER_BACK_RIGHT
].Renderbuffer
)
285 mask
|= BUFFER_BIT_BACK_RIGHT
;
287 case GL_FRONT_AND_BACK
:
288 if (att
[BUFFER_FRONT_LEFT
].Renderbuffer
)
289 mask
|= BUFFER_BIT_FRONT_LEFT
;
290 if (att
[BUFFER_BACK_LEFT
].Renderbuffer
)
291 mask
|= BUFFER_BIT_BACK_LEFT
;
292 if (att
[BUFFER_FRONT_RIGHT
].Renderbuffer
)
293 mask
|= BUFFER_BIT_FRONT_RIGHT
;
294 if (att
[BUFFER_BACK_RIGHT
].Renderbuffer
)
295 mask
|= BUFFER_BIT_BACK_RIGHT
;
299 GLint buf
= ctx
->DrawBuffer
->_ColorDrawBufferIndexes
[drawbuffer
];
301 if (buf
>= 0 && att
[buf
].Renderbuffer
) {
314 * Clear signed integer color buffer or stencil buffer (not depth).
317 _mesa_ClearBufferiv(GLenum buffer
, GLint drawbuffer
, const GLint
*value
)
319 GET_CURRENT_CONTEXT(ctx
);
320 FLUSH_VERTICES(ctx
, 0);
322 FLUSH_CURRENT(ctx
, 0);
325 _mesa_update_state( ctx
);
328 /* Page 498 of the PDF, section '17.4.3.1 Clearing Individual Buffers'
329 * of the OpenGL 4.5 spec states:
331 * "An INVALID_ENUM error is generated by ClearBufferiv and
332 * ClearNamedFramebufferiv if buffer is not COLOR or STENCIL."
334 if (buffer
== GL_DEPTH
|| buffer
== GL_DEPTH_STENCIL
) {
335 _mesa_error(ctx
, GL_INVALID_ENUM
,
336 "glClearBufferiv(buffer=GL_DEPTH || GL_DEPTH_STENCIL)");
342 /* Page 264 (page 280 of the PDF) of the OpenGL 3.0 spec says:
344 * "ClearBuffer generates an INVALID VALUE error if buffer is
345 * COLOR and drawbuffer is less than zero, or greater than the
346 * value of MAX DRAW BUFFERS minus one; or if buffer is DEPTH,
347 * STENCIL, or DEPTH STENCIL and drawbuffer is not zero."
349 if (drawbuffer
!= 0) {
350 _mesa_error(ctx
, GL_INVALID_VALUE
, "glClearBufferiv(drawbuffer=%d)",
354 else if (ctx
->DrawBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
355 && !ctx
->RasterDiscard
) {
356 /* Save current stencil clear value, set to 'value', do the
357 * stencil clear and restore the clear value.
358 * XXX in the future we may have a new ctx->Driver.ClearBuffer()
361 const GLuint clearSave
= ctx
->Stencil
.Clear
;
362 ctx
->Stencil
.Clear
= *value
;
363 ctx
->Driver
.Clear(ctx
, BUFFER_BIT_STENCIL
);
364 ctx
->Stencil
.Clear
= clearSave
;
369 const GLbitfield mask
= make_color_buffer_mask(ctx
, drawbuffer
);
370 if (mask
== INVALID_MASK
) {
371 _mesa_error(ctx
, GL_INVALID_VALUE
, "glClearBufferiv(drawbuffer=%d)",
375 else if (mask
&& !ctx
->RasterDiscard
) {
376 union gl_color_union clearSave
;
379 clearSave
= ctx
->Color
.ClearColor
;
381 COPY_4V(ctx
->Color
.ClearColor
.i
, value
);
382 /* clear buffer(s) */
383 ctx
->Driver
.Clear(ctx
, mask
);
385 ctx
->Color
.ClearColor
= clearSave
;
390 _mesa_error(ctx
, GL_INVALID_ENUM
, "glClearBufferiv(buffer=%s)",
391 _mesa_enum_to_string(buffer
));
398 * The ClearBuffer framework is so complicated and so riddled with the
399 * assumption that the framebuffer is bound that, for now, we will just fake
400 * direct state access clearing for the user.
403 _mesa_ClearNamedFramebufferiv(GLuint framebuffer
, GLenum buffer
,
404 GLint drawbuffer
, const GLint
*value
)
408 _mesa_GetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING
, &oldfb
);
409 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER
, framebuffer
);
410 _mesa_ClearBufferiv(buffer
, drawbuffer
, value
);
411 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER
, (GLuint
) oldfb
);
417 * Clear unsigned integer color buffer (not depth, not stencil).
420 _mesa_ClearBufferuiv(GLenum buffer
, GLint drawbuffer
, const GLuint
*value
)
422 GET_CURRENT_CONTEXT(ctx
);
424 FLUSH_VERTICES(ctx
, 0);
425 FLUSH_CURRENT(ctx
, 0);
428 _mesa_update_state( ctx
);
434 const GLbitfield mask
= make_color_buffer_mask(ctx
, drawbuffer
);
435 if (mask
== INVALID_MASK
) {
436 _mesa_error(ctx
, GL_INVALID_VALUE
, "glClearBufferuiv(drawbuffer=%d)",
440 else if (mask
&& !ctx
->RasterDiscard
) {
441 union gl_color_union clearSave
;
444 clearSave
= ctx
->Color
.ClearColor
;
446 COPY_4V(ctx
->Color
.ClearColor
.ui
, value
);
447 /* clear buffer(s) */
448 ctx
->Driver
.Clear(ctx
, mask
);
450 ctx
->Color
.ClearColor
= clearSave
;
456 /* Page 264 (page 280 of the PDF) of the OpenGL 3.0 spec says:
458 * "The result of ClearBuffer is undefined if no conversion between
459 * the type of the specified value and the type of the buffer being
460 * cleared is defined (for example, if ClearBufferiv is called for a
461 * fixed- or floating-point buffer, or if ClearBufferfv is called
462 * for a signed or unsigned integer buffer). This is not an error."
464 * In this case we take "undefined" and "not an error" to mean "ignore."
465 * Even though we could do something sensible for GL_STENCIL, page 263
466 * (page 279 of the PDF) says:
468 * "Only ClearBufferiv should be used to clear stencil buffers."
470 * Note that we still need to generate an error for the invalid
471 * drawbuffer case (see the GL_STENCIL case in _mesa_ClearBufferiv).
473 if (drawbuffer
!= 0) {
474 _mesa_error(ctx
, GL_INVALID_VALUE
, "glClearBufferuiv(drawbuffer=%d)",
480 _mesa_error(ctx
, GL_INVALID_ENUM
, "glClearBufferuiv(buffer=%s)",
481 _mesa_enum_to_string(buffer
));
488 * The ClearBuffer framework is so complicated and so riddled with the
489 * assumption that the framebuffer is bound that, for now, we will just fake
490 * direct state access clearing for the user.
493 _mesa_ClearNamedFramebufferuiv(GLuint framebuffer
, GLenum buffer
,
494 GLint drawbuffer
, const GLuint
*value
)
498 _mesa_GetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING
, &oldfb
);
499 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER
, framebuffer
);
500 _mesa_ClearBufferuiv(buffer
, drawbuffer
, value
);
501 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER
, (GLuint
) oldfb
);
507 * Clear fixed-pt or float color buffer or depth buffer (not stencil).
510 _mesa_ClearBufferfv(GLenum buffer
, GLint drawbuffer
, const GLfloat
*value
)
512 GET_CURRENT_CONTEXT(ctx
);
514 FLUSH_VERTICES(ctx
, 0);
515 FLUSH_CURRENT(ctx
, 0);
518 _mesa_update_state( ctx
);
523 /* Page 264 (page 280 of the PDF) of the OpenGL 3.0 spec says:
525 * "ClearBuffer generates an INVALID VALUE error if buffer is
526 * COLOR and drawbuffer is less than zero, or greater than the
527 * value of MAX DRAW BUFFERS minus one; or if buffer is DEPTH,
528 * STENCIL, or DEPTH STENCIL and drawbuffer is not zero."
530 if (drawbuffer
!= 0) {
531 _mesa_error(ctx
, GL_INVALID_VALUE
, "glClearBufferfv(drawbuffer=%d)",
535 else if (ctx
->DrawBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
536 && !ctx
->RasterDiscard
) {
537 /* Save current depth clear value, set to 'value', do the
538 * depth clear and restore the clear value.
539 * XXX in the future we may have a new ctx->Driver.ClearBuffer()
542 const GLclampd clearSave
= ctx
->Depth
.Clear
;
543 ctx
->Depth
.Clear
= *value
;
544 ctx
->Driver
.Clear(ctx
, BUFFER_BIT_DEPTH
);
545 ctx
->Depth
.Clear
= clearSave
;
547 /* clear depth buffer to value */
551 const GLbitfield mask
= make_color_buffer_mask(ctx
, drawbuffer
);
552 if (mask
== INVALID_MASK
) {
553 _mesa_error(ctx
, GL_INVALID_VALUE
, "glClearBufferfv(drawbuffer=%d)",
557 else if (mask
&& !ctx
->RasterDiscard
) {
558 union gl_color_union clearSave
;
561 clearSave
= ctx
->Color
.ClearColor
;
563 COPY_4V(ctx
->Color
.ClearColor
.f
, value
);
564 /* clear buffer(s) */
565 ctx
->Driver
.Clear(ctx
, mask
);
567 ctx
->Color
.ClearColor
= clearSave
;
572 /* Page 264 (page 280 of the PDF) of the OpenGL 3.0 spec says:
574 * "The result of ClearBuffer is undefined if no conversion between
575 * the type of the specified value and the type of the buffer being
576 * cleared is defined (for example, if ClearBufferiv is called for a
577 * fixed- or floating-point buffer, or if ClearBufferfv is called
578 * for a signed or unsigned integer buffer). This is not an error."
580 * In this case we take "undefined" and "not an error" to mean "ignore."
581 * Note that we still need to generate an error for the invalid
582 * drawbuffer case (see the GL_DEPTH case above).
584 if (drawbuffer
!= 0) {
585 _mesa_error(ctx
, GL_INVALID_VALUE
, "glClearBufferfv(drawbuffer=%d)",
591 _mesa_error(ctx
, GL_INVALID_ENUM
, "glClearBufferfv(buffer=%s)",
592 _mesa_enum_to_string(buffer
));
599 * The ClearBuffer framework is so complicated and so riddled with the
600 * assumption that the framebuffer is bound that, for now, we will just fake
601 * direct state access clearing for the user.
604 _mesa_ClearNamedFramebufferfv(GLuint framebuffer
, GLenum buffer
,
605 GLint drawbuffer
, const GLfloat
*value
)
609 _mesa_GetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING
, &oldfb
);
610 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER
, framebuffer
);
611 _mesa_ClearBufferfv(buffer
, drawbuffer
, value
);
612 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER
, (GLuint
) oldfb
);
618 * Clear depth/stencil buffer only.
621 _mesa_ClearBufferfi(GLenum buffer
, GLint drawbuffer
,
622 GLfloat depth
, GLint stencil
)
624 GET_CURRENT_CONTEXT(ctx
);
627 FLUSH_VERTICES(ctx
, 0);
628 FLUSH_CURRENT(ctx
, 0);
630 if (buffer
!= GL_DEPTH_STENCIL
) {
631 _mesa_error(ctx
, GL_INVALID_ENUM
, "glClearBufferfi(buffer=%s)",
632 _mesa_enum_to_string(buffer
));
636 /* Page 264 (page 280 of the PDF) of the OpenGL 3.0 spec says:
638 * "ClearBuffer generates an INVALID VALUE error if buffer is
639 * COLOR and drawbuffer is less than zero, or greater than the
640 * value of MAX DRAW BUFFERS minus one; or if buffer is DEPTH,
641 * STENCIL, or DEPTH STENCIL and drawbuffer is not zero."
643 if (drawbuffer
!= 0) {
644 _mesa_error(ctx
, GL_INVALID_VALUE
, "glClearBufferfi(drawbuffer=%d)",
649 if (ctx
->RasterDiscard
)
653 _mesa_update_state( ctx
);
656 if (ctx
->DrawBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
)
657 mask
|= BUFFER_BIT_DEPTH
;
658 if (ctx
->DrawBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
)
659 mask
|= BUFFER_BIT_STENCIL
;
662 /* save current clear values */
663 const GLclampd clearDepthSave
= ctx
->Depth
.Clear
;
664 const GLuint clearStencilSave
= ctx
->Stencil
.Clear
;
666 /* set new clear values */
667 ctx
->Depth
.Clear
= depth
;
668 ctx
->Stencil
.Clear
= stencil
;
671 ctx
->Driver
.Clear(ctx
, mask
);
674 ctx
->Depth
.Clear
= clearDepthSave
;
675 ctx
->Stencil
.Clear
= clearStencilSave
;
681 * The ClearBuffer framework is so complicated and so riddled with the
682 * assumption that the framebuffer is bound that, for now, we will just fake
683 * direct state access clearing for the user.
686 _mesa_ClearNamedFramebufferfi(GLuint framebuffer
, GLenum buffer
,
687 GLfloat depth
, GLint stencil
)
691 _mesa_GetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING
, &oldfb
);
692 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER
, framebuffer
);
693 _mesa_ClearBufferfi(buffer
, 0, depth
, stencil
);
694 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER
, (GLuint
) oldfb
);