fixed bug in 24bpp ximage pixel addressing
[mesa.git] / src / mesa / drivers / x11 / xmesaP.h
1 /* $Id: xmesaP.h,v 1.7 2000/02/22 17:14:02 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.3
6 *
7 * Copyright (C) 1999-2000 Brian Paul All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27
28 #ifndef XMESAP_H
29 #define XMESAP_H
30
31
32 #ifdef XFree86Server
33 # include "GL/xf86glx.h"
34 # include "xf86glx_util.h"
35 #else
36 # ifdef GLX_DIRECT_RENDERING
37 # include "dri_mesa.h"
38 # endif
39 # ifdef USE_XSHM
40 # include <X11/extensions/XShm.h>
41 # endif
42 #endif
43 #include "GL/xmesa.h"
44 #include "types.h"
45 #if defined(FX) && !defined(GLX_DIRECT_RENDERING)
46 #include "GL/fxmesa.h"
47 #include "../FX/fxdrv.h"
48 #endif
49
50
51 #if defined(GLX_DIRECT_RENDERING) && !defined(XFree86Server)
52 # include "xdriP.h"
53 #else
54 # define DRI_DRAWABLE_ARG
55 # define DRI_DRAWABLE_PARM
56 # define DRI_CTX_ARG
57 #endif
58
59
60 /* for PF_8R8G8B24 pixel format */
61 typedef struct {
62 GLubyte b;
63 GLubyte g;
64 GLubyte r;
65 } bgr_t;
66
67
68 /* Function pointer for clearing color buffers */
69 typedef void (*clear_func)( GLcontext *ctx,
70 GLboolean all, GLint x, GLint y,
71 GLint width, GLint height );
72
73
74 /*
75 * "Derived" from gl_visual. Basically corresponds to an XVisualInfo.
76 */
77 struct xmesa_visual {
78 GLvisual *gl_visual; /* Device independent visual parameters */
79 XMesaDisplay *display; /* The X11 display */
80 #ifdef XFree86Server
81 GLint screen_depth; /* The depth of the screen */
82 #else
83 XVisualInfo *vishandle; /* The pointer returned by glXChooseVisual */
84 #endif
85 XMesaVisualInfo visinfo; /* X's visual info */
86 GLint BitsPerPixel; /* True bits per pixel for XImages */
87
88 GLint level; /* 0=normal, 1=overlay, etc */
89
90 GLboolean ximage_flag; /* Use XImage for back buffer (not pixmap)? */
91
92 GLuint dithered_pf; /* Pixel format when dithering */
93 GLuint undithered_pf; /* Pixel format when not dithering */
94
95 GLfloat RedGamma; /* Gamma values, 1.0 is default */
96 GLfloat GreenGamma;
97 GLfloat BlueGamma;
98
99 GLint rmult, gmult, bmult; /* Range of color values */
100 GLint index_bits; /* Bits per pixel in CI mode */
101
102 /* For PF_TRUECOLOR */
103 GLint rshift, gshift, bshift;/* Pixel color component shifts */
104 GLubyte Kernel[16]; /* Dither kernel */
105 unsigned long RtoPixel[512]; /* RGB to pixel conversion */
106 unsigned long GtoPixel[512];
107 unsigned long BtoPixel[512];
108 GLubyte PixelToR[256]; /* Pixel to RGB conversion */
109 GLubyte PixelToG[256];
110 GLubyte PixelToB[256];
111
112 /* For PF_HPCR */
113 short hpcr_rgbTbl[3][256];
114 GLboolean hpcr_clear_flag;
115 GLubyte hpcr_clear_ximage_pattern[2][16];
116 XMesaImage *hpcr_clear_ximage;
117 XMesaPixmap hpcr_clear_pixmap;
118
119 /* For PF_1BIT */
120 int bitFlip;
121 };
122
123
124
125 /*
126 * "Derived" from gl_context. Basically corresponds to a GLXContext.
127 */
128 struct xmesa_context {
129 GLcontext *gl_ctx; /* the core library context */
130 XMesaVisual xm_visual; /* Describes the buffers */
131 XMesaBuffer xm_buffer; /* current draw framebuffer */
132 XMesaBuffer xm_read_buffer; /* current read framebuffer */
133 GLboolean use_read_buffer; /* read from the xm_read_buffer/ */
134
135 XMesaDisplay *display; /* == xm_visual->display */
136 GLboolean swapbytes; /* Host byte order != display byte order? */
137 GLboolean direct; /* Direct rendering context? */
138
139 GLuint pixelformat; /* Current pixel format */
140
141 GLubyte red, green, blue, alpha; /* current drawing color */
142 unsigned long pixel; /* current drawing pixel value */
143
144 GLubyte clearcolor[4]; /* current clearing color */
145 unsigned long clearpixel; /* current clearing pixel value */
146
147 #if defined(GLX_DIRECT_RENDERING) && !defined(XFree86Server)
148 __DRIcontextPrivate *driContextPriv; /* back pointer to DRI context
149 * used for locking
150 */
151 void *private; /* device-specific private context */
152 #endif
153 };
154
155
156
157 /*
158 * "Derived" from gl_buffer. Basically corresponds to a GLXDrawable.
159 */
160 struct xmesa_buffer {
161 GLboolean wasCurrent; /* was ever the current buffer? */
162 GLframebuffer *gl_buffer; /* depth, stencil, accum, etc buffers */
163 XMesaVisual xm_visual; /* the X/Mesa visual */
164
165 XMesaContext xm_context; /* the context associated with this buffer */
166 XMesaDisplay *display;
167 GLboolean pixmap_flag; /* is the buffer a Pixmap? */
168 XMesaDrawable frontbuffer; /* either a window or pixmap */
169 XMesaPixmap backpixmap; /* back buffer Pixmap */
170 XMesaImage *backimage; /* back buffer simulated XImage */
171
172 XMesaDrawable buffer; /* the current buffer, either equal to */
173 /* frontbuffer, backpixmap or XIMAGE (None) */
174
175 XMesaColormap cmap; /* the X colormap */
176
177 GLint db_state; /* 0 = single buffered */
178 /* BACK_PIXMAP = use Pixmap for back buffer */
179 /* BACK_XIMAGE = use XImage for back buffer */
180
181 #ifndef XFree86Server
182 GLuint shm; /* X Shared Memory extension status: */
183 /* 0 = not available */
184 /* 1 = XImage support available */
185 /* 2 = Pixmap support available too */
186 #ifdef USE_XSHM
187 XShmSegmentInfo shminfo;
188 #endif
189 #endif
190
191 XMesaImage *rowimage; /* Used for optimized span writing */
192
193 GLuint width, height; /* size of buffer */
194
195 GLint bottom; /* used for FLIP macro below */
196 GLubyte *ximage_origin1; /* used for PIXELADDR1 macro */
197 GLint ximage_width1;
198 GLushort *ximage_origin2; /* used for PIXELADDR2 macro */
199 GLint ximage_width2;
200 GLubyte *ximage_origin3; /* used for PIXELADDR3 macro */
201 GLint ximage_width3;
202 GLuint *ximage_origin4; /* used for PIXELADDR4 macro */
203 GLint ximage_width4;
204
205 XMesaPixmap stipple_pixmap; /* For polygon stippling */
206 XMesaGC stipple_gc; /* For polygon stippling */
207
208 XMesaGC gc1; /* GC for infrequent color changes */
209 XMesaGC gc2; /* GC for frequent color changes */
210 XMesaGC cleargc; /* GC for clearing the color buffer */
211
212 /* The following are here instead of in the XMesaVisual
213 * because they depend on the window's colormap.
214 */
215
216 /* For PF_DITHER, PF_LOOKUP, PF_GRAYSCALE */
217 unsigned long color_table[576]; /* RGB -> pixel value */
218
219 /* For PF_DITHER, PF_LOOKUP, PF_GRAYSCALE */
220 GLubyte pixel_to_r[65536]; /* pixel value -> red */
221 GLubyte pixel_to_g[65536]; /* pixel value -> green */
222 GLubyte pixel_to_b[65536]; /* pixel value -> blue */
223
224 /* Used to do XAllocColor/XFreeColors accounting: */
225 int num_alloced;
226 unsigned long alloced_colors[256];
227
228 #if defined(GLX_DIRECT_RENDERING) && !defined(XFree86Server)
229 __DRIdrawablePrivate *driDrawPriv; /* back pointer to DRI drawable
230 * used for direct access to framebuffer
231 */
232 void *private; /* device-specific private drawable */
233 #endif
234
235 #if defined( FX ) && !defined(GLX_DIRECT_RENDERING)
236 /* For 3Dfx Glide only */
237 GLboolean FXisHackUsable; /* Can we render into window? */
238 GLboolean FXwindowHack; /* Are we rendering into a window? */
239 fxMesaContext FXctx;
240 #endif
241
242 /* functions for clearing the front and back color buffers */
243 clear_func front_clear_func;
244 clear_func back_clear_func;
245
246 struct xmesa_buffer *Next; /* Linked list pointer: */
247 };
248
249
250
251 /* Values for xmesa->dest: */
252 #define FRONT_PIXMAP 1
253 #define BACK_PIXMAP 2
254 #define BACK_XIMAGE 4
255
256
257 /* Values for xmesa->pixelformat: */
258 #define PF_INDEX 1 /* Color Index mode */
259 #define PF_TRUECOLOR 2 /* TrueColor or DirectColor, any depth */
260 #define PF_TRUEDITHER 3 /* TrueColor with dithering */
261 #define PF_8A8B8G8R 4 /* 32-bit TrueColor: 8-A, 8-B, 8-G, 8-R */
262 #define PF_8R8G8B 5 /* 32-bit TrueColor: 8-R, 8-G, 8-B bits */
263 #define PF_5R6G5B 6 /* 16-bit TrueColor: 5-R, 6-G, 5-B bits */
264 #define PF_DITHER 7 /* Color-mapped RGB with dither */
265 #define PF_LOOKUP 8 /* Color-mapped RGB without dither */
266 #define PF_HPCR 9 /* HP Color Recovery (ad@lms.be 30/08/95) */
267 #define PF_1BIT 10 /* monochrome dithering of RGB */
268 #define PF_GRAYSCALE 11 /* Grayscale or StaticGray */
269 #define PF_8R8G8B24 12 /* 24-bit TrueColor: 8-R, 8-G, 8-B bits */
270 #define PF_DITHER_5R6G5B 13 /* 16-bit dithered TrueColor: 5-R, 6-G, 5-B */
271
272
273 /*
274 * If pixelformat==PF_TRUECOLOR:
275 */
276 #define PACK_TRUECOLOR( PIXEL, R, G, B ) \
277 PIXEL = xmesa->xm_visual->RtoPixel[R] \
278 | xmesa->xm_visual->GtoPixel[G] \
279 | xmesa->xm_visual->BtoPixel[B]; \
280
281
282 /*
283 * If pixelformat==PF_TRUEDITHER:
284 */
285 #define PACK_TRUEDITHER( PIXEL, X, Y, R, G, B ) \
286 { \
287 int d = xmesa->xm_visual->Kernel[((X)&3) | (((Y)&3)<<2)]; \
288 PIXEL = xmesa->xm_visual->RtoPixel[(R)+d] \
289 | xmesa->xm_visual->GtoPixel[(G)+d] \
290 | xmesa->xm_visual->BtoPixel[(B)+d]; \
291 }
292
293
294
295 /*
296 * If pixelformat==PF_8A8B8G8R:
297 */
298 #define PACK_8A8B8G8R( R, G, B, A ) \
299 ( ((A) << 24) | ((B) << 16) | ((G) << 8) | (R) )
300
301
302 /*
303 * Like PACK_8A8B8G8R() but don't use alpha. This is usually an acceptable
304 * shortcut.
305 */
306 #define PACK_8B8G8R( R, G, B ) ( ((B) << 16) | ((G) << 8) | (R) )
307
308
309
310 /*
311 * If pixelformat==PF_8R8G8B:
312 */
313 #define PACK_8R8G8B( R, G, B) ( ((R) << 16) | ((G) << 8) | (B) )
314
315
316 /*
317 * If pixelformat==PF_5R6G5B:
318 */
319 #define PACK_5R6G5B( R, G, B) ( (((R) & 0xf8) << 8) | (((G) & 0xfc) << 3) | ((B) >> 3) )
320
321
322
323
324 /*
325 * If pixelformat==PF_DITHER:
326 *
327 * Improved 8-bit RGB dithering code contributed by Bob Mercier
328 * (mercier@hollywood.cinenet.net). Thanks Bob!
329 */
330 #undef _R
331 #undef _G
332 #undef _B
333 #undef _D
334 #ifdef DITHER666
335 # define _R 6
336 # define _G 6
337 # define _B 6
338 # define _MIX(r,g,b) (((r)*_G+(g))*_B+(b))
339 #else
340 # define _R 5
341 # define _G 9
342 # define _B 5
343 # define _MIX(r,g,b) ( ((g)<<6) | ((b)<<3) | (r) )
344 #endif
345 #define _DX 4
346 #define _DY 4
347 #define _D (_DX*_DY)
348
349 /*#define _DITH(C,c,d) (((unsigned)((_D*(C-1)+1)*c+d))/(_D*256))*/
350 #define _DITH(C,c,d) (((unsigned)((_D*(C-1)+1)*c+d)) >> 12)
351
352 #define MAXC 256
353 static int kernel8[_DY*_DX] = {
354 0 * MAXC, 8 * MAXC, 2 * MAXC, 10 * MAXC,
355 12 * MAXC, 4 * MAXC, 14 * MAXC, 6 * MAXC,
356 3 * MAXC, 11 * MAXC, 1 * MAXC, 9 * MAXC,
357 15 * MAXC, 7 * MAXC, 13 * MAXC, 5 * MAXC,
358 };
359 /*static int __d;*/
360
361 /* Dither for random X,Y */
362 #define DITHER_SETUP \
363 int __d; \
364 unsigned long *ctable = xmesa->xm_buffer->color_table;
365
366 #define DITHER( X, Y, R, G, B ) \
367 (__d = kernel8[(((Y)&3)<<2) | ((X)&3)], \
368 ctable[_MIX(_DITH(_R, (R), __d), \
369 _DITH(_G, (G), __d), \
370 _DITH(_B, (B), __d))])
371
372 /* Dither for random X, fixed Y */
373 #define XDITHER_SETUP(Y) \
374 int __d; \
375 unsigned long *ctable = xmesa->xm_buffer->color_table; \
376 int *kernel = &kernel8[ ((Y)&3) << 2 ];
377
378 #define XDITHER( X, R, G, B ) \
379 (__d = kernel[(X)&3], \
380 ctable[_MIX(_DITH(_R, (R), __d), \
381 _DITH(_G, (G), __d), \
382 _DITH(_B, (B), __d))])
383
384
385
386 /*
387 * Dithering for flat-shaded triangles. Precompute all 16 possible
388 * pixel values given the triangle's RGB color. Contributed by Martin Shenk.
389 */
390 static GLushort DitherValues[16]; /* array of (up to) 16-bit pixel values */
391
392 #define FLAT_DITHER_SETUP( R, G, B ) \
393 { \
394 unsigned long *ctable = xmesa->xm_buffer->color_table; \
395 int msdr = (_D*((_R)-1)+1) * (R); \
396 int msdg = (_D*((_G)-1)+1) * (G); \
397 int msdb = (_D*((_B)-1)+1) * (B); \
398 int i; \
399 for (i=0;i<16;i++) { \
400 int k = kernel8[i]; \
401 int j = _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 _DITH0(C,c) (((unsigned)((_D*(C-1)+1)*c)) >> 12)
417
418 #define LOOKUP_SETUP \
419 unsigned long *ctable = xmesa->xm_buffer->color_table
420
421 #define LOOKUP( R, G, B ) \
422 ctable[_MIX(_DITH0(_R, (R)), \
423 _DITH0(_G, (G)), \
424 _DITH0(_B, (B)))]
425
426
427
428 /*
429 * If pixelformat==PF_HPCR:
430 *
431 * HP Color Recovery dithering (ad@lms.be 30/08/95)
432 * HP has on it's 8-bit 700-series computers, a feature called
433 * 'Color Recovery'. This allows near 24-bit output (so they say).
434 * It is enabled by selecting the 8-bit TrueColor visual AND
435 * corresponding colormap (see tkInitWindow) AND doing some special
436 * dither.
437 */
438 static const short HPCR_DRGB[3][2][16] = {
439 {
440 { 16, -4, 1,-11, 14, -6, 3, -9, 15, -5, 2,-10, 13, -7, 4, -8},
441 {-15, 5, 0, 12,-13, 7, -2, 10,-14, 6, -1, 11,-12, 8, -3, 9}
442 },
443 {
444 {-11, 15, -7, 3, -8, 14, -4, 2,-10, 16, -6, 4, -9, 13, -5, 1},
445 { 12,-14, 8, -2, 9,-13, 5, -1, 11,-15, 7, -3, 10,-12, 6, 0}
446 },
447 {
448 { 6,-18, 26,-14, 2,-22, 30,-10, 8,-16, 28,-12, 4,-20, 32, -8},
449 { -4, 20,-24, 16, 0, 24,-28, 12, -6, 18,-26, 14, -2, 22,-30, 10}
450 }
451 };
452
453 #define DITHER_HPCR( X, Y, R, G, B ) \
454 ( ((xmesa->xm_visual->hpcr_rgbTbl[0][R] + HPCR_DRGB[0][(Y)&1][(X)&15]) & 0xE0) \
455 |(((xmesa->xm_visual->hpcr_rgbTbl[1][G] + HPCR_DRGB[1][(Y)&1][(X)&15]) & 0xE0)>>3) \
456 | ((xmesa->xm_visual->hpcr_rgbTbl[2][B] + HPCR_DRGB[2][(Y)&1][(X)&15])>>6) \
457 )
458
459
460
461 /*
462 * If pixelformat==PF_1BIT:
463 */
464 static int const kernel1[16] = {
465 0*47, 9*47, 4*47, 12*47, /* 47 = (255*3)/16 */
466 6*47, 2*47, 14*47, 8*47,
467 10*47, 1*47, 5*47, 11*47,
468 7*47, 13*47, 3*47, 15*47 };
469
470 #define SETUP_1BIT int bitFlip = xmesa->xm_visual->bitFlip
471 #define DITHER_1BIT( X, Y, R, G, B ) \
472 (( ((int)(R)+(int)(G)+(int)(B)) > kernel1[(((Y)&3) << 2) | ((X)&3)] ) ^ bitFlip)
473
474
475
476 /*
477 * If pixelformat==PF_GRAYSCALE:
478 */
479 #define GRAY_RGB( R, G, B ) xmesa->xm_buffer->color_table[((R) + (G) + (B))/3]
480
481
482
483 #define XIMAGE None
484
485
486 /*
487 * Converts a GL window Y coord to an X window Y coord:
488 */
489 #define FLIP(BUFFER, Y) ((BUFFER)->bottom-(Y))
490
491
492 /*
493 * Return the address of a 1, 2 or 4-byte pixel in the back XImage:
494 * X==0 is left, Y==0 is bottom.
495 */
496 #define PIXELADDR1( BUFFER, X, Y ) \
497 ( (BUFFER)->ximage_origin1 - (Y) * (BUFFER)->ximage_width1 + (X) )
498
499 #define PIXELADDR2( BUFFER, X, Y ) \
500 ( (BUFFER)->ximage_origin2 - (Y) * (BUFFER)->ximage_width2 + (X) )
501
502 #define PIXELADDR3( BUFFER, X, Y ) \
503 ( (bgr_t *) ( (BUFFER)->ximage_origin3 - (Y) * (BUFFER)->ximage_width3 + 3 * (X) ))
504
505 #define PIXELADDR4( BUFFER, X, Y ) \
506 ( (BUFFER)->ximage_origin4 - (Y) * (BUFFER)->ximage_width4 + (X) )
507
508
509
510 /*
511 * External functions:
512 */
513
514 extern unsigned long xmesa_color_to_pixel( XMesaContext xmesa,
515 GLubyte r, GLubyte g, GLubyte b, GLubyte a );
516
517 extern void xmesa_alloc_back_buffer( XMesaBuffer b );
518
519 extern void xmesa_update_state( GLcontext *ctx );
520
521 extern points_func xmesa_get_points_func( GLcontext *ctx );
522
523 extern line_func xmesa_get_line_func( GLcontext *ctx );
524
525 extern triangle_func xmesa_get_triangle_func( GLcontext *ctx );
526
527
528 /* XXX this is a hack to implement shared display lists with 3Dfx */
529 extern XMesaBuffer XMesaCreateWindowBuffer2( XMesaVisual v,
530 XMesaWindow w,
531 XMesaContext c
532 DRI_DRAWABLE_ARG
533 );
534
535 /*
536 * These are the extra routines required for integration with XFree86.
537 * None of these routines should be user visible. -KEM
538 */
539 extern void XMesaSetVisualDisplay( XMesaDisplay *dpy, XMesaVisual v );
540 extern GLboolean XMesaForceCurrent(XMesaContext c);
541 extern GLboolean XMesaLoseCurrent(XMesaContext c);
542 extern void XMesaReset( void );
543
544 #endif