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 "main/bufferobj.h"
33 #include "main/buffers.h"
34 #include "main/context.h"
35 #include "main/colormac.h"
36 #include "main/depth.h"
37 #include "main/drawpix.h"
38 #include "main/extensions.h"
39 #include "main/framebuffer.h"
40 #include "main/macros.h"
41 #include "main/image.h"
42 #include "main/imports.h"
43 #include "main/mtypes.h"
44 #include "main/state.h"
45 #include "main/texobj.h"
46 #include "main/teximage.h"
47 #include "main/texstore.h"
48 #include "main/texformat.h"
49 #include "swrast/swrast.h"
50 #include "swrast/s_context.h"
51 #include "swrast_setup/swrast_setup.h"
53 #include "tnl/t_context.h"
56 #include "softpipe/sp_context.h"
57 #include "state_tracker/st_public.h"
58 #include "state_tracker/st_context.h"
59 #include "state_tracker/st_draw.h"
63 * Dithering kernels and lookup tables.
66 const int xmesa_kernel8
[DITH_DY
* DITH_DX
] = {
67 0 * MAXC
, 8 * MAXC
, 2 * MAXC
, 10 * MAXC
,
68 12 * MAXC
, 4 * MAXC
, 14 * MAXC
, 6 * MAXC
,
69 3 * MAXC
, 11 * MAXC
, 1 * MAXC
, 9 * MAXC
,
70 15 * MAXC
, 7 * MAXC
, 13 * MAXC
, 5 * MAXC
,
73 const short xmesa_HPCR_DRGB
[3][2][16] = {
75 { 16, -4, 1,-11, 14, -6, 3, -9, 15, -5, 2,-10, 13, -7, 4, -8},
76 {-15, 5, 0, 12,-13, 7, -2, 10,-14, 6, -1, 11,-12, 8, -3, 9}
79 {-11, 15, -7, 3, -8, 14, -4, 2,-10, 16, -6, 4, -9, 13, -5, 1},
80 { 12,-14, 8, -2, 9,-13, 5, -1, 11,-15, 7, -3, 10,-12, 6, 0}
83 { 6,-18, 26,-14, 2,-22, 30,-10, 8,-16, 28,-12, 4,-20, 32, -8},
84 { -4, 20,-24, 16, 0, 24,-28, 12, -6, 18,-26, 14, -2, 22,-30, 10}
88 const int xmesa_kernel1
[16] = {
89 0*47, 9*47, 4*47, 12*47, /* 47 = (255*3)/16 */
90 6*47, 2*47, 14*47, 8*47,
91 10*47, 1*47, 5*47, 11*47,
92 7*47, 13*47, 3*47, 15*47
97 finish_or_flush( GLcontext
*ctx
)
102 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
104 _glthread_LOCK_MUTEX(_xmesa_lock
);
105 XSync( xmesa
->display
, False
);
106 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
113 clear_index( GLcontext
*ctx
, GLuint index
)
115 if (ctx
->DrawBuffer
->Name
== 0) {
116 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
117 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
118 xmesa
->clearpixel
= (unsigned long) index
;
119 XMesaSetForeground( xmesa
->display
, xmbuf
->cleargc
, (unsigned long) index
);
125 clear_color( GLcontext
*ctx
, const GLfloat color
[4] )
127 if (ctx
->DrawBuffer
->Name
== 0) {
128 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
129 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
131 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[0], color
[0]);
132 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[1], color
[1]);
133 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[2], color
[2]);
134 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[3], color
[3]);
135 xmesa
->clearpixel
= xmesa_color_to_pixel( ctx
,
136 xmesa
->clearcolor
[0],
137 xmesa
->clearcolor
[1],
138 xmesa
->clearcolor
[2],
139 xmesa
->clearcolor
[3],
140 xmesa
->xm_visual
->undithered_pf
);
141 _glthread_LOCK_MUTEX(_xmesa_lock
);
142 XMesaSetForeground( xmesa
->display
, xmbuf
->cleargc
,
144 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
150 /* Set index mask ala glIndexMask */
152 index_mask( GLcontext
*ctx
, GLuint mask
)
154 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
155 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
156 /* not sure this conditional is really needed */
157 if (xmbuf
->backxrb
&& xmbuf
->backxrb
->pixmap
) {
159 if (mask
==0xffffffff) {
160 m
= ((unsigned long)~0L);
163 m
= (unsigned long) mask
;
165 XMesaSetPlaneMask( xmesa
->display
, xmbuf
->cleargc
, m
);
170 /* Implements glColorMask() */
172 color_mask(GLcontext
*ctx
,
173 GLboolean rmask
, GLboolean gmask
, GLboolean bmask
, GLboolean amask
)
175 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
177 const int xclass
= xmesa
->xm_visual
->mesa_visual
.visualType
;
180 if (ctx
->DrawBuffer
->Name
!= 0)
183 xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
185 if (xclass
== GLX_TRUE_COLOR
|| xclass
== GLX_DIRECT_COLOR
) {
187 if (rmask
&& gmask
&& bmask
) {
188 m
= ((unsigned long)~0L);
192 if (rmask
) m
|= GET_REDMASK(xmesa
->xm_visual
);
193 if (gmask
) m
|= GET_GREENMASK(xmesa
->xm_visual
);
194 if (bmask
) m
|= GET_BLUEMASK(xmesa
->xm_visual
);
196 XMesaSetPlaneMask( xmesa
->display
, xmbuf
->cleargc
, m
);
202 /**********************************************************************/
203 /*** glClear implementations ***/
204 /**********************************************************************/
208 * Clear the front or back color buffer, if it's implemented with a pixmap.
211 clear_pixmap(GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
212 GLint x
, GLint y
, GLint width
, GLint height
)
214 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
215 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
220 assert(xmesa
->display
);
222 assert(xmbuf
->cleargc
);
224 XMesaFillRectangle( xmesa
->display
, xrb
->pixmap
, xmbuf
->cleargc
,
225 x
, xrb
->St
.Base
.Height
- y
- height
,
231 clear_8bit_ximage( GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
232 GLint x
, GLint y
, GLint width
, GLint height
)
234 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
236 for (i
= 0; i
< height
; i
++) {
237 GLubyte
*ptr
= PIXEL_ADDR1(xrb
, x
, y
+ i
);
238 MEMSET( ptr
, xmesa
->clearpixel
, width
);
244 clear_HPCR_ximage( GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
245 GLint x
, GLint y
, GLint width
, GLint height
)
247 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
249 for (i
= y
; i
< y
+ height
; i
++) {
250 GLubyte
*ptr
= PIXEL_ADDR1( xrb
, x
, i
);
252 const GLubyte
*sptr
= xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0];
256 for (j
= x
; j
< x
+ width
; j
++) {
265 clear_16bit_ximage( GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
266 GLint x
, GLint y
, GLint width
, GLint height
)
268 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
269 GLuint pixel
= (GLuint
) xmesa
->clearpixel
;
272 if (xmesa
->swapbytes
) {
273 pixel
= ((pixel
>> 8) & 0x00ff) | ((pixel
<< 8) & 0xff00);
276 for (j
= 0; j
< height
; j
++) {
277 GLushort
*ptr2
= PIXEL_ADDR2(xrb
, x
, y
+ j
);
278 for (i
= 0; i
< width
; i
++) {
285 /* Optimized code provided by Nozomi Ytow <noz@xfree86.org> */
287 clear_24bit_ximage(GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
288 GLint x
, GLint y
, GLint width
, GLint height
)
290 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
291 const GLubyte r
= xmesa
->clearcolor
[0];
292 const GLubyte g
= xmesa
->clearcolor
[1];
293 const GLubyte b
= xmesa
->clearcolor
[2];
295 if (r
== g
&& g
== b
) {
296 /* same value for all three components (gray) */
298 for (j
= 0; j
< height
; j
++) {
299 bgr_t
*ptr3
= PIXEL_ADDR3(xrb
, x
, y
+ j
);
300 MEMSET(ptr3
, r
, 3 * width
);
304 /* non-gray clear color */
306 for (j
= 0; j
< height
; j
++) {
307 bgr_t
*ptr3
= PIXEL_ADDR3(xrb
, x
, y
+ j
);
308 for (i
= 0; i
< width
; i
++) {
320 clear_32bit_ximage(GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
321 GLint x
, GLint y
, GLint width
, GLint height
)
323 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
324 register GLuint pixel
= (GLuint
) xmesa
->clearpixel
;
329 if (xmesa
->swapbytes
) {
330 pixel
= ((pixel
>> 24) & 0x000000ff)
331 | ((pixel
>> 8) & 0x0000ff00)
332 | ((pixel
<< 8) & 0x00ff0000)
333 | ((pixel
<< 24) & 0xff000000);
336 if (width
== xrb
->St
.Base
.Width
&& height
== xrb
->St
.Base
.Height
) {
337 /* clearing whole buffer */
338 const GLuint n
= xrb
->St
.Base
.Width
* xrb
->St
.Base
.Height
;
339 GLuint
*ptr4
= (GLuint
*) xrb
->ximage
->data
;
342 _mesa_memset(ptr4
, pixel
, 4 * n
);
346 for (i
= 0; i
< n
; i
++)
351 /* clearing scissored region */
353 for (j
= 0; j
< height
; j
++) {
354 GLuint
*ptr4
= PIXEL_ADDR4(xrb
, x
, y
+ j
);
355 for (i
= 0; i
< width
; i
++) {
364 clear_nbit_ximage(GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
365 GLint x
, GLint y
, GLint width
, GLint height
)
367 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
368 XMesaImage
*img
= xrb
->ximage
;
371 /* TODO: optimize this */
373 for (j
= 0; j
< height
; j
++) {
374 for (i
= 0; i
< width
; i
++) {
375 XMesaPutPixel(img
, x
+i
, y
-j
, xmesa
->clearpixel
);
383 xmesa_clear_buffers(GLcontext
*ctx
, GLbitfield buffers
)
385 if (ctx
->DrawBuffer
->Name
== 0) {
386 /* this is a window system framebuffer */
387 const GLuint
*colorMask
= (GLuint
*) &ctx
->Color
.ColorMask
;
388 XMesaBuffer b
= XMESA_BUFFER(ctx
->DrawBuffer
);
389 const GLint x
= ctx
->DrawBuffer
->_Xmin
;
390 const GLint y
= ctx
->DrawBuffer
->_Ymin
;
391 const GLint width
= ctx
->DrawBuffer
->_Xmax
- x
;
392 const GLint height
= ctx
->DrawBuffer
->_Ymax
- y
;
394 /* we can't handle color or index masking */
395 if (*colorMask
== 0xffffffff && ctx
->Color
.IndexMask
== 0xffffffff) {
396 if (buffers
& BUFFER_BIT_FRONT_LEFT
) {
397 /* clear front color buffer */
398 struct gl_renderbuffer
*frontRb
399 = ctx
->DrawBuffer
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
;
400 if (b
->frontxrb
== xmesa_renderbuffer(frontRb
)) {
401 /* renderbuffer is not wrapped - great! */
402 b
->frontxrb
->clearFunc(ctx
, b
->frontxrb
, x
, y
, width
, height
);
403 buffers
&= ~BUFFER_BIT_FRONT_LEFT
;
406 /* we can't directly clear an alpha-wrapped color buffer */
409 if (buffers
& BUFFER_BIT_BACK_LEFT
) {
410 /* clear back color buffer */
411 struct gl_renderbuffer
*backRb
412 = ctx
->DrawBuffer
->Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
;
413 if (b
->backxrb
== xmesa_renderbuffer(backRb
)) {
414 /* renderbuffer is not wrapped - great! */
415 b
->backxrb
->clearFunc(ctx
, b
->backxrb
, x
, y
, width
, height
);
416 buffers
&= ~BUFFER_BIT_BACK_LEFT
;
422 _swrast_Clear(ctx
, buffers
);
426 #ifndef XFree86Server
427 /* XXX these functions haven't been tested in the Xserver environment */
431 * Check if we can do an optimized glDrawPixels into an 8R8G8B visual.
434 can_do_DrawPixels_8R8G8B(GLcontext
*ctx
, GLenum format
, GLenum type
)
436 if (format
== GL_BGRA
&&
437 type
== GL_UNSIGNED_BYTE
&&
439 ctx
->DrawBuffer
->Name
== 0 &&
440 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
441 ctx
->Pixel
.ZoomY
== 1.0 &&
442 ctx
->_ImageTransferState
== 0 /* no color tables, scale/bias, etc */) {
443 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
445 if (swrast
->NewState
)
446 _swrast_validate_derived( ctx
);
448 if ((swrast
->_RasterMask
& ~CLIP_BIT
) == 0) /* no blend, z-test, etc */ {
449 struct gl_renderbuffer
*rb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[0];
451 struct xmesa_renderbuffer
*xrb
= xmesa_renderbuffer(rb
->Wrapped
);
453 xrb
->pixmap
&& /* drawing to pixmap or window */
454 xrb
->Base
.AlphaBits
== 0) {
465 * This function implements glDrawPixels() with an XPutImage call when
466 * drawing to the front buffer (X Window drawable).
467 * The image format must be GL_BGRA to match the PF_8R8G8B pixel format.
470 xmesa_DrawPixels_8R8G8B( GLcontext
*ctx
,
471 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
472 GLenum format
, GLenum type
,
473 const struct gl_pixelstore_attrib
*unpack
,
474 const GLvoid
*pixels
)
476 if (can_do_DrawPixels_8R8G8B(ctx
, format
, type
)) {
477 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
478 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
484 if (swrast
->NewState
)
485 _swrast_validate_derived( ctx
);
487 if (unpack
->BufferObj
->Name
) {
488 /* unpack from PBO */
490 if (!_mesa_validate_pbo_access(2, unpack
, width
, height
, 1,
491 format
, type
, pixels
)) {
492 _mesa_error(ctx
, GL_INVALID_OPERATION
,
493 "glDrawPixels(invalid PBO access)");
496 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
,
497 GL_PIXEL_UNPACK_BUFFER_EXT
,
501 /* buffer is already mapped - that's an error */
502 _mesa_error(ctx
, GL_INVALID_OPERATION
,
503 "glDrawPixels(PBO is mapped)");
506 pixels
= ADD_POINTERS(buf
, pixels
);
509 if (_mesa_clip_drawpixels(ctx
, &dstX
, &dstY
, &w
, &h
, &clippedUnpack
)) {
510 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
511 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
512 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
513 const XMesaGC gc
= xmbuf
->cleargc
; /* effected by glColorMask */
514 struct xmesa_renderbuffer
*xrb
515 = xmesa_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]->Wrapped
);
516 const int srcX
= clippedUnpack
.SkipPixels
;
517 const int srcY
= clippedUnpack
.SkipRows
;
518 const int rowLength
= clippedUnpack
.RowLength
;
521 ASSERT(xmesa
->xm_visual
->dithered_pf
== PF_8R8G8B
);
522 ASSERT(xmesa
->xm_visual
->undithered_pf
== PF_8R8G8B
);
526 /* This is a little tricky since all coordinates up to now have
527 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
528 * so we have to carefully compute the Y coordinates/addresses here.
530 MEMSET(&ximage
, 0, sizeof(XMesaImage
));
531 ximage
.width
= width
;
532 ximage
.height
= height
;
533 ximage
.format
= ZPixmap
;
534 ximage
.data
= (char *) pixels
535 + ((srcY
+ h
- 1) * rowLength
+ srcX
) * 4;
536 ximage
.byte_order
= LSBFirst
;
537 ximage
.bitmap_unit
= 32;
538 ximage
.bitmap_bit_order
= LSBFirst
;
539 ximage
.bitmap_pad
= 32;
541 ximage
.bits_per_pixel
= 32;
542 ximage
.bytes_per_line
= -rowLength
* 4; /* negative to flip image */
543 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
544 /* flip Y axis for dest position */
545 dstY
= YFLIP(xrb
, dstY
) - h
+ 1;
546 XPutImage(dpy
, xrb
->pixmap
, gc
, &ximage
, 0, 0, dstX
, dstY
, w
, h
);
549 if (unpack
->BufferObj
->Name
) {
550 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
555 /* software fallback */
556 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
557 format
, type
, unpack
, pixels
);
564 * Check if we can do an optimized glDrawPixels into an 5R6G5B visual.
567 can_do_DrawPixels_5R6G5B(GLcontext
*ctx
, GLenum format
, GLenum type
)
569 if (format
== GL_RGB
&&
570 type
== GL_UNSIGNED_SHORT_5_6_5
&&
571 !ctx
->Color
.DitherFlag
&& /* no dithering */
573 ctx
->DrawBuffer
->Name
== 0 &&
574 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
575 ctx
->Pixel
.ZoomY
== 1.0 &&
576 ctx
->_ImageTransferState
== 0 /* no color tables, scale/bias, etc */) {
577 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
579 if (swrast
->NewState
)
580 _swrast_validate_derived( ctx
);
582 if ((swrast
->_RasterMask
& ~CLIP_BIT
) == 0) /* no blend, z-test, etc */ {
583 struct gl_renderbuffer
*rb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[0];
585 struct xmesa_renderbuffer
*xrb
= xmesa_renderbuffer(rb
->Wrapped
);
587 xrb
->pixmap
&& /* drawing to pixmap or window */
588 xrb
->Base
.AlphaBits
== 0) {
599 * This function implements glDrawPixels() with an XPutImage call when
600 * drawing to the front buffer (X Window drawable). The image format
601 * must be GL_RGB and image type must be GL_UNSIGNED_SHORT_5_6_5 to
602 * match the PF_5R6G5B pixel format.
605 xmesa_DrawPixels_5R6G5B( GLcontext
*ctx
,
606 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
607 GLenum format
, GLenum type
,
608 const struct gl_pixelstore_attrib
*unpack
,
609 const GLvoid
*pixels
)
611 if (can_do_DrawPixels_5R6G5B(ctx
, format
, type
)) {
612 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
613 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
619 if (swrast
->NewState
)
620 _swrast_validate_derived( ctx
);
622 if (unpack
->BufferObj
->Name
) {
623 /* unpack from PBO */
625 if (!_mesa_validate_pbo_access(2, unpack
, width
, height
, 1,
626 format
, type
, pixels
)) {
627 _mesa_error(ctx
, GL_INVALID_OPERATION
,
628 "glDrawPixels(invalid PBO access)");
631 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
,
632 GL_PIXEL_UNPACK_BUFFER_EXT
,
636 /* buffer is already mapped - that's an error */
637 _mesa_error(ctx
, GL_INVALID_OPERATION
,
638 "glDrawPixels(PBO is mapped)");
641 pixels
= ADD_POINTERS(buf
, pixels
);
644 if (_mesa_clip_drawpixels(ctx
, &dstX
, &dstY
, &w
, &h
, &clippedUnpack
)) {
645 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
646 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
647 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
648 const XMesaGC gc
= xmbuf
->cleargc
; /* effected by glColorMask */
649 struct xmesa_renderbuffer
*xrb
650 = xmesa_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]->Wrapped
);
651 const int srcX
= clippedUnpack
.SkipPixels
;
652 const int srcY
= clippedUnpack
.SkipRows
;
653 const int rowLength
= clippedUnpack
.RowLength
;
656 ASSERT(xmesa
->xm_visual
->undithered_pf
== PF_5R6G5B
);
660 /* This is a little tricky since all coordinates up to now have
661 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
662 * so we have to carefully compute the Y coordinates/addresses here.
664 MEMSET(&ximage
, 0, sizeof(XMesaImage
));
665 ximage
.width
= width
;
666 ximage
.height
= height
;
667 ximage
.format
= ZPixmap
;
668 ximage
.data
= (char *) pixels
669 + ((srcY
+ h
- 1) * rowLength
+ srcX
) * 2;
670 ximage
.byte_order
= LSBFirst
;
671 ximage
.bitmap_unit
= 16;
672 ximage
.bitmap_bit_order
= LSBFirst
;
673 ximage
.bitmap_pad
= 16;
675 ximage
.bits_per_pixel
= 16;
676 ximage
.bytes_per_line
= -rowLength
* 2; /* negative to flip image */
677 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
678 /* flip Y axis for dest position */
679 dstY
= YFLIP(xrb
, dstY
) - h
+ 1;
680 XPutImage(dpy
, xrb
->pixmap
, gc
, &ximage
, 0, 0, dstX
, dstY
, w
, h
);
683 if (unpack
->BufferObj
->Name
) {
684 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
689 /* software fallback */
690 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
691 format
, type
, unpack
, pixels
);
697 * Determine if we can do an optimized glCopyPixels.
700 can_do_CopyPixels(GLcontext
*ctx
, GLenum type
)
702 if (type
== GL_COLOR
&&
703 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
704 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
705 ctx
->Pixel
.ZoomY
== 1.0 &&
706 ctx
->Color
.DrawBuffer
[0] == GL_FRONT
&& /* copy to front buf */
707 ctx
->Pixel
.ReadBuffer
== GL_FRONT
&& /* copy from front buf */
708 ctx
->ReadBuffer
->_ColorReadBuffer
&&
709 ctx
->DrawBuffer
->_ColorDrawBuffers
[0]) {
710 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
712 if (swrast
->NewState
)
713 _swrast_validate_derived( ctx
);
715 if ((swrast
->_RasterMask
& ~CLIP_BIT
) == 0x0 &&
717 ctx
->ReadBuffer
->_ColorReadBuffer
&&
719 ctx
->DrawBuffer
->_ColorDrawBuffers
[0]) {
720 struct xmesa_renderbuffer
*srcXrb
721 = xmesa_renderbuffer(ctx
->ReadBuffer
->_ColorReadBuffer
->Wrapped
);
722 struct xmesa_renderbuffer
*dstXrb
723 = xmesa_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]->Wrapped
);
724 if (srcXrb
->pixmap
&& dstXrb
->pixmap
) {
734 * Implement glCopyPixels for the front color buffer (or back buffer Pixmap)
735 * for the color buffer. Don't support zooming, pixel transfer, etc.
736 * We do support copying from one window to another, ala glXMakeCurrentRead.
739 xmesa_CopyPixels( GLcontext
*ctx
,
740 GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
741 GLint destx
, GLint desty
, GLenum type
)
743 if (can_do_CopyPixels(ctx
, type
)) {
744 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
745 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
746 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
747 const XMesaGC gc
= xmbuf
->cleargc
; /* effected by glColorMask */
748 struct xmesa_renderbuffer
*srcXrb
749 = xmesa_renderbuffer(ctx
->ReadBuffer
->_ColorReadBuffer
->Wrapped
);
750 struct xmesa_renderbuffer
*dstXrb
751 = xmesa_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]->Wrapped
);
756 /* Note: we don't do any special clipping work here. We could,
757 * but X will do it for us.
759 srcy
= YFLIP(srcXrb
, srcy
) - height
+ 1;
760 desty
= YFLIP(dstXrb
, desty
) - height
+ 1;
761 XCopyArea(dpy
, srcXrb
->pixmap
, dstXrb
->pixmap
, gc
,
762 srcx
, srcy
, width
, height
, destx
, desty
);
765 _swrast_CopyPixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
, type
);
769 #endif /* XFree86Server */
774 * Every driver should implement a GetString function in order to
775 * return a meaningful GL_RENDERER string.
777 static const GLubyte
*
778 get_string( GLcontext
*ctx
, GLenum name
)
784 return (const GLubyte
*) "Mesa GLX Indirect";
786 return (const GLubyte
*) "Mesa X11 (softpipe)";
790 return (const GLubyte
*) "Mesa project: www.mesa3d.org";
801 * We implement the glEnable function only because we care about
802 * dither enable/disable.
805 enable( GLcontext
*ctx
, GLenum pname
, GLboolean state
)
807 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
812 xmesa
->pixelformat
= xmesa
->xm_visual
->dithered_pf
;
814 xmesa
->pixelformat
= xmesa
->xm_visual
->undithered_pf
;
817 ; /* silence compiler warning */
823 clear_color_HPCR_ximage( GLcontext
*ctx
, const GLfloat color
[4] )
826 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
828 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[0], color
[0]);
829 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[1], color
[1]);
830 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[2], color
[2]);
831 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[3], color
[3]);
833 if (color
[0] == 0.0 && color
[1] == 0.0 && color
[2] == 0.0) {
835 MEMSET( xmesa
->xm_visual
->hpcr_clear_ximage_pattern
, 0x0 ,
836 sizeof(xmesa
->xm_visual
->hpcr_clear_ximage_pattern
));
839 /* build clear pattern */
840 for (i
=0; i
<16; i
++) {
841 xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0][i
] =
843 xmesa
->clearcolor
[0],
844 xmesa
->clearcolor
[1],
845 xmesa
->clearcolor
[2]);
846 xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[1][i
] =
848 xmesa
->clearcolor
[0],
849 xmesa
->clearcolor
[1],
850 xmesa
->clearcolor
[2]);
857 clear_color_HPCR_pixmap( 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 for (i
=0; i
<16; i
++) {
870 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 0, 0);
871 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 1, 0);
875 for (i
=0; i
<16; i
++) {
876 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 0,
878 xmesa
->clearcolor
[0],
879 xmesa
->clearcolor
[1],
880 xmesa
->clearcolor
[2]));
881 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 1,
883 xmesa
->clearcolor
[0],
884 xmesa
->clearcolor
[1],
885 xmesa
->clearcolor
[2]));
888 /* change tile pixmap content */
889 XMesaPutImage(xmesa
->display
,
890 (XMesaDrawable
)xmesa
->xm_visual
->hpcr_clear_pixmap
,
891 XMESA_BUFFER(ctx
->DrawBuffer
)->cleargc
,
892 xmesa
->xm_visual
->hpcr_clear_ximage
, 0, 0, 0, 0, 16, 2);
897 * Called when the driver should update its state, based on the new_state
901 xmesa_update_state( GLcontext
*ctx
, GLbitfield new_state
)
903 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
905 /* Propagate statechange information to swrast and swrast_setup
906 * modules. The X11 driver has no internal GL-dependent state.
908 _swrast_InvalidateState( ctx
, new_state
);
909 _tnl_InvalidateState( ctx
, new_state
);
910 _vbo_InvalidateState( ctx
, new_state
);
911 _swsetup_InvalidateState( ctx
, new_state
);
913 st_invalidate_state( ctx
, new_state
);
916 if (ctx
->DrawBuffer
->Name
!= 0)
920 * GL_DITHER, GL_READ/DRAW_BUFFER, buffer binding state, etc. effect
921 * renderbuffer span/clear funcs.
923 if (new_state
& (_NEW_COLOR
| _NEW_BUFFERS
)) {
924 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
925 struct xmesa_renderbuffer
*front_xrb
, *back_xrb
;
927 front_xrb
= xmbuf
->frontxrb
;
929 xmesa_set_renderbuffer_funcs(front_xrb
, xmesa
->pixelformat
,
930 xmesa
->xm_visual
->BitsPerPixel
);
931 front_xrb
->clearFunc
= clear_pixmap
;
934 back_xrb
= xmbuf
->backxrb
;
936 xmesa_set_renderbuffer_funcs(back_xrb
, xmesa
->pixelformat
,
937 xmesa
->xm_visual
->BitsPerPixel
);
938 if (xmbuf
->backxrb
->pixmap
) {
939 back_xrb
->clearFunc
= clear_pixmap
;
942 switch (xmesa
->xm_visual
->BitsPerPixel
) {
944 if (xmesa
->xm_visual
->hpcr_clear_flag
) {
945 back_xrb
->clearFunc
= clear_HPCR_ximage
;
948 back_xrb
->clearFunc
= clear_8bit_ximage
;
952 back_xrb
->clearFunc
= clear_16bit_ximage
;
955 back_xrb
->clearFunc
= clear_24bit_ximage
;
958 back_xrb
->clearFunc
= clear_32bit_ximage
;
961 back_xrb
->clearFunc
= clear_nbit_ximage
;
968 if (xmesa
->xm_visual
->hpcr_clear_flag
) {
969 /* this depends on whether we're drawing to the front or back buffer */
973 ctx
->Driver
.ClearColor
= clear_color_HPCR_pixmap
;
976 ctx
->Driver
.ClearColor
= clear_color_HPCR_ximage
;
979 (void) clear_color_HPCR_pixmap
;
980 (void) clear_color_HPCR_ximage
;
988 * Called via ctx->Driver.TestProxyTeximage(). Normally, we'd just use
989 * the _mesa_test_proxy_teximage() fallback function, but we're going to
990 * special-case the 3D texture case to allow textures up to 512x512x32
994 test_proxy_teximage(GLcontext
*ctx
, GLenum target
, GLint level
,
995 GLint internalFormat
, GLenum format
, GLenum type
,
996 GLint width
, GLint height
, GLint depth
, GLint border
)
998 if (target
== GL_PROXY_TEXTURE_3D
) {
999 /* special case for 3D textures */
1000 if (width
* height
* depth
> 512 * 512 * 64 ||
1001 width
< 2 * border
||
1002 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1003 _mesa_bitcount(width
- 2 * border
) != 1) ||
1004 height
< 2 * border
||
1005 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1006 _mesa_bitcount(height
- 2 * border
) != 1) ||
1007 depth
< 2 * border
||
1008 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1009 _mesa_bitcount(depth
- 2 * border
) != 1)) {
1010 /* Bad size, or too many texels */
1016 /* use the fallback routine for 1D, 2D, cube and rect targets */
1017 return _mesa_test_proxy_teximage(ctx
, target
, level
, internalFormat
,
1018 format
, type
, width
, height
, depth
,
1025 * In SW, we don't really compress GL_COMPRESSED_RGB[A] textures!
1027 static const struct gl_texture_format
*
1028 choose_tex_format( GLcontext
*ctx
, GLint internalFormat
,
1029 GLenum format
, GLenum type
)
1031 switch (internalFormat
) {
1032 case GL_COMPRESSED_RGB_ARB
:
1033 return &_mesa_texformat_rgb
;
1034 case GL_COMPRESSED_RGBA_ARB
:
1035 return &_mesa_texformat_rgba
;
1037 return _mesa_choose_tex_format(ctx
, internalFormat
, format
, type
);
1043 * Called by glViewport.
1044 * This is a good time for us to poll the current X window size and adjust
1045 * our renderbuffers to match the current window size.
1046 * Remember, we have no opportunity to respond to conventional
1047 * X Resize/StructureNotify events since the X driver has no event loop.
1049 * Note that this trick isn't fool-proof. If the application never calls
1050 * glViewport, our notion of the current window size may be incorrect.
1051 * That problem led to the GLX_MESA_resize_buffers extension.
1054 xmesa_viewport(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei w
, GLsizei h
)
1056 XMesaContext xmctx
= XMESA_CONTEXT(ctx
);
1057 XMesaBuffer xmdrawbuf
= XMESA_BUFFER(ctx
->WinSysDrawBuffer
);
1058 XMesaBuffer xmreadbuf
= XMESA_BUFFER(ctx
->WinSysReadBuffer
);
1059 xmesa_check_and_update_buffer_size(xmctx
, xmdrawbuf
);
1060 xmesa_check_and_update_buffer_size(xmctx
, xmreadbuf
);
1068 #if ENABLE_EXT_timer_query
1071 * The GL_EXT_timer_query extension is not enabled for the XServer
1072 * indirect renderer. Not sure about how/if wrapping of gettimeofday()
1076 struct xmesa_query_object
1078 struct gl_query_object Base
;
1079 struct timeval StartTime
;
1083 static struct gl_query_object
*
1084 xmesa_new_query_object(GLcontext
*ctx
, GLuint id
)
1086 struct xmesa_query_object
*q
= CALLOC_STRUCT(xmesa_query_object
);
1089 q
->Base
.Ready
= GL_TRUE
;
1096 xmesa_begin_query(GLcontext
*ctx
, struct gl_query_object
*q
)
1098 if (q
->Target
== GL_TIME_ELAPSED_EXT
) {
1099 struct xmesa_query_object
*xq
= (struct xmesa_query_object
*) q
;
1100 (void) gettimeofday(&xq
->StartTime
, NULL
);
1106 * Return the difference between the two given times in microseconds.
1109 #define suseconds_t unsigned int
1112 time_diff(const struct timeval
*t0
, const struct timeval
*t1
)
1114 GLuint64EXT seconds0
= t0
->tv_sec
& 0xff; /* 0 .. 255 seconds */
1115 GLuint64EXT seconds1
= t1
->tv_sec
& 0xff; /* 0 .. 255 seconds */
1116 GLuint64EXT nanosec0
= (seconds0
* 1000000 + t0
->tv_usec
) * 1000;
1117 GLuint64EXT nanosec1
= (seconds1
* 1000000 + t1
->tv_usec
) * 1000;
1118 return nanosec1
- nanosec0
;
1123 xmesa_end_query(GLcontext
*ctx
, struct gl_query_object
*q
)
1125 if (q
->Target
== GL_TIME_ELAPSED_EXT
) {
1126 struct xmesa_query_object
*xq
= (struct xmesa_query_object
*) q
;
1127 struct timeval endTime
;
1128 (void) gettimeofday(&endTime
, NULL
);
1129 /* result is in nanoseconds! */
1130 q
->Result
= time_diff(&xq
->StartTime
, &endTime
);
1135 #endif /* ENABLE_timer_query */
1139 * Initialize the device driver function table with the functions
1140 * we implement in this driver.
1143 xmesa_init_driver_functions( XMesaVisual xmvisual
,
1144 struct dd_function_table
*driver
)
1146 driver
->GetString
= get_string
;
1147 driver
->UpdateState
= xmesa_update_state
;
1148 driver
->GetBufferSize
= NULL
; /* OBSOLETE */
1149 driver
->Flush
= finish_or_flush
;
1150 driver
->Finish
= finish_or_flush
;
1151 driver
->ClearIndex
= clear_index
;
1152 driver
->ClearColor
= clear_color
;
1153 driver
->IndexMask
= index_mask
;
1154 driver
->ColorMask
= color_mask
;
1155 driver
->Enable
= enable
;
1156 driver
->Clear
= xmesa_clear_buffers
;
1157 driver
->Viewport
= xmesa_viewport
;
1158 #ifndef XFree86Server
1159 driver
->CopyPixels
= xmesa_CopyPixels
;
1160 if (xmvisual
->undithered_pf
== PF_8R8G8B
&&
1161 xmvisual
->dithered_pf
== PF_8R8G8B
&&
1162 xmvisual
->BitsPerPixel
== 32) {
1163 driver
->DrawPixels
= xmesa_DrawPixels_8R8G8B
;
1165 else if (xmvisual
->undithered_pf
== PF_5R6G5B
) {
1166 driver
->DrawPixels
= xmesa_DrawPixels_5R6G5B
;
1169 driver
->TestProxyTexImage
= test_proxy_teximage
;
1170 #if ENABLE_EXT_texure_compression_s3tc
1171 driver
->ChooseTextureFormat
= choose_tex_format
;
1173 (void) choose_tex_format
;
1176 #if ENABLE_EXT_timer_query
1177 driver
->NewQueryObject
= xmesa_new_query_object
;
1178 driver
->BeginQuery
= xmesa_begin_query
;
1179 driver
->EndQuery
= xmesa_end_query
;
1185 #define XMESA_NEW_POINT (_NEW_POINT | \
1187 _SWRAST_NEW_RASTERMASK)
1189 #define XMESA_NEW_LINE (_NEW_LINE | \
1194 _SWRAST_NEW_RASTERMASK)
1196 #define XMESA_NEW_TRIANGLE (_NEW_POLYGON | \
1201 _SWRAST_NEW_RASTERMASK)
1205 * Extend the software rasterizer with our line/point/triangle
1207 * Called during context creation only.
1209 void xmesa_register_swrast_functions( GLcontext
*ctx
)
1211 SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
1213 swrast
->choose_point
= xmesa_choose_point
;
1214 swrast
->choose_line
= xmesa_choose_line
;
1215 swrast
->choose_triangle
= xmesa_choose_triangle
;
1217 /* XXX these lines have no net effect. Remove??? */
1218 swrast
->InvalidatePointMask
|= XMESA_NEW_POINT
;
1219 swrast
->InvalidateLineMask
|= XMESA_NEW_LINE
;
1220 swrast
->InvalidateTriangleMask
|= XMESA_NEW_TRIANGLE
;