1 /* $Id: buffers.c,v 1.40 2002/10/11 15:17:12 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2002 Brian Paul All Rights Reserved.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
48 _mesa_ClearIndex( GLfloat c
)
50 GET_CURRENT_CONTEXT(ctx
);
51 ASSERT_OUTSIDE_BEGIN_END(ctx
);
53 if (ctx
->Color
.ClearIndex
== (GLuint
) c
)
56 FLUSH_VERTICES(ctx
, _NEW_COLOR
);
57 ctx
->Color
.ClearIndex
= (GLuint
) c
;
59 if (!ctx
->Visual
.rgbMode
&& ctx
->Driver
.ClearIndex
) {
60 /* it's OK to call glClearIndex in RGBA mode but it should be a NOP */
61 (*ctx
->Driver
.ClearIndex
)( ctx
, ctx
->Color
.ClearIndex
);
68 _mesa_ClearColor( GLclampf red
, GLclampf green
, GLclampf blue
, GLclampf alpha
)
71 GET_CURRENT_CONTEXT(ctx
);
72 ASSERT_OUTSIDE_BEGIN_END(ctx
);
74 tmp
[0] = CLAMP(red
, 0.0F
, 1.0F
);
75 tmp
[1] = CLAMP(green
, 0.0F
, 1.0F
);
76 tmp
[2] = CLAMP(blue
, 0.0F
, 1.0F
);
77 tmp
[3] = CLAMP(alpha
, 0.0F
, 1.0F
);
79 if (TEST_EQ_4V(tmp
, ctx
->Color
.ClearColor
))
80 return; /* no change */
82 FLUSH_VERTICES(ctx
, _NEW_COLOR
);
83 COPY_4V(ctx
->Color
.ClearColor
, tmp
);
85 if (ctx
->Visual
.rgbMode
&& ctx
->Driver
.ClearColor
) {
86 /* it's OK to call glClearColor in CI mode but it should be a NOP */
87 (*ctx
->Driver
.ClearColor
)(ctx
, ctx
->Color
.ClearColor
);
94 _mesa_Clear( GLbitfield mask
)
96 GET_CURRENT_CONTEXT(ctx
);
97 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
99 if (MESA_VERBOSE
& VERBOSE_API
)
100 _mesa_debug(ctx
, "glClear 0x%x\n", mask
);
102 if (mask
& ~(GL_COLOR_BUFFER_BIT
|
103 GL_DEPTH_BUFFER_BIT
|
104 GL_STENCIL_BUFFER_BIT
|
105 GL_ACCUM_BUFFER_BIT
)) {
106 /* invalid bit set */
107 _mesa_error( ctx
, GL_INVALID_VALUE
, "glClear(mask)");
112 _mesa_update_state( ctx
); /* update _Xmin, etc */
115 if (ctx
->RenderMode
==GL_RENDER
) {
116 const GLint x
= ctx
->DrawBuffer
->_Xmin
;
117 const GLint y
= ctx
->DrawBuffer
->_Ymin
;
118 const GLint height
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
119 const GLint width
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
122 /* don't clear depth buffer if depth writing disabled */
123 if (!ctx
->Depth
.Mask
)
124 mask
&= ~GL_DEPTH_BUFFER_BIT
;
126 /* Build bitmask to send to driver Clear function */
127 ddMask
= mask
& (GL_DEPTH_BUFFER_BIT
|
128 GL_STENCIL_BUFFER_BIT
|
129 GL_ACCUM_BUFFER_BIT
);
130 if (mask
& GL_COLOR_BUFFER_BIT
) {
131 ddMask
|= ctx
->Color
._DrawDestMask
;
134 ASSERT(ctx
->Driver
.Clear
);
135 ctx
->Driver
.Clear( ctx
, ddMask
, (GLboolean
) !ctx
->Scissor
.Enabled
,
136 x
, y
, width
, height
);
142 _mesa_DrawBuffer( GLenum mode
)
144 GET_CURRENT_CONTEXT(ctx
);
145 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
); /* too complex... */
147 if (MESA_VERBOSE
& VERBOSE_API
)
148 _mesa_debug(ctx
, "glDrawBuffer %s\n", _mesa_lookup_enum_by_nr(mode
));
151 * Do error checking and compute the _DrawDestMask bitfield.
158 /* AUX buffers not implemented in Mesa at this time */
159 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glDrawBuffer" );
162 if (!ctx
->Visual
.stereoMode
) {
163 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glDrawBuffer" );
165 if (ctx
->Visual
.doubleBufferMode
)
166 ctx
->Color
._DrawDestMask
= FRONT_RIGHT_BIT
| BACK_RIGHT_BIT
;
168 ctx
->Color
._DrawDestMask
= FRONT_RIGHT_BIT
;
171 if (!ctx
->Visual
.stereoMode
) {
172 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glDrawBuffer" );
175 ctx
->Color
._DrawDestMask
= FRONT_RIGHT_BIT
;
178 if (!ctx
->Visual
.stereoMode
) {
179 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glDrawBuffer" );
182 if (!ctx
->Visual
.doubleBufferMode
) {
183 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glDrawBuffer" );
186 ctx
->Color
._DrawDestMask
= BACK_RIGHT_BIT
;
189 if (!ctx
->Visual
.doubleBufferMode
) {
190 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glDrawBuffer" );
193 ctx
->Color
._DrawDestMask
= BACK_LEFT_BIT
;
195 case GL_FRONT_AND_BACK
:
196 if (!ctx
->Visual
.doubleBufferMode
) {
197 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glDrawBuffer" );
200 if (ctx
->Visual
.stereoMode
)
201 ctx
->Color
._DrawDestMask
= FRONT_LEFT_BIT
| BACK_LEFT_BIT
202 | FRONT_RIGHT_BIT
| BACK_RIGHT_BIT
;
204 ctx
->Color
._DrawDestMask
= FRONT_LEFT_BIT
| BACK_LEFT_BIT
;
207 if (!ctx
->Visual
.doubleBufferMode
) {
208 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glDrawBuffer" );
211 if (ctx
->Visual
.stereoMode
)
212 ctx
->Color
._DrawDestMask
= BACK_LEFT_BIT
| BACK_RIGHT_BIT
;
214 ctx
->Color
._DrawDestMask
= BACK_LEFT_BIT
;
218 if (ctx
->Visual
.doubleBufferMode
)
219 ctx
->Color
._DrawDestMask
= FRONT_LEFT_BIT
| BACK_LEFT_BIT
;
221 ctx
->Color
._DrawDestMask
= FRONT_LEFT_BIT
;
225 ctx
->Color
._DrawDestMask
= FRONT_LEFT_BIT
;
229 if (ctx
->Visual
.stereoMode
)
230 ctx
->Color
._DrawDestMask
= FRONT_LEFT_BIT
| FRONT_RIGHT_BIT
;
232 ctx
->Color
._DrawDestMask
= FRONT_LEFT_BIT
;
236 ctx
->Color
._DrawDestMask
= 0;
239 _mesa_error( ctx
, GL_INVALID_ENUM
, "glDrawBuffer" );
243 ctx
->Color
.DrawBuffer
= mode
;
244 ctx
->NewState
|= _NEW_COLOR
;
247 * Call device driver function.
249 if (ctx
->Driver
.DrawBuffer
)
250 (*ctx
->Driver
.DrawBuffer
)(ctx
, mode
);
256 _mesa_ReadBuffer( GLenum mode
)
258 GET_CURRENT_CONTEXT(ctx
);
259 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
261 if (MESA_VERBOSE
& VERBOSE_API
)
262 _mesa_debug(ctx
, "glReadBuffer %s\n", _mesa_lookup_enum_by_nr(mode
));
264 if (ctx
->Pixel
.ReadBuffer
== mode
)
265 return; /* no change */
272 /* AUX buffers not implemented in Mesa at this time */
273 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadBuffer" );
278 /* Front-Left buffer, always exists */
279 ctx
->Pixel
._DriverReadBuffer
= GL_FRONT_LEFT
;
283 /* Back-Left buffer, requires double buffering */
284 if (!ctx
->Visual
.doubleBufferMode
) {
285 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadBuffer" );
288 ctx
->Pixel
._DriverReadBuffer
= GL_BACK_LEFT
;
292 if (!ctx
->Visual
.stereoMode
) {
293 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadBuffer" );
296 ctx
->Pixel
._DriverReadBuffer
= GL_FRONT_RIGHT
;
299 if (!ctx
->Visual
.stereoMode
|| !ctx
->Visual
.doubleBufferMode
) {
300 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadBuffer" );
303 ctx
->Pixel
._DriverReadBuffer
= GL_BACK_RIGHT
;
306 _mesa_error( ctx
, GL_INVALID_ENUM
, "glReadBuffer" );
310 ctx
->Pixel
.ReadBuffer
= mode
;
311 ctx
->NewState
|= _NEW_PIXEL
;
314 * Call device driver function.
316 if (ctx
->Driver
.ReadBuffer
)
317 (*ctx
->Driver
.ReadBuffer
)(ctx
, mode
);
322 * GL_MESA_resize_buffers extension
323 * When this function is called, we'll ask the window system how large
324 * the current window is. If it's not what we expect, we'll have to
325 * resize/reallocate the software accum/stencil/depth/alpha buffers.
328 _mesa_ResizeBuffersMESA( void )
330 GLcontext
*ctx
= _mesa_get_current_context();
332 if (MESA_VERBOSE
& VERBOSE_API
)
333 _mesa_debug(ctx
, "glResizeBuffersMESA\n");
336 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH( ctx
);
338 if (ctx
->DrawBuffer
) {
339 GLuint buf_width
, buf_height
;
340 GLframebuffer
*buffer
= ctx
->DrawBuffer
;
342 /* ask device driver for size of output buffer */
343 (*ctx
->Driver
.GetBufferSize
)( buffer
, &buf_width
, &buf_height
);
345 /* see if size of device driver's color buffer (window) has changed */
346 if (buffer
->Width
== buf_width
&& buffer
->Height
== buf_height
)
347 return; /* size is as expected */
349 buffer
->Width
= buf_width
;
350 buffer
->Height
= buf_height
;
352 ctx
->Driver
.ResizeBuffers( buffer
);
355 if (ctx
->ReadBuffer
&& ctx
->ReadBuffer
!= ctx
->DrawBuffer
) {
356 GLuint buf_width
, buf_height
;
357 GLframebuffer
*buffer
= ctx
->DrawBuffer
;
359 /* ask device driver for size of output buffer */
360 (*ctx
->Driver
.GetBufferSize
)( buffer
, &buf_width
, &buf_height
);
362 /* see if size of device driver's color buffer (window) has changed */
363 if (buffer
->Width
== buf_width
&& buffer
->Height
== buf_height
)
364 return; /* size is as expected */
366 buffer
->Width
= buf_width
;
367 buffer
->Height
= buf_height
;
369 ctx
->Driver
.ResizeBuffers( buffer
);
372 ctx
->NewState
|= _NEW_BUFFERS
; /* to update scissor / window bounds */
378 _mesa_Scissor( GLint x
, GLint y
, GLsizei width
, GLsizei height
)
380 GET_CURRENT_CONTEXT(ctx
);
381 ASSERT_OUTSIDE_BEGIN_END(ctx
);
383 if (width
< 0 || height
< 0) {
384 _mesa_error( ctx
, GL_INVALID_VALUE
, "glScissor" );
388 if (MESA_VERBOSE
& VERBOSE_API
)
389 _mesa_debug(ctx
, "glScissor %d %d %d %d\n", x
, y
, width
, height
);
391 if (x
== ctx
->Scissor
.X
&&
392 y
== ctx
->Scissor
.Y
&&
393 width
== ctx
->Scissor
.Width
&&
394 height
== ctx
->Scissor
.Height
)
397 FLUSH_VERTICES(ctx
, _NEW_SCISSOR
);
400 ctx
->Scissor
.Width
= width
;
401 ctx
->Scissor
.Height
= height
;
403 if (ctx
->Driver
.Scissor
)
404 ctx
->Driver
.Scissor( ctx
, x
, y
, width
, height
);
409 * XXX move somewhere else someday?
412 _mesa_SampleCoverageARB(GLclampf value
, GLboolean invert
)
414 GLcontext
*ctx
= _mesa_get_current_context();
416 if (!ctx
->Extensions
.ARB_multisample
) {
417 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glSampleCoverageARB");
421 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH( ctx
);
422 ctx
->Multisample
.SampleCoverageValue
= (GLfloat
) CLAMP(value
, 0.0, 1.0);
423 ctx
->Multisample
.SampleCoverageInvert
= invert
;
424 ctx
->NewState
|= _NEW_MULTISAMPLE
;