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"
57 #include <GL/glxtokens.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
);
166 XMesaSetPlaneMask( xmesa
->display
, xmbuf
->gc
, m
);
171 /* Implements glColorMask() */
173 color_mask(GLcontext
*ctx
,
174 GLboolean rmask
, GLboolean gmask
, GLboolean bmask
, GLboolean amask
)
176 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
178 const int xclass
= xmesa
->xm_visual
->mesa_visual
.visualType
;
181 if (ctx
->DrawBuffer
->Name
!= 0)
184 xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
186 if (xclass
== GLX_TRUE_COLOR
|| xclass
== GLX_DIRECT_COLOR
) {
188 if (rmask
&& gmask
&& bmask
) {
189 m
= ((unsigned long)~0L);
193 if (rmask
) m
|= GET_REDMASK(xmesa
->xm_visual
);
194 if (gmask
) m
|= GET_GREENMASK(xmesa
->xm_visual
);
195 if (bmask
) m
|= GET_BLUEMASK(xmesa
->xm_visual
);
197 XMesaSetPlaneMask( xmesa
->display
, xmbuf
->cleargc
, m
);
198 XMesaSetPlaneMask( xmesa
->display
, xmbuf
->gc
, m
);
204 /**********************************************************************/
205 /*** glClear implementations ***/
206 /**********************************************************************/
210 * Clear the front or back color buffer, if it's implemented with a pixmap.
213 clear_pixmap(GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
214 GLint x
, GLint y
, GLint width
, GLint height
)
216 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
217 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
222 assert(xmesa
->display
);
224 assert(xmbuf
->cleargc
);
226 XMesaFillRectangle( xmesa
->display
, xrb
->pixmap
, xmbuf
->cleargc
,
227 x
, xrb
->Base
.Height
- y
- height
,
233 clear_8bit_ximage( GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
234 GLint x
, GLint y
, GLint width
, GLint height
)
236 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
238 for (i
= 0; i
< height
; i
++) {
239 GLubyte
*ptr
= PIXEL_ADDR1(xrb
, x
, y
+ i
);
240 MEMSET( ptr
, xmesa
->clearpixel
, width
);
246 clear_HPCR_ximage( GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
247 GLint x
, GLint y
, GLint width
, GLint height
)
249 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
251 for (i
= y
; i
< y
+ height
; i
++) {
252 GLubyte
*ptr
= PIXEL_ADDR1( xrb
, x
, i
);
254 const GLubyte
*sptr
= xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0];
258 for (j
= x
; j
< x
+ width
; j
++) {
267 clear_16bit_ximage( GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
268 GLint x
, GLint y
, GLint width
, GLint height
)
270 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
271 GLuint pixel
= (GLuint
) xmesa
->clearpixel
;
274 if (xmesa
->swapbytes
) {
275 pixel
= ((pixel
>> 8) & 0x00ff) | ((pixel
<< 8) & 0xff00);
278 for (j
= 0; j
< height
; j
++) {
279 GLushort
*ptr2
= PIXEL_ADDR2(xrb
, x
, y
+ j
);
280 for (i
= 0; i
< width
; i
++) {
287 /* Optimized code provided by Nozomi Ytow <noz@xfree86.org> */
289 clear_24bit_ximage(GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
290 GLint x
, GLint y
, GLint width
, GLint height
)
292 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
293 const GLubyte r
= xmesa
->clearcolor
[0];
294 const GLubyte g
= xmesa
->clearcolor
[1];
295 const GLubyte b
= xmesa
->clearcolor
[2];
297 if (r
== g
&& g
== b
) {
298 /* same value for all three components (gray) */
300 for (j
= 0; j
< height
; j
++) {
301 bgr_t
*ptr3
= PIXEL_ADDR3(xrb
, x
, y
+ j
);
302 MEMSET(ptr3
, r
, 3 * width
);
306 /* non-gray clear color */
308 for (j
= 0; j
< height
; j
++) {
309 bgr_t
*ptr3
= PIXEL_ADDR3(xrb
, x
, y
+ j
);
310 for (i
= 0; i
< width
; i
++) {
322 clear_32bit_ximage(GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
323 GLint x
, GLint y
, GLint width
, GLint height
)
325 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
326 register GLuint pixel
= (GLuint
) xmesa
->clearpixel
;
331 if (xmesa
->swapbytes
) {
332 pixel
= ((pixel
>> 24) & 0x000000ff)
333 | ((pixel
>> 8) & 0x0000ff00)
334 | ((pixel
<< 8) & 0x00ff0000)
335 | ((pixel
<< 24) & 0xff000000);
338 if (width
== xrb
->Base
.Width
&& height
== xrb
->Base
.Height
) {
339 /* clearing whole buffer */
340 const GLuint n
= xrb
->Base
.Width
* xrb
->Base
.Height
;
341 GLuint
*ptr4
= (GLuint
*) xrb
->ximage
->data
;
344 _mesa_memset(ptr4
, pixel
, 4 * n
);
348 for (i
= 0; i
< n
; i
++)
353 /* clearing scissored region */
355 for (j
= 0; j
< height
; j
++) {
356 GLuint
*ptr4
= PIXEL_ADDR4(xrb
, x
, y
+ j
);
357 for (i
= 0; i
< width
; i
++) {
366 clear_nbit_ximage(GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
367 GLint x
, GLint y
, GLint width
, GLint height
)
369 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
370 XMesaImage
*img
= xrb
->ximage
;
373 /* TODO: optimize this */
375 for (j
= 0; j
< height
; j
++) {
376 for (i
= 0; i
< width
; i
++) {
377 XMesaPutPixel(img
, x
+i
, y
-j
, xmesa
->clearpixel
);
385 clear_buffers( GLcontext
*ctx
, GLbitfield mask
,
386 GLboolean all
, GLint xFoo
, GLint yFoo
,
387 GLint widthFoo
, GLint heightFoo
)
389 if (ctx
->DrawBuffer
->Name
== 0) {
390 /* this is a window system framebuffer */
391 const GLuint
*colorMask
= (GLuint
*) &ctx
->Color
.ColorMask
;
392 XMesaBuffer b
= XMESA_BUFFER(ctx
->DrawBuffer
);
393 const GLint x
= ctx
->DrawBuffer
->_Xmin
;
394 const GLint y
= ctx
->DrawBuffer
->_Ymin
;
395 const GLint width
= ctx
->DrawBuffer
->_Xmax
- x
;
396 const GLint height
= ctx
->DrawBuffer
->_Ymax
- y
;
398 /* we can't handle color or index masking */
399 if (*colorMask
== 0xffffffff && ctx
->Color
.IndexMask
== 0xffffffff) {
400 if (mask
& BUFFER_BIT_FRONT_LEFT
) {
401 /* clear front color buffer */
402 struct gl_renderbuffer
*frontRb
403 = ctx
->DrawBuffer
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
;
404 if (b
->frontxrb
== xmesa_renderbuffer(frontRb
)) {
405 /* renderbuffer is not wrapped - great! */
406 b
->frontxrb
->clearFunc(ctx
, b
->frontxrb
, x
, y
, width
, height
);
407 mask
&= ~BUFFER_BIT_FRONT_LEFT
;
410 /* we can't directly clear an alpha-wrapped color buffer */
413 if (mask
& BUFFER_BIT_BACK_LEFT
) {
414 /* clear back color buffer */
415 struct gl_renderbuffer
*backRb
416 = ctx
->DrawBuffer
->Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
;
417 if (b
->backxrb
== xmesa_renderbuffer(backRb
)) {
418 /* renderbuffer is not wrapped - great! */
419 b
->backxrb
->clearFunc(ctx
, b
->backxrb
, x
, y
, width
, height
);
420 mask
&= ~BUFFER_BIT_BACK_LEFT
;
426 _swrast_Clear( ctx
, mask
, 0, 0, 0, 0, 0);
430 #ifndef XFree86Server
431 /* XXX this was never tested in the Xserver environment */
434 * This function implements glDrawPixels() with an XPutImage call when
435 * drawing to the front buffer (X Window drawable).
436 * The image format must be GL_BGRA to match the PF_8R8G8B pixel format.
439 xmesa_DrawPixels_8R8G8B( GLcontext
*ctx
,
440 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
441 GLenum format
, GLenum type
,
442 const struct gl_pixelstore_attrib
*unpack
,
443 const GLvoid
*pixels
)
445 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
446 struct gl_renderbuffer
*rb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[0][0];
447 struct xmesa_renderbuffer
*xrb
= (struct xmesa_renderbuffer
*) rb
->Wrapped
;
449 if (swrast
->NewState
)
450 _swrast_validate_derived( ctx
);
452 if (ctx
->DrawBuffer
->Name
== 0 &&
454 type
== GL_UNSIGNED_BYTE
&&
455 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
456 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
457 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
458 ctx
->Pixel
.ZoomY
== 1.0 &&
460 xrb
->Base
.AlphaBits
== 0)
462 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
463 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
464 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
465 const XMesaGC gc
= xmbuf
->gc
;
470 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
472 ASSERT(xmesa
->xm_visual
->dithered_pf
== PF_8R8G8B
);
473 ASSERT(xmesa
->xm_visual
->undithered_pf
== PF_8R8G8B
);
477 if (unpack
->BufferObj
->Name
) {
478 /* unpack from PBO */
480 if (!_mesa_validate_pbo_access(2, unpack
, width
, height
, 1,
481 format
, type
, pixels
)) {
482 _mesa_error(ctx
, GL_INVALID_OPERATION
,
483 "glDrawPixels(invalid PBO access)");
486 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
,
487 GL_PIXEL_UNPACK_BUFFER_EXT
,
491 /* buffer is already mapped - that's an error */
492 _mesa_error(ctx
, GL_INVALID_OPERATION
,
493 "glDrawPixels(PBO is mapped)");
496 pixels
= ADD_POINTERS(buf
, pixels
);
499 if (_mesa_clip_drawpixels(ctx
, &dstX
, &dstY
, &w
, &h
, &clippedUnpack
)) {
500 /* This is a little tricky since all coordinates up to now have
501 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
502 * so we have to carefully compute the Y coordinates/addresses here.
504 int srcX
= clippedUnpack
.SkipPixels
;
505 int srcY
= clippedUnpack
.SkipRows
;
506 int rowLength
= clippedUnpack
.RowLength
;
508 MEMSET(&ximage
, 0, sizeof(XMesaImage
));
509 ximage
.width
= width
;
510 ximage
.height
= height
;
511 ximage
.format
= ZPixmap
;
512 ximage
.data
= (char *) pixels
513 + ((srcY
+ h
- 1) * rowLength
+ srcX
) * 4;
514 ximage
.byte_order
= LSBFirst
;
515 ximage
.bitmap_unit
= 32;
516 ximage
.bitmap_bit_order
= LSBFirst
;
517 ximage
.bitmap_pad
= 32;
519 ximage
.bytes_per_line
= -rowLength
* 4; /* negative to flip image */
520 ximage
.bits_per_pixel
= 32;
521 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
522 /* flip Y axis for dest position */
523 dstY
= YFLIP(xrb
, dstY
) - h
+ 1;
524 XPutImage(dpy
, xrb
->pixmap
, gc
, &ximage
, 0, 0, dstX
, dstY
, w
, h
);
527 if (unpack
->BufferObj
->Name
) {
528 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
533 /* software fallback */
534 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
535 format
, type
, unpack
, pixels
);
542 * This function implements glDrawPixels() with an XPutImage call when
543 * drawing to the front buffer (X Window drawable). The image format
544 * must be GL_RGB and image type must be GL_UNSIGNED_SHORT_5_6_5 to
545 * match the PF_5R6G5B pixel format.
548 xmesa_DrawPixels_5R6G5B( GLcontext
*ctx
,
549 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
550 GLenum format
, GLenum type
,
551 const struct gl_pixelstore_attrib
*unpack
,
552 const GLvoid
*pixels
)
554 struct xmesa_renderbuffer
*xrb
555 = (struct xmesa_renderbuffer
*) ctx
->DrawBuffer
->_ColorDrawBuffers
[0][0];
556 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
557 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
558 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
559 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
560 const XMesaGC gc
= xmbuf
->gc
;
564 ASSERT(xmesa
->xm_visual
->undithered_pf
== PF_5R6G5B
);
566 if (swrast
->NewState
)
567 _swrast_validate_derived( ctx
);
571 type
== GL_UNSIGNED_SHORT_5_6_5
&&
572 !ctx
->Color
.DitherFlag
&& /* no dithering */
573 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
574 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
575 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
576 ctx
->Pixel
.ZoomY
== 1.0) {
581 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
583 if (unpack
->BufferObj
->Name
) {
584 /* unpack from PBO */
586 if (!_mesa_validate_pbo_access(2, unpack
, width
, height
, 1,
587 format
, type
, pixels
)) {
588 _mesa_error(ctx
, GL_INVALID_OPERATION
,
589 "glDrawPixels(invalid PBO access)");
592 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
,
593 GL_PIXEL_UNPACK_BUFFER_EXT
,
597 /* buffer is already mapped - that's an error */
598 _mesa_error(ctx
, GL_INVALID_OPERATION
,
599 "glDrawPixels(PBO is mapped)");
602 pixels
= ADD_POINTERS(buf
, pixels
);
605 if (_mesa_clip_drawpixels(ctx
, &dstX
, &dstY
, &w
, &h
, &clippedUnpack
)) {
606 /* This is a little tricky since all coordinates up to now have
607 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
608 * so we have to carefully compute the Y coordinates/addresses here.
610 int srcX
= clippedUnpack
.SkipPixels
;
611 int srcY
= clippedUnpack
.SkipRows
;
612 int rowLength
= clippedUnpack
.RowLength
;
614 MEMSET(&ximage
, 0, sizeof(XMesaImage
));
615 ximage
.width
= width
;
616 ximage
.height
= height
;
617 ximage
.format
= ZPixmap
;
618 ximage
.data
= (char *) pixels
619 + ((srcY
+ h
- 1) * rowLength
+ srcX
) * 2;
620 ximage
.byte_order
= LSBFirst
;
621 ximage
.bitmap_unit
= 16;
622 ximage
.bitmap_bit_order
= LSBFirst
;
623 ximage
.bitmap_pad
= 16;
625 ximage
.bytes_per_line
= -rowLength
* 2; /* negative to flip image */
626 ximage
.bits_per_pixel
= 16;
627 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
628 /* flip Y axis for dest position */
629 dstY
= YFLIP(xrb
, dstY
) - h
+ 1;
630 XPutImage(dpy
, xrb
->pixmap
, gc
, &ximage
, 0, 0, dstX
, dstY
, w
, h
);
633 if (unpack
->BufferObj
->Name
) {
634 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
639 /* software fallback */
640 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
641 format
, type
, unpack
, pixels
);
648 * Implement glCopyPixels for the front color buffer (or back buffer Pixmap)
649 * for the color buffer. Don't support zooming, pixel transfer, etc.
650 * We do support copying from one window to another, ala glXMakeCurrentRead.
653 xmesa_CopyPixels( GLcontext
*ctx
,
654 GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
655 GLint destx
, GLint desty
, GLenum type
)
657 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
658 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
659 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
660 const XMesaGC gc
= ((XMesaBuffer
) ctx
->DrawBuffer
)->gc
;
661 struct xmesa_renderbuffer
*srcXrb
= (struct xmesa_renderbuffer
*)
662 ctx
->ReadBuffer
->_ColorReadBuffer
;
663 struct xmesa_renderbuffer
*dstXrb
= (struct xmesa_renderbuffer
*)
664 ctx
->DrawBuffer
->_ColorDrawBuffers
[0][0];
669 if (swrast
->NewState
)
670 _swrast_validate_derived( ctx
);
672 if (ctx
->Color
.DrawBuffer
[0] == GL_FRONT
&&
673 ctx
->Pixel
.ReadBuffer
== GL_FRONT
&&
677 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
678 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
679 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
680 ctx
->Pixel
.ZoomY
== 1.0) {
681 /* Note: we don't do any special clipping work here. We could,
682 * but X will do it for us.
684 srcy
= YFLIP(srcXrb
, srcy
) - height
+ 1;
685 desty
= YFLIP(dstXrb
, desty
) - height
+ 1;
686 XCopyArea(dpy
, srcXrb
->pixmap
, dstXrb
->pixmap
, gc
,
687 srcx
, srcy
, width
, height
, destx
, desty
);
690 _swrast_CopyPixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
, type
);
693 #endif /* XFree86Server */
698 * Every driver should implement a GetString function in order to
699 * return a meaningful GL_RENDERER string.
701 static const GLubyte
*
702 get_string( GLcontext
*ctx
, GLenum name
)
708 return (const GLubyte
*) "Mesa GLX Indirect";
710 return (const GLubyte
*) "Mesa X11";
714 return (const GLubyte
*) "Mesa project: www.mesa3d.org";
725 * We implement the glEnable function only because we care about
726 * dither enable/disable.
729 enable( GLcontext
*ctx
, GLenum pname
, GLboolean state
)
731 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
736 xmesa
->pixelformat
= xmesa
->xm_visual
->dithered_pf
;
738 xmesa
->pixelformat
= xmesa
->xm_visual
->undithered_pf
;
741 ; /* silence compiler warning */
747 clear_color_HPCR_ximage( GLcontext
*ctx
, const GLfloat color
[4] )
750 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
752 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[0], color
[0]);
753 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[1], color
[1]);
754 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[2], color
[2]);
755 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[3], color
[3]);
757 if (color
[0] == 0.0 && color
[1] == 0.0 && color
[2] == 0.0) {
759 MEMSET( xmesa
->xm_visual
->hpcr_clear_ximage_pattern
, 0x0 ,
760 sizeof(xmesa
->xm_visual
->hpcr_clear_ximage_pattern
));
763 /* build clear pattern */
764 for (i
=0; i
<16; i
++) {
765 xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0][i
] =
767 xmesa
->clearcolor
[0],
768 xmesa
->clearcolor
[1],
769 xmesa
->clearcolor
[2]);
770 xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[1][i
] =
772 xmesa
->clearcolor
[0],
773 xmesa
->clearcolor
[1],
774 xmesa
->clearcolor
[2]);
781 clear_color_HPCR_pixmap( GLcontext
*ctx
, const GLfloat color
[4] )
784 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
786 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[0], color
[0]);
787 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[1], color
[1]);
788 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[2], color
[2]);
789 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[3], color
[3]);
791 if (color
[0] == 0.0 && color
[1] == 0.0 && color
[2] == 0.0) {
793 for (i
=0; i
<16; i
++) {
794 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 0, 0);
795 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 1, 0);
799 for (i
=0; i
<16; i
++) {
800 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 0,
802 xmesa
->clearcolor
[0],
803 xmesa
->clearcolor
[1],
804 xmesa
->clearcolor
[2]));
805 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 1,
807 xmesa
->clearcolor
[0],
808 xmesa
->clearcolor
[1],
809 xmesa
->clearcolor
[2]));
812 /* change tile pixmap content */
813 XMesaPutImage(xmesa
->display
,
814 (XMesaDrawable
)xmesa
->xm_visual
->hpcr_clear_pixmap
,
815 XMESA_BUFFER(ctx
->DrawBuffer
)->cleargc
,
816 xmesa
->xm_visual
->hpcr_clear_ximage
, 0, 0, 0, 0, 16, 2);
821 * Called when the driver should update its state, based on the new_state
825 xmesa_update_state( GLcontext
*ctx
, GLbitfield new_state
)
827 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
829 /* Propagate statechange information to swrast and swrast_setup
830 * modules. The X11 driver has no internal GL-dependent state.
832 _swrast_InvalidateState( ctx
, new_state
);
833 _tnl_InvalidateState( ctx
, new_state
);
834 _vbo_InvalidateState( ctx
, new_state
);
835 _swsetup_InvalidateState( ctx
, new_state
);
837 if (ctx
->DrawBuffer
->Name
!= 0)
841 * GL_DITHER, GL_READ/DRAW_BUFFER, buffer binding state, etc. effect
842 * renderbuffer span/clear funcs.
844 if (new_state
& (_NEW_COLOR
| _NEW_PIXEL
| _NEW_BUFFERS
)) {
845 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
846 struct xmesa_renderbuffer
*front_xrb
, *back_xrb
;
848 front_xrb
= xmbuf
->frontxrb
;
850 xmesa_set_renderbuffer_funcs(front_xrb
, xmesa
->pixelformat
,
851 xmesa
->xm_visual
->BitsPerPixel
);
852 front_xrb
->clearFunc
= clear_pixmap
;
855 back_xrb
= xmbuf
->backxrb
;
857 xmesa_set_renderbuffer_funcs(back_xrb
, xmesa
->pixelformat
,
858 xmesa
->xm_visual
->BitsPerPixel
);
859 if (xmbuf
->backxrb
->pixmap
) {
860 back_xrb
->clearFunc
= clear_pixmap
;
863 switch (xmesa
->xm_visual
->BitsPerPixel
) {
865 if (xmesa
->xm_visual
->hpcr_clear_flag
) {
866 back_xrb
->clearFunc
= clear_HPCR_ximage
;
869 back_xrb
->clearFunc
= clear_8bit_ximage
;
873 back_xrb
->clearFunc
= clear_16bit_ximage
;
876 back_xrb
->clearFunc
= clear_24bit_ximage
;
879 back_xrb
->clearFunc
= clear_32bit_ximage
;
882 back_xrb
->clearFunc
= clear_nbit_ximage
;
889 if (xmesa
->xm_visual
->hpcr_clear_flag
) {
890 /* this depends on whether we're drawing to the front or back buffer */
894 ctx
->Driver
.ClearColor
= clear_color_HPCR_pixmap
;
897 ctx
->Driver
.ClearColor
= clear_color_HPCR_ximage
;
900 (void) clear_color_HPCR_pixmap
;
901 (void) clear_color_HPCR_ximage
;
909 * Called via ctx->Driver.TestProxyTeximage(). Normally, we'd just use
910 * the _mesa_test_proxy_teximage() fallback function, but we're going to
911 * special-case the 3D texture case to allow textures up to 512x512x32
915 test_proxy_teximage(GLcontext
*ctx
, GLenum target
, GLint level
,
916 GLint internalFormat
, GLenum format
, GLenum type
,
917 GLint width
, GLint height
, GLint depth
, GLint border
)
919 if (target
== GL_PROXY_TEXTURE_3D
) {
920 /* special case for 3D textures */
921 if (width
* height
* depth
> 512 * 512 * 64 ||
922 width
< 2 * border
||
923 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
924 _mesa_bitcount(width
- 2 * border
) != 1) ||
925 height
< 2 * border
||
926 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
927 _mesa_bitcount(height
- 2 * border
) != 1) ||
928 depth
< 2 * border
||
929 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
930 _mesa_bitcount(depth
- 2 * border
) != 1)) {
931 /* Bad size, or too many texels */
937 /* use the fallback routine for 1D, 2D, cube and rect targets */
938 return _mesa_test_proxy_teximage(ctx
, target
, level
, internalFormat
,
939 format
, type
, width
, height
, depth
,
946 * In SW, we don't really compress GL_COMPRESSED_RGB[A] textures!
948 static const struct gl_texture_format
*
949 choose_tex_format( GLcontext
*ctx
, GLint internalFormat
,
950 GLenum format
, GLenum type
)
952 switch (internalFormat
) {
953 case GL_COMPRESSED_RGB_ARB
:
954 return &_mesa_texformat_rgb
;
955 case GL_COMPRESSED_RGBA_ARB
:
956 return &_mesa_texformat_rgba
;
958 return _mesa_choose_tex_format(ctx
, internalFormat
, format
, type
);
964 * Called by glViewport.
965 * This is a good time for us to poll the current X window size and adjust
966 * our renderbuffers to match the current window size.
967 * Remember, we have no opportunity to respond to conventional
968 * X Resize/StructureNotify events since the X driver has no event loop.
970 * Note that this trick isn't fool-proof. If the application never calls
971 * glViewport, our notion of the current window size may be incorrect.
972 * That problem led to the GLX_MESA_resize_buffers extension.
975 xmesa_viewport(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei w
, GLsizei h
)
977 XMesaContext xmctx
= XMESA_CONTEXT(ctx
);
978 XMesaBuffer xmdrawbuf
= XMESA_BUFFER(ctx
->WinSysDrawBuffer
);
979 XMesaBuffer xmreadbuf
= XMESA_BUFFER(ctx
->WinSysReadBuffer
);
980 xmesa_check_and_update_buffer_size(xmctx
, xmdrawbuf
);
981 xmesa_check_and_update_buffer_size(xmctx
, xmreadbuf
);
989 #if ENABLE_EXT_timer_query
992 * The GL_EXT_timer_query extension is not enabled for the XServer
993 * indirect renderer. Not sure about how/if wrapping of gettimeofday()
997 struct xmesa_query_object
999 struct gl_query_object Base
;
1000 struct timeval StartTime
;
1004 static struct gl_query_object
*
1005 xmesa_new_query_object(GLcontext
*ctx
, GLuint id
)
1007 struct xmesa_query_object
*q
= CALLOC_STRUCT(xmesa_query_object
);
1010 q
->Base
.Ready
= GL_TRUE
;
1017 xmesa_begin_query(GLcontext
*ctx
, GLenum target
, struct gl_query_object
*q
)
1019 if (target
== GL_TIME_ELAPSED_EXT
) {
1020 struct xmesa_query_object
*xq
= (struct xmesa_query_object
*) q
;
1021 (void) gettimeofday(&xq
->StartTime
, NULL
);
1027 * Return the difference between the two given times in microseconds.
1030 #define suseconds_t unsigned int
1033 time_diff(const struct timeval
*t0
, const struct timeval
*t1
)
1035 GLuint64EXT seconds0
= t0
->tv_sec
& 0xff; /* 0 .. 255 seconds */
1036 GLuint64EXT seconds1
= t1
->tv_sec
& 0xff; /* 0 .. 255 seconds */
1037 GLuint64EXT nanosec0
= (seconds0
* 1000000 + t0
->tv_usec
) * 1000;
1038 GLuint64EXT nanosec1
= (seconds1
* 1000000 + t1
->tv_usec
) * 1000;
1039 return nanosec1
- nanosec0
;
1044 xmesa_end_query(GLcontext
*ctx
, GLenum target
, struct gl_query_object
*q
)
1046 if (target
== GL_TIME_ELAPSED_EXT
) {
1047 struct xmesa_query_object
*xq
= (struct xmesa_query_object
*) q
;
1048 struct timeval endTime
;
1049 (void) gettimeofday(&endTime
, NULL
);
1050 /* result is in nanoseconds! */
1051 q
->Result
= time_diff(&xq
->StartTime
, &endTime
);
1056 #endif /* ENABLE_timer_query */
1060 * Initialize the device driver function table with the functions
1061 * we implement in this driver.
1064 xmesa_init_driver_functions( XMesaVisual xmvisual
,
1065 struct dd_function_table
*driver
)
1067 driver
->GetString
= get_string
;
1068 driver
->UpdateState
= xmesa_update_state
;
1069 driver
->GetBufferSize
= NULL
; /* OBSOLETE */
1070 driver
->Flush
= finish_or_flush
;
1071 driver
->Finish
= finish_or_flush
;
1072 driver
->ClearIndex
= clear_index
;
1073 driver
->ClearColor
= clear_color
;
1074 driver
->IndexMask
= index_mask
;
1075 driver
->ColorMask
= color_mask
;
1076 driver
->Enable
= enable
;
1077 driver
->Clear
= clear_buffers
;
1078 driver
->Viewport
= xmesa_viewport
;
1079 #ifndef XFree86Server
1080 driver
->CopyPixels
= xmesa_CopyPixels
;
1081 if (xmvisual
->undithered_pf
== PF_8R8G8B
&&
1082 xmvisual
->dithered_pf
== PF_8R8G8B
) {
1083 driver
->DrawPixels
= xmesa_DrawPixels_8R8G8B
;
1085 else if (xmvisual
->undithered_pf
== PF_5R6G5B
) {
1086 driver
->DrawPixels
= xmesa_DrawPixels_5R6G5B
;
1089 driver
->TestProxyTexImage
= test_proxy_teximage
;
1090 #if ENABLE_EXT_texure_compression_s3tc
1091 driver
->ChooseTextureFormat
= choose_tex_format
;
1093 (void) choose_tex_format
;
1096 #if ENABLE_EXT_timer_query
1097 driver
->NewQueryObject
= xmesa_new_query_object
;
1098 driver
->BeginQuery
= xmesa_begin_query
;
1099 driver
->EndQuery
= xmesa_end_query
;
1104 #define XMESA_NEW_POINT (_NEW_POINT | \
1106 _SWRAST_NEW_RASTERMASK)
1108 #define XMESA_NEW_LINE (_NEW_LINE | \
1113 _SWRAST_NEW_RASTERMASK)
1115 #define XMESA_NEW_TRIANGLE (_NEW_POLYGON | \
1120 _SWRAST_NEW_RASTERMASK)
1124 * Extend the software rasterizer with our line/point/triangle
1126 * Called during context creation only.
1128 void xmesa_register_swrast_functions( GLcontext
*ctx
)
1130 SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
1132 swrast
->choose_point
= xmesa_choose_point
;
1133 swrast
->choose_line
= xmesa_choose_line
;
1134 swrast
->choose_triangle
= xmesa_choose_triangle
;
1136 /* XXX these lines have no net effect. Remove??? */
1137 swrast
->InvalidatePointMask
|= XMESA_NEW_POINT
;
1138 swrast
->InvalidateLineMask
|= XMESA_NEW_LINE
;
1139 swrast
->InvalidateTriangleMask
|= XMESA_NEW_TRIANGLE
;