1 /* $Id: xmesaP.h,v 1.22 2001/05/01 22:01:11 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2000 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"
51 #if defined(GLX_DIRECT_RENDERING) && !defined(XFree86Server)
54 # define DRI_DRAWABLE_ARG
55 # define DRI_DRAWABLE_PARM
60 extern _glthread_Mutex _xmesa_lock
;
63 /* for PF_8R8G8B24 pixel format */
71 /* Function pointer for clearing color buffers */
72 typedef void (*clear_func
)( GLcontext
*ctx
,
73 GLboolean all
, GLint x
, GLint y
,
74 GLint width
, GLint height
);
78 * "Derived" from GLvisual. Basically corresponds to an XVisualInfo.
81 GLvisual mesa_visual
; /* Device independent visual parameters */
82 XMesaDisplay
*display
; /* The X11 display */
84 GLint screen_depth
; /* The depth of the screen */
86 XVisualInfo
*vishandle
; /* The pointer returned by glXChooseVisual */
88 XMesaVisualInfo visinfo
; /* X's visual info */
89 GLint BitsPerPixel
; /* True bits per pixel for XImages */
91 GLint level
; /* 0=normal, 1=overlay, etc */
92 GLint VisualCaveat
; /* for GLX_EXT_visual_rating extension */
94 GLboolean ximage_flag
; /* Use XImage for back buffer (not pixmap)? */
96 GLuint dithered_pf
; /* Pixel format when dithering */
97 GLuint undithered_pf
; /* Pixel format when not dithering */
99 GLfloat RedGamma
; /* Gamma values, 1.0 is default */
103 GLint rmult
, gmult
, bmult
; /* Range of color values */
104 GLint index_bits
; /* Bits per pixel in CI mode */
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
;
130 * "Derived" from __GLcontextRec. Basically corresponds to a GLXContext.
132 struct xmesa_context
{
133 GLcontext
*gl_ctx
; /* the core library context */
134 XMesaVisual xm_visual
; /* Describes the buffers */
135 XMesaBuffer xm_buffer
; /* current draw framebuffer */
136 XMesaBuffer xm_read_buffer
; /* current read framebuffer */
137 GLboolean use_read_buffer
; /* read from the xm_read_buffer/ */
139 XMesaDisplay
*display
; /* == xm_visual->display */
140 GLboolean swapbytes
; /* Host byte order != display byte order? */
141 GLboolean direct
; /* Direct rendering context? */
143 GLuint pixelformat
; /* Current pixel format */
145 GLubyte clearcolor
[4]; /* current clearing color */
146 unsigned long clearpixel
; /* current clearing pixel value */
148 #if defined(GLX_DIRECT_RENDERING) && !defined(XFree86Server)
149 __DRIcontextPrivate
*driContextPriv
; /* back pointer to DRI context
152 void *private; /* device-specific private context */
159 * "Derived" from GLframebuffer. Basically corresponds to a GLXDrawable.
161 struct xmesa_buffer
{
162 GLframebuffer mesa_buffer
; /* depth, stencil, accum, etc buffers */
163 GLboolean wasCurrent
; /* was ever the current buffer? */
164 XMesaVisual xm_visual
; /* the X/Mesa visual */
166 XMesaContext xm_context
; /* the context associated with this buffer */
167 XMesaDisplay
*display
;
168 GLboolean pixmap_flag
; /* is the buffer a Pixmap? */
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 GLint db_state
; /* 0 = single buffered */
179 /* BACK_PIXMAP = use Pixmap for back buffer */
180 /* BACK_XIMAGE = use XImage for back buffer */
182 #ifndef XFree86Server
183 GLuint shm
; /* X Shared Memory extension status: */
184 /* 0 = not available */
185 /* 1 = XImage support available */
186 /* 2 = Pixmap support available too */
188 XShmSegmentInfo shminfo
;
192 XMesaImage
*rowimage
; /* Used for optimized span writing */
194 GLuint width
, height
; /* size of buffer */
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 */
212 /* The following are here instead of in the XMesaVisual
213 * because they depend on the window's colormap.
216 /* For PF_DITHER, PF_LOOKUP, PF_GRAYSCALE */
217 unsigned long color_table
[576]; /* RGB -> pixel value */
219 /* For PF_DITHER, PF_LOOKUP, PF_GRAYSCALE */
220 GLubyte pixel_to_r
[65536]; /* pixel value -> red */
221 GLubyte pixel_to_g
[65536]; /* pixel value -> green */
222 GLubyte pixel_to_b
[65536]; /* pixel value -> blue */
224 /* Used to do XAllocColor/XFreeColors accounting: */
226 #if defined(XFree86Server)
227 Pixel alloced_colors
[256];
229 unsigned long alloced_colors
[256];
232 #if defined(GLX_DIRECT_RENDERING) && !defined(XFree86Server)
233 __DRIdrawablePrivate
*driDrawPriv
; /* back pointer to DRI drawable
234 * used for direct access to framebuffer
236 void *private; /* device-specific private drawable */
239 #if defined( FX ) && !defined(GLX_DIRECT_RENDERING)
240 /* For 3Dfx Glide only */
241 GLboolean FXisHackUsable
; /* Can we render into window? */
242 GLboolean FXwindowHack
; /* Are we rendering into a window? */
246 /* functions for clearing the front and back color buffers */
247 clear_func front_clear_func
;
248 clear_func back_clear_func
;
250 struct xmesa_buffer
*Next
; /* Linked list pointer: */
255 /* Values for xmesa->dest: */
256 #define FRONT_PIXMAP 1
257 #define BACK_PIXMAP 2
258 #define BACK_XIMAGE 4
261 /* Values for xmesa->pixelformat: */
262 #define PF_INDEX 1 /* Color Index mode */
263 #define PF_TRUECOLOR 2 /* TrueColor or DirectColor, any depth */
264 #define PF_TRUEDITHER 3 /* TrueColor with dithering */
265 #define PF_8A8B8G8R 4 /* 32-bit TrueColor: 8-A, 8-B, 8-G, 8-R */
266 #define PF_8R8G8B 5 /* 32-bit TrueColor: 8-R, 8-G, 8-B bits */
267 #define PF_5R6G5B 6 /* 16-bit TrueColor: 5-R, 6-G, 5-B bits */
268 #define PF_DITHER 7 /* Color-mapped RGB with dither */
269 #define PF_LOOKUP 8 /* Color-mapped RGB without dither */
270 #define PF_HPCR 9 /* HP Color Recovery (ad@lms.be 30/08/95) */
271 #define PF_1BIT 10 /* monochrome dithering of RGB */
272 #define PF_GRAYSCALE 11 /* Grayscale or StaticGray */
273 #define PF_8R8G8B24 12 /* 24-bit TrueColor: 8-R, 8-G, 8-B bits */
274 #define PF_DITHER_5R6G5B 13 /* 16-bit dithered TrueColor: 5-R, 6-G, 5-B */
278 * If pixelformat==PF_TRUECOLOR:
280 #define PACK_TRUECOLOR( PIXEL, R, G, B ) \
281 PIXEL = xmesa->xm_visual->RtoPixel[R] \
282 | xmesa->xm_visual->GtoPixel[G] \
283 | xmesa->xm_visual->BtoPixel[B]; \
287 * If pixelformat==PF_TRUEDITHER:
289 #define PACK_TRUEDITHER( PIXEL, X, Y, R, G, B ) \
291 int d = xmesa->xm_visual->Kernel[((X)&3) | (((Y)&3)<<2)]; \
292 PIXEL = xmesa->xm_visual->RtoPixel[(R)+d] \
293 | xmesa->xm_visual->GtoPixel[(G)+d] \
294 | xmesa->xm_visual->BtoPixel[(B)+d]; \
300 * If pixelformat==PF_8A8B8G8R:
302 #define PACK_8A8B8G8R( R, G, B, A ) \
303 ( ((A) << 24) | ((B) << 16) | ((G) << 8) | (R) )
307 * Like PACK_8A8B8G8R() but don't use alpha. This is usually an acceptable
310 #define PACK_8B8G8R( R, G, B ) ( ((B) << 16) | ((G) << 8) | (R) )
315 * If pixelformat==PF_8R8G8B:
317 #define PACK_8R8G8B( R, G, B) ( ((R) << 16) | ((G) << 8) | (B) )
321 * If pixelformat==PF_5R6G5B:
323 #define PACK_5R6G5B( R, G, B) ( (((R) & 0xf8) << 8) | (((G) & 0xfc) << 3) | ((B) >> 3) )
329 * If pixelformat==PF_DITHER:
331 * Improved 8-bit RGB dithering code contributed by Bob Mercier
332 * (mercier@hollywood.cinenet.net). Thanks Bob!
342 # define _MIX(r,g,b) (((r)*_G+(g))*_B+(b))
347 # define _MIX(r,g,b) ( ((g)<<6) | ((b)<<3) | (r) )
353 /*#define _DITH(C,c,d) (((unsigned)((_D*(C-1)+1)*c+d))/(_D*256))*/
354 #define _DITH(C,c,d) (((unsigned)((_D*(C-1)+1)*c+d)) >> 12)
357 static int kernel8
[_DY
*_DX
] = {
358 0 * MAXC
, 8 * MAXC
, 2 * MAXC
, 10 * MAXC
,
359 12 * MAXC
, 4 * MAXC
, 14 * MAXC
, 6 * MAXC
,
360 3 * MAXC
, 11 * MAXC
, 1 * MAXC
, 9 * MAXC
,
361 15 * MAXC
, 7 * MAXC
, 13 * MAXC
, 5 * MAXC
,
365 /* Dither for random X,Y */
366 #define DITHER_SETUP \
368 unsigned long *ctable = xmesa->xm_buffer->color_table;
370 #define DITHER( X, Y, R, G, B ) \
371 (__d = kernel8[(((Y)&3)<<2) | ((X)&3)], \
372 ctable[_MIX(_DITH(_R, (R), __d), \
373 _DITH(_G, (G), __d), \
374 _DITH(_B, (B), __d))])
376 /* Dither for random X, fixed Y */
377 #define XDITHER_SETUP(Y) \
379 unsigned long *ctable = xmesa->xm_buffer->color_table; \
380 int *kernel = &kernel8[ ((Y)&3) << 2 ];
382 #define XDITHER( X, R, G, B ) \
383 (__d = kernel[(X)&3], \
384 ctable[_MIX(_DITH(_R, (R), __d), \
385 _DITH(_G, (G), __d), \
386 _DITH(_B, (B), __d))])
391 * Dithering for flat-shaded triangles. Precompute all 16 possible
392 * pixel values given the triangle's RGB color. Contributed by Martin Shenk.
394 static GLushort DitherValues
[16]; /* array of (up to) 16-bit pixel values */
396 #define FLAT_DITHER_SETUP( R, G, B ) \
398 unsigned long *ctable = xmesa->xm_buffer->color_table; \
399 int msdr = (_D*((_R)-1)+1) * (R); \
400 int msdg = (_D*((_G)-1)+1) * (G); \
401 int msdb = (_D*((_B)-1)+1) * (B); \
403 for (i=0;i<16;i++) { \
404 int k = kernel8[i]; \
405 int j = _MIX( (msdr+k)>>12, (msdg+k)>>12, (msdb+k)>>12 ); \
406 DitherValues[i] = (GLushort) ctable[j]; \
410 #define FLAT_DITHER_ROW_SETUP(Y) \
411 GLushort *ditherRow = DitherValues + ( ((Y)&3) << 2);
413 #define FLAT_DITHER(X) ditherRow[(X)&3]
418 * If pixelformat==PF_LOOKUP:
420 #define _DITH0(C,c) (((unsigned)((_D*(C-1)+1)*c)) >> 12)
422 #define LOOKUP_SETUP \
423 unsigned long *ctable = xmesa->xm_buffer->color_table
425 #define LOOKUP( R, G, B ) \
426 ctable[_MIX(_DITH0(_R, (R)), \
433 * If pixelformat==PF_HPCR:
435 * HP Color Recovery dithering (ad@lms.be 30/08/95)
436 * HP has on it's 8-bit 700-series computers, a feature called
437 * 'Color Recovery'. This allows near 24-bit output (so they say).
438 * It is enabled by selecting the 8-bit TrueColor visual AND
439 * corresponding colormap (see tkInitWindow) AND doing some special
442 static const short HPCR_DRGB
[3][2][16] = {
444 { 16, -4, 1,-11, 14, -6, 3, -9, 15, -5, 2,-10, 13, -7, 4, -8},
445 {-15, 5, 0, 12,-13, 7, -2, 10,-14, 6, -1, 11,-12, 8, -3, 9}
448 {-11, 15, -7, 3, -8, 14, -4, 2,-10, 16, -6, 4, -9, 13, -5, 1},
449 { 12,-14, 8, -2, 9,-13, 5, -1, 11,-15, 7, -3, 10,-12, 6, 0}
452 { 6,-18, 26,-14, 2,-22, 30,-10, 8,-16, 28,-12, 4,-20, 32, -8},
453 { -4, 20,-24, 16, 0, 24,-28, 12, -6, 18,-26, 14, -2, 22,-30, 10}
457 #define DITHER_HPCR( X, Y, R, G, B ) \
458 ( ((xmesa->xm_visual->hpcr_rgbTbl[0][R] + HPCR_DRGB[0][(Y)&1][(X)&15]) & 0xE0) \
459 |(((xmesa->xm_visual->hpcr_rgbTbl[1][G] + HPCR_DRGB[1][(Y)&1][(X)&15]) & 0xE0)>>3) \
460 | ((xmesa->xm_visual->hpcr_rgbTbl[2][B] + HPCR_DRGB[2][(Y)&1][(X)&15])>>6) \
466 * If pixelformat==PF_1BIT:
468 static int const kernel1
[16] = {
469 0*47, 9*47, 4*47, 12*47, /* 47 = (255*3)/16 */
470 6*47, 2*47, 14*47, 8*47,
471 10*47, 1*47, 5*47, 11*47,
472 7*47, 13*47, 3*47, 15*47 };
474 #define SETUP_1BIT int bitFlip = xmesa->xm_visual->bitFlip
475 #define DITHER_1BIT( X, Y, R, G, B ) \
476 (( ((int)(R)+(int)(G)+(int)(B)) > kernel1[(((Y)&3) << 2) | ((X)&3)] ) ^ bitFlip)
481 * If pixelformat==PF_GRAYSCALE:
483 #define GRAY_RGB( R, G, B ) xmesa->xm_buffer->color_table[((R) + (G) + (B))/3]
491 * Converts a GL window Y coord to an X window Y coord:
493 #define FLIP(BUFFER, Y) ((BUFFER)->bottom-(Y))
497 * Return the address of a 1, 2 or 4-byte pixel in the back XImage:
498 * X==0 is left, Y==0 is bottom.
500 #define PIXELADDR1( BUFFER, X, Y ) \
501 ( (BUFFER)->ximage_origin1 - (Y) * (BUFFER)->ximage_width1 + (X) )
503 #define PIXELADDR2( BUFFER, X, Y ) \
504 ( (BUFFER)->ximage_origin2 - (Y) * (BUFFER)->ximage_width2 + (X) )
506 #define PIXELADDR3( BUFFER, X, Y ) \
507 ( (bgr_t *) ( (BUFFER)->ximage_origin3 - (Y) * (BUFFER)->ximage_width3 + 3 * (X) ))
509 #define PIXELADDR4( BUFFER, X, Y ) \
510 ( (BUFFER)->ximage_origin4 - (Y) * (BUFFER)->ximage_width4 + (X) )
515 * External functions:
519 xmesa_color_to_pixel( XMesaContext xmesa
,
520 GLubyte r
, GLubyte g
, GLubyte b
, GLubyte a
,
521 GLuint pixelFormat
);
523 extern void xmesa_alloc_back_buffer( XMesaBuffer b
);
525 extern void xmesa_init_pointers( GLcontext
*ctx
);
526 extern void xmesa_update_state( GLcontext
*ctx
, GLuint new_state
);
528 extern void xmesa_update_span_funcs( GLcontext
*ctx
);
530 /* Plugged into the software rasterizer. Try to use internal
531 * swrast-style point, line and triangle functions.
533 extern void xmesa_choose_point( GLcontext
*ctx
);
534 extern void xmesa_choose_line( GLcontext
*ctx
);
535 extern void xmesa_choose_triangle( GLcontext
*ctx
);
538 extern void xmesa_register_swrast_functions( GLcontext
*ctx
);
542 /* XXX this is a hack to implement shared display lists with 3Dfx */
543 extern XMesaBuffer
XMesaCreateWindowBuffer2( XMesaVisual v
,
550 * These are the extra routines required for integration with XFree86.
551 * None of these routines should be user visible. -KEM
553 extern void XMesaSetVisualDisplay( XMesaDisplay
*dpy
, XMesaVisual v
);
554 extern GLboolean
XMesaForceCurrent(XMesaContext c
);
555 extern GLboolean
XMesaLoseCurrent(XMesaContext c
);
556 extern void XMesaReset( void );
558 extern void xmesa_set_read_buffer( GLcontext
*ctx
,
559 GLframebuffer
*buffer
, GLenum mode
);