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"
33 #include "extensions.h"
42 #include "texformat.h"
44 #include "array_cache/acache.h"
45 #include "swrast/swrast.h"
46 #include "swrast/s_auxbuffer.h"
47 #include "swrast/s_context.h"
48 #include "swrast/s_alphabuf.h"
49 #include "swrast_setup/swrast_setup.h"
51 #include "tnl/t_context.h"
54 #include <GL/glxtokens.h>
60 * Dithering kernels and lookup tables.
63 const int xmesa_kernel8
[DITH_DY
* DITH_DX
] = {
64 0 * MAXC
, 8 * MAXC
, 2 * MAXC
, 10 * MAXC
,
65 12 * MAXC
, 4 * MAXC
, 14 * MAXC
, 6 * MAXC
,
66 3 * MAXC
, 11 * MAXC
, 1 * MAXC
, 9 * MAXC
,
67 15 * MAXC
, 7 * MAXC
, 13 * MAXC
, 5 * MAXC
,
70 const short xmesa_HPCR_DRGB
[3][2][16] = {
72 { 16, -4, 1,-11, 14, -6, 3, -9, 15, -5, 2,-10, 13, -7, 4, -8},
73 {-15, 5, 0, 12,-13, 7, -2, 10,-14, 6, -1, 11,-12, 8, -3, 9}
76 {-11, 15, -7, 3, -8, 14, -4, 2,-10, 16, -6, 4, -9, 13, -5, 1},
77 { 12,-14, 8, -2, 9,-13, 5, -1, 11,-15, 7, -3, 10,-12, 6, 0}
80 { 6,-18, 26,-14, 2,-22, 30,-10, 8,-16, 28,-12, 4,-20, 32, -8},
81 { -4, 20,-24, 16, 0, 24,-28, 12, -6, 18,-26, 14, -2, 22,-30, 10}
85 const int xmesa_kernel1
[16] = {
86 0*47, 9*47, 4*47, 12*47, /* 47 = (255*3)/16 */
87 6*47, 2*47, 14*47, 8*47,
88 10*47, 1*47, 5*47, 11*47,
89 7*47, 13*47, 3*47, 15*47
94 * Return the size (width, height) of the X window for the given GLframebuffer.
95 * Output: width - width of buffer in pixels.
96 * height - height of buffer in pixels.
99 get_buffer_size( GLframebuffer
*buffer
, GLuint
*width
, GLuint
*height
)
101 /* We can do this cast because the first field in the XMesaBuffer
102 * struct is a GLframebuffer struct. If this weren't true, we'd
103 * need a pointer from the GLframebuffer to the XMesaBuffer.
105 const XMesaBuffer xmBuffer
= (XMesaBuffer
) buffer
;
106 unsigned int winwidth
, winheight
;
108 /* XFree86 GLX renderer */
109 winwidth
= MIN2(xmBuffer
->frontbuffer
->width
, MAX_WIDTH
);
110 winheight
= MIN2(xmBuffer
->frontbuffer
->height
, MAX_HEIGHT
);
116 _glthread_LOCK_MUTEX(_xmesa_lock
);
117 XSync(xmBuffer
->xm_visual
->display
, 0); /* added for Chromium */
118 XGetGeometry( xmBuffer
->xm_visual
->display
, xmBuffer
->frontbuffer
, &root
,
119 &winx
, &winy
, &winwidth
, &winheight
, &bw
, &d
);
120 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
129 finish_or_flush( GLcontext
*ctx
)
134 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
136 _glthread_LOCK_MUTEX(_xmesa_lock
);
137 XSync( xmesa
->display
, False
);
138 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
146 * This chooses the color buffer for reading and writing spans, points,
147 * lines, and triangles.
150 xmesa_set_buffer( GLcontext
*ctx
, GLframebuffer
*buffer
, GLuint bufferBit
)
152 /* We can make this cast since the XMesaBuffer wraps GLframebuffer.
153 * GLframebuffer is the first member in a XMesaBuffer struct.
155 XMesaBuffer target
= (XMesaBuffer
) buffer
;
156 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
158 /* This assignment tells the span/point/line/triangle functions
159 * which XMesaBuffer to use.
161 xmesa
->xm_buffer
= target
;
164 * Now determine front vs back color buffer.
166 if (bufferBit
== DD_FRONT_LEFT_BIT
) {
167 target
->buffer
= target
->frontbuffer
;
168 xmesa_update_span_funcs(ctx
);
170 else if (bufferBit
== DD_BACK_LEFT_BIT
) {
171 ASSERT(target
->db_state
);
172 if (target
->backpixmap
) {
173 /* back buffer is a pixmap */
174 target
->buffer
= (XMesaDrawable
) target
->backpixmap
;
176 else if (target
->backimage
) {
177 /* back buffer is an XImage */
178 target
->buffer
= None
;
181 /* No back buffer!!!! Must be out of memory, use front buffer */
182 target
->buffer
= target
->frontbuffer
;
184 xmesa_update_span_funcs(ctx
);
186 else if (bufferBit
& (DD_AUX0_BIT
| DD_AUX1_BIT
| DD_AUX2_BIT
| DD_AUX3_BIT
)) {
187 _swrast_use_aux_buffer(ctx
, buffer
, bufferBit
);
190 _mesa_problem(ctx
, "invalid buffer 0x%x in set_buffer() in xm_dd.c");
198 clear_index( GLcontext
*ctx
, GLuint index
)
200 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
201 xmesa
->clearpixel
= (unsigned long) index
;
202 XMesaSetForeground( xmesa
->display
, xmesa
->xm_draw_buffer
->cleargc
,
203 (unsigned long) index
);
208 clear_color( GLcontext
*ctx
, const GLfloat color
[4] )
210 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
211 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[0], color
[0]);
212 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[1], color
[1]);
213 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[2], color
[2]);
214 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[3], color
[3]);
215 xmesa
->clearpixel
= xmesa_color_to_pixel( xmesa
,
216 xmesa
->clearcolor
[0],
217 xmesa
->clearcolor
[1],
218 xmesa
->clearcolor
[2],
219 xmesa
->clearcolor
[3],
220 xmesa
->xm_visual
->undithered_pf
);
221 _glthread_LOCK_MUTEX(_xmesa_lock
);
222 XMesaSetForeground( xmesa
->display
, xmesa
->xm_draw_buffer
->cleargc
,
224 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
229 /* Set index mask ala glIndexMask */
231 index_mask( GLcontext
*ctx
, GLuint mask
)
233 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
234 if (xmesa
->xm_draw_buffer
->buffer
!= XIMAGE
) {
236 if (mask
==0xffffffff) {
237 m
= ((unsigned long)~0L);
240 m
= (unsigned long) mask
;
242 XMesaSetPlaneMask( xmesa
->display
, xmesa
->xm_draw_buffer
->cleargc
, m
);
243 XMesaSetPlaneMask( xmesa
->display
, xmesa
->xm_draw_buffer
->gc
, m
);
248 /* Implements glColorMask() */
250 color_mask(GLcontext
*ctx
,
251 GLboolean rmask
, GLboolean gmask
, GLboolean bmask
, GLboolean amask
)
253 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
254 const int xclass
= xmesa
->xm_visual
->mesa_visual
.visualType
;
257 if (xclass
== GLX_TRUE_COLOR
|| xclass
== GLX_DIRECT_COLOR
) {
259 if (rmask
&& gmask
&& bmask
) {
260 m
= ((unsigned long)~0L);
264 if (rmask
) m
|= GET_REDMASK(xmesa
->xm_visual
);
265 if (gmask
) m
|= GET_GREENMASK(xmesa
->xm_visual
);
266 if (bmask
) m
|= GET_BLUEMASK(xmesa
->xm_visual
);
268 XMesaSetPlaneMask( xmesa
->display
, xmesa
->xm_draw_buffer
->cleargc
, m
);
269 XMesaSetPlaneMask( xmesa
->display
, xmesa
->xm_draw_buffer
->gc
, m
);
275 /**********************************************************************/
276 /*** glClear implementations ***/
277 /**********************************************************************/
281 clear_front_pixmap( GLcontext
*ctx
, GLboolean all
,
282 GLint x
, GLint y
, GLint width
, GLint height
)
284 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
286 XMesaFillRectangle( xmesa
->display
, xmesa
->xm_draw_buffer
->frontbuffer
,
287 xmesa
->xm_draw_buffer
->cleargc
,
289 xmesa
->xm_draw_buffer
->mesa_buffer
.Width
+ 1,
290 xmesa
->xm_draw_buffer
->mesa_buffer
.Height
+ 1 );
293 XMesaFillRectangle( xmesa
->display
, xmesa
->xm_draw_buffer
->frontbuffer
,
294 xmesa
->xm_draw_buffer
->cleargc
,
295 x
, xmesa
->xm_draw_buffer
->mesa_buffer
.Height
- y
- height
,
302 clear_back_pixmap( GLcontext
*ctx
, GLboolean all
,
303 GLint x
, GLint y
, GLint width
, GLint height
)
305 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
307 XMesaFillRectangle( xmesa
->display
, xmesa
->xm_draw_buffer
->backpixmap
,
308 xmesa
->xm_draw_buffer
->cleargc
,
310 xmesa
->xm_draw_buffer
->mesa_buffer
.Width
+ 1,
311 xmesa
->xm_draw_buffer
->mesa_buffer
.Height
+ 1 );
314 XMesaFillRectangle( xmesa
->display
, xmesa
->xm_draw_buffer
->backpixmap
,
315 xmesa
->xm_draw_buffer
->cleargc
,
316 x
, xmesa
->xm_draw_buffer
->mesa_buffer
.Height
- y
- height
,
323 clear_8bit_ximage( GLcontext
*ctx
, GLboolean all
,
324 GLint x
, GLint y
, GLint width
, GLint height
)
326 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
328 size_t n
= xmesa
->xm_draw_buffer
->backimage
->bytes_per_line
329 * xmesa
->xm_draw_buffer
->backimage
->height
;
330 MEMSET( xmesa
->xm_draw_buffer
->backimage
->data
, xmesa
->clearpixel
, n
);
334 for (i
=0;i
<height
;i
++) {
335 GLubyte
*ptr
= PIXELADDR1( xmesa
->xm_draw_buffer
, x
, y
+i
);
336 MEMSET( ptr
, xmesa
->clearpixel
, width
);
343 clear_HPCR_ximage( GLcontext
*ctx
, GLboolean all
,
344 GLint x
, GLint y
, GLint width
, GLint height
)
346 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
348 GLint i
, c16
= (xmesa
->xm_draw_buffer
->backimage
->bytes_per_line
>>4)<<4;
349 GLubyte
*ptr
= (GLubyte
*)xmesa
->xm_draw_buffer
->backimage
->data
;
350 for (i
=0; i
<xmesa
->xm_draw_buffer
->backimage
->height
; i
++) {
352 GLubyte
*sptr
= xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0];
356 for (j
=0; j
<c16
; j
+=16) {
375 for (; j
<xmesa
->xm_draw_buffer
->backimage
->bytes_per_line
; j
++) {
383 for (i
=y
; i
<y
+height
; i
++) {
384 GLubyte
*ptr
= PIXELADDR1( xmesa
->xm_draw_buffer
, x
, i
);
386 GLubyte
*sptr
= xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0];
390 for (j
=x
; j
<x
+width
; j
++) {
400 clear_16bit_ximage( GLcontext
*ctx
, GLboolean all
,
401 GLint x
, GLint y
, GLint width
, GLint height
)
403 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
404 register GLuint pixel
= (GLuint
) xmesa
->clearpixel
;
405 if (xmesa
->swapbytes
) {
406 pixel
= ((pixel
>> 8) & 0x00ff) | ((pixel
<< 8) & 0xff00);
410 register GLuint
*ptr4
= (GLuint
*) xmesa
->xm_draw_buffer
->backimage
->data
;
411 if ((pixel
& 0xff) == ((pixel
>> 8) & 0xff)) {
412 /* low and high bytes are equal so use memset() */
413 n
= xmesa
->xm_draw_buffer
->backimage
->bytes_per_line
414 * xmesa
->xm_draw_buffer
->mesa_buffer
.Height
;
415 MEMSET( ptr4
, pixel
& 0xff, n
);
418 pixel
= pixel
| (pixel
<<16);
419 n
= xmesa
->xm_draw_buffer
->backimage
->bytes_per_line
420 * xmesa
->xm_draw_buffer
->mesa_buffer
.Height
/ 4;
426 if ((xmesa
->xm_draw_buffer
->backimage
->bytes_per_line
*
427 xmesa
->xm_draw_buffer
->mesa_buffer
.Height
) & 0x2)
428 *(GLushort
*)ptr4
= pixel
& 0xffff;
433 for (j
=0;j
<height
;j
++) {
434 register GLushort
*ptr2
= PIXELADDR2( xmesa
->xm_draw_buffer
, x
, y
+j
);
435 for (i
=0;i
<width
;i
++) {
443 /* Optimized code provided by Nozomi Ytow <noz@xfree86.org> */
445 clear_24bit_ximage( GLcontext
*ctx
, GLboolean all
,
446 GLint x
, GLint y
, GLint width
, GLint height
)
448 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
449 const GLubyte r
= xmesa
->clearcolor
[0];
450 const GLubyte g
= xmesa
->clearcolor
[1];
451 const GLubyte b
= xmesa
->clearcolor
[2];
452 #if 0 /* See below */
453 register GLuint clearPixel
;
454 if (xmesa
->swapbytes
) {
455 clearPixel
= (b
<< 16) | (g
<< 8) | r
;
458 clearPixel
= (r
<< 16) | (g
<< 8) | b
;
464 /* same value for all three components (gray) */
465 const GLint w3
= xmesa
->xm_draw_buffer
->mesa_buffer
.Width
* 3;
466 const GLint h
= xmesa
->xm_draw_buffer
->mesa_buffer
.Height
;
468 for (i
= 0; i
< h
; i
++) {
469 bgr_t
*ptr3
= PIXELADDR3(xmesa
->xm_draw_buffer
, 0, i
);
475 const GLint w
= xmesa
->xm_draw_buffer
->mesa_buffer
.Width
;
476 const GLint h
= xmesa
->xm_draw_buffer
->mesa_buffer
.Height
;
478 for (i
= 0; i
< h
; i
++) {
479 bgr_t
*ptr3
= PIXELADDR3(xmesa
->xm_draw_buffer
, 0, i
);
480 for (j
= 0; j
< w
; j
++) {
487 #if 0 /* this code doesn't work for all window widths */
488 register GLuint
*ptr4
= (GLuint
*) ptr3
;
491 register GLuint
*p
= pixel4
;
492 pixel4
[0] = clearPixel
| (clearPixel
<< 24);
493 pixel4
[1] = (clearPixel
<< 16) | (clearPixel
>> 8);
494 pixel4
[2] = (clearPixel
<< 8) | (clearPixel
>> 16);
495 switch (3 & (int)(ptr3
- (bgr_t
*) ptr4
)){
499 px
= *ptr4
& 0x00ffffff;
500 px
|= pixel4
[0] & 0xff000000;
502 px
= *ptr4
& 0xffff0000;
503 px
|= pixel4
[2] & 0x0000ffff;
508 px
= *ptr4
& 0x0000fffff;
509 px
|= pixel4
[1] & 0xffff0000;
511 px
= *ptr4
& 0xffffff00;
512 px
|= pixel4
[2] & 0x000000ff;
517 px
= *ptr4
& 0x000000ff;
518 px
|= pixel4
[2] & 0xffffff00;
535 px
= *ptr4
& 0xffffff00;
536 px
|= clearPixel
& 0xff;
542 px
= *ptr4
& 0xffff0000;
547 px
= *ptr4
& 0xff000000;
558 /* only clear subrect of color buffer */
560 /* same value for all three components (gray) */
562 for (j
=0;j
<height
;j
++) {
563 bgr_t
*ptr3
= PIXELADDR3( xmesa
->xm_draw_buffer
, x
, y
+j
);
564 MEMSET(ptr3
, r
, 3 * width
);
568 /* non-gray clear color */
570 for (j
= 0; j
< height
; j
++) {
571 bgr_t
*ptr3
= PIXELADDR3( xmesa
->xm_draw_buffer
, x
, y
+j
);
572 for (i
= 0; i
< width
; i
++) {
579 #if 0 /* this code might not always (seems ptr3 always == ptr4) */
582 pixel4
[0] = clearPixel
| (clearPixel
<< 24);
583 pixel4
[1] = (clearPixel
<< 16) | (clearPixel
>> 8);
584 pixel4
[2] = (clearPixel
<< 8) | (clearPixel
>> 16);
585 for (j
=0;j
<height
;j
++) {
586 bgr_t
*ptr3
= PIXELADDR3( xmesa
->xm_draw_buffer
, x
, y
+j
);
587 register GLuint
*ptr4
= (GLuint
*)ptr3
;
588 register GLuint
*p
, px
;
590 switch (3 & (int)(ptr3
- (bgr_t
*) ptr4
)){
594 px
= *ptr4
& 0x00ffffff;
595 px
|= pixel4
[0] & 0xff000000;
597 px
= *ptr4
& 0xffff0000;
598 px
|= pixel4
[2] & 0x0000ffff;
603 px
= *ptr4
& 0x0000fffff;
604 px
|= pixel4
[1] & 0xffff0000;
606 px
= *ptr4
& 0xffffff00;
607 px
|= pixel4
[2] & 0x000000ff;
612 px
= *ptr4
& 0x000000ff;
613 px
|= pixel4
[2] & 0xffffff00;
630 px
= *ptr4
& 0xffffff00;
637 px
= *ptr4
& 0xffff0000;
642 px
= *ptr4
& 0xff000000;
643 px
|= pixel4
[0] & 0xffffff;
657 clear_32bit_ximage( GLcontext
*ctx
, GLboolean all
,
658 GLint x
, GLint y
, GLint width
, GLint height
)
660 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
661 register GLuint pixel
= (GLuint
) xmesa
->clearpixel
;
662 if (xmesa
->swapbytes
) {
663 pixel
= ((pixel
>> 24) & 0x000000ff)
664 | ((pixel
>> 8) & 0x0000ff00)
665 | ((pixel
<< 8) & 0x00ff0000)
666 | ((pixel
<< 24) & 0xff000000);
669 register GLint n
= xmesa
->xm_draw_buffer
->mesa_buffer
.Width
670 * xmesa
->xm_draw_buffer
->mesa_buffer
.Height
;
671 register GLuint
*ptr4
= (GLuint
*) xmesa
->xm_draw_buffer
->backimage
->data
;
673 MEMSET( ptr4
, pixel
, 4*n
);
684 for (j
=0;j
<height
;j
++) {
685 register GLuint
*ptr4
= PIXELADDR4( xmesa
->xm_draw_buffer
, x
, y
+j
);
686 for (i
=0;i
<width
;i
++) {
695 clear_nbit_ximage( GLcontext
*ctx
, GLboolean all
,
696 GLint x
, GLint y
, GLint width
, GLint height
)
698 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
699 XMesaImage
*img
= xmesa
->xm_draw_buffer
->backimage
;
702 /* We can ignore 'all' here - x, y, width, height are always right */
705 /* TODO: optimize this */
706 y
= FLIP(xmesa
->xm_draw_buffer
, y
);
707 for (j
= 0; j
< height
; j
++) {
708 for (i
= 0; i
< width
; i
++) {
709 XMesaPutPixel(img
, x
+i
, y
-j
, xmesa
->clearpixel
);
717 clear_buffers( GLcontext
*ctx
, GLbitfield mask
,
718 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
720 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
721 const GLuint
*colorMask
= (GLuint
*) &ctx
->Color
.ColorMask
;
723 if ((mask
& (DD_FRONT_LEFT_BIT
| DD_BACK_LEFT_BIT
)) &&
724 xmesa
->xm_draw_buffer
->mesa_buffer
.UseSoftwareAlphaBuffers
&&
725 ctx
->Color
.ColorMask
[ACOMP
]) {
726 _swrast_clear_alpha_buffers(ctx
);
729 /* we can't handle color or index masking */
730 if (*colorMask
== 0xffffffff && ctx
->Color
.IndexMask
== 0xffffffff) {
731 if (mask
& DD_FRONT_LEFT_BIT
) {
732 ASSERT(xmesa
->xm_draw_buffer
->front_clear_func
);
733 (*xmesa
->xm_draw_buffer
->front_clear_func
)( ctx
, all
, x
, y
, width
, height
);
734 mask
&= ~DD_FRONT_LEFT_BIT
;
736 if (mask
& DD_BACK_LEFT_BIT
) {
737 ASSERT(xmesa
->xm_draw_buffer
->back_clear_func
);
738 (*xmesa
->xm_draw_buffer
->back_clear_func
)( ctx
, all
, x
, y
, width
, height
);
739 mask
&= ~DD_BACK_LEFT_BIT
;
744 _swrast_Clear( ctx
, mask
, all
, x
, y
, width
, height
);
749 * When we detect that the user has resized the window this function will
750 * get called. Here we'll reallocate the back buffer, depth buffer,
751 * stencil buffer etc. to match the new window size.
752 * The buffer->Width and buffer->Height values will indicate the new size.
755 xmesa_resize_buffers( GLframebuffer
*buffer
)
757 int height
= (int) buffer
->Height
;
758 /* We can do this cast because the first field in the XMesaBuffer
759 * struct is a GLframebuffer struct. If this weren't true, we'd
760 * need a pointer from the GLframebuffer to the XMesaBuffer.
762 XMesaBuffer xmBuffer
= (XMesaBuffer
) buffer
;
764 xmesa_alloc_back_buffer( xmBuffer
);
766 /* Needed by FLIP macro */
767 xmBuffer
->bottom
= height
- 1;
769 if (xmBuffer
->backimage
) {
770 /* Needed by PIXELADDR1 macro */
771 xmBuffer
->ximage_width1
= xmBuffer
->backimage
->bytes_per_line
;
772 xmBuffer
->ximage_origin1
= (GLubyte
*) xmBuffer
->backimage
->data
773 + xmBuffer
->ximage_width1
* (height
-1);
775 /* Needed by PIXELADDR2 macro */
776 xmBuffer
->ximage_width2
= xmBuffer
->backimage
->bytes_per_line
/ 2;
777 xmBuffer
->ximage_origin2
= (GLushort
*) xmBuffer
->backimage
->data
778 + xmBuffer
->ximage_width2
* (height
-1);
780 /* Needed by PIXELADDR3 macro */
781 xmBuffer
->ximage_width3
= xmBuffer
->backimage
->bytes_per_line
;
782 xmBuffer
->ximage_origin3
= (GLubyte
*) xmBuffer
->backimage
->data
783 + xmBuffer
->ximage_width3
* (height
-1);
785 /* Needed by PIXELADDR4 macro */
786 xmBuffer
->ximage_width4
= xmBuffer
->backimage
->width
;
787 xmBuffer
->ximage_origin4
= (GLuint
*) xmBuffer
->backimage
->data
788 + xmBuffer
->ximage_width4
* (height
-1);
791 _swrast_alloc_buffers( buffer
);
795 #ifndef XFree86Server
796 /* XXX this was never tested in the Xserver environment */
799 * This function implements glDrawPixels() with an XPutImage call when
800 * drawing to the front buffer (X Window drawable).
801 * The image format must be GL_BGRA to match the PF_8R8G8B pixel format.
804 xmesa_DrawPixels_8R8G8B( GLcontext
*ctx
,
805 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
806 GLenum format
, GLenum type
,
807 const struct gl_pixelstore_attrib
*unpack
,
808 const GLvoid
*pixels
)
810 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
811 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
812 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
813 const XMesaDrawable buffer
= xmesa
->xm_draw_buffer
->buffer
;
814 const XMesaGC gc
= xmesa
->xm_draw_buffer
->gc
;
818 ASSERT(xmesa
->xm_visual
->dithered_pf
== PF_8R8G8B
);
819 ASSERT(xmesa
->xm_visual
->undithered_pf
== PF_8R8G8B
);
821 if (swrast
->NewState
)
822 _swrast_validate_derived( ctx
);
824 if (buffer
&& /* buffer != 0 means it's a Window or Pixmap */
826 type
== GL_UNSIGNED_BYTE
&&
827 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
828 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
829 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
830 ctx
->Pixel
.ZoomY
== 1.0) {
835 int srcX
= unpack
->SkipPixels
;
836 int srcY
= unpack
->SkipRows
;
837 int rowLength
= unpack
->RowLength
? unpack
->RowLength
: width
;
839 if (unpack
->BufferObj
->Name
) {
840 /* unpack from PBO */
842 if (!_mesa_validate_pbo_access(2, unpack
, width
, height
, 1,
843 format
, type
, pixels
)) {
844 _mesa_error(ctx
, GL_INVALID_OPERATION
,
845 "glDrawPixels(invalid PBO access)");
848 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
,
849 GL_PIXEL_UNPACK_BUFFER_EXT
,
853 /* buffer is already mapped - that's an error */
854 _mesa_error(ctx
, GL_INVALID_OPERATION
,
855 "glDrawPixels(PBO is mapped)");
858 pixels
= ADD_POINTERS(buf
, pixels
);
861 if (_mesa_clip_drawpixels(ctx
, &dstX
, &dstY
, &w
, &h
, &srcX
, &srcY
)) {
862 /* This is a little tricky since all coordinates up to now have
863 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
864 * so we have to carefully compute the Y coordinates/addresses here.
867 MEMSET(&ximage
, 0, sizeof(XMesaImage
));
868 ximage
.width
= width
;
869 ximage
.height
= height
;
870 ximage
.format
= ZPixmap
;
871 ximage
.data
= (char *) pixels
872 + ((srcY
+ h
- 1) * rowLength
+ srcX
) * 4;
873 ximage
.byte_order
= LSBFirst
;
874 ximage
.bitmap_unit
= 32;
875 ximage
.bitmap_bit_order
= LSBFirst
;
876 ximage
.bitmap_pad
= 32;
878 ximage
.bytes_per_line
= -rowLength
* 4; /* negative to flip image */
879 ximage
.bits_per_pixel
= 32;
880 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
881 /* flip Y axis for dest position */
882 dstY
= FLIP(xmesa
->xm_draw_buffer
, dstY
) - h
+ 1;
883 XPutImage(dpy
, buffer
, gc
, &ximage
, 0, 0, dstX
, dstY
, w
, h
);
886 if (unpack
->BufferObj
->Name
) {
887 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
892 /* software fallback */
893 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
894 format
, type
, unpack
, pixels
);
901 * This function implements glDrawPixels() with an XPutImage call when
902 * drawing to the front buffer (X Window drawable). The image format
903 * must be GL_RGB and image type must be GL_UNSIGNED_SHORT_5_6_5 to
904 * match the PF_5R6G5B pixel format.
907 xmesa_DrawPixels_5R6G5B( GLcontext
*ctx
,
908 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
909 GLenum format
, GLenum type
,
910 const struct gl_pixelstore_attrib
*unpack
,
911 const GLvoid
*pixels
)
913 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
914 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
915 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
916 const XMesaDrawable buffer
= xmesa
->xm_draw_buffer
->buffer
;
917 const XMesaGC gc
= xmesa
->xm_draw_buffer
->gc
;
921 ASSERT(xmesa
->xm_visual
->undithered_pf
== PF_5R6G5B
);
923 if (swrast
->NewState
)
924 _swrast_validate_derived( ctx
);
926 if (buffer
&& /* buffer != 0 means it's a Window or Pixmap */
928 type
== GL_UNSIGNED_SHORT_5_6_5
&&
929 !ctx
->Color
.DitherFlag
&& /* no dithering */
930 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
931 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
932 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
933 ctx
->Pixel
.ZoomY
== 1.0) {
938 int srcX
= unpack
->SkipPixels
;
939 int srcY
= unpack
->SkipRows
;
940 int rowLength
= unpack
->RowLength
? unpack
->RowLength
: width
;
942 if (unpack
->BufferObj
->Name
) {
943 /* unpack from PBO */
945 if (!_mesa_validate_pbo_access(2, unpack
, width
, height
, 1,
946 format
, type
, pixels
)) {
947 _mesa_error(ctx
, GL_INVALID_OPERATION
,
948 "glDrawPixels(invalid PBO access)");
951 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
,
952 GL_PIXEL_UNPACK_BUFFER_EXT
,
956 /* buffer is already mapped - that's an error */
957 _mesa_error(ctx
, GL_INVALID_OPERATION
,
958 "glDrawPixels(PBO is mapped)");
961 pixels
= ADD_POINTERS(buf
, pixels
);
964 if (_mesa_clip_drawpixels(ctx
, &dstX
, &dstY
, &w
, &h
, &srcX
, &srcY
)) {
965 /* This is a little tricky since all coordinates up to now have
966 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
967 * so we have to carefully compute the Y coordinates/addresses here.
970 MEMSET(&ximage
, 0, sizeof(XMesaImage
));
971 ximage
.width
= width
;
972 ximage
.height
= height
;
973 ximage
.format
= ZPixmap
;
974 ximage
.data
= (char *) pixels
975 + ((srcY
+ h
- 1) * rowLength
+ srcX
) * 2;
976 ximage
.byte_order
= LSBFirst
;
977 ximage
.bitmap_unit
= 16;
978 ximage
.bitmap_bit_order
= LSBFirst
;
979 ximage
.bitmap_pad
= 16;
981 ximage
.bytes_per_line
= -rowLength
* 2; /* negative to flip image */
982 ximage
.bits_per_pixel
= 16;
983 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
984 /* flip Y axis for dest position */
985 dstY
= FLIP(xmesa
->xm_draw_buffer
, dstY
) - h
+ 1;
986 XPutImage(dpy
, buffer
, gc
, &ximage
, 0, 0, dstX
, dstY
, w
, h
);
989 if (unpack
->BufferObj
->Name
) {
990 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
995 /* software fallback */
996 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
997 format
, type
, unpack
, pixels
);
1004 * Implement glCopyPixels for the front color buffer (or back buffer Pixmap)
1005 * for the color buffer. Don't support zooming, pixel transfer, etc.
1006 * We do support copying from one window to another, ala glXMakeCurrentRead.
1009 xmesa_CopyPixels( GLcontext
*ctx
,
1010 GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
1011 GLint destx
, GLint desty
, GLenum type
)
1013 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
1014 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
1015 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
1016 const XMesaDrawable drawBuffer
= xmesa
->xm_draw_buffer
->buffer
;
1017 const XMesaDrawable readBuffer
= xmesa
->xm_read_buffer
->buffer
;
1018 const XMesaGC gc
= xmesa
->xm_draw_buffer
->gc
;
1023 if (swrast
->NewState
)
1024 _swrast_validate_derived( ctx
);
1026 if (ctx
->Color
.DrawBuffer
[0] == GL_FRONT
&&
1027 ctx
->Pixel
.ReadBuffer
== GL_FRONT
&&
1028 drawBuffer
&& /* buffer != 0 means it's a Window or Pixmap */
1031 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
1032 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
1033 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
1034 ctx
->Pixel
.ZoomY
== 1.0) {
1035 /* Note: we don't do any special clipping work here. We could,
1036 * but X will do it for us.
1038 srcy
= FLIP(xmesa
->xm_read_buffer
, srcy
) - height
+ 1;
1039 desty
= FLIP(xmesa
->xm_draw_buffer
, desty
) - height
+ 1;
1040 XCopyArea(dpy
, readBuffer
, drawBuffer
, gc
,
1041 srcx
, srcy
, width
, height
, destx
, desty
);
1044 _swrast_CopyPixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
, type
);
1047 #endif /* XFree86Server */
1052 * Every driver should implement a GetString function in order to
1053 * return a meaningful GL_RENDERER string.
1055 static const GLubyte
*
1056 get_string( GLcontext
*ctx
, GLenum name
)
1061 #ifdef XFree86Server
1062 return (const GLubyte
*) "Mesa GLX Indirect";
1064 return (const GLubyte
*) "Mesa X11";
1067 #ifdef XFree86Server
1068 return (const GLubyte
*) "Mesa project: www.mesa3d.org";
1079 * We implement the glEnable function only because we care about
1080 * dither enable/disable.
1083 enable( GLcontext
*ctx
, GLenum pname
, GLboolean state
)
1085 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
1090 xmesa
->pixelformat
= xmesa
->xm_visual
->dithered_pf
;
1092 xmesa
->pixelformat
= xmesa
->xm_visual
->undithered_pf
;
1095 ; /* silence compiler warning */
1100 void xmesa_update_state( GLcontext
*ctx
, GLuint new_state
)
1102 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
1104 /* Propogate statechange information to swrast and swrast_setup
1105 * modules. The X11 driver has no internal GL-dependent state.
1107 _swrast_InvalidateState( ctx
, new_state
);
1108 _ac_InvalidateState( ctx
, new_state
);
1109 _tnl_InvalidateState( ctx
, new_state
);
1110 _swsetup_InvalidateState( ctx
, new_state
);
1113 /* setup pointers to front and back buffer clear functions */
1114 xmesa
->xm_draw_buffer
->front_clear_func
= clear_front_pixmap
;
1115 if (xmesa
->xm_draw_buffer
->backpixmap
!= XIMAGE
) {
1116 xmesa
->xm_draw_buffer
->back_clear_func
= clear_back_pixmap
;
1118 else if (sizeof(GLushort
)!=2 || sizeof(GLuint
)!=4) {
1119 xmesa
->xm_draw_buffer
->back_clear_func
= clear_nbit_ximage
;
1121 else switch (xmesa
->xm_visual
->BitsPerPixel
) {
1123 if (xmesa
->xm_visual
->hpcr_clear_flag
) {
1124 xmesa
->xm_draw_buffer
->back_clear_func
= clear_HPCR_ximage
;
1127 xmesa
->xm_draw_buffer
->back_clear_func
= clear_8bit_ximage
;
1131 xmesa
->xm_draw_buffer
->back_clear_func
= clear_16bit_ximage
;
1134 xmesa
->xm_draw_buffer
->back_clear_func
= clear_24bit_ximage
;
1137 xmesa
->xm_draw_buffer
->back_clear_func
= clear_32bit_ximage
;
1140 xmesa
->xm_draw_buffer
->back_clear_func
= clear_nbit_ximage
;
1144 if (ctx
->Color
._DrawDestMask
[0] & (DD_FRONT_LEFT_BIT
| DD_BACK_LEFT_BIT
)) {
1145 xmesa_update_span_funcs(ctx
);
1152 * Called via ctx->Driver.TestProxyTeximage(). Normally, we'd just use
1153 * the _mesa_test_proxy_teximage() fallback function, but we're going to
1154 * special-case the 3D texture case to allow textures up to 512x512x32
1158 test_proxy_teximage(GLcontext
*ctx
, GLenum target
, GLint level
,
1159 GLint internalFormat
, GLenum format
, GLenum type
,
1160 GLint width
, GLint height
, GLint depth
, GLint border
)
1162 if (target
== GL_PROXY_TEXTURE_3D
) {
1163 /* special case for 3D textures */
1164 if (width
* height
* depth
> 512 * 512 * 64 ||
1165 width
< 2 * border
||
1166 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1167 _mesa_bitcount(width
- 2 * border
) != 1) ||
1168 height
< 2 * border
||
1169 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1170 _mesa_bitcount(height
- 2 * border
) != 1) ||
1171 depth
< 2 * border
||
1172 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1173 _mesa_bitcount(depth
- 2 * border
) != 1)) {
1174 /* Bad size, or too many texels */
1180 /* use the fallback routine for 1D, 2D, cube and rect targets */
1181 return _mesa_test_proxy_teximage(ctx
, target
, level
, internalFormat
,
1182 format
, type
, width
, height
, depth
,
1189 * In SW, we don't really compress GL_COMPRESSED_RGB[A] textures!
1191 static const struct gl_texture_format
*
1192 choose_tex_format( GLcontext
*ctx
, GLint internalFormat
,
1193 GLenum format
, GLenum type
)
1195 switch (internalFormat
) {
1196 case GL_COMPRESSED_RGB_ARB
:
1197 return &_mesa_texformat_rgb
;
1198 case GL_COMPRESSED_RGBA_ARB
:
1199 return &_mesa_texformat_rgba
;
1201 return _mesa_choose_tex_format(ctx
, internalFormat
, format
, type
);
1207 * Called by glViewport.
1208 * This is a good time for us to poll the current X window size and adjust
1209 * our ancillary (back color, depth, stencil, etc) buffers to match the
1210 * current window size. Remember, we have no opportunity to respond to
1211 * conventional X Resize/StructureNotify events since the X driver has no
1212 * event loop. Thus, we poll.
1213 * Note that this trick isn't fool-proof. If the application never calls
1214 * glViewport, our notion of the current window size may be incorrect.
1217 xmesa_viewport(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei w
, GLsizei h
)
1219 _mesa_ResizeBuffersMESA();
1224 * Initialize the device driver function table with the functions
1225 * we implement in this driver.
1228 xmesa_init_driver_functions( XMesaVisual xmvisual
,
1229 struct dd_function_table
*driver
)
1231 driver
->GetString
= get_string
;
1232 driver
->UpdateState
= xmesa_update_state
;
1233 driver
->GetBufferSize
= get_buffer_size
;
1234 driver
->Flush
= finish_or_flush
;
1235 driver
->Finish
= finish_or_flush
;
1236 driver
->ClearIndex
= clear_index
;
1237 driver
->ClearColor
= clear_color
;
1238 driver
->IndexMask
= index_mask
;
1239 driver
->ColorMask
= color_mask
;
1240 driver
->Enable
= enable
;
1241 driver
->Clear
= clear_buffers
;
1242 driver
->ResizeBuffers
= xmesa_resize_buffers
;
1243 driver
->Viewport
= xmesa_viewport
;
1244 #ifndef XFree86Server
1245 driver
->CopyPixels
= xmesa_CopyPixels
;
1246 if (xmvisual
->undithered_pf
== PF_8R8G8B
&&
1247 xmvisual
->dithered_pf
== PF_8R8G8B
) {
1248 driver
->DrawPixels
= xmesa_DrawPixels_8R8G8B
;
1250 else if (xmvisual
->undithered_pf
== PF_5R6G5B
) {
1251 driver
->DrawPixels
= xmesa_DrawPixels_5R6G5B
;
1254 driver
->TestProxyTexImage
= test_proxy_teximage
;
1256 driver
->ChooseTextureFormat
= choose_tex_format
;
1258 (void) choose_tex_format
;
1263 #define XMESA_NEW_POINT (_NEW_POINT | \
1265 _SWRAST_NEW_RASTERMASK)
1267 #define XMESA_NEW_LINE (_NEW_LINE | \
1272 _SWRAST_NEW_RASTERMASK)
1274 #define XMESA_NEW_TRIANGLE (_NEW_POLYGON | \
1279 _SWRAST_NEW_RASTERMASK)
1282 /* Extend the software rasterizer with our line/point/triangle
1285 void xmesa_register_swrast_functions( GLcontext
*ctx
)
1287 SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
1288 struct swrast_device_driver
*dd
= _swrast_GetDeviceDriverReference(ctx
);
1290 dd
->SetBuffer
= xmesa_set_buffer
;
1292 swrast
->choose_point
= xmesa_choose_point
;
1293 swrast
->choose_line
= xmesa_choose_line
;
1294 swrast
->choose_triangle
= xmesa_choose_triangle
;
1296 swrast
->invalidate_point
|= XMESA_NEW_POINT
;
1297 swrast
->invalidate_line
|= XMESA_NEW_LINE
;
1298 swrast
->invalidate_triangle
|= XMESA_NEW_TRIANGLE
;