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 if (xmBuffer
->frontbuffer
->width
> MAX_WIDTH
||
109 xmBuffer
->frontbuffer
->height
> MAX_HEIGHT
) {
110 winwidth
= buffer
->Width
;
111 winheight
= buffer
->Height
;
113 winwidth
= xmBuffer
->frontbuffer
->width
;
114 winheight
= xmBuffer
->frontbuffer
->height
;
121 _glthread_LOCK_MUTEX(_xmesa_lock
);
122 XSync(xmBuffer
->xm_visual
->display
, 0); /* added for Chromium */
123 XGetGeometry( xmBuffer
->xm_visual
->display
, xmBuffer
->frontbuffer
, &root
,
124 &winx
, &winy
, &winwidth
, &winheight
, &bw
, &d
);
125 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
134 finish_or_flush( GLcontext
*ctx
)
139 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
141 _glthread_LOCK_MUTEX(_xmesa_lock
);
142 XSync( xmesa
->display
, False
);
143 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
151 * This chooses the color buffer for reading and writing spans, points,
152 * lines, and triangles.
155 xmesa_set_buffer( GLcontext
*ctx
, GLframebuffer
*buffer
, GLuint bufferBit
)
157 /* We can make this cast since the XMesaBuffer wraps GLframebuffer.
158 * GLframebuffer is the first member in a XMesaBuffer struct.
160 XMesaBuffer target
= (XMesaBuffer
) buffer
;
161 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
163 /* This assignment tells the span/point/line/triangle functions
164 * which XMesaBuffer to use.
166 xmesa
->xm_buffer
= target
;
169 * Now determine front vs back color buffer.
171 if (bufferBit
== DD_FRONT_LEFT_BIT
) {
172 target
->buffer
= target
->frontbuffer
;
173 xmesa_update_span_funcs(ctx
);
175 else if (bufferBit
== DD_BACK_LEFT_BIT
) {
176 ASSERT(target
->db_state
);
177 if (target
->backpixmap
) {
178 /* back buffer is a pixmap */
179 target
->buffer
= (XMesaDrawable
) target
->backpixmap
;
181 else if (target
->backimage
) {
182 /* back buffer is an XImage */
183 target
->buffer
= None
;
186 /* No back buffer!!!! Must be out of memory, use front buffer */
187 target
->buffer
= target
->frontbuffer
;
189 xmesa_update_span_funcs(ctx
);
191 else if (bufferBit
& (DD_AUX0_BIT
| DD_AUX1_BIT
| DD_AUX2_BIT
| DD_AUX3_BIT
)) {
192 _swrast_use_aux_buffer(ctx
, buffer
, bufferBit
);
195 _mesa_problem(ctx
, "invalid buffer 0x%x in set_buffer() in xm_dd.c");
203 clear_index( GLcontext
*ctx
, GLuint index
)
205 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
206 xmesa
->clearpixel
= (unsigned long) index
;
207 XMesaSetForeground( xmesa
->display
, xmesa
->xm_draw_buffer
->cleargc
,
208 (unsigned long) index
);
213 clear_color( GLcontext
*ctx
, const GLfloat color
[4] )
215 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
216 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[0], color
[0]);
217 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[1], color
[1]);
218 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[2], color
[2]);
219 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[3], color
[3]);
220 xmesa
->clearpixel
= xmesa_color_to_pixel( xmesa
,
221 xmesa
->clearcolor
[0],
222 xmesa
->clearcolor
[1],
223 xmesa
->clearcolor
[2],
224 xmesa
->clearcolor
[3],
225 xmesa
->xm_visual
->undithered_pf
);
226 _glthread_LOCK_MUTEX(_xmesa_lock
);
227 XMesaSetForeground( xmesa
->display
, xmesa
->xm_draw_buffer
->cleargc
,
229 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
234 /* Set index mask ala glIndexMask */
236 index_mask( GLcontext
*ctx
, GLuint mask
)
238 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
239 if (xmesa
->xm_draw_buffer
->buffer
!= XIMAGE
) {
241 if (mask
==0xffffffff) {
242 m
= ((unsigned long)~0L);
245 m
= (unsigned long) mask
;
247 XMesaSetPlaneMask( xmesa
->display
, xmesa
->xm_draw_buffer
->cleargc
, m
);
248 XMesaSetPlaneMask( xmesa
->display
, xmesa
->xm_draw_buffer
->gc
, m
);
253 /* Implements glColorMask() */
255 color_mask(GLcontext
*ctx
,
256 GLboolean rmask
, GLboolean gmask
, GLboolean bmask
, GLboolean amask
)
258 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
259 const int xclass
= xmesa
->xm_visual
->mesa_visual
.visualType
;
262 if (xclass
== GLX_TRUE_COLOR
|| xclass
== GLX_DIRECT_COLOR
) {
264 if (rmask
&& gmask
&& bmask
) {
265 m
= ((unsigned long)~0L);
269 if (rmask
) m
|= GET_REDMASK(xmesa
->xm_visual
);
270 if (gmask
) m
|= GET_GREENMASK(xmesa
->xm_visual
);
271 if (bmask
) m
|= GET_BLUEMASK(xmesa
->xm_visual
);
273 XMesaSetPlaneMask( xmesa
->display
, xmesa
->xm_draw_buffer
->cleargc
, m
);
274 XMesaSetPlaneMask( xmesa
->display
, xmesa
->xm_draw_buffer
->gc
, m
);
280 /**********************************************************************/
281 /*** glClear implementations ***/
282 /**********************************************************************/
286 clear_front_pixmap( GLcontext
*ctx
, GLboolean all
,
287 GLint x
, GLint y
, GLint width
, GLint height
)
289 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
291 XMesaFillRectangle( xmesa
->display
, xmesa
->xm_draw_buffer
->frontbuffer
,
292 xmesa
->xm_draw_buffer
->cleargc
,
294 xmesa
->xm_draw_buffer
->width
+1,
295 xmesa
->xm_draw_buffer
->height
+1 );
298 XMesaFillRectangle( xmesa
->display
, xmesa
->xm_draw_buffer
->frontbuffer
,
299 xmesa
->xm_draw_buffer
->cleargc
,
300 x
, xmesa
->xm_draw_buffer
->height
- y
- height
,
307 clear_back_pixmap( GLcontext
*ctx
, GLboolean all
,
308 GLint x
, GLint y
, GLint width
, GLint height
)
310 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
312 XMesaFillRectangle( xmesa
->display
, xmesa
->xm_draw_buffer
->backpixmap
,
313 xmesa
->xm_draw_buffer
->cleargc
,
315 xmesa
->xm_draw_buffer
->width
+1,
316 xmesa
->xm_draw_buffer
->height
+1 );
319 XMesaFillRectangle( xmesa
->display
, xmesa
->xm_draw_buffer
->backpixmap
,
320 xmesa
->xm_draw_buffer
->cleargc
,
321 x
, xmesa
->xm_draw_buffer
->height
- y
- height
,
328 clear_8bit_ximage( GLcontext
*ctx
, GLboolean all
,
329 GLint x
, GLint y
, GLint width
, GLint height
)
331 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
333 size_t n
= xmesa
->xm_draw_buffer
->backimage
->bytes_per_line
334 * xmesa
->xm_draw_buffer
->backimage
->height
;
335 MEMSET( xmesa
->xm_draw_buffer
->backimage
->data
, xmesa
->clearpixel
, n
);
339 for (i
=0;i
<height
;i
++) {
340 GLubyte
*ptr
= PIXELADDR1( xmesa
->xm_draw_buffer
, x
, y
+i
);
341 MEMSET( ptr
, xmesa
->clearpixel
, width
);
348 clear_HPCR_ximage( GLcontext
*ctx
, GLboolean all
,
349 GLint x
, GLint y
, GLint width
, GLint height
)
351 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
353 GLint i
, c16
= (xmesa
->xm_draw_buffer
->backimage
->bytes_per_line
>>4)<<4;
354 GLubyte
*ptr
= (GLubyte
*)xmesa
->xm_draw_buffer
->backimage
->data
;
355 for (i
=0; i
<xmesa
->xm_draw_buffer
->backimage
->height
; i
++) {
357 GLubyte
*sptr
= xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0];
361 for (j
=0; j
<c16
; j
+=16) {
380 for (; j
<xmesa
->xm_draw_buffer
->backimage
->bytes_per_line
; j
++) {
388 for (i
=y
; i
<y
+height
; i
++) {
389 GLubyte
*ptr
= PIXELADDR1( xmesa
->xm_draw_buffer
, x
, i
);
391 GLubyte
*sptr
= xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0];
395 for (j
=x
; j
<x
+width
; j
++) {
405 clear_16bit_ximage( GLcontext
*ctx
, GLboolean all
,
406 GLint x
, GLint y
, GLint width
, GLint height
)
408 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
409 register GLuint pixel
= (GLuint
) xmesa
->clearpixel
;
410 if (xmesa
->swapbytes
) {
411 pixel
= ((pixel
>> 8) & 0x00ff) | ((pixel
<< 8) & 0xff00);
415 register GLuint
*ptr4
= (GLuint
*) xmesa
->xm_draw_buffer
->backimage
->data
;
416 if ((pixel
& 0xff) == ((pixel
>> 8) & 0xff)) {
417 /* low and high bytes are equal so use memset() */
418 n
= xmesa
->xm_draw_buffer
->backimage
->bytes_per_line
419 * xmesa
->xm_draw_buffer
->height
;
420 MEMSET( ptr4
, pixel
& 0xff, n
);
423 pixel
= pixel
| (pixel
<<16);
424 n
= xmesa
->xm_draw_buffer
->backimage
->bytes_per_line
425 * xmesa
->xm_draw_buffer
->height
/ 4;
431 if ((xmesa
->xm_draw_buffer
->backimage
->bytes_per_line
*
432 xmesa
->xm_draw_buffer
->height
) & 0x2)
433 *(GLushort
*)ptr4
= pixel
& 0xffff;
438 for (j
=0;j
<height
;j
++) {
439 register GLushort
*ptr2
= PIXELADDR2( xmesa
->xm_draw_buffer
, x
, y
+j
);
440 for (i
=0;i
<width
;i
++) {
448 /* Optimized code provided by Nozomi Ytow <noz@xfree86.org> */
450 clear_24bit_ximage( GLcontext
*ctx
, GLboolean all
,
451 GLint x
, GLint y
, GLint width
, GLint height
)
453 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
454 const GLubyte r
= xmesa
->clearcolor
[0];
455 const GLubyte g
= xmesa
->clearcolor
[1];
456 const GLubyte b
= xmesa
->clearcolor
[2];
457 #if 0 /* See below */
458 register GLuint clearPixel
;
459 if (xmesa
->swapbytes
) {
460 clearPixel
= (b
<< 16) | (g
<< 8) | r
;
463 clearPixel
= (r
<< 16) | (g
<< 8) | b
;
469 /* same value for all three components (gray) */
470 const GLint w3
= xmesa
->xm_draw_buffer
->width
* 3;
471 const GLint h
= xmesa
->xm_draw_buffer
->height
;
473 for (i
= 0; i
< h
; i
++) {
474 bgr_t
*ptr3
= PIXELADDR3(xmesa
->xm_draw_buffer
, 0, i
);
480 const GLint w
= xmesa
->xm_draw_buffer
->width
;
481 const GLint h
= xmesa
->xm_draw_buffer
->height
;
483 for (i
= 0; i
< h
; i
++) {
484 bgr_t
*ptr3
= PIXELADDR3(xmesa
->xm_draw_buffer
, 0, i
);
485 for (j
= 0; j
< w
; j
++) {
492 #if 0 /* this code doesn't work for all window widths */
493 register GLuint
*ptr4
= (GLuint
*) ptr3
;
496 register GLuint
*p
= pixel4
;
497 pixel4
[0] = clearPixel
| (clearPixel
<< 24);
498 pixel4
[1] = (clearPixel
<< 16) | (clearPixel
>> 8);
499 pixel4
[2] = (clearPixel
<< 8) | (clearPixel
>> 16);
500 switch (3 & (int)(ptr3
- (bgr_t
*) ptr4
)){
504 px
= *ptr4
& 0x00ffffff;
505 px
|= pixel4
[0] & 0xff000000;
507 px
= *ptr4
& 0xffff0000;
508 px
|= pixel4
[2] & 0x0000ffff;
513 px
= *ptr4
& 0x0000fffff;
514 px
|= pixel4
[1] & 0xffff0000;
516 px
= *ptr4
& 0xffffff00;
517 px
|= pixel4
[2] & 0x000000ff;
522 px
= *ptr4
& 0x000000ff;
523 px
|= pixel4
[2] & 0xffffff00;
540 px
= *ptr4
& 0xffffff00;
541 px
|= clearPixel
& 0xff;
547 px
= *ptr4
& 0xffff0000;
552 px
= *ptr4
& 0xff000000;
563 /* only clear subrect of color buffer */
565 /* same value for all three components (gray) */
567 for (j
=0;j
<height
;j
++) {
568 bgr_t
*ptr3
= PIXELADDR3( xmesa
->xm_draw_buffer
, x
, y
+j
);
569 MEMSET(ptr3
, r
, 3 * width
);
573 /* non-gray clear color */
575 for (j
= 0; j
< height
; j
++) {
576 bgr_t
*ptr3
= PIXELADDR3( xmesa
->xm_draw_buffer
, x
, y
+j
);
577 for (i
= 0; i
< width
; i
++) {
584 #if 0 /* this code might not always (seems ptr3 always == ptr4) */
587 pixel4
[0] = clearPixel
| (clearPixel
<< 24);
588 pixel4
[1] = (clearPixel
<< 16) | (clearPixel
>> 8);
589 pixel4
[2] = (clearPixel
<< 8) | (clearPixel
>> 16);
590 for (j
=0;j
<height
;j
++) {
591 bgr_t
*ptr3
= PIXELADDR3( xmesa
->xm_draw_buffer
, x
, y
+j
);
592 register GLuint
*ptr4
= (GLuint
*)ptr3
;
593 register GLuint
*p
, px
;
595 switch (3 & (int)(ptr3
- (bgr_t
*) ptr4
)){
599 px
= *ptr4
& 0x00ffffff;
600 px
|= pixel4
[0] & 0xff000000;
602 px
= *ptr4
& 0xffff0000;
603 px
|= pixel4
[2] & 0x0000ffff;
608 px
= *ptr4
& 0x0000fffff;
609 px
|= pixel4
[1] & 0xffff0000;
611 px
= *ptr4
& 0xffffff00;
612 px
|= pixel4
[2] & 0x000000ff;
617 px
= *ptr4
& 0x000000ff;
618 px
|= pixel4
[2] & 0xffffff00;
635 px
= *ptr4
& 0xffffff00;
642 px
= *ptr4
& 0xffff0000;
647 px
= *ptr4
& 0xff000000;
648 px
|= pixel4
[0] & 0xffffff;
662 clear_32bit_ximage( GLcontext
*ctx
, GLboolean all
,
663 GLint x
, GLint y
, GLint width
, GLint height
)
665 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
666 register GLuint pixel
= (GLuint
) xmesa
->clearpixel
;
667 if (xmesa
->swapbytes
) {
668 pixel
= ((pixel
>> 24) & 0x000000ff)
669 | ((pixel
>> 8) & 0x0000ff00)
670 | ((pixel
<< 8) & 0x00ff0000)
671 | ((pixel
<< 24) & 0xff000000);
674 register GLint n
= xmesa
->xm_draw_buffer
->width
* xmesa
->xm_draw_buffer
->height
;
675 register GLuint
*ptr4
= (GLuint
*) xmesa
->xm_draw_buffer
->backimage
->data
;
677 MEMSET( ptr4
, pixel
, 4*n
);
688 for (j
=0;j
<height
;j
++) {
689 register GLuint
*ptr4
= PIXELADDR4( xmesa
->xm_draw_buffer
, x
, y
+j
);
690 for (i
=0;i
<width
;i
++) {
699 clear_nbit_ximage( GLcontext
*ctx
, GLboolean all
,
700 GLint x
, GLint y
, GLint width
, GLint height
)
702 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
703 XMesaImage
*img
= xmesa
->xm_draw_buffer
->backimage
;
706 width
= xmesa
->xm_draw_buffer
->width
;
707 height
= xmesa
->xm_draw_buffer
->height
;
708 for (j
=0;j
<height
;j
++) {
709 for (i
=0;i
<width
;i
++) {
710 XMesaPutPixel( img
, i
, j
, xmesa
->clearpixel
);
715 /* TODO: optimize this */
717 y
= FLIP(xmesa
->xm_draw_buffer
, y
);
718 for (j
=0;j
<height
;j
++) {
719 for (i
=0;i
<width
;i
++) {
720 XMesaPutPixel( img
, x
+i
, y
-j
, xmesa
->clearpixel
);
729 clear_buffers( GLcontext
*ctx
, GLbitfield mask
,
730 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
732 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
733 const GLuint
*colorMask
= (GLuint
*) &ctx
->Color
.ColorMask
;
735 if ((mask
& (DD_FRONT_LEFT_BIT
| DD_BACK_LEFT_BIT
)) &&
736 xmesa
->xm_draw_buffer
->mesa_buffer
.UseSoftwareAlphaBuffers
&&
737 ctx
->Color
.ColorMask
[ACOMP
]) {
738 _swrast_clear_alpha_buffers(ctx
);
741 /* we can't handle color or index masking */
742 if (*colorMask
== 0xffffffff && ctx
->Color
.IndexMask
== 0xffffffff) {
743 if (mask
& DD_FRONT_LEFT_BIT
) {
744 ASSERT(xmesa
->xm_draw_buffer
->front_clear_func
);
745 (*xmesa
->xm_draw_buffer
->front_clear_func
)( ctx
, all
, x
, y
, width
, height
);
746 mask
&= ~DD_FRONT_LEFT_BIT
;
748 if (mask
& DD_BACK_LEFT_BIT
) {
749 ASSERT(xmesa
->xm_draw_buffer
->back_clear_func
);
750 (*xmesa
->xm_draw_buffer
->back_clear_func
)( ctx
, all
, x
, y
, width
, height
);
751 mask
&= ~DD_BACK_LEFT_BIT
;
756 _swrast_Clear( ctx
, mask
, all
, x
, y
, width
, height
);
761 * When we detect that the user has resized the window this function will
762 * get called. Here we'll reallocate the back buffer, depth buffer,
763 * stencil buffer etc. to match the new window size.
766 xmesa_resize_buffers( GLframebuffer
*buffer
)
768 int height
= (int) buffer
->Height
;
769 /* We can do this cast because the first field in the XMesaBuffer
770 * struct is a GLframebuffer struct. If this weren't true, we'd
771 * need a pointer from the GLframebuffer to the XMesaBuffer.
773 XMesaBuffer xmBuffer
= (XMesaBuffer
) buffer
;
775 xmBuffer
->width
= buffer
->Width
;
776 xmBuffer
->height
= buffer
->Height
;
777 xmesa_alloc_back_buffer( xmBuffer
);
779 /* Needed by FLIP macro */
780 xmBuffer
->bottom
= height
- 1;
782 if (xmBuffer
->backimage
) {
783 /* Needed by PIXELADDR1 macro */
784 xmBuffer
->ximage_width1
= xmBuffer
->backimage
->bytes_per_line
;
785 xmBuffer
->ximage_origin1
= (GLubyte
*) xmBuffer
->backimage
->data
786 + xmBuffer
->ximage_width1
* (height
-1);
788 /* Needed by PIXELADDR2 macro */
789 xmBuffer
->ximage_width2
= xmBuffer
->backimage
->bytes_per_line
/ 2;
790 xmBuffer
->ximage_origin2
= (GLushort
*) xmBuffer
->backimage
->data
791 + xmBuffer
->ximage_width2
* (height
-1);
793 /* Needed by PIXELADDR3 macro */
794 xmBuffer
->ximage_width3
= xmBuffer
->backimage
->bytes_per_line
;
795 xmBuffer
->ximage_origin3
= (GLubyte
*) xmBuffer
->backimage
->data
796 + xmBuffer
->ximage_width3
* (height
-1);
798 /* Needed by PIXELADDR4 macro */
799 xmBuffer
->ximage_width4
= xmBuffer
->backimage
->width
;
800 xmBuffer
->ximage_origin4
= (GLuint
*) xmBuffer
->backimage
->data
801 + xmBuffer
->ximage_width4
* (height
-1);
804 _swrast_alloc_buffers( buffer
);
808 #ifndef XFree86Server
809 /* XXX this was never tested in the Xserver environment */
812 * This function implements glDrawPixels() with an XPutImage call when
813 * drawing to the front buffer (X Window drawable).
814 * The image format must be GL_BGRA to match the PF_8R8G8B pixel format.
817 xmesa_DrawPixels_8R8G8B( GLcontext
*ctx
,
818 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
819 GLenum format
, GLenum type
,
820 const struct gl_pixelstore_attrib
*unpack
,
821 const GLvoid
*pixels
)
823 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
824 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
825 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
826 const XMesaDrawable buffer
= xmesa
->xm_draw_buffer
->buffer
;
827 const XMesaGC gc
= xmesa
->xm_draw_buffer
->gc
;
831 ASSERT(xmesa
->xm_visual
->dithered_pf
== PF_8R8G8B
);
832 ASSERT(xmesa
->xm_visual
->undithered_pf
== PF_8R8G8B
);
834 if (swrast
->NewState
)
835 _swrast_validate_derived( ctx
);
837 if (buffer
&& /* buffer != 0 means it's a Window or Pixmap */
839 type
== GL_UNSIGNED_BYTE
&&
840 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
841 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
842 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
843 ctx
->Pixel
.ZoomY
== 1.0) {
848 int srcX
= unpack
->SkipPixels
;
849 int srcY
= unpack
->SkipRows
;
850 int rowLength
= unpack
->RowLength
? unpack
->RowLength
: width
;
852 pixels
= _swrast_validate_pbo_access(unpack
, width
, height
, 1,
853 format
, type
, (GLvoid
*) pixels
);
857 if (_swrast_clip_pixelrect(ctx
, &dstX
, &dstY
, &w
, &h
, &srcX
, &srcY
)) {
858 /* This is a little tricky since all coordinates up to now have
859 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
860 * so we have to carefully compute the Y coordinates/addresses here.
863 MEMSET(&ximage
, 0, sizeof(XMesaImage
));
864 ximage
.width
= width
;
865 ximage
.height
= height
;
866 ximage
.format
= ZPixmap
;
867 ximage
.data
= (char *) pixels
868 + ((srcY
+ h
- 1) * rowLength
+ srcX
) * 4;
869 ximage
.byte_order
= LSBFirst
;
870 ximage
.bitmap_unit
= 32;
871 ximage
.bitmap_bit_order
= LSBFirst
;
872 ximage
.bitmap_pad
= 32;
874 ximage
.bytes_per_line
= -rowLength
* 4; /* negative to flip image */
875 ximage
.bits_per_pixel
= 32;
876 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
877 /* flip Y axis for dest position */
878 dstY
= FLIP(xmesa
->xm_draw_buffer
, dstY
) - h
+ 1;
879 XPutImage(dpy
, buffer
, gc
, &ximage
, 0, 0, dstX
, dstY
, w
, h
);
883 /* software fallback */
884 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
885 format
, type
, unpack
, pixels
);
892 * This function implements glDrawPixels() with an XPutImage call when
893 * drawing to the front buffer (X Window drawable). The image format
894 * must be GL_RGB and image type must be GL_UNSIGNED_SHORT_5_6_5 to
895 * match the PF_5R6G5B pixel format.
898 xmesa_DrawPixels_5R6G5B( GLcontext
*ctx
,
899 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
900 GLenum format
, GLenum type
,
901 const struct gl_pixelstore_attrib
*unpack
,
902 const GLvoid
*pixels
)
904 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
905 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
906 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
907 const XMesaDrawable buffer
= xmesa
->xm_draw_buffer
->buffer
;
908 const XMesaGC gc
= xmesa
->xm_draw_buffer
->gc
;
912 ASSERT(xmesa
->xm_visual
->undithered_pf
== PF_5R6G5B
);
914 if (swrast
->NewState
)
915 _swrast_validate_derived( ctx
);
917 if (buffer
&& /* buffer != 0 means it's a Window or Pixmap */
919 type
== GL_UNSIGNED_SHORT_5_6_5
&&
920 !ctx
->Color
.DitherFlag
&& /* no dithering */
921 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
922 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
923 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
924 ctx
->Pixel
.ZoomY
== 1.0) {
929 int srcX
= unpack
->SkipPixels
;
930 int srcY
= unpack
->SkipRows
;
931 int rowLength
= unpack
->RowLength
? unpack
->RowLength
: width
;
933 pixels
= _swrast_validate_pbo_access(unpack
, width
, height
, 1,
934 format
, type
, (GLvoid
*) pixels
);
938 if (_swrast_clip_pixelrect(ctx
, &dstX
, &dstY
, &w
, &h
, &srcX
, &srcY
)) {
939 /* This is a little tricky since all coordinates up to now have
940 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
941 * so we have to carefully compute the Y coordinates/addresses here.
944 MEMSET(&ximage
, 0, sizeof(XMesaImage
));
945 ximage
.width
= width
;
946 ximage
.height
= height
;
947 ximage
.format
= ZPixmap
;
948 ximage
.data
= (char *) pixels
949 + ((srcY
+ h
- 1) * rowLength
+ srcX
) * 2;
950 ximage
.byte_order
= LSBFirst
;
951 ximage
.bitmap_unit
= 16;
952 ximage
.bitmap_bit_order
= LSBFirst
;
953 ximage
.bitmap_pad
= 16;
955 ximage
.bytes_per_line
= -rowLength
* 2; /* negative to flip image */
956 ximage
.bits_per_pixel
= 16;
957 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
958 /* flip Y axis for dest position */
959 dstY
= FLIP(xmesa
->xm_draw_buffer
, dstY
) - h
+ 1;
960 XPutImage(dpy
, buffer
, gc
, &ximage
, 0, 0, dstX
, dstY
, w
, h
);
964 /* software fallback */
965 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
966 format
, type
, unpack
, pixels
);
973 * Implement glCopyPixels for the front color buffer (or back buffer Pixmap)
974 * for the color buffer. Don't support zooming, pixel transfer, etc.
975 * We do support copying from one window to another, ala glXMakeCurrentRead.
978 xmesa_CopyPixels( GLcontext
*ctx
,
979 GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
980 GLint destx
, GLint desty
, GLenum type
)
982 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
983 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
984 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
985 const XMesaDrawable drawBuffer
= xmesa
->xm_draw_buffer
->buffer
;
986 const XMesaDrawable readBuffer
= xmesa
->xm_read_buffer
->buffer
;
987 const XMesaGC gc
= xmesa
->xm_draw_buffer
->gc
;
992 if (swrast
->NewState
)
993 _swrast_validate_derived( ctx
);
995 if (ctx
->Color
.DrawBuffer
== GL_FRONT
&&
996 ctx
->Pixel
.ReadBuffer
== GL_FRONT
&&
997 drawBuffer
&& /* buffer != 0 means it's a Window or Pixmap */
1000 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
1001 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
1002 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
1003 ctx
->Pixel
.ZoomY
== 1.0) {
1004 /* Note: we don't do any special clipping work here. We could,
1005 * but X will do it for us.
1007 srcy
= FLIP(xmesa
->xm_read_buffer
, srcy
) - height
+ 1;
1008 desty
= FLIP(xmesa
->xm_draw_buffer
, desty
) - height
+ 1;
1009 XCopyArea(dpy
, readBuffer
, drawBuffer
, gc
,
1010 srcx
, srcy
, width
, height
, destx
, desty
);
1013 _swrast_CopyPixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
, type
);
1016 #endif /* XFree86Server */
1021 * Every driver should implement a GetString function in order to
1022 * return a meaningful GL_RENDERER string.
1024 static const GLubyte
*
1025 get_string( GLcontext
*ctx
, GLenum name
)
1030 #ifdef XFree86Server
1031 return (const GLubyte
*) "Mesa GLX Indirect";
1033 return (const GLubyte
*) "Mesa X11";
1036 #ifdef XFree86Server
1037 return (const GLubyte
*) "Mesa project: www.mesa3d.org";
1048 * We implement the glEnable function only because we care about
1049 * dither enable/disable.
1052 enable( GLcontext
*ctx
, GLenum pname
, GLboolean state
)
1054 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
1059 xmesa
->pixelformat
= xmesa
->xm_visual
->dithered_pf
;
1061 xmesa
->pixelformat
= xmesa
->xm_visual
->undithered_pf
;
1064 ; /* silence compiler warning */
1069 void xmesa_update_state( GLcontext
*ctx
, GLuint new_state
)
1071 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
1073 /* Propogate statechange information to swrast and swrast_setup
1074 * modules. The X11 driver has no internal GL-dependent state.
1076 _swrast_InvalidateState( ctx
, new_state
);
1077 _ac_InvalidateState( ctx
, new_state
);
1078 _tnl_InvalidateState( ctx
, new_state
);
1079 _swsetup_InvalidateState( ctx
, new_state
);
1082 /* setup pointers to front and back buffer clear functions */
1083 xmesa
->xm_draw_buffer
->front_clear_func
= clear_front_pixmap
;
1084 if (xmesa
->xm_draw_buffer
->backpixmap
!= XIMAGE
) {
1085 xmesa
->xm_draw_buffer
->back_clear_func
= clear_back_pixmap
;
1087 else if (sizeof(GLushort
)!=2 || sizeof(GLuint
)!=4) {
1088 xmesa
->xm_draw_buffer
->back_clear_func
= clear_nbit_ximage
;
1090 else switch (xmesa
->xm_visual
->BitsPerPixel
) {
1092 if (xmesa
->xm_visual
->hpcr_clear_flag
) {
1093 xmesa
->xm_draw_buffer
->back_clear_func
= clear_HPCR_ximage
;
1096 xmesa
->xm_draw_buffer
->back_clear_func
= clear_8bit_ximage
;
1100 xmesa
->xm_draw_buffer
->back_clear_func
= clear_16bit_ximage
;
1103 xmesa
->xm_draw_buffer
->back_clear_func
= clear_24bit_ximage
;
1106 xmesa
->xm_draw_buffer
->back_clear_func
= clear_32bit_ximage
;
1109 xmesa
->xm_draw_buffer
->back_clear_func
= clear_nbit_ximage
;
1113 if (ctx
->Color
._DrawDestMask
& (DD_FRONT_LEFT_BIT
| DD_BACK_LEFT_BIT
)) {
1114 xmesa_update_span_funcs(ctx
);
1121 * Called via ctx->Driver.TestProxyTeximage(). Normally, we'd just use
1122 * the _mesa_test_proxy_teximage() fallback function, but we're going to
1123 * special-case the 3D texture case to allow textures up to 512x512x32
1127 test_proxy_teximage(GLcontext
*ctx
, GLenum target
, GLint level
,
1128 GLint internalFormat
, GLenum format
, GLenum type
,
1129 GLint width
, GLint height
, GLint depth
, GLint border
)
1131 if (target
== GL_PROXY_TEXTURE_3D
) {
1132 /* special case for 3D textures */
1133 if (width
* height
* depth
> 512 * 512 * 64 ||
1134 width
< 2 * border
||
1135 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1136 _mesa_bitcount(width
- 2 * border
) != 1) ||
1137 height
< 2 * border
||
1138 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1139 _mesa_bitcount(height
- 2 * border
) != 1) ||
1140 depth
< 2 * border
||
1141 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1142 _mesa_bitcount(depth
- 2 * border
) != 1)) {
1143 /* Bad size, or too many texels */
1149 /* use the fallback routine for 1D, 2D, cube and rect targets */
1150 return _mesa_test_proxy_teximage(ctx
, target
, level
, internalFormat
,
1151 format
, type
, width
, height
, depth
,
1158 * Initialize the device driver function table with the functions
1159 * we implement in this driver.
1161 void xmesa_init_driver_functions( XMesaVisual xmvisual
,
1162 struct dd_function_table
*driver
)
1164 driver
->GetString
= get_string
;
1165 driver
->UpdateState
= xmesa_update_state
;
1166 driver
->GetBufferSize
= get_buffer_size
;
1167 driver
->Flush
= finish_or_flush
;
1168 driver
->Finish
= finish_or_flush
;
1169 driver
->ClearIndex
= clear_index
;
1170 driver
->ClearColor
= clear_color
;
1171 driver
->IndexMask
= index_mask
;
1172 driver
->ColorMask
= color_mask
;
1173 driver
->Enable
= enable
;
1174 driver
->Clear
= clear_buffers
;
1175 driver
->ResizeBuffers
= xmesa_resize_buffers
;
1176 #ifndef XFree86Server
1177 driver
->CopyPixels
= /*_swrast_CopyPixels;*/xmesa_CopyPixels
;
1178 if (xmvisual
->undithered_pf
== PF_8R8G8B
&&
1179 xmvisual
->dithered_pf
== PF_8R8G8B
) {
1180 driver
->DrawPixels
= xmesa_DrawPixels_8R8G8B
;
1182 else if (xmvisual
->undithered_pf
== PF_5R6G5B
) {
1183 driver
->DrawPixels
= xmesa_DrawPixels_5R6G5B
;
1186 driver
->TestProxyTexImage
= test_proxy_teximage
;
1190 #define XMESA_NEW_POINT (_NEW_POINT | \
1192 _SWRAST_NEW_RASTERMASK)
1194 #define XMESA_NEW_LINE (_NEW_LINE | \
1199 _SWRAST_NEW_RASTERMASK)
1201 #define XMESA_NEW_TRIANGLE (_NEW_POLYGON | \
1206 _SWRAST_NEW_RASTERMASK)
1209 /* Extend the software rasterizer with our line/point/triangle
1212 void xmesa_register_swrast_functions( GLcontext
*ctx
)
1214 SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
1215 struct swrast_device_driver
*dd
= _swrast_GetDeviceDriverReference(ctx
);
1217 dd
->SetBuffer
= xmesa_set_buffer
;
1219 swrast
->choose_point
= xmesa_choose_point
;
1220 swrast
->choose_line
= xmesa_choose_line
;
1221 swrast
->choose_triangle
= xmesa_choose_triangle
;
1223 swrast
->invalidate_point
|= XMESA_NEW_POINT
;
1224 swrast
->invalidate_line
|= XMESA_NEW_LINE
;
1225 swrast
->invalidate_triangle
|= XMESA_NEW_TRIANGLE
;