1 /* $Id: xmesaP.h,v 1.23 2001/05/29 19:48:47 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"
37 # include <X11/extensions/XShm.h>
43 #include "GL/fxmesa.h"
48 extern _glthread_Mutex _xmesa_lock
;
51 /* for PF_8R8G8B24 pixel format */
59 /* Function pointer for clearing color buffers */
60 typedef void (*clear_func
)( GLcontext
*ctx
,
61 GLboolean all
, GLint x
, GLint y
,
62 GLint width
, GLint height
);
66 * "Derived" from GLvisual. Basically corresponds to an XVisualInfo.
69 GLvisual mesa_visual
; /* Device independent visual parameters */
70 XMesaDisplay
*display
; /* The X11 display */
72 GLint screen_depth
; /* The depth of the screen */
74 XVisualInfo
*vishandle
; /* The pointer returned by glXChooseVisual */
76 XMesaVisualInfo visinfo
; /* X's visual info */
77 GLint BitsPerPixel
; /* True bits per pixel for XImages */
79 GLint level
; /* 0=normal, 1=overlay, etc */
80 GLint VisualCaveat
; /* for GLX_EXT_visual_rating extension */
82 GLboolean ximage_flag
; /* Use XImage for back buffer (not pixmap)? */
84 GLuint dithered_pf
; /* Pixel format when dithering */
85 GLuint undithered_pf
; /* Pixel format when not dithering */
87 GLfloat RedGamma
; /* Gamma values, 1.0 is default */
91 GLint rmult
, gmult
, bmult
; /* Range of color values */
92 GLint index_bits
; /* Bits per pixel in CI mode */
94 /* For PF_TRUECOLOR */
95 GLint rshift
, gshift
, bshift
;/* Pixel color component shifts */
96 GLubyte Kernel
[16]; /* Dither kernel */
97 unsigned long RtoPixel
[512]; /* RGB to pixel conversion */
98 unsigned long GtoPixel
[512];
99 unsigned long BtoPixel
[512];
100 GLubyte PixelToR
[256]; /* Pixel to RGB conversion */
101 GLubyte PixelToG
[256];
102 GLubyte PixelToB
[256];
105 short hpcr_rgbTbl
[3][256];
106 GLboolean hpcr_clear_flag
;
107 GLubyte hpcr_clear_ximage_pattern
[2][16];
108 XMesaImage
*hpcr_clear_ximage
;
109 XMesaPixmap hpcr_clear_pixmap
;
118 * "Derived" from __GLcontextRec. Basically corresponds to a GLXContext.
120 struct xmesa_context
{
121 GLcontext
*gl_ctx
; /* the core library context */
122 XMesaVisual xm_visual
; /* Describes the buffers */
123 XMesaBuffer xm_buffer
; /* current draw framebuffer */
124 XMesaBuffer xm_read_buffer
; /* current read framebuffer */
125 GLboolean use_read_buffer
; /* read from the xm_read_buffer/ */
127 XMesaDisplay
*display
; /* == xm_visual->display */
128 GLboolean swapbytes
; /* Host byte order != display byte order? */
129 GLboolean direct
; /* Direct rendering context? */
131 GLuint pixelformat
; /* Current pixel format */
133 GLubyte clearcolor
[4]; /* current clearing color */
134 unsigned long clearpixel
; /* current clearing pixel value */
140 * "Derived" from GLframebuffer. Basically corresponds to a GLXDrawable.
142 struct xmesa_buffer
{
143 GLframebuffer mesa_buffer
; /* depth, stencil, accum, etc buffers */
144 GLboolean wasCurrent
; /* was ever the current buffer? */
145 XMesaVisual xm_visual
; /* the X/Mesa visual */
147 XMesaContext xm_context
; /* the context associated with this buffer */
148 XMesaDisplay
*display
;
149 GLboolean pixmap_flag
; /* is the buffer a Pixmap? */
150 XMesaDrawable frontbuffer
; /* either a window or pixmap */
151 XMesaPixmap backpixmap
; /* back buffer Pixmap */
152 XMesaImage
*backimage
; /* back buffer simulated XImage */
154 XMesaDrawable buffer
; /* the current buffer, either equal to */
155 /* frontbuffer, backpixmap or XIMAGE (None) */
157 XMesaColormap cmap
; /* the X colormap */
159 GLint db_state
; /* 0 = single buffered */
160 /* BACK_PIXMAP = use Pixmap for back buffer */
161 /* BACK_XIMAGE = use XImage for back buffer */
163 #ifndef XFree86Server
164 GLuint shm
; /* X Shared Memory extension status: */
165 /* 0 = not available */
166 /* 1 = XImage support available */
167 /* 2 = Pixmap support available too */
169 XShmSegmentInfo shminfo
;
173 XMesaImage
*rowimage
; /* Used for optimized span writing */
175 GLuint width
, height
; /* size of buffer */
177 GLint bottom
; /* used for FLIP macro below */
178 GLubyte
*ximage_origin1
; /* used for PIXELADDR1 macro */
180 GLushort
*ximage_origin2
; /* used for PIXELADDR2 macro */
182 GLubyte
*ximage_origin3
; /* used for PIXELADDR3 macro */
184 GLuint
*ximage_origin4
; /* used for PIXELADDR4 macro */
187 XMesaPixmap stipple_pixmap
; /* For polygon stippling */
188 XMesaGC stipple_gc
; /* For polygon stippling */
190 XMesaGC gc
; /* scratch GC for span, line, tri drawing */
191 XMesaGC cleargc
; /* GC for clearing the color buffer */
193 /* The following are here instead of in the XMesaVisual
194 * because they depend on the window's colormap.
197 /* For PF_DITHER, PF_LOOKUP, PF_GRAYSCALE */
198 unsigned long color_table
[576]; /* RGB -> pixel value */
200 /* For PF_DITHER, PF_LOOKUP, PF_GRAYSCALE */
201 GLubyte pixel_to_r
[65536]; /* pixel value -> red */
202 GLubyte pixel_to_g
[65536]; /* pixel value -> green */
203 GLubyte pixel_to_b
[65536]; /* pixel value -> blue */
205 /* Used to do XAllocColor/XFreeColors accounting: */
207 #if defined(XFree86Server)
208 Pixel alloced_colors
[256];
210 unsigned long alloced_colors
[256];
214 /* For 3Dfx Glide only */
215 GLboolean FXisHackUsable
; /* Can we render into window? */
216 GLboolean FXwindowHack
; /* Are we rendering into a window? */
220 /* functions for clearing the front and back color buffers */
221 clear_func front_clear_func
;
222 clear_func back_clear_func
;
224 struct xmesa_buffer
*Next
; /* Linked list pointer: */
229 /* Values for xmesa->dest: */
230 #define FRONT_PIXMAP 1
231 #define BACK_PIXMAP 2
232 #define BACK_XIMAGE 4
235 /* Values for xmesa->pixelformat: */
236 #define PF_INDEX 1 /* Color Index mode */
237 #define PF_TRUECOLOR 2 /* TrueColor or DirectColor, any depth */
238 #define PF_TRUEDITHER 3 /* TrueColor with dithering */
239 #define PF_8A8B8G8R 4 /* 32-bit TrueColor: 8-A, 8-B, 8-G, 8-R */
240 #define PF_8R8G8B 5 /* 32-bit TrueColor: 8-R, 8-G, 8-B bits */
241 #define PF_5R6G5B 6 /* 16-bit TrueColor: 5-R, 6-G, 5-B bits */
242 #define PF_DITHER 7 /* Color-mapped RGB with dither */
243 #define PF_LOOKUP 8 /* Color-mapped RGB without dither */
244 #define PF_HPCR 9 /* HP Color Recovery (ad@lms.be 30/08/95) */
245 #define PF_1BIT 10 /* monochrome dithering of RGB */
246 #define PF_GRAYSCALE 11 /* Grayscale or StaticGray */
247 #define PF_8R8G8B24 12 /* 24-bit TrueColor: 8-R, 8-G, 8-B bits */
248 #define PF_DITHER_5R6G5B 13 /* 16-bit dithered TrueColor: 5-R, 6-G, 5-B */
252 * If pixelformat==PF_TRUECOLOR:
254 #define PACK_TRUECOLOR( PIXEL, R, G, B ) \
255 PIXEL = xmesa->xm_visual->RtoPixel[R] \
256 | xmesa->xm_visual->GtoPixel[G] \
257 | xmesa->xm_visual->BtoPixel[B]; \
261 * If pixelformat==PF_TRUEDITHER:
263 #define PACK_TRUEDITHER( PIXEL, X, Y, R, G, B ) \
265 int d = xmesa->xm_visual->Kernel[((X)&3) | (((Y)&3)<<2)]; \
266 PIXEL = xmesa->xm_visual->RtoPixel[(R)+d] \
267 | xmesa->xm_visual->GtoPixel[(G)+d] \
268 | xmesa->xm_visual->BtoPixel[(B)+d]; \
274 * If pixelformat==PF_8A8B8G8R:
276 #define PACK_8A8B8G8R( R, G, B, A ) \
277 ( ((A) << 24) | ((B) << 16) | ((G) << 8) | (R) )
281 * Like PACK_8A8B8G8R() but don't use alpha. This is usually an acceptable
284 #define PACK_8B8G8R( R, G, B ) ( ((B) << 16) | ((G) << 8) | (R) )
289 * If pixelformat==PF_8R8G8B:
291 #define PACK_8R8G8B( R, G, B) ( ((R) << 16) | ((G) << 8) | (B) )
295 * If pixelformat==PF_5R6G5B:
297 #define PACK_5R6G5B( R, G, B) ( (((R) & 0xf8) << 8) | (((G) & 0xfc) << 3) | ((B) >> 3) )
303 * If pixelformat==PF_DITHER:
305 * Improved 8-bit RGB dithering code contributed by Bob Mercier
306 * (mercier@hollywood.cinenet.net). Thanks Bob!
316 # define _MIX(r,g,b) (((r)*_G+(g))*_B+(b))
321 # define _MIX(r,g,b) ( ((g)<<6) | ((b)<<3) | (r) )
327 /*#define _DITH(C,c,d) (((unsigned)((_D*(C-1)+1)*c+d))/(_D*256))*/
328 #define _DITH(C,c,d) (((unsigned)((_D*(C-1)+1)*c+d)) >> 12)
331 static int kernel8
[_DY
*_DX
] = {
332 0 * MAXC
, 8 * MAXC
, 2 * MAXC
, 10 * MAXC
,
333 12 * MAXC
, 4 * MAXC
, 14 * MAXC
, 6 * MAXC
,
334 3 * MAXC
, 11 * MAXC
, 1 * MAXC
, 9 * MAXC
,
335 15 * MAXC
, 7 * MAXC
, 13 * MAXC
, 5 * MAXC
,
339 /* Dither for random X,Y */
340 #define DITHER_SETUP \
342 unsigned long *ctable = xmesa->xm_buffer->color_table;
344 #define DITHER( X, Y, R, G, B ) \
345 (__d = kernel8[(((Y)&3)<<2) | ((X)&3)], \
346 ctable[_MIX(_DITH(_R, (R), __d), \
347 _DITH(_G, (G), __d), \
348 _DITH(_B, (B), __d))])
350 /* Dither for random X, fixed Y */
351 #define XDITHER_SETUP(Y) \
353 unsigned long *ctable = xmesa->xm_buffer->color_table; \
354 int *kernel = &kernel8[ ((Y)&3) << 2 ];
356 #define XDITHER( X, R, G, B ) \
357 (__d = kernel[(X)&3], \
358 ctable[_MIX(_DITH(_R, (R), __d), \
359 _DITH(_G, (G), __d), \
360 _DITH(_B, (B), __d))])
365 * Dithering for flat-shaded triangles. Precompute all 16 possible
366 * pixel values given the triangle's RGB color. Contributed by Martin Shenk.
368 static GLushort DitherValues
[16]; /* array of (up to) 16-bit pixel values */
370 #define FLAT_DITHER_SETUP( R, G, B ) \
372 unsigned long *ctable = xmesa->xm_buffer->color_table; \
373 int msdr = (_D*((_R)-1)+1) * (R); \
374 int msdg = (_D*((_G)-1)+1) * (G); \
375 int msdb = (_D*((_B)-1)+1) * (B); \
377 for (i=0;i<16;i++) { \
378 int k = kernel8[i]; \
379 int j = _MIX( (msdr+k)>>12, (msdg+k)>>12, (msdb+k)>>12 ); \
380 DitherValues[i] = (GLushort) ctable[j]; \
384 #define FLAT_DITHER_ROW_SETUP(Y) \
385 GLushort *ditherRow = DitherValues + ( ((Y)&3) << 2);
387 #define FLAT_DITHER(X) ditherRow[(X)&3]
392 * If pixelformat==PF_LOOKUP:
394 #define _DITH0(C,c) (((unsigned)((_D*(C-1)+1)*c)) >> 12)
396 #define LOOKUP_SETUP \
397 unsigned long *ctable = xmesa->xm_buffer->color_table
399 #define LOOKUP( R, G, B ) \
400 ctable[_MIX(_DITH0(_R, (R)), \
407 * If pixelformat==PF_HPCR:
409 * HP Color Recovery dithering (ad@lms.be 30/08/95)
410 * HP has on it's 8-bit 700-series computers, a feature called
411 * 'Color Recovery'. This allows near 24-bit output (so they say).
412 * It is enabled by selecting the 8-bit TrueColor visual AND
413 * corresponding colormap (see tkInitWindow) AND doing some special
416 static const short HPCR_DRGB
[3][2][16] = {
418 { 16, -4, 1,-11, 14, -6, 3, -9, 15, -5, 2,-10, 13, -7, 4, -8},
419 {-15, 5, 0, 12,-13, 7, -2, 10,-14, 6, -1, 11,-12, 8, -3, 9}
422 {-11, 15, -7, 3, -8, 14, -4, 2,-10, 16, -6, 4, -9, 13, -5, 1},
423 { 12,-14, 8, -2, 9,-13, 5, -1, 11,-15, 7, -3, 10,-12, 6, 0}
426 { 6,-18, 26,-14, 2,-22, 30,-10, 8,-16, 28,-12, 4,-20, 32, -8},
427 { -4, 20,-24, 16, 0, 24,-28, 12, -6, 18,-26, 14, -2, 22,-30, 10}
431 #define DITHER_HPCR( X, Y, R, G, B ) \
432 ( ((xmesa->xm_visual->hpcr_rgbTbl[0][R] + HPCR_DRGB[0][(Y)&1][(X)&15]) & 0xE0) \
433 |(((xmesa->xm_visual->hpcr_rgbTbl[1][G] + HPCR_DRGB[1][(Y)&1][(X)&15]) & 0xE0)>>3) \
434 | ((xmesa->xm_visual->hpcr_rgbTbl[2][B] + HPCR_DRGB[2][(Y)&1][(X)&15])>>6) \
440 * If pixelformat==PF_1BIT:
442 static int const kernel1
[16] = {
443 0*47, 9*47, 4*47, 12*47, /* 47 = (255*3)/16 */
444 6*47, 2*47, 14*47, 8*47,
445 10*47, 1*47, 5*47, 11*47,
446 7*47, 13*47, 3*47, 15*47 };
448 #define SETUP_1BIT int bitFlip = xmesa->xm_visual->bitFlip
449 #define DITHER_1BIT( X, Y, R, G, B ) \
450 (( ((int)(R)+(int)(G)+(int)(B)) > kernel1[(((Y)&3) << 2) | ((X)&3)] ) ^ bitFlip)
455 * If pixelformat==PF_GRAYSCALE:
457 #define GRAY_RGB( R, G, B ) xmesa->xm_buffer->color_table[((R) + (G) + (B))/3]
465 * Converts a GL window Y coord to an X window Y coord:
467 #define FLIP(BUFFER, Y) ((BUFFER)->bottom-(Y))
471 * Return the address of a 1, 2 or 4-byte pixel in the back XImage:
472 * X==0 is left, Y==0 is bottom.
474 #define PIXELADDR1( BUFFER, X, Y ) \
475 ( (BUFFER)->ximage_origin1 - (Y) * (BUFFER)->ximage_width1 + (X) )
477 #define PIXELADDR2( BUFFER, X, Y ) \
478 ( (BUFFER)->ximage_origin2 - (Y) * (BUFFER)->ximage_width2 + (X) )
480 #define PIXELADDR3( BUFFER, X, Y ) \
481 ( (bgr_t *) ( (BUFFER)->ximage_origin3 - (Y) * (BUFFER)->ximage_width3 + 3 * (X) ))
483 #define PIXELADDR4( BUFFER, X, Y ) \
484 ( (BUFFER)->ximage_origin4 - (Y) * (BUFFER)->ximage_width4 + (X) )
489 * External functions:
493 xmesa_color_to_pixel( XMesaContext xmesa
,
494 GLubyte r
, GLubyte g
, GLubyte b
, GLubyte a
,
495 GLuint pixelFormat
);
497 extern void xmesa_alloc_back_buffer( XMesaBuffer b
);
499 extern void xmesa_init_pointers( GLcontext
*ctx
);
500 extern void xmesa_update_state( GLcontext
*ctx
, GLuint new_state
);
502 extern void xmesa_update_span_funcs( GLcontext
*ctx
);
504 /* Plugged into the software rasterizer. Try to use internal
505 * swrast-style point, line and triangle functions.
507 extern void xmesa_choose_point( GLcontext
*ctx
);
508 extern void xmesa_choose_line( GLcontext
*ctx
);
509 extern void xmesa_choose_triangle( GLcontext
*ctx
);
512 extern void xmesa_register_swrast_functions( GLcontext
*ctx
);
516 /* XXX this is a hack to implement shared display lists with 3Dfx */
517 extern XMesaBuffer
XMesaCreateWindowBuffer2( XMesaVisual v
,
523 * These are the extra routines required for integration with XFree86.
524 * None of these routines should be user visible. -KEM
526 extern void XMesaSetVisualDisplay( XMesaDisplay
*dpy
, XMesaVisual v
);
527 extern GLboolean
XMesaForceCurrent(XMesaContext c
);
528 extern GLboolean
XMesaLoseCurrent(XMesaContext c
);
529 extern void XMesaReset( void );
531 extern void xmesa_set_read_buffer( GLcontext
*ctx
,
532 GLframebuffer
*buffer
, GLenum mode
);