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.
45 _mesa_ClearIndex( GLfloat c
)
47 GET_CURRENT_CONTEXT(ctx
);
49 ctx
->Color
.ClearIndex
= (GLuint
) c
;
54 * Specify the clear values for the color buffers.
56 * \param red red color component.
57 * \param green green color component.
58 * \param blue blue color component.
59 * \param alpha alpha component.
63 * Clamps the parameters and updates gl_colorbuffer_attrib::ClearColor. On a
64 * change, flushes the vertices and notifies the driver via the
65 * dd_function_table::ClearColor callback.
68 _mesa_ClearColor( GLclampf red
, GLclampf green
, GLclampf blue
, GLclampf alpha
)
70 GET_CURRENT_CONTEXT(ctx
);
72 ctx
->Color
.ClearColor
.f
[0] = red
;
73 ctx
->Color
.ClearColor
.f
[1] = green
;
74 ctx
->Color
.ClearColor
.f
[2] = blue
;
75 ctx
->Color
.ClearColor
.f
[3] = alpha
;
80 * GL_EXT_texture_integer
83 _mesa_ClearColorIiEXT(GLint r
, GLint g
, GLint b
, GLint a
)
85 GET_CURRENT_CONTEXT(ctx
);
87 ctx
->Color
.ClearColor
.i
[0] = r
;
88 ctx
->Color
.ClearColor
.i
[1] = g
;
89 ctx
->Color
.ClearColor
.i
[2] = b
;
90 ctx
->Color
.ClearColor
.i
[3] = a
;
95 * GL_EXT_texture_integer
98 _mesa_ClearColorIuiEXT(GLuint r
, GLuint g
, GLuint b
, GLuint a
)
100 GET_CURRENT_CONTEXT(ctx
);
102 ctx
->Color
.ClearColor
.ui
[0] = r
;
103 ctx
->Color
.ClearColor
.ui
[1] = g
;
104 ctx
->Color
.ClearColor
.ui
[2] = b
;
105 ctx
->Color
.ClearColor
.ui
[3] = a
;
110 * Returns true if color writes are enabled for the given color attachment.
112 * Beyond checking ColorMask, this uses _mesa_format_has_color_component to
113 * ignore components that don't actually exist in the format (such as X in
117 color_buffer_writes_enabled(const struct gl_context
*ctx
, unsigned idx
)
119 struct gl_renderbuffer
*rb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[idx
];
121 GLubyte colorMask
= 0;
124 for (c
= 0; c
< 4; c
++) {
125 if (_mesa_format_has_color_component(rb
->Format
, c
))
126 colorMask
|= ctx
->Color
.ColorMask
[idx
][c
];
130 return colorMask
!= 0;
137 * \param mask bit-mask indicating the buffers to be cleared.
139 * Flushes the vertices and verifies the parameter. If __struct gl_contextRec::NewState
140 * is set then calls _mesa_update_state() to update gl_frame_buffer::_Xmin,
141 * etc. If the rasterization mode is set to GL_RENDER then requests the driver
142 * to clear the buffers, via the dd_function_table::Clear callback.
145 _mesa_Clear( GLbitfield mask
)
147 GET_CURRENT_CONTEXT(ctx
);
148 FLUSH_VERTICES(ctx
, 0);
150 FLUSH_CURRENT(ctx
, 0);
152 if (MESA_VERBOSE
& VERBOSE_API
)
153 _mesa_debug(ctx
, "glClear 0x%x\n", mask
);
155 if (mask
& ~(GL_COLOR_BUFFER_BIT
|
156 GL_DEPTH_BUFFER_BIT
|
157 GL_STENCIL_BUFFER_BIT
|
158 GL_ACCUM_BUFFER_BIT
)) {
159 /* invalid bit set */
160 _mesa_error( ctx
, GL_INVALID_VALUE
, "glClear(0x%x)", mask
);
164 /* Accumulation buffers were removed in core contexts, and they never
165 * existed in OpenGL ES.
167 if ((mask
& GL_ACCUM_BUFFER_BIT
) != 0
168 && (ctx
->API
== API_OPENGL_CORE
|| _mesa_is_gles(ctx
))) {
169 _mesa_error( ctx
, GL_INVALID_VALUE
, "glClear(GL_ACCUM_BUFFER_BIT)");
174 _mesa_update_state( ctx
); /* update _Xmin, etc */
177 if (ctx
->DrawBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
178 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
179 "glClear(incomplete framebuffer)");
183 if (ctx
->RasterDiscard
)
186 if (ctx
->RenderMode
== GL_RENDER
) {
187 GLbitfield bufferMask
;
189 /* don't clear depth buffer if depth writing disabled */
190 if (!ctx
->Depth
.Mask
)
191 mask
&= ~GL_DEPTH_BUFFER_BIT
;
193 /* Build the bitmask to send to device driver's Clear function.
194 * Note that the GL_COLOR_BUFFER_BIT flag will expand to 0, 1, 2 or 4
195 * of the BUFFER_BIT_FRONT/BACK_LEFT/RIGHT flags, or one of the
196 * BUFFER_BIT_COLORn flags.
199 if (mask
& GL_COLOR_BUFFER_BIT
) {
201 for (i
= 0; i
< ctx
->DrawBuffer
->_NumColorDrawBuffers
; i
++) {
202 GLint buf
= ctx
->DrawBuffer
->_ColorDrawBufferIndexes
[i
];
204 if (buf
>= 0 && color_buffer_writes_enabled(ctx
, i
)) {
205 bufferMask
|= 1 << buf
;
210 if ((mask
& GL_DEPTH_BUFFER_BIT
)
211 && ctx
->DrawBuffer
->Visual
.haveDepthBuffer
) {
212 bufferMask
|= BUFFER_BIT_DEPTH
;
215 if ((mask
& GL_STENCIL_BUFFER_BIT
)
216 && ctx
->DrawBuffer
->Visual
.haveStencilBuffer
) {
217 bufferMask
|= BUFFER_BIT_STENCIL
;
220 if ((mask
& GL_ACCUM_BUFFER_BIT
)
221 && ctx
->DrawBuffer
->Visual
.haveAccumBuffer
) {
222 bufferMask
|= BUFFER_BIT_ACCUM
;
225 ASSERT(ctx
->Driver
.Clear
);
226 ctx
->Driver
.Clear(ctx
, bufferMask
);
231 /** Returned by make_color_buffer_mask() for errors */
232 #define INVALID_MASK ~0x0
236 * Convert the glClearBuffer 'drawbuffer' parameter into a bitmask of
237 * BUFFER_BIT_x values.
238 * Return INVALID_MASK if the drawbuffer value is invalid.
241 make_color_buffer_mask(struct gl_context
*ctx
, GLint drawbuffer
)
243 const struct gl_renderbuffer_attachment
*att
= ctx
->DrawBuffer
->Attachment
;
244 GLbitfield mask
= 0x0;
246 /* From the GL 4.0 specification:
247 * If buffer is COLOR, a particular draw buffer DRAW_BUFFERi is
248 * specified by passing i as the parameter drawbuffer, and value
249 * points to a four-element vector specifying the R, G, B, and A
250 * color to clear that draw buffer to. If the draw buffer is one
251 * of FRONT, BACK, LEFT, RIGHT, or FRONT_AND_BACK, identifying
252 * multiple buffers, each selected buffer is cleared to the same
255 * Note that "drawbuffer" and "draw buffer" have different meaning.
256 * "drawbuffer" specifies DRAW_BUFFERi, while "draw buffer" is what's
257 * assigned to DRAW_BUFFERi. It could be COLOR_ATTACHMENT0, FRONT, BACK,
260 if (drawbuffer
< 0 || drawbuffer
>= (GLint
)ctx
->Const
.MaxDrawBuffers
) {
264 switch (ctx
->DrawBuffer
->ColorDrawBuffer
[drawbuffer
]) {
266 if (att
[BUFFER_FRONT_LEFT
].Renderbuffer
)
267 mask
|= BUFFER_BIT_FRONT_LEFT
;
268 if (att
[BUFFER_FRONT_RIGHT
].Renderbuffer
)
269 mask
|= BUFFER_BIT_FRONT_RIGHT
;
272 if (att
[BUFFER_BACK_LEFT
].Renderbuffer
)
273 mask
|= BUFFER_BIT_BACK_LEFT
;
274 if (att
[BUFFER_BACK_RIGHT
].Renderbuffer
)
275 mask
|= BUFFER_BIT_BACK_RIGHT
;
278 if (att
[BUFFER_FRONT_LEFT
].Renderbuffer
)
279 mask
|= BUFFER_BIT_FRONT_LEFT
;
280 if (att
[BUFFER_BACK_LEFT
].Renderbuffer
)
281 mask
|= BUFFER_BIT_BACK_LEFT
;
284 if (att
[BUFFER_FRONT_RIGHT
].Renderbuffer
)
285 mask
|= BUFFER_BIT_FRONT_RIGHT
;
286 if (att
[BUFFER_BACK_RIGHT
].Renderbuffer
)
287 mask
|= BUFFER_BIT_BACK_RIGHT
;
289 case GL_FRONT_AND_BACK
:
290 if (att
[BUFFER_FRONT_LEFT
].Renderbuffer
)
291 mask
|= BUFFER_BIT_FRONT_LEFT
;
292 if (att
[BUFFER_BACK_LEFT
].Renderbuffer
)
293 mask
|= BUFFER_BIT_BACK_LEFT
;
294 if (att
[BUFFER_FRONT_RIGHT
].Renderbuffer
)
295 mask
|= BUFFER_BIT_FRONT_RIGHT
;
296 if (att
[BUFFER_BACK_RIGHT
].Renderbuffer
)
297 mask
|= BUFFER_BIT_BACK_RIGHT
;
301 GLint buf
= ctx
->DrawBuffer
->_ColorDrawBufferIndexes
[drawbuffer
];
303 if (buf
>= 0 && att
[buf
].Renderbuffer
) {
316 * Clear signed integer color buffer or stencil buffer (not depth).
319 _mesa_ClearBufferiv(GLenum buffer
, GLint drawbuffer
, const GLint
*value
)
321 GET_CURRENT_CONTEXT(ctx
);
322 FLUSH_VERTICES(ctx
, 0);
324 FLUSH_CURRENT(ctx
, 0);
327 _mesa_update_state( ctx
);
332 /* Page 264 (page 280 of the PDF) of the OpenGL 3.0 spec says:
334 * "ClearBuffer generates an INVALID VALUE error if buffer is
335 * COLOR and drawbuffer is less than zero, or greater than the
336 * value of MAX DRAW BUFFERS minus one; or if buffer is DEPTH,
337 * STENCIL, or DEPTH STENCIL and drawbuffer is not zero."
339 if (drawbuffer
!= 0) {
340 _mesa_error(ctx
, GL_INVALID_VALUE
, "glClearBufferiv(drawbuffer=%d)",
344 else if (ctx
->DrawBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
&& !ctx
->RasterDiscard
) {
345 /* Save current stencil clear value, set to 'value', do the
346 * stencil clear and restore the clear value.
347 * XXX in the future we may have a new ctx->Driver.ClearBuffer()
350 const GLuint clearSave
= ctx
->Stencil
.Clear
;
351 ctx
->Stencil
.Clear
= *value
;
352 ctx
->Driver
.Clear(ctx
, BUFFER_BIT_STENCIL
);
353 ctx
->Stencil
.Clear
= clearSave
;
358 const GLbitfield mask
= make_color_buffer_mask(ctx
, drawbuffer
);
359 if (mask
== INVALID_MASK
) {
360 _mesa_error(ctx
, GL_INVALID_VALUE
, "glClearBufferiv(drawbuffer=%d)",
364 else if (mask
&& !ctx
->RasterDiscard
) {
365 union gl_color_union clearSave
;
368 clearSave
= ctx
->Color
.ClearColor
;
370 COPY_4V(ctx
->Color
.ClearColor
.i
, value
);
371 /* clear buffer(s) */
372 ctx
->Driver
.Clear(ctx
, mask
);
374 ctx
->Color
.ClearColor
= clearSave
;
379 /* Page 264 (page 280 of the PDF) of the OpenGL 3.0 spec says:
381 * "The result of ClearBuffer is undefined if no conversion between
382 * the type of the specified value and the type of the buffer being
383 * cleared is defined (for example, if ClearBufferiv is called for a
384 * fixed- or floating-point buffer, or if ClearBufferfv is called
385 * for a signed or unsigned integer buffer). This is not an error."
387 * In this case we take "undefined" and "not an error" to mean "ignore."
388 * Note that we still need to generate an error for the invalid
389 * drawbuffer case (see the GL_STENCIL case above).
391 if (drawbuffer
!= 0) {
392 _mesa_error(ctx
, GL_INVALID_VALUE
, "glClearBufferiv(drawbuffer=%d)",
398 _mesa_error(ctx
, GL_INVALID_ENUM
, "glClearBufferiv(buffer=%s)",
399 _mesa_lookup_enum_by_nr(buffer
));
407 * Clear unsigned integer color buffer (not depth, not stencil).
410 _mesa_ClearBufferuiv(GLenum buffer
, GLint drawbuffer
, const GLuint
*value
)
412 GET_CURRENT_CONTEXT(ctx
);
414 FLUSH_VERTICES(ctx
, 0);
415 FLUSH_CURRENT(ctx
, 0);
418 _mesa_update_state( ctx
);
424 const GLbitfield mask
= make_color_buffer_mask(ctx
, drawbuffer
);
425 if (mask
== INVALID_MASK
) {
426 _mesa_error(ctx
, GL_INVALID_VALUE
, "glClearBufferuiv(drawbuffer=%d)",
430 else if (mask
&& !ctx
->RasterDiscard
) {
431 union gl_color_union clearSave
;
434 clearSave
= ctx
->Color
.ClearColor
;
436 COPY_4V(ctx
->Color
.ClearColor
.ui
, value
);
437 /* clear buffer(s) */
438 ctx
->Driver
.Clear(ctx
, mask
);
440 ctx
->Color
.ClearColor
= clearSave
;
446 /* Page 264 (page 280 of the PDF) of the OpenGL 3.0 spec says:
448 * "The result of ClearBuffer is undefined if no conversion between
449 * the type of the specified value and the type of the buffer being
450 * cleared is defined (for example, if ClearBufferiv is called for a
451 * fixed- or floating-point buffer, or if ClearBufferfv is called
452 * for a signed or unsigned integer buffer). This is not an error."
454 * In this case we take "undefined" and "not an error" to mean "ignore."
455 * Even though we could do something sensible for GL_STENCIL, page 263
456 * (page 279 of the PDF) says:
458 * "Only ClearBufferiv should be used to clear stencil buffers."
460 * Note that we still need to generate an error for the invalid
461 * drawbuffer case (see the GL_STENCIL case in _mesa_ClearBufferiv).
463 if (drawbuffer
!= 0) {
464 _mesa_error(ctx
, GL_INVALID_VALUE
, "glClearBufferuiv(drawbuffer=%d)",
470 _mesa_error(ctx
, GL_INVALID_ENUM
, "glClearBufferuiv(buffer=%s)",
471 _mesa_lookup_enum_by_nr(buffer
));
479 * Clear fixed-pt or float color buffer or depth buffer (not stencil).
482 _mesa_ClearBufferfv(GLenum buffer
, GLint drawbuffer
, const GLfloat
*value
)
484 GET_CURRENT_CONTEXT(ctx
);
486 FLUSH_VERTICES(ctx
, 0);
487 FLUSH_CURRENT(ctx
, 0);
490 _mesa_update_state( ctx
);
495 /* Page 264 (page 280 of the PDF) of the OpenGL 3.0 spec says:
497 * "ClearBuffer generates an INVALID VALUE error if buffer is
498 * COLOR and drawbuffer is less than zero, or greater than the
499 * value of MAX DRAW BUFFERS minus one; or if buffer is DEPTH,
500 * STENCIL, or DEPTH STENCIL and drawbuffer is not zero."
502 if (drawbuffer
!= 0) {
503 _mesa_error(ctx
, GL_INVALID_VALUE
, "glClearBufferfv(drawbuffer=%d)",
507 else if (ctx
->DrawBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
&& !ctx
->RasterDiscard
) {
508 /* Save current depth clear value, set to 'value', do the
509 * depth clear and restore the clear value.
510 * XXX in the future we may have a new ctx->Driver.ClearBuffer()
513 const GLclampd clearSave
= ctx
->Depth
.Clear
;
514 ctx
->Depth
.Clear
= *value
;
515 ctx
->Driver
.Clear(ctx
, BUFFER_BIT_DEPTH
);
516 ctx
->Depth
.Clear
= clearSave
;
518 /* clear depth buffer to value */
522 const GLbitfield mask
= make_color_buffer_mask(ctx
, drawbuffer
);
523 if (mask
== INVALID_MASK
) {
524 _mesa_error(ctx
, GL_INVALID_VALUE
, "glClearBufferfv(drawbuffer=%d)",
528 else if (mask
&& !ctx
->RasterDiscard
) {
529 union gl_color_union clearSave
;
532 clearSave
= ctx
->Color
.ClearColor
;
534 COPY_4V(ctx
->Color
.ClearColor
.f
, value
);
535 /* clear buffer(s) */
536 ctx
->Driver
.Clear(ctx
, mask
);
538 ctx
->Color
.ClearColor
= clearSave
;
543 /* Page 264 (page 280 of the PDF) of the OpenGL 3.0 spec says:
545 * "The result of ClearBuffer is undefined if no conversion between
546 * the type of the specified value and the type of the buffer being
547 * cleared is defined (for example, if ClearBufferiv is called for a
548 * fixed- or floating-point buffer, or if ClearBufferfv is called
549 * for a signed or unsigned integer buffer). This is not an error."
551 * In this case we take "undefined" and "not an error" to mean "ignore."
552 * Note that we still need to generate an error for the invalid
553 * drawbuffer case (see the GL_DEPTH case above).
555 if (drawbuffer
!= 0) {
556 _mesa_error(ctx
, GL_INVALID_VALUE
, "glClearBufferfv(drawbuffer=%d)",
562 _mesa_error(ctx
, GL_INVALID_ENUM
, "glClearBufferfv(buffer=%s)",
563 _mesa_lookup_enum_by_nr(buffer
));
571 * Clear depth/stencil buffer only.
574 _mesa_ClearBufferfi(GLenum buffer
, GLint drawbuffer
,
575 GLfloat depth
, GLint stencil
)
577 GET_CURRENT_CONTEXT(ctx
);
580 FLUSH_VERTICES(ctx
, 0);
581 FLUSH_CURRENT(ctx
, 0);
583 if (buffer
!= GL_DEPTH_STENCIL
) {
584 _mesa_error(ctx
, GL_INVALID_ENUM
, "glClearBufferfi(buffer=%s)",
585 _mesa_lookup_enum_by_nr(buffer
));
589 /* Page 264 (page 280 of the PDF) of the OpenGL 3.0 spec says:
591 * "ClearBuffer generates an INVALID VALUE error if buffer is
592 * COLOR and drawbuffer is less than zero, or greater than the
593 * value of MAX DRAW BUFFERS minus one; or if buffer is DEPTH,
594 * STENCIL, or DEPTH STENCIL and drawbuffer is not zero."
596 if (drawbuffer
!= 0) {
597 _mesa_error(ctx
, GL_INVALID_VALUE
, "glClearBufferfi(drawbuffer=%d)",
602 if (ctx
->RasterDiscard
)
606 _mesa_update_state( ctx
);
609 if (ctx
->DrawBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
)
610 mask
|= BUFFER_BIT_DEPTH
;
611 if (ctx
->DrawBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
)
612 mask
|= BUFFER_BIT_STENCIL
;
615 /* save current clear values */
616 const GLclampd clearDepthSave
= ctx
->Depth
.Clear
;
617 const GLuint clearStencilSave
= ctx
->Stencil
.Clear
;
619 /* set new clear values */
620 ctx
->Depth
.Clear
= depth
;
621 ctx
->Stencil
.Clear
= stencil
;
624 ctx
->Driver
.Clear(ctx
, mask
);
627 ctx
->Depth
.Clear
= clearDepthSave
;
628 ctx
->Stencil
.Clear
= clearStencilSave
;