2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2005 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.
26 #include "glxheader.h"
27 #include "bufferobj.h"
33 #include "extensions.h"
34 #include "framebuffer.h"
43 #include "texformat.h"
45 #include "array_cache/acache.h"
46 #include "swrast/swrast.h"
47 #include "swrast/s_context.h"
48 #include "swrast_setup/swrast_setup.h"
50 #include "tnl/t_context.h"
53 #include <GL/glxtokens.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 * Return the size (width, height) of the X window for the given GLframebuffer.
94 * Output: width - width of buffer in pixels.
95 * height - height of buffer in pixels.
98 get_buffer_size( GLframebuffer
*buffer
, GLuint
*width
, GLuint
*height
)
100 /* We can do this cast because the first field in the XMesaBuffer
101 * struct is a GLframebuffer struct. If this weren't true, we'd
102 * need a pointer from the GLframebuffer to the XMesaBuffer.
104 const XMesaBuffer xmBuffer
= (XMesaBuffer
) buffer
;
105 unsigned int winwidth
, winheight
;
107 /* XFree86 GLX renderer */
108 winwidth
= MIN2(xmBuffer
->frontxrb
->drawable
->width
, MAX_WIDTH
);
109 winheight
= MIN2(xmBuffer
->frontxrb
->drawable
->height
, MAX_HEIGHT
);
116 _glthread_LOCK_MUTEX(_xmesa_lock
);
117 XSync(xmBuffer
->xm_visual
->display
, 0); /* added for Chromium */
119 stat
= XGetGeometry( xmBuffer
->xm_visual
->display
, xmBuffer
->frontxrb
->pixmap
,
120 &root
, &winx
, &winy
, &winwidth
, &winheight
, &bw
, &d
);
121 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
124 /* probably querying a window that's recently been destroyed */
125 _mesa_warning(NULL
, "XGetGeometry failed!\n");
126 *width
= *height
= 1;
137 finish_or_flush( GLcontext
*ctx
)
142 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
144 _glthread_LOCK_MUTEX(_xmesa_lock
);
145 XSync( xmesa
->display
, False
);
146 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
153 clear_index( GLcontext
*ctx
, GLuint index
)
155 if (ctx
->DrawBuffer
->Name
== 0) {
156 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
157 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
158 xmesa
->clearpixel
= (unsigned long) index
;
159 XMesaSetForeground( xmesa
->display
, xmbuf
->cleargc
, (unsigned long) index
);
165 clear_color( GLcontext
*ctx
, const GLfloat color
[4] )
167 if (ctx
->DrawBuffer
->Name
== 0) {
168 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
169 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
171 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[0], color
[0]);
172 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[1], color
[1]);
173 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[2], color
[2]);
174 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[3], color
[3]);
175 xmesa
->clearpixel
= xmesa_color_to_pixel( ctx
,
176 xmesa
->clearcolor
[0],
177 xmesa
->clearcolor
[1],
178 xmesa
->clearcolor
[2],
179 xmesa
->clearcolor
[3],
180 xmesa
->xm_visual
->undithered_pf
);
181 _glthread_LOCK_MUTEX(_xmesa_lock
);
182 XMesaSetForeground( xmesa
->display
, xmbuf
->cleargc
,
184 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
190 /* Set index mask ala glIndexMask */
192 index_mask( GLcontext
*ctx
, GLuint mask
)
194 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
195 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
196 /* not sure this conditional is really needed */
197 if (xmbuf
->backxrb
&& xmbuf
->backxrb
->pixmap
) {
199 if (mask
==0xffffffff) {
200 m
= ((unsigned long)~0L);
203 m
= (unsigned long) mask
;
205 XMesaSetPlaneMask( xmesa
->display
, xmbuf
->cleargc
, m
);
206 XMesaSetPlaneMask( xmesa
->display
, xmbuf
->gc
, m
);
211 /* Implements glColorMask() */
213 color_mask(GLcontext
*ctx
,
214 GLboolean rmask
, GLboolean gmask
, GLboolean bmask
, GLboolean amask
)
216 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
217 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
218 const int xclass
= xmesa
->xm_visual
->mesa_visual
.visualType
;
221 if (xclass
== GLX_TRUE_COLOR
|| xclass
== GLX_DIRECT_COLOR
) {
223 if (rmask
&& gmask
&& bmask
) {
224 m
= ((unsigned long)~0L);
228 if (rmask
) m
|= GET_REDMASK(xmesa
->xm_visual
);
229 if (gmask
) m
|= GET_GREENMASK(xmesa
->xm_visual
);
230 if (bmask
) m
|= GET_BLUEMASK(xmesa
->xm_visual
);
232 XMesaSetPlaneMask( xmesa
->display
, xmbuf
->cleargc
, m
);
233 XMesaSetPlaneMask( xmesa
->display
, xmbuf
->gc
, m
);
239 /**********************************************************************/
240 /*** glClear implementations ***/
241 /**********************************************************************/
245 * Clear the front or back color buffer, if it's implemented with a pixmap.
248 clear_pixmap(GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
, GLboolean all
,
249 GLint x
, GLint y
, GLint width
, GLint height
)
251 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
252 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
257 assert(xmesa
->display
);
259 assert(xmbuf
->cleargc
);
262 XMesaFillRectangle( xmesa
->display
, xrb
->pixmap
, xmbuf
->cleargc
,
263 0, 0, xrb
->Base
.Width
+ 1, xrb
->Base
.Height
+ 1 );
266 XMesaFillRectangle( xmesa
->display
, xrb
->pixmap
, xmbuf
->cleargc
,
267 x
, xrb
->Base
.Height
- y
- height
,
274 clear_8bit_ximage( GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
275 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
277 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
280 const size_t n
= xrb
->ximage
->bytes_per_line
* xrb
->Base
.Height
;
281 MEMSET( xrb
->ximage
->data
, xmesa
->clearpixel
, n
);
285 for (i
=0;i
<height
;i
++) {
286 GLubyte
*ptr
= PIXEL_ADDR1(xrb
, x
, y
+ i
);
287 MEMSET( ptr
, xmesa
->clearpixel
, width
);
294 clear_HPCR_ximage( GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
295 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
297 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
300 GLint i
, c16
= (xrb
->ximage
->bytes_per_line
>>4)<<4;
301 GLubyte
*ptr
= (GLubyte
*) xrb
->ximage
->data
;
302 for (i
= 0; i
< xrb
->Base
.Height
; i
++) {
304 GLubyte
*sptr
= xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0];
308 for (j
=0; j
<c16
; j
+=16) {
327 for (; j
< xrb
->ximage
->bytes_per_line
; j
++) {
335 for (i
=y
; i
<y
+height
; i
++) {
336 GLubyte
*ptr
= PIXEL_ADDR1( xrb
, x
, i
);
338 const GLubyte
*sptr
= xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0];
342 for (j
=x
; j
<x
+width
; j
++) {
352 clear_16bit_ximage( GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
353 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
355 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
356 register GLuint pixel
= (GLuint
) xmesa
->clearpixel
;
358 if (xmesa
->swapbytes
) {
359 pixel
= ((pixel
>> 8) & 0x00ff) | ((pixel
<< 8) & 0xff00);
363 GLuint
*ptr4
= (GLuint
*) xrb
->ximage
->data
;
364 if ((pixel
& 0xff) == ((pixel
>> 8) & 0xff)) {
365 /* low and high bytes are equal so use memset() */
366 const GLuint n
= xrb
->ximage
->bytes_per_line
* xrb
->Base
.Height
;
367 MEMSET( ptr4
, pixel
& 0xff, n
);
370 const GLuint n
= xrb
->ximage
->bytes_per_line
* xrb
->Base
.Height
/ 4;
372 pixel
= pixel
| (pixel
<<16);
373 for (i
= 0; i
< n
; i
++) {
377 /* might be one last GLushort to set */
378 if ((xrb
->ximage
->bytes_per_line
* xrb
->Base
.Height
) & 0x2)
379 *(GLushort
*)ptr4
= pixel
& 0xffff;
384 for (j
=0;j
<height
;j
++) {
385 GLushort
*ptr2
= PIXEL_ADDR2(xrb
, x
, y
+ j
);
386 for (i
=0;i
<width
;i
++) {
394 /* Optimized code provided by Nozomi Ytow <noz@xfree86.org> */
396 clear_24bit_ximage(GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
397 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
399 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
400 const GLubyte r
= xmesa
->clearcolor
[0];
401 const GLubyte g
= xmesa
->clearcolor
[1];
402 const GLubyte b
= xmesa
->clearcolor
[2];
405 if (r
== g
&& g
== b
) {
406 /* same value for all three components (gray) */
407 const GLint w3
= xrb
->Base
.Width
* 3;
408 const GLint h
= xrb
->Base
.Height
;
410 for (i
= 0; i
< h
; i
++) {
411 bgr_t
*ptr3
= PIXEL_ADDR3(xrb
, 0, i
);
417 const GLint w
= xrb
->Base
.Width
;
418 const GLint h
= xrb
->Base
.Height
;
420 for (i
= 0; i
< h
; i
++) {
421 bgr_t
*ptr3
= PIXEL_ADDR3(xrb
, 0, i
);
422 for (j
= 0; j
< w
; j
++) {
432 /* only clear subrect of color buffer */
433 if (r
== g
&& g
== b
) {
434 /* same value for all three components (gray) */
436 for (j
=0;j
<height
;j
++) {
437 bgr_t
*ptr3
= PIXEL_ADDR3(xrb
, x
, y
+ j
);
438 MEMSET(ptr3
, r
, 3 * width
);
442 /* non-gray clear color */
444 for (j
= 0; j
< height
; j
++) {
445 bgr_t
*ptr3
= PIXEL_ADDR3(xrb
, x
, y
+ j
);
446 for (i
= 0; i
< width
; i
++) {
459 clear_32bit_ximage(GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
460 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
462 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
463 register GLuint pixel
= (GLuint
) xmesa
->clearpixel
;
468 if (xmesa
->swapbytes
) {
469 pixel
= ((pixel
>> 24) & 0x000000ff)
470 | ((pixel
>> 8) & 0x0000ff00)
471 | ((pixel
<< 8) & 0x00ff0000)
472 | ((pixel
<< 24) & 0xff000000);
476 const GLuint n
= xrb
->Base
.Width
* xrb
->Base
.Height
;
477 GLuint
*ptr4
= (GLuint
*) xrb
->ximage
->data
;
479 _mesa_memset(ptr4
, pixel
, 4 * n
);
483 for (i
= 0; i
< n
; i
++)
489 for (j
= 0; j
< height
; j
++) {
490 GLuint
*ptr4
= PIXEL_ADDR4(xrb
, x
, y
+ j
);
491 for (i
= 0; i
< width
; i
++) {
500 clear_nbit_ximage(GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
501 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
503 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
504 XMesaImage
*img
= xrb
->ximage
;
507 /* We can ignore 'all' here - x, y, width, height are always right */
510 /* TODO: optimize this */
512 for (j
= 0; j
< height
; j
++) {
513 for (i
= 0; i
< width
; i
++) {
514 XMesaPutPixel(img
, x
+i
, y
-j
, xmesa
->clearpixel
);
522 clear_buffers( GLcontext
*ctx
, GLbitfield mask
,
523 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
525 if (ctx
->DrawBuffer
->Name
== 0) {
526 /* this is a window system framebuffer */
527 const GLuint
*colorMask
= (GLuint
*) &ctx
->Color
.ColorMask
;
528 XMesaBuffer b
= (XMesaBuffer
) ctx
->DrawBuffer
;
530 /* we can't handle color or index masking */
531 if (*colorMask
== 0xffffffff && ctx
->Color
.IndexMask
== 0xffffffff) {
532 if (mask
& BUFFER_BIT_FRONT_LEFT
) {
533 /* clear front color buffer */
534 if (b
->frontxrb
== (struct xmesa_renderbuffer
*)
535 ctx
->DrawBuffer
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
) {
536 /* renderbuffer is not wrapped - great! */
537 b
->frontxrb
->clearFunc(ctx
, b
->frontxrb
, all
, x
, y
,
539 mask
&= ~BUFFER_BIT_FRONT_LEFT
;
542 /* we can't directly clear an alpha-wrapped color buffer */
545 if (mask
& BUFFER_BIT_BACK_LEFT
) {
546 /* clear back color buffer */
547 if (b
->backxrb
== (struct xmesa_renderbuffer
*)
548 ctx
->DrawBuffer
->Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
) {
549 /* renderbuffer is not wrapped - great! */
550 b
->backxrb
->clearFunc(ctx
, b
->backxrb
, all
, x
, y
,
552 mask
&= ~BUFFER_BIT_BACK_LEFT
;
558 _swrast_Clear( ctx
, mask
, all
, x
, y
, width
, height
);
563 * Called by ctx->Driver.ResizeBuffers()
564 * Resize the front/back colorbuffers to match the latest window size.
567 xmesa_resize_buffers(GLcontext
*ctx
, GLframebuffer
*buffer
,
568 GLuint width
, GLuint height
)
570 /* We can do this cast because the first field in the XMesaBuffer
571 * struct is a GLframebuffer struct. If this weren't true, we'd
572 * need a pointer from the GLframebuffer to the XMesaBuffer.
574 XMesaBuffer xmBuffer
= (XMesaBuffer
) buffer
;
576 xmesa_alloc_back_buffer(xmBuffer
, width
, height
);
578 _mesa_resize_framebuffer(ctx
, buffer
, width
, height
);
582 #ifndef XFree86Server
583 /* XXX this was never tested in the Xserver environment */
586 * This function implements glDrawPixels() with an XPutImage call when
587 * drawing to the front buffer (X Window drawable).
588 * The image format must be GL_BGRA to match the PF_8R8G8B pixel format.
591 xmesa_DrawPixels_8R8G8B( GLcontext
*ctx
,
592 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
593 GLenum format
, GLenum type
,
594 const struct gl_pixelstore_attrib
*unpack
,
595 const GLvoid
*pixels
)
597 struct xmesa_renderbuffer
*xrb
598 = (struct xmesa_renderbuffer
*) ctx
->DrawBuffer
->_ColorDrawBuffers
[0][0];
600 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
601 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
602 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
603 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
604 const XMesaGC gc
= xmbuf
->gc
;
608 ASSERT(xmesa
->xm_visual
->dithered_pf
== PF_8R8G8B
);
609 ASSERT(xmesa
->xm_visual
->undithered_pf
== PF_8R8G8B
);
611 if (swrast
->NewState
)
612 _swrast_validate_derived( ctx
);
616 type
== GL_UNSIGNED_BYTE
&&
617 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
618 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
619 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
620 ctx
->Pixel
.ZoomY
== 1.0) {
625 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
627 if (unpack
->BufferObj
->Name
) {
628 /* unpack from PBO */
630 if (!_mesa_validate_pbo_access(2, unpack
, width
, height
, 1,
631 format
, type
, pixels
)) {
632 _mesa_error(ctx
, GL_INVALID_OPERATION
,
633 "glDrawPixels(invalid PBO access)");
636 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
,
637 GL_PIXEL_UNPACK_BUFFER_EXT
,
641 /* buffer is already mapped - that's an error */
642 _mesa_error(ctx
, GL_INVALID_OPERATION
,
643 "glDrawPixels(PBO is mapped)");
646 pixels
= ADD_POINTERS(buf
, pixels
);
649 if (_mesa_clip_drawpixels(ctx
, &dstX
, &dstY
, &w
, &h
, &clippedUnpack
)) {
650 /* This is a little tricky since all coordinates up to now have
651 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
652 * so we have to carefully compute the Y coordinates/addresses here.
654 int srcX
= clippedUnpack
.SkipPixels
;
655 int srcY
= clippedUnpack
.SkipRows
;
656 int rowLength
= clippedUnpack
.RowLength
;
658 MEMSET(&ximage
, 0, sizeof(XMesaImage
));
659 ximage
.width
= width
;
660 ximage
.height
= height
;
661 ximage
.format
= ZPixmap
;
662 ximage
.data
= (char *) pixels
663 + ((srcY
+ h
- 1) * rowLength
+ srcX
) * 4;
664 ximage
.byte_order
= LSBFirst
;
665 ximage
.bitmap_unit
= 32;
666 ximage
.bitmap_bit_order
= LSBFirst
;
667 ximage
.bitmap_pad
= 32;
669 ximage
.bytes_per_line
= -rowLength
* 4; /* negative to flip image */
670 ximage
.bits_per_pixel
= 32;
671 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
672 /* flip Y axis for dest position */
673 dstY
= YFLIP(xrb
, dstY
) - h
+ 1;
674 XPutImage(dpy
, xrb
->pixmap
, gc
, &ximage
, 0, 0, dstX
, dstY
, w
, h
);
677 if (unpack
->BufferObj
->Name
) {
678 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
683 /* software fallback */
684 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
685 format
, type
, unpack
, pixels
);
692 * This function implements glDrawPixels() with an XPutImage call when
693 * drawing to the front buffer (X Window drawable). The image format
694 * must be GL_RGB and image type must be GL_UNSIGNED_SHORT_5_6_5 to
695 * match the PF_5R6G5B pixel format.
698 xmesa_DrawPixels_5R6G5B( GLcontext
*ctx
,
699 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
700 GLenum format
, GLenum type
,
701 const struct gl_pixelstore_attrib
*unpack
,
702 const GLvoid
*pixels
)
704 struct xmesa_renderbuffer
*xrb
705 = (struct xmesa_renderbuffer
*) ctx
->DrawBuffer
->_ColorDrawBuffers
[0][0];
706 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
707 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
708 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
709 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
710 const XMesaGC gc
= xmbuf
->gc
;
714 ASSERT(xmesa
->xm_visual
->undithered_pf
== PF_5R6G5B
);
716 if (swrast
->NewState
)
717 _swrast_validate_derived( ctx
);
721 type
== GL_UNSIGNED_SHORT_5_6_5
&&
722 !ctx
->Color
.DitherFlag
&& /* no dithering */
723 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
724 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
725 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
726 ctx
->Pixel
.ZoomY
== 1.0) {
731 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
733 if (unpack
->BufferObj
->Name
) {
734 /* unpack from PBO */
736 if (!_mesa_validate_pbo_access(2, unpack
, width
, height
, 1,
737 format
, type
, pixels
)) {
738 _mesa_error(ctx
, GL_INVALID_OPERATION
,
739 "glDrawPixels(invalid PBO access)");
742 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
,
743 GL_PIXEL_UNPACK_BUFFER_EXT
,
747 /* buffer is already mapped - that's an error */
748 _mesa_error(ctx
, GL_INVALID_OPERATION
,
749 "glDrawPixels(PBO is mapped)");
752 pixels
= ADD_POINTERS(buf
, pixels
);
755 if (_mesa_clip_drawpixels(ctx
, &dstX
, &dstY
, &w
, &h
, &clippedUnpack
)) {
756 /* This is a little tricky since all coordinates up to now have
757 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
758 * so we have to carefully compute the Y coordinates/addresses here.
760 int srcX
= clippedUnpack
.SkipPixels
;
761 int srcY
= clippedUnpack
.SkipRows
;
762 int rowLength
= clippedUnpack
.RowLength
;
764 MEMSET(&ximage
, 0, sizeof(XMesaImage
));
765 ximage
.width
= width
;
766 ximage
.height
= height
;
767 ximage
.format
= ZPixmap
;
768 ximage
.data
= (char *) pixels
769 + ((srcY
+ h
- 1) * rowLength
+ srcX
) * 2;
770 ximage
.byte_order
= LSBFirst
;
771 ximage
.bitmap_unit
= 16;
772 ximage
.bitmap_bit_order
= LSBFirst
;
773 ximage
.bitmap_pad
= 16;
775 ximage
.bytes_per_line
= -rowLength
* 2; /* negative to flip image */
776 ximage
.bits_per_pixel
= 16;
777 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
778 /* flip Y axis for dest position */
779 dstY
= YFLIP(xrb
, dstY
) - h
+ 1;
780 XPutImage(dpy
, xrb
->pixmap
, gc
, &ximage
, 0, 0, dstX
, dstY
, w
, h
);
783 if (unpack
->BufferObj
->Name
) {
784 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
789 /* software fallback */
790 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
791 format
, type
, unpack
, pixels
);
798 * Implement glCopyPixels for the front color buffer (or back buffer Pixmap)
799 * for the color buffer. Don't support zooming, pixel transfer, etc.
800 * We do support copying from one window to another, ala glXMakeCurrentRead.
803 xmesa_CopyPixels( GLcontext
*ctx
,
804 GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
805 GLint destx
, GLint desty
, GLenum type
)
807 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
808 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
809 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
810 const XMesaGC gc
= ((XMesaBuffer
) ctx
->DrawBuffer
)->gc
;
811 struct xmesa_renderbuffer
*srcXrb
= (struct xmesa_renderbuffer
*)
812 ctx
->ReadBuffer
->_ColorReadBuffer
;
813 struct xmesa_renderbuffer
*dstXrb
= (struct xmesa_renderbuffer
*)
814 ctx
->DrawBuffer
->_ColorDrawBuffers
[0][0];
819 if (swrast
->NewState
)
820 _swrast_validate_derived( ctx
);
822 if (ctx
->Color
.DrawBuffer
[0] == GL_FRONT
&&
823 ctx
->Pixel
.ReadBuffer
== GL_FRONT
&&
827 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
828 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
829 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
830 ctx
->Pixel
.ZoomY
== 1.0) {
831 /* Note: we don't do any special clipping work here. We could,
832 * but X will do it for us.
834 srcy
= YFLIP(srcXrb
, srcy
) - height
+ 1;
835 desty
= YFLIP(dstXrb
, desty
) - height
+ 1;
836 XCopyArea(dpy
, srcXrb
->pixmap
, dstXrb
->pixmap
, gc
,
837 srcx
, srcy
, width
, height
, destx
, desty
);
840 _swrast_CopyPixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
, type
);
843 #endif /* XFree86Server */
848 * Every driver should implement a GetString function in order to
849 * return a meaningful GL_RENDERER string.
851 static const GLubyte
*
852 get_string( GLcontext
*ctx
, GLenum name
)
858 return (const GLubyte
*) "Mesa GLX Indirect";
860 return (const GLubyte
*) "Mesa X11";
864 return (const GLubyte
*) "Mesa project: www.mesa3d.org";
875 * We implement the glEnable function only because we care about
876 * dither enable/disable.
879 enable( GLcontext
*ctx
, GLenum pname
, GLboolean state
)
881 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
886 xmesa
->pixelformat
= xmesa
->xm_visual
->dithered_pf
;
888 xmesa
->pixelformat
= xmesa
->xm_visual
->undithered_pf
;
891 ; /* silence compiler warning */
897 clear_color_HPCR_ximage( GLcontext
*ctx
, const GLfloat color
[4] )
900 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
902 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[0], color
[0]);
903 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[1], color
[1]);
904 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[2], color
[2]);
905 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[3], color
[3]);
907 if (color
[0] == 0.0 && color
[1] == 0.0 && color
[2] == 0.0) {
909 MEMSET( xmesa
->xm_visual
->hpcr_clear_ximage_pattern
, 0x0 ,
910 sizeof(xmesa
->xm_visual
->hpcr_clear_ximage_pattern
));
913 /* build clear pattern */
914 for (i
=0; i
<16; i
++) {
915 xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0][i
] =
917 xmesa
->clearcolor
[0],
918 xmesa
->clearcolor
[1],
919 xmesa
->clearcolor
[2]);
920 xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[1][i
] =
922 xmesa
->clearcolor
[0],
923 xmesa
->clearcolor
[1],
924 xmesa
->clearcolor
[2]);
931 clear_color_HPCR_pixmap( GLcontext
*ctx
, const GLfloat color
[4] )
934 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
936 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[0], color
[0]);
937 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[1], color
[1]);
938 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[2], color
[2]);
939 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[3], color
[3]);
941 if (color
[0] == 0.0 && color
[1] == 0.0 && color
[2] == 0.0) {
943 for (i
=0; i
<16; i
++) {
944 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 0, 0);
945 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 1, 0);
949 for (i
=0; i
<16; i
++) {
950 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 0,
952 xmesa
->clearcolor
[0],
953 xmesa
->clearcolor
[1],
954 xmesa
->clearcolor
[2]));
955 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 1,
957 xmesa
->clearcolor
[0],
958 xmesa
->clearcolor
[1],
959 xmesa
->clearcolor
[2]));
962 /* change tile pixmap content */
963 XMesaPutImage(xmesa
->display
,
964 (XMesaDrawable
)xmesa
->xm_visual
->hpcr_clear_pixmap
,
965 XMESA_BUFFER(ctx
->DrawBuffer
)->cleargc
,
966 xmesa
->xm_visual
->hpcr_clear_ximage
, 0, 0, 0, 0, 16, 2);
971 * Called when the driver should update its state, based on the new_state
975 xmesa_update_state( GLcontext
*ctx
, GLbitfield new_state
)
977 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
979 /* Propagate statechange information to swrast and swrast_setup
980 * modules. The X11 driver has no internal GL-dependent state.
982 _swrast_InvalidateState( ctx
, new_state
);
983 _ac_InvalidateState( ctx
, new_state
);
984 _tnl_InvalidateState( ctx
, new_state
);
985 _swsetup_InvalidateState( ctx
, new_state
);
987 if (ctx
->DrawBuffer
->Name
!= 0)
991 * GL_DITHER and GL_READ/DRAW_BUFFER state effect renderbuffer funcs
993 if (new_state
& (_NEW_COLOR
| _NEW_PIXEL
)) {
994 struct xmesa_renderbuffer
*front_xrb
, *back_xrb
;
996 front_xrb
= XMESA_BUFFER(ctx
->DrawBuffer
)->frontxrb
;
998 /* XXX check for relevant new_state flags */
999 xmesa_set_renderbuffer_funcs(front_xrb
, xmesa
->pixelformat
,
1000 xmesa
->xm_visual
->BitsPerPixel
);
1001 /* setup pointers to front and back buffer clear functions */
1002 front_xrb
->clearFunc
= clear_pixmap
;
1005 back_xrb
= XMESA_BUFFER(ctx
->DrawBuffer
)->backxrb
;
1007 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
1009 /* XXX check for relevant new_state flags */
1010 xmesa_set_renderbuffer_funcs(back_xrb
, xmesa
->pixelformat
,
1011 xmesa
->xm_visual
->BitsPerPixel
);
1013 if (xmbuf
->backxrb
->pixmap
) {
1014 back_xrb
->clearFunc
= clear_pixmap
;
1017 switch (xmesa
->xm_visual
->BitsPerPixel
) {
1019 if (xmesa
->xm_visual
->hpcr_clear_flag
) {
1020 back_xrb
->clearFunc
= clear_HPCR_ximage
;
1023 back_xrb
->clearFunc
= clear_8bit_ximage
;
1027 back_xrb
->clearFunc
= clear_16bit_ximage
;
1030 back_xrb
->clearFunc
= clear_24bit_ximage
;
1033 back_xrb
->clearFunc
= clear_32bit_ximage
;
1036 back_xrb
->clearFunc
= clear_nbit_ximage
;
1043 if (xmesa
->xm_visual
->hpcr_clear_flag
) {
1044 /* this depends on whether we're drawing to the front or back buffer */
1048 ctx
->Driver
.ClearColor
= clear_color_HPCR_pixmap
;
1051 ctx
->Driver
.ClearColor
= clear_color_HPCR_ximage
;
1054 (void) clear_color_HPCR_pixmap
;
1055 (void) clear_color_HPCR_ximage
;
1063 * Called via ctx->Driver.TestProxyTeximage(). Normally, we'd just use
1064 * the _mesa_test_proxy_teximage() fallback function, but we're going to
1065 * special-case the 3D texture case to allow textures up to 512x512x32
1069 test_proxy_teximage(GLcontext
*ctx
, GLenum target
, GLint level
,
1070 GLint internalFormat
, GLenum format
, GLenum type
,
1071 GLint width
, GLint height
, GLint depth
, GLint border
)
1073 if (target
== GL_PROXY_TEXTURE_3D
) {
1074 /* special case for 3D textures */
1075 if (width
* height
* depth
> 512 * 512 * 64 ||
1076 width
< 2 * border
||
1077 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1078 _mesa_bitcount(width
- 2 * border
) != 1) ||
1079 height
< 2 * border
||
1080 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1081 _mesa_bitcount(height
- 2 * border
) != 1) ||
1082 depth
< 2 * border
||
1083 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1084 _mesa_bitcount(depth
- 2 * border
) != 1)) {
1085 /* Bad size, or too many texels */
1091 /* use the fallback routine for 1D, 2D, cube and rect targets */
1092 return _mesa_test_proxy_teximage(ctx
, target
, level
, internalFormat
,
1093 format
, type
, width
, height
, depth
,
1100 * In SW, we don't really compress GL_COMPRESSED_RGB[A] textures!
1102 static const struct gl_texture_format
*
1103 choose_tex_format( GLcontext
*ctx
, GLint internalFormat
,
1104 GLenum format
, GLenum type
)
1106 switch (internalFormat
) {
1107 case GL_COMPRESSED_RGB_ARB
:
1108 return &_mesa_texformat_rgb
;
1109 case GL_COMPRESSED_RGBA_ARB
:
1110 return &_mesa_texformat_rgba
;
1112 return _mesa_choose_tex_format(ctx
, internalFormat
, format
, type
);
1118 * Get the current drawing (and reading) window's size and update the
1119 * corresponding gl_framebuffer(s) if needed.
1122 update_framebuffer_size(GLcontext
*ctx
)
1124 struct gl_framebuffer
*fb
= ctx
->WinSysDrawBuffer
;
1125 GLuint newWidth
, newHeight
;
1126 get_buffer_size(fb
, &newWidth
, &newHeight
);
1127 if (newWidth
!= fb
->Width
|| newHeight
!= fb
->Height
) {
1128 xmesa_resize_buffers(ctx
, fb
, newWidth
, newHeight
);
1131 if (ctx
->WinSysReadBuffer
!= ctx
->WinSysDrawBuffer
) {
1132 /* Update readbuffer's size */
1133 struct gl_framebuffer
*fb
= ctx
->WinSysReadBuffer
;
1134 GLuint newWidth
, newHeight
;
1135 get_buffer_size(fb
, &newWidth
, &newHeight
);
1136 if (newWidth
!= fb
->Width
|| newHeight
!= fb
->Height
) {
1137 xmesa_resize_buffers(ctx
, fb
, newWidth
, newHeight
);
1138 ctx
->NewState
|= _NEW_BUFFERS
;
1145 * Called by glViewport.
1146 * This is a good time for us to poll the current X window size and adjust
1147 * our renderbuffers to match the current window size.
1148 * Remember, we have no opportunity to respond to conventional
1149 * X Resize/StructureNotify events since the X driver has no event loop.
1151 * Note that this trick isn't fool-proof. If the application never calls
1152 * glViewport, our notion of the current window size may be incorrect.
1155 xmesa_viewport(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei w
, GLsizei h
)
1157 update_framebuffer_size(ctx
);
1161 #if ENABLE_EXT_timer_query
1164 * The GL_EXT_timer_query extension is not enabled for the XServer
1165 * indirect renderer. Not sure about how/if wrapping of gettimeofday()
1169 struct xmesa_query_object
1171 struct gl_query_object Base
;
1172 struct timeval StartTime
;
1176 static struct gl_query_object
*
1177 xmesa_new_query_object(GLcontext
*ctx
, GLuint id
)
1179 struct xmesa_query_object
*q
= CALLOC_STRUCT(xmesa_query_object
);
1182 q
->Base
.Ready
= GL_TRUE
;
1189 xmesa_begin_query(GLcontext
*ctx
, GLenum target
, struct gl_query_object
*q
)
1191 if (target
== GL_TIME_ELAPSED_EXT
) {
1192 struct xmesa_query_object
*xq
= (struct xmesa_query_object
*) q
;
1193 (void) gettimeofday(&xq
->StartTime
, NULL
);
1199 * Return the difference between the two given times in microseconds.
1202 #define suseconds_t unsigned int
1205 time_diff(const struct timeval
*t0
, const struct timeval
*t1
)
1207 GLuint64EXT seconds0
= t0
->tv_sec
& 0xff; /* 0 .. 255 seconds */
1208 GLuint64EXT seconds1
= t1
->tv_sec
& 0xff; /* 0 .. 255 seconds */
1209 GLuint64EXT nanosec0
= (seconds0
* 1000000 + t0
->tv_usec
) * 1000;
1210 GLuint64EXT nanosec1
= (seconds1
* 1000000 + t1
->tv_usec
) * 1000;
1211 return nanosec1
- nanosec0
;
1216 xmesa_end_query(GLcontext
*ctx
, GLenum target
, struct gl_query_object
*q
)
1218 if (target
== GL_TIME_ELAPSED_EXT
) {
1219 struct xmesa_query_object
*xq
= (struct xmesa_query_object
*) q
;
1220 struct timeval endTime
;
1221 (void) gettimeofday(&endTime
, NULL
);
1222 /* result is in nanoseconds! */
1223 q
->Result
= time_diff(&xq
->StartTime
, &endTime
);
1228 #endif /* ENABLE_timer_query */
1232 * Initialize the device driver function table with the functions
1233 * we implement in this driver.
1236 xmesa_init_driver_functions( XMesaVisual xmvisual
,
1237 struct dd_function_table
*driver
)
1239 driver
->GetString
= get_string
;
1240 driver
->UpdateState
= xmesa_update_state
;
1241 driver
->GetBufferSize
= get_buffer_size
;
1242 driver
->Flush
= finish_or_flush
;
1243 driver
->Finish
= finish_or_flush
;
1244 driver
->ClearIndex
= clear_index
;
1245 driver
->ClearColor
= clear_color
;
1246 driver
->IndexMask
= index_mask
;
1247 driver
->ColorMask
= color_mask
;
1248 driver
->Enable
= enable
;
1249 driver
->Clear
= clear_buffers
;
1250 driver
->ResizeBuffers
= xmesa_resize_buffers
;
1251 driver
->Viewport
= xmesa_viewport
;
1252 #ifndef XFree86Server
1253 driver
->CopyPixels
= xmesa_CopyPixels
;
1254 if (xmvisual
->undithered_pf
== PF_8R8G8B
&&
1255 xmvisual
->dithered_pf
== PF_8R8G8B
) {
1256 driver
->DrawPixels
= xmesa_DrawPixels_8R8G8B
;
1258 else if (xmvisual
->undithered_pf
== PF_5R6G5B
) {
1259 driver
->DrawPixels
= xmesa_DrawPixels_5R6G5B
;
1262 driver
->TestProxyTexImage
= test_proxy_teximage
;
1263 #if ENABLE_EXT_texure_compression_s3tc
1264 driver
->ChooseTextureFormat
= choose_tex_format
;
1266 (void) choose_tex_format
;
1269 #if ENABLE_EXT_timer_query
1270 driver
->NewQueryObject
= xmesa_new_query_object
;
1271 driver
->BeginQuery
= xmesa_begin_query
;
1272 driver
->EndQuery
= xmesa_end_query
;
1277 #define XMESA_NEW_POINT (_NEW_POINT | \
1279 _SWRAST_NEW_RASTERMASK)
1281 #define XMESA_NEW_LINE (_NEW_LINE | \
1286 _SWRAST_NEW_RASTERMASK)
1288 #define XMESA_NEW_TRIANGLE (_NEW_POLYGON | \
1293 _SWRAST_NEW_RASTERMASK)
1296 /* Extend the software rasterizer with our line/point/triangle
1299 void xmesa_register_swrast_functions( GLcontext
*ctx
)
1301 SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
1303 swrast
->choose_point
= xmesa_choose_point
;
1304 swrast
->choose_line
= xmesa_choose_line
;
1305 swrast
->choose_triangle
= xmesa_choose_triangle
;
1307 swrast
->invalidate_point
|= XMESA_NEW_POINT
;
1308 swrast
->invalidate_line
|= XMESA_NEW_LINE
;
1309 swrast
->invalidate_triangle
|= XMESA_NEW_TRIANGLE
;