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