Changed a number of context fields from GLchan to GLfloat (such as ClearColor).
[mesa.git] / src / mesa / main / buffers.c
1 /* $Id: buffers.c,v 1.38 2002/10/04 19:10:07 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 4.1
6 *
7 * Copyright (C) 1999-2002 Brian Paul All Rights Reserved.
8 *
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:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
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.
25 */
26
27
28 #ifdef PC_HEADER
29 #include "all.h"
30 #else
31 #include "glheader.h"
32 #include "accum.h"
33 #include "buffers.h"
34 #include "colormac.h"
35 #include "context.h"
36 #include "depth.h"
37 #include "enums.h"
38 #include "macros.h"
39 #include "mem.h"
40 #include "stencil.h"
41 #include "state.h"
42 #include "mtypes.h"
43 #endif
44
45
46
47 void
48 _mesa_ClearIndex( GLfloat c )
49 {
50 GET_CURRENT_CONTEXT(ctx);
51 ASSERT_OUTSIDE_BEGIN_END(ctx);
52
53 if (ctx->Color.ClearIndex == (GLuint) c)
54 return;
55
56 FLUSH_VERTICES(ctx, _NEW_COLOR);
57 ctx->Color.ClearIndex = (GLuint) c;
58
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 );
62 }
63 }
64
65
66
67 void
68 _mesa_ClearColor( GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha )
69 {
70 GLfloat tmp[4];
71 GET_CURRENT_CONTEXT(ctx);
72 ASSERT_OUTSIDE_BEGIN_END(ctx);
73
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);
78
79 if (TEST_EQ_4V(tmp, ctx->Color.ClearColor))
80 return; /* no change */
81
82 FLUSH_VERTICES(ctx, _NEW_COLOR);
83 COPY_4V(ctx->Color.ClearColor, tmp);
84
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);
88 }
89 }
90
91
92
93 void
94 _mesa_Clear( GLbitfield mask )
95 {
96 GET_CURRENT_CONTEXT(ctx);
97 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
98
99 if (MESA_VERBOSE & VERBOSE_API)
100 _mesa_debug(ctx, "glClear 0x%x\n", mask);
101
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)");
108 return;
109 }
110
111 if (ctx->NewState) {
112 _mesa_update_state( ctx ); /* update _Xmin, etc */
113 }
114
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;
120 GLbitfield ddMask;
121
122 /* don't clear depth buffer if depth writing disabled */
123 if (!ctx->Depth.Mask)
124 mask &= ~GL_DEPTH_BUFFER_BIT;
125
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;
132 }
133
134 ASSERT(ctx->Driver.Clear);
135 ctx->Driver.Clear( ctx, ddMask, (GLboolean) !ctx->Scissor.Enabled,
136 x, y, width, height );
137 }
138 }
139
140
141 void
142 _mesa_DrawBuffer( GLenum mode )
143 {
144 GET_CURRENT_CONTEXT(ctx);
145 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); /* too complex... */
146
147
148 if (MESA_VERBOSE & VERBOSE_API)
149 _mesa_debug(ctx, "glDrawBuffer %s\n", _mesa_lookup_enum_by_nr(mode));
150
151 /*
152 * Do error checking and compute the _DrawDestMask bitfield.
153 */
154 switch (mode) {
155 case GL_AUX0:
156 case GL_AUX1:
157 case GL_AUX2:
158 case GL_AUX3:
159 /* AUX buffers not implemented in Mesa at this time */
160 _mesa_error( ctx, GL_INVALID_OPERATION, "glDrawBuffer" );
161 return;
162 case GL_RIGHT:
163 if (!ctx->Visual.stereoMode) {
164 _mesa_error( ctx, GL_INVALID_OPERATION, "glDrawBuffer" );
165 return;}
166 if (ctx->Visual.doubleBufferMode)
167 ctx->Color._DrawDestMask = FRONT_RIGHT_BIT | BACK_RIGHT_BIT;
168 else
169 ctx->Color._DrawDestMask = FRONT_RIGHT_BIT;
170 break;
171 case GL_FRONT_RIGHT:
172 if (!ctx->Visual.stereoMode) {
173 _mesa_error( ctx, GL_INVALID_OPERATION, "glDrawBuffer" );
174 return;
175 }
176 ctx->Color._DrawDestMask = FRONT_RIGHT_BIT;
177 break;
178 case GL_BACK_RIGHT:
179 if (!ctx->Visual.stereoMode) {
180 _mesa_error( ctx, GL_INVALID_OPERATION, "glDrawBuffer" );
181 return;
182 }
183 if (!ctx->Visual.doubleBufferMode) {
184 _mesa_error( ctx, GL_INVALID_OPERATION, "glDrawBuffer" );
185 return;
186 }
187 ctx->Color._DrawDestMask = BACK_RIGHT_BIT;
188 break;
189 case GL_BACK_LEFT:
190 if (!ctx->Visual.doubleBufferMode) {
191 _mesa_error( ctx, GL_INVALID_OPERATION, "glDrawBuffer" );
192 return;
193 }
194 ctx->Color._DrawDestMask = BACK_LEFT_BIT;
195 break;
196 case GL_FRONT_AND_BACK:
197 if (!ctx->Visual.doubleBufferMode) {
198 _mesa_error( ctx, GL_INVALID_OPERATION, "glDrawBuffer" );
199 return;
200 }
201 if (ctx->Visual.stereoMode)
202 ctx->Color._DrawDestMask = FRONT_LEFT_BIT | BACK_LEFT_BIT
203 | FRONT_RIGHT_BIT | BACK_RIGHT_BIT;
204 else
205 ctx->Color._DrawDestMask = FRONT_LEFT_BIT | BACK_LEFT_BIT;
206 break;
207 case GL_BACK:
208 if (!ctx->Visual.doubleBufferMode) {
209 _mesa_error( ctx, GL_INVALID_OPERATION, "glDrawBuffer" );
210 return;
211 }
212 if (ctx->Visual.stereoMode)
213 ctx->Color._DrawDestMask = BACK_LEFT_BIT | BACK_RIGHT_BIT;
214 else
215 ctx->Color._DrawDestMask = BACK_LEFT_BIT;
216 break;
217 case GL_LEFT:
218 /* never an error */
219 if (ctx->Visual.doubleBufferMode)
220 ctx->Color._DrawDestMask = FRONT_LEFT_BIT | BACK_LEFT_BIT;
221 else
222 ctx->Color._DrawDestMask = FRONT_LEFT_BIT;
223 break;
224 case GL_FRONT_LEFT:
225 /* never an error */
226 ctx->Color._DrawDestMask = FRONT_LEFT_BIT;
227 break;
228 case GL_FRONT:
229 /* never an error */
230 if (ctx->Visual.stereoMode)
231 ctx->Color._DrawDestMask = FRONT_LEFT_BIT | FRONT_RIGHT_BIT;
232 else
233 ctx->Color._DrawDestMask = FRONT_LEFT_BIT;
234 break;
235 case GL_NONE:
236 /* never an error */
237 ctx->Color._DrawDestMask = 0;
238 break;
239 default:
240 _mesa_error( ctx, GL_INVALID_ENUM, "glDrawBuffer" );
241 return;
242 }
243
244 ctx->Color.DrawBuffer = mode;
245 ctx->NewState |= _NEW_COLOR;
246
247 /*
248 * Call device driver function.
249 */
250 if (ctx->Driver.DrawBuffer)
251 (*ctx->Driver.DrawBuffer)(ctx, mode);
252 }
253
254
255
256 void
257 _mesa_ReadBuffer( GLenum mode )
258 {
259 GET_CURRENT_CONTEXT(ctx);
260 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
261
262 if (MESA_VERBOSE & VERBOSE_API)
263 _mesa_debug(ctx, "glReadBuffer %s\n", _mesa_lookup_enum_by_nr(mode));
264
265 switch (mode) {
266 case GL_AUX0:
267 case GL_AUX1:
268 case GL_AUX2:
269 case GL_AUX3:
270 /* AUX buffers not implemented in Mesa at this time */
271 _mesa_error( ctx, GL_INVALID_OPERATION, "glReadBuffer" );
272 return;
273 case GL_LEFT:
274 case GL_FRONT:
275 case GL_FRONT_LEFT:
276 /* Front-Left buffer, always exists */
277 ctx->Pixel._DriverReadBuffer = GL_FRONT_LEFT;
278 break;
279 case GL_BACK:
280 case GL_BACK_LEFT:
281 /* Back-Left buffer, requires double buffering */
282 if (!ctx->Visual.doubleBufferMode) {
283 _mesa_error( ctx, GL_INVALID_OPERATION, "glReadBuffer" );
284 return;
285 }
286 ctx->Pixel._DriverReadBuffer = GL_BACK_LEFT;
287 break;
288 case GL_FRONT_RIGHT:
289 case GL_RIGHT:
290 if (!ctx->Visual.stereoMode) {
291 _mesa_error( ctx, GL_INVALID_OPERATION, "glReadBuffer" );
292 return;
293 }
294 ctx->Pixel._DriverReadBuffer = GL_FRONT_RIGHT;
295 break;
296 case GL_BACK_RIGHT:
297 if (!ctx->Visual.stereoMode || !ctx->Visual.doubleBufferMode) {
298 _mesa_error( ctx, GL_INVALID_OPERATION, "glReadBuffer" );
299 return;
300 }
301 ctx->Pixel._DriverReadBuffer = GL_BACK_RIGHT;
302 break;
303 default:
304 _mesa_error( ctx, GL_INVALID_ENUM, "glReadBuffer" );
305 return;
306 }
307
308 ctx->Pixel.ReadBuffer = mode;
309 ctx->NewState |= _NEW_PIXEL;
310
311 /*
312 * Call device driver function.
313 */
314 if (ctx->Driver.ReadBuffer)
315 (*ctx->Driver.ReadBuffer)(ctx, mode);
316 }
317
318
319 /*
320 * GL_MESA_resize_buffers extension
321 * When this function is called, we'll ask the window system how large
322 * the current window is. If it's not what we expect, we'll have to
323 * resize/reallocate the software accum/stencil/depth/alpha buffers.
324 */
325 void
326 _mesa_ResizeBuffersMESA( void )
327 {
328 GLcontext *ctx = _mesa_get_current_context();
329
330 if (MESA_VERBOSE & VERBOSE_API)
331 _mesa_debug(ctx, "glResizeBuffersMESA\n");
332
333 if (ctx) {
334 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH( ctx );
335
336 if (ctx->DrawBuffer) {
337 GLuint buf_width, buf_height;
338 GLframebuffer *buffer = ctx->DrawBuffer;
339
340 /* ask device driver for size of output buffer */
341 (*ctx->Driver.GetBufferSize)( buffer, &buf_width, &buf_height );
342
343 /* see if size of device driver's color buffer (window) has changed */
344 if (buffer->Width == buf_width && buffer->Height == buf_height)
345 return; /* size is as expected */
346
347 buffer->Width = buf_width;
348 buffer->Height = buf_height;
349
350 ctx->Driver.ResizeBuffers( buffer );
351 }
352
353 if (ctx->ReadBuffer && ctx->ReadBuffer != ctx->DrawBuffer) {
354 GLuint buf_width, buf_height;
355 GLframebuffer *buffer = ctx->DrawBuffer;
356
357 /* ask device driver for size of output buffer */
358 (*ctx->Driver.GetBufferSize)( buffer, &buf_width, &buf_height );
359
360 /* see if size of device driver's color buffer (window) has changed */
361 if (buffer->Width == buf_width && buffer->Height == buf_height)
362 return; /* size is as expected */
363
364 buffer->Width = buf_width;
365 buffer->Height = buf_height;
366
367 ctx->Driver.ResizeBuffers( buffer );
368 }
369
370 ctx->NewState |= _NEW_BUFFERS; /* to update scissor / window bounds */
371 }
372 }
373
374
375 void
376 _mesa_Scissor( GLint x, GLint y, GLsizei width, GLsizei height )
377 {
378 GET_CURRENT_CONTEXT(ctx);
379 ASSERT_OUTSIDE_BEGIN_END(ctx);
380
381 if (width < 0 || height < 0) {
382 _mesa_error( ctx, GL_INVALID_VALUE, "glScissor" );
383 return;
384 }
385
386 if (MESA_VERBOSE & VERBOSE_API)
387 _mesa_debug(ctx, "glScissor %d %d %d %d\n", x, y, width, height);
388
389 if (x == ctx->Scissor.X &&
390 y == ctx->Scissor.Y &&
391 width == ctx->Scissor.Width &&
392 height == ctx->Scissor.Height)
393 return;
394
395 FLUSH_VERTICES(ctx, _NEW_SCISSOR);
396 ctx->Scissor.X = x;
397 ctx->Scissor.Y = y;
398 ctx->Scissor.Width = width;
399 ctx->Scissor.Height = height;
400
401 if (ctx->Driver.Scissor)
402 ctx->Driver.Scissor( ctx, x, y, width, height );
403 }
404
405
406 /*
407 * XXX move somewhere else someday?
408 */
409 void
410 _mesa_SampleCoverageARB(GLclampf value, GLboolean invert)
411 {
412 GLcontext *ctx = _mesa_get_current_context();
413
414 if (!ctx->Extensions.ARB_multisample) {
415 _mesa_error(ctx, GL_INVALID_OPERATION, "glSampleCoverageARB");
416 return;
417 }
418
419 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH( ctx );
420 ctx->Multisample.SampleCoverageValue = (GLfloat) CLAMP(value, 0.0, 1.0);
421 ctx->Multisample.SampleCoverageInvert = invert;
422 ctx->NewState |= _NEW_MULTISAMPLE;
423 }
424