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