lots of gl_*() to _mesa_*() namespace clean-up
[mesa.git] / src / mesa / main / buffers.c
1 /* $Id: buffers.c,v 1.27 2001/03/03 20:33:27 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27
28 #ifdef PC_HEADER
29 #include "all.h"
30 #else
31 #include "glheader.h"
32 #include "accum.h"
33 #include "buffers.h"
34 #include "colormac.h"
35 #include "context.h"
36 #include "depth.h"
37 #include "enums.h"
38 #include "macros.h"
39 #include "masking.h"
40 #include "mem.h"
41 #include "stencil.h"
42 #include "state.h"
43 #include "mtypes.h"
44 #endif
45
46
47
48 void
49 _mesa_ClearIndex( GLfloat c )
50 {
51 GET_CURRENT_CONTEXT(ctx);
52 ASSERT_OUTSIDE_BEGIN_END(ctx);
53
54 if (ctx->Color.ClearIndex == (GLuint) c)
55 return;
56
57 FLUSH_VERTICES(ctx, _NEW_COLOR);
58 ctx->Color.ClearIndex = (GLuint) c;
59
60 if (!ctx->Visual.rgbMode && ctx->Driver.ClearIndex) {
61 /* it's OK to call glClearIndex in RGBA mode but it should be a NOP */
62 (*ctx->Driver.ClearIndex)( ctx, ctx->Color.ClearIndex );
63 }
64 }
65
66
67
68 void
69 _mesa_ClearColor( GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha )
70 {
71 GLchan tmp[4];
72 GET_CURRENT_CONTEXT(ctx);
73 ASSERT_OUTSIDE_BEGIN_END(ctx);
74
75 UNCLAMPED_FLOAT_TO_CHAN(tmp[0], red);
76 UNCLAMPED_FLOAT_TO_CHAN(tmp[1], green);
77 UNCLAMPED_FLOAT_TO_CHAN(tmp[2], blue);
78 UNCLAMPED_FLOAT_TO_CHAN(tmp[3], alpha);
79
80 if (TEST_EQ_4V(tmp, ctx->Color.ClearColor))
81 return;
82
83 FLUSH_VERTICES(ctx, _NEW_COLOR);
84 COPY_CHAN4(ctx->Color.ClearColor, tmp);
85
86 if (ctx->Visual.rgbMode && ctx->Driver.ClearColor) {
87 /* it's OK to call glClearColor in CI mode but it should be a NOP */
88 (*ctx->Driver.ClearColor)(ctx, ctx->Color.ClearColor);
89 }
90 }
91
92
93
94 void
95 _mesa_Clear( GLbitfield mask )
96 {
97 GET_CURRENT_CONTEXT(ctx);
98 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
99
100 if (MESA_VERBOSE & VERBOSE_API)
101 fprintf(stderr, "glClear 0x%x\n", mask);
102
103 if (ctx->NewState) {
104 _mesa_update_state( ctx ); /* update _Xmin, etc */
105 }
106
107 if (ctx->RenderMode==GL_RENDER) {
108 const GLint x = ctx->DrawBuffer->_Xmin;
109 const GLint y = ctx->DrawBuffer->_Ymin;
110 const GLint height = ctx->DrawBuffer->_Ymax - ctx->DrawBuffer->_Ymin;
111 const GLint width = ctx->DrawBuffer->_Xmax - ctx->DrawBuffer->_Xmin;
112 GLbitfield ddMask;
113
114 /* don't clear depth buffer if depth writing disabled */
115 if (!ctx->Depth.Mask)
116 CLEAR_BITS(mask, GL_DEPTH_BUFFER_BIT);
117
118 /* Build bitmask to send to driver Clear function */
119 ddMask = mask & (GL_DEPTH_BUFFER_BIT |
120 GL_STENCIL_BUFFER_BIT |
121 GL_ACCUM_BUFFER_BIT);
122 if (mask & GL_COLOR_BUFFER_BIT) {
123 ddMask |= ctx->Color.DrawDestMask;
124 }
125
126 ASSERT(ctx->Driver.Clear);
127 ctx->Driver.Clear( ctx, ddMask, !ctx->Scissor.Enabled,
128 x, y, width, height );
129 }
130 }
131
132
133 void
134 _mesa_DrawBuffer( GLenum mode )
135 {
136 GET_CURRENT_CONTEXT(ctx);
137 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); /* too complex... */
138
139
140 if (MESA_VERBOSE & VERBOSE_API)
141 fprintf(stderr, "glDrawBuffer %s\n", _mesa_lookup_enum_by_nr(mode));
142
143 switch (mode) {
144 case GL_AUX0:
145 case GL_AUX1:
146 case GL_AUX2:
147 case GL_AUX3:
148 /* AUX buffers not implemented in Mesa at this time */
149 _mesa_error( ctx, GL_INVALID_OPERATION, "glDrawBuffer" );
150 return;
151 case GL_RIGHT:
152 if (!ctx->Visual.stereoMode) {
153 _mesa_error( ctx, GL_INVALID_OPERATION, "glDrawBuffer" );
154 return;}
155 if (ctx->Visual.doubleBufferMode)
156 ctx->Color.DrawDestMask = FRONT_RIGHT_BIT | BACK_RIGHT_BIT;
157 else
158 ctx->Color.DrawDestMask = FRONT_RIGHT_BIT;
159 break;
160 case GL_FRONT_RIGHT:
161 if (!ctx->Visual.stereoMode) {
162 _mesa_error( ctx, GL_INVALID_OPERATION, "glDrawBuffer" );
163 return;
164 }
165 ctx->Color.DrawDestMask = FRONT_RIGHT_BIT;
166 break;
167 case GL_BACK_RIGHT:
168 if (!ctx->Visual.stereoMode) {
169 _mesa_error( ctx, GL_INVALID_OPERATION, "glDrawBuffer" );
170 return;
171 }
172 if (!ctx->Visual.doubleBufferMode) {
173 _mesa_error( ctx, GL_INVALID_OPERATION, "glDrawBuffer" );
174 return;
175 }
176 ctx->Color.DrawDestMask = BACK_RIGHT_BIT;
177 break;
178 case GL_BACK_LEFT:
179 if (!ctx->Visual.doubleBufferMode) {
180 _mesa_error( ctx, GL_INVALID_OPERATION, "glDrawBuffer" );
181 return;
182 }
183 ctx->Color.DrawDestMask = BACK_LEFT_BIT;
184 break;
185 case GL_FRONT_AND_BACK:
186 if (!ctx->Visual.doubleBufferMode) {
187 _mesa_error( ctx, GL_INVALID_OPERATION, "glDrawBuffer" );
188 return;
189 }
190 if (ctx->Visual.stereoMode)
191 ctx->Color.DrawDestMask = FRONT_LEFT_BIT | BACK_LEFT_BIT
192 | FRONT_RIGHT_BIT | BACK_RIGHT_BIT;
193 else
194 ctx->Color.DrawDestMask = FRONT_LEFT_BIT | BACK_LEFT_BIT;
195 break;
196 case GL_BACK:
197 if (!ctx->Visual.doubleBufferMode) {
198 _mesa_error( ctx, GL_INVALID_OPERATION, "glDrawBuffer" );
199 return;
200 }
201 if (ctx->Visual.stereoMode)
202 ctx->Color.DrawDestMask = BACK_LEFT_BIT | BACK_RIGHT_BIT;
203 else
204 ctx->Color.DrawDestMask = BACK_LEFT_BIT;
205 break;
206 case GL_LEFT:
207 /* never an error */
208 if (ctx->Visual.doubleBufferMode)
209 ctx->Color.DrawDestMask = FRONT_LEFT_BIT | BACK_LEFT_BIT;
210 else
211 ctx->Color.DrawDestMask = FRONT_LEFT_BIT;
212 break;
213 case GL_FRONT_LEFT:
214 /* never an error */
215 ctx->Color.DrawDestMask = FRONT_LEFT_BIT;
216 break;
217 case GL_FRONT:
218 /* never an error */
219 if (ctx->Visual.stereoMode)
220 ctx->Color.DrawDestMask = FRONT_LEFT_BIT | FRONT_RIGHT_BIT;
221 else
222 ctx->Color.DrawDestMask = FRONT_LEFT_BIT;
223 break;
224 case GL_NONE:
225 /* never an error */
226 ctx->Color.DrawDestMask = 0;
227 break;
228 default:
229 _mesa_error( ctx, GL_INVALID_ENUM, "glDrawBuffer" );
230 return;
231 }
232
233 /*
234 * Make the dest buffer mode more precise if possible
235 */
236 if (mode == GL_LEFT && !ctx->Visual.doubleBufferMode)
237 ctx->Color.DriverDrawBuffer = GL_FRONT_LEFT;
238 else if (mode == GL_RIGHT && !ctx->Visual.doubleBufferMode)
239 ctx->Color.DriverDrawBuffer = GL_FRONT_RIGHT;
240 else if (mode == GL_FRONT && !ctx->Visual.stereoMode)
241 ctx->Color.DriverDrawBuffer = GL_FRONT_LEFT;
242 else if (mode == GL_BACK && !ctx->Visual.stereoMode)
243 ctx->Color.DriverDrawBuffer = GL_BACK_LEFT;
244 else
245 ctx->Color.DriverDrawBuffer = mode;
246
247 /*
248 * Set current alpha buffer pointer
249 */
250 if (ctx->DrawBuffer->UseSoftwareAlphaBuffers) {
251 if (ctx->Color.DriverDrawBuffer == GL_FRONT_LEFT)
252 ctx->DrawBuffer->Alpha = ctx->DrawBuffer->FrontLeftAlpha;
253 else if (ctx->Color.DriverDrawBuffer == GL_BACK_LEFT)
254 ctx->DrawBuffer->Alpha = ctx->DrawBuffer->BackLeftAlpha;
255 else if (ctx->Color.DriverDrawBuffer == GL_FRONT_RIGHT)
256 ctx->DrawBuffer->Alpha = ctx->DrawBuffer->FrontRightAlpha;
257 else if (ctx->Color.DriverDrawBuffer == GL_BACK_RIGHT)
258 ctx->DrawBuffer->Alpha = ctx->DrawBuffer->BackRightAlpha;
259 }
260
261 /*
262 * If we get here there can't have been an error.
263 * Now see if device driver can implement the drawing to the target
264 * buffer(s). The driver may not be able to do GL_FRONT_AND_BACK mode
265 * for example. We'll take care of that in the core code by looping
266 * over the individual buffers.
267 */
268 ASSERT(ctx->Driver.SetDrawBuffer);
269 if ( (*ctx->Driver.SetDrawBuffer)(ctx, ctx->Color.DriverDrawBuffer) ) {
270 /* All OK, the driver will do all buffer writes */
271 ctx->Color.MultiDrawBuffer = GL_FALSE;
272 }
273 else {
274 /* We'll have to loop over the multiple draw buffer targets */
275 ctx->Color.MultiDrawBuffer = GL_TRUE;
276 /* Set drawing buffer to front for now */
277 (void) (*ctx->Driver.SetDrawBuffer)(ctx, GL_FRONT_LEFT);
278 }
279
280 ctx->Color.DrawBuffer = mode;
281 ctx->NewState |= _NEW_COLOR;
282 }
283
284
285
286 void
287 _mesa_ReadBuffer( GLenum mode )
288 {
289 GET_CURRENT_CONTEXT(ctx);
290 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
291
292 if (MESA_VERBOSE & VERBOSE_API)
293 fprintf(stderr, "glReadBuffer %s\n", _mesa_lookup_enum_by_nr(mode));
294
295 switch (mode) {
296 case GL_AUX0:
297 case GL_AUX1:
298 case GL_AUX2:
299 case GL_AUX3:
300 /* AUX buffers not implemented in Mesa at this time */
301 _mesa_error( ctx, GL_INVALID_OPERATION, "glReadBuffer" );
302 return;
303 case GL_LEFT:
304 case GL_FRONT:
305 case GL_FRONT_LEFT:
306 /* Front-Left buffer, always exists */
307 ctx->Pixel.DriverReadBuffer = GL_FRONT_LEFT;
308 break;
309 case GL_BACK:
310 case GL_BACK_LEFT:
311 /* Back-Left buffer, requires double buffering */
312 if (!ctx->Visual.doubleBufferMode) {
313 _mesa_error( ctx, GL_INVALID_OPERATION, "glReadBuffer" );
314 return;
315 }
316 ctx->Pixel.DriverReadBuffer = GL_BACK_LEFT;
317 break;
318 case GL_FRONT_RIGHT:
319 case GL_RIGHT:
320 if (!ctx->Visual.stereoMode) {
321 _mesa_error( ctx, GL_INVALID_OPERATION, "glReadBuffer" );
322 return;
323 }
324 ctx->Pixel.DriverReadBuffer = GL_FRONT_RIGHT;
325 break;
326 case GL_BACK_RIGHT:
327 if (!ctx->Visual.stereoMode || !ctx->Visual.doubleBufferMode) {
328 _mesa_error( ctx, GL_INVALID_OPERATION, "glReadBuffer" );
329 return;
330 }
331 ctx->Pixel.DriverReadBuffer = GL_BACK_RIGHT;
332 break;
333 default:
334 _mesa_error( ctx, GL_INVALID_ENUM, "glReadBuffer" );
335 return;
336 }
337
338 ctx->Pixel.ReadBuffer = mode;
339 ctx->NewState |= _NEW_PIXEL;
340 }
341
342
343 /*
344 * GL_MESA_resize_buffers extension
345 */
346 void
347 _mesa_ResizeBuffersMESA( void )
348 {
349 GLcontext *ctx = _mesa_get_current_context();
350 GLuint buf_width, buf_height;
351 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH( ctx );
352
353
354 if (MESA_VERBOSE & VERBOSE_API)
355 fprintf(stderr, "glResizeBuffersMESA\n");
356
357 /* ask device driver for size of output buffer */
358 (*ctx->Driver.GetBufferSize)( ctx, &buf_width, &buf_height );
359
360 /* see if size of device driver's color buffer (window) has changed */
361 if (ctx->DrawBuffer->Width == (GLint) buf_width &&
362 ctx->DrawBuffer->Height == (GLint) buf_height)
363 return;
364
365 ctx->NewState |= _NEW_BUFFERS; /* to update scissor / window bounds */
366
367 /* save buffer size */
368 ctx->DrawBuffer->Width = buf_width;
369 ctx->DrawBuffer->Height = buf_height;
370
371 ctx->Driver.ResizeBuffersMESA( ctx );
372 }