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