reverse some debug that slipped through
[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 GLint bottom; /* used for FLIP macro below */
196 GLubyte *ximage_origin1; /* used for PIXELADDR1 macro */
197 GLint ximage_width1;
198 GLushort *ximage_origin2; /* used for PIXELADDR2 macro */
199 GLint ximage_width2;
200 GLubyte *ximage_origin3; /* used for PIXELADDR3 macro */
201 GLint ximage_width3;
202 GLuint *ximage_origin4; /* used for PIXELADDR4 macro */
203 GLint ximage_width4;
204
205 XMesaPixmap stipple_pixmap; /* For polygon stippling */
206 XMesaGC stipple_gc; /* For polygon stippling */
207
208 XMesaGC gc; /* scratch GC for span, line, tri drawing */
209 XMesaGC cleargc; /* GC for clearing the color buffer */
210 XMesaGC swapgc; /* GC for swapping the color buffers */
211
212 /* The following are here instead of in the XMesaVisual
213 * because they depend on the window's colormap.
214 */
215
216 /* For PF_DITHER, PF_LOOKUP, PF_GRAYSCALE */
217 unsigned long color_table[576]; /* RGB -> pixel value */
218
219 /* For PF_DITHER, PF_LOOKUP, PF_GRAYSCALE */
220 GLubyte pixel_to_r[65536]; /* pixel value -> red */
221 GLubyte pixel_to_g[65536]; /* pixel value -> green */
222 GLubyte pixel_to_b[65536]; /* pixel value -> blue */
223
224 /* Used to do XAllocColor/XFreeColors accounting: */
225 int num_alloced;
226 #if defined(XFree86Server)
227 Pixel alloced_colors[256];
228 #else
229 unsigned long alloced_colors[256];
230 #endif
231
232 #if defined( FX )
233 /* For 3Dfx Glide only */
234 GLboolean FXisHackUsable; /* Can we render into window? */
235 GLboolean FXwindowHack; /* Are we rendering into a window? */
236 fxMesaContext FXctx;
237 #endif
238
239 /* functions for clearing the front and back color buffers */
240 clear_func front_clear_func;
241 clear_func back_clear_func;
242
243 struct xmesa_buffer *Next; /* Linked list pointer: */
244 };
245
246
247
248 /* Values for xmesa->dest: */
249 #define FRONT_PIXMAP 1
250 #define BACK_PIXMAP 2
251 #define BACK_XIMAGE 4
252
253 /* Special value for X Drawable variables to indicate use of XImage instead */
254 #define XIMAGE None
255
256
257 /*
258 * If pixelformat==PF_TRUECOLOR:
259 */
260 #define PACK_TRUECOLOR( PIXEL, R, G, B ) \
261 PIXEL = xmesa->xm_visual->RtoPixel[R] \
262 | xmesa->xm_visual->GtoPixel[G] \
263 | xmesa->xm_visual->BtoPixel[B]; \
264
265
266 /*
267 * If pixelformat==PF_TRUEDITHER:
268 */
269 #define PACK_TRUEDITHER( PIXEL, X, Y, R, G, B ) \
270 { \
271 int d = xmesa->xm_visual->Kernel[((X)&3) | (((Y)&3)<<2)]; \
272 PIXEL = xmesa->xm_visual->RtoPixel[(R)+d] \
273 | xmesa->xm_visual->GtoPixel[(G)+d] \
274 | xmesa->xm_visual->BtoPixel[(B)+d]; \
275 }
276
277
278
279 /*
280 * If pixelformat==PF_8A8B8G8R:
281 */
282 #define PACK_8A8B8G8R( R, G, B, A ) \
283 ( ((A) << 24) | ((B) << 16) | ((G) << 8) | (R) )
284
285
286 /*
287 * Like PACK_8A8B8G8R() but don't use alpha. This is usually an acceptable
288 * shortcut.
289 */
290 #define PACK_8B8G8R( R, G, B ) ( ((B) << 16) | ((G) << 8) | (R) )
291
292
293
294 /*
295 * If pixelformat==PF_8R8G8B:
296 */
297 #define PACK_8R8G8B( R, G, B) ( ((R) << 16) | ((G) << 8) | (B) )
298
299
300 /*
301 * If pixelformat==PF_5R6G5B:
302 */
303 #define PACK_5R6G5B( R, G, B) ( (((R) & 0xf8) << 8) | (((G) & 0xfc) << 3) | ((B) >> 3) )
304
305
306
307
308 /*
309 * If pixelformat==PF_DITHER:
310 *
311 * Improved 8-bit RGB dithering code contributed by Bob Mercier
312 * (mercier@hollywood.cinenet.net). Thanks Bob!
313 */
314 #ifdef DITHER666
315 # define DITH_R 6
316 # define DITH_G 6
317 # define DITH_B 6
318 # define DITH_MIX(r,g,b) (((r) * DITH_G + (g)) * DITH_B + (b))
319 #else
320 # define DITH_R 5
321 # define DITH_G 9
322 # define DITH_B 5
323 # define DITH_MIX(r,g,b) (((g) << 6) | ((b) << 3) | (r))
324 #endif
325 #define DITH_DX 4
326 #define DITH_DY 4
327 #define DITH_N (DITH_DX * DITH_DY)
328
329 #define _dither(C, c, d) (((unsigned)((DITH_N * (C - 1) + 1) * c + d)) >> 12)
330
331 #define MAXC 256
332 extern const int xmesa_kernel8[DITH_DY * DITH_DX];
333
334 /* Dither for random X,Y */
335 #define DITHER_SETUP \
336 int __d; \
337 unsigned long *ctable = xmesa->xm_buffer->color_table;
338
339 #define DITHER( X, Y, R, G, B ) \
340 (__d = xmesa_kernel8[(((Y)&3)<<2) | ((X)&3)], \
341 ctable[DITH_MIX(_dither(DITH_R, (R), __d), \
342 _dither(DITH_G, (G), __d), \
343 _dither(DITH_B, (B), __d))])
344
345 /* Dither for random X, fixed Y */
346 #define XDITHER_SETUP(Y) \
347 int __d; \
348 unsigned long *ctable = xmesa->xm_buffer->color_table; \
349 const int *kernel = &xmesa_kernel8[ ((Y)&3) << 2 ];
350
351 #define XDITHER( X, R, G, B ) \
352 (__d = kernel[(X)&3], \
353 ctable[DITH_MIX(_dither(DITH_R, (R), __d), \
354 _dither(DITH_G, (G), __d), \
355 _dither(DITH_B, (B), __d))])
356
357
358
359 /*
360 * Dithering for flat-shaded triangles. Precompute all 16 possible
361 * pixel values given the triangle's RGB color. Contributed by Martin Shenk.
362 */
363 #define FLAT_DITHER_SETUP( R, G, B ) \
364 GLushort ditherValues[16]; \
365 { \
366 unsigned long *ctable = xmesa->xm_buffer->color_table; \
367 int msdr = (DITH_N*((DITH_R)-1)+1) * (R); \
368 int msdg = (DITH_N*((DITH_G)-1)+1) * (G); \
369 int msdb = (DITH_N*((DITH_B)-1)+1) * (B); \
370 int i; \
371 for (i=0;i<16;i++) { \
372 int k = xmesa_kernel8[i]; \
373 int j = DITH_MIX( (msdr+k)>>12, (msdg+k)>>12, (msdb+k)>>12 );\
374 ditherValues[i] = (GLushort) ctable[j]; \
375 } \
376 }
377
378 #define FLAT_DITHER_ROW_SETUP(Y) \
379 GLushort *ditherRow = ditherValues + ( ((Y)&3) << 2);
380
381 #define FLAT_DITHER(X) ditherRow[(X)&3]
382
383
384
385 /*
386 * If pixelformat==PF_LOOKUP:
387 */
388 #define _dither_lookup(C, c) (((unsigned)((DITH_N * (C - 1) + 1) * c)) >> 12)
389
390 #define LOOKUP_SETUP \
391 unsigned long *ctable = xmesa->xm_buffer->color_table
392
393 #define LOOKUP( R, G, B ) \
394 ctable[DITH_MIX(_dither_lookup(DITH_R, (R)), \
395 _dither_lookup(DITH_G, (G)), \
396 _dither_lookup(DITH_B, (B)))]
397
398
399
400 /*
401 * If pixelformat==PF_HPCR:
402 *
403 * HP Color Recovery dithering (ad@lms.be 30/08/95)
404 * HP has on it's 8-bit 700-series computers, a feature called
405 * 'Color Recovery'. This allows near 24-bit output (so they say).
406 * It is enabled by selecting the 8-bit TrueColor visual AND
407 * corresponding colormap (see tkInitWindow) AND doing some special
408 * dither.
409 */
410 extern const short xmesa_HPCR_DRGB[3][2][16];
411
412 #define DITHER_HPCR( X, Y, R, G, B ) \
413 ( ((xmesa->xm_visual->hpcr_rgbTbl[0][R] + xmesa_HPCR_DRGB[0][(Y)&1][(X)&15]) & 0xE0) \
414 |(((xmesa->xm_visual->hpcr_rgbTbl[1][G] + xmesa_HPCR_DRGB[1][(Y)&1][(X)&15]) & 0xE0)>>3) \
415 | ((xmesa->xm_visual->hpcr_rgbTbl[2][B] + xmesa_HPCR_DRGB[2][(Y)&1][(X)&15])>>6) \
416 )
417
418
419
420 /*
421 * If pixelformat==PF_1BIT:
422 */
423 extern const int xmesa_kernel1[16];
424
425 #define SETUP_1BIT int bitFlip = xmesa->xm_visual->bitFlip
426 #define DITHER_1BIT( X, Y, R, G, B ) \
427 (( ((int)(R)+(int)(G)+(int)(B)) > xmesa_kernel1[(((Y)&3) << 2) | ((X)&3)] ) ^ bitFlip)
428
429
430
431 /*
432 * If pixelformat==PF_GRAYSCALE:
433 */
434 #define GRAY_RGB( R, G, B ) xmesa->xm_buffer->color_table[((R) + (G) + (B))/3]
435
436
437
438 /*
439 * Converts a GL window Y coord to an X window Y coord:
440 */
441 #define FLIP(BUFFER, Y) ((BUFFER)->bottom-(Y))
442
443
444 /*
445 * Return the address of a 1, 2 or 4-byte pixel in the back XImage:
446 * X==0 is left, Y==0 is bottom.
447 */
448 #define PIXELADDR1( BUFFER, X, Y ) \
449 ( (BUFFER)->ximage_origin1 - (Y) * (BUFFER)->ximage_width1 + (X) )
450
451 #define PIXELADDR2( BUFFER, X, Y ) \
452 ( (BUFFER)->ximage_origin2 - (Y) * (BUFFER)->ximage_width2 + (X) )
453
454 #define PIXELADDR3( BUFFER, X, Y ) \
455 ( (bgr_t *) ( (BUFFER)->ximage_origin3 - (Y) * (BUFFER)->ximage_width3 + 3 * (X) ))
456
457 #define PIXELADDR4( BUFFER, X, Y ) \
458 ( (BUFFER)->ximage_origin4 - (Y) * (BUFFER)->ximage_width4 + (X) )
459
460
461
462
463 /*
464 * Return pointer to XMesaContext corresponding to a Mesa GLcontext.
465 * Since we're using structure containment, it's just a cast!.
466 */
467 #define XMESA_CONTEXT(MESACTX) (XMesaContext) (MESACTX)
468
469
470
471 /*
472 * External functions:
473 */
474
475 extern unsigned long
476 xmesa_color_to_pixel( XMesaContext xmesa,
477 GLubyte r, GLubyte g, GLubyte b, GLubyte a,
478 GLuint pixelFormat );
479
480 extern void xmesa_alloc_back_buffer( XMesaBuffer b );
481
482 extern void xmesa_resize_buffers( GLframebuffer *buffer );
483
484 extern void xmesa_init_driver_functions( XMesaVisual xmvisual,
485 struct dd_function_table *driver );
486
487 extern void xmesa_update_state( GLcontext *ctx, GLuint new_state );
488
489 extern void xmesa_update_span_funcs( GLcontext *ctx );
490
491 extern void xmesa_set_buffer( GLcontext *ctx, GLframebuffer *buffer,
492 GLuint bufferBit );
493
494
495 /* Plugged into the software rasterizer. Try to use internal
496 * swrast-style point, line and triangle functions.
497 */
498 extern void xmesa_choose_point( GLcontext *ctx );
499 extern void xmesa_choose_line( GLcontext *ctx );
500 extern void xmesa_choose_triangle( GLcontext *ctx );
501
502
503 extern void xmesa_register_swrast_functions( GLcontext *ctx );
504
505
506
507 /* XXX this is a hack to implement shared display lists with 3Dfx */
508 extern XMesaBuffer XMesaCreateWindowBuffer2( XMesaVisual v,
509 XMesaWindow w,
510 XMesaContext c );
511
512 /*
513 * These are the extra routines required for integration with XFree86.
514 * None of these routines should be user visible. -KEM
515 */
516 extern void XMesaSetVisualDisplay( XMesaDisplay *dpy, XMesaVisual v );
517 extern GLboolean XMesaForceCurrent(XMesaContext c);
518 extern GLboolean XMesaLoseCurrent(XMesaContext c);
519 extern void XMesaReset( void );
520
521
522 #define SWTC 0 /* SW texture compression */
523
524
525 #endif