1 /* $Id: xmesaP.h,v 1.1 1999/08/19 00:55:42 jtg 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 "xf86glx_util.h"
36 #include <X11/extensions/XShm.h>
42 #include "GL/fxmesa.h"
43 #include "../FX/fxdrv.h"
47 /* for PF_8R8G8B24 pixel format */
56 * "Derived" from gl_visual. Basically corresponds to an XVisualInfo.
59 GLvisual
*gl_visual
; /* Device independent visual parameters */
60 XMesaDisplay
*display
; /* The X11 display */
62 XVisualInfo
*vishandle
; /* The pointer returned by glXChooseVisual */
64 XMesaVisualInfo visinfo
; /* X's visual info */
66 GLint level
; /* 0=normal, 1=overlay, etc */
68 GLboolean ximage_flag
; /* Use XImage for back buffer (not pixmap)? */
70 GLuint dithered_pf
; /* Pixel format when dithering */
71 GLuint undithered_pf
; /* Pixel format when not dithering */
73 GLfloat RedGamma
; /* Gamma values, 1.0 is default */
77 GLint rmult
, gmult
, bmult
; /* Range of color values */
78 GLint index_bits
; /* Bits per pixel in CI mode */
80 /* For PF_TRUECOLOR */
81 GLint rshift
, gshift
, bshift
;/* Pixel color component shifts */
82 GLubyte Kernel
[16]; /* Dither kernel */
83 unsigned long RtoPixel
[512]; /* RGB to pixel conversion */
84 unsigned long GtoPixel
[512];
85 unsigned long BtoPixel
[512];
86 GLubyte PixelToR
[256]; /* Pixel to RGB conversion */
87 GLubyte PixelToG
[256];
88 GLubyte PixelToB
[256];
91 short hpcr_rgbTbl
[3][256];
92 GLboolean hpcr_clear_flag
;
93 GLubyte hpcr_clear_ximage_pattern
[2][16];
94 XMesaImage
*hpcr_clear_ximage
;
95 XMesaPixmap hpcr_clear_pixmap
;
104 * "Derived" from gl_context. Basically corresponds to a GLXContext.
106 struct xmesa_context
{
107 GLcontext
*gl_ctx
; /* the core library context */
108 XMesaVisual xm_visual
; /* Describes the buffers */
109 XMesaBuffer xm_buffer
; /* current framebuffer */
111 XMesaDisplay
*display
; /* == xm_visual->display */
112 GLboolean swapbytes
; /* Host byte order != display byte order? */
113 GLboolean direct
; /* Direct rendering context? */
115 GLuint pixelformat
; /* Current pixel format */
117 GLubyte red
, green
, blue
, alpha
; /* current drawing color */
118 unsigned long pixel
; /* current drawing pixel value */
120 GLubyte clearcolor
[4]; /* current clearing color */
121 unsigned long clearpixel
; /* current clearing pixel value */
127 * "Derived" from gl_buffer. Basically corresponds to a GLXDrawable.
129 struct xmesa_buffer
{
130 GLboolean wasCurrent
; /* was ever the current buffer? */
131 GLframebuffer
*gl_buffer
; /* depth, stencil, accum, etc buffers */
132 XMesaVisual xm_visual
; /* the X/Mesa visual */
134 XMesaContext xm_context
; /* the context associated with this buffer */
135 XMesaDisplay
*display
;
136 GLboolean pixmap_flag
; /* is the buffer a Pixmap? */
137 XMesaDrawable frontbuffer
; /* either a window or pixmap */
138 XMesaPixmap backpixmap
; /* back buffer Pixmap */
139 XMesaImage
*backimage
; /* back buffer simulated XImage */
141 XMesaDrawable buffer
; /* the current buffer, either equal to */
142 /* frontbuffer, backpixmap or XIMAGE (None) */
144 XMesaColormap cmap
; /* the X colormap */
146 GLint db_state
; /* 0 = single buffered */
147 /* BACK_PIXMAP = use Pixmap for back buffer */
148 /* BACK_XIMAGE = use XImage for back buffer */
150 #ifndef XFree86Server
151 GLuint shm
; /* X Shared Memory extension status: */
152 /* 0 = not available */
153 /* 1 = XImage support available */
154 /* 2 = Pixmap support available too */
156 XShmSegmentInfo shminfo
;
160 XMesaImage
*rowimage
; /* Used for optimized span writing */
162 GLuint width
, height
; /* size of buffer */
164 GLint bottom
; /* used for FLIP macro below */
165 GLubyte
*ximage_origin1
; /* used for PIXELADDR1 macro */
167 GLushort
*ximage_origin2
; /* used for PIXELADDR2 macro */
169 bgr_t
*ximage_origin3
; /* used for PIXELADDR3 macro */
171 GLuint
*ximage_origin4
; /* used for PIXELADDR4 macro */
174 XMesaPixmap stipple_pixmap
; /* For polygon stippling */
175 XMesaGC stipple_gc
; /* For polygon stippling */
177 XMesaGC gc1
; /* GC for infrequent color changes */
178 XMesaGC gc2
; /* GC for frequent color changes */
179 XMesaGC cleargc
; /* GC for clearing the color buffer */
181 /* The following are here instead of in the XMesaVisual
182 * because they depend on the window's colormap.
185 /* For PF_DITHER, PF_LOOKUP, PF_GRAYSCALE */
186 unsigned long color_table
[576]; /* RGB -> pixel value */
188 /* For PF_DITHER, PF_LOOKUP, PF_GRAYSCALE */
189 GLubyte pixel_to_r
[65536]; /* pixel value -> red */
190 GLubyte pixel_to_g
[65536]; /* pixel value -> green */
191 GLubyte pixel_to_b
[65536]; /* pixel value -> blue */
193 /* Used to do XAllocColor/XFreeColors accounting: */
195 unsigned long alloced_colors
[256];
198 /* For 3Dfx Glide only */
199 GLboolean FXisHackUsable
; /* Can we render into window? */
200 GLboolean FXwindowHack
; /* Are we rendering into a window? */
204 struct xmesa_buffer
*Next
; /* Linked list pointer: */
209 /* Values for xmesa->dest: */
210 #define FRONT_PIXMAP 1
211 #define BACK_PIXMAP 2
212 #define BACK_XIMAGE 4
215 /* Values for xmesa->pixelformat: */
216 #define PF_INDEX 1 /* Color Index mode */
217 #define PF_TRUECOLOR 2 /* TrueColor or DirectColor, any depth */
218 #define PF_TRUEDITHER 3 /* TrueColor with dithering */
219 #define PF_8A8B8G8R 4 /* 32-bit TrueColor: 8-A, 8-B, 8-G, 8-R */
220 #define PF_8R8G8B 5 /* 32-bit TrueColor: 8-R, 8-G, 8-B bits */
221 #define PF_5R6G5B 6 /* 16-bit TrueColor: 5-R, 6-G, 5-B bits */
222 #define PF_DITHER 7 /* Color-mapped RGB with dither */
223 #define PF_LOOKUP 8 /* Color-mapped RGB without dither */
224 #define PF_HPCR 9 /* HP Color Recovery (ad@lms.be 30/08/95) */
225 #define PF_1BIT 10 /* monochrome dithering of RGB */
226 #define PF_GRAYSCALE 11 /* Grayscale or StaticGray */
227 #define PF_8R8G8B24 12 /* 24-bit TrueColor: 8-R, 8-G, 8-B bits */
228 #define PF_DITHER_5R6G5B 13 /* 16-bit dithered TrueColor: 5-R, 6-G, 5-B */
232 * If pixelformat==PF_TRUECOLOR:
234 #define PACK_TRUECOLOR( PIXEL, R, G, B ) \
235 PIXEL = xmesa->xm_visual->RtoPixel[R] \
236 | xmesa->xm_visual->GtoPixel[G] \
237 | xmesa->xm_visual->BtoPixel[B]; \
241 * If pixelformat==PF_TRUEDITHER:
243 #define PACK_TRUEDITHER( PIXEL, X, Y, R, G, B ) \
245 int d = xmesa->xm_visual->Kernel[((X)&3) | (((Y)&3)<<2)]; \
246 PIXEL = xmesa->xm_visual->RtoPixel[(R)+d] \
247 | xmesa->xm_visual->GtoPixel[(G)+d] \
248 | xmesa->xm_visual->BtoPixel[(B)+d]; \
254 * If pixelformat==PF_8A8B8G8R:
256 #define PACK_8A8B8G8R( R, G, B, A ) \
257 ( ((A) << 24) | ((B) << 16) | ((G) << 8) | (R) )
261 * Like PACK_8A8B8G8R() but don't use alpha. This is usually an acceptable
264 #define PACK_8B8G8R( R, G, B ) ( ((B) << 16) | ((G) << 8) | (R) )
269 * If pixelformat==PF_8R8G8B:
271 #define PACK_8R8G8B( R, G, B) ( ((R) << 16) | ((G) << 8) | (B) )
275 * If pixelformat==PF_5R6G5B:
277 #define PACK_5R6G5B( R, G, B) ( (((R) & 0xf8) << 8) | (((G) & 0xfc) << 3) | ((B) >> 3) )
282 * If pixelformat==PF_DITHER:
284 * Improved 8-bit RGB dithering code contributed by Bob Mercier
285 * (mercier@hollywood.cinenet.net). Thanks Bob!
294 # define _MIX(r,g,b) (((r)*_G+(g))*_B+(b))
299 # define _MIX(r,g,b) ( ((g)<<6) | ((b)<<3) | (r) )
305 /*#define _DITH(C,c,d) (((unsigned)((_D*(C-1)+1)*c+d))/(_D*256))*/
306 #define _DITH(C,c,d) (((unsigned)((_D*(C-1)+1)*c+d)) >> 12)
309 static int kernel8
[_DY
*_DX
] = {
310 0 * MAXC
, 8 * MAXC
, 2 * MAXC
, 10 * MAXC
,
311 12 * MAXC
, 4 * MAXC
, 14 * MAXC
, 6 * MAXC
,
312 3 * MAXC
, 11 * MAXC
, 1 * MAXC
, 9 * MAXC
,
313 15 * MAXC
, 7 * MAXC
, 13 * MAXC
, 5 * MAXC
,
317 /* Dither for random X,Y */
318 #define DITHER_SETUP \
320 unsigned long *ctable = xmesa->xm_buffer->color_table;
322 #define DITHER( X, Y, R, G, B ) \
323 (__d = kernel8[(((Y)&3)<<2) | ((X)&3)], \
324 ctable[_MIX(_DITH(_R, (R), __d), \
325 _DITH(_G, (G), __d), \
326 _DITH(_B, (B), __d))])
328 /* Dither for random X, fixed Y */
329 #define XDITHER_SETUP(Y) \
331 unsigned long *ctable = xmesa->xm_buffer->color_table; \
332 int *kernel = &kernel8[ ((Y)&3) << 2 ];
334 #define XDITHER( X, R, G, B ) \
335 (__d = kernel[(X)&3], \
336 ctable[_MIX(_DITH(_R, (R), __d), \
337 _DITH(_G, (G), __d), \
338 _DITH(_B, (B), __d))])
343 * Dithering for flat-shaded triangles. Precompute all 16 possible
344 * pixel values given the triangle's RGB color. Contributed by Martin Shenk.
346 static GLushort DitherValues
[16]; /* array of (up to) 16-bit pixel values */
348 #define FLAT_DITHER_SETUP( R, G, B ) \
350 unsigned long *ctable = xmesa->xm_buffer->color_table; \
351 int msdr = (_D*((_R)-1)+1) * (R); \
352 int msdg = (_D*((_G)-1)+1) * (G); \
353 int msdb = (_D*((_B)-1)+1) * (B); \
355 for (i=0;i<16;i++) { \
356 int k = kernel8[i]; \
357 int j = _MIX( (msdr+k)>>12, (msdg+k)>>12, (msdb+k)>>12 ); \
358 DitherValues[i] = (GLushort) ctable[j]; \
362 #define FLAT_DITHER_ROW_SETUP(Y) \
363 GLushort *ditherRow = DitherValues + ( ((Y)&3) << 2);
365 #define FLAT_DITHER(X) ditherRow[(X)&3]
370 * If pixelformat==PF_LOOKUP:
372 #define _DITH0(C,c) (((unsigned)((_D*(C-1)+1)*c)) >> 12)
374 #define LOOKUP_SETUP \
375 unsigned long *ctable = xmesa->xm_buffer->color_table
377 #define LOOKUP( R, G, B ) \
378 ctable[_MIX(_DITH0(_R, (R)), \
385 * If pixelformat==PF_HPCR:
387 * HP Color Recovery dithering (ad@lms.be 30/08/95)
388 * HP has on it's 8-bit 700-series computers, a feature called
389 * 'Color Recovery'. This allows near 24-bit output (so they say).
390 * It is enabled by selecting the 8-bit TrueColor visual AND
391 * corresponding colormap (see tkInitWindow) AND doing some special
394 static const short HPCR_DRGB
[3][2][16] = {
396 { 16, -4, 1,-11, 14, -6, 3, -9, 15, -5, 2,-10, 13, -7, 4, -8},
397 {-15, 5, 0, 12,-13, 7, -2, 10,-14, 6, -1, 11,-12, 8, -3, 9}
400 {-11, 15, -7, 3, -8, 14, -4, 2,-10, 16, -6, 4, -9, 13, -5, 1},
401 { 12,-14, 8, -2, 9,-13, 5, -1, 11,-15, 7, -3, 10,-12, 6, 0}
404 { 6,-18, 26,-14, 2,-22, 30,-10, 8,-16, 28,-12, 4,-20, 32, -8},
405 { -4, 20,-24, 16, 0, 24,-28, 12, -6, 18,-26, 14, -2, 22,-30, 10}
409 #define DITHER_HPCR( X, Y, R, G, B ) \
410 ( ((xmesa->xm_visual->hpcr_rgbTbl[0][R] + HPCR_DRGB[0][(Y)&1][(X)&15]) & 0xE0) \
411 |(((xmesa->xm_visual->hpcr_rgbTbl[1][G] + HPCR_DRGB[1][(Y)&1][(X)&15]) & 0xE0)>>3) \
412 | ((xmesa->xm_visual->hpcr_rgbTbl[2][B] + HPCR_DRGB[2][(Y)&1][(X)&15])>>6) \
418 * If pixelformat==PF_1BIT:
420 static int const kernel1
[16] = {
421 0*47, 9*47, 4*47, 12*47, /* 47 = (255*3)/16 */
422 6*47, 2*47, 14*47, 8*47,
423 10*47, 1*47, 5*47, 11*47,
424 7*47, 13*47, 3*47, 15*47 };
426 #define SETUP_1BIT int bitFlip = xmesa->xm_visual->bitFlip
427 #define DITHER_1BIT( X, Y, R, G, B ) \
428 (( ((int)(R)+(int)(G)+(int)(B)) > kernel1[(((Y)&3) << 2) | ((X)&3)] ) ^ bitFlip)
433 * If pixelformat==PF_GRAYSCALE:
435 #define GRAY_RGB( R, G, B ) xmesa->xm_buffer->color_table[((R) + (G) + (B))/3]
443 * Converts a GL window Y coord to an X window Y coord:
445 #define FLIP(Y) (xmesa->xm_buffer->bottom-(Y))
449 * Return the address of a 1, 2 or 4-byte pixel in the back XImage:
450 * X==0 is left, Y==0 is bottom.
452 #define PIXELADDR1( X, Y ) \
453 ( xmesa->xm_buffer->ximage_origin1 - (Y) * xmesa->xm_buffer->ximage_width1 + (X) )
455 #define PIXELADDR2( X, Y ) \
456 ( xmesa->xm_buffer->ximage_origin2 - (Y) * xmesa->xm_buffer->ximage_width2 + (X) )
458 #define PIXELADDR3( X, Y ) \
459 ( xmesa->xm_buffer->ximage_origin3 - (Y) * xmesa->xm_buffer->ximage_width3 + (X) )
461 #define PIXELADDR4( X, Y ) \
462 ( xmesa->xm_buffer->ximage_origin4 - (Y) * xmesa->xm_buffer->ximage_width4 + (X) )
467 * External functions:
470 extern unsigned long xmesa_color_to_pixel( XMesaContext xmesa
,
471 GLubyte r
, GLubyte g
, GLubyte b
, GLubyte a
);
473 extern void xmesa_alloc_back_buffer( XMesaBuffer b
);
475 extern void xmesa_update_state( GLcontext
*ctx
);
477 extern points_func
xmesa_get_points_func( GLcontext
*ctx
);
479 extern line_func
xmesa_get_line_func( GLcontext
*ctx
);
481 extern triangle_func
xmesa_get_triangle_func( GLcontext
*ctx
);
484 /* XXX this is a hack to implement shared display lists with 3Dfx */
485 extern XMesaBuffer
XMesaCreateWindowBuffer2( XMesaVisual v
,
491 * These are the extra routines required for integration with XFree86.
492 * None of these routines should be user visible. -KEM
494 extern void XMesaSetVisualDisplay( XMesaDisplay
*dpy
, XMesaVisual v
);
495 extern GLboolean
XMesaForceCurrent(XMesaContext c
);
496 extern GLboolean
XMesaLoseCurrent(XMesaContext c
);
497 extern void XMesaReset( void );