1 /* $XFree86$ */ /* -*- mode: c; c-basic-offset: 3 -*- */
3 * Copyright 2000 Gareth Hughes
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * GARETH HUGHES BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 * Gareth Hughes <gareth@valinux.com>
28 * Leif Delgass <ldelgass@retinalburn.net>
29 * Jos�Fonseca <j_r_fonseca@yahoo.co.uk>
34 #include "simple_list.h"
37 #include "extensions.h"
39 #include "swrast/swrast.h"
40 #include "swrast_setup/swrast_setup.h"
41 #include "array_cache/acache.h"
44 #include "tnl/t_pipeline.h"
46 #include "drivers/common/driverfuncs.h"
48 #include "mach64_context.h"
49 #include "mach64_ioctl.h"
50 #include "mach64_dd.h"
51 #include "mach64_span.h"
52 #include "mach64_state.h"
53 #include "mach64_tex.h"
54 #include "mach64_tris.h"
55 #include "mach64_vb.h"
61 int MACH64_DEBUG
= (0);
64 static const struct dri_debug_control debug_control
[] =
66 { "sync", DEBUG_ALWAYS_SYNC
},
67 { "api", DEBUG_VERBOSE_API
},
68 { "msg", DEBUG_VERBOSE_MSG
},
69 { "lru", DEBUG_VERBOSE_LRU
},
70 { "dri", DEBUG_VERBOSE_DRI
},
71 { "ioctl", DEBUG_VERBOSE_IOCTL
},
72 { "prims", DEBUG_VERBOSE_PRIMS
},
73 { "count", DEBUG_VERBOSE_COUNT
},
74 { "nowait", DEBUG_NOWAIT
},
78 static const char * const card_extensions
[] =
80 "GL_ARB_multitexture",
81 "GL_EXT_texture_edge_clamp",
82 "GL_MESA_ycbcr_texture",
83 "GL_SGIS_generate_mipmap",
84 "GL_SGIS_texture_edge_clamp",
89 /* Create the device specific context.
91 GLboolean
mach64CreateContext( const __GLcontextModes
*glVisual
,
92 __DRIcontextPrivate
*driContextPriv
,
93 void *sharedContextPrivate
)
95 GLcontext
*ctx
, *shareCtx
;
96 __DRIscreenPrivate
*driScreen
= driContextPriv
->driScreenPriv
;
97 struct dd_function_table functions
;
98 mach64ContextPtr mmesa
;
99 mach64ScreenPtr mach64Screen
;
103 MACH64_DEBUG
= driParseDebugString(getenv("MACH64_DEBUG"), debug_control
);
106 /* Allocate the mach64 context */
107 mmesa
= (mach64ContextPtr
) CALLOC( sizeof(*mmesa
) );
111 /* Init default driver functions then plug in our Mach64-specific functions
112 * (the texture functions are especially important)
114 _mesa_init_driver_functions( &functions
);
115 mach64InitDriverFuncs( &functions
);
116 mach64InitIoctlFuncs( &functions
);
117 mach64InitTextureFuncs( &functions
);
119 /* Allocate the Mesa context */
120 if (sharedContextPrivate
)
121 shareCtx
= ((mach64ContextPtr
) sharedContextPrivate
)->glCtx
;
124 mmesa
->glCtx
= _mesa_create_context(glVisual
, shareCtx
,
125 &functions
, (void *)mmesa
);
130 driContextPriv
->driverPrivate
= mmesa
;
133 mmesa
->driContext
= driContextPriv
;
134 mmesa
->driScreen
= driScreen
;
135 mmesa
->driDrawable
= NULL
;
136 mmesa
->hHWContext
= driContextPriv
->hHWContext
;
137 mmesa
->driHwLock
= &driScreen
->pSAREA
->lock
;
138 mmesa
->driFd
= driScreen
->fd
;
140 mach64Screen
= mmesa
->mach64Screen
= (mach64ScreenPtr
)driScreen
->private;
142 /* Parse configuration files */
143 driParseConfigFiles (&mmesa
->optionCache
, &mach64Screen
->optionCache
,
144 mach64Screen
->driScreen
->myNum
, "mach64");
146 mmesa
->sarea
= (drm_mach64_sarea_t
*)((char *)driScreen
->pSAREA
+
147 sizeof(drm_sarea_t
));
149 mmesa
->CurrentTexObj
[0] = NULL
;
150 mmesa
->CurrentTexObj
[1] = NULL
;
152 make_empty_list( &mmesa
->SwappedOut
);
154 mmesa
->firstTexHeap
= mach64Screen
->firstTexHeap
;
155 mmesa
->lastTexHeap
= mach64Screen
->firstTexHeap
+ mach64Screen
->numTexHeaps
;
157 for ( i
= mmesa
->firstTexHeap
; i
< mmesa
->lastTexHeap
; i
++ ) {
158 make_empty_list( &mmesa
->TexObjList
[i
] );
159 mmesa
->texHeap
[i
] = mmInit( 0, mach64Screen
->texSize
[i
] );
160 mmesa
->lastTexAge
[i
] = -1;
163 mmesa
->RenderIndex
= -1; /* Impossible value */
164 mmesa
->vert_buf
= NULL
;
165 mmesa
->num_verts
= 0;
166 mmesa
->new_state
= MACH64_NEW_ALL
;
167 mmesa
->dirty
= MACH64_UPLOAD_ALL
;
169 /* Set the maximum texture size small enough that we can
170 * guarentee that both texture units can bind a maximal texture
171 * and have them both in memory (on-card or AGP) at once.
172 * Test for 2 textures * bytes/texel * size * size. There's no
173 * need to account for mipmaps since we only upload one level.
175 heap
= mach64Screen
->IsPCI
? MACH64_CARD_HEAP
: MACH64_AGP_HEAP
;
177 if ( mach64Screen
->texSize
[heap
] >= 2 * mach64Screen
->cpp
* 1024*1024 ) {
178 ctx
->Const
.MaxTextureLevels
= 11; /* 1024x1024 */
179 } else if ( mach64Screen
->texSize
[heap
] >= 2 * mach64Screen
->cpp
* 512*512 ) {
180 ctx
->Const
.MaxTextureLevels
= 10; /* 512x512 */
182 ctx
->Const
.MaxTextureLevels
= 9; /* 256x256 */
185 ctx
->Const
.MaxTextureUnits
= 2;
187 #if ENABLE_PERF_BOXES
188 mmesa
->boxes
= ( getenv( "LIBGL_PERFORMANCE_BOXES" ) != NULL
);
191 /* Allocate the vertex buffer
193 mmesa
->vert_buf
= ALIGN_MALLOC(MACH64_BUFFER_SIZE
, 32);
194 if ( !mmesa
->vert_buf
)
196 mmesa
->vert_used
= 0;
197 mmesa
->vert_total
= MACH64_BUFFER_SIZE
;
199 /* Initialize the software rasterizer and helper modules.
201 _swrast_CreateContext( ctx
);
202 _ac_CreateContext( ctx
);
203 _tnl_CreateContext( ctx
);
204 _swsetup_CreateContext( ctx
);
206 /* Install the customized pipeline:
208 /* _tnl_destroy_pipeline( ctx ); */
209 /* _tnl_install_pipeline( ctx, mach64_pipeline ); */
211 /* Configure swrast and T&L to match hardware characteristics:
213 _swrast_allow_pixel_fog( ctx
, GL_FALSE
);
214 _swrast_allow_vertex_fog( ctx
, GL_TRUE
);
215 _tnl_allow_pixel_fog( ctx
, GL_FALSE
);
216 _tnl_allow_vertex_fog( ctx
, GL_TRUE
);
218 driInitExtensions( ctx
, card_extensions
, GL_TRUE
);
221 mach64InitTriFuncs( ctx
);
222 mach64DDInitStateFuncs( ctx
);
223 mach64DDInitSpanFuncs( ctx
);
224 mach64DDInitState( mmesa
);
226 mmesa
->do_irqs
= (mmesa
->mach64Screen
->irq
&& !getenv("MACH64_NO_IRQS"));
228 mmesa
->vblank_flags
= (mmesa
->do_irqs
)
229 ? driGetDefaultVBlankFlags(&mmesa
->optionCache
) : VBLANK_FLAG_NO_IRQ
;
231 driContextPriv
->driverPrivate
= (void *)mmesa
;
236 /* Destroy the device specific context.
238 void mach64DestroyContext( __DRIcontextPrivate
*driContextPriv
)
240 mach64ContextPtr mmesa
= (mach64ContextPtr
) driContextPriv
->driverPrivate
;
242 assert(mmesa
); /* should never be null */
244 if (mmesa
->glCtx
->Shared
->RefCount
== 1) {
245 /* This share group is about to go away, free our private
246 * texture object data.
248 mach64TexObjPtr t
, next_t
;
251 for ( i
= mmesa
->firstTexHeap
; i
< mmesa
->lastTexHeap
; i
++ ) {
252 foreach_s ( t
, next_t
, &mmesa
->TexObjList
[i
] ) {
253 mach64DestroyTexObj( mmesa
, t
);
255 mmDestroy( mmesa
->texHeap
[i
] );
256 mmesa
->texHeap
[i
] = NULL
;
259 foreach_s ( t
, next_t
, &mmesa
->SwappedOut
) {
260 mach64DestroyTexObj( mmesa
, t
);
264 _swsetup_DestroyContext( mmesa
->glCtx
);
265 _tnl_DestroyContext( mmesa
->glCtx
);
266 _ac_DestroyContext( mmesa
->glCtx
);
267 _swrast_DestroyContext( mmesa
->glCtx
);
269 mach64FreeVB( mmesa
->glCtx
);
271 /* Free the vertex buffer */
272 if ( mmesa
->vert_buf
)
273 ALIGN_FREE( mmesa
->vert_buf
);
275 /* free the Mesa context */
276 mmesa
->glCtx
->DriverCtx
= NULL
;
277 _mesa_destroy_context(mmesa
->glCtx
);
283 /* Force the context `c' to be the current context and associate with it
287 mach64MakeCurrent( __DRIcontextPrivate
*driContextPriv
,
288 __DRIdrawablePrivate
*driDrawPriv
,
289 __DRIdrawablePrivate
*driReadPriv
)
291 if ( driContextPriv
) {
292 GET_CURRENT_CONTEXT(ctx
);
293 mach64ContextPtr oldMach64Ctx
= ctx
? MACH64_CONTEXT(ctx
) : NULL
;
294 mach64ContextPtr newMach64Ctx
= (mach64ContextPtr
) driContextPriv
->driverPrivate
;
296 if ( newMach64Ctx
!= oldMach64Ctx
) {
297 newMach64Ctx
->new_state
|= MACH64_NEW_CONTEXT
;
298 newMach64Ctx
->dirty
= MACH64_UPLOAD_ALL
;
302 driDrawableInitVBlank( driDrawPriv
, newMach64Ctx
->vblank_flags
);
304 if ( newMach64Ctx
->driDrawable
!= driDrawPriv
) {
305 newMach64Ctx
->driDrawable
= driDrawPriv
;
306 mach64CalcViewport( newMach64Ctx
->glCtx
);
309 _mesa_make_current2( newMach64Ctx
->glCtx
,
310 (GLframebuffer
*) driDrawPriv
->driverPrivate
,
311 (GLframebuffer
*) driReadPriv
->driverPrivate
);
314 newMach64Ctx
->new_state
|= MACH64_NEW_CLIP
;
316 if ( !newMach64Ctx
->glCtx
->Viewport
.Width
) {
317 _mesa_set_viewport(newMach64Ctx
->glCtx
, 0, 0,
318 driDrawPriv
->w
, driDrawPriv
->h
);
321 _mesa_make_current( 0, 0 );
328 /* Force the context `c' to be unbound from its buffer.
331 mach64UnbindContext( __DRIcontextPrivate
*driContextPriv
)