2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2006 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.
28 * General device driver functions for Xlib driver.
31 #include "glxheader.h"
32 #include "bufferobj.h"
38 #include "extensions.h"
39 #include "framebuffer.h"
48 #include "texformat.h"
50 #include "array_cache/acache.h"
51 #include "swrast/swrast.h"
52 #include "swrast/s_context.h"
53 #include "swrast_setup/swrast_setup.h"
55 #include "tnl/t_context.h"
58 #include <GL/glxtokens.h>
64 * Dithering kernels and lookup tables.
67 const int xmesa_kernel8
[DITH_DY
* DITH_DX
] = {
68 0 * MAXC
, 8 * MAXC
, 2 * MAXC
, 10 * MAXC
,
69 12 * MAXC
, 4 * MAXC
, 14 * MAXC
, 6 * MAXC
,
70 3 * MAXC
, 11 * MAXC
, 1 * MAXC
, 9 * MAXC
,
71 15 * MAXC
, 7 * MAXC
, 13 * MAXC
, 5 * MAXC
,
74 const short xmesa_HPCR_DRGB
[3][2][16] = {
76 { 16, -4, 1,-11, 14, -6, 3, -9, 15, -5, 2,-10, 13, -7, 4, -8},
77 {-15, 5, 0, 12,-13, 7, -2, 10,-14, 6, -1, 11,-12, 8, -3, 9}
80 {-11, 15, -7, 3, -8, 14, -4, 2,-10, 16, -6, 4, -9, 13, -5, 1},
81 { 12,-14, 8, -2, 9,-13, 5, -1, 11,-15, 7, -3, 10,-12, 6, 0}
84 { 6,-18, 26,-14, 2,-22, 30,-10, 8,-16, 28,-12, 4,-20, 32, -8},
85 { -4, 20,-24, 16, 0, 24,-28, 12, -6, 18,-26, 14, -2, 22,-30, 10}
89 const int xmesa_kernel1
[16] = {
90 0*47, 9*47, 4*47, 12*47, /* 47 = (255*3)/16 */
91 6*47, 2*47, 14*47, 8*47,
92 10*47, 1*47, 5*47, 11*47,
93 7*47, 13*47, 3*47, 15*47
98 finish_or_flush( GLcontext
*ctx
)
103 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
105 _glthread_LOCK_MUTEX(_xmesa_lock
);
106 XSync( xmesa
->display
, False
);
107 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
114 clear_index( GLcontext
*ctx
, GLuint index
)
116 if (ctx
->DrawBuffer
->Name
== 0) {
117 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
118 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
119 xmesa
->clearpixel
= (unsigned long) index
;
120 XMesaSetForeground( xmesa
->display
, xmbuf
->cleargc
, (unsigned long) index
);
126 clear_color( GLcontext
*ctx
, const GLfloat color
[4] )
128 if (ctx
->DrawBuffer
->Name
== 0) {
129 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
130 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
132 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[0], color
[0]);
133 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[1], color
[1]);
134 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[2], color
[2]);
135 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[3], color
[3]);
136 xmesa
->clearpixel
= xmesa_color_to_pixel( ctx
,
137 xmesa
->clearcolor
[0],
138 xmesa
->clearcolor
[1],
139 xmesa
->clearcolor
[2],
140 xmesa
->clearcolor
[3],
141 xmesa
->xm_visual
->undithered_pf
);
142 _glthread_LOCK_MUTEX(_xmesa_lock
);
143 XMesaSetForeground( xmesa
->display
, xmbuf
->cleargc
,
145 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
151 /* Set index mask ala glIndexMask */
153 index_mask( GLcontext
*ctx
, GLuint mask
)
155 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
156 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
157 /* not sure this conditional is really needed */
158 if (xmbuf
->backxrb
&& xmbuf
->backxrb
->pixmap
) {
160 if (mask
==0xffffffff) {
161 m
= ((unsigned long)~0L);
164 m
= (unsigned long) mask
;
166 XMesaSetPlaneMask( xmesa
->display
, xmbuf
->cleargc
, m
);
167 XMesaSetPlaneMask( xmesa
->display
, xmbuf
->gc
, m
);
172 /* Implements glColorMask() */
174 color_mask(GLcontext
*ctx
,
175 GLboolean rmask
, GLboolean gmask
, GLboolean bmask
, GLboolean amask
)
177 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
179 const int xclass
= xmesa
->xm_visual
->mesa_visual
.visualType
;
182 if (ctx
->DrawBuffer
->Name
!= 0)
185 xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
187 if (xclass
== GLX_TRUE_COLOR
|| xclass
== GLX_DIRECT_COLOR
) {
189 if (rmask
&& gmask
&& bmask
) {
190 m
= ((unsigned long)~0L);
194 if (rmask
) m
|= GET_REDMASK(xmesa
->xm_visual
);
195 if (gmask
) m
|= GET_GREENMASK(xmesa
->xm_visual
);
196 if (bmask
) m
|= GET_BLUEMASK(xmesa
->xm_visual
);
198 XMesaSetPlaneMask( xmesa
->display
, xmbuf
->cleargc
, m
);
199 XMesaSetPlaneMask( xmesa
->display
, xmbuf
->gc
, m
);
205 /**********************************************************************/
206 /*** glClear implementations ***/
207 /**********************************************************************/
211 * Clear the front or back color buffer, if it's implemented with a pixmap.
214 clear_pixmap(GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
, GLboolean all
,
215 GLint x
, GLint y
, GLint width
, GLint height
)
217 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
218 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
223 assert(xmesa
->display
);
225 assert(xmbuf
->cleargc
);
228 XMesaFillRectangle( xmesa
->display
, xrb
->pixmap
, xmbuf
->cleargc
,
229 0, 0, xrb
->Base
.Width
+ 1, xrb
->Base
.Height
+ 1 );
232 XMesaFillRectangle( xmesa
->display
, xrb
->pixmap
, xmbuf
->cleargc
,
233 x
, xrb
->Base
.Height
- y
- height
,
240 clear_8bit_ximage( GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
241 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
243 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
246 const size_t n
= xrb
->ximage
->bytes_per_line
* xrb
->Base
.Height
;
247 MEMSET( xrb
->ximage
->data
, xmesa
->clearpixel
, n
);
251 for (i
=0;i
<height
;i
++) {
252 GLubyte
*ptr
= PIXEL_ADDR1(xrb
, x
, y
+ i
);
253 MEMSET( ptr
, xmesa
->clearpixel
, width
);
260 clear_HPCR_ximage( GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
261 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
263 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
266 const GLuint c16
= xrb
->ximage
->bytes_per_line
& ~0xf;
268 GLubyte
*ptr
= (GLubyte
*) xrb
->ximage
->data
;
269 for (i
= 0; i
< xrb
->Base
.Height
; i
++) {
271 const GLubyte
*sptr
= xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0];
275 for (j
=0; j
<c16
; j
+=16) {
294 for (; j
< (GLuint
) xrb
->ximage
->bytes_per_line
; j
++) {
302 for (i
=y
; i
<y
+height
; i
++) {
303 GLubyte
*ptr
= PIXEL_ADDR1( xrb
, x
, i
);
305 const GLubyte
*sptr
= xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0];
309 for (j
=x
; j
<x
+width
; j
++) {
319 clear_16bit_ximage( GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
320 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
322 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
323 register GLuint pixel
= (GLuint
) xmesa
->clearpixel
;
325 if (xmesa
->swapbytes
) {
326 pixel
= ((pixel
>> 8) & 0x00ff) | ((pixel
<< 8) & 0xff00);
330 GLuint
*ptr4
= (GLuint
*) xrb
->ximage
->data
;
331 if ((pixel
& 0xff) == ((pixel
>> 8) & 0xff)) {
332 /* low and high bytes are equal so use memset() */
333 const GLuint n
= xrb
->ximage
->bytes_per_line
* xrb
->Base
.Height
;
334 MEMSET( ptr4
, pixel
& 0xff, n
);
337 const GLuint n
= xrb
->ximage
->bytes_per_line
* xrb
->Base
.Height
/ 4;
339 pixel
= pixel
| (pixel
<<16);
340 for (i
= 0; i
< n
; i
++) {
344 /* might be one last GLushort to set */
345 if ((xrb
->ximage
->bytes_per_line
* xrb
->Base
.Height
) & 0x2)
346 *(GLushort
*)ptr4
= pixel
& 0xffff;
351 for (j
=0;j
<height
;j
++) {
352 GLushort
*ptr2
= PIXEL_ADDR2(xrb
, x
, y
+ j
);
353 for (i
=0;i
<width
;i
++) {
361 /* Optimized code provided by Nozomi Ytow <noz@xfree86.org> */
363 clear_24bit_ximage(GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
364 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
366 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
367 const GLubyte r
= xmesa
->clearcolor
[0];
368 const GLubyte g
= xmesa
->clearcolor
[1];
369 const GLubyte b
= xmesa
->clearcolor
[2];
372 if (r
== g
&& g
== b
) {
373 /* same value for all three components (gray) */
374 const GLint w3
= xrb
->Base
.Width
* 3;
375 const GLint h
= xrb
->Base
.Height
;
377 for (i
= 0; i
< h
; i
++) {
378 bgr_t
*ptr3
= PIXEL_ADDR3(xrb
, 0, i
);
384 const GLint w
= xrb
->Base
.Width
;
385 const GLint h
= xrb
->Base
.Height
;
387 for (i
= 0; i
< h
; i
++) {
388 bgr_t
*ptr3
= PIXEL_ADDR3(xrb
, 0, i
);
389 for (j
= 0; j
< w
; j
++) {
399 /* only clear subrect of color buffer */
400 if (r
== g
&& g
== b
) {
401 /* same value for all three components (gray) */
403 for (j
=0;j
<height
;j
++) {
404 bgr_t
*ptr3
= PIXEL_ADDR3(xrb
, x
, y
+ j
);
405 MEMSET(ptr3
, r
, 3 * width
);
409 /* non-gray clear color */
411 for (j
= 0; j
< height
; j
++) {
412 bgr_t
*ptr3
= PIXEL_ADDR3(xrb
, x
, y
+ j
);
413 for (i
= 0; i
< width
; i
++) {
426 clear_32bit_ximage(GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
427 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
429 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
430 register GLuint pixel
= (GLuint
) xmesa
->clearpixel
;
435 if (xmesa
->swapbytes
) {
436 pixel
= ((pixel
>> 24) & 0x000000ff)
437 | ((pixel
>> 8) & 0x0000ff00)
438 | ((pixel
<< 8) & 0x00ff0000)
439 | ((pixel
<< 24) & 0xff000000);
443 const GLuint n
= xrb
->Base
.Width
* xrb
->Base
.Height
;
444 GLuint
*ptr4
= (GLuint
*) xrb
->ximage
->data
;
446 _mesa_memset(ptr4
, pixel
, 4 * n
);
450 for (i
= 0; i
< n
; i
++)
456 for (j
= 0; j
< height
; j
++) {
457 GLuint
*ptr4
= PIXEL_ADDR4(xrb
, x
, y
+ j
);
458 for (i
= 0; i
< width
; i
++) {
467 clear_nbit_ximage(GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
468 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
470 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
471 XMesaImage
*img
= xrb
->ximage
;
474 /* We can ignore 'all' here - x, y, width, height are always right */
477 /* TODO: optimize this */
479 for (j
= 0; j
< height
; j
++) {
480 for (i
= 0; i
< width
; i
++) {
481 XMesaPutPixel(img
, x
+i
, y
-j
, xmesa
->clearpixel
);
489 clear_buffers( GLcontext
*ctx
, GLbitfield mask
,
490 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
492 if (ctx
->DrawBuffer
->Name
== 0) {
493 /* this is a window system framebuffer */
494 const GLuint
*colorMask
= (GLuint
*) &ctx
->Color
.ColorMask
;
495 XMesaBuffer b
= XMESA_BUFFER(ctx
->DrawBuffer
);
497 /* we can't handle color or index masking */
498 if (*colorMask
== 0xffffffff && ctx
->Color
.IndexMask
== 0xffffffff) {
499 if (mask
& BUFFER_BIT_FRONT_LEFT
) {
500 /* clear front color buffer */
501 struct gl_renderbuffer
*frontRb
502 = ctx
->DrawBuffer
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
;
503 if (b
->frontxrb
== xmesa_renderbuffer(frontRb
)) {
504 /* renderbuffer is not wrapped - great! */
505 b
->frontxrb
->clearFunc(ctx
, b
->frontxrb
, all
, x
, y
,
507 mask
&= ~BUFFER_BIT_FRONT_LEFT
;
510 /* we can't directly clear an alpha-wrapped color buffer */
513 if (mask
& BUFFER_BIT_BACK_LEFT
) {
514 /* clear back color buffer */
515 struct gl_renderbuffer
*backRb
516 = ctx
->DrawBuffer
->Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
;
517 if (b
->backxrb
== xmesa_renderbuffer(backRb
)) {
518 /* renderbuffer is not wrapped - great! */
519 b
->backxrb
->clearFunc(ctx
, b
->backxrb
, all
, x
, y
,
521 mask
&= ~BUFFER_BIT_BACK_LEFT
;
527 _swrast_Clear( ctx
, mask
, all
, x
, y
, width
, height
);
531 #ifndef XFree86Server
532 /* XXX this was never tested in the Xserver environment */
535 * This function implements glDrawPixels() with an XPutImage call when
536 * drawing to the front buffer (X Window drawable).
537 * The image format must be GL_BGRA to match the PF_8R8G8B pixel format.
540 xmesa_DrawPixels_8R8G8B( GLcontext
*ctx
,
541 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
542 GLenum format
, GLenum type
,
543 const struct gl_pixelstore_attrib
*unpack
,
544 const GLvoid
*pixels
)
546 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
547 struct gl_renderbuffer
*rb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[0][0];
548 struct xmesa_renderbuffer
*xrb
= (struct xmesa_renderbuffer
*) rb
->Wrapped
;
550 if (swrast
->NewState
)
551 _swrast_validate_derived( ctx
);
553 if (ctx
->DrawBuffer
->Name
== 0 &&
555 type
== GL_UNSIGNED_BYTE
&&
556 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
557 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
558 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
559 ctx
->Pixel
.ZoomY
== 1.0 &&
561 xrb
->Base
.AlphaBits
== 0)
563 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
564 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
565 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
566 const XMesaGC gc
= xmbuf
->gc
;
571 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
573 ASSERT(xmesa
->xm_visual
->dithered_pf
== PF_8R8G8B
);
574 ASSERT(xmesa
->xm_visual
->undithered_pf
== PF_8R8G8B
);
578 if (unpack
->BufferObj
->Name
) {
579 /* unpack from PBO */
581 if (!_mesa_validate_pbo_access(2, unpack
, width
, height
, 1,
582 format
, type
, pixels
)) {
583 _mesa_error(ctx
, GL_INVALID_OPERATION
,
584 "glDrawPixels(invalid PBO access)");
587 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
,
588 GL_PIXEL_UNPACK_BUFFER_EXT
,
592 /* buffer is already mapped - that's an error */
593 _mesa_error(ctx
, GL_INVALID_OPERATION
,
594 "glDrawPixels(PBO is mapped)");
597 pixels
= ADD_POINTERS(buf
, pixels
);
600 if (_mesa_clip_drawpixels(ctx
, &dstX
, &dstY
, &w
, &h
, &clippedUnpack
)) {
601 /* This is a little tricky since all coordinates up to now have
602 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
603 * so we have to carefully compute the Y coordinates/addresses here.
605 int srcX
= clippedUnpack
.SkipPixels
;
606 int srcY
= clippedUnpack
.SkipRows
;
607 int rowLength
= clippedUnpack
.RowLength
;
609 MEMSET(&ximage
, 0, sizeof(XMesaImage
));
610 ximage
.width
= width
;
611 ximage
.height
= height
;
612 ximage
.format
= ZPixmap
;
613 ximage
.data
= (char *) pixels
614 + ((srcY
+ h
- 1) * rowLength
+ srcX
) * 4;
615 ximage
.byte_order
= LSBFirst
;
616 ximage
.bitmap_unit
= 32;
617 ximage
.bitmap_bit_order
= LSBFirst
;
618 ximage
.bitmap_pad
= 32;
620 ximage
.bytes_per_line
= -rowLength
* 4; /* negative to flip image */
621 ximage
.bits_per_pixel
= 32;
622 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
623 /* flip Y axis for dest position */
624 dstY
= YFLIP(xrb
, dstY
) - h
+ 1;
625 XPutImage(dpy
, xrb
->pixmap
, gc
, &ximage
, 0, 0, dstX
, dstY
, w
, h
);
628 if (unpack
->BufferObj
->Name
) {
629 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
634 /* software fallback */
635 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
636 format
, type
, unpack
, pixels
);
643 * This function implements glDrawPixels() with an XPutImage call when
644 * drawing to the front buffer (X Window drawable). The image format
645 * must be GL_RGB and image type must be GL_UNSIGNED_SHORT_5_6_5 to
646 * match the PF_5R6G5B pixel format.
649 xmesa_DrawPixels_5R6G5B( GLcontext
*ctx
,
650 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
651 GLenum format
, GLenum type
,
652 const struct gl_pixelstore_attrib
*unpack
,
653 const GLvoid
*pixels
)
655 struct xmesa_renderbuffer
*xrb
656 = (struct xmesa_renderbuffer
*) ctx
->DrawBuffer
->_ColorDrawBuffers
[0][0];
657 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
658 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
659 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
660 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
661 const XMesaGC gc
= xmbuf
->gc
;
665 ASSERT(xmesa
->xm_visual
->undithered_pf
== PF_5R6G5B
);
667 if (swrast
->NewState
)
668 _swrast_validate_derived( ctx
);
672 type
== GL_UNSIGNED_SHORT_5_6_5
&&
673 !ctx
->Color
.DitherFlag
&& /* no dithering */
674 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
675 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
676 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
677 ctx
->Pixel
.ZoomY
== 1.0) {
682 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
684 if (unpack
->BufferObj
->Name
) {
685 /* unpack from PBO */
687 if (!_mesa_validate_pbo_access(2, unpack
, width
, height
, 1,
688 format
, type
, pixels
)) {
689 _mesa_error(ctx
, GL_INVALID_OPERATION
,
690 "glDrawPixels(invalid PBO access)");
693 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
,
694 GL_PIXEL_UNPACK_BUFFER_EXT
,
698 /* buffer is already mapped - that's an error */
699 _mesa_error(ctx
, GL_INVALID_OPERATION
,
700 "glDrawPixels(PBO is mapped)");
703 pixels
= ADD_POINTERS(buf
, pixels
);
706 if (_mesa_clip_drawpixels(ctx
, &dstX
, &dstY
, &w
, &h
, &clippedUnpack
)) {
707 /* This is a little tricky since all coordinates up to now have
708 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
709 * so we have to carefully compute the Y coordinates/addresses here.
711 int srcX
= clippedUnpack
.SkipPixels
;
712 int srcY
= clippedUnpack
.SkipRows
;
713 int rowLength
= clippedUnpack
.RowLength
;
715 MEMSET(&ximage
, 0, sizeof(XMesaImage
));
716 ximage
.width
= width
;
717 ximage
.height
= height
;
718 ximage
.format
= ZPixmap
;
719 ximage
.data
= (char *) pixels
720 + ((srcY
+ h
- 1) * rowLength
+ srcX
) * 2;
721 ximage
.byte_order
= LSBFirst
;
722 ximage
.bitmap_unit
= 16;
723 ximage
.bitmap_bit_order
= LSBFirst
;
724 ximage
.bitmap_pad
= 16;
726 ximage
.bytes_per_line
= -rowLength
* 2; /* negative to flip image */
727 ximage
.bits_per_pixel
= 16;
728 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
729 /* flip Y axis for dest position */
730 dstY
= YFLIP(xrb
, dstY
) - h
+ 1;
731 XPutImage(dpy
, xrb
->pixmap
, gc
, &ximage
, 0, 0, dstX
, dstY
, w
, h
);
734 if (unpack
->BufferObj
->Name
) {
735 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
740 /* software fallback */
741 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
742 format
, type
, unpack
, pixels
);
749 * Implement glCopyPixels for the front color buffer (or back buffer Pixmap)
750 * for the color buffer. Don't support zooming, pixel transfer, etc.
751 * We do support copying from one window to another, ala glXMakeCurrentRead.
754 xmesa_CopyPixels( GLcontext
*ctx
,
755 GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
756 GLint destx
, GLint desty
, GLenum type
)
758 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
759 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
760 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
761 const XMesaGC gc
= ((XMesaBuffer
) ctx
->DrawBuffer
)->gc
;
762 struct xmesa_renderbuffer
*srcXrb
= (struct xmesa_renderbuffer
*)
763 ctx
->ReadBuffer
->_ColorReadBuffer
;
764 struct xmesa_renderbuffer
*dstXrb
= (struct xmesa_renderbuffer
*)
765 ctx
->DrawBuffer
->_ColorDrawBuffers
[0][0];
770 if (swrast
->NewState
)
771 _swrast_validate_derived( ctx
);
773 if (ctx
->Color
.DrawBuffer
[0] == GL_FRONT
&&
774 ctx
->Pixel
.ReadBuffer
== GL_FRONT
&&
778 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
779 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
780 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
781 ctx
->Pixel
.ZoomY
== 1.0) {
782 /* Note: we don't do any special clipping work here. We could,
783 * but X will do it for us.
785 srcy
= YFLIP(srcXrb
, srcy
) - height
+ 1;
786 desty
= YFLIP(dstXrb
, desty
) - height
+ 1;
787 XCopyArea(dpy
, srcXrb
->pixmap
, dstXrb
->pixmap
, gc
,
788 srcx
, srcy
, width
, height
, destx
, desty
);
791 _swrast_CopyPixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
, type
);
794 #endif /* XFree86Server */
799 * Every driver should implement a GetString function in order to
800 * return a meaningful GL_RENDERER string.
802 static const GLubyte
*
803 get_string( GLcontext
*ctx
, GLenum name
)
809 return (const GLubyte
*) "Mesa GLX Indirect";
811 return (const GLubyte
*) "Mesa X11";
815 return (const GLubyte
*) "Mesa project: www.mesa3d.org";
826 * We implement the glEnable function only because we care about
827 * dither enable/disable.
830 enable( GLcontext
*ctx
, GLenum pname
, GLboolean state
)
832 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
837 xmesa
->pixelformat
= xmesa
->xm_visual
->dithered_pf
;
839 xmesa
->pixelformat
= xmesa
->xm_visual
->undithered_pf
;
842 ; /* silence compiler warning */
848 clear_color_HPCR_ximage( GLcontext
*ctx
, const GLfloat color
[4] )
851 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
853 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[0], color
[0]);
854 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[1], color
[1]);
855 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[2], color
[2]);
856 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[3], color
[3]);
858 if (color
[0] == 0.0 && color
[1] == 0.0 && color
[2] == 0.0) {
860 MEMSET( xmesa
->xm_visual
->hpcr_clear_ximage_pattern
, 0x0 ,
861 sizeof(xmesa
->xm_visual
->hpcr_clear_ximage_pattern
));
864 /* build clear pattern */
865 for (i
=0; i
<16; i
++) {
866 xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0][i
] =
868 xmesa
->clearcolor
[0],
869 xmesa
->clearcolor
[1],
870 xmesa
->clearcolor
[2]);
871 xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[1][i
] =
873 xmesa
->clearcolor
[0],
874 xmesa
->clearcolor
[1],
875 xmesa
->clearcolor
[2]);
882 clear_color_HPCR_pixmap( GLcontext
*ctx
, const GLfloat color
[4] )
885 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
887 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[0], color
[0]);
888 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[1], color
[1]);
889 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[2], color
[2]);
890 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[3], color
[3]);
892 if (color
[0] == 0.0 && color
[1] == 0.0 && color
[2] == 0.0) {
894 for (i
=0; i
<16; i
++) {
895 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 0, 0);
896 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 1, 0);
900 for (i
=0; i
<16; i
++) {
901 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 0,
903 xmesa
->clearcolor
[0],
904 xmesa
->clearcolor
[1],
905 xmesa
->clearcolor
[2]));
906 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 1,
908 xmesa
->clearcolor
[0],
909 xmesa
->clearcolor
[1],
910 xmesa
->clearcolor
[2]));
913 /* change tile pixmap content */
914 XMesaPutImage(xmesa
->display
,
915 (XMesaDrawable
)xmesa
->xm_visual
->hpcr_clear_pixmap
,
916 XMESA_BUFFER(ctx
->DrawBuffer
)->cleargc
,
917 xmesa
->xm_visual
->hpcr_clear_ximage
, 0, 0, 0, 0, 16, 2);
922 * Called when the driver should update its state, based on the new_state
926 xmesa_update_state( GLcontext
*ctx
, GLbitfield new_state
)
928 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
930 /* Propagate statechange information to swrast and swrast_setup
931 * modules. The X11 driver has no internal GL-dependent state.
933 _swrast_InvalidateState( ctx
, new_state
);
934 _ac_InvalidateState( ctx
, new_state
);
935 _tnl_InvalidateState( ctx
, new_state
);
936 _swsetup_InvalidateState( ctx
, new_state
);
938 if (ctx
->DrawBuffer
->Name
!= 0)
942 * GL_DITHER, GL_READ/DRAW_BUFFER, buffer binding state, etc. effect
943 * renderbuffer span/clear funcs.
945 if (new_state
& (_NEW_COLOR
| _NEW_PIXEL
| _NEW_BUFFERS
)) {
946 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
947 struct xmesa_renderbuffer
*front_xrb
, *back_xrb
;
949 front_xrb
= xmbuf
->frontxrb
;
951 xmesa_set_renderbuffer_funcs(front_xrb
, xmesa
->pixelformat
,
952 xmesa
->xm_visual
->BitsPerPixel
);
953 front_xrb
->clearFunc
= clear_pixmap
;
956 back_xrb
= xmbuf
->backxrb
;
958 xmesa_set_renderbuffer_funcs(back_xrb
, xmesa
->pixelformat
,
959 xmesa
->xm_visual
->BitsPerPixel
);
960 if (xmbuf
->backxrb
->pixmap
) {
961 back_xrb
->clearFunc
= clear_pixmap
;
964 switch (xmesa
->xm_visual
->BitsPerPixel
) {
966 if (xmesa
->xm_visual
->hpcr_clear_flag
) {
967 back_xrb
->clearFunc
= clear_HPCR_ximage
;
970 back_xrb
->clearFunc
= clear_8bit_ximage
;
974 back_xrb
->clearFunc
= clear_16bit_ximage
;
977 back_xrb
->clearFunc
= clear_24bit_ximage
;
980 back_xrb
->clearFunc
= clear_32bit_ximage
;
983 back_xrb
->clearFunc
= clear_nbit_ximage
;
990 if (xmesa
->xm_visual
->hpcr_clear_flag
) {
991 /* this depends on whether we're drawing to the front or back buffer */
995 ctx
->Driver
.ClearColor
= clear_color_HPCR_pixmap
;
998 ctx
->Driver
.ClearColor
= clear_color_HPCR_ximage
;
1001 (void) clear_color_HPCR_pixmap
;
1002 (void) clear_color_HPCR_ximage
;
1010 * Called via ctx->Driver.TestProxyTeximage(). Normally, we'd just use
1011 * the _mesa_test_proxy_teximage() fallback function, but we're going to
1012 * special-case the 3D texture case to allow textures up to 512x512x32
1016 test_proxy_teximage(GLcontext
*ctx
, GLenum target
, GLint level
,
1017 GLint internalFormat
, GLenum format
, GLenum type
,
1018 GLint width
, GLint height
, GLint depth
, GLint border
)
1020 if (target
== GL_PROXY_TEXTURE_3D
) {
1021 /* special case for 3D textures */
1022 if (width
* height
* depth
> 512 * 512 * 64 ||
1023 width
< 2 * border
||
1024 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1025 _mesa_bitcount(width
- 2 * border
) != 1) ||
1026 height
< 2 * border
||
1027 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1028 _mesa_bitcount(height
- 2 * border
) != 1) ||
1029 depth
< 2 * border
||
1030 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1031 _mesa_bitcount(depth
- 2 * border
) != 1)) {
1032 /* Bad size, or too many texels */
1038 /* use the fallback routine for 1D, 2D, cube and rect targets */
1039 return _mesa_test_proxy_teximage(ctx
, target
, level
, internalFormat
,
1040 format
, type
, width
, height
, depth
,
1047 * In SW, we don't really compress GL_COMPRESSED_RGB[A] textures!
1049 static const struct gl_texture_format
*
1050 choose_tex_format( GLcontext
*ctx
, GLint internalFormat
,
1051 GLenum format
, GLenum type
)
1053 switch (internalFormat
) {
1054 case GL_COMPRESSED_RGB_ARB
:
1055 return &_mesa_texformat_rgb
;
1056 case GL_COMPRESSED_RGBA_ARB
:
1057 return &_mesa_texformat_rgba
;
1059 return _mesa_choose_tex_format(ctx
, internalFormat
, format
, type
);
1065 * Called by glViewport.
1066 * This is a good time for us to poll the current X window size and adjust
1067 * our renderbuffers to match the current window size.
1068 * Remember, we have no opportunity to respond to conventional
1069 * X Resize/StructureNotify events since the X driver has no event loop.
1071 * Note that this trick isn't fool-proof. If the application never calls
1072 * glViewport, our notion of the current window size may be incorrect.
1073 * That problem led to the GLX_MESA_resize_buffers extension.
1076 xmesa_viewport(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei w
, GLsizei h
)
1078 XMesaContext xmctx
= XMESA_CONTEXT(ctx
);
1079 XMesaBuffer xmdrawbuf
= XMESA_BUFFER(ctx
->WinSysDrawBuffer
);
1080 XMesaBuffer xmreadbuf
= XMESA_BUFFER(ctx
->WinSysReadBuffer
);
1081 xmesa_check_and_update_buffer_size(xmctx
, xmdrawbuf
);
1082 xmesa_check_and_update_buffer_size(xmctx
, xmreadbuf
);
1090 #if ENABLE_EXT_timer_query
1093 * The GL_EXT_timer_query extension is not enabled for the XServer
1094 * indirect renderer. Not sure about how/if wrapping of gettimeofday()
1098 struct xmesa_query_object
1100 struct gl_query_object Base
;
1101 struct timeval StartTime
;
1105 static struct gl_query_object
*
1106 xmesa_new_query_object(GLcontext
*ctx
, GLuint id
)
1108 struct xmesa_query_object
*q
= CALLOC_STRUCT(xmesa_query_object
);
1111 q
->Base
.Ready
= GL_TRUE
;
1118 xmesa_begin_query(GLcontext
*ctx
, GLenum target
, struct gl_query_object
*q
)
1120 if (target
== GL_TIME_ELAPSED_EXT
) {
1121 struct xmesa_query_object
*xq
= (struct xmesa_query_object
*) q
;
1122 (void) gettimeofday(&xq
->StartTime
, NULL
);
1128 * Return the difference between the two given times in microseconds.
1131 #define suseconds_t unsigned int
1134 time_diff(const struct timeval
*t0
, const struct timeval
*t1
)
1136 GLuint64EXT seconds0
= t0
->tv_sec
& 0xff; /* 0 .. 255 seconds */
1137 GLuint64EXT seconds1
= t1
->tv_sec
& 0xff; /* 0 .. 255 seconds */
1138 GLuint64EXT nanosec0
= (seconds0
* 1000000 + t0
->tv_usec
) * 1000;
1139 GLuint64EXT nanosec1
= (seconds1
* 1000000 + t1
->tv_usec
) * 1000;
1140 return nanosec1
- nanosec0
;
1145 xmesa_end_query(GLcontext
*ctx
, GLenum target
, struct gl_query_object
*q
)
1147 if (target
== GL_TIME_ELAPSED_EXT
) {
1148 struct xmesa_query_object
*xq
= (struct xmesa_query_object
*) q
;
1149 struct timeval endTime
;
1150 (void) gettimeofday(&endTime
, NULL
);
1151 /* result is in nanoseconds! */
1152 q
->Result
= time_diff(&xq
->StartTime
, &endTime
);
1157 #endif /* ENABLE_timer_query */
1161 * Initialize the device driver function table with the functions
1162 * we implement in this driver.
1165 xmesa_init_driver_functions( XMesaVisual xmvisual
,
1166 struct dd_function_table
*driver
)
1168 driver
->GetString
= get_string
;
1169 driver
->UpdateState
= xmesa_update_state
;
1170 driver
->GetBufferSize
= NULL
; /* OBSOLETE */
1171 driver
->Flush
= finish_or_flush
;
1172 driver
->Finish
= finish_or_flush
;
1173 driver
->ClearIndex
= clear_index
;
1174 driver
->ClearColor
= clear_color
;
1175 driver
->IndexMask
= index_mask
;
1176 driver
->ColorMask
= color_mask
;
1177 driver
->Enable
= enable
;
1178 driver
->Clear
= clear_buffers
;
1179 driver
->Viewport
= xmesa_viewport
;
1180 #ifndef XFree86Server
1181 driver
->CopyPixels
= xmesa_CopyPixels
;
1182 if (xmvisual
->undithered_pf
== PF_8R8G8B
&&
1183 xmvisual
->dithered_pf
== PF_8R8G8B
) {
1184 driver
->DrawPixels
= xmesa_DrawPixels_8R8G8B
;
1186 else if (xmvisual
->undithered_pf
== PF_5R6G5B
) {
1187 driver
->DrawPixels
= xmesa_DrawPixels_5R6G5B
;
1190 driver
->TestProxyTexImage
= test_proxy_teximage
;
1191 #if ENABLE_EXT_texure_compression_s3tc
1192 driver
->ChooseTextureFormat
= choose_tex_format
;
1194 (void) choose_tex_format
;
1197 #if ENABLE_EXT_timer_query
1198 driver
->NewQueryObject
= xmesa_new_query_object
;
1199 driver
->BeginQuery
= xmesa_begin_query
;
1200 driver
->EndQuery
= xmesa_end_query
;
1205 #define XMESA_NEW_POINT (_NEW_POINT | \
1207 _SWRAST_NEW_RASTERMASK)
1209 #define XMESA_NEW_LINE (_NEW_LINE | \
1214 _SWRAST_NEW_RASTERMASK)
1216 #define XMESA_NEW_TRIANGLE (_NEW_POLYGON | \
1221 _SWRAST_NEW_RASTERMASK)
1225 * Extend the software rasterizer with our line/point/triangle
1227 * Called during context creation only.
1229 void xmesa_register_swrast_functions( GLcontext
*ctx
)
1231 SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
1233 swrast
->choose_point
= xmesa_choose_point
;
1234 swrast
->choose_line
= xmesa_choose_line
;
1235 swrast
->choose_triangle
= xmesa_choose_triangle
;
1237 /* XXX these lines have no net effect. Remove??? */
1238 swrast
->InvalidatePointMask
|= XMESA_NEW_POINT
;
1239 swrast
->InvalidateLineMask
|= XMESA_NEW_LINE
;
1240 swrast
->InvalidateTriangleMask
|= XMESA_NEW_TRIANGLE
;