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