30fa4332804e71f612dc260d1a2adbc796b6f115
3 * Frame buffer management.
7 * Mesa 3-D graphics library
10 * Copyright (C) 1999-2004 Brian Paul All Rights Reserved.
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
19 * The above copyright notice and this permission notice shall be included
20 * in all copies or substantial portions of the Software.
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
27 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
45 _mesa_ClearIndex( GLfloat c
)
47 GET_CURRENT_CONTEXT(ctx
);
48 ASSERT_OUTSIDE_BEGIN_END(ctx
);
50 if (ctx
->Color
.ClearIndex
== (GLuint
) c
)
53 FLUSH_VERTICES(ctx
, _NEW_COLOR
);
54 ctx
->Color
.ClearIndex
= (GLuint
) c
;
56 if (!ctx
->Visual
.rgbMode
&& ctx
->Driver
.ClearIndex
) {
57 /* it's OK to call glClearIndex in RGBA mode but it should be a NOP */
58 (*ctx
->Driver
.ClearIndex
)( ctx
, ctx
->Color
.ClearIndex
);
65 * Specify the clear values for the color buffers.
67 * \param red red color component.
68 * \param green green color component.
69 * \param blue blue color component.
70 * \param alpha alpha component.
74 * Clamps the parameters and updates gl_colorbuffer_attrib::ClearColor. On a
75 * change, flushes the vertices and notifies the driver via the
76 * dd_function_table::ClearColor callback.
79 _mesa_ClearColor( GLclampf red
, GLclampf green
, GLclampf blue
, GLclampf alpha
)
82 GET_CURRENT_CONTEXT(ctx
);
83 ASSERT_OUTSIDE_BEGIN_END(ctx
);
85 tmp
[0] = CLAMP(red
, 0.0F
, 1.0F
);
86 tmp
[1] = CLAMP(green
, 0.0F
, 1.0F
);
87 tmp
[2] = CLAMP(blue
, 0.0F
, 1.0F
);
88 tmp
[3] = CLAMP(alpha
, 0.0F
, 1.0F
);
90 if (TEST_EQ_4V(tmp
, ctx
->Color
.ClearColor
))
91 return; /* no change */
93 FLUSH_VERTICES(ctx
, _NEW_COLOR
);
94 COPY_4V(ctx
->Color
.ClearColor
, tmp
);
96 if (ctx
->Visual
.rgbMode
&& ctx
->Driver
.ClearColor
) {
97 /* it's OK to call glClearColor in CI mode but it should be a NOP */
98 (*ctx
->Driver
.ClearColor
)(ctx
, ctx
->Color
.ClearColor
);
106 * \param mask bit-mask indicating the buffers to be cleared.
108 * Flushes the vertices and verifies the parameter. If __GLcontextRec::NewState
109 * is set then calls _mesa_update_state() to update gl_frame_buffer::_Xmin,
110 * etc. If the rasterization mode is set to GL_RENDER then requests the driver
111 * to clear the buffers, via the dd_function_table::Clear callback.
114 _mesa_Clear( GLbitfield mask
)
116 GET_CURRENT_CONTEXT(ctx
);
117 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
119 if (MESA_VERBOSE
& VERBOSE_API
)
120 _mesa_debug(ctx
, "glClear 0x%x\n", mask
);
122 if (mask
& ~(GL_COLOR_BUFFER_BIT
|
123 GL_DEPTH_BUFFER_BIT
|
124 GL_STENCIL_BUFFER_BIT
|
125 GL_ACCUM_BUFFER_BIT
)) {
126 /* invalid bit set */
127 _mesa_error( ctx
, GL_INVALID_VALUE
, "glClear(0x%x)", mask
);
132 _mesa_update_state( ctx
); /* update _Xmin, etc */
135 if (ctx
->RenderMode
==GL_RENDER
) {
136 const GLint x
= ctx
->DrawBuffer
->_Xmin
;
137 const GLint y
= ctx
->DrawBuffer
->_Ymin
;
138 const GLint height
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
139 const GLint width
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
142 /* don't clear depth buffer if depth writing disabled */
143 if (!ctx
->Depth
.Mask
)
144 mask
&= ~GL_DEPTH_BUFFER_BIT
;
146 /* Build the bitmask to send to device driver's Clear function.
147 * Note that the GL_COLOR_BUFFER_BIT flag will expand to 0, 1, 2 or 4
148 * of the FRONT/BACK_LEFT/RIGHT_BIT flags.
151 if (mask
& GL_COLOR_BUFFER_BIT
)
152 ddMask
|= ctx
->Color
._DrawDestMask
;
153 if ((mask
& GL_DEPTH_BUFFER_BIT
) && ctx
->Visual
.depthBits
> 0)
154 ddMask
|= GL_DEPTH_BUFFER_BIT
;
155 if ((mask
& GL_STENCIL_BUFFER_BIT
) && ctx
->Visual
.stencilBits
> 0)
156 ddMask
|= GL_STENCIL_BUFFER_BIT
;
157 if ((mask
& GL_ACCUM_BUFFER_BIT
) && ctx
->Visual
.accumRedBits
> 0)
158 ddMask
|= GL_ACCUM_BUFFER_BIT
;
160 ASSERT(ctx
->Driver
.Clear
);
161 ctx
->Driver
.Clear( ctx
, ddMask
, (GLboolean
) !ctx
->Scissor
.Enabled
,
162 x
, y
, width
, height
);
168 * Specify which color buffers to draw into.
170 * \param mode color buffer combination.
172 * \sa glDrawBuffer().
174 * Flushes the vertices and verifies the parameter and updates the
175 * gl_colorbuffer_attrib::_DrawDestMask bitfield. Marks new color state in
176 * __GLcontextRec::NewState and notifies the driver via the
177 * dd_function_table::DrawBuffer callback.
180 _mesa_DrawBuffer( GLenum mode
)
182 GET_CURRENT_CONTEXT(ctx
);
183 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
); /* too complex... */
185 if (MESA_VERBOSE
& VERBOSE_API
)
186 _mesa_debug(ctx
, "glDrawBuffer %s\n", _mesa_lookup_enum_by_nr(mode
));
189 * Do error checking and compute the _DrawDestMask bitfield.
194 if (ctx
->Visual
.stereoMode
)
195 ctx
->Color
._DrawDestMask
= FRONT_LEFT_BIT
| FRONT_RIGHT_BIT
;
197 ctx
->Color
._DrawDestMask
= FRONT_LEFT_BIT
;
200 if (!ctx
->Visual
.doubleBufferMode
) {
201 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDrawBuffer(GL_BACK)");
204 if (ctx
->Visual
.stereoMode
)
205 ctx
->Color
._DrawDestMask
= BACK_LEFT_BIT
| BACK_RIGHT_BIT
;
207 ctx
->Color
._DrawDestMask
= BACK_LEFT_BIT
;
211 ctx
->Color
._DrawDestMask
= 0;
215 if (!ctx
->Visual
.stereoMode
) {
216 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDrawBuffer(GL_RIGHT)");
218 if (ctx
->Visual
.doubleBufferMode
)
219 ctx
->Color
._DrawDestMask
= FRONT_RIGHT_BIT
| BACK_RIGHT_BIT
;
221 ctx
->Color
._DrawDestMask
= FRONT_RIGHT_BIT
;
224 if (!ctx
->Visual
.stereoMode
) {
225 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDrawBuffer(GL_FRONT_RIGHT)");
228 ctx
->Color
._DrawDestMask
= FRONT_RIGHT_BIT
;
231 if (!ctx
->Visual
.stereoMode
|| !ctx
->Visual
.doubleBufferMode
) {
232 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDrawBuffer(GL_BACK_RIGHT)");
235 ctx
->Color
._DrawDestMask
= BACK_RIGHT_BIT
;
238 if (!ctx
->Visual
.doubleBufferMode
) {
239 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDrawBuffer(GL_BACK_LEFT)");
242 ctx
->Color
._DrawDestMask
= BACK_LEFT_BIT
;
244 case GL_FRONT_AND_BACK
:
245 if (!ctx
->Visual
.doubleBufferMode
) {
246 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDrawBuffer(GL_FRONT_AND_BACK)");
249 if (ctx
->Visual
.stereoMode
)
250 ctx
->Color
._DrawDestMask
= FRONT_LEFT_BIT
| BACK_LEFT_BIT
251 | FRONT_RIGHT_BIT
| BACK_RIGHT_BIT
;
253 ctx
->Color
._DrawDestMask
= FRONT_LEFT_BIT
| BACK_LEFT_BIT
;
257 if (ctx
->Visual
.doubleBufferMode
)
258 ctx
->Color
._DrawDestMask
= FRONT_LEFT_BIT
| BACK_LEFT_BIT
;
260 ctx
->Color
._DrawDestMask
= FRONT_LEFT_BIT
;
264 ctx
->Color
._DrawDestMask
= FRONT_LEFT_BIT
;
267 if (ctx
->Const
.NumAuxBuffers
>= 1) {
268 ctx
->Color
._DrawDestMask
= AUX0_BIT
;
271 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glDrawBuffer(GL_AUX0)" );
276 if (ctx
->Const
.NumAuxBuffers
>= 2) {
277 ctx
->Color
._DrawDestMask
= AUX1_BIT
;
280 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glDrawBuffer(GL_AUX1)" );
285 if (ctx
->Const
.NumAuxBuffers
>= 3) {
286 ctx
->Color
._DrawDestMask
= AUX2_BIT
;
289 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glDrawBuffer(GL_AUX2)" );
294 if (ctx
->Const
.NumAuxBuffers
>= 4) {
295 ctx
->Color
._DrawDestMask
= AUX3_BIT
;
298 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glDrawBuffer(GL_AUX3)" );
304 _mesa_error( ctx
, GL_INVALID_ENUM
, "glDrawBuffer" );
308 ctx
->Color
.DrawBuffer
= mode
;
309 ctx
->NewState
|= _NEW_COLOR
;
312 * Call device driver function.
314 if (ctx
->Driver
.DrawBuffer
)
315 (*ctx
->Driver
.DrawBuffer
)(ctx
, mode
);
320 * Set the color buffer source for reading pixels.
322 * \param mode color buffer.
324 * \sa glReadBuffer().
326 * Verifies the parameter and updates gl_pixel_attrib::_ReadSrcMask. Marks
327 * new pixel state in __GLcontextRec::NewState and notifies the driver via
328 * dd_function_table::ReadBuffer.
331 _mesa_ReadBuffer( GLenum mode
)
333 GET_CURRENT_CONTEXT(ctx
);
334 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
336 if (MESA_VERBOSE
& VERBOSE_API
)
337 _mesa_debug(ctx
, "glReadBuffer %s\n", _mesa_lookup_enum_by_nr(mode
));
340 * Do error checking and compute ctx->Pixel._ReadSrcMask.
346 /* Front-Left buffer, always exists */
347 ctx
->Pixel
._ReadSrcMask
= FRONT_LEFT_BIT
;
351 /* Back-Left buffer, requires double buffering */
352 if (!ctx
->Visual
.doubleBufferMode
) {
353 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadBuffer" );
356 ctx
->Pixel
._ReadSrcMask
= BACK_LEFT_BIT
;
361 if (!ctx
->Visual
.stereoMode
) {
362 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadBuffer" );
365 ctx
->Pixel
._ReadSrcMask
= FRONT_RIGHT_BIT
;
368 if (!ctx
->Visual
.stereoMode
|| !ctx
->Visual
.doubleBufferMode
) {
369 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadBuffer" );
372 ctx
->Pixel
._ReadSrcMask
= BACK_RIGHT_BIT
;
375 if (ctx
->Const
.NumAuxBuffers
>= 1) {
376 ctx
->Pixel
._ReadSrcMask
= AUX0_BIT
;
379 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadBuffer(GL_AUX0)" );
384 if (ctx
->Const
.NumAuxBuffers
>= 2) {
385 ctx
->Pixel
._ReadSrcMask
= AUX1_BIT
;
388 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadBuffer(GL_AUX1)" );
393 if (ctx
->Const
.NumAuxBuffers
>= 3) {
394 ctx
->Pixel
._ReadSrcMask
= AUX2_BIT
;
397 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadBuffer(GL_AUX2)" );
402 if (ctx
->Const
.NumAuxBuffers
>= 4) {
403 ctx
->Pixel
._ReadSrcMask
= AUX3_BIT
;
406 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadBuffer(GL_AUX3)" );
412 _mesa_error( ctx
, GL_INVALID_ENUM
, "glReadBuffer" );
416 ctx
->Pixel
.ReadBuffer
= mode
;
417 ctx
->NewState
|= _NEW_PIXEL
;
420 * Call device driver function.
422 if (ctx
->Driver
.ReadBuffer
)
423 (*ctx
->Driver
.ReadBuffer
)(ctx
, mode
);
429 * GL_MESA_resize_buffers extension.
431 * When this function is called, we'll ask the window system how large
432 * the current window is. If it's a new size, we'll call the driver's
433 * ResizeBuffers function. The driver will then resize its color buffers
434 * as needed, and maybe call the swrast's routine for reallocating
435 * swrast-managed depth/stencil/accum/etc buffers.
436 * \note This function may be called from within Mesa or called by the
437 * user directly (see the GL_MESA_resize_buffers extension).
440 _mesa_ResizeBuffersMESA( void )
442 GLcontext
*ctx
= _mesa_get_current_context();
444 if (MESA_VERBOSE
& VERBOSE_API
)
445 _mesa_debug(ctx
, "glResizeBuffersMESA\n");
448 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH( ctx
);
450 if (ctx
->DrawBuffer
) {
451 GLuint buf_width
, buf_height
;
452 GLframebuffer
*buffer
= ctx
->DrawBuffer
;
454 /* ask device driver for size of output buffer */
455 (*ctx
->Driver
.GetBufferSize
)( buffer
, &buf_width
, &buf_height
);
457 /* see if size of device driver's color buffer (window) has changed */
458 if (buffer
->Width
== buf_width
&& buffer
->Height
== buf_height
)
459 return; /* size is as expected */
461 buffer
->Width
= buf_width
;
462 buffer
->Height
= buf_height
;
464 ctx
->Driver
.ResizeBuffers( buffer
);
467 if (ctx
->ReadBuffer
&& ctx
->ReadBuffer
!= ctx
->DrawBuffer
) {
468 GLuint buf_width
, buf_height
;
469 GLframebuffer
*buffer
= ctx
->ReadBuffer
;
471 /* ask device driver for size of read buffer */
472 (*ctx
->Driver
.GetBufferSize
)( buffer
, &buf_width
, &buf_height
);
474 /* see if size of device driver's color buffer (window) has changed */
475 if (buffer
->Width
== buf_width
&& buffer
->Height
== buf_height
)
476 return; /* size is as expected */
478 buffer
->Width
= buf_width
;
479 buffer
->Height
= buf_height
;
481 ctx
->Driver
.ResizeBuffers( buffer
);
484 ctx
->NewState
|= _NEW_BUFFERS
; /* to update scissor / window bounds */
489 * XXX move somewhere else someday?
492 _mesa_SampleCoverageARB(GLclampf value
, GLboolean invert
)
494 GLcontext
*ctx
= _mesa_get_current_context();
496 if (!ctx
->Extensions
.ARB_multisample
) {
497 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glSampleCoverageARB");
501 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH( ctx
);
502 ctx
->Multisample
.SampleCoverageValue
= (GLfloat
) CLAMP(value
, 0.0, 1.0);
503 ctx
->Multisample
.SampleCoverageInvert
= invert
;
504 ctx
->NewState
|= _NEW_MULTISAMPLE
;
511 * Define the scissor box.
513 * \param x, y coordinates of the scissor box lower-left corner.
514 * \param width width of the scissor box.
515 * \param height height of the scissor box.
519 * Verifies the parameters and updates __GLcontextRec::Scissor. On a
520 * change flushes the vertices and notifies the driver via
521 * the dd_function_table::Scissor callback.
524 _mesa_Scissor( GLint x
, GLint y
, GLsizei width
, GLsizei height
)
526 GET_CURRENT_CONTEXT(ctx
);
527 ASSERT_OUTSIDE_BEGIN_END(ctx
);
529 if (width
< 0 || height
< 0) {
530 _mesa_error( ctx
, GL_INVALID_VALUE
, "glScissor" );
534 if (MESA_VERBOSE
& VERBOSE_API
)
535 _mesa_debug(ctx
, "glScissor %d %d %d %d\n", x
, y
, width
, height
);
537 if (x
== ctx
->Scissor
.X
&&
538 y
== ctx
->Scissor
.Y
&&
539 width
== ctx
->Scissor
.Width
&&
540 height
== ctx
->Scissor
.Height
)
543 FLUSH_VERTICES(ctx
, _NEW_SCISSOR
);
546 ctx
->Scissor
.Width
= width
;
547 ctx
->Scissor
.Height
= height
;
549 if (ctx
->Driver
.Scissor
)
550 ctx
->Driver
.Scissor( ctx
, x
, y
, width
, height
);
553 /**********************************************************************/
554 /** \name State management */
558 * Update screen bounds.
560 * \param ctx GL context.
562 * Update gl_frame_buffer::_Xmin, and etc.
564 void _mesa_update_buffers( GLcontext
*ctx
)
566 ctx
->DrawBuffer
->_Xmin
= 0;
567 ctx
->DrawBuffer
->_Ymin
= 0;
568 ctx
->DrawBuffer
->_Xmax
= ctx
->DrawBuffer
->Width
;
569 ctx
->DrawBuffer
->_Ymax
= ctx
->DrawBuffer
->Height
;
570 if (ctx
->Scissor
.Enabled
) {
571 if (ctx
->Scissor
.X
> ctx
->DrawBuffer
->_Xmin
) {
572 ctx
->DrawBuffer
->_Xmin
= ctx
->Scissor
.X
;
574 if (ctx
->Scissor
.Y
> ctx
->DrawBuffer
->_Ymin
) {
575 ctx
->DrawBuffer
->_Ymin
= ctx
->Scissor
.Y
;
577 if (ctx
->Scissor
.X
+ ctx
->Scissor
.Width
< ctx
->DrawBuffer
->_Xmax
) {
578 ctx
->DrawBuffer
->_Xmax
= ctx
->Scissor
.X
+ ctx
->Scissor
.Width
;
580 if (ctx
->Scissor
.Y
+ ctx
->Scissor
.Height
< ctx
->DrawBuffer
->_Ymax
) {
581 ctx
->DrawBuffer
->_Ymax
= ctx
->Scissor
.Y
+ ctx
->Scissor
.Height
;
589 /**********************************************************************/
590 /** \name Initialization */
594 * Initialize the context scissor data.
596 * \param ctx GL context.
598 * Initializes the __GLcontextRec::Scissor and __GLcontextRec::Multisample
599 * attribute groups, and related constants in __GLcontextRec::Const.
601 void _mesa_init_buffers( GLcontext
* ctx
)
604 ctx
->Scissor
.Enabled
= GL_FALSE
;
607 ctx
->Scissor
.Width
= 0;
608 ctx
->Scissor
.Height
= 0;
611 ctx
->Multisample
.Enabled
= GL_FALSE
;
612 ctx
->Multisample
.SampleAlphaToCoverage
= GL_FALSE
;
613 ctx
->Multisample
.SampleAlphaToOne
= GL_FALSE
;
614 ctx
->Multisample
.SampleCoverage
= GL_FALSE
;
615 ctx
->Multisample
.SampleCoverageValue
= 1.0;
616 ctx
->Multisample
.SampleCoverageInvert
= GL_FALSE
;