f7edb1b855c58e0552e122b879dc7fbb4b936714
1 /* $Id: buffers.c,v 1.39 2002/10/11 00:02:16 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
));
150 if (ctx
->Color
.DrawBuffer
== mode
)
151 return; /* no change */
154 * Do error checking and compute the _DrawDestMask bitfield.
161 /* AUX buffers not implemented in Mesa at this time */
162 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glDrawBuffer" );
165 if (!ctx
->Visual
.stereoMode
) {
166 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glDrawBuffer" );
168 if (ctx
->Visual
.doubleBufferMode
)
169 ctx
->Color
._DrawDestMask
= FRONT_RIGHT_BIT
| BACK_RIGHT_BIT
;
171 ctx
->Color
._DrawDestMask
= FRONT_RIGHT_BIT
;
174 if (!ctx
->Visual
.stereoMode
) {
175 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glDrawBuffer" );
178 ctx
->Color
._DrawDestMask
= FRONT_RIGHT_BIT
;
181 if (!ctx
->Visual
.stereoMode
) {
182 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glDrawBuffer" );
185 if (!ctx
->Visual
.doubleBufferMode
) {
186 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glDrawBuffer" );
189 ctx
->Color
._DrawDestMask
= BACK_RIGHT_BIT
;
192 if (!ctx
->Visual
.doubleBufferMode
) {
193 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glDrawBuffer" );
196 ctx
->Color
._DrawDestMask
= BACK_LEFT_BIT
;
198 case GL_FRONT_AND_BACK
:
199 if (!ctx
->Visual
.doubleBufferMode
) {
200 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glDrawBuffer" );
203 if (ctx
->Visual
.stereoMode
)
204 ctx
->Color
._DrawDestMask
= FRONT_LEFT_BIT
| BACK_LEFT_BIT
205 | FRONT_RIGHT_BIT
| BACK_RIGHT_BIT
;
207 ctx
->Color
._DrawDestMask
= FRONT_LEFT_BIT
| BACK_LEFT_BIT
;
210 if (!ctx
->Visual
.doubleBufferMode
) {
211 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glDrawBuffer" );
214 if (ctx
->Visual
.stereoMode
)
215 ctx
->Color
._DrawDestMask
= BACK_LEFT_BIT
| BACK_RIGHT_BIT
;
217 ctx
->Color
._DrawDestMask
= BACK_LEFT_BIT
;
221 if (ctx
->Visual
.doubleBufferMode
)
222 ctx
->Color
._DrawDestMask
= FRONT_LEFT_BIT
| BACK_LEFT_BIT
;
224 ctx
->Color
._DrawDestMask
= FRONT_LEFT_BIT
;
228 ctx
->Color
._DrawDestMask
= FRONT_LEFT_BIT
;
232 if (ctx
->Visual
.stereoMode
)
233 ctx
->Color
._DrawDestMask
= FRONT_LEFT_BIT
| FRONT_RIGHT_BIT
;
235 ctx
->Color
._DrawDestMask
= FRONT_LEFT_BIT
;
239 ctx
->Color
._DrawDestMask
= 0;
242 _mesa_error( ctx
, GL_INVALID_ENUM
, "glDrawBuffer" );
246 ctx
->Color
.DrawBuffer
= mode
;
247 ctx
->NewState
|= _NEW_COLOR
;
250 * Call device driver function.
252 if (ctx
->Driver
.DrawBuffer
)
253 (*ctx
->Driver
.DrawBuffer
)(ctx
, mode
);
259 _mesa_ReadBuffer( GLenum mode
)
261 GET_CURRENT_CONTEXT(ctx
);
262 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
264 if (MESA_VERBOSE
& VERBOSE_API
)
265 _mesa_debug(ctx
, "glReadBuffer %s\n", _mesa_lookup_enum_by_nr(mode
));
267 if (ctx
->Pixel
.ReadBuffer
== mode
)
268 return; /* no change */
275 /* AUX buffers not implemented in Mesa at this time */
276 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadBuffer" );
281 /* Front-Left buffer, always exists */
282 ctx
->Pixel
._DriverReadBuffer
= GL_FRONT_LEFT
;
286 /* Back-Left buffer, requires double buffering */
287 if (!ctx
->Visual
.doubleBufferMode
) {
288 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadBuffer" );
291 ctx
->Pixel
._DriverReadBuffer
= GL_BACK_LEFT
;
295 if (!ctx
->Visual
.stereoMode
) {
296 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadBuffer" );
299 ctx
->Pixel
._DriverReadBuffer
= GL_FRONT_RIGHT
;
302 if (!ctx
->Visual
.stereoMode
|| !ctx
->Visual
.doubleBufferMode
) {
303 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadBuffer" );
306 ctx
->Pixel
._DriverReadBuffer
= GL_BACK_RIGHT
;
309 _mesa_error( ctx
, GL_INVALID_ENUM
, "glReadBuffer" );
313 ctx
->Pixel
.ReadBuffer
= mode
;
314 ctx
->NewState
|= _NEW_PIXEL
;
317 * Call device driver function.
319 if (ctx
->Driver
.ReadBuffer
)
320 (*ctx
->Driver
.ReadBuffer
)(ctx
, mode
);
325 * GL_MESA_resize_buffers extension
326 * When this function is called, we'll ask the window system how large
327 * the current window is. If it's not what we expect, we'll have to
328 * resize/reallocate the software accum/stencil/depth/alpha buffers.
331 _mesa_ResizeBuffersMESA( void )
333 GLcontext
*ctx
= _mesa_get_current_context();
335 if (MESA_VERBOSE
& VERBOSE_API
)
336 _mesa_debug(ctx
, "glResizeBuffersMESA\n");
339 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH( ctx
);
341 if (ctx
->DrawBuffer
) {
342 GLuint buf_width
, buf_height
;
343 GLframebuffer
*buffer
= ctx
->DrawBuffer
;
345 /* ask device driver for size of output buffer */
346 (*ctx
->Driver
.GetBufferSize
)( buffer
, &buf_width
, &buf_height
);
348 /* see if size of device driver's color buffer (window) has changed */
349 if (buffer
->Width
== buf_width
&& buffer
->Height
== buf_height
)
350 return; /* size is as expected */
352 buffer
->Width
= buf_width
;
353 buffer
->Height
= buf_height
;
355 ctx
->Driver
.ResizeBuffers( buffer
);
358 if (ctx
->ReadBuffer
&& ctx
->ReadBuffer
!= ctx
->DrawBuffer
) {
359 GLuint buf_width
, buf_height
;
360 GLframebuffer
*buffer
= ctx
->DrawBuffer
;
362 /* ask device driver for size of output buffer */
363 (*ctx
->Driver
.GetBufferSize
)( buffer
, &buf_width
, &buf_height
);
365 /* see if size of device driver's color buffer (window) has changed */
366 if (buffer
->Width
== buf_width
&& buffer
->Height
== buf_height
)
367 return; /* size is as expected */
369 buffer
->Width
= buf_width
;
370 buffer
->Height
= buf_height
;
372 ctx
->Driver
.ResizeBuffers( buffer
);
375 ctx
->NewState
|= _NEW_BUFFERS
; /* to update scissor / window bounds */
381 _mesa_Scissor( GLint x
, GLint y
, GLsizei width
, GLsizei height
)
383 GET_CURRENT_CONTEXT(ctx
);
384 ASSERT_OUTSIDE_BEGIN_END(ctx
);
386 if (width
< 0 || height
< 0) {
387 _mesa_error( ctx
, GL_INVALID_VALUE
, "glScissor" );
391 if (MESA_VERBOSE
& VERBOSE_API
)
392 _mesa_debug(ctx
, "glScissor %d %d %d %d\n", x
, y
, width
, height
);
394 if (x
== ctx
->Scissor
.X
&&
395 y
== ctx
->Scissor
.Y
&&
396 width
== ctx
->Scissor
.Width
&&
397 height
== ctx
->Scissor
.Height
)
400 FLUSH_VERTICES(ctx
, _NEW_SCISSOR
);
403 ctx
->Scissor
.Width
= width
;
404 ctx
->Scissor
.Height
= height
;
406 if (ctx
->Driver
.Scissor
)
407 ctx
->Driver
.Scissor( ctx
, x
, y
, width
, height
);
412 * XXX move somewhere else someday?
415 _mesa_SampleCoverageARB(GLclampf value
, GLboolean invert
)
417 GLcontext
*ctx
= _mesa_get_current_context();
419 if (!ctx
->Extensions
.ARB_multisample
) {
420 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glSampleCoverageARB");
424 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH( ctx
);
425 ctx
->Multisample
.SampleCoverageValue
= (GLfloat
) CLAMP(value
, 0.0, 1.0);
426 ctx
->Multisample
.SampleCoverageInvert
= invert
;
427 ctx
->NewState
|= _NEW_MULTISAMPLE
;