2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2004 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
->pixmap
->width
, MAX_WIDTH
);
109 winheight
= MIN2(xmBuffer
->frontxrb
->pixmap
->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
, &root
,
118 &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
);
145 * This chooses the color buffer for reading and writing spans, points,
146 * lines, and triangles.
147 * Remember that a GLframebuffer has several distinct color buffers:
148 * front/left, front/right, back/left, back/right and aux buffers.
149 * The bufferBit specifies which one to use.
152 xmesa_set_buffer( GLcontext
*ctx
, GLframebuffer
*buffer
, GLuint bufferBit
)
155 /* We can make this cast since the XMesaBuffer wraps GLframebuffer.
156 * GLframebuffer is the first member in a XMesaBuffer struct.
158 XMesaBuffer target
= (XMesaBuffer
) buffer
;
159 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
162 if (buffer
->Name
!= 0)
166 /* This assignment tells the span/point/line/triangle functions
167 * which XMesaBuffer to use.
169 /* xmesa->xm_buffer = target;*/
172 * Now determine front vs back color buffer.
174 if (bufferBit
== BUFFER_BIT_FRONT_LEFT
) {
175 target
->buffer
= target
->frontxrb
->pixmap
;
176 printf("set get/put!\n");
177 xmesa_update_span_funcs(ctx
);
179 else if (bufferBit
== BUFFER_BIT_BACK_LEFT
) {
180 ASSERT(target
->db_state
);
181 if (target
->backxrb
->pixmap
) {
182 /* back buffer is a pixmap */
183 target
->buffer
= (XMesaDrawable
) target
->backxrb
->pixmap
;
185 else if (target
->backimage
) {
186 /* back buffer is an XImage */
187 target
->buffer
= None
;
190 /* No back buffer!!!! Must be out of memory, use front buffer */
191 target
->buffer
= target
->frontxrb
->pixmap
;
193 printf("set get/put!\n");
194 xmesa_update_span_funcs(ctx
);
196 else if (bufferBit
& (BUFFER_BIT_AUX0
| BUFFER_BIT_AUX1
| BUFFER_BIT_AUX2
| BUFFER_BIT_AUX3
)) {
197 /*_swrast_use_aux_buffer(ctx, buffer, bufferBit);*/
200 _mesa_problem(ctx
, "invalid buffer 0x%x in set_buffer() in xm_dd.c");
209 clear_index( GLcontext
*ctx
, GLuint index
)
211 if (ctx
->DrawBuffer
->Name
== 0) {
212 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
213 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
214 xmesa
->clearpixel
= (unsigned long) index
;
215 XMesaSetForeground( xmesa
->display
, xmbuf
->cleargc
, (unsigned long) index
);
221 clear_color( GLcontext
*ctx
, const GLfloat color
[4] )
223 if (ctx
->DrawBuffer
->Name
== 0) {
224 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
225 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
227 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[0], color
[0]);
228 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[1], color
[1]);
229 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[2], color
[2]);
230 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[3], color
[3]);
231 xmesa
->clearpixel
= xmesa_color_to_pixel( ctx
,
232 xmesa
->clearcolor
[0],
233 xmesa
->clearcolor
[1],
234 xmesa
->clearcolor
[2],
235 xmesa
->clearcolor
[3],
236 xmesa
->xm_visual
->undithered_pf
);
237 _glthread_LOCK_MUTEX(_xmesa_lock
);
238 XMesaSetForeground( xmesa
->display
, xmbuf
->cleargc
,
240 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
246 /* Set index mask ala glIndexMask */
248 index_mask( GLcontext
*ctx
, GLuint mask
)
250 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
251 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
252 /* not sure this conditional is really needed */
253 if (xmbuf
->backxrb
&& xmbuf
->backxrb
->pixmap
) {
255 if (mask
==0xffffffff) {
256 m
= ((unsigned long)~0L);
259 m
= (unsigned long) mask
;
261 XMesaSetPlaneMask( xmesa
->display
, xmbuf
->cleargc
, m
);
262 XMesaSetPlaneMask( xmesa
->display
, xmbuf
->gc
, m
);
267 /* Implements glColorMask() */
269 color_mask(GLcontext
*ctx
,
270 GLboolean rmask
, GLboolean gmask
, GLboolean bmask
, GLboolean amask
)
272 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
273 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
274 const int xclass
= xmesa
->xm_visual
->mesa_visual
.visualType
;
277 if (xclass
== GLX_TRUE_COLOR
|| xclass
== GLX_DIRECT_COLOR
) {
279 if (rmask
&& gmask
&& bmask
) {
280 m
= ((unsigned long)~0L);
284 if (rmask
) m
|= GET_REDMASK(xmesa
->xm_visual
);
285 if (gmask
) m
|= GET_GREENMASK(xmesa
->xm_visual
);
286 if (bmask
) m
|= GET_BLUEMASK(xmesa
->xm_visual
);
288 XMesaSetPlaneMask( xmesa
->display
, xmbuf
->cleargc
, m
);
289 XMesaSetPlaneMask( xmesa
->display
, xmbuf
->gc
, m
);
295 /**********************************************************************/
296 /*** glClear implementations ***/
297 /**********************************************************************/
301 * Clear the front or back color buffer, if it's implemented with a pixmap.
304 clear_pixmap(GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
, GLboolean all
,
305 GLint x
, GLint y
, GLint width
, GLint height
)
307 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
308 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
310 assert(xrb
->pixmap
== xmbuf
->backxrb
->pixmap
||
311 xrb
->pixmap
== xmbuf
->frontxrb
->pixmap
);
314 XMesaFillRectangle( xmesa
->display
, xrb
->pixmap
, xmbuf
->cleargc
,
315 0, 0, xrb
->Base
.Width
+ 1, xrb
->Base
.Height
+ 1 );
318 XMesaFillRectangle( xmesa
->display
, xrb
->pixmap
, xmbuf
->cleargc
,
319 x
, xrb
->Base
.Height
- y
- height
,
326 clear_8bit_ximage( GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
327 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
329 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
332 const size_t n
= xrb
->ximage
->bytes_per_line
* xrb
->Base
.Height
;
333 MEMSET( xrb
->ximage
->data
, xmesa
->clearpixel
, n
);
337 for (i
=0;i
<height
;i
++) {
338 GLubyte
*ptr
= PIXEL_ADDR1(xrb
, x
, y
+ i
);
339 MEMSET( ptr
, xmesa
->clearpixel
, width
);
346 clear_HPCR_ximage( GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
347 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
349 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
352 GLint i
, c16
= (xrb
->ximage
->bytes_per_line
>>4)<<4;
353 GLubyte
*ptr
= (GLubyte
*) xrb
->ximage
->data
;
354 for (i
= 0; i
< xrb
->Base
.Height
; i
++) {
356 GLubyte
*sptr
= xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0];
360 for (j
=0; j
<c16
; j
+=16) {
379 for (; j
< xrb
->ximage
->bytes_per_line
; j
++) {
387 for (i
=y
; i
<y
+height
; i
++) {
388 GLubyte
*ptr
= PIXEL_ADDR1( xrb
, x
, i
);
390 const GLubyte
*sptr
= xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0];
394 for (j
=x
; j
<x
+width
; j
++) {
404 clear_16bit_ximage( GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
405 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
407 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
408 register GLuint pixel
= (GLuint
) xmesa
->clearpixel
;
410 if (xmesa
->swapbytes
) {
411 pixel
= ((pixel
>> 8) & 0x00ff) | ((pixel
<< 8) & 0xff00);
415 GLuint
*ptr4
= (GLuint
*) xrb
->ximage
->data
;
416 if ((pixel
& 0xff) == ((pixel
>> 8) & 0xff)) {
417 /* low and high bytes are equal so use memset() */
418 const GLuint n
= xrb
->ximage
->bytes_per_line
* xrb
->Base
.Height
;
419 MEMSET( ptr4
, pixel
& 0xff, n
);
422 const GLuint n
= xrb
->ximage
->bytes_per_line
* xrb
->Base
.Height
/ 4;
424 pixel
= pixel
| (pixel
<<16);
425 for (i
= 0; i
< n
; i
++) {
429 /* might be one last GLushort to set */
430 if ((xrb
->ximage
->bytes_per_line
* xrb
->Base
.Height
) & 0x2)
431 *(GLushort
*)ptr4
= pixel
& 0xffff;
436 for (j
=0;j
<height
;j
++) {
437 GLushort
*ptr2
= PIXEL_ADDR2(xrb
, x
, y
+ j
);
438 for (i
=0;i
<width
;i
++) {
446 /* Optimized code provided by Nozomi Ytow <noz@xfree86.org> */
448 clear_24bit_ximage(GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
449 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
451 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
452 const GLubyte r
= xmesa
->clearcolor
[0];
453 const GLubyte g
= xmesa
->clearcolor
[1];
454 const GLubyte b
= xmesa
->clearcolor
[2];
457 if (r
== g
&& g
== b
) {
458 /* same value for all three components (gray) */
459 const GLint w3
= xrb
->Base
.Width
* 3;
460 const GLint h
= xrb
->Base
.Height
;
462 for (i
= 0; i
< h
; i
++) {
463 bgr_t
*ptr3
= PIXEL_ADDR3(xrb
, 0, i
);
469 const GLint w
= xrb
->Base
.Width
;
470 const GLint h
= xrb
->Base
.Height
;
472 for (i
= 0; i
< h
; i
++) {
473 bgr_t
*ptr3
= PIXEL_ADDR3(xrb
, 0, i
);
474 for (j
= 0; j
< w
; j
++) {
484 /* only clear subrect of color buffer */
485 if (r
== g
&& g
== b
) {
486 /* same value for all three components (gray) */
488 for (j
=0;j
<height
;j
++) {
489 bgr_t
*ptr3
= PIXEL_ADDR3(xrb
, x
, y
+ j
);
490 MEMSET(ptr3
, r
, 3 * width
);
494 /* non-gray clear color */
496 for (j
= 0; j
< height
; j
++) {
497 bgr_t
*ptr3
= PIXEL_ADDR3(xrb
, x
, y
+ j
);
498 for (i
= 0; i
< width
; i
++) {
511 clear_32bit_ximage(GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
512 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
514 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
515 register GLuint pixel
= (GLuint
) xmesa
->clearpixel
;
517 if (xmesa
->swapbytes
) {
518 pixel
= ((pixel
>> 24) & 0x000000ff)
519 | ((pixel
>> 8) & 0x0000ff00)
520 | ((pixel
<< 8) & 0x00ff0000)
521 | ((pixel
<< 24) & 0xff000000);
525 const GLuint n
= xrb
->Base
.Width
* xrb
->Base
.Height
;
526 GLuint
*ptr4
= (GLuint
*) xrb
->ximage
->data
;
528 _mesa_memset(ptr4
, pixel
, 4 * n
);
532 for (i
= 0; i
< n
; i
++)
538 for (j
= 0; j
< height
; j
++) {
539 GLuint
*ptr4
= PIXEL_ADDR4(xrb
, x
, y
+ j
);
540 for (i
= 0; i
< width
; i
++) {
549 clear_nbit_ximage(GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
550 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
552 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
553 XMesaImage
*img
= xrb
->ximage
;
556 /* We can ignore 'all' here - x, y, width, height are always right */
559 /* TODO: optimize this */
561 for (j
= 0; j
< height
; j
++) {
562 for (i
= 0; i
< width
; i
++) {
563 XMesaPutPixel(img
, x
+i
, y
-j
, xmesa
->clearpixel
);
571 clear_buffers( GLcontext
*ctx
, GLbitfield mask
,
572 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
574 if (ctx
->DrawBuffer
->Name
== 0) {
575 /* this is a window system framebuffer */
576 const GLuint
*colorMask
= (GLuint
*) &ctx
->Color
.ColorMask
;
577 XMesaBuffer b
= (XMesaBuffer
) ctx
->DrawBuffer
;
579 /* we can't handle color or index masking */
580 if (*colorMask
== 0xffffffff && ctx
->Color
.IndexMask
== 0xffffffff) {
581 if (mask
& BUFFER_BIT_FRONT_LEFT
) {
582 /* clear front color buffer */
583 if (b
->frontxrb
== (struct xmesa_renderbuffer
*)
584 ctx
->DrawBuffer
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
) {
585 /* renderbuffer is not wrapped - great! */
586 b
->frontxrb
->clearFunc(ctx
, b
->frontxrb
, all
, x
, y
,
588 mask
&= ~BUFFER_BIT_FRONT_LEFT
;
591 /* we can't directly clear an alpha-wrapped color buffer */
594 if (mask
& BUFFER_BIT_BACK_LEFT
) {
595 /* clear back color buffer */
596 if (b
->backxrb
== (struct xmesa_renderbuffer
*)
597 ctx
->DrawBuffer
->Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
) {
598 /* renderbuffer is not wrapped - great! */
599 b
->backxrb
->clearFunc(ctx
, b
->backxrb
, all
, x
, y
,
601 mask
&= ~BUFFER_BIT_BACK_LEFT
;
607 _swrast_Clear( ctx
, mask
, all
, x
, y
, width
, height
);
612 * Called by ctx->Driver.ResizeBuffers()
613 * Resize the front/back colorbuffers to match the latest window size.
616 xmesa_resize_buffers(GLcontext
*ctx
, GLframebuffer
*buffer
,
617 GLuint width
, GLuint height
)
619 /* We can do this cast because the first field in the XMesaBuffer
620 * struct is a GLframebuffer struct. If this weren't true, we'd
621 * need a pointer from the GLframebuffer to the XMesaBuffer.
623 XMesaBuffer xmBuffer
= (XMesaBuffer
) buffer
;
625 xmesa_alloc_back_buffer(xmBuffer
, width
, height
);
628 _mesa_resize_framebuffer(ctx
, buffer
, width
, height
);
633 #ifndef XFree86Server
634 /* XXX this was never tested in the Xserver environment */
637 * This function implements glDrawPixels() with an XPutImage call when
638 * drawing to the front buffer (X Window drawable).
639 * The image format must be GL_BGRA to match the PF_8R8G8B pixel format.
642 xmesa_DrawPixels_8R8G8B( GLcontext
*ctx
,
643 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
644 GLenum format
, GLenum type
,
645 const struct gl_pixelstore_attrib
*unpack
,
646 const GLvoid
*pixels
)
648 struct xmesa_renderbuffer
*xrb
649 = (struct xmesa_renderbuffer
*) ctx
->DrawBuffer
->_ColorDrawBuffers
[0][0];
651 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
652 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
653 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
654 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
655 const XMesaGC gc
= xmbuf
->gc
;
659 ASSERT(xmesa
->xm_visual
->dithered_pf
== PF_8R8G8B
);
660 ASSERT(xmesa
->xm_visual
->undithered_pf
== PF_8R8G8B
);
662 if (swrast
->NewState
)
663 _swrast_validate_derived( ctx
);
667 type
== GL_UNSIGNED_BYTE
&&
668 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
669 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
670 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
671 ctx
->Pixel
.ZoomY
== 1.0) {
676 int srcX
= unpack
->SkipPixels
;
677 int srcY
= unpack
->SkipRows
;
678 int rowLength
= unpack
->RowLength
? unpack
->RowLength
: width
;
680 if (unpack
->BufferObj
->Name
) {
681 /* unpack from PBO */
683 if (!_mesa_validate_pbo_access(2, unpack
, width
, height
, 1,
684 format
, type
, pixels
)) {
685 _mesa_error(ctx
, GL_INVALID_OPERATION
,
686 "glDrawPixels(invalid PBO access)");
689 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
,
690 GL_PIXEL_UNPACK_BUFFER_EXT
,
694 /* buffer is already mapped - that's an error */
695 _mesa_error(ctx
, GL_INVALID_OPERATION
,
696 "glDrawPixels(PBO is mapped)");
699 pixels
= ADD_POINTERS(buf
, pixels
);
702 if (_mesa_clip_drawpixels(ctx
, &dstX
, &dstY
, &w
, &h
, &srcX
, &srcY
)) {
703 /* This is a little tricky since all coordinates up to now have
704 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
705 * so we have to carefully compute the Y coordinates/addresses here.
708 MEMSET(&ximage
, 0, sizeof(XMesaImage
));
709 ximage
.width
= width
;
710 ximage
.height
= height
;
711 ximage
.format
= ZPixmap
;
712 ximage
.data
= (char *) pixels
713 + ((srcY
+ h
- 1) * rowLength
+ srcX
) * 4;
714 ximage
.byte_order
= LSBFirst
;
715 ximage
.bitmap_unit
= 32;
716 ximage
.bitmap_bit_order
= LSBFirst
;
717 ximage
.bitmap_pad
= 32;
719 ximage
.bytes_per_line
= -rowLength
* 4; /* negative to flip image */
720 ximage
.bits_per_pixel
= 32;
721 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
722 /* flip Y axis for dest position */
723 dstY
= YFLIP(xrb
, dstY
) - h
+ 1;
724 XPutImage(dpy
, xrb
->pixmap
, gc
, &ximage
, 0, 0, dstX
, dstY
, w
, h
);
727 if (unpack
->BufferObj
->Name
) {
728 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
733 /* software fallback */
734 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
735 format
, type
, unpack
, pixels
);
742 * This function implements glDrawPixels() with an XPutImage call when
743 * drawing to the front buffer (X Window drawable). The image format
744 * must be GL_RGB and image type must be GL_UNSIGNED_SHORT_5_6_5 to
745 * match the PF_5R6G5B pixel format.
748 xmesa_DrawPixels_5R6G5B( GLcontext
*ctx
,
749 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
750 GLenum format
, GLenum type
,
751 const struct gl_pixelstore_attrib
*unpack
,
752 const GLvoid
*pixels
)
754 struct xmesa_renderbuffer
*xrb
755 = (struct xmesa_renderbuffer
*) ctx
->DrawBuffer
->_ColorDrawBuffers
[0][0];
756 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
757 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
758 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
759 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
760 const XMesaGC gc
= xmbuf
->gc
;
764 ASSERT(xmesa
->xm_visual
->undithered_pf
== PF_5R6G5B
);
766 if (swrast
->NewState
)
767 _swrast_validate_derived( ctx
);
771 type
== GL_UNSIGNED_SHORT_5_6_5
&&
772 !ctx
->Color
.DitherFlag
&& /* no dithering */
773 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
774 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
775 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
776 ctx
->Pixel
.ZoomY
== 1.0) {
781 int srcX
= unpack
->SkipPixels
;
782 int srcY
= unpack
->SkipRows
;
783 int rowLength
= unpack
->RowLength
? unpack
->RowLength
: width
;
785 if (unpack
->BufferObj
->Name
) {
786 /* unpack from PBO */
788 if (!_mesa_validate_pbo_access(2, unpack
, width
, height
, 1,
789 format
, type
, pixels
)) {
790 _mesa_error(ctx
, GL_INVALID_OPERATION
,
791 "glDrawPixels(invalid PBO access)");
794 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
,
795 GL_PIXEL_UNPACK_BUFFER_EXT
,
799 /* buffer is already mapped - that's an error */
800 _mesa_error(ctx
, GL_INVALID_OPERATION
,
801 "glDrawPixels(PBO is mapped)");
804 pixels
= ADD_POINTERS(buf
, pixels
);
807 if (_mesa_clip_drawpixels(ctx
, &dstX
, &dstY
, &w
, &h
, &srcX
, &srcY
)) {
808 /* This is a little tricky since all coordinates up to now have
809 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
810 * so we have to carefully compute the Y coordinates/addresses here.
813 MEMSET(&ximage
, 0, sizeof(XMesaImage
));
814 ximage
.width
= width
;
815 ximage
.height
= height
;
816 ximage
.format
= ZPixmap
;
817 ximage
.data
= (char *) pixels
818 + ((srcY
+ h
- 1) * rowLength
+ srcX
) * 2;
819 ximage
.byte_order
= LSBFirst
;
820 ximage
.bitmap_unit
= 16;
821 ximage
.bitmap_bit_order
= LSBFirst
;
822 ximage
.bitmap_pad
= 16;
824 ximage
.bytes_per_line
= -rowLength
* 2; /* negative to flip image */
825 ximage
.bits_per_pixel
= 16;
826 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
827 /* flip Y axis for dest position */
828 dstY
= YFLIP(xrb
, dstY
) - h
+ 1;
829 XPutImage(dpy
, xrb
->pixmap
, gc
, &ximage
, 0, 0, dstX
, dstY
, w
, h
);
832 if (unpack
->BufferObj
->Name
) {
833 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
838 /* software fallback */
839 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
840 format
, type
, unpack
, pixels
);
847 * Implement glCopyPixels for the front color buffer (or back buffer Pixmap)
848 * for the color buffer. Don't support zooming, pixel transfer, etc.
849 * We do support copying from one window to another, ala glXMakeCurrentRead.
852 xmesa_CopyPixels( GLcontext
*ctx
,
853 GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
854 GLint destx
, GLint desty
, GLenum type
)
856 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
857 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
858 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
859 const XMesaGC gc
= ((XMesaBuffer
) ctx
->DrawBuffer
)->gc
;
860 struct xmesa_renderbuffer
*srcXrb
= (struct xmesa_renderbuffer
*)
861 ctx
->ReadBuffer
->_ColorReadBuffer
;
862 struct xmesa_renderbuffer
*dstXrb
= (struct xmesa_renderbuffer
*)
863 ctx
->DrawBuffer
->_ColorDrawBuffers
[0][0];
868 if (swrast
->NewState
)
869 _swrast_validate_derived( ctx
);
871 if (ctx
->Color
.DrawBuffer
[0] == GL_FRONT
&&
872 ctx
->Pixel
.ReadBuffer
== GL_FRONT
&&
876 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
877 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
878 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
879 ctx
->Pixel
.ZoomY
== 1.0) {
880 /* Note: we don't do any special clipping work here. We could,
881 * but X will do it for us.
883 srcy
= YFLIP(srcXrb
, srcy
) - height
+ 1;
884 desty
= YFLIP(dstXrb
, desty
) - height
+ 1;
885 XCopyArea(dpy
, srcXrb
->pixmap
, dstXrb
->pixmap
, gc
,
886 srcx
, srcy
, width
, height
, destx
, desty
);
889 _swrast_CopyPixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
, type
);
892 #endif /* XFree86Server */
897 * Every driver should implement a GetString function in order to
898 * return a meaningful GL_RENDERER string.
900 static const GLubyte
*
901 get_string( GLcontext
*ctx
, GLenum name
)
907 return (const GLubyte
*) "Mesa GLX Indirect";
909 return (const GLubyte
*) "Mesa X11";
913 return (const GLubyte
*) "Mesa project: www.mesa3d.org";
924 * We implement the glEnable function only because we care about
925 * dither enable/disable.
928 enable( GLcontext
*ctx
, GLenum pname
, GLboolean state
)
930 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
935 xmesa
->pixelformat
= xmesa
->xm_visual
->dithered_pf
;
937 xmesa
->pixelformat
= xmesa
->xm_visual
->undithered_pf
;
940 ; /* silence compiler warning */
946 clear_color_HPCR_ximage( GLcontext
*ctx
, const GLfloat color
[4] )
949 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
951 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[0], color
[0]);
952 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[1], color
[1]);
953 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[2], color
[2]);
954 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[3], color
[3]);
956 if (color
[0] == 0.0 && color
[1] == 0.0 && color
[2] == 0.0) {
958 MEMSET( xmesa
->xm_visual
->hpcr_clear_ximage_pattern
, 0x0 ,
959 sizeof(xmesa
->xm_visual
->hpcr_clear_ximage_pattern
));
962 /* build clear pattern */
963 for (i
=0; i
<16; i
++) {
964 xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0][i
] =
966 xmesa
->clearcolor
[0],
967 xmesa
->clearcolor
[1],
968 xmesa
->clearcolor
[2]);
969 xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[1][i
] =
971 xmesa
->clearcolor
[0],
972 xmesa
->clearcolor
[1],
973 xmesa
->clearcolor
[2]);
980 clear_color_HPCR_pixmap( GLcontext
*ctx
, const GLfloat color
[4] )
983 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
985 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[0], color
[0]);
986 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[1], color
[1]);
987 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[2], color
[2]);
988 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[3], color
[3]);
990 if (color
[0] == 0.0 && color
[1] == 0.0 && color
[2] == 0.0) {
992 for (i
=0; i
<16; i
++) {
993 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 0, 0);
994 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 1, 0);
998 for (i
=0; i
<16; i
++) {
999 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 0,
1001 xmesa
->clearcolor
[0],
1002 xmesa
->clearcolor
[1],
1003 xmesa
->clearcolor
[2]));
1004 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 1,
1006 xmesa
->clearcolor
[0],
1007 xmesa
->clearcolor
[1],
1008 xmesa
->clearcolor
[2]));
1011 /* change tile pixmap content */
1012 XMesaPutImage(xmesa
->display
,
1013 (XMesaDrawable
)xmesa
->xm_visual
->hpcr_clear_pixmap
,
1014 XMESA_BUFFER(ctx
->DrawBuffer
)->cleargc
,
1015 xmesa
->xm_visual
->hpcr_clear_ximage
, 0, 0, 0, 0, 16, 2);
1020 * Called when the driver should update it's state, based on the new_state
1024 xmesa_update_state( GLcontext
*ctx
, GLuint new_state
)
1026 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
1027 struct xmesa_renderbuffer
*front_xrb
, *back_xrb
;
1029 /* Propagate statechange information to swrast and swrast_setup
1030 * modules. The X11 driver has no internal GL-dependent state.
1032 _swrast_InvalidateState( ctx
, new_state
);
1033 _ac_InvalidateState( ctx
, new_state
);
1034 _tnl_InvalidateState( ctx
, new_state
);
1035 _swsetup_InvalidateState( ctx
, new_state
);
1037 if (ctx
->DrawBuffer
->Name
!= 0)
1040 front_xrb
= XMESA_BUFFER(ctx
->DrawBuffer
)->frontxrb
;
1042 /* XXX check for relevant new_state flags */
1043 xmesa_set_renderbuffer_funcs(front_xrb
, xmesa
->pixelformat
,
1044 xmesa
->xm_visual
->BitsPerPixel
);
1045 /* setup pointers to front and back buffer clear functions */
1046 front_xrb
->clearFunc
= clear_pixmap
;
1049 back_xrb
= XMESA_BUFFER(ctx
->DrawBuffer
)->backxrb
;
1051 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
1053 /* XXX check for relevant new_state flags */
1054 xmesa_set_renderbuffer_funcs(back_xrb
, xmesa
->pixelformat
,
1055 xmesa
->xm_visual
->BitsPerPixel
);
1057 if (xmbuf
->backxrb
->pixmap
) {
1058 back_xrb
->clearFunc
= clear_pixmap
;
1061 switch (xmesa
->xm_visual
->BitsPerPixel
) {
1063 if (xmesa
->xm_visual
->hpcr_clear_flag
) {
1064 back_xrb
->clearFunc
= clear_HPCR_ximage
;
1067 back_xrb
->clearFunc
= clear_8bit_ximage
;
1071 back_xrb
->clearFunc
= clear_16bit_ximage
;
1074 back_xrb
->clearFunc
= clear_24bit_ximage
;
1077 back_xrb
->clearFunc
= clear_32bit_ximage
;
1080 back_xrb
->clearFunc
= clear_nbit_ximage
;
1086 #if OLD_RENDERBUFFER && 0
1087 if (ctx
->DrawBuffer
->_ColorDrawBufferMask
[0] & (BUFFER_BIT_FRONT_LEFT
| BUFFER_BIT_BACK_LEFT
)) {
1088 xmesa_update_span_funcs(ctx
);
1092 if (xmesa
->xm_visual
->hpcr_clear_flag
) {
1093 /* this depends on whether we're drawing to the front or back buffer */
1097 ctx
->Driver
.ClearColor
= clear_color_HPCR_pixmap
;
1100 ctx
->Driver
.ClearColor
= clear_color_HPCR_ximage
;
1103 (void) clear_color_HPCR_pixmap
;
1104 (void) clear_color_HPCR_ximage
;
1112 * Called via ctx->Driver.TestProxyTeximage(). Normally, we'd just use
1113 * the _mesa_test_proxy_teximage() fallback function, but we're going to
1114 * special-case the 3D texture case to allow textures up to 512x512x32
1118 test_proxy_teximage(GLcontext
*ctx
, GLenum target
, GLint level
,
1119 GLint internalFormat
, GLenum format
, GLenum type
,
1120 GLint width
, GLint height
, GLint depth
, GLint border
)
1122 if (target
== GL_PROXY_TEXTURE_3D
) {
1123 /* special case for 3D textures */
1124 if (width
* height
* depth
> 512 * 512 * 64 ||
1125 width
< 2 * border
||
1126 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1127 _mesa_bitcount(width
- 2 * border
) != 1) ||
1128 height
< 2 * border
||
1129 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1130 _mesa_bitcount(height
- 2 * border
) != 1) ||
1131 depth
< 2 * border
||
1132 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1133 _mesa_bitcount(depth
- 2 * border
) != 1)) {
1134 /* Bad size, or too many texels */
1140 /* use the fallback routine for 1D, 2D, cube and rect targets */
1141 return _mesa_test_proxy_teximage(ctx
, target
, level
, internalFormat
,
1142 format
, type
, width
, height
, depth
,
1149 * In SW, we don't really compress GL_COMPRESSED_RGB[A] textures!
1151 static const struct gl_texture_format
*
1152 choose_tex_format( GLcontext
*ctx
, GLint internalFormat
,
1153 GLenum format
, GLenum type
)
1155 switch (internalFormat
) {
1156 case GL_COMPRESSED_RGB_ARB
:
1157 return &_mesa_texformat_rgb
;
1158 case GL_COMPRESSED_RGBA_ARB
:
1159 return &_mesa_texformat_rgba
;
1161 return _mesa_choose_tex_format(ctx
, internalFormat
, format
, type
);
1167 * Called by glViewport.
1168 * This is a good time for us to poll the current X window size and adjust
1169 * our renderbuffers to match the current window size.
1170 * Remember, we have no opportunity to respond to conventional
1171 * X Resize/StructureNotify events since the X driver has no event loop.
1173 * Note that this trick isn't fool-proof. If the application never calls
1174 * glViewport, our notion of the current window size may be incorrect.
1177 xmesa_viewport(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei w
, GLsizei h
)
1180 struct gl_framebuffer
*fb
= ctx
->WinSysDrawBuffer
;
1181 GLuint newWidth
, newHeight
;
1184 printf("%s before %d x %d\n", __FUNCTION__, fb->Width, fb->Height);
1187 get_buffer_size(fb
, &newWidth
, &newHeight
);
1188 if (newWidth
!= fb
->Width
|| newHeight
!= fb
->Height
) {
1189 xmesa_resize_buffers(ctx
, fb
, newWidth
, newHeight
);
1190 ctx
->NewState
|= _NEW_BUFFERS
; /* to update scissor / window bounds */
1193 printf("%s after %d x %d\n", __FUNCTION__, fb->Width, fb->Height);
1196 /* This also works: */
1197 _mesa_ResizeBuffersMESA();
1203 * Initialize the device driver function table with the functions
1204 * we implement in this driver.
1207 xmesa_init_driver_functions( XMesaVisual xmvisual
,
1208 struct dd_function_table
*driver
)
1210 driver
->GetString
= get_string
;
1211 driver
->UpdateState
= xmesa_update_state
;
1212 driver
->GetBufferSize
= get_buffer_size
;
1213 driver
->Flush
= finish_or_flush
;
1214 driver
->Finish
= finish_or_flush
;
1215 driver
->ClearIndex
= clear_index
;
1216 driver
->ClearColor
= clear_color
;
1217 driver
->IndexMask
= index_mask
;
1218 driver
->ColorMask
= color_mask
;
1219 driver
->Enable
= enable
;
1220 driver
->Clear
= clear_buffers
;
1221 driver
->ResizeBuffers
= xmesa_resize_buffers
;
1222 driver
->Viewport
= xmesa_viewport
;
1223 #ifndef XFree86Server
1224 driver
->CopyPixels
= xmesa_CopyPixels
;
1225 if (xmvisual
->undithered_pf
== PF_8R8G8B
&&
1226 xmvisual
->dithered_pf
== PF_8R8G8B
) {
1227 driver
->DrawPixels
= xmesa_DrawPixels_8R8G8B
;
1229 else if (xmvisual
->undithered_pf
== PF_5R6G5B
) {
1230 driver
->DrawPixels
= xmesa_DrawPixels_5R6G5B
;
1233 driver
->TestProxyTexImage
= test_proxy_teximage
;
1235 driver
->ChooseTextureFormat
= choose_tex_format
;
1237 (void) choose_tex_format
;
1242 #define XMESA_NEW_POINT (_NEW_POINT | \
1244 _SWRAST_NEW_RASTERMASK)
1246 #define XMESA_NEW_LINE (_NEW_LINE | \
1251 _SWRAST_NEW_RASTERMASK)
1253 #define XMESA_NEW_TRIANGLE (_NEW_POLYGON | \
1258 _SWRAST_NEW_RASTERMASK)
1261 /* Extend the software rasterizer with our line/point/triangle
1264 void xmesa_register_swrast_functions( GLcontext
*ctx
)
1266 SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
1267 struct swrast_device_driver
*dd
= _swrast_GetDeviceDriverReference(ctx
);
1269 dd
->SetBuffer
= xmesa_set_buffer
;
1271 swrast
->choose_point
= xmesa_choose_point
;
1272 swrast
->choose_line
= xmesa_choose_line
;
1273 swrast
->choose_triangle
= xmesa_choose_triangle
;
1275 swrast
->invalidate_point
|= XMESA_NEW_POINT
;
1276 swrast
->invalidate_line
|= XMESA_NEW_LINE
;
1277 swrast
->invalidate_triangle
|= XMESA_NEW_TRIANGLE
;