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"
54 #include "drivers/common/meta.h"
60 * Dithering kernels and lookup tables.
63 const int xmesa_kernel8
[DITH_DY
* DITH_DX
] = {
64 0 * MAXC
, 8 * MAXC
, 2 * MAXC
, 10 * MAXC
,
65 12 * MAXC
, 4 * MAXC
, 14 * MAXC
, 6 * MAXC
,
66 3 * MAXC
, 11 * MAXC
, 1 * MAXC
, 9 * MAXC
,
67 15 * MAXC
, 7 * MAXC
, 13 * MAXC
, 5 * MAXC
,
70 const short xmesa_HPCR_DRGB
[3][2][16] = {
72 { 16, -4, 1,-11, 14, -6, 3, -9, 15, -5, 2,-10, 13, -7, 4, -8},
73 {-15, 5, 0, 12,-13, 7, -2, 10,-14, 6, -1, 11,-12, 8, -3, 9}
76 {-11, 15, -7, 3, -8, 14, -4, 2,-10, 16, -6, 4, -9, 13, -5, 1},
77 { 12,-14, 8, -2, 9,-13, 5, -1, 11,-15, 7, -3, 10,-12, 6, 0}
80 { 6,-18, 26,-14, 2,-22, 30,-10, 8,-16, 28,-12, 4,-20, 32, -8},
81 { -4, 20,-24, 16, 0, 24,-28, 12, -6, 18,-26, 14, -2, 22,-30, 10}
85 const int xmesa_kernel1
[16] = {
86 0*47, 9*47, 4*47, 12*47, /* 47 = (255*3)/16 */
87 6*47, 2*47, 14*47, 8*47,
88 10*47, 1*47, 5*47, 11*47,
89 7*47, 13*47, 3*47, 15*47
94 finish_or_flush( struct gl_context
*ctx
)
99 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
101 _glthread_LOCK_MUTEX(_xmesa_lock
);
102 XSync( xmesa
->display
, False
);
103 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
110 clear_color( struct gl_context
*ctx
, const GLfloat color
[4] )
112 if (ctx
->DrawBuffer
->Name
== 0) {
113 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
114 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
116 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[0], color
[0]);
117 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[1], color
[1]);
118 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[2], color
[2]);
119 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[3], color
[3]);
120 xmesa
->clearpixel
= xmesa_color_to_pixel( ctx
,
121 xmesa
->clearcolor
[0],
122 xmesa
->clearcolor
[1],
123 xmesa
->clearcolor
[2],
124 xmesa
->clearcolor
[3],
125 xmesa
->xm_visual
->undithered_pf
);
126 _glthread_LOCK_MUTEX(_xmesa_lock
);
127 XMesaSetForeground( xmesa
->display
, xmbuf
->cleargc
,
129 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
135 /* Implements glColorMask() */
137 color_mask(struct gl_context
*ctx
,
138 GLboolean rmask
, GLboolean gmask
, GLboolean bmask
, GLboolean amask
)
140 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
142 const int xclass
= xmesa
->xm_visual
->visualType
;
145 if (ctx
->DrawBuffer
->Name
!= 0)
148 xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
150 if (xclass
== GLX_TRUE_COLOR
|| xclass
== GLX_DIRECT_COLOR
) {
152 if (rmask
&& gmask
&& bmask
) {
153 m
= ((unsigned long)~0L);
157 if (rmask
) m
|= GET_REDMASK(xmesa
->xm_visual
);
158 if (gmask
) m
|= GET_GREENMASK(xmesa
->xm_visual
);
159 if (bmask
) m
|= GET_BLUEMASK(xmesa
->xm_visual
);
161 XMesaSetPlaneMask( xmesa
->display
, xmbuf
->cleargc
, m
);
167 /**********************************************************************/
168 /*** glClear implementations ***/
169 /**********************************************************************/
173 * Clear the front or back color buffer, if it's implemented with a pixmap.
176 clear_pixmap(struct gl_context
*ctx
, struct xmesa_renderbuffer
*xrb
,
177 GLint x
, GLint y
, GLint width
, GLint height
)
179 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
180 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
185 assert(xmesa
->display
);
187 assert(xmbuf
->cleargc
);
189 XMesaFillRectangle( xmesa
->display
, xrb
->pixmap
, xmbuf
->cleargc
,
190 x
, xrb
->Base
.Height
- y
- height
,
196 clear_8bit_ximage( struct gl_context
*ctx
, struct xmesa_renderbuffer
*xrb
,
197 GLint x
, GLint y
, GLint width
, GLint height
)
199 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
201 for (i
= 0; i
< height
; i
++) {
202 GLubyte
*ptr
= PIXEL_ADDR1(xrb
, x
, y
+ i
);
203 memset( ptr
, xmesa
->clearpixel
, width
);
209 clear_HPCR_ximage( struct gl_context
*ctx
, struct xmesa_renderbuffer
*xrb
,
210 GLint x
, GLint y
, GLint width
, GLint height
)
212 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
214 for (i
= y
; i
< y
+ height
; i
++) {
215 GLubyte
*ptr
= PIXEL_ADDR1( xrb
, x
, i
);
217 const GLubyte
*sptr
= xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0];
221 for (j
= x
; j
< x
+ width
; j
++) {
230 clear_16bit_ximage( struct gl_context
*ctx
, struct xmesa_renderbuffer
*xrb
,
231 GLint x
, GLint y
, GLint width
, GLint height
)
233 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
234 GLuint pixel
= (GLuint
) xmesa
->clearpixel
;
237 if (xmesa
->swapbytes
) {
238 pixel
= ((pixel
>> 8) & 0x00ff) | ((pixel
<< 8) & 0xff00);
241 for (j
= 0; j
< height
; j
++) {
242 GLushort
*ptr2
= PIXEL_ADDR2(xrb
, x
, y
+ j
);
243 for (i
= 0; i
< width
; i
++) {
250 /* Optimized code provided by Nozomi Ytow <noz@xfree86.org> */
252 clear_24bit_ximage(struct gl_context
*ctx
, struct xmesa_renderbuffer
*xrb
,
253 GLint x
, GLint y
, GLint width
, GLint height
)
255 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
256 const GLubyte r
= xmesa
->clearcolor
[0];
257 const GLubyte g
= xmesa
->clearcolor
[1];
258 const GLubyte b
= xmesa
->clearcolor
[2];
260 if (r
== g
&& g
== b
) {
261 /* same value for all three components (gray) */
263 for (j
= 0; j
< height
; j
++) {
264 bgr_t
*ptr3
= PIXEL_ADDR3(xrb
, x
, y
+ j
);
265 memset(ptr3
, r
, 3 * width
);
269 /* non-gray clear color */
271 for (j
= 0; j
< height
; j
++) {
272 bgr_t
*ptr3
= PIXEL_ADDR3(xrb
, x
, y
+ j
);
273 for (i
= 0; i
< width
; i
++) {
285 clear_32bit_ximage(struct gl_context
*ctx
, struct xmesa_renderbuffer
*xrb
,
286 GLint x
, GLint y
, GLint width
, GLint height
)
288 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
289 register GLuint pixel
= (GLuint
) xmesa
->clearpixel
;
294 if (xmesa
->swapbytes
) {
295 pixel
= ((pixel
>> 24) & 0x000000ff)
296 | ((pixel
>> 8) & 0x0000ff00)
297 | ((pixel
<< 8) & 0x00ff0000)
298 | ((pixel
<< 24) & 0xff000000);
301 if (width
== xrb
->Base
.Width
&& height
== xrb
->Base
.Height
) {
302 /* clearing whole buffer */
303 const GLuint n
= xrb
->Base
.Width
* xrb
->Base
.Height
;
304 GLuint
*ptr4
= (GLuint
*) xrb
->ximage
->data
;
307 memset(ptr4
, pixel
, 4 * n
);
311 for (i
= 0; i
< n
; i
++)
316 /* clearing scissored region */
318 for (j
= 0; j
< height
; j
++) {
319 GLuint
*ptr4
= PIXEL_ADDR4(xrb
, x
, y
+ j
);
320 for (i
= 0; i
< width
; i
++) {
329 clear_nbit_ximage(struct gl_context
*ctx
, struct xmesa_renderbuffer
*xrb
,
330 GLint x
, GLint y
, GLint width
, GLint height
)
332 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
333 XMesaImage
*img
= xrb
->ximage
;
336 /* TODO: optimize this */
338 for (j
= 0; j
< height
; j
++) {
339 for (i
= 0; i
< width
; i
++) {
340 XMesaPutPixel(img
, x
+i
, y
-j
, xmesa
->clearpixel
);
348 clear_buffers(struct gl_context
*ctx
, GLbitfield buffers
)
350 if (ctx
->DrawBuffer
->Name
== 0) {
351 /* this is a window system framebuffer */
352 const GLuint
*colorMask
= (GLuint
*) &ctx
->Color
.ColorMask
[0];
353 XMesaBuffer b
= XMESA_BUFFER(ctx
->DrawBuffer
);
354 const GLint x
= ctx
->DrawBuffer
->_Xmin
;
355 const GLint y
= ctx
->DrawBuffer
->_Ymin
;
356 const GLint width
= ctx
->DrawBuffer
->_Xmax
- x
;
357 const GLint height
= ctx
->DrawBuffer
->_Ymax
- y
;
359 /* we can't handle color or index masking */
360 if (*colorMask
== 0xffffffff && ctx
->Color
.IndexMask
== 0xffffffff) {
361 if (buffers
& BUFFER_BIT_FRONT_LEFT
) {
362 /* clear front color buffer */
363 struct gl_renderbuffer
*frontRb
364 = ctx
->DrawBuffer
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
;
365 if (b
->frontxrb
== xmesa_renderbuffer(frontRb
)) {
366 /* renderbuffer is not wrapped - great! */
367 b
->frontxrb
->clearFunc(ctx
, b
->frontxrb
, x
, y
, width
, height
);
368 buffers
&= ~BUFFER_BIT_FRONT_LEFT
;
371 /* we can't directly clear an alpha-wrapped color buffer */
374 if (buffers
& BUFFER_BIT_BACK_LEFT
) {
375 /* clear back color buffer */
376 struct gl_renderbuffer
*backRb
377 = ctx
->DrawBuffer
->Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
;
378 if (b
->backxrb
== xmesa_renderbuffer(backRb
)) {
379 /* renderbuffer is not wrapped - great! */
380 b
->backxrb
->clearFunc(ctx
, b
->backxrb
, x
, y
, width
, height
);
381 buffers
&= ~BUFFER_BIT_BACK_LEFT
;
387 _swrast_Clear(ctx
, buffers
);
391 #ifndef XFree86Server
392 /* XXX these functions haven't been tested in the Xserver environment */
396 * Check if we can do an optimized glDrawPixels into an 8R8G8B visual.
399 can_do_DrawPixels_8R8G8B(struct gl_context
*ctx
, GLenum format
, GLenum type
)
401 if (format
== GL_BGRA
&&
402 type
== GL_UNSIGNED_BYTE
&&
404 ctx
->DrawBuffer
->Name
== 0 &&
405 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
406 ctx
->Pixel
.ZoomY
== 1.0 &&
407 ctx
->_ImageTransferState
== 0 /* no color tables, scale/bias, etc */) {
408 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
410 if (swrast
->NewState
)
411 _swrast_validate_derived( ctx
);
413 if ((swrast
->_RasterMask
& ~CLIP_BIT
) == 0) /* no blend, z-test, etc */ {
414 struct gl_renderbuffer
*rb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[0];
416 struct xmesa_renderbuffer
*xrb
= xmesa_renderbuffer(rb
->Wrapped
);
418 xrb
->pixmap
&& /* drawing to pixmap or window */
419 _mesa_get_format_bits(xrb
->Base
.Format
, GL_ALPHA_BITS
) == 0) {
430 * This function implements glDrawPixels() with an XPutImage call when
431 * drawing to the front buffer (X Window drawable).
432 * The image format must be GL_BGRA to match the PF_8R8G8B pixel format.
435 xmesa_DrawPixels_8R8G8B( struct gl_context
*ctx
,
436 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
437 GLenum format
, GLenum type
,
438 const struct gl_pixelstore_attrib
*unpack
,
439 const GLvoid
*pixels
)
441 if (can_do_DrawPixels_8R8G8B(ctx
, format
, type
)) {
442 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
443 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
449 if (swrast
->NewState
)
450 _swrast_validate_derived( ctx
);
452 if (unpack
->BufferObj
->Name
) {
453 /* unpack from PBO */
455 if (!_mesa_validate_pbo_access(2, unpack
, width
, height
, 1,
456 format
, type
, pixels
)) {
457 _mesa_error(ctx
, GL_INVALID_OPERATION
,
458 "glDrawPixels(invalid PBO access)");
461 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
,
462 GL_PIXEL_UNPACK_BUFFER_EXT
,
466 /* buffer is already mapped - that's an error */
467 _mesa_error(ctx
, GL_INVALID_OPERATION
,
468 "glDrawPixels(PBO is mapped)");
471 pixels
= ADD_POINTERS(buf
, pixels
);
474 if (_mesa_clip_drawpixels(ctx
, &dstX
, &dstY
, &w
, &h
, &clippedUnpack
)) {
475 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
476 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
477 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
478 const XMesaGC gc
= xmbuf
->cleargc
; /* effected by glColorMask */
479 struct xmesa_renderbuffer
*xrb
480 = xmesa_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]->Wrapped
);
481 const int srcX
= clippedUnpack
.SkipPixels
;
482 const int srcY
= clippedUnpack
.SkipRows
;
483 const int rowLength
= clippedUnpack
.RowLength
;
486 ASSERT(xmesa
->xm_visual
->dithered_pf
== PF_8R8G8B
);
487 ASSERT(xmesa
->xm_visual
->undithered_pf
== PF_8R8G8B
);
491 /* This is a little tricky since all coordinates up to now have
492 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
493 * so we have to carefully compute the Y coordinates/addresses here.
495 memset(&ximage
, 0, sizeof(XMesaImage
));
496 ximage
.width
= width
;
497 ximage
.height
= height
;
498 ximage
.format
= ZPixmap
;
499 ximage
.data
= (char *) pixels
500 + ((srcY
+ h
- 1) * rowLength
+ srcX
) * 4;
501 ximage
.byte_order
= LSBFirst
;
502 ximage
.bitmap_unit
= 32;
503 ximage
.bitmap_bit_order
= LSBFirst
;
504 ximage
.bitmap_pad
= 32;
506 ximage
.bits_per_pixel
= 32;
507 ximage
.bytes_per_line
= -rowLength
* 4; /* negative to flip image */
508 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
509 /* flip Y axis for dest position */
510 dstY
= YFLIP(xrb
, dstY
) - h
+ 1;
511 XPutImage(dpy
, xrb
->pixmap
, gc
, &ximage
, 0, 0, dstX
, dstY
, w
, h
);
514 if (unpack
->BufferObj
->Name
) {
515 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
520 /* software fallback */
521 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
522 format
, type
, unpack
, pixels
);
529 * Check if we can do an optimized glDrawPixels into an 5R6G5B visual.
532 can_do_DrawPixels_5R6G5B(struct gl_context
*ctx
, GLenum format
, GLenum type
)
534 if (format
== GL_RGB
&&
535 type
== GL_UNSIGNED_SHORT_5_6_5
&&
536 !ctx
->Color
.DitherFlag
&& /* no dithering */
538 ctx
->DrawBuffer
->Name
== 0 &&
539 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
540 ctx
->Pixel
.ZoomY
== 1.0 &&
541 ctx
->_ImageTransferState
== 0 /* no color tables, scale/bias, etc */) {
542 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
544 if (swrast
->NewState
)
545 _swrast_validate_derived( ctx
);
547 if ((swrast
->_RasterMask
& ~CLIP_BIT
) == 0) /* no blend, z-test, etc */ {
548 struct gl_renderbuffer
*rb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[0];
550 struct xmesa_renderbuffer
*xrb
= xmesa_renderbuffer(rb
->Wrapped
);
552 xrb
->pixmap
&& /* drawing to pixmap or window */
553 _mesa_get_format_bits(xrb
->Base
.Format
, GL_ALPHA_BITS
) == 0) {
564 * This function implements glDrawPixels() with an XPutImage call when
565 * drawing to the front buffer (X Window drawable). The image format
566 * must be GL_RGB and image type must be GL_UNSIGNED_SHORT_5_6_5 to
567 * match the PF_5R6G5B pixel format.
570 xmesa_DrawPixels_5R6G5B( struct gl_context
*ctx
,
571 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
572 GLenum format
, GLenum type
,
573 const struct gl_pixelstore_attrib
*unpack
,
574 const GLvoid
*pixels
)
576 if (can_do_DrawPixels_5R6G5B(ctx
, format
, type
)) {
577 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
578 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
584 if (swrast
->NewState
)
585 _swrast_validate_derived( ctx
);
587 if (unpack
->BufferObj
->Name
) {
588 /* unpack from PBO */
590 if (!_mesa_validate_pbo_access(2, unpack
, width
, height
, 1,
591 format
, type
, pixels
)) {
592 _mesa_error(ctx
, GL_INVALID_OPERATION
,
593 "glDrawPixels(invalid PBO access)");
596 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
,
597 GL_PIXEL_UNPACK_BUFFER_EXT
,
601 /* buffer is already mapped - that's an error */
602 _mesa_error(ctx
, GL_INVALID_OPERATION
,
603 "glDrawPixels(PBO is mapped)");
606 pixels
= ADD_POINTERS(buf
, pixels
);
609 if (_mesa_clip_drawpixels(ctx
, &dstX
, &dstY
, &w
, &h
, &clippedUnpack
)) {
610 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
611 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
612 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
613 const XMesaGC gc
= xmbuf
->cleargc
; /* effected by glColorMask */
614 struct xmesa_renderbuffer
*xrb
615 = xmesa_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]->Wrapped
);
616 const int srcX
= clippedUnpack
.SkipPixels
;
617 const int srcY
= clippedUnpack
.SkipRows
;
618 const int rowLength
= clippedUnpack
.RowLength
;
621 ASSERT(xmesa
->xm_visual
->undithered_pf
== PF_5R6G5B
);
625 /* This is a little tricky since all coordinates up to now have
626 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
627 * so we have to carefully compute the Y coordinates/addresses here.
629 memset(&ximage
, 0, sizeof(XMesaImage
));
630 ximage
.width
= width
;
631 ximage
.height
= height
;
632 ximage
.format
= ZPixmap
;
633 ximage
.data
= (char *) pixels
634 + ((srcY
+ h
- 1) * rowLength
+ srcX
) * 2;
635 ximage
.byte_order
= LSBFirst
;
636 ximage
.bitmap_unit
= 16;
637 ximage
.bitmap_bit_order
= LSBFirst
;
638 ximage
.bitmap_pad
= 16;
640 ximage
.bits_per_pixel
= 16;
641 ximage
.bytes_per_line
= -rowLength
* 2; /* negative to flip image */
642 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
643 /* flip Y axis for dest position */
644 dstY
= YFLIP(xrb
, dstY
) - h
+ 1;
645 XPutImage(dpy
, xrb
->pixmap
, gc
, &ximage
, 0, 0, dstX
, dstY
, w
, h
);
648 if (unpack
->BufferObj
->Name
) {
649 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
654 /* software fallback */
655 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
656 format
, type
, unpack
, pixels
);
662 * Determine if we can do an optimized glCopyPixels.
665 can_do_CopyPixels(struct gl_context
*ctx
, GLenum type
)
667 if (type
== GL_COLOR
&&
668 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
669 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
670 ctx
->Pixel
.ZoomY
== 1.0 &&
671 ctx
->Color
.DrawBuffer
[0] == GL_FRONT
&& /* copy to front buf */
672 ctx
->Pixel
.ReadBuffer
== GL_FRONT
&& /* copy from front buf */
673 ctx
->ReadBuffer
->_ColorReadBuffer
&&
674 ctx
->DrawBuffer
->_ColorDrawBuffers
[0]) {
675 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
677 if (swrast
->NewState
)
678 _swrast_validate_derived( ctx
);
680 if ((swrast
->_RasterMask
& ~CLIP_BIT
) == 0x0 &&
682 ctx
->ReadBuffer
->_ColorReadBuffer
&&
684 ctx
->DrawBuffer
->_ColorDrawBuffers
[0]) {
685 struct xmesa_renderbuffer
*srcXrb
686 = xmesa_renderbuffer(ctx
->ReadBuffer
->_ColorReadBuffer
->Wrapped
);
687 struct xmesa_renderbuffer
*dstXrb
688 = xmesa_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]->Wrapped
);
689 if (srcXrb
->pixmap
&& dstXrb
->pixmap
) {
699 * Implement glCopyPixels for the front color buffer (or back buffer Pixmap)
700 * for the color buffer. Don't support zooming, pixel transfer, etc.
701 * We do support copying from one window to another, ala glXMakeCurrentRead.
704 xmesa_CopyPixels( struct gl_context
*ctx
,
705 GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
706 GLint destx
, GLint desty
, GLenum type
)
708 if (can_do_CopyPixels(ctx
, type
)) {
709 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
710 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
711 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
712 const XMesaGC gc
= xmbuf
->cleargc
; /* effected by glColorMask */
713 struct xmesa_renderbuffer
*srcXrb
714 = xmesa_renderbuffer(ctx
->ReadBuffer
->_ColorReadBuffer
->Wrapped
);
715 struct xmesa_renderbuffer
*dstXrb
716 = xmesa_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]->Wrapped
);
721 /* Note: we don't do any special clipping work here. We could,
722 * but X will do it for us.
724 srcy
= YFLIP(srcXrb
, srcy
) - height
+ 1;
725 desty
= YFLIP(dstXrb
, desty
) - height
+ 1;
726 XCopyArea(dpy
, srcXrb
->pixmap
, dstXrb
->pixmap
, gc
,
727 srcx
, srcy
, width
, height
, destx
, desty
);
730 _swrast_CopyPixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
, type
);
734 #endif /* XFree86Server */
739 * Every driver should implement a GetString function in order to
740 * return a meaningful GL_RENDERER string.
742 static const GLubyte
*
743 get_string( struct gl_context
*ctx
, GLenum name
)
749 return (const GLubyte
*) "Mesa GLX Indirect";
751 return (const GLubyte
*) "Mesa X11";
755 return (const GLubyte
*) "Mesa project: www.mesa3d.org";
766 * We implement the glEnable function only because we care about
767 * dither enable/disable.
770 enable( struct gl_context
*ctx
, GLenum pname
, GLboolean state
)
772 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
777 xmesa
->pixelformat
= xmesa
->xm_visual
->dithered_pf
;
779 xmesa
->pixelformat
= xmesa
->xm_visual
->undithered_pf
;
782 ; /* silence compiler warning */
788 clear_color_HPCR_ximage( struct gl_context
*ctx
, const GLfloat color
[4] )
791 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
793 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[0], color
[0]);
794 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[1], color
[1]);
795 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[2], color
[2]);
796 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[3], color
[3]);
798 if (color
[0] == 0.0 && color
[1] == 0.0 && color
[2] == 0.0) {
800 memset( xmesa
->xm_visual
->hpcr_clear_ximage_pattern
, 0x0 ,
801 sizeof(xmesa
->xm_visual
->hpcr_clear_ximage_pattern
));
804 /* build clear pattern */
805 for (i
=0; i
<16; i
++) {
806 xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0][i
] =
808 xmesa
->clearcolor
[0],
809 xmesa
->clearcolor
[1],
810 xmesa
->clearcolor
[2]);
811 xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[1][i
] =
813 xmesa
->clearcolor
[0],
814 xmesa
->clearcolor
[1],
815 xmesa
->clearcolor
[2]);
822 clear_color_HPCR_pixmap( struct gl_context
*ctx
, const GLfloat color
[4] )
825 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
827 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[0], color
[0]);
828 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[1], color
[1]);
829 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[2], color
[2]);
830 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[3], color
[3]);
832 if (color
[0] == 0.0 && color
[1] == 0.0 && color
[2] == 0.0) {
834 for (i
=0; i
<16; i
++) {
835 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 0, 0);
836 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 1, 0);
840 for (i
=0; i
<16; i
++) {
841 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 0,
843 xmesa
->clearcolor
[0],
844 xmesa
->clearcolor
[1],
845 xmesa
->clearcolor
[2]));
846 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 1,
848 xmesa
->clearcolor
[0],
849 xmesa
->clearcolor
[1],
850 xmesa
->clearcolor
[2]));
853 /* change tile pixmap content */
854 XMesaPutImage(xmesa
->display
,
855 (XMesaDrawable
)xmesa
->xm_visual
->hpcr_clear_pixmap
,
856 XMESA_BUFFER(ctx
->DrawBuffer
)->cleargc
,
857 xmesa
->xm_visual
->hpcr_clear_ximage
, 0, 0, 0, 0, 16, 2);
862 * Called when the driver should update its state, based on the new_state
866 xmesa_update_state( struct gl_context
*ctx
, GLbitfield new_state
)
868 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
870 /* Propagate statechange information to swrast and swrast_setup
871 * modules. The X11 driver has no internal GL-dependent state.
873 _swrast_InvalidateState( ctx
, new_state
);
874 _tnl_InvalidateState( ctx
, new_state
);
875 _vbo_InvalidateState( ctx
, new_state
);
876 _swsetup_InvalidateState( ctx
, new_state
);
878 if (ctx
->DrawBuffer
->Name
!= 0)
882 * GL_DITHER, GL_READ/DRAW_BUFFER, buffer binding state, etc. effect
883 * renderbuffer span/clear funcs.
884 * Check _NEW_COLOR to detect dither enable/disable.
886 if (new_state
& (_NEW_COLOR
| _NEW_BUFFERS
)) {
887 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
888 struct xmesa_renderbuffer
*front_xrb
, *back_xrb
;
890 front_xrb
= xmbuf
->frontxrb
;
892 xmesa_set_renderbuffer_funcs(front_xrb
, xmesa
->pixelformat
,
893 xmesa
->xm_visual
->BitsPerPixel
);
894 front_xrb
->clearFunc
= clear_pixmap
;
897 back_xrb
= xmbuf
->backxrb
;
899 xmesa_set_renderbuffer_funcs(back_xrb
, xmesa
->pixelformat
,
900 xmesa
->xm_visual
->BitsPerPixel
);
901 if (xmbuf
->backxrb
->pixmap
) {
902 back_xrb
->clearFunc
= clear_pixmap
;
905 switch (xmesa
->xm_visual
->BitsPerPixel
) {
907 if (xmesa
->xm_visual
->hpcr_clear_flag
) {
908 back_xrb
->clearFunc
= clear_HPCR_ximage
;
911 back_xrb
->clearFunc
= clear_8bit_ximage
;
915 back_xrb
->clearFunc
= clear_16bit_ximage
;
918 back_xrb
->clearFunc
= clear_24bit_ximage
;
921 back_xrb
->clearFunc
= clear_32bit_ximage
;
924 back_xrb
->clearFunc
= clear_nbit_ximage
;
931 if (xmesa
->xm_visual
->hpcr_clear_flag
) {
932 /* this depends on whether we're drawing to the front or back buffer */
936 ctx
->Driver
.ClearColor
= clear_color_HPCR_pixmap
;
939 ctx
->Driver
.ClearColor
= clear_color_HPCR_ximage
;
942 (void) clear_color_HPCR_pixmap
;
943 (void) clear_color_HPCR_ximage
;
951 * Called via ctx->Driver.TestProxyTeximage(). Normally, we'd just use
952 * the _mesa_test_proxy_teximage() fallback function, but we're going to
953 * special-case the 3D texture case to allow textures up to 512x512x32
957 test_proxy_teximage(struct gl_context
*ctx
, GLenum target
, GLint level
,
958 GLint internalFormat
, GLenum format
, GLenum type
,
959 GLint width
, GLint height
, GLint depth
, GLint border
)
961 if (target
== GL_PROXY_TEXTURE_3D
) {
962 /* special case for 3D textures */
963 if (width
* height
* depth
> 512 * 512 * 64 ||
964 width
< 2 * border
||
965 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
966 _mesa_bitcount(width
- 2 * border
) != 1) ||
967 height
< 2 * border
||
968 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
969 _mesa_bitcount(height
- 2 * border
) != 1) ||
970 depth
< 2 * border
||
971 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
972 _mesa_bitcount(depth
- 2 * border
) != 1)) {
973 /* Bad size, or too many texels */
979 /* use the fallback routine for 1D, 2D, cube and rect targets */
980 return _mesa_test_proxy_teximage(ctx
, target
, level
, internalFormat
,
981 format
, type
, width
, height
, depth
,
988 * In SW, we don't really compress GL_COMPRESSED_RGB[A] textures!
991 choose_tex_format( struct gl_context
*ctx
, GLint internalFormat
,
992 GLenum format
, GLenum type
)
994 switch (internalFormat
) {
995 case GL_COMPRESSED_RGB_ARB
:
996 return MESA_FORMAT_RGB888
;
997 case GL_COMPRESSED_RGBA_ARB
:
998 return MESA_FORMAT_RGBA8888
;
1000 return _mesa_choose_tex_format(ctx
, internalFormat
, format
, type
);
1006 * Called by glViewport.
1007 * This is a good time for us to poll the current X window size and adjust
1008 * our renderbuffers to match the current window size.
1009 * Remember, we have no opportunity to respond to conventional
1010 * X Resize/StructureNotify events since the X driver has no event loop.
1012 * Note that this trick isn't fool-proof. If the application never calls
1013 * glViewport, our notion of the current window size may be incorrect.
1014 * That problem led to the GLX_MESA_resize_buffers extension.
1017 xmesa_viewport(struct gl_context
*ctx
, GLint x
, GLint y
, GLsizei w
, GLsizei h
)
1019 XMesaContext xmctx
= XMESA_CONTEXT(ctx
);
1020 XMesaBuffer xmdrawbuf
= XMESA_BUFFER(ctx
->WinSysDrawBuffer
);
1021 XMesaBuffer xmreadbuf
= XMESA_BUFFER(ctx
->WinSysReadBuffer
);
1022 xmesa_check_and_update_buffer_size(xmctx
, xmdrawbuf
);
1023 xmesa_check_and_update_buffer_size(xmctx
, xmreadbuf
);
1031 #if ENABLE_EXT_timer_query
1034 * The GL_EXT_timer_query extension is not enabled for the XServer
1035 * indirect renderer. Not sure about how/if wrapping of gettimeofday()
1039 struct xmesa_query_object
1041 struct gl_query_object Base
;
1042 struct timeval StartTime
;
1046 static struct gl_query_object
*
1047 xmesa_new_query_object(struct gl_context
*ctx
, GLuint id
)
1049 struct xmesa_query_object
*q
= CALLOC_STRUCT(xmesa_query_object
);
1052 q
->Base
.Ready
= GL_TRUE
;
1059 xmesa_begin_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
1061 if (q
->Target
== GL_TIME_ELAPSED_EXT
) {
1062 struct xmesa_query_object
*xq
= (struct xmesa_query_object
*) q
;
1063 (void) gettimeofday(&xq
->StartTime
, NULL
);
1069 * Return the difference between the two given times in microseconds.
1072 #define suseconds_t unsigned int
1075 time_diff(const struct timeval
*t0
, const struct timeval
*t1
)
1077 GLuint64EXT seconds0
= t0
->tv_sec
& 0xff; /* 0 .. 255 seconds */
1078 GLuint64EXT seconds1
= t1
->tv_sec
& 0xff; /* 0 .. 255 seconds */
1079 GLuint64EXT nanosec0
= (seconds0
* 1000000 + t0
->tv_usec
) * 1000;
1080 GLuint64EXT nanosec1
= (seconds1
* 1000000 + t1
->tv_usec
) * 1000;
1081 return nanosec1
- nanosec0
;
1086 xmesa_end_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
1088 if (q
->Target
== GL_TIME_ELAPSED_EXT
) {
1089 struct xmesa_query_object
*xq
= (struct xmesa_query_object
*) q
;
1090 struct timeval endTime
;
1091 (void) gettimeofday(&endTime
, NULL
);
1092 /* result is in nanoseconds! */
1093 q
->Result
= time_diff(&xq
->StartTime
, &endTime
);
1098 #endif /* ENABLE_timer_query */
1102 * Initialize the device driver function table with the functions
1103 * we implement in this driver.
1106 xmesa_init_driver_functions( XMesaVisual xmvisual
,
1107 struct dd_function_table
*driver
)
1109 driver
->GetString
= get_string
;
1110 driver
->UpdateState
= xmesa_update_state
;
1111 driver
->GetBufferSize
= NULL
; /* OBSOLETE */
1112 driver
->Flush
= finish_or_flush
;
1113 driver
->Finish
= finish_or_flush
;
1114 driver
->ClearColor
= clear_color
;
1115 driver
->ColorMask
= color_mask
;
1116 driver
->Enable
= enable
;
1117 driver
->Viewport
= xmesa_viewport
;
1118 if (TEST_META_FUNCS
) {
1119 driver
->Clear
= _mesa_meta_Clear
;
1120 driver
->CopyPixels
= _mesa_meta_CopyPixels
;
1121 driver
->BlitFramebuffer
= _mesa_meta_BlitFramebuffer
;
1122 driver
->DrawPixels
= _mesa_meta_DrawPixels
;
1123 driver
->Bitmap
= _mesa_meta_Bitmap
;
1126 driver
->Clear
= clear_buffers
;
1127 #ifndef XFree86Server
1128 driver
->CopyPixels
= xmesa_CopyPixels
;
1129 if (xmvisual
->undithered_pf
== PF_8R8G8B
&&
1130 xmvisual
->dithered_pf
== PF_8R8G8B
&&
1131 xmvisual
->BitsPerPixel
== 32) {
1132 driver
->DrawPixels
= xmesa_DrawPixels_8R8G8B
;
1134 else if (xmvisual
->undithered_pf
== PF_5R6G5B
) {
1135 driver
->DrawPixels
= xmesa_DrawPixels_5R6G5B
;
1139 driver
->TestProxyTexImage
= test_proxy_teximage
;
1140 #if ENABLE_EXT_texure_compression_s3tc
1141 driver
->ChooseTextureFormat
= choose_tex_format
;
1143 (void) choose_tex_format
;
1146 #if ENABLE_EXT_timer_query
1147 driver
->NewQueryObject
= xmesa_new_query_object
;
1148 driver
->BeginQuery
= xmesa_begin_query
;
1149 driver
->EndQuery
= xmesa_end_query
;
1154 #define XMESA_NEW_POINT (_NEW_POINT | \
1156 _SWRAST_NEW_RASTERMASK)
1158 #define XMESA_NEW_LINE (_NEW_LINE | \
1163 _SWRAST_NEW_RASTERMASK)
1165 #define XMESA_NEW_TRIANGLE (_NEW_POLYGON | \
1170 _SWRAST_NEW_RASTERMASK)
1174 * Extend the software rasterizer with our line/point/triangle
1176 * Called during context creation only.
1178 void xmesa_register_swrast_functions( struct gl_context
*ctx
)
1180 SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
1182 swrast
->choose_point
= xmesa_choose_point
;
1183 swrast
->choose_line
= xmesa_choose_line
;
1184 swrast
->choose_triangle
= xmesa_choose_triangle
;
1186 /* XXX these lines have no net effect. Remove??? */
1187 swrast
->InvalidatePointMask
|= XMESA_NEW_POINT
;
1188 swrast
->InvalidateLineMask
|= XMESA_NEW_LINE
;
1189 swrast
->InvalidateTriangleMask
|= XMESA_NEW_TRIANGLE
;