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