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