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