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