Implemented GL_ARB_texture_non_power_of_two (except for auto mipmap generation).
[mesa.git] / src / mesa / drivers / x11 / xm_dd.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 5.1
4 *
5 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 #include "glxheader.h"
27 #include "context.h"
28 #include "colormac.h"
29 #include "depth.h"
30 #include "drawpix.h"
31 #include "extensions.h"
32 #include "macros.h"
33 #include "imports.h"
34 #include "mtypes.h"
35 #include "state.h"
36 #include "texobj.h"
37 #include "teximage.h"
38 #include "texstore.h"
39 #include "texformat.h"
40 #include "xmesaP.h"
41 #include "array_cache/acache.h"
42 #include "swrast/s_context.h"
43 #include "swrast/swrast.h"
44 #include "swrast/s_alphabuf.h"
45 #include "swrast_setup/swrast_setup.h"
46 #include "tnl/tnl.h"
47 #include "tnl/t_context.h"
48 #include "tnl/t_pipeline.h"
49
50
51 /*
52 * Return the size (width, height) of the X window for the given GLframebuffer.
53 * Output: width - width of buffer in pixels.
54 * height - height of buffer in pixels.
55 */
56 static void
57 get_buffer_size( GLframebuffer *buffer, GLuint *width, GLuint *height )
58 {
59 /* We can do this cast because the first field in the XMesaBuffer
60 * struct is a GLframebuffer struct. If this weren't true, we'd
61 * need a pointer from the GLframebuffer to the XMesaBuffer.
62 */
63 const XMesaBuffer xmBuffer = (XMesaBuffer) buffer;
64 unsigned int winwidth, winheight;
65 #ifdef XFree86Server
66 /* XFree86 GLX renderer */
67 if (xmBuffer->frontbuffer->width > MAX_WIDTH ||
68 xmBuffer->frontbuffer->height > MAX_HEIGHT) {
69 winwidth = buffer->Width;
70 winheight = buffer->Height;
71 } else {
72 winwidth = xmBuffer->frontbuffer->width;
73 winheight = xmBuffer->frontbuffer->height;
74 }
75 #else
76 Window root;
77 int winx, winy;
78 unsigned int bw, d;
79
80 _glthread_LOCK_MUTEX(_xmesa_lock);
81 XSync(xmBuffer->xm_visual->display, 0); /* added for Chromium */
82 XGetGeometry( xmBuffer->xm_visual->display, xmBuffer->frontbuffer, &root,
83 &winx, &winy, &winwidth, &winheight, &bw, &d );
84 _glthread_UNLOCK_MUTEX(_xmesa_lock);
85 #endif
86
87 (void)kernel8; /* Muffle compiler */
88
89 *width = winwidth;
90 *height = winheight;
91 }
92
93
94 static void
95 finish( GLcontext *ctx )
96 {
97 #ifdef XFree86Server
98 /* NOT_NEEDED */
99 #else
100 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
101 if (xmesa) {
102 _glthread_LOCK_MUTEX(_xmesa_lock);
103 XSync( xmesa->display, False );
104 _glthread_UNLOCK_MUTEX(_xmesa_lock);
105 }
106 #endif
107 }
108
109
110 static void
111 flush( GLcontext *ctx )
112 {
113 #ifdef XFree86Server
114 /* NOT_NEEDED */
115 #else
116 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
117 if (xmesa) {
118 _glthread_LOCK_MUTEX(_xmesa_lock);
119 XFlush( xmesa->display );
120 _glthread_UNLOCK_MUTEX(_xmesa_lock);
121 }
122 #endif
123 }
124
125
126
127 /*
128 * This chooses the color buffer for reading and writing spans, points,
129 * lines, and triangles.
130 */
131 static void
132 set_buffer( GLcontext *ctx, GLframebuffer *buffer, GLuint bufferBit )
133 {
134 /* We can make this cast since the XMesaBuffer wraps GLframebuffer.
135 * GLframebuffer is the first member in a XMesaBuffer struct.
136 */
137 XMesaBuffer target = (XMesaBuffer) buffer;
138 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
139
140 /* This assignment tells the span/point/line/triangle functions
141 * which XMesaBuffer to use.
142 */
143 xmesa->xm_buffer = target;
144
145 /*
146 * Now determine front vs back color buffer.
147 */
148 if (bufferBit == FRONT_LEFT_BIT) {
149 target->buffer = target->frontbuffer;
150 }
151 else if (bufferBit == BACK_LEFT_BIT) {
152 ASSERT(target->db_state);
153 if (target->backpixmap) {
154 /* back buffer is a pixmape */
155 target->buffer = target->backpixmap; /* incompatible types? */
156 }
157 else if (target->backimage) {
158 /* back buffer is an XImage */
159 target->buffer = None;
160 }
161 else {
162 /* No back buffer!!!! Must be out of memory, use front buffer */
163 target->buffer = target->frontbuffer;
164 }
165 }
166 else {
167 _mesa_problem(ctx, "invalid buffer 0x%x in set_buffer() in xm_dd.c");
168 return;
169 }
170 xmesa_update_span_funcs(ctx);
171 }
172
173
174
175 static void
176 clear_index( GLcontext *ctx, GLuint index )
177 {
178 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
179 xmesa->clearpixel = (unsigned long) index;
180 XMesaSetForeground( xmesa->display, xmesa->xm_draw_buffer->cleargc,
181 (unsigned long) index );
182 }
183
184
185 static void
186 clear_color( GLcontext *ctx, const GLfloat color[4] )
187 {
188 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
189 CLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[0], color[0]);
190 CLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[1], color[1]);
191 CLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[2], color[2]);
192 CLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[3], color[3]);
193 xmesa->clearpixel = xmesa_color_to_pixel( xmesa,
194 xmesa->clearcolor[0],
195 xmesa->clearcolor[1],
196 xmesa->clearcolor[2],
197 xmesa->clearcolor[3],
198 xmesa->xm_visual->undithered_pf );
199 _glthread_LOCK_MUTEX(_xmesa_lock);
200 XMesaSetForeground( xmesa->display, xmesa->xm_draw_buffer->cleargc,
201 xmesa->clearpixel );
202 _glthread_UNLOCK_MUTEX(_xmesa_lock);
203 }
204
205
206
207 /* Set index mask ala glIndexMask */
208 static void
209 index_mask( GLcontext *ctx, GLuint mask )
210 {
211 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
212 if (xmesa->xm_draw_buffer->buffer != XIMAGE) {
213 unsigned long m;
214 if (mask==0xffffffff) {
215 m = ((unsigned long)~0L);
216 }
217 else {
218 m = (unsigned long) mask;
219 }
220 XMesaSetPlaneMask( xmesa->display, xmesa->xm_draw_buffer->cleargc, m );
221 XMesaSetPlaneMask( xmesa->display, xmesa->xm_draw_buffer->gc, m );
222 }
223 }
224
225
226 /* Implements glColorMask() */
227 static void
228 color_mask(GLcontext *ctx,
229 GLboolean rmask, GLboolean gmask, GLboolean bmask, GLboolean amask)
230 {
231 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
232 int xclass = GET_VISUAL_CLASS(xmesa->xm_visual);
233 (void) amask;
234
235 if (xclass == TrueColor || xclass == DirectColor) {
236 unsigned long m;
237 if (rmask && gmask && bmask) {
238 m = ((unsigned long)~0L);
239 }
240 else {
241 m = 0;
242 if (rmask) m |= GET_REDMASK(xmesa->xm_visual);
243 if (gmask) m |= GET_GREENMASK(xmesa->xm_visual);
244 if (bmask) m |= GET_BLUEMASK(xmesa->xm_visual);
245 }
246 XMesaSetPlaneMask( xmesa->display, xmesa->xm_draw_buffer->cleargc, m );
247 XMesaSetPlaneMask( xmesa->display, xmesa->xm_draw_buffer->gc, m );
248 }
249 }
250
251
252
253 /**********************************************************************/
254 /*** glClear implementations ***/
255 /**********************************************************************/
256
257
258 static void
259 clear_front_pixmap( GLcontext *ctx, GLboolean all,
260 GLint x, GLint y, GLint width, GLint height )
261 {
262 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
263 if (all) {
264 XMesaFillRectangle( xmesa->display, xmesa->xm_draw_buffer->frontbuffer,
265 xmesa->xm_draw_buffer->cleargc,
266 0, 0,
267 xmesa->xm_draw_buffer->width+1,
268 xmesa->xm_draw_buffer->height+1 );
269 }
270 else {
271 XMesaFillRectangle( xmesa->display, xmesa->xm_draw_buffer->frontbuffer,
272 xmesa->xm_draw_buffer->cleargc,
273 x, xmesa->xm_draw_buffer->height - y - height,
274 width, height );
275 }
276 }
277
278
279 static void
280 clear_back_pixmap( GLcontext *ctx, GLboolean all,
281 GLint x, GLint y, GLint width, GLint height )
282 {
283 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
284 if (all) {
285 XMesaFillRectangle( xmesa->display, xmesa->xm_draw_buffer->backpixmap,
286 xmesa->xm_draw_buffer->cleargc,
287 0, 0,
288 xmesa->xm_draw_buffer->width+1,
289 xmesa->xm_draw_buffer->height+1 );
290 }
291 else {
292 XMesaFillRectangle( xmesa->display, xmesa->xm_draw_buffer->backpixmap,
293 xmesa->xm_draw_buffer->cleargc,
294 x, xmesa->xm_draw_buffer->height - y - height,
295 width, height );
296 }
297 }
298
299
300 static void
301 clear_8bit_ximage( GLcontext *ctx, GLboolean all,
302 GLint x, GLint y, GLint width, GLint height )
303 {
304 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
305 if (all) {
306 size_t n = xmesa->xm_draw_buffer->backimage->bytes_per_line
307 * xmesa->xm_draw_buffer->backimage->height;
308 MEMSET( xmesa->xm_draw_buffer->backimage->data, xmesa->clearpixel, n );
309 }
310 else {
311 GLint i;
312 for (i=0;i<height;i++) {
313 GLubyte *ptr = PIXELADDR1( xmesa->xm_draw_buffer, x, y+i );
314 MEMSET( ptr, xmesa->clearpixel, width );
315 }
316 }
317 }
318
319
320 static void
321 clear_HPCR_ximage( GLcontext *ctx, GLboolean all,
322 GLint x, GLint y, GLint width, GLint height )
323 {
324 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
325 if (all) {
326 GLint i, c16 = (xmesa->xm_draw_buffer->backimage->bytes_per_line>>4)<<4;
327 GLubyte *ptr = (GLubyte *)xmesa->xm_draw_buffer->backimage->data;
328 for (i=0; i<xmesa->xm_draw_buffer->backimage->height; i++) {
329 GLint j;
330 GLubyte *sptr = xmesa->xm_visual->hpcr_clear_ximage_pattern[0];
331 if (i&1) {
332 sptr += 16;
333 }
334 for (j=0; j<c16; j+=16) {
335 ptr[0] = sptr[0];
336 ptr[1] = sptr[1];
337 ptr[2] = sptr[2];
338 ptr[3] = sptr[3];
339 ptr[4] = sptr[4];
340 ptr[5] = sptr[5];
341 ptr[6] = sptr[6];
342 ptr[7] = sptr[7];
343 ptr[8] = sptr[8];
344 ptr[9] = sptr[9];
345 ptr[10] = sptr[10];
346 ptr[11] = sptr[11];
347 ptr[12] = sptr[12];
348 ptr[13] = sptr[13];
349 ptr[14] = sptr[14];
350 ptr[15] = sptr[15];
351 ptr += 16;
352 }
353 for (; j<xmesa->xm_draw_buffer->backimage->bytes_per_line; j++) {
354 *ptr = sptr[j&15];
355 ptr++;
356 }
357 }
358 }
359 else {
360 GLint i;
361 for (i=y; i<y+height; i++) {
362 GLubyte *ptr = PIXELADDR1( xmesa->xm_draw_buffer, x, i );
363 int j;
364 GLubyte *sptr = xmesa->xm_visual->hpcr_clear_ximage_pattern[0];
365 if (i&1) {
366 sptr += 16;
367 }
368 for (j=x; j<x+width; j++) {
369 *ptr = sptr[j&15];
370 ptr++;
371 }
372 }
373 }
374 }
375
376
377 static void
378 clear_16bit_ximage( GLcontext *ctx, GLboolean all,
379 GLint x, GLint y, GLint width, GLint height )
380 {
381 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
382 register GLuint pixel = (GLuint) xmesa->clearpixel;
383 if (xmesa->swapbytes) {
384 pixel = ((pixel >> 8) & 0x00ff) | ((pixel << 8) & 0xff00);
385 }
386 if (all) {
387 register GLuint n;
388 register GLuint *ptr4 = (GLuint *) xmesa->xm_draw_buffer->backimage->data;
389 if ((pixel & 0xff) == ((pixel >> 8) & 0xff)) {
390 /* low and high bytes are equal so use memset() */
391 n = xmesa->xm_draw_buffer->backimage->bytes_per_line
392 * xmesa->xm_draw_buffer->height;
393 MEMSET( ptr4, pixel & 0xff, n );
394 }
395 else {
396 pixel = pixel | (pixel<<16);
397 n = xmesa->xm_draw_buffer->backimage->bytes_per_line
398 * xmesa->xm_draw_buffer->height / 4;
399 do {
400 *ptr4++ = pixel;
401 n--;
402 } while (n!=0);
403
404 if ((xmesa->xm_draw_buffer->backimage->bytes_per_line *
405 xmesa->xm_draw_buffer->height) & 0x2)
406 *(GLushort *)ptr4 = pixel & 0xffff;
407 }
408 }
409 else {
410 register int i, j;
411 for (j=0;j<height;j++) {
412 register GLushort *ptr2 = PIXELADDR2( xmesa->xm_draw_buffer, x, y+j );
413 for (i=0;i<width;i++) {
414 *ptr2++ = pixel;
415 }
416 }
417 }
418 }
419
420
421 /* Optimized code provided by Nozomi Ytow <noz@xfree86.org> */
422 static void
423 clear_24bit_ximage( GLcontext *ctx, GLboolean all,
424 GLint x, GLint y, GLint width, GLint height )
425 {
426 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
427 const GLubyte r = xmesa->clearcolor[0];
428 const GLubyte g = xmesa->clearcolor[1];
429 const GLubyte b = xmesa->clearcolor[2];
430 register GLuint clearPixel;
431 if (xmesa->swapbytes) {
432 clearPixel = (b << 16) | (g << 8) | r;
433 }
434 else {
435 clearPixel = (r << 16) | (g << 8) | b;
436 }
437
438 if (all) {
439 if (r==g && g==b) {
440 /* same value for all three components (gray) */
441 const GLint w3 = xmesa->xm_draw_buffer->width * 3;
442 const GLint h = xmesa->xm_draw_buffer->height;
443 GLint i;
444 for (i = 0; i < h; i++) {
445 bgr_t *ptr3 = PIXELADDR3(xmesa->xm_draw_buffer, 0, i);
446 MEMSET(ptr3, r, w3);
447 }
448 }
449 else {
450 /* the usual case */
451 const GLint w = xmesa->xm_draw_buffer->width;
452 const GLint h = xmesa->xm_draw_buffer->height;
453 GLint i, j;
454 for (i = 0; i < h; i++) {
455 bgr_t *ptr3 = PIXELADDR3(xmesa->xm_draw_buffer, 0, i);
456 for (j = 0; j < w; j++) {
457 ptr3->r = r;
458 ptr3->g = g;
459 ptr3->b = b;
460 ptr3++;
461 }
462 }
463 #if 0 /* this code doesn't work for all window widths */
464 register GLuint *ptr4 = (GLuint *) ptr3;
465 register GLuint px;
466 GLuint pixel4[3];
467 register GLuint *p = pixel4;
468 pixel4[0] = clearPixel | (clearPixel << 24);
469 pixel4[1] = (clearPixel << 16) | (clearPixel >> 8);
470 pixel4[2] = (clearPixel << 8) | (clearPixel >> 16);
471 switch (3 & (int)(ptr3 - (bgr_t*) ptr4)){
472 case 0:
473 break;
474 case 1:
475 px = *ptr4 & 0x00ffffff;
476 px |= pixel4[0] & 0xff000000;
477 *ptr4++ = px;
478 px = *ptr4 & 0xffff0000;
479 px |= pixel4[2] & 0x0000ffff;
480 *ptr4 = px;
481 if (0 == --n)
482 break;
483 case 2:
484 px = *ptr4 & 0x0000fffff;
485 px |= pixel4[1] & 0xffff0000;
486 *ptr4++ = px;
487 px = *ptr4 & 0xffffff00;
488 px |= pixel4[2] & 0x000000ff;
489 *ptr4 = px;
490 if (0 == --n)
491 break;
492 case 3:
493 px = *ptr4 & 0x000000ff;
494 px |= pixel4[2] & 0xffffff00;
495 *ptr4++ = px;
496 --n;
497 break;
498 }
499 while (n > 3) {
500 p = pixel4;
501 *ptr4++ = *p++;
502 *ptr4++ = *p++;
503 *ptr4++ = *p++;
504 n -= 4;
505 }
506 switch (n) {
507 case 3:
508 p = pixel4;
509 *ptr4++ = *p++;
510 *ptr4++ = *p++;
511 px = *ptr4 & 0xffffff00;
512 px |= clearPixel & 0xff;
513 *ptr4 = px;
514 break;
515 case 2:
516 p = pixel4;
517 *ptr4++ = *p++;
518 px = *ptr4 & 0xffff0000;
519 px |= *p & 0xffff;
520 *ptr4 = px;
521 break;
522 case 1:
523 px = *ptr4 & 0xff000000;
524 px |= *p & 0xffffff;
525 *ptr4 = px;
526 break;
527 case 0:
528 break;
529 }
530 #endif
531 }
532 }
533 else {
534 /* only clear subrect of color buffer */
535 if (r==g && g==b) {
536 /* same value for all three components (gray) */
537 GLint j;
538 for (j=0;j<height;j++) {
539 bgr_t *ptr3 = PIXELADDR3( xmesa->xm_draw_buffer, x, y+j );
540 MEMSET(ptr3, r, 3 * width);
541 }
542 }
543 else {
544 /* non-gray clear color */
545 GLint i, j;
546 for (j = 0; j < height; j++) {
547 bgr_t *ptr3 = PIXELADDR3( xmesa->xm_draw_buffer, x, y+j );
548 for (i = 0; i < width; i++) {
549 ptr3->r = r;
550 ptr3->g = g;
551 ptr3->b = b;
552 ptr3++;
553 }
554 }
555 #if 0 /* this code might not always (seems ptr3 always == ptr4) */
556 GLint j;
557 GLuint pixel4[3];
558 pixel4[0] = clearPixel | (clearPixel << 24);
559 pixel4[1] = (clearPixel << 16) | (clearPixel >> 8);
560 pixel4[2] = (clearPixel << 8) | (clearPixel >> 16);
561 for (j=0;j<height;j++) {
562 bgr_t *ptr3 = PIXELADDR3( xmesa->xm_draw_buffer, x, y+j );
563 register GLuint *ptr4 = (GLuint *)ptr3;
564 register GLuint *p, px;
565 GLuint w = width;
566 switch (3 & (int)(ptr3 - (bgr_t*) ptr4)){
567 case 0:
568 break;
569 case 1:
570 px = *ptr4 & 0x00ffffff;
571 px |= pixel4[0] & 0xff000000;
572 *ptr4++ = px;
573 px = *ptr4 & 0xffff0000;
574 px |= pixel4[2] & 0x0000ffff;
575 *ptr4 = px;
576 if (0 == --w)
577 break;
578 case 2:
579 px = *ptr4 & 0x0000fffff;
580 px |= pixel4[1] & 0xffff0000;
581 *ptr4++ = px;
582 px = *ptr4 & 0xffffff00;
583 px |= pixel4[2] & 0x000000ff;
584 *ptr4 = px;
585 if (0 == --w)
586 break;
587 case 3:
588 px = *ptr4 & 0x000000ff;
589 px |= pixel4[2] & 0xffffff00;
590 *ptr4++ = px;
591 --w;
592 break;
593 }
594 while (w > 3){
595 p = pixel4;
596 *ptr4++ = *p++;
597 *ptr4++ = *p++;
598 *ptr4++ = *p++;
599 w -= 4;
600 }
601 switch (w) {
602 case 3:
603 p = pixel4;
604 *ptr4++ = *p++;
605 *ptr4++ = *p++;
606 px = *ptr4 & 0xffffff00;
607 px |= *p & 0xff;
608 *ptr4 = px;
609 break;
610 case 2:
611 p = pixel4;
612 *ptr4++ = *p++;
613 px = *ptr4 & 0xffff0000;
614 px |= *p & 0xffff;
615 *ptr4 = px;
616 break;
617 case 1:
618 px = *ptr4 & 0xff000000;
619 px |= pixel4[0] & 0xffffff;
620 *ptr4 = px;
621 break;
622 case 0:
623 break;
624 }
625 }
626 #endif
627 }
628 }
629 }
630
631
632 static void
633 clear_32bit_ximage( GLcontext *ctx, GLboolean all,
634 GLint x, GLint y, GLint width, GLint height )
635 {
636 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
637 register GLuint pixel = (GLuint) xmesa->clearpixel;
638 if (xmesa->swapbytes) {
639 pixel = ((pixel >> 24) & 0x000000ff)
640 | ((pixel >> 8) & 0x0000ff00)
641 | ((pixel << 8) & 0x00ff0000)
642 | ((pixel << 24) & 0xff000000);
643 }
644 if (all) {
645 register GLint n = xmesa->xm_draw_buffer->width * xmesa->xm_draw_buffer->height;
646 register GLuint *ptr4 = (GLuint *) xmesa->xm_draw_buffer->backimage->data;
647 if (pixel==0) {
648 MEMSET( ptr4, pixel, 4*n );
649 }
650 else {
651 do {
652 *ptr4++ = pixel;
653 n--;
654 } while (n!=0);
655 }
656 }
657 else {
658 register int i, j;
659 for (j=0;j<height;j++) {
660 register GLuint *ptr4 = PIXELADDR4( xmesa->xm_draw_buffer, x, y+j );
661 for (i=0;i<width;i++) {
662 *ptr4++ = pixel;
663 }
664 }
665 }
666 }
667
668
669 static void
670 clear_nbit_ximage( GLcontext *ctx, GLboolean all,
671 GLint x, GLint y, GLint width, GLint height )
672 {
673 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
674 XMesaImage *img = xmesa->xm_draw_buffer->backimage;
675 if (all) {
676 register int i, j;
677 width = xmesa->xm_draw_buffer->width;
678 height = xmesa->xm_draw_buffer->height;
679 for (j=0;j<height;j++) {
680 for (i=0;i<width;i++) {
681 XMesaPutPixel( img, i, j, xmesa->clearpixel );
682 }
683 }
684 }
685 else {
686 /* TODO: optimize this */
687 register int i, j;
688 y = FLIP(xmesa->xm_draw_buffer, y);
689 for (j=0;j<height;j++) {
690 for (i=0;i<width;i++) {
691 XMesaPutPixel( img, x+i, y-j, xmesa->clearpixel );
692 }
693 }
694 }
695 }
696
697
698
699 static void
700 clear_buffers( GLcontext *ctx, GLbitfield mask,
701 GLboolean all, GLint x, GLint y, GLint width, GLint height )
702 {
703 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
704 const GLuint *colorMask = (GLuint *) &ctx->Color.ColorMask;
705
706 if ((mask & (DD_FRONT_LEFT_BIT | DD_BACK_LEFT_BIT)) &&
707 xmesa->xm_draw_buffer->mesa_buffer.UseSoftwareAlphaBuffers &&
708 ctx->Color.ColorMask[ACOMP]) {
709 _swrast_clear_alpha_buffers(ctx);
710 }
711
712 /* we can't handle color or index masking */
713 if (*colorMask == 0xffffffff && ctx->Color.IndexMask == 0xffffffff) {
714 if (mask & DD_FRONT_LEFT_BIT) {
715 ASSERT(xmesa->xm_draw_buffer->front_clear_func);
716 (*xmesa->xm_draw_buffer->front_clear_func)( ctx, all, x, y, width, height );
717 mask &= ~DD_FRONT_LEFT_BIT;
718 }
719 if (mask & DD_BACK_LEFT_BIT) {
720 ASSERT(xmesa->xm_draw_buffer->back_clear_func);
721 (*xmesa->xm_draw_buffer->back_clear_func)( ctx, all, x, y, width, height );
722 mask &= ~DD_BACK_LEFT_BIT;
723 }
724 }
725
726 if (mask)
727 _swrast_Clear( ctx, mask, all, x, y, width, height );
728 }
729
730
731 /*
732 * When we detect that the user has resized the window this function will
733 * get called. Here we'll reallocate the back buffer, depth buffer,
734 * stencil buffer etc. to match the new window size.
735 */
736 void
737 xmesa_resize_buffers( GLframebuffer *buffer )
738 {
739 int height = (int) buffer->Height;
740 /* We can do this cast because the first field in the XMesaBuffer
741 * struct is a GLframebuffer struct. If this weren't true, we'd
742 * need a pointer from the GLframebuffer to the XMesaBuffer.
743 */
744 XMesaBuffer xmBuffer = (XMesaBuffer) buffer;
745
746 xmBuffer->width = buffer->Width;
747 xmBuffer->height = buffer->Height;
748 xmesa_alloc_back_buffer( xmBuffer );
749
750 /* Needed by FLIP macro */
751 xmBuffer->bottom = height - 1;
752
753 if (xmBuffer->backimage) {
754 /* Needed by PIXELADDR1 macro */
755 xmBuffer->ximage_width1 = xmBuffer->backimage->bytes_per_line;
756 xmBuffer->ximage_origin1 = (GLubyte *) xmBuffer->backimage->data
757 + xmBuffer->ximage_width1 * (height-1);
758
759 /* Needed by PIXELADDR2 macro */
760 xmBuffer->ximage_width2 = xmBuffer->backimage->bytes_per_line / 2;
761 xmBuffer->ximage_origin2 = (GLushort *) xmBuffer->backimage->data
762 + xmBuffer->ximage_width2 * (height-1);
763
764 /* Needed by PIXELADDR3 macro */
765 xmBuffer->ximage_width3 = xmBuffer->backimage->bytes_per_line;
766 xmBuffer->ximage_origin3 = (GLubyte *) xmBuffer->backimage->data
767 + xmBuffer->ximage_width3 * (height-1);
768
769 /* Needed by PIXELADDR4 macro */
770 xmBuffer->ximage_width4 = xmBuffer->backimage->width;
771 xmBuffer->ximage_origin4 = (GLuint *) xmBuffer->backimage->data
772 + xmBuffer->ximage_width4 * (height-1);
773 }
774
775 _swrast_alloc_buffers( buffer );
776 }
777
778 #if 0
779 /*
780 * This function implements glDrawPixels() with an XPutImage call when
781 * drawing to the front buffer (X Window drawable).
782 * The image format must be GL_BGRA to match the PF_8R8G8B pixel format.
783 * XXX top/bottom edge clipping is broken!
784 */
785 static GLboolean
786 drawpixels_8R8G8B( GLcontext *ctx,
787 GLint x, GLint y, GLsizei width, GLsizei height,
788 GLenum format, GLenum type,
789 const struct gl_pixelstore_attrib *unpack,
790 const GLvoid *pixels )
791 {
792 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
793 XMesaDisplay *dpy = xmesa->xm_visual->display;
794 XMesaDrawable buffer = xmesa->xm_draw_buffer->buffer;
795 XMesaGC gc = xmesa->xm_draw_buffer->gc;
796 assert(dpy);
797 assert(buffer);
798 assert(gc);
799
800 /* XXX also check for pixel scale/bias/lookup/zooming! */
801 if (format == GL_BGRA && type == GL_UNSIGNED_BYTE) {
802 int dstX = x;
803 int dstY = y;
804 int w = width;
805 int h = height;
806 int srcX = unpack->SkipPixels;
807 int srcY = unpack->SkipRows;
808 if (_mesa_clip_pixelrect(ctx, &dstX, &dstY, &w, &h, &srcX, &srcY)) {
809 XMesaImage ximage;
810 MEMSET(&ximage, 0, sizeof(XMesaImage));
811 ximage.width = width;
812 ximage.height = height;
813 ximage.format = ZPixmap;
814 ximage.data = (char *) pixels + (height - 1) * width * 4;
815 ximage.byte_order = LSBFirst;
816 ximage.bitmap_unit = 32;
817 ximage.bitmap_bit_order = LSBFirst;
818 ximage.bitmap_pad = 32;
819 ximage.depth = 24;
820 ximage.bytes_per_line = -width * 4;
821 ximage.bits_per_pixel = 32;
822 ximage.red_mask = 0xff0000;
823 ximage.green_mask = 0x00ff00;
824 ximage.blue_mask = 0x0000ff;
825 dstY = FLIP(xmesa->xm_draw_buffer,dstY) - height + 1;
826 XPutImage(dpy, buffer, gc, &ximage, srcX, srcY, dstX, dstY, w, h);
827 return GL_TRUE;
828 }
829 }
830 return GL_FALSE;
831 }
832 #endif
833
834
835
836 static const GLubyte *
837 get_string( GLcontext *ctx, GLenum name )
838 {
839 (void) ctx;
840 switch (name) {
841 case GL_RENDERER:
842 #ifdef XFree86Server
843 return (const GLubyte *) "Mesa GLX Indirect";
844 #else
845 return (const GLubyte *) "Mesa X11";
846 #endif
847 case GL_VENDOR:
848 #ifdef XFree86Server
849 return (const GLubyte *) "Mesa project: www.mesa3d.org";
850 #else
851 return NULL;
852 #endif
853 default:
854 return NULL;
855 }
856 }
857
858
859 static void
860 enable( GLcontext *ctx, GLenum pname, GLboolean state )
861 {
862 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
863
864 switch (pname) {
865 case GL_DITHER:
866 if (state)
867 xmesa->pixelformat = xmesa->xm_visual->dithered_pf;
868 else
869 xmesa->pixelformat = xmesa->xm_visual->undithered_pf;
870 break;
871 default:
872 ; /* silence compiler warning */
873 }
874 }
875
876
877 void xmesa_update_state( GLcontext *ctx, GLuint new_state )
878 {
879 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
880
881 /* Propogate statechange information to swrast and swrast_setup
882 * modules. The X11 driver has no internal GL-dependent state.
883 */
884 _swrast_InvalidateState( ctx, new_state );
885 _ac_InvalidateState( ctx, new_state );
886 _tnl_InvalidateState( ctx, new_state );
887 _swsetup_InvalidateState( ctx, new_state );
888
889
890 /* setup pointers to front and back buffer clear functions */
891 xmesa->xm_draw_buffer->front_clear_func = clear_front_pixmap;
892 if (xmesa->xm_draw_buffer->backpixmap != XIMAGE) {
893 xmesa->xm_draw_buffer->back_clear_func = clear_back_pixmap;
894 }
895 else if (sizeof(GLushort)!=2 || sizeof(GLuint)!=4) {
896 xmesa->xm_draw_buffer->back_clear_func = clear_nbit_ximage;
897 }
898 else switch (xmesa->xm_visual->BitsPerPixel) {
899 case 8:
900 if (xmesa->xm_visual->hpcr_clear_flag) {
901 xmesa->xm_draw_buffer->back_clear_func = clear_HPCR_ximage;
902 }
903 else {
904 xmesa->xm_draw_buffer->back_clear_func = clear_8bit_ximage;
905 }
906 break;
907 case 16:
908 xmesa->xm_draw_buffer->back_clear_func = clear_16bit_ximage;
909 break;
910 case 24:
911 xmesa->xm_draw_buffer->back_clear_func = clear_24bit_ximage;
912 break;
913 case 32:
914 xmesa->xm_draw_buffer->back_clear_func = clear_32bit_ximage;
915 break;
916 default:
917 xmesa->xm_draw_buffer->back_clear_func = clear_nbit_ximage;
918 break;
919 }
920
921 xmesa_update_span_funcs(ctx);
922 }
923
924
925
926 /**
927 * Called via ctx->Driver.TestProxyTeximage(). Normally, we'd just use
928 * the _mesa_test_proxy_teximage() fallback function, but we're going to
929 * special-case the 3D texture case to allow textures up to 512x512x32
930 * texels.
931 */
932 static GLboolean
933 test_proxy_teximage(GLcontext *ctx, GLenum target, GLint level,
934 GLint internalFormat, GLenum format, GLenum type,
935 GLint width, GLint height, GLint depth, GLint border)
936 {
937 if (target == GL_PROXY_TEXTURE_3D) {
938 /* special case for 3D textures */
939 if (width * height * depth > 512 * 512 * 64 ||
940 width < 2 * border ||
941 (!ctx->Extensions.ARB_texture_non_power_of_two &&
942 _mesa_bitcount(width - 2 * border) != 1) ||
943 height < 2 * border ||
944 (!ctx->Extensions.ARB_texture_non_power_of_two &&
945 _mesa_bitcount(height - 2 * border) != 1) ||
946 depth < 2 * border ||
947 (!ctx->Extensions.ARB_texture_non_power_of_two &&
948 _mesa_bitcount(depth - 2 * border) != 1)) {
949 /* Bad size, or too many texels */
950 return GL_FALSE;
951 }
952 return GL_TRUE;
953 }
954 else {
955 /* use the fallback routine for 1D, 2D, cube and rect targets */
956 return _mesa_test_proxy_teximage(ctx, target, level, internalFormat,
957 format, type, width, height, depth,
958 border);
959 }
960 }
961
962
963
964
965 /* Setup pointers and other driver state that is constant for the life
966 * of a context.
967 */
968 void xmesa_init_pointers( GLcontext *ctx )
969 {
970 TNLcontext *tnl;
971 struct swrast_device_driver *dd = _swrast_GetDeviceDriverReference( ctx );
972
973 ctx->Driver.GetString = get_string;
974 ctx->Driver.GetBufferSize = get_buffer_size;
975 ctx->Driver.Flush = flush;
976 ctx->Driver.Finish = finish;
977
978 /* Software rasterizer pixel paths:
979 */
980 ctx->Driver.Accum = _swrast_Accum;
981 ctx->Driver.Bitmap = _swrast_Bitmap;
982 ctx->Driver.Clear = clear_buffers;
983 ctx->Driver.ResizeBuffers = xmesa_resize_buffers;
984 ctx->Driver.CopyPixels = _swrast_CopyPixels;
985 ctx->Driver.DrawPixels = _swrast_DrawPixels;
986 ctx->Driver.ReadPixels = _swrast_ReadPixels;
987 ctx->Driver.DrawBuffer = _swrast_DrawBuffer;
988
989 /* Software texture functions:
990 */
991 ctx->Driver.ChooseTextureFormat = _mesa_choose_tex_format;
992 ctx->Driver.TexImage1D = _mesa_store_teximage1d;
993 ctx->Driver.TexImage2D = _mesa_store_teximage2d;
994 ctx->Driver.TexImage3D = _mesa_store_teximage3d;
995 ctx->Driver.TexSubImage1D = _mesa_store_texsubimage1d;
996 ctx->Driver.TexSubImage2D = _mesa_store_texsubimage2d;
997 ctx->Driver.TexSubImage3D = _mesa_store_texsubimage3d;
998 ctx->Driver.TestProxyTexImage = test_proxy_teximage;
999
1000 ctx->Driver.CopyTexImage1D = _swrast_copy_teximage1d;
1001 ctx->Driver.CopyTexImage2D = _swrast_copy_teximage2d;
1002 ctx->Driver.CopyTexSubImage1D = _swrast_copy_texsubimage1d;
1003 ctx->Driver.CopyTexSubImage2D = _swrast_copy_texsubimage2d;
1004 ctx->Driver.CopyTexSubImage3D = _swrast_copy_texsubimage3d;
1005
1006 ctx->Driver.CompressedTexImage1D = _mesa_store_compressed_teximage1d;
1007 ctx->Driver.CompressedTexImage2D = _mesa_store_compressed_teximage2d;
1008 ctx->Driver.CompressedTexImage3D = _mesa_store_compressed_teximage3d;
1009 ctx->Driver.CompressedTexSubImage1D = _mesa_store_compressed_texsubimage1d;
1010 ctx->Driver.CompressedTexSubImage2D = _mesa_store_compressed_texsubimage2d;
1011 ctx->Driver.CompressedTexSubImage3D = _mesa_store_compressed_texsubimage3d;
1012
1013 /* Swrast hooks for imaging extensions:
1014 */
1015 ctx->Driver.CopyColorTable = _swrast_CopyColorTable;
1016 ctx->Driver.CopyColorSubTable = _swrast_CopyColorSubTable;
1017 ctx->Driver.CopyConvolutionFilter1D = _swrast_CopyConvolutionFilter1D;
1018 ctx->Driver.CopyConvolutionFilter2D = _swrast_CopyConvolutionFilter2D;
1019
1020
1021 /* Statechange callbacks:
1022 */
1023 ctx->Driver.ClearIndex = clear_index;
1024 ctx->Driver.ClearColor = clear_color;
1025 ctx->Driver.IndexMask = index_mask;
1026 ctx->Driver.ColorMask = color_mask;
1027 ctx->Driver.Enable = enable;
1028
1029
1030 /* Initialize the TNL driver interface:
1031 */
1032 tnl = TNL_CONTEXT(ctx);
1033 tnl->Driver.RunPipeline = _tnl_run_pipeline;
1034
1035 dd->SetBuffer = set_buffer;
1036
1037 /* Install swsetup for tnl->Driver.Render.*:
1038 */
1039 _swsetup_Wakeup(ctx);
1040
1041 (void) DitherValues; /* silenced unused var warning */
1042 }
1043
1044
1045
1046
1047
1048 #define XMESA_NEW_POINT (_NEW_POINT | \
1049 _NEW_RENDERMODE | \
1050 _SWRAST_NEW_RASTERMASK)
1051
1052 #define XMESA_NEW_LINE (_NEW_LINE | \
1053 _NEW_TEXTURE | \
1054 _NEW_LIGHT | \
1055 _NEW_DEPTH | \
1056 _NEW_RENDERMODE | \
1057 _SWRAST_NEW_RASTERMASK)
1058
1059 #define XMESA_NEW_TRIANGLE (_NEW_POLYGON | \
1060 _NEW_TEXTURE | \
1061 _NEW_LIGHT | \
1062 _NEW_DEPTH | \
1063 _NEW_RENDERMODE | \
1064 _SWRAST_NEW_RASTERMASK)
1065
1066
1067 /* Extend the software rasterizer with our line/point/triangle
1068 * functions.
1069 */
1070 void xmesa_register_swrast_functions( GLcontext *ctx )
1071 {
1072 SWcontext *swrast = SWRAST_CONTEXT( ctx );
1073
1074 swrast->choose_point = xmesa_choose_point;
1075 swrast->choose_line = xmesa_choose_line;
1076 swrast->choose_triangle = xmesa_choose_triangle;
1077
1078 swrast->invalidate_point |= XMESA_NEW_POINT;
1079 swrast->invalidate_line |= XMESA_NEW_LINE;
1080 swrast->invalidate_triangle |= XMESA_NEW_TRIANGLE;
1081 }