7db265dff1b57cb6c26d27426064e29a4078b25a
[mesa.git] / src / mesa / main / buffers.c
1 /* $Id: buffers.c,v 1.13 2000/10/17 00:42:02 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999-2000 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 "alphabuf.h"
34 #include "buffers.h"
35 #include "context.h"
36 #include "depth.h"
37 #include "enums.h"
38 #include "macros.h"
39 #include "masking.h"
40 #include "mem.h"
41 #include "stencil.h"
42 #include "state.h"
43 #include "types.h"
44 #endif
45
46
47
48 void
49 _mesa_ClearIndex( GLfloat c )
50 {
51 GET_CURRENT_CONTEXT(ctx);
52 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glClearIndex");
53 ctx->Color.ClearIndex = (GLuint) c;
54 if (!ctx->Visual.RGBAflag) {
55 /* it's OK to call glClearIndex in RGBA mode but it should be a NOP */
56 (*ctx->Driver.ClearIndex)( ctx, ctx->Color.ClearIndex );
57 }
58 }
59
60
61
62 void
63 _mesa_ClearColor( GLclampf red, GLclampf green,
64 GLclampf blue, GLclampf alpha )
65 {
66 GET_CURRENT_CONTEXT(ctx);
67 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glClearColor");
68
69 ctx->Color.ClearColor[0] = CLAMP( red, 0.0F, 1.0F );
70 ctx->Color.ClearColor[1] = CLAMP( green, 0.0F, 1.0F );
71 ctx->Color.ClearColor[2] = CLAMP( blue, 0.0F, 1.0F );
72 ctx->Color.ClearColor[3] = CLAMP( alpha, 0.0F, 1.0F );
73
74 if (ctx->Visual.RGBAflag) {
75 GLubyte r = (GLint) (ctx->Color.ClearColor[0] * 255.0F);
76 GLubyte g = (GLint) (ctx->Color.ClearColor[1] * 255.0F);
77 GLubyte b = (GLint) (ctx->Color.ClearColor[2] * 255.0F);
78 GLubyte a = (GLint) (ctx->Color.ClearColor[3] * 255.0F);
79 (*ctx->Driver.ClearColor)( ctx, r, g, b, a );
80 }
81 }
82
83
84
85
86 /*
87 * Clear the color buffer when glColorMask or glIndexMask is in effect.
88 */
89 static void
90 clear_color_buffer_with_masking( GLcontext *ctx )
91 {
92 const GLint x = ctx->DrawBuffer->Xmin;
93 const GLint y = ctx->DrawBuffer->Ymin;
94 const GLint height = ctx->DrawBuffer->Ymax - ctx->DrawBuffer->Ymin;
95 const GLint width = ctx->DrawBuffer->Xmax - ctx->DrawBuffer->Xmin;
96
97 if (ctx->Visual.RGBAflag) {
98 /* RGBA mode */
99 const GLubyte r = (GLint) (ctx->Color.ClearColor[0] * 255.0F);
100 const GLubyte g = (GLint) (ctx->Color.ClearColor[1] * 255.0F);
101 const GLubyte b = (GLint) (ctx->Color.ClearColor[2] * 255.0F);
102 const GLubyte a = (GLint) (ctx->Color.ClearColor[3] * 255.0F);
103 GLint i;
104 for (i = 0; i < height; i++) {
105 GLubyte rgba[MAX_WIDTH][4];
106 GLint j;
107 for (j=0; j<width; j++) {
108 rgba[j][RCOMP] = r;
109 rgba[j][GCOMP] = g;
110 rgba[j][BCOMP] = b;
111 rgba[j][ACOMP] = a;
112 }
113 _mesa_mask_rgba_span( ctx, width, x, y + i, rgba );
114 (*ctx->Driver.WriteRGBASpan)( ctx, width, x, y + i,
115 (CONST GLubyte (*)[4])rgba, NULL );
116 }
117 }
118 else {
119 /* Color index mode */
120 GLuint span[MAX_WIDTH];
121 GLubyte mask[MAX_WIDTH];
122 GLint i, j;
123 MEMSET( mask, 1, width );
124 for (i=0;i<height;i++) {
125 for (j=0;j<width;j++) {
126 span[j] = ctx->Color.ClearIndex;
127 }
128 _mesa_mask_index_span( ctx, width, x, y + i, span );
129 (*ctx->Driver.WriteCI32Span)( ctx, width, x, y + i, span, mask );
130 }
131 }
132 }
133
134
135
136 /*
137 * Clear a color buffer without index/channel masking.
138 */
139 static void
140 clear_color_buffer(GLcontext *ctx)
141 {
142 const GLint x = ctx->DrawBuffer->Xmin;
143 const GLint y = ctx->DrawBuffer->Ymin;
144 const GLint height = ctx->DrawBuffer->Ymax - ctx->DrawBuffer->Ymin;
145 const GLint width = ctx->DrawBuffer->Xmax - ctx->DrawBuffer->Xmin;
146 const GLuint colorMask = *((GLuint *) &ctx->Color.ColorMask);
147
148 if (ctx->Visual.RGBAflag) {
149 /* RGBA mode */
150 const GLubyte r = (GLint) (ctx->Color.ClearColor[0] * 255.0F);
151 const GLubyte g = (GLint) (ctx->Color.ClearColor[1] * 255.0F);
152 const GLubyte b = (GLint) (ctx->Color.ClearColor[2] * 255.0F);
153 const GLubyte a = (GLint) (ctx->Color.ClearColor[3] * 255.0F);
154 GLubyte span[MAX_WIDTH][4];
155 GLint i;
156
157 ASSERT(colorMask == 0xffffffff);
158
159 for (i = 0; i < width; i++) {
160 span[i][RCOMP] = r;
161 span[i][GCOMP] = g;
162 span[i][BCOMP] = b;
163 span[i][ACOMP] = a;
164 }
165 for (i = 0; i < height; i++) {
166 (*ctx->Driver.WriteRGBASpan)( ctx, width, x, y + i,
167 (CONST GLubyte (*)[4]) span, NULL );
168 }
169 }
170 else {
171 /* Color index mode */
172 ASSERT(ctx->Color.IndexMask == ~0);
173 if (ctx->Visual.IndexBits == 8) {
174 /* 8-bit clear */
175 GLubyte span[MAX_WIDTH];
176 GLint i;
177 MEMSET(span, ctx->Color.ClearIndex, width);
178 for (i = 0; i < height; i++) {
179 (*ctx->Driver.WriteCI8Span)( ctx, width, x, y + i, span, NULL );
180 }
181 }
182 else {
183 /* non 8-bit clear */
184 GLuint span[MAX_WIDTH];
185 GLint i;
186 for (i = 0; i < width; i++) {
187 span[i] = ctx->Color.ClearIndex;
188 }
189 for (i = 0; i < height; i++) {
190 (*ctx->Driver.WriteCI32Span)( ctx, width, x, y + i, span, NULL );
191 }
192 }
193 }
194 }
195
196
197
198 /*
199 * Clear the front/back/left/right color buffers.
200 * This function is usually only called if we need to clear the
201 * buffers with masking.
202 */
203 static void
204 clear_color_buffers(GLcontext *ctx)
205 {
206 const GLuint colorMask = *((GLuint *) &ctx->Color.ColorMask);
207 GLuint bufferBit;
208
209 /* loop over four possible dest color buffers */
210 for (bufferBit = 1; bufferBit <= 8; bufferBit = bufferBit << 1) {
211 if (bufferBit & ctx->Color.DrawDestMask) {
212 if (bufferBit == FRONT_LEFT_BIT) {
213 (void) (*ctx->Driver.SetDrawBuffer)( ctx, GL_FRONT_LEFT);
214 }
215 else if (bufferBit == FRONT_RIGHT_BIT) {
216 (void) (*ctx->Driver.SetDrawBuffer)( ctx, GL_FRONT_RIGHT);
217 }
218 else if (bufferBit == BACK_LEFT_BIT) {
219 (void) (*ctx->Driver.SetDrawBuffer)( ctx, GL_BACK_LEFT);
220 }
221 else {
222 (void) (*ctx->Driver.SetDrawBuffer)( ctx, GL_BACK_RIGHT);
223 }
224
225 if (colorMask != 0xffffffff) {
226 clear_color_buffer_with_masking(ctx);
227 }
228 else {
229 clear_color_buffer(ctx);
230 }
231 }
232 }
233
234 /* restore default dest buffer */
235 (void) (*ctx->Driver.SetDrawBuffer)( ctx, ctx->Color.DriverDrawBuffer );
236 }
237
238
239
240 void
241 _mesa_Clear( GLbitfield mask )
242 {
243 GET_CURRENT_CONTEXT(ctx);
244 #ifdef PROFILE
245 GLdouble t0 = gl_time();
246 #endif
247 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glClear");
248
249 if (MESA_VERBOSE & VERBOSE_API)
250 fprintf(stderr, "glClear 0x%x\n", mask);
251
252 if (ctx->NewState) {
253 gl_update_state( ctx );
254 }
255
256 if (ctx->RenderMode==GL_RENDER) {
257 const GLint x = ctx->DrawBuffer->Xmin;
258 const GLint y = ctx->DrawBuffer->Ymin;
259 const GLint height = ctx->DrawBuffer->Ymax - ctx->DrawBuffer->Ymin;
260 const GLint width = ctx->DrawBuffer->Xmax - ctx->DrawBuffer->Xmin;
261 GLbitfield ddMask;
262 GLbitfield newMask;
263
264 /* don't clear depth buffer if depth writing disabled */
265 if (!ctx->Depth.Mask)
266 CLEAR_BITS(mask, GL_DEPTH_BUFFER_BIT);
267
268 /* Build bitmask to send to driver Clear function */
269 ddMask = mask & (GL_DEPTH_BUFFER_BIT |
270 GL_STENCIL_BUFFER_BIT |
271 GL_ACCUM_BUFFER_BIT);
272 if (mask & GL_COLOR_BUFFER_BIT) {
273 ddMask |= ctx->Color.DrawDestMask;
274 }
275
276 ASSERT(ctx->Driver.Clear);
277 newMask = (*ctx->Driver.Clear)( ctx, ddMask, !ctx->Scissor.Enabled,
278 x, y, width, height );
279
280 #ifdef DEBUG
281 {
282 GLbitfield legalBits = DD_FRONT_LEFT_BIT |
283 DD_FRONT_RIGHT_BIT |
284 DD_BACK_LEFT_BIT |
285 DD_BACK_RIGHT_BIT |
286 DD_DEPTH_BIT |
287 DD_STENCIL_BIT |
288 DD_ACCUM_BIT;
289 assert((newMask & (~legalBits)) == 0);
290 }
291 #endif
292
293 RENDER_START(ctx);
294
295 /* do software clearing here */
296 if (newMask) {
297 if (newMask & ctx->Color.DrawDestMask) clear_color_buffers(ctx);
298 if (newMask & GL_DEPTH_BUFFER_BIT) _mesa_clear_depth_buffer(ctx);
299 if (newMask & GL_ACCUM_BUFFER_BIT) _mesa_clear_accum_buffer(ctx);
300 if (newMask & GL_STENCIL_BUFFER_BIT) _mesa_clear_stencil_buffer(ctx);
301 }
302
303 /* clear software-based alpha buffer(s) */
304 if ( (mask & GL_COLOR_BUFFER_BIT)
305 && ctx->DrawBuffer->UseSoftwareAlphaBuffers
306 && ctx->Color.ColorMask[ACOMP]) {
307 _mesa_clear_alpha_buffers( ctx );
308 }
309
310 RENDER_FINISH(ctx);
311
312 #ifdef PROFILE
313 ctx->ClearTime += gl_time() - t0;
314 ctx->ClearCount++;
315 #endif
316 }
317 }
318
319
320 void
321 _mesa_DrawBuffer( GLenum mode )
322 {
323 GET_CURRENT_CONTEXT(ctx);
324 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glDrawBuffer");
325
326 if (MESA_VERBOSE & VERBOSE_API)
327 fprintf(stderr, "glDrawBuffer %s\n", gl_lookup_enum_by_nr(mode));
328
329 switch (mode) {
330 case GL_AUX0:
331 case GL_AUX1:
332 case GL_AUX2:
333 case GL_AUX3:
334 /* AUX buffers not implemented in Mesa at this time */
335 gl_error( ctx, GL_INVALID_OPERATION, "glDrawBuffer" );
336 return;
337 case GL_RIGHT:
338 if (!ctx->Visual.StereoFlag) {
339 gl_error( ctx, GL_INVALID_OPERATION, "glDrawBuffer" );
340 return;}
341 if (ctx->Visual.DBflag)
342 ctx->Color.DrawDestMask = FRONT_RIGHT_BIT | BACK_RIGHT_BIT;
343 else
344 ctx->Color.DrawDestMask = FRONT_RIGHT_BIT;
345 break;
346 case GL_FRONT_RIGHT:
347 if (!ctx->Visual.StereoFlag) {
348 gl_error( ctx, GL_INVALID_OPERATION, "glDrawBuffer" );
349 return;
350 }
351 ctx->Color.DrawDestMask = FRONT_RIGHT_BIT;
352 break;
353 case GL_BACK_RIGHT:
354 if (!ctx->Visual.StereoFlag) {
355 gl_error( ctx, GL_INVALID_OPERATION, "glDrawBuffer" );
356 return;
357 }
358 if (!ctx->Visual.DBflag) {
359 gl_error( ctx, GL_INVALID_OPERATION, "glDrawBuffer" );
360 return;
361 }
362 ctx->Color.DrawDestMask = BACK_RIGHT_BIT;
363 break;
364 case GL_BACK_LEFT:
365 if (!ctx->Visual.DBflag) {
366 gl_error( ctx, GL_INVALID_OPERATION, "glDrawBuffer" );
367 return;
368 }
369 ctx->Color.DrawDestMask = BACK_LEFT_BIT;
370 break;
371 case GL_FRONT_AND_BACK:
372 if (!ctx->Visual.DBflag) {
373 gl_error( ctx, GL_INVALID_OPERATION, "glDrawBuffer" );
374 return;
375 }
376 if (ctx->Visual.StereoFlag)
377 ctx->Color.DrawDestMask = FRONT_LEFT_BIT | BACK_LEFT_BIT
378 | FRONT_RIGHT_BIT | BACK_RIGHT_BIT;
379 else
380 ctx->Color.DrawDestMask = FRONT_LEFT_BIT | BACK_LEFT_BIT;
381 break;
382 case GL_BACK:
383 if (!ctx->Visual.DBflag) {
384 gl_error( ctx, GL_INVALID_OPERATION, "glDrawBuffer" );
385 return;
386 }
387 if (ctx->Visual.StereoFlag)
388 ctx->Color.DrawDestMask = BACK_LEFT_BIT | BACK_RIGHT_BIT;
389 else
390 ctx->Color.DrawDestMask = BACK_LEFT_BIT;
391 break;
392 case GL_LEFT:
393 /* never an error */
394 if (ctx->Visual.DBflag)
395 ctx->Color.DrawDestMask = FRONT_LEFT_BIT | BACK_LEFT_BIT;
396 else
397 ctx->Color.DrawDestMask = FRONT_LEFT_BIT;
398 break;
399 case GL_FRONT_LEFT:
400 /* never an error */
401 ctx->Color.DrawDestMask = FRONT_LEFT_BIT;
402 break;
403 case GL_FRONT:
404 /* never an error */
405 if (ctx->Visual.StereoFlag)
406 ctx->Color.DrawDestMask = FRONT_LEFT_BIT | FRONT_RIGHT_BIT;
407 else
408 ctx->Color.DrawDestMask = FRONT_LEFT_BIT;
409 break;
410 case GL_NONE:
411 /* never an error */
412 ctx->Color.DrawDestMask = 0;
413 break;
414 default:
415 gl_error( ctx, GL_INVALID_ENUM, "glDrawBuffer" );
416 return;
417 }
418
419 /*
420 * Make the dest buffer mode more precise if possible
421 */
422 if (mode == GL_LEFT && !ctx->Visual.DBflag)
423 ctx->Color.DriverDrawBuffer = GL_FRONT_LEFT;
424 else if (mode == GL_RIGHT && !ctx->Visual.DBflag)
425 ctx->Color.DriverDrawBuffer = GL_FRONT_RIGHT;
426 else if (mode == GL_FRONT && !ctx->Visual.StereoFlag)
427 ctx->Color.DriverDrawBuffer = GL_FRONT_LEFT;
428 else if (mode == GL_BACK && !ctx->Visual.StereoFlag)
429 ctx->Color.DriverDrawBuffer = GL_BACK_LEFT;
430 else
431 ctx->Color.DriverDrawBuffer = mode;
432
433 /*
434 * Set current alpha buffer pointer
435 */
436 if (ctx->DrawBuffer->UseSoftwareAlphaBuffers) {
437 if (ctx->Color.DriverDrawBuffer == GL_FRONT_LEFT)
438 ctx->DrawBuffer->Alpha = ctx->DrawBuffer->FrontLeftAlpha;
439 else if (ctx->Color.DriverDrawBuffer == GL_BACK_LEFT)
440 ctx->DrawBuffer->Alpha = ctx->DrawBuffer->BackLeftAlpha;
441 else if (ctx->Color.DriverDrawBuffer == GL_FRONT_RIGHT)
442 ctx->DrawBuffer->Alpha = ctx->DrawBuffer->FrontRightAlpha;
443 else if (ctx->Color.DriverDrawBuffer == GL_BACK_RIGHT)
444 ctx->DrawBuffer->Alpha = ctx->DrawBuffer->BackRightAlpha;
445 }
446
447 /*
448 * If we get here there can't have been an error.
449 * Now see if device driver can implement the drawing to the target
450 * buffer(s). The driver may not be able to do GL_FRONT_AND_BACK mode
451 * for example. We'll take care of that in the core code by looping
452 * over the individual buffers.
453 */
454 ASSERT(ctx->Driver.SetDrawBuffer);
455 if ( (*ctx->Driver.SetDrawBuffer)(ctx, ctx->Color.DriverDrawBuffer) ) {
456 /* All OK, the driver will do all buffer writes */
457 ctx->Color.MultiDrawBuffer = GL_FALSE;
458 }
459 else {
460 /* We'll have to loop over the multiple draw buffer targets */
461 ctx->Color.MultiDrawBuffer = GL_TRUE;
462 /* Set drawing buffer to front for now */
463 (void) (*ctx->Driver.SetDrawBuffer)(ctx, GL_FRONT_LEFT);
464 }
465
466 ctx->Color.DrawBuffer = mode;
467 ctx->NewState |= NEW_RASTER_OPS;
468 }
469
470
471
472 void
473 _mesa_ReadBuffer( GLenum mode )
474 {
475 GET_CURRENT_CONTEXT(ctx);
476 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glReadBuffer");
477
478 if (MESA_VERBOSE & VERBOSE_API)
479 fprintf(stderr, "glReadBuffer %s\n", gl_lookup_enum_by_nr(mode));
480
481 switch (mode) {
482 case GL_AUX0:
483 case GL_AUX1:
484 case GL_AUX2:
485 case GL_AUX3:
486 /* AUX buffers not implemented in Mesa at this time */
487 gl_error( ctx, GL_INVALID_OPERATION, "glReadBuffer" );
488 return;
489 case GL_LEFT:
490 case GL_FRONT:
491 case GL_FRONT_LEFT:
492 /* Front-Left buffer, always exists */
493 ctx->Pixel.DriverReadBuffer = GL_FRONT_LEFT;
494 break;
495 case GL_BACK:
496 case GL_BACK_LEFT:
497 /* Back-Left buffer, requires double buffering */
498 if (!ctx->Visual.DBflag) {
499 gl_error( ctx, GL_INVALID_OPERATION, "glReadBuffer" );
500 return;
501 }
502 ctx->Pixel.DriverReadBuffer = GL_BACK_LEFT;
503 break;
504 case GL_FRONT_RIGHT:
505 case GL_RIGHT:
506 if (!ctx->Visual.StereoFlag) {
507 gl_error( ctx, GL_INVALID_OPERATION, "glReadBuffer" );
508 return;
509 }
510 ctx->Pixel.DriverReadBuffer = GL_FRONT_RIGHT;
511 break;
512 case GL_BACK_RIGHT:
513 if (!ctx->Visual.StereoFlag || !ctx->Visual.DBflag) {
514 gl_error( ctx, GL_INVALID_OPERATION, "glReadBuffer" );
515 return;
516 }
517 ctx->Pixel.DriverReadBuffer = GL_BACK_RIGHT;
518 break;
519 default:
520 gl_error( ctx, GL_INVALID_ENUM, "glReadBuffer" );
521 return;
522 }
523
524 ctx->Pixel.ReadBuffer = mode;
525 ctx->NewState |= NEW_RASTER_OPS;
526 }
527
528
529 /*
530 * GL_MESA_resize_buffers extension
531 */
532 void
533 _mesa_ResizeBuffersMESA( void )
534 {
535 GLcontext *ctx = _mesa_get_current_context();
536
537 GLuint buf_width, buf_height;
538
539 if (MESA_VERBOSE & VERBOSE_API)
540 fprintf(stderr, "glResizeBuffersMESA\n");
541
542 /* ask device driver for size of output buffer */
543 (*ctx->Driver.GetBufferSize)( ctx, &buf_width, &buf_height );
544
545 /* see if size of device driver's color buffer (window) has changed */
546 if (ctx->DrawBuffer->Width == (GLint) buf_width &&
547 ctx->DrawBuffer->Height == (GLint) buf_height)
548 return;
549
550 ctx->NewState |= NEW_RASTER_OPS; /* to update scissor / window bounds */
551
552 /* save buffer size */
553 ctx->DrawBuffer->Width = buf_width;
554 ctx->DrawBuffer->Height = buf_height;
555
556 /* Reallocate other buffers if needed. */
557 if (ctx->DrawBuffer->UseSoftwareDepthBuffer) {
558 _mesa_alloc_depth_buffer( ctx );
559 }
560 if (ctx->DrawBuffer->UseSoftwareStencilBuffer) {
561 _mesa_alloc_stencil_buffer( ctx );
562 }
563 if (ctx->DrawBuffer->UseSoftwareAccumBuffer) {
564 _mesa_alloc_accum_buffer( ctx );
565 }
566 if (ctx->DrawBuffer->UseSoftwareAlphaBuffers) {
567 _mesa_alloc_alpha_buffers( ctx );
568 }
569 }