5f4e3d4cf29abf322de0904a82c7febb4a6ee075
[mesa.git] / src / mesa / drivers / x11 / xmesaP.h
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.3
4 *
5 * Copyright (C) 1999-2005 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 struct xmesa_renderbuffer;
56
57
58 /* Function pointer for clearing color buffers */
59 typedef void (*ClearFunc)( GLcontext *ctx, struct xmesa_renderbuffer *xrb,
60 GLboolean all, GLint x, GLint y,
61 GLint width, GLint height );
62
63
64
65
66 /** Framebuffer pixel formats */
67 enum pixel_format {
68 PF_Index, /**< Color Index mode */
69 PF_Truecolor, /**< TrueColor or DirectColor, any depth */
70 PF_Dither_True, /**< TrueColor with dithering */
71 PF_8A8B8G8R, /**< 32-bit TrueColor: 8-A, 8-B, 8-G, 8-R */
72 PF_8R8G8B, /**< 32-bit TrueColor: 8-R, 8-G, 8-B bits */
73 PF_5R6G5B, /**< 16-bit TrueColor: 5-R, 6-G, 5-B bits */
74 PF_Dither, /**< Color-mapped RGB with dither */
75 PF_Lookup, /**< Color-mapped RGB without dither */
76 PF_HPCR, /**< HP Color Recovery (ad@lms.be 30/08/95) */
77 PF_1Bit, /**< monochrome dithering of RGB */
78 PF_Grayscale, /**< Grayscale or StaticGray */
79 PF_8R8G8B24, /**< 24-bit TrueColor: 8-R, 8-G, 8-B bits */
80 PF_Dither_5R6G5B, /**< 16-bit dithered TrueColor: 5-R, 6-G, 5-B */
81 PF_8A8R8G8B /**< 32-bit TrueColor: 8-A, 8-R, 8-G, 8-B */
82 };
83
84
85 /*
86 * "Derived" from GLvisual. Basically corresponds to an XVisualInfo.
87 */
88 struct xmesa_visual {
89 GLvisual mesa_visual; /* Device independent visual parameters */
90 XMesaDisplay *display; /* The X11 display */
91 #ifdef XFree86Server
92 GLint ColormapEntries;
93 GLint nplanes;
94 #else
95 XMesaVisualInfo visinfo; /* X's visual info (pointer to private copy) */
96 XVisualInfo *vishandle; /* Only used in fakeglx.c */
97 #endif
98 GLint BitsPerPixel; /* True bits per pixel for XImages */
99
100 GLboolean ximage_flag; /* Use XImage for back buffer (not pixmap)? */
101
102 enum pixel_format dithered_pf; /* Pixel format when dithering */
103 enum pixel_format undithered_pf;/* Pixel format when not dithering */
104
105 GLfloat RedGamma; /* Gamma values, 1.0 is default */
106 GLfloat GreenGamma;
107 GLfloat BlueGamma;
108
109 /* For PF_TRUECOLOR */
110 GLint rshift, gshift, bshift;/* Pixel color component shifts */
111 GLubyte Kernel[16]; /* Dither kernel */
112 unsigned long RtoPixel[512]; /* RGB to pixel conversion */
113 unsigned long GtoPixel[512];
114 unsigned long BtoPixel[512];
115 GLubyte PixelToR[256]; /* Pixel to RGB conversion */
116 GLubyte PixelToG[256];
117 GLubyte PixelToB[256];
118
119 /* For PF_HPCR */
120 short hpcr_rgbTbl[3][256];
121 GLboolean hpcr_clear_flag;
122 GLubyte hpcr_clear_ximage_pattern[2][16];
123 XMesaImage *hpcr_clear_ximage;
124 XMesaPixmap hpcr_clear_pixmap;
125
126 /* For PF_1BIT */
127 int bitFlip;
128 };
129
130
131 /*
132 * "Derived" from __GLcontextRec. Basically corresponds to a GLXContext.
133 */
134 struct xmesa_context {
135 GLcontext mesa; /* the core library context (containment) */
136 XMesaVisual xm_visual; /* Describes the buffers */
137 XMesaBuffer xm_buffer; /* current span/point/line/triangle buffer */
138
139 XMesaDisplay *display; /* == xm_visual->display */
140 GLboolean swapbytes; /* Host byte order != display byte order? */
141 GLboolean direct; /* Direct rendering context? */
142
143 enum pixel_format pixelformat;
144
145 GLubyte clearcolor[4]; /* current clearing color */
146 unsigned long clearpixel; /* current clearing pixel value */
147 };
148
149
150
151 typedef enum {
152 WINDOW, /* An X window */
153 GLXWINDOW, /* GLX window */
154 PIXMAP, /* GLX pixmap */
155 PBUFFER /* GLX Pbuffer */
156 } BufferType;
157
158
159 /* Values for db_mode: */
160 #define BACK_PIXMAP 1
161 #define BACK_XIMAGE 2
162
163
164 struct xmesa_renderbuffer
165 {
166 struct gl_renderbuffer Base; /* Base class */
167
168 XMesaDrawable pixmap; /* Either an X Window ID (front color buf) */
169 /* or X Pixmap ID (back color buf) */
170
171 XMesaImage *ximage; /* The back buffer, if not using a Pixmap */
172
173 GLubyte *origin1; /* used for PIXEL_ADDR1 macro */
174 GLint width1;
175 GLushort *origin2; /* used for PIXEL_ADDR2 macro */
176 GLint width2;
177 GLubyte *origin3; /* used for PIXEL_ADDR3 macro */
178 GLint width3;
179 GLuint *origin4; /* used for PIXEL_ADDR4 macro */
180 GLint width4;
181
182 GLint bottom; /* used for FLIP macro, equals height - 1 */
183
184 ClearFunc clearFunc;
185 };
186
187
188 /*
189 * "Derived" from GLframebuffer. Basically corresponds to a GLXDrawable.
190 */
191 struct xmesa_buffer {
192 GLframebuffer mesa_buffer; /* depth, stencil, accum, etc buffers */
193 /* This MUST BE FIRST! */
194 GLboolean wasCurrent; /* was ever the current buffer? */
195 XMesaVisual xm_visual; /* the X/Mesa visual */
196
197 XMesaDisplay *display;
198 BufferType type; /* window, pixmap, pbuffer or glxwindow */
199
200 struct xmesa_renderbuffer *frontxrb; /* front color renderbuffer */
201 struct xmesa_renderbuffer *backxrb; /* back color renderbuffer */
202
203 XMesaColormap cmap; /* the X colormap */
204
205 unsigned long selectedEvents;/* for pbuffers only */
206
207 GLint db_mode; /* 0 = single buffered */
208 /* BACK_PIXMAP = use Pixmap for back buffer */
209 /* BACK_XIMAGE = use XImage for back buffer */
210
211 GLuint shm; /* X Shared Memory extension status: */
212 /* 0 = not available */
213 /* 1 = XImage support available */
214 /* 2 = Pixmap support available too */
215 #if defined(USE_XSHM) && !defined(XFree86Server)
216 XShmSegmentInfo shminfo;
217 #endif
218
219 XMesaImage *rowimage; /* Used for optimized span writing */
220 XMesaPixmap stipple_pixmap; /* For polygon stippling */
221 XMesaGC stipple_gc; /* For polygon stippling */
222
223 XMesaGC gc; /* scratch GC for span, line, tri drawing */
224 XMesaGC cleargc; /* GC for clearing the color buffer */
225 XMesaGC swapgc; /* GC for swapping the color buffers */
226
227 /* The following are here instead of in the XMesaVisual
228 * because they depend on the window's colormap.
229 */
230
231 /* For PF_DITHER, PF_LOOKUP, PF_GRAYSCALE */
232 unsigned long color_table[576]; /* RGB -> pixel value */
233
234 /* For PF_DITHER, PF_LOOKUP, PF_GRAYSCALE */
235 GLubyte pixel_to_r[65536]; /* pixel value -> red */
236 GLubyte pixel_to_g[65536]; /* pixel value -> green */
237 GLubyte pixel_to_b[65536]; /* pixel value -> blue */
238
239 /* Used to do XAllocColor/XFreeColors accounting: */
240 int num_alloced;
241 #if defined(XFree86Server)
242 Pixel alloced_colors[256];
243 #else
244 unsigned long alloced_colors[256];
245 #endif
246
247 #if defined( FX )
248 /* For 3Dfx Glide only */
249 GLboolean FXisHackUsable; /* Can we render into window? */
250 GLboolean FXwindowHack; /* Are we rendering into a window? */
251 fxMesaContext FXctx;
252 #endif
253
254 struct xmesa_buffer *Next; /* Linked list pointer: */
255 };
256
257
258 /*
259 * If pixelformat==PF_TRUECOLOR:
260 */
261 #define PACK_TRUECOLOR( PIXEL, R, G, B ) \
262 PIXEL = xmesa->xm_visual->RtoPixel[R] \
263 | xmesa->xm_visual->GtoPixel[G] \
264 | xmesa->xm_visual->BtoPixel[B]; \
265
266
267 /*
268 * If pixelformat==PF_TRUEDITHER:
269 */
270 #define PACK_TRUEDITHER( PIXEL, X, Y, R, G, B ) \
271 { \
272 int d = xmesa->xm_visual->Kernel[((X)&3) | (((Y)&3)<<2)]; \
273 PIXEL = xmesa->xm_visual->RtoPixel[(R)+d] \
274 | xmesa->xm_visual->GtoPixel[(G)+d] \
275 | xmesa->xm_visual->BtoPixel[(B)+d]; \
276 }
277
278
279
280 /*
281 * If pixelformat==PF_8A8B8G8R:
282 */
283 #define PACK_8A8B8G8R( R, G, B, A ) \
284 ( ((A) << 24) | ((B) << 16) | ((G) << 8) | (R) )
285
286
287 /*
288 * Like PACK_8A8B8G8R() but don't use alpha. This is usually an acceptable
289 * shortcut.
290 */
291 #define PACK_8B8G8R( R, G, B ) ( ((B) << 16) | ((G) << 8) | (R) )
292
293
294
295 /*
296 * If pixelformat==PF_8R8G8B:
297 */
298 #define PACK_8R8G8B( R, G, B) ( ((R) << 16) | ((G) << 8) | (B) )
299
300
301 /*
302 * If pixelformat==PF_5R6G5B:
303 */
304 #define PACK_5R6G5B( R, G, B) ( (((R) & 0xf8) << 8) | (((G) & 0xfc) << 3) | ((B) >> 3) )
305
306
307 /*
308 * If pixelformat==PF_8A8R8G8B:
309 */
310 #define PACK_8A8R8G8B( R, G, B, A ) \
311 ( ((A) << 24) | ((R) << 16) | ((G) << 8) | (B) )
312
313
314
315 /*
316 * If pixelformat==PF_DITHER:
317 *
318 * Improved 8-bit RGB dithering code contributed by Bob Mercier
319 * (mercier@hollywood.cinenet.net). Thanks Bob!
320 */
321 #ifdef DITHER666
322 # define DITH_R 6
323 # define DITH_G 6
324 # define DITH_B 6
325 # define DITH_MIX(r,g,b) (((r) * DITH_G + (g)) * DITH_B + (b))
326 #else
327 # define DITH_R 5
328 # define DITH_G 9
329 # define DITH_B 5
330 # define DITH_MIX(r,g,b) (((g) << 6) | ((b) << 3) | (r))
331 #endif
332 #define DITH_DX 4
333 #define DITH_DY 4
334 #define DITH_N (DITH_DX * DITH_DY)
335
336 #define _dither(C, c, d) (((unsigned)((DITH_N * (C - 1) + 1) * c + d)) >> 12)
337
338 #define MAXC 256
339 extern const int xmesa_kernel8[DITH_DY * DITH_DX];
340
341 /* Dither for random X,Y */
342 #define DITHER_SETUP \
343 int __d; \
344 unsigned long *ctable = XMESA_BUFFER(ctx->DrawBuffer)->color_table;
345
346 #define DITHER( X, Y, R, G, B ) \
347 (__d = xmesa_kernel8[(((Y)&3)<<2) | ((X)&3)], \
348 ctable[DITH_MIX(_dither(DITH_R, (R), __d), \
349 _dither(DITH_G, (G), __d), \
350 _dither(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_BUFFER(ctx->DrawBuffer)->color_table; \
356 const int *kernel = &xmesa_kernel8[ ((Y)&3) << 2 ];
357
358 #define XDITHER( X, R, G, B ) \
359 (__d = kernel[(X)&3], \
360 ctable[DITH_MIX(_dither(DITH_R, (R), __d), \
361 _dither(DITH_G, (G), __d), \
362 _dither(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 #define FLAT_DITHER_SETUP( R, G, B ) \
371 GLushort ditherValues[16]; \
372 { \
373 unsigned long *ctable = XMESA_BUFFER(ctx->DrawBuffer)->color_table; \
374 int msdr = (DITH_N*((DITH_R)-1)+1) * (R); \
375 int msdg = (DITH_N*((DITH_G)-1)+1) * (G); \
376 int msdb = (DITH_N*((DITH_B)-1)+1) * (B); \
377 int i; \
378 for (i=0;i<16;i++) { \
379 int k = xmesa_kernel8[i]; \
380 int j = DITH_MIX( (msdr+k)>>12, (msdg+k)>>12, (msdb+k)>>12 );\
381 ditherValues[i] = (GLushort) ctable[j]; \
382 } \
383 }
384
385 #define FLAT_DITHER_ROW_SETUP(Y) \
386 GLushort *ditherRow = ditherValues + ( ((Y)&3) << 2);
387
388 #define FLAT_DITHER(X) ditherRow[(X)&3]
389
390
391
392 /*
393 * If pixelformat==PF_LOOKUP:
394 */
395 #define _dither_lookup(C, c) (((unsigned)((DITH_N * (C - 1) + 1) * c)) >> 12)
396
397 #define LOOKUP_SETUP \
398 unsigned long *ctable = XMESA_BUFFER(ctx->DrawBuffer)->color_table
399
400 #define LOOKUP( R, G, B ) \
401 ctable[DITH_MIX(_dither_lookup(DITH_R, (R)), \
402 _dither_lookup(DITH_G, (G)), \
403 _dither_lookup(DITH_B, (B)))]
404
405
406
407 /*
408 * If pixelformat==PF_HPCR:
409 *
410 * HP Color Recovery dithering (ad@lms.be 30/08/95)
411 * HP has on it's 8-bit 700-series computers, a feature called
412 * 'Color Recovery'. This allows near 24-bit output (so they say).
413 * It is enabled by selecting the 8-bit TrueColor visual AND
414 * corresponding colormap (see tkInitWindow) AND doing some special
415 * dither.
416 */
417 extern const short xmesa_HPCR_DRGB[3][2][16];
418
419 #define DITHER_HPCR( X, Y, R, G, B ) \
420 ( ((xmesa->xm_visual->hpcr_rgbTbl[0][R] + xmesa_HPCR_DRGB[0][(Y)&1][(X)&15]) & 0xE0) \
421 |(((xmesa->xm_visual->hpcr_rgbTbl[1][G] + xmesa_HPCR_DRGB[1][(Y)&1][(X)&15]) & 0xE0)>>3) \
422 | ((xmesa->xm_visual->hpcr_rgbTbl[2][B] + xmesa_HPCR_DRGB[2][(Y)&1][(X)&15])>>6) \
423 )
424
425
426
427 /*
428 * If pixelformat==PF_1BIT:
429 */
430 extern const int xmesa_kernel1[16];
431
432 #define SETUP_1BIT int bitFlip = xmesa->xm_visual->bitFlip
433 #define DITHER_1BIT( X, Y, R, G, B ) \
434 (( ((int)(R)+(int)(G)+(int)(B)) > xmesa_kernel1[(((Y)&3) << 2) | ((X)&3)] ) ^ bitFlip)
435
436
437
438 /*
439 * If pixelformat==PF_GRAYSCALE:
440 */
441 #define GRAY_RGB( R, G, B ) XMESA_BUFFER(ctx->DrawBuffer)->color_table[((R) + (G) + (B))/3]
442
443
444
445 /*
446 * Converts a GL window Y coord to an X window Y coord:
447 */
448 #define YFLIP(XRB, Y) ((XRB)->bottom - (Y))
449
450
451 /*
452 * Return the address of a 1, 2 or 4-byte pixel in the buffer's XImage:
453 * X==0 is left, Y==0 is bottom.
454 */
455 #define PIXEL_ADDR1(XRB, X, Y) \
456 ( (XRB)->origin1 - (Y) * (XRB)->width1 + (X) )
457
458 #define PIXEL_ADDR2(XRB, X, Y) \
459 ( (XRB)->origin2 - (Y) * (XRB)->width2 + (X) )
460
461 #define PIXEL_ADDR3(XRB, X, Y) \
462 ( (bgr_t *) ( (XRB)->origin3 - (Y) * (XRB)->width3 + 3 * (X) ))
463
464 #define PIXEL_ADDR4(XRB, X, Y) \
465 ( (XRB)->origin4 - (Y) * (XRB)->width4 + (X) )
466
467
468
469
470 /*
471 * Return pointer to XMesaContext corresponding to a Mesa GLcontext.
472 * Since we're using structure containment, it's just a cast!.
473 */
474 #define XMESA_CONTEXT(MESACTX) ((XMesaContext) (MESACTX))
475
476 /*
477 * Return pointer to XMesaBuffer corresponding to a Mesa GLframebuffer.
478 * Since we're using structure containment, it's just a cast!.
479 */
480 #define XMESA_BUFFER(MESABUFF) ((XMesaBuffer) (MESABUFF))
481
482
483
484 /*
485 * External functions:
486 */
487
488 extern struct xmesa_renderbuffer *
489 xmesa_new_renderbuffer(GLcontext *ctx, GLuint name, GLboolean rgbMode,
490 GLboolean backBuffer);
491
492 extern unsigned long
493 xmesa_color_to_pixel( GLcontext *ctx,
494 GLubyte r, GLubyte g, GLubyte b, GLubyte a,
495 GLuint pixelFormat );
496
497 extern void
498 xmesa_alloc_back_buffer(XMesaBuffer b, GLuint width, GLuint height);
499
500 extern void xmesa_resize_buffers(GLcontext *ctx, GLframebuffer *buffer,
501 GLuint width, GLuint height);
502
503 extern void xmesa_init_driver_functions( XMesaVisual xmvisual,
504 struct dd_function_table *driver );
505
506 extern void xmesa_update_state( GLcontext *ctx, GLuint new_state );
507
508 extern void
509 xmesa_set_renderbuffer_funcs(struct xmesa_renderbuffer *xrb,
510 enum pixel_format pixelformat, GLint depth);
511
512
513 /* Plugged into the software rasterizer. Try to use internal
514 * swrast-style point, line and triangle functions.
515 */
516 extern void xmesa_choose_point( GLcontext *ctx );
517 extern void xmesa_choose_line( GLcontext *ctx );
518 extern void xmesa_choose_triangle( GLcontext *ctx );
519
520
521 extern void xmesa_register_swrast_functions( GLcontext *ctx );
522
523
524
525 /* XXX this is a hack to implement shared display lists with 3Dfx */
526 extern XMesaBuffer XMesaCreateWindowBuffer2( XMesaVisual v,
527 XMesaWindow w,
528 XMesaContext c );
529
530 /*
531 * These are the extra routines required for integration with XFree86.
532 * None of these routines should be user visible. -KEM
533 */
534 extern void XMesaSetVisualDisplay( XMesaDisplay *dpy, XMesaVisual v );
535 extern GLboolean XMesaForceCurrent(XMesaContext c);
536 extern GLboolean XMesaLoseCurrent(XMesaContext c);
537 extern void XMesaReset( void );
538
539
540 #define ENABLE_EXT_texure_compression_s3tc 0 /* SW texture compression */
541
542 #ifdef XFree86Server
543 #define ENABLE_EXT_timer_query 0
544 #else
545 #define ENABLE_EXT_timer_query 1
546 #endif
547
548 #endif