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 pixmap */
141 target
->buffer
= (XMesaDrawable
) target
->backpixmap
;
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 #if 0 /* See below */
417 register GLuint clearPixel
;
418 if (xmesa
->swapbytes
) {
419 clearPixel
= (b
<< 16) | (g
<< 8) | r
;
422 clearPixel
= (r
<< 16) | (g
<< 8) | b
;
428 /* same value for all three components (gray) */
429 const GLint w3
= xmesa
->xm_draw_buffer
->width
* 3;
430 const GLint h
= xmesa
->xm_draw_buffer
->height
;
432 for (i
= 0; i
< h
; i
++) {
433 bgr_t
*ptr3
= PIXELADDR3(xmesa
->xm_draw_buffer
, 0, i
);
439 const GLint w
= xmesa
->xm_draw_buffer
->width
;
440 const GLint h
= xmesa
->xm_draw_buffer
->height
;
442 for (i
= 0; i
< h
; i
++) {
443 bgr_t
*ptr3
= PIXELADDR3(xmesa
->xm_draw_buffer
, 0, i
);
444 for (j
= 0; j
< w
; j
++) {
451 #if 0 /* this code doesn't work for all window widths */
452 register GLuint
*ptr4
= (GLuint
*) ptr3
;
455 register GLuint
*p
= pixel4
;
456 pixel4
[0] = clearPixel
| (clearPixel
<< 24);
457 pixel4
[1] = (clearPixel
<< 16) | (clearPixel
>> 8);
458 pixel4
[2] = (clearPixel
<< 8) | (clearPixel
>> 16);
459 switch (3 & (int)(ptr3
- (bgr_t
*) ptr4
)){
463 px
= *ptr4
& 0x00ffffff;
464 px
|= pixel4
[0] & 0xff000000;
466 px
= *ptr4
& 0xffff0000;
467 px
|= pixel4
[2] & 0x0000ffff;
472 px
= *ptr4
& 0x0000fffff;
473 px
|= pixel4
[1] & 0xffff0000;
475 px
= *ptr4
& 0xffffff00;
476 px
|= pixel4
[2] & 0x000000ff;
481 px
= *ptr4
& 0x000000ff;
482 px
|= pixel4
[2] & 0xffffff00;
499 px
= *ptr4
& 0xffffff00;
500 px
|= clearPixel
& 0xff;
506 px
= *ptr4
& 0xffff0000;
511 px
= *ptr4
& 0xff000000;
522 /* only clear subrect of color buffer */
524 /* same value for all three components (gray) */
526 for (j
=0;j
<height
;j
++) {
527 bgr_t
*ptr3
= PIXELADDR3( xmesa
->xm_draw_buffer
, x
, y
+j
);
528 MEMSET(ptr3
, r
, 3 * width
);
532 /* non-gray clear color */
534 for (j
= 0; j
< height
; j
++) {
535 bgr_t
*ptr3
= PIXELADDR3( xmesa
->xm_draw_buffer
, x
, y
+j
);
536 for (i
= 0; i
< width
; i
++) {
543 #if 0 /* this code might not always (seems ptr3 always == ptr4) */
546 pixel4
[0] = clearPixel
| (clearPixel
<< 24);
547 pixel4
[1] = (clearPixel
<< 16) | (clearPixel
>> 8);
548 pixel4
[2] = (clearPixel
<< 8) | (clearPixel
>> 16);
549 for (j
=0;j
<height
;j
++) {
550 bgr_t
*ptr3
= PIXELADDR3( xmesa
->xm_draw_buffer
, x
, y
+j
);
551 register GLuint
*ptr4
= (GLuint
*)ptr3
;
552 register GLuint
*p
, px
;
554 switch (3 & (int)(ptr3
- (bgr_t
*) ptr4
)){
558 px
= *ptr4
& 0x00ffffff;
559 px
|= pixel4
[0] & 0xff000000;
561 px
= *ptr4
& 0xffff0000;
562 px
|= pixel4
[2] & 0x0000ffff;
567 px
= *ptr4
& 0x0000fffff;
568 px
|= pixel4
[1] & 0xffff0000;
570 px
= *ptr4
& 0xffffff00;
571 px
|= pixel4
[2] & 0x000000ff;
576 px
= *ptr4
& 0x000000ff;
577 px
|= pixel4
[2] & 0xffffff00;
594 px
= *ptr4
& 0xffffff00;
601 px
= *ptr4
& 0xffff0000;
606 px
= *ptr4
& 0xff000000;
607 px
|= pixel4
[0] & 0xffffff;
621 clear_32bit_ximage( GLcontext
*ctx
, GLboolean all
,
622 GLint x
, GLint y
, GLint width
, GLint height
)
624 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
625 register GLuint pixel
= (GLuint
) xmesa
->clearpixel
;
626 if (xmesa
->swapbytes
) {
627 pixel
= ((pixel
>> 24) & 0x000000ff)
628 | ((pixel
>> 8) & 0x0000ff00)
629 | ((pixel
<< 8) & 0x00ff0000)
630 | ((pixel
<< 24) & 0xff000000);
633 register GLint n
= xmesa
->xm_draw_buffer
->width
* xmesa
->xm_draw_buffer
->height
;
634 register GLuint
*ptr4
= (GLuint
*) xmesa
->xm_draw_buffer
->backimage
->data
;
636 MEMSET( ptr4
, pixel
, 4*n
);
647 for (j
=0;j
<height
;j
++) {
648 register GLuint
*ptr4
= PIXELADDR4( xmesa
->xm_draw_buffer
, x
, y
+j
);
649 for (i
=0;i
<width
;i
++) {
658 clear_nbit_ximage( GLcontext
*ctx
, GLboolean all
,
659 GLint x
, GLint y
, GLint width
, GLint height
)
661 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
662 XMesaImage
*img
= xmesa
->xm_draw_buffer
->backimage
;
665 width
= xmesa
->xm_draw_buffer
->width
;
666 height
= xmesa
->xm_draw_buffer
->height
;
667 for (j
=0;j
<height
;j
++) {
668 for (i
=0;i
<width
;i
++) {
669 XMesaPutPixel( img
, i
, j
, xmesa
->clearpixel
);
674 /* TODO: optimize this */
676 y
= FLIP(xmesa
->xm_draw_buffer
, y
);
677 for (j
=0;j
<height
;j
++) {
678 for (i
=0;i
<width
;i
++) {
679 XMesaPutPixel( img
, x
+i
, y
-j
, xmesa
->clearpixel
);
688 clear_buffers( GLcontext
*ctx
, GLbitfield mask
,
689 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
691 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
692 const GLuint
*colorMask
= (GLuint
*) &ctx
->Color
.ColorMask
;
694 if ((mask
& (DD_FRONT_LEFT_BIT
| DD_BACK_LEFT_BIT
)) &&
695 xmesa
->xm_draw_buffer
->mesa_buffer
.UseSoftwareAlphaBuffers
&&
696 ctx
->Color
.ColorMask
[ACOMP
]) {
697 _swrast_clear_alpha_buffers(ctx
);
700 /* we can't handle color or index masking */
701 if (*colorMask
== 0xffffffff && ctx
->Color
.IndexMask
== 0xffffffff) {
702 if (mask
& DD_FRONT_LEFT_BIT
) {
703 ASSERT(xmesa
->xm_draw_buffer
->front_clear_func
);
704 (*xmesa
->xm_draw_buffer
->front_clear_func
)( ctx
, all
, x
, y
, width
, height
);
705 mask
&= ~DD_FRONT_LEFT_BIT
;
707 if (mask
& DD_BACK_LEFT_BIT
) {
708 ASSERT(xmesa
->xm_draw_buffer
->back_clear_func
);
709 (*xmesa
->xm_draw_buffer
->back_clear_func
)( ctx
, all
, x
, y
, width
, height
);
710 mask
&= ~DD_BACK_LEFT_BIT
;
715 _swrast_Clear( ctx
, mask
, all
, x
, y
, width
, height
);
720 * When we detect that the user has resized the window this function will
721 * get called. Here we'll reallocate the back buffer, depth buffer,
722 * stencil buffer etc. to match the new window size.
725 xmesa_resize_buffers( GLframebuffer
*buffer
)
727 int height
= (int) buffer
->Height
;
728 /* We can do this cast because the first field in the XMesaBuffer
729 * struct is a GLframebuffer struct. If this weren't true, we'd
730 * need a pointer from the GLframebuffer to the XMesaBuffer.
732 XMesaBuffer xmBuffer
= (XMesaBuffer
) buffer
;
734 xmBuffer
->width
= buffer
->Width
;
735 xmBuffer
->height
= buffer
->Height
;
736 xmesa_alloc_back_buffer( xmBuffer
);
738 /* Needed by FLIP macro */
739 xmBuffer
->bottom
= height
- 1;
741 if (xmBuffer
->backimage
) {
742 /* Needed by PIXELADDR1 macro */
743 xmBuffer
->ximage_width1
= xmBuffer
->backimage
->bytes_per_line
;
744 xmBuffer
->ximage_origin1
= (GLubyte
*) xmBuffer
->backimage
->data
745 + xmBuffer
->ximage_width1
* (height
-1);
747 /* Needed by PIXELADDR2 macro */
748 xmBuffer
->ximage_width2
= xmBuffer
->backimage
->bytes_per_line
/ 2;
749 xmBuffer
->ximage_origin2
= (GLushort
*) xmBuffer
->backimage
->data
750 + xmBuffer
->ximage_width2
* (height
-1);
752 /* Needed by PIXELADDR3 macro */
753 xmBuffer
->ximage_width3
= xmBuffer
->backimage
->bytes_per_line
;
754 xmBuffer
->ximage_origin3
= (GLubyte
*) xmBuffer
->backimage
->data
755 + xmBuffer
->ximage_width3
* (height
-1);
757 /* Needed by PIXELADDR4 macro */
758 xmBuffer
->ximage_width4
= xmBuffer
->backimage
->width
;
759 xmBuffer
->ximage_origin4
= (GLuint
*) xmBuffer
->backimage
->data
760 + xmBuffer
->ximage_width4
* (height
-1);
763 _swrast_alloc_buffers( buffer
);
767 #ifndef XFree86Server
768 /* XXX this was never tested in the Xserver environment */
771 * This function implements glDrawPixels() with an XPutImage call when
772 * drawing to the front buffer (X Window drawable).
773 * The image format must be GL_BGRA to match the PF_8R8G8B pixel format.
776 xmesa_DrawPixels_8R8G8B( GLcontext
*ctx
,
777 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
778 GLenum format
, GLenum type
,
779 const struct gl_pixelstore_attrib
*unpack
,
780 const GLvoid
*pixels
)
782 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
783 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
784 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
785 const XMesaDrawable buffer
= xmesa
->xm_draw_buffer
->buffer
;
786 const XMesaGC gc
= xmesa
->xm_draw_buffer
->gc
;
790 ASSERT(xmesa
->xm_visual
->dithered_pf
== PF_8R8G8B
);
791 ASSERT(xmesa
->xm_visual
->undithered_pf
== PF_8R8G8B
);
793 if (buffer
&& /* buffer != 0 means it's a Window or Pixmap */
795 type
== GL_UNSIGNED_BYTE
&&
796 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
797 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
798 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
799 ctx
->Pixel
.ZoomY
== 1.0) {
804 int srcX
= unpack
->SkipPixels
;
805 int srcY
= unpack
->SkipRows
;
806 int rowLength
= unpack
->RowLength
? unpack
->RowLength
: width
;
807 if (_swrast_clip_pixelrect(ctx
, &dstX
, &dstY
, &w
, &h
, &srcX
, &srcY
)) {
808 /* This is a little tricky since all coordinates up to now have
809 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
810 * so we have to carefully compute the Y coordinates/addresses here.
813 MEMSET(&ximage
, 0, sizeof(XMesaImage
));
814 ximage
.width
= width
;
815 ximage
.height
= height
;
816 ximage
.format
= ZPixmap
;
817 ximage
.data
= (char *) pixels
818 + ((srcY
+ h
- 1) * rowLength
+ srcX
) * 4;
819 ximage
.byte_order
= LSBFirst
;
820 ximage
.bitmap_unit
= 32;
821 ximage
.bitmap_bit_order
= LSBFirst
;
822 ximage
.bitmap_pad
= 32;
824 ximage
.bytes_per_line
= -rowLength
* 4; /* negative to flip image */
825 ximage
.bits_per_pixel
= 32;
826 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
827 /* flip Y axis for dest position */
828 dstY
= FLIP(xmesa
->xm_draw_buffer
, dstY
) - h
+ 1;
829 XPutImage(dpy
, buffer
, gc
, &ximage
, 0, 0, dstX
, dstY
, w
, h
);
833 /* software fallback */
834 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
835 format
, type
, unpack
, pixels
);
842 * This function implements glDrawPixels() with an XPutImage call when
843 * drawing to the front buffer (X Window drawable). The image format
844 * must be GL_RGB and image type must be GL_UNSIGNED_SHORT_5_6_5 to
845 * match the PF_5R6G5B pixel format.
848 xmesa_DrawPixels_5R6G5B( GLcontext
*ctx
,
849 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
850 GLenum format
, GLenum type
,
851 const struct gl_pixelstore_attrib
*unpack
,
852 const GLvoid
*pixels
)
854 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
855 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
856 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
857 const XMesaDrawable buffer
= xmesa
->xm_draw_buffer
->buffer
;
858 const XMesaGC gc
= xmesa
->xm_draw_buffer
->gc
;
862 ASSERT(xmesa
->xm_visual
->undithered_pf
== PF_5R6G5B
);
864 if (buffer
&& /* buffer != 0 means it's a Window or Pixmap */
866 type
== GL_UNSIGNED_SHORT_5_6_5
&&
867 !ctx
->Color
.DitherFlag
&& /* no dithering */
868 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
869 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
870 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
871 ctx
->Pixel
.ZoomY
== 1.0) {
876 int srcX
= unpack
->SkipPixels
;
877 int srcY
= unpack
->SkipRows
;
878 int rowLength
= unpack
->RowLength
? unpack
->RowLength
: width
;
879 if (_swrast_clip_pixelrect(ctx
, &dstX
, &dstY
, &w
, &h
, &srcX
, &srcY
)) {
880 /* This is a little tricky since all coordinates up to now have
881 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
882 * so we have to carefully compute the Y coordinates/addresses here.
885 MEMSET(&ximage
, 0, sizeof(XMesaImage
));
886 ximage
.width
= width
;
887 ximage
.height
= height
;
888 ximage
.format
= ZPixmap
;
889 ximage
.data
= (char *) pixels
890 + ((srcY
+ h
- 1) * rowLength
+ srcX
) * 2;
891 ximage
.byte_order
= LSBFirst
;
892 ximage
.bitmap_unit
= 16;
893 ximage
.bitmap_bit_order
= LSBFirst
;
894 ximage
.bitmap_pad
= 16;
896 ximage
.bytes_per_line
= -rowLength
* 2; /* negative to flip image */
897 ximage
.bits_per_pixel
= 16;
898 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
899 /* flip Y axis for dest position */
900 dstY
= FLIP(xmesa
->xm_draw_buffer
, dstY
) - h
+ 1;
901 XPutImage(dpy
, buffer
, gc
, &ximage
, 0, 0, dstX
, dstY
, w
, h
);
905 /* software fallback */
906 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
907 format
, type
, unpack
, pixels
);
914 * Implement glCopyPixels for the front color buffer (or back buffer Pixmap)
915 * for the color buffer. Don't support zooming, pixel transfer, etc.
916 * We do support copying from one window to another, ala glXMakeCurrentRead.
919 xmesa_CopyPixels( GLcontext
*ctx
,
920 GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
921 GLint destx
, GLint desty
, GLenum type
)
923 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
924 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
925 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
926 const XMesaDrawable drawBuffer
= xmesa
->xm_draw_buffer
->buffer
;
927 const XMesaDrawable readBuffer
= xmesa
->xm_read_buffer
->buffer
;
928 const XMesaGC gc
= xmesa
->xm_draw_buffer
->gc
;
933 if (drawBuffer
&& /* buffer != 0 means it's a Window or Pixmap */
936 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
937 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
938 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
939 ctx
->Pixel
.ZoomY
== 1.0) {
940 /* Note: we don't do any special clipping work here. We could,
941 * but X will do it for us.
943 srcy
= FLIP(xmesa
->xm_read_buffer
, srcy
) - height
+ 1;
944 desty
= FLIP(xmesa
->xm_draw_buffer
, desty
) - height
+ 1;
945 XCopyArea(dpy
, readBuffer
, drawBuffer
, gc
,
946 srcx
, srcy
, width
, height
, destx
, desty
);
949 _swrast_CopyPixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
, type
);
952 #endif /* XFree86Server */
957 * Every driver should implement a GetString function in order to
958 * return a meaningful GL_RENDERER string.
960 static const GLubyte
*
961 get_string( GLcontext
*ctx
, GLenum name
)
967 return (const GLubyte
*) "Mesa GLX Indirect";
969 return (const GLubyte
*) "Mesa X11";
973 return (const GLubyte
*) "Mesa project: www.mesa3d.org";
984 * We implement the glEnable function only because we care about
985 * dither enable/disable.
988 enable( GLcontext
*ctx
, GLenum pname
, GLboolean state
)
990 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
995 xmesa
->pixelformat
= xmesa
->xm_visual
->dithered_pf
;
997 xmesa
->pixelformat
= xmesa
->xm_visual
->undithered_pf
;
1000 ; /* silence compiler warning */
1005 void xmesa_update_state( GLcontext
*ctx
, GLuint new_state
)
1007 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
1009 /* Propogate statechange information to swrast and swrast_setup
1010 * modules. The X11 driver has no internal GL-dependent state.
1012 _swrast_InvalidateState( ctx
, new_state
);
1013 _ac_InvalidateState( ctx
, new_state
);
1014 _tnl_InvalidateState( ctx
, new_state
);
1015 _swsetup_InvalidateState( ctx
, new_state
);
1018 /* setup pointers to front and back buffer clear functions */
1019 xmesa
->xm_draw_buffer
->front_clear_func
= clear_front_pixmap
;
1020 if (xmesa
->xm_draw_buffer
->backpixmap
!= XIMAGE
) {
1021 xmesa
->xm_draw_buffer
->back_clear_func
= clear_back_pixmap
;
1023 else if (sizeof(GLushort
)!=2 || sizeof(GLuint
)!=4) {
1024 xmesa
->xm_draw_buffer
->back_clear_func
= clear_nbit_ximage
;
1026 else switch (xmesa
->xm_visual
->BitsPerPixel
) {
1028 if (xmesa
->xm_visual
->hpcr_clear_flag
) {
1029 xmesa
->xm_draw_buffer
->back_clear_func
= clear_HPCR_ximage
;
1032 xmesa
->xm_draw_buffer
->back_clear_func
= clear_8bit_ximage
;
1036 xmesa
->xm_draw_buffer
->back_clear_func
= clear_16bit_ximage
;
1039 xmesa
->xm_draw_buffer
->back_clear_func
= clear_24bit_ximage
;
1042 xmesa
->xm_draw_buffer
->back_clear_func
= clear_32bit_ximage
;
1045 xmesa
->xm_draw_buffer
->back_clear_func
= clear_nbit_ximage
;
1049 xmesa_update_span_funcs(ctx
);
1055 * Called via ctx->Driver.TestProxyTeximage(). Normally, we'd just use
1056 * the _mesa_test_proxy_teximage() fallback function, but we're going to
1057 * special-case the 3D texture case to allow textures up to 512x512x32
1061 test_proxy_teximage(GLcontext
*ctx
, GLenum target
, GLint level
,
1062 GLint internalFormat
, GLenum format
, GLenum type
,
1063 GLint width
, GLint height
, GLint depth
, GLint border
)
1065 if (target
== GL_PROXY_TEXTURE_3D
) {
1066 /* special case for 3D textures */
1067 if (width
* height
* depth
> 512 * 512 * 64 ||
1068 width
< 2 * border
||
1069 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1070 _mesa_bitcount(width
- 2 * border
) != 1) ||
1071 height
< 2 * border
||
1072 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1073 _mesa_bitcount(height
- 2 * border
) != 1) ||
1074 depth
< 2 * border
||
1075 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1076 _mesa_bitcount(depth
- 2 * border
) != 1)) {
1077 /* Bad size, or too many texels */
1083 /* use the fallback routine for 1D, 2D, cube and rect targets */
1084 return _mesa_test_proxy_teximage(ctx
, target
, level
, internalFormat
,
1085 format
, type
, width
, height
, depth
,
1093 /* Setup pointers and other driver state that is constant for the life
1096 void xmesa_init_pointers( GLcontext
*ctx
)
1099 struct swrast_device_driver
*dd
= _swrast_GetDeviceDriverReference( ctx
);
1100 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
1102 /* Plug in our driver-specific functions here */
1103 ctx
->Driver
.GetString
= get_string
;
1104 ctx
->Driver
.GetBufferSize
= get_buffer_size
;
1105 ctx
->Driver
.Flush
= finish_or_flush
;
1106 ctx
->Driver
.Finish
= finish_or_flush
;
1107 ctx
->Driver
.ClearIndex
= clear_index
;
1108 ctx
->Driver
.ClearColor
= clear_color
;
1109 ctx
->Driver
.IndexMask
= index_mask
;
1110 ctx
->Driver
.ColorMask
= color_mask
;
1111 ctx
->Driver
.Enable
= enable
;
1113 /* Software rasterizer pixel paths:
1115 ctx
->Driver
.Accum
= _swrast_Accum
;
1116 ctx
->Driver
.Bitmap
= _swrast_Bitmap
;
1117 ctx
->Driver
.Clear
= clear_buffers
;
1118 ctx
->Driver
.ResizeBuffers
= xmesa_resize_buffers
;
1119 #ifdef XFree86Server
1120 ctx
->Driver
.DrawPixels
= _swrast_DrawPixels
;
1121 ctx
->Driver
.CopyPixels
= _swrast_CopyPixels
;
1123 ctx
->Driver
.CopyPixels
= xmesa_CopyPixels
;
1124 if (xmesa
->xm_visual
->undithered_pf
== PF_8R8G8B
&&
1125 xmesa
->xm_visual
->dithered_pf
== PF_8R8G8B
) {
1126 ctx
->Driver
.DrawPixels
= xmesa_DrawPixels_8R8G8B
;
1128 else if (xmesa
->xm_visual
->undithered_pf
== PF_5R6G5B
) {
1129 ctx
->Driver
.DrawPixels
= xmesa_DrawPixels_5R6G5B
;
1132 ctx
->Driver
.DrawPixels
= _swrast_DrawPixels
;
1135 ctx
->Driver
.ReadPixels
= _swrast_ReadPixels
;
1136 ctx
->Driver
.DrawBuffer
= _swrast_DrawBuffer
;
1138 /* Software texture functions:
1140 ctx
->Driver
.ChooseTextureFormat
= _mesa_choose_tex_format
;
1141 ctx
->Driver
.TexImage1D
= _mesa_store_teximage1d
;
1142 ctx
->Driver
.TexImage2D
= _mesa_store_teximage2d
;
1143 ctx
->Driver
.TexImage3D
= _mesa_store_teximage3d
;
1144 ctx
->Driver
.TexSubImage1D
= _mesa_store_texsubimage1d
;
1145 ctx
->Driver
.TexSubImage2D
= _mesa_store_texsubimage2d
;
1146 ctx
->Driver
.TexSubImage3D
= _mesa_store_texsubimage3d
;
1147 ctx
->Driver
.TestProxyTexImage
= test_proxy_teximage
;
1149 ctx
->Driver
.CopyTexImage1D
= _swrast_copy_teximage1d
;
1150 ctx
->Driver
.CopyTexImage2D
= _swrast_copy_teximage2d
;
1151 ctx
->Driver
.CopyTexSubImage1D
= _swrast_copy_texsubimage1d
;
1152 ctx
->Driver
.CopyTexSubImage2D
= _swrast_copy_texsubimage2d
;
1153 ctx
->Driver
.CopyTexSubImage3D
= _swrast_copy_texsubimage3d
;
1155 ctx
->Driver
.CompressedTexImage1D
= _mesa_store_compressed_teximage1d
;
1156 ctx
->Driver
.CompressedTexImage2D
= _mesa_store_compressed_teximage2d
;
1157 ctx
->Driver
.CompressedTexImage3D
= _mesa_store_compressed_teximage3d
;
1158 ctx
->Driver
.CompressedTexSubImage1D
= _mesa_store_compressed_texsubimage1d
;
1159 ctx
->Driver
.CompressedTexSubImage2D
= _mesa_store_compressed_texsubimage2d
;
1160 ctx
->Driver
.CompressedTexSubImage3D
= _mesa_store_compressed_texsubimage3d
;
1162 /* Swrast hooks for imaging extensions:
1164 ctx
->Driver
.CopyColorTable
= _swrast_CopyColorTable
;
1165 ctx
->Driver
.CopyColorSubTable
= _swrast_CopyColorSubTable
;
1166 ctx
->Driver
.CopyConvolutionFilter1D
= _swrast_CopyConvolutionFilter1D
;
1167 ctx
->Driver
.CopyConvolutionFilter2D
= _swrast_CopyConvolutionFilter2D
;
1169 /* Initialize the TNL driver interface:
1171 tnl
= TNL_CONTEXT(ctx
);
1172 tnl
->Driver
.RunPipeline
= _tnl_run_pipeline
;
1174 dd
->SetBuffer
= set_buffer
;
1176 /* Install swsetup for tnl->Driver.Render.*:
1178 _swsetup_Wakeup(ctx
);
1180 (void) DitherValues
; /* silenced unused var warning */
1187 #define XMESA_NEW_POINT (_NEW_POINT | \
1189 _SWRAST_NEW_RASTERMASK)
1191 #define XMESA_NEW_LINE (_NEW_LINE | \
1196 _SWRAST_NEW_RASTERMASK)
1198 #define XMESA_NEW_TRIANGLE (_NEW_POLYGON | \
1203 _SWRAST_NEW_RASTERMASK)
1206 /* Extend the software rasterizer with our line/point/triangle
1209 void xmesa_register_swrast_functions( GLcontext
*ctx
)
1211 SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
1213 swrast
->choose_point
= xmesa_choose_point
;
1214 swrast
->choose_line
= xmesa_choose_line
;
1215 swrast
->choose_triangle
= xmesa_choose_triangle
;
1217 swrast
->invalidate_point
|= XMESA_NEW_POINT
;
1218 swrast
->invalidate_line
|= XMESA_NEW_LINE
;
1219 swrast
->invalidate_triangle
|= XMESA_NEW_TRIANGLE
;