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