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