use separate GC for SwapBuffers to avoid colormask problem
[mesa.git] / src / mesa / drivers / x11 / xmesaP.h
1 /* $Id: xmesaP.h,v 1.25 2002/02/15 19:15:33 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 4.2
6 *
7 * Copyright (C) 1999-2002 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 USE_XSHM
37 # include <X11/extensions/XShm.h>
38 # endif
39 #endif
40 #include "GL/xmesa.h"
41 #include "mtypes.h"
42 #if defined(FX)
43 #include "GL/fxmesa.h"
44 #include "FX/fxdrv.h"
45 #endif
46
47
48 extern _glthread_Mutex _xmesa_lock;
49
50
51 /* for PF_8R8G8B24 pixel format */
52 typedef struct {
53 GLubyte b;
54 GLubyte g;
55 GLubyte r;
56 } bgr_t;
57
58
59 /* Function pointer for clearing color buffers */
60 typedef void (*clear_func)( GLcontext *ctx,
61 GLboolean all, GLint x, GLint y,
62 GLint width, GLint height );
63
64
65 /*
66 * "Derived" from GLvisual. Basically corresponds to an XVisualInfo.
67 */
68 struct xmesa_visual {
69 GLvisual mesa_visual; /* Device independent visual parameters */
70 XMesaDisplay *display; /* The X11 display */
71 #ifdef XFree86Server
72 GLint screen_depth; /* The depth of the screen */
73 #else
74 XVisualInfo *vishandle; /* The pointer returned by glXChooseVisual */
75 #endif
76 XMesaVisualInfo visinfo; /* X's visual info */
77 GLint BitsPerPixel; /* True bits per pixel for XImages */
78
79 GLint level; /* 0=normal, 1=overlay, etc */
80 GLint VisualCaveat; /* for GLX_EXT_visual_rating extension */
81
82 GLboolean ximage_flag; /* Use XImage for back buffer (not pixmap)? */
83
84 GLuint dithered_pf; /* Pixel format when dithering */
85 GLuint undithered_pf; /* Pixel format when not dithering */
86
87 GLfloat RedGamma; /* Gamma values, 1.0 is default */
88 GLfloat GreenGamma;
89 GLfloat BlueGamma;
90
91 GLint rmult, gmult, bmult; /* Range of color values */
92 GLint index_bits; /* Bits per pixel in CI mode */
93
94 /* For PF_TRUECOLOR */
95 GLint rshift, gshift, bshift;/* Pixel color component shifts */
96 GLubyte Kernel[16]; /* Dither kernel */
97 unsigned long RtoPixel[512]; /* RGB to pixel conversion */
98 unsigned long GtoPixel[512];
99 unsigned long BtoPixel[512];
100 GLubyte PixelToR[256]; /* Pixel to RGB conversion */
101 GLubyte PixelToG[256];
102 GLubyte PixelToB[256];
103
104 /* For PF_HPCR */
105 short hpcr_rgbTbl[3][256];
106 GLboolean hpcr_clear_flag;
107 GLubyte hpcr_clear_ximage_pattern[2][16];
108 XMesaImage *hpcr_clear_ximage;
109 XMesaPixmap hpcr_clear_pixmap;
110
111 /* For PF_1BIT */
112 int bitFlip;
113 };
114
115
116
117 /*
118 * "Derived" from __GLcontextRec. Basically corresponds to a GLXContext.
119 */
120 struct xmesa_context {
121 GLcontext *gl_ctx; /* the core library context */
122 XMesaVisual xm_visual; /* Describes the buffers */
123 XMesaBuffer xm_buffer; /* current draw framebuffer */
124 XMesaBuffer xm_read_buffer; /* current read framebuffer */
125 GLboolean use_read_buffer; /* read from the xm_read_buffer/ */
126
127 XMesaDisplay *display; /* == xm_visual->display */
128 GLboolean swapbytes; /* Host byte order != display byte order? */
129 GLboolean direct; /* Direct rendering context? */
130
131 GLuint pixelformat; /* Current pixel format */
132
133 GLubyte clearcolor[4]; /* current clearing color */
134 unsigned long clearpixel; /* current clearing pixel value */
135 };
136
137
138
139 /*
140 * "Derived" from GLframebuffer. Basically corresponds to a GLXDrawable.
141 */
142 struct xmesa_buffer {
143 GLframebuffer mesa_buffer; /* depth, stencil, accum, etc buffers */
144 GLboolean wasCurrent; /* was ever the current buffer? */
145 XMesaVisual xm_visual; /* the X/Mesa visual */
146
147 XMesaContext xm_context; /* the context associated with this buffer */
148 XMesaDisplay *display;
149 GLboolean pixmap_flag; /* is the buffer a Pixmap? */
150 GLboolean pbuffer_flag; /* is the buffer a Pbuffer? */
151 XMesaDrawable frontbuffer; /* either a window or pixmap */
152 XMesaPixmap backpixmap; /* back buffer Pixmap */
153 XMesaImage *backimage; /* back buffer simulated XImage */
154
155 XMesaDrawable buffer; /* the current buffer, either equal to */
156 /* frontbuffer, backpixmap or XIMAGE (None) */
157
158 XMesaColormap cmap; /* the X colormap */
159
160 GLint db_state; /* 0 = single buffered */
161 /* BACK_PIXMAP = use Pixmap for back buffer */
162 /* BACK_XIMAGE = use XImage for back buffer */
163
164 #ifndef XFree86Server
165 GLuint shm; /* X Shared Memory extension status: */
166 /* 0 = not available */
167 /* 1 = XImage support available */
168 /* 2 = Pixmap support available too */
169 #ifdef USE_XSHM
170 XShmSegmentInfo shminfo;
171 #endif
172 #endif
173
174 XMesaImage *rowimage; /* Used for optimized span writing */
175
176 GLuint width, height; /* size of buffer */
177
178 GLint bottom; /* used for FLIP macro below */
179 GLubyte *ximage_origin1; /* used for PIXELADDR1 macro */
180 GLint ximage_width1;
181 GLushort *ximage_origin2; /* used for PIXELADDR2 macro */
182 GLint ximage_width2;
183 GLubyte *ximage_origin3; /* used for PIXELADDR3 macro */
184 GLint ximage_width3;
185 GLuint *ximage_origin4; /* used for PIXELADDR4 macro */
186 GLint ximage_width4;
187
188 XMesaPixmap stipple_pixmap; /* For polygon stippling */
189 XMesaGC stipple_gc; /* For polygon stippling */
190
191 XMesaGC gc; /* scratch GC for span, line, tri drawing */
192 XMesaGC cleargc; /* GC for clearing the color buffer */
193 XMesaGC swapgc; /* GC for swapping the color buffers */
194
195 /* The following are here instead of in the XMesaVisual
196 * because they depend on the window's colormap.
197 */
198
199 /* For PF_DITHER, PF_LOOKUP, PF_GRAYSCALE */
200 unsigned long color_table[576]; /* RGB -> pixel value */
201
202 /* For PF_DITHER, PF_LOOKUP, PF_GRAYSCALE */
203 GLubyte pixel_to_r[65536]; /* pixel value -> red */
204 GLubyte pixel_to_g[65536]; /* pixel value -> green */
205 GLubyte pixel_to_b[65536]; /* pixel value -> blue */
206
207 /* Used to do XAllocColor/XFreeColors accounting: */
208 int num_alloced;
209 #if defined(XFree86Server)
210 Pixel alloced_colors[256];
211 #else
212 unsigned long alloced_colors[256];
213 #endif
214
215 #if defined( FX )
216 /* For 3Dfx Glide only */
217 GLboolean FXisHackUsable; /* Can we render into window? */
218 GLboolean FXwindowHack; /* Are we rendering into a window? */
219 fxMesaContext FXctx;
220 #endif
221
222 /* functions for clearing the front and back color buffers */
223 clear_func front_clear_func;
224 clear_func back_clear_func;
225
226 struct xmesa_buffer *Next; /* Linked list pointer: */
227 };
228
229
230
231 /* Values for xmesa->dest: */
232 #define FRONT_PIXMAP 1
233 #define BACK_PIXMAP 2
234 #define BACK_XIMAGE 4
235
236
237 /* Values for xmesa->pixelformat: */
238 #define PF_INDEX 1 /* Color Index mode */
239 #define PF_TRUECOLOR 2 /* TrueColor or DirectColor, any depth */
240 #define PF_TRUEDITHER 3 /* TrueColor with dithering */
241 #define PF_8A8B8G8R 4 /* 32-bit TrueColor: 8-A, 8-B, 8-G, 8-R */
242 #define PF_8R8G8B 5 /* 32-bit TrueColor: 8-R, 8-G, 8-B bits */
243 #define PF_5R6G5B 6 /* 16-bit TrueColor: 5-R, 6-G, 5-B bits */
244 #define PF_DITHER 7 /* Color-mapped RGB with dither */
245 #define PF_LOOKUP 8 /* Color-mapped RGB without dither */
246 #define PF_HPCR 9 /* HP Color Recovery (ad@lms.be 30/08/95) */
247 #define PF_1BIT 10 /* monochrome dithering of RGB */
248 #define PF_GRAYSCALE 11 /* Grayscale or StaticGray */
249 #define PF_8R8G8B24 12 /* 24-bit TrueColor: 8-R, 8-G, 8-B bits */
250 #define PF_DITHER_5R6G5B 13 /* 16-bit dithered TrueColor: 5-R, 6-G, 5-B */
251
252
253 /*
254 * If pixelformat==PF_TRUECOLOR:
255 */
256 #define PACK_TRUECOLOR( PIXEL, R, G, B ) \
257 PIXEL = xmesa->xm_visual->RtoPixel[R] \
258 | xmesa->xm_visual->GtoPixel[G] \
259 | xmesa->xm_visual->BtoPixel[B]; \
260
261
262 /*
263 * If pixelformat==PF_TRUEDITHER:
264 */
265 #define PACK_TRUEDITHER( PIXEL, X, Y, R, G, B ) \
266 { \
267 int d = xmesa->xm_visual->Kernel[((X)&3) | (((Y)&3)<<2)]; \
268 PIXEL = xmesa->xm_visual->RtoPixel[(R)+d] \
269 | xmesa->xm_visual->GtoPixel[(G)+d] \
270 | xmesa->xm_visual->BtoPixel[(B)+d]; \
271 }
272
273
274
275 /*
276 * If pixelformat==PF_8A8B8G8R:
277 */
278 #define PACK_8A8B8G8R( R, G, B, A ) \
279 ( ((A) << 24) | ((B) << 16) | ((G) << 8) | (R) )
280
281
282 /*
283 * Like PACK_8A8B8G8R() but don't use alpha. This is usually an acceptable
284 * shortcut.
285 */
286 #define PACK_8B8G8R( R, G, B ) ( ((B) << 16) | ((G) << 8) | (R) )
287
288
289
290 /*
291 * If pixelformat==PF_8R8G8B:
292 */
293 #define PACK_8R8G8B( R, G, B) ( ((R) << 16) | ((G) << 8) | (B) )
294
295
296 /*
297 * If pixelformat==PF_5R6G5B:
298 */
299 #define PACK_5R6G5B( R, G, B) ( (((R) & 0xf8) << 8) | (((G) & 0xfc) << 3) | ((B) >> 3) )
300
301
302
303
304 /*
305 * If pixelformat==PF_DITHER:
306 *
307 * Improved 8-bit RGB dithering code contributed by Bob Mercier
308 * (mercier@hollywood.cinenet.net). Thanks Bob!
309 */
310 #undef _R
311 #undef _G
312 #undef _B
313 #undef _D
314 #ifdef DITHER666
315 # define _R 6
316 # define _G 6
317 # define _B 6
318 # define _MIX(r,g,b) (((r)*_G+(g))*_B+(b))
319 #else
320 # define _R 5
321 # define _G 9
322 # define _B 5
323 # define _MIX(r,g,b) ( ((g)<<6) | ((b)<<3) | (r) )
324 #endif
325 #define _DX 4
326 #define _DY 4
327 #define _D (_DX*_DY)
328
329 /*#define _DITH(C,c,d) (((unsigned)((_D*(C-1)+1)*c+d))/(_D*256))*/
330 #define _DITH(C,c,d) (((unsigned)((_D*(C-1)+1)*c+d)) >> 12)
331
332 #define MAXC 256
333 static int kernel8[_DY*_DX] = {
334 0 * MAXC, 8 * MAXC, 2 * MAXC, 10 * MAXC,
335 12 * MAXC, 4 * MAXC, 14 * MAXC, 6 * MAXC,
336 3 * MAXC, 11 * MAXC, 1 * MAXC, 9 * MAXC,
337 15 * MAXC, 7 * MAXC, 13 * MAXC, 5 * MAXC,
338 };
339 /*static int __d;*/
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[_MIX(_DITH(_R, (R), __d), \
349 _DITH(_G, (G), __d), \
350 _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[_MIX(_DITH(_R, (R), __d), \
361 _DITH(_G, (G), __d), \
362 _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 = (_D*((_R)-1)+1) * (R); \
376 int msdg = (_D*((_G)-1)+1) * (G); \
377 int msdb = (_D*((_B)-1)+1) * (B); \
378 int i; \
379 for (i=0;i<16;i++) { \
380 int k = kernel8[i]; \
381 int j = _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 _DITH0(C,c) (((unsigned)((_D*(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[_MIX(_DITH0(_R, (R)), \
403 _DITH0(_G, (G)), \
404 _DITH0(_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 * External functions:
492 */
493
494 extern unsigned long
495 xmesa_color_to_pixel( XMesaContext xmesa,
496 GLubyte r, GLubyte g, GLubyte b, GLubyte a,
497 GLuint pixelFormat );
498
499 extern void xmesa_alloc_back_buffer( XMesaBuffer b );
500
501 extern void xmesa_init_pointers( GLcontext *ctx );
502 extern void xmesa_update_state( GLcontext *ctx, GLuint new_state );
503
504 extern void xmesa_update_span_funcs( GLcontext *ctx );
505
506 /* Plugged into the software rasterizer. Try to use internal
507 * swrast-style point, line and triangle functions.
508 */
509 extern void xmesa_choose_point( GLcontext *ctx );
510 extern void xmesa_choose_line( GLcontext *ctx );
511 extern void xmesa_choose_triangle( GLcontext *ctx );
512
513
514 extern void xmesa_register_swrast_functions( GLcontext *ctx );
515
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 );
523
524 /*
525 * These are the extra routines required for integration with XFree86.
526 * None of these routines should be user visible. -KEM
527 */
528 extern void XMesaSetVisualDisplay( XMesaDisplay *dpy, XMesaVisual v );
529 extern GLboolean XMesaForceCurrent(XMesaContext c);
530 extern GLboolean XMesaLoseCurrent(XMesaContext c);
531 extern void XMesaReset( void );
532
533 extern void xmesa_set_read_buffer( GLcontext *ctx,
534 GLframebuffer *buffer, GLenum mode );
535
536 #endif