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