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
);
166 /* Implements glColorMask() */
168 color_mask(GLcontext
*ctx
,
169 GLboolean rmask
, GLboolean gmask
, GLboolean bmask
, GLboolean amask
)
171 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
173 const int xclass
= xmesa
->xm_visual
->mesa_visual
.visualType
;
176 if (ctx
->DrawBuffer
->Name
!= 0)
179 xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
181 if (xclass
== GLX_TRUE_COLOR
|| xclass
== GLX_DIRECT_COLOR
) {
183 if (rmask
&& gmask
&& bmask
) {
184 m
= ((unsigned long)~0L);
188 if (rmask
) m
|= GET_REDMASK(xmesa
->xm_visual
);
189 if (gmask
) m
|= GET_GREENMASK(xmesa
->xm_visual
);
190 if (bmask
) m
|= GET_BLUEMASK(xmesa
->xm_visual
);
192 XMesaSetPlaneMask( xmesa
->display
, xmbuf
->cleargc
, m
);
198 /**********************************************************************/
199 /*** glClear implementations ***/
200 /**********************************************************************/
204 * Clear the front or back color buffer, if it's implemented with a pixmap.
207 clear_pixmap(GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
208 GLint x
, GLint y
, GLint width
, GLint height
)
210 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
211 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
216 assert(xmesa
->display
);
218 assert(xmbuf
->cleargc
);
220 XMesaFillRectangle( xmesa
->display
, xrb
->pixmap
, xmbuf
->cleargc
,
221 x
, xrb
->Base
.Height
- y
- height
,
227 clear_8bit_ximage( GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
228 GLint x
, GLint y
, GLint width
, GLint height
)
230 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
232 for (i
= 0; i
< height
; i
++) {
233 GLubyte
*ptr
= PIXEL_ADDR1(xrb
, x
, y
+ i
);
234 MEMSET( ptr
, xmesa
->clearpixel
, width
);
240 clear_HPCR_ximage( GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
241 GLint x
, GLint y
, GLint width
, GLint height
)
243 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
245 for (i
= y
; i
< y
+ height
; i
++) {
246 GLubyte
*ptr
= PIXEL_ADDR1( xrb
, x
, i
);
248 const GLubyte
*sptr
= xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0];
252 for (j
= x
; j
< x
+ width
; j
++) {
261 clear_16bit_ximage( GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
262 GLint x
, GLint y
, GLint width
, GLint height
)
264 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
265 GLuint pixel
= (GLuint
) xmesa
->clearpixel
;
268 if (xmesa
->swapbytes
) {
269 pixel
= ((pixel
>> 8) & 0x00ff) | ((pixel
<< 8) & 0xff00);
272 for (j
= 0; j
< height
; j
++) {
273 GLushort
*ptr2
= PIXEL_ADDR2(xrb
, x
, y
+ j
);
274 for (i
= 0; i
< width
; i
++) {
281 /* Optimized code provided by Nozomi Ytow <noz@xfree86.org> */
283 clear_24bit_ximage(GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
284 GLint x
, GLint y
, GLint width
, GLint height
)
286 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
287 const GLubyte r
= xmesa
->clearcolor
[0];
288 const GLubyte g
= xmesa
->clearcolor
[1];
289 const GLubyte b
= xmesa
->clearcolor
[2];
291 if (r
== g
&& g
== b
) {
292 /* same value for all three components (gray) */
294 for (j
= 0; j
< height
; j
++) {
295 bgr_t
*ptr3
= PIXEL_ADDR3(xrb
, x
, y
+ j
);
296 MEMSET(ptr3
, r
, 3 * width
);
300 /* non-gray clear color */
302 for (j
= 0; j
< height
; j
++) {
303 bgr_t
*ptr3
= PIXEL_ADDR3(xrb
, x
, y
+ j
);
304 for (i
= 0; i
< width
; i
++) {
316 clear_32bit_ximage(GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
317 GLint x
, GLint y
, GLint width
, GLint height
)
319 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
320 register GLuint pixel
= (GLuint
) xmesa
->clearpixel
;
325 if (xmesa
->swapbytes
) {
326 pixel
= ((pixel
>> 24) & 0x000000ff)
327 | ((pixel
>> 8) & 0x0000ff00)
328 | ((pixel
<< 8) & 0x00ff0000)
329 | ((pixel
<< 24) & 0xff000000);
332 if (width
== xrb
->Base
.Width
&& height
== xrb
->Base
.Height
) {
333 /* clearing whole buffer */
334 const GLuint n
= xrb
->Base
.Width
* xrb
->Base
.Height
;
335 GLuint
*ptr4
= (GLuint
*) xrb
->ximage
->data
;
338 _mesa_memset(ptr4
, pixel
, 4 * n
);
342 for (i
= 0; i
< n
; i
++)
347 /* clearing scissored region */
349 for (j
= 0; j
< height
; j
++) {
350 GLuint
*ptr4
= PIXEL_ADDR4(xrb
, x
, y
+ j
);
351 for (i
= 0; i
< width
; i
++) {
360 clear_nbit_ximage(GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
361 GLint x
, GLint y
, GLint width
, GLint height
)
363 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
364 XMesaImage
*img
= xrb
->ximage
;
367 /* TODO: optimize this */
369 for (j
= 0; j
< height
; j
++) {
370 for (i
= 0; i
< width
; i
++) {
371 XMesaPutPixel(img
, x
+i
, y
-j
, xmesa
->clearpixel
);
379 clear_buffers(GLcontext
*ctx
, GLbitfield buffers
)
381 if (ctx
->DrawBuffer
->Name
== 0) {
382 /* this is a window system framebuffer */
383 const GLuint
*colorMask
= (GLuint
*) &ctx
->Color
.ColorMask
;
384 XMesaBuffer b
= XMESA_BUFFER(ctx
->DrawBuffer
);
385 const GLint x
= ctx
->DrawBuffer
->_Xmin
;
386 const GLint y
= ctx
->DrawBuffer
->_Ymin
;
387 const GLint width
= ctx
->DrawBuffer
->_Xmax
- x
;
388 const GLint height
= ctx
->DrawBuffer
->_Ymax
- y
;
390 /* we can't handle color or index masking */
391 if (*colorMask
== 0xffffffff && ctx
->Color
.IndexMask
== 0xffffffff) {
392 if (buffers
& BUFFER_BIT_FRONT_LEFT
) {
393 /* clear front color buffer */
394 struct gl_renderbuffer
*frontRb
395 = ctx
->DrawBuffer
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
;
396 if (b
->frontxrb
== xmesa_renderbuffer(frontRb
)) {
397 /* renderbuffer is not wrapped - great! */
398 b
->frontxrb
->clearFunc(ctx
, b
->frontxrb
, x
, y
, width
, height
);
399 buffers
&= ~BUFFER_BIT_FRONT_LEFT
;
402 /* we can't directly clear an alpha-wrapped color buffer */
405 if (buffers
& BUFFER_BIT_BACK_LEFT
) {
406 /* clear back color buffer */
407 struct gl_renderbuffer
*backRb
408 = ctx
->DrawBuffer
->Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
;
409 if (b
->backxrb
== xmesa_renderbuffer(backRb
)) {
410 /* renderbuffer is not wrapped - great! */
411 b
->backxrb
->clearFunc(ctx
, b
->backxrb
, x
, y
, width
, height
);
412 buffers
&= ~BUFFER_BIT_BACK_LEFT
;
418 _swrast_Clear(ctx
, buffers
);
422 #ifndef XFree86Server
423 /* XXX this was never tested in the Xserver environment */
426 * This function implements glDrawPixels() with an XPutImage call when
427 * drawing to the front buffer (X Window drawable).
428 * The image format must be GL_BGRA to match the PF_8R8G8B pixel format.
431 xmesa_DrawPixels_8R8G8B( GLcontext
*ctx
,
432 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
433 GLenum format
, GLenum type
,
434 const struct gl_pixelstore_attrib
*unpack
,
435 const GLvoid
*pixels
)
437 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
438 struct gl_renderbuffer
*rb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[0];
439 struct xmesa_renderbuffer
*xrb
;
444 xrb
= xmesa_renderbuffer(rb
->Wrapped
);
446 if (swrast
->NewState
)
447 _swrast_validate_derived( ctx
);
449 if (ctx
->DrawBuffer
->Name
== 0 &&
451 type
== GL_UNSIGNED_BYTE
&&
452 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
453 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
454 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
455 ctx
->Pixel
.ZoomY
== 1.0 &&
456 xrb
->pixmap
&& /* drawing to pixmap or window */
457 xrb
->Base
.AlphaBits
== 0)
459 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
460 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
461 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
462 const XMesaGC gc
= xmbuf
->cleargc
; /* effected by glColorMask */
467 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
469 ASSERT(xmesa
->xm_visual
->dithered_pf
== PF_8R8G8B
);
470 ASSERT(xmesa
->xm_visual
->undithered_pf
== PF_8R8G8B
);
474 if (unpack
->BufferObj
->Name
) {
475 /* unpack from PBO */
477 if (!_mesa_validate_pbo_access(2, unpack
, width
, height
, 1,
478 format
, type
, pixels
)) {
479 _mesa_error(ctx
, GL_INVALID_OPERATION
,
480 "glDrawPixels(invalid PBO access)");
483 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
,
484 GL_PIXEL_UNPACK_BUFFER_EXT
,
488 /* buffer is already mapped - that's an error */
489 _mesa_error(ctx
, GL_INVALID_OPERATION
,
490 "glDrawPixels(PBO is mapped)");
493 pixels
= ADD_POINTERS(buf
, pixels
);
496 if (_mesa_clip_drawpixels(ctx
, &dstX
, &dstY
, &w
, &h
, &clippedUnpack
)) {
497 /* This is a little tricky since all coordinates up to now have
498 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
499 * so we have to carefully compute the Y coordinates/addresses here.
501 int srcX
= clippedUnpack
.SkipPixels
;
502 int srcY
= clippedUnpack
.SkipRows
;
503 int rowLength
= clippedUnpack
.RowLength
;
505 MEMSET(&ximage
, 0, sizeof(XMesaImage
));
506 ximage
.width
= width
;
507 ximage
.height
= height
;
508 ximage
.format
= ZPixmap
;
509 ximage
.data
= (char *) pixels
510 + ((srcY
+ h
- 1) * rowLength
+ srcX
) * 4;
511 ximage
.byte_order
= LSBFirst
;
512 ximage
.bitmap_unit
= 32;
513 ximage
.bitmap_bit_order
= LSBFirst
;
514 ximage
.bitmap_pad
= 32;
516 ximage
.bytes_per_line
= -rowLength
* 4; /* negative to flip image */
517 ximage
.bits_per_pixel
= 32;
518 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
519 /* flip Y axis for dest position */
520 dstY
= YFLIP(xrb
, dstY
) - h
+ 1;
521 XPutImage(dpy
, xrb
->pixmap
, gc
, &ximage
, 0, 0, dstX
, dstY
, w
, h
);
524 if (unpack
->BufferObj
->Name
) {
525 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
530 /* software fallback */
531 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
532 format
, type
, unpack
, pixels
);
539 * This function implements glDrawPixels() with an XPutImage call when
540 * drawing to the front buffer (X Window drawable). The image format
541 * must be GL_RGB and image type must be GL_UNSIGNED_SHORT_5_6_5 to
542 * match the PF_5R6G5B pixel format.
545 xmesa_DrawPixels_5R6G5B( GLcontext
*ctx
,
546 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
547 GLenum format
, GLenum type
,
548 const struct gl_pixelstore_attrib
*unpack
,
549 const GLvoid
*pixels
)
551 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
552 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
553 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
554 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
555 const XMesaGC gc
= xmbuf
->cleargc
; /* effected by glColorMask */
556 struct xmesa_renderbuffer
*xrb
;
560 ASSERT(xmesa
->xm_visual
->undithered_pf
== PF_5R6G5B
);
562 if (!ctx
->DrawBuffer
->_ColorDrawBuffers
[0])
565 xrb
= xmesa_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]->Wrapped
);
567 if (swrast
->NewState
)
568 _swrast_validate_derived( ctx
);
570 if (xrb
->pixmap
&& /* drawing to pixmap or window */
572 type
== GL_UNSIGNED_SHORT_5_6_5
&&
573 !ctx
->Color
.DitherFlag
&& /* no dithering */
574 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
575 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
576 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
577 ctx
->Pixel
.ZoomY
== 1.0) {
582 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
584 if (unpack
->BufferObj
->Name
) {
585 /* unpack from PBO */
587 if (!_mesa_validate_pbo_access(2, unpack
, width
, height
, 1,
588 format
, type
, pixels
)) {
589 _mesa_error(ctx
, GL_INVALID_OPERATION
,
590 "glDrawPixels(invalid PBO access)");
593 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
,
594 GL_PIXEL_UNPACK_BUFFER_EXT
,
598 /* buffer is already mapped - that's an error */
599 _mesa_error(ctx
, GL_INVALID_OPERATION
,
600 "glDrawPixels(PBO is mapped)");
603 pixels
= ADD_POINTERS(buf
, pixels
);
606 if (_mesa_clip_drawpixels(ctx
, &dstX
, &dstY
, &w
, &h
, &clippedUnpack
)) {
607 /* This is a little tricky since all coordinates up to now have
608 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
609 * so we have to carefully compute the Y coordinates/addresses here.
611 int srcX
= clippedUnpack
.SkipPixels
;
612 int srcY
= clippedUnpack
.SkipRows
;
613 int rowLength
= clippedUnpack
.RowLength
;
615 MEMSET(&ximage
, 0, sizeof(XMesaImage
));
616 ximage
.width
= width
;
617 ximage
.height
= height
;
618 ximage
.format
= ZPixmap
;
619 ximage
.data
= (char *) pixels
620 + ((srcY
+ h
- 1) * rowLength
+ srcX
) * 2;
621 ximage
.byte_order
= LSBFirst
;
622 ximage
.bitmap_unit
= 16;
623 ximage
.bitmap_bit_order
= LSBFirst
;
624 ximage
.bitmap_pad
= 16;
626 ximage
.bytes_per_line
= -rowLength
* 2; /* negative to flip image */
627 ximage
.bits_per_pixel
= 16;
628 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
629 /* flip Y axis for dest position */
630 dstY
= YFLIP(xrb
, dstY
) - h
+ 1;
631 XPutImage(dpy
, xrb
->pixmap
, gc
, &ximage
, 0, 0, dstX
, dstY
, w
, h
);
634 if (unpack
->BufferObj
->Name
) {
635 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
640 /* software fallback */
641 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
642 format
, type
, unpack
, pixels
);
649 * Implement glCopyPixels for the front color buffer (or back buffer Pixmap)
650 * for the color buffer. Don't support zooming, pixel transfer, etc.
651 * We do support copying from one window to another, ala glXMakeCurrentRead.
654 xmesa_CopyPixels( GLcontext
*ctx
,
655 GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
656 GLint destx
, GLint desty
, GLenum type
)
658 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
659 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
660 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
661 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
662 const XMesaGC gc
= xmbuf
->cleargc
; /* effected by glColorMask */
663 struct xmesa_renderbuffer
*srcXrb
, *dstXrb
;
665 if (!ctx
->ReadBuffer
->_ColorReadBuffer
||
666 !ctx
->DrawBuffer
->_ColorDrawBuffers
[0])
672 srcXrb
= xmesa_renderbuffer(ctx
->ReadBuffer
->_ColorReadBuffer
->Wrapped
);
673 dstXrb
= xmesa_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]->Wrapped
);
675 if (swrast
->NewState
)
676 _swrast_validate_derived( ctx
);
678 if (ctx
->Color
.DrawBuffer
[0] == GL_FRONT
&&
679 ctx
->Pixel
.ReadBuffer
== GL_FRONT
&&
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) {
687 /* Note: we don't do any special clipping work here. We could,
688 * but X will do it for us.
690 srcy
= YFLIP(srcXrb
, srcy
) - height
+ 1;
691 desty
= YFLIP(dstXrb
, desty
) - height
+ 1;
692 XCopyArea(dpy
, srcXrb
->pixmap
, dstXrb
->pixmap
, gc
,
693 srcx
, srcy
, width
, height
, destx
, desty
);
696 _swrast_CopyPixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
, type
);
699 #endif /* XFree86Server */
704 * Every driver should implement a GetString function in order to
705 * return a meaningful GL_RENDERER string.
707 static const GLubyte
*
708 get_string( GLcontext
*ctx
, GLenum name
)
714 return (const GLubyte
*) "Mesa GLX Indirect";
716 return (const GLubyte
*) "Mesa X11";
720 return (const GLubyte
*) "Mesa project: www.mesa3d.org";
731 * We implement the glEnable function only because we care about
732 * dither enable/disable.
735 enable( GLcontext
*ctx
, GLenum pname
, GLboolean state
)
737 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
742 xmesa
->pixelformat
= xmesa
->xm_visual
->dithered_pf
;
744 xmesa
->pixelformat
= xmesa
->xm_visual
->undithered_pf
;
747 ; /* silence compiler warning */
753 clear_color_HPCR_ximage( GLcontext
*ctx
, const GLfloat color
[4] )
756 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
758 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[0], color
[0]);
759 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[1], color
[1]);
760 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[2], color
[2]);
761 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[3], color
[3]);
763 if (color
[0] == 0.0 && color
[1] == 0.0 && color
[2] == 0.0) {
765 MEMSET( xmesa
->xm_visual
->hpcr_clear_ximage_pattern
, 0x0 ,
766 sizeof(xmesa
->xm_visual
->hpcr_clear_ximage_pattern
));
769 /* build clear pattern */
770 for (i
=0; i
<16; i
++) {
771 xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0][i
] =
773 xmesa
->clearcolor
[0],
774 xmesa
->clearcolor
[1],
775 xmesa
->clearcolor
[2]);
776 xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[1][i
] =
778 xmesa
->clearcolor
[0],
779 xmesa
->clearcolor
[1],
780 xmesa
->clearcolor
[2]);
787 clear_color_HPCR_pixmap( GLcontext
*ctx
, const GLfloat color
[4] )
790 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
792 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[0], color
[0]);
793 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[1], color
[1]);
794 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[2], color
[2]);
795 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[3], color
[3]);
797 if (color
[0] == 0.0 && color
[1] == 0.0 && color
[2] == 0.0) {
799 for (i
=0; i
<16; i
++) {
800 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 0, 0);
801 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 1, 0);
805 for (i
=0; i
<16; i
++) {
806 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 0,
808 xmesa
->clearcolor
[0],
809 xmesa
->clearcolor
[1],
810 xmesa
->clearcolor
[2]));
811 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 1,
813 xmesa
->clearcolor
[0],
814 xmesa
->clearcolor
[1],
815 xmesa
->clearcolor
[2]));
818 /* change tile pixmap content */
819 XMesaPutImage(xmesa
->display
,
820 (XMesaDrawable
)xmesa
->xm_visual
->hpcr_clear_pixmap
,
821 XMESA_BUFFER(ctx
->DrawBuffer
)->cleargc
,
822 xmesa
->xm_visual
->hpcr_clear_ximage
, 0, 0, 0, 0, 16, 2);
827 * Called when the driver should update its state, based on the new_state
831 xmesa_update_state( GLcontext
*ctx
, GLbitfield new_state
)
833 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
835 /* Propagate statechange information to swrast and swrast_setup
836 * modules. The X11 driver has no internal GL-dependent state.
838 _swrast_InvalidateState( ctx
, new_state
);
839 _tnl_InvalidateState( ctx
, new_state
);
840 _vbo_InvalidateState( ctx
, new_state
);
841 _swsetup_InvalidateState( ctx
, new_state
);
843 if (ctx
->DrawBuffer
->Name
!= 0)
847 * GL_DITHER, GL_READ/DRAW_BUFFER, buffer binding state, etc. effect
848 * renderbuffer span/clear funcs.
850 if (new_state
& (_NEW_COLOR
| _NEW_PIXEL
| _NEW_BUFFERS
)) {
851 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
852 struct xmesa_renderbuffer
*front_xrb
, *back_xrb
;
854 front_xrb
= xmbuf
->frontxrb
;
856 xmesa_set_renderbuffer_funcs(front_xrb
, xmesa
->pixelformat
,
857 xmesa
->xm_visual
->BitsPerPixel
);
858 front_xrb
->clearFunc
= clear_pixmap
;
861 back_xrb
= xmbuf
->backxrb
;
863 xmesa_set_renderbuffer_funcs(back_xrb
, xmesa
->pixelformat
,
864 xmesa
->xm_visual
->BitsPerPixel
);
865 if (xmbuf
->backxrb
->pixmap
) {
866 back_xrb
->clearFunc
= clear_pixmap
;
869 switch (xmesa
->xm_visual
->BitsPerPixel
) {
871 if (xmesa
->xm_visual
->hpcr_clear_flag
) {
872 back_xrb
->clearFunc
= clear_HPCR_ximage
;
875 back_xrb
->clearFunc
= clear_8bit_ximage
;
879 back_xrb
->clearFunc
= clear_16bit_ximage
;
882 back_xrb
->clearFunc
= clear_24bit_ximage
;
885 back_xrb
->clearFunc
= clear_32bit_ximage
;
888 back_xrb
->clearFunc
= clear_nbit_ximage
;
895 if (xmesa
->xm_visual
->hpcr_clear_flag
) {
896 /* this depends on whether we're drawing to the front or back buffer */
900 ctx
->Driver
.ClearColor
= clear_color_HPCR_pixmap
;
903 ctx
->Driver
.ClearColor
= clear_color_HPCR_ximage
;
906 (void) clear_color_HPCR_pixmap
;
907 (void) clear_color_HPCR_ximage
;
915 * Called via ctx->Driver.TestProxyTeximage(). Normally, we'd just use
916 * the _mesa_test_proxy_teximage() fallback function, but we're going to
917 * special-case the 3D texture case to allow textures up to 512x512x32
921 test_proxy_teximage(GLcontext
*ctx
, GLenum target
, GLint level
,
922 GLint internalFormat
, GLenum format
, GLenum type
,
923 GLint width
, GLint height
, GLint depth
, GLint border
)
925 if (target
== GL_PROXY_TEXTURE_3D
) {
926 /* special case for 3D textures */
927 if (width
* height
* depth
> 512 * 512 * 64 ||
928 width
< 2 * border
||
929 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
930 _mesa_bitcount(width
- 2 * border
) != 1) ||
931 height
< 2 * border
||
932 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
933 _mesa_bitcount(height
- 2 * border
) != 1) ||
934 depth
< 2 * border
||
935 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
936 _mesa_bitcount(depth
- 2 * border
) != 1)) {
937 /* Bad size, or too many texels */
943 /* use the fallback routine for 1D, 2D, cube and rect targets */
944 return _mesa_test_proxy_teximage(ctx
, target
, level
, internalFormat
,
945 format
, type
, width
, height
, depth
,
952 * In SW, we don't really compress GL_COMPRESSED_RGB[A] textures!
954 static const struct gl_texture_format
*
955 choose_tex_format( GLcontext
*ctx
, GLint internalFormat
,
956 GLenum format
, GLenum type
)
958 switch (internalFormat
) {
959 case GL_COMPRESSED_RGB_ARB
:
960 return &_mesa_texformat_rgb
;
961 case GL_COMPRESSED_RGBA_ARB
:
962 return &_mesa_texformat_rgba
;
964 return _mesa_choose_tex_format(ctx
, internalFormat
, format
, type
);
970 * Called by glViewport.
971 * This is a good time for us to poll the current X window size and adjust
972 * our renderbuffers to match the current window size.
973 * Remember, we have no opportunity to respond to conventional
974 * X Resize/StructureNotify events since the X driver has no event loop.
976 * Note that this trick isn't fool-proof. If the application never calls
977 * glViewport, our notion of the current window size may be incorrect.
978 * That problem led to the GLX_MESA_resize_buffers extension.
981 xmesa_viewport(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei w
, GLsizei h
)
983 XMesaContext xmctx
= XMESA_CONTEXT(ctx
);
984 XMesaBuffer xmdrawbuf
= XMESA_BUFFER(ctx
->WinSysDrawBuffer
);
985 XMesaBuffer xmreadbuf
= XMESA_BUFFER(ctx
->WinSysReadBuffer
);
986 xmesa_check_and_update_buffer_size(xmctx
, xmdrawbuf
);
987 xmesa_check_and_update_buffer_size(xmctx
, xmreadbuf
);
995 #if ENABLE_EXT_timer_query
998 * The GL_EXT_timer_query extension is not enabled for the XServer
999 * indirect renderer. Not sure about how/if wrapping of gettimeofday()
1003 struct xmesa_query_object
1005 struct gl_query_object Base
;
1006 struct timeval StartTime
;
1010 static struct gl_query_object
*
1011 xmesa_new_query_object(GLcontext
*ctx
, GLuint id
)
1013 struct xmesa_query_object
*q
= CALLOC_STRUCT(xmesa_query_object
);
1016 q
->Base
.Ready
= GL_TRUE
;
1023 xmesa_begin_query(GLcontext
*ctx
, GLenum target
, struct gl_query_object
*q
)
1025 if (target
== GL_TIME_ELAPSED_EXT
) {
1026 struct xmesa_query_object
*xq
= (struct xmesa_query_object
*) q
;
1027 (void) gettimeofday(&xq
->StartTime
, NULL
);
1033 * Return the difference between the two given times in microseconds.
1036 #define suseconds_t unsigned int
1039 time_diff(const struct timeval
*t0
, const struct timeval
*t1
)
1041 GLuint64EXT seconds0
= t0
->tv_sec
& 0xff; /* 0 .. 255 seconds */
1042 GLuint64EXT seconds1
= t1
->tv_sec
& 0xff; /* 0 .. 255 seconds */
1043 GLuint64EXT nanosec0
= (seconds0
* 1000000 + t0
->tv_usec
) * 1000;
1044 GLuint64EXT nanosec1
= (seconds1
* 1000000 + t1
->tv_usec
) * 1000;
1045 return nanosec1
- nanosec0
;
1050 xmesa_end_query(GLcontext
*ctx
, GLenum target
, struct gl_query_object
*q
)
1052 if (target
== GL_TIME_ELAPSED_EXT
) {
1053 struct xmesa_query_object
*xq
= (struct xmesa_query_object
*) q
;
1054 struct timeval endTime
;
1055 (void) gettimeofday(&endTime
, NULL
);
1056 /* result is in nanoseconds! */
1057 q
->Result
= time_diff(&xq
->StartTime
, &endTime
);
1062 #endif /* ENABLE_timer_query */
1066 * Initialize the device driver function table with the functions
1067 * we implement in this driver.
1070 xmesa_init_driver_functions( XMesaVisual xmvisual
,
1071 struct dd_function_table
*driver
)
1073 driver
->GetString
= get_string
;
1074 driver
->UpdateState
= xmesa_update_state
;
1075 driver
->GetBufferSize
= NULL
; /* OBSOLETE */
1076 driver
->Flush
= finish_or_flush
;
1077 driver
->Finish
= finish_or_flush
;
1078 driver
->ClearIndex
= clear_index
;
1079 driver
->ClearColor
= clear_color
;
1080 driver
->IndexMask
= index_mask
;
1081 driver
->ColorMask
= color_mask
;
1082 driver
->Enable
= enable
;
1083 driver
->Clear
= clear_buffers
;
1084 driver
->Viewport
= xmesa_viewport
;
1085 #ifndef XFree86Server
1086 driver
->CopyPixels
= xmesa_CopyPixels
;
1087 if (xmvisual
->undithered_pf
== PF_8R8G8B
&&
1088 xmvisual
->dithered_pf
== PF_8R8G8B
) {
1089 driver
->DrawPixels
= xmesa_DrawPixels_8R8G8B
;
1091 else if (xmvisual
->undithered_pf
== PF_5R6G5B
) {
1092 driver
->DrawPixels
= xmesa_DrawPixels_5R6G5B
;
1095 driver
->TestProxyTexImage
= test_proxy_teximage
;
1096 #if ENABLE_EXT_texure_compression_s3tc
1097 driver
->ChooseTextureFormat
= choose_tex_format
;
1099 (void) choose_tex_format
;
1102 #if ENABLE_EXT_timer_query
1103 driver
->NewQueryObject
= xmesa_new_query_object
;
1104 driver
->BeginQuery
= xmesa_begin_query
;
1105 driver
->EndQuery
= xmesa_end_query
;
1110 #define XMESA_NEW_POINT (_NEW_POINT | \
1112 _SWRAST_NEW_RASTERMASK)
1114 #define XMESA_NEW_LINE (_NEW_LINE | \
1119 _SWRAST_NEW_RASTERMASK)
1121 #define XMESA_NEW_TRIANGLE (_NEW_POLYGON | \
1126 _SWRAST_NEW_RASTERMASK)
1130 * Extend the software rasterizer with our line/point/triangle
1132 * Called during context creation only.
1134 void xmesa_register_swrast_functions( GLcontext
*ctx
)
1136 SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
1138 swrast
->choose_point
= xmesa_choose_point
;
1139 swrast
->choose_line
= xmesa_choose_line
;
1140 swrast
->choose_triangle
= xmesa_choose_triangle
;
1142 /* XXX these lines have no net effect. Remove??? */
1143 swrast
->InvalidatePointMask
|= XMESA_NEW_POINT
;
1144 swrast
->InvalidateLineMask
|= XMESA_NEW_LINE
;
1145 swrast
->InvalidateTriangleMask
|= XMESA_NEW_TRIANGLE
;