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