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"
53 * Return the size (width, height) of the X window for the given GLframebuffer.
54 * Output: width - width of buffer in pixels.
55 * height - height of buffer in pixels.
58 get_buffer_size( GLframebuffer
*buffer
, GLuint
*width
, GLuint
*height
)
60 /* We can do this cast because the first field in the XMesaBuffer
61 * struct is a GLframebuffer struct. If this weren't true, we'd
62 * need a pointer from the GLframebuffer to the XMesaBuffer.
64 const XMesaBuffer xmBuffer
= (XMesaBuffer
) buffer
;
65 unsigned int winwidth
, winheight
;
67 /* XFree86 GLX renderer */
68 if (xmBuffer
->frontbuffer
->width
> MAX_WIDTH
||
69 xmBuffer
->frontbuffer
->height
> MAX_HEIGHT
) {
70 winwidth
= buffer
->Width
;
71 winheight
= buffer
->Height
;
73 winwidth
= xmBuffer
->frontbuffer
->width
;
74 winheight
= xmBuffer
->frontbuffer
->height
;
81 _glthread_LOCK_MUTEX(_xmesa_lock
);
82 XSync(xmBuffer
->xm_visual
->display
, 0); /* added for Chromium */
83 XGetGeometry( xmBuffer
->xm_visual
->display
, xmBuffer
->frontbuffer
, &root
,
84 &winx
, &winy
, &winwidth
, &winheight
, &bw
, &d
);
85 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
88 (void)kernel8
; /* Muffle compiler */
96 finish_or_flush( GLcontext
*ctx
)
101 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
103 _glthread_LOCK_MUTEX(_xmesa_lock
);
104 XSync( xmesa
->display
, False
);
105 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
113 * This chooses the color buffer for reading and writing spans, points,
114 * lines, and triangles.
117 xmesa_set_buffer( GLcontext
*ctx
, GLframebuffer
*buffer
, GLuint bufferBit
)
119 /* We can make this cast since the XMesaBuffer wraps GLframebuffer.
120 * GLframebuffer is the first member in a XMesaBuffer struct.
122 XMesaBuffer target
= (XMesaBuffer
) buffer
;
123 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
125 /* This assignment tells the span/point/line/triangle functions
126 * which XMesaBuffer to use.
128 xmesa
->xm_buffer
= target
;
131 * Now determine front vs back color buffer.
133 if (bufferBit
== FRONT_LEFT_BIT
) {
134 target
->buffer
= target
->frontbuffer
;
136 else if (bufferBit
== BACK_LEFT_BIT
) {
137 ASSERT(target
->db_state
);
138 if (target
->backpixmap
) {
139 /* back buffer is a pixmap */
140 target
->buffer
= (XMesaDrawable
) target
->backpixmap
;
142 else if (target
->backimage
) {
143 /* back buffer is an XImage */
144 target
->buffer
= None
;
147 /* No back buffer!!!! Must be out of memory, use front buffer */
148 target
->buffer
= target
->frontbuffer
;
152 _mesa_problem(ctx
, "invalid buffer 0x%x in set_buffer() in xm_dd.c");
155 xmesa_update_span_funcs(ctx
);
161 clear_index( GLcontext
*ctx
, GLuint index
)
163 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
164 xmesa
->clearpixel
= (unsigned long) index
;
165 XMesaSetForeground( xmesa
->display
, xmesa
->xm_draw_buffer
->cleargc
,
166 (unsigned long) index
);
171 clear_color( GLcontext
*ctx
, const GLfloat color
[4] )
173 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
174 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[0], color
[0]);
175 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[1], color
[1]);
176 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[2], color
[2]);
177 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[3], color
[3]);
178 xmesa
->clearpixel
= xmesa_color_to_pixel( xmesa
,
179 xmesa
->clearcolor
[0],
180 xmesa
->clearcolor
[1],
181 xmesa
->clearcolor
[2],
182 xmesa
->clearcolor
[3],
183 xmesa
->xm_visual
->undithered_pf
);
184 _glthread_LOCK_MUTEX(_xmesa_lock
);
185 XMesaSetForeground( xmesa
->display
, xmesa
->xm_draw_buffer
->cleargc
,
187 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
192 /* Set index mask ala glIndexMask */
194 index_mask( GLcontext
*ctx
, GLuint mask
)
196 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
197 if (xmesa
->xm_draw_buffer
->buffer
!= XIMAGE
) {
199 if (mask
==0xffffffff) {
200 m
= ((unsigned long)~0L);
203 m
= (unsigned long) mask
;
205 XMesaSetPlaneMask( xmesa
->display
, xmesa
->xm_draw_buffer
->cleargc
, m
);
206 XMesaSetPlaneMask( xmesa
->display
, xmesa
->xm_draw_buffer
->gc
, m
);
211 /* Implements glColorMask() */
213 color_mask(GLcontext
*ctx
,
214 GLboolean rmask
, GLboolean gmask
, GLboolean bmask
, GLboolean amask
)
216 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
217 int xclass
= GET_VISUAL_CLASS(xmesa
->xm_visual
);
220 if (xclass
== TrueColor
|| xclass
== DirectColor
) {
222 if (rmask
&& gmask
&& bmask
) {
223 m
= ((unsigned long)~0L);
227 if (rmask
) m
|= GET_REDMASK(xmesa
->xm_visual
);
228 if (gmask
) m
|= GET_GREENMASK(xmesa
->xm_visual
);
229 if (bmask
) m
|= GET_BLUEMASK(xmesa
->xm_visual
);
231 XMesaSetPlaneMask( xmesa
->display
, xmesa
->xm_draw_buffer
->cleargc
, m
);
232 XMesaSetPlaneMask( xmesa
->display
, xmesa
->xm_draw_buffer
->gc
, m
);
238 /**********************************************************************/
239 /*** glClear implementations ***/
240 /**********************************************************************/
244 clear_front_pixmap( GLcontext
*ctx
, GLboolean all
,
245 GLint x
, GLint y
, GLint width
, GLint height
)
247 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
249 XMesaFillRectangle( xmesa
->display
, xmesa
->xm_draw_buffer
->frontbuffer
,
250 xmesa
->xm_draw_buffer
->cleargc
,
252 xmesa
->xm_draw_buffer
->width
+1,
253 xmesa
->xm_draw_buffer
->height
+1 );
256 XMesaFillRectangle( xmesa
->display
, xmesa
->xm_draw_buffer
->frontbuffer
,
257 xmesa
->xm_draw_buffer
->cleargc
,
258 x
, xmesa
->xm_draw_buffer
->height
- y
- height
,
265 clear_back_pixmap( GLcontext
*ctx
, GLboolean all
,
266 GLint x
, GLint y
, GLint width
, GLint height
)
268 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
270 XMesaFillRectangle( xmesa
->display
, xmesa
->xm_draw_buffer
->backpixmap
,
271 xmesa
->xm_draw_buffer
->cleargc
,
273 xmesa
->xm_draw_buffer
->width
+1,
274 xmesa
->xm_draw_buffer
->height
+1 );
277 XMesaFillRectangle( xmesa
->display
, xmesa
->xm_draw_buffer
->backpixmap
,
278 xmesa
->xm_draw_buffer
->cleargc
,
279 x
, xmesa
->xm_draw_buffer
->height
- y
- height
,
286 clear_8bit_ximage( GLcontext
*ctx
, GLboolean all
,
287 GLint x
, GLint y
, GLint width
, GLint height
)
289 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
291 size_t n
= xmesa
->xm_draw_buffer
->backimage
->bytes_per_line
292 * xmesa
->xm_draw_buffer
->backimage
->height
;
293 MEMSET( xmesa
->xm_draw_buffer
->backimage
->data
, xmesa
->clearpixel
, n
);
297 for (i
=0;i
<height
;i
++) {
298 GLubyte
*ptr
= PIXELADDR1( xmesa
->xm_draw_buffer
, x
, y
+i
);
299 MEMSET( ptr
, xmesa
->clearpixel
, width
);
306 clear_HPCR_ximage( GLcontext
*ctx
, GLboolean all
,
307 GLint x
, GLint y
, GLint width
, GLint height
)
309 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
311 GLint i
, c16
= (xmesa
->xm_draw_buffer
->backimage
->bytes_per_line
>>4)<<4;
312 GLubyte
*ptr
= (GLubyte
*)xmesa
->xm_draw_buffer
->backimage
->data
;
313 for (i
=0; i
<xmesa
->xm_draw_buffer
->backimage
->height
; i
++) {
315 GLubyte
*sptr
= xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0];
319 for (j
=0; j
<c16
; j
+=16) {
338 for (; j
<xmesa
->xm_draw_buffer
->backimage
->bytes_per_line
; j
++) {
346 for (i
=y
; i
<y
+height
; i
++) {
347 GLubyte
*ptr
= PIXELADDR1( xmesa
->xm_draw_buffer
, x
, i
);
349 GLubyte
*sptr
= xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0];
353 for (j
=x
; j
<x
+width
; j
++) {
363 clear_16bit_ximage( GLcontext
*ctx
, GLboolean all
,
364 GLint x
, GLint y
, GLint width
, GLint height
)
366 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
367 register GLuint pixel
= (GLuint
) xmesa
->clearpixel
;
368 if (xmesa
->swapbytes
) {
369 pixel
= ((pixel
>> 8) & 0x00ff) | ((pixel
<< 8) & 0xff00);
373 register GLuint
*ptr4
= (GLuint
*) xmesa
->xm_draw_buffer
->backimage
->data
;
374 if ((pixel
& 0xff) == ((pixel
>> 8) & 0xff)) {
375 /* low and high bytes are equal so use memset() */
376 n
= xmesa
->xm_draw_buffer
->backimage
->bytes_per_line
377 * xmesa
->xm_draw_buffer
->height
;
378 MEMSET( ptr4
, pixel
& 0xff, n
);
381 pixel
= pixel
| (pixel
<<16);
382 n
= xmesa
->xm_draw_buffer
->backimage
->bytes_per_line
383 * xmesa
->xm_draw_buffer
->height
/ 4;
389 if ((xmesa
->xm_draw_buffer
->backimage
->bytes_per_line
*
390 xmesa
->xm_draw_buffer
->height
) & 0x2)
391 *(GLushort
*)ptr4
= pixel
& 0xffff;
396 for (j
=0;j
<height
;j
++) {
397 register GLushort
*ptr2
= PIXELADDR2( xmesa
->xm_draw_buffer
, x
, y
+j
);
398 for (i
=0;i
<width
;i
++) {
406 /* Optimized code provided by Nozomi Ytow <noz@xfree86.org> */
408 clear_24bit_ximage( GLcontext
*ctx
, GLboolean all
,
409 GLint x
, GLint y
, GLint width
, GLint height
)
411 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
412 const GLubyte r
= xmesa
->clearcolor
[0];
413 const GLubyte g
= xmesa
->clearcolor
[1];
414 const GLubyte b
= xmesa
->clearcolor
[2];
415 #if 0 /* See below */
416 register GLuint clearPixel
;
417 if (xmesa
->swapbytes
) {
418 clearPixel
= (b
<< 16) | (g
<< 8) | r
;
421 clearPixel
= (r
<< 16) | (g
<< 8) | b
;
427 /* same value for all three components (gray) */
428 const GLint w3
= xmesa
->xm_draw_buffer
->width
* 3;
429 const GLint h
= xmesa
->xm_draw_buffer
->height
;
431 for (i
= 0; i
< h
; i
++) {
432 bgr_t
*ptr3
= PIXELADDR3(xmesa
->xm_draw_buffer
, 0, i
);
438 const GLint w
= xmesa
->xm_draw_buffer
->width
;
439 const GLint h
= xmesa
->xm_draw_buffer
->height
;
441 for (i
= 0; i
< h
; i
++) {
442 bgr_t
*ptr3
= PIXELADDR3(xmesa
->xm_draw_buffer
, 0, i
);
443 for (j
= 0; j
< w
; j
++) {
450 #if 0 /* this code doesn't work for all window widths */
451 register GLuint
*ptr4
= (GLuint
*) ptr3
;
454 register GLuint
*p
= pixel4
;
455 pixel4
[0] = clearPixel
| (clearPixel
<< 24);
456 pixel4
[1] = (clearPixel
<< 16) | (clearPixel
>> 8);
457 pixel4
[2] = (clearPixel
<< 8) | (clearPixel
>> 16);
458 switch (3 & (int)(ptr3
- (bgr_t
*) ptr4
)){
462 px
= *ptr4
& 0x00ffffff;
463 px
|= pixel4
[0] & 0xff000000;
465 px
= *ptr4
& 0xffff0000;
466 px
|= pixel4
[2] & 0x0000ffff;
471 px
= *ptr4
& 0x0000fffff;
472 px
|= pixel4
[1] & 0xffff0000;
474 px
= *ptr4
& 0xffffff00;
475 px
|= pixel4
[2] & 0x000000ff;
480 px
= *ptr4
& 0x000000ff;
481 px
|= pixel4
[2] & 0xffffff00;
498 px
= *ptr4
& 0xffffff00;
499 px
|= clearPixel
& 0xff;
505 px
= *ptr4
& 0xffff0000;
510 px
= *ptr4
& 0xff000000;
521 /* only clear subrect of color buffer */
523 /* same value for all three components (gray) */
525 for (j
=0;j
<height
;j
++) {
526 bgr_t
*ptr3
= PIXELADDR3( xmesa
->xm_draw_buffer
, x
, y
+j
);
527 MEMSET(ptr3
, r
, 3 * width
);
531 /* non-gray clear color */
533 for (j
= 0; j
< height
; j
++) {
534 bgr_t
*ptr3
= PIXELADDR3( xmesa
->xm_draw_buffer
, x
, y
+j
);
535 for (i
= 0; i
< width
; i
++) {
542 #if 0 /* this code might not always (seems ptr3 always == ptr4) */
545 pixel4
[0] = clearPixel
| (clearPixel
<< 24);
546 pixel4
[1] = (clearPixel
<< 16) | (clearPixel
>> 8);
547 pixel4
[2] = (clearPixel
<< 8) | (clearPixel
>> 16);
548 for (j
=0;j
<height
;j
++) {
549 bgr_t
*ptr3
= PIXELADDR3( xmesa
->xm_draw_buffer
, x
, y
+j
);
550 register GLuint
*ptr4
= (GLuint
*)ptr3
;
551 register GLuint
*p
, px
;
553 switch (3 & (int)(ptr3
- (bgr_t
*) ptr4
)){
557 px
= *ptr4
& 0x00ffffff;
558 px
|= pixel4
[0] & 0xff000000;
560 px
= *ptr4
& 0xffff0000;
561 px
|= pixel4
[2] & 0x0000ffff;
566 px
= *ptr4
& 0x0000fffff;
567 px
|= pixel4
[1] & 0xffff0000;
569 px
= *ptr4
& 0xffffff00;
570 px
|= pixel4
[2] & 0x000000ff;
575 px
= *ptr4
& 0x000000ff;
576 px
|= pixel4
[2] & 0xffffff00;
593 px
= *ptr4
& 0xffffff00;
600 px
= *ptr4
& 0xffff0000;
605 px
= *ptr4
& 0xff000000;
606 px
|= pixel4
[0] & 0xffffff;
620 clear_32bit_ximage( GLcontext
*ctx
, GLboolean all
,
621 GLint x
, GLint y
, GLint width
, GLint height
)
623 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
624 register GLuint pixel
= (GLuint
) xmesa
->clearpixel
;
625 if (xmesa
->swapbytes
) {
626 pixel
= ((pixel
>> 24) & 0x000000ff)
627 | ((pixel
>> 8) & 0x0000ff00)
628 | ((pixel
<< 8) & 0x00ff0000)
629 | ((pixel
<< 24) & 0xff000000);
632 register GLint n
= xmesa
->xm_draw_buffer
->width
* xmesa
->xm_draw_buffer
->height
;
633 register GLuint
*ptr4
= (GLuint
*) xmesa
->xm_draw_buffer
->backimage
->data
;
635 MEMSET( ptr4
, pixel
, 4*n
);
646 for (j
=0;j
<height
;j
++) {
647 register GLuint
*ptr4
= PIXELADDR4( xmesa
->xm_draw_buffer
, x
, y
+j
);
648 for (i
=0;i
<width
;i
++) {
657 clear_nbit_ximage( GLcontext
*ctx
, GLboolean all
,
658 GLint x
, GLint y
, GLint width
, GLint height
)
660 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
661 XMesaImage
*img
= xmesa
->xm_draw_buffer
->backimage
;
664 width
= xmesa
->xm_draw_buffer
->width
;
665 height
= xmesa
->xm_draw_buffer
->height
;
666 for (j
=0;j
<height
;j
++) {
667 for (i
=0;i
<width
;i
++) {
668 XMesaPutPixel( img
, i
, j
, xmesa
->clearpixel
);
673 /* TODO: optimize this */
675 y
= FLIP(xmesa
->xm_draw_buffer
, y
);
676 for (j
=0;j
<height
;j
++) {
677 for (i
=0;i
<width
;i
++) {
678 XMesaPutPixel( img
, x
+i
, y
-j
, xmesa
->clearpixel
);
687 clear_buffers( GLcontext
*ctx
, GLbitfield mask
,
688 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
690 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
691 const GLuint
*colorMask
= (GLuint
*) &ctx
->Color
.ColorMask
;
693 if ((mask
& (DD_FRONT_LEFT_BIT
| DD_BACK_LEFT_BIT
)) &&
694 xmesa
->xm_draw_buffer
->mesa_buffer
.UseSoftwareAlphaBuffers
&&
695 ctx
->Color
.ColorMask
[ACOMP
]) {
696 _swrast_clear_alpha_buffers(ctx
);
699 /* we can't handle color or index masking */
700 if (*colorMask
== 0xffffffff && ctx
->Color
.IndexMask
== 0xffffffff) {
701 if (mask
& DD_FRONT_LEFT_BIT
) {
702 ASSERT(xmesa
->xm_draw_buffer
->front_clear_func
);
703 (*xmesa
->xm_draw_buffer
->front_clear_func
)( ctx
, all
, x
, y
, width
, height
);
704 mask
&= ~DD_FRONT_LEFT_BIT
;
706 if (mask
& DD_BACK_LEFT_BIT
) {
707 ASSERT(xmesa
->xm_draw_buffer
->back_clear_func
);
708 (*xmesa
->xm_draw_buffer
->back_clear_func
)( ctx
, all
, x
, y
, width
, height
);
709 mask
&= ~DD_BACK_LEFT_BIT
;
714 _swrast_Clear( ctx
, mask
, all
, x
, y
, width
, height
);
719 * When we detect that the user has resized the window this function will
720 * get called. Here we'll reallocate the back buffer, depth buffer,
721 * stencil buffer etc. to match the new window size.
724 xmesa_resize_buffers( GLframebuffer
*buffer
)
726 int height
= (int) buffer
->Height
;
727 /* We can do this cast because the first field in the XMesaBuffer
728 * struct is a GLframebuffer struct. If this weren't true, we'd
729 * need a pointer from the GLframebuffer to the XMesaBuffer.
731 XMesaBuffer xmBuffer
= (XMesaBuffer
) buffer
;
733 xmBuffer
->width
= buffer
->Width
;
734 xmBuffer
->height
= buffer
->Height
;
735 xmesa_alloc_back_buffer( xmBuffer
);
737 /* Needed by FLIP macro */
738 xmBuffer
->bottom
= height
- 1;
740 if (xmBuffer
->backimage
) {
741 /* Needed by PIXELADDR1 macro */
742 xmBuffer
->ximage_width1
= xmBuffer
->backimage
->bytes_per_line
;
743 xmBuffer
->ximage_origin1
= (GLubyte
*) xmBuffer
->backimage
->data
744 + xmBuffer
->ximage_width1
* (height
-1);
746 /* Needed by PIXELADDR2 macro */
747 xmBuffer
->ximage_width2
= xmBuffer
->backimage
->bytes_per_line
/ 2;
748 xmBuffer
->ximage_origin2
= (GLushort
*) xmBuffer
->backimage
->data
749 + xmBuffer
->ximage_width2
* (height
-1);
751 /* Needed by PIXELADDR3 macro */
752 xmBuffer
->ximage_width3
= xmBuffer
->backimage
->bytes_per_line
;
753 xmBuffer
->ximage_origin3
= (GLubyte
*) xmBuffer
->backimage
->data
754 + xmBuffer
->ximage_width3
* (height
-1);
756 /* Needed by PIXELADDR4 macro */
757 xmBuffer
->ximage_width4
= xmBuffer
->backimage
->width
;
758 xmBuffer
->ximage_origin4
= (GLuint
*) xmBuffer
->backimage
->data
759 + xmBuffer
->ximage_width4
* (height
-1);
762 _swrast_alloc_buffers( buffer
);
766 #ifndef XFree86Server
767 /* XXX this was never tested in the Xserver environment */
770 * This function implements glDrawPixels() with an XPutImage call when
771 * drawing to the front buffer (X Window drawable).
772 * The image format must be GL_BGRA to match the PF_8R8G8B pixel format.
775 xmesa_DrawPixels_8R8G8B( GLcontext
*ctx
,
776 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
777 GLenum format
, GLenum type
,
778 const struct gl_pixelstore_attrib
*unpack
,
779 const GLvoid
*pixels
)
781 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
782 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
783 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
784 const XMesaDrawable buffer
= xmesa
->xm_draw_buffer
->buffer
;
785 const XMesaGC gc
= xmesa
->xm_draw_buffer
->gc
;
789 ASSERT(xmesa
->xm_visual
->dithered_pf
== PF_8R8G8B
);
790 ASSERT(xmesa
->xm_visual
->undithered_pf
== PF_8R8G8B
);
792 if (swrast
->NewState
)
793 _swrast_validate_derived( ctx
);
795 if (buffer
&& /* buffer != 0 means it's a Window or Pixmap */
797 type
== GL_UNSIGNED_BYTE
&&
798 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
799 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
800 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
801 ctx
->Pixel
.ZoomY
== 1.0) {
806 int srcX
= unpack
->SkipPixels
;
807 int srcY
= unpack
->SkipRows
;
808 int rowLength
= unpack
->RowLength
? unpack
->RowLength
: width
;
809 if (_swrast_clip_pixelrect(ctx
, &dstX
, &dstY
, &w
, &h
, &srcX
, &srcY
)) {
810 /* This is a little tricky since all coordinates up to now have
811 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
812 * so we have to carefully compute the Y coordinates/addresses here.
815 MEMSET(&ximage
, 0, sizeof(XMesaImage
));
816 ximage
.width
= width
;
817 ximage
.height
= height
;
818 ximage
.format
= ZPixmap
;
819 ximage
.data
= (char *) pixels
820 + ((srcY
+ h
- 1) * rowLength
+ srcX
) * 4;
821 ximage
.byte_order
= LSBFirst
;
822 ximage
.bitmap_unit
= 32;
823 ximage
.bitmap_bit_order
= LSBFirst
;
824 ximage
.bitmap_pad
= 32;
826 ximage
.bytes_per_line
= -rowLength
* 4; /* negative to flip image */
827 ximage
.bits_per_pixel
= 32;
828 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
829 /* flip Y axis for dest position */
830 dstY
= FLIP(xmesa
->xm_draw_buffer
, dstY
) - h
+ 1;
831 XPutImage(dpy
, buffer
, gc
, &ximage
, 0, 0, dstX
, dstY
, w
, h
);
835 /* software fallback */
836 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
837 format
, type
, unpack
, pixels
);
844 * This function implements glDrawPixels() with an XPutImage call when
845 * drawing to the front buffer (X Window drawable). The image format
846 * must be GL_RGB and image type must be GL_UNSIGNED_SHORT_5_6_5 to
847 * match the PF_5R6G5B pixel format.
850 xmesa_DrawPixels_5R6G5B( GLcontext
*ctx
,
851 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
852 GLenum format
, GLenum type
,
853 const struct gl_pixelstore_attrib
*unpack
,
854 const GLvoid
*pixels
)
856 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
857 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
858 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
859 const XMesaDrawable buffer
= xmesa
->xm_draw_buffer
->buffer
;
860 const XMesaGC gc
= xmesa
->xm_draw_buffer
->gc
;
864 ASSERT(xmesa
->xm_visual
->undithered_pf
== PF_5R6G5B
);
866 if (swrast
->NewState
)
867 _swrast_validate_derived( ctx
);
869 if (buffer
&& /* buffer != 0 means it's a Window or Pixmap */
871 type
== GL_UNSIGNED_SHORT_5_6_5
&&
872 !ctx
->Color
.DitherFlag
&& /* no dithering */
873 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
874 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
875 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
876 ctx
->Pixel
.ZoomY
== 1.0) {
881 int srcX
= unpack
->SkipPixels
;
882 int srcY
= unpack
->SkipRows
;
883 int rowLength
= unpack
->RowLength
? unpack
->RowLength
: width
;
884 if (_swrast_clip_pixelrect(ctx
, &dstX
, &dstY
, &w
, &h
, &srcX
, &srcY
)) {
885 /* This is a little tricky since all coordinates up to now have
886 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
887 * so we have to carefully compute the Y coordinates/addresses here.
890 MEMSET(&ximage
, 0, sizeof(XMesaImage
));
891 ximage
.width
= width
;
892 ximage
.height
= height
;
893 ximage
.format
= ZPixmap
;
894 ximage
.data
= (char *) pixels
895 + ((srcY
+ h
- 1) * rowLength
+ srcX
) * 2;
896 ximage
.byte_order
= LSBFirst
;
897 ximage
.bitmap_unit
= 16;
898 ximage
.bitmap_bit_order
= LSBFirst
;
899 ximage
.bitmap_pad
= 16;
901 ximage
.bytes_per_line
= -rowLength
* 2; /* negative to flip image */
902 ximage
.bits_per_pixel
= 16;
903 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
904 /* flip Y axis for dest position */
905 dstY
= FLIP(xmesa
->xm_draw_buffer
, dstY
) - h
+ 1;
906 XPutImage(dpy
, buffer
, gc
, &ximage
, 0, 0, dstX
, dstY
, w
, h
);
910 /* software fallback */
911 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
912 format
, type
, unpack
, pixels
);
919 * Implement glCopyPixels for the front color buffer (or back buffer Pixmap)
920 * for the color buffer. Don't support zooming, pixel transfer, etc.
921 * We do support copying from one window to another, ala glXMakeCurrentRead.
924 xmesa_CopyPixels( GLcontext
*ctx
,
925 GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
926 GLint destx
, GLint desty
, GLenum type
)
928 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
929 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
930 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
931 const XMesaDrawable drawBuffer
= xmesa
->xm_draw_buffer
->buffer
;
932 const XMesaDrawable readBuffer
= xmesa
->xm_read_buffer
->buffer
;
933 const XMesaGC gc
= xmesa
->xm_draw_buffer
->gc
;
938 if (swrast
->NewState
)
939 _swrast_validate_derived( ctx
);
941 if (ctx
->Color
.DrawBuffer
== GL_FRONT
&&
942 ctx
->Pixel
.ReadBuffer
== GL_FRONT
&&
943 drawBuffer
&& /* buffer != 0 means it's a Window or Pixmap */
946 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
947 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
948 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
949 ctx
->Pixel
.ZoomY
== 1.0) {
950 /* Note: we don't do any special clipping work here. We could,
951 * but X will do it for us.
953 srcy
= FLIP(xmesa
->xm_read_buffer
, srcy
) - height
+ 1;
954 desty
= FLIP(xmesa
->xm_draw_buffer
, desty
) - height
+ 1;
955 XCopyArea(dpy
, readBuffer
, drawBuffer
, gc
,
956 srcx
, srcy
, width
, height
, destx
, desty
);
959 _swrast_CopyPixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
, type
);
962 #endif /* XFree86Server */
967 * Every driver should implement a GetString function in order to
968 * return a meaningful GL_RENDERER string.
970 static const GLubyte
*
971 get_string( GLcontext
*ctx
, GLenum name
)
977 return (const GLubyte
*) "Mesa GLX Indirect";
979 return (const GLubyte
*) "Mesa X11";
983 return (const GLubyte
*) "Mesa project: www.mesa3d.org";
994 * We implement the glEnable function only because we care about
995 * dither enable/disable.
998 enable( GLcontext
*ctx
, GLenum pname
, GLboolean state
)
1000 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
1005 xmesa
->pixelformat
= xmesa
->xm_visual
->dithered_pf
;
1007 xmesa
->pixelformat
= xmesa
->xm_visual
->undithered_pf
;
1010 ; /* silence compiler warning */
1015 void xmesa_update_state( GLcontext
*ctx
, GLuint new_state
)
1017 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
1019 /* Propogate statechange information to swrast and swrast_setup
1020 * modules. The X11 driver has no internal GL-dependent state.
1022 _swrast_InvalidateState( ctx
, new_state
);
1023 _ac_InvalidateState( ctx
, new_state
);
1024 _tnl_InvalidateState( ctx
, new_state
);
1025 _swsetup_InvalidateState( ctx
, new_state
);
1028 /* setup pointers to front and back buffer clear functions */
1029 xmesa
->xm_draw_buffer
->front_clear_func
= clear_front_pixmap
;
1030 if (xmesa
->xm_draw_buffer
->backpixmap
!= XIMAGE
) {
1031 xmesa
->xm_draw_buffer
->back_clear_func
= clear_back_pixmap
;
1033 else if (sizeof(GLushort
)!=2 || sizeof(GLuint
)!=4) {
1034 xmesa
->xm_draw_buffer
->back_clear_func
= clear_nbit_ximage
;
1036 else switch (xmesa
->xm_visual
->BitsPerPixel
) {
1038 if (xmesa
->xm_visual
->hpcr_clear_flag
) {
1039 xmesa
->xm_draw_buffer
->back_clear_func
= clear_HPCR_ximage
;
1042 xmesa
->xm_draw_buffer
->back_clear_func
= clear_8bit_ximage
;
1046 xmesa
->xm_draw_buffer
->back_clear_func
= clear_16bit_ximage
;
1049 xmesa
->xm_draw_buffer
->back_clear_func
= clear_24bit_ximage
;
1052 xmesa
->xm_draw_buffer
->back_clear_func
= clear_32bit_ximage
;
1055 xmesa
->xm_draw_buffer
->back_clear_func
= clear_nbit_ximage
;
1059 xmesa_update_span_funcs(ctx
);
1065 * Called via ctx->Driver.TestProxyTeximage(). Normally, we'd just use
1066 * the _mesa_test_proxy_teximage() fallback function, but we're going to
1067 * special-case the 3D texture case to allow textures up to 512x512x32
1071 test_proxy_teximage(GLcontext
*ctx
, GLenum target
, GLint level
,
1072 GLint internalFormat
, GLenum format
, GLenum type
,
1073 GLint width
, GLint height
, GLint depth
, GLint border
)
1075 if (target
== GL_PROXY_TEXTURE_3D
) {
1076 /* special case for 3D textures */
1077 if (width
* height
* depth
> 512 * 512 * 64 ||
1078 width
< 2 * border
||
1079 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1080 _mesa_bitcount(width
- 2 * border
) != 1) ||
1081 height
< 2 * border
||
1082 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1083 _mesa_bitcount(height
- 2 * border
) != 1) ||
1084 depth
< 2 * border
||
1085 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1086 _mesa_bitcount(depth
- 2 * border
) != 1)) {
1087 /* Bad size, or too many texels */
1093 /* use the fallback routine for 1D, 2D, cube and rect targets */
1094 return _mesa_test_proxy_teximage(ctx
, target
, level
, internalFormat
,
1095 format
, type
, width
, height
, depth
,
1102 * Initialize the device driver function table with the functions
1103 * we implement in this driver.
1105 void xmesa_init_driver_functions( XMesaVisual xmvisual
,
1106 struct dd_function_table
*driver
)
1108 driver
->GetString
= get_string
;
1109 driver
->UpdateState
= xmesa_update_state
;
1110 driver
->GetBufferSize
= get_buffer_size
;
1111 driver
->Flush
= finish_or_flush
;
1112 driver
->Finish
= finish_or_flush
;
1113 driver
->ClearIndex
= clear_index
;
1114 driver
->ClearColor
= clear_color
;
1115 driver
->IndexMask
= index_mask
;
1116 driver
->ColorMask
= color_mask
;
1117 driver
->Enable
= enable
;
1118 driver
->Clear
= clear_buffers
;
1119 driver
->ResizeBuffers
= xmesa_resize_buffers
;
1120 #ifndef XFree86Server
1121 driver
->CopyPixels
= /*_swrast_CopyPixels;*/xmesa_CopyPixels
;
1122 if (xmvisual
->undithered_pf
== PF_8R8G8B
&&
1123 xmvisual
->dithered_pf
== PF_8R8G8B
) {
1124 driver
->DrawPixels
= xmesa_DrawPixels_8R8G8B
;
1126 else if (xmvisual
->undithered_pf
== PF_5R6G5B
) {
1127 driver
->DrawPixels
= xmesa_DrawPixels_5R6G5B
;
1130 driver
->TestProxyTexImage
= test_proxy_teximage
;
1134 #define XMESA_NEW_POINT (_NEW_POINT | \
1136 _SWRAST_NEW_RASTERMASK)
1138 #define XMESA_NEW_LINE (_NEW_LINE | \
1143 _SWRAST_NEW_RASTERMASK)
1145 #define XMESA_NEW_TRIANGLE (_NEW_POLYGON | \
1150 _SWRAST_NEW_RASTERMASK)
1153 /* Extend the software rasterizer with our line/point/triangle
1156 void xmesa_register_swrast_functions( GLcontext
*ctx
)
1158 SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
1159 struct swrast_device_driver
*dd
= _swrast_GetDeviceDriverReference(ctx
);
1161 dd
->SetBuffer
= xmesa_set_buffer
;
1163 swrast
->choose_point
= xmesa_choose_point
;
1164 swrast
->choose_line
= xmesa_choose_line
;
1165 swrast
->choose_triangle
= xmesa_choose_triangle
;
1167 swrast
->invalidate_point
|= XMESA_NEW_POINT
;
1168 swrast
->invalidate_line
|= XMESA_NEW_LINE
;
1169 swrast
->invalidate_triangle
|= XMESA_NEW_TRIANGLE
;