Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / mesa / drivers / dri / tdfx / tdfx_context.h
1 /* -*- mode: c; c-basic-offset: 3 -*-
2  *
3  * Copyright 2000 VA Linux Systems Inc., Fremont, California.
4  *
5  * 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 (including the next
15  * paragraph) shall be included in all copies or substantial portions of the
16  * Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
21  * VA LINUX SYSTEMS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
23  * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24  * SOFTWARE.
25  */
26
27 /*
28  * New fixes:
29  *      Daniel Borca <dborca@users.sourceforge.net>, 19 Jul 2004
30  *
31  * Original rewrite:
32  *      Gareth Hughes <gareth@valinux.com>, 29 Sep - 1 Oct 2000
33  *
34  * Authors:
35  *      Gareth Hughes <gareth@valinux.com>
36  *
37  */
38
39 #ifndef __TDFX_CONTEXT_H__
40 #define __TDFX_CONTEXT_H__
41
42 #include <sys/time.h>
43 #include "dri_util.h"
44 #include "main/glheader.h"
45 #if defined(__linux__)
46 #include <signal.h>
47 #endif
48
49 #include "drm.h"
50 #include "drm_sarea.h"
51 #include "tdfx_glide.h"
52 #include "xmlconfig.h"
53
54 #include "main/clip.h"
55 #include "main/context.h"
56 #include "main/macros.h"
57 #include "main/matrix.h"
58 #include "main/imports.h"
59 #include "main/mtypes.h"
60
61 #include "tdfx_screen.h"
62
63
64
65
66 #define TDFX_TMU0               GR_TMU0
67 #define TDFX_TMU1               GR_TMU1
68 #define TDFX_TMU_SPLIT          98
69 #define TDFX_TMU_BOTH           99
70 #define TDFX_TMU_NONE           100
71
72
73
74 /* Flags for fxMesa->new_state
75  */
76 #define TDFX_NEW_COLOR          0x0001
77 #define TDFX_NEW_ALPHA          0x0002
78 #define TDFX_NEW_DEPTH          0x0004
79 #define TDFX_NEW_FOG            0x0008
80 #define TDFX_NEW_STENCIL        0x0010
81 #define TDFX_NEW_CLIP           0x0020
82 #define TDFX_NEW_VIEWPORT       0x0040
83 #define TDFX_NEW_CULL           0x0080
84 #define TDFX_NEW_GLIDE          0x0100
85 #define TDFX_NEW_TEXTURE        0x0200
86 #define TDFX_NEW_CONTEXT        0x0400
87 #define TDFX_NEW_LINE           0x0800
88 #define TDFX_NEW_RENDER         0x1000
89 #define TDFX_NEW_STIPPLE        0x2000
90 #define TDFX_NEW_TEXTURE_BIND   0x4000 /* experimental */
91
92
93 /* Flags for fxMesa->dirty
94  */
95 #define TDFX_UPLOAD_COLOR_COMBINE       0x00000001
96 #define TDFX_UPLOAD_ALPHA_COMBINE       0x00000002
97 #define TDFX_UPLOAD_RENDER_BUFFER       0x00000004
98 #define TDFX_UPLOAD_ALPHA_TEST          0x00000008
99 #define TDFX_UPLOAD_ALPHA_REF           0x00000010
100 #define TDFX_UPLOAD_BLEND_FUNC          0x00000020
101 #define TDFX_UPLOAD_DEPTH_MODE          0x00000040
102 #define TDFX_UPLOAD_DEPTH_BIAS          0x00000080
103 #define TDFX_UPLOAD_DEPTH_FUNC          0x00000100
104 #define TDFX_UPLOAD_DEPTH_MASK          0x00000200
105 #define TDFX_UPLOAD_FOG_MODE            0x00000400
106 #define TDFX_UPLOAD_FOG_COLOR           0x00000800
107 #define TDFX_UPLOAD_FOG_TABLE           0x00001000
108 #define TDFX_UPLOAD_CONSTANT_COLOR      0x00002000
109
110 #define TDFX_UPLOAD_CLIP                0x00002000
111 #define TDFX_UPLOAD_CULL                0x00004000
112 #define TDFX_UPLOAD_VERTEX_LAYOUT       0x00008000
113 #define TDFX_UPLOAD_COLOR_MASK          0x00010000
114 #define TDFX_UPLOAD_DITHER              0x00040000
115 #define TDFX_UPLOAD_STENCIL             0x00080000
116
117 #define TDFX_UPLOAD_TEXTURE_SOURCE      0x00100000
118 #define TDFX_UPLOAD_TEXTURE_PARAMS      0x00200000
119 #define TDFX_UPLOAD_TEXTURE_PALETTE     0x00400000
120 #define TDFX_UPLOAD_TEXTURE_ENV         0x00800000
121 #define TDFX_UPLOAD_TEXTURE_IMAGES      0x01000000
122
123 #define TDFX_UPLOAD_LINE                0x02000000
124
125 #define TDFX_UPLOAD_STIPPLE             0x04000000
126
127 /* Flags for software fallback cases */
128 /* See correponding strings in tdfx_tris.c */
129 #define TDFX_FALLBACK_TEXTURE_MAP       0x0001
130 #define TDFX_FALLBACK_DRAW_BUFFER       0x0002
131 #define TDFX_FALLBACK_SPECULAR          0x0004
132 #define TDFX_FALLBACK_STENCIL           0x0008
133 #define TDFX_FALLBACK_RENDER_MODE       0x0010
134 #define TDFX_FALLBACK_LOGICOP           0x0020
135 #define TDFX_FALLBACK_TEXTURE_ENV       0x0040
136 #define TDFX_FALLBACK_TEXTURE_BORDER    0x0080
137 #define TDFX_FALLBACK_COLORMASK         0x0100
138 #define TDFX_FALLBACK_BLEND             0x0200
139 #define TDFX_FALLBACK_LINE_STIPPLE      0x0400
140 #define TDFX_FALLBACK_DISABLE           0x0800
141
142 /* Different Glide vertex layouts
143  */
144 #define TDFX_LAYOUT_TINY        0
145 #define TDFX_LAYOUT_NOTEX       1
146 #define TDFX_LAYOUT_SINGLE      2
147 #define TDFX_LAYOUT_MULTI       3
148 #define TDFX_LAYOUT_PROJ1       4
149 #define TDFX_LAYOUT_PROJ2       5
150 #define TDFX_NUM_LAYOUTS        6
151
152 #define TDFX_XY_OFFSET          0
153 #define TDFX_Z_OFFSET           8
154 #define TDFX_Q_OFFSET           12
155 #define TDFX_ARGB_OFFSET        16
156 #define TDFX_FOG_OFFSET         20
157 #define TDFX_ST0_OFFSET         24
158 #define TDFX_ST1_OFFSET         32
159 #define TDFX_Q0_OFFSET          40
160 #define TDFX_Q1_OFFSET          44
161
162
163 /* Flags for buffer clears
164  */
165 #define TDFX_FRONT              0x1
166 #define TDFX_BACK               0x2
167 #define TDFX_DEPTH              0x4
168 #define TDFX_STENCIL            0x8
169
170 /*
171  * Subpixel offsets to adjust Mesa's (true) window coordinates to
172  * Glide coordinates.  We need these to ensure precise rasterization.
173  * Otherwise, we'll fail a bunch of conformance tests.
174  */
175 #define TRI_X_OFFSET    ( 0.0F)
176 #define TRI_Y_OFFSET    ( 0.0F)
177 #define LINE_X_OFFSET   ( 0.0F)
178 #define LINE_Y_OFFSET   ( 0.125F)
179 #define PNT_X_OFFSET    ( 0.375F)
180 #define PNT_Y_OFFSET    ( 0.375F)
181
182
183 #define TDFX_DEPTH_BIAS_SCALE   128
184
185 /* Including xf86PciInfo.h causes a bunch of errors
186  */
187 #ifndef PCI_CHIP_BANSHEE
188 #define PCI_CHIP_BANSHEE        0x0003
189 #define PCI_CHIP_VOODOO3        0x0005
190 #define PCI_CHIP_VOODOO4        0x0009
191 #define PCI_CHIP_VOODOO5        0x0009
192 #endif
193
194 #define TDFX_IS_BANSHEE( fxMesa ) \
195                 ( fxMesa->fxScreen->deviceID == PCI_CHIP_BANSHEE )
196 #define TDFX_IS_VOODOO3( fxMesa ) \
197                 ( fxMesa->fxScreen->deviceID == PCI_CHIP_VOODOO3 )
198 #define TDFX_IS_VOODOO4( fxMesa ) \
199                 ( fxMesa->fxScreen->deviceID == PCI_CHIP_VOODOO4 )
200 #define TDFX_IS_VOODOO5( fxMesa ) \
201                 ( fxMesa->fxScreen->deviceID == PCI_CHIP_VOODOO5 )
202 #define TDFX_IS_NAPALM( fxMesa ) \
203                 ( (fxMesa->fxScreen->deviceID == PCI_CHIP_VOODOO4) || \
204                   (fxMesa->fxScreen->deviceID == PCI_CHIP_VOODOO5) )
205
206
207 #define PACK_BGRA32(R, G, B, A)  \
208     ( (((GLuint) (R)) << 16) | \
209       (((GLuint) (G)) <<  8) | \
210       (((GLuint) (B))      ) | \
211       (((GLuint) (A)) << 24) )
212
213 #define PACK_RGBA32(R, G, B, A)  \
214     ( (((GLuint) (R))      ) | \
215       (((GLuint) (G)) <<  8) | \
216       (((GLuint) (B)) << 16) | \
217       (((GLuint) (A)) << 24) )
218
219 /*
220  * The first two macros are to pack 8 bit color
221  * channel values into a 565 format.
222  */
223 #define PACK_RGB16(R, G, B)         \
224     ((((GLuint) (R) & 0xF8) << 8) | \
225      (((GLuint) (G) & 0xFC) << 3) | \
226       (((GLuint) (B) & 0xFF)         >> 3))
227 #define PACK_BGR16(R, G, B)         \
228     ((((GLuint) (B) & 0xF8) << 8) | \
229      (((GLuint) (G) & 0xFC) << 3) | \
230      (((GLuint) (R) & 0xFF) >> 3))
231 /*
232  * The second two macros pack 8 bit color channel values
233  * into 1555 values.
234  */
235 #define PACK_RGBA16(R, G, B, A)       \
236     (((((GLuint) (A) & 0xFF) > 0) << 15)| \
237      (((GLuint) (R)  & 0xF8)      << 7) | \
238      (((GLuint) (G)  & 0xF8)      << 2) | \
239      (((GLuint) (B)  & 0xF8)      >> 3))
240 #define PACK_BGRA16(R, G, B, A) \
241     (((((GLuint) (A) & 0xFF) > 0) << 15)| \
242       (((GLuint) (B) & 0xF8)     << 7)  | \
243       (((GLuint) (G) & 0xF8)     << 2)  | \
244       (((GLuint) (R) & 0xF8)     >> 3))
245
246 /* Used in calls to grColorMaskv()...
247  */
248 extern const GLboolean false4[4];
249 extern const GLboolean true4[4];
250
251
252 typedef struct tdfx_context tdfxContextRec;
253 typedef struct tdfx_context *tdfxContextPtr;
254
255
256 typedef struct {
257    volatile int fifoPtr;
258    volatile int fifoRead;
259    volatile int fifoOwner;
260    volatile int ctxOwner;
261    volatile int texOwner;
262 }
263 TDFXSAREAPriv;
264
265
266 typedef struct {
267    GLuint swapBuffer;
268    GLuint reqTexUpload;
269    GLuint texUpload;
270    GLuint memTexUpload;
271    GLuint texSwaps;
272 } tdfxStats;
273
274
275
276 /*
277  *  Memory range from startAddr to endAddr-1
278  */
279 typedef struct mem_range {
280    struct mem_range *next;
281    FxU32 startAddr, endAddr;
282 }
283 tdfxMemRange;
284
285
286 typedef struct {
287     GLsizei             width, height;  /* image size */
288     GLint               wScale, hScale; /* scale factors */
289     GrTextureFormat_t   glideFormat;    /* Glide image format */
290 }
291 tdfxMipMapLevel;
292
293
294 #define TDFX_NUM_TMU            2
295
296
297 typedef struct tdfxTexInfo_t
298 {
299    GLboolean isInTM;
300    GLboolean reloadImages;  /* if true, resend images to Glide */
301    GLuint lastTimeUsed;
302    FxU32 whichTMU;
303
304    GrTexInfo info;
305    GrAspectRatio_t aspectRatio;
306    tdfxMemRange *tm[TDFX_NUM_TMU];
307
308    GLint minLevel, maxLevel;
309    GrTextureFilterMode_t minFilt;
310    GrTextureFilterMode_t magFilt;
311    GrTextureClampMode_t sClamp;
312    GrTextureClampMode_t tClamp;
313    FxBool LODblend;
314    GrMipMapMode_t mmMode;
315
316    GLfloat sScale, tScale;  /* texcoord scale factor */
317
318    GrTexTable_t paltype;
319    GuTexPalette palette;
320
321    GLboolean padded;
322 }
323 tdfxTexInfo;
324
325
326 #define TDFX_TEXTURE_DATA(mesaObj) ((tdfxTexInfo *)((mesaObj)->DriverData))
327
328 #define TDFX_TEXIMAGE_DATA(mesaImg) ((tdfxMipMapLevel *)((mesaImg)->DriverData))
329
330
331
332 /*
333  * This is state which may be shared by several tdfx contexts.
334  * It hangs off of Mesa's gl_shared_state object (ctx->Shared->DriverData).
335  */
336 struct tdfxSharedState {
337    GLboolean umaTexMemory;
338    GLuint totalTexMem[TDFX_NUM_TMU]; /* constant */
339    GLuint freeTexMem[TDFX_NUM_TMU]; /* changes as we go */
340    tdfxMemRange *tmPool;
341    tdfxMemRange *tmFree[TDFX_NUM_TMU];
342 };
343
344
345
346 /* ================================================================
347  * The vertex structures.
348  */
349 /* The size of this union is not of relevence:
350  */
351 typedef struct tdfx_vertex_t {
352    GLfloat x, y, z;                     /* Coordinates in screen space */
353    GLfloat rhw;                         /* Reciprocal homogeneous w */
354    GLubyte color[4];            /* Diffuse color */
355    GLfloat fog;
356    GLfloat tu0, tv0;            /* Texture 0 coordinates */
357    GLfloat tu1, tv1;            /* Texture 1 coordinates */
358    GLfloat tq0, tq1;            /* Texture 0/1 q coords */
359    unsigned char pspec[4];      /* B, G, R, A [0..255] */
360    float psize;         /* point size */
361    long pad[16 - 14];   /* ensure 64b structure */
362 } tdfxVertex, *tdfxVertexPtr;
363
364
365 /* ================================================================
366  *
367  * We want to keep a mirror of the Glide function call parameters so we
368  * can avoid updating our state too often.
369  *
370  * Each of these broad groups will typically have a new state flag
371  * associated with it, and will be updated together.  The individual
372  * Glide function calls each have a dirty flag and will only be called
373  * when absolutely necessary.
374  */
375
376 /* for grTexSource() */
377 struct tdfx_texsource {
378    FxU32 StartAddress;
379    FxU32 EvenOdd;
380    GrTexInfo *Info;
381 };
382
383 /* Texture object params */
384 struct tdfx_texparams {
385    GrTextureClampMode_t sClamp;
386    GrTextureClampMode_t tClamp;
387    GrTextureFilterMode_t minFilt;
388    GrTextureFilterMode_t magFilt;
389    GrMipMapMode_t mmMode;
390    FxBool LODblend;
391    GLfloat LodBias;
392 };
393
394 /* for grTexDownloadTable() texture palettes */
395 struct tdfx_texpalette {
396    GrTexTable_t Type;
397    void *Data;
398 };
399
400 /* for Voodoo3/Banshee's grColorCombine() and grAlphaCombine() */
401 struct tdfx_combine {
402    GrCombineFunction_t Function;        /* Combine function */
403    GrCombineFactor_t Factor;            /* Combine scale factor */
404    GrCombineLocal_t Local;              /* Local combine source */
405    GrCombineOther_t Other;              /* Other combine source */
406    FxBool Invert;                       /* Combine result inversion flag */
407 };
408
409 /* for Voodoo3's grTexCombine() */
410 struct tdfx_texcombine {
411    GrCombineFunction_t FunctionRGB;
412    GrCombineFactor_t FactorRGB;
413    GrCombineFunction_t FunctionAlpha;
414    GrCombineFactor_t FactorAlpha;
415    FxBool InvertRGB;
416    FxBool InvertAlpha;
417 };
418
419
420 /* for Voodoo5's grColorCombineExt() */
421 struct tdfx_combine_color_ext {
422    GrCCUColor_t SourceA;
423    GrCombineMode_t ModeA;
424    GrCCUColor_t SourceB;
425    GrCombineMode_t ModeB;
426    GrCCUColor_t SourceC;
427    FxBool InvertC;
428    GrCCUColor_t SourceD;
429    FxBool InvertD;
430    FxU32 Shift;
431    FxBool Invert;
432 };
433
434 /* for Voodoo5's grAlphaCombineExt() */
435 struct tdfx_combine_alpha_ext {
436    GrACUColor_t SourceA;
437    GrCombineMode_t ModeA;
438    GrACUColor_t SourceB;
439    GrCombineMode_t ModeB;
440    GrACUColor_t SourceC;
441    FxBool InvertC;
442    GrACUColor_t SourceD;
443    FxBool InvertD;
444    FxU32 Shift;
445    FxBool Invert;
446 };
447
448 /* for Voodoo5's grTexColorCombineExt() */
449 struct tdfx_color_texenv {
450    GrTCCUColor_t SourceA;
451    GrCombineMode_t ModeA;
452    GrTCCUColor_t SourceB;
453    GrCombineMode_t ModeB;
454    GrTCCUColor_t SourceC;
455    FxBool InvertC;
456    GrTCCUColor_t SourceD;
457    FxBool InvertD;
458    FxU32 Shift;
459    FxBool Invert;
460 };
461
462 /* for Voodoo5's grTexAlphaCombineExt() */
463 struct tdfx_alpha_texenv {
464    GrTACUColor_t SourceA;
465    GrCombineMode_t ModeA;
466    GrTACUColor_t SourceB;
467    GrCombineMode_t ModeB;
468    GrTACUColor_t SourceC;
469    FxBool InvertC;
470    GrTCCUColor_t SourceD;
471    FxBool InvertD;
472    FxU32 Shift;
473    FxBool Invert;
474 };
475
476 /* Voodoo5's texture combine environment */
477 struct tdfx_texcombine_ext {
478    struct tdfx_alpha_texenv Alpha;
479    struct tdfx_color_texenv Color;
480    GrColor_t EnvColor;
481 };
482
483 /* Used to track changes between Glide's state and Mesa's */
484 struct tdfx_texstate {
485    GLuint Enabled[2];              /* values ala ctx->Texture.Unit[i]._ReallyEnabled */
486    GLenum EnvMode[TDFX_NUM_TMU];   /* index is Glide index, not OpenGL */
487    GLenum TexFormat[TDFX_NUM_TMU]; /* index is Glide index, not OpenGL */
488 };
489
490 struct tdfx_color {
491    GrColor_t ClearColor;                /* Buffer clear color value */
492    GrAlpha_t ClearAlpha;                /* Buffer clear alpha value */
493    FxBool ColorMask[4];                 /* Per-channel write enable flags */
494
495    GrColor_t MonoColor;                 /* Constant color value */
496
497    /* Alpha testing */
498    GrCmpFnc_t AlphaFunc;                /* Alpha test function */
499    GrAlpha_t AlphaRef;                  /* Alpha ref value in range [0,255] */
500
501    /* Blending */
502    GrAlphaBlendFnc_t BlendSrcRGB;       /* Blend source RGB factor */
503    GrAlphaBlendFnc_t BlendDstRGB;       /* Blend destination RGB factor */
504    GrAlphaBlendOp_t BlendEqRGB;         /* Blend source RGB op */
505    GrAlphaBlendFnc_t BlendSrcA;         /* Blend source alpha factor */
506    GrAlphaBlendFnc_t BlendDstA;         /* Blend destination alpha factor */
507    GrAlphaBlendOp_t BlendEqA;           /* Blend source alpha op */
508
509    GrDitherMode_t Dither;               /* Dither enable */
510 };
511
512 struct tdfx_depth {
513    GrDepthBufferMode_t Mode;            /* Fixed-point Z or floating-point W */
514    FxI32 Bias;                          /* Polygon offset factor */
515    GrCmpFnc_t Func;                     /* Depth test function */
516    FxU32 Clear;                         /* Buffer clear value */
517    FxBool Mask;                         /* Write enable flag */
518 };
519
520 struct tdfx_stipple {
521    GrStippleMode_t Mode;                /* Stipple enable/disable */
522    FxU32 Pattern;                       /* 8x4 Stipple Pattern */
523 };
524
525 struct tdfx_fog {
526    GrFogMode_t Mode;                    /* Glide fog mode */
527    GrColor_t Color;                     /* Fog color value */
528    GLenum TableMode;                    /* GL fog mode currently in table */
529    GrFog_t *Table;                      /* Fog value table */
530    FxFloat Density;                     /* Density >= 0 */
531    FxFloat Near;                        /* Start distance in eye coords */
532    FxFloat Far;                         /* End distance in eye coords */
533 };
534
535 struct tdfx_stencil {
536    GrCmpFnc_t Function;                 /* Stencil function */
537    GrStencil_t RefValue;                /* Stencil reference value */
538    GrStencil_t ValueMask;               /* Value mask */
539    GrStencil_t WriteMask;               /* Write mask */
540    GrStencil_t FailFunc;                /* Stencil fail function */
541    GrStencil_t ZFailFunc;               /* Stencil pass, depth fail function */
542    GrStencil_t ZPassFunc;               /* Stencil pass, depth pass function */
543    GrStencil_t Clear;                   /* Buffer clear value */
544 };
545
546 struct tdfx_scissor {
547    FxU32 minX, minY;                    /* Lower left corner */
548    FxU32 maxX, maxY;                    /* Upper right corner */
549 };
550
551 struct tdfx_viewport {
552    GrCoordinateSpaceMode_t Mode;        /* Coordinate space */
553    FxI32 X, Y;                          /* Position */
554    FxI32 Width, Height;                 /* Size */
555    FxFloat Near, Far;                   /* Depth buffer range */
556 };
557
558 struct tdfx_glide {
559    void *State;                         /* Mirror of internal Glide state */
560    GrContext_t Context;                 /* Glide context identifier */
561    FxI32 Board;                         /* Current graphics subsystem */
562    GrColorFormat_t ColorFormat;         /* Framebuffer format */
563    GrOriginLocation_t Origin;           /* Location of screen space origin */
564
565    FxBool Initialized;                  /* Glide initialization done? */
566
567    FxI32 SwapInterval;                  /* SwapBuffers interval */
568    FxI32 MaxPendingSwaps;               /* Maximum outstanding SwapBuffers */
569    FxI32 TextureAlign;
570
571    /* Extensions */
572    FxBool HaveCombineExt;               /* COMBINE */
573    FxBool HaveCommandTransportExt;      /* COMMAND_TRANSPORT */
574    FxBool HaveFogCoordExt;              /* FOGCOORD */
575    FxBool HavePixelExt;                 /* PIXEXT */
576    FxBool HaveTextureBufferExt;         /* TEXTUREBUFFER */
577    FxBool HaveTexFmtExt;                /* TEXFMT */
578    FxBool HaveTexUMAExt;                /* TEXUMA */
579    FxBool HaveMirrorExt;                /* MIRROR */
580    FxBool HaveTexus2;                   /* Texus 2 - FXT1 */
581
582    /* Glide library function pointers */
583    void (*grDrawPoint)( const void *pt );
584    void (*grDrawLine)( const void *v1, const void *v2 );
585    void (*grDrawTriangle)( const void *a, const void *b, const void *c );
586    void (*grVertexLayout)(FxU32 param, FxI32 offset, FxU32 mode);
587    void (*grDrawVertexArray)(FxU32 mode, FxU32 Count, void *pointers);
588    void (*grDrawVertexArrayContiguous)(FxU32 mode, FxU32 Count,
589                                        void *pointers, FxU32 stride);
590    void (*grBufferClear)( GrColor_t color, GrAlpha_t alpha, FxU32 depth );
591    void (*grBufferSwap)( FxU32 swap_interval );
592    void (*grRenderBuffer)( GrBuffer_t buffer );
593    void (*grErrorSetCallback)( GrErrorCallbackFnc_t fnc );
594    void (*grFinish)(void);
595    void (*grFlush)(void);
596    GrContext_t (*grSstWinOpen)(FxU32                hWnd,
597                                GrScreenResolution_t screen_resolution,
598                                GrScreenRefresh_t    refresh_rate,
599                                GrColorFormat_t      color_format,
600                                GrOriginLocation_t   origin_location,
601                                int                  nColBuffers,
602                                int                  nAuxBuffers);
603    void (*grSstWinClose)( GrContext_t context );
604 /* Not used */
605 #if 0
606    void (*grSetNumPendingBuffers)(FxI32 NumPendingBuffers);
607 #endif
608    void (*grSelectContext)( GrContext_t context );
609    void (*grSstOrigin)(GrOriginLocation_t  origin);
610    void (*grSstSelect)( int which_sst );
611    void (*grAlphaBlendFunction)(GrAlphaBlendFnc_t rgb_sf,
612                                 GrAlphaBlendFnc_t rgb_df,
613                                 GrAlphaBlendFnc_t alpha_sf,
614                                 GrAlphaBlendFnc_t alpha_df);
615    void (*grAlphaCombine)(GrCombineFunction_t function,
616                           GrCombineFactor_t factor,
617                           GrCombineLocal_t local, GrCombineOther_t other,
618                           FxBool invert);
619    void (*grAlphaControlsITRGBLighting)( FxBool enable );
620    void (*grAlphaTestFunction)( GrCmpFnc_t function );
621    void (*grAlphaTestReferenceValue)( GrAlpha_t value );
622    void (*grChromakeyMode)( GrChromakeyMode_t mode );
623    void (*grChromakeyValue)( GrColor_t value );
624    void (*grClipWindow)( FxU32 minx, FxU32 miny, FxU32 maxx, FxU32 maxy );
625    void (*grColorCombine)( GrCombineFunction_t function,
626                            GrCombineFactor_t factor,
627                            GrCombineLocal_t local,
628                            GrCombineOther_t other,
629                            FxBool invert );
630    void (*grColorMask)( FxBool rgb, FxBool a );
631    void (*grCullMode)( GrCullMode_t mode );
632    void (*grConstantColorValue)( GrColor_t value );
633    void (*grDepthBiasLevel)( FxI32 level );
634    void (*grDepthBufferFunction)( GrCmpFnc_t function );
635    void (*grDepthBufferMode)( GrDepthBufferMode_t mode );
636    void (*grDepthMask)( FxBool mask );
637    void (*grDisableAllEffects)( void );
638    void (*grDitherMode)( GrDitherMode_t mode );
639    void (*grFogColorValue)( GrColor_t fogcolor );
640    void (*grFogMode)( GrFogMode_t mode );
641    void (*grFogTable)( const GrFog_t ft[] );
642    void (*grLoadGammaTable)( FxU32 nentries, FxU32 *red, FxU32 *green, FxU32 *blue);
643    void (*grSplash)(float x, float y, float width, float height, FxU32 frame);
644    FxU32 (*grGet)( FxU32 pname, FxU32 plength, FxI32 *params );
645    const char * (*grGetString)( FxU32 pname );
646    FxI32 (*grQueryResolutions)( const GrResolution *resTemplate,
647                                 GrResolution *output );
648    FxBool (*grReset)( FxU32 what );
649    GrProc (*grGetProcAddress)( char *procName );
650    void (*grEnable)( GrEnableMode_t mode );
651    void (*grDisable)( GrEnableMode_t mode );
652    void (*grCoordinateSpace)( GrCoordinateSpaceMode_t mode );
653    void (*grDepthRange)( FxFloat n, FxFloat f );
654    void (*grStippleMode)( GrStippleMode_t mode );
655    void (*grStipplePattern)( GrStipplePattern_t mode );
656    void (*grViewport)( FxI32 x, FxI32 y, FxI32 width, FxI32 height );
657    FxU32 (*grTexCalcMemRequired)(GrLOD_t lodmin, GrLOD_t lodmax,
658                                 GrAspectRatio_t aspect, GrTextureFormat_t fmt);
659    FxU32 (*grTexTextureMemRequired)( FxU32 evenOdd, GrTexInfo *info );
660    FxU32 (*grTexMinAddress)( GrChipID_t tmu );
661    FxU32 (*grTexMaxAddress)( GrChipID_t tmu );
662    void (*grTexNCCTable)( GrNCCTable_t table );
663    void (*grTexSource)( GrChipID_t tmu, FxU32 startAddress,
664                         FxU32 evenOdd, GrTexInfo *info );
665    void (*grTexClampMode)( GrChipID_t tmu,
666                            GrTextureClampMode_t s_clampmode,
667                            GrTextureClampMode_t t_clampmode );
668    void (*grTexCombine)( GrChipID_t tmu,
669                          GrCombineFunction_t rgb_function,
670                          GrCombineFactor_t rgb_factor, 
671                          GrCombineFunction_t alpha_function,
672                          GrCombineFactor_t alpha_factor,
673                          FxBool rgb_invert,
674                          FxBool alpha_invert);
675    void (*grTexDetailControl)( GrChipID_t tmu, int lod_bias,
676                                FxU8 detail_scale, float detail_max );
677    void (*grTexFilterMode)( GrChipID_t tmu,
678                             GrTextureFilterMode_t minfilter_mode,
679                             GrTextureFilterMode_t magfilter_mode );
680    void (*grTexLodBiasValue)(GrChipID_t tmu, float bias );
681    void (*grTexDownloadMipMap)( GrChipID_t tmu, FxU32 startAddress,
682                                 FxU32 evenOdd, GrTexInfo *info );
683    void (*grTexDownloadMipMapLevel)( GrChipID_t        tmu,
684                                      FxU32             startAddress,
685                                      GrLOD_t           thisLod,
686                                      GrLOD_t           largeLod,
687                                      GrAspectRatio_t   aspectRatio,
688                                      GrTextureFormat_t format,
689                                      FxU32             evenOdd,
690                                      void              *data );
691    FxBool (*grTexDownloadMipMapLevelPartial)( GrChipID_t        tmu,
692                                               FxU32             startAddress,
693                                               GrLOD_t           thisLod,
694                                               GrLOD_t           largeLod,
695                                               GrAspectRatio_t   aspectRatio,
696                                               GrTextureFormat_t format,
697                                               FxU32             evenOdd,
698                                               void              *data,
699                                               int               start,
700                                               int               end );
701    void (*grTexDownloadTable)( GrTexTable_t type, void *data );
702    void (*grTexDownloadTablePartial)( GrTexTable_t type, 
703                                       void *data, int start, int end );
704    void (*grTexMipMapMode)( GrChipID_t tmu, GrMipMapMode_t mode,
705                             FxBool lodBlend );
706    void (*grTexMultibase)( GrChipID_t tmu, FxBool enable );
707    void (*grTexMultibaseAddress)( GrChipID_t       tmu,
708                                   GrTexBaseRange_t range,
709                                   FxU32            startAddress,
710                                   FxU32            evenOdd,
711                                   GrTexInfo        *info );
712    FxBool (*grLfbLock)( GrLock_t type, GrBuffer_t buffer,
713                         GrLfbWriteMode_t writeMode,
714                         GrOriginLocation_t origin, FxBool pixelPipeline, 
715                         GrLfbInfo_t *info );
716    FxBool (*grLfbUnlock)( GrLock_t type, GrBuffer_t buffer );
717    void (*grLfbConstantAlpha)( GrAlpha_t alpha );
718    void (*grLfbConstantDepth)( FxU32 depth );
719    void (*grLfbWriteColorSwizzle)(FxBool swizzleBytes, FxBool swapWords);
720    void (*grLfbWriteColorFormat)(GrColorFormat_t colorFormat);
721    FxBool (*grLfbWriteRegion)( GrBuffer_t dst_buffer, 
722                                FxU32 dst_x, FxU32 dst_y, 
723                                GrLfbSrcFmt_t src_format, 
724                                FxU32 src_width, FxU32 src_height, 
725                                FxBool pixelPipeline,
726                                FxI32 src_stride, void *src_data );
727    FxBool (*grLfbReadRegion)( GrBuffer_t src_buffer,
728                               FxU32 src_x, FxU32 src_y,
729                               FxU32 src_width, FxU32 src_height,
730                               FxU32 dst_stride, void *dst_data );
731    void (*grGlideInit)( void );
732    void (*grGlideShutdown)( void );
733    void (*grGlideGetState)( void *state );
734    void (*grGlideSetState)( const void *state );
735    void (*grGlideGetVertexLayout)( void *layout );
736    void (*grGlideSetVertexLayout)( const void *layout );
737    /* Glide utility functions */
738    void (*guFogGenerateExp)( GrFog_t *fogtable, float density );
739    void (*guFogGenerateExp2)( GrFog_t *fogtable, float density );
740    void (*guFogGenerateLinear)(GrFog_t *fogtable, float nearZ, float farZ );
741    /* DRI functions */
742    void (*grDRIOpen)( char *pFB, char *pRegs, int deviceID,
743                       int width, int height,
744                       int mem, int cpp, int stride,
745                       int fifoOffset, int fifoSize,
746                       int fbOffset, int backOffset, int depthOffset,
747                       int textureOffset, int textureSize,
748                       volatile int *fifoPtr, volatile int *fifoRead );
749    void (*grDRIPosition)( int x, int y, int w, int h,
750                           int numClip, drm_clip_rect_t *pClip );
751    void (*grDRILostContext)( void );
752    void (*grDRIImportFifo)( int fifoPtr, int fifoRead );
753    void (*grDRIInvalidateAll)( void );
754    void (*grDRIResetSAREA)( void );
755    void (*grDRIBufferSwap)( FxU32 swapInterval );
756    /* Glide extensions */
757    /* PIXEXT extension */
758    void (*grStencilFunc)( GrCmpFnc_t func, GrStencil_t ref, GrStencil_t mask );
759    void (*grStencilMask)( GrStencil_t mask );
760    void (*grStencilOp)( GrStencilOp_t fail, GrStencilOp_t zfail,
761                         GrStencilOp_t zpass );
762    void (*grBufferClearExt)( GrColor_t color, GrAlpha_t alpha,
763                              FxU32 depth, GrStencil_t stencil );
764    void (*grColorMaskExt)( FxBool r, FxBool g, FxBool b, FxBool a );
765    /* COMBINE extension */
766    void (*grColorCombineExt)( GrCCUColor_t a, GrCombineMode_t a_mode,
767                               GrCCUColor_t b, GrCombineMode_t b_mode,
768                               GrCCUColor_t c, FxBool c_invert,
769                               GrCCUColor_t d, FxBool d_invert,
770                               FxU32 shift, FxBool invert );
771    void (*grTexColorCombineExt)( FxU32 tmu,
772                                  GrTCCUColor_t a, GrCombineMode_t a_mode,
773                                  GrTCCUColor_t b, GrCombineMode_t b_mode,
774                                  GrTCCUColor_t c, FxBool c_invert,
775                                  GrTCCUColor_t d, FxBool d_invert,
776                                  FxU32 shift, FxBool invert );
777    void (*grAlphaCombineExt)( GrACUColor_t a, GrCombineMode_t a_mode,
778                               GrACUColor_t b, GrCombineMode_t b_mode,
779                               GrACUColor_t c, FxBool c_invert,
780                               GrACUColor_t d, FxBool d_invert,
781                               FxU32 shift, FxBool invert );
782    void (*grTexAlphaCombineExt)( FxU32 tmu,
783                                  GrTACUColor_t a, GrCombineMode_t a_mode,
784                                  GrTACUColor_t b, GrCombineMode_t b_mode,
785                                  GrTACUColor_t c, FxBool c_invert,
786                                  GrTACUColor_t d, FxBool d_invert,
787                                  FxU32 shift, FxBool invert );
788    void (*grAlphaBlendFunctionExt)( GrAlphaBlendFnc_t rgb_sf,
789                                     GrAlphaBlendFnc_t rgb_df,
790                                     GrAlphaBlendOp_t rgb_op,
791                                     GrAlphaBlendFnc_t alpha_sf,
792                                     GrAlphaBlendFnc_t alpha_df,
793                                     GrAlphaBlendOp_t alpha_op );
794    void (*grConstantColorValueExt)( FxU32 tmu, GrColor_t value );
795    /* Texus 2 */
796    void (*txImgQuantize)( void *xxx_unknown_arguments );
797    void (*txImgDequantizeFXT1)( void *txMip, void *pxMip );
798    void (*txErrorSetCallback)( void *fnc );
799 };
800
801 typedef void (*tdfx_tri_func)( tdfxContextPtr, tdfxVertex *, tdfxVertex *,
802                                tdfxVertex * );
803 typedef void (*tdfx_line_func)( tdfxContextPtr, tdfxVertex *, tdfxVertex * );
804 typedef void (*tdfx_point_func)( tdfxContextPtr, tdfxVertex * );
805
806 struct tdfx_context {
807    /* Set once and never changed:
808     */
809    struct gl_context *glCtx;                    /* The core Mesa context */
810
811    GLuint new_gl_state;
812    GLuint new_state;
813    GLuint dirty;
814
815    /* Mirror of hardware state, Glide parameters
816     */
817    GLuint tmu_source[TDFX_NUM_TMU];
818    struct tdfx_texsource        TexSource[TDFX_NUM_TMU];
819    struct tdfx_texparams        TexParams[TDFX_NUM_TMU];
820    struct tdfx_texpalette       TexPalette;
821
822    /* Voodoo3 texture/color combine state */
823    struct tdfx_combine          ColorCombine;
824    struct tdfx_combine          AlphaCombine;
825    struct tdfx_texcombine       TexCombine[TDFX_NUM_TMU];
826
827    /* Voodoo5 texture/color combine state */
828    struct tdfx_combine_color_ext        ColorCombineExt;
829    struct tdfx_combine_alpha_ext        AlphaCombineExt;
830    struct tdfx_texcombine_ext           TexCombineExt[TDFX_NUM_TMU];
831
832    /* Tracks tex state difference between Glide and Mesa */
833    struct tdfx_texstate         TexState;
834
835    GrBuffer_t           DrawBuffer;     /* Current draw buffer */
836    GrBuffer_t           ReadBuffer;     /* Current read buffer */
837
838    struct tdfx_color    Color;
839    struct tdfx_depth    Depth;
840    struct tdfx_fog      Fog;
841    struct tdfx_stencil  Stencil;
842    struct tdfx_scissor  Scissor;
843    struct tdfx_viewport Viewport;
844    struct tdfx_stipple  Stipple;
845
846    GrCullMode_t         CullMode;
847
848    struct tdfx_glide    Glide;
849
850    /* Fallback rasterization functions 
851     */
852    tdfx_point_func draw_point;
853    tdfx_line_func draw_line;
854    tdfx_tri_func draw_triangle;
855
856
857    /* Variable-size Glide vertex formats
858     */
859    GLuint vertexFormat;         /* the current format */
860    void *layout[TDFX_NUM_LAYOUTS];
861    tdfxVertex *verts;
862    
863    GLfloat hw_viewport[16];
864    
865    GLuint SetupIndex;
866    GLuint SetupNewInputs;
867    GLuint RenderIndex;
868    GLuint Fallback;
869    GLenum render_primitive;     /* what GL thinks */
870    GLenum raster_primitive;     /* what the hardware thinks */
871
872    GLfloat sScale0, tScale0;
873    GLfloat sScale1, tScale1;
874
875    GLuint texBindNumber;
876    GLint tmuSrc;
877
878    int screen_width;
879    int screen_height;
880
881    GLboolean haveTwoTMUs;      /* True if we have 2 tmu's  */
882    GLboolean haveHwAlpha;
883    GLboolean haveHwStencil;
884    GLboolean haveHwStipple;
885
886    GLint maxPendingSwapBuffers;
887
888    char rendererString[100];
889
890    /* stuff added for DRI */
891    __DRIscreen *driScreen;
892    __DRIcontext *driContext;
893
894    /**
895     * DRI drawable bound to this context for drawing.
896     */
897    __DRIdrawable        *driDrawable;
898
899    /**
900     * DRI drawable bound to this context for reading.
901     */
902    __DRIdrawable        *driReadable;
903
904    drm_context_t hHWContext;
905    drm_hw_lock_t *driHwLock;
906    int driFd;
907    tdfxScreenPrivate *fxScreen;
908    TDFXSAREAPriv *sarea;
909
910
911    /*
912     * Changes during execution:
913     */
914    int width, height;   /* size of window */
915    int x_offset;        /* distance from window left to screen left */
916    int y_offset;        /* distance from window top to screen top */
917    int y_delta;         /* distance from window bottom to screen bottom */
918
919    int numClipRects;
920    drm_clip_rect_t *pClipRects;
921    GLboolean scissoredClipRects;  /* if true, pClipRects is private storage */
922
923    GuTexPalette glbPalette;         /* global texture palette */
924
925    tdfxStats stats;
926
927    /* Configuration cache
928     */
929    driOptionCache optionCache;
930 };
931
932 #define TDFX_CONTEXT(ctx)       ((tdfxContextPtr)((ctx)->DriverCtx))
933
934
935 extern GLboolean
936 tdfxCreateContext( gl_api api,
937                    const struct gl_config *mesaVis,
938                    __DRIcontext *driContextPriv,
939                    void *sharedContextPrivate );
940
941 extern void
942 tdfxDestroyContext( __DRIcontext *driContextPriv );
943
944 extern GLboolean
945 tdfxUnbindContext( __DRIcontext *driContextPriv );
946
947 extern GLboolean
948 tdfxMakeCurrent( __DRIcontext *driContextPriv,
949                  __DRIdrawable *driDrawPriv,
950                  __DRIdrawable *driReadPriv );
951
952 extern GLboolean
953 tdfxInitGlide( tdfxContextPtr tmesa );
954
955 extern void
956 FX_grColorMaskv(struct gl_context *ctx, const GLboolean rgba[4]);
957
958 extern void
959 FX_grColorMaskv_NoLock(struct gl_context *ctx, const GLboolean rgba[4]);
960
961
962 /* Color packing utilities
963  */
964 #define TDFXPACKCOLOR332( r, g, b )                                        \
965    (((b) & 0xe0) | (((g) & 0xe0) >> 3) | (((r) & 0xc0) >> 6))
966
967 #define TDFXPACKCOLOR1555( r, g, b, a )                                    \
968    ((((r) & 0xf8) << 7) | (((g) & 0xf8) << 2) | (((b) & 0xf8) >> 3) |      \
969     ((a) ? 0x8000 : 0))
970
971 #define TDFXPACKCOLOR565( r, g, b )                                        \
972    ((((r) & 0xf8) << 8) | (((g) & 0xfc) << 3) | (((b) & 0xf8) >> 3))
973
974 #define TDFXPACKCOLOR888( r, g, b )                                        \
975    (((b) << 16) | ((g) << 8) | (r))
976
977 #define TDFXPACKCOLOR8888( r, g, b, a )                                    \
978    (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
979
980 #define TDFXPACKCOLOR4444( r, g, b, a )                                    \
981    ((((a) & 0xf0) << 8) | (((b) & 0xf0) << 4) | ((g) & 0xf0) | ((r) >> 4))
982
983 static INLINE GrColor_t tdfxPackColor( GLuint cpp,
984                                        GLubyte r, GLubyte g,
985                                        GLubyte b, GLubyte a )
986 {
987    switch ( cpp ) {
988    case 2:
989       return TDFXPACKCOLOR565( r, g, b );
990    case 4:
991       return TDFXPACKCOLOR8888( r, g, b, a );
992    default:
993       return 0;
994   }
995 }
996
997 #define DO_DEBUG                1
998 #if DO_DEBUG
999 extern int TDFX_DEBUG;
1000 #else
1001 #define TDFX_DEBUG              0
1002 #endif
1003
1004 #define DEBUG_ALWAYS_SYNC       0x01
1005 #define DEBUG_VERBOSE_API       0x02
1006 #define DEBUG_VERBOSE_DRI       0x04
1007 #define DEBUG_VERBOSE_FALL      0x08
1008
1009 /* conf */
1010 #define FX_COMPRESS_S3TC_AS_FXT1_HACK 1
1011 #define FX_TC_NAPALM 0
1012
1013 #endif /* __TDFX_CONTEXT_H__ */