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
99 get_buffer_size( GLframebuffer
*buffer
, GLuint
*width
, GLuint
*height
)
101 XMesaBuffer b
= XMESA_BUFFER(buffer
);
102 xmesa_get_window_size(b
->display
, b
, width
, height
);
107 finish_or_flush( GLcontext
*ctx
)
112 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
114 _glthread_LOCK_MUTEX(_xmesa_lock
);
115 XSync( xmesa
->display
, False
);
116 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
123 clear_index( GLcontext
*ctx
, GLuint index
)
125 if (ctx
->DrawBuffer
->Name
== 0) {
126 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
127 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
128 xmesa
->clearpixel
= (unsigned long) index
;
129 XMesaSetForeground( xmesa
->display
, xmbuf
->cleargc
, (unsigned long) index
);
135 clear_color( GLcontext
*ctx
, const GLfloat color
[4] )
137 if (ctx
->DrawBuffer
->Name
== 0) {
138 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
139 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
141 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[0], color
[0]);
142 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[1], color
[1]);
143 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[2], color
[2]);
144 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[3], color
[3]);
145 xmesa
->clearpixel
= xmesa_color_to_pixel( ctx
,
146 xmesa
->clearcolor
[0],
147 xmesa
->clearcolor
[1],
148 xmesa
->clearcolor
[2],
149 xmesa
->clearcolor
[3],
150 xmesa
->xm_visual
->undithered_pf
);
151 _glthread_LOCK_MUTEX(_xmesa_lock
);
152 XMesaSetForeground( xmesa
->display
, xmbuf
->cleargc
,
154 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
160 /* Set index mask ala glIndexMask */
162 index_mask( GLcontext
*ctx
, GLuint mask
)
164 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
165 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
166 /* not sure this conditional is really needed */
167 if (xmbuf
->backxrb
&& xmbuf
->backxrb
->pixmap
) {
169 if (mask
==0xffffffff) {
170 m
= ((unsigned long)~0L);
173 m
= (unsigned long) mask
;
175 XMesaSetPlaneMask( xmesa
->display
, xmbuf
->cleargc
, m
);
176 XMesaSetPlaneMask( xmesa
->display
, xmbuf
->gc
, m
);
181 /* Implements glColorMask() */
183 color_mask(GLcontext
*ctx
,
184 GLboolean rmask
, GLboolean gmask
, GLboolean bmask
, GLboolean amask
)
186 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
188 const int xclass
= xmesa
->xm_visual
->mesa_visual
.visualType
;
191 if (ctx
->DrawBuffer
->Name
!= 0)
194 xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
196 if (xclass
== GLX_TRUE_COLOR
|| xclass
== GLX_DIRECT_COLOR
) {
198 if (rmask
&& gmask
&& bmask
) {
199 m
= ((unsigned long)~0L);
203 if (rmask
) m
|= GET_REDMASK(xmesa
->xm_visual
);
204 if (gmask
) m
|= GET_GREENMASK(xmesa
->xm_visual
);
205 if (bmask
) m
|= GET_BLUEMASK(xmesa
->xm_visual
);
207 XMesaSetPlaneMask( xmesa
->display
, xmbuf
->cleargc
, m
);
208 XMesaSetPlaneMask( xmesa
->display
, xmbuf
->gc
, m
);
214 /**********************************************************************/
215 /*** glClear implementations ***/
216 /**********************************************************************/
220 * Clear the front or back color buffer, if it's implemented with a pixmap.
223 clear_pixmap(GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
, GLboolean all
,
224 GLint x
, GLint y
, GLint width
, GLint height
)
226 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
227 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
232 assert(xmesa
->display
);
234 assert(xmbuf
->cleargc
);
237 XMesaFillRectangle( xmesa
->display
, xrb
->pixmap
, xmbuf
->cleargc
,
238 0, 0, xrb
->Base
.Width
+ 1, xrb
->Base
.Height
+ 1 );
241 XMesaFillRectangle( xmesa
->display
, xrb
->pixmap
, xmbuf
->cleargc
,
242 x
, xrb
->Base
.Height
- y
- height
,
249 clear_8bit_ximage( GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
250 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
252 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
255 const size_t n
= xrb
->ximage
->bytes_per_line
* xrb
->Base
.Height
;
256 MEMSET( xrb
->ximage
->data
, xmesa
->clearpixel
, n
);
260 for (i
=0;i
<height
;i
++) {
261 GLubyte
*ptr
= PIXEL_ADDR1(xrb
, x
, y
+ i
);
262 MEMSET( ptr
, xmesa
->clearpixel
, width
);
269 clear_HPCR_ximage( GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
270 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
272 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
275 const GLuint c16
= xrb
->ximage
->bytes_per_line
& ~0xf;
277 GLubyte
*ptr
= (GLubyte
*) xrb
->ximage
->data
;
278 for (i
= 0; i
< xrb
->Base
.Height
; i
++) {
280 const GLubyte
*sptr
= xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0];
284 for (j
=0; j
<c16
; j
+=16) {
303 for (; j
< (GLuint
) xrb
->ximage
->bytes_per_line
; j
++) {
311 for (i
=y
; i
<y
+height
; i
++) {
312 GLubyte
*ptr
= PIXEL_ADDR1( xrb
, x
, i
);
314 const GLubyte
*sptr
= xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0];
318 for (j
=x
; j
<x
+width
; j
++) {
328 clear_16bit_ximage( GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
329 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
331 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
332 register GLuint pixel
= (GLuint
) xmesa
->clearpixel
;
334 if (xmesa
->swapbytes
) {
335 pixel
= ((pixel
>> 8) & 0x00ff) | ((pixel
<< 8) & 0xff00);
339 GLuint
*ptr4
= (GLuint
*) xrb
->ximage
->data
;
340 if ((pixel
& 0xff) == ((pixel
>> 8) & 0xff)) {
341 /* low and high bytes are equal so use memset() */
342 const GLuint n
= xrb
->ximage
->bytes_per_line
* xrb
->Base
.Height
;
343 MEMSET( ptr4
, pixel
& 0xff, n
);
346 const GLuint n
= xrb
->ximage
->bytes_per_line
* xrb
->Base
.Height
/ 4;
348 pixel
= pixel
| (pixel
<<16);
349 for (i
= 0; i
< n
; i
++) {
353 /* might be one last GLushort to set */
354 if ((xrb
->ximage
->bytes_per_line
* xrb
->Base
.Height
) & 0x2)
355 *(GLushort
*)ptr4
= pixel
& 0xffff;
360 for (j
=0;j
<height
;j
++) {
361 GLushort
*ptr2
= PIXEL_ADDR2(xrb
, x
, y
+ j
);
362 for (i
=0;i
<width
;i
++) {
370 /* Optimized code provided by Nozomi Ytow <noz@xfree86.org> */
372 clear_24bit_ximage(GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
373 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
375 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
376 const GLubyte r
= xmesa
->clearcolor
[0];
377 const GLubyte g
= xmesa
->clearcolor
[1];
378 const GLubyte b
= xmesa
->clearcolor
[2];
381 if (r
== g
&& g
== b
) {
382 /* same value for all three components (gray) */
383 const GLint w3
= xrb
->Base
.Width
* 3;
384 const GLint h
= xrb
->Base
.Height
;
386 for (i
= 0; i
< h
; i
++) {
387 bgr_t
*ptr3
= PIXEL_ADDR3(xrb
, 0, i
);
393 const GLint w
= xrb
->Base
.Width
;
394 const GLint h
= xrb
->Base
.Height
;
396 for (i
= 0; i
< h
; i
++) {
397 bgr_t
*ptr3
= PIXEL_ADDR3(xrb
, 0, i
);
398 for (j
= 0; j
< w
; j
++) {
408 /* only clear subrect of color buffer */
409 if (r
== g
&& g
== b
) {
410 /* same value for all three components (gray) */
412 for (j
=0;j
<height
;j
++) {
413 bgr_t
*ptr3
= PIXEL_ADDR3(xrb
, x
, y
+ j
);
414 MEMSET(ptr3
, r
, 3 * width
);
418 /* non-gray clear color */
420 for (j
= 0; j
< height
; j
++) {
421 bgr_t
*ptr3
= PIXEL_ADDR3(xrb
, x
, y
+ j
);
422 for (i
= 0; i
< width
; i
++) {
435 clear_32bit_ximage(GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
436 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
438 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
439 register GLuint pixel
= (GLuint
) xmesa
->clearpixel
;
444 if (xmesa
->swapbytes
) {
445 pixel
= ((pixel
>> 24) & 0x000000ff)
446 | ((pixel
>> 8) & 0x0000ff00)
447 | ((pixel
<< 8) & 0x00ff0000)
448 | ((pixel
<< 24) & 0xff000000);
452 const GLuint n
= xrb
->Base
.Width
* xrb
->Base
.Height
;
453 GLuint
*ptr4
= (GLuint
*) xrb
->ximage
->data
;
455 _mesa_memset(ptr4
, pixel
, 4 * n
);
459 for (i
= 0; i
< n
; i
++)
465 for (j
= 0; j
< height
; j
++) {
466 GLuint
*ptr4
= PIXEL_ADDR4(xrb
, x
, y
+ j
);
467 for (i
= 0; i
< width
; i
++) {
476 clear_nbit_ximage(GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
477 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
479 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
480 XMesaImage
*img
= xrb
->ximage
;
483 /* We can ignore 'all' here - x, y, width, height are always right */
486 /* TODO: optimize this */
488 for (j
= 0; j
< height
; j
++) {
489 for (i
= 0; i
< width
; i
++) {
490 XMesaPutPixel(img
, x
+i
, y
-j
, xmesa
->clearpixel
);
498 clear_buffers( GLcontext
*ctx
, GLbitfield mask
,
499 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
501 if (ctx
->DrawBuffer
->Name
== 0) {
502 /* this is a window system framebuffer */
503 const GLuint
*colorMask
= (GLuint
*) &ctx
->Color
.ColorMask
;
504 XMesaBuffer b
= XMESA_BUFFER(ctx
->DrawBuffer
);
506 /* we can't handle color or index masking */
507 if (*colorMask
== 0xffffffff && ctx
->Color
.IndexMask
== 0xffffffff) {
508 if (mask
& BUFFER_BIT_FRONT_LEFT
) {
509 /* clear front color buffer */
510 struct gl_renderbuffer
*frontRb
511 = ctx
->DrawBuffer
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
;
512 if (b
->frontxrb
== xmesa_renderbuffer(frontRb
)) {
513 /* renderbuffer is not wrapped - great! */
514 b
->frontxrb
->clearFunc(ctx
, b
->frontxrb
, all
, x
, y
,
516 mask
&= ~BUFFER_BIT_FRONT_LEFT
;
519 /* we can't directly clear an alpha-wrapped color buffer */
522 if (mask
& BUFFER_BIT_BACK_LEFT
) {
523 /* clear back color buffer */
524 struct gl_renderbuffer
*backRb
525 = ctx
->DrawBuffer
->Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
;
526 if (b
->backxrb
== xmesa_renderbuffer(backRb
)) {
527 /* renderbuffer is not wrapped - great! */
528 b
->backxrb
->clearFunc(ctx
, b
->backxrb
, all
, x
, y
,
530 mask
&= ~BUFFER_BIT_BACK_LEFT
;
536 _swrast_Clear( ctx
, mask
, all
, x
, y
, width
, height
);
540 #ifndef XFree86Server
541 /* XXX this was never tested in the Xserver environment */
544 * This function implements glDrawPixels() with an XPutImage call when
545 * drawing to the front buffer (X Window drawable).
546 * The image format must be GL_BGRA to match the PF_8R8G8B pixel format.
549 xmesa_DrawPixels_8R8G8B( GLcontext
*ctx
,
550 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
551 GLenum format
, GLenum type
,
552 const struct gl_pixelstore_attrib
*unpack
,
553 const GLvoid
*pixels
)
555 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
556 struct gl_renderbuffer
*rb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[0][0];
557 struct xmesa_renderbuffer
*xrb
= (struct xmesa_renderbuffer
*) rb
->Wrapped
;
559 if (swrast
->NewState
)
560 _swrast_validate_derived( ctx
);
562 if (ctx
->DrawBuffer
->Name
== 0 &&
564 type
== GL_UNSIGNED_BYTE
&&
565 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
566 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
567 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
568 ctx
->Pixel
.ZoomY
== 1.0 &&
570 xrb
->Base
.AlphaBits
== 0)
572 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
573 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
574 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
575 const XMesaGC gc
= xmbuf
->gc
;
580 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
582 ASSERT(xmesa
->xm_visual
->dithered_pf
== PF_8R8G8B
);
583 ASSERT(xmesa
->xm_visual
->undithered_pf
== PF_8R8G8B
);
587 if (unpack
->BufferObj
->Name
) {
588 /* unpack from PBO */
590 if (!_mesa_validate_pbo_access(2, unpack
, width
, height
, 1,
591 format
, type
, pixels
)) {
592 _mesa_error(ctx
, GL_INVALID_OPERATION
,
593 "glDrawPixels(invalid PBO access)");
596 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
,
597 GL_PIXEL_UNPACK_BUFFER_EXT
,
601 /* buffer is already mapped - that's an error */
602 _mesa_error(ctx
, GL_INVALID_OPERATION
,
603 "glDrawPixels(PBO is mapped)");
606 pixels
= ADD_POINTERS(buf
, pixels
);
609 if (_mesa_clip_drawpixels(ctx
, &dstX
, &dstY
, &w
, &h
, &clippedUnpack
)) {
610 /* This is a little tricky since all coordinates up to now have
611 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
612 * so we have to carefully compute the Y coordinates/addresses here.
614 int srcX
= clippedUnpack
.SkipPixels
;
615 int srcY
= clippedUnpack
.SkipRows
;
616 int rowLength
= clippedUnpack
.RowLength
;
618 MEMSET(&ximage
, 0, sizeof(XMesaImage
));
619 ximage
.width
= width
;
620 ximage
.height
= height
;
621 ximage
.format
= ZPixmap
;
622 ximage
.data
= (char *) pixels
623 + ((srcY
+ h
- 1) * rowLength
+ srcX
) * 4;
624 ximage
.byte_order
= LSBFirst
;
625 ximage
.bitmap_unit
= 32;
626 ximage
.bitmap_bit_order
= LSBFirst
;
627 ximage
.bitmap_pad
= 32;
629 ximage
.bytes_per_line
= -rowLength
* 4; /* negative to flip image */
630 ximage
.bits_per_pixel
= 32;
631 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
632 /* flip Y axis for dest position */
633 dstY
= YFLIP(xrb
, dstY
) - h
+ 1;
634 XPutImage(dpy
, xrb
->pixmap
, gc
, &ximage
, 0, 0, dstX
, dstY
, w
, h
);
637 if (unpack
->BufferObj
->Name
) {
638 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
643 /* software fallback */
644 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
645 format
, type
, unpack
, pixels
);
652 * This function implements glDrawPixels() with an XPutImage call when
653 * drawing to the front buffer (X Window drawable). The image format
654 * must be GL_RGB and image type must be GL_UNSIGNED_SHORT_5_6_5 to
655 * match the PF_5R6G5B pixel format.
658 xmesa_DrawPixels_5R6G5B( GLcontext
*ctx
,
659 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
660 GLenum format
, GLenum type
,
661 const struct gl_pixelstore_attrib
*unpack
,
662 const GLvoid
*pixels
)
664 struct xmesa_renderbuffer
*xrb
665 = (struct xmesa_renderbuffer
*) ctx
->DrawBuffer
->_ColorDrawBuffers
[0][0];
666 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
667 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
668 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
669 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
670 const XMesaGC gc
= xmbuf
->gc
;
674 ASSERT(xmesa
->xm_visual
->undithered_pf
== PF_5R6G5B
);
676 if (swrast
->NewState
)
677 _swrast_validate_derived( ctx
);
681 type
== GL_UNSIGNED_SHORT_5_6_5
&&
682 !ctx
->Color
.DitherFlag
&& /* no dithering */
683 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
684 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
685 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
686 ctx
->Pixel
.ZoomY
== 1.0) {
691 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
693 if (unpack
->BufferObj
->Name
) {
694 /* unpack from PBO */
696 if (!_mesa_validate_pbo_access(2, unpack
, width
, height
, 1,
697 format
, type
, pixels
)) {
698 _mesa_error(ctx
, GL_INVALID_OPERATION
,
699 "glDrawPixels(invalid PBO access)");
702 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
,
703 GL_PIXEL_UNPACK_BUFFER_EXT
,
707 /* buffer is already mapped - that's an error */
708 _mesa_error(ctx
, GL_INVALID_OPERATION
,
709 "glDrawPixels(PBO is mapped)");
712 pixels
= ADD_POINTERS(buf
, pixels
);
715 if (_mesa_clip_drawpixels(ctx
, &dstX
, &dstY
, &w
, &h
, &clippedUnpack
)) {
716 /* This is a little tricky since all coordinates up to now have
717 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
718 * so we have to carefully compute the Y coordinates/addresses here.
720 int srcX
= clippedUnpack
.SkipPixels
;
721 int srcY
= clippedUnpack
.SkipRows
;
722 int rowLength
= clippedUnpack
.RowLength
;
724 MEMSET(&ximage
, 0, sizeof(XMesaImage
));
725 ximage
.width
= width
;
726 ximage
.height
= height
;
727 ximage
.format
= ZPixmap
;
728 ximage
.data
= (char *) pixels
729 + ((srcY
+ h
- 1) * rowLength
+ srcX
) * 2;
730 ximage
.byte_order
= LSBFirst
;
731 ximage
.bitmap_unit
= 16;
732 ximage
.bitmap_bit_order
= LSBFirst
;
733 ximage
.bitmap_pad
= 16;
735 ximage
.bytes_per_line
= -rowLength
* 2; /* negative to flip image */
736 ximage
.bits_per_pixel
= 16;
737 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
738 /* flip Y axis for dest position */
739 dstY
= YFLIP(xrb
, dstY
) - h
+ 1;
740 XPutImage(dpy
, xrb
->pixmap
, gc
, &ximage
, 0, 0, dstX
, dstY
, w
, h
);
743 if (unpack
->BufferObj
->Name
) {
744 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
749 /* software fallback */
750 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
751 format
, type
, unpack
, pixels
);
758 * Implement glCopyPixels for the front color buffer (or back buffer Pixmap)
759 * for the color buffer. Don't support zooming, pixel transfer, etc.
760 * We do support copying from one window to another, ala glXMakeCurrentRead.
763 xmesa_CopyPixels( GLcontext
*ctx
,
764 GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
765 GLint destx
, GLint desty
, GLenum type
)
767 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
768 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
769 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
770 const XMesaGC gc
= ((XMesaBuffer
) ctx
->DrawBuffer
)->gc
;
771 struct xmesa_renderbuffer
*srcXrb
= (struct xmesa_renderbuffer
*)
772 ctx
->ReadBuffer
->_ColorReadBuffer
;
773 struct xmesa_renderbuffer
*dstXrb
= (struct xmesa_renderbuffer
*)
774 ctx
->DrawBuffer
->_ColorDrawBuffers
[0][0];
779 if (swrast
->NewState
)
780 _swrast_validate_derived( ctx
);
782 if (ctx
->Color
.DrawBuffer
[0] == GL_FRONT
&&
783 ctx
->Pixel
.ReadBuffer
== GL_FRONT
&&
787 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
788 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
789 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
790 ctx
->Pixel
.ZoomY
== 1.0) {
791 /* Note: we don't do any special clipping work here. We could,
792 * but X will do it for us.
794 srcy
= YFLIP(srcXrb
, srcy
) - height
+ 1;
795 desty
= YFLIP(dstXrb
, desty
) - height
+ 1;
796 XCopyArea(dpy
, srcXrb
->pixmap
, dstXrb
->pixmap
, gc
,
797 srcx
, srcy
, width
, height
, destx
, desty
);
800 _swrast_CopyPixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
, type
);
803 #endif /* XFree86Server */
808 * Every driver should implement a GetString function in order to
809 * return a meaningful GL_RENDERER string.
811 static const GLubyte
*
812 get_string( GLcontext
*ctx
, GLenum name
)
818 return (const GLubyte
*) "Mesa GLX Indirect";
820 return (const GLubyte
*) "Mesa X11";
824 return (const GLubyte
*) "Mesa project: www.mesa3d.org";
835 * We implement the glEnable function only because we care about
836 * dither enable/disable.
839 enable( GLcontext
*ctx
, GLenum pname
, GLboolean state
)
841 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
846 xmesa
->pixelformat
= xmesa
->xm_visual
->dithered_pf
;
848 xmesa
->pixelformat
= xmesa
->xm_visual
->undithered_pf
;
851 ; /* silence compiler warning */
857 clear_color_HPCR_ximage( GLcontext
*ctx
, const GLfloat color
[4] )
860 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
862 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[0], color
[0]);
863 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[1], color
[1]);
864 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[2], color
[2]);
865 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[3], color
[3]);
867 if (color
[0] == 0.0 && color
[1] == 0.0 && color
[2] == 0.0) {
869 MEMSET( xmesa
->xm_visual
->hpcr_clear_ximage_pattern
, 0x0 ,
870 sizeof(xmesa
->xm_visual
->hpcr_clear_ximage_pattern
));
873 /* build clear pattern */
874 for (i
=0; i
<16; i
++) {
875 xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0][i
] =
877 xmesa
->clearcolor
[0],
878 xmesa
->clearcolor
[1],
879 xmesa
->clearcolor
[2]);
880 xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[1][i
] =
882 xmesa
->clearcolor
[0],
883 xmesa
->clearcolor
[1],
884 xmesa
->clearcolor
[2]);
891 clear_color_HPCR_pixmap( GLcontext
*ctx
, const GLfloat color
[4] )
894 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
896 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[0], color
[0]);
897 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[1], color
[1]);
898 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[2], color
[2]);
899 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[3], color
[3]);
901 if (color
[0] == 0.0 && color
[1] == 0.0 && color
[2] == 0.0) {
903 for (i
=0; i
<16; i
++) {
904 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 0, 0);
905 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 1, 0);
909 for (i
=0; i
<16; i
++) {
910 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 0,
912 xmesa
->clearcolor
[0],
913 xmesa
->clearcolor
[1],
914 xmesa
->clearcolor
[2]));
915 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 1,
917 xmesa
->clearcolor
[0],
918 xmesa
->clearcolor
[1],
919 xmesa
->clearcolor
[2]));
922 /* change tile pixmap content */
923 XMesaPutImage(xmesa
->display
,
924 (XMesaDrawable
)xmesa
->xm_visual
->hpcr_clear_pixmap
,
925 XMESA_BUFFER(ctx
->DrawBuffer
)->cleargc
,
926 xmesa
->xm_visual
->hpcr_clear_ximage
, 0, 0, 0, 0, 16, 2);
931 * Called when the driver should update its state, based on the new_state
935 xmesa_update_state( GLcontext
*ctx
, GLbitfield new_state
)
937 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
939 /* Propagate statechange information to swrast and swrast_setup
940 * modules. The X11 driver has no internal GL-dependent state.
942 _swrast_InvalidateState( ctx
, new_state
);
943 _ac_InvalidateState( ctx
, new_state
);
944 _tnl_InvalidateState( ctx
, new_state
);
945 _swsetup_InvalidateState( ctx
, new_state
);
947 if (ctx
->DrawBuffer
->Name
!= 0)
951 * GL_DITHER, GL_READ/DRAW_BUFFER, buffer binding state, etc. effect
952 * renderbuffer span/clear funcs.
954 if (new_state
& (_NEW_COLOR
| _NEW_PIXEL
| _NEW_BUFFERS
)) {
955 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
956 struct xmesa_renderbuffer
*front_xrb
, *back_xrb
;
958 front_xrb
= xmbuf
->frontxrb
;
960 xmesa_set_renderbuffer_funcs(front_xrb
, xmesa
->pixelformat
,
961 xmesa
->xm_visual
->BitsPerPixel
);
962 front_xrb
->clearFunc
= clear_pixmap
;
965 back_xrb
= xmbuf
->backxrb
;
967 xmesa_set_renderbuffer_funcs(back_xrb
, xmesa
->pixelformat
,
968 xmesa
->xm_visual
->BitsPerPixel
);
969 if (xmbuf
->backxrb
->pixmap
) {
970 back_xrb
->clearFunc
= clear_pixmap
;
973 switch (xmesa
->xm_visual
->BitsPerPixel
) {
975 if (xmesa
->xm_visual
->hpcr_clear_flag
) {
976 back_xrb
->clearFunc
= clear_HPCR_ximage
;
979 back_xrb
->clearFunc
= clear_8bit_ximage
;
983 back_xrb
->clearFunc
= clear_16bit_ximage
;
986 back_xrb
->clearFunc
= clear_24bit_ximage
;
989 back_xrb
->clearFunc
= clear_32bit_ximage
;
992 back_xrb
->clearFunc
= clear_nbit_ximage
;
999 if (xmesa
->xm_visual
->hpcr_clear_flag
) {
1000 /* this depends on whether we're drawing to the front or back buffer */
1004 ctx
->Driver
.ClearColor
= clear_color_HPCR_pixmap
;
1007 ctx
->Driver
.ClearColor
= clear_color_HPCR_ximage
;
1010 (void) clear_color_HPCR_pixmap
;
1011 (void) clear_color_HPCR_ximage
;
1019 * Called via ctx->Driver.TestProxyTeximage(). Normally, we'd just use
1020 * the _mesa_test_proxy_teximage() fallback function, but we're going to
1021 * special-case the 3D texture case to allow textures up to 512x512x32
1025 test_proxy_teximage(GLcontext
*ctx
, GLenum target
, GLint level
,
1026 GLint internalFormat
, GLenum format
, GLenum type
,
1027 GLint width
, GLint height
, GLint depth
, GLint border
)
1029 if (target
== GL_PROXY_TEXTURE_3D
) {
1030 /* special case for 3D textures */
1031 if (width
* height
* depth
> 512 * 512 * 64 ||
1032 width
< 2 * border
||
1033 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1034 _mesa_bitcount(width
- 2 * border
) != 1) ||
1035 height
< 2 * border
||
1036 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1037 _mesa_bitcount(height
- 2 * border
) != 1) ||
1038 depth
< 2 * border
||
1039 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1040 _mesa_bitcount(depth
- 2 * border
) != 1)) {
1041 /* Bad size, or too many texels */
1047 /* use the fallback routine for 1D, 2D, cube and rect targets */
1048 return _mesa_test_proxy_teximage(ctx
, target
, level
, internalFormat
,
1049 format
, type
, width
, height
, depth
,
1056 * In SW, we don't really compress GL_COMPRESSED_RGB[A] textures!
1058 static const struct gl_texture_format
*
1059 choose_tex_format( GLcontext
*ctx
, GLint internalFormat
,
1060 GLenum format
, GLenum type
)
1062 switch (internalFormat
) {
1063 case GL_COMPRESSED_RGB_ARB
:
1064 return &_mesa_texformat_rgb
;
1065 case GL_COMPRESSED_RGBA_ARB
:
1066 return &_mesa_texformat_rgba
;
1068 return _mesa_choose_tex_format(ctx
, internalFormat
, format
, type
);
1074 * Called by glViewport.
1075 * This is a good time for us to poll the current X window size and adjust
1076 * our renderbuffers to match the current window size.
1077 * Remember, we have no opportunity to respond to conventional
1078 * X Resize/StructureNotify events since the X driver has no event loop.
1080 * Note that this trick isn't fool-proof. If the application never calls
1081 * glViewport, our notion of the current window size may be incorrect.
1082 * That problem led to the GLX_MESA_resize_buffers extension.
1085 xmesa_viewport(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei w
, GLsizei h
)
1087 XMesaContext xmctx
= XMESA_CONTEXT(ctx
);
1088 XMesaBuffer xmdrawbuf
= XMESA_BUFFER(ctx
->WinSysDrawBuffer
);
1089 XMesaBuffer xmreadbuf
= XMESA_BUFFER(ctx
->WinSysReadBuffer
);
1090 xmesa_check_and_update_buffer_size(xmctx
, xmdrawbuf
);
1091 xmesa_check_and_update_buffer_size(xmctx
, xmreadbuf
);
1099 #if ENABLE_EXT_timer_query
1102 * The GL_EXT_timer_query extension is not enabled for the XServer
1103 * indirect renderer. Not sure about how/if wrapping of gettimeofday()
1107 struct xmesa_query_object
1109 struct gl_query_object Base
;
1110 struct timeval StartTime
;
1114 static struct gl_query_object
*
1115 xmesa_new_query_object(GLcontext
*ctx
, GLuint id
)
1117 struct xmesa_query_object
*q
= CALLOC_STRUCT(xmesa_query_object
);
1120 q
->Base
.Ready
= GL_TRUE
;
1127 xmesa_begin_query(GLcontext
*ctx
, GLenum target
, struct gl_query_object
*q
)
1129 if (target
== GL_TIME_ELAPSED_EXT
) {
1130 struct xmesa_query_object
*xq
= (struct xmesa_query_object
*) q
;
1131 (void) gettimeofday(&xq
->StartTime
, NULL
);
1137 * Return the difference between the two given times in microseconds.
1140 #define suseconds_t unsigned int
1143 time_diff(const struct timeval
*t0
, const struct timeval
*t1
)
1145 GLuint64EXT seconds0
= t0
->tv_sec
& 0xff; /* 0 .. 255 seconds */
1146 GLuint64EXT seconds1
= t1
->tv_sec
& 0xff; /* 0 .. 255 seconds */
1147 GLuint64EXT nanosec0
= (seconds0
* 1000000 + t0
->tv_usec
) * 1000;
1148 GLuint64EXT nanosec1
= (seconds1
* 1000000 + t1
->tv_usec
) * 1000;
1149 return nanosec1
- nanosec0
;
1154 xmesa_end_query(GLcontext
*ctx
, GLenum target
, struct gl_query_object
*q
)
1156 if (target
== GL_TIME_ELAPSED_EXT
) {
1157 struct xmesa_query_object
*xq
= (struct xmesa_query_object
*) q
;
1158 struct timeval endTime
;
1159 (void) gettimeofday(&endTime
, NULL
);
1160 /* result is in nanoseconds! */
1161 q
->Result
= time_diff(&xq
->StartTime
, &endTime
);
1166 #endif /* ENABLE_timer_query */
1170 * Initialize the device driver function table with the functions
1171 * we implement in this driver.
1174 xmesa_init_driver_functions( XMesaVisual xmvisual
,
1175 struct dd_function_table
*driver
)
1177 driver
->GetString
= get_string
;
1178 driver
->UpdateState
= xmesa_update_state
;
1179 driver
->GetBufferSize
= get_buffer_size
;
1180 driver
->Flush
= finish_or_flush
;
1181 driver
->Finish
= finish_or_flush
;
1182 driver
->ClearIndex
= clear_index
;
1183 driver
->ClearColor
= clear_color
;
1184 driver
->IndexMask
= index_mask
;
1185 driver
->ColorMask
= color_mask
;
1186 driver
->Enable
= enable
;
1187 driver
->Clear
= clear_buffers
;
1188 driver
->Viewport
= xmesa_viewport
;
1189 #ifndef XFree86Server
1190 driver
->CopyPixels
= xmesa_CopyPixels
;
1191 if (xmvisual
->undithered_pf
== PF_8R8G8B
&&
1192 xmvisual
->dithered_pf
== PF_8R8G8B
) {
1193 driver
->DrawPixels
= xmesa_DrawPixels_8R8G8B
;
1195 else if (xmvisual
->undithered_pf
== PF_5R6G5B
) {
1196 driver
->DrawPixels
= xmesa_DrawPixels_5R6G5B
;
1199 driver
->TestProxyTexImage
= test_proxy_teximage
;
1200 #if ENABLE_EXT_texure_compression_s3tc
1201 driver
->ChooseTextureFormat
= choose_tex_format
;
1203 (void) choose_tex_format
;
1206 #if ENABLE_EXT_timer_query
1207 driver
->NewQueryObject
= xmesa_new_query_object
;
1208 driver
->BeginQuery
= xmesa_begin_query
;
1209 driver
->EndQuery
= xmesa_end_query
;
1214 #define XMESA_NEW_POINT (_NEW_POINT | \
1216 _SWRAST_NEW_RASTERMASK)
1218 #define XMESA_NEW_LINE (_NEW_LINE | \
1223 _SWRAST_NEW_RASTERMASK)
1225 #define XMESA_NEW_TRIANGLE (_NEW_POLYGON | \
1230 _SWRAST_NEW_RASTERMASK)
1234 * Extend the software rasterizer with our line/point/triangle
1236 * Called during context creation only.
1238 void xmesa_register_swrast_functions( GLcontext
*ctx
)
1240 SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
1242 swrast
->choose_point
= xmesa_choose_point
;
1243 swrast
->choose_line
= xmesa_choose_line
;
1244 swrast
->choose_triangle
= xmesa_choose_triangle
;
1246 /* XXX these lines have no net effect. Remove??? */
1247 swrast
->InvalidatePointMask
|= XMESA_NEW_POINT
;
1248 swrast
->InvalidateLineMask
|= XMESA_NEW_LINE
;
1249 swrast
->InvalidateTriangleMask
|= XMESA_NEW_TRIANGLE
;