bb014a0df39f7526008ff895f0adea76868b6880
[mesa.git] / src / mesa / drivers / x11 / xmesaP.h
1
2 /*
3 * Mesa 3-D graphics library
4 * Version: 5.0.1
5 *
6 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27 #ifndef XMESAP_H
28 #define XMESAP_H
29
30
31 #ifdef XFree86Server
32 # include "GL/xf86glx.h"
33 # include "xf86glx_util.h"
34 #else
35 # ifdef USE_XSHM
36 # include <X11/extensions/XShm.h>
37 # endif
38 #endif
39 #include "GL/xmesa.h"
40 #include "mtypes.h"
41 #if defined(FX)
42 #include "GL/fxmesa.h"
43 #include "FX/fxdrv.h"
44 #endif
45
46
47 extern _glthread_Mutex _xmesa_lock;
48
49
50 /* for PF_8R8G8B24 pixel format */
51 typedef struct {
52 GLubyte b;
53 GLubyte g;
54 GLubyte r;
55 } bgr_t;
56
57
58 /* Function pointer for clearing color buffers */
59 typedef void (*clear_func)( GLcontext *ctx,
60 GLboolean all, GLint x, GLint y,
61 GLint width, GLint height );
62
63
64
65
66 /** Framebuffer pixel formats */
67 enum pixel_format {
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 };
82
83
84 /*
85 * "Derived" from GLvisual. Basically corresponds to an XVisualInfo.
86 */
87 struct xmesa_visual {
88 GLvisual mesa_visual; /* Device independent visual parameters */
89 XMesaDisplay *display; /* The X11 display */
90 #ifdef XFree86Server
91 GLint screen_depth; /* The depth of the screen */
92 #else
93 XVisualInfo *vishandle; /* Only used in fakeglx.c */
94 #endif
95 XMesaVisualInfo visinfo; /* X's visual info (pointer to private copy) */
96 GLint BitsPerPixel; /* True bits per pixel for XImages */
97
98 GLint level; /* 0=normal, 1=overlay, etc */
99 GLint VisualCaveat; /* for GLX_EXT_visual_rating extension */
100
101 GLboolean ximage_flag; /* Use XImage for back buffer (not pixmap)? */
102
103 enum pixel_format dithered_pf; /* Pixel format when dithering */
104 enum pixel_format undithered_pf;/* Pixel format when not dithering */
105
106 GLfloat RedGamma; /* Gamma values, 1.0 is default */
107 GLfloat GreenGamma;
108 GLfloat BlueGamma;
109
110 GLint rmult, gmult, bmult; /* Range of color values */
111 GLint index_bits; /* Bits per pixel in CI mode */
112
113 /* For PF_TRUECOLOR */
114 GLint rshift, gshift, bshift;/* Pixel color component shifts */
115 GLubyte Kernel[16]; /* Dither kernel */
116 unsigned long RtoPixel[512]; /* RGB to pixel conversion */
117 unsigned long GtoPixel[512];
118 unsigned long BtoPixel[512];
119 GLubyte PixelToR[256]; /* Pixel to RGB conversion */
120 GLubyte PixelToG[256];
121 GLubyte PixelToB[256];
122
123 /* For PF_HPCR */
124 short hpcr_rgbTbl[3][256];
125 GLboolean hpcr_clear_flag;
126 GLubyte hpcr_clear_ximage_pattern[2][16];
127 XMesaImage *hpcr_clear_ximage;
128 XMesaPixmap hpcr_clear_pixmap;
129
130 /* For PF_1BIT */
131 int bitFlip;
132 };
133
134
135 /*
136 * "Derived" from __GLcontextRec. Basically corresponds to a GLXContext.
137 */
138 struct xmesa_context {
139 GLcontext mesa; /* the core library context (containment) */
140 XMesaVisual xm_visual; /* Describes the buffers */
141 XMesaBuffer xm_draw_buffer; /* current draw framebuffer */
142 XMesaBuffer xm_read_buffer; /* current read framebuffer */
143 XMesaBuffer xm_buffer; /* current span/point/line/triangle buffer */
144
145 XMesaDisplay *display; /* == xm_visual->display */
146 GLboolean swapbytes; /* Host byte order != display byte order? */
147 GLboolean direct; /* Direct rendering context? */
148
149 enum pixel_format pixelformat;
150
151 GLubyte clearcolor[4]; /* current clearing color */
152 unsigned long clearpixel; /* current clearing pixel value */
153 };
154
155
156
157 typedef enum {
158 WINDOW, /* An X window */
159 GLXWINDOW, /* GLX window */
160 PIXMAP, /* GLX pixmap */
161 PBUFFER /* GLX Pbuffer */
162 } BufferType;
163
164
165 /*
166 * "Derived" from GLframebuffer. Basically corresponds to a GLXDrawable.
167 */
168 struct xmesa_buffer {
169 GLframebuffer mesa_buffer; /* depth, stencil, accum, etc buffers */
170 /* This MUST BE FIRST! */
171 GLboolean wasCurrent; /* was ever the current buffer? */
172 XMesaVisual xm_visual; /* the X/Mesa visual */
173
174 XMesaDisplay *display;
175 BufferType type; /* window, pixmap, pbuffer or glxwindow */
176 XMesaDrawable frontbuffer; /* either a window or pixmap */
177 XMesaPixmap backpixmap; /* back buffer Pixmap */
178 XMesaImage *backimage; /* back buffer simulated XImage */
179
180 XMesaDrawable buffer; /* the current buffer, either equal to */
181 /* frontbuffer, backpixmap or XIMAGE (None) */
182
183 XMesaColormap cmap; /* the X colormap */
184
185 unsigned long selectedEvents;/* for pbuffers only */
186
187 GLint db_state; /* 0 = single buffered */
188 /* BACK_PIXMAP = use Pixmap for back buffer */
189 /* BACK_XIMAGE = use XImage for back buffer */
190
191 #ifndef XFree86Server
192 GLuint shm; /* X Shared Memory extension status: */
193 /* 0 = not available */
194 /* 1 = XImage support available */
195 /* 2 = Pixmap support available too */
196 #ifdef USE_XSHM
197 XShmSegmentInfo shminfo;
198 #endif
199 #endif
200
201 XMesaImage *rowimage; /* Used for optimized span writing */
202
203 GLuint width, height; /* size of buffer */
204
205 GLint bottom; /* used for FLIP macro below */
206 GLubyte *ximage_origin1; /* used for PIXELADDR1 macro */
207 GLint ximage_width1;
208 GLushort *ximage_origin2; /* used for PIXELADDR2 macro */
209 GLint ximage_width2;
210 GLubyte *ximage_origin3; /* used for PIXELADDR3 macro */
211 GLint ximage_width3;
212 GLuint *ximage_origin4; /* used for PIXELADDR4 macro */
213 GLint ximage_width4;
214
215 XMesaPixmap stipple_pixmap; /* For polygon stippling */
216 XMesaGC stipple_gc; /* For polygon stippling */
217
218 XMesaGC gc; /* scratch GC for span, line, tri drawing */
219 XMesaGC cleargc; /* GC for clearing the color buffer */
220 XMesaGC swapgc; /* GC for swapping the color buffers */
221
222 /* The following are here instead of in the XMesaVisual
223 * because they depend on the window's colormap.
224 */
225
226 /* For PF_DITHER, PF_LOOKUP, PF_GRAYSCALE */
227 unsigned long color_table[576]; /* RGB -> pixel value */
228
229 /* For PF_DITHER, PF_LOOKUP, PF_GRAYSCALE */
230 GLubyte pixel_to_r[65536]; /* pixel value -> red */
231 GLubyte pixel_to_g[65536]; /* pixel value -> green */
232 GLubyte pixel_to_b[65536]; /* pixel value -> blue */
233
234 /* Used to do XAllocColor/XFreeColors accounting: */
235 int num_alloced;
236 #if defined(XFree86Server)
237 Pixel alloced_colors[256];
238 #else
239 unsigned long alloced_colors[256];
240 #endif
241
242 #if defined( FX )
243 /* For 3Dfx Glide only */
244 GLboolean FXisHackUsable; /* Can we render into window? */
245 GLboolean FXwindowHack; /* Are we rendering into a window? */
246 fxMesaContext FXctx;
247 #endif
248
249 /* functions for clearing the front and back color buffers */
250 clear_func front_clear_func;
251 clear_func back_clear_func;
252
253 struct xmesa_buffer *Next; /* Linked list pointer: */
254 };
255
256
257
258 /* Values for xmesa->dest: */
259 #define FRONT_PIXMAP 1
260 #define BACK_PIXMAP 2
261 #define BACK_XIMAGE 4
262
263
264 /*
265 * If pixelformat==PF_TRUECOLOR:
266 */
267 #define PACK_TRUECOLOR( PIXEL, R, G, B ) \
268 PIXEL = xmesa->xm_visual->RtoPixel[R] \
269 | xmesa->xm_visual->GtoPixel[G] \
270 | xmesa->xm_visual->BtoPixel[B]; \
271
272
273 /*
274 * If pixelformat==PF_TRUEDITHER:
275 */
276 #define PACK_TRUEDITHER( PIXEL, X, Y, R, G, B ) \
277 { \
278 int d = xmesa->xm_visual->Kernel[((X)&3) | (((Y)&3)<<2)]; \
279 PIXEL = xmesa->xm_visual->RtoPixel[(R)+d] \
280 | xmesa->xm_visual->GtoPixel[(G)+d] \
281 | xmesa->xm_visual->BtoPixel[(B)+d]; \
282 }
283
284
285
286 /*
287 * If pixelformat==PF_8A8B8G8R:
288 */
289 #define PACK_8A8B8G8R( R, G, B, A ) \
290 ( ((A) << 24) | ((B) << 16) | ((G) << 8) | (R) )
291
292
293 /*
294 * Like PACK_8A8B8G8R() but don't use alpha. This is usually an acceptable
295 * shortcut.
296 */
297 #define PACK_8B8G8R( R, G, B ) ( ((B) << 16) | ((G) << 8) | (R) )
298
299
300
301 /*
302 * If pixelformat==PF_8R8G8B:
303 */
304 #define PACK_8R8G8B( R, G, B) ( ((R) << 16) | ((G) << 8) | (B) )
305
306
307 /*
308 * If pixelformat==PF_5R6G5B:
309 */
310 #define PACK_5R6G5B( R, G, B) ( (((R) & 0xf8) << 8) | (((G) & 0xfc) << 3) | ((B) >> 3) )
311
312
313
314
315 /*
316 * If pixelformat==PF_DITHER:
317 *
318 * Improved 8-bit RGB dithering code contributed by Bob Mercier
319 * (mercier@hollywood.cinenet.net). Thanks Bob!
320 */
321 #ifdef DITHER666
322 # define DITH_R 6
323 # define DITH_G 6
324 # define DITH_B 6
325 # define DITH_MIX(r,g,b) (((r) * DITH_G + (g)) * DITH_B + (b))
326 #else
327 # define DITH_R 5
328 # define DITH_G 9
329 # define DITH_B 5
330 # define DITH_MIX(r,g,b) (((g) << 6) | ((b) << 3) | (r))
331 #endif
332 #define DITH_DX 4
333 #define DITH_DY 4
334 #define DITH_N (DITH_DX * DITH_DY)
335
336 /*#define _dither(C,c,d) (((unsigned)((DITH_N*(C-1)+1)*c+d))/(DITH_N*256))*/
337 #define _dither(C, c, d) (((unsigned)((DITH_N * (C - 1) + 1) * c + d)) >> 12)
338
339 #define MAXC 256
340 static int kernel8[DITH_DY * DITH_DX] = {
341 0 * MAXC, 8 * MAXC, 2 * MAXC, 10 * MAXC,
342 12 * MAXC, 4 * MAXC, 14 * MAXC, 6 * MAXC,
343 3 * MAXC, 11 * MAXC, 1 * MAXC, 9 * MAXC,
344 15 * MAXC, 7 * MAXC, 13 * MAXC, 5 * MAXC,
345 };
346
347 /* Dither for random X,Y */
348 #define DITHER_SETUP \
349 int __d; \
350 unsigned long *ctable = xmesa->xm_buffer->color_table;
351
352 #define DITHER( X, Y, R, G, B ) \
353 (__d = kernel8[(((Y)&3)<<2) | ((X)&3)], \
354 ctable[DITH_MIX(_dither(DITH_R, (R), __d), \
355 _dither(DITH_G, (G), __d), \
356 _dither(DITH_B, (B), __d))])
357
358 /* Dither for random X, fixed Y */
359 #define XDITHER_SETUP(Y) \
360 int __d; \
361 unsigned long *ctable = xmesa->xm_buffer->color_table; \
362 int *kernel = &kernel8[ ((Y)&3) << 2 ];
363
364 #define XDITHER( X, R, G, B ) \
365 (__d = kernel[(X)&3], \
366 ctable[DITH_MIX(_dither(DITH_R, (R), __d), \
367 _dither(DITH_G, (G), __d), \
368 _dither(DITH_B, (B), __d))])
369
370
371
372 /*
373 * Dithering for flat-shaded triangles. Precompute all 16 possible
374 * pixel values given the triangle's RGB color. Contributed by Martin Shenk.
375 */
376 static GLushort DitherValues[16]; /* array of (up to) 16-bit pixel values */
377
378 #define FLAT_DITHER_SETUP( R, G, B ) \
379 { \
380 unsigned long *ctable = xmesa->xm_buffer->color_table; \
381 int msdr = (DITH_N*((DITH_R)-1)+1) * (R); \
382 int msdg = (DITH_N*((DITH_G)-1)+1) * (G); \
383 int msdb = (DITH_N*((DITH_B)-1)+1) * (B); \
384 int i; \
385 for (i=0;i<16;i++) { \
386 int k = kernel8[i]; \
387 int j = DITH_MIX( (msdr+k)>>12, (msdg+k)>>12, (msdb+k)>>12 ); \
388 DitherValues[i] = (GLushort) ctable[j]; \
389 } \
390 }
391
392 #define FLAT_DITHER_ROW_SETUP(Y) \
393 GLushort *ditherRow = DitherValues + ( ((Y)&3) << 2);
394
395 #define FLAT_DITHER(X) ditherRow[(X)&3]
396
397
398
399 /*
400 * If pixelformat==PF_LOOKUP:
401 */
402 #define _dither_lookup(C, c) (((unsigned)((DITH_N * (C - 1) + 1) * c)) >> 12)
403
404 #define LOOKUP_SETUP \
405 unsigned long *ctable = xmesa->xm_buffer->color_table
406
407 #define LOOKUP( R, G, B ) \
408 ctable[DITH_MIX(_dither_lookup(DITH_R, (R)), \
409 _dither_lookup(DITH_G, (G)), \
410 _dither_lookup(DITH_B, (B)))]
411
412
413
414 /*
415 * If pixelformat==PF_HPCR:
416 *
417 * HP Color Recovery dithering (ad@lms.be 30/08/95)
418 * HP has on it's 8-bit 700-series computers, a feature called
419 * 'Color Recovery'. This allows near 24-bit output (so they say).
420 * It is enabled by selecting the 8-bit TrueColor visual AND
421 * corresponding colormap (see tkInitWindow) AND doing some special
422 * dither.
423 */
424 static const short HPCR_DRGB[3][2][16] = {
425 {
426 { 16, -4, 1,-11, 14, -6, 3, -9, 15, -5, 2,-10, 13, -7, 4, -8},
427 {-15, 5, 0, 12,-13, 7, -2, 10,-14, 6, -1, 11,-12, 8, -3, 9}
428 },
429 {
430 {-11, 15, -7, 3, -8, 14, -4, 2,-10, 16, -6, 4, -9, 13, -5, 1},
431 { 12,-14, 8, -2, 9,-13, 5, -1, 11,-15, 7, -3, 10,-12, 6, 0}
432 },
433 {
434 { 6,-18, 26,-14, 2,-22, 30,-10, 8,-16, 28,-12, 4,-20, 32, -8},
435 { -4, 20,-24, 16, 0, 24,-28, 12, -6, 18,-26, 14, -2, 22,-30, 10}
436 }
437 };
438
439 #define DITHER_HPCR( X, Y, R, G, B ) \
440 ( ((xmesa->xm_visual->hpcr_rgbTbl[0][R] + HPCR_DRGB[0][(Y)&1][(X)&15]) & 0xE0) \
441 |(((xmesa->xm_visual->hpcr_rgbTbl[1][G] + HPCR_DRGB[1][(Y)&1][(X)&15]) & 0xE0)>>3) \
442 | ((xmesa->xm_visual->hpcr_rgbTbl[2][B] + HPCR_DRGB[2][(Y)&1][(X)&15])>>6) \
443 )
444
445
446
447 /*
448 * If pixelformat==PF_1BIT:
449 */
450 static int const kernel1[16] = {
451 0*47, 9*47, 4*47, 12*47, /* 47 = (255*3)/16 */
452 6*47, 2*47, 14*47, 8*47,
453 10*47, 1*47, 5*47, 11*47,
454 7*47, 13*47, 3*47, 15*47 };
455
456 #define SETUP_1BIT int bitFlip = xmesa->xm_visual->bitFlip
457 #define DITHER_1BIT( X, Y, R, G, B ) \
458 (( ((int)(R)+(int)(G)+(int)(B)) > kernel1[(((Y)&3) << 2) | ((X)&3)] ) ^ bitFlip)
459
460
461
462 /*
463 * If pixelformat==PF_GRAYSCALE:
464 */
465 #define GRAY_RGB( R, G, B ) xmesa->xm_buffer->color_table[((R) + (G) + (B))/3]
466
467
468
469 #define XIMAGE None
470
471
472 /*
473 * Converts a GL window Y coord to an X window Y coord:
474 */
475 #define FLIP(BUFFER, Y) ((BUFFER)->bottom-(Y))
476
477
478 /*
479 * Return the address of a 1, 2 or 4-byte pixel in the back XImage:
480 * X==0 is left, Y==0 is bottom.
481 */
482 #define PIXELADDR1( BUFFER, X, Y ) \
483 ( (BUFFER)->ximage_origin1 - (Y) * (BUFFER)->ximage_width1 + (X) )
484
485 #define PIXELADDR2( BUFFER, X, Y ) \
486 ( (BUFFER)->ximage_origin2 - (Y) * (BUFFER)->ximage_width2 + (X) )
487
488 #define PIXELADDR3( BUFFER, X, Y ) \
489 ( (bgr_t *) ( (BUFFER)->ximage_origin3 - (Y) * (BUFFER)->ximage_width3 + 3 * (X) ))
490
491 #define PIXELADDR4( BUFFER, X, Y ) \
492 ( (BUFFER)->ximage_origin4 - (Y) * (BUFFER)->ximage_width4 + (X) )
493
494
495
496
497 /*
498 * Return pointer to XMesaContext corresponding to a Mesa GLcontext.
499 * Since we're using structure containment, it's just a cast!.
500 */
501 #define XMESA_CONTEXT(MESACTX) (XMesaContext) (MESACTX)
502
503
504
505 /*
506 * External functions:
507 */
508
509 extern unsigned long
510 xmesa_color_to_pixel( XMesaContext xmesa,
511 GLubyte r, GLubyte g, GLubyte b, GLubyte a,
512 GLuint pixelFormat );
513
514 extern void xmesa_alloc_back_buffer( XMesaBuffer b );
515
516 extern void xmesa_init_pointers( GLcontext *ctx );
517 extern void xmesa_update_state( GLcontext *ctx, GLuint new_state );
518
519 extern void xmesa_update_span_funcs( GLcontext *ctx );
520
521 /* Plugged into the software rasterizer. Try to use internal
522 * swrast-style point, line and triangle functions.
523 */
524 extern void xmesa_choose_point( GLcontext *ctx );
525 extern void xmesa_choose_line( GLcontext *ctx );
526 extern void xmesa_choose_triangle( GLcontext *ctx );
527
528
529 extern void xmesa_register_swrast_functions( GLcontext *ctx );
530
531
532
533 /* XXX this is a hack to implement shared display lists with 3Dfx */
534 extern XMesaBuffer XMesaCreateWindowBuffer2( XMesaVisual v,
535 XMesaWindow w,
536 XMesaContext c
537 );
538
539 /*
540 * These are the extra routines required for integration with XFree86.
541 * None of these routines should be user visible. -KEM
542 */
543 extern void XMesaSetVisualDisplay( XMesaDisplay *dpy, XMesaVisual v );
544 extern GLboolean XMesaForceCurrent(XMesaContext c);
545 extern GLboolean XMesaLoseCurrent(XMesaContext c);
546 extern void XMesaReset( void );
547
548 extern void xmesa_resize_buffers( GLframebuffer *buffer );
549
550
551 #endif