Imported the Savage DRI driver from the savage-2-0-0-branch of DRI CVS
[mesa.git] / src / mesa / drivers / dri / tdfx / tdfx_context.c
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 /* $XFree86: xc/lib/GL/mesa/src/drv/tdfx/tdfx_context.c,v 1.12 2003/05/08 09:25:35 herrb Exp $ */
27
28 /*
29 * Original rewrite:
30 * Gareth Hughes <gareth@valinux.com>, 29 Sep - 1 Oct 2000
31 *
32 * Authors:
33 * Gareth Hughes <gareth@valinux.com>
34 * Brian Paul <brianp@valinux.com>
35 *
36 */
37
38 #include <dlfcn.h>
39 #include "tdfx_context.h"
40 #include "tdfx_dd.h"
41 #include "tdfx_state.h"
42 #include "tdfx_vb.h"
43 #include "tdfx_tex.h"
44 #include "tdfx_tris.h"
45 #include "tdfx_render.h"
46 #include "tdfx_span.h"
47 #include "tdfx_texman.h"
48 #include "extensions.h"
49
50 #include "swrast/swrast.h"
51 #include "swrast_setup/swrast_setup.h"
52 #include "array_cache/acache.h"
53
54 #include "tnl/tnl.h"
55 #include "tnl/t_pipeline.h"
56
57 #include "drivers/common/driverfuncs.h"
58
59 const char __driConfigOptions[] = { 0 };
60 const GLuint __driNConfigOptions = 0;
61
62 /*
63 * Enable/Disable the extensions for this context.
64 */
65 static void tdfxDDInitExtensions( GLcontext *ctx )
66 {
67 tdfxContextPtr fxMesa = TDFX_CONTEXT(ctx);
68
69 _mesa_enable_extension( ctx, "GL_HP_occlusion_test" );
70 _mesa_enable_extension( ctx, "GL_EXT_paletted_texture" );
71 _mesa_enable_extension( ctx, "GL_EXT_texture_lod_bias" );
72
73 if ( fxMesa->haveTwoTMUs ) {
74 _mesa_enable_extension( ctx, "GL_EXT_texture_env_add" );
75 _mesa_enable_extension( ctx, "GL_ARB_multitexture" );
76 }
77
78 if ( TDFX_IS_NAPALM( fxMesa ) ) {
79 #if 0
80 _mesa_enable_extension( ctx, "GL_ARB_texture_compression" );
81 _mesa_enable_extension( ctx, "GL_3DFX_texture_compression_FXT1" );
82 #endif
83 _mesa_enable_extension( ctx, "GL_EXT_texture_env_combine" );
84 }
85
86 #if 0
87 _mesa_enable_extension( ctx, "GL_ARB_texture_cube_map");
88 _mesa_enable_extension( ctx, "GL_NV_texture_rectangle");
89 #endif
90
91 if (fxMesa->haveHwStencil) {
92 _mesa_enable_extension( ctx, "GL_EXT_stencil_wrap" );
93 }
94 }
95
96
97
98 static const struct tnl_pipeline_stage *tdfx_pipeline[] = {
99 &_tnl_vertex_transform_stage,
100 &_tnl_normal_transform_stage,
101 &_tnl_lighting_stage, /* REMOVE: fog coord stage */
102 &_tnl_texgen_stage,
103 &_tnl_texture_transform_stage,
104 /* REMOVE: point attenuation stage */
105 &_tnl_render_stage,
106 0,
107 };
108
109
110 GLboolean tdfxCreateContext( const __GLcontextModes *mesaVis,
111 __DRIcontextPrivate *driContextPriv,
112 void *sharedContextPrivate )
113 {
114 tdfxContextPtr fxMesa;
115 GLcontext *ctx, *shareCtx;
116 __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
117 tdfxScreenPrivate *fxScreen = (tdfxScreenPrivate *) sPriv->private;
118 TDFXSAREAPriv *saPriv = (TDFXSAREAPriv *) ((char *) sPriv->pSAREA +
119 sizeof(XF86DRISAREARec));
120 struct dd_function_table functions;
121
122 /* Allocate tdfx context */
123 fxMesa = (tdfxContextPtr) CALLOC( sizeof(tdfxContextRec) );
124 if (!fxMesa)
125 return GL_FALSE;
126
127 /* Init default driver functions then plug in our tdfx-specific functions
128 * (the texture functions are especially important)
129 */
130 _mesa_init_driver_functions(&functions);
131 tdfxDDInitDriverFuncs(mesaVis, &functions);
132 tdfxInitTextureFuncs(&functions);
133 tdfxInitRenderFuncs(&functions);
134
135 /* Allocate the Mesa context */
136 if (sharedContextPrivate)
137 shareCtx = ((tdfxContextPtr) sharedContextPrivate)->glCtx;
138 else
139 shareCtx = NULL;
140
141 fxMesa->glCtx = _mesa_create_context(mesaVis, shareCtx,
142 &functions, (void *) fxMesa);
143 if (!fxMesa->glCtx) {
144 FREE(fxMesa);
145 return GL_FALSE;
146 }
147 driContextPriv->driverPrivate = fxMesa;
148
149 /* Mirror some important DRI state
150 */
151 fxMesa->hHWContext = driContextPriv->hHWContext;
152 fxMesa->driHwLock = &sPriv->pSAREA->lock;
153 fxMesa->driFd = sPriv->fd;
154
155 fxMesa->driScreen = sPriv;
156 fxMesa->driContext = driContextPriv;
157 fxMesa->fxScreen = fxScreen;
158 fxMesa->sarea = saPriv;
159
160 fxMesa->haveHwStencil = ( TDFX_IS_NAPALM( fxMesa ) &&
161 mesaVis->stencilBits &&
162 mesaVis->depthBits == 24 );
163
164 fxMesa->screen_width = fxScreen->width;
165 fxMesa->screen_height = fxScreen->height;
166
167 fxMesa->new_gl_state = ~0;
168 fxMesa->new_state = ~0;
169 fxMesa->dirty = ~0;
170
171 /* NOTE: This must be here before any Glide calls! */
172 if (!tdfxInitGlide( fxMesa )) {
173 FREE(fxMesa);
174 return GL_FALSE;
175 }
176
177 fxMesa->Glide.grDRIOpen( (char*) sPriv->pFB, fxScreen->regs.map, fxScreen->deviceID,
178 fxScreen->width, fxScreen->height, fxScreen->mem, fxScreen->cpp,
179 fxScreen->stride, fxScreen->fifoOffset, fxScreen->fifoSize,
180 fxScreen->fbOffset, fxScreen->backOffset, fxScreen->depthOffset,
181 fxScreen->textureOffset, fxScreen->textureSize, &saPriv->fifoPtr,
182 &saPriv->fifoRead );
183
184 if ( getenv( "FX_GLIDE_SWAPINTERVAL" ) ) {
185 fxMesa->Glide.SwapInterval = atoi( getenv( "FX_GLIDE_SWAPINTERVAL" ) );
186 } else {
187 fxMesa->Glide.SwapInterval = 0;
188 }
189 if ( getenv( "FX_MAX_PENDING_SWAPS" ) ) {
190 fxMesa->Glide.MaxPendingSwaps = atoi( getenv( "FX_MAX_PENDING_SWAPS" ) );
191 } else {
192 fxMesa->Glide.MaxPendingSwaps = 2;
193 }
194
195 fxMesa->Glide.Initialized = GL_FALSE;
196 fxMesa->Glide.Board = 0;
197
198
199 if (getenv("FX_EMULATE_SINGLE_TMU")) {
200 fxMesa->haveTwoTMUs = GL_FALSE;
201 }
202 else {
203 if ( TDFX_IS_BANSHEE( fxMesa ) ) {
204 fxMesa->haveTwoTMUs = GL_FALSE;
205 } else {
206 fxMesa->haveTwoTMUs = GL_TRUE;
207 }
208 }
209
210 fxMesa->stats.swapBuffer = 0;
211 fxMesa->stats.reqTexUpload = 0;
212 fxMesa->stats.texUpload = 0;
213 fxMesa->stats.memTexUpload = 0;
214
215 fxMesa->tmuSrc = TDFX_TMU_NONE;
216
217 ctx = fxMesa->glCtx;
218 if ( TDFX_IS_NAPALM( fxMesa ) ) {
219 ctx->Const.MaxTextureLevels = 12;
220 } else {
221 ctx->Const.MaxTextureLevels = 9;
222 }
223 ctx->Const.MaxTextureUnits = TDFX_IS_BANSHEE( fxMesa ) ? 1 : 2;
224 ctx->Const.MaxTextureImageUnits = ctx->Const.MaxTextureUnits;
225 ctx->Const.MaxTextureCoordUnits = ctx->Const.MaxTextureUnits;
226
227 /* No wide points.
228 */
229 ctx->Const.MinPointSize = 1.0;
230 ctx->Const.MinPointSizeAA = 1.0;
231 ctx->Const.MaxPointSize = 1.0;
232 ctx->Const.MaxPointSizeAA = 1.0;
233
234 /* Disable wide lines as we can't antialias them correctly in
235 * hardware.
236 */
237 ctx->Const.MinLineWidth = 1.0;
238 ctx->Const.MinLineWidthAA = 1.0;
239 ctx->Const.MaxLineWidth = 1.0;
240 ctx->Const.MaxLineWidthAA = 1.0;
241 ctx->Const.LineWidthGranularity = 1.0;
242
243 /* Initialize the software rasterizer and helper modules.
244 */
245 _swrast_CreateContext( ctx );
246 _ac_CreateContext( ctx );
247 _tnl_CreateContext( ctx );
248 _swsetup_CreateContext( ctx );
249
250 /* Install the customized pipeline:
251 */
252 _tnl_destroy_pipeline( ctx );
253 _tnl_install_pipeline( ctx, tdfx_pipeline );
254
255 /* Configure swrast to match hardware characteristics:
256 */
257 _swrast_allow_pixel_fog( ctx, GL_TRUE );
258 _swrast_allow_vertex_fog( ctx, GL_FALSE );
259
260 tdfxDDInitExtensions( ctx );
261 /* XXX these should really go right after _mesa_init_driver_functions() */
262 tdfxDDInitSpanFuncs( ctx );
263 tdfxDDInitStateFuncs( ctx );
264 tdfxDDInitTriFuncs( ctx );
265 tdfxInitVB( ctx );
266 tdfxInitState( fxMesa );
267
268 return GL_TRUE;
269 }
270
271
272 static GLboolean tdfxInitVertexFormats( tdfxContextPtr fxMesa )
273 {
274 FxI32 result;
275 int i;
276
277 LOCK_HARDWARE( fxMesa );
278
279 fxMesa->Glide.grGet( GR_GLIDE_VERTEXLAYOUT_SIZE, sizeof(FxI32), &result );
280 for ( i = 0 ; i < TDFX_NUM_LAYOUTS ; i++ ) {
281 fxMesa->layout[i] = MALLOC( result );
282 if ( !fxMesa->layout[i] ) {
283 UNLOCK_HARDWARE( fxMesa );
284 return GL_FALSE;
285 }
286 }
287
288 /* Tiny vertex format - 16 bytes.
289 */
290 fxMesa->Glide.grReset( GR_VERTEX_PARAMETER );
291 fxMesa->Glide.grCoordinateSpace( GR_WINDOW_COORDS );
292 fxMesa->Glide.grVertexLayout( GR_PARAM_XY, TDFX_XY_OFFSET, GR_PARAM_ENABLE );
293 fxMesa->Glide.grVertexLayout( GR_PARAM_Z, TDFX_Z_OFFSET, GR_PARAM_ENABLE );
294 fxMesa->Glide.grVertexLayout( GR_PARAM_PARGB, TDFX_Q_OFFSET, GR_PARAM_ENABLE );
295 fxMesa->Glide.grGlideGetVertexLayout( fxMesa->layout[TDFX_LAYOUT_TINY] );
296
297 /* Non textured vertex format - 24 bytes (Need w for table fog)
298 */
299 fxMesa->Glide.grReset( GR_VERTEX_PARAMETER );
300 fxMesa->Glide.grCoordinateSpace( GR_WINDOW_COORDS );
301 fxMesa->Glide.grVertexLayout( GR_PARAM_XY, TDFX_XY_OFFSET, GR_PARAM_ENABLE );
302 fxMesa->Glide.grVertexLayout( GR_PARAM_Z, TDFX_Z_OFFSET, GR_PARAM_ENABLE );
303 fxMesa->Glide.grVertexLayout( GR_PARAM_Q, TDFX_Q_OFFSET, GR_PARAM_ENABLE );
304 fxMesa->Glide.grVertexLayout( GR_PARAM_PARGB, TDFX_ARGB_OFFSET, GR_PARAM_ENABLE );
305 fxMesa->Glide.grGlideGetVertexLayout( fxMesa->layout[TDFX_LAYOUT_NOTEX] );
306
307 /* Single textured vertex format - 32 bytes.
308 */
309 fxMesa->Glide.grReset( GR_VERTEX_PARAMETER );
310 fxMesa->Glide.grCoordinateSpace( GR_WINDOW_COORDS );
311 fxMesa->Glide.grVertexLayout( GR_PARAM_XY, TDFX_XY_OFFSET, GR_PARAM_ENABLE );
312 fxMesa->Glide.grVertexLayout( GR_PARAM_Z, TDFX_Z_OFFSET, GR_PARAM_ENABLE );
313 fxMesa->Glide.grVertexLayout( GR_PARAM_Q, TDFX_Q_OFFSET, GR_PARAM_ENABLE );
314 fxMesa->Glide.grVertexLayout( GR_PARAM_PARGB, TDFX_ARGB_OFFSET, GR_PARAM_ENABLE );
315 fxMesa->Glide.grVertexLayout( GR_PARAM_ST0, TDFX_ST0_OFFSET, GR_PARAM_ENABLE );
316 /*grVertexLayout( GR_PARAM_FOG_EXT, TDFX_FOG_OFFSET, GR_PARAM_ENABLE );*/
317 fxMesa->Glide.grGlideGetVertexLayout( fxMesa->layout[TDFX_LAYOUT_SINGLE] );
318
319 /* Multitextured vertex format - 40 bytes.
320 */
321 fxMesa->Glide.grReset( GR_VERTEX_PARAMETER );
322 fxMesa->Glide.grCoordinateSpace( GR_WINDOW_COORDS );
323 fxMesa->Glide.grVertexLayout( GR_PARAM_XY, TDFX_XY_OFFSET, GR_PARAM_ENABLE );
324 fxMesa->Glide.grVertexLayout( GR_PARAM_Z, TDFX_Z_OFFSET, GR_PARAM_ENABLE );
325 fxMesa->Glide.grVertexLayout( GR_PARAM_Q, TDFX_Q_OFFSET, GR_PARAM_ENABLE );
326 fxMesa->Glide.grVertexLayout( GR_PARAM_PARGB, TDFX_ARGB_OFFSET, GR_PARAM_ENABLE );
327 fxMesa->Glide.grVertexLayout( GR_PARAM_ST0, TDFX_ST0_OFFSET, GR_PARAM_ENABLE );
328 fxMesa->Glide.grVertexLayout( GR_PARAM_ST1, TDFX_ST1_OFFSET, GR_PARAM_ENABLE );
329 /*fxMesa->Glide.grVertexLayout( GR_PARAM_FOG_EXT, TDFX_FOG_OFFSET, GR_PARAM_ENABLE );*/
330 fxMesa->Glide.grGlideGetVertexLayout( fxMesa->layout[TDFX_LAYOUT_MULTI] );
331
332 /* Projected texture vertex format - 48 bytes.
333 */
334 fxMesa->Glide.grReset( GR_VERTEX_PARAMETER );
335 fxMesa->Glide.grCoordinateSpace( GR_WINDOW_COORDS );
336 fxMesa->Glide.grVertexLayout( GR_PARAM_XY, TDFX_XY_OFFSET, GR_PARAM_ENABLE );
337 fxMesa->Glide.grVertexLayout( GR_PARAM_Z, TDFX_Z_OFFSET, GR_PARAM_ENABLE );
338 fxMesa->Glide.grVertexLayout( GR_PARAM_Q, TDFX_Q_OFFSET, GR_PARAM_ENABLE );
339 fxMesa->Glide.grVertexLayout( GR_PARAM_PARGB, TDFX_ARGB_OFFSET, GR_PARAM_ENABLE );
340 fxMesa->Glide.grVertexLayout( GR_PARAM_ST0, TDFX_ST0_OFFSET, GR_PARAM_ENABLE );
341 fxMesa->Glide.grVertexLayout( GR_PARAM_Q0, TDFX_Q0_OFFSET, GR_PARAM_ENABLE );
342 fxMesa->Glide.grVertexLayout( GR_PARAM_ST1, TDFX_ST1_OFFSET, GR_PARAM_ENABLE );
343 fxMesa->Glide.grVertexLayout( GR_PARAM_Q1, TDFX_Q1_OFFSET, GR_PARAM_ENABLE );
344 /*fxMesa->Glide.grVertexLayout( GR_PARAM_FOG_EXT, TDFX_FOG_OFFSET, GR_PARAM_ENABLE );*/
345 fxMesa->Glide.grGlideGetVertexLayout( fxMesa->layout[TDFX_LAYOUT_PROJECT] );
346
347 UNLOCK_HARDWARE( fxMesa );
348
349 return GL_TRUE;
350 }
351
352
353 /*
354 * Initialize the state in an tdfxContextPtr struct.
355 */
356 static GLboolean
357 tdfxInitContext( __DRIdrawablePrivate *driDrawPriv, tdfxContextPtr fxMesa )
358 {
359 /* KW: Would be nice to make one of these a member of the other.
360 */
361 FxI32 result[2];
362
363 if ( TDFX_DEBUG & DEBUG_VERBOSE_DRI ) {
364 fprintf( stderr, "%s( %p )\n", __FUNCTION__, (void *)fxMesa );
365 }
366
367 #if DEBUG_LOCKING
368 fprintf(stderr, "Debug locking enabled\n");
369 #endif
370
371 if ( fxMesa->Glide.Initialized )
372 return GL_TRUE;
373
374 fxMesa->width = driDrawPriv->w;
375 fxMesa->height = driDrawPriv->h;
376
377 /* We have to use a light lock here, because we can't do any glide
378 * operations yet. No use of FX_* functions in this function.
379 */
380 DRM_LIGHT_LOCK( fxMesa->driFd, fxMesa->driHwLock, fxMesa->hHWContext );
381
382 fxMesa->Glide.grGlideInit();
383 fxMesa->Glide.grSstSelect( fxMesa->Glide.Board );
384
385 fxMesa->Glide.Context = fxMesa->Glide.grSstWinOpen( (FxU32) -1,
386 GR_RESOLUTION_NONE,
387 GR_REFRESH_NONE,
388 fxMesa->Glide.ColorFormat,
389 fxMesa->Glide.Origin,
390 2, 1 );
391
392 fxMesa->Glide.grDRIResetSAREA();
393
394 DRM_UNLOCK( fxMesa->driFd, fxMesa->driHwLock, fxMesa->hHWContext );
395
396 if ( !fxMesa->Glide.Context )
397 return GL_FALSE;
398
399
400 /* Perform the Glide-dependant part of the context initialization.
401 */
402 FX_grColorMaskv( fxMesa->glCtx, true4 );
403
404 tdfxTMInit( fxMesa );
405
406 LOCK_HARDWARE( fxMesa );
407
408 if ( fxMesa->glCtx->Visual.depthBits > 0 ) {
409 fxMesa->Glide.grDepthBufferMode(GR_DEPTHBUFFER_ZBUFFER);
410 } else {
411 fxMesa->Glide.grDepthBufferMode(GR_DEPTHBUFFER_DISABLE);
412 }
413
414 fxMesa->Glide.grLfbWriteColorFormat( GR_COLORFORMAT_ABGR );
415
416 fxMesa->Glide.grGet( GR_TEXTURE_ALIGN, sizeof(FxI32), result );
417 fxMesa->Glide.TextureAlign = result[0];
418
419 fxMesa->Glide.State = NULL;
420 fxMesa->Glide.grGet( GR_GLIDE_STATE_SIZE, sizeof(FxI32), result );
421 fxMesa->Glide.State = MALLOC( result[0] );
422
423 fxMesa->Fog.Table = NULL;
424 fxMesa->Glide.grGet( GR_FOG_TABLE_ENTRIES, sizeof(FxI32), result );
425 fxMesa->Fog.Table = MALLOC( result[0] * sizeof(GrFog_t) );
426
427 UNLOCK_HARDWARE( fxMesa );
428
429 if ( !fxMesa->Glide.State || !fxMesa->Fog.Table ) {
430 if ( fxMesa->Glide.State )
431 FREE( fxMesa->Glide.State );
432 if ( fxMesa->Fog.Table )
433 FREE( fxMesa->Fog.Table );
434 return GL_FALSE;
435 }
436
437 if ( !tdfxInitVertexFormats( fxMesa ) ) {
438 return GL_FALSE;
439 }
440
441 LOCK_HARDWARE( fxMesa );
442
443 fxMesa->Glide.grGlideGetState( fxMesa->Glide.State );
444
445 if ( getenv( "FX_GLIDE_INFO" ) ) {
446 printf( "GR_RENDERER = %s\n", (char *) fxMesa->Glide.grGetString( GR_RENDERER ) );
447 printf( "GR_VERSION = %s\n", (char *) fxMesa->Glide.grGetString( GR_VERSION ) );
448 printf( "GR_VENDOR = %s\n", (char *) fxMesa->Glide.grGetString( GR_VENDOR ) );
449 printf( "GR_HARDWARE = %s\n", (char *) fxMesa->Glide.grGetString( GR_HARDWARE ) );
450 printf( "GR_EXTENSION = %s\n", (char *) fxMesa->Glide.grGetString( GR_EXTENSION ) );
451 }
452
453 UNLOCK_HARDWARE( fxMesa );
454
455 {
456 const char *debug = getenv("LIBGL_DEBUG");
457 if (debug && strstr(debug, "fallbacks")) {
458 fxMesa->debugFallbacks = GL_TRUE;
459 }
460 }
461
462
463 fxMesa->numClipRects = 0;
464 fxMesa->pClipRects = NULL;
465 fxMesa->scissoredClipRects = GL_FALSE;
466
467 fxMesa->Glide.Initialized = GL_TRUE;
468
469 return GL_TRUE;
470 }
471
472
473 void
474 tdfxDestroyContext( __DRIcontextPrivate *driContextPriv )
475 {
476 tdfxContextPtr fxMesa = (tdfxContextPtr) driContextPriv->driverPrivate;
477
478 if ( TDFX_DEBUG & DEBUG_VERBOSE_DRI ) {
479 fprintf( stderr, "%s( %p )\n", __FUNCTION__, (void *)fxMesa );
480 }
481
482 if ( fxMesa ) {
483 if (fxMesa->glCtx->Shared->RefCount == 1 && fxMesa->driDrawable) {
484 /* This share group is about to go away, free our private
485 * texture object data.
486 */
487 struct gl_texture_object *tObj;
488 tObj = fxMesa->glCtx->Shared->TexObjectList;
489 while (tObj) {
490 tdfxTMFreeTexture(fxMesa, tObj);
491 tObj = tObj->Next;
492 }
493 }
494
495 tdfxTMClose(fxMesa); /* free texture memory */
496
497 _swsetup_DestroyContext( fxMesa->glCtx );
498 _tnl_DestroyContext( fxMesa->glCtx );
499 _ac_DestroyContext( fxMesa->glCtx );
500 _swrast_DestroyContext( fxMesa->glCtx );
501
502 tdfxFreeVB( fxMesa->glCtx );
503
504 /* Free Mesa context */
505 fxMesa->glCtx->DriverCtx = NULL;
506 _mesa_destroy_context(fxMesa->glCtx);
507
508 /* free the tdfx context */
509 FREE( fxMesa );
510 }
511 }
512
513
514 GLboolean
515 tdfxUnbindContext( __DRIcontextPrivate *driContextPriv )
516 {
517 GET_CURRENT_CONTEXT(ctx);
518 tdfxContextPtr fxMesa = TDFX_CONTEXT(ctx);
519
520 if ( TDFX_DEBUG & DEBUG_VERBOSE_DRI ) {
521 fprintf( stderr, "%s( %p )\n", __FUNCTION__, (void *)driContextPriv );
522 }
523
524 if ( driContextPriv && (tdfxContextPtr) driContextPriv == fxMesa ) {
525 LOCK_HARDWARE(fxMesa);
526 fxMesa->Glide.grGlideGetState(fxMesa->Glide.State);
527 UNLOCK_HARDWARE(fxMesa);
528 }
529 return GL_TRUE;
530 }
531
532
533 GLboolean
534 tdfxMakeCurrent( __DRIcontextPrivate *driContextPriv,
535 __DRIdrawablePrivate *driDrawPriv,
536 __DRIdrawablePrivate *driReadPriv )
537 {
538 if ( TDFX_DEBUG & DEBUG_VERBOSE_DRI ) {
539 fprintf( stderr, "%s( %p )\n", __FUNCTION__, (void *)driContextPriv );
540 }
541
542 if ( driContextPriv ) {
543 tdfxContextPtr newFx = (tdfxContextPtr) driContextPriv->driverPrivate;
544 GLcontext *newCtx = newFx->glCtx;
545 GET_CURRENT_CONTEXT(curCtx);
546
547 if ( newFx->driDrawable != driDrawPriv ) {
548 newFx->driDrawable = driDrawPriv;
549 newFx->dirty = ~0;
550 }
551 else if (curCtx == newCtx) {
552 /* same drawable, same context -> no-op */
553 /* Need to call _mesa_make_current2() in order to make sure API
554 * dispatch is set correctly.
555 */
556 _mesa_make_current2( newCtx,
557 (GLframebuffer *) driDrawPriv->driverPrivate,
558 (GLframebuffer *) driReadPriv->driverPrivate );
559 return GL_TRUE;
560 }
561
562 if ( !newFx->Glide.Initialized ) {
563 if ( !tdfxInitContext( driDrawPriv, newFx ) )
564 return GL_FALSE;
565
566 LOCK_HARDWARE( newFx );
567
568 /* FIXME: Force loading of window information */
569 newFx->width = 0;
570 tdfxUpdateClipping(newCtx);
571 tdfxUploadClipping(newFx);
572
573 UNLOCK_HARDWARE( newFx );
574 } else {
575 LOCK_HARDWARE( newFx );
576
577 newFx->Glide.grSstSelect( newFx->Glide.Board );
578 newFx->Glide.grGlideSetState( newFx->Glide.State );
579
580 tdfxUpdateClipping(newCtx);
581 tdfxUploadClipping(newFx);
582
583 UNLOCK_HARDWARE( newFx );
584 }
585
586 _mesa_make_current2( newCtx,
587 (GLframebuffer *) driDrawPriv->driverPrivate,
588 (GLframebuffer *) driReadPriv->driverPrivate );
589
590 if ( !newCtx->Viewport.Width ) {
591 _mesa_set_viewport( newCtx, 0, 0, driDrawPriv->w, driDrawPriv->h );
592 }
593 } else {
594 _mesa_make_current( 0, 0 );
595 }
596
597 return GL_TRUE;
598 }
599
600
601 /*
602 * Enable this to trace calls to various Glide functions.
603 */
604 /*#define DEBUG_TRAP*/
605 #ifdef DEBUG_TRAP
606 static void (*real_grDrawTriangle)( const void *a, const void *b, const void *c );
607 static void (*real_grDrawPoint)( const void *a );
608 static void (*real_grDrawVertexArray)(FxU32 mode, FxU32 Count, void *pointers);
609 static void (*real_grDrawVertexArrayContiguous)(FxU32 mode, FxU32 Count,
610 void *pointers, FxU32 stride);
611 static void (*real_grClipWindow)( FxU32 minx, FxU32 miny, FxU32 maxx, FxU32 maxy );
612
613 static void (*real_grVertexLayout)(FxU32 param, FxI32 offset, FxU32 mode);
614 static void (*real_grGlideGetVertexLayout)( void *layout );
615 static void (*real_grGlideSetVertexLayout)( const void *layout );
616
617 static void (*real_grTexDownloadMipMapLevel)( GrChipID_t tmu,
618 FxU32 startAddress,
619 GrLOD_t thisLod,
620 GrLOD_t largeLod,
621 GrAspectRatio_t aspectRatio,
622 GrTextureFormat_t format,
623 FxU32 evenOdd,
624 void *data );
625
626
627 static void debug_grDrawTriangle( const void *a, const void *b, const void *c )
628 {
629 printf("%s\n", __FUNCTION__);
630 (*real_grDrawTriangle)(a, b, c);
631 }
632
633 static void debug_grDrawPoint( const void *a )
634 {
635 const float *f = (const float *) a;
636 printf("%s %g %g\n", __FUNCTION__, f[0], f[1]);
637 (*real_grDrawPoint)(a);
638 }
639
640 static void debug_grDrawVertexArray(FxU32 mode, FxU32 Count, void *pointers)
641 {
642 printf("%s count=%d\n", __FUNCTION__, (int) Count);
643 (*real_grDrawVertexArray)(mode, Count, pointers);
644 }
645
646 static void debug_grDrawVertexArrayContiguous(FxU32 mode, FxU32 Count,
647 void *pointers, FxU32 stride)
648 {
649 printf("%s mode=0x%x count=%d\n", __FUNCTION__, (int) mode, (int) Count);
650 (*real_grDrawVertexArrayContiguous)(mode, Count, pointers, stride);
651 }
652
653 static void debug_grClipWindow( FxU32 minx, FxU32 miny, FxU32 maxx, FxU32 maxy )
654 {
655 printf("%s %d,%d .. %d,%d\n", __FUNCTION__,
656 (int) minx, (int) miny, (int) maxx, (int) maxy);
657 (*real_grClipWindow)(minx, miny, maxx, maxy);
658 }
659
660 static void debug_grVertexLayout(FxU32 param, FxI32 offset, FxU32 mode)
661 {
662 (*real_grVertexLayout)(param, offset, mode);
663 }
664
665 static void debug_grGlideGetVertexLayout( void *layout )
666 {
667 (*real_grGlideGetVertexLayout)(layout);
668 }
669
670 static void debug_grGlideSetVertexLayout( const void *layout )
671 {
672 (*real_grGlideSetVertexLayout)(layout);
673 }
674
675 static void debug_grTexDownloadMipMapLevel( GrChipID_t tmu,
676 FxU32 startAddress,
677 GrLOD_t thisLod,
678 GrLOD_t largeLod,
679 GrAspectRatio_t aspectRatio,
680 GrTextureFormat_t format,
681 FxU32 evenOdd,
682 void *data )
683 {
684 (*real_grTexDownloadMipMapLevel)(tmu, startAddress, thisLod, largeLod,
685 aspectRatio, format, evenOdd, data);
686 }
687
688 #endif
689
690
691 /*
692 * Examine the context's deviceID to determine what kind of 3dfx hardware
693 * is installed. dlopen() the appropriate Glide library and initialize
694 * this context's Glide function pointers.
695 * Return: true/false = success/failure
696 */
697 GLboolean tdfxInitGlide(tdfxContextPtr tmesa)
698 {
699 static const char *defaultGlide = "libglide3.so";
700 const char *libName;
701 void *libHandle;
702
703 /*
704 * XXX this code which selects a Glide library filename given the
705 * deviceID may need to be cleaned up a bit.
706 * Non-Linux systems may have different filenames, for example.
707 */
708 switch (tmesa->fxScreen->deviceID) {
709 case PCI_CHIP_BANSHEE:
710 case PCI_CHIP_VOODOO3:
711 libName = "libglide3-v3.so";
712 break;
713 case PCI_CHIP_VOODOO5: /* same as PCI_CHIP_VOODOO4 */
714 libName = "libglide3-v5.so";
715 break;
716 default:
717 {
718 __driUtilMessage("unrecognized 3dfx deviceID: 0x%x",
719 tmesa->fxScreen->deviceID);
720 }
721 return GL_FALSE;
722 }
723
724 libHandle = dlopen(libName, RTLD_NOW);
725 if (!libHandle) {
726 /* The device-specific Glide library filename didn't work, try the
727 * old, generic libglide3.so library.
728 */
729 libHandle = dlopen(defaultGlide, RTLD_NOW);
730 if (!libHandle) {
731 __driUtilMessage(
732 "can't find Glide library, dlopen(%s) and dlopen(%s) both failed.",
733 libName, defaultGlide);
734 __driUtilMessage("dlerror() message: %s", dlerror());
735 return GL_FALSE;
736 }
737 libName = defaultGlide;
738 }
739
740 {
741 const char *env = getenv("LIBGL_DEBUG");
742 if (env && strstr(env, "verbose")) {
743 fprintf(stderr, "libGL: using Glide library %s\n", libName);
744 }
745 }
746
747 #define GET_FUNCTION(PTR, NAME) \
748 tmesa->Glide.PTR = dlsym(libHandle, NAME); \
749 if (!tmesa->Glide.PTR) { \
750 __driUtilMessage("couldn't find Glide function %s in %s.", \
751 NAME, libName); \
752 }
753
754 GET_FUNCTION(grDrawPoint, "grDrawPoint");
755 GET_FUNCTION(grDrawLine, "grDrawLine");
756 GET_FUNCTION(grDrawTriangle, "grDrawTriangle");
757 GET_FUNCTION(grVertexLayout, "grVertexLayout");
758 GET_FUNCTION(grDrawVertexArray, "grDrawVertexArray");
759 GET_FUNCTION(grDrawVertexArrayContiguous, "grDrawVertexArrayContiguous");
760 GET_FUNCTION(grBufferClear, "grBufferClear");
761 /*GET_FUNCTION(grBufferSwap, "grBufferSwap");*/
762 GET_FUNCTION(grRenderBuffer, "grRenderBuffer");
763 GET_FUNCTION(grErrorSetCallback, "grErrorSetCallback");
764 GET_FUNCTION(grFinish, "grFinish");
765 GET_FUNCTION(grFlush, "grFlush");
766 GET_FUNCTION(grSstWinOpen, "grSstWinOpen");
767 GET_FUNCTION(grSstWinClose, "grSstWinClose");
768 #if 0
769 /* Not in V3 lib, and not used anyway. */
770 GET_FUNCTION(grSetNumPendingBuffers, "grSetNumPendingBuffers");
771 #endif
772 GET_FUNCTION(grSelectContext, "grSelectContext");
773 GET_FUNCTION(grSstOrigin, "grSstOrigin");
774 GET_FUNCTION(grSstSelect, "grSstSelect");
775 GET_FUNCTION(grAlphaBlendFunction, "grAlphaBlendFunction");
776 GET_FUNCTION(grAlphaCombine, "grAlphaCombine");
777 GET_FUNCTION(grAlphaControlsITRGBLighting, "grAlphaControlsITRGBLighting");
778 GET_FUNCTION(grAlphaTestFunction, "grAlphaTestFunction");
779 GET_FUNCTION(grAlphaTestReferenceValue, "grAlphaTestReferenceValue");
780 GET_FUNCTION(grChromakeyMode, "grChromakeyMode");
781 GET_FUNCTION(grChromakeyValue, "grChromakeyValue");
782 GET_FUNCTION(grClipWindow, "grClipWindow");
783 GET_FUNCTION(grColorCombine, "grColorCombine");
784 GET_FUNCTION(grColorMask, "grColorMask");
785 GET_FUNCTION(grCullMode, "grCullMode");
786 GET_FUNCTION(grConstantColorValue, "grConstantColorValue");
787 GET_FUNCTION(grDepthBiasLevel, "grDepthBiasLevel");
788 GET_FUNCTION(grDepthBufferFunction, "grDepthBufferFunction");
789 GET_FUNCTION(grDepthBufferMode, "grDepthBufferMode");
790 GET_FUNCTION(grDepthMask, "grDepthMask");
791 GET_FUNCTION(grDisableAllEffects, "grDisableAllEffects");
792 GET_FUNCTION(grDitherMode, "grDitherMode");
793 GET_FUNCTION(grFogColorValue, "grFogColorValue");
794 GET_FUNCTION(grFogMode, "grFogMode");
795 GET_FUNCTION(grFogTable, "grFogTable");
796 GET_FUNCTION(grLoadGammaTable, "grLoadGammaTable");
797 GET_FUNCTION(grSplash, "grSplash");
798 GET_FUNCTION(grGet, "grGet");
799 GET_FUNCTION(grGetString, "grGetString");
800 GET_FUNCTION(grQueryResolutions, "grQueryResolutions");
801 GET_FUNCTION(grReset, "grReset");
802 GET_FUNCTION(grGetProcAddress, "grGetProcAddress");
803 GET_FUNCTION(grEnable, "grEnable");
804 GET_FUNCTION(grDisable, "grDisable");
805 GET_FUNCTION(grCoordinateSpace, "grCoordinateSpace");
806 GET_FUNCTION(grDepthRange, "grDepthRange");
807 GET_FUNCTION(grStippleMode, "grStippleMode");
808 GET_FUNCTION(grStipplePattern, "grStipplePattern");
809 GET_FUNCTION(grViewport, "grViewport");
810 GET_FUNCTION(grTexCalcMemRequired, "grTexCalcMemRequired");
811 GET_FUNCTION(grTexTextureMemRequired, "grTexTextureMemRequired");
812 GET_FUNCTION(grTexMinAddress, "grTexMinAddress");
813 GET_FUNCTION(grTexMaxAddress, "grTexMaxAddress");
814 GET_FUNCTION(grTexNCCTable, "grTexNCCTable");
815 GET_FUNCTION(grTexSource, "grTexSource");
816 GET_FUNCTION(grTexClampMode, "grTexClampMode");
817 GET_FUNCTION(grTexCombine, "grTexCombine");
818 GET_FUNCTION(grTexDetailControl, "grTexDetailControl");
819 GET_FUNCTION(grTexFilterMode, "grTexFilterMode");
820 GET_FUNCTION(grTexLodBiasValue, "grTexLodBiasValue");
821 GET_FUNCTION(grTexDownloadMipMap, "grTexDownloadMipMap");
822 GET_FUNCTION(grTexDownloadMipMapLevel, "grTexDownloadMipMapLevel");
823 GET_FUNCTION(grTexDownloadMipMapLevelPartial, "grTexDownloadMipMapLevelPartial");
824 GET_FUNCTION(grTexDownloadTable, "grTexDownloadTable");
825 GET_FUNCTION(grTexDownloadTablePartial, "grTexDownloadTablePartial");
826 GET_FUNCTION(grTexMipMapMode, "grTexMipMapMode");
827 GET_FUNCTION(grTexMultibase, "grTexMultibase");
828 GET_FUNCTION(grTexMultibaseAddress, "grTexMultibaseAddress");
829 GET_FUNCTION(grLfbLock, "grLfbLock");
830 GET_FUNCTION(grLfbUnlock, "grLfbUnlock");
831 GET_FUNCTION(grLfbConstantAlpha, "grLfbConstantAlpha");
832 GET_FUNCTION(grLfbConstantDepth, "grLfbConstantDepth");
833 GET_FUNCTION(grLfbWriteColorSwizzle, "grLfbWriteColorSwizzle");
834 GET_FUNCTION(grLfbWriteColorFormat, "grLfbWriteColorFormat");
835 GET_FUNCTION(grLfbWriteRegion, "grLfbWriteRegion");
836 GET_FUNCTION(grLfbReadRegion, "grLfbReadRegion");
837 GET_FUNCTION(grGlideInit, "grGlideInit");
838 GET_FUNCTION(grGlideShutdown, "grGlideShutdown");
839 GET_FUNCTION(grGlideGetState, "grGlideGetState");
840 GET_FUNCTION(grGlideSetState, "grGlideSetState");
841 GET_FUNCTION(grGlideGetVertexLayout, "grGlideGetVertexLayout");
842 GET_FUNCTION(grGlideSetVertexLayout, "grGlideSetVertexLayout");
843
844 /* Glide utility functions */
845 GET_FUNCTION(guFogGenerateExp, "guFogGenerateExp");
846 GET_FUNCTION(guFogGenerateExp2, "guFogGenerateExp2");
847 GET_FUNCTION(guFogGenerateLinear, "guFogGenerateLinear");
848
849 /* DRI functions */
850 GET_FUNCTION(grDRIOpen, "grDRIOpen");
851 GET_FUNCTION(grDRIPosition, "grDRIPosition");
852 /*GET_FUNCTION(grDRILostContext, "grDRILostContext");*/
853 GET_FUNCTION(grDRIImportFifo, "grDRIImportFifo");
854 GET_FUNCTION(grDRIInvalidateAll, "grDRIInvalidateAll");
855 GET_FUNCTION(grDRIResetSAREA, "grDRIResetSAREA");
856 GET_FUNCTION(grDRIBufferSwap, "grDRIBufferSwap");
857
858 /*
859 * Extension functions:
860 * Just use dlysm() because we want a NULL pointer if the function is
861 * not found.
862 */
863 /* PIXEXT extension */
864 tmesa->Glide.grStencilFunc = dlsym(libHandle, "grStencilFunc");
865 tmesa->Glide.grStencilMask = dlsym(libHandle, "grStencilMask");
866 tmesa->Glide.grStencilOp = dlsym(libHandle, "grStencilOp");
867 tmesa->Glide.grBufferClearExt = dlsym(libHandle, "grBufferClearExt");
868 tmesa->Glide.grColorMaskExt = dlsym(libHandle, "grColorMaskExt");
869 /* COMBINE extension */
870 tmesa->Glide.grColorCombineExt = dlsym(libHandle, "grColorCombineExt");
871 tmesa->Glide.grTexColorCombineExt = dlsym(libHandle, "grTexColorCombineExt");
872 tmesa->Glide.grAlphaCombineExt = dlsym(libHandle, "grAlphaCombineExt");
873 tmesa->Glide.grTexAlphaCombineExt = dlsym(libHandle, "grTexAlphaCombineExt");
874 tmesa->Glide.grAlphaBlendFunctionExt = dlsym(libHandle, "grAlphaBlendFunctionExt");
875 tmesa->Glide.grConstantColorValueExt = dlsym(libHandle, "grConstantColorValueExt");
876 /* Texus 2 */
877 tmesa->Glide.txImgQuantize = dlsym(libHandle, "txImgQuantize");
878 tmesa->Glide.txImgDequantizeFXT1 = dlsym(libHandle, "_txImgDequantizeFXT1");
879 tmesa->Glide.txErrorSetCallback = dlsym(libHandle, "txErrorSetCallback");
880
881 #ifdef DEBUG_TRAP
882 /* wrap the drawing functions so we can trap them */
883 real_grDrawTriangle = tmesa->Glide.grDrawTriangle;
884 tmesa->Glide.grDrawTriangle = debug_grDrawTriangle;
885
886 real_grDrawPoint = tmesa->Glide.grDrawPoint;
887 tmesa->Glide.grDrawPoint = debug_grDrawPoint;
888
889 real_grDrawVertexArray = tmesa->Glide.grDrawVertexArray;
890 tmesa->Glide.grDrawVertexArray = debug_grDrawVertexArray;
891
892 real_grDrawVertexArrayContiguous = tmesa->Glide.grDrawVertexArrayContiguous;
893 tmesa->Glide.grDrawVertexArrayContiguous = debug_grDrawVertexArrayContiguous;
894
895 real_grClipWindow = tmesa->Glide.grClipWindow;
896 tmesa->Glide.grClipWindow = debug_grClipWindow;
897
898 real_grVertexLayout = tmesa->Glide.grVertexLayout;
899 tmesa->Glide.grVertexLayout = debug_grVertexLayout;
900
901 real_grGlideGetVertexLayout = tmesa->Glide.grGlideGetVertexLayout;
902 tmesa->Glide.grGlideGetVertexLayout = debug_grGlideGetVertexLayout;
903
904 real_grGlideSetVertexLayout = tmesa->Glide.grGlideSetVertexLayout;
905 tmesa->Glide.grGlideSetVertexLayout = debug_grGlideSetVertexLayout;
906
907 real_grTexDownloadMipMapLevel = tmesa->Glide.grTexDownloadMipMapLevel;
908 tmesa->Glide.grTexDownloadMipMapLevel = debug_grTexDownloadMipMapLevel;
909
910 #endif
911 return GL_TRUE;
912 }