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.
31 # include "GL/xf86glx.h"
32 # include "xf86glx_util.h"
33 #elif defined(USE_XSHM)
34 # include <X11/extensions/XShm.h>
39 #include "GL/fxmesa.h"
40 #include "../glide/fxdrv.h"
44 extern _glthread_Mutex _xmesa_lock
;
47 /* for PF_8R8G8B24 pixel format */
55 /* Function pointer for clearing color buffers */
56 typedef void (*clear_func
)( GLcontext
*ctx
,
57 GLboolean all
, GLint x
, GLint y
,
58 GLint width
, GLint height
);
63 /** Framebuffer pixel formats */
65 PF_Index
, /**< Color Index mode */
66 PF_Truecolor
, /**< TrueColor or DirectColor, any depth */
67 PF_Dither_True
, /**< TrueColor with dithering */
68 PF_8A8B8G8R
, /**< 32-bit TrueColor: 8-A, 8-B, 8-G, 8-R */
69 PF_8R8G8B
, /**< 32-bit TrueColor: 8-R, 8-G, 8-B bits */
70 PF_5R6G5B
, /**< 16-bit TrueColor: 5-R, 6-G, 5-B bits */
71 PF_Dither
, /**< Color-mapped RGB with dither */
72 PF_Lookup
, /**< Color-mapped RGB without dither */
73 PF_HPCR
, /**< HP Color Recovery (ad@lms.be 30/08/95) */
74 PF_1Bit
, /**< monochrome dithering of RGB */
75 PF_Grayscale
, /**< Grayscale or StaticGray */
76 PF_8R8G8B24
, /**< 24-bit TrueColor: 8-R, 8-G, 8-B bits */
77 PF_Dither_5R6G5B
, /**< 16-bit dithered TrueColor: 5-R, 6-G, 5-B */
78 PF_8A8R8G8B
/**< 32-bit TrueColor: 8-A, 8-R, 8-G, 8-B */
83 * "Derived" from GLvisual. Basically corresponds to an XVisualInfo.
86 GLvisual mesa_visual
; /* Device independent visual parameters */
87 XMesaDisplay
*display
; /* The X11 display */
89 GLint ColormapEntries
;
92 XMesaVisualInfo visinfo
; /* X's visual info (pointer to private copy) */
93 XVisualInfo
*vishandle
; /* Only used in fakeglx.c */
95 GLint BitsPerPixel
; /* True bits per pixel for XImages */
97 GLboolean ximage_flag
; /* Use XImage for back buffer (not pixmap)? */
99 enum pixel_format dithered_pf
; /* Pixel format when dithering */
100 enum pixel_format undithered_pf
;/* Pixel format when not dithering */
102 GLfloat RedGamma
; /* Gamma values, 1.0 is default */
106 /* For PF_TRUECOLOR */
107 GLint rshift
, gshift
, bshift
;/* Pixel color component shifts */
108 GLubyte Kernel
[16]; /* Dither kernel */
109 unsigned long RtoPixel
[512]; /* RGB to pixel conversion */
110 unsigned long GtoPixel
[512];
111 unsigned long BtoPixel
[512];
112 GLubyte PixelToR
[256]; /* Pixel to RGB conversion */
113 GLubyte PixelToG
[256];
114 GLubyte PixelToB
[256];
117 short hpcr_rgbTbl
[3][256];
118 GLboolean hpcr_clear_flag
;
119 GLubyte hpcr_clear_ximage_pattern
[2][16];
120 XMesaImage
*hpcr_clear_ximage
;
121 XMesaPixmap hpcr_clear_pixmap
;
129 * "Derived" from __GLcontextRec. Basically corresponds to a GLXContext.
131 struct xmesa_context
{
132 GLcontext mesa
; /* the core library context (containment) */
133 XMesaVisual xm_visual
; /* Describes the buffers */
134 XMesaBuffer xm_draw_buffer
; /* current draw framebuffer */
135 XMesaBuffer xm_read_buffer
; /* current read framebuffer */
136 XMesaBuffer xm_buffer
; /* current span/point/line/triangle buffer */
138 XMesaDisplay
*display
; /* == xm_visual->display */
139 GLboolean swapbytes
; /* Host byte order != display byte order? */
140 GLboolean direct
; /* Direct rendering context? */
142 enum pixel_format pixelformat
;
144 GLubyte clearcolor
[4]; /* current clearing color */
145 unsigned long clearpixel
; /* current clearing pixel value */
151 WINDOW
, /* An X window */
152 GLXWINDOW
, /* GLX window */
153 PIXMAP
, /* GLX pixmap */
154 PBUFFER
/* GLX Pbuffer */
159 * "Derived" from GLframebuffer. Basically corresponds to a GLXDrawable.
161 struct xmesa_buffer
{
162 GLframebuffer mesa_buffer
; /* depth, stencil, accum, etc buffers */
163 /* This MUST BE FIRST! */
164 GLboolean wasCurrent
; /* was ever the current buffer? */
165 XMesaVisual xm_visual
; /* the X/Mesa visual */
167 XMesaDisplay
*display
;
168 BufferType type
; /* window, pixmap, pbuffer or glxwindow */
169 XMesaDrawable frontbuffer
; /* either a window or pixmap */
170 XMesaPixmap backpixmap
; /* back buffer Pixmap */
171 XMesaImage
*backimage
; /* back buffer simulated XImage */
173 XMesaDrawable buffer
; /* the current buffer, either equal to */
174 /* frontbuffer, backpixmap or XIMAGE (None) */
176 XMesaColormap cmap
; /* the X colormap */
178 unsigned long selectedEvents
;/* for pbuffers only */
180 GLint db_state
; /* 0 = single buffered */
181 /* BACK_PIXMAP = use Pixmap for back buffer */
182 /* BACK_XIMAGE = use XImage for back buffer */
184 #ifndef XFree86Server
185 GLuint shm
; /* X Shared Memory extension status: */
186 /* 0 = not available */
187 /* 1 = XImage support available */
188 /* 2 = Pixmap support available too */
190 XShmSegmentInfo shminfo
;
194 XMesaImage
*rowimage
; /* Used for optimized span writing */
196 GLint bottom
; /* used for FLIP macro below */
197 GLubyte
*ximage_origin1
; /* used for PIXELADDR1 macro */
199 GLushort
*ximage_origin2
; /* used for PIXELADDR2 macro */
201 GLubyte
*ximage_origin3
; /* used for PIXELADDR3 macro */
203 GLuint
*ximage_origin4
; /* used for PIXELADDR4 macro */
206 XMesaPixmap stipple_pixmap
; /* For polygon stippling */
207 XMesaGC stipple_gc
; /* For polygon stippling */
209 XMesaGC gc
; /* scratch GC for span, line, tri drawing */
210 XMesaGC cleargc
; /* GC for clearing the color buffer */
211 XMesaGC swapgc
; /* GC for swapping the color buffers */
213 /* The following are here instead of in the XMesaVisual
214 * because they depend on the window's colormap.
217 /* For PF_DITHER, PF_LOOKUP, PF_GRAYSCALE */
218 unsigned long color_table
[576]; /* RGB -> pixel value */
220 /* For PF_DITHER, PF_LOOKUP, PF_GRAYSCALE */
221 GLubyte pixel_to_r
[65536]; /* pixel value -> red */
222 GLubyte pixel_to_g
[65536]; /* pixel value -> green */
223 GLubyte pixel_to_b
[65536]; /* pixel value -> blue */
225 /* Used to do XAllocColor/XFreeColors accounting: */
227 #if defined(XFree86Server)
228 Pixel alloced_colors
[256];
230 unsigned long alloced_colors
[256];
234 /* For 3Dfx Glide only */
235 GLboolean FXisHackUsable
; /* Can we render into window? */
236 GLboolean FXwindowHack
; /* Are we rendering into a window? */
240 /* functions for clearing the front and back color buffers */
241 clear_func front_clear_func
;
242 clear_func back_clear_func
;
244 struct xmesa_buffer
*Next
; /* Linked list pointer: */
249 /* Values for xmesa->dest: */
250 #define FRONT_PIXMAP 1
251 #define BACK_PIXMAP 2
252 #define BACK_XIMAGE 4
254 /* Special value for X Drawable variables to indicate use of XImage instead */
259 * If pixelformat==PF_TRUECOLOR:
261 #define PACK_TRUECOLOR( PIXEL, R, G, B ) \
262 PIXEL = xmesa->xm_visual->RtoPixel[R] \
263 | xmesa->xm_visual->GtoPixel[G] \
264 | xmesa->xm_visual->BtoPixel[B]; \
268 * If pixelformat==PF_TRUEDITHER:
270 #define PACK_TRUEDITHER( PIXEL, X, Y, R, G, B ) \
272 int d = xmesa->xm_visual->Kernel[((X)&3) | (((Y)&3)<<2)]; \
273 PIXEL = xmesa->xm_visual->RtoPixel[(R)+d] \
274 | xmesa->xm_visual->GtoPixel[(G)+d] \
275 | xmesa->xm_visual->BtoPixel[(B)+d]; \
281 * If pixelformat==PF_8A8B8G8R:
283 #define PACK_8A8B8G8R( R, G, B, A ) \
284 ( ((A) << 24) | ((B) << 16) | ((G) << 8) | (R) )
288 * Like PACK_8A8B8G8R() but don't use alpha. This is usually an acceptable
291 #define PACK_8B8G8R( R, G, B ) ( ((B) << 16) | ((G) << 8) | (R) )
296 * If pixelformat==PF_8R8G8B:
298 #define PACK_8R8G8B( R, G, B) ( ((R) << 16) | ((G) << 8) | (B) )
302 * If pixelformat==PF_5R6G5B:
304 #define PACK_5R6G5B( R, G, B) ( (((R) & 0xf8) << 8) | (((G) & 0xfc) << 3) | ((B) >> 3) )
308 * If pixelformat==PF_8A8R8G8B:
310 #define PACK_8A8R8G8B( R, G, B, A ) \
311 ( ((A) << 24) | ((R) << 16) | ((G) << 8) | (B) )
316 * If pixelformat==PF_DITHER:
318 * Improved 8-bit RGB dithering code contributed by Bob Mercier
319 * (mercier@hollywood.cinenet.net). Thanks Bob!
325 # define DITH_MIX(r,g,b) (((r) * DITH_G + (g)) * DITH_B + (b))
330 # define DITH_MIX(r,g,b) (((g) << 6) | ((b) << 3) | (r))
334 #define DITH_N (DITH_DX * DITH_DY)
336 #define _dither(C, c, d) (((unsigned)((DITH_N * (C - 1) + 1) * c + d)) >> 12)
339 extern const int xmesa_kernel8
[DITH_DY
* DITH_DX
];
341 /* Dither for random X,Y */
342 #define DITHER_SETUP \
344 unsigned long *ctable = xmesa->xm_buffer->color_table;
346 #define DITHER( X, Y, R, G, B ) \
347 (__d = xmesa_kernel8[(((Y)&3)<<2) | ((X)&3)], \
348 ctable[DITH_MIX(_dither(DITH_R, (R), __d), \
349 _dither(DITH_G, (G), __d), \
350 _dither(DITH_B, (B), __d))])
352 /* Dither for random X, fixed Y */
353 #define XDITHER_SETUP(Y) \
355 unsigned long *ctable = xmesa->xm_buffer->color_table; \
356 const int *kernel = &xmesa_kernel8[ ((Y)&3) << 2 ];
358 #define XDITHER( X, R, G, B ) \
359 (__d = kernel[(X)&3], \
360 ctable[DITH_MIX(_dither(DITH_R, (R), __d), \
361 _dither(DITH_G, (G), __d), \
362 _dither(DITH_B, (B), __d))])
367 * Dithering for flat-shaded triangles. Precompute all 16 possible
368 * pixel values given the triangle's RGB color. Contributed by Martin Shenk.
370 #define FLAT_DITHER_SETUP( R, G, B ) \
371 GLushort ditherValues[16]; \
373 unsigned long *ctable = xmesa->xm_buffer->color_table; \
374 int msdr = (DITH_N*((DITH_R)-1)+1) * (R); \
375 int msdg = (DITH_N*((DITH_G)-1)+1) * (G); \
376 int msdb = (DITH_N*((DITH_B)-1)+1) * (B); \
378 for (i=0;i<16;i++) { \
379 int k = xmesa_kernel8[i]; \
380 int j = DITH_MIX( (msdr+k)>>12, (msdg+k)>>12, (msdb+k)>>12 );\
381 ditherValues[i] = (GLushort) ctable[j]; \
385 #define FLAT_DITHER_ROW_SETUP(Y) \
386 GLushort *ditherRow = ditherValues + ( ((Y)&3) << 2);
388 #define FLAT_DITHER(X) ditherRow[(X)&3]
393 * If pixelformat==PF_LOOKUP:
395 #define _dither_lookup(C, c) (((unsigned)((DITH_N * (C - 1) + 1) * c)) >> 12)
397 #define LOOKUP_SETUP \
398 unsigned long *ctable = xmesa->xm_buffer->color_table
400 #define LOOKUP( R, G, B ) \
401 ctable[DITH_MIX(_dither_lookup(DITH_R, (R)), \
402 _dither_lookup(DITH_G, (G)), \
403 _dither_lookup(DITH_B, (B)))]
408 * If pixelformat==PF_HPCR:
410 * HP Color Recovery dithering (ad@lms.be 30/08/95)
411 * HP has on it's 8-bit 700-series computers, a feature called
412 * 'Color Recovery'. This allows near 24-bit output (so they say).
413 * It is enabled by selecting the 8-bit TrueColor visual AND
414 * corresponding colormap (see tkInitWindow) AND doing some special
417 extern const short xmesa_HPCR_DRGB
[3][2][16];
419 #define DITHER_HPCR( X, Y, R, G, B ) \
420 ( ((xmesa->xm_visual->hpcr_rgbTbl[0][R] + xmesa_HPCR_DRGB[0][(Y)&1][(X)&15]) & 0xE0) \
421 |(((xmesa->xm_visual->hpcr_rgbTbl[1][G] + xmesa_HPCR_DRGB[1][(Y)&1][(X)&15]) & 0xE0)>>3) \
422 | ((xmesa->xm_visual->hpcr_rgbTbl[2][B] + xmesa_HPCR_DRGB[2][(Y)&1][(X)&15])>>6) \
428 * If pixelformat==PF_1BIT:
430 extern const int xmesa_kernel1
[16];
432 #define SETUP_1BIT int bitFlip = xmesa->xm_visual->bitFlip
433 #define DITHER_1BIT( X, Y, R, G, B ) \
434 (( ((int)(R)+(int)(G)+(int)(B)) > xmesa_kernel1[(((Y)&3) << 2) | ((X)&3)] ) ^ bitFlip)
439 * If pixelformat==PF_GRAYSCALE:
441 #define GRAY_RGB( R, G, B ) xmesa->xm_buffer->color_table[((R) + (G) + (B))/3]
446 * Converts a GL window Y coord to an X window Y coord:
448 #define FLIP(BUFFER, Y) ((BUFFER)->bottom-(Y))
452 * Return the address of a 1, 2 or 4-byte pixel in the back XImage:
453 * X==0 is left, Y==0 is bottom.
455 #define PIXELADDR1( BUFFER, X, Y ) \
456 ( (BUFFER)->ximage_origin1 - (Y) * (BUFFER)->ximage_width1 + (X) )
458 #define PIXELADDR2( BUFFER, X, Y ) \
459 ( (BUFFER)->ximage_origin2 - (Y) * (BUFFER)->ximage_width2 + (X) )
461 #define PIXELADDR3( BUFFER, X, Y ) \
462 ( (bgr_t *) ( (BUFFER)->ximage_origin3 - (Y) * (BUFFER)->ximage_width3 + 3 * (X) ))
464 #define PIXELADDR4( BUFFER, X, Y ) \
465 ( (BUFFER)->ximage_origin4 - (Y) * (BUFFER)->ximage_width4 + (X) )
471 * Return pointer to XMesaContext corresponding to a Mesa GLcontext.
472 * Since we're using structure containment, it's just a cast!.
474 #define XMESA_CONTEXT(MESACTX) (XMesaContext) (MESACTX)
479 * External functions:
483 xmesa_color_to_pixel( XMesaContext xmesa
,
484 GLubyte r
, GLubyte g
, GLubyte b
, GLubyte a
,
485 GLuint pixelFormat
);
487 extern void xmesa_alloc_back_buffer( XMesaBuffer b
);
489 extern void xmesa_resize_buffers( GLframebuffer
*buffer
);
491 extern void xmesa_init_driver_functions( XMesaVisual xmvisual
,
492 struct dd_function_table
*driver
);
494 extern void xmesa_update_state( GLcontext
*ctx
, GLuint new_state
);
496 extern void xmesa_update_span_funcs( GLcontext
*ctx
);
498 extern void xmesa_set_buffer( GLcontext
*ctx
, GLframebuffer
*buffer
,
502 /* Plugged into the software rasterizer. Try to use internal
503 * swrast-style point, line and triangle functions.
505 extern void xmesa_choose_point( GLcontext
*ctx
);
506 extern void xmesa_choose_line( GLcontext
*ctx
);
507 extern void xmesa_choose_triangle( GLcontext
*ctx
);
510 extern void xmesa_register_swrast_functions( GLcontext
*ctx
);
514 /* XXX this is a hack to implement shared display lists with 3Dfx */
515 extern XMesaBuffer
XMesaCreateWindowBuffer2( XMesaVisual v
,
520 * These are the extra routines required for integration with XFree86.
521 * None of these routines should be user visible. -KEM
523 extern void XMesaSetVisualDisplay( XMesaDisplay
*dpy
, XMesaVisual v
);
524 extern GLboolean
XMesaForceCurrent(XMesaContext c
);
525 extern GLboolean
XMesaLoseCurrent(XMesaContext c
);
526 extern void XMesaReset( void );
529 #define SWTC 0 /* SW texture compression */