2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2003 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"
50 #include "tnl/t_pipeline.h"
54 * Return the size (width, height) of the X window for the given GLframebuffer.
55 * Output: width - width of buffer in pixels.
56 * height - height of buffer in pixels.
59 get_buffer_size( GLframebuffer
*buffer
, GLuint
*width
, GLuint
*height
)
61 /* We can do this cast because the first field in the XMesaBuffer
62 * struct is a GLframebuffer struct. If this weren't true, we'd
63 * need a pointer from the GLframebuffer to the XMesaBuffer.
65 const XMesaBuffer xmBuffer
= (XMesaBuffer
) buffer
;
66 unsigned int winwidth
, winheight
;
68 /* XFree86 GLX renderer */
69 if (xmBuffer
->frontbuffer
->width
> MAX_WIDTH
||
70 xmBuffer
->frontbuffer
->height
> MAX_HEIGHT
) {
71 winwidth
= buffer
->Width
;
72 winheight
= buffer
->Height
;
74 winwidth
= xmBuffer
->frontbuffer
->width
;
75 winheight
= xmBuffer
->frontbuffer
->height
;
82 _glthread_LOCK_MUTEX(_xmesa_lock
);
83 XSync(xmBuffer
->xm_visual
->display
, 0); /* added for Chromium */
84 XGetGeometry( xmBuffer
->xm_visual
->display
, xmBuffer
->frontbuffer
, &root
,
85 &winx
, &winy
, &winwidth
, &winheight
, &bw
, &d
);
86 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
89 (void)kernel8
; /* Muffle compiler */
97 finish_or_flush( GLcontext
*ctx
)
102 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
104 _glthread_LOCK_MUTEX(_xmesa_lock
);
105 XSync( xmesa
->display
, False
);
106 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
114 * This chooses the color buffer for reading and writing spans, points,
115 * lines, and triangles.
118 set_buffer( GLcontext
*ctx
, GLframebuffer
*buffer
, GLuint bufferBit
)
120 /* We can make this cast since the XMesaBuffer wraps GLframebuffer.
121 * GLframebuffer is the first member in a XMesaBuffer struct.
123 XMesaBuffer target
= (XMesaBuffer
) buffer
;
124 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
126 /* This assignment tells the span/point/line/triangle functions
127 * which XMesaBuffer to use.
129 xmesa
->xm_buffer
= target
;
132 * Now determine front vs back color buffer.
134 if (bufferBit
== FRONT_LEFT_BIT
) {
135 target
->buffer
= target
->frontbuffer
;
137 else if (bufferBit
== BACK_LEFT_BIT
) {
138 ASSERT(target
->db_state
);
139 if (target
->backpixmap
) {
140 /* back buffer is a pixmape */
141 target
->buffer
= target
->backpixmap
; /* incompatible types? */
143 else if (target
->backimage
) {
144 /* back buffer is an XImage */
145 target
->buffer
= None
;
148 /* No back buffer!!!! Must be out of memory, use front buffer */
149 target
->buffer
= target
->frontbuffer
;
153 _mesa_problem(ctx
, "invalid buffer 0x%x in set_buffer() in xm_dd.c");
156 xmesa_update_span_funcs(ctx
);
162 clear_index( GLcontext
*ctx
, GLuint index
)
164 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
165 xmesa
->clearpixel
= (unsigned long) index
;
166 XMesaSetForeground( xmesa
->display
, xmesa
->xm_draw_buffer
->cleargc
,
167 (unsigned long) index
);
172 clear_color( GLcontext
*ctx
, const GLfloat color
[4] )
174 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
175 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[0], color
[0]);
176 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[1], color
[1]);
177 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[2], color
[2]);
178 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[3], color
[3]);
179 xmesa
->clearpixel
= xmesa_color_to_pixel( xmesa
,
180 xmesa
->clearcolor
[0],
181 xmesa
->clearcolor
[1],
182 xmesa
->clearcolor
[2],
183 xmesa
->clearcolor
[3],
184 xmesa
->xm_visual
->undithered_pf
);
185 _glthread_LOCK_MUTEX(_xmesa_lock
);
186 XMesaSetForeground( xmesa
->display
, xmesa
->xm_draw_buffer
->cleargc
,
188 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
193 /* Set index mask ala glIndexMask */
195 index_mask( GLcontext
*ctx
, GLuint mask
)
197 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
198 if (xmesa
->xm_draw_buffer
->buffer
!= XIMAGE
) {
200 if (mask
==0xffffffff) {
201 m
= ((unsigned long)~0L);
204 m
= (unsigned long) mask
;
206 XMesaSetPlaneMask( xmesa
->display
, xmesa
->xm_draw_buffer
->cleargc
, m
);
207 XMesaSetPlaneMask( xmesa
->display
, xmesa
->xm_draw_buffer
->gc
, m
);
212 /* Implements glColorMask() */
214 color_mask(GLcontext
*ctx
,
215 GLboolean rmask
, GLboolean gmask
, GLboolean bmask
, GLboolean amask
)
217 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
218 int xclass
= GET_VISUAL_CLASS(xmesa
->xm_visual
);
221 if (xclass
== TrueColor
|| xclass
== DirectColor
) {
223 if (rmask
&& gmask
&& bmask
) {
224 m
= ((unsigned long)~0L);
228 if (rmask
) m
|= GET_REDMASK(xmesa
->xm_visual
);
229 if (gmask
) m
|= GET_GREENMASK(xmesa
->xm_visual
);
230 if (bmask
) m
|= GET_BLUEMASK(xmesa
->xm_visual
);
232 XMesaSetPlaneMask( xmesa
->display
, xmesa
->xm_draw_buffer
->cleargc
, m
);
233 XMesaSetPlaneMask( xmesa
->display
, xmesa
->xm_draw_buffer
->gc
, m
);
239 /**********************************************************************/
240 /*** glClear implementations ***/
241 /**********************************************************************/
245 clear_front_pixmap( GLcontext
*ctx
, GLboolean all
,
246 GLint x
, GLint y
, GLint width
, GLint height
)
248 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
250 XMesaFillRectangle( xmesa
->display
, xmesa
->xm_draw_buffer
->frontbuffer
,
251 xmesa
->xm_draw_buffer
->cleargc
,
253 xmesa
->xm_draw_buffer
->width
+1,
254 xmesa
->xm_draw_buffer
->height
+1 );
257 XMesaFillRectangle( xmesa
->display
, xmesa
->xm_draw_buffer
->frontbuffer
,
258 xmesa
->xm_draw_buffer
->cleargc
,
259 x
, xmesa
->xm_draw_buffer
->height
- y
- height
,
266 clear_back_pixmap( GLcontext
*ctx
, GLboolean all
,
267 GLint x
, GLint y
, GLint width
, GLint height
)
269 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
271 XMesaFillRectangle( xmesa
->display
, xmesa
->xm_draw_buffer
->backpixmap
,
272 xmesa
->xm_draw_buffer
->cleargc
,
274 xmesa
->xm_draw_buffer
->width
+1,
275 xmesa
->xm_draw_buffer
->height
+1 );
278 XMesaFillRectangle( xmesa
->display
, xmesa
->xm_draw_buffer
->backpixmap
,
279 xmesa
->xm_draw_buffer
->cleargc
,
280 x
, xmesa
->xm_draw_buffer
->height
- y
- height
,
287 clear_8bit_ximage( GLcontext
*ctx
, GLboolean all
,
288 GLint x
, GLint y
, GLint width
, GLint height
)
290 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
292 size_t n
= xmesa
->xm_draw_buffer
->backimage
->bytes_per_line
293 * xmesa
->xm_draw_buffer
->backimage
->height
;
294 MEMSET( xmesa
->xm_draw_buffer
->backimage
->data
, xmesa
->clearpixel
, n
);
298 for (i
=0;i
<height
;i
++) {
299 GLubyte
*ptr
= PIXELADDR1( xmesa
->xm_draw_buffer
, x
, y
+i
);
300 MEMSET( ptr
, xmesa
->clearpixel
, width
);
307 clear_HPCR_ximage( GLcontext
*ctx
, GLboolean all
,
308 GLint x
, GLint y
, GLint width
, GLint height
)
310 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
312 GLint i
, c16
= (xmesa
->xm_draw_buffer
->backimage
->bytes_per_line
>>4)<<4;
313 GLubyte
*ptr
= (GLubyte
*)xmesa
->xm_draw_buffer
->backimage
->data
;
314 for (i
=0; i
<xmesa
->xm_draw_buffer
->backimage
->height
; i
++) {
316 GLubyte
*sptr
= xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0];
320 for (j
=0; j
<c16
; j
+=16) {
339 for (; j
<xmesa
->xm_draw_buffer
->backimage
->bytes_per_line
; j
++) {
347 for (i
=y
; i
<y
+height
; i
++) {
348 GLubyte
*ptr
= PIXELADDR1( xmesa
->xm_draw_buffer
, x
, i
);
350 GLubyte
*sptr
= xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0];
354 for (j
=x
; j
<x
+width
; j
++) {
364 clear_16bit_ximage( GLcontext
*ctx
, GLboolean all
,
365 GLint x
, GLint y
, GLint width
, GLint height
)
367 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
368 register GLuint pixel
= (GLuint
) xmesa
->clearpixel
;
369 if (xmesa
->swapbytes
) {
370 pixel
= ((pixel
>> 8) & 0x00ff) | ((pixel
<< 8) & 0xff00);
374 register GLuint
*ptr4
= (GLuint
*) xmesa
->xm_draw_buffer
->backimage
->data
;
375 if ((pixel
& 0xff) == ((pixel
>> 8) & 0xff)) {
376 /* low and high bytes are equal so use memset() */
377 n
= xmesa
->xm_draw_buffer
->backimage
->bytes_per_line
378 * xmesa
->xm_draw_buffer
->height
;
379 MEMSET( ptr4
, pixel
& 0xff, n
);
382 pixel
= pixel
| (pixel
<<16);
383 n
= xmesa
->xm_draw_buffer
->backimage
->bytes_per_line
384 * xmesa
->xm_draw_buffer
->height
/ 4;
390 if ((xmesa
->xm_draw_buffer
->backimage
->bytes_per_line
*
391 xmesa
->xm_draw_buffer
->height
) & 0x2)
392 *(GLushort
*)ptr4
= pixel
& 0xffff;
397 for (j
=0;j
<height
;j
++) {
398 register GLushort
*ptr2
= PIXELADDR2( xmesa
->xm_draw_buffer
, x
, y
+j
);
399 for (i
=0;i
<width
;i
++) {
407 /* Optimized code provided by Nozomi Ytow <noz@xfree86.org> */
409 clear_24bit_ximage( GLcontext
*ctx
, GLboolean all
,
410 GLint x
, GLint y
, GLint width
, GLint height
)
412 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
413 const GLubyte r
= xmesa
->clearcolor
[0];
414 const GLubyte g
= xmesa
->clearcolor
[1];
415 const GLubyte b
= xmesa
->clearcolor
[2];
416 register GLuint clearPixel
;
417 if (xmesa
->swapbytes
) {
418 clearPixel
= (b
<< 16) | (g
<< 8) | r
;
421 clearPixel
= (r
<< 16) | (g
<< 8) | b
;
426 /* same value for all three components (gray) */
427 const GLint w3
= xmesa
->xm_draw_buffer
->width
* 3;
428 const GLint h
= xmesa
->xm_draw_buffer
->height
;
430 for (i
= 0; i
< h
; i
++) {
431 bgr_t
*ptr3
= PIXELADDR3(xmesa
->xm_draw_buffer
, 0, i
);
437 const GLint w
= xmesa
->xm_draw_buffer
->width
;
438 const GLint h
= xmesa
->xm_draw_buffer
->height
;
440 for (i
= 0; i
< h
; i
++) {
441 bgr_t
*ptr3
= PIXELADDR3(xmesa
->xm_draw_buffer
, 0, i
);
442 for (j
= 0; j
< w
; j
++) {
449 #if 0 /* this code doesn't work for all window widths */
450 register GLuint
*ptr4
= (GLuint
*) ptr3
;
453 register GLuint
*p
= pixel4
;
454 pixel4
[0] = clearPixel
| (clearPixel
<< 24);
455 pixel4
[1] = (clearPixel
<< 16) | (clearPixel
>> 8);
456 pixel4
[2] = (clearPixel
<< 8) | (clearPixel
>> 16);
457 switch (3 & (int)(ptr3
- (bgr_t
*) ptr4
)){
461 px
= *ptr4
& 0x00ffffff;
462 px
|= pixel4
[0] & 0xff000000;
464 px
= *ptr4
& 0xffff0000;
465 px
|= pixel4
[2] & 0x0000ffff;
470 px
= *ptr4
& 0x0000fffff;
471 px
|= pixel4
[1] & 0xffff0000;
473 px
= *ptr4
& 0xffffff00;
474 px
|= pixel4
[2] & 0x000000ff;
479 px
= *ptr4
& 0x000000ff;
480 px
|= pixel4
[2] & 0xffffff00;
497 px
= *ptr4
& 0xffffff00;
498 px
|= clearPixel
& 0xff;
504 px
= *ptr4
& 0xffff0000;
509 px
= *ptr4
& 0xff000000;
520 /* only clear subrect of color buffer */
522 /* same value for all three components (gray) */
524 for (j
=0;j
<height
;j
++) {
525 bgr_t
*ptr3
= PIXELADDR3( xmesa
->xm_draw_buffer
, x
, y
+j
);
526 MEMSET(ptr3
, r
, 3 * width
);
530 /* non-gray clear color */
532 for (j
= 0; j
< height
; j
++) {
533 bgr_t
*ptr3
= PIXELADDR3( xmesa
->xm_draw_buffer
, x
, y
+j
);
534 for (i
= 0; i
< width
; i
++) {
541 #if 0 /* this code might not always (seems ptr3 always == ptr4) */
544 pixel4
[0] = clearPixel
| (clearPixel
<< 24);
545 pixel4
[1] = (clearPixel
<< 16) | (clearPixel
>> 8);
546 pixel4
[2] = (clearPixel
<< 8) | (clearPixel
>> 16);
547 for (j
=0;j
<height
;j
++) {
548 bgr_t
*ptr3
= PIXELADDR3( xmesa
->xm_draw_buffer
, x
, y
+j
);
549 register GLuint
*ptr4
= (GLuint
*)ptr3
;
550 register GLuint
*p
, px
;
552 switch (3 & (int)(ptr3
- (bgr_t
*) ptr4
)){
556 px
= *ptr4
& 0x00ffffff;
557 px
|= pixel4
[0] & 0xff000000;
559 px
= *ptr4
& 0xffff0000;
560 px
|= pixel4
[2] & 0x0000ffff;
565 px
= *ptr4
& 0x0000fffff;
566 px
|= pixel4
[1] & 0xffff0000;
568 px
= *ptr4
& 0xffffff00;
569 px
|= pixel4
[2] & 0x000000ff;
574 px
= *ptr4
& 0x000000ff;
575 px
|= pixel4
[2] & 0xffffff00;
592 px
= *ptr4
& 0xffffff00;
599 px
= *ptr4
& 0xffff0000;
604 px
= *ptr4
& 0xff000000;
605 px
|= pixel4
[0] & 0xffffff;
619 clear_32bit_ximage( GLcontext
*ctx
, GLboolean all
,
620 GLint x
, GLint y
, GLint width
, GLint height
)
622 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
623 register GLuint pixel
= (GLuint
) xmesa
->clearpixel
;
624 if (xmesa
->swapbytes
) {
625 pixel
= ((pixel
>> 24) & 0x000000ff)
626 | ((pixel
>> 8) & 0x0000ff00)
627 | ((pixel
<< 8) & 0x00ff0000)
628 | ((pixel
<< 24) & 0xff000000);
631 register GLint n
= xmesa
->xm_draw_buffer
->width
* xmesa
->xm_draw_buffer
->height
;
632 register GLuint
*ptr4
= (GLuint
*) xmesa
->xm_draw_buffer
->backimage
->data
;
634 MEMSET( ptr4
, pixel
, 4*n
);
645 for (j
=0;j
<height
;j
++) {
646 register GLuint
*ptr4
= PIXELADDR4( xmesa
->xm_draw_buffer
, x
, y
+j
);
647 for (i
=0;i
<width
;i
++) {
656 clear_nbit_ximage( GLcontext
*ctx
, GLboolean all
,
657 GLint x
, GLint y
, GLint width
, GLint height
)
659 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
660 XMesaImage
*img
= xmesa
->xm_draw_buffer
->backimage
;
663 width
= xmesa
->xm_draw_buffer
->width
;
664 height
= xmesa
->xm_draw_buffer
->height
;
665 for (j
=0;j
<height
;j
++) {
666 for (i
=0;i
<width
;i
++) {
667 XMesaPutPixel( img
, i
, j
, xmesa
->clearpixel
);
672 /* TODO: optimize this */
674 y
= FLIP(xmesa
->xm_draw_buffer
, y
);
675 for (j
=0;j
<height
;j
++) {
676 for (i
=0;i
<width
;i
++) {
677 XMesaPutPixel( img
, x
+i
, y
-j
, xmesa
->clearpixel
);
686 clear_buffers( GLcontext
*ctx
, GLbitfield mask
,
687 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
689 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
690 const GLuint
*colorMask
= (GLuint
*) &ctx
->Color
.ColorMask
;
692 if ((mask
& (DD_FRONT_LEFT_BIT
| DD_BACK_LEFT_BIT
)) &&
693 xmesa
->xm_draw_buffer
->mesa_buffer
.UseSoftwareAlphaBuffers
&&
694 ctx
->Color
.ColorMask
[ACOMP
]) {
695 _swrast_clear_alpha_buffers(ctx
);
698 /* we can't handle color or index masking */
699 if (*colorMask
== 0xffffffff && ctx
->Color
.IndexMask
== 0xffffffff) {
700 if (mask
& DD_FRONT_LEFT_BIT
) {
701 ASSERT(xmesa
->xm_draw_buffer
->front_clear_func
);
702 (*xmesa
->xm_draw_buffer
->front_clear_func
)( ctx
, all
, x
, y
, width
, height
);
703 mask
&= ~DD_FRONT_LEFT_BIT
;
705 if (mask
& DD_BACK_LEFT_BIT
) {
706 ASSERT(xmesa
->xm_draw_buffer
->back_clear_func
);
707 (*xmesa
->xm_draw_buffer
->back_clear_func
)( ctx
, all
, x
, y
, width
, height
);
708 mask
&= ~DD_BACK_LEFT_BIT
;
713 _swrast_Clear( ctx
, mask
, all
, x
, y
, width
, height
);
718 * When we detect that the user has resized the window this function will
719 * get called. Here we'll reallocate the back buffer, depth buffer,
720 * stencil buffer etc. to match the new window size.
723 xmesa_resize_buffers( GLframebuffer
*buffer
)
725 int height
= (int) buffer
->Height
;
726 /* We can do this cast because the first field in the XMesaBuffer
727 * struct is a GLframebuffer struct. If this weren't true, we'd
728 * need a pointer from the GLframebuffer to the XMesaBuffer.
730 XMesaBuffer xmBuffer
= (XMesaBuffer
) buffer
;
732 xmBuffer
->width
= buffer
->Width
;
733 xmBuffer
->height
= buffer
->Height
;
734 xmesa_alloc_back_buffer( xmBuffer
);
736 /* Needed by FLIP macro */
737 xmBuffer
->bottom
= height
- 1;
739 if (xmBuffer
->backimage
) {
740 /* Needed by PIXELADDR1 macro */
741 xmBuffer
->ximage_width1
= xmBuffer
->backimage
->bytes_per_line
;
742 xmBuffer
->ximage_origin1
= (GLubyte
*) xmBuffer
->backimage
->data
743 + xmBuffer
->ximage_width1
* (height
-1);
745 /* Needed by PIXELADDR2 macro */
746 xmBuffer
->ximage_width2
= xmBuffer
->backimage
->bytes_per_line
/ 2;
747 xmBuffer
->ximage_origin2
= (GLushort
*) xmBuffer
->backimage
->data
748 + xmBuffer
->ximage_width2
* (height
-1);
750 /* Needed by PIXELADDR3 macro */
751 xmBuffer
->ximage_width3
= xmBuffer
->backimage
->bytes_per_line
;
752 xmBuffer
->ximage_origin3
= (GLubyte
*) xmBuffer
->backimage
->data
753 + xmBuffer
->ximage_width3
* (height
-1);
755 /* Needed by PIXELADDR4 macro */
756 xmBuffer
->ximage_width4
= xmBuffer
->backimage
->width
;
757 xmBuffer
->ximage_origin4
= (GLuint
*) xmBuffer
->backimage
->data
758 + xmBuffer
->ximage_width4
* (height
-1);
761 _swrast_alloc_buffers( buffer
);
766 * This function implements glDrawPixels() with an XPutImage call when
767 * drawing to the front buffer (X Window drawable).
768 * The image format must be GL_BGRA to match the PF_8R8G8B pixel format.
771 xmesa_DrawPixels_8R8G8B( GLcontext
*ctx
,
772 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
773 GLenum format
, GLenum type
,
774 const struct gl_pixelstore_attrib
*unpack
,
775 const GLvoid
*pixels
)
777 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
778 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
779 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
780 const XMesaDrawable buffer
= xmesa
->xm_draw_buffer
->buffer
;
781 const XMesaGC gc
= xmesa
->xm_draw_buffer
->gc
;
785 ASSERT(xmesa
->xm_visual
->dithered_pf
== PF_8R8G8B
);
786 ASSERT(xmesa
->xm_visual
->undithered_pf
== PF_8R8G8B
);
788 if (buffer
&& /* buffer != 0 means it's a Window or Pixmap */
790 type
== GL_UNSIGNED_BYTE
&&
791 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
792 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
793 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
794 ctx
->Pixel
.ZoomY
== 1.0) {
799 int srcX
= unpack
->SkipPixels
;
800 int srcY
= unpack
->SkipRows
;
801 int rowLength
= unpack
->RowLength
? unpack
->RowLength
: width
;
802 if (_swrast_clip_pixelrect(ctx
, &dstX
, &dstY
, &w
, &h
, &srcX
, &srcY
)) {
803 /* This is a little tricky since all coordinates up to now have
804 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
805 * so we have to carefully compute the Y coordinates/addresses here.
808 MEMSET(&ximage
, 0, sizeof(XMesaImage
));
809 ximage
.width
= width
;
810 ximage
.height
= height
;
811 ximage
.format
= ZPixmap
;
812 ximage
.data
= (char *) pixels
813 + ((srcY
+ h
- 1) * rowLength
+ srcX
) * 4;
814 ximage
.byte_order
= LSBFirst
;
815 ximage
.bitmap_unit
= 32;
816 ximage
.bitmap_bit_order
= LSBFirst
;
817 ximage
.bitmap_pad
= 32;
819 ximage
.bytes_per_line
= -rowLength
* 4; /* negative to flip image */
820 ximage
.bits_per_pixel
= 32;
821 ximage
.red_mask
= 0xff0000;
822 ximage
.green_mask
= 0x00ff00;
823 ximage
.blue_mask
= 0x0000ff;
824 /* flip Y axis for dest position */
825 dstY
= FLIP(xmesa
->xm_draw_buffer
, dstY
) - h
+ 1;
826 XPutImage(dpy
, buffer
, gc
, &ximage
, 0, 0, dstX
, dstY
, w
, h
);
830 /* software fallback */
831 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
832 format
, type
, unpack
, pixels
);
839 * This function implements glDrawPixels() with an XPutImage call when
840 * drawing to the front buffer (X Window drawable). The image format
841 * must be GL_RGB and image type must be GL_UNSIGNED_SHORT_5_6_5 to
842 * match the PF_5R6G5B pixel format.
845 xmesa_DrawPixels_5R6G5B( GLcontext
*ctx
,
846 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
847 GLenum format
, GLenum type
,
848 const struct gl_pixelstore_attrib
*unpack
,
849 const GLvoid
*pixels
)
851 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
852 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
853 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
854 const XMesaDrawable buffer
= xmesa
->xm_draw_buffer
->buffer
;
855 const XMesaGC gc
= xmesa
->xm_draw_buffer
->gc
;
859 ASSERT(xmesa
->xm_visual
->undithered_pf
== PF_5R6G5B
);
861 if (buffer
&& /* buffer != 0 means it's a Window or Pixmap */
863 type
== GL_UNSIGNED_SHORT_5_6_5
&&
864 !ctx
->Color
.DitherFlag
&& /* no dithering */
865 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
866 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
867 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
868 ctx
->Pixel
.ZoomY
== 1.0) {
873 int srcX
= unpack
->SkipPixels
;
874 int srcY
= unpack
->SkipRows
;
875 int rowLength
= unpack
->RowLength
? unpack
->RowLength
: width
;
876 if (_swrast_clip_pixelrect(ctx
, &dstX
, &dstY
, &w
, &h
, &srcX
, &srcY
)) {
877 /* This is a little tricky since all coordinates up to now have
878 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
879 * so we have to carefully compute the Y coordinates/addresses here.
882 MEMSET(&ximage
, 0, sizeof(XMesaImage
));
883 ximage
.width
= width
;
884 ximage
.height
= height
;
885 ximage
.format
= ZPixmap
;
886 ximage
.data
= (char *) pixels
887 + ((srcY
+ h
- 1) * rowLength
+ srcX
) * 2;
888 ximage
.byte_order
= LSBFirst
;
889 ximage
.bitmap_unit
= 32;
890 ximage
.bitmap_bit_order
= LSBFirst
;
891 ximage
.bitmap_pad
= 32;
893 ximage
.bytes_per_line
= -rowLength
* 2; /* negative to flip image */
894 ximage
.bits_per_pixel
= 16;
895 ximage
.red_mask
= 0xff0000;
896 ximage
.green_mask
= 0x00ff00;
897 ximage
.blue_mask
= 0x0000ff;
898 /* flip Y axis for dest position */
899 dstY
= FLIP(xmesa
->xm_draw_buffer
, dstY
) - h
+ 1;
900 XPutImage(dpy
, buffer
, gc
, &ximage
, 0, 0, dstX
, dstY
, w
, h
);
904 /* software fallback */
905 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
906 format
, type
, unpack
, pixels
);
913 * Implement glCopyPixels for the front color buffer (or back buffer Pixmap)
914 * for the color buffer. Don't support zooming, pixel transfer, etc.
915 * We do support copying from one window to another, ala glXMakeCurrentRead.
918 xmesa_CopyPixels( GLcontext
*ctx
,
919 GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
920 GLint destx
, GLint desty
, GLenum type
)
922 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
923 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
924 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
925 const XMesaDrawable drawBuffer
= xmesa
->xm_draw_buffer
->buffer
;
926 const XMesaDrawable readBuffer
= xmesa
->xm_read_buffer
->buffer
;
927 const XMesaGC gc
= xmesa
->xm_draw_buffer
->gc
;
932 if (drawBuffer
&& /* buffer != 0 means it's a Window or Pixmap */
935 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
936 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
937 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
938 ctx
->Pixel
.ZoomY
== 1.0) {
939 /* Note: we don't do any special clipping work here. We could,
940 * but X will do it for us.
942 srcy
= FLIP(xmesa
->xm_read_buffer
, srcy
) - height
+ 1;
943 desty
= FLIP(xmesa
->xm_draw_buffer
, desty
) - height
+ 1;
944 XCopyArea(dpy
, readBuffer
, drawBuffer
, gc
,
945 srcx
, srcy
, width
, height
, destx
, desty
);
948 _swrast_CopyPixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
, type
);
955 * Every driver should implement a GetString function in order to
956 * return a meaningful GL_RENDERER string.
958 static const GLubyte
*
959 get_string( GLcontext
*ctx
, GLenum name
)
965 return (const GLubyte
*) "Mesa GLX Indirect";
967 return (const GLubyte
*) "Mesa X11";
971 return (const GLubyte
*) "Mesa project: www.mesa3d.org";
982 * We implement the glEnable function only because we care about
983 * dither enable/disable.
986 enable( GLcontext
*ctx
, GLenum pname
, GLboolean state
)
988 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
993 xmesa
->pixelformat
= xmesa
->xm_visual
->dithered_pf
;
995 xmesa
->pixelformat
= xmesa
->xm_visual
->undithered_pf
;
998 ; /* silence compiler warning */
1003 void xmesa_update_state( GLcontext
*ctx
, GLuint new_state
)
1005 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
1007 /* Propogate statechange information to swrast and swrast_setup
1008 * modules. The X11 driver has no internal GL-dependent state.
1010 _swrast_InvalidateState( ctx
, new_state
);
1011 _ac_InvalidateState( ctx
, new_state
);
1012 _tnl_InvalidateState( ctx
, new_state
);
1013 _swsetup_InvalidateState( ctx
, new_state
);
1016 /* setup pointers to front and back buffer clear functions */
1017 xmesa
->xm_draw_buffer
->front_clear_func
= clear_front_pixmap
;
1018 if (xmesa
->xm_draw_buffer
->backpixmap
!= XIMAGE
) {
1019 xmesa
->xm_draw_buffer
->back_clear_func
= clear_back_pixmap
;
1021 else if (sizeof(GLushort
)!=2 || sizeof(GLuint
)!=4) {
1022 xmesa
->xm_draw_buffer
->back_clear_func
= clear_nbit_ximage
;
1024 else switch (xmesa
->xm_visual
->BitsPerPixel
) {
1026 if (xmesa
->xm_visual
->hpcr_clear_flag
) {
1027 xmesa
->xm_draw_buffer
->back_clear_func
= clear_HPCR_ximage
;
1030 xmesa
->xm_draw_buffer
->back_clear_func
= clear_8bit_ximage
;
1034 xmesa
->xm_draw_buffer
->back_clear_func
= clear_16bit_ximage
;
1037 xmesa
->xm_draw_buffer
->back_clear_func
= clear_24bit_ximage
;
1040 xmesa
->xm_draw_buffer
->back_clear_func
= clear_32bit_ximage
;
1043 xmesa
->xm_draw_buffer
->back_clear_func
= clear_nbit_ximage
;
1047 xmesa_update_span_funcs(ctx
);
1053 * Called via ctx->Driver.TestProxyTeximage(). Normally, we'd just use
1054 * the _mesa_test_proxy_teximage() fallback function, but we're going to
1055 * special-case the 3D texture case to allow textures up to 512x512x32
1059 test_proxy_teximage(GLcontext
*ctx
, GLenum target
, GLint level
,
1060 GLint internalFormat
, GLenum format
, GLenum type
,
1061 GLint width
, GLint height
, GLint depth
, GLint border
)
1063 if (target
== GL_PROXY_TEXTURE_3D
) {
1064 /* special case for 3D textures */
1065 if (width
* height
* depth
> 512 * 512 * 64 ||
1066 width
< 2 * border
||
1067 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1068 _mesa_bitcount(width
- 2 * border
) != 1) ||
1069 height
< 2 * border
||
1070 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1071 _mesa_bitcount(height
- 2 * border
) != 1) ||
1072 depth
< 2 * border
||
1073 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1074 _mesa_bitcount(depth
- 2 * border
) != 1)) {
1075 /* Bad size, or too many texels */
1081 /* use the fallback routine for 1D, 2D, cube and rect targets */
1082 return _mesa_test_proxy_teximage(ctx
, target
, level
, internalFormat
,
1083 format
, type
, width
, height
, depth
,
1091 /* Setup pointers and other driver state that is constant for the life
1094 void xmesa_init_pointers( GLcontext
*ctx
)
1097 struct swrast_device_driver
*dd
= _swrast_GetDeviceDriverReference( ctx
);
1098 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
1100 /* Plug in our driver-specific functions here */
1101 ctx
->Driver
.GetString
= get_string
;
1102 ctx
->Driver
.GetBufferSize
= get_buffer_size
;
1103 ctx
->Driver
.Flush
= finish_or_flush
;
1104 ctx
->Driver
.Finish
= finish_or_flush
;
1105 ctx
->Driver
.ClearIndex
= clear_index
;
1106 ctx
->Driver
.ClearColor
= clear_color
;
1107 ctx
->Driver
.IndexMask
= index_mask
;
1108 ctx
->Driver
.ColorMask
= color_mask
;
1109 ctx
->Driver
.Enable
= enable
;
1111 /* Software rasterizer pixel paths:
1113 ctx
->Driver
.Accum
= _swrast_Accum
;
1114 ctx
->Driver
.Bitmap
= _swrast_Bitmap
;
1115 ctx
->Driver
.Clear
= clear_buffers
;
1116 ctx
->Driver
.ResizeBuffers
= xmesa_resize_buffers
;
1117 ctx
->Driver
.CopyPixels
= xmesa_CopyPixels
;
1118 if (xmesa
->xm_visual
->undithered_pf
== PF_8R8G8B
&&
1119 xmesa
->xm_visual
->dithered_pf
== PF_8R8G8B
) {
1120 ctx
->Driver
.DrawPixels
= xmesa_DrawPixels_8R8G8B
;
1122 else if (xmesa
->xm_visual
->undithered_pf
== PF_5R6G5B
) {
1123 ctx
->Driver
.DrawPixels
= xmesa_DrawPixels_5R6G5B
;
1126 ctx
->Driver
.DrawPixels
= _swrast_DrawPixels
;
1128 ctx
->Driver
.ReadPixels
= _swrast_ReadPixels
;
1129 ctx
->Driver
.DrawBuffer
= _swrast_DrawBuffer
;
1131 /* Software texture functions:
1133 ctx
->Driver
.ChooseTextureFormat
= _mesa_choose_tex_format
;
1134 ctx
->Driver
.TexImage1D
= _mesa_store_teximage1d
;
1135 ctx
->Driver
.TexImage2D
= _mesa_store_teximage2d
;
1136 ctx
->Driver
.TexImage3D
= _mesa_store_teximage3d
;
1137 ctx
->Driver
.TexSubImage1D
= _mesa_store_texsubimage1d
;
1138 ctx
->Driver
.TexSubImage2D
= _mesa_store_texsubimage2d
;
1139 ctx
->Driver
.TexSubImage3D
= _mesa_store_texsubimage3d
;
1140 ctx
->Driver
.TestProxyTexImage
= test_proxy_teximage
;
1142 ctx
->Driver
.CopyTexImage1D
= _swrast_copy_teximage1d
;
1143 ctx
->Driver
.CopyTexImage2D
= _swrast_copy_teximage2d
;
1144 ctx
->Driver
.CopyTexSubImage1D
= _swrast_copy_texsubimage1d
;
1145 ctx
->Driver
.CopyTexSubImage2D
= _swrast_copy_texsubimage2d
;
1146 ctx
->Driver
.CopyTexSubImage3D
= _swrast_copy_texsubimage3d
;
1148 ctx
->Driver
.CompressedTexImage1D
= _mesa_store_compressed_teximage1d
;
1149 ctx
->Driver
.CompressedTexImage2D
= _mesa_store_compressed_teximage2d
;
1150 ctx
->Driver
.CompressedTexImage3D
= _mesa_store_compressed_teximage3d
;
1151 ctx
->Driver
.CompressedTexSubImage1D
= _mesa_store_compressed_texsubimage1d
;
1152 ctx
->Driver
.CompressedTexSubImage2D
= _mesa_store_compressed_texsubimage2d
;
1153 ctx
->Driver
.CompressedTexSubImage3D
= _mesa_store_compressed_texsubimage3d
;
1155 /* Swrast hooks for imaging extensions:
1157 ctx
->Driver
.CopyColorTable
= _swrast_CopyColorTable
;
1158 ctx
->Driver
.CopyColorSubTable
= _swrast_CopyColorSubTable
;
1159 ctx
->Driver
.CopyConvolutionFilter1D
= _swrast_CopyConvolutionFilter1D
;
1160 ctx
->Driver
.CopyConvolutionFilter2D
= _swrast_CopyConvolutionFilter2D
;
1162 /* Initialize the TNL driver interface:
1164 tnl
= TNL_CONTEXT(ctx
);
1165 tnl
->Driver
.RunPipeline
= _tnl_run_pipeline
;
1167 dd
->SetBuffer
= set_buffer
;
1169 /* Install swsetup for tnl->Driver.Render.*:
1171 _swsetup_Wakeup(ctx
);
1173 (void) DitherValues
; /* silenced unused var warning */
1180 #define XMESA_NEW_POINT (_NEW_POINT | \
1182 _SWRAST_NEW_RASTERMASK)
1184 #define XMESA_NEW_LINE (_NEW_LINE | \
1189 _SWRAST_NEW_RASTERMASK)
1191 #define XMESA_NEW_TRIANGLE (_NEW_POLYGON | \
1196 _SWRAST_NEW_RASTERMASK)
1199 /* Extend the software rasterizer with our line/point/triangle
1202 void xmesa_register_swrast_functions( GLcontext
*ctx
)
1204 SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
1206 swrast
->choose_point
= xmesa_choose_point
;
1207 swrast
->choose_line
= xmesa_choose_line
;
1208 swrast
->choose_triangle
= xmesa_choose_triangle
;
1210 swrast
->invalidate_point
|= XMESA_NEW_POINT
;
1211 swrast
->invalidate_line
|= XMESA_NEW_LINE
;
1212 swrast
->invalidate_triangle
|= XMESA_NEW_TRIANGLE
;