1 /* $Id: buffers.c,v 1.32 2002/02/15 16:25: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 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 fprintf(stderr
, "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 fprintf(stderr
, "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.
271 * Now see if device driver can implement the drawing to the target
272 * buffer(s). The driver may not be able to do GL_FRONT_AND_BACK mode
273 * for example. We'll take care of that in the core code by looping
274 * over the individual buffers.
276 ASSERT(ctx
->Driver
.SetDrawBuffer
);
277 if ( (*ctx
->Driver
.SetDrawBuffer
)(ctx
, ctx
->Color
.DriverDrawBuffer
) ) {
278 /* All OK, the driver will do all buffer writes */
279 ctx
->Color
.MultiDrawBuffer
= GL_FALSE
;
282 /* We'll have to loop over the multiple draw buffer targets */
283 ctx
->Color
.MultiDrawBuffer
= GL_TRUE
;
284 /* Set drawing buffer to front for now */
285 (void) (*ctx
->Driver
.SetDrawBuffer
)(ctx
, GL_FRONT_LEFT
);
288 ctx
->Color
.DrawBuffer
= mode
;
289 ctx
->NewState
|= _NEW_COLOR
;
295 _mesa_ReadBuffer( GLenum mode
)
297 GET_CURRENT_CONTEXT(ctx
);
298 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
300 if (MESA_VERBOSE
& VERBOSE_API
)
301 fprintf(stderr
, "glReadBuffer %s\n", _mesa_lookup_enum_by_nr(mode
));
308 /* AUX buffers not implemented in Mesa at this time */
309 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadBuffer" );
314 /* Front-Left buffer, always exists */
315 ctx
->Pixel
.DriverReadBuffer
= GL_FRONT_LEFT
;
319 /* Back-Left buffer, requires double buffering */
320 if (!ctx
->Visual
.doubleBufferMode
) {
321 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadBuffer" );
324 ctx
->Pixel
.DriverReadBuffer
= GL_BACK_LEFT
;
328 if (!ctx
->Visual
.stereoMode
) {
329 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadBuffer" );
332 ctx
->Pixel
.DriverReadBuffer
= GL_FRONT_RIGHT
;
335 if (!ctx
->Visual
.stereoMode
|| !ctx
->Visual
.doubleBufferMode
) {
336 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadBuffer" );
339 ctx
->Pixel
.DriverReadBuffer
= GL_BACK_RIGHT
;
342 _mesa_error( ctx
, GL_INVALID_ENUM
, "glReadBuffer" );
346 ctx
->Pixel
.ReadBuffer
= mode
;
347 ctx
->NewState
|= _NEW_PIXEL
;
352 * GL_MESA_resize_buffers extension
355 _mesa_ResizeBuffersMESA( void )
357 GLcontext
*ctx
= _mesa_get_current_context();
358 GLuint buf_width
, buf_height
;
359 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH( ctx
);
362 if (MESA_VERBOSE
& VERBOSE_API
)
363 fprintf(stderr
, "glResizeBuffersMESA\n");
365 /* ask device driver for size of output buffer */
366 (*ctx
->Driver
.GetBufferSize
)( ctx
, &buf_width
, &buf_height
);
368 /* see if size of device driver's color buffer (window) has changed */
369 if (ctx
->DrawBuffer
->Width
== (GLint
) buf_width
&&
370 ctx
->DrawBuffer
->Height
== (GLint
) buf_height
)
373 ctx
->NewState
|= _NEW_BUFFERS
; /* to update scissor / window bounds */
375 /* save buffer size */
376 ctx
->DrawBuffer
->Width
= buf_width
;
377 ctx
->DrawBuffer
->Height
= buf_height
;
379 ctx
->Driver
.ResizeBuffersMESA( ctx
);
384 _mesa_Scissor( GLint x
, GLint y
, GLsizei width
, GLsizei height
)
386 GET_CURRENT_CONTEXT(ctx
);
387 ASSERT_OUTSIDE_BEGIN_END(ctx
);
389 if (width
< 0 || height
< 0) {
390 _mesa_error( ctx
, GL_INVALID_VALUE
, "glScissor" );
394 if (MESA_VERBOSE
& VERBOSE_API
)
395 fprintf(stderr
, "glScissor %d %d %d %d\n", x
, y
, width
, height
);
397 if (x
== ctx
->Scissor
.X
&&
398 y
== ctx
->Scissor
.Y
&&
399 width
== ctx
->Scissor
.Width
&&
400 height
== ctx
->Scissor
.Height
)
403 FLUSH_VERTICES(ctx
, _NEW_SCISSOR
);
406 ctx
->Scissor
.Width
= width
;
407 ctx
->Scissor
.Height
= height
;
409 if (ctx
->Driver
.Scissor
)
410 ctx
->Driver
.Scissor( ctx
, x
, y
, width
, height
);
415 * XXX move somewhere else someday?
418 _mesa_SampleCoverageARB(GLclampf value
, GLboolean invert
)
420 GLcontext
*ctx
= _mesa_get_current_context();
422 if (!ctx
->Extensions
.ARB_multisample
) {
423 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glSampleCoverageARB");
427 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH( ctx
);
428 ctx
->Multisample
.SampleCoverageValue
= (GLfloat
) CLAMP(value
, 0.0, 1.0);
429 ctx
->Multisample
.SampleCoverageInvert
= invert
;
430 ctx
->NewState
|= _NEW_MULTISAMPLE
;