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.
46 _mesa_ClearIndex( GLfloat c
)
48 GET_CURRENT_CONTEXT(ctx
);
49 ASSERT_OUTSIDE_BEGIN_END(ctx
);
51 if (ctx
->Color
.ClearIndex
== (GLuint
) c
)
54 FLUSH_VERTICES(ctx
, _NEW_COLOR
);
55 ctx
->Color
.ClearIndex
= (GLuint
) c
;
61 * Specify the clear values for the color buffers.
63 * \param red red color component.
64 * \param green green color component.
65 * \param blue blue color component.
66 * \param alpha alpha component.
70 * Clamps the parameters and updates gl_colorbuffer_attrib::ClearColor. On a
71 * change, flushes the vertices and notifies the driver via the
72 * dd_function_table::ClearColor callback.
75 _mesa_ClearColor( GLclampf red
, GLclampf green
, GLclampf blue
, GLclampf alpha
)
78 GET_CURRENT_CONTEXT(ctx
);
79 ASSERT_OUTSIDE_BEGIN_END(ctx
);
81 tmp
[0] = CLAMP(red
, 0.0F
, 1.0F
);
82 tmp
[1] = CLAMP(green
, 0.0F
, 1.0F
);
83 tmp
[2] = CLAMP(blue
, 0.0F
, 1.0F
);
84 tmp
[3] = CLAMP(alpha
, 0.0F
, 1.0F
);
86 if (TEST_EQ_4V(tmp
, ctx
->Color
.ClearColor
))
87 return; /* no change */
89 FLUSH_VERTICES(ctx
, _NEW_COLOR
);
90 COPY_4V(ctx
->Color
.ClearColor
, tmp
);
92 if (ctx
->Driver
.ClearColor
) {
93 /* it's OK to call glClearColor in CI mode but it should be a NOP */
94 (*ctx
->Driver
.ClearColor
)(ctx
, ctx
->Color
.ClearColor
);
100 * GL_EXT_texture_integer
103 _mesa_ClearColorIiEXT(GLint r
, GLint g
, GLint b
, GLint a
)
106 GET_CURRENT_CONTEXT(ctx
);
107 ASSERT_OUTSIDE_BEGIN_END(ctx
);
109 tmp
[0] = (GLfloat
) r
;
110 tmp
[1] = (GLfloat
) g
;
111 tmp
[2] = (GLfloat
) b
;
112 tmp
[3] = (GLfloat
) a
;
114 if (TEST_EQ_4V(tmp
, ctx
->Color
.ClearColor
))
115 return; /* no change */
117 FLUSH_VERTICES(ctx
, _NEW_COLOR
);
119 /* XXX we should eventually have a float/int/uint union for
120 * the ctx->Color.ClearColor state.
122 COPY_4V(ctx
->Color
.ClearColor
, tmp
);
124 if (ctx
->Driver
.ClearColor
) {
125 ctx
->Driver
.ClearColor(ctx
, ctx
->Color
.ClearColor
);
131 * GL_EXT_texture_integer
134 _mesa_ClearColorIuiEXT(GLuint r
, GLuint g
, GLuint b
, GLuint a
)
137 GET_CURRENT_CONTEXT(ctx
);
138 ASSERT_OUTSIDE_BEGIN_END(ctx
);
140 tmp
[0] = (GLfloat
) r
;
141 tmp
[1] = (GLfloat
) g
;
142 tmp
[2] = (GLfloat
) b
;
143 tmp
[3] = (GLfloat
) a
;
145 if (TEST_EQ_4V(tmp
, ctx
->Color
.ClearColor
))
146 return; /* no change */
148 FLUSH_VERTICES(ctx
, _NEW_COLOR
);
150 /* XXX we should eventually have a float/int/uint union for
151 * the ctx->Color.ClearColor state.
153 COPY_4V(ctx
->Color
.ClearColor
, tmp
);
155 if (ctx
->Driver
.ClearColor
) {
156 ctx
->Driver
.ClearColor(ctx
, ctx
->Color
.ClearColor
);
164 * \param mask bit-mask indicating the buffers to be cleared.
166 * Flushes the vertices and verifies the parameter. If __struct gl_contextRec::NewState
167 * is set then calls _mesa_update_state() to update gl_frame_buffer::_Xmin,
168 * etc. If the rasterization mode is set to GL_RENDER then requests the driver
169 * to clear the buffers, via the dd_function_table::Clear callback.
172 _mesa_Clear( GLbitfield mask
)
174 GET_CURRENT_CONTEXT(ctx
);
175 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
177 FLUSH_CURRENT(ctx
, 0);
179 if (MESA_VERBOSE
& VERBOSE_API
)
180 _mesa_debug(ctx
, "glClear 0x%x\n", mask
);
182 if (mask
& ~(GL_COLOR_BUFFER_BIT
|
183 GL_DEPTH_BUFFER_BIT
|
184 GL_STENCIL_BUFFER_BIT
|
185 GL_ACCUM_BUFFER_BIT
)) {
186 /* invalid bit set */
187 _mesa_error( ctx
, GL_INVALID_VALUE
, "glClear(0x%x)", mask
);
192 _mesa_update_state( ctx
); /* update _Xmin, etc */
195 if (ctx
->DrawBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
196 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
197 "glClear(incomplete framebuffer)");
201 if (ctx
->DrawBuffer
->Width
== 0 || ctx
->DrawBuffer
->Height
== 0 ||
202 ctx
->DrawBuffer
->_Xmin
>= ctx
->DrawBuffer
->_Xmax
||
203 ctx
->DrawBuffer
->_Ymin
>= ctx
->DrawBuffer
->_Ymax
)
206 if (ctx
->RenderMode
== GL_RENDER
) {
207 GLbitfield bufferMask
;
209 /* don't clear depth buffer if depth writing disabled */
210 if (!ctx
->Depth
.Mask
)
211 mask
&= ~GL_DEPTH_BUFFER_BIT
;
213 /* Build the bitmask to send to device driver's Clear function.
214 * Note that the GL_COLOR_BUFFER_BIT flag will expand to 0, 1, 2 or 4
215 * of the BUFFER_BIT_FRONT/BACK_LEFT/RIGHT flags, or one of the
216 * BUFFER_BIT_COLORn flags.
219 if (mask
& GL_COLOR_BUFFER_BIT
) {
221 for (i
= 0; i
< ctx
->DrawBuffer
->_NumColorDrawBuffers
; i
++) {
222 bufferMask
|= (1 << ctx
->DrawBuffer
->_ColorDrawBufferIndexes
[i
]);
226 if ((mask
& GL_DEPTH_BUFFER_BIT
)
227 && ctx
->DrawBuffer
->Visual
.haveDepthBuffer
) {
228 bufferMask
|= BUFFER_BIT_DEPTH
;
231 if ((mask
& GL_STENCIL_BUFFER_BIT
)
232 && ctx
->DrawBuffer
->Visual
.haveStencilBuffer
) {
233 bufferMask
|= BUFFER_BIT_STENCIL
;
236 if ((mask
& GL_ACCUM_BUFFER_BIT
)
237 && ctx
->DrawBuffer
->Visual
.haveAccumBuffer
) {
238 bufferMask
|= BUFFER_BIT_ACCUM
;
241 ASSERT(ctx
->Driver
.Clear
);
242 ctx
->Driver
.Clear(ctx
, bufferMask
);
247 /** Returned by make_color_buffer_mask() for errors */
248 #define INVALID_MASK ~0x0
252 * Convert the glClearBuffer 'drawbuffer' parameter into a bitmask of
253 * BUFFER_BIT_x values.
254 * Return INVALID_MASK if the drawbuffer value is invalid.
257 make_color_buffer_mask(struct gl_context
*ctx
, GLint drawbuffer
)
259 const struct gl_renderbuffer_attachment
*att
= ctx
->DrawBuffer
->Attachment
;
260 GLbitfield mask
= 0x0;
262 switch (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
;
298 if (drawbuffer
< 0 || drawbuffer
>= (GLint
)ctx
->Const
.MaxDrawBuffers
) {
301 else if (att
[BUFFER_COLOR0
+ drawbuffer
].Renderbuffer
) {
302 mask
|= (BUFFER_BIT_COLOR0
<< drawbuffer
);
313 * Clear signed integer color buffer or stencil buffer (not depth).
316 _mesa_ClearBufferiv(GLenum buffer
, GLint drawbuffer
, const GLint
*value
)
318 GET_CURRENT_CONTEXT(ctx
);
319 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
321 FLUSH_CURRENT(ctx
, 0);
324 _mesa_update_state( ctx
);
329 if (drawbuffer
!= 0) {
330 _mesa_error(ctx
, GL_INVALID_VALUE
, "glClearBufferiv(drawbuffer=%d)",
335 /* Save current stencil clear value, set to 'value', do the
336 * stencil clear and restore the clear value.
337 * XXX in the future we may have a new ctx->Driver.ClearBuffer()
340 const GLuint clearSave
= ctx
->Stencil
.Clear
;
341 ctx
->Stencil
.Clear
= *value
;
342 if (ctx
->Driver
.ClearStencil
)
343 ctx
->Driver
.ClearStencil(ctx
, *value
);
344 ctx
->Driver
.Clear(ctx
, BUFFER_BIT_STENCIL
);
345 ctx
->Stencil
.Clear
= clearSave
;
346 if (ctx
->Driver
.ClearStencil
)
347 ctx
->Driver
.ClearStencil(ctx
, clearSave
);
352 const GLbitfield mask
= make_color_buffer_mask(ctx
, drawbuffer
);
353 if (mask
== INVALID_MASK
) {
354 _mesa_error(ctx
, GL_INVALID_VALUE
, "glClearBufferiv(drawbuffer=%d)",
359 /* XXX note: we're putting the integer clear values into the
360 * floating point state var. This will not always work. We'll
361 * need a new ctx->Driver.ClearBuffer() hook....
363 GLclampf clearSave
[4];
365 COPY_4V(clearSave
, ctx
->Color
.ClearColor
);
367 COPY_4V_CAST(ctx
->Color
.ClearColor
, value
, GLclampf
);
368 if (ctx
->Driver
.ClearColor
)
369 ctx
->Driver
.ClearColor(ctx
, ctx
->Color
.ClearColor
);
370 /* clear buffer(s) */
371 ctx
->Driver
.Clear(ctx
, mask
);
373 COPY_4V(ctx
->Color
.ClearColor
, clearSave
);
374 if (ctx
->Driver
.ClearColor
)
375 ctx
->Driver
.ClearColor(ctx
, clearSave
);
380 _mesa_error(ctx
, GL_INVALID_ENUM
, "glClearBufferiv(buffer=%s)",
381 _mesa_lookup_enum_by_nr(buffer
));
389 * Clear unsigned integer color buffer (not depth, not stencil).
392 _mesa_ClearBufferuiv(GLenum buffer
, GLint drawbuffer
, const GLuint
*value
)
394 GET_CURRENT_CONTEXT(ctx
);
395 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
397 FLUSH_CURRENT(ctx
, 0);
400 _mesa_update_state( ctx
);
406 const GLbitfield mask
= make_color_buffer_mask(ctx
, drawbuffer
);
407 if (mask
== INVALID_MASK
) {
408 _mesa_error(ctx
, GL_INVALID_VALUE
, "glClearBufferuiv(drawbuffer=%d)",
413 /* XXX note: we're putting the uint clear values into the
414 * floating point state var. This will not always work. We'll
415 * need a new ctx->Driver.ClearBuffer() hook....
417 GLclampf clearSave
[4];
419 COPY_4V(clearSave
, ctx
->Color
.ClearColor
);
421 COPY_4V_CAST(ctx
->Color
.ClearColor
, value
, GLclampf
);
422 if (ctx
->Driver
.ClearColor
)
423 ctx
->Driver
.ClearColor(ctx
, ctx
->Color
.ClearColor
);
424 /* clear buffer(s) */
425 ctx
->Driver
.Clear(ctx
, mask
);
427 COPY_4V(ctx
->Color
.ClearColor
, clearSave
);
428 if (ctx
->Driver
.ClearColor
)
429 ctx
->Driver
.ClearColor(ctx
, clearSave
);
434 _mesa_error(ctx
, GL_INVALID_ENUM
, "glClearBufferuiv(buffer=%s)",
435 _mesa_lookup_enum_by_nr(buffer
));
443 * Clear fixed-pt or float color buffer or depth buffer (not stencil).
446 _mesa_ClearBufferfv(GLenum buffer
, GLint drawbuffer
, const GLfloat
*value
)
448 GET_CURRENT_CONTEXT(ctx
);
449 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
451 FLUSH_CURRENT(ctx
, 0);
454 _mesa_update_state( ctx
);
459 if (drawbuffer
!= 0) {
460 _mesa_error(ctx
, GL_INVALID_VALUE
, "glClearBufferfv(drawbuffer=%d)",
465 /* Save current depth clear value, set to 'value', do the
466 * depth clear and restore the clear value.
467 * XXX in the future we may have a new ctx->Driver.ClearBuffer()
470 const GLclampd clearSave
= ctx
->Depth
.Clear
;
471 ctx
->Depth
.Clear
= *value
;
472 if (ctx
->Driver
.ClearDepth
)
473 ctx
->Driver
.ClearDepth(ctx
, *value
);
474 ctx
->Driver
.Clear(ctx
, BUFFER_BIT_DEPTH
);
475 ctx
->Depth
.Clear
= clearSave
;
476 if (ctx
->Driver
.ClearDepth
)
477 ctx
->Driver
.ClearDepth(ctx
, clearSave
);
479 /* clear depth buffer to value */
483 const GLbitfield mask
= make_color_buffer_mask(ctx
, drawbuffer
);
484 if (mask
== INVALID_MASK
) {
485 _mesa_error(ctx
, GL_INVALID_VALUE
, "glClearBufferfv(drawbuffer=%d)",
490 GLclampf clearSave
[4];
492 COPY_4V(clearSave
, ctx
->Color
.ClearColor
);
494 COPY_4V_CAST(ctx
->Color
.ClearColor
, value
, GLclampf
);
495 if (ctx
->Driver
.ClearColor
)
496 ctx
->Driver
.ClearColor(ctx
, ctx
->Color
.ClearColor
);
497 /* clear buffer(s) */
498 ctx
->Driver
.Clear(ctx
, mask
);
500 COPY_4V(ctx
->Color
.ClearColor
, clearSave
);
501 if (ctx
->Driver
.ClearColor
)
502 ctx
->Driver
.ClearColor(ctx
, clearSave
);
507 _mesa_error(ctx
, GL_INVALID_ENUM
, "glClearBufferfv(buffer=%s)",
508 _mesa_lookup_enum_by_nr(buffer
));
516 * Clear depth/stencil buffer only.
519 _mesa_ClearBufferfi(GLenum buffer
, GLint drawbuffer
,
520 GLfloat depth
, GLint stencil
)
522 GET_CURRENT_CONTEXT(ctx
);
523 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
525 FLUSH_CURRENT(ctx
, 0);
527 if (buffer
!= GL_DEPTH_STENCIL
) {
528 _mesa_error(ctx
, GL_INVALID_ENUM
, "glClearBufferfi(buffer=%s)",
529 _mesa_lookup_enum_by_nr(buffer
));
533 if (drawbuffer
!= 0) {
534 _mesa_error(ctx
, GL_INVALID_VALUE
, "glClearBufferfi(drawbuffer=%d)",
540 _mesa_update_state( ctx
);
544 /* save current clear values */
545 const GLclampd clearDepthSave
= ctx
->Depth
.Clear
;
546 const GLuint clearStencilSave
= ctx
->Stencil
.Clear
;
548 /* set new clear values */
549 ctx
->Depth
.Clear
= depth
;
550 ctx
->Stencil
.Clear
= stencil
;
551 if (ctx
->Driver
.ClearDepth
)
552 ctx
->Driver
.ClearDepth(ctx
, depth
);
553 if (ctx
->Driver
.ClearStencil
)
554 ctx
->Driver
.ClearStencil(ctx
, stencil
);
557 ctx
->Driver
.Clear(ctx
, BUFFER_BIT_DEPTH
| BUFFER_BIT_STENCIL
);
560 ctx
->Depth
.Clear
= clearDepthSave
;
561 ctx
->Stencil
.Clear
= clearStencilSave
;
562 if (ctx
->Driver
.ClearDepth
)
563 ctx
->Driver
.ClearDepth(ctx
, clearDepthSave
);
564 if (ctx
->Driver
.ClearStencil
)
565 ctx
->Driver
.ClearStencil(ctx
, clearStencilSave
);