[MESA] Addendum to r60576.
[reactos.git] / reactos / dll / opengl / 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 * \file clear.c
27 * glClearColor, glClearIndex, glClear() functions.
28 */
29
30 #include <precomp.h>
31
32 #if _HAVE_FULL_GL
33 void GLAPIENTRY
34 _mesa_ClearIndex( GLfloat c )
35 {
36 GET_CURRENT_CONTEXT(ctx);
37 ASSERT_OUTSIDE_BEGIN_END(ctx);
38
39 if (ctx->Color.ClearIndex == (GLuint) c)
40 return;
41
42 FLUSH_VERTICES(ctx, _NEW_COLOR);
43 ctx->Color.ClearIndex = (GLuint) c;
44 }
45 #endif
46
47
48 /**
49 * Specify the clear values for the color buffers.
50 *
51 * \param red red color component.
52 * \param green green color component.
53 * \param blue blue color component.
54 * \param alpha alpha component.
55 *
56 * \sa glClearColor().
57 *
58 * Clamps the parameters and updates gl_colorbuffer_attrib::ClearColor. On a
59 * change, flushes the vertices and notifies the driver via the
60 * dd_function_table::ClearColor callback.
61 */
62 void GLAPIENTRY
63 _mesa_ClearColor( GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha )
64 {
65 GLfloat tmp[4];
66 GET_CURRENT_CONTEXT(ctx);
67 ASSERT_OUTSIDE_BEGIN_END(ctx);
68
69 tmp[0] = red;
70 tmp[1] = green;
71 tmp[2] = blue;
72 tmp[3] = alpha;
73
74 if (TEST_EQ_4V(tmp, ctx->Color.ClearColor.f))
75 return; /* no change */
76
77 FLUSH_VERTICES(ctx, _NEW_COLOR);
78 COPY_4V(ctx->Color.ClearColor.f, tmp);
79
80 if (ctx->Driver.ClearColor) {
81 /* it's OK to call glClearColor in CI mode but it should be a NOP */
82 /* we pass the clamped color, since all drivers that need this don't
83 * support GL_ARB_color_buffer_float
84 */
85 (*ctx->Driver.ClearColor)(ctx, ctx->Color.ClearColor);
86 }
87 }
88
89
90 /**
91 * GL_EXT_texture_integer
92 */
93 void GLAPIENTRY
94 _mesa_ClearColorIiEXT(GLint r, GLint g, GLint b, GLint a)
95 {
96 GLint tmp[4];
97 GET_CURRENT_CONTEXT(ctx);
98 ASSERT_OUTSIDE_BEGIN_END(ctx);
99
100 tmp[0] = r;
101 tmp[1] = g;
102 tmp[2] = b;
103 tmp[3] = a;
104
105 if (TEST_EQ_4V(tmp, ctx->Color.ClearColor.i))
106 return; /* no change */
107
108 FLUSH_VERTICES(ctx, _NEW_COLOR);
109 COPY_4V(ctx->Color.ClearColor.i, tmp);
110
111 /* these should be NOP calls for drivers supporting EXT_texture_integer */
112 if (ctx->Driver.ClearColor) {
113 ctx->Driver.ClearColor(ctx, ctx->Color.ClearColor);
114 }
115 }
116
117
118 /**
119 * GL_EXT_texture_integer
120 */
121 void GLAPIENTRY
122 _mesa_ClearColorIuiEXT(GLuint r, GLuint g, GLuint b, GLuint a)
123 {
124 GLuint tmp[4];
125 GET_CURRENT_CONTEXT(ctx);
126 ASSERT_OUTSIDE_BEGIN_END(ctx);
127
128 tmp[0] = r;
129 tmp[1] = g;
130 tmp[2] = b;
131 tmp[3] = a;
132
133 if (TEST_EQ_4V(tmp, ctx->Color.ClearColor.ui))
134 return; /* no change */
135
136 FLUSH_VERTICES(ctx, _NEW_COLOR);
137 COPY_4V(ctx->Color.ClearColor.ui, tmp);
138
139 /* these should be NOP calls for drivers supporting EXT_texture_integer */
140 if (ctx->Driver.ClearColor) {
141 ctx->Driver.ClearColor(ctx, ctx->Color.ClearColor);
142 }
143 }
144
145
146 /**
147 * Clear buffers.
148 *
149 * \param mask bit-mask indicating the buffers to be cleared.
150 *
151 * Flushes the vertices and verifies the parameter. If __struct gl_contextRec::NewState
152 * is set then calls _mesa_update_state() to update gl_frame_buffer::_Xmin,
153 * etc. If the rasterization mode is set to GL_RENDER then requests the driver
154 * to clear the buffers, via the dd_function_table::Clear callback.
155 */
156 void GLAPIENTRY
157 _mesa_Clear( GLbitfield mask )
158 {
159 GET_CURRENT_CONTEXT(ctx);
160 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
161
162 FLUSH_CURRENT(ctx, 0);
163
164 if (MESA_VERBOSE & VERBOSE_API)
165 _mesa_debug(ctx, "glClear 0x%x\n", mask);
166
167 if (mask & ~(GL_COLOR_BUFFER_BIT |
168 GL_DEPTH_BUFFER_BIT |
169 GL_STENCIL_BUFFER_BIT |
170 GL_ACCUM_BUFFER_BIT)) {
171 /* invalid bit set */
172 _mesa_error( ctx, GL_INVALID_VALUE, "glClear(0x%x)", mask);
173 return;
174 }
175
176 if (ctx->NewState) {
177 _mesa_update_state( ctx ); /* update _Xmin, etc */
178 }
179
180 if (ctx->DrawBuffer->Width == 0 || ctx->DrawBuffer->Height == 0 ||
181 ctx->DrawBuffer->_Xmin >= ctx->DrawBuffer->_Xmax ||
182 ctx->DrawBuffer->_Ymin >= ctx->DrawBuffer->_Ymax)
183 return;
184
185 if (ctx->RasterDiscard)
186 return;
187
188 if (ctx->RenderMode == GL_RENDER) {
189 GLbitfield bufferMask;
190
191 /* don't clear depth buffer if depth writing disabled */
192 if (!ctx->Depth.Mask)
193 mask &= ~GL_DEPTH_BUFFER_BIT;
194
195 /* Build the bitmask to send to device driver's Clear function.
196 * Note that the GL_COLOR_BUFFER_BIT flag will expand to 0, 1, 2 or 4
197 * of the BUFFER_BIT_FRONT/BACK_LEFT/RIGHT flags, or one of the
198 * BUFFER_BIT_COLORn flags.
199 */
200 bufferMask = 0;
201 if (mask & GL_COLOR_BUFFER_BIT) {
202 bufferMask |= (1 << ctx->DrawBuffer->_ColorDrawBufferIndex);
203 }
204
205 if ((mask & GL_DEPTH_BUFFER_BIT)
206 && ctx->DrawBuffer->Visual.haveDepthBuffer) {
207 bufferMask |= BUFFER_BIT_DEPTH;
208 }
209
210 if ((mask & GL_STENCIL_BUFFER_BIT)
211 && ctx->DrawBuffer->Visual.haveStencilBuffer) {
212 bufferMask |= BUFFER_BIT_STENCIL;
213 }
214
215 if ((mask & GL_ACCUM_BUFFER_BIT)
216 && ctx->DrawBuffer->Visual.haveAccumBuffer) {
217 bufferMask |= BUFFER_BIT_ACCUM;
218 }
219
220 ASSERT(ctx->Driver.Clear);
221 ctx->Driver.Clear(ctx, bufferMask);
222 }
223 }