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);
86 if (TEST_EQ_4V(tmp, ctx->Color.ClearColorUnclamped))
87 return; /* no change */
89 FLUSH_VERTICES(ctx, _NEW_COLOR);
90 COPY_4V(ctx->Color.ClearColorUnclamped, tmp);
92 ctx->Color.ClearColor[0] = CLAMP(tmp[0], 0.0F, 1.0F);
93 ctx->Color.ClearColor[1] = CLAMP(tmp[1], 0.0F, 1.0F);
94 ctx->Color.ClearColor[2] = CLAMP(tmp[2], 0.0F, 1.0F);
95 ctx->Color.ClearColor[3] = CLAMP(tmp[3], 0.0F, 1.0F);
97 if (ctx->Driver.ClearColor) {
98 /* it's OK to call glClearColor in CI mode but it should be a NOP */
99 /* we pass the clamped color, since all drivers that need this don't
100 * support GL_ARB_color_buffer_float
102 (*ctx->Driver.ClearColor)(ctx, ctx->Color.ClearColor);
108 * GL_EXT_texture_integer
111 _mesa_ClearColorIiEXT(GLint r, GLint g, GLint b, GLint a)
114 GET_CURRENT_CONTEXT(ctx);
115 ASSERT_OUTSIDE_BEGIN_END(ctx);
117 tmp[0] = (GLfloat) r;
118 tmp[1] = (GLfloat) g;
119 tmp[2] = (GLfloat) b;
120 tmp[3] = (GLfloat) a;
122 if (TEST_EQ_4V(tmp, ctx->Color.ClearColor))
123 return; /* no change */
125 FLUSH_VERTICES(ctx, _NEW_COLOR);
127 /* XXX we should eventually have a float/int/uint union for
128 * the ctx->Color.ClearColor state.
130 COPY_4V(ctx->Color.ClearColor, tmp);
132 if (ctx->Driver.ClearColor) {
133 ctx->Driver.ClearColor(ctx, ctx->Color.ClearColor);
139 * GL_EXT_texture_integer
142 _mesa_ClearColorIuiEXT(GLuint r, GLuint g, GLuint b, GLuint a)
145 GET_CURRENT_CONTEXT(ctx);
146 ASSERT_OUTSIDE_BEGIN_END(ctx);
148 tmp[0] = (GLfloat) r;
149 tmp[1] = (GLfloat) g;
150 tmp[2] = (GLfloat) b;
151 tmp[3] = (GLfloat) a;
153 if (TEST_EQ_4V(tmp, ctx->Color.ClearColor))
154 return; /* no change */
156 FLUSH_VERTICES(ctx, _NEW_COLOR);
158 /* XXX we should eventually have a float/int/uint union for
159 * the ctx->Color.ClearColor state.
161 COPY_4V(ctx->Color.ClearColor, tmp);
163 if (ctx->Driver.ClearColor) {
164 ctx->Driver.ClearColor(ctx, ctx->Color.ClearColor);
172 * \param mask bit-mask indicating the buffers to be cleared.
174 * Flushes the vertices and verifies the parameter. If __struct gl_contextRec::NewState
175 * is set then calls _mesa_update_state() to update gl_frame_buffer::_Xmin,
176 * etc. If the rasterization mode is set to GL_RENDER then requests the driver
177 * to clear the buffers, via the dd_function_table::Clear callback.
180 _mesa_Clear( GLbitfield mask )
182 GET_CURRENT_CONTEXT(ctx);
183 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
185 FLUSH_CURRENT(ctx, 0);
187 if (MESA_VERBOSE & VERBOSE_API)
188 _mesa_debug(ctx, "glClear 0x%x\n", mask);
190 if (mask & ~(GL_COLOR_BUFFER_BIT |
191 GL_DEPTH_BUFFER_BIT |
192 GL_STENCIL_BUFFER_BIT |
193 GL_ACCUM_BUFFER_BIT)) {
194 /* invalid bit set */
195 _mesa_error( ctx, GL_INVALID_VALUE, "glClear(0x%x)", mask);
200 _mesa_update_state( ctx ); /* update _Xmin, etc */
203 if (ctx->DrawBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
204 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
205 "glClear(incomplete framebuffer)");
209 if (ctx->DrawBuffer->Width == 0 || ctx->DrawBuffer->Height == 0 ||
210 ctx->DrawBuffer->_Xmin >= ctx->DrawBuffer->_Xmax ||
211 ctx->DrawBuffer->_Ymin >= ctx->DrawBuffer->_Ymax)
214 if (ctx->RenderMode == GL_RENDER) {
215 GLbitfield bufferMask;
217 /* don't clear depth buffer if depth writing disabled */
218 if (!ctx->Depth.Mask)
219 mask &= ~GL_DEPTH_BUFFER_BIT;
221 /* Build the bitmask to send to device driver's Clear function.
222 * Note that the GL_COLOR_BUFFER_BIT flag will expand to 0, 1, 2 or 4
223 * of the BUFFER_BIT_FRONT/BACK_LEFT/RIGHT flags, or one of the
224 * BUFFER_BIT_COLORn flags.
227 if (mask & GL_COLOR_BUFFER_BIT) {
229 for (i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; i++) {
230 bufferMask |= (1 << ctx->DrawBuffer->_ColorDrawBufferIndexes[i]);
234 if ((mask & GL_DEPTH_BUFFER_BIT)
235 && ctx->DrawBuffer->Visual.haveDepthBuffer) {
236 bufferMask |= BUFFER_BIT_DEPTH;
239 if ((mask & GL_STENCIL_BUFFER_BIT)
240 && ctx->DrawBuffer->Visual.haveStencilBuffer) {
241 bufferMask |= BUFFER_BIT_STENCIL;
244 if ((mask & GL_ACCUM_BUFFER_BIT)
245 && ctx->DrawBuffer->Visual.haveAccumBuffer) {
246 bufferMask |= BUFFER_BIT_ACCUM;
249 ASSERT(ctx->Driver.Clear);
250 ctx->Driver.Clear(ctx, bufferMask);
255 /** Returned by make_color_buffer_mask() for errors */
256 #define INVALID_MASK ~0x0
260 * Convert the glClearBuffer 'drawbuffer' parameter into a bitmask of
261 * BUFFER_BIT_x values.
262 * Return INVALID_MASK if the drawbuffer value is invalid.
265 make_color_buffer_mask(struct gl_context *ctx, GLint drawbuffer)
267 const struct gl_renderbuffer_attachment *att = ctx->DrawBuffer->Attachment;
268 GLbitfield mask = 0x0;
270 switch (drawbuffer) {
272 if (att[BUFFER_FRONT_LEFT].Renderbuffer)
273 mask |= BUFFER_BIT_FRONT_LEFT;
274 if (att[BUFFER_FRONT_RIGHT].Renderbuffer)
275 mask |= BUFFER_BIT_FRONT_RIGHT;
278 if (att[BUFFER_BACK_LEFT].Renderbuffer)
279 mask |= BUFFER_BIT_BACK_LEFT;
280 if (att[BUFFER_BACK_RIGHT].Renderbuffer)
281 mask |= BUFFER_BIT_BACK_RIGHT;
284 if (att[BUFFER_FRONT_LEFT].Renderbuffer)
285 mask |= BUFFER_BIT_FRONT_LEFT;
286 if (att[BUFFER_BACK_LEFT].Renderbuffer)
287 mask |= BUFFER_BIT_BACK_LEFT;
290 if (att[BUFFER_FRONT_RIGHT].Renderbuffer)
291 mask |= BUFFER_BIT_FRONT_RIGHT;
292 if (att[BUFFER_BACK_RIGHT].Renderbuffer)
293 mask |= BUFFER_BIT_BACK_RIGHT;
295 case GL_FRONT_AND_BACK:
296 if (att[BUFFER_FRONT_LEFT].Renderbuffer)
297 mask |= BUFFER_BIT_FRONT_LEFT;
298 if (att[BUFFER_BACK_LEFT].Renderbuffer)
299 mask |= BUFFER_BIT_BACK_LEFT;
300 if (att[BUFFER_FRONT_RIGHT].Renderbuffer)
301 mask |= BUFFER_BIT_FRONT_RIGHT;
302 if (att[BUFFER_BACK_RIGHT].Renderbuffer)
303 mask |= BUFFER_BIT_BACK_RIGHT;
306 if (drawbuffer < 0 || drawbuffer >= (GLint)ctx->Const.MaxDrawBuffers) {
309 else if (att[BUFFER_COLOR0 + drawbuffer].Renderbuffer) {
310 mask |= (BUFFER_BIT_COLOR0 << drawbuffer);
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 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
329 FLUSH_CURRENT(ctx, 0);
332 _mesa_update_state( ctx );
337 if (drawbuffer != 0) {
338 _mesa_error(ctx, GL_INVALID_VALUE, "glClearBufferiv(drawbuffer=%d)",
343 /* Save current stencil clear value, set to 'value', do the
344 * stencil clear and restore the clear value.
345 * XXX in the future we may have a new ctx->Driver.ClearBuffer()
348 const GLuint clearSave = ctx->Stencil.Clear;
349 ctx->Stencil.Clear = *value;
350 if (ctx->Driver.ClearStencil)
351 ctx->Driver.ClearStencil(ctx, *value);
352 ctx->Driver.Clear(ctx, BUFFER_BIT_STENCIL);
353 ctx->Stencil.Clear = clearSave;
354 if (ctx->Driver.ClearStencil)
355 ctx->Driver.ClearStencil(ctx, clearSave);
360 const GLbitfield mask = make_color_buffer_mask(ctx, drawbuffer);
361 if (mask == INVALID_MASK) {
362 _mesa_error(ctx, GL_INVALID_VALUE, "glClearBufferiv(drawbuffer=%d)",
367 /* XXX note: we're putting the integer clear values into the
368 * floating point state var. This will not always work. We'll
369 * need a new ctx->Driver.ClearBuffer() hook....
371 GLclampf clearSave[4];
373 COPY_4V(clearSave, ctx->Color.ClearColor);
375 COPY_4V_CAST(ctx->Color.ClearColor, value, GLclampf);
376 if (ctx->Driver.ClearColor)
377 ctx->Driver.ClearColor(ctx, ctx->Color.ClearColor);
378 /* clear buffer(s) */
379 ctx->Driver.Clear(ctx, mask);
381 COPY_4V(ctx->Color.ClearColor, clearSave);
382 if (ctx->Driver.ClearColor)
383 ctx->Driver.ClearColor(ctx, clearSave);
388 _mesa_error(ctx, GL_INVALID_ENUM, "glClearBufferiv(buffer=%s)",
389 _mesa_lookup_enum_by_nr(buffer));
397 * Clear unsigned integer color buffer (not depth, not stencil).
400 _mesa_ClearBufferuiv(GLenum buffer, GLint drawbuffer, const GLuint *value)
402 GET_CURRENT_CONTEXT(ctx);
403 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
405 FLUSH_CURRENT(ctx, 0);
408 _mesa_update_state( ctx );
414 const GLbitfield mask = make_color_buffer_mask(ctx, drawbuffer);
415 if (mask == INVALID_MASK) {
416 _mesa_error(ctx, GL_INVALID_VALUE, "glClearBufferuiv(drawbuffer=%d)",
421 /* XXX note: we're putting the uint clear values into the
422 * floating point state var. This will not always work. We'll
423 * need a new ctx->Driver.ClearBuffer() hook....
425 GLclampf clearSave[4];
427 COPY_4V(clearSave, ctx->Color.ClearColor);
429 COPY_4V_CAST(ctx->Color.ClearColor, value, GLclampf);
430 if (ctx->Driver.ClearColor)
431 ctx->Driver.ClearColor(ctx, ctx->Color.ClearColor);
432 /* clear buffer(s) */
433 ctx->Driver.Clear(ctx, mask);
435 COPY_4V(ctx->Color.ClearColor, clearSave);
436 if (ctx->Driver.ClearColor)
437 ctx->Driver.ClearColor(ctx, clearSave);
442 _mesa_error(ctx, GL_INVALID_ENUM, "glClearBufferuiv(buffer=%s)",
443 _mesa_lookup_enum_by_nr(buffer));
451 * Clear fixed-pt or float color buffer or depth buffer (not stencil).
454 _mesa_ClearBufferfv(GLenum buffer, GLint drawbuffer, const GLfloat *value)
456 GET_CURRENT_CONTEXT(ctx);
457 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
459 FLUSH_CURRENT(ctx, 0);
462 _mesa_update_state( ctx );
467 if (drawbuffer != 0) {
468 _mesa_error(ctx, GL_INVALID_VALUE, "glClearBufferfv(drawbuffer=%d)",
473 /* Save current depth clear value, set to 'value', do the
474 * depth clear and restore the clear value.
475 * XXX in the future we may have a new ctx->Driver.ClearBuffer()
478 const GLclampd clearSave = ctx->Depth.Clear;
479 ctx->Depth.Clear = *value;
480 if (ctx->Driver.ClearDepth)
481 ctx->Driver.ClearDepth(ctx, *value);
482 ctx->Driver.Clear(ctx, BUFFER_BIT_DEPTH);
483 ctx->Depth.Clear = clearSave;
484 if (ctx->Driver.ClearDepth)
485 ctx->Driver.ClearDepth(ctx, clearSave);
487 /* clear depth buffer to value */
491 const GLbitfield mask = make_color_buffer_mask(ctx, drawbuffer);
492 if (mask == INVALID_MASK) {
493 _mesa_error(ctx, GL_INVALID_VALUE, "glClearBufferfv(drawbuffer=%d)",
498 GLclampf clearSave[4];
500 COPY_4V(clearSave, ctx->Color.ClearColor);
502 COPY_4V_CAST(ctx->Color.ClearColor, value, GLclampf);
503 if (ctx->Driver.ClearColor)
504 ctx->Driver.ClearColor(ctx, ctx->Color.ClearColor);
505 /* clear buffer(s) */
506 ctx->Driver.Clear(ctx, mask);
508 COPY_4V(ctx->Color.ClearColor, clearSave);
509 if (ctx->Driver.ClearColor)
510 ctx->Driver.ClearColor(ctx, clearSave);
515 _mesa_error(ctx, GL_INVALID_ENUM, "glClearBufferfv(buffer=%s)",
516 _mesa_lookup_enum_by_nr(buffer));
524 * Clear depth/stencil buffer only.
527 _mesa_ClearBufferfi(GLenum buffer, GLint drawbuffer,
528 GLfloat depth, GLint stencil)
530 GET_CURRENT_CONTEXT(ctx);
531 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
533 FLUSH_CURRENT(ctx, 0);
535 if (buffer != GL_DEPTH_STENCIL) {
536 _mesa_error(ctx, GL_INVALID_ENUM, "glClearBufferfi(buffer=%s)",
537 _mesa_lookup_enum_by_nr(buffer));
541 if (drawbuffer != 0) {
542 _mesa_error(ctx, GL_INVALID_VALUE, "glClearBufferfi(drawbuffer=%d)",
548 _mesa_update_state( ctx );
552 /* save current clear values */
553 const GLclampd clearDepthSave = ctx->Depth.Clear;
554 const GLuint clearStencilSave = ctx->Stencil.Clear;
556 /* set new clear values */
557 ctx->Depth.Clear = depth;
558 ctx->Stencil.Clear = stencil;
559 if (ctx->Driver.ClearDepth)
560 ctx->Driver.ClearDepth(ctx, depth);
561 if (ctx->Driver.ClearStencil)
562 ctx->Driver.ClearStencil(ctx, stencil);
565 ctx->Driver.Clear(ctx, BUFFER_BIT_DEPTH | BUFFER_BIT_STENCIL);
568 ctx->Depth.Clear = clearDepthSave;
569 ctx->Stencil.Clear = clearStencilSave;
570 if (ctx->Driver.ClearDepth)
571 ctx->Driver.ClearDepth(ctx, clearDepthSave);
572 if (ctx->Driver.ClearStencil)
573 ctx->Driver.ClearStencil(ctx, clearStencilSave);