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