1 /* -*- 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>
32 #include "main/glheader.h"
33 #include "main/context.h"
34 #include "main/simple_list.h"
35 #include "main/imports.h"
37 #include "swrast/swrast.h"
38 #include "swrast_setup/swrast_setup.h"
42 #include "tnl/t_pipeline.h"
44 #include "drivers/common/driverfuncs.h"
46 #include "mach64_context.h"
47 #include "mach64_ioctl.h"
48 #include "mach64_dd.h"
49 #include "mach64_span.h"
50 #include "mach64_state.h"
51 #include "mach64_tex.h"
52 #include "mach64_tris.h"
53 #include "mach64_vb.h"
59 int MACH64_DEBUG
= (0);
62 static const struct dri_debug_control debug_control
[] =
64 { "sync", DEBUG_ALWAYS_SYNC
},
65 { "api", DEBUG_VERBOSE_API
},
66 { "msg", DEBUG_VERBOSE_MSG
},
67 { "lru", DEBUG_VERBOSE_LRU
},
68 { "dri", DEBUG_VERBOSE_DRI
},
69 { "ioctl", DEBUG_VERBOSE_IOCTL
},
70 { "prims", DEBUG_VERBOSE_PRIMS
},
71 { "count", DEBUG_VERBOSE_COUNT
},
72 { "nowait", DEBUG_NOWAIT
},
73 { "fall", DEBUG_VERBOSE_FALLBACK
},
77 static const struct dri_extension card_extensions
[] =
79 { "GL_ARB_multitexture", NULL
},
80 { "GL_EXT_texture_edge_clamp", NULL
},
81 { "GL_MESA_ycbcr_texture", NULL
},
82 { "GL_SGIS_generate_mipmap", NULL
},
87 /* Create the device specific context.
89 GLboolean
mach64CreateContext( const __GLcontextModes
*glVisual
,
90 __DRIcontextPrivate
*driContextPriv
,
91 void *sharedContextPrivate
)
93 GLcontext
*ctx
, *shareCtx
;
94 __DRIscreenPrivate
*driScreen
= driContextPriv
->driScreenPriv
;
95 struct dd_function_table functions
;
96 mach64ContextPtr mmesa
;
97 mach64ScreenPtr mach64Screen
;
99 GLuint
*c_textureSwapsPtr
= NULL
;
102 MACH64_DEBUG
= driParseDebugString(getenv("MACH64_DEBUG"), debug_control
);
105 /* Allocate the mach64 context */
106 mmesa
= (mach64ContextPtr
) CALLOC( sizeof(*mmesa
) );
110 /* Init default driver functions then plug in our Mach64-specific functions
111 * (the texture functions are especially important)
113 _mesa_init_driver_functions( &functions
);
114 mach64InitDriverFuncs( &functions
);
115 mach64InitIoctlFuncs( &functions
);
116 mach64InitTextureFuncs( &functions
);
118 /* Allocate the Mesa context */
119 if (sharedContextPrivate
)
120 shareCtx
= ((mach64ContextPtr
) sharedContextPrivate
)->glCtx
;
123 mmesa
->glCtx
= _mesa_create_context(glVisual
, shareCtx
,
124 &functions
, (void *)mmesa
);
129 driContextPriv
->driverPrivate
= mmesa
;
132 mmesa
->driContext
= driContextPriv
;
133 mmesa
->driScreen
= driScreen
;
134 mmesa
->driDrawable
= NULL
;
135 mmesa
->hHWContext
= driContextPriv
->hHWContext
;
136 mmesa
->driHwLock
= &driScreen
->pSAREA
->lock
;
137 mmesa
->driFd
= driScreen
->fd
;
139 mach64Screen
= mmesa
->mach64Screen
= (mach64ScreenPtr
)driScreen
->private;
141 /* Parse configuration files */
142 driParseConfigFiles (&mmesa
->optionCache
, &mach64Screen
->optionCache
,
143 mach64Screen
->driScreen
->myNum
, "mach64");
145 mmesa
->sarea
= (drm_mach64_sarea_t
*)((char *)driScreen
->pSAREA
+
146 sizeof(drm_sarea_t
));
148 mmesa
->CurrentTexObj
[0] = NULL
;
149 mmesa
->CurrentTexObj
[1] = NULL
;
151 (void) memset( mmesa
->texture_heaps
, 0, sizeof( mmesa
->texture_heaps
) );
152 make_empty_list( &mmesa
->swapped
);
154 mmesa
->firstTexHeap
= mach64Screen
->firstTexHeap
;
155 mmesa
->lastTexHeap
= mach64Screen
->firstTexHeap
+ mach64Screen
->numTexHeaps
;
157 for ( i
= mmesa
->firstTexHeap
; i
< mmesa
->lastTexHeap
; i
++ ) {
158 mmesa
->texture_heaps
[i
] = driCreateTextureHeap( i
, mmesa
,
159 mach64Screen
->texSize
[i
],
160 6, /* align to 64-byte boundary, use 12 for page-size boundary */
161 MACH64_NR_TEX_REGIONS
,
162 (drmTextureRegionPtr
)mmesa
->sarea
->tex_list
[i
],
163 &mmesa
->sarea
->tex_age
[i
],
165 sizeof( mach64TexObj
),
166 (destroy_texture_object_t
*) mach64DestroyTexObj
);
168 #if ENABLE_PERF_BOXES
169 c_textureSwapsPtr
= & mmesa
->c_textureSwaps
;
171 driSetTextureSwapCounterLocation( mmesa
->texture_heaps
[i
],
175 mmesa
->RenderIndex
= -1; /* Impossible value */
176 mmesa
->vert_buf
= NULL
;
177 mmesa
->num_verts
= 0;
178 mmesa
->new_state
= MACH64_NEW_ALL
;
179 mmesa
->dirty
= MACH64_UPLOAD_ALL
;
181 /* Set the maximum texture size small enough that we can
182 * guarentee that both texture units can bind a maximal texture
183 * and have them both in memory (on-card or AGP) at once.
184 * Test for 2 textures * bytes/texel * size * size. There's no
185 * need to account for mipmaps since we only upload one level.
188 ctx
->Const
.MaxTextureUnits
= 2;
189 ctx
->Const
.MaxTextureImageUnits
= 2;
190 ctx
->Const
.MaxTextureCoordUnits
= 2;
191 ctx
->Const
.MaxDrawBuffers
= 1;
193 heap
= mach64Screen
->IsPCI
? MACH64_CARD_HEAP
: MACH64_AGP_HEAP
;
195 driCalculateMaxTextureLevels( & mmesa
->texture_heaps
[heap
],
199 10, /* max 2D texture size is 1024x1024 */
200 0, /* 3D textures unsupported. */
201 0, /* cube textures unsupported. */
202 0, /* texture rectangles unsupported. */
203 1, /* mipmapping unsupported. */
204 GL_TRUE
, /* need to have both textures in
205 either local or AGP memory */
208 #if ENABLE_PERF_BOXES
209 mmesa
->boxes
= ( getenv( "LIBGL_PERFORMANCE_BOXES" ) != NULL
);
212 /* Allocate the vertex buffer
214 mmesa
->vert_buf
= ALIGN_MALLOC(MACH64_BUFFER_SIZE
, 32);
215 if ( !mmesa
->vert_buf
)
217 mmesa
->vert_used
= 0;
218 mmesa
->vert_total
= MACH64_BUFFER_SIZE
;
220 /* Initialize the software rasterizer and helper modules.
222 _swrast_CreateContext( ctx
);
223 _vbo_CreateContext( ctx
);
224 _tnl_CreateContext( ctx
);
225 _swsetup_CreateContext( ctx
);
227 /* Install the customized pipeline:
229 /* _tnl_destroy_pipeline( ctx ); */
230 /* _tnl_install_pipeline( ctx, mach64_pipeline ); */
232 /* Configure swrast and T&L to match hardware characteristics:
234 _swrast_allow_pixel_fog( ctx
, GL_FALSE
);
235 _swrast_allow_vertex_fog( ctx
, GL_TRUE
);
236 _tnl_allow_pixel_fog( ctx
, GL_FALSE
);
237 _tnl_allow_vertex_fog( ctx
, GL_TRUE
);
239 driInitExtensions( ctx
, card_extensions
, GL_TRUE
);
242 mach64InitTriFuncs( ctx
);
243 mach64DDInitStateFuncs( ctx
);
244 mach64DDInitSpanFuncs( ctx
);
245 mach64DDInitState( mmesa
);
247 mmesa
->do_irqs
= (mmesa
->mach64Screen
->irq
&& !getenv("MACH64_NO_IRQS"));
249 driContextPriv
->driverPrivate
= (void *)mmesa
;
251 if (driQueryOptionb(&mmesa
->optionCache
, "no_rast")) {
252 fprintf(stderr
, "disabling 3D acceleration\n");
253 FALLBACK(mmesa
, MACH64_FALLBACK_DISABLE
, 1);
259 /* Destroy the device specific context.
261 void mach64DestroyContext( __DRIcontextPrivate
*driContextPriv
)
263 mach64ContextPtr mmesa
= (mach64ContextPtr
) driContextPriv
->driverPrivate
;
265 assert(mmesa
); /* should never be null */
267 GLboolean release_texture_heaps
;
269 release_texture_heaps
= (mmesa
->glCtx
->Shared
->RefCount
== 1);
271 _swsetup_DestroyContext( mmesa
->glCtx
);
272 _tnl_DestroyContext( mmesa
->glCtx
);
273 _vbo_DestroyContext( mmesa
->glCtx
);
274 _swrast_DestroyContext( mmesa
->glCtx
);
276 if (release_texture_heaps
) {
277 /* This share group is about to go away, free our private
278 * texture object data.
282 for ( i
= mmesa
->firstTexHeap
; i
< mmesa
->lastTexHeap
; i
++ ) {
283 driDestroyTextureHeap( mmesa
->texture_heaps
[i
] );
284 mmesa
->texture_heaps
[i
] = NULL
;
287 assert( is_empty_list( & mmesa
->swapped
) );
290 mach64FreeVB( mmesa
->glCtx
);
292 /* Free the vertex buffer */
293 if ( mmesa
->vert_buf
)
294 ALIGN_FREE( mmesa
->vert_buf
);
296 /* free the Mesa context */
297 mmesa
->glCtx
->DriverCtx
= NULL
;
298 _mesa_destroy_context(mmesa
->glCtx
);
304 /* Force the context `c' to be the current context and associate with it
308 mach64MakeCurrent( __DRIcontextPrivate
*driContextPriv
,
309 __DRIdrawablePrivate
*driDrawPriv
,
310 __DRIdrawablePrivate
*driReadPriv
)
312 if ( driContextPriv
) {
313 GET_CURRENT_CONTEXT(ctx
);
314 mach64ContextPtr oldMach64Ctx
= ctx
? MACH64_CONTEXT(ctx
) : NULL
;
315 mach64ContextPtr newMach64Ctx
= (mach64ContextPtr
) driContextPriv
->driverPrivate
;
317 if ( newMach64Ctx
!= oldMach64Ctx
) {
318 newMach64Ctx
->new_state
|= MACH64_NEW_CONTEXT
;
319 newMach64Ctx
->dirty
= MACH64_UPLOAD_ALL
;
323 if ( newMach64Ctx
->driDrawable
!= driDrawPriv
) {
324 if (driDrawPriv
->swap_interval
== (unsigned)-1) {
325 driDrawPriv
->vblFlags
= (newMach64Ctx
->do_irqs
)
326 ? driGetDefaultVBlankFlags(&newMach64Ctx
->optionCache
)
327 : VBLANK_FLAG_NO_IRQ
;
329 driDrawableInitVBlank( driDrawPriv
);
332 newMach64Ctx
->driDrawable
= driDrawPriv
;
333 mach64CalcViewport( newMach64Ctx
->glCtx
);
336 _mesa_make_current( newMach64Ctx
->glCtx
,
337 (GLframebuffer
*) driDrawPriv
->driverPrivate
,
338 (GLframebuffer
*) driReadPriv
->driverPrivate
);
341 newMach64Ctx
->new_state
|= MACH64_NEW_CLIP
;
343 _mesa_make_current( NULL
, NULL
, NULL
);
350 /* Force the context `c' to be unbound from its buffer.
353 mach64UnbindContext( __DRIcontextPrivate
*driContextPriv
)