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_auxbuffer.h"
45 #include "swrast/s_context.h"
46 #include "swrast/s_drawpix.h"
47 #include "swrast/s_alphabuf.h"
48 #include "swrast_setup/swrast_setup.h"
50 #include "tnl/t_context.h"
53 #include <GL/glxtokens.h>
59 * Dithering kernels and lookup tables.
62 const int xmesa_kernel8
[DITH_DY
* DITH_DX
] = {
63 0 * MAXC
, 8 * MAXC
, 2 * MAXC
, 10 * MAXC
,
64 12 * MAXC
, 4 * MAXC
, 14 * MAXC
, 6 * MAXC
,
65 3 * MAXC
, 11 * MAXC
, 1 * MAXC
, 9 * MAXC
,
66 15 * MAXC
, 7 * MAXC
, 13 * MAXC
, 5 * MAXC
,
69 const short xmesa_HPCR_DRGB
[3][2][16] = {
71 { 16, -4, 1,-11, 14, -6, 3, -9, 15, -5, 2,-10, 13, -7, 4, -8},
72 {-15, 5, 0, 12,-13, 7, -2, 10,-14, 6, -1, 11,-12, 8, -3, 9}
75 {-11, 15, -7, 3, -8, 14, -4, 2,-10, 16, -6, 4, -9, 13, -5, 1},
76 { 12,-14, 8, -2, 9,-13, 5, -1, 11,-15, 7, -3, 10,-12, 6, 0}
79 { 6,-18, 26,-14, 2,-22, 30,-10, 8,-16, 28,-12, 4,-20, 32, -8},
80 { -4, 20,-24, 16, 0, 24,-28, 12, -6, 18,-26, 14, -2, 22,-30, 10}
84 const int xmesa_kernel1
[16] = {
85 0*47, 9*47, 4*47, 12*47, /* 47 = (255*3)/16 */
86 6*47, 2*47, 14*47, 8*47,
87 10*47, 1*47, 5*47, 11*47,
88 7*47, 13*47, 3*47, 15*47
93 * Return the size (width, height) of the X window for the given GLframebuffer.
94 * Output: width - width of buffer in pixels.
95 * height - height of buffer in pixels.
98 get_buffer_size( GLframebuffer
*buffer
, GLuint
*width
, GLuint
*height
)
100 /* We can do this cast because the first field in the XMesaBuffer
101 * struct is a GLframebuffer struct. If this weren't true, we'd
102 * need a pointer from the GLframebuffer to the XMesaBuffer.
104 const XMesaBuffer xmBuffer
= (XMesaBuffer
) buffer
;
105 unsigned int winwidth
, winheight
;
107 /* XFree86 GLX renderer */
108 winwidth
= MIN2(xmBuffer
->frontbuffer
->width
, MAX_WIDTH
);
109 winheight
= MIN2(xmBuffer
->frontbuffer
->height
, MAX_HEIGHT
);
115 _glthread_LOCK_MUTEX(_xmesa_lock
);
116 XSync(xmBuffer
->xm_visual
->display
, 0); /* added for Chromium */
117 XGetGeometry( xmBuffer
->xm_visual
->display
, xmBuffer
->frontbuffer
, &root
,
118 &winx
, &winy
, &winwidth
, &winheight
, &bw
, &d
);
119 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
128 finish_or_flush( GLcontext
*ctx
)
133 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
135 _glthread_LOCK_MUTEX(_xmesa_lock
);
136 XSync( xmesa
->display
, False
);
137 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
145 * This chooses the color buffer for reading and writing spans, points,
146 * lines, and triangles.
149 xmesa_set_buffer( GLcontext
*ctx
, GLframebuffer
*buffer
, GLuint bufferBit
)
151 /* We can make this cast since the XMesaBuffer wraps GLframebuffer.
152 * GLframebuffer is the first member in a XMesaBuffer struct.
154 XMesaBuffer target
= (XMesaBuffer
) buffer
;
155 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
157 /* This assignment tells the span/point/line/triangle functions
158 * which XMesaBuffer to use.
160 xmesa
->xm_buffer
= target
;
163 * Now determine front vs back color buffer.
165 if (bufferBit
== DD_FRONT_LEFT_BIT
) {
166 target
->buffer
= target
->frontbuffer
;
167 xmesa_update_span_funcs(ctx
);
169 else if (bufferBit
== DD_BACK_LEFT_BIT
) {
170 ASSERT(target
->db_state
);
171 if (target
->backpixmap
) {
172 /* back buffer is a pixmap */
173 target
->buffer
= (XMesaDrawable
) target
->backpixmap
;
175 else if (target
->backimage
) {
176 /* back buffer is an XImage */
177 target
->buffer
= None
;
180 /* No back buffer!!!! Must be out of memory, use front buffer */
181 target
->buffer
= target
->frontbuffer
;
183 xmesa_update_span_funcs(ctx
);
185 else if (bufferBit
& (DD_AUX0_BIT
| DD_AUX1_BIT
| DD_AUX2_BIT
| DD_AUX3_BIT
)) {
186 _swrast_use_aux_buffer(ctx
, buffer
, bufferBit
);
189 _mesa_problem(ctx
, "invalid buffer 0x%x in set_buffer() in xm_dd.c");
197 clear_index( GLcontext
*ctx
, GLuint index
)
199 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
200 xmesa
->clearpixel
= (unsigned long) index
;
201 XMesaSetForeground( xmesa
->display
, xmesa
->xm_draw_buffer
->cleargc
,
202 (unsigned long) index
);
207 clear_color( GLcontext
*ctx
, const GLfloat color
[4] )
209 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
210 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[0], color
[0]);
211 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[1], color
[1]);
212 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[2], color
[2]);
213 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[3], color
[3]);
214 xmesa
->clearpixel
= xmesa_color_to_pixel( xmesa
,
215 xmesa
->clearcolor
[0],
216 xmesa
->clearcolor
[1],
217 xmesa
->clearcolor
[2],
218 xmesa
->clearcolor
[3],
219 xmesa
->xm_visual
->undithered_pf
);
220 _glthread_LOCK_MUTEX(_xmesa_lock
);
221 XMesaSetForeground( xmesa
->display
, xmesa
->xm_draw_buffer
->cleargc
,
223 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
228 /* Set index mask ala glIndexMask */
230 index_mask( GLcontext
*ctx
, GLuint mask
)
232 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
233 if (xmesa
->xm_draw_buffer
->buffer
!= XIMAGE
) {
235 if (mask
==0xffffffff) {
236 m
= ((unsigned long)~0L);
239 m
= (unsigned long) mask
;
241 XMesaSetPlaneMask( xmesa
->display
, xmesa
->xm_draw_buffer
->cleargc
, m
);
242 XMesaSetPlaneMask( xmesa
->display
, xmesa
->xm_draw_buffer
->gc
, m
);
247 /* Implements glColorMask() */
249 color_mask(GLcontext
*ctx
,
250 GLboolean rmask
, GLboolean gmask
, GLboolean bmask
, GLboolean amask
)
252 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
253 const int xclass
= xmesa
->xm_visual
->mesa_visual
.visualType
;
256 if (xclass
== GLX_TRUE_COLOR
|| xclass
== GLX_DIRECT_COLOR
) {
258 if (rmask
&& gmask
&& bmask
) {
259 m
= ((unsigned long)~0L);
263 if (rmask
) m
|= GET_REDMASK(xmesa
->xm_visual
);
264 if (gmask
) m
|= GET_GREENMASK(xmesa
->xm_visual
);
265 if (bmask
) m
|= GET_BLUEMASK(xmesa
->xm_visual
);
267 XMesaSetPlaneMask( xmesa
->display
, xmesa
->xm_draw_buffer
->cleargc
, m
);
268 XMesaSetPlaneMask( xmesa
->display
, xmesa
->xm_draw_buffer
->gc
, m
);
274 /**********************************************************************/
275 /*** glClear implementations ***/
276 /**********************************************************************/
280 clear_front_pixmap( GLcontext
*ctx
, GLboolean all
,
281 GLint x
, GLint y
, GLint width
, GLint height
)
283 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
285 XMesaFillRectangle( xmesa
->display
, xmesa
->xm_draw_buffer
->frontbuffer
,
286 xmesa
->xm_draw_buffer
->cleargc
,
288 xmesa
->xm_draw_buffer
->width
+1,
289 xmesa
->xm_draw_buffer
->height
+1 );
292 XMesaFillRectangle( xmesa
->display
, xmesa
->xm_draw_buffer
->frontbuffer
,
293 xmesa
->xm_draw_buffer
->cleargc
,
294 x
, xmesa
->xm_draw_buffer
->height
- y
- height
,
301 clear_back_pixmap( GLcontext
*ctx
, GLboolean all
,
302 GLint x
, GLint y
, GLint width
, GLint height
)
304 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
306 XMesaFillRectangle( xmesa
->display
, xmesa
->xm_draw_buffer
->backpixmap
,
307 xmesa
->xm_draw_buffer
->cleargc
,
309 xmesa
->xm_draw_buffer
->width
+1,
310 xmesa
->xm_draw_buffer
->height
+1 );
313 XMesaFillRectangle( xmesa
->display
, xmesa
->xm_draw_buffer
->backpixmap
,
314 xmesa
->xm_draw_buffer
->cleargc
,
315 x
, xmesa
->xm_draw_buffer
->height
- y
- height
,
322 clear_8bit_ximage( GLcontext
*ctx
, GLboolean all
,
323 GLint x
, GLint y
, GLint width
, GLint height
)
325 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
327 size_t n
= xmesa
->xm_draw_buffer
->backimage
->bytes_per_line
328 * xmesa
->xm_draw_buffer
->backimage
->height
;
329 MEMSET( xmesa
->xm_draw_buffer
->backimage
->data
, xmesa
->clearpixel
, n
);
333 for (i
=0;i
<height
;i
++) {
334 GLubyte
*ptr
= PIXELADDR1( xmesa
->xm_draw_buffer
, x
, y
+i
);
335 MEMSET( ptr
, xmesa
->clearpixel
, width
);
342 clear_HPCR_ximage( GLcontext
*ctx
, GLboolean all
,
343 GLint x
, GLint y
, GLint width
, GLint height
)
345 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
347 GLint i
, c16
= (xmesa
->xm_draw_buffer
->backimage
->bytes_per_line
>>4)<<4;
348 GLubyte
*ptr
= (GLubyte
*)xmesa
->xm_draw_buffer
->backimage
->data
;
349 for (i
=0; i
<xmesa
->xm_draw_buffer
->backimage
->height
; i
++) {
351 GLubyte
*sptr
= xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0];
355 for (j
=0; j
<c16
; j
+=16) {
374 for (; j
<xmesa
->xm_draw_buffer
->backimage
->bytes_per_line
; j
++) {
382 for (i
=y
; i
<y
+height
; i
++) {
383 GLubyte
*ptr
= PIXELADDR1( xmesa
->xm_draw_buffer
, x
, i
);
385 GLubyte
*sptr
= xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0];
389 for (j
=x
; j
<x
+width
; j
++) {
399 clear_16bit_ximage( GLcontext
*ctx
, GLboolean all
,
400 GLint x
, GLint y
, GLint width
, GLint height
)
402 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
403 register GLuint pixel
= (GLuint
) xmesa
->clearpixel
;
404 if (xmesa
->swapbytes
) {
405 pixel
= ((pixel
>> 8) & 0x00ff) | ((pixel
<< 8) & 0xff00);
409 register GLuint
*ptr4
= (GLuint
*) xmesa
->xm_draw_buffer
->backimage
->data
;
410 if ((pixel
& 0xff) == ((pixel
>> 8) & 0xff)) {
411 /* low and high bytes are equal so use memset() */
412 n
= xmesa
->xm_draw_buffer
->backimage
->bytes_per_line
413 * xmesa
->xm_draw_buffer
->height
;
414 MEMSET( ptr4
, pixel
& 0xff, n
);
417 pixel
= pixel
| (pixel
<<16);
418 n
= xmesa
->xm_draw_buffer
->backimage
->bytes_per_line
419 * xmesa
->xm_draw_buffer
->height
/ 4;
425 if ((xmesa
->xm_draw_buffer
->backimage
->bytes_per_line
*
426 xmesa
->xm_draw_buffer
->height
) & 0x2)
427 *(GLushort
*)ptr4
= pixel
& 0xffff;
432 for (j
=0;j
<height
;j
++) {
433 register GLushort
*ptr2
= PIXELADDR2( xmesa
->xm_draw_buffer
, x
, y
+j
);
434 for (i
=0;i
<width
;i
++) {
442 /* Optimized code provided by Nozomi Ytow <noz@xfree86.org> */
444 clear_24bit_ximage( GLcontext
*ctx
, GLboolean all
,
445 GLint x
, GLint y
, GLint width
, GLint height
)
447 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
448 const GLubyte r
= xmesa
->clearcolor
[0];
449 const GLubyte g
= xmesa
->clearcolor
[1];
450 const GLubyte b
= xmesa
->clearcolor
[2];
451 #if 0 /* See below */
452 register GLuint clearPixel
;
453 if (xmesa
->swapbytes
) {
454 clearPixel
= (b
<< 16) | (g
<< 8) | r
;
457 clearPixel
= (r
<< 16) | (g
<< 8) | b
;
463 /* same value for all three components (gray) */
464 const GLint w3
= xmesa
->xm_draw_buffer
->width
* 3;
465 const GLint h
= xmesa
->xm_draw_buffer
->height
;
467 for (i
= 0; i
< h
; i
++) {
468 bgr_t
*ptr3
= PIXELADDR3(xmesa
->xm_draw_buffer
, 0, i
);
474 const GLint w
= xmesa
->xm_draw_buffer
->width
;
475 const GLint h
= xmesa
->xm_draw_buffer
->height
;
477 for (i
= 0; i
< h
; i
++) {
478 bgr_t
*ptr3
= PIXELADDR3(xmesa
->xm_draw_buffer
, 0, i
);
479 for (j
= 0; j
< w
; j
++) {
486 #if 0 /* this code doesn't work for all window widths */
487 register GLuint
*ptr4
= (GLuint
*) ptr3
;
490 register GLuint
*p
= pixel4
;
491 pixel4
[0] = clearPixel
| (clearPixel
<< 24);
492 pixel4
[1] = (clearPixel
<< 16) | (clearPixel
>> 8);
493 pixel4
[2] = (clearPixel
<< 8) | (clearPixel
>> 16);
494 switch (3 & (int)(ptr3
- (bgr_t
*) ptr4
)){
498 px
= *ptr4
& 0x00ffffff;
499 px
|= pixel4
[0] & 0xff000000;
501 px
= *ptr4
& 0xffff0000;
502 px
|= pixel4
[2] & 0x0000ffff;
507 px
= *ptr4
& 0x0000fffff;
508 px
|= pixel4
[1] & 0xffff0000;
510 px
= *ptr4
& 0xffffff00;
511 px
|= pixel4
[2] & 0x000000ff;
516 px
= *ptr4
& 0x000000ff;
517 px
|= pixel4
[2] & 0xffffff00;
534 px
= *ptr4
& 0xffffff00;
535 px
|= clearPixel
& 0xff;
541 px
= *ptr4
& 0xffff0000;
546 px
= *ptr4
& 0xff000000;
557 /* only clear subrect of color buffer */
559 /* same value for all three components (gray) */
561 for (j
=0;j
<height
;j
++) {
562 bgr_t
*ptr3
= PIXELADDR3( xmesa
->xm_draw_buffer
, x
, y
+j
);
563 MEMSET(ptr3
, r
, 3 * width
);
567 /* non-gray clear color */
569 for (j
= 0; j
< height
; j
++) {
570 bgr_t
*ptr3
= PIXELADDR3( xmesa
->xm_draw_buffer
, x
, y
+j
);
571 for (i
= 0; i
< width
; i
++) {
578 #if 0 /* this code might not always (seems ptr3 always == ptr4) */
581 pixel4
[0] = clearPixel
| (clearPixel
<< 24);
582 pixel4
[1] = (clearPixel
<< 16) | (clearPixel
>> 8);
583 pixel4
[2] = (clearPixel
<< 8) | (clearPixel
>> 16);
584 for (j
=0;j
<height
;j
++) {
585 bgr_t
*ptr3
= PIXELADDR3( xmesa
->xm_draw_buffer
, x
, y
+j
);
586 register GLuint
*ptr4
= (GLuint
*)ptr3
;
587 register GLuint
*p
, px
;
589 switch (3 & (int)(ptr3
- (bgr_t
*) ptr4
)){
593 px
= *ptr4
& 0x00ffffff;
594 px
|= pixel4
[0] & 0xff000000;
596 px
= *ptr4
& 0xffff0000;
597 px
|= pixel4
[2] & 0x0000ffff;
602 px
= *ptr4
& 0x0000fffff;
603 px
|= pixel4
[1] & 0xffff0000;
605 px
= *ptr4
& 0xffffff00;
606 px
|= pixel4
[2] & 0x000000ff;
611 px
= *ptr4
& 0x000000ff;
612 px
|= pixel4
[2] & 0xffffff00;
629 px
= *ptr4
& 0xffffff00;
636 px
= *ptr4
& 0xffff0000;
641 px
= *ptr4
& 0xff000000;
642 px
|= pixel4
[0] & 0xffffff;
656 clear_32bit_ximage( GLcontext
*ctx
, GLboolean all
,
657 GLint x
, GLint y
, GLint width
, GLint height
)
659 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
660 register GLuint pixel
= (GLuint
) xmesa
->clearpixel
;
661 if (xmesa
->swapbytes
) {
662 pixel
= ((pixel
>> 24) & 0x000000ff)
663 | ((pixel
>> 8) & 0x0000ff00)
664 | ((pixel
<< 8) & 0x00ff0000)
665 | ((pixel
<< 24) & 0xff000000);
668 register GLint n
= xmesa
->xm_draw_buffer
->width
* xmesa
->xm_draw_buffer
->height
;
669 register GLuint
*ptr4
= (GLuint
*) xmesa
->xm_draw_buffer
->backimage
->data
;
671 MEMSET( ptr4
, pixel
, 4*n
);
682 for (j
=0;j
<height
;j
++) {
683 register GLuint
*ptr4
= PIXELADDR4( xmesa
->xm_draw_buffer
, x
, y
+j
);
684 for (i
=0;i
<width
;i
++) {
693 clear_nbit_ximage( GLcontext
*ctx
, GLboolean all
,
694 GLint x
, GLint y
, GLint width
, GLint height
)
696 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
697 XMesaImage
*img
= xmesa
->xm_draw_buffer
->backimage
;
700 width
= xmesa
->xm_draw_buffer
->width
;
701 height
= xmesa
->xm_draw_buffer
->height
;
702 for (j
=0;j
<height
;j
++) {
703 for (i
=0;i
<width
;i
++) {
704 XMesaPutPixel( img
, i
, j
, xmesa
->clearpixel
);
709 /* TODO: optimize this */
711 y
= FLIP(xmesa
->xm_draw_buffer
, y
);
712 for (j
=0;j
<height
;j
++) {
713 for (i
=0;i
<width
;i
++) {
714 XMesaPutPixel( img
, x
+i
, y
-j
, xmesa
->clearpixel
);
723 clear_buffers( GLcontext
*ctx
, GLbitfield mask
,
724 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
726 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
727 const GLuint
*colorMask
= (GLuint
*) &ctx
->Color
.ColorMask
;
729 if ((mask
& (DD_FRONT_LEFT_BIT
| DD_BACK_LEFT_BIT
)) &&
730 xmesa
->xm_draw_buffer
->mesa_buffer
.UseSoftwareAlphaBuffers
&&
731 ctx
->Color
.ColorMask
[ACOMP
]) {
732 _swrast_clear_alpha_buffers(ctx
);
735 /* we can't handle color or index masking */
736 if (*colorMask
== 0xffffffff && ctx
->Color
.IndexMask
== 0xffffffff) {
737 if (mask
& DD_FRONT_LEFT_BIT
) {
738 ASSERT(xmesa
->xm_draw_buffer
->front_clear_func
);
739 (*xmesa
->xm_draw_buffer
->front_clear_func
)( ctx
, all
, x
, y
, width
, height
);
740 mask
&= ~DD_FRONT_LEFT_BIT
;
742 if (mask
& DD_BACK_LEFT_BIT
) {
743 ASSERT(xmesa
->xm_draw_buffer
->back_clear_func
);
744 (*xmesa
->xm_draw_buffer
->back_clear_func
)( ctx
, all
, x
, y
, width
, height
);
745 mask
&= ~DD_BACK_LEFT_BIT
;
750 _swrast_Clear( ctx
, mask
, all
, x
, y
, width
, height
);
755 * When we detect that the user has resized the window this function will
756 * get called. Here we'll reallocate the back buffer, depth buffer,
757 * stencil buffer etc. to match the new window size.
760 xmesa_resize_buffers( GLframebuffer
*buffer
)
762 int height
= (int) buffer
->Height
;
763 /* We can do this cast because the first field in the XMesaBuffer
764 * struct is a GLframebuffer struct. If this weren't true, we'd
765 * need a pointer from the GLframebuffer to the XMesaBuffer.
767 XMesaBuffer xmBuffer
= (XMesaBuffer
) buffer
;
769 xmBuffer
->width
= buffer
->Width
;
770 xmBuffer
->height
= buffer
->Height
;
771 xmesa_alloc_back_buffer( xmBuffer
);
773 /* Needed by FLIP macro */
774 xmBuffer
->bottom
= height
- 1;
776 if (xmBuffer
->backimage
) {
777 /* Needed by PIXELADDR1 macro */
778 xmBuffer
->ximage_width1
= xmBuffer
->backimage
->bytes_per_line
;
779 xmBuffer
->ximage_origin1
= (GLubyte
*) xmBuffer
->backimage
->data
780 + xmBuffer
->ximage_width1
* (height
-1);
782 /* Needed by PIXELADDR2 macro */
783 xmBuffer
->ximage_width2
= xmBuffer
->backimage
->bytes_per_line
/ 2;
784 xmBuffer
->ximage_origin2
= (GLushort
*) xmBuffer
->backimage
->data
785 + xmBuffer
->ximage_width2
* (height
-1);
787 /* Needed by PIXELADDR3 macro */
788 xmBuffer
->ximage_width3
= xmBuffer
->backimage
->bytes_per_line
;
789 xmBuffer
->ximage_origin3
= (GLubyte
*) xmBuffer
->backimage
->data
790 + xmBuffer
->ximage_width3
* (height
-1);
792 /* Needed by PIXELADDR4 macro */
793 xmBuffer
->ximage_width4
= xmBuffer
->backimage
->width
;
794 xmBuffer
->ximage_origin4
= (GLuint
*) xmBuffer
->backimage
->data
795 + xmBuffer
->ximage_width4
* (height
-1);
798 _swrast_alloc_buffers( buffer
);
802 #ifndef XFree86Server
803 /* XXX this was never tested in the Xserver environment */
806 * This function implements glDrawPixels() with an XPutImage call when
807 * drawing to the front buffer (X Window drawable).
808 * The image format must be GL_BGRA to match the PF_8R8G8B pixel format.
811 xmesa_DrawPixels_8R8G8B( GLcontext
*ctx
,
812 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
813 GLenum format
, GLenum type
,
814 const struct gl_pixelstore_attrib
*unpack
,
815 const GLvoid
*pixels
)
817 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
818 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
819 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
820 const XMesaDrawable buffer
= xmesa
->xm_draw_buffer
->buffer
;
821 const XMesaGC gc
= xmesa
->xm_draw_buffer
->gc
;
825 ASSERT(xmesa
->xm_visual
->dithered_pf
== PF_8R8G8B
);
826 ASSERT(xmesa
->xm_visual
->undithered_pf
== PF_8R8G8B
);
828 if (swrast
->NewState
)
829 _swrast_validate_derived( ctx
);
831 if (buffer
&& /* buffer != 0 means it's a Window or Pixmap */
833 type
== GL_UNSIGNED_BYTE
&&
834 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
835 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
836 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
837 ctx
->Pixel
.ZoomY
== 1.0) {
842 int srcX
= unpack
->SkipPixels
;
843 int srcY
= unpack
->SkipRows
;
844 int rowLength
= unpack
->RowLength
? unpack
->RowLength
: width
;
846 pixels
= _swrast_validate_pbo_access(unpack
, width
, height
, 1,
847 format
, type
, (GLvoid
*) pixels
);
851 if (_swrast_clip_pixelrect(ctx
, &dstX
, &dstY
, &w
, &h
, &srcX
, &srcY
)) {
852 /* This is a little tricky since all coordinates up to now have
853 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
854 * so we have to carefully compute the Y coordinates/addresses here.
857 MEMSET(&ximage
, 0, sizeof(XMesaImage
));
858 ximage
.width
= width
;
859 ximage
.height
= height
;
860 ximage
.format
= ZPixmap
;
861 ximage
.data
= (char *) pixels
862 + ((srcY
+ h
- 1) * rowLength
+ srcX
) * 4;
863 ximage
.byte_order
= LSBFirst
;
864 ximage
.bitmap_unit
= 32;
865 ximage
.bitmap_bit_order
= LSBFirst
;
866 ximage
.bitmap_pad
= 32;
868 ximage
.bytes_per_line
= -rowLength
* 4; /* negative to flip image */
869 ximage
.bits_per_pixel
= 32;
870 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
871 /* flip Y axis for dest position */
872 dstY
= FLIP(xmesa
->xm_draw_buffer
, dstY
) - h
+ 1;
873 XPutImage(dpy
, buffer
, gc
, &ximage
, 0, 0, dstX
, dstY
, w
, h
);
877 /* software fallback */
878 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
879 format
, type
, unpack
, pixels
);
886 * This function implements glDrawPixels() with an XPutImage call when
887 * drawing to the front buffer (X Window drawable). The image format
888 * must be GL_RGB and image type must be GL_UNSIGNED_SHORT_5_6_5 to
889 * match the PF_5R6G5B pixel format.
892 xmesa_DrawPixels_5R6G5B( GLcontext
*ctx
,
893 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
894 GLenum format
, GLenum type
,
895 const struct gl_pixelstore_attrib
*unpack
,
896 const GLvoid
*pixels
)
898 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
899 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
900 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
901 const XMesaDrawable buffer
= xmesa
->xm_draw_buffer
->buffer
;
902 const XMesaGC gc
= xmesa
->xm_draw_buffer
->gc
;
906 ASSERT(xmesa
->xm_visual
->undithered_pf
== PF_5R6G5B
);
908 if (swrast
->NewState
)
909 _swrast_validate_derived( ctx
);
911 if (buffer
&& /* buffer != 0 means it's a Window or Pixmap */
913 type
== GL_UNSIGNED_SHORT_5_6_5
&&
914 !ctx
->Color
.DitherFlag
&& /* no dithering */
915 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
916 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
917 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
918 ctx
->Pixel
.ZoomY
== 1.0) {
923 int srcX
= unpack
->SkipPixels
;
924 int srcY
= unpack
->SkipRows
;
925 int rowLength
= unpack
->RowLength
? unpack
->RowLength
: width
;
927 pixels
= _swrast_validate_pbo_access(unpack
, width
, height
, 1,
928 format
, type
, (GLvoid
*) pixels
);
932 if (_swrast_clip_pixelrect(ctx
, &dstX
, &dstY
, &w
, &h
, &srcX
, &srcY
)) {
933 /* This is a little tricky since all coordinates up to now have
934 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
935 * so we have to carefully compute the Y coordinates/addresses here.
938 MEMSET(&ximage
, 0, sizeof(XMesaImage
));
939 ximage
.width
= width
;
940 ximage
.height
= height
;
941 ximage
.format
= ZPixmap
;
942 ximage
.data
= (char *) pixels
943 + ((srcY
+ h
- 1) * rowLength
+ srcX
) * 2;
944 ximage
.byte_order
= LSBFirst
;
945 ximage
.bitmap_unit
= 16;
946 ximage
.bitmap_bit_order
= LSBFirst
;
947 ximage
.bitmap_pad
= 16;
949 ximage
.bytes_per_line
= -rowLength
* 2; /* negative to flip image */
950 ximage
.bits_per_pixel
= 16;
951 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
952 /* flip Y axis for dest position */
953 dstY
= FLIP(xmesa
->xm_draw_buffer
, dstY
) - h
+ 1;
954 XPutImage(dpy
, buffer
, gc
, &ximage
, 0, 0, dstX
, dstY
, w
, h
);
958 /* software fallback */
959 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
960 format
, type
, unpack
, pixels
);
967 * Implement glCopyPixels for the front color buffer (or back buffer Pixmap)
968 * for the color buffer. Don't support zooming, pixel transfer, etc.
969 * We do support copying from one window to another, ala glXMakeCurrentRead.
972 xmesa_CopyPixels( GLcontext
*ctx
,
973 GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
974 GLint destx
, GLint desty
, GLenum type
)
976 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
977 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
978 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
979 const XMesaDrawable drawBuffer
= xmesa
->xm_draw_buffer
->buffer
;
980 const XMesaDrawable readBuffer
= xmesa
->xm_read_buffer
->buffer
;
981 const XMesaGC gc
= xmesa
->xm_draw_buffer
->gc
;
986 if (swrast
->NewState
)
987 _swrast_validate_derived( ctx
);
989 if (ctx
->Color
.DrawBuffer
== GL_FRONT
&&
990 ctx
->Pixel
.ReadBuffer
== GL_FRONT
&&
991 drawBuffer
&& /* buffer != 0 means it's a Window or Pixmap */
994 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
995 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
996 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
997 ctx
->Pixel
.ZoomY
== 1.0) {
998 /* Note: we don't do any special clipping work here. We could,
999 * but X will do it for us.
1001 srcy
= FLIP(xmesa
->xm_read_buffer
, srcy
) - height
+ 1;
1002 desty
= FLIP(xmesa
->xm_draw_buffer
, desty
) - height
+ 1;
1003 XCopyArea(dpy
, readBuffer
, drawBuffer
, gc
,
1004 srcx
, srcy
, width
, height
, destx
, desty
);
1007 _swrast_CopyPixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
, type
);
1010 #endif /* XFree86Server */
1015 * Every driver should implement a GetString function in order to
1016 * return a meaningful GL_RENDERER string.
1018 static const GLubyte
*
1019 get_string( GLcontext
*ctx
, GLenum name
)
1024 #ifdef XFree86Server
1025 return (const GLubyte
*) "Mesa GLX Indirect";
1027 return (const GLubyte
*) "Mesa X11";
1030 #ifdef XFree86Server
1031 return (const GLubyte
*) "Mesa project: www.mesa3d.org";
1042 * We implement the glEnable function only because we care about
1043 * dither enable/disable.
1046 enable( GLcontext
*ctx
, GLenum pname
, GLboolean state
)
1048 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
1053 xmesa
->pixelformat
= xmesa
->xm_visual
->dithered_pf
;
1055 xmesa
->pixelformat
= xmesa
->xm_visual
->undithered_pf
;
1058 ; /* silence compiler warning */
1063 void xmesa_update_state( GLcontext
*ctx
, GLuint new_state
)
1065 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
1067 /* Propogate statechange information to swrast and swrast_setup
1068 * modules. The X11 driver has no internal GL-dependent state.
1070 _swrast_InvalidateState( ctx
, new_state
);
1071 _ac_InvalidateState( ctx
, new_state
);
1072 _tnl_InvalidateState( ctx
, new_state
);
1073 _swsetup_InvalidateState( ctx
, new_state
);
1076 /* setup pointers to front and back buffer clear functions */
1077 xmesa
->xm_draw_buffer
->front_clear_func
= clear_front_pixmap
;
1078 if (xmesa
->xm_draw_buffer
->backpixmap
!= XIMAGE
) {
1079 xmesa
->xm_draw_buffer
->back_clear_func
= clear_back_pixmap
;
1081 else if (sizeof(GLushort
)!=2 || sizeof(GLuint
)!=4) {
1082 xmesa
->xm_draw_buffer
->back_clear_func
= clear_nbit_ximage
;
1084 else switch (xmesa
->xm_visual
->BitsPerPixel
) {
1086 if (xmesa
->xm_visual
->hpcr_clear_flag
) {
1087 xmesa
->xm_draw_buffer
->back_clear_func
= clear_HPCR_ximage
;
1090 xmesa
->xm_draw_buffer
->back_clear_func
= clear_8bit_ximage
;
1094 xmesa
->xm_draw_buffer
->back_clear_func
= clear_16bit_ximage
;
1097 xmesa
->xm_draw_buffer
->back_clear_func
= clear_24bit_ximage
;
1100 xmesa
->xm_draw_buffer
->back_clear_func
= clear_32bit_ximage
;
1103 xmesa
->xm_draw_buffer
->back_clear_func
= clear_nbit_ximage
;
1107 if (ctx
->Color
._DrawDestMask
& (DD_FRONT_LEFT_BIT
| DD_BACK_LEFT_BIT
)) {
1108 xmesa_update_span_funcs(ctx
);
1115 * Called via ctx->Driver.TestProxyTeximage(). Normally, we'd just use
1116 * the _mesa_test_proxy_teximage() fallback function, but we're going to
1117 * special-case the 3D texture case to allow textures up to 512x512x32
1121 test_proxy_teximage(GLcontext
*ctx
, GLenum target
, GLint level
,
1122 GLint internalFormat
, GLenum format
, GLenum type
,
1123 GLint width
, GLint height
, GLint depth
, GLint border
)
1125 if (target
== GL_PROXY_TEXTURE_3D
) {
1126 /* special case for 3D textures */
1127 if (width
* height
* depth
> 512 * 512 * 64 ||
1128 width
< 2 * border
||
1129 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1130 _mesa_bitcount(width
- 2 * border
) != 1) ||
1131 height
< 2 * border
||
1132 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1133 _mesa_bitcount(height
- 2 * border
) != 1) ||
1134 depth
< 2 * border
||
1135 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1136 _mesa_bitcount(depth
- 2 * border
) != 1)) {
1137 /* Bad size, or too many texels */
1143 /* use the fallback routine for 1D, 2D, cube and rect targets */
1144 return _mesa_test_proxy_teximage(ctx
, target
, level
, internalFormat
,
1145 format
, type
, width
, height
, depth
,
1152 * Initialize the device driver function table with the functions
1153 * we implement in this driver.
1155 void xmesa_init_driver_functions( XMesaVisual xmvisual
,
1156 struct dd_function_table
*driver
)
1158 driver
->GetString
= get_string
;
1159 driver
->UpdateState
= xmesa_update_state
;
1160 driver
->GetBufferSize
= get_buffer_size
;
1161 driver
->Flush
= finish_or_flush
;
1162 driver
->Finish
= finish_or_flush
;
1163 driver
->ClearIndex
= clear_index
;
1164 driver
->ClearColor
= clear_color
;
1165 driver
->IndexMask
= index_mask
;
1166 driver
->ColorMask
= color_mask
;
1167 driver
->Enable
= enable
;
1168 driver
->Clear
= clear_buffers
;
1169 driver
->ResizeBuffers
= xmesa_resize_buffers
;
1170 #ifndef XFree86Server
1171 driver
->CopyPixels
= /*_swrast_CopyPixels;*/xmesa_CopyPixels
;
1172 if (xmvisual
->undithered_pf
== PF_8R8G8B
&&
1173 xmvisual
->dithered_pf
== PF_8R8G8B
) {
1174 driver
->DrawPixels
= xmesa_DrawPixels_8R8G8B
;
1176 else if (xmvisual
->undithered_pf
== PF_5R6G5B
) {
1177 driver
->DrawPixels
= xmesa_DrawPixels_5R6G5B
;
1180 driver
->TestProxyTexImage
= test_proxy_teximage
;
1184 #define XMESA_NEW_POINT (_NEW_POINT | \
1186 _SWRAST_NEW_RASTERMASK)
1188 #define XMESA_NEW_LINE (_NEW_LINE | \
1193 _SWRAST_NEW_RASTERMASK)
1195 #define XMESA_NEW_TRIANGLE (_NEW_POLYGON | \
1200 _SWRAST_NEW_RASTERMASK)
1203 /* Extend the software rasterizer with our line/point/triangle
1206 void xmesa_register_swrast_functions( GLcontext
*ctx
)
1208 SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
1209 struct swrast_device_driver
*dd
= _swrast_GetDeviceDriverReference(ctx
);
1211 dd
->SetBuffer
= xmesa_set_buffer
;
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
;