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
);
115 _glthread_LOCK_MUTEX(_xmesa_lock
);
116 XSync(xmBuffer
->xm_visual
->display
, 0); /* added for Chromium */
117 XGetGeometry( xmBuffer
->xm_visual
->display
, xmBuffer
->frontxrb
->pixmap
,
118 &root
, &winx
, &winy
, &winwidth
, &winheight
, &bw
, &d
);
119 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
128 finish_or_flush( GLcontext
*ctx
)
133 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
135 _glthread_LOCK_MUTEX(_xmesa_lock
);
136 XSync( xmesa
->display
, False
);
137 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
144 clear_index( GLcontext
*ctx
, GLuint index
)
146 if (ctx
->DrawBuffer
->Name
== 0) {
147 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
148 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
149 xmesa
->clearpixel
= (unsigned long) index
;
150 XMesaSetForeground( xmesa
->display
, xmbuf
->cleargc
, (unsigned long) index
);
156 clear_color( GLcontext
*ctx
, const GLfloat color
[4] )
158 if (ctx
->DrawBuffer
->Name
== 0) {
159 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
160 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
162 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[0], color
[0]);
163 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[1], color
[1]);
164 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[2], color
[2]);
165 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[3], color
[3]);
166 xmesa
->clearpixel
= xmesa_color_to_pixel( ctx
,
167 xmesa
->clearcolor
[0],
168 xmesa
->clearcolor
[1],
169 xmesa
->clearcolor
[2],
170 xmesa
->clearcolor
[3],
171 xmesa
->xm_visual
->undithered_pf
);
172 _glthread_LOCK_MUTEX(_xmesa_lock
);
173 XMesaSetForeground( xmesa
->display
, xmbuf
->cleargc
,
175 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
181 /* Set index mask ala glIndexMask */
183 index_mask( GLcontext
*ctx
, GLuint mask
)
185 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
186 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
187 /* not sure this conditional is really needed */
188 if (xmbuf
->backxrb
&& xmbuf
->backxrb
->pixmap
) {
190 if (mask
==0xffffffff) {
191 m
= ((unsigned long)~0L);
194 m
= (unsigned long) mask
;
196 XMesaSetPlaneMask( xmesa
->display
, xmbuf
->cleargc
, m
);
197 XMesaSetPlaneMask( xmesa
->display
, xmbuf
->gc
, m
);
202 /* Implements glColorMask() */
204 color_mask(GLcontext
*ctx
,
205 GLboolean rmask
, GLboolean gmask
, GLboolean bmask
, GLboolean amask
)
207 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
208 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
209 const int xclass
= xmesa
->xm_visual
->mesa_visual
.visualType
;
212 if (xclass
== GLX_TRUE_COLOR
|| xclass
== GLX_DIRECT_COLOR
) {
214 if (rmask
&& gmask
&& bmask
) {
215 m
= ((unsigned long)~0L);
219 if (rmask
) m
|= GET_REDMASK(xmesa
->xm_visual
);
220 if (gmask
) m
|= GET_GREENMASK(xmesa
->xm_visual
);
221 if (bmask
) m
|= GET_BLUEMASK(xmesa
->xm_visual
);
223 XMesaSetPlaneMask( xmesa
->display
, xmbuf
->cleargc
, m
);
224 XMesaSetPlaneMask( xmesa
->display
, xmbuf
->gc
, m
);
230 /**********************************************************************/
231 /*** glClear implementations ***/
232 /**********************************************************************/
236 * Clear the front or back color buffer, if it's implemented with a pixmap.
239 clear_pixmap(GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
, GLboolean all
,
240 GLint x
, GLint y
, GLint width
, GLint height
)
242 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
243 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
248 assert(xmesa
->display
);
250 assert(xmbuf
->cleargc
);
253 XMesaFillRectangle( xmesa
->display
, xrb
->pixmap
, xmbuf
->cleargc
,
254 0, 0, xrb
->Base
.Width
+ 1, xrb
->Base
.Height
+ 1 );
257 XMesaFillRectangle( xmesa
->display
, xrb
->pixmap
, xmbuf
->cleargc
,
258 x
, xrb
->Base
.Height
- y
- height
,
265 clear_8bit_ximage( GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
266 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
268 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
271 const size_t n
= xrb
->ximage
->bytes_per_line
* xrb
->Base
.Height
;
272 MEMSET( xrb
->ximage
->data
, xmesa
->clearpixel
, n
);
276 for (i
=0;i
<height
;i
++) {
277 GLubyte
*ptr
= PIXEL_ADDR1(xrb
, x
, y
+ i
);
278 MEMSET( ptr
, xmesa
->clearpixel
, width
);
285 clear_HPCR_ximage( GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
286 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
288 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
291 GLint i
, c16
= (xrb
->ximage
->bytes_per_line
>>4)<<4;
292 GLubyte
*ptr
= (GLubyte
*) xrb
->ximage
->data
;
293 for (i
= 0; i
< xrb
->Base
.Height
; i
++) {
295 GLubyte
*sptr
= xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0];
299 for (j
=0; j
<c16
; j
+=16) {
318 for (; j
< xrb
->ximage
->bytes_per_line
; j
++) {
326 for (i
=y
; i
<y
+height
; i
++) {
327 GLubyte
*ptr
= PIXEL_ADDR1( xrb
, x
, i
);
329 const GLubyte
*sptr
= xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0];
333 for (j
=x
; j
<x
+width
; j
++) {
343 clear_16bit_ximage( GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
344 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
346 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
347 register GLuint pixel
= (GLuint
) xmesa
->clearpixel
;
349 if (xmesa
->swapbytes
) {
350 pixel
= ((pixel
>> 8) & 0x00ff) | ((pixel
<< 8) & 0xff00);
354 GLuint
*ptr4
= (GLuint
*) xrb
->ximage
->data
;
355 if ((pixel
& 0xff) == ((pixel
>> 8) & 0xff)) {
356 /* low and high bytes are equal so use memset() */
357 const GLuint n
= xrb
->ximage
->bytes_per_line
* xrb
->Base
.Height
;
358 MEMSET( ptr4
, pixel
& 0xff, n
);
361 const GLuint n
= xrb
->ximage
->bytes_per_line
* xrb
->Base
.Height
/ 4;
363 pixel
= pixel
| (pixel
<<16);
364 for (i
= 0; i
< n
; i
++) {
368 /* might be one last GLushort to set */
369 if ((xrb
->ximage
->bytes_per_line
* xrb
->Base
.Height
) & 0x2)
370 *(GLushort
*)ptr4
= pixel
& 0xffff;
375 for (j
=0;j
<height
;j
++) {
376 GLushort
*ptr2
= PIXEL_ADDR2(xrb
, x
, y
+ j
);
377 for (i
=0;i
<width
;i
++) {
385 /* Optimized code provided by Nozomi Ytow <noz@xfree86.org> */
387 clear_24bit_ximage(GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
388 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
390 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
391 const GLubyte r
= xmesa
->clearcolor
[0];
392 const GLubyte g
= xmesa
->clearcolor
[1];
393 const GLubyte b
= xmesa
->clearcolor
[2];
396 if (r
== g
&& g
== b
) {
397 /* same value for all three components (gray) */
398 const GLint w3
= xrb
->Base
.Width
* 3;
399 const GLint h
= xrb
->Base
.Height
;
401 for (i
= 0; i
< h
; i
++) {
402 bgr_t
*ptr3
= PIXEL_ADDR3(xrb
, 0, i
);
408 const GLint w
= xrb
->Base
.Width
;
409 const GLint h
= xrb
->Base
.Height
;
411 for (i
= 0; i
< h
; i
++) {
412 bgr_t
*ptr3
= PIXEL_ADDR3(xrb
, 0, i
);
413 for (j
= 0; j
< w
; j
++) {
423 /* only clear subrect of color buffer */
424 if (r
== g
&& g
== b
) {
425 /* same value for all three components (gray) */
427 for (j
=0;j
<height
;j
++) {
428 bgr_t
*ptr3
= PIXEL_ADDR3(xrb
, x
, y
+ j
);
429 MEMSET(ptr3
, r
, 3 * width
);
433 /* non-gray clear color */
435 for (j
= 0; j
< height
; j
++) {
436 bgr_t
*ptr3
= PIXEL_ADDR3(xrb
, x
, y
+ j
);
437 for (i
= 0; i
< width
; i
++) {
450 clear_32bit_ximage(GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
451 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
453 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
454 register GLuint pixel
= (GLuint
) xmesa
->clearpixel
;
456 if (xmesa
->swapbytes
) {
457 pixel
= ((pixel
>> 24) & 0x000000ff)
458 | ((pixel
>> 8) & 0x0000ff00)
459 | ((pixel
<< 8) & 0x00ff0000)
460 | ((pixel
<< 24) & 0xff000000);
464 const GLuint n
= xrb
->Base
.Width
* xrb
->Base
.Height
;
465 GLuint
*ptr4
= (GLuint
*) xrb
->ximage
->data
;
467 _mesa_memset(ptr4
, pixel
, 4 * n
);
471 for (i
= 0; i
< n
; i
++)
477 for (j
= 0; j
< height
; j
++) {
478 GLuint
*ptr4
= PIXEL_ADDR4(xrb
, x
, y
+ j
);
479 for (i
= 0; i
< width
; i
++) {
488 clear_nbit_ximage(GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
489 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
491 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
492 XMesaImage
*img
= xrb
->ximage
;
495 /* We can ignore 'all' here - x, y, width, height are always right */
498 /* TODO: optimize this */
500 for (j
= 0; j
< height
; j
++) {
501 for (i
= 0; i
< width
; i
++) {
502 XMesaPutPixel(img
, x
+i
, y
-j
, xmesa
->clearpixel
);
510 clear_buffers( GLcontext
*ctx
, GLbitfield mask
,
511 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
513 if (ctx
->DrawBuffer
->Name
== 0) {
514 /* this is a window system framebuffer */
515 const GLuint
*colorMask
= (GLuint
*) &ctx
->Color
.ColorMask
;
516 XMesaBuffer b
= (XMesaBuffer
) ctx
->DrawBuffer
;
518 /* we can't handle color or index masking */
519 if (*colorMask
== 0xffffffff && ctx
->Color
.IndexMask
== 0xffffffff) {
520 if (mask
& BUFFER_BIT_FRONT_LEFT
) {
521 /* clear front color buffer */
522 if (b
->frontxrb
== (struct xmesa_renderbuffer
*)
523 ctx
->DrawBuffer
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
) {
524 /* renderbuffer is not wrapped - great! */
525 b
->frontxrb
->clearFunc(ctx
, b
->frontxrb
, all
, x
, y
,
527 mask
&= ~BUFFER_BIT_FRONT_LEFT
;
530 /* we can't directly clear an alpha-wrapped color buffer */
533 if (mask
& BUFFER_BIT_BACK_LEFT
) {
534 /* clear back color buffer */
535 if (b
->backxrb
== (struct xmesa_renderbuffer
*)
536 ctx
->DrawBuffer
->Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
) {
537 /* renderbuffer is not wrapped - great! */
538 b
->backxrb
->clearFunc(ctx
, b
->backxrb
, all
, x
, y
,
540 mask
&= ~BUFFER_BIT_BACK_LEFT
;
546 _swrast_Clear( ctx
, mask
, all
, x
, y
, width
, height
);
551 * Called by ctx->Driver.ResizeBuffers()
552 * Resize the front/back colorbuffers to match the latest window size.
555 xmesa_resize_buffers(GLcontext
*ctx
, GLframebuffer
*buffer
,
556 GLuint width
, GLuint height
)
558 /* We can do this cast because the first field in the XMesaBuffer
559 * struct is a GLframebuffer struct. If this weren't true, we'd
560 * need a pointer from the GLframebuffer to the XMesaBuffer.
562 XMesaBuffer xmBuffer
= (XMesaBuffer
) buffer
;
564 xmesa_alloc_back_buffer(xmBuffer
, width
, height
);
566 _mesa_resize_framebuffer(ctx
, buffer
, width
, height
);
570 #ifndef XFree86Server
571 /* XXX this was never tested in the Xserver environment */
574 * This function implements glDrawPixels() with an XPutImage call when
575 * drawing to the front buffer (X Window drawable).
576 * The image format must be GL_BGRA to match the PF_8R8G8B pixel format.
579 xmesa_DrawPixels_8R8G8B( GLcontext
*ctx
,
580 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
581 GLenum format
, GLenum type
,
582 const struct gl_pixelstore_attrib
*unpack
,
583 const GLvoid
*pixels
)
585 struct xmesa_renderbuffer
*xrb
586 = (struct xmesa_renderbuffer
*) ctx
->DrawBuffer
->_ColorDrawBuffers
[0][0];
588 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
589 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
590 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
591 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
592 const XMesaGC gc
= xmbuf
->gc
;
596 ASSERT(xmesa
->xm_visual
->dithered_pf
== PF_8R8G8B
);
597 ASSERT(xmesa
->xm_visual
->undithered_pf
== PF_8R8G8B
);
599 if (swrast
->NewState
)
600 _swrast_validate_derived( ctx
);
604 type
== GL_UNSIGNED_BYTE
&&
605 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
606 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
607 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
608 ctx
->Pixel
.ZoomY
== 1.0) {
613 int srcX
= unpack
->SkipPixels
;
614 int srcY
= unpack
->SkipRows
;
615 int rowLength
= unpack
->RowLength
? unpack
->RowLength
: width
;
617 if (unpack
->BufferObj
->Name
) {
618 /* unpack from PBO */
620 if (!_mesa_validate_pbo_access(2, unpack
, width
, height
, 1,
621 format
, type
, pixels
)) {
622 _mesa_error(ctx
, GL_INVALID_OPERATION
,
623 "glDrawPixels(invalid PBO access)");
626 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
,
627 GL_PIXEL_UNPACK_BUFFER_EXT
,
631 /* buffer is already mapped - that's an error */
632 _mesa_error(ctx
, GL_INVALID_OPERATION
,
633 "glDrawPixels(PBO is mapped)");
636 pixels
= ADD_POINTERS(buf
, pixels
);
639 if (_mesa_clip_drawpixels(ctx
, &dstX
, &dstY
, &w
, &h
, &srcX
, &srcY
)) {
640 /* This is a little tricky since all coordinates up to now have
641 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
642 * so we have to carefully compute the Y coordinates/addresses here.
645 MEMSET(&ximage
, 0, sizeof(XMesaImage
));
646 ximage
.width
= width
;
647 ximage
.height
= height
;
648 ximage
.format
= ZPixmap
;
649 ximage
.data
= (char *) pixels
650 + ((srcY
+ h
- 1) * rowLength
+ srcX
) * 4;
651 ximage
.byte_order
= LSBFirst
;
652 ximage
.bitmap_unit
= 32;
653 ximage
.bitmap_bit_order
= LSBFirst
;
654 ximage
.bitmap_pad
= 32;
656 ximage
.bytes_per_line
= -rowLength
* 4; /* negative to flip image */
657 ximage
.bits_per_pixel
= 32;
658 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
659 /* flip Y axis for dest position */
660 dstY
= YFLIP(xrb
, dstY
) - h
+ 1;
661 XPutImage(dpy
, xrb
->pixmap
, gc
, &ximage
, 0, 0, dstX
, dstY
, w
, h
);
664 if (unpack
->BufferObj
->Name
) {
665 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
670 /* software fallback */
671 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
672 format
, type
, unpack
, pixels
);
679 * This function implements glDrawPixels() with an XPutImage call when
680 * drawing to the front buffer (X Window drawable). The image format
681 * must be GL_RGB and image type must be GL_UNSIGNED_SHORT_5_6_5 to
682 * match the PF_5R6G5B pixel format.
685 xmesa_DrawPixels_5R6G5B( GLcontext
*ctx
,
686 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
687 GLenum format
, GLenum type
,
688 const struct gl_pixelstore_attrib
*unpack
,
689 const GLvoid
*pixels
)
691 struct xmesa_renderbuffer
*xrb
692 = (struct xmesa_renderbuffer
*) ctx
->DrawBuffer
->_ColorDrawBuffers
[0][0];
693 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
694 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
695 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
696 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
697 const XMesaGC gc
= xmbuf
->gc
;
701 ASSERT(xmesa
->xm_visual
->undithered_pf
== PF_5R6G5B
);
703 if (swrast
->NewState
)
704 _swrast_validate_derived( ctx
);
708 type
== GL_UNSIGNED_SHORT_5_6_5
&&
709 !ctx
->Color
.DitherFlag
&& /* no dithering */
710 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
711 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
712 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
713 ctx
->Pixel
.ZoomY
== 1.0) {
718 int srcX
= unpack
->SkipPixels
;
719 int srcY
= unpack
->SkipRows
;
720 int rowLength
= unpack
->RowLength
? unpack
->RowLength
: width
;
722 if (unpack
->BufferObj
->Name
) {
723 /* unpack from PBO */
725 if (!_mesa_validate_pbo_access(2, unpack
, width
, height
, 1,
726 format
, type
, pixels
)) {
727 _mesa_error(ctx
, GL_INVALID_OPERATION
,
728 "glDrawPixels(invalid PBO access)");
731 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
,
732 GL_PIXEL_UNPACK_BUFFER_EXT
,
736 /* buffer is already mapped - that's an error */
737 _mesa_error(ctx
, GL_INVALID_OPERATION
,
738 "glDrawPixels(PBO is mapped)");
741 pixels
= ADD_POINTERS(buf
, pixels
);
744 if (_mesa_clip_drawpixels(ctx
, &dstX
, &dstY
, &w
, &h
, &srcX
, &srcY
)) {
745 /* This is a little tricky since all coordinates up to now have
746 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
747 * so we have to carefully compute the Y coordinates/addresses here.
750 MEMSET(&ximage
, 0, sizeof(XMesaImage
));
751 ximage
.width
= width
;
752 ximage
.height
= height
;
753 ximage
.format
= ZPixmap
;
754 ximage
.data
= (char *) pixels
755 + ((srcY
+ h
- 1) * rowLength
+ srcX
) * 2;
756 ximage
.byte_order
= LSBFirst
;
757 ximage
.bitmap_unit
= 16;
758 ximage
.bitmap_bit_order
= LSBFirst
;
759 ximage
.bitmap_pad
= 16;
761 ximage
.bytes_per_line
= -rowLength
* 2; /* negative to flip image */
762 ximage
.bits_per_pixel
= 16;
763 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
764 /* flip Y axis for dest position */
765 dstY
= YFLIP(xrb
, dstY
) - h
+ 1;
766 XPutImage(dpy
, xrb
->pixmap
, gc
, &ximage
, 0, 0, dstX
, dstY
, w
, h
);
769 if (unpack
->BufferObj
->Name
) {
770 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
775 /* software fallback */
776 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
777 format
, type
, unpack
, pixels
);
784 * Implement glCopyPixels for the front color buffer (or back buffer Pixmap)
785 * for the color buffer. Don't support zooming, pixel transfer, etc.
786 * We do support copying from one window to another, ala glXMakeCurrentRead.
789 xmesa_CopyPixels( GLcontext
*ctx
,
790 GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
791 GLint destx
, GLint desty
, GLenum type
)
793 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
794 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
795 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
796 const XMesaGC gc
= ((XMesaBuffer
) ctx
->DrawBuffer
)->gc
;
797 struct xmesa_renderbuffer
*srcXrb
= (struct xmesa_renderbuffer
*)
798 ctx
->ReadBuffer
->_ColorReadBuffer
;
799 struct xmesa_renderbuffer
*dstXrb
= (struct xmesa_renderbuffer
*)
800 ctx
->DrawBuffer
->_ColorDrawBuffers
[0][0];
805 if (swrast
->NewState
)
806 _swrast_validate_derived( ctx
);
808 if (ctx
->Color
.DrawBuffer
[0] == GL_FRONT
&&
809 ctx
->Pixel
.ReadBuffer
== GL_FRONT
&&
813 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
814 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
815 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
816 ctx
->Pixel
.ZoomY
== 1.0) {
817 /* Note: we don't do any special clipping work here. We could,
818 * but X will do it for us.
820 srcy
= YFLIP(srcXrb
, srcy
) - height
+ 1;
821 desty
= YFLIP(dstXrb
, desty
) - height
+ 1;
822 XCopyArea(dpy
, srcXrb
->pixmap
, dstXrb
->pixmap
, gc
,
823 srcx
, srcy
, width
, height
, destx
, desty
);
826 _swrast_CopyPixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
, type
);
829 #endif /* XFree86Server */
834 * Every driver should implement a GetString function in order to
835 * return a meaningful GL_RENDERER string.
837 static const GLubyte
*
838 get_string( GLcontext
*ctx
, GLenum name
)
844 return (const GLubyte
*) "Mesa GLX Indirect";
846 return (const GLubyte
*) "Mesa X11";
850 return (const GLubyte
*) "Mesa project: www.mesa3d.org";
861 * We implement the glEnable function only because we care about
862 * dither enable/disable.
865 enable( GLcontext
*ctx
, GLenum pname
, GLboolean state
)
867 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
872 xmesa
->pixelformat
= xmesa
->xm_visual
->dithered_pf
;
874 xmesa
->pixelformat
= xmesa
->xm_visual
->undithered_pf
;
877 ; /* silence compiler warning */
883 clear_color_HPCR_ximage( GLcontext
*ctx
, const GLfloat color
[4] )
886 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
888 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[0], color
[0]);
889 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[1], color
[1]);
890 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[2], color
[2]);
891 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[3], color
[3]);
893 if (color
[0] == 0.0 && color
[1] == 0.0 && color
[2] == 0.0) {
895 MEMSET( xmesa
->xm_visual
->hpcr_clear_ximage_pattern
, 0x0 ,
896 sizeof(xmesa
->xm_visual
->hpcr_clear_ximage_pattern
));
899 /* build clear pattern */
900 for (i
=0; i
<16; i
++) {
901 xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0][i
] =
903 xmesa
->clearcolor
[0],
904 xmesa
->clearcolor
[1],
905 xmesa
->clearcolor
[2]);
906 xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[1][i
] =
908 xmesa
->clearcolor
[0],
909 xmesa
->clearcolor
[1],
910 xmesa
->clearcolor
[2]);
917 clear_color_HPCR_pixmap( GLcontext
*ctx
, const GLfloat color
[4] )
920 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
922 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[0], color
[0]);
923 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[1], color
[1]);
924 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[2], color
[2]);
925 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[3], color
[3]);
927 if (color
[0] == 0.0 && color
[1] == 0.0 && color
[2] == 0.0) {
929 for (i
=0; i
<16; i
++) {
930 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 0, 0);
931 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 1, 0);
935 for (i
=0; i
<16; i
++) {
936 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 0,
938 xmesa
->clearcolor
[0],
939 xmesa
->clearcolor
[1],
940 xmesa
->clearcolor
[2]));
941 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 1,
943 xmesa
->clearcolor
[0],
944 xmesa
->clearcolor
[1],
945 xmesa
->clearcolor
[2]));
948 /* change tile pixmap content */
949 XMesaPutImage(xmesa
->display
,
950 (XMesaDrawable
)xmesa
->xm_visual
->hpcr_clear_pixmap
,
951 XMESA_BUFFER(ctx
->DrawBuffer
)->cleargc
,
952 xmesa
->xm_visual
->hpcr_clear_ximage
, 0, 0, 0, 0, 16, 2);
957 * Called when the driver should update it's state, based on the new_state
961 xmesa_update_state( GLcontext
*ctx
, GLuint new_state
)
963 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
964 struct xmesa_renderbuffer
*front_xrb
, *back_xrb
;
966 /* Propagate statechange information to swrast and swrast_setup
967 * modules. The X11 driver has no internal GL-dependent state.
969 _swrast_InvalidateState( ctx
, new_state
);
970 _ac_InvalidateState( ctx
, new_state
);
971 _tnl_InvalidateState( ctx
, new_state
);
972 _swsetup_InvalidateState( ctx
, new_state
);
974 if (ctx
->DrawBuffer
->Name
!= 0)
977 front_xrb
= XMESA_BUFFER(ctx
->DrawBuffer
)->frontxrb
;
979 /* XXX check for relevant new_state flags */
980 xmesa_set_renderbuffer_funcs(front_xrb
, xmesa
->pixelformat
,
981 xmesa
->xm_visual
->BitsPerPixel
);
982 /* setup pointers to front and back buffer clear functions */
983 front_xrb
->clearFunc
= clear_pixmap
;
986 back_xrb
= XMESA_BUFFER(ctx
->DrawBuffer
)->backxrb
;
988 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
990 /* XXX check for relevant new_state flags */
991 xmesa_set_renderbuffer_funcs(back_xrb
, xmesa
->pixelformat
,
992 xmesa
->xm_visual
->BitsPerPixel
);
994 if (xmbuf
->backxrb
->pixmap
) {
995 back_xrb
->clearFunc
= clear_pixmap
;
998 switch (xmesa
->xm_visual
->BitsPerPixel
) {
1000 if (xmesa
->xm_visual
->hpcr_clear_flag
) {
1001 back_xrb
->clearFunc
= clear_HPCR_ximage
;
1004 back_xrb
->clearFunc
= clear_8bit_ximage
;
1008 back_xrb
->clearFunc
= clear_16bit_ximage
;
1011 back_xrb
->clearFunc
= clear_24bit_ximage
;
1014 back_xrb
->clearFunc
= clear_32bit_ximage
;
1017 back_xrb
->clearFunc
= clear_nbit_ximage
;
1023 if (xmesa
->xm_visual
->hpcr_clear_flag
) {
1024 /* this depends on whether we're drawing to the front or back buffer */
1028 ctx
->Driver
.ClearColor
= clear_color_HPCR_pixmap
;
1031 ctx
->Driver
.ClearColor
= clear_color_HPCR_ximage
;
1034 (void) clear_color_HPCR_pixmap
;
1035 (void) clear_color_HPCR_ximage
;
1043 * Called via ctx->Driver.TestProxyTeximage(). Normally, we'd just use
1044 * the _mesa_test_proxy_teximage() fallback function, but we're going to
1045 * special-case the 3D texture case to allow textures up to 512x512x32
1049 test_proxy_teximage(GLcontext
*ctx
, GLenum target
, GLint level
,
1050 GLint internalFormat
, GLenum format
, GLenum type
,
1051 GLint width
, GLint height
, GLint depth
, GLint border
)
1053 if (target
== GL_PROXY_TEXTURE_3D
) {
1054 /* special case for 3D textures */
1055 if (width
* height
* depth
> 512 * 512 * 64 ||
1056 width
< 2 * border
||
1057 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1058 _mesa_bitcount(width
- 2 * border
) != 1) ||
1059 height
< 2 * border
||
1060 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1061 _mesa_bitcount(height
- 2 * border
) != 1) ||
1062 depth
< 2 * border
||
1063 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1064 _mesa_bitcount(depth
- 2 * border
) != 1)) {
1065 /* Bad size, or too many texels */
1071 /* use the fallback routine for 1D, 2D, cube and rect targets */
1072 return _mesa_test_proxy_teximage(ctx
, target
, level
, internalFormat
,
1073 format
, type
, width
, height
, depth
,
1080 * In SW, we don't really compress GL_COMPRESSED_RGB[A] textures!
1082 static const struct gl_texture_format
*
1083 choose_tex_format( GLcontext
*ctx
, GLint internalFormat
,
1084 GLenum format
, GLenum type
)
1086 switch (internalFormat
) {
1087 case GL_COMPRESSED_RGB_ARB
:
1088 return &_mesa_texformat_rgb
;
1089 case GL_COMPRESSED_RGBA_ARB
:
1090 return &_mesa_texformat_rgba
;
1092 return _mesa_choose_tex_format(ctx
, internalFormat
, format
, type
);
1098 * Get the current drawing (and reading) window's size and update the
1099 * corresponding gl_framebuffer(s) if needed.
1102 update_framebuffer_size(GLcontext
*ctx
)
1104 struct gl_framebuffer
*fb
= ctx
->WinSysDrawBuffer
;
1105 GLuint newWidth
, newHeight
;
1106 get_buffer_size(fb
, &newWidth
, &newHeight
);
1107 if (newWidth
!= fb
->Width
|| newHeight
!= fb
->Height
) {
1108 xmesa_resize_buffers(ctx
, fb
, newWidth
, newHeight
);
1109 ctx
->NewState
|= _NEW_BUFFERS
; /* to update scissor / window bounds */
1112 if (ctx
->WinSysReadBuffer
!= ctx
->WinSysDrawBuffer
) {
1113 /* Update readbuffer's size */
1114 struct gl_framebuffer
*fb
= ctx
->WinSysReadBuffer
;
1115 GLuint newWidth
, newHeight
;
1116 get_buffer_size(fb
, &newWidth
, &newHeight
);
1117 if (newWidth
!= fb
->Width
|| newHeight
!= fb
->Height
) {
1118 xmesa_resize_buffers(ctx
, fb
, newWidth
, newHeight
);
1119 ctx
->NewState
|= _NEW_BUFFERS
;
1126 * Called by glViewport.
1127 * This is a good time for us to poll the current X window size and adjust
1128 * our renderbuffers to match the current window size.
1129 * Remember, we have no opportunity to respond to conventional
1130 * X Resize/StructureNotify events since the X driver has no event loop.
1132 * Note that this trick isn't fool-proof. If the application never calls
1133 * glViewport, our notion of the current window size may be incorrect.
1136 xmesa_viewport(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei w
, GLsizei h
)
1138 update_framebuffer_size(ctx
);
1142 #if ENABLE_EXT_timer_query
1145 * The GL_EXT_timer_query extension is not enabled for the XServer
1146 * indirect renderer. Not sure about how/if wrapping of gettimeofday()
1150 struct xmesa_query_object
1152 struct gl_query_object Base
;
1153 struct timeval StartTime
;
1157 static struct gl_query_object
*
1158 xmesa_new_query_object(GLcontext
*ctx
, GLuint id
)
1160 struct xmesa_query_object
*q
= CALLOC_STRUCT(xmesa_query_object
);
1163 q
->Base
.Ready
= GL_TRUE
;
1170 xmesa_begin_query(GLcontext
*ctx
, GLenum target
, struct gl_query_object
*q
)
1172 if (target
== GL_TIME_ELAPSED_EXT
) {
1173 struct xmesa_query_object
*xq
= (struct xmesa_query_object
*) q
;
1174 (void) gettimeofday(&xq
->StartTime
, NULL
);
1180 * Return the difference between the two given times in microseconds.
1183 #define suseconds_t unsigned int
1186 time_diff(const struct timeval
*t0
, const struct timeval
*t1
)
1188 time_t seconds0
= t0
->tv_sec
& 0xff; /* 0 .. 255 seconds */
1189 time_t seconds1
= t1
->tv_sec
& 0xff; /* 0 .. 255 seconds */
1190 suseconds_t useconds0
= seconds0
* 1000000 + t0
->tv_usec
;
1191 suseconds_t useconds1
= seconds1
* 1000000 + t1
->tv_usec
;
1192 return useconds1
- useconds0
;
1197 xmesa_end_query(GLcontext
*ctx
, GLenum target
, struct gl_query_object
*q
)
1199 if (target
== GL_TIME_ELAPSED_EXT
) {
1200 struct xmesa_query_object
*xq
= (struct xmesa_query_object
*) q
;
1201 struct timeval endTime
;
1203 (void) gettimeofday(&endTime
, NULL
);
1204 dt
= time_diff(&xq
->StartTime
, &endTime
);
1205 /* clamp if we'd overflow a 32-bit unsigned int */
1206 if (dt
>= 0xffffffffU
/ 1000U)
1207 q
->Result
= 0xffffffffU
;
1209 q
->Result
= dt
* 1000; /* result is in nanoseconds! */
1214 #endif /* ENABLE_timer_query */
1218 * Initialize the device driver function table with the functions
1219 * we implement in this driver.
1222 xmesa_init_driver_functions( XMesaVisual xmvisual
,
1223 struct dd_function_table
*driver
)
1225 driver
->GetString
= get_string
;
1226 driver
->UpdateState
= xmesa_update_state
;
1227 driver
->GetBufferSize
= get_buffer_size
;
1228 driver
->Flush
= finish_or_flush
;
1229 driver
->Finish
= finish_or_flush
;
1230 driver
->ClearIndex
= clear_index
;
1231 driver
->ClearColor
= clear_color
;
1232 driver
->IndexMask
= index_mask
;
1233 driver
->ColorMask
= color_mask
;
1234 driver
->Enable
= enable
;
1235 driver
->Clear
= clear_buffers
;
1236 driver
->ResizeBuffers
= xmesa_resize_buffers
;
1237 driver
->Viewport
= xmesa_viewport
;
1238 #ifndef XFree86Server
1239 driver
->CopyPixels
= xmesa_CopyPixels
;
1240 if (xmvisual
->undithered_pf
== PF_8R8G8B
&&
1241 xmvisual
->dithered_pf
== PF_8R8G8B
) {
1242 driver
->DrawPixels
= xmesa_DrawPixels_8R8G8B
;
1244 else if (xmvisual
->undithered_pf
== PF_5R6G5B
) {
1245 driver
->DrawPixels
= xmesa_DrawPixels_5R6G5B
;
1248 driver
->TestProxyTexImage
= test_proxy_teximage
;
1249 #if ENABLE_EXT_texure_compression_s3tc
1250 driver
->ChooseTextureFormat
= choose_tex_format
;
1252 (void) choose_tex_format
;
1255 #if ENABLE_EXT_timer_query
1256 driver
->NewQueryObject
= xmesa_new_query_object
;
1257 driver
->BeginQuery
= xmesa_begin_query
;
1258 driver
->EndQuery
= xmesa_end_query
;
1263 #define XMESA_NEW_POINT (_NEW_POINT | \
1265 _SWRAST_NEW_RASTERMASK)
1267 #define XMESA_NEW_LINE (_NEW_LINE | \
1272 _SWRAST_NEW_RASTERMASK)
1274 #define XMESA_NEW_TRIANGLE (_NEW_POLYGON | \
1279 _SWRAST_NEW_RASTERMASK)
1282 /* Extend the software rasterizer with our line/point/triangle
1285 void xmesa_register_swrast_functions( GLcontext
*ctx
)
1287 SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
1289 swrast
->choose_point
= xmesa_choose_point
;
1290 swrast
->choose_line
= xmesa_choose_line
;
1291 swrast
->choose_triangle
= xmesa_choose_triangle
;
1293 swrast
->invalidate_point
|= XMESA_NEW_POINT
;
1294 swrast
->invalidate_line
|= XMESA_NEW_LINE
;
1295 swrast
->invalidate_triangle
|= XMESA_NEW_TRIANGLE
;