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"
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 these functions haven't been tested in the Xserver environment */
427 * Check if we can do an optimized glDrawPixels into an 8R8G8B visual.
430 can_do_DrawPixels_8R8G8B(GLcontext
*ctx
, GLenum format
, GLenum type
)
432 if (format
== GL_BGRA
&&
433 type
== GL_UNSIGNED_BYTE
&&
435 ctx
->DrawBuffer
->Name
== 0 &&
436 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
437 ctx
->Pixel
.ZoomY
== 1.0 &&
438 ctx
->_ImageTransferState
== 0 /* no color tables, scale/bias, etc */) {
439 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
441 if (swrast
->NewState
)
442 _swrast_validate_derived( ctx
);
444 if ((swrast
->_RasterMask
& ~CLIP_BIT
) == 0) /* no blend, z-test, etc */ {
445 struct gl_renderbuffer
*rb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[0];
447 struct xmesa_renderbuffer
*xrb
= xmesa_renderbuffer(rb
->Wrapped
);
449 xrb
->pixmap
&& /* drawing to pixmap or window */
450 xrb
->Base
.AlphaBits
== 0) {
461 * This function implements glDrawPixels() with an XPutImage call when
462 * drawing to the front buffer (X Window drawable).
463 * The image format must be GL_BGRA to match the PF_8R8G8B pixel format.
466 xmesa_DrawPixels_8R8G8B( GLcontext
*ctx
,
467 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
468 GLenum format
, GLenum type
,
469 const struct gl_pixelstore_attrib
*unpack
,
470 const GLvoid
*pixels
)
472 if (can_do_DrawPixels_8R8G8B(ctx
, format
, type
)) {
473 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
474 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
480 if (swrast
->NewState
)
481 _swrast_validate_derived( ctx
);
483 if (unpack
->BufferObj
->Name
) {
484 /* unpack from PBO */
486 if (!_mesa_validate_pbo_access(2, unpack
, width
, height
, 1,
487 format
, type
, pixels
)) {
488 _mesa_error(ctx
, GL_INVALID_OPERATION
,
489 "glDrawPixels(invalid PBO access)");
492 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
,
493 GL_PIXEL_UNPACK_BUFFER_EXT
,
497 /* buffer is already mapped - that's an error */
498 _mesa_error(ctx
, GL_INVALID_OPERATION
,
499 "glDrawPixels(PBO is mapped)");
502 pixels
= ADD_POINTERS(buf
, pixels
);
505 if (_mesa_clip_drawpixels(ctx
, &dstX
, &dstY
, &w
, &h
, &clippedUnpack
)) {
506 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
507 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
508 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
509 const XMesaGC gc
= xmbuf
->cleargc
; /* effected by glColorMask */
510 struct xmesa_renderbuffer
*xrb
511 = xmesa_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]->Wrapped
);
512 const int srcX
= clippedUnpack
.SkipPixels
;
513 const int srcY
= clippedUnpack
.SkipRows
;
514 const int rowLength
= clippedUnpack
.RowLength
;
517 ASSERT(xmesa
->xm_visual
->dithered_pf
== PF_8R8G8B
);
518 ASSERT(xmesa
->xm_visual
->undithered_pf
== PF_8R8G8B
);
522 /* This is a little tricky since all coordinates up to now have
523 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
524 * so we have to carefully compute the Y coordinates/addresses here.
526 MEMSET(&ximage
, 0, sizeof(XMesaImage
));
527 ximage
.width
= width
;
528 ximage
.height
= height
;
529 ximage
.format
= ZPixmap
;
530 ximage
.data
= (char *) pixels
531 + ((srcY
+ h
- 1) * rowLength
+ srcX
) * 4;
532 ximage
.byte_order
= LSBFirst
;
533 ximage
.bitmap_unit
= 32;
534 ximage
.bitmap_bit_order
= LSBFirst
;
535 ximage
.bitmap_pad
= 32;
537 ximage
.bits_per_pixel
= 32;
538 ximage
.bytes_per_line
= -rowLength
* 4; /* negative to flip image */
539 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
540 /* flip Y axis for dest position */
541 dstY
= YFLIP(xrb
, dstY
) - h
+ 1;
542 XPutImage(dpy
, xrb
->pixmap
, gc
, &ximage
, 0, 0, dstX
, dstY
, w
, h
);
545 if (unpack
->BufferObj
->Name
) {
546 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
551 /* software fallback */
552 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
553 format
, type
, unpack
, pixels
);
560 * Check if we can do an optimized glDrawPixels into an 5R6G5B visual.
563 can_do_DrawPixels_5R6G5B(GLcontext
*ctx
, GLenum format
, GLenum type
)
565 if (format
== GL_RGB
&&
566 type
== GL_UNSIGNED_SHORT_5_6_5
&&
567 !ctx
->Color
.DitherFlag
&& /* no dithering */
569 ctx
->DrawBuffer
->Name
== 0 &&
570 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
571 ctx
->Pixel
.ZoomY
== 1.0 &&
572 ctx
->_ImageTransferState
== 0 /* no color tables, scale/bias, etc */) {
573 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
575 if (swrast
->NewState
)
576 _swrast_validate_derived( ctx
);
578 if ((swrast
->_RasterMask
& ~CLIP_BIT
) == 0) /* no blend, z-test, etc */ {
579 struct gl_renderbuffer
*rb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[0];
581 struct xmesa_renderbuffer
*xrb
= xmesa_renderbuffer(rb
->Wrapped
);
583 xrb
->pixmap
&& /* drawing to pixmap or window */
584 xrb
->Base
.AlphaBits
== 0) {
595 * This function implements glDrawPixels() with an XPutImage call when
596 * drawing to the front buffer (X Window drawable). The image format
597 * must be GL_RGB and image type must be GL_UNSIGNED_SHORT_5_6_5 to
598 * match the PF_5R6G5B pixel format.
601 xmesa_DrawPixels_5R6G5B( GLcontext
*ctx
,
602 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
603 GLenum format
, GLenum type
,
604 const struct gl_pixelstore_attrib
*unpack
,
605 const GLvoid
*pixels
)
607 if (can_do_DrawPixels_5R6G5B(ctx
, format
, type
)) {
608 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
609 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
615 if (swrast
->NewState
)
616 _swrast_validate_derived( ctx
);
618 if (unpack
->BufferObj
->Name
) {
619 /* unpack from PBO */
621 if (!_mesa_validate_pbo_access(2, unpack
, width
, height
, 1,
622 format
, type
, pixels
)) {
623 _mesa_error(ctx
, GL_INVALID_OPERATION
,
624 "glDrawPixels(invalid PBO access)");
627 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
,
628 GL_PIXEL_UNPACK_BUFFER_EXT
,
632 /* buffer is already mapped - that's an error */
633 _mesa_error(ctx
, GL_INVALID_OPERATION
,
634 "glDrawPixels(PBO is mapped)");
637 pixels
= ADD_POINTERS(buf
, pixels
);
640 if (_mesa_clip_drawpixels(ctx
, &dstX
, &dstY
, &w
, &h
, &clippedUnpack
)) {
641 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
642 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
643 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
644 const XMesaGC gc
= xmbuf
->cleargc
; /* effected by glColorMask */
645 struct xmesa_renderbuffer
*xrb
646 = xmesa_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]->Wrapped
);
647 const int srcX
= clippedUnpack
.SkipPixels
;
648 const int srcY
= clippedUnpack
.SkipRows
;
649 const int rowLength
= clippedUnpack
.RowLength
;
652 ASSERT(xmesa
->xm_visual
->undithered_pf
== PF_5R6G5B
);
656 /* This is a little tricky since all coordinates up to now have
657 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
658 * so we have to carefully compute the Y coordinates/addresses here.
660 MEMSET(&ximage
, 0, sizeof(XMesaImage
));
661 ximage
.width
= width
;
662 ximage
.height
= height
;
663 ximage
.format
= ZPixmap
;
664 ximage
.data
= (char *) pixels
665 + ((srcY
+ h
- 1) * rowLength
+ srcX
) * 2;
666 ximage
.byte_order
= LSBFirst
;
667 ximage
.bitmap_unit
= 16;
668 ximage
.bitmap_bit_order
= LSBFirst
;
669 ximage
.bitmap_pad
= 16;
671 ximage
.bits_per_pixel
= 16;
672 ximage
.bytes_per_line
= -rowLength
* 2; /* negative to flip image */
673 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
674 /* flip Y axis for dest position */
675 dstY
= YFLIP(xrb
, dstY
) - h
+ 1;
676 XPutImage(dpy
, xrb
->pixmap
, gc
, &ximage
, 0, 0, dstX
, dstY
, w
, h
);
679 if (unpack
->BufferObj
->Name
) {
680 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
685 /* software fallback */
686 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
687 format
, type
, unpack
, pixels
);
693 * Determine if we can do an optimized glCopyPixels.
696 can_do_CopyPixels(GLcontext
*ctx
, GLenum type
)
698 if (type
== GL_COLOR
&&
699 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
700 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
701 ctx
->Pixel
.ZoomY
== 1.0 &&
702 ctx
->Color
.DrawBuffer
[0] == GL_FRONT
&& /* copy to front buf */
703 ctx
->Pixel
.ReadBuffer
== GL_FRONT
&& /* copy from front buf */
704 ctx
->ReadBuffer
->_ColorReadBuffer
&&
705 ctx
->DrawBuffer
->_ColorDrawBuffers
[0]) {
706 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
708 if (swrast
->NewState
)
709 _swrast_validate_derived( ctx
);
711 if ((swrast
->_RasterMask
& ~CLIP_BIT
) == 0x0 &&
713 ctx
->ReadBuffer
->_ColorReadBuffer
&&
715 ctx
->DrawBuffer
->_ColorDrawBuffers
[0]) {
716 struct xmesa_renderbuffer
*srcXrb
717 = xmesa_renderbuffer(ctx
->ReadBuffer
->_ColorReadBuffer
->Wrapped
);
718 struct xmesa_renderbuffer
*dstXrb
719 = xmesa_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]->Wrapped
);
720 if (srcXrb
->pixmap
&& dstXrb
->pixmap
) {
730 * Implement glCopyPixels for the front color buffer (or back buffer Pixmap)
731 * for the color buffer. Don't support zooming, pixel transfer, etc.
732 * We do support copying from one window to another, ala glXMakeCurrentRead.
735 xmesa_CopyPixels( GLcontext
*ctx
,
736 GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
737 GLint destx
, GLint desty
, GLenum type
)
739 if (can_do_CopyPixels(ctx
, type
)) {
740 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
741 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
742 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
743 const XMesaGC gc
= xmbuf
->cleargc
; /* effected by glColorMask */
744 struct xmesa_renderbuffer
*srcXrb
745 = xmesa_renderbuffer(ctx
->ReadBuffer
->_ColorReadBuffer
->Wrapped
);
746 struct xmesa_renderbuffer
*dstXrb
747 = xmesa_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]->Wrapped
);
752 /* Note: we don't do any special clipping work here. We could,
753 * but X will do it for us.
755 srcy
= YFLIP(srcXrb
, srcy
) - height
+ 1;
756 desty
= YFLIP(dstXrb
, desty
) - height
+ 1;
757 XCopyArea(dpy
, srcXrb
->pixmap
, dstXrb
->pixmap
, gc
,
758 srcx
, srcy
, width
, height
, destx
, desty
);
761 _swrast_CopyPixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
, type
);
765 #endif /* XFree86Server */
770 * Every driver should implement a GetString function in order to
771 * return a meaningful GL_RENDERER string.
773 static const GLubyte
*
774 get_string( GLcontext
*ctx
, GLenum name
)
780 return (const GLubyte
*) "Mesa GLX Indirect";
782 return (const GLubyte
*) "Mesa X11";
786 return (const GLubyte
*) "Mesa project: www.mesa3d.org";
797 * We implement the glEnable function only because we care about
798 * dither enable/disable.
801 enable( GLcontext
*ctx
, GLenum pname
, GLboolean state
)
803 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
808 xmesa
->pixelformat
= xmesa
->xm_visual
->dithered_pf
;
810 xmesa
->pixelformat
= xmesa
->xm_visual
->undithered_pf
;
813 ; /* silence compiler warning */
819 clear_color_HPCR_ximage( GLcontext
*ctx
, const GLfloat color
[4] )
822 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
824 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[0], color
[0]);
825 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[1], color
[1]);
826 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[2], color
[2]);
827 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[3], color
[3]);
829 if (color
[0] == 0.0 && color
[1] == 0.0 && color
[2] == 0.0) {
831 MEMSET( xmesa
->xm_visual
->hpcr_clear_ximage_pattern
, 0x0 ,
832 sizeof(xmesa
->xm_visual
->hpcr_clear_ximage_pattern
));
835 /* build clear pattern */
836 for (i
=0; i
<16; i
++) {
837 xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0][i
] =
839 xmesa
->clearcolor
[0],
840 xmesa
->clearcolor
[1],
841 xmesa
->clearcolor
[2]);
842 xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[1][i
] =
844 xmesa
->clearcolor
[0],
845 xmesa
->clearcolor
[1],
846 xmesa
->clearcolor
[2]);
853 clear_color_HPCR_pixmap( GLcontext
*ctx
, const GLfloat color
[4] )
856 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
858 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[0], color
[0]);
859 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[1], color
[1]);
860 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[2], color
[2]);
861 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[3], color
[3]);
863 if (color
[0] == 0.0 && color
[1] == 0.0 && color
[2] == 0.0) {
865 for (i
=0; i
<16; i
++) {
866 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 0, 0);
867 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 1, 0);
871 for (i
=0; i
<16; i
++) {
872 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 0,
874 xmesa
->clearcolor
[0],
875 xmesa
->clearcolor
[1],
876 xmesa
->clearcolor
[2]));
877 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 1,
879 xmesa
->clearcolor
[0],
880 xmesa
->clearcolor
[1],
881 xmesa
->clearcolor
[2]));
884 /* change tile pixmap content */
885 XMesaPutImage(xmesa
->display
,
886 (XMesaDrawable
)xmesa
->xm_visual
->hpcr_clear_pixmap
,
887 XMESA_BUFFER(ctx
->DrawBuffer
)->cleargc
,
888 xmesa
->xm_visual
->hpcr_clear_ximage
, 0, 0, 0, 0, 16, 2);
893 * Called when the driver should update its state, based on the new_state
897 xmesa_update_state( GLcontext
*ctx
, GLbitfield new_state
)
899 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
901 /* Propagate statechange information to swrast and swrast_setup
902 * modules. The X11 driver has no internal GL-dependent state.
904 _swrast_InvalidateState( ctx
, new_state
);
905 _tnl_InvalidateState( ctx
, new_state
);
906 _vbo_InvalidateState( ctx
, new_state
);
907 _swsetup_InvalidateState( ctx
, new_state
);
909 if (ctx
->DrawBuffer
->Name
!= 0)
913 * GL_DITHER, GL_READ/DRAW_BUFFER, buffer binding state, etc. effect
914 * renderbuffer span/clear funcs.
915 * Check _NEW_COLOR to detect dither enable/disable.
917 if (new_state
& (_NEW_COLOR
| _NEW_BUFFERS
)) {
918 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
919 struct xmesa_renderbuffer
*front_xrb
, *back_xrb
;
921 front_xrb
= xmbuf
->frontxrb
;
923 xmesa_set_renderbuffer_funcs(front_xrb
, xmesa
->pixelformat
,
924 xmesa
->xm_visual
->BitsPerPixel
);
925 front_xrb
->clearFunc
= clear_pixmap
;
928 back_xrb
= xmbuf
->backxrb
;
930 xmesa_set_renderbuffer_funcs(back_xrb
, xmesa
->pixelformat
,
931 xmesa
->xm_visual
->BitsPerPixel
);
932 if (xmbuf
->backxrb
->pixmap
) {
933 back_xrb
->clearFunc
= clear_pixmap
;
936 switch (xmesa
->xm_visual
->BitsPerPixel
) {
938 if (xmesa
->xm_visual
->hpcr_clear_flag
) {
939 back_xrb
->clearFunc
= clear_HPCR_ximage
;
942 back_xrb
->clearFunc
= clear_8bit_ximage
;
946 back_xrb
->clearFunc
= clear_16bit_ximage
;
949 back_xrb
->clearFunc
= clear_24bit_ximage
;
952 back_xrb
->clearFunc
= clear_32bit_ximage
;
955 back_xrb
->clearFunc
= clear_nbit_ximage
;
962 if (xmesa
->xm_visual
->hpcr_clear_flag
) {
963 /* this depends on whether we're drawing to the front or back buffer */
967 ctx
->Driver
.ClearColor
= clear_color_HPCR_pixmap
;
970 ctx
->Driver
.ClearColor
= clear_color_HPCR_ximage
;
973 (void) clear_color_HPCR_pixmap
;
974 (void) clear_color_HPCR_ximage
;
982 * Called via ctx->Driver.TestProxyTeximage(). Normally, we'd just use
983 * the _mesa_test_proxy_teximage() fallback function, but we're going to
984 * special-case the 3D texture case to allow textures up to 512x512x32
988 test_proxy_teximage(GLcontext
*ctx
, GLenum target
, GLint level
,
989 GLint internalFormat
, GLenum format
, GLenum type
,
990 GLint width
, GLint height
, GLint depth
, GLint border
)
992 if (target
== GL_PROXY_TEXTURE_3D
) {
993 /* special case for 3D textures */
994 if (width
* height
* depth
> 512 * 512 * 64 ||
995 width
< 2 * border
||
996 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
997 _mesa_bitcount(width
- 2 * border
) != 1) ||
998 height
< 2 * border
||
999 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1000 _mesa_bitcount(height
- 2 * border
) != 1) ||
1001 depth
< 2 * border
||
1002 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1003 _mesa_bitcount(depth
- 2 * border
) != 1)) {
1004 /* Bad size, or too many texels */
1010 /* use the fallback routine for 1D, 2D, cube and rect targets */
1011 return _mesa_test_proxy_teximage(ctx
, target
, level
, internalFormat
,
1012 format
, type
, width
, height
, depth
,
1019 * In SW, we don't really compress GL_COMPRESSED_RGB[A] textures!
1021 static const struct gl_texture_format
*
1022 choose_tex_format( GLcontext
*ctx
, GLint internalFormat
,
1023 GLenum format
, GLenum type
)
1025 switch (internalFormat
) {
1026 case GL_COMPRESSED_RGB_ARB
:
1027 return &_mesa_texformat_rgb
;
1028 case GL_COMPRESSED_RGBA_ARB
:
1029 return &_mesa_texformat_rgba
;
1031 return _mesa_choose_tex_format(ctx
, internalFormat
, format
, type
);
1037 * Called by glViewport.
1038 * This is a good time for us to poll the current X window size and adjust
1039 * our renderbuffers to match the current window size.
1040 * Remember, we have no opportunity to respond to conventional
1041 * X Resize/StructureNotify events since the X driver has no event loop.
1043 * Note that this trick isn't fool-proof. If the application never calls
1044 * glViewport, our notion of the current window size may be incorrect.
1045 * That problem led to the GLX_MESA_resize_buffers extension.
1048 xmesa_viewport(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei w
, GLsizei h
)
1050 XMesaContext xmctx
= XMESA_CONTEXT(ctx
);
1051 XMesaBuffer xmdrawbuf
= XMESA_BUFFER(ctx
->WinSysDrawBuffer
);
1052 XMesaBuffer xmreadbuf
= XMESA_BUFFER(ctx
->WinSysReadBuffer
);
1053 xmesa_check_and_update_buffer_size(xmctx
, xmdrawbuf
);
1054 xmesa_check_and_update_buffer_size(xmctx
, xmreadbuf
);
1062 #if ENABLE_EXT_timer_query
1065 * The GL_EXT_timer_query extension is not enabled for the XServer
1066 * indirect renderer. Not sure about how/if wrapping of gettimeofday()
1070 struct xmesa_query_object
1072 struct gl_query_object Base
;
1073 struct timeval StartTime
;
1077 static struct gl_query_object
*
1078 xmesa_new_query_object(GLcontext
*ctx
, GLuint id
)
1080 struct xmesa_query_object
*q
= CALLOC_STRUCT(xmesa_query_object
);
1083 q
->Base
.Ready
= GL_TRUE
;
1090 xmesa_begin_query(GLcontext
*ctx
, struct gl_query_object
*q
)
1092 if (q
->Target
== GL_TIME_ELAPSED_EXT
) {
1093 struct xmesa_query_object
*xq
= (struct xmesa_query_object
*) q
;
1094 (void) gettimeofday(&xq
->StartTime
, NULL
);
1100 * Return the difference between the two given times in microseconds.
1103 #define suseconds_t unsigned int
1106 time_diff(const struct timeval
*t0
, const struct timeval
*t1
)
1108 GLuint64EXT seconds0
= t0
->tv_sec
& 0xff; /* 0 .. 255 seconds */
1109 GLuint64EXT seconds1
= t1
->tv_sec
& 0xff; /* 0 .. 255 seconds */
1110 GLuint64EXT nanosec0
= (seconds0
* 1000000 + t0
->tv_usec
) * 1000;
1111 GLuint64EXT nanosec1
= (seconds1
* 1000000 + t1
->tv_usec
) * 1000;
1112 return nanosec1
- nanosec0
;
1117 xmesa_end_query(GLcontext
*ctx
, struct gl_query_object
*q
)
1119 if (q
->Target
== GL_TIME_ELAPSED_EXT
) {
1120 struct xmesa_query_object
*xq
= (struct xmesa_query_object
*) q
;
1121 struct timeval endTime
;
1122 (void) gettimeofday(&endTime
, NULL
);
1123 /* result is in nanoseconds! */
1124 q
->Result
= time_diff(&xq
->StartTime
, &endTime
);
1129 #endif /* ENABLE_timer_query */
1133 * Initialize the device driver function table with the functions
1134 * we implement in this driver.
1137 xmesa_init_driver_functions( XMesaVisual xmvisual
,
1138 struct dd_function_table
*driver
)
1140 driver
->GetString
= get_string
;
1141 driver
->UpdateState
= xmesa_update_state
;
1142 driver
->GetBufferSize
= NULL
; /* OBSOLETE */
1143 driver
->Flush
= finish_or_flush
;
1144 driver
->Finish
= finish_or_flush
;
1145 driver
->ClearIndex
= clear_index
;
1146 driver
->ClearColor
= clear_color
;
1147 driver
->IndexMask
= index_mask
;
1148 driver
->ColorMask
= color_mask
;
1149 driver
->Enable
= enable
;
1150 driver
->Clear
= clear_buffers
;
1151 driver
->Viewport
= xmesa_viewport
;
1152 #ifndef XFree86Server
1153 driver
->CopyPixels
= xmesa_CopyPixels
;
1154 if (xmvisual
->undithered_pf
== PF_8R8G8B
&&
1155 xmvisual
->dithered_pf
== PF_8R8G8B
&&
1156 xmvisual
->BitsPerPixel
== 32) {
1157 driver
->DrawPixels
= xmesa_DrawPixels_8R8G8B
;
1159 else if (xmvisual
->undithered_pf
== PF_5R6G5B
) {
1160 driver
->DrawPixels
= xmesa_DrawPixels_5R6G5B
;
1163 driver
->TestProxyTexImage
= test_proxy_teximage
;
1164 #if ENABLE_EXT_texure_compression_s3tc
1165 driver
->ChooseTextureFormat
= choose_tex_format
;
1167 (void) choose_tex_format
;
1170 #if ENABLE_EXT_timer_query
1171 driver
->NewQueryObject
= xmesa_new_query_object
;
1172 driver
->BeginQuery
= xmesa_begin_query
;
1173 driver
->EndQuery
= xmesa_end_query
;
1178 #define XMESA_NEW_POINT (_NEW_POINT | \
1180 _SWRAST_NEW_RASTERMASK)
1182 #define XMESA_NEW_LINE (_NEW_LINE | \
1187 _SWRAST_NEW_RASTERMASK)
1189 #define XMESA_NEW_TRIANGLE (_NEW_POLYGON | \
1194 _SWRAST_NEW_RASTERMASK)
1198 * Extend the software rasterizer with our line/point/triangle
1200 * Called during context creation only.
1202 void xmesa_register_swrast_functions( GLcontext
*ctx
)
1204 SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
1206 swrast
->choose_point
= xmesa_choose_point
;
1207 swrast
->choose_line
= xmesa_choose_line
;
1208 swrast
->choose_triangle
= xmesa_choose_triangle
;
1210 /* XXX these lines have no net effect. Remove??? */
1211 swrast
->InvalidatePointMask
|= XMESA_NEW_POINT
;
1212 swrast
->InvalidateLineMask
|= XMESA_NEW_LINE
;
1213 swrast
->InvalidateTriangleMask
|= XMESA_NEW_TRIANGLE
;