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
->DrawBuffer
->Width
== 0 || ctx
->DrawBuffer
->Height
== 0 ||
184 ctx
->DrawBuffer
->_Xmin
>= ctx
->DrawBuffer
->_Xmax
||
185 ctx
->DrawBuffer
->_Ymin
>= ctx
->DrawBuffer
->_Ymax
)
188 if (ctx
->RasterDiscard
)
191 if (ctx
->RenderMode
== GL_RENDER
) {
192 GLbitfield bufferMask
;
194 /* don't clear depth buffer if depth writing disabled */
195 if (!ctx
->Depth
.Mask
)
196 mask
&= ~GL_DEPTH_BUFFER_BIT
;
198 /* Build the bitmask to send to device driver's Clear function.
199 * Note that the GL_COLOR_BUFFER_BIT flag will expand to 0, 1, 2 or 4
200 * of the BUFFER_BIT_FRONT/BACK_LEFT/RIGHT flags, or one of the
201 * BUFFER_BIT_COLORn flags.
204 if (mask
& GL_COLOR_BUFFER_BIT
) {
206 for (i
= 0; i
< ctx
->DrawBuffer
->_NumColorDrawBuffers
; i
++) {
207 GLint buf
= ctx
->DrawBuffer
->_ColorDrawBufferIndexes
[i
];
209 if (buf
>= 0 && color_buffer_writes_enabled(ctx
, i
)) {
210 bufferMask
|= 1 << buf
;
215 if ((mask
& GL_DEPTH_BUFFER_BIT
)
216 && ctx
->DrawBuffer
->Visual
.haveDepthBuffer
) {
217 bufferMask
|= BUFFER_BIT_DEPTH
;
220 if ((mask
& GL_STENCIL_BUFFER_BIT
)
221 && ctx
->DrawBuffer
->Visual
.haveStencilBuffer
) {
222 bufferMask
|= BUFFER_BIT_STENCIL
;
225 if ((mask
& GL_ACCUM_BUFFER_BIT
)
226 && ctx
->DrawBuffer
->Visual
.haveAccumBuffer
) {
227 bufferMask
|= BUFFER_BIT_ACCUM
;
230 ASSERT(ctx
->Driver
.Clear
);
231 ctx
->Driver
.Clear(ctx
, bufferMask
);
236 /** Returned by make_color_buffer_mask() for errors */
237 #define INVALID_MASK ~0x0
241 * Convert the glClearBuffer 'drawbuffer' parameter into a bitmask of
242 * BUFFER_BIT_x values.
243 * Return INVALID_MASK if the drawbuffer value is invalid.
246 make_color_buffer_mask(struct gl_context
*ctx
, GLint drawbuffer
)
248 const struct gl_renderbuffer_attachment
*att
= ctx
->DrawBuffer
->Attachment
;
249 GLbitfield mask
= 0x0;
251 /* From the GL 4.0 specification:
252 * If buffer is COLOR, a particular draw buffer DRAW_BUFFERi is
253 * specified by passing i as the parameter drawbuffer, and value
254 * points to a four-element vector specifying the R, G, B, and A
255 * color to clear that draw buffer to. If the draw buffer is one
256 * of FRONT, BACK, LEFT, RIGHT, or FRONT_AND_BACK, identifying
257 * multiple buffers, each selected buffer is cleared to the same
260 * Note that "drawbuffer" and "draw buffer" have different meaning.
261 * "drawbuffer" specifies DRAW_BUFFERi, while "draw buffer" is what's
262 * assigned to DRAW_BUFFERi. It could be COLOR_ATTACHMENT0, FRONT, BACK,
265 if (drawbuffer
< 0 || drawbuffer
>= (GLint
)ctx
->Const
.MaxDrawBuffers
) {
269 switch (ctx
->DrawBuffer
->ColorDrawBuffer
[drawbuffer
]) {
271 if (att
[BUFFER_FRONT_LEFT
].Renderbuffer
)
272 mask
|= BUFFER_BIT_FRONT_LEFT
;
273 if (att
[BUFFER_FRONT_RIGHT
].Renderbuffer
)
274 mask
|= BUFFER_BIT_FRONT_RIGHT
;
277 if (att
[BUFFER_BACK_LEFT
].Renderbuffer
)
278 mask
|= BUFFER_BIT_BACK_LEFT
;
279 if (att
[BUFFER_BACK_RIGHT
].Renderbuffer
)
280 mask
|= BUFFER_BIT_BACK_RIGHT
;
283 if (att
[BUFFER_FRONT_LEFT
].Renderbuffer
)
284 mask
|= BUFFER_BIT_FRONT_LEFT
;
285 if (att
[BUFFER_BACK_LEFT
].Renderbuffer
)
286 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
;
294 case GL_FRONT_AND_BACK
:
295 if (att
[BUFFER_FRONT_LEFT
].Renderbuffer
)
296 mask
|= BUFFER_BIT_FRONT_LEFT
;
297 if (att
[BUFFER_BACK_LEFT
].Renderbuffer
)
298 mask
|= BUFFER_BIT_BACK_LEFT
;
299 if (att
[BUFFER_FRONT_RIGHT
].Renderbuffer
)
300 mask
|= BUFFER_BIT_FRONT_RIGHT
;
301 if (att
[BUFFER_BACK_RIGHT
].Renderbuffer
)
302 mask
|= BUFFER_BIT_BACK_RIGHT
;
306 GLint buf
= ctx
->DrawBuffer
->_ColorDrawBufferIndexes
[drawbuffer
];
308 if (buf
>= 0 && att
[buf
].Renderbuffer
) {
321 * Clear signed integer color buffer or stencil buffer (not depth).
324 _mesa_ClearBufferiv(GLenum buffer
, GLint drawbuffer
, const GLint
*value
)
326 GET_CURRENT_CONTEXT(ctx
);
327 FLUSH_VERTICES(ctx
, 0);
329 FLUSH_CURRENT(ctx
, 0);
332 _mesa_update_state( ctx
);
337 /* Page 264 (page 280 of the PDF) of the OpenGL 3.0 spec says:
339 * "ClearBuffer generates an INVALID VALUE error if buffer is
340 * COLOR and drawbuffer is less than zero, or greater than the
341 * value of MAX DRAW BUFFERS minus one; or if buffer is DEPTH,
342 * STENCIL, or DEPTH STENCIL and drawbuffer is not zero."
344 if (drawbuffer
!= 0) {
345 _mesa_error(ctx
, GL_INVALID_VALUE
, "glClearBufferiv(drawbuffer=%d)",
349 else if (ctx
->DrawBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
&& !ctx
->RasterDiscard
) {
350 /* Save current stencil clear value, set to 'value', do the
351 * stencil clear and restore the clear value.
352 * XXX in the future we may have a new ctx->Driver.ClearBuffer()
355 const GLuint clearSave
= ctx
->Stencil
.Clear
;
356 ctx
->Stencil
.Clear
= *value
;
357 ctx
->Driver
.Clear(ctx
, BUFFER_BIT_STENCIL
);
358 ctx
->Stencil
.Clear
= clearSave
;
363 const GLbitfield mask
= make_color_buffer_mask(ctx
, drawbuffer
);
364 if (mask
== INVALID_MASK
) {
365 _mesa_error(ctx
, GL_INVALID_VALUE
, "glClearBufferiv(drawbuffer=%d)",
369 else if (mask
&& !ctx
->RasterDiscard
) {
370 union gl_color_union clearSave
;
373 clearSave
= ctx
->Color
.ClearColor
;
375 COPY_4V(ctx
->Color
.ClearColor
.i
, value
);
376 /* clear buffer(s) */
377 ctx
->Driver
.Clear(ctx
, mask
);
379 ctx
->Color
.ClearColor
= clearSave
;
384 /* Page 264 (page 280 of the PDF) of the OpenGL 3.0 spec says:
386 * "The result of ClearBuffer is undefined if no conversion between
387 * the type of the specified value and the type of the buffer being
388 * cleared is defined (for example, if ClearBufferiv is called for a
389 * fixed- or floating-point buffer, or if ClearBufferfv is called
390 * for a signed or unsigned integer buffer). This is not an error."
392 * In this case we take "undefined" and "not an error" to mean "ignore."
393 * Note that we still need to generate an error for the invalid
394 * drawbuffer case (see the GL_STENCIL case above).
396 if (drawbuffer
!= 0) {
397 _mesa_error(ctx
, GL_INVALID_VALUE
, "glClearBufferiv(drawbuffer=%d)",
403 _mesa_error(ctx
, GL_INVALID_ENUM
, "glClearBufferiv(buffer=%s)",
404 _mesa_lookup_enum_by_nr(buffer
));
412 * Clear unsigned integer color buffer (not depth, not stencil).
415 _mesa_ClearBufferuiv(GLenum buffer
, GLint drawbuffer
, const GLuint
*value
)
417 GET_CURRENT_CONTEXT(ctx
);
419 FLUSH_VERTICES(ctx
, 0);
420 FLUSH_CURRENT(ctx
, 0);
423 _mesa_update_state( ctx
);
429 const GLbitfield mask
= make_color_buffer_mask(ctx
, drawbuffer
);
430 if (mask
== INVALID_MASK
) {
431 _mesa_error(ctx
, GL_INVALID_VALUE
, "glClearBufferuiv(drawbuffer=%d)",
435 else if (mask
&& !ctx
->RasterDiscard
) {
436 union gl_color_union clearSave
;
439 clearSave
= ctx
->Color
.ClearColor
;
441 COPY_4V(ctx
->Color
.ClearColor
.ui
, value
);
442 /* clear buffer(s) */
443 ctx
->Driver
.Clear(ctx
, mask
);
445 ctx
->Color
.ClearColor
= clearSave
;
451 /* Page 264 (page 280 of the PDF) of the OpenGL 3.0 spec says:
453 * "The result of ClearBuffer is undefined if no conversion between
454 * the type of the specified value and the type of the buffer being
455 * cleared is defined (for example, if ClearBufferiv is called for a
456 * fixed- or floating-point buffer, or if ClearBufferfv is called
457 * for a signed or unsigned integer buffer). This is not an error."
459 * In this case we take "undefined" and "not an error" to mean "ignore."
460 * Even though we could do something sensible for GL_STENCIL, page 263
461 * (page 279 of the PDF) says:
463 * "Only ClearBufferiv should be used to clear stencil buffers."
465 * Note that we still need to generate an error for the invalid
466 * drawbuffer case (see the GL_STENCIL case in _mesa_ClearBufferiv).
468 if (drawbuffer
!= 0) {
469 _mesa_error(ctx
, GL_INVALID_VALUE
, "glClearBufferuiv(drawbuffer=%d)",
475 _mesa_error(ctx
, GL_INVALID_ENUM
, "glClearBufferuiv(buffer=%s)",
476 _mesa_lookup_enum_by_nr(buffer
));
484 * Clear fixed-pt or float color buffer or depth buffer (not stencil).
487 _mesa_ClearBufferfv(GLenum buffer
, GLint drawbuffer
, const GLfloat
*value
)
489 GET_CURRENT_CONTEXT(ctx
);
491 FLUSH_VERTICES(ctx
, 0);
492 FLUSH_CURRENT(ctx
, 0);
495 _mesa_update_state( ctx
);
500 /* Page 264 (page 280 of the PDF) of the OpenGL 3.0 spec says:
502 * "ClearBuffer generates an INVALID VALUE error if buffer is
503 * COLOR and drawbuffer is less than zero, or greater than the
504 * value of MAX DRAW BUFFERS minus one; or if buffer is DEPTH,
505 * STENCIL, or DEPTH STENCIL and drawbuffer is not zero."
507 if (drawbuffer
!= 0) {
508 _mesa_error(ctx
, GL_INVALID_VALUE
, "glClearBufferfv(drawbuffer=%d)",
512 else if (ctx
->DrawBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
&& !ctx
->RasterDiscard
) {
513 /* Save current depth clear value, set to 'value', do the
514 * depth clear and restore the clear value.
515 * XXX in the future we may have a new ctx->Driver.ClearBuffer()
518 const GLclampd clearSave
= ctx
->Depth
.Clear
;
519 ctx
->Depth
.Clear
= *value
;
520 ctx
->Driver
.Clear(ctx
, BUFFER_BIT_DEPTH
);
521 ctx
->Depth
.Clear
= clearSave
;
523 /* clear depth buffer to value */
527 const GLbitfield mask
= make_color_buffer_mask(ctx
, drawbuffer
);
528 if (mask
== INVALID_MASK
) {
529 _mesa_error(ctx
, GL_INVALID_VALUE
, "glClearBufferfv(drawbuffer=%d)",
533 else if (mask
&& !ctx
->RasterDiscard
) {
534 union gl_color_union clearSave
;
537 clearSave
= ctx
->Color
.ClearColor
;
539 COPY_4V(ctx
->Color
.ClearColor
.f
, value
);
540 /* clear buffer(s) */
541 ctx
->Driver
.Clear(ctx
, mask
);
543 ctx
->Color
.ClearColor
= clearSave
;
548 /* Page 264 (page 280 of the PDF) of the OpenGL 3.0 spec says:
550 * "The result of ClearBuffer is undefined if no conversion between
551 * the type of the specified value and the type of the buffer being
552 * cleared is defined (for example, if ClearBufferiv is called for a
553 * fixed- or floating-point buffer, or if ClearBufferfv is called
554 * for a signed or unsigned integer buffer). This is not an error."
556 * In this case we take "undefined" and "not an error" to mean "ignore."
557 * Note that we still need to generate an error for the invalid
558 * drawbuffer case (see the GL_DEPTH case above).
560 if (drawbuffer
!= 0) {
561 _mesa_error(ctx
, GL_INVALID_VALUE
, "glClearBufferfv(drawbuffer=%d)",
567 _mesa_error(ctx
, GL_INVALID_ENUM
, "glClearBufferfv(buffer=%s)",
568 _mesa_lookup_enum_by_nr(buffer
));
576 * Clear depth/stencil buffer only.
579 _mesa_ClearBufferfi(GLenum buffer
, GLint drawbuffer
,
580 GLfloat depth
, GLint stencil
)
582 GET_CURRENT_CONTEXT(ctx
);
585 FLUSH_VERTICES(ctx
, 0);
586 FLUSH_CURRENT(ctx
, 0);
588 if (buffer
!= GL_DEPTH_STENCIL
) {
589 _mesa_error(ctx
, GL_INVALID_ENUM
, "glClearBufferfi(buffer=%s)",
590 _mesa_lookup_enum_by_nr(buffer
));
594 /* Page 264 (page 280 of the PDF) of the OpenGL 3.0 spec says:
596 * "ClearBuffer generates an INVALID VALUE error if buffer is
597 * COLOR and drawbuffer is less than zero, or greater than the
598 * value of MAX DRAW BUFFERS minus one; or if buffer is DEPTH,
599 * STENCIL, or DEPTH STENCIL and drawbuffer is not zero."
601 if (drawbuffer
!= 0) {
602 _mesa_error(ctx
, GL_INVALID_VALUE
, "glClearBufferfi(drawbuffer=%d)",
607 if (ctx
->RasterDiscard
)
611 _mesa_update_state( ctx
);
614 if (ctx
->DrawBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
)
615 mask
|= BUFFER_BIT_DEPTH
;
616 if (ctx
->DrawBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
)
617 mask
|= BUFFER_BIT_STENCIL
;
620 /* save current clear values */
621 const GLclampd clearDepthSave
= ctx
->Depth
.Clear
;
622 const GLuint clearStencilSave
= ctx
->Stencil
.Clear
;
624 /* set new clear values */
625 ctx
->Depth
.Clear
= depth
;
626 ctx
->Stencil
.Clear
= stencil
;
629 ctx
->Driver
.Clear(ctx
, mask
);
632 ctx
->Depth
.Clear
= clearDepthSave
;
633 ctx
->Stencil
.Clear
= clearStencilSave
;