Implementation of GL_EXT_pixel_buffer_object extension.
[mesa.git] / src / mesa / drivers / x11 / xm_dd.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.1
4 *
5 * Copyright (C) 1999-2004 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 "bufferobj.h"
28 #include "context.h"
29 #include "colormac.h"
30 #include "depth.h"
31 #include "drawpix.h"
32 #include "extensions.h"
33 #include "macros.h"
34 #include "imports.h"
35 #include "mtypes.h"
36 #include "state.h"
37 #include "texobj.h"
38 #include "teximage.h"
39 #include "texstore.h"
40 #include "texformat.h"
41 #include "xmesaP.h"
42 #include "array_cache/acache.h"
43 #include "swrast/swrast.h"
44 #include "swrast/s_context.h"
45 #include "swrast/s_drawpix.h"
46 #include "swrast/s_alphabuf.h"
47 #include "swrast_setup/swrast_setup.h"
48 #include "tnl/tnl.h"
49 #include "tnl/t_context.h"
50
51 #ifdef XFree86Server
52 #include <GL/glxtokens.h>
53 #endif
54
55
56
57 /*
58 * Dithering kernels and lookup tables.
59 */
60
61 const int xmesa_kernel8[DITH_DY * DITH_DX] = {
62 0 * MAXC, 8 * MAXC, 2 * MAXC, 10 * MAXC,
63 12 * MAXC, 4 * MAXC, 14 * MAXC, 6 * MAXC,
64 3 * MAXC, 11 * MAXC, 1 * MAXC, 9 * MAXC,
65 15 * MAXC, 7 * MAXC, 13 * MAXC, 5 * MAXC,
66 };
67
68 const short xmesa_HPCR_DRGB[3][2][16] = {
69 {
70 { 16, -4, 1,-11, 14, -6, 3, -9, 15, -5, 2,-10, 13, -7, 4, -8},
71 {-15, 5, 0, 12,-13, 7, -2, 10,-14, 6, -1, 11,-12, 8, -3, 9}
72 },
73 {
74 {-11, 15, -7, 3, -8, 14, -4, 2,-10, 16, -6, 4, -9, 13, -5, 1},
75 { 12,-14, 8, -2, 9,-13, 5, -1, 11,-15, 7, -3, 10,-12, 6, 0}
76 },
77 {
78 { 6,-18, 26,-14, 2,-22, 30,-10, 8,-16, 28,-12, 4,-20, 32, -8},
79 { -4, 20,-24, 16, 0, 24,-28, 12, -6, 18,-26, 14, -2, 22,-30, 10}
80 }
81 };
82
83 const int xmesa_kernel1[16] = {
84 0*47, 9*47, 4*47, 12*47, /* 47 = (255*3)/16 */
85 6*47, 2*47, 14*47, 8*47,
86 10*47, 1*47, 5*47, 11*47,
87 7*47, 13*47, 3*47, 15*47
88 };
89
90
91 /*
92 * Return the size (width, height) of the X window for the given GLframebuffer.
93 * Output: width - width of buffer in pixels.
94 * height - height of buffer in pixels.
95 */
96 static void
97 get_buffer_size( GLframebuffer *buffer, GLuint *width, GLuint *height )
98 {
99 /* We can do this cast because the first field in the XMesaBuffer
100 * struct is a GLframebuffer struct. If this weren't true, we'd
101 * need a pointer from the GLframebuffer to the XMesaBuffer.
102 */
103 const XMesaBuffer xmBuffer = (XMesaBuffer) buffer;
104 unsigned int winwidth, winheight;
105 #ifdef XFree86Server
106 /* XFree86 GLX renderer */
107 if (xmBuffer->frontbuffer->width > MAX_WIDTH ||
108 xmBuffer->frontbuffer->height > MAX_HEIGHT) {
109 winwidth = buffer->Width;
110 winheight = buffer->Height;
111 } else {
112 winwidth = xmBuffer->frontbuffer->width;
113 winheight = xmBuffer->frontbuffer->height;
114 }
115 #else
116 Window root;
117 int winx, winy;
118 unsigned int bw, d;
119
120 _glthread_LOCK_MUTEX(_xmesa_lock);
121 XSync(xmBuffer->xm_visual->display, 0); /* added for Chromium */
122 XGetGeometry( xmBuffer->xm_visual->display, xmBuffer->frontbuffer, &root,
123 &winx, &winy, &winwidth, &winheight, &bw, &d );
124 _glthread_UNLOCK_MUTEX(_xmesa_lock);
125 #endif
126
127 *width = winwidth;
128 *height = winheight;
129 }
130
131
132 static void
133 finish_or_flush( GLcontext *ctx )
134 {
135 #ifdef XFree86Server
136 /* NOT_NEEDED */
137 #else
138 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
139 if (xmesa) {
140 _glthread_LOCK_MUTEX(_xmesa_lock);
141 XSync( xmesa->display, False );
142 _glthread_UNLOCK_MUTEX(_xmesa_lock);
143 }
144 #endif
145 }
146
147
148
149 /*
150 * This chooses the color buffer for reading and writing spans, points,
151 * lines, and triangles.
152 */
153 void
154 xmesa_set_buffer( GLcontext *ctx, GLframebuffer *buffer, GLuint bufferBit )
155 {
156 /* We can make this cast since the XMesaBuffer wraps GLframebuffer.
157 * GLframebuffer is the first member in a XMesaBuffer struct.
158 */
159 XMesaBuffer target = (XMesaBuffer) buffer;
160 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
161
162 /* This assignment tells the span/point/line/triangle functions
163 * which XMesaBuffer to use.
164 */
165 xmesa->xm_buffer = target;
166
167 /*
168 * Now determine front vs back color buffer.
169 */
170 if (bufferBit == FRONT_LEFT_BIT) {
171 target->buffer = target->frontbuffer;
172 }
173 else if (bufferBit == BACK_LEFT_BIT) {
174 ASSERT(target->db_state);
175 if (target->backpixmap) {
176 /* back buffer is a pixmap */
177 target->buffer = (XMesaDrawable) target->backpixmap;
178 }
179 else if (target->backimage) {
180 /* back buffer is an XImage */
181 target->buffer = None;
182 }
183 else {
184 /* No back buffer!!!! Must be out of memory, use front buffer */
185 target->buffer = target->frontbuffer;
186 }
187 }
188 else {
189 _mesa_problem(ctx, "invalid buffer 0x%x in set_buffer() in xm_dd.c");
190 return;
191 }
192 xmesa_update_span_funcs(ctx);
193 }
194
195
196
197 static void
198 clear_index( GLcontext *ctx, GLuint index )
199 {
200 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
201 xmesa->clearpixel = (unsigned long) index;
202 XMesaSetForeground( xmesa->display, xmesa->xm_draw_buffer->cleargc,
203 (unsigned long) index );
204 }
205
206
207 static void
208 clear_color( GLcontext *ctx, const GLfloat color[4] )
209 {
210 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
211 CLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[0], color[0]);
212 CLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[1], color[1]);
213 CLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[2], color[2]);
214 CLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[3], color[3]);
215 xmesa->clearpixel = xmesa_color_to_pixel( xmesa,
216 xmesa->clearcolor[0],
217 xmesa->clearcolor[1],
218 xmesa->clearcolor[2],
219 xmesa->clearcolor[3],
220 xmesa->xm_visual->undithered_pf );
221 _glthread_LOCK_MUTEX(_xmesa_lock);
222 XMesaSetForeground( xmesa->display, xmesa->xm_draw_buffer->cleargc,
223 xmesa->clearpixel );
224 _glthread_UNLOCK_MUTEX(_xmesa_lock);
225 }
226
227
228
229 /* Set index mask ala glIndexMask */
230 static void
231 index_mask( GLcontext *ctx, GLuint mask )
232 {
233 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
234 if (xmesa->xm_draw_buffer->buffer != XIMAGE) {
235 unsigned long m;
236 if (mask==0xffffffff) {
237 m = ((unsigned long)~0L);
238 }
239 else {
240 m = (unsigned long) mask;
241 }
242 XMesaSetPlaneMask( xmesa->display, xmesa->xm_draw_buffer->cleargc, m );
243 XMesaSetPlaneMask( xmesa->display, xmesa->xm_draw_buffer->gc, m );
244 }
245 }
246
247
248 /* Implements glColorMask() */
249 static void
250 color_mask(GLcontext *ctx,
251 GLboolean rmask, GLboolean gmask, GLboolean bmask, GLboolean amask)
252 {
253 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
254 const int xclass = xmesa->xm_visual->mesa_visual.visualType;
255 (void) amask;
256
257 if (xclass == GLX_TRUE_COLOR || xclass == GLX_DIRECT_COLOR) {
258 unsigned long m;
259 if (rmask && gmask && bmask) {
260 m = ((unsigned long)~0L);
261 }
262 else {
263 m = 0;
264 if (rmask) m |= GET_REDMASK(xmesa->xm_visual);
265 if (gmask) m |= GET_GREENMASK(xmesa->xm_visual);
266 if (bmask) m |= GET_BLUEMASK(xmesa->xm_visual);
267 }
268 XMesaSetPlaneMask( xmesa->display, xmesa->xm_draw_buffer->cleargc, m );
269 XMesaSetPlaneMask( xmesa->display, xmesa->xm_draw_buffer->gc, m );
270 }
271 }
272
273
274
275 /**********************************************************************/
276 /*** glClear implementations ***/
277 /**********************************************************************/
278
279
280 static void
281 clear_front_pixmap( GLcontext *ctx, GLboolean all,
282 GLint x, GLint y, GLint width, GLint height )
283 {
284 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
285 if (all) {
286 XMesaFillRectangle( xmesa->display, xmesa->xm_draw_buffer->frontbuffer,
287 xmesa->xm_draw_buffer->cleargc,
288 0, 0,
289 xmesa->xm_draw_buffer->width+1,
290 xmesa->xm_draw_buffer->height+1 );
291 }
292 else {
293 XMesaFillRectangle( xmesa->display, xmesa->xm_draw_buffer->frontbuffer,
294 xmesa->xm_draw_buffer->cleargc,
295 x, xmesa->xm_draw_buffer->height - y - height,
296 width, height );
297 }
298 }
299
300
301 static void
302 clear_back_pixmap( GLcontext *ctx, GLboolean all,
303 GLint x, GLint y, GLint width, GLint height )
304 {
305 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
306 if (all) {
307 XMesaFillRectangle( xmesa->display, xmesa->xm_draw_buffer->backpixmap,
308 xmesa->xm_draw_buffer->cleargc,
309 0, 0,
310 xmesa->xm_draw_buffer->width+1,
311 xmesa->xm_draw_buffer->height+1 );
312 }
313 else {
314 XMesaFillRectangle( xmesa->display, xmesa->xm_draw_buffer->backpixmap,
315 xmesa->xm_draw_buffer->cleargc,
316 x, xmesa->xm_draw_buffer->height - y - height,
317 width, height );
318 }
319 }
320
321
322 static void
323 clear_8bit_ximage( GLcontext *ctx, GLboolean all,
324 GLint x, GLint y, GLint width, GLint height )
325 {
326 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
327 if (all) {
328 size_t n = xmesa->xm_draw_buffer->backimage->bytes_per_line
329 * xmesa->xm_draw_buffer->backimage->height;
330 MEMSET( xmesa->xm_draw_buffer->backimage->data, xmesa->clearpixel, n );
331 }
332 else {
333 GLint i;
334 for (i=0;i<height;i++) {
335 GLubyte *ptr = PIXELADDR1( xmesa->xm_draw_buffer, x, y+i );
336 MEMSET( ptr, xmesa->clearpixel, width );
337 }
338 }
339 }
340
341
342 static void
343 clear_HPCR_ximage( GLcontext *ctx, GLboolean all,
344 GLint x, GLint y, GLint width, GLint height )
345 {
346 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
347 if (all) {
348 GLint i, c16 = (xmesa->xm_draw_buffer->backimage->bytes_per_line>>4)<<4;
349 GLubyte *ptr = (GLubyte *)xmesa->xm_draw_buffer->backimage->data;
350 for (i=0; i<xmesa->xm_draw_buffer->backimage->height; i++) {
351 GLint j;
352 GLubyte *sptr = xmesa->xm_visual->hpcr_clear_ximage_pattern[0];
353 if (i&1) {
354 sptr += 16;
355 }
356 for (j=0; j<c16; j+=16) {
357 ptr[0] = sptr[0];
358 ptr[1] = sptr[1];
359 ptr[2] = sptr[2];
360 ptr[3] = sptr[3];
361 ptr[4] = sptr[4];
362 ptr[5] = sptr[5];
363 ptr[6] = sptr[6];
364 ptr[7] = sptr[7];
365 ptr[8] = sptr[8];
366 ptr[9] = sptr[9];
367 ptr[10] = sptr[10];
368 ptr[11] = sptr[11];
369 ptr[12] = sptr[12];
370 ptr[13] = sptr[13];
371 ptr[14] = sptr[14];
372 ptr[15] = sptr[15];
373 ptr += 16;
374 }
375 for (; j<xmesa->xm_draw_buffer->backimage->bytes_per_line; j++) {
376 *ptr = sptr[j&15];
377 ptr++;
378 }
379 }
380 }
381 else {
382 GLint i;
383 for (i=y; i<y+height; i++) {
384 GLubyte *ptr = PIXELADDR1( xmesa->xm_draw_buffer, x, i );
385 int j;
386 GLubyte *sptr = xmesa->xm_visual->hpcr_clear_ximage_pattern[0];
387 if (i&1) {
388 sptr += 16;
389 }
390 for (j=x; j<x+width; j++) {
391 *ptr = sptr[j&15];
392 ptr++;
393 }
394 }
395 }
396 }
397
398
399 static void
400 clear_16bit_ximage( GLcontext *ctx, GLboolean all,
401 GLint x, GLint y, GLint width, GLint height )
402 {
403 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
404 register GLuint pixel = (GLuint) xmesa->clearpixel;
405 if (xmesa->swapbytes) {
406 pixel = ((pixel >> 8) & 0x00ff) | ((pixel << 8) & 0xff00);
407 }
408 if (all) {
409 register GLuint n;
410 register GLuint *ptr4 = (GLuint *) xmesa->xm_draw_buffer->backimage->data;
411 if ((pixel & 0xff) == ((pixel >> 8) & 0xff)) {
412 /* low and high bytes are equal so use memset() */
413 n = xmesa->xm_draw_buffer->backimage->bytes_per_line
414 * xmesa->xm_draw_buffer->height;
415 MEMSET( ptr4, pixel & 0xff, n );
416 }
417 else {
418 pixel = pixel | (pixel<<16);
419 n = xmesa->xm_draw_buffer->backimage->bytes_per_line
420 * xmesa->xm_draw_buffer->height / 4;
421 do {
422 *ptr4++ = pixel;
423 n--;
424 } while (n!=0);
425
426 if ((xmesa->xm_draw_buffer->backimage->bytes_per_line *
427 xmesa->xm_draw_buffer->height) & 0x2)
428 *(GLushort *)ptr4 = pixel & 0xffff;
429 }
430 }
431 else {
432 register int i, j;
433 for (j=0;j<height;j++) {
434 register GLushort *ptr2 = PIXELADDR2( xmesa->xm_draw_buffer, x, y+j );
435 for (i=0;i<width;i++) {
436 *ptr2++ = pixel;
437 }
438 }
439 }
440 }
441
442
443 /* Optimized code provided by Nozomi Ytow <noz@xfree86.org> */
444 static void
445 clear_24bit_ximage( GLcontext *ctx, GLboolean all,
446 GLint x, GLint y, GLint width, GLint height )
447 {
448 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
449 const GLubyte r = xmesa->clearcolor[0];
450 const GLubyte g = xmesa->clearcolor[1];
451 const GLubyte b = xmesa->clearcolor[2];
452 #if 0 /* See below */
453 register GLuint clearPixel;
454 if (xmesa->swapbytes) {
455 clearPixel = (b << 16) | (g << 8) | r;
456 }
457 else {
458 clearPixel = (r << 16) | (g << 8) | b;
459 }
460 #endif
461
462 if (all) {
463 if (r==g && g==b) {
464 /* same value for all three components (gray) */
465 const GLint w3 = xmesa->xm_draw_buffer->width * 3;
466 const GLint h = xmesa->xm_draw_buffer->height;
467 GLint i;
468 for (i = 0; i < h; i++) {
469 bgr_t *ptr3 = PIXELADDR3(xmesa->xm_draw_buffer, 0, i);
470 MEMSET(ptr3, r, w3);
471 }
472 }
473 else {
474 /* the usual case */
475 const GLint w = xmesa->xm_draw_buffer->width;
476 const GLint h = xmesa->xm_draw_buffer->height;
477 GLint i, j;
478 for (i = 0; i < h; i++) {
479 bgr_t *ptr3 = PIXELADDR3(xmesa->xm_draw_buffer, 0, i);
480 for (j = 0; j < w; j++) {
481 ptr3->r = r;
482 ptr3->g = g;
483 ptr3->b = b;
484 ptr3++;
485 }
486 }
487 #if 0 /* this code doesn't work for all window widths */
488 register GLuint *ptr4 = (GLuint *) ptr3;
489 register GLuint px;
490 GLuint pixel4[3];
491 register GLuint *p = pixel4;
492 pixel4[0] = clearPixel | (clearPixel << 24);
493 pixel4[1] = (clearPixel << 16) | (clearPixel >> 8);
494 pixel4[2] = (clearPixel << 8) | (clearPixel >> 16);
495 switch (3 & (int)(ptr3 - (bgr_t*) ptr4)){
496 case 0:
497 break;
498 case 1:
499 px = *ptr4 & 0x00ffffff;
500 px |= pixel4[0] & 0xff000000;
501 *ptr4++ = px;
502 px = *ptr4 & 0xffff0000;
503 px |= pixel4[2] & 0x0000ffff;
504 *ptr4 = px;
505 if (0 == --n)
506 break;
507 case 2:
508 px = *ptr4 & 0x0000fffff;
509 px |= pixel4[1] & 0xffff0000;
510 *ptr4++ = px;
511 px = *ptr4 & 0xffffff00;
512 px |= pixel4[2] & 0x000000ff;
513 *ptr4 = px;
514 if (0 == --n)
515 break;
516 case 3:
517 px = *ptr4 & 0x000000ff;
518 px |= pixel4[2] & 0xffffff00;
519 *ptr4++ = px;
520 --n;
521 break;
522 }
523 while (n > 3) {
524 p = pixel4;
525 *ptr4++ = *p++;
526 *ptr4++ = *p++;
527 *ptr4++ = *p++;
528 n -= 4;
529 }
530 switch (n) {
531 case 3:
532 p = pixel4;
533 *ptr4++ = *p++;
534 *ptr4++ = *p++;
535 px = *ptr4 & 0xffffff00;
536 px |= clearPixel & 0xff;
537 *ptr4 = px;
538 break;
539 case 2:
540 p = pixel4;
541 *ptr4++ = *p++;
542 px = *ptr4 & 0xffff0000;
543 px |= *p & 0xffff;
544 *ptr4 = px;
545 break;
546 case 1:
547 px = *ptr4 & 0xff000000;
548 px |= *p & 0xffffff;
549 *ptr4 = px;
550 break;
551 case 0:
552 break;
553 }
554 #endif
555 }
556 }
557 else {
558 /* only clear subrect of color buffer */
559 if (r==g && g==b) {
560 /* same value for all three components (gray) */
561 GLint j;
562 for (j=0;j<height;j++) {
563 bgr_t *ptr3 = PIXELADDR3( xmesa->xm_draw_buffer, x, y+j );
564 MEMSET(ptr3, r, 3 * width);
565 }
566 }
567 else {
568 /* non-gray clear color */
569 GLint i, j;
570 for (j = 0; j < height; j++) {
571 bgr_t *ptr3 = PIXELADDR3( xmesa->xm_draw_buffer, x, y+j );
572 for (i = 0; i < width; i++) {
573 ptr3->r = r;
574 ptr3->g = g;
575 ptr3->b = b;
576 ptr3++;
577 }
578 }
579 #if 0 /* this code might not always (seems ptr3 always == ptr4) */
580 GLint j;
581 GLuint pixel4[3];
582 pixel4[0] = clearPixel | (clearPixel << 24);
583 pixel4[1] = (clearPixel << 16) | (clearPixel >> 8);
584 pixel4[2] = (clearPixel << 8) | (clearPixel >> 16);
585 for (j=0;j<height;j++) {
586 bgr_t *ptr3 = PIXELADDR3( xmesa->xm_draw_buffer, x, y+j );
587 register GLuint *ptr4 = (GLuint *)ptr3;
588 register GLuint *p, px;
589 GLuint w = width;
590 switch (3 & (int)(ptr3 - (bgr_t*) ptr4)){
591 case 0:
592 break;
593 case 1:
594 px = *ptr4 & 0x00ffffff;
595 px |= pixel4[0] & 0xff000000;
596 *ptr4++ = px;
597 px = *ptr4 & 0xffff0000;
598 px |= pixel4[2] & 0x0000ffff;
599 *ptr4 = px;
600 if (0 == --w)
601 break;
602 case 2:
603 px = *ptr4 & 0x0000fffff;
604 px |= pixel4[1] & 0xffff0000;
605 *ptr4++ = px;
606 px = *ptr4 & 0xffffff00;
607 px |= pixel4[2] & 0x000000ff;
608 *ptr4 = px;
609 if (0 == --w)
610 break;
611 case 3:
612 px = *ptr4 & 0x000000ff;
613 px |= pixel4[2] & 0xffffff00;
614 *ptr4++ = px;
615 --w;
616 break;
617 }
618 while (w > 3){
619 p = pixel4;
620 *ptr4++ = *p++;
621 *ptr4++ = *p++;
622 *ptr4++ = *p++;
623 w -= 4;
624 }
625 switch (w) {
626 case 3:
627 p = pixel4;
628 *ptr4++ = *p++;
629 *ptr4++ = *p++;
630 px = *ptr4 & 0xffffff00;
631 px |= *p & 0xff;
632 *ptr4 = px;
633 break;
634 case 2:
635 p = pixel4;
636 *ptr4++ = *p++;
637 px = *ptr4 & 0xffff0000;
638 px |= *p & 0xffff;
639 *ptr4 = px;
640 break;
641 case 1:
642 px = *ptr4 & 0xff000000;
643 px |= pixel4[0] & 0xffffff;
644 *ptr4 = px;
645 break;
646 case 0:
647 break;
648 }
649 }
650 #endif
651 }
652 }
653 }
654
655
656 static void
657 clear_32bit_ximage( GLcontext *ctx, GLboolean all,
658 GLint x, GLint y, GLint width, GLint height )
659 {
660 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
661 register GLuint pixel = (GLuint) xmesa->clearpixel;
662 if (xmesa->swapbytes) {
663 pixel = ((pixel >> 24) & 0x000000ff)
664 | ((pixel >> 8) & 0x0000ff00)
665 | ((pixel << 8) & 0x00ff0000)
666 | ((pixel << 24) & 0xff000000);
667 }
668 if (all) {
669 register GLint n = xmesa->xm_draw_buffer->width * xmesa->xm_draw_buffer->height;
670 register GLuint *ptr4 = (GLuint *) xmesa->xm_draw_buffer->backimage->data;
671 if (pixel==0) {
672 MEMSET( ptr4, pixel, 4*n );
673 }
674 else {
675 do {
676 *ptr4++ = pixel;
677 n--;
678 } while (n!=0);
679 }
680 }
681 else {
682 register int i, j;
683 for (j=0;j<height;j++) {
684 register GLuint *ptr4 = PIXELADDR4( xmesa->xm_draw_buffer, x, y+j );
685 for (i=0;i<width;i++) {
686 *ptr4++ = pixel;
687 }
688 }
689 }
690 }
691
692
693 static void
694 clear_nbit_ximage( GLcontext *ctx, GLboolean all,
695 GLint x, GLint y, GLint width, GLint height )
696 {
697 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
698 XMesaImage *img = xmesa->xm_draw_buffer->backimage;
699 if (all) {
700 register int i, j;
701 width = xmesa->xm_draw_buffer->width;
702 height = xmesa->xm_draw_buffer->height;
703 for (j=0;j<height;j++) {
704 for (i=0;i<width;i++) {
705 XMesaPutPixel( img, i, j, xmesa->clearpixel );
706 }
707 }
708 }
709 else {
710 /* TODO: optimize this */
711 register int i, j;
712 y = FLIP(xmesa->xm_draw_buffer, y);
713 for (j=0;j<height;j++) {
714 for (i=0;i<width;i++) {
715 XMesaPutPixel( img, x+i, y-j, xmesa->clearpixel );
716 }
717 }
718 }
719 }
720
721
722
723 static void
724 clear_buffers( GLcontext *ctx, GLbitfield mask,
725 GLboolean all, GLint x, GLint y, GLint width, GLint height )
726 {
727 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
728 const GLuint *colorMask = (GLuint *) &ctx->Color.ColorMask;
729
730 if ((mask & (DD_FRONT_LEFT_BIT | DD_BACK_LEFT_BIT)) &&
731 xmesa->xm_draw_buffer->mesa_buffer.UseSoftwareAlphaBuffers &&
732 ctx->Color.ColorMask[ACOMP]) {
733 _swrast_clear_alpha_buffers(ctx);
734 }
735
736 /* we can't handle color or index masking */
737 if (*colorMask == 0xffffffff && ctx->Color.IndexMask == 0xffffffff) {
738 if (mask & DD_FRONT_LEFT_BIT) {
739 ASSERT(xmesa->xm_draw_buffer->front_clear_func);
740 (*xmesa->xm_draw_buffer->front_clear_func)( ctx, all, x, y, width, height );
741 mask &= ~DD_FRONT_LEFT_BIT;
742 }
743 if (mask & DD_BACK_LEFT_BIT) {
744 ASSERT(xmesa->xm_draw_buffer->back_clear_func);
745 (*xmesa->xm_draw_buffer->back_clear_func)( ctx, all, x, y, width, height );
746 mask &= ~DD_BACK_LEFT_BIT;
747 }
748 }
749
750 if (mask)
751 _swrast_Clear( ctx, mask, all, x, y, width, height );
752 }
753
754
755 /*
756 * When we detect that the user has resized the window this function will
757 * get called. Here we'll reallocate the back buffer, depth buffer,
758 * stencil buffer etc. to match the new window size.
759 */
760 void
761 xmesa_resize_buffers( GLframebuffer *buffer )
762 {
763 int height = (int) buffer->Height;
764 /* We can do this cast because the first field in the XMesaBuffer
765 * struct is a GLframebuffer struct. If this weren't true, we'd
766 * need a pointer from the GLframebuffer to the XMesaBuffer.
767 */
768 XMesaBuffer xmBuffer = (XMesaBuffer) buffer;
769
770 xmBuffer->width = buffer->Width;
771 xmBuffer->height = buffer->Height;
772 xmesa_alloc_back_buffer( xmBuffer );
773
774 /* Needed by FLIP macro */
775 xmBuffer->bottom = height - 1;
776
777 if (xmBuffer->backimage) {
778 /* Needed by PIXELADDR1 macro */
779 xmBuffer->ximage_width1 = xmBuffer->backimage->bytes_per_line;
780 xmBuffer->ximage_origin1 = (GLubyte *) xmBuffer->backimage->data
781 + xmBuffer->ximage_width1 * (height-1);
782
783 /* Needed by PIXELADDR2 macro */
784 xmBuffer->ximage_width2 = xmBuffer->backimage->bytes_per_line / 2;
785 xmBuffer->ximage_origin2 = (GLushort *) xmBuffer->backimage->data
786 + xmBuffer->ximage_width2 * (height-1);
787
788 /* Needed by PIXELADDR3 macro */
789 xmBuffer->ximage_width3 = xmBuffer->backimage->bytes_per_line;
790 xmBuffer->ximage_origin3 = (GLubyte *) xmBuffer->backimage->data
791 + xmBuffer->ximage_width3 * (height-1);
792
793 /* Needed by PIXELADDR4 macro */
794 xmBuffer->ximage_width4 = xmBuffer->backimage->width;
795 xmBuffer->ximage_origin4 = (GLuint *) xmBuffer->backimage->data
796 + xmBuffer->ximage_width4 * (height-1);
797 }
798
799 _swrast_alloc_buffers( buffer );
800 }
801
802
803 #ifndef XFree86Server
804 /* XXX this was never tested in the Xserver environment */
805
806 /**
807 * This function implements glDrawPixels() with an XPutImage call when
808 * drawing to the front buffer (X Window drawable).
809 * The image format must be GL_BGRA to match the PF_8R8G8B pixel format.
810 */
811 static void
812 xmesa_DrawPixels_8R8G8B( GLcontext *ctx,
813 GLint x, GLint y, GLsizei width, GLsizei height,
814 GLenum format, GLenum type,
815 const struct gl_pixelstore_attrib *unpack,
816 const GLvoid *pixels )
817 {
818 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
819 const SWcontext *swrast = SWRAST_CONTEXT( ctx );
820 XMesaDisplay *dpy = xmesa->xm_visual->display;
821 const XMesaDrawable buffer = xmesa->xm_draw_buffer->buffer;
822 const XMesaGC gc = xmesa->xm_draw_buffer->gc;
823
824 ASSERT(dpy);
825 ASSERT(gc);
826 ASSERT(xmesa->xm_visual->dithered_pf == PF_8R8G8B);
827 ASSERT(xmesa->xm_visual->undithered_pf == PF_8R8G8B);
828
829 if (swrast->NewState)
830 _swrast_validate_derived( ctx );
831
832 if (buffer && /* buffer != 0 means it's a Window or Pixmap */
833 format == GL_BGRA &&
834 type == GL_UNSIGNED_BYTE &&
835 (swrast->_RasterMask & ~CLIP_BIT) == 0 && /* no blend, z-test, etc */
836 ctx->_ImageTransferState == 0 && /* no color tables, scale/bias, etc */
837 ctx->Pixel.ZoomX == 1.0 && /* no zooming */
838 ctx->Pixel.ZoomY == 1.0) {
839 int dstX = x;
840 int dstY = y;
841 int w = width;
842 int h = height;
843 int srcX = unpack->SkipPixels;
844 int srcY = unpack->SkipRows;
845 int rowLength = unpack->RowLength ? unpack->RowLength : width;
846
847 pixels = _swrast_validate_pbo_access(unpack, width, height, 1,
848 format, type, (GLvoid *) pixels);
849 if (!pixels)
850 return;
851
852 if (_swrast_clip_pixelrect(ctx, &dstX, &dstY, &w, &h, &srcX, &srcY)) {
853 /* This is a little tricky since all coordinates up to now have
854 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
855 * so we have to carefully compute the Y coordinates/addresses here.
856 */
857 XMesaImage ximage;
858 MEMSET(&ximage, 0, sizeof(XMesaImage));
859 ximage.width = width;
860 ximage.height = height;
861 ximage.format = ZPixmap;
862 ximage.data = (char *) pixels
863 + ((srcY + h - 1) * rowLength + srcX) * 4;
864 ximage.byte_order = LSBFirst;
865 ximage.bitmap_unit = 32;
866 ximage.bitmap_bit_order = LSBFirst;
867 ximage.bitmap_pad = 32;
868 ximage.depth = 24;
869 ximage.bytes_per_line = -rowLength * 4; /* negative to flip image */
870 ximage.bits_per_pixel = 32;
871 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
872 /* flip Y axis for dest position */
873 dstY = FLIP(xmesa->xm_draw_buffer, dstY) - h + 1;
874 XPutImage(dpy, buffer, gc, &ximage, 0, 0, dstX, dstY, w, h);
875 }
876 }
877 else {
878 /* software fallback */
879 _swrast_DrawPixels(ctx, x, y, width, height,
880 format, type, unpack, pixels);
881 }
882 }
883
884
885
886 /**
887 * This function implements glDrawPixels() with an XPutImage call when
888 * drawing to the front buffer (X Window drawable). The image format
889 * must be GL_RGB and image type must be GL_UNSIGNED_SHORT_5_6_5 to
890 * match the PF_5R6G5B pixel format.
891 */
892 static void
893 xmesa_DrawPixels_5R6G5B( GLcontext *ctx,
894 GLint x, GLint y, GLsizei width, GLsizei height,
895 GLenum format, GLenum type,
896 const struct gl_pixelstore_attrib *unpack,
897 const GLvoid *pixels )
898 {
899 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
900 const SWcontext *swrast = SWRAST_CONTEXT( ctx );
901 XMesaDisplay *dpy = xmesa->xm_visual->display;
902 const XMesaDrawable buffer = xmesa->xm_draw_buffer->buffer;
903 const XMesaGC gc = xmesa->xm_draw_buffer->gc;
904
905 ASSERT(dpy);
906 ASSERT(gc);
907 ASSERT(xmesa->xm_visual->undithered_pf == PF_5R6G5B);
908
909 if (swrast->NewState)
910 _swrast_validate_derived( ctx );
911
912 if (buffer && /* buffer != 0 means it's a Window or Pixmap */
913 format == GL_RGB &&
914 type == GL_UNSIGNED_SHORT_5_6_5 &&
915 !ctx->Color.DitherFlag && /* no dithering */
916 (swrast->_RasterMask & ~CLIP_BIT) == 0 && /* no blend, z-test, etc */
917 ctx->_ImageTransferState == 0 && /* no color tables, scale/bias, etc */
918 ctx->Pixel.ZoomX == 1.0 && /* no zooming */
919 ctx->Pixel.ZoomY == 1.0) {
920 int dstX = x;
921 int dstY = y;
922 int w = width;
923 int h = height;
924 int srcX = unpack->SkipPixels;
925 int srcY = unpack->SkipRows;
926 int rowLength = unpack->RowLength ? unpack->RowLength : width;
927
928 pixels = _swrast_validate_pbo_access(unpack, width, height, 1,
929 format, type, (GLvoid *) pixels);
930 if (!pixels)
931 return;
932
933 if (_swrast_clip_pixelrect(ctx, &dstX, &dstY, &w, &h, &srcX, &srcY)) {
934 /* This is a little tricky since all coordinates up to now have
935 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
936 * so we have to carefully compute the Y coordinates/addresses here.
937 */
938 XMesaImage ximage;
939 MEMSET(&ximage, 0, sizeof(XMesaImage));
940 ximage.width = width;
941 ximage.height = height;
942 ximage.format = ZPixmap;
943 ximage.data = (char *) pixels
944 + ((srcY + h - 1) * rowLength + srcX) * 2;
945 ximage.byte_order = LSBFirst;
946 ximage.bitmap_unit = 16;
947 ximage.bitmap_bit_order = LSBFirst;
948 ximage.bitmap_pad = 16;
949 ximage.depth = 16;
950 ximage.bytes_per_line = -rowLength * 2; /* negative to flip image */
951 ximage.bits_per_pixel = 16;
952 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
953 /* flip Y axis for dest position */
954 dstY = FLIP(xmesa->xm_draw_buffer, dstY) - h + 1;
955 XPutImage(dpy, buffer, gc, &ximage, 0, 0, dstX, dstY, w, h);
956 }
957 }
958 else {
959 /* software fallback */
960 _swrast_DrawPixels(ctx, x, y, width, height,
961 format, type, unpack, pixels);
962 }
963 }
964
965
966
967 /**
968 * Implement glCopyPixels for the front color buffer (or back buffer Pixmap)
969 * for the color buffer. Don't support zooming, pixel transfer, etc.
970 * We do support copying from one window to another, ala glXMakeCurrentRead.
971 */
972 static void
973 xmesa_CopyPixels( GLcontext *ctx,
974 GLint srcx, GLint srcy, GLsizei width, GLsizei height,
975 GLint destx, GLint desty, GLenum type )
976 {
977 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
978 const SWcontext *swrast = SWRAST_CONTEXT( ctx );
979 XMesaDisplay *dpy = xmesa->xm_visual->display;
980 const XMesaDrawable drawBuffer = xmesa->xm_draw_buffer->buffer;
981 const XMesaDrawable readBuffer = xmesa->xm_read_buffer->buffer;
982 const XMesaGC gc = xmesa->xm_draw_buffer->gc;
983
984 ASSERT(dpy);
985 ASSERT(gc);
986
987 if (swrast->NewState)
988 _swrast_validate_derived( ctx );
989
990 if (ctx->Color.DrawBuffer == GL_FRONT &&
991 ctx->Pixel.ReadBuffer == GL_FRONT &&
992 drawBuffer && /* buffer != 0 means it's a Window or Pixmap */
993 readBuffer &&
994 type == GL_COLOR &&
995 (swrast->_RasterMask & ~CLIP_BIT) == 0 && /* no blend, z-test, etc */
996 ctx->_ImageTransferState == 0 && /* no color tables, scale/bias, etc */
997 ctx->Pixel.ZoomX == 1.0 && /* no zooming */
998 ctx->Pixel.ZoomY == 1.0) {
999 /* Note: we don't do any special clipping work here. We could,
1000 * but X will do it for us.
1001 */
1002 srcy = FLIP(xmesa->xm_read_buffer, srcy) - height + 1;
1003 desty = FLIP(xmesa->xm_draw_buffer, desty) - height + 1;
1004 XCopyArea(dpy, readBuffer, drawBuffer, gc,
1005 srcx, srcy, width, height, destx, desty);
1006 }
1007 else {
1008 _swrast_CopyPixels(ctx, srcx, srcy, width, height, destx, desty, type );
1009 }
1010 }
1011 #endif /* XFree86Server */
1012
1013
1014
1015 /*
1016 * Every driver should implement a GetString function in order to
1017 * return a meaningful GL_RENDERER string.
1018 */
1019 static const GLubyte *
1020 get_string( GLcontext *ctx, GLenum name )
1021 {
1022 (void) ctx;
1023 switch (name) {
1024 case GL_RENDERER:
1025 #ifdef XFree86Server
1026 return (const GLubyte *) "Mesa GLX Indirect";
1027 #else
1028 return (const GLubyte *) "Mesa X11";
1029 #endif
1030 case GL_VENDOR:
1031 #ifdef XFree86Server
1032 return (const GLubyte *) "Mesa project: www.mesa3d.org";
1033 #else
1034 return NULL;
1035 #endif
1036 default:
1037 return NULL;
1038 }
1039 }
1040
1041
1042 /*
1043 * We implement the glEnable function only because we care about
1044 * dither enable/disable.
1045 */
1046 static void
1047 enable( GLcontext *ctx, GLenum pname, GLboolean state )
1048 {
1049 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
1050
1051 switch (pname) {
1052 case GL_DITHER:
1053 if (state)
1054 xmesa->pixelformat = xmesa->xm_visual->dithered_pf;
1055 else
1056 xmesa->pixelformat = xmesa->xm_visual->undithered_pf;
1057 break;
1058 default:
1059 ; /* silence compiler warning */
1060 }
1061 }
1062
1063
1064 void xmesa_update_state( GLcontext *ctx, GLuint new_state )
1065 {
1066 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
1067
1068 /* Propogate statechange information to swrast and swrast_setup
1069 * modules. The X11 driver has no internal GL-dependent state.
1070 */
1071 _swrast_InvalidateState( ctx, new_state );
1072 _ac_InvalidateState( ctx, new_state );
1073 _tnl_InvalidateState( ctx, new_state );
1074 _swsetup_InvalidateState( ctx, new_state );
1075
1076
1077 /* setup pointers to front and back buffer clear functions */
1078 xmesa->xm_draw_buffer->front_clear_func = clear_front_pixmap;
1079 if (xmesa->xm_draw_buffer->backpixmap != XIMAGE) {
1080 xmesa->xm_draw_buffer->back_clear_func = clear_back_pixmap;
1081 }
1082 else if (sizeof(GLushort)!=2 || sizeof(GLuint)!=4) {
1083 xmesa->xm_draw_buffer->back_clear_func = clear_nbit_ximage;
1084 }
1085 else switch (xmesa->xm_visual->BitsPerPixel) {
1086 case 8:
1087 if (xmesa->xm_visual->hpcr_clear_flag) {
1088 xmesa->xm_draw_buffer->back_clear_func = clear_HPCR_ximage;
1089 }
1090 else {
1091 xmesa->xm_draw_buffer->back_clear_func = clear_8bit_ximage;
1092 }
1093 break;
1094 case 16:
1095 xmesa->xm_draw_buffer->back_clear_func = clear_16bit_ximage;
1096 break;
1097 case 24:
1098 xmesa->xm_draw_buffer->back_clear_func = clear_24bit_ximage;
1099 break;
1100 case 32:
1101 xmesa->xm_draw_buffer->back_clear_func = clear_32bit_ximage;
1102 break;
1103 default:
1104 xmesa->xm_draw_buffer->back_clear_func = clear_nbit_ximage;
1105 break;
1106 }
1107
1108 xmesa_update_span_funcs(ctx);
1109 }
1110
1111
1112
1113 /**
1114 * Called via ctx->Driver.TestProxyTeximage(). Normally, we'd just use
1115 * the _mesa_test_proxy_teximage() fallback function, but we're going to
1116 * special-case the 3D texture case to allow textures up to 512x512x32
1117 * texels.
1118 */
1119 static GLboolean
1120 test_proxy_teximage(GLcontext *ctx, GLenum target, GLint level,
1121 GLint internalFormat, GLenum format, GLenum type,
1122 GLint width, GLint height, GLint depth, GLint border)
1123 {
1124 if (target == GL_PROXY_TEXTURE_3D) {
1125 /* special case for 3D textures */
1126 if (width * height * depth > 512 * 512 * 64 ||
1127 width < 2 * border ||
1128 (!ctx->Extensions.ARB_texture_non_power_of_two &&
1129 _mesa_bitcount(width - 2 * border) != 1) ||
1130 height < 2 * border ||
1131 (!ctx->Extensions.ARB_texture_non_power_of_two &&
1132 _mesa_bitcount(height - 2 * border) != 1) ||
1133 depth < 2 * border ||
1134 (!ctx->Extensions.ARB_texture_non_power_of_two &&
1135 _mesa_bitcount(depth - 2 * border) != 1)) {
1136 /* Bad size, or too many texels */
1137 return GL_FALSE;
1138 }
1139 return GL_TRUE;
1140 }
1141 else {
1142 /* use the fallback routine for 1D, 2D, cube and rect targets */
1143 return _mesa_test_proxy_teximage(ctx, target, level, internalFormat,
1144 format, type, width, height, depth,
1145 border);
1146 }
1147 }
1148
1149
1150 /**
1151 * Initialize the device driver function table with the functions
1152 * we implement in this driver.
1153 */
1154 void xmesa_init_driver_functions( XMesaVisual xmvisual,
1155 struct dd_function_table *driver )
1156 {
1157 driver->GetString = get_string;
1158 driver->UpdateState = xmesa_update_state;
1159 driver->GetBufferSize = get_buffer_size;
1160 driver->Flush = finish_or_flush;
1161 driver->Finish = finish_or_flush;
1162 driver->ClearIndex = clear_index;
1163 driver->ClearColor = clear_color;
1164 driver->IndexMask = index_mask;
1165 driver->ColorMask = color_mask;
1166 driver->Enable = enable;
1167 driver->Clear = clear_buffers;
1168 driver->ResizeBuffers = xmesa_resize_buffers;
1169 #ifndef XFree86Server
1170 driver->CopyPixels = /*_swrast_CopyPixels;*/xmesa_CopyPixels;
1171 if (xmvisual->undithered_pf == PF_8R8G8B &&
1172 xmvisual->dithered_pf == PF_8R8G8B) {
1173 driver->DrawPixels = xmesa_DrawPixels_8R8G8B;
1174 }
1175 else if (xmvisual->undithered_pf == PF_5R6G5B) {
1176 driver->DrawPixels = xmesa_DrawPixels_5R6G5B;
1177 }
1178 #endif
1179 driver->TestProxyTexImage = test_proxy_teximage;
1180 }
1181
1182
1183 #define XMESA_NEW_POINT (_NEW_POINT | \
1184 _NEW_RENDERMODE | \
1185 _SWRAST_NEW_RASTERMASK)
1186
1187 #define XMESA_NEW_LINE (_NEW_LINE | \
1188 _NEW_TEXTURE | \
1189 _NEW_LIGHT | \
1190 _NEW_DEPTH | \
1191 _NEW_RENDERMODE | \
1192 _SWRAST_NEW_RASTERMASK)
1193
1194 #define XMESA_NEW_TRIANGLE (_NEW_POLYGON | \
1195 _NEW_TEXTURE | \
1196 _NEW_LIGHT | \
1197 _NEW_DEPTH | \
1198 _NEW_RENDERMODE | \
1199 _SWRAST_NEW_RASTERMASK)
1200
1201
1202 /* Extend the software rasterizer with our line/point/triangle
1203 * functions.
1204 */
1205 void xmesa_register_swrast_functions( GLcontext *ctx )
1206 {
1207 SWcontext *swrast = SWRAST_CONTEXT( ctx );
1208 struct swrast_device_driver *dd = _swrast_GetDeviceDriverReference(ctx);
1209
1210 dd->SetBuffer = xmesa_set_buffer;
1211
1212 swrast->choose_point = xmesa_choose_point;
1213 swrast->choose_line = xmesa_choose_line;
1214 swrast->choose_triangle = xmesa_choose_triangle;
1215
1216 swrast->invalidate_point |= XMESA_NEW_POINT;
1217 swrast->invalidate_line |= XMESA_NEW_LINE;
1218 swrast->invalidate_triangle |= XMESA_NEW_TRIANGLE;
1219 }