Merge remote branch 'origin/mesa_7_6_branch'
[mesa.git] / src / mesa / main / clear.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.1
4 *
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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.
23 */
24
25
26 /**
27 * \file clear.c
28 * glClearColor, glClearIndex, glClear() functions.
29 */
30
31
32
33 #include "glheader.h"
34 #include "clear.h"
35 #include "context.h"
36 #include "colormac.h"
37 #include "state.h"
38
39
40
41 #if _HAVE_FULL_GL
42 void GLAPIENTRY
43 _mesa_ClearIndex( GLfloat c )
44 {
45 GET_CURRENT_CONTEXT(ctx);
46 ASSERT_OUTSIDE_BEGIN_END(ctx);
47
48 if (ctx->Color.ClearIndex == (GLuint) c)
49 return;
50
51 FLUSH_VERTICES(ctx, _NEW_COLOR);
52 ctx->Color.ClearIndex = (GLuint) c;
53
54 if (!ctx->Visual.rgbMode && ctx->Driver.ClearIndex) {
55 /* it's OK to call glClearIndex in RGBA mode but it should be a NOP */
56 (*ctx->Driver.ClearIndex)( ctx, ctx->Color.ClearIndex );
57 }
58 }
59 #endif
60
61
62 /**
63 * Specify the clear values for the color buffers.
64 *
65 * \param red red color component.
66 * \param green green color component.
67 * \param blue blue color component.
68 * \param alpha alpha component.
69 *
70 * \sa glClearColor().
71 *
72 * Clamps the parameters and updates gl_colorbuffer_attrib::ClearColor. On a
73 * change, flushes the vertices and notifies the driver via the
74 * dd_function_table::ClearColor callback.
75 */
76 void GLAPIENTRY
77 _mesa_ClearColor( GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha )
78 {
79 GLfloat tmp[4];
80 GET_CURRENT_CONTEXT(ctx);
81 ASSERT_OUTSIDE_BEGIN_END(ctx);
82
83 tmp[0] = CLAMP(red, 0.0F, 1.0F);
84 tmp[1] = CLAMP(green, 0.0F, 1.0F);
85 tmp[2] = CLAMP(blue, 0.0F, 1.0F);
86 tmp[3] = CLAMP(alpha, 0.0F, 1.0F);
87
88 if (TEST_EQ_4V(tmp, ctx->Color.ClearColor))
89 return; /* no change */
90
91 FLUSH_VERTICES(ctx, _NEW_COLOR);
92 COPY_4V(ctx->Color.ClearColor, tmp);
93
94 if (ctx->Visual.rgbMode && ctx->Driver.ClearColor) {
95 /* it's OK to call glClearColor in CI mode but it should be a NOP */
96 (*ctx->Driver.ClearColor)(ctx, ctx->Color.ClearColor);
97 }
98 }
99
100
101 /**
102 * Clear buffers.
103 *
104 * \param mask bit-mask indicating the buffers to be cleared.
105 *
106 * Flushes the vertices and verifies the parameter. If __GLcontextRec::NewState
107 * is set then calls _mesa_update_state() to update gl_frame_buffer::_Xmin,
108 * etc. If the rasterization mode is set to GL_RENDER then requests the driver
109 * to clear the buffers, via the dd_function_table::Clear callback.
110 */
111 void GLAPIENTRY
112 _mesa_Clear( GLbitfield mask )
113 {
114 GET_CURRENT_CONTEXT(ctx);
115 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
116
117 FLUSH_CURRENT(ctx, 0);
118
119 if (MESA_VERBOSE & VERBOSE_API)
120 _mesa_debug(ctx, "glClear 0x%x\n", mask);
121
122 if (mask & ~(GL_COLOR_BUFFER_BIT |
123 GL_DEPTH_BUFFER_BIT |
124 GL_STENCIL_BUFFER_BIT |
125 GL_ACCUM_BUFFER_BIT)) {
126 /* invalid bit set */
127 _mesa_error( ctx, GL_INVALID_VALUE, "glClear(0x%x)", mask);
128 return;
129 }
130
131 if (ctx->NewState) {
132 _mesa_update_state( ctx ); /* update _Xmin, etc */
133 }
134
135 if (ctx->DrawBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
136 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
137 "glClear(incomplete framebuffer)");
138 return;
139 }
140
141 if (ctx->DrawBuffer->Width == 0 || ctx->DrawBuffer->Height == 0 ||
142 ctx->DrawBuffer->_Xmin >= ctx->DrawBuffer->_Xmax ||
143 ctx->DrawBuffer->_Ymin >= ctx->DrawBuffer->_Ymax)
144 return;
145
146 if (ctx->RenderMode == GL_RENDER) {
147 GLbitfield bufferMask;
148
149 /* don't clear depth buffer if depth writing disabled */
150 if (!ctx->Depth.Mask)
151 mask &= ~GL_DEPTH_BUFFER_BIT;
152
153 /* Build the bitmask to send to device driver's Clear function.
154 * Note that the GL_COLOR_BUFFER_BIT flag will expand to 0, 1, 2 or 4
155 * of the BUFFER_BIT_FRONT/BACK_LEFT/RIGHT flags, or one of the
156 * BUFFER_BIT_COLORn flags.
157 */
158 bufferMask = 0;
159 if (mask & GL_COLOR_BUFFER_BIT) {
160 GLuint i;
161 for (i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; i++) {
162 bufferMask |= (1 << ctx->DrawBuffer->_ColorDrawBufferIndexes[i]);
163 }
164 }
165
166 if ((mask & GL_DEPTH_BUFFER_BIT)
167 && ctx->DrawBuffer->Visual.haveDepthBuffer) {
168 bufferMask |= BUFFER_BIT_DEPTH;
169 }
170
171 if ((mask & GL_STENCIL_BUFFER_BIT)
172 && ctx->DrawBuffer->Visual.haveStencilBuffer) {
173 bufferMask |= BUFFER_BIT_STENCIL;
174 }
175
176 if ((mask & GL_ACCUM_BUFFER_BIT)
177 && ctx->DrawBuffer->Visual.haveAccumBuffer) {
178 bufferMask |= BUFFER_BIT_ACCUM;
179 }
180
181 ASSERT(ctx->Driver.Clear);
182 ctx->Driver.Clear(ctx, bufferMask);
183 }
184 }