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.
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 struct xmesa_renderbuffer
;
58 /* Function pointer for clearing color buffers */
59 typedef void (*ClearFunc
)( GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
60 GLboolean all
, GLint x
, GLint y
,
61 GLint width
, GLint height
);
66 /** Framebuffer pixel formats */
68 PF_Index
, /**< Color Index mode */
69 PF_Truecolor
, /**< TrueColor or DirectColor, any depth */
70 PF_Dither_True
, /**< TrueColor with dithering */
71 PF_8A8B8G8R
, /**< 32-bit TrueColor: 8-A, 8-B, 8-G, 8-R */
72 PF_8R8G8B
, /**< 32-bit TrueColor: 8-R, 8-G, 8-B bits */
73 PF_5R6G5B
, /**< 16-bit TrueColor: 5-R, 6-G, 5-B bits */
74 PF_Dither
, /**< Color-mapped RGB with dither */
75 PF_Lookup
, /**< Color-mapped RGB without dither */
76 PF_HPCR
, /**< HP Color Recovery (ad@lms.be 30/08/95) */
77 PF_1Bit
, /**< monochrome dithering of RGB */
78 PF_Grayscale
, /**< Grayscale or StaticGray */
79 PF_8R8G8B24
, /**< 24-bit TrueColor: 8-R, 8-G, 8-B bits */
80 PF_Dither_5R6G5B
, /**< 16-bit dithered TrueColor: 5-R, 6-G, 5-B */
81 PF_8A8R8G8B
/**< 32-bit TrueColor: 8-A, 8-R, 8-G, 8-B */
86 * "Derived" from GLvisual. Basically corresponds to an XVisualInfo.
89 GLvisual mesa_visual
; /* Device independent visual parameters */
90 XMesaDisplay
*display
; /* The X11 display */
92 GLint ColormapEntries
;
95 XMesaVisualInfo visinfo
; /* X's visual info (pointer to private copy) */
96 XVisualInfo
*vishandle
; /* Only used in fakeglx.c */
98 GLint BitsPerPixel
; /* True bits per pixel for XImages */
100 GLboolean ximage_flag
; /* Use XImage for back buffer (not pixmap)? */
102 enum pixel_format dithered_pf
; /* Pixel format when dithering */
103 enum pixel_format undithered_pf
;/* Pixel format when not dithering */
105 GLfloat RedGamma
; /* Gamma values, 1.0 is default */
109 /* For PF_TRUECOLOR */
110 GLint rshift
, gshift
, bshift
;/* Pixel color component shifts */
111 GLubyte Kernel
[16]; /* Dither kernel */
112 unsigned long RtoPixel
[512]; /* RGB to pixel conversion */
113 unsigned long GtoPixel
[512];
114 unsigned long BtoPixel
[512];
115 GLubyte PixelToR
[256]; /* Pixel to RGB conversion */
116 GLubyte PixelToG
[256];
117 GLubyte PixelToB
[256];
120 short hpcr_rgbTbl
[3][256];
121 GLboolean hpcr_clear_flag
;
122 GLubyte hpcr_clear_ximage_pattern
[2][16];
123 XMesaImage
*hpcr_clear_ximage
;
124 XMesaPixmap hpcr_clear_pixmap
;
132 * "Derived" from __GLcontextRec. Basically corresponds to a GLXContext.
134 struct xmesa_context
{
135 GLcontext mesa
; /* the core library context (containment) */
136 XMesaVisual xm_visual
; /* Describes the buffers */
137 XMesaBuffer xm_buffer
; /* current span/point/line/triangle buffer */
139 XMesaDisplay
*display
; /* == xm_visual->display */
140 GLboolean swapbytes
; /* Host byte order != display byte order? */
141 GLboolean direct
; /* Direct rendering context? */
143 enum pixel_format pixelformat
;
145 GLubyte clearcolor
[4]; /* current clearing color */
146 unsigned long clearpixel
; /* current clearing pixel value */
152 WINDOW
, /* An X window */
153 GLXWINDOW
, /* GLX window */
154 PIXMAP
, /* GLX pixmap */
155 PBUFFER
/* GLX Pbuffer */
159 struct xmesa_renderbuffer
161 struct gl_renderbuffer Base
; /* Base class */
163 XMesaDrawable pixmap
; /* Either an X Window ID (front color buf) */
164 /* or X Pixmap ID (back color buf) */
166 XMesaImage
*ximage
; /* The back buffer, if not using a Pixmap */
168 GLubyte
*origin1
; /* used for PIXEL_ADDR1 macro */
170 GLushort
*origin2
; /* used for PIXEL_ADDR2 macro */
172 GLubyte
*origin3
; /* used for PIXEL_ADDR3 macro */
174 GLuint
*origin4
; /* used for PIXEL_ADDR4 macro */
177 GLint bottom
; /* used for FLIP macro */
184 * "Derived" from GLframebuffer. Basically corresponds to a GLXDrawable.
186 struct xmesa_buffer
{
187 GLframebuffer mesa_buffer
; /* depth, stencil, accum, etc buffers */
188 /* This MUST BE FIRST! */
189 GLboolean wasCurrent
; /* was ever the current buffer? */
190 XMesaVisual xm_visual
; /* the X/Mesa visual */
192 XMesaDisplay
*display
;
193 BufferType type
; /* window, pixmap, pbuffer or glxwindow */
195 struct xmesa_renderbuffer
*frontxrb
; /* front color renderbuffer */
196 struct xmesa_renderbuffer
*backxrb
; /* back color renderbuffer */
198 XMesaColormap cmap
; /* the X colormap */
200 unsigned long selectedEvents
;/* for pbuffers only */
202 GLint db_state
; /* 0 = single buffered */
203 /* BACK_PIXMAP = use Pixmap for back buffer */
204 /* BACK_XIMAGE = use XImage for back buffer */
206 #ifndef XFree86Server
207 GLuint shm
; /* X Shared Memory extension status: */
208 /* 0 = not available */
209 /* 1 = XImage support available */
210 /* 2 = Pixmap support available too */
212 XShmSegmentInfo shminfo
;
216 XMesaImage
*rowimage
; /* Used for optimized span writing */
217 XMesaPixmap stipple_pixmap
; /* For polygon stippling */
218 XMesaGC stipple_gc
; /* For polygon stippling */
220 XMesaGC gc
; /* scratch GC for span, line, tri drawing */
221 XMesaGC cleargc
; /* GC for clearing the color buffer */
222 XMesaGC swapgc
; /* GC for swapping the color buffers */
224 /* The following are here instead of in the XMesaVisual
225 * because they depend on the window's colormap.
228 /* For PF_DITHER, PF_LOOKUP, PF_GRAYSCALE */
229 unsigned long color_table
[576]; /* RGB -> pixel value */
231 /* For PF_DITHER, PF_LOOKUP, PF_GRAYSCALE */
232 GLubyte pixel_to_r
[65536]; /* pixel value -> red */
233 GLubyte pixel_to_g
[65536]; /* pixel value -> green */
234 GLubyte pixel_to_b
[65536]; /* pixel value -> blue */
236 /* Used to do XAllocColor/XFreeColors accounting: */
238 #if defined(XFree86Server)
239 Pixel alloced_colors
[256];
241 unsigned long alloced_colors
[256];
245 /* For 3Dfx Glide only */
246 GLboolean FXisHackUsable
; /* Can we render into window? */
247 GLboolean FXwindowHack
; /* Are we rendering into a window? */
251 struct xmesa_buffer
*Next
; /* Linked list pointer: */
255 /* Values for xmesa->db_state: */
256 #define FRONT_PIXMAP 1
257 #define BACK_PIXMAP 2
258 #define BACK_XIMAGE 4
262 * If pixelformat==PF_TRUECOLOR:
264 #define PACK_TRUECOLOR( PIXEL, R, G, B ) \
265 PIXEL = xmesa->xm_visual->RtoPixel[R] \
266 | xmesa->xm_visual->GtoPixel[G] \
267 | xmesa->xm_visual->BtoPixel[B]; \
271 * If pixelformat==PF_TRUEDITHER:
273 #define PACK_TRUEDITHER( PIXEL, X, Y, R, G, B ) \
275 int d = xmesa->xm_visual->Kernel[((X)&3) | (((Y)&3)<<2)]; \
276 PIXEL = xmesa->xm_visual->RtoPixel[(R)+d] \
277 | xmesa->xm_visual->GtoPixel[(G)+d] \
278 | xmesa->xm_visual->BtoPixel[(B)+d]; \
284 * If pixelformat==PF_8A8B8G8R:
286 #define PACK_8A8B8G8R( R, G, B, A ) \
287 ( ((A) << 24) | ((B) << 16) | ((G) << 8) | (R) )
291 * Like PACK_8A8B8G8R() but don't use alpha. This is usually an acceptable
294 #define PACK_8B8G8R( R, G, B ) ( ((B) << 16) | ((G) << 8) | (R) )
299 * If pixelformat==PF_8R8G8B:
301 #define PACK_8R8G8B( R, G, B) ( ((R) << 16) | ((G) << 8) | (B) )
305 * If pixelformat==PF_5R6G5B:
307 #define PACK_5R6G5B( R, G, B) ( (((R) & 0xf8) << 8) | (((G) & 0xfc) << 3) | ((B) >> 3) )
311 * If pixelformat==PF_8A8R8G8B:
313 #define PACK_8A8R8G8B( R, G, B, A ) \
314 ( ((A) << 24) | ((R) << 16) | ((G) << 8) | (B) )
319 * If pixelformat==PF_DITHER:
321 * Improved 8-bit RGB dithering code contributed by Bob Mercier
322 * (mercier@hollywood.cinenet.net). Thanks Bob!
328 # define DITH_MIX(r,g,b) (((r) * DITH_G + (g)) * DITH_B + (b))
333 # define DITH_MIX(r,g,b) (((g) << 6) | ((b) << 3) | (r))
337 #define DITH_N (DITH_DX * DITH_DY)
339 #define _dither(C, c, d) (((unsigned)((DITH_N * (C - 1) + 1) * c + d)) >> 12)
342 extern const int xmesa_kernel8
[DITH_DY
* DITH_DX
];
344 /* Dither for random X,Y */
345 #define DITHER_SETUP \
347 unsigned long *ctable = XMESA_BUFFER(ctx->DrawBuffer)->color_table;
349 #define DITHER( X, Y, R, G, B ) \
350 (__d = xmesa_kernel8[(((Y)&3)<<2) | ((X)&3)], \
351 ctable[DITH_MIX(_dither(DITH_R, (R), __d), \
352 _dither(DITH_G, (G), __d), \
353 _dither(DITH_B, (B), __d))])
355 /* Dither for random X, fixed Y */
356 #define XDITHER_SETUP(Y) \
358 unsigned long *ctable = XMESA_BUFFER(ctx->DrawBuffer)->color_table; \
359 const int *kernel = &xmesa_kernel8[ ((Y)&3) << 2 ];
361 #define XDITHER( X, R, G, B ) \
362 (__d = kernel[(X)&3], \
363 ctable[DITH_MIX(_dither(DITH_R, (R), __d), \
364 _dither(DITH_G, (G), __d), \
365 _dither(DITH_B, (B), __d))])
370 * Dithering for flat-shaded triangles. Precompute all 16 possible
371 * pixel values given the triangle's RGB color. Contributed by Martin Shenk.
373 #define FLAT_DITHER_SETUP( R, G, B ) \
374 GLushort ditherValues[16]; \
376 unsigned long *ctable = XMESA_BUFFER(ctx->DrawBuffer)->color_table; \
377 int msdr = (DITH_N*((DITH_R)-1)+1) * (R); \
378 int msdg = (DITH_N*((DITH_G)-1)+1) * (G); \
379 int msdb = (DITH_N*((DITH_B)-1)+1) * (B); \
381 for (i=0;i<16;i++) { \
382 int k = xmesa_kernel8[i]; \
383 int j = DITH_MIX( (msdr+k)>>12, (msdg+k)>>12, (msdb+k)>>12 );\
384 ditherValues[i] = (GLushort) ctable[j]; \
388 #define FLAT_DITHER_ROW_SETUP(Y) \
389 GLushort *ditherRow = ditherValues + ( ((Y)&3) << 2);
391 #define FLAT_DITHER(X) ditherRow[(X)&3]
396 * If pixelformat==PF_LOOKUP:
398 #define _dither_lookup(C, c) (((unsigned)((DITH_N * (C - 1) + 1) * c)) >> 12)
400 #define LOOKUP_SETUP \
401 unsigned long *ctable = XMESA_BUFFER(ctx->DrawBuffer)->color_table
403 #define LOOKUP( R, G, B ) \
404 ctable[DITH_MIX(_dither_lookup(DITH_R, (R)), \
405 _dither_lookup(DITH_G, (G)), \
406 _dither_lookup(DITH_B, (B)))]
411 * If pixelformat==PF_HPCR:
413 * HP Color Recovery dithering (ad@lms.be 30/08/95)
414 * HP has on it's 8-bit 700-series computers, a feature called
415 * 'Color Recovery'. This allows near 24-bit output (so they say).
416 * It is enabled by selecting the 8-bit TrueColor visual AND
417 * corresponding colormap (see tkInitWindow) AND doing some special
420 extern const short xmesa_HPCR_DRGB
[3][2][16];
422 #define DITHER_HPCR( X, Y, R, G, B ) \
423 ( ((xmesa->xm_visual->hpcr_rgbTbl[0][R] + xmesa_HPCR_DRGB[0][(Y)&1][(X)&15]) & 0xE0) \
424 |(((xmesa->xm_visual->hpcr_rgbTbl[1][G] + xmesa_HPCR_DRGB[1][(Y)&1][(X)&15]) & 0xE0)>>3) \
425 | ((xmesa->xm_visual->hpcr_rgbTbl[2][B] + xmesa_HPCR_DRGB[2][(Y)&1][(X)&15])>>6) \
431 * If pixelformat==PF_1BIT:
433 extern const int xmesa_kernel1
[16];
435 #define SETUP_1BIT int bitFlip = xmesa->xm_visual->bitFlip
436 #define DITHER_1BIT( X, Y, R, G, B ) \
437 (( ((int)(R)+(int)(G)+(int)(B)) > xmesa_kernel1[(((Y)&3) << 2) | ((X)&3)] ) ^ bitFlip)
442 * If pixelformat==PF_GRAYSCALE:
444 #define GRAY_RGB( R, G, B ) XMESA_BUFFER(ctx->DrawBuffer)->color_table[((R) + (G) + (B))/3]
449 * Converts a GL window Y coord to an X window Y coord:
451 #define YFLIP(XRB, Y) ((XRB)->bottom - (Y))
455 * Return the address of a 1, 2 or 4-byte pixel in the buffer's XImage:
456 * X==0 is left, Y==0 is bottom.
458 #define PIXEL_ADDR1(XRB, X, Y) \
459 ( (XRB)->origin1 - (Y) * (XRB)->width1 + (X) )
461 #define PIXEL_ADDR2(XRB, X, Y) \
462 ( (XRB)->origin2 - (Y) * (XRB)->width2 + (X) )
464 #define PIXEL_ADDR3(XRB, X, Y) \
465 ( (bgr_t *) ( (XRB)->origin3 - (Y) * (XRB)->width3 + 3 * (X) ))
467 #define PIXEL_ADDR4(XRB, X, Y) \
468 ( (XRB)->origin4 - (Y) * (XRB)->width4 + (X) )
474 * Return pointer to XMesaContext corresponding to a Mesa GLcontext.
475 * Since we're using structure containment, it's just a cast!.
477 #define XMESA_CONTEXT(MESACTX) ((XMesaContext) (MESACTX))
480 * Return pointer to XMesaBuffer corresponding to a Mesa GLframebuffer.
481 * Since we're using structure containment, it's just a cast!.
483 #define XMESA_BUFFER(MESABUFF) ((XMesaBuffer) (MESABUFF))
488 * External functions:
491 extern struct xmesa_renderbuffer
*
492 xmesa_new_renderbuffer(GLcontext
*ctx
, GLuint name
, GLboolean rgbMode
);
495 xmesa_color_to_pixel( GLcontext
*ctx
,
496 GLubyte r
, GLubyte g
, GLubyte b
, GLubyte a
,
497 GLuint pixelFormat
);
500 xmesa_alloc_back_buffer(XMesaBuffer b
, GLuint width
, GLuint height
);
502 extern void xmesa_resize_buffers(GLcontext
*ctx
, GLframebuffer
*buffer
,
503 GLuint width
, GLuint height
);
505 extern void xmesa_init_driver_functions( XMesaVisual xmvisual
,
506 struct dd_function_table
*driver
);
508 extern void xmesa_update_state( GLcontext
*ctx
, GLuint new_state
);
511 xmesa_set_renderbuffer_funcs(struct xmesa_renderbuffer
*xrb
,
512 enum pixel_format pixelformat
, GLint depth
);
515 /* Plugged into the software rasterizer. Try to use internal
516 * swrast-style point, line and triangle functions.
518 extern void xmesa_choose_point( GLcontext
*ctx
);
519 extern void xmesa_choose_line( GLcontext
*ctx
);
520 extern void xmesa_choose_triangle( GLcontext
*ctx
);
523 extern void xmesa_register_swrast_functions( GLcontext
*ctx
);
527 /* XXX this is a hack to implement shared display lists with 3Dfx */
528 extern XMesaBuffer
XMesaCreateWindowBuffer2( XMesaVisual v
,
533 * These are the extra routines required for integration with XFree86.
534 * None of these routines should be user visible. -KEM
536 extern void XMesaSetVisualDisplay( XMesaDisplay
*dpy
, XMesaVisual v
);
537 extern GLboolean
XMesaForceCurrent(XMesaContext c
);
538 extern GLboolean
XMesaLoseCurrent(XMesaContext c
);
539 extern void XMesaReset( void );
542 #define ENABLE_EXT_texure_compression_s3tc 0 /* SW texture compression */
545 #define ENABLE_EXT_timer_query 0
547 #define ENABLE_EXT_timer_query 1