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