mesa: fix a bad cast in put_values_z24.
[mesa.git] / src / mesa / main / buffers.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 buffers.c
28 * General framebuffer-related functions, like glClear, glScissor, etc.
29 */
30
31
32
33 #include "glheader.h"
34 #include "buffers.h"
35 #include "colormac.h"
36 #include "context.h"
37 #include "enums.h"
38 #include "fbobject.h"
39 #include "state.h"
40
41
42 #define BAD_MASK ~0u
43
44
45 #if _HAVE_FULL_GL
46 void GLAPIENTRY
47 _mesa_ClearIndex( GLfloat c )
48 {
49 GET_CURRENT_CONTEXT(ctx);
50 ASSERT_OUTSIDE_BEGIN_END(ctx);
51
52 if (ctx->Color.ClearIndex == (GLuint) c)
53 return;
54
55 FLUSH_VERTICES(ctx, _NEW_COLOR);
56 ctx->Color.ClearIndex = (GLuint) c;
57
58 if (!ctx->Visual.rgbMode && ctx->Driver.ClearIndex) {
59 /* it's OK to call glClearIndex in RGBA mode but it should be a NOP */
60 (*ctx->Driver.ClearIndex)( ctx, ctx->Color.ClearIndex );
61 }
62 }
63 #endif
64
65
66 /**
67 * Specify the clear values for the color buffers.
68 *
69 * \param red red color component.
70 * \param green green color component.
71 * \param blue blue color component.
72 * \param alpha alpha component.
73 *
74 * \sa glClearColor().
75 *
76 * Clamps the parameters and updates gl_colorbuffer_attrib::ClearColor. On a
77 * change, flushes the vertices and notifies the driver via the
78 * dd_function_table::ClearColor callback.
79 */
80 void GLAPIENTRY
81 _mesa_ClearColor( GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha )
82 {
83 GLfloat tmp[4];
84 GET_CURRENT_CONTEXT(ctx);
85 ASSERT_OUTSIDE_BEGIN_END(ctx);
86
87 tmp[0] = CLAMP(red, 0.0F, 1.0F);
88 tmp[1] = CLAMP(green, 0.0F, 1.0F);
89 tmp[2] = CLAMP(blue, 0.0F, 1.0F);
90 tmp[3] = CLAMP(alpha, 0.0F, 1.0F);
91
92 if (TEST_EQ_4V(tmp, ctx->Color.ClearColor))
93 return; /* no change */
94
95 FLUSH_VERTICES(ctx, _NEW_COLOR);
96 COPY_4V(ctx->Color.ClearColor, tmp);
97
98 if (ctx->Visual.rgbMode && ctx->Driver.ClearColor) {
99 /* it's OK to call glClearColor in CI mode but it should be a NOP */
100 (*ctx->Driver.ClearColor)(ctx, ctx->Color.ClearColor);
101 }
102 }
103
104
105 /**
106 * Clear buffers.
107 *
108 * \param mask bit-mask indicating the buffers to be cleared.
109 *
110 * Flushes the vertices and verifies the parameter. If __GLcontextRec::NewState
111 * is set then calls _mesa_update_state() to update gl_frame_buffer::_Xmin,
112 * etc. If the rasterization mode is set to GL_RENDER then requests the driver
113 * to clear the buffers, via the dd_function_table::Clear callback.
114 */
115 void GLAPIENTRY
116 _mesa_Clear( GLbitfield mask )
117 {
118 GET_CURRENT_CONTEXT(ctx);
119 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
120
121 if (MESA_VERBOSE & VERBOSE_API)
122 _mesa_debug(ctx, "glClear 0x%x\n", mask);
123
124 if (mask & ~(GL_COLOR_BUFFER_BIT |
125 GL_DEPTH_BUFFER_BIT |
126 GL_STENCIL_BUFFER_BIT |
127 GL_ACCUM_BUFFER_BIT)) {
128 /* invalid bit set */
129 _mesa_error( ctx, GL_INVALID_VALUE, "glClear(0x%x)", mask);
130 return;
131 }
132
133 if (ctx->NewState) {
134 _mesa_update_state( ctx ); /* update _Xmin, etc */
135 }
136
137 if (ctx->DrawBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
138 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
139 "glClear(incomplete framebuffer)");
140 return;
141 }
142
143 if (ctx->DrawBuffer->Width == 0 || ctx->DrawBuffer->Height == 0 ||
144 ctx->DrawBuffer->_Xmin >= ctx->DrawBuffer->_Xmax ||
145 ctx->DrawBuffer->_Ymin >= ctx->DrawBuffer->_Ymax)
146 return;
147
148 if (ctx->RenderMode == GL_RENDER) {
149 GLbitfield bufferMask;
150
151 /* don't clear depth buffer if depth writing disabled */
152 if (!ctx->Depth.Mask)
153 mask &= ~GL_DEPTH_BUFFER_BIT;
154
155 /* Build the bitmask to send to device driver's Clear function.
156 * Note that the GL_COLOR_BUFFER_BIT flag will expand to 0, 1, 2 or 4
157 * of the BUFFER_BIT_FRONT/BACK_LEFT/RIGHT flags, or one of the
158 * BUFFER_BIT_COLORn flags.
159 */
160 bufferMask = 0;
161 if (mask & GL_COLOR_BUFFER_BIT) {
162 bufferMask |= ctx->DrawBuffer->_ColorDrawBufferMask[0];
163 }
164
165 if ((mask & GL_DEPTH_BUFFER_BIT)
166 && ctx->DrawBuffer->Visual.haveDepthBuffer) {
167 bufferMask |= BUFFER_BIT_DEPTH;
168 }
169
170 if ((mask & GL_STENCIL_BUFFER_BIT)
171 && ctx->DrawBuffer->Visual.haveStencilBuffer) {
172 bufferMask |= BUFFER_BIT_STENCIL;
173 }
174
175 if ((mask & GL_ACCUM_BUFFER_BIT)
176 && ctx->DrawBuffer->Visual.haveAccumBuffer) {
177 bufferMask |= BUFFER_BIT_ACCUM;
178 }
179
180 ASSERT(ctx->Driver.Clear);
181 ctx->Driver.Clear(ctx, bufferMask);
182 }
183 }
184
185
186
187 /**
188 * Return bitmask of BUFFER_BIT_* flags indicating which color buffers are
189 * available to the rendering context (for drawing or reading).
190 * This depends on the type of framebuffer. For window system framebuffers
191 * we look at the framebuffer's visual. But for user-create framebuffers we
192 * look at the number of supported color attachments.
193 * \param fb the framebuffer to draw to, or read from
194 * \return bitmask of BUFFER_BIT_* flags
195 */
196 static GLbitfield
197 supported_buffer_bitmask(const GLcontext *ctx, const struct gl_framebuffer *fb)
198 {
199 GLbitfield mask = 0x0;
200
201 if (fb->Name > 0) {
202 /* A user-created renderbuffer */
203 GLuint i;
204 ASSERT(ctx->Extensions.EXT_framebuffer_object);
205 for (i = 0; i < ctx->Const.MaxColorAttachments; i++) {
206 mask |= (BUFFER_BIT_COLOR0 << i);
207 }
208 }
209 else {
210 /* A window system framebuffer */
211 GLint i;
212 mask = BUFFER_BIT_FRONT_LEFT; /* always have this */
213 if (fb->Visual.stereoMode) {
214 mask |= BUFFER_BIT_FRONT_RIGHT;
215 if (fb->Visual.doubleBufferMode) {
216 mask |= BUFFER_BIT_BACK_LEFT | BUFFER_BIT_BACK_RIGHT;
217 }
218 }
219 else if (fb->Visual.doubleBufferMode) {
220 mask |= BUFFER_BIT_BACK_LEFT;
221 }
222
223 for (i = 0; i < fb->Visual.numAuxBuffers; i++) {
224 mask |= (BUFFER_BIT_AUX0 << i);
225 }
226 }
227
228 return mask;
229 }
230
231
232 /**
233 * Helper routine used by glDrawBuffer and glDrawBuffersARB.
234 * Given a GLenum naming one or more color buffers (such as
235 * GL_FRONT_AND_BACK), return the corresponding bitmask of BUFFER_BIT_* flags.
236 */
237 static GLbitfield
238 draw_buffer_enum_to_bitmask(GLenum buffer)
239 {
240 switch (buffer) {
241 case GL_NONE:
242 return 0;
243 case GL_FRONT:
244 return BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_FRONT_RIGHT;
245 case GL_BACK:
246 return BUFFER_BIT_BACK_LEFT | BUFFER_BIT_BACK_RIGHT;
247 case GL_RIGHT:
248 return BUFFER_BIT_FRONT_RIGHT | BUFFER_BIT_BACK_RIGHT;
249 case GL_FRONT_RIGHT:
250 return BUFFER_BIT_FRONT_RIGHT;
251 case GL_BACK_RIGHT:
252 return BUFFER_BIT_BACK_RIGHT;
253 case GL_BACK_LEFT:
254 return BUFFER_BIT_BACK_LEFT;
255 case GL_FRONT_AND_BACK:
256 return BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_BACK_LEFT
257 | BUFFER_BIT_FRONT_RIGHT | BUFFER_BIT_BACK_RIGHT;
258 case GL_LEFT:
259 return BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_BACK_LEFT;
260 case GL_FRONT_LEFT:
261 return BUFFER_BIT_FRONT_LEFT;
262 case GL_AUX0:
263 return BUFFER_BIT_AUX0;
264 case GL_AUX1:
265 return BUFFER_BIT_AUX1;
266 case GL_AUX2:
267 return BUFFER_BIT_AUX2;
268 case GL_AUX3:
269 return BUFFER_BIT_AUX3;
270 case GL_COLOR_ATTACHMENT0_EXT:
271 return BUFFER_BIT_COLOR0;
272 case GL_COLOR_ATTACHMENT1_EXT:
273 return BUFFER_BIT_COLOR1;
274 case GL_COLOR_ATTACHMENT2_EXT:
275 return BUFFER_BIT_COLOR2;
276 case GL_COLOR_ATTACHMENT3_EXT:
277 return BUFFER_BIT_COLOR3;
278 default:
279 /* error */
280 return BAD_MASK;
281 }
282 }
283
284
285 /**
286 * Helper routine used by glReadBuffer.
287 * Given a GLenum naming a color buffer, return the index of the corresponding
288 * renderbuffer (a BUFFER_* value).
289 * return -1 for an invalid buffer.
290 */
291 static GLint
292 read_buffer_enum_to_index(GLenum buffer)
293 {
294 switch (buffer) {
295 case GL_FRONT:
296 return BUFFER_FRONT_LEFT;
297 case GL_BACK:
298 return BUFFER_BACK_LEFT;
299 case GL_RIGHT:
300 return BUFFER_FRONT_RIGHT;
301 case GL_FRONT_RIGHT:
302 return BUFFER_FRONT_RIGHT;
303 case GL_BACK_RIGHT:
304 return BUFFER_BACK_RIGHT;
305 case GL_BACK_LEFT:
306 return BUFFER_BACK_LEFT;
307 case GL_LEFT:
308 return BUFFER_FRONT_LEFT;
309 case GL_FRONT_LEFT:
310 return BUFFER_FRONT_LEFT;
311 case GL_AUX0:
312 return BUFFER_AUX0;
313 case GL_AUX1:
314 return BUFFER_AUX1;
315 case GL_AUX2:
316 return BUFFER_AUX2;
317 case GL_AUX3:
318 return BUFFER_AUX3;
319 case GL_COLOR_ATTACHMENT0_EXT:
320 return BUFFER_COLOR0;
321 case GL_COLOR_ATTACHMENT1_EXT:
322 return BUFFER_COLOR1;
323 case GL_COLOR_ATTACHMENT2_EXT:
324 return BUFFER_COLOR2;
325 case GL_COLOR_ATTACHMENT3_EXT:
326 return BUFFER_COLOR3;
327 default:
328 /* error */
329 return -1;
330 }
331 }
332
333
334 /**
335 * Called by glDrawBuffer().
336 * Specify which renderbuffer(s) to draw into for the first color output.
337 * <buffer> can name zero, one, two or four renderbuffers!
338 * \sa _mesa_DrawBuffersARB
339 *
340 * \param buffer buffer token such as GL_LEFT or GL_FRONT_AND_BACK, etc.
341 *
342 * Note that the behaviour of this function depends on whether the
343 * current ctx->DrawBuffer is a window-system framebuffer (Name=0) or
344 * a user-created framebuffer object (Name!=0).
345 * In the former case, we update the per-context ctx->Color.DrawBuffer
346 * state var _and_ the FB's ColorDrawBuffer state.
347 * In the later case, we update the FB's ColorDrawBuffer state only.
348 *
349 * Furthermore, upon a MakeCurrent() or BindFramebuffer() call, if the
350 * new FB is a window system FB, we need to re-update the FB's
351 * ColorDrawBuffer state to match the context. This is handled in
352 * _mesa_update_framebuffer().
353 *
354 * See the GL_EXT_framebuffer_object spec for more info.
355 */
356 void GLAPIENTRY
357 _mesa_DrawBuffer(GLenum buffer)
358 {
359 GLbitfield destMask;
360 GET_CURRENT_CONTEXT(ctx);
361 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); /* too complex... */
362
363 if (MESA_VERBOSE & VERBOSE_API) {
364 _mesa_debug(ctx, "glDrawBuffer %s\n", _mesa_lookup_enum_by_nr(buffer));
365 }
366
367 if (buffer == GL_NONE) {
368 destMask = 0x0;
369 }
370 else {
371 const GLbitfield supportedMask
372 = supported_buffer_bitmask(ctx, ctx->DrawBuffer);
373 destMask = draw_buffer_enum_to_bitmask(buffer);
374 if (destMask == BAD_MASK) {
375 /* totally bogus buffer */
376 _mesa_error(ctx, GL_INVALID_ENUM, "glDrawBuffer(buffer)");
377 return;
378 }
379 destMask &= supportedMask;
380 if (destMask == 0x0) {
381 /* none of the named color buffers exist! */
382 _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawBuffer(buffer)");
383 return;
384 }
385 }
386
387 /* if we get here, there's no error so set new state */
388 _mesa_drawbuffers(ctx, 1, &buffer, &destMask);
389
390 /*
391 * Call device driver function.
392 */
393 if (ctx->Driver.DrawBuffers)
394 ctx->Driver.DrawBuffers(ctx, 1, &buffer);
395 else if (ctx->Driver.DrawBuffer)
396 ctx->Driver.DrawBuffer(ctx, buffer);
397 }
398
399
400 /**
401 * Called by glDrawBuffersARB; specifies the destination color renderbuffers
402 * for N fragment program color outputs.
403 * \sa _mesa_DrawBuffer
404 * \param n number of outputs
405 * \param buffers array [n] of renderbuffer names. Unlike glDrawBuffer, the
406 * names cannot specify more than one buffer. For example,
407 * GL_FRONT_AND_BACK is illegal.
408 */
409 void GLAPIENTRY
410 _mesa_DrawBuffersARB(GLsizei n, const GLenum *buffers)
411 {
412 GLint output;
413 GLbitfield usedBufferMask, supportedMask;
414 GLbitfield destMask[MAX_DRAW_BUFFERS];
415 GET_CURRENT_CONTEXT(ctx);
416 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
417
418 if (!ctx->Extensions.ARB_draw_buffers) {
419 _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawBuffersARB");
420 return;
421 }
422 if (n < 1 || n > (GLsizei) ctx->Const.MaxDrawBuffers) {
423 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawBuffersARB(n)");
424 return;
425 }
426
427 supportedMask = supported_buffer_bitmask(ctx, ctx->DrawBuffer);
428 usedBufferMask = 0x0;
429
430 /* complicated error checking... */
431 for (output = 0; output < n; output++) {
432 if (buffers[output] == GL_NONE) {
433 destMask[output] = 0x0;
434 }
435 else {
436 destMask[output] = draw_buffer_enum_to_bitmask(buffers[output]);
437 if (destMask[output] == BAD_MASK
438 || _mesa_bitcount(destMask[output]) > 1) {
439 _mesa_error(ctx, GL_INVALID_ENUM, "glDrawBuffersARB(buffer)");
440 return;
441 }
442 destMask[output] &= supportedMask;
443 if (destMask[output] == 0) {
444 _mesa_error(ctx, GL_INVALID_OPERATION,
445 "glDrawBuffersARB(unsupported buffer)");
446 return;
447 }
448 if (destMask[output] & usedBufferMask) {
449 /* can't specify a dest buffer more than once! */
450 _mesa_error(ctx, GL_INVALID_OPERATION,
451 "glDrawBuffersARB(duplicated buffer)");
452 return;
453 }
454
455 /* update bitmask */
456 usedBufferMask |= destMask[output];
457 }
458 }
459
460 /* OK, if we get here, there were no errors so set the new state */
461 _mesa_drawbuffers(ctx, n, buffers, destMask);
462
463 /*
464 * Call device driver function.
465 */
466 if (ctx->Driver.DrawBuffers)
467 ctx->Driver.DrawBuffers(ctx, n, buffers);
468 else if (ctx->Driver.DrawBuffer)
469 ctx->Driver.DrawBuffer(ctx, buffers[0]);
470 }
471
472
473 /**
474 * Set color output state. Traditionally, there was only one color
475 * output, but fragment programs can now have several distinct color
476 * outputs (see GL_ARB_draw_buffers). This function sets the state
477 * for one such color output.
478 * \param ctx current context
479 * \param output which fragment program output
480 * \param buffer buffer to write to (like GL_LEFT)
481 * \param destMask BUFFER_* bitmask
482 * (like BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_BACK_LEFT).
483 */
484 static void
485 set_color_output(GLcontext *ctx, GLuint output, GLenum buffer,
486 GLbitfield destMask)
487 {
488 struct gl_framebuffer *fb = ctx->DrawBuffer;
489
490 ASSERT(output < ctx->Const.MaxDrawBuffers);
491
492 /* Set per-FBO state */
493 fb->ColorDrawBuffer[output] = buffer;
494 fb->_ColorDrawBufferMask[output] = destMask;
495 /* not really needed, will be set later */
496 fb->_NumColorDrawBuffers[output] = 0;
497
498 if (fb->Name == 0) {
499 /* Only set the per-context DrawBuffer state if we're currently
500 * drawing to a window system framebuffer.
501 */
502 ctx->Color.DrawBuffer[output] = buffer;
503 }
504 }
505
506
507 /**
508 * Helper function to set the GL_DRAW_BUFFER state in the context and
509 * current FBO.
510 *
511 * All error checking will have been done prior to calling this function
512 * so nothing should go wrong at this point.
513 *
514 * \param ctx current context
515 * \param n number of color outputs to set
516 * \param buffers array[n] of colorbuffer names, like GL_LEFT.
517 * \param destMask array[n] of BUFFER_* bitmasks which correspond to the
518 * colorbuffer names. (i.e. GL_FRONT_AND_BACK =>
519 * BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_BACK_LEFT).
520 */
521 void
522 _mesa_drawbuffers(GLcontext *ctx, GLuint n, const GLenum *buffers,
523 const GLbitfield *destMask)
524 {
525 GLbitfield mask[MAX_DRAW_BUFFERS];
526 GLuint output;
527
528 if (!destMask) {
529 /* compute destMask values now */
530 const GLbitfield supportedMask
531 = supported_buffer_bitmask(ctx, ctx->DrawBuffer);
532 for (output = 0; output < n; output++) {
533 mask[output] = draw_buffer_enum_to_bitmask(buffers[output]);
534 ASSERT(mask[output] != BAD_MASK);
535 mask[output] &= supportedMask;
536 }
537 destMask = mask;
538 }
539
540 for (output = 0; output < n; output++) {
541 set_color_output(ctx, output, buffers[output], destMask[output]);
542 }
543
544 /* set remaining color outputs to NONE */
545 for (output = n; output < ctx->Const.MaxDrawBuffers; output++) {
546 set_color_output(ctx, output, GL_NONE, 0x0);
547 }
548
549 ctx->NewState |= _NEW_COLOR;
550 }
551
552
553 /**
554 * Like \sa _mesa_drawbuffers(), this is a helper function for setting
555 * GL_READ_BUFFER state in the context and current FBO.
556 * \param ctx the rendering context
557 * \param buffer GL_FRONT, GL_BACK, GL_COLOR_ATTACHMENT0, etc.
558 * \param bufferIndex the numerical index corresponding to 'buffer'
559 */
560 void
561 _mesa_readbuffer(GLcontext *ctx, GLenum buffer, GLint bufferIndex)
562 {
563 struct gl_framebuffer *fb = ctx->ReadBuffer;
564
565 if (fb->Name == 0) {
566 /* Only update the per-context READ_BUFFER state if we're bound to
567 * a window-system framebuffer.
568 */
569 ctx->Pixel.ReadBuffer = buffer;
570 }
571
572 fb->ColorReadBuffer = buffer;
573 fb->_ColorReadBufferIndex = bufferIndex;
574
575 ctx->NewState |= _NEW_PIXEL;
576 }
577
578
579
580 /**
581 * Called by glReadBuffer to set the source renderbuffer for reading pixels.
582 * \param mode color buffer such as GL_FRONT, GL_BACK, etc.
583 */
584 void GLAPIENTRY
585 _mesa_ReadBuffer(GLenum buffer)
586 {
587 struct gl_framebuffer *fb;
588 GLbitfield supportedMask;
589 GLint srcBuffer;
590 GET_CURRENT_CONTEXT(ctx);
591 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
592
593 if (MESA_VERBOSE & VERBOSE_API)
594 _mesa_debug(ctx, "glReadBuffer %s\n", _mesa_lookup_enum_by_nr(buffer));
595
596 fb = ctx->ReadBuffer;
597
598 if (MESA_VERBOSE & VERBOSE_API)
599 _mesa_debug(ctx, "glReadBuffer %s\n", _mesa_lookup_enum_by_nr(buffer));
600
601 if (fb->Name > 0 && buffer == GL_NONE) {
602 /* This is legal for user-created framebuffer objects */
603 srcBuffer = -1;
604 }
605 else {
606 /* general case / window-system framebuffer */
607 srcBuffer = read_buffer_enum_to_index(buffer);
608 if (srcBuffer == -1) {
609 _mesa_error(ctx, GL_INVALID_ENUM,
610 "glReadBuffer(buffer=0x%x)", buffer);
611 return;
612 }
613 supportedMask = supported_buffer_bitmask(ctx, fb);
614 if (((1 << srcBuffer) & supportedMask) == 0) {
615 _mesa_error(ctx, GL_INVALID_OPERATION,
616 "glReadBuffer(buffer=0x%x)", buffer);
617 return;
618 }
619 }
620
621 /* OK, all error checking has been completed now */
622
623 _mesa_readbuffer(ctx, buffer, srcBuffer);
624
625 /*
626 * Call device driver function.
627 */
628 if (ctx->Driver.ReadBuffer)
629 (*ctx->Driver.ReadBuffer)(ctx, buffer);
630 }
631
632
633 #if _HAVE_FULL_GL
634
635 /**
636 * XXX THIS IS OBSOLETE - drivers should take care of detecting window
637 * size changes and act accordingly, likely calling _mesa_resize_framebuffer().
638 *
639 * GL_MESA_resize_buffers extension.
640 *
641 * When this function is called, we'll ask the window system how large
642 * the current window is. If it's a new size, we'll call the driver's
643 * ResizeBuffers function. The driver will then resize its color buffers
644 * as needed, and maybe call the swrast's routine for reallocating
645 * swrast-managed depth/stencil/accum/etc buffers.
646 * \note This function should only be called through the GL API, not
647 * from device drivers (as was done in the past).
648 */
649
650 void _mesa_resizebuffers( GLcontext *ctx )
651 {
652 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH( ctx );
653
654 if (MESA_VERBOSE & VERBOSE_API)
655 _mesa_debug(ctx, "glResizeBuffersMESA\n");
656
657 if (!ctx->Driver.GetBufferSize) {
658 return;
659 }
660
661 if (ctx->WinSysDrawBuffer) {
662 GLuint newWidth, newHeight;
663 GLframebuffer *buffer = ctx->WinSysDrawBuffer;
664
665 assert(buffer->Name == 0);
666
667 /* ask device driver for size of output buffer */
668 ctx->Driver.GetBufferSize( buffer, &newWidth, &newHeight );
669
670 /* see if size of device driver's color buffer (window) has changed */
671 if (buffer->Width != newWidth || buffer->Height != newHeight) {
672 if (ctx->Driver.ResizeBuffers)
673 ctx->Driver.ResizeBuffers(ctx, buffer, newWidth, newHeight );
674 }
675 }
676
677 if (ctx->WinSysReadBuffer
678 && ctx->WinSysReadBuffer != ctx->WinSysDrawBuffer) {
679 GLuint newWidth, newHeight;
680 GLframebuffer *buffer = ctx->WinSysReadBuffer;
681
682 assert(buffer->Name == 0);
683
684 /* ask device driver for size of read buffer */
685 ctx->Driver.GetBufferSize( buffer, &newWidth, &newHeight );
686
687 /* see if size of device driver's color buffer (window) has changed */
688 if (buffer->Width != newWidth || buffer->Height != newHeight) {
689 if (ctx->Driver.ResizeBuffers)
690 ctx->Driver.ResizeBuffers(ctx, buffer, newWidth, newHeight );
691 }
692 }
693
694 ctx->NewState |= _NEW_BUFFERS; /* to update scissor / window bounds */
695 }
696
697
698 /*
699 * XXX THIS IS OBSOLETE
700 */
701 void GLAPIENTRY
702 _mesa_ResizeBuffersMESA( void )
703 {
704 GET_CURRENT_CONTEXT(ctx);
705
706 if (ctx->Extensions.MESA_resize_buffers)
707 _mesa_resizebuffers( ctx );
708 }
709
710
711 /*
712 * XXX move somewhere else someday?
713 */
714 void GLAPIENTRY
715 _mesa_SampleCoverageARB(GLclampf value, GLboolean invert)
716 {
717 GET_CURRENT_CONTEXT(ctx);
718
719 if (!ctx->Extensions.ARB_multisample) {
720 _mesa_error(ctx, GL_INVALID_OPERATION, "glSampleCoverageARB");
721 return;
722 }
723
724 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH( ctx );
725 ctx->Multisample.SampleCoverageValue = (GLfloat) CLAMP(value, 0.0, 1.0);
726 ctx->Multisample.SampleCoverageInvert = invert;
727 ctx->NewState |= _NEW_MULTISAMPLE;
728 }
729
730 #endif /* _HAVE_FULL_GL */
731
732
733
734 /**
735 * Define the scissor box.
736 *
737 * \param x, y coordinates of the scissor box lower-left corner.
738 * \param width width of the scissor box.
739 * \param height height of the scissor box.
740 *
741 * \sa glScissor().
742 *
743 * Verifies the parameters and updates __GLcontextRec::Scissor. On a
744 * change flushes the vertices and notifies the driver via
745 * the dd_function_table::Scissor callback.
746 */
747 void
748 _mesa_set_scissor(GLcontext *ctx,
749 GLint x, GLint y, GLsizei width, GLsizei height)
750 {
751 if (x == ctx->Scissor.X &&
752 y == ctx->Scissor.Y &&
753 width == ctx->Scissor.Width &&
754 height == ctx->Scissor.Height)
755 return;
756
757 FLUSH_VERTICES(ctx, _NEW_SCISSOR);
758 ctx->Scissor.X = x;
759 ctx->Scissor.Y = y;
760 ctx->Scissor.Width = width;
761 ctx->Scissor.Height = height;
762
763 if (ctx->Driver.Scissor)
764 ctx->Driver.Scissor( ctx, x, y, width, height );
765 }
766
767
768 void GLAPIENTRY
769 _mesa_Scissor( GLint x, GLint y, GLsizei width, GLsizei height )
770 {
771 GET_CURRENT_CONTEXT(ctx);
772 ASSERT_OUTSIDE_BEGIN_END(ctx);
773
774 if (width < 0 || height < 0) {
775 _mesa_error( ctx, GL_INVALID_VALUE, "glScissor" );
776 return;
777 }
778
779 if (MESA_VERBOSE & VERBOSE_API)
780 _mesa_debug(ctx, "glScissor %d %d %d %d\n", x, y, width, height);
781
782 _mesa_set_scissor(ctx, x, y, width, height);
783 }
784
785
786
787 /**********************************************************************/
788 /** \name Initialization */
789 /*@{*/
790
791 /**
792 * Initialize the context's scissor state.
793 * \param ctx the GL context.
794 */
795 void
796 _mesa_init_scissor(GLcontext *ctx)
797 {
798 /* Scissor group */
799 ctx->Scissor.Enabled = GL_FALSE;
800 ctx->Scissor.X = 0;
801 ctx->Scissor.Y = 0;
802 ctx->Scissor.Width = 0;
803 ctx->Scissor.Height = 0;
804 }
805
806
807 /**
808 * Initialize the context's multisample state.
809 * \param ctx the GL context.
810 */
811 void
812 _mesa_init_multisample(GLcontext *ctx)
813 {
814 ctx->Multisample.Enabled = GL_FALSE;
815 ctx->Multisample.SampleAlphaToCoverage = GL_FALSE;
816 ctx->Multisample.SampleAlphaToOne = GL_FALSE;
817 ctx->Multisample.SampleCoverage = GL_FALSE;
818 ctx->Multisample.SampleCoverageValue = 1.0;
819 ctx->Multisample.SampleCoverageInvert = GL_FALSE;
820 }
821
822 /*@}*/