2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
28 * glClearColor, glClearIndex, glClear() functions.
45 _mesa_ClearIndex( GLfloat c
)
47 GET_CURRENT_CONTEXT(ctx
);
48 ASSERT_OUTSIDE_BEGIN_END(ctx
);
50 if (ctx
->Color
.ClearIndex
== (GLuint
) c
)
53 FLUSH_VERTICES(ctx
, _NEW_COLOR
);
54 ctx
->Color
.ClearIndex
= (GLuint
) c
;
60 * Specify the clear values for the color buffers.
62 * \param red red color component.
63 * \param green green color component.
64 * \param blue blue color component.
65 * \param alpha alpha component.
69 * Clamps the parameters and updates gl_colorbuffer_attrib::ClearColor. On a
70 * change, flushes the vertices and notifies the driver via the
71 * dd_function_table::ClearColor callback.
74 _mesa_ClearColor( GLclampf red
, GLclampf green
, GLclampf blue
, GLclampf alpha
)
77 GET_CURRENT_CONTEXT(ctx
);
78 ASSERT_OUTSIDE_BEGIN_END(ctx
);
80 tmp
[0] = CLAMP(red
, 0.0F
, 1.0F
);
81 tmp
[1] = CLAMP(green
, 0.0F
, 1.0F
);
82 tmp
[2] = CLAMP(blue
, 0.0F
, 1.0F
);
83 tmp
[3] = CLAMP(alpha
, 0.0F
, 1.0F
);
85 if (TEST_EQ_4V(tmp
, ctx
->Color
.ClearColor
))
86 return; /* no change */
88 FLUSH_VERTICES(ctx
, _NEW_COLOR
);
89 COPY_4V(ctx
->Color
.ClearColor
, tmp
);
91 if (ctx
->Driver
.ClearColor
) {
92 /* it's OK to call glClearColor in CI mode but it should be a NOP */
93 (*ctx
->Driver
.ClearColor
)(ctx
, ctx
->Color
.ClearColor
);
101 * \param mask bit-mask indicating the buffers to be cleared.
103 * Flushes the vertices and verifies the parameter. If __GLcontextRec::NewState
104 * is set then calls _mesa_update_state() to update gl_frame_buffer::_Xmin,
105 * etc. If the rasterization mode is set to GL_RENDER then requests the driver
106 * to clear the buffers, via the dd_function_table::Clear callback.
109 _mesa_Clear( GLbitfield mask
)
111 GET_CURRENT_CONTEXT(ctx
);
112 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
114 FLUSH_CURRENT(ctx
, 0);
116 if (MESA_VERBOSE
& VERBOSE_API
)
117 _mesa_debug(ctx
, "glClear 0x%x\n", mask
);
119 if (mask
& ~(GL_COLOR_BUFFER_BIT
|
120 GL_DEPTH_BUFFER_BIT
|
121 GL_STENCIL_BUFFER_BIT
|
122 GL_ACCUM_BUFFER_BIT
)) {
123 /* invalid bit set */
124 _mesa_error( ctx
, GL_INVALID_VALUE
, "glClear(0x%x)", mask
);
129 _mesa_update_state( ctx
); /* update _Xmin, etc */
132 if (ctx
->DrawBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
133 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
134 "glClear(incomplete framebuffer)");
138 if (ctx
->DrawBuffer
->Width
== 0 || ctx
->DrawBuffer
->Height
== 0 ||
139 ctx
->DrawBuffer
->_Xmin
>= ctx
->DrawBuffer
->_Xmax
||
140 ctx
->DrawBuffer
->_Ymin
>= ctx
->DrawBuffer
->_Ymax
)
143 if (ctx
->RenderMode
== GL_RENDER
) {
144 GLbitfield bufferMask
;
146 /* don't clear depth buffer if depth writing disabled */
147 if (!ctx
->Depth
.Mask
)
148 mask
&= ~GL_DEPTH_BUFFER_BIT
;
150 /* Build the bitmask to send to device driver's Clear function.
151 * Note that the GL_COLOR_BUFFER_BIT flag will expand to 0, 1, 2 or 4
152 * of the BUFFER_BIT_FRONT/BACK_LEFT/RIGHT flags, or one of the
153 * BUFFER_BIT_COLORn flags.
156 if (mask
& GL_COLOR_BUFFER_BIT
) {
158 for (i
= 0; i
< ctx
->DrawBuffer
->_NumColorDrawBuffers
; i
++) {
159 bufferMask
|= (1 << ctx
->DrawBuffer
->_ColorDrawBufferIndexes
[i
]);
163 if ((mask
& GL_DEPTH_BUFFER_BIT
)
164 && ctx
->DrawBuffer
->Visual
.haveDepthBuffer
) {
165 bufferMask
|= BUFFER_BIT_DEPTH
;
168 if ((mask
& GL_STENCIL_BUFFER_BIT
)
169 && ctx
->DrawBuffer
->Visual
.haveStencilBuffer
) {
170 bufferMask
|= BUFFER_BIT_STENCIL
;
173 if ((mask
& GL_ACCUM_BUFFER_BIT
)
174 && ctx
->DrawBuffer
->Visual
.haveAccumBuffer
) {
175 bufferMask
|= BUFFER_BIT_ACCUM
;
178 ASSERT(ctx
->Driver
.Clear
);
179 ctx
->Driver
.Clear(ctx
, bufferMask
);
184 /** Returned by make_color_buffer_mask() for errors */
185 #define INVALID_MASK ~0x0
189 * Convert the glClearBuffer 'drawbuffer' parameter into a bitmask of
190 * BUFFER_BIT_x values.
191 * Return INVALID_MASK if the drawbuffer value is invalid.
194 make_color_buffer_mask(GLcontext
*ctx
, GLint drawbuffer
)
196 const struct gl_renderbuffer_attachment
*att
= ctx
->DrawBuffer
->Attachment
;
197 GLbitfield mask
= 0x0;
199 switch (drawbuffer
) {
201 if (att
[BUFFER_FRONT_LEFT
].Renderbuffer
)
202 mask
|= BUFFER_BIT_FRONT_LEFT
;
203 if (att
[BUFFER_FRONT_RIGHT
].Renderbuffer
)
204 mask
|= BUFFER_BIT_FRONT_RIGHT
;
207 if (att
[BUFFER_BACK_LEFT
].Renderbuffer
)
208 mask
|= BUFFER_BIT_BACK_LEFT
;
209 if (att
[BUFFER_BACK_RIGHT
].Renderbuffer
)
210 mask
|= BUFFER_BIT_BACK_RIGHT
;
213 if (att
[BUFFER_FRONT_LEFT
].Renderbuffer
)
214 mask
|= BUFFER_BIT_FRONT_LEFT
;
215 if (att
[BUFFER_BACK_LEFT
].Renderbuffer
)
216 mask
|= BUFFER_BIT_BACK_LEFT
;
219 if (att
[BUFFER_FRONT_RIGHT
].Renderbuffer
)
220 mask
|= BUFFER_BIT_FRONT_RIGHT
;
221 if (att
[BUFFER_BACK_RIGHT
].Renderbuffer
)
222 mask
|= BUFFER_BIT_BACK_RIGHT
;
224 case GL_FRONT_AND_BACK
:
225 if (att
[BUFFER_FRONT_LEFT
].Renderbuffer
)
226 mask
|= BUFFER_BIT_FRONT_LEFT
;
227 if (att
[BUFFER_BACK_LEFT
].Renderbuffer
)
228 mask
|= BUFFER_BIT_BACK_LEFT
;
229 if (att
[BUFFER_FRONT_RIGHT
].Renderbuffer
)
230 mask
|= BUFFER_BIT_FRONT_RIGHT
;
231 if (att
[BUFFER_BACK_RIGHT
].Renderbuffer
)
232 mask
|= BUFFER_BIT_BACK_RIGHT
;
235 if (drawbuffer
< 0 || drawbuffer
>= (GLint
)ctx
->Const
.MaxDrawBuffers
) {
238 else if (att
[BUFFER_COLOR0
+ drawbuffer
].Renderbuffer
) {
239 mask
|= (BUFFER_BIT_COLOR0
<< drawbuffer
);
250 * Clear signed integer color buffer or stencil buffer (not depth).
253 _mesa_ClearBufferiv(GLenum buffer
, GLint drawbuffer
, const GLint
*value
)
255 GET_CURRENT_CONTEXT(ctx
);
256 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
258 FLUSH_CURRENT(ctx
, 0);
261 _mesa_update_state( ctx
);
266 if (drawbuffer
!= 0) {
267 _mesa_error(ctx
, GL_INVALID_VALUE
, "glClearBufferiv(drawbuffer=%d)",
272 /* Save current stencil clear value, set to 'value', do the
273 * stencil clear and restore the clear value.
274 * XXX in the future we may have a new ctx->Driver.ClearBuffer()
277 const GLuint clearSave
= ctx
->Stencil
.Clear
;
278 ctx
->Stencil
.Clear
= *value
;
279 if (ctx
->Driver
.ClearStencil
)
280 ctx
->Driver
.ClearStencil(ctx
, *value
);
281 ctx
->Driver
.Clear(ctx
, BUFFER_BIT_STENCIL
);
282 ctx
->Stencil
.Clear
= clearSave
;
283 if (ctx
->Driver
.ClearStencil
)
284 ctx
->Driver
.ClearStencil(ctx
, clearSave
);
289 const GLbitfield mask
= make_color_buffer_mask(ctx
, drawbuffer
);
290 if (mask
== INVALID_MASK
) {
291 _mesa_error(ctx
, GL_INVALID_VALUE
, "glClearBufferiv(drawbuffer=%d)",
296 /* XXX note: we're putting the integer clear values into the
297 * floating point state var. This will not always work. We'll
298 * need a new ctx->Driver.ClearBuffer() hook....
300 GLclampf clearSave
[4];
302 COPY_4V(clearSave
, ctx
->Color
.ClearColor
);
304 COPY_4V_CAST(ctx
->Color
.ClearColor
, value
, GLclampf
);
305 if (ctx
->Driver
.ClearColor
)
306 ctx
->Driver
.ClearColor(ctx
, ctx
->Color
.ClearColor
);
307 /* clear buffer(s) */
308 ctx
->Driver
.Clear(ctx
, mask
);
310 COPY_4V(ctx
->Color
.ClearColor
, clearSave
);
311 if (ctx
->Driver
.ClearColor
)
312 ctx
->Driver
.ClearColor(ctx
, clearSave
);
317 _mesa_error(ctx
, GL_INVALID_ENUM
, "glClearBufferiv(buffer=%s)",
318 _mesa_lookup_enum_by_nr(buffer
));
326 * Clear unsigned integer color buffer (not depth, not stencil).
329 _mesa_ClearBufferuiv(GLenum buffer
, GLint drawbuffer
, const GLuint
*value
)
331 GET_CURRENT_CONTEXT(ctx
);
332 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
334 FLUSH_CURRENT(ctx
, 0);
337 _mesa_update_state( ctx
);
343 const GLbitfield mask
= make_color_buffer_mask(ctx
, drawbuffer
);
344 if (mask
== INVALID_MASK
) {
345 _mesa_error(ctx
, GL_INVALID_VALUE
, "glClearBufferiv(drawbuffer=%d)",
350 /* XXX note: we're putting the uint clear values into the
351 * floating point state var. This will not always work. We'll
352 * need a new ctx->Driver.ClearBuffer() hook....
354 GLclampf clearSave
[4];
356 COPY_4V(clearSave
, ctx
->Color
.ClearColor
);
358 COPY_4V_CAST(ctx
->Color
.ClearColor
, value
, GLclampf
);
359 if (ctx
->Driver
.ClearColor
)
360 ctx
->Driver
.ClearColor(ctx
, ctx
->Color
.ClearColor
);
361 /* clear buffer(s) */
362 ctx
->Driver
.Clear(ctx
, mask
);
364 COPY_4V(ctx
->Color
.ClearColor
, clearSave
);
365 if (ctx
->Driver
.ClearColor
)
366 ctx
->Driver
.ClearColor(ctx
, clearSave
);
371 _mesa_error(ctx
, GL_INVALID_ENUM
, "glClearBufferuiv(buffer=%s)",
372 _mesa_lookup_enum_by_nr(buffer
));
380 * Clear fixed-pt or float color buffer or depth buffer (not stencil).
383 _mesa_ClearBufferfv(GLenum buffer
, GLint drawbuffer
, const GLfloat
*value
)
385 GET_CURRENT_CONTEXT(ctx
);
386 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
388 FLUSH_CURRENT(ctx
, 0);
391 _mesa_update_state( ctx
);
396 if (drawbuffer
!= 0) {
397 _mesa_error(ctx
, GL_INVALID_VALUE
, "glClearBufferfv(drawbuffer=%d)",
402 /* Save current depth clear value, set to 'value', do the
403 * depth clear and restore the clear value.
404 * XXX in the future we may have a new ctx->Driver.ClearBuffer()
407 const GLclampd clearSave
= ctx
->Depth
.Clear
;
408 ctx
->Depth
.Clear
= *value
;
409 if (ctx
->Driver
.ClearDepth
)
410 ctx
->Driver
.ClearDepth(ctx
, *value
);
411 ctx
->Driver
.Clear(ctx
, BUFFER_BIT_DEPTH
);
412 ctx
->Depth
.Clear
= clearSave
;
413 if (ctx
->Driver
.ClearDepth
)
414 ctx
->Driver
.ClearDepth(ctx
, clearSave
);
416 /* clear depth buffer to value */
420 const GLbitfield mask
= make_color_buffer_mask(ctx
, drawbuffer
);
421 if (mask
== INVALID_MASK
) {
422 _mesa_error(ctx
, GL_INVALID_VALUE
, "glClearBufferfv(drawbuffer=%d)",
427 GLclampf clearSave
[4];
429 COPY_4V(clearSave
, ctx
->Color
.ClearColor
);
431 COPY_4V_CAST(ctx
->Color
.ClearColor
, value
, GLclampf
);
432 if (ctx
->Driver
.ClearColor
)
433 ctx
->Driver
.ClearColor(ctx
, ctx
->Color
.ClearColor
);
434 /* clear buffer(s) */
435 ctx
->Driver
.Clear(ctx
, mask
);
437 COPY_4V(ctx
->Color
.ClearColor
, clearSave
);
438 if (ctx
->Driver
.ClearColor
)
439 ctx
->Driver
.ClearColor(ctx
, clearSave
);
444 _mesa_error(ctx
, GL_INVALID_ENUM
, "glClearBufferfv(buffer=%s)",
445 _mesa_lookup_enum_by_nr(buffer
));
453 * Clear depth/stencil buffer only.
456 _mesa_ClearBufferfi(GLenum buffer
, GLint drawbuffer
,
457 GLfloat depth
, GLint stencil
)
459 GET_CURRENT_CONTEXT(ctx
);
460 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
462 FLUSH_CURRENT(ctx
, 0);
464 if (buffer
!= GL_DEPTH_STENCIL
) {
465 _mesa_error(ctx
, GL_INVALID_ENUM
, "glClearBufferfi(buffer=%s)",
466 _mesa_lookup_enum_by_nr(buffer
));
470 if (drawbuffer
!= 0) {
471 _mesa_error(ctx
, GL_INVALID_VALUE
, "glClearBufferfi(drawbuffer=%d)",
477 _mesa_update_state( ctx
);
481 /* save current clear values */
482 const GLclampd clearDepthSave
= ctx
->Depth
.Clear
;
483 const GLuint clearStencilSave
= ctx
->Stencil
.Clear
;
485 /* set new clear values */
486 ctx
->Depth
.Clear
= depth
;
487 ctx
->Stencil
.Clear
= stencil
;
488 if (ctx
->Driver
.ClearDepth
)
489 ctx
->Driver
.ClearDepth(ctx
, depth
);
490 if (ctx
->Driver
.ClearStencil
)
491 ctx
->Driver
.ClearStencil(ctx
, stencil
);
494 ctx
->Driver
.Clear(ctx
, BUFFER_BIT_DEPTH
| BUFFER_BIT_STENCIL
);
497 ctx
->Depth
.Clear
= clearDepthSave
;
498 ctx
->Stencil
.Clear
= clearStencilSave
;
499 if (ctx
->Driver
.ClearDepth
)
500 ctx
->Driver
.ClearDepth(ctx
, clearDepthSave
);
501 if (ctx
->Driver
.ClearStencil
)
502 ctx
->Driver
.ClearStencil(ctx
, clearStencilSave
);