1 /* $Id: xmesaP.h,v 1.5 2000/01/06 09:28:58 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999 Brian Paul All Rights Reserved.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 # include "GL/xf86glx.h"
34 # include "xf86glx_util.h"
36 # ifdef GLX_DIRECT_RENDERING
37 # include "dri_mesa.h"
40 # include <X11/extensions/XShm.h>
45 #if defined(FX) && !defined(GLX_DIRECT_RENDERING)
46 #include "GL/fxmesa.h"
47 #include "../FX/fxdrv.h"
51 #if defined(GLX_DIRECT_RENDERING) && !defined(XFree86Server)
54 # define DRI_DRAWABLE_ARG
55 # define DRI_DRAWABLE_PARM
60 /* for PF_8R8G8B24 pixel format */
68 /* Function pointer for clearing color buffers */
69 typedef void (*clear_func
)( GLcontext
*ctx
,
70 GLboolean all
, GLint x
, GLint y
,
71 GLint width
, GLint height
);
75 * "Derived" from gl_visual. Basically corresponds to an XVisualInfo.
78 GLvisual
*gl_visual
; /* Device independent visual parameters */
79 XMesaDisplay
*display
; /* The X11 display */
81 GLint screen_depth
; /* The depth of the screen */
83 XVisualInfo
*vishandle
; /* The pointer returned by glXChooseVisual */
85 XMesaVisualInfo visinfo
; /* X's visual info */
87 GLint level
; /* 0=normal, 1=overlay, etc */
89 GLboolean ximage_flag
; /* Use XImage for back buffer (not pixmap)? */
91 GLuint dithered_pf
; /* Pixel format when dithering */
92 GLuint undithered_pf
; /* Pixel format when not dithering */
94 GLfloat RedGamma
; /* Gamma values, 1.0 is default */
98 GLint rmult
, gmult
, bmult
; /* Range of color values */
99 GLint index_bits
; /* Bits per pixel in CI mode */
101 /* For PF_TRUECOLOR */
102 GLint rshift
, gshift
, bshift
;/* Pixel color component shifts */
103 GLubyte Kernel
[16]; /* Dither kernel */
104 unsigned long RtoPixel
[512]; /* RGB to pixel conversion */
105 unsigned long GtoPixel
[512];
106 unsigned long BtoPixel
[512];
107 GLubyte PixelToR
[256]; /* Pixel to RGB conversion */
108 GLubyte PixelToG
[256];
109 GLubyte PixelToB
[256];
112 short hpcr_rgbTbl
[3][256];
113 GLboolean hpcr_clear_flag
;
114 GLubyte hpcr_clear_ximage_pattern
[2][16];
115 XMesaImage
*hpcr_clear_ximage
;
116 XMesaPixmap hpcr_clear_pixmap
;
125 * "Derived" from gl_context. Basically corresponds to a GLXContext.
127 struct xmesa_context
{
128 GLcontext
*gl_ctx
; /* the core library context */
129 XMesaVisual xm_visual
; /* Describes the buffers */
130 XMesaBuffer xm_buffer
; /* current draw framebuffer */
131 XMesaBuffer xm_read_buffer
; /* current read framebuffer */
132 GLboolean use_read_buffer
; /* read from the xm_read_buffer/ */
134 XMesaDisplay
*display
; /* == xm_visual->display */
135 GLboolean swapbytes
; /* Host byte order != display byte order? */
136 GLboolean direct
; /* Direct rendering context? */
138 GLuint pixelformat
; /* Current pixel format */
140 GLubyte red
, green
, blue
, alpha
; /* current drawing color */
141 unsigned long pixel
; /* current drawing pixel value */
143 GLubyte clearcolor
[4]; /* current clearing color */
144 unsigned long clearpixel
; /* current clearing pixel value */
146 #if defined(GLX_DIRECT_RENDERING) && !defined(XFree86Server)
147 __DRIcontextPrivate
*driContextPriv
; /* back pointer to DRI context
150 void *private; /* device-specific private context */
157 * "Derived" from gl_buffer. Basically corresponds to a GLXDrawable.
159 struct xmesa_buffer
{
160 GLboolean wasCurrent
; /* was ever the current buffer? */
161 GLframebuffer
*gl_buffer
; /* depth, stencil, accum, etc buffers */
162 XMesaVisual xm_visual
; /* the X/Mesa visual */
164 XMesaContext xm_context
; /* the context associated with this buffer */
165 XMesaDisplay
*display
;
166 GLboolean pixmap_flag
; /* is the buffer a Pixmap? */
167 XMesaDrawable frontbuffer
; /* either a window or pixmap */
168 XMesaPixmap backpixmap
; /* back buffer Pixmap */
169 XMesaImage
*backimage
; /* back buffer simulated XImage */
171 XMesaDrawable buffer
; /* the current buffer, either equal to */
172 /* frontbuffer, backpixmap or XIMAGE (None) */
174 XMesaColormap cmap
; /* the X colormap */
176 GLint db_state
; /* 0 = single buffered */
177 /* BACK_PIXMAP = use Pixmap for back buffer */
178 /* BACK_XIMAGE = use XImage for back buffer */
180 #ifndef XFree86Server
181 GLuint shm
; /* X Shared Memory extension status: */
182 /* 0 = not available */
183 /* 1 = XImage support available */
184 /* 2 = Pixmap support available too */
186 XShmSegmentInfo shminfo
;
190 XMesaImage
*rowimage
; /* Used for optimized span writing */
192 GLuint width
, height
; /* size of buffer */
194 GLint bottom
; /* used for FLIP macro below */
195 GLubyte
*ximage_origin1
; /* used for PIXELADDR1 macro */
197 GLushort
*ximage_origin2
; /* used for PIXELADDR2 macro */
199 bgr_t
*ximage_origin3
; /* used for PIXELADDR3 macro */
201 GLuint
*ximage_origin4
; /* used for PIXELADDR4 macro */
204 XMesaPixmap stipple_pixmap
; /* For polygon stippling */
205 XMesaGC stipple_gc
; /* For polygon stippling */
207 XMesaGC gc1
; /* GC for infrequent color changes */
208 XMesaGC gc2
; /* GC for frequent color changes */
209 XMesaGC cleargc
; /* GC for clearing the color buffer */
211 /* The following are here instead of in the XMesaVisual
212 * because they depend on the window's colormap.
215 /* For PF_DITHER, PF_LOOKUP, PF_GRAYSCALE */
216 unsigned long color_table
[576]; /* RGB -> pixel value */
218 /* For PF_DITHER, PF_LOOKUP, PF_GRAYSCALE */
219 GLubyte pixel_to_r
[65536]; /* pixel value -> red */
220 GLubyte pixel_to_g
[65536]; /* pixel value -> green */
221 GLubyte pixel_to_b
[65536]; /* pixel value -> blue */
223 /* Used to do XAllocColor/XFreeColors accounting: */
225 unsigned long alloced_colors
[256];
227 #if defined(GLX_DIRECT_RENDERING) && !defined(XFree86Server)
228 __DRIdrawablePrivate
*driDrawPriv
; /* back pointer to DRI drawable
229 * used for direct access to framebuffer
231 void *private; /* device-specific private drawable */
234 #if defined( FX ) && !defined(GLX_DIRECT_RENDERING)
235 /* For 3Dfx Glide only */
236 GLboolean FXisHackUsable
; /* Can we render into window? */
237 GLboolean FXwindowHack
; /* Are we rendering into a window? */
241 /* functions for clearing the front and back color buffers */
242 clear_func front_clear_func
;
243 clear_func back_clear_func
;
245 struct xmesa_buffer
*Next
; /* Linked list pointer: */
250 /* Values for xmesa->dest: */
251 #define FRONT_PIXMAP 1
252 #define BACK_PIXMAP 2
253 #define BACK_XIMAGE 4
256 /* Values for xmesa->pixelformat: */
257 #define PF_INDEX 1 /* Color Index mode */
258 #define PF_TRUECOLOR 2 /* TrueColor or DirectColor, any depth */
259 #define PF_TRUEDITHER 3 /* TrueColor with dithering */
260 #define PF_8A8B8G8R 4 /* 32-bit TrueColor: 8-A, 8-B, 8-G, 8-R */
261 #define PF_8R8G8B 5 /* 32-bit TrueColor: 8-R, 8-G, 8-B bits */
262 #define PF_5R6G5B 6 /* 16-bit TrueColor: 5-R, 6-G, 5-B bits */
263 #define PF_DITHER 7 /* Color-mapped RGB with dither */
264 #define PF_LOOKUP 8 /* Color-mapped RGB without dither */
265 #define PF_HPCR 9 /* HP Color Recovery (ad@lms.be 30/08/95) */
266 #define PF_1BIT 10 /* monochrome dithering of RGB */
267 #define PF_GRAYSCALE 11 /* Grayscale or StaticGray */
268 #define PF_8R8G8B24 12 /* 24-bit TrueColor: 8-R, 8-G, 8-B bits */
269 #define PF_DITHER_5R6G5B 13 /* 16-bit dithered TrueColor: 5-R, 6-G, 5-B */
273 * If pixelformat==PF_TRUECOLOR:
275 #define PACK_TRUECOLOR( PIXEL, R, G, B ) \
276 PIXEL = xmesa->xm_visual->RtoPixel[R] \
277 | xmesa->xm_visual->GtoPixel[G] \
278 | xmesa->xm_visual->BtoPixel[B]; \
282 * If pixelformat==PF_TRUEDITHER:
284 #define PACK_TRUEDITHER( PIXEL, X, Y, R, G, B ) \
286 int d = xmesa->xm_visual->Kernel[((X)&3) | (((Y)&3)<<2)]; \
287 PIXEL = xmesa->xm_visual->RtoPixel[(R)+d] \
288 | xmesa->xm_visual->GtoPixel[(G)+d] \
289 | xmesa->xm_visual->BtoPixel[(B)+d]; \
295 * If pixelformat==PF_8A8B8G8R:
297 #define PACK_8A8B8G8R( R, G, B, A ) \
298 ( ((A) << 24) | ((B) << 16) | ((G) << 8) | (R) )
302 * Like PACK_8A8B8G8R() but don't use alpha. This is usually an acceptable
305 #define PACK_8B8G8R( R, G, B ) ( ((B) << 16) | ((G) << 8) | (R) )
310 * If pixelformat==PF_8R8G8B:
312 #define PACK_8R8G8B( R, G, B) ( ((R) << 16) | ((G) << 8) | (B) )
316 * If pixelformat==PF_5R6G5B:
318 #define PACK_5R6G5B( R, G, B) ( (((R) & 0xf8) << 8) | (((G) & 0xfc) << 3) | ((B) >> 3) )
324 * If pixelformat==PF_DITHER:
326 * Improved 8-bit RGB dithering code contributed by Bob Mercier
327 * (mercier@hollywood.cinenet.net). Thanks Bob!
337 # define _MIX(r,g,b) (((r)*_G+(g))*_B+(b))
342 # define _MIX(r,g,b) ( ((g)<<6) | ((b)<<3) | (r) )
348 /*#define _DITH(C,c,d) (((unsigned)((_D*(C-1)+1)*c+d))/(_D*256))*/
349 #define _DITH(C,c,d) (((unsigned)((_D*(C-1)+1)*c+d)) >> 12)
352 static int kernel8
[_DY
*_DX
] = {
353 0 * MAXC
, 8 * MAXC
, 2 * MAXC
, 10 * MAXC
,
354 12 * MAXC
, 4 * MAXC
, 14 * MAXC
, 6 * MAXC
,
355 3 * MAXC
, 11 * MAXC
, 1 * MAXC
, 9 * MAXC
,
356 15 * MAXC
, 7 * MAXC
, 13 * MAXC
, 5 * MAXC
,
360 /* Dither for random X,Y */
361 #define DITHER_SETUP \
363 unsigned long *ctable = xmesa->xm_buffer->color_table;
365 #define DITHER( X, Y, R, G, B ) \
366 (__d = kernel8[(((Y)&3)<<2) | ((X)&3)], \
367 ctable[_MIX(_DITH(_R, (R), __d), \
368 _DITH(_G, (G), __d), \
369 _DITH(_B, (B), __d))])
371 /* Dither for random X, fixed Y */
372 #define XDITHER_SETUP(Y) \
374 unsigned long *ctable = xmesa->xm_buffer->color_table; \
375 int *kernel = &kernel8[ ((Y)&3) << 2 ];
377 #define XDITHER( X, R, G, B ) \
378 (__d = kernel[(X)&3], \
379 ctable[_MIX(_DITH(_R, (R), __d), \
380 _DITH(_G, (G), __d), \
381 _DITH(_B, (B), __d))])
386 * Dithering for flat-shaded triangles. Precompute all 16 possible
387 * pixel values given the triangle's RGB color. Contributed by Martin Shenk.
389 static GLushort DitherValues
[16]; /* array of (up to) 16-bit pixel values */
391 #define FLAT_DITHER_SETUP( R, G, B ) \
393 unsigned long *ctable = xmesa->xm_buffer->color_table; \
394 int msdr = (_D*((_R)-1)+1) * (R); \
395 int msdg = (_D*((_G)-1)+1) * (G); \
396 int msdb = (_D*((_B)-1)+1) * (B); \
398 for (i=0;i<16;i++) { \
399 int k = kernel8[i]; \
400 int j = _MIX( (msdr+k)>>12, (msdg+k)>>12, (msdb+k)>>12 ); \
401 DitherValues[i] = (GLushort) ctable[j]; \
405 #define FLAT_DITHER_ROW_SETUP(Y) \
406 GLushort *ditherRow = DitherValues + ( ((Y)&3) << 2);
408 #define FLAT_DITHER(X) ditherRow[(X)&3]
413 * If pixelformat==PF_LOOKUP:
415 #define _DITH0(C,c) (((unsigned)((_D*(C-1)+1)*c)) >> 12)
417 #define LOOKUP_SETUP \
418 unsigned long *ctable = xmesa->xm_buffer->color_table
420 #define LOOKUP( R, G, B ) \
421 ctable[_MIX(_DITH0(_R, (R)), \
428 * If pixelformat==PF_HPCR:
430 * HP Color Recovery dithering (ad@lms.be 30/08/95)
431 * HP has on it's 8-bit 700-series computers, a feature called
432 * 'Color Recovery'. This allows near 24-bit output (so they say).
433 * It is enabled by selecting the 8-bit TrueColor visual AND
434 * corresponding colormap (see tkInitWindow) AND doing some special
437 static const short HPCR_DRGB
[3][2][16] = {
439 { 16, -4, 1,-11, 14, -6, 3, -9, 15, -5, 2,-10, 13, -7, 4, -8},
440 {-15, 5, 0, 12,-13, 7, -2, 10,-14, 6, -1, 11,-12, 8, -3, 9}
443 {-11, 15, -7, 3, -8, 14, -4, 2,-10, 16, -6, 4, -9, 13, -5, 1},
444 { 12,-14, 8, -2, 9,-13, 5, -1, 11,-15, 7, -3, 10,-12, 6, 0}
447 { 6,-18, 26,-14, 2,-22, 30,-10, 8,-16, 28,-12, 4,-20, 32, -8},
448 { -4, 20,-24, 16, 0, 24,-28, 12, -6, 18,-26, 14, -2, 22,-30, 10}
452 #define DITHER_HPCR( X, Y, R, G, B ) \
453 ( ((xmesa->xm_visual->hpcr_rgbTbl[0][R] + HPCR_DRGB[0][(Y)&1][(X)&15]) & 0xE0) \
454 |(((xmesa->xm_visual->hpcr_rgbTbl[1][G] + HPCR_DRGB[1][(Y)&1][(X)&15]) & 0xE0)>>3) \
455 | ((xmesa->xm_visual->hpcr_rgbTbl[2][B] + HPCR_DRGB[2][(Y)&1][(X)&15])>>6) \
461 * If pixelformat==PF_1BIT:
463 static int const kernel1
[16] = {
464 0*47, 9*47, 4*47, 12*47, /* 47 = (255*3)/16 */
465 6*47, 2*47, 14*47, 8*47,
466 10*47, 1*47, 5*47, 11*47,
467 7*47, 13*47, 3*47, 15*47 };
469 #define SETUP_1BIT int bitFlip = xmesa->xm_visual->bitFlip
470 #define DITHER_1BIT( X, Y, R, G, B ) \
471 (( ((int)(R)+(int)(G)+(int)(B)) > kernel1[(((Y)&3) << 2) | ((X)&3)] ) ^ bitFlip)
476 * If pixelformat==PF_GRAYSCALE:
478 #define GRAY_RGB( R, G, B ) xmesa->xm_buffer->color_table[((R) + (G) + (B))/3]
486 * Converts a GL window Y coord to an X window Y coord:
488 #define FLIP(BUFFER, Y) ((BUFFER)->bottom-(Y))
492 * Return the address of a 1, 2 or 4-byte pixel in the back XImage:
493 * X==0 is left, Y==0 is bottom.
495 #define PIXELADDR1( BUFFER, X, Y ) \
496 ( (BUFFER)->ximage_origin1 - (Y) * (BUFFER)->ximage_width1 + (X) )
498 #define PIXELADDR2( BUFFER, X, Y ) \
499 ( (BUFFER)->ximage_origin2 - (Y) * (BUFFER)->ximage_width2 + (X) )
501 #define PIXELADDR3( BUFFER, X, Y ) \
502 ( (BUFFER)->ximage_origin3 - (Y) * (BUFFER)->ximage_width3 + (X) )
504 #define PIXELADDR4( BUFFER, X, Y ) \
505 ( (BUFFER)->ximage_origin4 - (Y) * (BUFFER)->ximage_width4 + (X) )
510 * External functions:
513 extern unsigned long xmesa_color_to_pixel( XMesaContext xmesa
,
514 GLubyte r
, GLubyte g
, GLubyte b
, GLubyte a
);
516 extern void xmesa_alloc_back_buffer( XMesaBuffer b
);
518 extern void xmesa_update_state( GLcontext
*ctx
);
520 extern points_func
xmesa_get_points_func( GLcontext
*ctx
);
522 extern line_func
xmesa_get_line_func( GLcontext
*ctx
);
524 extern triangle_func
xmesa_get_triangle_func( GLcontext
*ctx
);
527 /* XXX this is a hack to implement shared display lists with 3Dfx */
528 extern XMesaBuffer
XMesaCreateWindowBuffer2( XMesaVisual v
,
535 * These are the extra routines required for integration with XFree86.
536 * None of these routines should be user visible. -KEM
538 extern void XMesaSetVisualDisplay( XMesaDisplay
*dpy
, XMesaVisual v
);
539 extern GLboolean
XMesaForceCurrent(XMesaContext c
);
540 extern GLboolean
XMesaLoseCurrent(XMesaContext c
);
541 extern void XMesaReset( void );