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"
34 extern _glthread_Mutex _xmesa_lock
;
36 extern XMesaBuffer XMesaBufferList
;
38 /* for PF_8R8G8B24 pixel format */
46 struct xmesa_renderbuffer
;
49 /* Function pointer for clearing color buffers */
50 typedef void (*ClearFunc
)( struct gl_context
*ctx
, struct xmesa_renderbuffer
*xrb
,
51 GLint x
, GLint y
, GLint width
, GLint height
);
56 /** Framebuffer pixel formats */
58 PF_Index
, /**< Color Index mode */
59 PF_Truecolor
, /**< TrueColor or DirectColor, any depth */
60 PF_Dither_True
, /**< TrueColor with dithering */
61 PF_8A8R8G8B
, /**< 32-bit TrueColor: 8-A, 8-R, 8-G, 8-B bits */
62 PF_8A8B8G8R
, /**< 32-bit TrueColor: 8-A, 8-B, 8-G, 8-R bits */
63 PF_8R8G8B
, /**< 32-bit TrueColor: 8-R, 8-G, 8-B bits */
64 PF_8R8G8B24
, /**< 24-bit TrueColor: 8-R, 8-G, 8-B bits */
65 PF_5R6G5B
, /**< 16-bit TrueColor: 5-R, 6-G, 5-B bits */
66 PF_Dither
, /**< Color-mapped RGB with dither */
67 PF_Lookup
, /**< Color-mapped RGB without dither */
68 PF_HPCR
, /**< HP Color Recovery (ad@lms.be 30/08/95) */
69 PF_1Bit
, /**< monochrome dithering of RGB */
70 PF_Grayscale
, /**< Grayscale or StaticGray */
71 PF_Dither_5R6G5B
/**< 16-bit dithered TrueColor: 5-R, 6-G, 5-B */
76 * Visual inforation, derived from struct gl_config.
77 * Basically corresponds to an XVisualInfo.
80 struct gl_config mesa_visual
; /* Device independent visual parameters */
81 XMesaDisplay
*display
; /* The X11 display */
84 XMesaVisualInfo visinfo
; /* X's visual info (pointer to private copy) */
85 XVisualInfo
*vishandle
; /* Only used in fakeglx.c */
86 GLint BitsPerPixel
; /* True bits per pixel for XImages */
88 GLboolean ximage_flag
; /* Use XImage for back buffer (not pixmap)? */
90 enum pixel_format dithered_pf
; /* Pixel format when dithering */
91 enum pixel_format undithered_pf
;/* Pixel format when not dithering */
93 GLfloat RedGamma
; /* Gamma values, 1.0 is default */
97 /* For PF_TRUECOLOR */
98 GLint rshift
, gshift
, bshift
;/* Pixel color component shifts */
99 GLubyte Kernel
[16]; /* Dither kernel */
100 unsigned long RtoPixel
[512]; /* RGB to pixel conversion */
101 unsigned long GtoPixel
[512];
102 unsigned long BtoPixel
[512];
103 GLubyte PixelToR
[256]; /* Pixel to RGB conversion */
104 GLubyte PixelToG
[256];
105 GLubyte PixelToB
[256];
108 short hpcr_rgbTbl
[3][256];
109 GLboolean hpcr_clear_flag
;
110 GLubyte hpcr_clear_ximage_pattern
[2][16];
111 XMesaImage
*hpcr_clear_ximage
;
112 XMesaPixmap hpcr_clear_pixmap
;
120 * Context info, derived from struct gl_context.
121 * Basically corresponds to a GLXContext.
123 struct xmesa_context
{
124 struct gl_context mesa
; /* the core library context (containment) */
125 XMesaVisual xm_visual
; /* Describes the buffers */
126 XMesaBuffer xm_buffer
; /* current span/point/line/triangle buffer */
128 XMesaDisplay
*display
; /* == xm_visual->display */
129 GLboolean swapbytes
; /* Host byte order != display byte order? */
130 GLboolean direct
; /* Direct rendering context? */
132 enum pixel_format pixelformat
;
134 GLubyte clearcolor
[4]; /* current clearing color */
135 unsigned long clearpixel
; /* current clearing pixel value */
140 * Types of X/GLX drawables we might render into.
143 WINDOW
, /* An X window */
144 GLXWINDOW
, /* GLX window */
145 PIXMAP
, /* GLX pixmap */
146 PBUFFER
/* GLX Pbuffer */
150 /** Values for db_mode: */
152 #define BACK_PIXMAP 1
153 #define BACK_XIMAGE 2
158 * An xmesa_renderbuffer represents the back or front color buffer.
159 * For the front color buffer:
160 * <drawable> is the X window
161 * For the back color buffer:
162 * Either <ximage> or <pixmap> will be used, never both.
163 * In any case, <drawable> always equals <pixmap>.
164 * For stand-alone Mesa, we could merge <drawable> and <pixmap> into one
165 * field. We don't do that for the server-side GLcore module because
166 * pixmaps and drawables are different and we'd need a bunch of casts.
168 struct xmesa_renderbuffer
170 struct gl_renderbuffer Base
; /* Base class */
172 XMesaBuffer Parent
; /**< The XMesaBuffer this renderbuffer belongs to */
173 XMesaDrawable drawable
; /* Usually the X window ID */
174 XMesaPixmap pixmap
; /* Back color buffer */
175 XMesaImage
*ximage
; /* The back buffer, if not using a Pixmap */
177 GLubyte
*origin1
; /* used for PIXEL_ADDR1 macro */
179 GLushort
*origin2
; /* used for PIXEL_ADDR2 macro */
181 GLubyte
*origin3
; /* used for PIXEL_ADDR3 macro */
183 GLuint
*origin4
; /* used for PIXEL_ADDR4 macro */
186 GLint bottom
; /* used for FLIP macro, equals height - 1 */
190 GLuint map_x
, map_y
, map_w
, map_h
;
192 XMesaImage
*map_ximage
;
197 * Framebuffer information, derived from.
198 * Basically corresponds to a GLXDrawable.
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 */
206 XMesaDisplay
*display
;
207 BufferType type
; /* window, pixmap, pbuffer or glxwindow */
209 GLboolean largestPbuffer
; /**< for pbuffers */
210 GLboolean preservedContents
; /**< for pbuffers */
212 struct xmesa_renderbuffer
*frontxrb
; /* front color renderbuffer */
213 struct xmesa_renderbuffer
*backxrb
; /* back color renderbuffer */
215 XMesaColormap cmap
; /* the X colormap */
217 unsigned long selectedEvents
;/* for pbuffers only */
219 GLint db_mode
; /* 0 = single buffered */
220 /* BACK_PIXMAP = use Pixmap for back buffer */
221 /* BACK_XIMAGE = use XImage for back buffer */
222 GLuint shm
; /* X Shared Memory extension status: */
223 /* 0 = not available */
224 /* 1 = XImage support available */
225 /* 2 = Pixmap support available too */
226 #if defined(USE_XSHM)
227 XShmSegmentInfo shminfo
;
230 XMesaImage
*rowimage
; /* Used for optimized span writing */
231 XMesaPixmap stipple_pixmap
; /* For polygon stippling */
232 XMesaGC stipple_gc
; /* For polygon stippling */
234 XMesaGC gc
; /* scratch GC for span, line, tri drawing */
235 XMesaGC cleargc
; /* GC for clearing the color buffer */
236 XMesaGC swapgc
; /* GC for swapping the color buffers */
238 /* The following are here instead of in the XMesaVisual
239 * because they depend on the window's colormap.
242 /* For PF_DITHER, PF_LOOKUP, PF_GRAYSCALE */
243 unsigned long color_table
[576]; /* RGB -> pixel value */
245 /* For PF_DITHER, PF_LOOKUP, PF_GRAYSCALE */
246 GLubyte pixel_to_r
[65536]; /* pixel value -> red */
247 GLubyte pixel_to_g
[65536]; /* pixel value -> green */
248 GLubyte pixel_to_b
[65536]; /* pixel value -> blue */
250 /* Used to do XAllocColor/XFreeColors accounting: */
252 unsigned long alloced_colors
[256];
254 /* GLX_EXT_texture_from_pixmap */
255 GLint TextureTarget
; /** GLX_TEXTURE_1D_EXT, for example */
256 GLint TextureFormat
; /** GLX_TEXTURE_FORMAT_RGB_EXT, for example */
257 GLint TextureMipmap
; /** 0 or 1 */
259 struct xmesa_buffer
*Next
; /* Linked list pointer: */
264 * If pixelformat==PF_TRUECOLOR:
266 #define PACK_TRUECOLOR( PIXEL, R, G, B ) \
267 PIXEL = xmesa->xm_visual->RtoPixel[R] \
268 | xmesa->xm_visual->GtoPixel[G] \
269 | xmesa->xm_visual->BtoPixel[B]; \
273 * If pixelformat==PF_TRUEDITHER:
275 #define PACK_TRUEDITHER( PIXEL, X, Y, R, G, B ) \
277 int d = xmesa->xm_visual->Kernel[((X)&3) | (((Y)&3)<<2)]; \
278 PIXEL = xmesa->xm_visual->RtoPixel[(R)+d] \
279 | xmesa->xm_visual->GtoPixel[(G)+d] \
280 | xmesa->xm_visual->BtoPixel[(B)+d]; \
286 * If pixelformat==PF_8A8B8G8R:
288 #define PACK_8A8B8G8R( R, G, B, A ) \
289 ( ((A) << 24) | ((B) << 16) | ((G) << 8) | (R) )
293 * Like PACK_8A8B8G8R() but don't use alpha. This is usually an acceptable
296 #define PACK_8B8G8R( R, G, B ) ( ((B) << 16) | ((G) << 8) | (R) )
301 * If pixelformat==PF_8R8G8B:
303 #define PACK_8R8G8B( R, G, B) ( ((R) << 16) | ((G) << 8) | (B) )
307 * If pixelformat==PF_5R6G5B:
309 #define PACK_5R6G5B( R, G, B) ( (((R) & 0xf8) << 8) | (((G) & 0xfc) << 3) | ((B) >> 3) )
313 * If pixelformat==PF_8A8R8G8B:
315 #define PACK_8A8R8G8B( R, G, B, A ) \
316 ( ((A) << 24) | ((R) << 16) | ((G) << 8) | (B) )
321 * If pixelformat==PF_DITHER:
323 * Improved 8-bit RGB dithering code contributed by Bob Mercier
324 * (mercier@hollywood.cinenet.net). Thanks Bob!
330 # define DITH_MIX(r,g,b) (((r) * DITH_G + (g)) * DITH_B + (b))
335 # define DITH_MIX(r,g,b) (((g) << 6) | ((b) << 3) | (r))
339 #define DITH_N (DITH_DX * DITH_DY)
341 #define _dither(C, c, d) (((unsigned)((DITH_N * (C - 1) + 1) * c + d)) >> 12)
344 extern const int xmesa_kernel8
[DITH_DY
* DITH_DX
];
346 /* Dither for random X,Y */
347 #define DITHER_SETUP \
349 unsigned long *ctable = XMESA_BUFFER(ctx->DrawBuffer)->color_table;
351 #define DITHER( X, Y, R, G, B ) \
352 (__d = xmesa_kernel8[(((Y)&3)<<2) | ((X)&3)], \
353 ctable[DITH_MIX(_dither(DITH_R, (R), __d), \
354 _dither(DITH_G, (G), __d), \
355 _dither(DITH_B, (B), __d))])
357 /* Dither for random X, fixed Y */
358 #define XDITHER_SETUP(Y) \
360 unsigned long *ctable = XMESA_BUFFER(ctx->DrawBuffer)->color_table; \
361 const int *kernel = &xmesa_kernel8[ ((Y)&3) << 2 ];
363 #define XDITHER( X, R, G, B ) \
364 (__d = kernel[(X)&3], \
365 ctable[DITH_MIX(_dither(DITH_R, (R), __d), \
366 _dither(DITH_G, (G), __d), \
367 _dither(DITH_B, (B), __d))])
372 * Dithering for flat-shaded triangles. Precompute all 16 possible
373 * pixel values given the triangle's RGB color. Contributed by Martin Shenk.
375 #define FLAT_DITHER_SETUP( R, G, B ) \
376 GLushort ditherValues[16]; \
378 unsigned long *ctable = XMESA_BUFFER(ctx->DrawBuffer)->color_table; \
379 int msdr = (DITH_N*((DITH_R)-1)+1) * (R); \
380 int msdg = (DITH_N*((DITH_G)-1)+1) * (G); \
381 int msdb = (DITH_N*((DITH_B)-1)+1) * (B); \
383 for (i=0;i<16;i++) { \
384 int k = xmesa_kernel8[i]; \
385 int j = DITH_MIX( (msdr+k)>>12, (msdg+k)>>12, (msdb+k)>>12 );\
386 ditherValues[i] = (GLushort) ctable[j]; \
390 #define FLAT_DITHER_ROW_SETUP(Y) \
391 GLushort *ditherRow = ditherValues + ( ((Y)&3) << 2);
393 #define FLAT_DITHER(X) ditherRow[(X)&3]
398 * If pixelformat==PF_LOOKUP:
400 #define _dither_lookup(C, c) (((unsigned)((DITH_N * (C - 1) + 1) * c)) >> 12)
402 #define LOOKUP_SETUP \
403 unsigned long *ctable = XMESA_BUFFER(ctx->DrawBuffer)->color_table
405 #define LOOKUP( R, G, B ) \
406 ctable[DITH_MIX(_dither_lookup(DITH_R, (R)), \
407 _dither_lookup(DITH_G, (G)), \
408 _dither_lookup(DITH_B, (B)))]
412 * If pixelformat==PF_HPCR:
414 * HP Color Recovery dithering (ad@lms.be 30/08/95)
415 * HP has on its 8-bit 700-series computers, a feature called
416 * 'Color Recovery'. This allows near 24-bit output (so they say).
417 * It is enabled by selecting the 8-bit TrueColor visual AND
418 * corresponding colormap (see tkInitWindow) AND doing some special
421 extern const short xmesa_HPCR_DRGB
[3][2][16];
423 #define DITHER_HPCR( X, Y, R, G, B ) \
424 ( ((xmesa->xm_visual->hpcr_rgbTbl[0][R] + xmesa_HPCR_DRGB[0][(Y)&1][(X)&15]) & 0xE0) \
425 |(((xmesa->xm_visual->hpcr_rgbTbl[1][G] + xmesa_HPCR_DRGB[1][(Y)&1][(X)&15]) & 0xE0)>>3) \
426 | ((xmesa->xm_visual->hpcr_rgbTbl[2][B] + xmesa_HPCR_DRGB[2][(Y)&1][(X)&15])>>6) \
432 * If pixelformat==PF_1BIT:
434 extern const int xmesa_kernel1
[16];
436 #define SETUP_1BIT int bitFlip = xmesa->xm_visual->bitFlip
437 #define DITHER_1BIT( X, Y, R, G, B ) \
438 (( ((int)(R)+(int)(G)+(int)(B)) > xmesa_kernel1[(((Y)&3) << 2) | ((X)&3)] ) ^ bitFlip)
443 * If pixelformat==PF_GRAYSCALE:
445 #define GRAY_RGB( R, G, B ) XMESA_BUFFER(ctx->DrawBuffer)->color_table[((R) + (G) + (B))/3]
450 * Converts a GL window Y coord to an X window Y coord:
452 #define YFLIP(XRB, Y) ((XRB)->bottom - (Y))
456 * Return the address of a 1, 2 or 4-byte pixel in the buffer's XImage:
457 * X==0 is left, Y==0 is bottom.
459 #define PIXEL_ADDR1(XRB, X, Y) \
460 ( (XRB)->origin1 - (Y) * (XRB)->width1 + (X) )
462 #define PIXEL_ADDR2(XRB, X, Y) \
463 ( (XRB)->origin2 - (Y) * (XRB)->width2 + (X) )
465 #define PIXEL_ADDR3(XRB, X, Y) \
466 ( (bgr_t *) ( (XRB)->origin3 - (Y) * (XRB)->width3 + 3 * (X) ))
468 #define PIXEL_ADDR4(XRB, X, Y) \
469 ( (XRB)->origin4 - (Y) * (XRB)->width4 + (X) )
474 * External functions:
477 extern struct xmesa_renderbuffer
*
478 xmesa_new_renderbuffer(struct gl_context
*ctx
, GLuint name
,
479 const struct xmesa_visual
*xmvis
,
480 GLboolean backBuffer
);
483 xmesa_delete_framebuffer(struct gl_framebuffer
*fb
);
486 xmesa_find_buffer(XMesaDisplay
*dpy
, XMesaColormap cmap
, XMesaBuffer notThis
);
489 xmesa_color_to_pixel( struct gl_context
*ctx
,
490 GLubyte r
, GLubyte g
, GLubyte b
, GLubyte a
,
491 GLuint pixelFormat
);
494 xmesa_get_window_size(XMesaDisplay
*dpy
, XMesaBuffer b
,
495 GLuint
*width
, GLuint
*height
);
498 xmesa_check_and_update_buffer_size(XMesaContext xmctx
, XMesaBuffer drawBuffer
);
501 xmesa_init_driver_functions( XMesaVisual xmvisual
,
502 struct dd_function_table
*driver
);
505 xmesa_update_state( struct gl_context
*ctx
, GLbitfield new_state
);
508 xmesa_set_renderbuffer_funcs(struct xmesa_renderbuffer
*xrb
,
509 enum pixel_format pixelformat
, GLint depth
);
512 xmesa_MapRenderbuffer(struct gl_context
*ctx
,
513 struct gl_renderbuffer
*rb
,
514 GLuint x
, GLuint y
, GLuint w
, GLuint h
,
516 GLubyte
**mapOut
, GLint
*rowStrideOut
);
519 xmesa_UnmapRenderbuffer(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
);
522 xmesa_destroy_buffers_on_display(XMesaDisplay
*dpy
);
526 * Using a function instead of an ordinary cast is safer.
528 static INLINE
struct xmesa_renderbuffer
*
529 xmesa_renderbuffer(struct gl_renderbuffer
*rb
)
531 return (struct xmesa_renderbuffer
*) rb
;
536 * Return pointer to XMesaContext corresponding to a Mesa struct gl_context.
537 * Since we're using structure containment, it's just a cast!.
539 static INLINE XMesaContext
540 XMESA_CONTEXT(struct gl_context
*ctx
)
542 return (XMesaContext
) ctx
;
547 * Return pointer to XMesaBuffer corresponding to a Mesa struct gl_framebuffer.
548 * Since we're using structure containment, it's just a cast!.
550 static INLINE XMesaBuffer
551 XMESA_BUFFER(struct gl_framebuffer
*b
)
553 return (XMesaBuffer
) b
;
557 /* Plugged into the software rasterizer. Try to use internal
558 * swrast-style point, line and triangle functions.
560 extern void xmesa_choose_point( struct gl_context
*ctx
);
561 extern void xmesa_choose_line( struct gl_context
*ctx
);
562 extern void xmesa_choose_triangle( struct gl_context
*ctx
);
565 extern void xmesa_register_swrast_functions( struct gl_context
*ctx
);
569 #define ENABLE_EXT_texure_compression_s3tc 0 /* SW texture compression */
571 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
572 #define ENABLE_EXT_timer_query 1 /* should have 64-bit GLuint64EXT */
574 #define ENABLE_EXT_timer_query 0 /* may not have 64-bit GLuint64EXT */
578 #define TEST_META_FUNCS 0