2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
31 #include "main/mtypes.h"
41 extern _glthread_Mutex _xmesa_lock
;
43 extern XMesaBuffer XMesaBufferList
;
45 /* for PF_8R8G8B24 pixel format */
53 struct xmesa_renderbuffer
;
56 /* Function pointer for clearing color buffers */
57 typedef void (*ClearFunc
)( GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
58 GLint x
, GLint y
, GLint width
, GLint height
);
63 /** Framebuffer pixel formats */
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 */
83 * Visual inforation, derived from GLvisual.
84 * Basically corresponds to an XVisualInfo.
87 GLvisual mesa_visual
; /* Device independent visual parameters */
88 XMesaDisplay
*display
; /* The X11 display */
90 GLint ColormapEntries
;
93 XMesaVisualInfo visinfo
; /* X's visual info (pointer to private copy) */
94 XVisualInfo
*vishandle
; /* Only used in fakeglx.c */
96 GLint BitsPerPixel
; /* True bits per pixel for XImages */
98 GLboolean ximage_flag
; /* Use XImage for back buffer (not pixmap)? */
100 enum pixel_format dithered_pf
; /* Pixel format when dithering */
101 enum pixel_format undithered_pf
;/* Pixel format when not dithering */
103 GLfloat RedGamma
; /* Gamma values, 1.0 is default */
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];
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
;
130 * Context info, derived from GLcontext.
131 * Basically corresponds to a GLXContext.
133 struct 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 */
138 XMesaDisplay
*display
; /* == xm_visual->display */
139 GLboolean swapbytes
; /* Host byte order != display byte order? */
140 GLboolean direct
; /* Direct rendering context? */
142 enum pixel_format pixelformat
;
144 GLubyte clearcolor
[4]; /* current clearing color */
145 unsigned long clearpixel
; /* current clearing pixel value */
150 * Types of X/GLX drawables we might render into.
153 WINDOW
, /* An X window */
154 GLXWINDOW
, /* GLX window */
155 PIXMAP
, /* GLX pixmap */
156 PBUFFER
/* GLX Pbuffer */
160 /** Values for db_mode: */
162 #define BACK_PIXMAP 1
163 #define BACK_XIMAGE 2
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.
178 struct xmesa_renderbuffer
180 struct gl_renderbuffer Base
; /* Base class */
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 */
187 GLubyte
*origin1
; /* used for PIXEL_ADDR1 macro */
189 GLushort
*origin2
; /* used for PIXEL_ADDR2 macro */
191 GLubyte
*origin3
; /* used for PIXEL_ADDR3 macro */
193 GLuint
*origin4
; /* used for PIXEL_ADDR4 macro */
196 GLint bottom
; /* used for FLIP macro, equals height - 1 */
203 * Framebuffer information, derived from.
204 * Basically corresponds to a GLXDrawable.
206 struct 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 */
212 XMesaDisplay
*display
;
213 BufferType type
; /* window, pixmap, pbuffer or glxwindow */
215 GLboolean largestPbuffer
; /**< for pbuffers */
216 GLboolean preservedContents
; /**< for pbuffers */
218 struct xmesa_renderbuffer
*frontxrb
; /* front color renderbuffer */
219 struct xmesa_renderbuffer
*backxrb
; /* back color renderbuffer */
221 XMesaColormap cmap
; /* the X colormap */
223 unsigned long selectedEvents
;/* for pbuffers only */
225 GLint db_mode
; /* 0 = single buffered */
226 /* BACK_PIXMAP = use Pixmap for back buffer */
227 /* BACK_XIMAGE = use XImage for back buffer */
230 GLuint shm
; /* X Shared Memory extension status: */
231 /* 0 = not available */
232 /* 1 = XImage support available */
233 /* 2 = Pixmap support available too */
234 #if defined(USE_XSHM) && !defined(XFree86Server)
235 XShmSegmentInfo shminfo
;
238 XMesaImage
*rowimage
; /* Used for optimized span writing */
239 XMesaPixmap stipple_pixmap
; /* For polygon stippling */
240 XMesaGC stipple_gc
; /* For polygon stippling */
242 XMesaGC gc
; /* scratch GC for span, line, tri drawing */
243 XMesaGC cleargc
; /* GC for clearing the color buffer */
244 XMesaGC swapgc
; /* GC for swapping the color buffers */
246 /* The following are here instead of in the XMesaVisual
247 * because they depend on the window's colormap.
250 /* For PF_DITHER, PF_LOOKUP, PF_GRAYSCALE */
251 unsigned long color_table
[576]; /* RGB -> pixel value */
253 /* For PF_DITHER, PF_LOOKUP, PF_GRAYSCALE */
254 GLubyte pixel_to_r
[65536]; /* pixel value -> red */
255 GLubyte pixel_to_g
[65536]; /* pixel value -> green */
256 GLubyte pixel_to_b
[65536]; /* pixel value -> blue */
258 /* Used to do XAllocColor/XFreeColors accounting: */
260 #if defined(XFree86Server)
261 Pixel alloced_colors
[256];
263 unsigned long alloced_colors
[256];
267 /* For 3Dfx Glide only */
268 GLboolean FXisHackUsable
; /* Can we render into window? */
269 GLboolean FXwindowHack
; /* Are we rendering into a window? */
273 /* GLX_EXT_texture_from_pixmap */
274 GLint TextureTarget
; /** GLX_TEXTURE_1D_EXT, for example */
275 GLint TextureFormat
; /** GLX_TEXTURE_FORMAT_RGB_EXT, for example */
276 GLint TextureMipmap
; /** 0 or 1 */
278 struct xmesa_buffer
*Next
; /* Linked list pointer: */
283 * If pixelformat==PF_TRUECOLOR:
285 #define PACK_TRUECOLOR( PIXEL, R, G, B ) \
286 PIXEL = xmesa->xm_visual->RtoPixel[R] \
287 | xmesa->xm_visual->GtoPixel[G] \
288 | xmesa->xm_visual->BtoPixel[B]; \
292 * If pixelformat==PF_TRUEDITHER:
294 #define PACK_TRUEDITHER( PIXEL, X, Y, R, G, B ) \
296 int d = xmesa->xm_visual->Kernel[((X)&3) | (((Y)&3)<<2)]; \
297 PIXEL = xmesa->xm_visual->RtoPixel[(R)+d] \
298 | xmesa->xm_visual->GtoPixel[(G)+d] \
299 | xmesa->xm_visual->BtoPixel[(B)+d]; \
305 * If pixelformat==PF_8A8B8G8R:
307 #define PACK_8A8B8G8R( R, G, B, A ) \
308 ( ((A) << 24) | ((B) << 16) | ((G) << 8) | (R) )
312 * Like PACK_8A8B8G8R() but don't use alpha. This is usually an acceptable
315 #define PACK_8B8G8R( R, G, B ) ( ((B) << 16) | ((G) << 8) | (R) )
320 * If pixelformat==PF_8R8G8B:
322 #define PACK_8R8G8B( R, G, B) ( ((R) << 16) | ((G) << 8) | (B) )
326 * If pixelformat==PF_5R6G5B:
328 #define PACK_5R6G5B( R, G, B) ( (((R) & 0xf8) << 8) | (((G) & 0xfc) << 3) | ((B) >> 3) )
332 * If pixelformat==PF_8A8R8G8B:
334 #define PACK_8A8R8G8B( R, G, B, A ) \
335 ( ((A) << 24) | ((R) << 16) | ((G) << 8) | (B) )
340 * If pixelformat==PF_DITHER:
342 * Improved 8-bit RGB dithering code contributed by Bob Mercier
343 * (mercier@hollywood.cinenet.net). Thanks Bob!
349 # define DITH_MIX(r,g,b) (((r) * DITH_G + (g)) * DITH_B + (b))
354 # define DITH_MIX(r,g,b) (((g) << 6) | ((b) << 3) | (r))
358 #define DITH_N (DITH_DX * DITH_DY)
360 #define _dither(C, c, d) (((unsigned)((DITH_N * (C - 1) + 1) * c + d)) >> 12)
363 extern const int xmesa_kernel8
[DITH_DY
* DITH_DX
];
365 /* Dither for random X,Y */
366 #define DITHER_SETUP \
368 unsigned long *ctable = XMESA_BUFFER(ctx->DrawBuffer)->color_table;
370 #define DITHER( X, Y, R, G, B ) \
371 (__d = xmesa_kernel8[(((Y)&3)<<2) | ((X)&3)], \
372 ctable[DITH_MIX(_dither(DITH_R, (R), __d), \
373 _dither(DITH_G, (G), __d), \
374 _dither(DITH_B, (B), __d))])
376 /* Dither for random X, fixed Y */
377 #define XDITHER_SETUP(Y) \
379 unsigned long *ctable = XMESA_BUFFER(ctx->DrawBuffer)->color_table; \
380 const int *kernel = &xmesa_kernel8[ ((Y)&3) << 2 ];
382 #define XDITHER( X, R, G, B ) \
383 (__d = kernel[(X)&3], \
384 ctable[DITH_MIX(_dither(DITH_R, (R), __d), \
385 _dither(DITH_G, (G), __d), \
386 _dither(DITH_B, (B), __d))])
391 * Dithering for flat-shaded triangles. Precompute all 16 possible
392 * pixel values given the triangle's RGB color. Contributed by Martin Shenk.
394 #define FLAT_DITHER_SETUP( R, G, B ) \
395 GLushort ditherValues[16]; \
397 unsigned long *ctable = XMESA_BUFFER(ctx->DrawBuffer)->color_table; \
398 int msdr = (DITH_N*((DITH_R)-1)+1) * (R); \
399 int msdg = (DITH_N*((DITH_G)-1)+1) * (G); \
400 int msdb = (DITH_N*((DITH_B)-1)+1) * (B); \
402 for (i=0;i<16;i++) { \
403 int k = xmesa_kernel8[i]; \
404 int j = DITH_MIX( (msdr+k)>>12, (msdg+k)>>12, (msdb+k)>>12 );\
405 ditherValues[i] = (GLushort) ctable[j]; \
409 #define FLAT_DITHER_ROW_SETUP(Y) \
410 GLushort *ditherRow = ditherValues + ( ((Y)&3) << 2);
412 #define FLAT_DITHER(X) ditherRow[(X)&3]
417 * If pixelformat==PF_LOOKUP:
419 #define _dither_lookup(C, c) (((unsigned)((DITH_N * (C - 1) + 1) * c)) >> 12)
421 #define LOOKUP_SETUP \
422 unsigned long *ctable = XMESA_BUFFER(ctx->DrawBuffer)->color_table
424 #define LOOKUP( R, G, B ) \
425 ctable[DITH_MIX(_dither_lookup(DITH_R, (R)), \
426 _dither_lookup(DITH_G, (G)), \
427 _dither_lookup(DITH_B, (B)))]
431 * If pixelformat==PF_HPCR:
433 * HP Color Recovery dithering (ad@lms.be 30/08/95)
434 * HP has on it's 8-bit 700-series computers, a feature called
435 * 'Color Recovery'. This allows near 24-bit output (so they say).
436 * It is enabled by selecting the 8-bit TrueColor visual AND
437 * corresponding colormap (see tkInitWindow) AND doing some special
440 extern const short xmesa_HPCR_DRGB
[3][2][16];
442 #define DITHER_HPCR( X, Y, R, G, B ) \
443 ( ((xmesa->xm_visual->hpcr_rgbTbl[0][R] + xmesa_HPCR_DRGB[0][(Y)&1][(X)&15]) & 0xE0) \
444 |(((xmesa->xm_visual->hpcr_rgbTbl[1][G] + xmesa_HPCR_DRGB[1][(Y)&1][(X)&15]) & 0xE0)>>3) \
445 | ((xmesa->xm_visual->hpcr_rgbTbl[2][B] + xmesa_HPCR_DRGB[2][(Y)&1][(X)&15])>>6) \
451 * If pixelformat==PF_1BIT:
453 extern const int xmesa_kernel1
[16];
455 #define SETUP_1BIT int bitFlip = xmesa->xm_visual->bitFlip
456 #define DITHER_1BIT( X, Y, R, G, B ) \
457 (( ((int)(R)+(int)(G)+(int)(B)) > xmesa_kernel1[(((Y)&3) << 2) | ((X)&3)] ) ^ bitFlip)
462 * If pixelformat==PF_GRAYSCALE:
464 #define GRAY_RGB( R, G, B ) XMESA_BUFFER(ctx->DrawBuffer)->color_table[((R) + (G) + (B))/3]
469 * Converts a GL window Y coord to an X window Y coord:
471 #define YFLIP(XRB, Y) ((XRB)->bottom - (Y))
475 * Return the address of a 1, 2 or 4-byte pixel in the buffer's XImage:
476 * X==0 is left, Y==0 is bottom.
478 #define PIXEL_ADDR1(XRB, X, Y) \
479 ( (XRB)->origin1 - (Y) * (XRB)->width1 + (X) )
481 #define PIXEL_ADDR2(XRB, X, Y) \
482 ( (XRB)->origin2 - (Y) * (XRB)->width2 + (X) )
484 #define PIXEL_ADDR3(XRB, X, Y) \
485 ( (bgr_t *) ( (XRB)->origin3 - (Y) * (XRB)->width3 + 3 * (X) ))
487 #define PIXEL_ADDR4(XRB, X, Y) \
488 ( (XRB)->origin4 - (Y) * (XRB)->width4 + (X) )
493 * External functions:
496 extern struct xmesa_renderbuffer
*
497 xmesa_new_renderbuffer(GLcontext
*ctx
, GLuint name
, const GLvisual
*visual
,
498 GLboolean backBuffer
);
501 xmesa_delete_framebuffer(struct gl_framebuffer
*fb
);
504 xmesa_find_buffer(XMesaDisplay
*dpy
, XMesaColormap cmap
, XMesaBuffer notThis
);
507 xmesa_color_to_pixel( GLcontext
*ctx
,
508 GLubyte r
, GLubyte g
, GLubyte b
, GLubyte a
,
509 GLuint pixelFormat
);
512 xmesa_get_window_size(XMesaDisplay
*dpy
, XMesaBuffer b
,
513 GLuint
*width
, GLuint
*height
);
516 xmesa_check_and_update_buffer_size(XMesaContext xmctx
, XMesaBuffer drawBuffer
);
519 xmesa_init_driver_functions( XMesaVisual xmvisual
,
520 struct dd_function_table
*driver
);
523 xmesa_update_state( GLcontext
*ctx
, GLbitfield new_state
);
526 xmesa_set_renderbuffer_funcs(struct xmesa_renderbuffer
*xrb
,
527 enum pixel_format pixelformat
, GLint depth
);
530 xmesa_destroy_buffers_on_display(XMesaDisplay
*dpy
);
534 * Using a function instead of an ordinary cast is safer.
536 static INLINE
struct xmesa_renderbuffer
*
537 xmesa_renderbuffer(struct gl_renderbuffer
*rb
)
539 return (struct xmesa_renderbuffer
*) rb
;
544 * Return pointer to XMesaContext corresponding to a Mesa GLcontext.
545 * Since we're using structure containment, it's just a cast!.
547 static INLINE XMesaContext
548 XMESA_CONTEXT(GLcontext
*ctx
)
550 return (XMesaContext
) ctx
;
555 * Return pointer to XMesaBuffer corresponding to a Mesa GLframebuffer.
556 * Since we're using structure containment, it's just a cast!.
558 static INLINE XMesaBuffer
559 XMESA_BUFFER(GLframebuffer
*b
)
561 return (XMesaBuffer
) b
;
565 /* Plugged into the software rasterizer. Try to use internal
566 * swrast-style point, line and triangle functions.
568 extern void xmesa_choose_point( GLcontext
*ctx
);
569 extern void xmesa_choose_line( GLcontext
*ctx
);
570 extern void xmesa_choose_triangle( GLcontext
*ctx
);
573 extern void xmesa_register_swrast_functions( GLcontext
*ctx
);
577 #define ENABLE_EXT_texure_compression_s3tc 0 /* SW texture compression */
580 #define ENABLE_EXT_timer_query 0
581 #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
582 #define ENABLE_EXT_timer_query 1 /* should have 64-bit GLuint64EXT */
584 #define ENABLE_EXT_timer_query 0 /* may not have 64-bit GLuint64EXT */
588 #define TEST_META_FUNCS 0