home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 5 / MA_Cover_5.iso / ppc / mesa / src / xmesap.h < prev   
Encoding:
C/C++ Source or Header  |  1998-01-31  |  14.5 KB  |  511 lines

  1. /* $Id: xmesaP.h,v 1.20 1997/09/10 00:05:33 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  2.4
  6.  * Copyright (C) 1995-1997  Brian Paul
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library; if not, write to the Free
  20.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23.  
  24. /*
  25.  * $Log: xmesaP.h,v $
  26.  * Revision 1.20  1997/09/10 00:05:33  brianp
  27.  * undefine _R, _G and _B symbols to prevent compiler warnings
  28.  *
  29.  * Revision 1.19  1997/08/13 02:06:03  brianp
  30.  * incorporated David Bucciarelli's v0.18 changes
  31.  *
  32.  * Revision 1.18  1997/07/24 00:59:10  brianp
  33.  * implemented FXHACK (3Dfx rendering copied into X window)
  34.  *
  35.  * Revision 1.17  1997/06/20 02:58:18  brianp
  36.  * added wasCurrent to xmesa_buffer struct
  37.  *
  38.  * Revision 1.16  1997/06/03 02:03:17  brianp
  39.  * removed unused stipple_ximage variable
  40.  *
  41.  * Revision 1.15  1997/05/26 20:34:40  brianp
  42.  * added PF_TRUEDITHER pixel format
  43.  *
  44.  * Revision 1.14  1997/04/27 02:42:52  brianp
  45.  * Optimized color table setup.  Free X colormap entries when finished.
  46.  *
  47.  * Revision 1.13  1997/04/12 16:22:43  brianp
  48.  * removed XMesa declaration
  49.  *
  50.  * Revision 1.12  1997/04/04 05:23:57  brianp
  51.  * fixed problem with black/white being reversed on monochrome displays
  52.  *
  53.  * Revision 1.11  1997/03/16 02:16:52  brianp
  54.  * added PACK_8B8G8R macro
  55.  *
  56.  * Revision 1.10  1997/01/31 23:44:24  brianp
  57.  * added FLAT_DITHER macros inspired by code from Martin Schenk (schenkm@ping.at)
  58.  *
  59.  * Revision 1.9  1997/01/31 20:41:20  brianp
  60.  * added vishandle to xmesa_visual struct
  61.  *
  62.  * Revision 1.8  1997/01/08 20:54:02  brianp
  63.  * added DITHER666 option from Michael Pichler
  64.  *
  65.  * Revision 1.7  1996/10/22 02:54:35  brianp
  66.  * incorporated Jacques Leroy's changes to DITHER_HPCR macro
  67.  *
  68.  * Revision 1.6  1996/10/22 02:50:46  brianp
  69.  * new DITHER_SETUP, XDITHER_SETUP and XDITHER macros
  70.  * _MIX macro now uses shifts and ORs instead of mults and adds
  71.  *
  72.  * Revision 1.5  1996/09/27 17:10:20  brianp
  73.  * added index_bits field to XMesaVisual struct
  74.  *
  75.  * Revision 1.4  1996/09/27 01:32:12  brianp
  76.  * changed kernel1 array from 2-D to 1-D
  77.  *
  78.  * Revision 1.3  1996/09/19 03:16:04  brianp
  79.  * new X/Mesa interface with XMesaContext, XMesaVisual, and XMesaBuffer types
  80.  *
  81.  * Revision 1.2  1996/09/15 14:22:13  brianp
  82.  * now use GLframebuffer and GLvisual
  83.  *
  84.  * Revision 1.1  1996/09/13 01:38:16  brianp
  85.  * Initial revision
  86.  *
  87.  */
  88.  
  89.  
  90. #ifndef XMESAP_H
  91. #define XMESAP_H
  92.  
  93.  
  94. #ifdef SHM
  95. #  include <X11/extensions/XShm.h>
  96. #endif
  97. #include "GL/xmesa.h"
  98. #include "types.h"
  99. #ifdef FX
  100. #include "GL/fxmesa.h"
  101. #endif
  102.  
  103.  
  104. /*
  105.  * Mesa wrapper for XVisualInfo
  106.  */
  107. struct xmesa_visual {
  108.     GLvisual *gl_visual;    /* Device independent visual parameters */
  109.     Display *display;    /* The X11 display */
  110.     XVisualInfo *vishandle;    /* The pointer returned by glXChooseVisual */
  111.     XVisualInfo *visinfo;    /* Private copy of vishandle's struct */
  112.  
  113.     GLint level;        /* 0=normal, 1=overlay, etc */
  114.  
  115.     GLboolean ximage_flag;    /* Use XImage for back buffer (not pixmap)? */
  116.  
  117.         GLuint dithered_pf;    /* Pixel format when dithering */
  118.         GLuint undithered_pf;    /* Pixel format when not dithering */
  119.  
  120.         GLfloat RedGamma;    /* Gamma values, 1.0 is default */
  121.         GLfloat GreenGamma;
  122.         GLfloat BlueGamma;
  123.  
  124.         GLint rmult, gmult, bmult;    /* Range of color values */
  125.         GLint index_bits;        /* Bits per pixel in CI mode */
  126.  
  127.     /* For PF_TRUECOLOR */
  128.     GLint rshift, gshift, bshift;    /* Pixel color component shifts */
  129.     GLubyte Kernel[16];        /* Dither kernel */
  130.     unsigned long RtoPixel[512];    /* RGB to pixel conversion */
  131.     unsigned long GtoPixel[512];
  132.     unsigned long BtoPixel[512];
  133.     GLubyte PixelToR[256];        /* Pixel to RGB conversion */
  134.     GLubyte PixelToG[256];
  135.     GLubyte PixelToB[256];
  136.  
  137.     /* For PF_HPCR */
  138.     short hpcr_rTbl[256], hpcr_gTbl[256], hpcr_bTbl[256];
  139.  
  140.     /* For PF_1BIT */
  141.     int bitFlip;
  142. };
  143.  
  144.  
  145.  
  146. /*
  147.  * Mesa wrapper for core rendering context
  148.  */
  149. struct xmesa_context {
  150.     GLcontext *gl_ctx;        /* the core library context */
  151.         XMesaVisual xm_visual;        /* Describes the buffers */
  152.     XMesaBuffer xm_buffer;        /* current framebuffer */
  153.  
  154.     Display *display;    /* == xm_visual->display */
  155.         GLboolean swapbytes;    /* Host byte order != display byte order? */
  156.  
  157.     GLuint pixelformat;        /* Current pixel format */
  158.  
  159.         GLubyte red, green, blue, alpha;/* current drawing color */
  160.     unsigned long pixel;        /* current drawing pixel value */
  161.  
  162.     GLubyte clearcolor[4];        /* current clearing color */
  163.     unsigned long clearpixel;    /* current clearing pixel value */
  164.  
  165. #ifdef FX
  166.     GLboolean FXuse;    /* Use 3Dfx Glide for rendering ? */
  167.     GLboolean FXhack;    /* Use 3Dfx Glide for rendering into X win? */
  168.     fxMesaContext FXctx;
  169. #endif
  170. };
  171.  
  172.  
  173.  
  174. /*
  175.  * Mesa wrapper for X window or Pixmap
  176.  */
  177. struct xmesa_buffer {
  178.    GLboolean wasCurrent;    /* was ever the current buffer? */
  179.    GLframebuffer *gl_buffer;    /* depth, stencil, accum, etc buffers */
  180.    XMesaVisual xm_visual;    /* the X/Mesa visual */
  181.  
  182.    Display *display;
  183.    GLboolean pixmap_flag;    /* is the buffer a Pixmap? */
  184.    Drawable frontbuffer;    /* either a window or pixmap */
  185.    Pixmap backpixmap;        /* back buffer Pixmap */
  186.    XImage *backimage;        /* back buffer XImage */
  187.  
  188.    Drawable buffer;        /* the current buffer, either equal to */
  189.                 /* frontbuffer, backpixmap or XIMAGE (None) */
  190.  
  191.    Colormap cmap;        /* the X colormap */
  192.  
  193.    GLint db_state;        /* 0 = single buffered */
  194.                 /* BACK_PIXMAP = use Pixmap for back buffer */
  195.                 /* BACK_XIMAGE = use XImage for back buffer */
  196.  
  197.    GLuint shm;            /* X Shared Memory extension status:    */
  198.                 /*    0 = not available            */
  199.                 /*    1 = XImage support available    */
  200.                 /*    2 = Pixmap support available too    */
  201. #ifdef SHM
  202.    XShmSegmentInfo shminfo;
  203. #endif
  204.  
  205.    XImage *rowimage;        /* Used for optimized span writing */
  206.  
  207.    GLuint width, height;    /* size of buffer */
  208.  
  209.    GLint bottom;        /* used for FLIP macro below */
  210.    GLubyte *ximage_origin1;    /* used for PIXELADDR1 macro */
  211.    GLint ximage_width1;
  212.    GLushort *ximage_origin2;    /* used for PIXELADDR2 macro */
  213.    GLint ximage_width2;
  214.    GLuint *ximage_origin4;    /* used for PIXELADDR4 macro */
  215.    GLint ximage_width4;
  216.  
  217.    Pixmap stipple_pixmap;    /* For polygon stippling */
  218.    GC stipple_gc;        /* For polygon stippling */
  219.  
  220.    GC gc1;            /* GC for infrequent color changes */
  221.    GC gc2;            /* GC for frequent color changes */
  222.    GC cleargc;            /* GC for clearing the color buffer */
  223.  
  224.    /* The following are here instead of in the XMesaVisual
  225.     * because they depend on the window's colormap.
  226.     */
  227.  
  228.    /* For PF_DITHER, PF_LOOKUP, PF_GRAYSCALE */
  229.    unsigned long color_table[576];    /* RGB -> pixel value */
  230.  
  231.    /* For PF_DITHER, PF_LOOKUP, PF_GRAYSCALE */
  232.    GLubyte pixel_to_r[65536];        /* pixel value -> red */
  233.    GLubyte pixel_to_g[65536];        /* pixel value -> green */
  234.    GLubyte pixel_to_b[65536];        /* pixel value -> blue */
  235.  
  236.    /* Used to do XAllocColor/XFreeColors accounting: */
  237.    int num_alloced;
  238.    unsigned long alloced_colors[256];
  239.  
  240.    struct xmesa_buffer *Next;    /* Linked list pointer: */
  241. };
  242.  
  243.  
  244.  
  245. /* Values for xmesa->dest: */
  246. #define FRONT_PIXMAP    1
  247. #define BACK_PIXMAP    2
  248. #define BACK_XIMAGE    4
  249.  
  250.  
  251. /* Values for xmesa->pixelformat: */
  252. #define PF_INDEX    1    /* Color Index mode */
  253. #define PF_TRUECOLOR    2    /* TrueColor or DirectColor, any depth */
  254. #define PF_TRUEDITHER    3    /* TrueColor with dithering */
  255. #define PF_8A8B8G8R    4    /* 32-bit TrueColor:  8-A, 8-B, 8-G, 8-R */
  256. #define PF_8R8G8B    5    /* 32-bit TrueColor:  8-R, 8-G, 8-B bits */
  257. #define PF_5R6G5B    6    /* 16-bit TrueColor:  5-R, 6-G, 5-B bits */
  258. #define PF_DITHER    7    /* Color-mapped RGB with dither */
  259. #define PF_LOOKUP    8    /* Color-mapped RGB without dither */
  260. #define PF_HPCR        9    /* HP Color Recovery (ad@lms.be 30/08/95) */
  261. #define PF_1BIT        10    /* monochrome dithering of RGB */
  262. #define PF_GRAYSCALE    11    /* Grayscale or StaticGray */
  263.  
  264.  
  265. /*
  266.  * If pixelformat==PF_TRUECOLOR:
  267.  */
  268. #define PACK_TRUECOLOR( PIXEL, R, G, B )    \
  269.    PIXEL = xmesa->xm_visual->RtoPixel[R]    \
  270.          | xmesa->xm_visual->GtoPixel[G]    \
  271.          | xmesa->xm_visual->BtoPixel[B];    \
  272.  
  273.  
  274. /*
  275.  * If pixelformat==PF_TRUEDITHER:
  276.  */
  277. #define PACK_TRUEDITHER( PIXEL, X, Y, R, G, B )            \
  278. {                                \
  279.    int d = xmesa->xm_visual->Kernel[((X)&3) | (((Y)&3)<<2)];    \
  280.    PIXEL = xmesa->xm_visual->RtoPixel[(R)+d]            \
  281.          | xmesa->xm_visual->GtoPixel[(G)+d]            \
  282.          | xmesa->xm_visual->BtoPixel[(B)+d];            \
  283. }
  284.  
  285.  
  286.  
  287. /*
  288.  * If pixelformat==PF_8A8B8G8R:
  289.  */
  290. #define PACK_8A8B8G8R( R, G, B, A )    \
  291.     ( ((A) << 24) | ((B) << 16) | ((G) << 8) | (R) )
  292.  
  293.  
  294. /*
  295.  * Like PACK_8A8B8G8R() but don't use alpha.  This is usually an acceptable
  296.  * shortcut.
  297.  */
  298. #define PACK_8B8G8R( R, G, B )   ( ((B) << 16) | ((G) << 8) | (R) )
  299.  
  300.  
  301.  
  302. /*
  303.  * If pixelformat==PF_8R8G8B:
  304.  */
  305. #define PACK_8R8G8B( R, G, B)     ( ((R) << 16) | ((G) << 8) | (B) )
  306.  
  307.  
  308. /*
  309.  * If pixelformat==PF_5R6G5B:
  310.  */
  311. #define PACK_5R6G5B( R, G, B)     ( (((R) & 0xf8) << 8) | (((G) & 0xfc) << 3) | ((B) >> 3) )
  312.  
  313.  
  314.  
  315. /*
  316.  * If pixelformat==PF_DITHER:
  317.  *
  318.  * Improved 8-bit RGB dithering code contributed by Bob Mercier
  319.  * (mercier@hollywood.cinenet.net).  Thanks Bob!
  320.  */
  321. #undef _R
  322. #undef _G
  323. #undef _B
  324. #ifdef DITHER666
  325. # define _R   6
  326. # define _G   6
  327. # define _B   6
  328. # define _MIX(r,g,b)  (((r)*_G+(g))*_B+(b))
  329. #else
  330. # define _R    5
  331. # define _G    9
  332. # define _B    5
  333. # define _MIX(r,g,b)    ( ((g)<<6) | ((b)<<3) | (r) )
  334. #endif
  335. #define _DX    4
  336. #define _DY    4
  337. #define _D    (_DX*_DY)
  338.  
  339. /*#define _DITH(C,c,d)    (((unsigned)((_D*(C-1)+1)*c+d))/(_D*256))*/
  340. #define _DITH(C,c,d)    (((unsigned)((_D*(C-1)+1)*c+d)) >> 12)
  341.  
  342. #define MAXC    256
  343. static int kernel8[_DY*_DX] = {
  344.     0 * MAXC,  8 * MAXC,  2 * MAXC, 10 * MAXC,
  345.    12 * MAXC,  4 * MAXC, 14 * MAXC,  6 * MAXC,
  346.     3 * MAXC, 11 * MAXC,  1 * MAXC,  9 * MAXC,
  347.    15 * MAXC,  7 * MAXC, 13 * MAXC,  5 * MAXC,
  348. };
  349. /*static int __d;*/
  350.  
  351. /* Dither for random X,Y */
  352. #define DITHER_SETUP                        \
  353.     int __d;                        \
  354.     unsigned long *ctable = xmesa->xm_buffer->color_table;
  355.  
  356. #define DITHER( X, Y, R, G, B )                \
  357.     (__d = kernel8[(((Y)&3)<<2) | ((X)&3)],        \
  358.      ctable[_MIX(_DITH(_R, (R), __d),        \
  359.              _DITH(_G, (G), __d),        \
  360.              _DITH(_B, (B), __d))])
  361.  
  362. /* Dither for random X, fixed Y */
  363. #define XDITHER_SETUP(Y)                    \
  364.     int __d;                        \
  365.     unsigned long *ctable = xmesa->xm_buffer->color_table;    \
  366.     int *kernel = &kernel8[ ((Y)&3) << 2 ];
  367.  
  368. #define XDITHER( X, R, G, B )                \
  369.     (__d = kernel[(X)&3],                \
  370.     ctable[_MIX(_DITH(_R, (R), __d),        \
  371.             _DITH(_G, (G), __d),        \
  372.             _DITH(_B, (B), __d))])
  373.  
  374.  
  375. /*
  376.  * Dithering for flat-shaded triangles.  Precompute all 16 possible
  377.  * pixel values given the triangle's RGB color.  Contributed by Martin Shenk.
  378.  */
  379. static GLushort DitherValues[16];   /* array of (up to) 16-bit pixel values */
  380.  
  381. #define FLAT_DITHER_SETUP( R, G, B )                    \
  382.     {                                \
  383.        unsigned long *ctable = xmesa->xm_buffer->color_table;    \
  384.        int msdr = (_D*((_R)-1)+1) * (R);                \
  385.        int msdg = (_D*((_G)-1)+1) * (G);                \
  386.        int msdb = (_D*((_B)-1)+1) * (B);                \
  387.        int i;                            \
  388.        for (i=0;i<16;i++) {                        \
  389.           int k = kernel8[i];                    \
  390.           int j = _MIX( (msdr+k)>>12, (msdg+k)>>12, (msdb+k)>>12 );    \
  391.           DitherValues[i] = ctable[j];                \
  392.        }                                \
  393.         }
  394.  
  395. #define FLAT_DITHER_ROW_SETUP(Y)                    \
  396.     GLushort *ditherRow = DitherValues + ( ((Y)&3) << 2);
  397.  
  398. #define FLAT_DITHER(X)  ditherRow[(X)&3]
  399.  
  400.  
  401.  
  402. /*
  403.  * If pixelformat==PF_LOOKUP:
  404.  */
  405. #define _DITH0(C,c)    (((unsigned)((_D*(C-1)+1)*c)) >> 12)
  406.  
  407. #define LOOKUP_SETUP                        \
  408.     unsigned long *ctable = xmesa->xm_buffer->color_table
  409.  
  410. #define LOOKUP( R, G, B )            \
  411.     ctable[_MIX(_DITH0(_R, (R)),        \
  412.             _DITH0(_G, (G)),        \
  413.             _DITH0(_B, (B)))]
  414.  
  415.  
  416.  
  417. /*
  418.  * If pixelformat==PF_HPCR:
  419.  *
  420.  *      HP Color Recovery dithering               (ad@lms.be 30/08/95)
  421.  *      HP has on it's 8-bit 700-series computers, a feature called
  422.  *      'Color Recovery'.  This allows near 24-bit output (so they say).
  423.  *      It is enabled by selecting the 8-bit  TrueColor  visual AND
  424.  *      corresponding  colormap (see tkInitWindow) AND doing some special
  425.  *      dither.
  426.  */
  427. static const short HPCR_DR[2][16] = {
  428.     { 16, -4,  1,-11, 14, -6,  3, -9, 15, -5,  2,-10, 13, -7,  4, -8},
  429.     {-15,  5,  0, 12,-13,  7, -2, 10,-14,  6, -1, 11,-12,  8, -3,  9} };
  430. static const short HPCR_DG[2][16] = {
  431.     {-11, 15, -7,  3, -8, 14, -4,  2,-10, 16, -6,  4, -9, 13, -5,  1},
  432.     { 12,-14,  8, -2,  9,-13,  5, -1, 11,-15,  7, -3, 10,-12,  6,  0} };
  433. static const short HPCR_DB[2][16] = {
  434.     {  6,-18, 26,-14,  2,-22, 30,-10,  8,-16, 28,-12,  4,-20, 32, -8},
  435.     { -4, 20,-24, 16,  0, 24,-28, 12, -6, 18,-26, 14, -2, 22,-30, 10} };
  436.  
  437. #define DITHER_HPCR( X, Y, R, G, B )                       \
  438.   ( ((xmesa->xm_visual->hpcr_rTbl[R] + HPCR_DR[(Y)&1][(X)&15]) & 0xE0)     \
  439.   |(((xmesa->xm_visual->hpcr_gTbl[G] + HPCR_DG[(Y)&1][(X)&15]) & 0xE0)>>3) \
  440.   | ((xmesa->xm_visual->hpcr_bTbl[B] + HPCR_DB[(Y)&1][(X)&15])>>6)       \
  441.   )
  442.  
  443.  
  444.  
  445. /*
  446.  * If pixelformat==PF_1BIT:
  447.  */
  448. static int const kernel1[16] = {
  449.    0*47,  9*47,  4*47, 12*47,     /* 47 = (255*3)/16 */
  450.    6*47,  2*47, 14*47,  8*47,
  451.   10*47,  1*47,  5*47, 11*47,
  452.    7*47, 13*47,  3*47, 15*47 };
  453.  
  454. #define SETUP_1BIT  int bitFlip = xmesa->xm_visual->bitFlip
  455. #define DITHER_1BIT( X, Y, R, G, B )    \
  456.     (( ((int)(R)+(int)(G)+(int)(B)) > kernel1[(((Y)&3) << 2) | ((X)&3)] ) ^ bitFlip)
  457.  
  458.  
  459.  
  460. /*
  461.  * If pixelformat==PF_GRAYSCALE:
  462.  */
  463. #define GRAY_RGB( R, G, B )   xmesa->xm_buffer->color_table[(R) + (G) + (B)]
  464.  
  465.  
  466.  
  467. #define XIMAGE None
  468.  
  469.  
  470. /*
  471.  * Converts a GL window Y coord to an X window Y coord:
  472.  */
  473. #define FLIP(Y)  (xmesa->xm_buffer->bottom-(Y))
  474.  
  475.  
  476. /*
  477.  * Return the address of a 1, 2 or 4-byte pixel in the back XImage:
  478.  * X==0 is left, Y==0 is bottom.
  479.  */
  480. #define PIXELADDR1( X, Y )  \
  481.       ( xmesa->xm_buffer->ximage_origin1 - (Y) * xmesa->xm_buffer->ximage_width1 + (X) )
  482.  
  483. #define PIXELADDR2( X, Y )  \
  484.       ( xmesa->xm_buffer->ximage_origin2 - (Y) * xmesa->xm_buffer->ximage_width2 + (X) )
  485.  
  486. #define PIXELADDR4( X, Y )  \
  487.       ( xmesa->xm_buffer->ximage_origin4 - (Y) * xmesa->xm_buffer->ximage_width4 + (X) )
  488.  
  489.  
  490.  
  491. /*
  492.  * External functions:
  493.  */
  494.  
  495. extern unsigned long xmesa_color_to_pixel( XMesaContext xmesa,
  496.                           GLubyte r, GLubyte g, GLubyte b, GLubyte a );
  497.  
  498. extern void xmesa_alloc_back_buffer( XMesaBuffer b );
  499.  
  500. extern void xmesa_setup_DD_pointers( GLcontext *ctx );
  501.  
  502. extern points_func xmesa_get_points_func( GLcontext *ctx );
  503.  
  504. extern line_func xmesa_get_line_func( GLcontext *ctx );
  505.  
  506. extern triangle_func xmesa_get_triangle_func( GLcontext *ctx );
  507.  
  508.  
  509.  
  510. #endif
  511.