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 "swrast/swrast.h"
51 #include "swrast/s_context.h"
52 #include "swrast_setup/swrast_setup.h"
54 #include "tnl/t_context.h"
59 * Dithering kernels and lookup tables.
62 const int xmesa_kernel8
[DITH_DY
* DITH_DX
] = {
63 0 * MAXC
, 8 * MAXC
, 2 * MAXC
, 10 * MAXC
,
64 12 * MAXC
, 4 * MAXC
, 14 * MAXC
, 6 * MAXC
,
65 3 * MAXC
, 11 * MAXC
, 1 * MAXC
, 9 * MAXC
,
66 15 * MAXC
, 7 * MAXC
, 13 * MAXC
, 5 * MAXC
,
69 const short xmesa_HPCR_DRGB
[3][2][16] = {
71 { 16, -4, 1,-11, 14, -6, 3, -9, 15, -5, 2,-10, 13, -7, 4, -8},
72 {-15, 5, 0, 12,-13, 7, -2, 10,-14, 6, -1, 11,-12, 8, -3, 9}
75 {-11, 15, -7, 3, -8, 14, -4, 2,-10, 16, -6, 4, -9, 13, -5, 1},
76 { 12,-14, 8, -2, 9,-13, 5, -1, 11,-15, 7, -3, 10,-12, 6, 0}
79 { 6,-18, 26,-14, 2,-22, 30,-10, 8,-16, 28,-12, 4,-20, 32, -8},
80 { -4, 20,-24, 16, 0, 24,-28, 12, -6, 18,-26, 14, -2, 22,-30, 10}
84 const int xmesa_kernel1
[16] = {
85 0*47, 9*47, 4*47, 12*47, /* 47 = (255*3)/16 */
86 6*47, 2*47, 14*47, 8*47,
87 10*47, 1*47, 5*47, 11*47,
88 7*47, 13*47, 3*47, 15*47
93 finish_or_flush( GLcontext
*ctx
)
98 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
100 _glthread_LOCK_MUTEX(_xmesa_lock
);
101 XSync( xmesa
->display
, False
);
102 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
109 clear_index( GLcontext
*ctx
, GLuint index
)
111 if (ctx
->DrawBuffer
->Name
== 0) {
112 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
113 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
114 xmesa
->clearpixel
= (unsigned long) index
;
115 XMesaSetForeground( xmesa
->display
, xmbuf
->cleargc
, (unsigned long) index
);
121 clear_color( GLcontext
*ctx
, const GLfloat color
[4] )
123 if (ctx
->DrawBuffer
->Name
== 0) {
124 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
125 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
127 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[0], color
[0]);
128 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[1], color
[1]);
129 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[2], color
[2]);
130 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[3], color
[3]);
131 xmesa
->clearpixel
= xmesa_color_to_pixel( ctx
,
132 xmesa
->clearcolor
[0],
133 xmesa
->clearcolor
[1],
134 xmesa
->clearcolor
[2],
135 xmesa
->clearcolor
[3],
136 xmesa
->xm_visual
->undithered_pf
);
137 _glthread_LOCK_MUTEX(_xmesa_lock
);
138 XMesaSetForeground( xmesa
->display
, xmbuf
->cleargc
,
140 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
146 /* Set index mask ala glIndexMask */
148 index_mask( GLcontext
*ctx
, GLuint mask
)
150 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
151 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
152 /* not sure this conditional is really needed */
153 if (xmbuf
->backxrb
&& xmbuf
->backxrb
->pixmap
) {
155 if (mask
==0xffffffff) {
156 m
= ((unsigned long)~0L);
159 m
= (unsigned long) mask
;
161 XMesaSetPlaneMask( xmesa
->display
, xmbuf
->cleargc
, m
);
162 XMesaSetPlaneMask( xmesa
->display
, xmbuf
->gc
, m
);
167 /* Implements glColorMask() */
169 color_mask(GLcontext
*ctx
,
170 GLboolean rmask
, GLboolean gmask
, GLboolean bmask
, GLboolean amask
)
172 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
174 const int xclass
= xmesa
->xm_visual
->mesa_visual
.visualType
;
177 if (ctx
->DrawBuffer
->Name
!= 0)
180 xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
182 if (xclass
== GLX_TRUE_COLOR
|| xclass
== GLX_DIRECT_COLOR
) {
184 if (rmask
&& gmask
&& bmask
) {
185 m
= ((unsigned long)~0L);
189 if (rmask
) m
|= GET_REDMASK(xmesa
->xm_visual
);
190 if (gmask
) m
|= GET_GREENMASK(xmesa
->xm_visual
);
191 if (bmask
) m
|= GET_BLUEMASK(xmesa
->xm_visual
);
193 XMesaSetPlaneMask( xmesa
->display
, xmbuf
->cleargc
, m
);
194 XMesaSetPlaneMask( xmesa
->display
, xmbuf
->gc
, m
);
200 /**********************************************************************/
201 /*** glClear implementations ***/
202 /**********************************************************************/
206 * Clear the front or back color buffer, if it's implemented with a pixmap.
209 clear_pixmap(GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
210 GLint x
, GLint y
, GLint width
, GLint height
)
212 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
213 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
218 assert(xmesa
->display
);
220 assert(xmbuf
->cleargc
);
222 XMesaFillRectangle( xmesa
->display
, xrb
->pixmap
, xmbuf
->cleargc
,
223 x
, xrb
->Base
.Height
- y
- height
,
229 clear_8bit_ximage( GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
230 GLint x
, GLint y
, GLint width
, GLint height
)
232 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
234 for (i
= 0; i
< height
; i
++) {
235 GLubyte
*ptr
= PIXEL_ADDR1(xrb
, x
, y
+ i
);
236 MEMSET( ptr
, xmesa
->clearpixel
, width
);
242 clear_HPCR_ximage( GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
243 GLint x
, GLint y
, GLint width
, GLint height
)
245 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
247 for (i
= y
; i
< y
+ height
; i
++) {
248 GLubyte
*ptr
= PIXEL_ADDR1( xrb
, x
, i
);
250 const GLubyte
*sptr
= xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0];
254 for (j
= x
; j
< x
+ width
; j
++) {
263 clear_16bit_ximage( GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
264 GLint x
, GLint y
, GLint width
, GLint height
)
266 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
267 GLuint pixel
= (GLuint
) xmesa
->clearpixel
;
270 if (xmesa
->swapbytes
) {
271 pixel
= ((pixel
>> 8) & 0x00ff) | ((pixel
<< 8) & 0xff00);
274 for (j
= 0; j
< height
; j
++) {
275 GLushort
*ptr2
= PIXEL_ADDR2(xrb
, x
, y
+ j
);
276 for (i
= 0; i
< width
; i
++) {
283 /* Optimized code provided by Nozomi Ytow <noz@xfree86.org> */
285 clear_24bit_ximage(GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
286 GLint x
, GLint y
, GLint width
, GLint height
)
288 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
289 const GLubyte r
= xmesa
->clearcolor
[0];
290 const GLubyte g
= xmesa
->clearcolor
[1];
291 const GLubyte b
= xmesa
->clearcolor
[2];
293 if (r
== g
&& g
== b
) {
294 /* same value for all three components (gray) */
296 for (j
= 0; j
< height
; j
++) {
297 bgr_t
*ptr3
= PIXEL_ADDR3(xrb
, x
, y
+ j
);
298 MEMSET(ptr3
, r
, 3 * width
);
302 /* non-gray clear color */
304 for (j
= 0; j
< height
; j
++) {
305 bgr_t
*ptr3
= PIXEL_ADDR3(xrb
, x
, y
+ j
);
306 for (i
= 0; i
< width
; i
++) {
318 clear_32bit_ximage(GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
319 GLint x
, GLint y
, GLint width
, GLint height
)
321 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
322 register GLuint pixel
= (GLuint
) xmesa
->clearpixel
;
327 if (xmesa
->swapbytes
) {
328 pixel
= ((pixel
>> 24) & 0x000000ff)
329 | ((pixel
>> 8) & 0x0000ff00)
330 | ((pixel
<< 8) & 0x00ff0000)
331 | ((pixel
<< 24) & 0xff000000);
334 if (width
== xrb
->Base
.Width
&& height
== xrb
->Base
.Height
) {
335 /* clearing whole buffer */
336 const GLuint n
= xrb
->Base
.Width
* xrb
->Base
.Height
;
337 GLuint
*ptr4
= (GLuint
*) xrb
->ximage
->data
;
340 _mesa_memset(ptr4
, pixel
, 4 * n
);
344 for (i
= 0; i
< n
; i
++)
349 /* clearing scissored region */
351 for (j
= 0; j
< height
; j
++) {
352 GLuint
*ptr4
= PIXEL_ADDR4(xrb
, x
, y
+ j
);
353 for (i
= 0; i
< width
; i
++) {
362 clear_nbit_ximage(GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
363 GLint x
, GLint y
, GLint width
, GLint height
)
365 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
366 XMesaImage
*img
= xrb
->ximage
;
369 /* TODO: optimize this */
371 for (j
= 0; j
< height
; j
++) {
372 for (i
= 0; i
< width
; i
++) {
373 XMesaPutPixel(img
, x
+i
, y
-j
, xmesa
->clearpixel
);
381 clear_buffers(GLcontext
*ctx
, GLbitfield buffers
)
383 if (ctx
->DrawBuffer
->Name
== 0) {
384 /* this is a window system framebuffer */
385 const GLuint
*colorMask
= (GLuint
*) &ctx
->Color
.ColorMask
;
386 XMesaBuffer b
= XMESA_BUFFER(ctx
->DrawBuffer
);
387 const GLint x
= ctx
->DrawBuffer
->_Xmin
;
388 const GLint y
= ctx
->DrawBuffer
->_Ymin
;
389 const GLint width
= ctx
->DrawBuffer
->_Xmax
- x
;
390 const GLint height
= ctx
->DrawBuffer
->_Ymax
- y
;
392 /* we can't handle color or index masking */
393 if (*colorMask
== 0xffffffff && ctx
->Color
.IndexMask
== 0xffffffff) {
394 if (buffers
& BUFFER_BIT_FRONT_LEFT
) {
395 /* clear front color buffer */
396 struct gl_renderbuffer
*frontRb
397 = ctx
->DrawBuffer
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
;
398 if (b
->frontxrb
== xmesa_renderbuffer(frontRb
)) {
399 /* renderbuffer is not wrapped - great! */
400 b
->frontxrb
->clearFunc(ctx
, b
->frontxrb
, x
, y
, width
, height
);
401 buffers
&= ~BUFFER_BIT_FRONT_LEFT
;
404 /* we can't directly clear an alpha-wrapped color buffer */
407 if (buffers
& BUFFER_BIT_BACK_LEFT
) {
408 /* clear back color buffer */
409 struct gl_renderbuffer
*backRb
410 = ctx
->DrawBuffer
->Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
;
411 if (b
->backxrb
== xmesa_renderbuffer(backRb
)) {
412 /* renderbuffer is not wrapped - great! */
413 b
->backxrb
->clearFunc(ctx
, b
->backxrb
, x
, y
, width
, height
);
414 buffers
&= ~BUFFER_BIT_BACK_LEFT
;
420 _swrast_Clear(ctx
, buffers
);
424 #ifndef XFree86Server
425 /* XXX this was never tested in the Xserver environment */
428 * This function implements glDrawPixels() with an XPutImage call when
429 * drawing to the front buffer (X Window drawable).
430 * The image format must be GL_BGRA to match the PF_8R8G8B pixel format.
433 xmesa_DrawPixels_8R8G8B( GLcontext
*ctx
,
434 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
435 GLenum format
, GLenum type
,
436 const struct gl_pixelstore_attrib
*unpack
,
437 const GLvoid
*pixels
)
439 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
440 struct gl_renderbuffer
*rb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[0][0];
441 struct xmesa_renderbuffer
*xrb
= xmesa_renderbuffer(rb
->Wrapped
);
443 if (swrast
->NewState
)
444 _swrast_validate_derived( ctx
);
446 if (ctx
->DrawBuffer
->Name
== 0 &&
448 type
== GL_UNSIGNED_BYTE
&&
449 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
450 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
451 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
452 ctx
->Pixel
.ZoomY
== 1.0 &&
454 xrb
->Base
.AlphaBits
== 0)
456 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
457 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
458 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
459 const XMesaGC gc
= xmbuf
->gc
;
464 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
466 ASSERT(xmesa
->xm_visual
->dithered_pf
== PF_8R8G8B
);
467 ASSERT(xmesa
->xm_visual
->undithered_pf
== PF_8R8G8B
);
471 if (unpack
->BufferObj
->Name
) {
472 /* unpack from PBO */
474 if (!_mesa_validate_pbo_access(2, unpack
, width
, height
, 1,
475 format
, type
, pixels
)) {
476 _mesa_error(ctx
, GL_INVALID_OPERATION
,
477 "glDrawPixels(invalid PBO access)");
480 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
,
481 GL_PIXEL_UNPACK_BUFFER_EXT
,
485 /* buffer is already mapped - that's an error */
486 _mesa_error(ctx
, GL_INVALID_OPERATION
,
487 "glDrawPixels(PBO is mapped)");
490 pixels
= ADD_POINTERS(buf
, pixels
);
493 if (_mesa_clip_drawpixels(ctx
, &dstX
, &dstY
, &w
, &h
, &clippedUnpack
)) {
494 /* This is a little tricky since all coordinates up to now have
495 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
496 * so we have to carefully compute the Y coordinates/addresses here.
498 int srcX
= clippedUnpack
.SkipPixels
;
499 int srcY
= clippedUnpack
.SkipRows
;
500 int rowLength
= clippedUnpack
.RowLength
;
502 MEMSET(&ximage
, 0, sizeof(XMesaImage
));
503 ximage
.width
= width
;
504 ximage
.height
= height
;
505 ximage
.format
= ZPixmap
;
506 ximage
.data
= (char *) pixels
507 + ((srcY
+ h
- 1) * rowLength
+ srcX
) * 4;
508 ximage
.byte_order
= LSBFirst
;
509 ximage
.bitmap_unit
= 32;
510 ximage
.bitmap_bit_order
= LSBFirst
;
511 ximage
.bitmap_pad
= 32;
513 ximage
.bytes_per_line
= -rowLength
* 4; /* negative to flip image */
514 ximage
.bits_per_pixel
= 32;
515 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
516 /* flip Y axis for dest position */
517 dstY
= YFLIP(xrb
, dstY
) - h
+ 1;
518 XPutImage(dpy
, xrb
->pixmap
, gc
, &ximage
, 0, 0, dstX
, dstY
, w
, h
);
521 if (unpack
->BufferObj
->Name
) {
522 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
527 /* software fallback */
528 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
529 format
, type
, unpack
, pixels
);
536 * This function implements glDrawPixels() with an XPutImage call when
537 * drawing to the front buffer (X Window drawable). The image format
538 * must be GL_RGB and image type must be GL_UNSIGNED_SHORT_5_6_5 to
539 * match the PF_5R6G5B pixel format.
542 xmesa_DrawPixels_5R6G5B( GLcontext
*ctx
,
543 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
544 GLenum format
, GLenum type
,
545 const struct gl_pixelstore_attrib
*unpack
,
546 const GLvoid
*pixels
)
548 struct xmesa_renderbuffer
*xrb
549 = xmesa_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0][0]->Wrapped
);
550 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
551 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
552 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
553 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
554 const XMesaGC gc
= xmbuf
->gc
;
558 ASSERT(xmesa
->xm_visual
->undithered_pf
== PF_5R6G5B
);
560 if (swrast
->NewState
)
561 _swrast_validate_derived( ctx
);
565 type
== GL_UNSIGNED_SHORT_5_6_5
&&
566 !ctx
->Color
.DitherFlag
&& /* no dithering */
567 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
568 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
569 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
570 ctx
->Pixel
.ZoomY
== 1.0) {
575 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
577 if (unpack
->BufferObj
->Name
) {
578 /* unpack from PBO */
580 if (!_mesa_validate_pbo_access(2, unpack
, width
, height
, 1,
581 format
, type
, pixels
)) {
582 _mesa_error(ctx
, GL_INVALID_OPERATION
,
583 "glDrawPixels(invalid PBO access)");
586 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
,
587 GL_PIXEL_UNPACK_BUFFER_EXT
,
591 /* buffer is already mapped - that's an error */
592 _mesa_error(ctx
, GL_INVALID_OPERATION
,
593 "glDrawPixels(PBO is mapped)");
596 pixels
= ADD_POINTERS(buf
, pixels
);
599 if (_mesa_clip_drawpixels(ctx
, &dstX
, &dstY
, &w
, &h
, &clippedUnpack
)) {
600 /* This is a little tricky since all coordinates up to now have
601 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
602 * so we have to carefully compute the Y coordinates/addresses here.
604 int srcX
= clippedUnpack
.SkipPixels
;
605 int srcY
= clippedUnpack
.SkipRows
;
606 int rowLength
= clippedUnpack
.RowLength
;
608 MEMSET(&ximage
, 0, sizeof(XMesaImage
));
609 ximage
.width
= width
;
610 ximage
.height
= height
;
611 ximage
.format
= ZPixmap
;
612 ximage
.data
= (char *) pixels
613 + ((srcY
+ h
- 1) * rowLength
+ srcX
) * 2;
614 ximage
.byte_order
= LSBFirst
;
615 ximage
.bitmap_unit
= 16;
616 ximage
.bitmap_bit_order
= LSBFirst
;
617 ximage
.bitmap_pad
= 16;
619 ximage
.bytes_per_line
= -rowLength
* 2; /* negative to flip image */
620 ximage
.bits_per_pixel
= 16;
621 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
622 /* flip Y axis for dest position */
623 dstY
= YFLIP(xrb
, dstY
) - h
+ 1;
624 XPutImage(dpy
, xrb
->pixmap
, gc
, &ximage
, 0, 0, dstX
, dstY
, w
, h
);
627 if (unpack
->BufferObj
->Name
) {
628 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
633 /* software fallback */
634 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
635 format
, type
, unpack
, pixels
);
642 * Implement glCopyPixels for the front color buffer (or back buffer Pixmap)
643 * for the color buffer. Don't support zooming, pixel transfer, etc.
644 * We do support copying from one window to another, ala glXMakeCurrentRead.
647 xmesa_CopyPixels( GLcontext
*ctx
,
648 GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
649 GLint destx
, GLint desty
, GLenum type
)
651 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
652 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
653 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
654 const XMesaGC gc
= ((XMesaBuffer
) ctx
->DrawBuffer
)->gc
;
655 struct xmesa_renderbuffer
*srcXrb
656 = xmesa_renderbuffer(ctx
->ReadBuffer
->_ColorReadBuffer
->Wrapped
);
657 struct xmesa_renderbuffer
*dstXrb
658 = xmesa_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0][0]->Wrapped
);
663 if (swrast
->NewState
)
664 _swrast_validate_derived( ctx
);
666 if (ctx
->Color
.DrawBuffer
[0] == GL_FRONT
&&
667 ctx
->Pixel
.ReadBuffer
== GL_FRONT
&&
671 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
672 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
673 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
674 ctx
->Pixel
.ZoomY
== 1.0) {
675 /* Note: we don't do any special clipping work here. We could,
676 * but X will do it for us.
678 srcy
= YFLIP(srcXrb
, srcy
) - height
+ 1;
679 desty
= YFLIP(dstXrb
, desty
) - height
+ 1;
680 XCopyArea(dpy
, srcXrb
->pixmap
, dstXrb
->pixmap
, gc
,
681 srcx
, srcy
, width
, height
, destx
, desty
);
684 _swrast_CopyPixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
, type
);
687 #endif /* XFree86Server */
692 * Every driver should implement a GetString function in order to
693 * return a meaningful GL_RENDERER string.
695 static const GLubyte
*
696 get_string( GLcontext
*ctx
, GLenum name
)
702 return (const GLubyte
*) "Mesa GLX Indirect";
704 return (const GLubyte
*) "Mesa X11";
708 return (const GLubyte
*) "Mesa project: www.mesa3d.org";
719 * We implement the glEnable function only because we care about
720 * dither enable/disable.
723 enable( GLcontext
*ctx
, GLenum pname
, GLboolean state
)
725 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
730 xmesa
->pixelformat
= xmesa
->xm_visual
->dithered_pf
;
732 xmesa
->pixelformat
= xmesa
->xm_visual
->undithered_pf
;
735 ; /* silence compiler warning */
741 clear_color_HPCR_ximage( GLcontext
*ctx
, const GLfloat color
[4] )
744 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
746 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[0], color
[0]);
747 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[1], color
[1]);
748 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[2], color
[2]);
749 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[3], color
[3]);
751 if (color
[0] == 0.0 && color
[1] == 0.0 && color
[2] == 0.0) {
753 MEMSET( xmesa
->xm_visual
->hpcr_clear_ximage_pattern
, 0x0 ,
754 sizeof(xmesa
->xm_visual
->hpcr_clear_ximage_pattern
));
757 /* build clear pattern */
758 for (i
=0; i
<16; i
++) {
759 xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0][i
] =
761 xmesa
->clearcolor
[0],
762 xmesa
->clearcolor
[1],
763 xmesa
->clearcolor
[2]);
764 xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[1][i
] =
766 xmesa
->clearcolor
[0],
767 xmesa
->clearcolor
[1],
768 xmesa
->clearcolor
[2]);
775 clear_color_HPCR_pixmap( GLcontext
*ctx
, const GLfloat color
[4] )
778 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
780 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[0], color
[0]);
781 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[1], color
[1]);
782 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[2], color
[2]);
783 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[3], color
[3]);
785 if (color
[0] == 0.0 && color
[1] == 0.0 && color
[2] == 0.0) {
787 for (i
=0; i
<16; i
++) {
788 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 0, 0);
789 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 1, 0);
793 for (i
=0; i
<16; i
++) {
794 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 0,
796 xmesa
->clearcolor
[0],
797 xmesa
->clearcolor
[1],
798 xmesa
->clearcolor
[2]));
799 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 1,
801 xmesa
->clearcolor
[0],
802 xmesa
->clearcolor
[1],
803 xmesa
->clearcolor
[2]));
806 /* change tile pixmap content */
807 XMesaPutImage(xmesa
->display
,
808 (XMesaDrawable
)xmesa
->xm_visual
->hpcr_clear_pixmap
,
809 XMESA_BUFFER(ctx
->DrawBuffer
)->cleargc
,
810 xmesa
->xm_visual
->hpcr_clear_ximage
, 0, 0, 0, 0, 16, 2);
815 * Called when the driver should update its state, based on the new_state
819 xmesa_update_state( GLcontext
*ctx
, GLbitfield new_state
)
821 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
823 /* Propagate statechange information to swrast and swrast_setup
824 * modules. The X11 driver has no internal GL-dependent state.
826 _swrast_InvalidateState( ctx
, new_state
);
827 _tnl_InvalidateState( ctx
, new_state
);
828 _vbo_InvalidateState( ctx
, new_state
);
829 _swsetup_InvalidateState( ctx
, new_state
);
831 if (ctx
->DrawBuffer
->Name
!= 0)
835 * GL_DITHER, GL_READ/DRAW_BUFFER, buffer binding state, etc. effect
836 * renderbuffer span/clear funcs.
838 if (new_state
& (_NEW_COLOR
| _NEW_PIXEL
| _NEW_BUFFERS
)) {
839 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
840 struct xmesa_renderbuffer
*front_xrb
, *back_xrb
;
842 front_xrb
= xmbuf
->frontxrb
;
844 xmesa_set_renderbuffer_funcs(front_xrb
, xmesa
->pixelformat
,
845 xmesa
->xm_visual
->BitsPerPixel
);
846 front_xrb
->clearFunc
= clear_pixmap
;
849 back_xrb
= xmbuf
->backxrb
;
851 xmesa_set_renderbuffer_funcs(back_xrb
, xmesa
->pixelformat
,
852 xmesa
->xm_visual
->BitsPerPixel
);
853 if (xmbuf
->backxrb
->pixmap
) {
854 back_xrb
->clearFunc
= clear_pixmap
;
857 switch (xmesa
->xm_visual
->BitsPerPixel
) {
859 if (xmesa
->xm_visual
->hpcr_clear_flag
) {
860 back_xrb
->clearFunc
= clear_HPCR_ximage
;
863 back_xrb
->clearFunc
= clear_8bit_ximage
;
867 back_xrb
->clearFunc
= clear_16bit_ximage
;
870 back_xrb
->clearFunc
= clear_24bit_ximage
;
873 back_xrb
->clearFunc
= clear_32bit_ximage
;
876 back_xrb
->clearFunc
= clear_nbit_ximage
;
883 if (xmesa
->xm_visual
->hpcr_clear_flag
) {
884 /* this depends on whether we're drawing to the front or back buffer */
888 ctx
->Driver
.ClearColor
= clear_color_HPCR_pixmap
;
891 ctx
->Driver
.ClearColor
= clear_color_HPCR_ximage
;
894 (void) clear_color_HPCR_pixmap
;
895 (void) clear_color_HPCR_ximage
;
903 * Called via ctx->Driver.TestProxyTeximage(). Normally, we'd just use
904 * the _mesa_test_proxy_teximage() fallback function, but we're going to
905 * special-case the 3D texture case to allow textures up to 512x512x32
909 test_proxy_teximage(GLcontext
*ctx
, GLenum target
, GLint level
,
910 GLint internalFormat
, GLenum format
, GLenum type
,
911 GLint width
, GLint height
, GLint depth
, GLint border
)
913 if (target
== GL_PROXY_TEXTURE_3D
) {
914 /* special case for 3D textures */
915 if (width
* height
* depth
> 512 * 512 * 64 ||
916 width
< 2 * border
||
917 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
918 _mesa_bitcount(width
- 2 * border
) != 1) ||
919 height
< 2 * border
||
920 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
921 _mesa_bitcount(height
- 2 * border
) != 1) ||
922 depth
< 2 * border
||
923 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
924 _mesa_bitcount(depth
- 2 * border
) != 1)) {
925 /* Bad size, or too many texels */
931 /* use the fallback routine for 1D, 2D, cube and rect targets */
932 return _mesa_test_proxy_teximage(ctx
, target
, level
, internalFormat
,
933 format
, type
, width
, height
, depth
,
940 * In SW, we don't really compress GL_COMPRESSED_RGB[A] textures!
942 static const struct gl_texture_format
*
943 choose_tex_format( GLcontext
*ctx
, GLint internalFormat
,
944 GLenum format
, GLenum type
)
946 switch (internalFormat
) {
947 case GL_COMPRESSED_RGB_ARB
:
948 return &_mesa_texformat_rgb
;
949 case GL_COMPRESSED_RGBA_ARB
:
950 return &_mesa_texformat_rgba
;
952 return _mesa_choose_tex_format(ctx
, internalFormat
, format
, type
);
958 * Called by glViewport.
959 * This is a good time for us to poll the current X window size and adjust
960 * our renderbuffers to match the current window size.
961 * Remember, we have no opportunity to respond to conventional
962 * X Resize/StructureNotify events since the X driver has no event loop.
964 * Note that this trick isn't fool-proof. If the application never calls
965 * glViewport, our notion of the current window size may be incorrect.
966 * That problem led to the GLX_MESA_resize_buffers extension.
969 xmesa_viewport(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei w
, GLsizei h
)
971 XMesaContext xmctx
= XMESA_CONTEXT(ctx
);
972 XMesaBuffer xmdrawbuf
= XMESA_BUFFER(ctx
->WinSysDrawBuffer
);
973 XMesaBuffer xmreadbuf
= XMESA_BUFFER(ctx
->WinSysReadBuffer
);
974 xmesa_check_and_update_buffer_size(xmctx
, xmdrawbuf
);
975 xmesa_check_and_update_buffer_size(xmctx
, xmreadbuf
);
983 #if ENABLE_EXT_timer_query
986 * The GL_EXT_timer_query extension is not enabled for the XServer
987 * indirect renderer. Not sure about how/if wrapping of gettimeofday()
991 struct xmesa_query_object
993 struct gl_query_object Base
;
994 struct timeval StartTime
;
998 static struct gl_query_object
*
999 xmesa_new_query_object(GLcontext
*ctx
, GLuint id
)
1001 struct xmesa_query_object
*q
= CALLOC_STRUCT(xmesa_query_object
);
1004 q
->Base
.Ready
= GL_TRUE
;
1011 xmesa_begin_query(GLcontext
*ctx
, GLenum target
, struct gl_query_object
*q
)
1013 if (target
== GL_TIME_ELAPSED_EXT
) {
1014 struct xmesa_query_object
*xq
= (struct xmesa_query_object
*) q
;
1015 (void) gettimeofday(&xq
->StartTime
, NULL
);
1021 * Return the difference between the two given times in microseconds.
1024 #define suseconds_t unsigned int
1027 time_diff(const struct timeval
*t0
, const struct timeval
*t1
)
1029 GLuint64EXT seconds0
= t0
->tv_sec
& 0xff; /* 0 .. 255 seconds */
1030 GLuint64EXT seconds1
= t1
->tv_sec
& 0xff; /* 0 .. 255 seconds */
1031 GLuint64EXT nanosec0
= (seconds0
* 1000000 + t0
->tv_usec
) * 1000;
1032 GLuint64EXT nanosec1
= (seconds1
* 1000000 + t1
->tv_usec
) * 1000;
1033 return nanosec1
- nanosec0
;
1038 xmesa_end_query(GLcontext
*ctx
, GLenum target
, struct gl_query_object
*q
)
1040 if (target
== GL_TIME_ELAPSED_EXT
) {
1041 struct xmesa_query_object
*xq
= (struct xmesa_query_object
*) q
;
1042 struct timeval endTime
;
1043 (void) gettimeofday(&endTime
, NULL
);
1044 /* result is in nanoseconds! */
1045 q
->Result
= time_diff(&xq
->StartTime
, &endTime
);
1050 #endif /* ENABLE_timer_query */
1054 * Initialize the device driver function table with the functions
1055 * we implement in this driver.
1058 xmesa_init_driver_functions( XMesaVisual xmvisual
,
1059 struct dd_function_table
*driver
)
1061 driver
->GetString
= get_string
;
1062 driver
->UpdateState
= xmesa_update_state
;
1063 driver
->GetBufferSize
= NULL
; /* OBSOLETE */
1064 driver
->Flush
= finish_or_flush
;
1065 driver
->Finish
= finish_or_flush
;
1066 driver
->ClearIndex
= clear_index
;
1067 driver
->ClearColor
= clear_color
;
1068 driver
->IndexMask
= index_mask
;
1069 driver
->ColorMask
= color_mask
;
1070 driver
->Enable
= enable
;
1071 driver
->Clear
= clear_buffers
;
1072 driver
->Viewport
= xmesa_viewport
;
1073 #ifndef XFree86Server
1074 driver
->CopyPixels
= xmesa_CopyPixels
;
1075 if (xmvisual
->undithered_pf
== PF_8R8G8B
&&
1076 xmvisual
->dithered_pf
== PF_8R8G8B
) {
1077 driver
->DrawPixels
= xmesa_DrawPixels_8R8G8B
;
1079 else if (xmvisual
->undithered_pf
== PF_5R6G5B
) {
1080 driver
->DrawPixels
= xmesa_DrawPixels_5R6G5B
;
1083 driver
->TestProxyTexImage
= test_proxy_teximage
;
1084 #if ENABLE_EXT_texure_compression_s3tc
1085 driver
->ChooseTextureFormat
= choose_tex_format
;
1087 (void) choose_tex_format
;
1090 #if ENABLE_EXT_timer_query
1091 driver
->NewQueryObject
= xmesa_new_query_object
;
1092 driver
->BeginQuery
= xmesa_begin_query
;
1093 driver
->EndQuery
= xmesa_end_query
;
1098 #define XMESA_NEW_POINT (_NEW_POINT | \
1100 _SWRAST_NEW_RASTERMASK)
1102 #define XMESA_NEW_LINE (_NEW_LINE | \
1107 _SWRAST_NEW_RASTERMASK)
1109 #define XMESA_NEW_TRIANGLE (_NEW_POLYGON | \
1114 _SWRAST_NEW_RASTERMASK)
1118 * Extend the software rasterizer with our line/point/triangle
1120 * Called during context creation only.
1122 void xmesa_register_swrast_functions( GLcontext
*ctx
)
1124 SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
1126 swrast
->choose_point
= xmesa_choose_point
;
1127 swrast
->choose_line
= xmesa_choose_line
;
1128 swrast
->choose_triangle
= xmesa_choose_triangle
;
1130 /* XXX these lines have no net effect. Remove??? */
1131 swrast
->InvalidatePointMask
|= XMESA_NEW_POINT
;
1132 swrast
->InvalidateLineMask
|= XMESA_NEW_LINE
;
1133 swrast
->InvalidateTriangleMask
|= XMESA_NEW_TRIANGLE
;