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.
148 * Remember that a GLframebuffer has several distinct color buffers:
149 * front/left, front/right, back/left, back/right and aux buffers.
150 * The bufferBit specifies which one to use.
153 xmesa_set_buffer( GLcontext
*ctx
, GLframebuffer
*buffer
, GLuint bufferBit
)
155 /* We can make this cast since the XMesaBuffer wraps GLframebuffer.
156 * GLframebuffer is the first member in a XMesaBuffer struct.
158 XMesaBuffer target
= (XMesaBuffer
) buffer
;
159 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
161 /* This assignment tells the span/point/line/triangle functions
162 * which XMesaBuffer to use.
164 xmesa
->xm_buffer
= target
;
167 * Now determine front vs back color buffer.
169 if (bufferBit
== DD_FRONT_LEFT_BIT
) {
170 target
->buffer
= target
->frontbuffer
;
171 xmesa_update_span_funcs(ctx
);
173 else if (bufferBit
== DD_BACK_LEFT_BIT
) {
174 ASSERT(target
->db_state
);
175 if (target
->backpixmap
) {
176 /* back buffer is a pixmap */
177 target
->buffer
= (XMesaDrawable
) target
->backpixmap
;
179 else if (target
->backimage
) {
180 /* back buffer is an XImage */
181 target
->buffer
= None
;
184 /* No back buffer!!!! Must be out of memory, use front buffer */
185 target
->buffer
= target
->frontbuffer
;
187 xmesa_update_span_funcs(ctx
);
189 else if (bufferBit
& (DD_AUX0_BIT
| DD_AUX1_BIT
| DD_AUX2_BIT
| DD_AUX3_BIT
)) {
190 _swrast_use_aux_buffer(ctx
, buffer
, bufferBit
);
193 _mesa_problem(ctx
, "invalid buffer 0x%x in set_buffer() in xm_dd.c");
201 clear_index( GLcontext
*ctx
, GLuint index
)
203 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
204 xmesa
->clearpixel
= (unsigned long) index
;
205 XMesaSetForeground( xmesa
->display
, xmesa
->xm_draw_buffer
->cleargc
,
206 (unsigned long) index
);
211 clear_color( GLcontext
*ctx
, const GLfloat color
[4] )
213 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
214 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[0], color
[0]);
215 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[1], color
[1]);
216 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[2], color
[2]);
217 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[3], color
[3]);
218 xmesa
->clearpixel
= xmesa_color_to_pixel( xmesa
,
219 xmesa
->clearcolor
[0],
220 xmesa
->clearcolor
[1],
221 xmesa
->clearcolor
[2],
222 xmesa
->clearcolor
[3],
223 xmesa
->xm_visual
->undithered_pf
);
224 _glthread_LOCK_MUTEX(_xmesa_lock
);
225 XMesaSetForeground( xmesa
->display
, xmesa
->xm_draw_buffer
->cleargc
,
227 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
232 /* Set index mask ala glIndexMask */
234 index_mask( GLcontext
*ctx
, GLuint mask
)
236 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
237 if (xmesa
->xm_draw_buffer
->buffer
!= XIMAGE
) {
239 if (mask
==0xffffffff) {
240 m
= ((unsigned long)~0L);
243 m
= (unsigned long) mask
;
245 XMesaSetPlaneMask( xmesa
->display
, xmesa
->xm_draw_buffer
->cleargc
, m
);
246 XMesaSetPlaneMask( xmesa
->display
, xmesa
->xm_draw_buffer
->gc
, m
);
251 /* Implements glColorMask() */
253 color_mask(GLcontext
*ctx
,
254 GLboolean rmask
, GLboolean gmask
, GLboolean bmask
, GLboolean amask
)
256 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
257 const int xclass
= xmesa
->xm_visual
->mesa_visual
.visualType
;
260 if (xclass
== GLX_TRUE_COLOR
|| xclass
== GLX_DIRECT_COLOR
) {
262 if (rmask
&& gmask
&& bmask
) {
263 m
= ((unsigned long)~0L);
267 if (rmask
) m
|= GET_REDMASK(xmesa
->xm_visual
);
268 if (gmask
) m
|= GET_GREENMASK(xmesa
->xm_visual
);
269 if (bmask
) m
|= GET_BLUEMASK(xmesa
->xm_visual
);
271 XMesaSetPlaneMask( xmesa
->display
, xmesa
->xm_draw_buffer
->cleargc
, m
);
272 XMesaSetPlaneMask( xmesa
->display
, xmesa
->xm_draw_buffer
->gc
, m
);
278 /**********************************************************************/
279 /*** glClear implementations ***/
280 /**********************************************************************/
284 clear_front_pixmap( GLcontext
*ctx
, GLboolean all
,
285 GLint x
, GLint y
, GLint width
, GLint height
)
287 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
289 XMesaFillRectangle( xmesa
->display
, xmesa
->xm_draw_buffer
->frontbuffer
,
290 xmesa
->xm_draw_buffer
->cleargc
,
292 xmesa
->xm_draw_buffer
->mesa_buffer
.Width
+ 1,
293 xmesa
->xm_draw_buffer
->mesa_buffer
.Height
+ 1 );
296 XMesaFillRectangle( xmesa
->display
, xmesa
->xm_draw_buffer
->frontbuffer
,
297 xmesa
->xm_draw_buffer
->cleargc
,
298 x
, xmesa
->xm_draw_buffer
->mesa_buffer
.Height
- y
- height
,
305 clear_back_pixmap( GLcontext
*ctx
, GLboolean all
,
306 GLint x
, GLint y
, GLint width
, GLint height
)
308 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
310 XMesaFillRectangle( xmesa
->display
, xmesa
->xm_draw_buffer
->backpixmap
,
311 xmesa
->xm_draw_buffer
->cleargc
,
313 xmesa
->xm_draw_buffer
->mesa_buffer
.Width
+ 1,
314 xmesa
->xm_draw_buffer
->mesa_buffer
.Height
+ 1 );
317 XMesaFillRectangle( xmesa
->display
, xmesa
->xm_draw_buffer
->backpixmap
,
318 xmesa
->xm_draw_buffer
->cleargc
,
319 x
, xmesa
->xm_draw_buffer
->mesa_buffer
.Height
- y
- height
,
326 clear_8bit_ximage( GLcontext
*ctx
, GLboolean all
,
327 GLint x
, GLint y
, GLint width
, GLint height
)
329 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
331 size_t n
= xmesa
->xm_draw_buffer
->backimage
->bytes_per_line
332 * xmesa
->xm_draw_buffer
->backimage
->height
;
333 MEMSET( xmesa
->xm_draw_buffer
->backimage
->data
, xmesa
->clearpixel
, n
);
337 for (i
=0;i
<height
;i
++) {
338 GLubyte
*ptr
= PIXELADDR1( xmesa
->xm_draw_buffer
, x
, y
+i
);
339 MEMSET( ptr
, xmesa
->clearpixel
, width
);
346 clear_HPCR_ximage( GLcontext
*ctx
, GLboolean all
,
347 GLint x
, GLint y
, GLint width
, GLint height
)
349 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
351 GLint i
, c16
= (xmesa
->xm_draw_buffer
->backimage
->bytes_per_line
>>4)<<4;
352 GLubyte
*ptr
= (GLubyte
*)xmesa
->xm_draw_buffer
->backimage
->data
;
353 for (i
=0; i
<xmesa
->xm_draw_buffer
->backimage
->height
; i
++) {
355 GLubyte
*sptr
= xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0];
359 for (j
=0; j
<c16
; j
+=16) {
378 for (; j
<xmesa
->xm_draw_buffer
->backimage
->bytes_per_line
; j
++) {
386 for (i
=y
; i
<y
+height
; i
++) {
387 GLubyte
*ptr
= PIXELADDR1( xmesa
->xm_draw_buffer
, x
, i
);
389 GLubyte
*sptr
= xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0];
393 for (j
=x
; j
<x
+width
; j
++) {
403 clear_16bit_ximage( GLcontext
*ctx
, GLboolean all
,
404 GLint x
, GLint y
, GLint width
, GLint height
)
406 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
407 register GLuint pixel
= (GLuint
) xmesa
->clearpixel
;
408 if (xmesa
->swapbytes
) {
409 pixel
= ((pixel
>> 8) & 0x00ff) | ((pixel
<< 8) & 0xff00);
413 register GLuint
*ptr4
= (GLuint
*) xmesa
->xm_draw_buffer
->backimage
->data
;
414 if ((pixel
& 0xff) == ((pixel
>> 8) & 0xff)) {
415 /* low and high bytes are equal so use memset() */
416 n
= xmesa
->xm_draw_buffer
->backimage
->bytes_per_line
417 * xmesa
->xm_draw_buffer
->mesa_buffer
.Height
;
418 MEMSET( ptr4
, pixel
& 0xff, n
);
421 pixel
= pixel
| (pixel
<<16);
422 n
= xmesa
->xm_draw_buffer
->backimage
->bytes_per_line
423 * xmesa
->xm_draw_buffer
->mesa_buffer
.Height
/ 4;
429 if ((xmesa
->xm_draw_buffer
->backimage
->bytes_per_line
*
430 xmesa
->xm_draw_buffer
->mesa_buffer
.Height
) & 0x2)
431 *(GLushort
*)ptr4
= pixel
& 0xffff;
436 for (j
=0;j
<height
;j
++) {
437 register GLushort
*ptr2
= PIXELADDR2( xmesa
->xm_draw_buffer
, x
, y
+j
);
438 for (i
=0;i
<width
;i
++) {
446 /* Optimized code provided by Nozomi Ytow <noz@xfree86.org> */
448 clear_24bit_ximage( GLcontext
*ctx
, GLboolean all
,
449 GLint x
, GLint y
, GLint width
, GLint height
)
451 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
452 const GLubyte r
= xmesa
->clearcolor
[0];
453 const GLubyte g
= xmesa
->clearcolor
[1];
454 const GLubyte b
= xmesa
->clearcolor
[2];
455 #if 0 /* See below */
456 register GLuint clearPixel
;
457 if (xmesa
->swapbytes
) {
458 clearPixel
= (b
<< 16) | (g
<< 8) | r
;
461 clearPixel
= (r
<< 16) | (g
<< 8) | b
;
467 /* same value for all three components (gray) */
468 const GLint w3
= xmesa
->xm_draw_buffer
->mesa_buffer
.Width
* 3;
469 const GLint h
= xmesa
->xm_draw_buffer
->mesa_buffer
.Height
;
471 for (i
= 0; i
< h
; i
++) {
472 bgr_t
*ptr3
= PIXELADDR3(xmesa
->xm_draw_buffer
, 0, i
);
478 const GLint w
= xmesa
->xm_draw_buffer
->mesa_buffer
.Width
;
479 const GLint h
= xmesa
->xm_draw_buffer
->mesa_buffer
.Height
;
481 for (i
= 0; i
< h
; i
++) {
482 bgr_t
*ptr3
= PIXELADDR3(xmesa
->xm_draw_buffer
, 0, i
);
483 for (j
= 0; j
< w
; j
++) {
490 #if 0 /* this code doesn't work for all window widths */
491 register GLuint
*ptr4
= (GLuint
*) ptr3
;
494 register GLuint
*p
= pixel4
;
495 pixel4
[0] = clearPixel
| (clearPixel
<< 24);
496 pixel4
[1] = (clearPixel
<< 16) | (clearPixel
>> 8);
497 pixel4
[2] = (clearPixel
<< 8) | (clearPixel
>> 16);
498 switch (3 & (int)(ptr3
- (bgr_t
*) ptr4
)){
502 px
= *ptr4
& 0x00ffffff;
503 px
|= pixel4
[0] & 0xff000000;
505 px
= *ptr4
& 0xffff0000;
506 px
|= pixel4
[2] & 0x0000ffff;
511 px
= *ptr4
& 0x0000fffff;
512 px
|= pixel4
[1] & 0xffff0000;
514 px
= *ptr4
& 0xffffff00;
515 px
|= pixel4
[2] & 0x000000ff;
520 px
= *ptr4
& 0x000000ff;
521 px
|= pixel4
[2] & 0xffffff00;
538 px
= *ptr4
& 0xffffff00;
539 px
|= clearPixel
& 0xff;
545 px
= *ptr4
& 0xffff0000;
550 px
= *ptr4
& 0xff000000;
561 /* only clear subrect of color buffer */
563 /* same value for all three components (gray) */
565 for (j
=0;j
<height
;j
++) {
566 bgr_t
*ptr3
= PIXELADDR3( xmesa
->xm_draw_buffer
, x
, y
+j
);
567 MEMSET(ptr3
, r
, 3 * width
);
571 /* non-gray clear color */
573 for (j
= 0; j
< height
; j
++) {
574 bgr_t
*ptr3
= PIXELADDR3( xmesa
->xm_draw_buffer
, x
, y
+j
);
575 for (i
= 0; i
< width
; i
++) {
582 #if 0 /* this code might not always (seems ptr3 always == ptr4) */
585 pixel4
[0] = clearPixel
| (clearPixel
<< 24);
586 pixel4
[1] = (clearPixel
<< 16) | (clearPixel
>> 8);
587 pixel4
[2] = (clearPixel
<< 8) | (clearPixel
>> 16);
588 for (j
=0;j
<height
;j
++) {
589 bgr_t
*ptr3
= PIXELADDR3( xmesa
->xm_draw_buffer
, x
, y
+j
);
590 register GLuint
*ptr4
= (GLuint
*)ptr3
;
591 register GLuint
*p
, px
;
593 switch (3 & (int)(ptr3
- (bgr_t
*) ptr4
)){
597 px
= *ptr4
& 0x00ffffff;
598 px
|= pixel4
[0] & 0xff000000;
600 px
= *ptr4
& 0xffff0000;
601 px
|= pixel4
[2] & 0x0000ffff;
606 px
= *ptr4
& 0x0000fffff;
607 px
|= pixel4
[1] & 0xffff0000;
609 px
= *ptr4
& 0xffffff00;
610 px
|= pixel4
[2] & 0x000000ff;
615 px
= *ptr4
& 0x000000ff;
616 px
|= pixel4
[2] & 0xffffff00;
633 px
= *ptr4
& 0xffffff00;
640 px
= *ptr4
& 0xffff0000;
645 px
= *ptr4
& 0xff000000;
646 px
|= pixel4
[0] & 0xffffff;
660 clear_32bit_ximage( GLcontext
*ctx
, GLboolean all
,
661 GLint x
, GLint y
, GLint width
, GLint height
)
663 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
664 register GLuint pixel
= (GLuint
) xmesa
->clearpixel
;
665 if (xmesa
->swapbytes
) {
666 pixel
= ((pixel
>> 24) & 0x000000ff)
667 | ((pixel
>> 8) & 0x0000ff00)
668 | ((pixel
<< 8) & 0x00ff0000)
669 | ((pixel
<< 24) & 0xff000000);
672 register GLint n
= xmesa
->xm_draw_buffer
->mesa_buffer
.Width
673 * xmesa
->xm_draw_buffer
->mesa_buffer
.Height
;
674 register GLuint
*ptr4
= (GLuint
*) xmesa
->xm_draw_buffer
->backimage
->data
;
676 MEMSET( ptr4
, pixel
, 4*n
);
687 for (j
=0;j
<height
;j
++) {
688 register GLuint
*ptr4
= PIXELADDR4( xmesa
->xm_draw_buffer
, x
, y
+j
);
689 for (i
=0;i
<width
;i
++) {
698 clear_nbit_ximage( GLcontext
*ctx
, GLboolean all
,
699 GLint x
, GLint y
, GLint width
, GLint height
)
701 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
702 XMesaImage
*img
= xmesa
->xm_draw_buffer
->backimage
;
705 /* We can ignore 'all' here - x, y, width, height are always right */
708 /* TODO: optimize this */
709 y
= FLIP(xmesa
->xm_draw_buffer
, y
);
710 for (j
= 0; j
< height
; j
++) {
711 for (i
= 0; i
< width
; i
++) {
712 XMesaPutPixel(img
, x
+i
, y
-j
, xmesa
->clearpixel
);
720 clear_buffers( GLcontext
*ctx
, GLbitfield mask
,
721 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
723 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
724 const GLuint
*colorMask
= (GLuint
*) &ctx
->Color
.ColorMask
;
726 if ((mask
& (DD_FRONT_LEFT_BIT
| DD_BACK_LEFT_BIT
)) &&
727 xmesa
->xm_draw_buffer
->mesa_buffer
.UseSoftwareAlphaBuffers
&&
728 ctx
->Color
.ColorMask
[ACOMP
]) {
729 _swrast_clear_alpha_buffers(ctx
);
732 /* we can't handle color or index masking */
733 if (*colorMask
== 0xffffffff && ctx
->Color
.IndexMask
== 0xffffffff) {
734 if (mask
& DD_FRONT_LEFT_BIT
) {
735 ASSERT(xmesa
->xm_draw_buffer
->front_clear_func
);
736 (*xmesa
->xm_draw_buffer
->front_clear_func
)( ctx
, all
, x
, y
, width
, height
);
737 mask
&= ~DD_FRONT_LEFT_BIT
;
739 if (mask
& DD_BACK_LEFT_BIT
) {
740 ASSERT(xmesa
->xm_draw_buffer
->back_clear_func
);
741 (*xmesa
->xm_draw_buffer
->back_clear_func
)( ctx
, all
, x
, y
, width
, height
);
742 mask
&= ~DD_BACK_LEFT_BIT
;
747 _swrast_Clear( ctx
, mask
, all
, x
, y
, width
, height
);
752 * When we detect that the user has resized the window this function will
753 * get called. Here we'll reallocate the back buffer, depth buffer,
754 * stencil buffer etc. to match the new window size.
755 * The buffer->Width and buffer->Height values will indicate the new size.
758 xmesa_resize_buffers( GLframebuffer
*buffer
)
760 int height
= (int) buffer
->Height
;
761 /* We can do this cast because the first field in the XMesaBuffer
762 * struct is a GLframebuffer struct. If this weren't true, we'd
763 * need a pointer from the GLframebuffer to the XMesaBuffer.
765 XMesaBuffer xmBuffer
= (XMesaBuffer
) buffer
;
767 xmesa_alloc_back_buffer( xmBuffer
);
769 /* Needed by FLIP macro */
770 xmBuffer
->bottom
= height
- 1;
772 if (xmBuffer
->backimage
) {
773 /* Needed by PIXELADDR1 macro */
774 xmBuffer
->ximage_width1
= xmBuffer
->backimage
->bytes_per_line
;
775 xmBuffer
->ximage_origin1
= (GLubyte
*) xmBuffer
->backimage
->data
776 + xmBuffer
->ximage_width1
* (height
-1);
778 /* Needed by PIXELADDR2 macro */
779 xmBuffer
->ximage_width2
= xmBuffer
->backimage
->bytes_per_line
/ 2;
780 xmBuffer
->ximage_origin2
= (GLushort
*) xmBuffer
->backimage
->data
781 + xmBuffer
->ximage_width2
* (height
-1);
783 /* Needed by PIXELADDR3 macro */
784 xmBuffer
->ximage_width3
= xmBuffer
->backimage
->bytes_per_line
;
785 xmBuffer
->ximage_origin3
= (GLubyte
*) xmBuffer
->backimage
->data
786 + xmBuffer
->ximage_width3
* (height
-1);
788 /* Needed by PIXELADDR4 macro */
789 xmBuffer
->ximage_width4
= xmBuffer
->backimage
->width
;
790 xmBuffer
->ximage_origin4
= (GLuint
*) xmBuffer
->backimage
->data
791 + xmBuffer
->ximage_width4
* (height
-1);
794 _swrast_alloc_buffers( buffer
);
798 #ifndef XFree86Server
799 /* XXX this was never tested in the Xserver environment */
802 * This function implements glDrawPixels() with an XPutImage call when
803 * drawing to the front buffer (X Window drawable).
804 * The image format must be GL_BGRA to match the PF_8R8G8B pixel format.
807 xmesa_DrawPixels_8R8G8B( GLcontext
*ctx
,
808 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
809 GLenum format
, GLenum type
,
810 const struct gl_pixelstore_attrib
*unpack
,
811 const GLvoid
*pixels
)
813 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
814 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
815 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
816 const XMesaDrawable buffer
= xmesa
->xm_draw_buffer
->buffer
;
817 const XMesaGC gc
= xmesa
->xm_draw_buffer
->gc
;
821 ASSERT(xmesa
->xm_visual
->dithered_pf
== PF_8R8G8B
);
822 ASSERT(xmesa
->xm_visual
->undithered_pf
== PF_8R8G8B
);
824 if (swrast
->NewState
)
825 _swrast_validate_derived( ctx
);
827 if (buffer
&& /* buffer != 0 means it's a Window or Pixmap */
829 type
== GL_UNSIGNED_BYTE
&&
830 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
831 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
832 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
833 ctx
->Pixel
.ZoomY
== 1.0) {
838 int srcX
= unpack
->SkipPixels
;
839 int srcY
= unpack
->SkipRows
;
840 int rowLength
= unpack
->RowLength
? unpack
->RowLength
: width
;
842 if (unpack
->BufferObj
->Name
) {
843 /* unpack from PBO */
845 if (!_mesa_validate_pbo_access(2, unpack
, width
, height
, 1,
846 format
, type
, pixels
)) {
847 _mesa_error(ctx
, GL_INVALID_OPERATION
,
848 "glDrawPixels(invalid PBO access)");
851 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
,
852 GL_PIXEL_UNPACK_BUFFER_EXT
,
856 /* buffer is already mapped - that's an error */
857 _mesa_error(ctx
, GL_INVALID_OPERATION
,
858 "glDrawPixels(PBO is mapped)");
861 pixels
= ADD_POINTERS(buf
, pixels
);
864 if (_mesa_clip_drawpixels(ctx
, &dstX
, &dstY
, &w
, &h
, &srcX
, &srcY
)) {
865 /* This is a little tricky since all coordinates up to now have
866 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
867 * so we have to carefully compute the Y coordinates/addresses here.
870 MEMSET(&ximage
, 0, sizeof(XMesaImage
));
871 ximage
.width
= width
;
872 ximage
.height
= height
;
873 ximage
.format
= ZPixmap
;
874 ximage
.data
= (char *) pixels
875 + ((srcY
+ h
- 1) * rowLength
+ srcX
) * 4;
876 ximage
.byte_order
= LSBFirst
;
877 ximage
.bitmap_unit
= 32;
878 ximage
.bitmap_bit_order
= LSBFirst
;
879 ximage
.bitmap_pad
= 32;
881 ximage
.bytes_per_line
= -rowLength
* 4; /* negative to flip image */
882 ximage
.bits_per_pixel
= 32;
883 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
884 /* flip Y axis for dest position */
885 dstY
= FLIP(xmesa
->xm_draw_buffer
, dstY
) - h
+ 1;
886 XPutImage(dpy
, buffer
, gc
, &ximage
, 0, 0, dstX
, dstY
, w
, h
);
889 if (unpack
->BufferObj
->Name
) {
890 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
895 /* software fallback */
896 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
897 format
, type
, unpack
, pixels
);
904 * This function implements glDrawPixels() with an XPutImage call when
905 * drawing to the front buffer (X Window drawable). The image format
906 * must be GL_RGB and image type must be GL_UNSIGNED_SHORT_5_6_5 to
907 * match the PF_5R6G5B pixel format.
910 xmesa_DrawPixels_5R6G5B( GLcontext
*ctx
,
911 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
912 GLenum format
, GLenum type
,
913 const struct gl_pixelstore_attrib
*unpack
,
914 const GLvoid
*pixels
)
916 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
917 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
918 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
919 const XMesaDrawable buffer
= xmesa
->xm_draw_buffer
->buffer
;
920 const XMesaGC gc
= xmesa
->xm_draw_buffer
->gc
;
924 ASSERT(xmesa
->xm_visual
->undithered_pf
== PF_5R6G5B
);
926 if (swrast
->NewState
)
927 _swrast_validate_derived( ctx
);
929 if (buffer
&& /* buffer != 0 means it's a Window or Pixmap */
931 type
== GL_UNSIGNED_SHORT_5_6_5
&&
932 !ctx
->Color
.DitherFlag
&& /* no dithering */
933 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
934 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
935 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
936 ctx
->Pixel
.ZoomY
== 1.0) {
941 int srcX
= unpack
->SkipPixels
;
942 int srcY
= unpack
->SkipRows
;
943 int rowLength
= unpack
->RowLength
? unpack
->RowLength
: width
;
945 if (unpack
->BufferObj
->Name
) {
946 /* unpack from PBO */
948 if (!_mesa_validate_pbo_access(2, unpack
, width
, height
, 1,
949 format
, type
, pixels
)) {
950 _mesa_error(ctx
, GL_INVALID_OPERATION
,
951 "glDrawPixels(invalid PBO access)");
954 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
,
955 GL_PIXEL_UNPACK_BUFFER_EXT
,
959 /* buffer is already mapped - that's an error */
960 _mesa_error(ctx
, GL_INVALID_OPERATION
,
961 "glDrawPixels(PBO is mapped)");
964 pixels
= ADD_POINTERS(buf
, pixels
);
967 if (_mesa_clip_drawpixels(ctx
, &dstX
, &dstY
, &w
, &h
, &srcX
, &srcY
)) {
968 /* This is a little tricky since all coordinates up to now have
969 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
970 * so we have to carefully compute the Y coordinates/addresses here.
973 MEMSET(&ximage
, 0, sizeof(XMesaImage
));
974 ximage
.width
= width
;
975 ximage
.height
= height
;
976 ximage
.format
= ZPixmap
;
977 ximage
.data
= (char *) pixels
978 + ((srcY
+ h
- 1) * rowLength
+ srcX
) * 2;
979 ximage
.byte_order
= LSBFirst
;
980 ximage
.bitmap_unit
= 16;
981 ximage
.bitmap_bit_order
= LSBFirst
;
982 ximage
.bitmap_pad
= 16;
984 ximage
.bytes_per_line
= -rowLength
* 2; /* negative to flip image */
985 ximage
.bits_per_pixel
= 16;
986 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
987 /* flip Y axis for dest position */
988 dstY
= FLIP(xmesa
->xm_draw_buffer
, dstY
) - h
+ 1;
989 XPutImage(dpy
, buffer
, gc
, &ximage
, 0, 0, dstX
, dstY
, w
, h
);
992 if (unpack
->BufferObj
->Name
) {
993 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
998 /* software fallback */
999 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
1000 format
, type
, unpack
, pixels
);
1007 * Implement glCopyPixels for the front color buffer (or back buffer Pixmap)
1008 * for the color buffer. Don't support zooming, pixel transfer, etc.
1009 * We do support copying from one window to another, ala glXMakeCurrentRead.
1012 xmesa_CopyPixels( GLcontext
*ctx
,
1013 GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
1014 GLint destx
, GLint desty
, GLenum type
)
1016 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
1017 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
1018 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
1019 const XMesaDrawable drawBuffer
= xmesa
->xm_draw_buffer
->buffer
;
1020 const XMesaDrawable readBuffer
= xmesa
->xm_read_buffer
->buffer
;
1021 const XMesaGC gc
= xmesa
->xm_draw_buffer
->gc
;
1026 if (swrast
->NewState
)
1027 _swrast_validate_derived( ctx
);
1029 if (ctx
->Color
.DrawBuffer
[0] == GL_FRONT
&&
1030 ctx
->Pixel
.ReadBuffer
== GL_FRONT
&&
1031 drawBuffer
&& /* buffer != 0 means it's a Window or Pixmap */
1034 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
1035 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
1036 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
1037 ctx
->Pixel
.ZoomY
== 1.0) {
1038 /* Note: we don't do any special clipping work here. We could,
1039 * but X will do it for us.
1041 srcy
= FLIP(xmesa
->xm_read_buffer
, srcy
) - height
+ 1;
1042 desty
= FLIP(xmesa
->xm_draw_buffer
, desty
) - height
+ 1;
1043 XCopyArea(dpy
, readBuffer
, drawBuffer
, gc
,
1044 srcx
, srcy
, width
, height
, destx
, desty
);
1047 _swrast_CopyPixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
, type
);
1050 #endif /* XFree86Server */
1055 * Every driver should implement a GetString function in order to
1056 * return a meaningful GL_RENDERER string.
1058 static const GLubyte
*
1059 get_string( GLcontext
*ctx
, GLenum name
)
1064 #ifdef XFree86Server
1065 return (const GLubyte
*) "Mesa GLX Indirect";
1067 return (const GLubyte
*) "Mesa X11";
1070 #ifdef XFree86Server
1071 return (const GLubyte
*) "Mesa project: www.mesa3d.org";
1082 * We implement the glEnable function only because we care about
1083 * dither enable/disable.
1086 enable( GLcontext
*ctx
, GLenum pname
, GLboolean state
)
1088 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
1093 xmesa
->pixelformat
= xmesa
->xm_visual
->dithered_pf
;
1095 xmesa
->pixelformat
= xmesa
->xm_visual
->undithered_pf
;
1098 ; /* silence compiler warning */
1103 void xmesa_update_state( GLcontext
*ctx
, GLuint new_state
)
1105 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
1107 /* Propagate statechange information to swrast and swrast_setup
1108 * modules. The X11 driver has no internal GL-dependent state.
1110 _swrast_InvalidateState( ctx
, new_state
);
1111 _ac_InvalidateState( ctx
, new_state
);
1112 _tnl_InvalidateState( ctx
, new_state
);
1113 _swsetup_InvalidateState( ctx
, new_state
);
1116 /* setup pointers to front and back buffer clear functions */
1117 xmesa
->xm_draw_buffer
->front_clear_func
= clear_front_pixmap
;
1118 if (xmesa
->xm_draw_buffer
->backpixmap
!= XIMAGE
) {
1119 xmesa
->xm_draw_buffer
->back_clear_func
= clear_back_pixmap
;
1122 switch (xmesa
->xm_visual
->BitsPerPixel
) {
1124 if (xmesa
->xm_visual
->hpcr_clear_flag
) {
1125 xmesa
->xm_draw_buffer
->back_clear_func
= clear_HPCR_ximage
;
1128 xmesa
->xm_draw_buffer
->back_clear_func
= clear_8bit_ximage
;
1132 xmesa
->xm_draw_buffer
->back_clear_func
= clear_16bit_ximage
;
1135 xmesa
->xm_draw_buffer
->back_clear_func
= clear_24bit_ximage
;
1138 xmesa
->xm_draw_buffer
->back_clear_func
= clear_32bit_ximage
;
1141 xmesa
->xm_draw_buffer
->back_clear_func
= clear_nbit_ximage
;
1146 if (ctx
->Color
._DrawDestMask
[0] & (DD_FRONT_LEFT_BIT
| DD_BACK_LEFT_BIT
)) {
1147 xmesa_update_span_funcs(ctx
);
1154 * Called via ctx->Driver.TestProxyTeximage(). Normally, we'd just use
1155 * the _mesa_test_proxy_teximage() fallback function, but we're going to
1156 * special-case the 3D texture case to allow textures up to 512x512x32
1160 test_proxy_teximage(GLcontext
*ctx
, GLenum target
, GLint level
,
1161 GLint internalFormat
, GLenum format
, GLenum type
,
1162 GLint width
, GLint height
, GLint depth
, GLint border
)
1164 if (target
== GL_PROXY_TEXTURE_3D
) {
1165 /* special case for 3D textures */
1166 if (width
* height
* depth
> 512 * 512 * 64 ||
1167 width
< 2 * border
||
1168 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1169 _mesa_bitcount(width
- 2 * border
) != 1) ||
1170 height
< 2 * border
||
1171 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1172 _mesa_bitcount(height
- 2 * border
) != 1) ||
1173 depth
< 2 * border
||
1174 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1175 _mesa_bitcount(depth
- 2 * border
) != 1)) {
1176 /* Bad size, or too many texels */
1182 /* use the fallback routine for 1D, 2D, cube and rect targets */
1183 return _mesa_test_proxy_teximage(ctx
, target
, level
, internalFormat
,
1184 format
, type
, width
, height
, depth
,
1191 * In SW, we don't really compress GL_COMPRESSED_RGB[A] textures!
1193 static const struct gl_texture_format
*
1194 choose_tex_format( GLcontext
*ctx
, GLint internalFormat
,
1195 GLenum format
, GLenum type
)
1197 switch (internalFormat
) {
1198 case GL_COMPRESSED_RGB_ARB
:
1199 return &_mesa_texformat_rgb
;
1200 case GL_COMPRESSED_RGBA_ARB
:
1201 return &_mesa_texformat_rgba
;
1203 return _mesa_choose_tex_format(ctx
, internalFormat
, format
, type
);
1209 * Called by glViewport.
1210 * This is a good time for us to poll the current X window size and adjust
1211 * our ancillary (back color, depth, stencil, etc) buffers to match the
1212 * current window size. Remember, we have no opportunity to respond to
1213 * conventional X Resize/StructureNotify events since the X driver has no
1214 * event loop. Thus, we poll.
1215 * Note that this trick isn't fool-proof. If the application never calls
1216 * glViewport, our notion of the current window size may be incorrect.
1219 xmesa_viewport(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei w
, GLsizei h
)
1221 _mesa_ResizeBuffersMESA();
1226 * Initialize the device driver function table with the functions
1227 * we implement in this driver.
1230 xmesa_init_driver_functions( XMesaVisual xmvisual
,
1231 struct dd_function_table
*driver
)
1233 driver
->GetString
= get_string
;
1234 driver
->UpdateState
= xmesa_update_state
;
1235 driver
->GetBufferSize
= get_buffer_size
;
1236 driver
->Flush
= finish_or_flush
;
1237 driver
->Finish
= finish_or_flush
;
1238 driver
->ClearIndex
= clear_index
;
1239 driver
->ClearColor
= clear_color
;
1240 driver
->IndexMask
= index_mask
;
1241 driver
->ColorMask
= color_mask
;
1242 driver
->Enable
= enable
;
1243 driver
->Clear
= clear_buffers
;
1244 driver
->ResizeBuffers
= xmesa_resize_buffers
;
1245 driver
->Viewport
= xmesa_viewport
;
1246 #ifndef XFree86Server
1247 driver
->CopyPixels
= xmesa_CopyPixels
;
1248 if (xmvisual
->undithered_pf
== PF_8R8G8B
&&
1249 xmvisual
->dithered_pf
== PF_8R8G8B
) {
1250 driver
->DrawPixels
= xmesa_DrawPixels_8R8G8B
;
1252 else if (xmvisual
->undithered_pf
== PF_5R6G5B
) {
1253 driver
->DrawPixels
= xmesa_DrawPixels_5R6G5B
;
1256 driver
->TestProxyTexImage
= test_proxy_teximage
;
1258 driver
->ChooseTextureFormat
= choose_tex_format
;
1260 (void) choose_tex_format
;
1265 #define XMESA_NEW_POINT (_NEW_POINT | \
1267 _SWRAST_NEW_RASTERMASK)
1269 #define XMESA_NEW_LINE (_NEW_LINE | \
1274 _SWRAST_NEW_RASTERMASK)
1276 #define XMESA_NEW_TRIANGLE (_NEW_POLYGON | \
1281 _SWRAST_NEW_RASTERMASK)
1284 /* Extend the software rasterizer with our line/point/triangle
1287 void xmesa_register_swrast_functions( GLcontext
*ctx
)
1289 SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
1290 struct swrast_device_driver
*dd
= _swrast_GetDeviceDriverReference(ctx
);
1292 dd
->SetBuffer
= xmesa_set_buffer
;
1294 swrast
->choose_point
= xmesa_choose_point
;
1295 swrast
->choose_line
= xmesa_choose_line
;
1296 swrast
->choose_triangle
= xmesa_choose_triangle
;
1298 swrast
->invalidate_point
|= XMESA_NEW_POINT
;
1299 swrast
->invalidate_line
|= XMESA_NEW_LINE
;
1300 swrast
->invalidate_triangle
|= XMESA_NEW_TRIANGLE
;