2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2004 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
26 #include "glxheader.h"
27 #include "bufferobj.h"
32 #include "extensions.h"
40 #include "texformat.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"
49 #include "tnl/t_context.h"
52 #include <GL/glxtokens.h>
58 * Dithering kernels and lookup tables.
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
,
68 const short xmesa_HPCR_DRGB
[3][2][16] = {
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}
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}
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}
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
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.
97 get_buffer_size( GLframebuffer
*buffer
, GLuint
*width
, GLuint
*height
)
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.
103 const XMesaBuffer xmBuffer
= (XMesaBuffer
) buffer
;
104 unsigned int winwidth
, winheight
;
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
;
112 winwidth
= xmBuffer
->frontbuffer
->width
;
113 winheight
= xmBuffer
->frontbuffer
->height
;
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
);
133 finish_or_flush( GLcontext
*ctx
)
138 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
140 _glthread_LOCK_MUTEX(_xmesa_lock
);
141 XSync( xmesa
->display
, False
);
142 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
150 * This chooses the color buffer for reading and writing spans, points,
151 * lines, and triangles.
154 xmesa_set_buffer( GLcontext
*ctx
, GLframebuffer
*buffer
, GLuint bufferBit
)
156 /* We can make this cast since the XMesaBuffer wraps GLframebuffer.
157 * GLframebuffer is the first member in a XMesaBuffer struct.
159 XMesaBuffer target
= (XMesaBuffer
) buffer
;
160 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
162 /* This assignment tells the span/point/line/triangle functions
163 * which XMesaBuffer to use.
165 xmesa
->xm_buffer
= target
;
168 * Now determine front vs back color buffer.
170 if (bufferBit
== FRONT_LEFT_BIT
) {
171 target
->buffer
= target
->frontbuffer
;
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
;
179 else if (target
->backimage
) {
180 /* back buffer is an XImage */
181 target
->buffer
= None
;
184 /* No back buffer!!!! Must be out of memory, use front buffer */
185 target
->buffer
= target
->frontbuffer
;
189 _mesa_problem(ctx
, "invalid buffer 0x%x in set_buffer() in xm_dd.c");
192 xmesa_update_span_funcs(ctx
);
198 clear_index( GLcontext
*ctx
, GLuint index
)
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
);
208 clear_color( GLcontext
*ctx
, const GLfloat color
[4] )
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
,
224 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
229 /* Set index mask ala glIndexMask */
231 index_mask( GLcontext
*ctx
, GLuint mask
)
233 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
234 if (xmesa
->xm_draw_buffer
->buffer
!= XIMAGE
) {
236 if (mask
==0xffffffff) {
237 m
= ((unsigned long)~0L);
240 m
= (unsigned long) mask
;
242 XMesaSetPlaneMask( xmesa
->display
, xmesa
->xm_draw_buffer
->cleargc
, m
);
243 XMesaSetPlaneMask( xmesa
->display
, xmesa
->xm_draw_buffer
->gc
, m
);
248 /* Implements glColorMask() */
250 color_mask(GLcontext
*ctx
,
251 GLboolean rmask
, GLboolean gmask
, GLboolean bmask
, GLboolean amask
)
253 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
254 const int xclass
= xmesa
->xm_visual
->mesa_visual
.visualType
;
257 if (xclass
== GLX_TRUE_COLOR
|| xclass
== GLX_DIRECT_COLOR
) {
259 if (rmask
&& gmask
&& bmask
) {
260 m
= ((unsigned long)~0L);
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
);
268 XMesaSetPlaneMask( xmesa
->display
, xmesa
->xm_draw_buffer
->cleargc
, m
);
269 XMesaSetPlaneMask( xmesa
->display
, xmesa
->xm_draw_buffer
->gc
, m
);
275 /**********************************************************************/
276 /*** glClear implementations ***/
277 /**********************************************************************/
281 clear_front_pixmap( GLcontext
*ctx
, GLboolean all
,
282 GLint x
, GLint y
, GLint width
, GLint height
)
284 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
286 XMesaFillRectangle( xmesa
->display
, xmesa
->xm_draw_buffer
->frontbuffer
,
287 xmesa
->xm_draw_buffer
->cleargc
,
289 xmesa
->xm_draw_buffer
->width
+1,
290 xmesa
->xm_draw_buffer
->height
+1 );
293 XMesaFillRectangle( xmesa
->display
, xmesa
->xm_draw_buffer
->frontbuffer
,
294 xmesa
->xm_draw_buffer
->cleargc
,
295 x
, xmesa
->xm_draw_buffer
->height
- y
- height
,
302 clear_back_pixmap( GLcontext
*ctx
, GLboolean all
,
303 GLint x
, GLint y
, GLint width
, GLint height
)
305 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
307 XMesaFillRectangle( xmesa
->display
, xmesa
->xm_draw_buffer
->backpixmap
,
308 xmesa
->xm_draw_buffer
->cleargc
,
310 xmesa
->xm_draw_buffer
->width
+1,
311 xmesa
->xm_draw_buffer
->height
+1 );
314 XMesaFillRectangle( xmesa
->display
, xmesa
->xm_draw_buffer
->backpixmap
,
315 xmesa
->xm_draw_buffer
->cleargc
,
316 x
, xmesa
->xm_draw_buffer
->height
- y
- height
,
323 clear_8bit_ximage( GLcontext
*ctx
, GLboolean all
,
324 GLint x
, GLint y
, GLint width
, GLint height
)
326 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
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
);
334 for (i
=0;i
<height
;i
++) {
335 GLubyte
*ptr
= PIXELADDR1( xmesa
->xm_draw_buffer
, x
, y
+i
);
336 MEMSET( ptr
, xmesa
->clearpixel
, width
);
343 clear_HPCR_ximage( GLcontext
*ctx
, GLboolean all
,
344 GLint x
, GLint y
, GLint width
, GLint height
)
346 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
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
++) {
352 GLubyte
*sptr
= xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0];
356 for (j
=0; j
<c16
; j
+=16) {
375 for (; j
<xmesa
->xm_draw_buffer
->backimage
->bytes_per_line
; j
++) {
383 for (i
=y
; i
<y
+height
; i
++) {
384 GLubyte
*ptr
= PIXELADDR1( xmesa
->xm_draw_buffer
, x
, i
);
386 GLubyte
*sptr
= xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0];
390 for (j
=x
; j
<x
+width
; j
++) {
400 clear_16bit_ximage( GLcontext
*ctx
, GLboolean all
,
401 GLint x
, GLint y
, GLint width
, GLint height
)
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);
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
);
418 pixel
= pixel
| (pixel
<<16);
419 n
= xmesa
->xm_draw_buffer
->backimage
->bytes_per_line
420 * xmesa
->xm_draw_buffer
->height
/ 4;
426 if ((xmesa
->xm_draw_buffer
->backimage
->bytes_per_line
*
427 xmesa
->xm_draw_buffer
->height
) & 0x2)
428 *(GLushort
*)ptr4
= pixel
& 0xffff;
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
++) {
443 /* Optimized code provided by Nozomi Ytow <noz@xfree86.org> */
445 clear_24bit_ximage( GLcontext
*ctx
, GLboolean all
,
446 GLint x
, GLint y
, GLint width
, GLint height
)
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
;
458 clearPixel
= (r
<< 16) | (g
<< 8) | 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
;
468 for (i
= 0; i
< h
; i
++) {
469 bgr_t
*ptr3
= PIXELADDR3(xmesa
->xm_draw_buffer
, 0, i
);
475 const GLint w
= xmesa
->xm_draw_buffer
->width
;
476 const GLint h
= xmesa
->xm_draw_buffer
->height
;
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
++) {
487 #if 0 /* this code doesn't work for all window widths */
488 register GLuint
*ptr4
= (GLuint
*) ptr3
;
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
)){
499 px
= *ptr4
& 0x00ffffff;
500 px
|= pixel4
[0] & 0xff000000;
502 px
= *ptr4
& 0xffff0000;
503 px
|= pixel4
[2] & 0x0000ffff;
508 px
= *ptr4
& 0x0000fffff;
509 px
|= pixel4
[1] & 0xffff0000;
511 px
= *ptr4
& 0xffffff00;
512 px
|= pixel4
[2] & 0x000000ff;
517 px
= *ptr4
& 0x000000ff;
518 px
|= pixel4
[2] & 0xffffff00;
535 px
= *ptr4
& 0xffffff00;
536 px
|= clearPixel
& 0xff;
542 px
= *ptr4
& 0xffff0000;
547 px
= *ptr4
& 0xff000000;
558 /* only clear subrect of color buffer */
560 /* same value for all three components (gray) */
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
);
568 /* non-gray clear color */
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
++) {
579 #if 0 /* this code might not always (seems ptr3 always == ptr4) */
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
;
590 switch (3 & (int)(ptr3
- (bgr_t
*) ptr4
)){
594 px
= *ptr4
& 0x00ffffff;
595 px
|= pixel4
[0] & 0xff000000;
597 px
= *ptr4
& 0xffff0000;
598 px
|= pixel4
[2] & 0x0000ffff;
603 px
= *ptr4
& 0x0000fffff;
604 px
|= pixel4
[1] & 0xffff0000;
606 px
= *ptr4
& 0xffffff00;
607 px
|= pixel4
[2] & 0x000000ff;
612 px
= *ptr4
& 0x000000ff;
613 px
|= pixel4
[2] & 0xffffff00;
630 px
= *ptr4
& 0xffffff00;
637 px
= *ptr4
& 0xffff0000;
642 px
= *ptr4
& 0xff000000;
643 px
|= pixel4
[0] & 0xffffff;
657 clear_32bit_ximage( GLcontext
*ctx
, GLboolean all
,
658 GLint x
, GLint y
, GLint width
, GLint height
)
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);
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
;
672 MEMSET( ptr4
, pixel
, 4*n
);
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
++) {
694 clear_nbit_ximage( GLcontext
*ctx
, GLboolean all
,
695 GLint x
, GLint y
, GLint width
, GLint height
)
697 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
698 XMesaImage
*img
= xmesa
->xm_draw_buffer
->backimage
;
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
);
710 /* TODO: optimize this */
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
);
724 clear_buffers( GLcontext
*ctx
, GLbitfield mask
,
725 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
727 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
728 const GLuint
*colorMask
= (GLuint
*) &ctx
->Color
.ColorMask
;
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
);
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
;
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
;
751 _swrast_Clear( ctx
, mask
, all
, x
, y
, width
, height
);
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.
761 xmesa_resize_buffers( GLframebuffer
*buffer
)
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.
768 XMesaBuffer xmBuffer
= (XMesaBuffer
) buffer
;
770 xmBuffer
->width
= buffer
->Width
;
771 xmBuffer
->height
= buffer
->Height
;
772 xmesa_alloc_back_buffer( xmBuffer
);
774 /* Needed by FLIP macro */
775 xmBuffer
->bottom
= height
- 1;
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);
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);
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);
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);
799 _swrast_alloc_buffers( buffer
);
803 #ifndef XFree86Server
804 /* XXX this was never tested in the Xserver environment */
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.
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
)
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
;
826 ASSERT(xmesa
->xm_visual
->dithered_pf
== PF_8R8G8B
);
827 ASSERT(xmesa
->xm_visual
->undithered_pf
== PF_8R8G8B
);
829 if (swrast
->NewState
)
830 _swrast_validate_derived( ctx
);
832 if (buffer
&& /* buffer != 0 means it's a Window or Pixmap */
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) {
843 int srcX
= unpack
->SkipPixels
;
844 int srcY
= unpack
->SkipRows
;
845 int rowLength
= unpack
->RowLength
? unpack
->RowLength
: width
;
847 pixels
= _swrast_validate_pbo_access(unpack
, width
, height
, 1,
848 format
, type
, (GLvoid
*) pixels
);
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.
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;
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
);
878 /* software fallback */
879 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
880 format
, type
, unpack
, pixels
);
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.
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
)
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
;
907 ASSERT(xmesa
->xm_visual
->undithered_pf
== PF_5R6G5B
);
909 if (swrast
->NewState
)
910 _swrast_validate_derived( ctx
);
912 if (buffer
&& /* buffer != 0 means it's a Window or Pixmap */
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) {
924 int srcX
= unpack
->SkipPixels
;
925 int srcY
= unpack
->SkipRows
;
926 int rowLength
= unpack
->RowLength
? unpack
->RowLength
: width
;
928 pixels
= _swrast_validate_pbo_access(unpack
, width
, height
, 1,
929 format
, type
, (GLvoid
*) pixels
);
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.
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;
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
);
959 /* software fallback */
960 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
961 format
, type
, unpack
, pixels
);
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.
973 xmesa_CopyPixels( GLcontext
*ctx
,
974 GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
975 GLint destx
, GLint desty
, GLenum type
)
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
;
987 if (swrast
->NewState
)
988 _swrast_validate_derived( ctx
);
990 if (ctx
->Color
.DrawBuffer
== GL_FRONT
&&
991 ctx
->Pixel
.ReadBuffer
== GL_FRONT
&&
992 drawBuffer
&& /* buffer != 0 means it's a Window or Pixmap */
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.
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
);
1008 _swrast_CopyPixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
, type
);
1011 #endif /* XFree86Server */
1016 * Every driver should implement a GetString function in order to
1017 * return a meaningful GL_RENDERER string.
1019 static const GLubyte
*
1020 get_string( GLcontext
*ctx
, GLenum name
)
1025 #ifdef XFree86Server
1026 return (const GLubyte
*) "Mesa GLX Indirect";
1028 return (const GLubyte
*) "Mesa X11";
1031 #ifdef XFree86Server
1032 return (const GLubyte
*) "Mesa project: www.mesa3d.org";
1043 * We implement the glEnable function only because we care about
1044 * dither enable/disable.
1047 enable( GLcontext
*ctx
, GLenum pname
, GLboolean state
)
1049 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
1054 xmesa
->pixelformat
= xmesa
->xm_visual
->dithered_pf
;
1056 xmesa
->pixelformat
= xmesa
->xm_visual
->undithered_pf
;
1059 ; /* silence compiler warning */
1064 void xmesa_update_state( GLcontext
*ctx
, GLuint new_state
)
1066 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
1068 /* Propogate statechange information to swrast and swrast_setup
1069 * modules. The X11 driver has no internal GL-dependent state.
1071 _swrast_InvalidateState( ctx
, new_state
);
1072 _ac_InvalidateState( ctx
, new_state
);
1073 _tnl_InvalidateState( ctx
, new_state
);
1074 _swsetup_InvalidateState( ctx
, new_state
);
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
;
1082 else if (sizeof(GLushort
)!=2 || sizeof(GLuint
)!=4) {
1083 xmesa
->xm_draw_buffer
->back_clear_func
= clear_nbit_ximage
;
1085 else switch (xmesa
->xm_visual
->BitsPerPixel
) {
1087 if (xmesa
->xm_visual
->hpcr_clear_flag
) {
1088 xmesa
->xm_draw_buffer
->back_clear_func
= clear_HPCR_ximage
;
1091 xmesa
->xm_draw_buffer
->back_clear_func
= clear_8bit_ximage
;
1095 xmesa
->xm_draw_buffer
->back_clear_func
= clear_16bit_ximage
;
1098 xmesa
->xm_draw_buffer
->back_clear_func
= clear_24bit_ximage
;
1101 xmesa
->xm_draw_buffer
->back_clear_func
= clear_32bit_ximage
;
1104 xmesa
->xm_draw_buffer
->back_clear_func
= clear_nbit_ximage
;
1108 xmesa_update_span_funcs(ctx
);
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
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
)
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 */
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
,
1151 * Initialize the device driver function table with the functions
1152 * we implement in this driver.
1154 void xmesa_init_driver_functions( XMesaVisual xmvisual
,
1155 struct dd_function_table
*driver
)
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
;
1175 else if (xmvisual
->undithered_pf
== PF_5R6G5B
) {
1176 driver
->DrawPixels
= xmesa_DrawPixels_5R6G5B
;
1179 driver
->TestProxyTexImage
= test_proxy_teximage
;
1183 #define XMESA_NEW_POINT (_NEW_POINT | \
1185 _SWRAST_NEW_RASTERMASK)
1187 #define XMESA_NEW_LINE (_NEW_LINE | \
1192 _SWRAST_NEW_RASTERMASK)
1194 #define XMESA_NEW_TRIANGLE (_NEW_POLYGON | \
1199 _SWRAST_NEW_RASTERMASK)
1202 /* Extend the software rasterizer with our line/point/triangle
1205 void xmesa_register_swrast_functions( GLcontext
*ctx
)
1207 SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
1208 struct swrast_device_driver
*dd
= _swrast_GetDeviceDriverReference(ctx
);
1210 dd
->SetBuffer
= xmesa_set_buffer
;
1212 swrast
->choose_point
= xmesa_choose_point
;
1213 swrast
->choose_line
= xmesa_choose_line
;
1214 swrast
->choose_triangle
= xmesa_choose_triangle
;
1216 swrast
->invalidate_point
|= XMESA_NEW_POINT
;
1217 swrast
->invalidate_line
|= XMESA_NEW_LINE
;
1218 swrast
->invalidate_triangle
|= XMESA_NEW_TRIANGLE
;