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