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"
36 #include "main/matrix.h"
37 #include "main/extensions.h"
39 #include "swrast/swrast.h"
40 #include "swrast_setup/swrast_setup.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
},
75 { "fall", DEBUG_VERBOSE_FALLBACK
},
79 static const struct dri_extension card_extensions
[] =
81 { "GL_ARB_multitexture", NULL
},
82 { "GL_EXT_texture_edge_clamp", NULL
},
83 { "GL_MESA_ycbcr_texture", NULL
},
84 { "GL_SGIS_generate_mipmap", NULL
},
89 /* Create the device specific context.
91 GLboolean
mach64CreateContext( const __GLcontextModes
*glVisual
,
92 __DRIcontext
*driContextPriv
,
93 void *sharedContextPrivate
)
95 GLcontext
*ctx
, *shareCtx
;
96 __DRIscreen
*driScreen
= driContextPriv
->driScreenPriv
;
97 struct dd_function_table functions
;
98 mach64ContextPtr mmesa
;
99 mach64ScreenPtr mach64Screen
;
101 GLuint
*c_textureSwapsPtr
= NULL
;
104 MACH64_DEBUG
= driParseDebugString(getenv("MACH64_DEBUG"), debug_control
);
107 /* Allocate the mach64 context */
108 mmesa
= (mach64ContextPtr
) CALLOC( sizeof(*mmesa
) );
112 /* Init default driver functions then plug in our Mach64-specific functions
113 * (the texture functions are especially important)
115 _mesa_init_driver_functions( &functions
);
116 mach64InitDriverFuncs( &functions
);
117 mach64InitIoctlFuncs( &functions
);
118 mach64InitTextureFuncs( &functions
);
120 /* Allocate the Mesa context */
121 if (sharedContextPrivate
)
122 shareCtx
= ((mach64ContextPtr
) sharedContextPrivate
)->glCtx
;
125 mmesa
->glCtx
= _mesa_create_context(glVisual
, shareCtx
,
126 &functions
, (void *)mmesa
);
131 driContextPriv
->driverPrivate
= mmesa
;
134 mmesa
->driContext
= driContextPriv
;
135 mmesa
->driScreen
= driScreen
;
136 mmesa
->driDrawable
= NULL
;
137 mmesa
->hHWContext
= driContextPriv
->hHWContext
;
138 mmesa
->driHwLock
= &driScreen
->pSAREA
->lock
;
139 mmesa
->driFd
= driScreen
->fd
;
141 mach64Screen
= mmesa
->mach64Screen
= (mach64ScreenPtr
)driScreen
->private;
143 /* Parse configuration files */
144 driParseConfigFiles (&mmesa
->optionCache
, &mach64Screen
->optionCache
,
145 mach64Screen
->driScreen
->myNum
, "mach64");
147 mmesa
->sarea
= (drm_mach64_sarea_t
*)((char *)driScreen
->pSAREA
+
148 sizeof(drm_sarea_t
));
150 mmesa
->CurrentTexObj
[0] = NULL
;
151 mmesa
->CurrentTexObj
[1] = NULL
;
153 (void) memset( mmesa
->texture_heaps
, 0, sizeof( mmesa
->texture_heaps
) );
154 make_empty_list( &mmesa
->swapped
);
156 mmesa
->firstTexHeap
= mach64Screen
->firstTexHeap
;
157 mmesa
->lastTexHeap
= mach64Screen
->firstTexHeap
+ mach64Screen
->numTexHeaps
;
159 for ( i
= mmesa
->firstTexHeap
; i
< mmesa
->lastTexHeap
; i
++ ) {
160 mmesa
->texture_heaps
[i
] = driCreateTextureHeap( i
, mmesa
,
161 mach64Screen
->texSize
[i
],
162 6, /* align to 64-byte boundary, use 12 for page-size boundary */
163 MACH64_NR_TEX_REGIONS
,
164 (drmTextureRegionPtr
)mmesa
->sarea
->tex_list
[i
],
165 &mmesa
->sarea
->tex_age
[i
],
167 sizeof( mach64TexObj
),
168 (destroy_texture_object_t
*) mach64DestroyTexObj
);
170 #if ENABLE_PERF_BOXES
171 c_textureSwapsPtr
= & mmesa
->c_textureSwaps
;
173 driSetTextureSwapCounterLocation( mmesa
->texture_heaps
[i
],
177 mmesa
->RenderIndex
= -1; /* Impossible value */
178 mmesa
->vert_buf
= NULL
;
179 mmesa
->num_verts
= 0;
180 mmesa
->new_state
= MACH64_NEW_ALL
;
181 mmesa
->dirty
= MACH64_UPLOAD_ALL
;
183 /* Set the maximum texture size small enough that we can
184 * guarentee that both texture units can bind a maximal texture
185 * and have them both in memory (on-card or AGP) at once.
186 * Test for 2 textures * bytes/texel * size * size. There's no
187 * need to account for mipmaps since we only upload one level.
190 ctx
->Const
.MaxTextureUnits
= 2;
191 ctx
->Const
.MaxTextureImageUnits
= 2;
192 ctx
->Const
.MaxTextureCoordUnits
= 2;
193 ctx
->Const
.MaxDrawBuffers
= 1;
195 heap
= mach64Screen
->IsPCI
? MACH64_CARD_HEAP
: MACH64_AGP_HEAP
;
197 driCalculateMaxTextureLevels( & mmesa
->texture_heaps
[heap
],
201 10, /* max 2D texture size is 1024x1024 */
202 0, /* 3D textures unsupported. */
203 0, /* cube textures unsupported. */
204 0, /* texture rectangles unsupported. */
205 1, /* mipmapping unsupported. */
206 GL_TRUE
, /* need to have both textures in
207 either local or AGP memory */
210 #if ENABLE_PERF_BOXES
211 mmesa
->boxes
= ( getenv( "LIBGL_PERFORMANCE_BOXES" ) != NULL
);
214 /* Allocate the vertex buffer
216 mmesa
->vert_buf
= ALIGN_MALLOC(MACH64_BUFFER_SIZE
, 32);
217 if ( !mmesa
->vert_buf
)
219 mmesa
->vert_used
= 0;
220 mmesa
->vert_total
= MACH64_BUFFER_SIZE
;
222 /* Initialize the software rasterizer and helper modules.
224 _swrast_CreateContext( ctx
);
225 _vbo_CreateContext( ctx
);
226 _tnl_CreateContext( ctx
);
227 _swsetup_CreateContext( ctx
);
229 /* Install the customized pipeline:
231 /* _tnl_destroy_pipeline( ctx ); */
232 /* _tnl_install_pipeline( ctx, mach64_pipeline ); */
234 /* Configure swrast and T&L to match hardware characteristics:
236 _swrast_allow_pixel_fog( ctx
, GL_FALSE
);
237 _swrast_allow_vertex_fog( ctx
, GL_TRUE
);
238 _tnl_allow_pixel_fog( ctx
, GL_FALSE
);
239 _tnl_allow_vertex_fog( ctx
, GL_TRUE
);
241 driInitExtensions( ctx
, card_extensions
, GL_TRUE
);
244 mach64InitTriFuncs( ctx
);
245 mach64DDInitStateFuncs( ctx
);
246 mach64DDInitSpanFuncs( ctx
);
247 mach64DDInitState( mmesa
);
249 mmesa
->do_irqs
= (mmesa
->mach64Screen
->irq
&& !getenv("MACH64_NO_IRQS"));
251 driContextPriv
->driverPrivate
= (void *)mmesa
;
253 if (driQueryOptionb(&mmesa
->optionCache
, "no_rast")) {
254 fprintf(stderr
, "disabling 3D acceleration\n");
255 FALLBACK(mmesa
, MACH64_FALLBACK_DISABLE
, 1);
261 /* Destroy the device specific context.
263 void mach64DestroyContext( __DRIcontext
*driContextPriv
)
265 mach64ContextPtr mmesa
= (mach64ContextPtr
) driContextPriv
->driverPrivate
;
267 assert(mmesa
); /* should never be null */
269 GLboolean release_texture_heaps
;
271 release_texture_heaps
= (mmesa
->glCtx
->Shared
->RefCount
== 1);
273 _swsetup_DestroyContext( mmesa
->glCtx
);
274 _tnl_DestroyContext( mmesa
->glCtx
);
275 _vbo_DestroyContext( mmesa
->glCtx
);
276 _swrast_DestroyContext( mmesa
->glCtx
);
278 if (release_texture_heaps
) {
279 /* This share group is about to go away, free our private
280 * texture object data.
284 for ( i
= mmesa
->firstTexHeap
; i
< mmesa
->lastTexHeap
; i
++ ) {
285 driDestroyTextureHeap( mmesa
->texture_heaps
[i
] );
286 mmesa
->texture_heaps
[i
] = NULL
;
289 assert( is_empty_list( & mmesa
->swapped
) );
292 mach64FreeVB( mmesa
->glCtx
);
294 /* Free the vertex buffer */
295 if ( mmesa
->vert_buf
)
296 ALIGN_FREE( mmesa
->vert_buf
);
298 /* free the Mesa context */
299 mmesa
->glCtx
->DriverCtx
= NULL
;
300 _mesa_destroy_context(mmesa
->glCtx
);
306 /* Force the context `c' to be the current context and associate with it
310 mach64MakeCurrent( __DRIcontext
*driContextPriv
,
311 __DRIdrawable
*driDrawPriv
,
312 __DRIdrawable
*driReadPriv
)
314 if ( driContextPriv
) {
315 GET_CURRENT_CONTEXT(ctx
);
316 mach64ContextPtr oldMach64Ctx
= ctx
? MACH64_CONTEXT(ctx
) : NULL
;
317 mach64ContextPtr newMach64Ctx
= (mach64ContextPtr
) driContextPriv
->driverPrivate
;
319 if ( newMach64Ctx
!= oldMach64Ctx
) {
320 newMach64Ctx
->new_state
|= MACH64_NEW_CONTEXT
;
321 newMach64Ctx
->dirty
= MACH64_UPLOAD_ALL
;
325 if ( newMach64Ctx
->driDrawable
!= driDrawPriv
) {
326 if (driDrawPriv
->swap_interval
== (unsigned)-1) {
327 driDrawPriv
->vblFlags
= (newMach64Ctx
->do_irqs
)
328 ? driGetDefaultVBlankFlags(&newMach64Ctx
->optionCache
)
329 : VBLANK_FLAG_NO_IRQ
;
331 driDrawableInitVBlank( driDrawPriv
);
334 newMach64Ctx
->driDrawable
= driDrawPriv
;
335 mach64CalcViewport( newMach64Ctx
->glCtx
);
338 _mesa_make_current( newMach64Ctx
->glCtx
,
339 (GLframebuffer
*) driDrawPriv
->driverPrivate
,
340 (GLframebuffer
*) driReadPriv
->driverPrivate
);
343 newMach64Ctx
->new_state
|= MACH64_NEW_CLIP
;
345 _mesa_make_current( NULL
, NULL
, NULL
);
352 /* Force the context `c' to be unbound from its buffer.
355 mach64UnbindContext( __DRIcontext
*driContextPriv
)