added a comment
[mesa.git] / src / mesa / main / buffers.c
1 /* $Id: buffers.c,v 1.36 2002/06/15 02:38:15 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 4.1
6 *
7 * Copyright (C) 1999-2002 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 "mem.h"
40 #include "stencil.h"
41 #include "state.h"
42 #include "mtypes.h"
43 #endif
44
45
46
47 void
48 _mesa_ClearIndex( GLfloat c )
49 {
50 GET_CURRENT_CONTEXT(ctx);
51 ASSERT_OUTSIDE_BEGIN_END(ctx);
52
53 if (ctx->Color.ClearIndex == (GLuint) c)
54 return;
55
56 FLUSH_VERTICES(ctx, _NEW_COLOR);
57 ctx->Color.ClearIndex = (GLuint) c;
58
59 if (!ctx->Visual.rgbMode && ctx->Driver.ClearIndex) {
60 /* it's OK to call glClearIndex in RGBA mode but it should be a NOP */
61 (*ctx->Driver.ClearIndex)( ctx, ctx->Color.ClearIndex );
62 }
63 }
64
65
66
67 void
68 _mesa_ClearColor( GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha )
69 {
70 GLchan tmp[4];
71 GET_CURRENT_CONTEXT(ctx);
72 ASSERT_OUTSIDE_BEGIN_END(ctx);
73
74 UNCLAMPED_FLOAT_TO_CHAN(tmp[0], red);
75 UNCLAMPED_FLOAT_TO_CHAN(tmp[1], green);
76 UNCLAMPED_FLOAT_TO_CHAN(tmp[2], blue);
77 UNCLAMPED_FLOAT_TO_CHAN(tmp[3], alpha);
78
79 if (TEST_EQ_4V(tmp, ctx->Color.ClearColor))
80 return;
81
82 FLUSH_VERTICES(ctx, _NEW_COLOR);
83 COPY_CHAN4(ctx->Color.ClearColor, tmp);
84
85 if (ctx->Visual.rgbMode && ctx->Driver.ClearColor) {
86 /* it's OK to call glClearColor in CI mode but it should be a NOP */
87 (*ctx->Driver.ClearColor)(ctx, ctx->Color.ClearColor);
88 }
89 }
90
91
92
93 void
94 _mesa_Clear( GLbitfield mask )
95 {
96 GET_CURRENT_CONTEXT(ctx);
97 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
98
99 if (MESA_VERBOSE & VERBOSE_API)
100 _mesa_debug(ctx, "glClear 0x%x\n", mask);
101
102 if (mask & ~(GL_COLOR_BUFFER_BIT |
103 GL_DEPTH_BUFFER_BIT |
104 GL_STENCIL_BUFFER_BIT |
105 GL_ACCUM_BUFFER_BIT)) {
106 /* invalid bit set */
107 _mesa_error( ctx, GL_INVALID_VALUE, "glClear(mask)");
108 return;
109 }
110
111 if (ctx->NewState) {
112 _mesa_update_state( ctx ); /* update _Xmin, etc */
113 }
114
115 if (ctx->RenderMode==GL_RENDER) {
116 const GLint x = ctx->DrawBuffer->_Xmin;
117 const GLint y = ctx->DrawBuffer->_Ymin;
118 const GLint height = ctx->DrawBuffer->_Ymax - ctx->DrawBuffer->_Ymin;
119 const GLint width = ctx->DrawBuffer->_Xmax - ctx->DrawBuffer->_Xmin;
120 GLbitfield ddMask;
121
122 /* don't clear depth buffer if depth writing disabled */
123 if (!ctx->Depth.Mask)
124 CLEAR_BITS(mask, GL_DEPTH_BUFFER_BIT);
125
126 /* Build bitmask to send to driver Clear function */
127 ddMask = mask & (GL_DEPTH_BUFFER_BIT |
128 GL_STENCIL_BUFFER_BIT |
129 GL_ACCUM_BUFFER_BIT);
130 if (mask & GL_COLOR_BUFFER_BIT) {
131 ddMask |= ctx->Color.DrawDestMask;
132 }
133
134 ASSERT(ctx->Driver.Clear);
135 ctx->Driver.Clear( ctx, ddMask, (GLboolean) !ctx->Scissor.Enabled,
136 x, y, width, height );
137 }
138 }
139
140
141 void
142 _mesa_DrawBuffer( GLenum mode )
143 {
144 GET_CURRENT_CONTEXT(ctx);
145 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); /* too complex... */
146
147
148 if (MESA_VERBOSE & VERBOSE_API)
149 _mesa_debug(ctx, "glDrawBuffer %s\n", _mesa_lookup_enum_by_nr(mode));
150
151 switch (mode) {
152 case GL_AUX0:
153 case GL_AUX1:
154 case GL_AUX2:
155 case GL_AUX3:
156 /* AUX buffers not implemented in Mesa at this time */
157 _mesa_error( ctx, GL_INVALID_OPERATION, "glDrawBuffer" );
158 return;
159 case GL_RIGHT:
160 if (!ctx->Visual.stereoMode) {
161 _mesa_error( ctx, GL_INVALID_OPERATION, "glDrawBuffer" );
162 return;}
163 if (ctx->Visual.doubleBufferMode)
164 ctx->Color.DrawDestMask = FRONT_RIGHT_BIT | BACK_RIGHT_BIT;
165 else
166 ctx->Color.DrawDestMask = FRONT_RIGHT_BIT;
167 break;
168 case GL_FRONT_RIGHT:
169 if (!ctx->Visual.stereoMode) {
170 _mesa_error( ctx, GL_INVALID_OPERATION, "glDrawBuffer" );
171 return;
172 }
173 ctx->Color.DrawDestMask = FRONT_RIGHT_BIT;
174 break;
175 case GL_BACK_RIGHT:
176 if (!ctx->Visual.stereoMode) {
177 _mesa_error( ctx, GL_INVALID_OPERATION, "glDrawBuffer" );
178 return;
179 }
180 if (!ctx->Visual.doubleBufferMode) {
181 _mesa_error( ctx, GL_INVALID_OPERATION, "glDrawBuffer" );
182 return;
183 }
184 ctx->Color.DrawDestMask = BACK_RIGHT_BIT;
185 break;
186 case GL_BACK_LEFT:
187 if (!ctx->Visual.doubleBufferMode) {
188 _mesa_error( ctx, GL_INVALID_OPERATION, "glDrawBuffer" );
189 return;
190 }
191 ctx->Color.DrawDestMask = BACK_LEFT_BIT;
192 break;
193 case GL_FRONT_AND_BACK:
194 if (!ctx->Visual.doubleBufferMode) {
195 _mesa_error( ctx, GL_INVALID_OPERATION, "glDrawBuffer" );
196 return;
197 }
198 if (ctx->Visual.stereoMode)
199 ctx->Color.DrawDestMask = FRONT_LEFT_BIT | BACK_LEFT_BIT
200 | FRONT_RIGHT_BIT | BACK_RIGHT_BIT;
201 else
202 ctx->Color.DrawDestMask = FRONT_LEFT_BIT | BACK_LEFT_BIT;
203 break;
204 case GL_BACK:
205 if (!ctx->Visual.doubleBufferMode) {
206 _mesa_error( ctx, GL_INVALID_OPERATION, "glDrawBuffer" );
207 return;
208 }
209 if (ctx->Visual.stereoMode)
210 ctx->Color.DrawDestMask = BACK_LEFT_BIT | BACK_RIGHT_BIT;
211 else
212 ctx->Color.DrawDestMask = BACK_LEFT_BIT;
213 break;
214 case GL_LEFT:
215 /* never an error */
216 if (ctx->Visual.doubleBufferMode)
217 ctx->Color.DrawDestMask = FRONT_LEFT_BIT | BACK_LEFT_BIT;
218 else
219 ctx->Color.DrawDestMask = FRONT_LEFT_BIT;
220 break;
221 case GL_FRONT_LEFT:
222 /* never an error */
223 ctx->Color.DrawDestMask = FRONT_LEFT_BIT;
224 break;
225 case GL_FRONT:
226 /* never an error */
227 if (ctx->Visual.stereoMode)
228 ctx->Color.DrawDestMask = FRONT_LEFT_BIT | FRONT_RIGHT_BIT;
229 else
230 ctx->Color.DrawDestMask = FRONT_LEFT_BIT;
231 break;
232 case GL_NONE:
233 /* never an error */
234 ctx->Color.DrawDestMask = 0;
235 break;
236 default:
237 _mesa_error( ctx, GL_INVALID_ENUM, "glDrawBuffer" );
238 return;
239 }
240
241 /*
242 * Make the dest buffer mode more precise if possible
243 */
244 if (mode == GL_LEFT && !ctx->Visual.doubleBufferMode)
245 ctx->Color.DriverDrawBuffer = GL_FRONT_LEFT;
246 else if (mode == GL_RIGHT && !ctx->Visual.doubleBufferMode)
247 ctx->Color.DriverDrawBuffer = GL_FRONT_RIGHT;
248 else if (mode == GL_FRONT && !ctx->Visual.stereoMode)
249 ctx->Color.DriverDrawBuffer = GL_FRONT_LEFT;
250 else if (mode == GL_BACK && !ctx->Visual.stereoMode)
251 ctx->Color.DriverDrawBuffer = GL_BACK_LEFT;
252 else
253 ctx->Color.DriverDrawBuffer = mode;
254
255 /*
256 * Set current alpha buffer pointer
257 */
258 if (ctx->DrawBuffer->UseSoftwareAlphaBuffers) {
259 if (ctx->Color.DriverDrawBuffer == GL_FRONT_LEFT)
260 ctx->DrawBuffer->Alpha = ctx->DrawBuffer->FrontLeftAlpha;
261 else if (ctx->Color.DriverDrawBuffer == GL_BACK_LEFT)
262 ctx->DrawBuffer->Alpha = ctx->DrawBuffer->BackLeftAlpha;
263 else if (ctx->Color.DriverDrawBuffer == GL_FRONT_RIGHT)
264 ctx->DrawBuffer->Alpha = ctx->DrawBuffer->FrontRightAlpha;
265 else if (ctx->Color.DriverDrawBuffer == GL_BACK_RIGHT)
266 ctx->DrawBuffer->Alpha = ctx->DrawBuffer->BackRightAlpha;
267 }
268
269 /*
270 * If we get here there can't have been an error. Now tell the
271 * device driver about it.
272 */
273 ASSERT(ctx->Driver.SetDrawBuffer);
274 (*ctx->Driver.SetDrawBuffer)(ctx, ctx->Color.DriverDrawBuffer);
275
276 ctx->Color.DrawBuffer = mode;
277 ctx->NewState |= _NEW_COLOR;
278 }
279
280
281
282 void
283 _mesa_ReadBuffer( GLenum mode )
284 {
285 GET_CURRENT_CONTEXT(ctx);
286 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
287
288 if (MESA_VERBOSE & VERBOSE_API)
289 _mesa_debug(ctx, "glReadBuffer %s\n", _mesa_lookup_enum_by_nr(mode));
290
291 switch (mode) {
292 case GL_AUX0:
293 case GL_AUX1:
294 case GL_AUX2:
295 case GL_AUX3:
296 /* AUX buffers not implemented in Mesa at this time */
297 _mesa_error( ctx, GL_INVALID_OPERATION, "glReadBuffer" );
298 return;
299 case GL_LEFT:
300 case GL_FRONT:
301 case GL_FRONT_LEFT:
302 /* Front-Left buffer, always exists */
303 ctx->Pixel.DriverReadBuffer = GL_FRONT_LEFT;
304 break;
305 case GL_BACK:
306 case GL_BACK_LEFT:
307 /* Back-Left buffer, requires double buffering */
308 if (!ctx->Visual.doubleBufferMode) {
309 _mesa_error( ctx, GL_INVALID_OPERATION, "glReadBuffer" );
310 return;
311 }
312 ctx->Pixel.DriverReadBuffer = GL_BACK_LEFT;
313 break;
314 case GL_FRONT_RIGHT:
315 case GL_RIGHT:
316 if (!ctx->Visual.stereoMode) {
317 _mesa_error( ctx, GL_INVALID_OPERATION, "glReadBuffer" );
318 return;
319 }
320 ctx->Pixel.DriverReadBuffer = GL_FRONT_RIGHT;
321 break;
322 case GL_BACK_RIGHT:
323 if (!ctx->Visual.stereoMode || !ctx->Visual.doubleBufferMode) {
324 _mesa_error( ctx, GL_INVALID_OPERATION, "glReadBuffer" );
325 return;
326 }
327 ctx->Pixel.DriverReadBuffer = GL_BACK_RIGHT;
328 break;
329 default:
330 _mesa_error( ctx, GL_INVALID_ENUM, "glReadBuffer" );
331 return;
332 }
333
334 ctx->Pixel.ReadBuffer = mode;
335 ctx->NewState |= _NEW_PIXEL;
336 }
337
338
339 /*
340 * GL_MESA_resize_buffers extension
341 * When this function is called, we'll ask the window system how large
342 * the current window is. If it's not what we expect, we'll have to
343 * resize/reallocate the software accum/stencil/depth/alpha buffers.
344 */
345 void
346 _mesa_ResizeBuffersMESA( void )
347 {
348 GLcontext *ctx = _mesa_get_current_context();
349
350 if (MESA_VERBOSE & VERBOSE_API)
351 _mesa_debug(ctx, "glResizeBuffersMESA\n");
352
353 if (ctx) {
354 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH( ctx );
355
356 if (ctx->DrawBuffer) {
357 GLuint buf_width, buf_height;
358 GLframebuffer *buffer = ctx->DrawBuffer;
359
360 /* ask device driver for size of output buffer */
361 (*ctx->Driver.GetBufferSize)( buffer, &buf_width, &buf_height );
362
363 /* see if size of device driver's color buffer (window) has changed */
364 if (buffer->Width == buf_width && buffer->Height == buf_height)
365 return; /* size is as expected */
366
367 buffer->Width = buf_width;
368 buffer->Height = buf_height;
369
370 ctx->Driver.ResizeBuffers( buffer );
371 }
372
373 if (ctx->ReadBuffer && ctx->ReadBuffer != ctx->DrawBuffer) {
374 GLuint buf_width, buf_height;
375 GLframebuffer *buffer = ctx->DrawBuffer;
376
377 /* ask device driver for size of output buffer */
378 (*ctx->Driver.GetBufferSize)( buffer, &buf_width, &buf_height );
379
380 /* see if size of device driver's color buffer (window) has changed */
381 if (buffer->Width == buf_width && buffer->Height == buf_height)
382 return; /* size is as expected */
383
384 buffer->Width = buf_width;
385 buffer->Height = buf_height;
386
387 ctx->Driver.ResizeBuffers( buffer );
388 }
389
390 ctx->NewState |= _NEW_BUFFERS; /* to update scissor / window bounds */
391 }
392 }
393
394
395 void
396 _mesa_Scissor( GLint x, GLint y, GLsizei width, GLsizei height )
397 {
398 GET_CURRENT_CONTEXT(ctx);
399 ASSERT_OUTSIDE_BEGIN_END(ctx);
400
401 if (width < 0 || height < 0) {
402 _mesa_error( ctx, GL_INVALID_VALUE, "glScissor" );
403 return;
404 }
405
406 if (MESA_VERBOSE & VERBOSE_API)
407 _mesa_debug(ctx, "glScissor %d %d %d %d\n", x, y, width, height);
408
409 if (x == ctx->Scissor.X &&
410 y == ctx->Scissor.Y &&
411 width == ctx->Scissor.Width &&
412 height == ctx->Scissor.Height)
413 return;
414
415 FLUSH_VERTICES(ctx, _NEW_SCISSOR);
416 ctx->Scissor.X = x;
417 ctx->Scissor.Y = y;
418 ctx->Scissor.Width = width;
419 ctx->Scissor.Height = height;
420
421 if (ctx->Driver.Scissor)
422 ctx->Driver.Scissor( ctx, x, y, width, height );
423 }
424
425
426 /*
427 * XXX move somewhere else someday?
428 */
429 void
430 _mesa_SampleCoverageARB(GLclampf value, GLboolean invert)
431 {
432 GLcontext *ctx = _mesa_get_current_context();
433
434 if (!ctx->Extensions.ARB_multisample) {
435 _mesa_error(ctx, GL_INVALID_OPERATION, "glSampleCoverageARB");
436 return;
437 }
438
439 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH( ctx );
440 ctx->Multisample.SampleCoverageValue = (GLfloat) CLAMP(value, 0.0, 1.0);
441 ctx->Multisample.SampleCoverageInvert = invert;
442 ctx->NewState |= _NEW_MULTISAMPLE;
443 }
444