c549e2736c49ae6a99fe03292df0b1361b089fac
1 /* $Id: buffers.c,v 1.36 2002/06/15 02:38:15 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 UNCLAMPED_FLOAT_TO_CHAN(tmp
[0], red
);
75 UNCLAMPED_FLOAT_TO_CHAN(tmp
[1], green
);
76 UNCLAMPED_FLOAT_TO_CHAN(tmp
[2], blue
);
77 UNCLAMPED_FLOAT_TO_CHAN(tmp
[3], alpha
);
79 if (TEST_EQ_4V(tmp
, ctx
->Color
.ClearColor
))
82 FLUSH_VERTICES(ctx
, _NEW_COLOR
);
83 COPY_CHAN4(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 CLEAR_BITS(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... */
148 if (MESA_VERBOSE
& VERBOSE_API
)
149 _mesa_debug(ctx
, "glDrawBuffer %s\n", _mesa_lookup_enum_by_nr(mode
));
156 /* AUX buffers not implemented in Mesa at this time */
157 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glDrawBuffer" );
160 if (!ctx
->Visual
.stereoMode
) {
161 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glDrawBuffer" );
163 if (ctx
->Visual
.doubleBufferMode
)
164 ctx
->Color
.DrawDestMask
= FRONT_RIGHT_BIT
| BACK_RIGHT_BIT
;
166 ctx
->Color
.DrawDestMask
= FRONT_RIGHT_BIT
;
169 if (!ctx
->Visual
.stereoMode
) {
170 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glDrawBuffer" );
173 ctx
->Color
.DrawDestMask
= FRONT_RIGHT_BIT
;
176 if (!ctx
->Visual
.stereoMode
) {
177 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glDrawBuffer" );
180 if (!ctx
->Visual
.doubleBufferMode
) {
181 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glDrawBuffer" );
184 ctx
->Color
.DrawDestMask
= BACK_RIGHT_BIT
;
187 if (!ctx
->Visual
.doubleBufferMode
) {
188 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glDrawBuffer" );
191 ctx
->Color
.DrawDestMask
= BACK_LEFT_BIT
;
193 case GL_FRONT_AND_BACK
:
194 if (!ctx
->Visual
.doubleBufferMode
) {
195 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glDrawBuffer" );
198 if (ctx
->Visual
.stereoMode
)
199 ctx
->Color
.DrawDestMask
= FRONT_LEFT_BIT
| BACK_LEFT_BIT
200 | FRONT_RIGHT_BIT
| BACK_RIGHT_BIT
;
202 ctx
->Color
.DrawDestMask
= FRONT_LEFT_BIT
| BACK_LEFT_BIT
;
205 if (!ctx
->Visual
.doubleBufferMode
) {
206 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glDrawBuffer" );
209 if (ctx
->Visual
.stereoMode
)
210 ctx
->Color
.DrawDestMask
= BACK_LEFT_BIT
| BACK_RIGHT_BIT
;
212 ctx
->Color
.DrawDestMask
= BACK_LEFT_BIT
;
216 if (ctx
->Visual
.doubleBufferMode
)
217 ctx
->Color
.DrawDestMask
= FRONT_LEFT_BIT
| BACK_LEFT_BIT
;
219 ctx
->Color
.DrawDestMask
= FRONT_LEFT_BIT
;
223 ctx
->Color
.DrawDestMask
= FRONT_LEFT_BIT
;
227 if (ctx
->Visual
.stereoMode
)
228 ctx
->Color
.DrawDestMask
= FRONT_LEFT_BIT
| FRONT_RIGHT_BIT
;
230 ctx
->Color
.DrawDestMask
= FRONT_LEFT_BIT
;
234 ctx
->Color
.DrawDestMask
= 0;
237 _mesa_error( ctx
, GL_INVALID_ENUM
, "glDrawBuffer" );
242 * Make the dest buffer mode more precise if possible
244 if (mode
== GL_LEFT
&& !ctx
->Visual
.doubleBufferMode
)
245 ctx
->Color
.DriverDrawBuffer
= GL_FRONT_LEFT
;
246 else if (mode
== GL_RIGHT
&& !ctx
->Visual
.doubleBufferMode
)
247 ctx
->Color
.DriverDrawBuffer
= GL_FRONT_RIGHT
;
248 else if (mode
== GL_FRONT
&& !ctx
->Visual
.stereoMode
)
249 ctx
->Color
.DriverDrawBuffer
= GL_FRONT_LEFT
;
250 else if (mode
== GL_BACK
&& !ctx
->Visual
.stereoMode
)
251 ctx
->Color
.DriverDrawBuffer
= GL_BACK_LEFT
;
253 ctx
->Color
.DriverDrawBuffer
= mode
;
256 * Set current alpha buffer pointer
258 if (ctx
->DrawBuffer
->UseSoftwareAlphaBuffers
) {
259 if (ctx
->Color
.DriverDrawBuffer
== GL_FRONT_LEFT
)
260 ctx
->DrawBuffer
->Alpha
= ctx
->DrawBuffer
->FrontLeftAlpha
;
261 else if (ctx
->Color
.DriverDrawBuffer
== GL_BACK_LEFT
)
262 ctx
->DrawBuffer
->Alpha
= ctx
->DrawBuffer
->BackLeftAlpha
;
263 else if (ctx
->Color
.DriverDrawBuffer
== GL_FRONT_RIGHT
)
264 ctx
->DrawBuffer
->Alpha
= ctx
->DrawBuffer
->FrontRightAlpha
;
265 else if (ctx
->Color
.DriverDrawBuffer
== GL_BACK_RIGHT
)
266 ctx
->DrawBuffer
->Alpha
= ctx
->DrawBuffer
->BackRightAlpha
;
270 * If we get here there can't have been an error. Now tell the
271 * device driver about it.
273 ASSERT(ctx
->Driver
.SetDrawBuffer
);
274 (*ctx
->Driver
.SetDrawBuffer
)(ctx
, ctx
->Color
.DriverDrawBuffer
);
276 ctx
->Color
.DrawBuffer
= mode
;
277 ctx
->NewState
|= _NEW_COLOR
;
283 _mesa_ReadBuffer( GLenum mode
)
285 GET_CURRENT_CONTEXT(ctx
);
286 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
288 if (MESA_VERBOSE
& VERBOSE_API
)
289 _mesa_debug(ctx
, "glReadBuffer %s\n", _mesa_lookup_enum_by_nr(mode
));
296 /* AUX buffers not implemented in Mesa at this time */
297 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadBuffer" );
302 /* Front-Left buffer, always exists */
303 ctx
->Pixel
.DriverReadBuffer
= GL_FRONT_LEFT
;
307 /* Back-Left buffer, requires double buffering */
308 if (!ctx
->Visual
.doubleBufferMode
) {
309 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadBuffer" );
312 ctx
->Pixel
.DriverReadBuffer
= GL_BACK_LEFT
;
316 if (!ctx
->Visual
.stereoMode
) {
317 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadBuffer" );
320 ctx
->Pixel
.DriverReadBuffer
= GL_FRONT_RIGHT
;
323 if (!ctx
->Visual
.stereoMode
|| !ctx
->Visual
.doubleBufferMode
) {
324 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadBuffer" );
327 ctx
->Pixel
.DriverReadBuffer
= GL_BACK_RIGHT
;
330 _mesa_error( ctx
, GL_INVALID_ENUM
, "glReadBuffer" );
334 ctx
->Pixel
.ReadBuffer
= mode
;
335 ctx
->NewState
|= _NEW_PIXEL
;
340 * GL_MESA_resize_buffers extension
341 * When this function is called, we'll ask the window system how large
342 * the current window is. If it's not what we expect, we'll have to
343 * resize/reallocate the software accum/stencil/depth/alpha buffers.
346 _mesa_ResizeBuffersMESA( void )
348 GLcontext
*ctx
= _mesa_get_current_context();
350 if (MESA_VERBOSE
& VERBOSE_API
)
351 _mesa_debug(ctx
, "glResizeBuffersMESA\n");
354 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH( ctx
);
356 if (ctx
->DrawBuffer
) {
357 GLuint buf_width
, buf_height
;
358 GLframebuffer
*buffer
= ctx
->DrawBuffer
;
360 /* ask device driver for size of output buffer */
361 (*ctx
->Driver
.GetBufferSize
)( buffer
, &buf_width
, &buf_height
);
363 /* see if size of device driver's color buffer (window) has changed */
364 if (buffer
->Width
== buf_width
&& buffer
->Height
== buf_height
)
365 return; /* size is as expected */
367 buffer
->Width
= buf_width
;
368 buffer
->Height
= buf_height
;
370 ctx
->Driver
.ResizeBuffers( buffer
);
373 if (ctx
->ReadBuffer
&& ctx
->ReadBuffer
!= ctx
->DrawBuffer
) {
374 GLuint buf_width
, buf_height
;
375 GLframebuffer
*buffer
= ctx
->DrawBuffer
;
377 /* ask device driver for size of output buffer */
378 (*ctx
->Driver
.GetBufferSize
)( buffer
, &buf_width
, &buf_height
);
380 /* see if size of device driver's color buffer (window) has changed */
381 if (buffer
->Width
== buf_width
&& buffer
->Height
== buf_height
)
382 return; /* size is as expected */
384 buffer
->Width
= buf_width
;
385 buffer
->Height
= buf_height
;
387 ctx
->Driver
.ResizeBuffers( buffer
);
390 ctx
->NewState
|= _NEW_BUFFERS
; /* to update scissor / window bounds */
396 _mesa_Scissor( GLint x
, GLint y
, GLsizei width
, GLsizei height
)
398 GET_CURRENT_CONTEXT(ctx
);
399 ASSERT_OUTSIDE_BEGIN_END(ctx
);
401 if (width
< 0 || height
< 0) {
402 _mesa_error( ctx
, GL_INVALID_VALUE
, "glScissor" );
406 if (MESA_VERBOSE
& VERBOSE_API
)
407 _mesa_debug(ctx
, "glScissor %d %d %d %d\n", x
, y
, width
, height
);
409 if (x
== ctx
->Scissor
.X
&&
410 y
== ctx
->Scissor
.Y
&&
411 width
== ctx
->Scissor
.Width
&&
412 height
== ctx
->Scissor
.Height
)
415 FLUSH_VERTICES(ctx
, _NEW_SCISSOR
);
418 ctx
->Scissor
.Width
= width
;
419 ctx
->Scissor
.Height
= height
;
421 if (ctx
->Driver
.Scissor
)
422 ctx
->Driver
.Scissor( ctx
, x
, y
, width
, height
);
427 * XXX move somewhere else someday?
430 _mesa_SampleCoverageARB(GLclampf value
, GLboolean invert
)
432 GLcontext
*ctx
= _mesa_get_current_context();
434 if (!ctx
->Extensions
.ARB_multisample
) {
435 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glSampleCoverageARB");
439 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH( ctx
);
440 ctx
->Multisample
.SampleCoverageValue
= (GLfloat
) CLAMP(value
, 0.0, 1.0);
441 ctx
->Multisample
.SampleCoverageInvert
= invert
;
442 ctx
->NewState
|= _NEW_MULTISAMPLE
;