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.
44 _mesa_ClearIndex( GLfloat c
)
46 GET_CURRENT_CONTEXT(ctx
);
47 ASSERT_OUTSIDE_BEGIN_END(ctx
);
49 if (ctx
->Color
.ClearIndex
== (GLuint
) c
)
52 FLUSH_VERTICES(ctx
, _NEW_COLOR
);
53 ctx
->Color
.ClearIndex
= (GLuint
) c
;
59 * Specify the clear values for the color buffers.
61 * \param red red color component.
62 * \param green green color component.
63 * \param blue blue color component.
64 * \param alpha alpha component.
68 * Clamps the parameters and updates gl_colorbuffer_attrib::ClearColor. On a
69 * change, flushes the vertices and notifies the driver via the
70 * dd_function_table::ClearColor callback.
73 _mesa_ClearColor( GLclampf red
, GLclampf green
, GLclampf blue
, GLclampf alpha
)
76 GET_CURRENT_CONTEXT(ctx
);
77 ASSERT_OUTSIDE_BEGIN_END(ctx
);
79 tmp
[0] = CLAMP(red
, 0.0F
, 1.0F
);
80 tmp
[1] = CLAMP(green
, 0.0F
, 1.0F
);
81 tmp
[2] = CLAMP(blue
, 0.0F
, 1.0F
);
82 tmp
[3] = CLAMP(alpha
, 0.0F
, 1.0F
);
84 if (TEST_EQ_4V(tmp
, ctx
->Color
.ClearColor
))
85 return; /* no change */
87 FLUSH_VERTICES(ctx
, _NEW_COLOR
);
88 COPY_4V(ctx
->Color
.ClearColor
, tmp
);
90 if (ctx
->Driver
.ClearColor
) {
91 /* it's OK to call glClearColor in CI mode but it should be a NOP */
92 (*ctx
->Driver
.ClearColor
)(ctx
, ctx
->Color
.ClearColor
);
100 * \param mask bit-mask indicating the buffers to be cleared.
102 * Flushes the vertices and verifies the parameter. If __GLcontextRec::NewState
103 * is set then calls _mesa_update_state() to update gl_frame_buffer::_Xmin,
104 * etc. If the rasterization mode is set to GL_RENDER then requests the driver
105 * to clear the buffers, via the dd_function_table::Clear callback.
108 _mesa_Clear( GLbitfield mask
)
110 GET_CURRENT_CONTEXT(ctx
);
111 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
113 FLUSH_CURRENT(ctx
, 0);
115 if (MESA_VERBOSE
& VERBOSE_API
)
116 _mesa_debug(ctx
, "glClear 0x%x\n", mask
);
118 if (mask
& ~(GL_COLOR_BUFFER_BIT
|
119 GL_DEPTH_BUFFER_BIT
|
120 GL_STENCIL_BUFFER_BIT
|
121 GL_ACCUM_BUFFER_BIT
)) {
122 /* invalid bit set */
123 _mesa_error( ctx
, GL_INVALID_VALUE
, "glClear(0x%x)", mask
);
128 _mesa_update_state( ctx
); /* update _Xmin, etc */
131 if (ctx
->DrawBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
132 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
133 "glClear(incomplete framebuffer)");
137 if (ctx
->DrawBuffer
->Width
== 0 || ctx
->DrawBuffer
->Height
== 0 ||
138 ctx
->DrawBuffer
->_Xmin
>= ctx
->DrawBuffer
->_Xmax
||
139 ctx
->DrawBuffer
->_Ymin
>= ctx
->DrawBuffer
->_Ymax
)
142 if (ctx
->RenderMode
== GL_RENDER
) {
143 GLbitfield bufferMask
;
145 /* don't clear depth buffer if depth writing disabled */
146 if (!ctx
->Depth
.Mask
)
147 mask
&= ~GL_DEPTH_BUFFER_BIT
;
149 /* Build the bitmask to send to device driver's Clear function.
150 * Note that the GL_COLOR_BUFFER_BIT flag will expand to 0, 1, 2 or 4
151 * of the BUFFER_BIT_FRONT/BACK_LEFT/RIGHT flags, or one of the
152 * BUFFER_BIT_COLORn flags.
155 if (mask
& GL_COLOR_BUFFER_BIT
) {
157 for (i
= 0; i
< ctx
->DrawBuffer
->_NumColorDrawBuffers
; i
++) {
158 bufferMask
|= (1 << ctx
->DrawBuffer
->_ColorDrawBufferIndexes
[i
]);
162 if ((mask
& GL_DEPTH_BUFFER_BIT
)
163 && ctx
->DrawBuffer
->Visual
.haveDepthBuffer
) {
164 bufferMask
|= BUFFER_BIT_DEPTH
;
167 if ((mask
& GL_STENCIL_BUFFER_BIT
)
168 && ctx
->DrawBuffer
->Visual
.haveStencilBuffer
) {
169 bufferMask
|= BUFFER_BIT_STENCIL
;
172 if ((mask
& GL_ACCUM_BUFFER_BIT
)
173 && ctx
->DrawBuffer
->Visual
.haveAccumBuffer
) {
174 bufferMask
|= BUFFER_BIT_ACCUM
;
177 ASSERT(ctx
->Driver
.Clear
);
178 ctx
->Driver
.Clear(ctx
, bufferMask
);
183 /** Returned by make_color_buffer_mask() for errors */
184 #define INVALID_MASK ~0x0
188 * Convert the glClearBuffer 'drawbuffer' parameter into a bitmask of
189 * BUFFER_BIT_x values.
190 * Return INVALID_MASK if the drawbuffer value is invalid.
193 make_color_buffer_mask(GLcontext
*ctx
, GLint drawbuffer
)
195 const struct gl_renderbuffer_attachment
*att
= ctx
->DrawBuffer
->Attachment
;
196 GLbitfield mask
= 0x0;
198 switch (drawbuffer
) {
200 if (att
[BUFFER_FRONT_LEFT
].Renderbuffer
)
201 mask
|= BUFFER_BIT_FRONT_LEFT
;
202 if (att
[BUFFER_FRONT_RIGHT
].Renderbuffer
)
203 mask
|= BUFFER_BIT_FRONT_RIGHT
;
206 if (att
[BUFFER_BACK_LEFT
].Renderbuffer
)
207 mask
|= BUFFER_BIT_BACK_LEFT
;
208 if (att
[BUFFER_BACK_RIGHT
].Renderbuffer
)
209 mask
|= BUFFER_BIT_BACK_RIGHT
;
212 if (att
[BUFFER_FRONT_LEFT
].Renderbuffer
)
213 mask
|= BUFFER_BIT_FRONT_LEFT
;
214 if (att
[BUFFER_BACK_LEFT
].Renderbuffer
)
215 mask
|= BUFFER_BIT_BACK_LEFT
;
218 if (att
[BUFFER_FRONT_RIGHT
].Renderbuffer
)
219 mask
|= BUFFER_BIT_FRONT_RIGHT
;
220 if (att
[BUFFER_BACK_RIGHT
].Renderbuffer
)
221 mask
|= BUFFER_BIT_BACK_RIGHT
;
223 case GL_FRONT_AND_BACK
:
224 if (att
[BUFFER_FRONT_LEFT
].Renderbuffer
)
225 mask
|= BUFFER_BIT_FRONT_LEFT
;
226 if (att
[BUFFER_BACK_LEFT
].Renderbuffer
)
227 mask
|= BUFFER_BIT_BACK_LEFT
;
228 if (att
[BUFFER_FRONT_RIGHT
].Renderbuffer
)
229 mask
|= BUFFER_BIT_FRONT_RIGHT
;
230 if (att
[BUFFER_BACK_RIGHT
].Renderbuffer
)
231 mask
|= BUFFER_BIT_BACK_RIGHT
;
234 if (drawbuffer
< 0 || drawbuffer
>= (GLint
)ctx
->Const
.MaxDrawBuffers
) {
237 else if (att
[BUFFER_COLOR0
+ drawbuffer
].Renderbuffer
) {
238 mask
|= (BUFFER_BIT_COLOR0
<< drawbuffer
);
249 * Clear signed integer color buffer or stencil buffer (not depth).
252 _mesa_ClearBufferiv(GLenum buffer
, GLint drawbuffer
, const GLint
*value
)
254 GET_CURRENT_CONTEXT(ctx
);
255 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
257 FLUSH_CURRENT(ctx
, 0);
260 _mesa_update_state( ctx
);
265 if (drawbuffer
!= 0) {
266 _mesa_error(ctx
, GL_INVALID_VALUE
, "glClearBufferiv(drawbuffer=%d)",
271 /* Save current stencil clear value, set to 'value', do the
272 * stencil clear and restore the clear value.
273 * XXX in the future we may have a new ctx->Driver.ClearBuffer()
276 const GLuint clearSave
= ctx
->Stencil
.Clear
;
277 ctx
->Stencil
.Clear
= *value
;
278 if (ctx
->Driver
.ClearStencil
)
279 ctx
->Driver
.ClearStencil(ctx
, *value
);
280 ctx
->Driver
.Clear(ctx
, BUFFER_BIT_STENCIL
);
281 ctx
->Stencil
.Clear
= clearSave
;
282 if (ctx
->Driver
.ClearStencil
)
283 ctx
->Driver
.ClearStencil(ctx
, clearSave
);
288 const GLbitfield mask
= make_color_buffer_mask(ctx
, drawbuffer
);
289 if (mask
== INVALID_MASK
) {
290 _mesa_error(ctx
, GL_INVALID_VALUE
, "glClearBufferiv(drawbuffer=%d)",
295 /* XXX note: we're putting the integer clear values into the
296 * floating point state var. This will not always work. We'll
297 * need a new ctx->Driver.ClearBuffer() hook....
299 GLclampf clearSave
[4];
301 COPY_4V(clearSave
, ctx
->Color
.ClearColor
);
303 COPY_4V_CAST(ctx
->Color
.ClearColor
, value
, GLclampf
);
304 if (ctx
->Driver
.ClearColor
)
305 ctx
->Driver
.ClearColor(ctx
, ctx
->Color
.ClearColor
);
306 /* clear buffer(s) */
307 ctx
->Driver
.Clear(ctx
, mask
);
309 COPY_4V(ctx
->Color
.ClearColor
, clearSave
);
310 if (ctx
->Driver
.ClearColor
)
311 ctx
->Driver
.ClearColor(ctx
, clearSave
);
316 _mesa_error(ctx
, GL_INVALID_ENUM
, "glClearBufferiv(buffer=%s)",
317 _mesa_lookup_enum_by_nr(buffer
));
325 * Clear unsigned integer color buffer (not depth, not stencil).
328 _mesa_ClearBufferuiv(GLenum buffer
, GLint drawbuffer
, const GLuint
*value
)
330 GET_CURRENT_CONTEXT(ctx
);
331 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
333 FLUSH_CURRENT(ctx
, 0);
336 _mesa_update_state( ctx
);
342 const GLbitfield mask
= make_color_buffer_mask(ctx
, drawbuffer
);
343 if (mask
== INVALID_MASK
) {
344 _mesa_error(ctx
, GL_INVALID_VALUE
, "glClearBufferiv(drawbuffer=%d)",
349 /* XXX note: we're putting the uint clear values into the
350 * floating point state var. This will not always work. We'll
351 * need a new ctx->Driver.ClearBuffer() hook....
353 GLclampf clearSave
[4];
355 COPY_4V(clearSave
, ctx
->Color
.ClearColor
);
357 COPY_4V_CAST(ctx
->Color
.ClearColor
, value
, GLclampf
);
358 if (ctx
->Driver
.ClearColor
)
359 ctx
->Driver
.ClearColor(ctx
, ctx
->Color
.ClearColor
);
360 /* clear buffer(s) */
361 ctx
->Driver
.Clear(ctx
, mask
);
363 COPY_4V(ctx
->Color
.ClearColor
, clearSave
);
364 if (ctx
->Driver
.ClearColor
)
365 ctx
->Driver
.ClearColor(ctx
, clearSave
);
370 _mesa_error(ctx
, GL_INVALID_ENUM
, "glClearBufferuiv(buffer=%s)",
371 _mesa_lookup_enum_by_nr(buffer
));
379 * Clear fixed-pt or float color buffer or depth buffer (not stencil).
382 _mesa_ClearBufferfv(GLenum buffer
, GLint drawbuffer
, const GLfloat
*value
)
384 GET_CURRENT_CONTEXT(ctx
);
385 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
387 FLUSH_CURRENT(ctx
, 0);
390 _mesa_update_state( ctx
);
395 if (drawbuffer
!= 0) {
396 _mesa_error(ctx
, GL_INVALID_VALUE
, "glClearBufferfv(drawbuffer=%d)",
401 /* Save current depth clear value, set to 'value', do the
402 * depth clear and restore the clear value.
403 * XXX in the future we may have a new ctx->Driver.ClearBuffer()
406 const GLclampd clearSave
= ctx
->Depth
.Clear
;
407 ctx
->Depth
.Clear
= *value
;
408 if (ctx
->Driver
.ClearDepth
)
409 ctx
->Driver
.ClearDepth(ctx
, *value
);
410 ctx
->Driver
.Clear(ctx
, BUFFER_BIT_DEPTH
);
411 ctx
->Depth
.Clear
= clearSave
;
412 if (ctx
->Driver
.ClearDepth
)
413 ctx
->Driver
.ClearDepth(ctx
, clearSave
);
415 /* clear depth buffer to value */
419 const GLbitfield mask
= make_color_buffer_mask(ctx
, drawbuffer
);
420 if (mask
== INVALID_MASK
) {
421 _mesa_error(ctx
, GL_INVALID_VALUE
, "glClearBufferfv(drawbuffer=%d)",
426 GLclampf clearSave
[4];
428 COPY_4V(clearSave
, ctx
->Color
.ClearColor
);
430 COPY_4V_CAST(ctx
->Color
.ClearColor
, value
, GLclampf
);
431 if (ctx
->Driver
.ClearColor
)
432 ctx
->Driver
.ClearColor(ctx
, ctx
->Color
.ClearColor
);
433 /* clear buffer(s) */
434 ctx
->Driver
.Clear(ctx
, mask
);
436 COPY_4V(ctx
->Color
.ClearColor
, clearSave
);
437 if (ctx
->Driver
.ClearColor
)
438 ctx
->Driver
.ClearColor(ctx
, clearSave
);
443 _mesa_error(ctx
, GL_INVALID_ENUM
, "glClearBufferfv(buffer=%s)",
444 _mesa_lookup_enum_by_nr(buffer
));
452 * Clear depth/stencil buffer only.
455 _mesa_ClearBufferfi(GLenum buffer
, GLint drawbuffer
,
456 GLfloat depth
, GLint stencil
)
458 GET_CURRENT_CONTEXT(ctx
);
459 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
461 FLUSH_CURRENT(ctx
, 0);
463 if (buffer
!= GL_DEPTH_STENCIL
) {
464 _mesa_error(ctx
, GL_INVALID_ENUM
, "glClearBufferfi(buffer=%s)",
465 _mesa_lookup_enum_by_nr(buffer
));
469 if (drawbuffer
!= 0) {
470 _mesa_error(ctx
, GL_INVALID_VALUE
, "glClearBufferfi(drawbuffer=%d)",
476 _mesa_update_state( ctx
);
480 /* save current clear values */
481 const GLclampd clearDepthSave
= ctx
->Depth
.Clear
;
482 const GLuint clearStencilSave
= ctx
->Stencil
.Clear
;
484 /* set new clear values */
485 ctx
->Depth
.Clear
= depth
;
486 ctx
->Stencil
.Clear
= stencil
;
487 if (ctx
->Driver
.ClearDepth
)
488 ctx
->Driver
.ClearDepth(ctx
, depth
);
489 if (ctx
->Driver
.ClearStencil
)
490 ctx
->Driver
.ClearStencil(ctx
, stencil
);
493 ctx
->Driver
.Clear(ctx
, BUFFER_BIT_DEPTH
| BUFFER_BIT_STENCIL
);
496 ctx
->Depth
.Clear
= clearDepthSave
;
497 ctx
->Stencil
.Clear
= clearStencilSave
;
498 if (ctx
->Driver
.ClearDepth
)
499 ctx
->Driver
.ClearDepth(ctx
, clearDepthSave
);
500 if (ctx
->Driver
.ClearStencil
)
501 ctx
->Driver
.ClearStencil(ctx
, clearStencilSave
);