fix up radeon span functions using latest r200 code from Brian,
[mesa.git] / src / mesa / drivers / dri / mga / mga_xmesa.c
1 /* $XFree86: xc/lib/GL/mesa/src/drv/mga/mga_xmesa.c,v 1.19 2003/03/26 20:43:49 tsi Exp $ */
2 /*
3 * Copyright 2000-2001 VA Linux Systems, Inc.
4 * All Rights Reserved.
5 *
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
10 * license, and/or sell copies of the Software, and to permit persons to whom
11 * the Software is furnished to do so, subject to the following conditions:
12 *
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
15 * Software.
16 *
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 NON-INFRINGEMENT. IN NO EVENT SHALL
20 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 /**
27 * \file mga_xmesa.c
28 * MGA screen and context initialization / creation code.
29 *
30 * \author Keith Whitwell <keith@tungstengraphics.com>
31 */
32
33 #include <stdlib.h>
34 #include <stdint.h>
35 #include "drm.h"
36 #include "mga_drm.h"
37 #include "mga_xmesa.h"
38 #include "context.h"
39 #include "matrix.h"
40 #include "simple_list.h"
41 #include "imports.h"
42 #include "framebuffer.h"
43 #include "renderbuffer.h"
44
45 #include "swrast/swrast.h"
46 #include "swrast_setup/swrast_setup.h"
47 #include "tnl/tnl.h"
48 #include "array_cache/acache.h"
49
50 #include "tnl/t_pipeline.h"
51
52 #include "drivers/common/driverfuncs.h"
53
54 #include "mgadd.h"
55 #include "mgastate.h"
56 #include "mgatex.h"
57 #include "mgaspan.h"
58 #include "mgaioctl.h"
59 #include "mgatris.h"
60 #include "mgavb.h"
61 #include "mgapixel.h"
62 #include "mga_xmesa.h"
63 #include "mga_dri.h"
64
65 #include "utils.h"
66 #include "vblank.h"
67
68 #include "extensions.h"
69 #include "drirenderbuffer.h"
70
71 #include "GL/internal/dri_interface.h"
72
73 #define need_GL_ARB_multisample
74 #define need_GL_ARB_texture_compression
75 #define need_GL_ARB_vertex_program
76 #define need_GL_EXT_fog_coord
77 #define need_GL_EXT_multi_draw_arrays
78 #define need_GL_EXT_secondary_color
79 #if 0
80 #define need_GL_EXT_paletted_texture
81 #endif
82 #define need_GL_NV_vertex_program
83 #include "extension_helper.h"
84
85 /* MGA configuration
86 */
87 #include "xmlpool.h"
88
89 PUBLIC const char __driConfigOptions[] =
90 DRI_CONF_BEGIN
91 DRI_CONF_SECTION_PERFORMANCE
92 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_0)
93 DRI_CONF_SECTION_END
94 DRI_CONF_SECTION_QUALITY
95 DRI_CONF_TEXTURE_DEPTH(DRI_CONF_TEXTURE_DEPTH_FB)
96 DRI_CONF_COLOR_REDUCTION(DRI_CONF_COLOR_REDUCTION_DITHER)
97 DRI_CONF_SECTION_END
98 DRI_CONF_SECTION_SOFTWARE
99 DRI_CONF_ARB_VERTEX_PROGRAM(true)
100 DRI_CONF_NV_VERTEX_PROGRAM(true)
101 DRI_CONF_SECTION_END
102 DRI_CONF_SECTION_DEBUG
103 DRI_CONF_NO_RAST(false)
104 DRI_CONF_SECTION_END
105 DRI_CONF_END;
106 static const GLuint __driNConfigOptions = 6;
107
108 #ifndef MGA_DEBUG
109 int MGA_DEBUG = 0;
110 #endif
111
112 static int getSwapInfo( __DRIdrawablePrivate *dPriv, __DRIswapInfo * sInfo );
113
114 static __GLcontextModes *
115 mgaFillInModes( unsigned pixel_bits, unsigned depth_bits,
116 unsigned stencil_bits, GLboolean have_back_buffer )
117 {
118 __GLcontextModes * modes;
119 __GLcontextModes * m;
120 unsigned num_modes;
121 unsigned depth_buffer_factor;
122 unsigned back_buffer_factor;
123 GLenum fb_format;
124 GLenum fb_type;
125
126 /* GLX_SWAP_COPY_OML is only supported because the MGA driver doesn't
127 * support pageflipping at all.
128 */
129 static const GLenum back_buffer_modes[] = {
130 GLX_NONE, GLX_SWAP_UNDEFINED_OML, GLX_SWAP_COPY_OML
131 };
132
133 u_int8_t depth_bits_array[3];
134 u_int8_t stencil_bits_array[3];
135
136
137 depth_bits_array[0] = 0;
138 depth_bits_array[1] = depth_bits;
139 depth_bits_array[2] = depth_bits;
140
141 /* Just like with the accumulation buffer, always provide some modes
142 * with a stencil buffer. It will be a sw fallback, but some apps won't
143 * care about that.
144 */
145 stencil_bits_array[0] = 0;
146 stencil_bits_array[1] = 0;
147 stencil_bits_array[2] = (stencil_bits == 0) ? 8 : stencil_bits;
148
149 depth_buffer_factor = ((depth_bits != 0) || (stencil_bits != 0)) ? 3 : 1;
150 back_buffer_factor = (have_back_buffer) ? 2 : 1;
151
152 num_modes = depth_buffer_factor * back_buffer_factor * 4;
153
154 if ( pixel_bits == 16 ) {
155 fb_format = GL_RGB;
156 fb_type = GL_UNSIGNED_SHORT_5_6_5;
157 }
158 else {
159 fb_format = GL_BGR;
160 fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
161 }
162
163 modes = (*dri_interface->createContextModes)( num_modes, sizeof( __GLcontextModes ) );
164 m = modes;
165 if ( ! driFillInModes( & m, fb_format, fb_type,
166 depth_bits_array, stencil_bits_array, depth_buffer_factor,
167 back_buffer_modes, back_buffer_factor,
168 GLX_TRUE_COLOR ) ) {
169 fprintf( stderr, "[%s:%u] Error creating FBConfig!\n",
170 __func__, __LINE__ );
171 return NULL;
172 }
173
174 if ( ! driFillInModes( & m, fb_format, fb_type,
175 depth_bits_array, stencil_bits_array, depth_buffer_factor,
176 back_buffer_modes, back_buffer_factor,
177 GLX_DIRECT_COLOR ) ) {
178 fprintf( stderr, "[%s:%u] Error creating FBConfig!\n",
179 __func__, __LINE__ );
180 return NULL;
181 }
182
183 /* Mark the visual as slow if there are "fake" stencil bits.
184 */
185 for ( m = modes ; m != NULL ; m = m->next ) {
186 if ( (m->stencilBits != 0) && (m->stencilBits != stencil_bits) ) {
187 m->visualRating = GLX_SLOW_CONFIG;
188 }
189 }
190
191 return modes;
192 }
193
194
195 static GLboolean
196 mgaInitDriver(__DRIscreenPrivate *sPriv)
197 {
198 mgaScreenPrivate *mgaScreen;
199 MGADRIPtr serverInfo = (MGADRIPtr)sPriv->pDevPriv;
200 PFNGLXSCRENABLEEXTENSIONPROC glx_enable_extension =
201 (PFNGLXSCRENABLEEXTENSIONPROC) (*dri_interface->getProcAddress("glxEnableExtension"));
202 void * const psc = sPriv->psc->screenConfigs;
203
204 if (sPriv->devPrivSize != sizeof(MGADRIRec)) {
205 fprintf(stderr,"\nERROR! sizeof(MGADRIRec) does not match passed size from device driver\n");
206 return GL_FALSE;
207 }
208
209 /* Allocate the private area */
210 mgaScreen = (mgaScreenPrivate *)MALLOC(sizeof(mgaScreenPrivate));
211 if (!mgaScreen) {
212 __driUtilMessage("Couldn't malloc screen struct");
213 return GL_FALSE;
214 }
215
216 mgaScreen->sPriv = sPriv;
217 sPriv->private = (void *)mgaScreen;
218
219 if (sPriv->drmMinor >= 1) {
220 int ret;
221 drm_mga_getparam_t gp;
222
223 gp.param = MGA_PARAM_IRQ_NR;
224 gp.value = &mgaScreen->irq;
225 mgaScreen->irq = 0;
226
227 ret = drmCommandWriteRead( sPriv->fd, DRM_MGA_GETPARAM,
228 &gp, sizeof(gp));
229 if (ret) {
230 fprintf(stderr, "drmMgaGetParam (MGA_PARAM_IRQ_NR): %d\n", ret);
231 FREE(mgaScreen);
232 sPriv->private = NULL;
233 return GL_FALSE;
234 }
235 }
236
237 if ( glx_enable_extension != NULL ) {
238 (*glx_enable_extension)( psc, "GLX_MESA_swap_control" );
239 (*glx_enable_extension)( psc, "GLX_MESA_swap_frame_usage" );
240 (*glx_enable_extension)( psc, "GLX_SGI_make_current_read" );
241 (*glx_enable_extension)( psc, "GLX_SGI_swap_control" );
242 (*glx_enable_extension)( psc, "GLX_SGI_video_sync" );
243 }
244
245 if (serverInfo->chipset != MGA_CARD_TYPE_G200 &&
246 serverInfo->chipset != MGA_CARD_TYPE_G400) {
247 FREE(mgaScreen);
248 sPriv->private = NULL;
249 __driUtilMessage("Unrecognized chipset");
250 return GL_FALSE;
251 }
252
253
254 mgaScreen->chipset = serverInfo->chipset;
255 mgaScreen->cpp = serverInfo->cpp;
256
257 mgaScreen->agpMode = serverInfo->agpMode;
258
259 mgaScreen->frontPitch = serverInfo->frontPitch;
260 mgaScreen->frontOffset = serverInfo->frontOffset;
261 mgaScreen->backOffset = serverInfo->backOffset;
262 mgaScreen->backPitch = serverInfo->backPitch;
263 mgaScreen->depthOffset = serverInfo->depthOffset;
264 mgaScreen->depthPitch = serverInfo->depthPitch;
265
266
267 /* The only reason that the MMIO region needs to be accessable and the
268 * primary DMA region base address needs to be known is so that the driver
269 * can busy wait for certain DMA operations to complete (see
270 * mgaWaitForFrameCompletion in mgaioctl.c).
271 *
272 * Starting with MGA DRM version 3.2, these are completely unneeded as
273 * there is a new, in-kernel mechanism for handling the wait.
274 */
275
276 if (mgaScreen->sPriv->drmMinor < 2) {
277 mgaScreen->mmio.handle = serverInfo->registers.handle;
278 mgaScreen->mmio.size = serverInfo->registers.size;
279 if ( drmMap( sPriv->fd,
280 mgaScreen->mmio.handle, mgaScreen->mmio.size,
281 &mgaScreen->mmio.map ) < 0 ) {
282 FREE( mgaScreen );
283 sPriv->private = NULL;
284 __driUtilMessage( "Couldn't map MMIO registers" );
285 return GL_FALSE;
286 }
287
288 mgaScreen->primary.handle = serverInfo->primary.handle;
289 mgaScreen->primary.size = serverInfo->primary.size;
290 }
291 else {
292 (void) memset( & mgaScreen->primary, 0, sizeof( mgaScreen->primary ) );
293 (void) memset( & mgaScreen->mmio, 0, sizeof( mgaScreen->mmio ) );
294 }
295
296 mgaScreen->textureOffset[MGA_CARD_HEAP] = serverInfo->textureOffset;
297 mgaScreen->textureOffset[MGA_AGP_HEAP] = (serverInfo->agpTextureOffset |
298 PDEA_pagpxfer_enable | 1);
299
300 mgaScreen->textureSize[MGA_CARD_HEAP] = serverInfo->textureSize;
301 mgaScreen->textureSize[MGA_AGP_HEAP] = serverInfo->agpTextureSize;
302
303
304 /* The texVirtual array stores the base addresses in the CPU's address
305 * space of the texture memory pools. The base address of the on-card
306 * memory pool is calculated as an offset of the base of video memory. The
307 * AGP texture pool has to be mapped into the processes address space by
308 * the DRM.
309 */
310
311 mgaScreen->texVirtual[MGA_CARD_HEAP] = (char *)(mgaScreen->sPriv->pFB +
312 serverInfo->textureOffset);
313
314 if ( serverInfo->agpTextureSize > 0 ) {
315 if (drmMap(sPriv->fd, serverInfo->agpTextureOffset,
316 serverInfo->agpTextureSize,
317 (drmAddress *)&mgaScreen->texVirtual[MGA_AGP_HEAP]) != 0) {
318 FREE(mgaScreen);
319 sPriv->private = NULL;
320 __driUtilMessage("Couldn't map agptexture region");
321 return GL_FALSE;
322 }
323 }
324
325
326 /* For calculating setupdma addresses.
327 */
328
329 mgaScreen->bufs = drmMapBufs(sPriv->fd);
330 if (!mgaScreen->bufs) {
331 FREE(mgaScreen);
332 sPriv->private = NULL;
333 __driUtilMessage("Couldn't map dma buffers");
334 return GL_FALSE;
335 }
336 mgaScreen->sarea_priv_offset = serverInfo->sarea_priv_offset;
337
338 /* parse information in __driConfigOptions */
339 driParseOptionInfo (&mgaScreen->optionCache,
340 __driConfigOptions, __driNConfigOptions);
341
342 return GL_TRUE;
343 }
344
345
346 static void
347 mgaDestroyScreen(__DRIscreenPrivate *sPriv)
348 {
349 mgaScreenPrivate *mgaScreen = (mgaScreenPrivate *) sPriv->private;
350
351 if (MGA_DEBUG&DEBUG_VERBOSE_DRI)
352 fprintf(stderr, "mgaDestroyScreen\n");
353
354 drmUnmapBufs(mgaScreen->bufs);
355
356
357 /* free all option information */
358 driDestroyOptionInfo (&mgaScreen->optionCache);
359
360 FREE(mgaScreen);
361 sPriv->private = NULL;
362 }
363
364
365 extern const struct tnl_pipeline_stage _mga_render_stage;
366
367 static const struct tnl_pipeline_stage *mga_pipeline[] = {
368 &_tnl_vertex_transform_stage,
369 &_tnl_normal_transform_stage,
370 &_tnl_lighting_stage,
371 &_tnl_fog_coordinate_stage,
372 &_tnl_texgen_stage,
373 &_tnl_texture_transform_stage,
374 &_tnl_arb_vertex_program_stage,
375 &_tnl_vertex_program_stage,
376
377 /* REMOVE: point attenuation stage */
378 #if 0
379 &_mga_render_stage, /* ADD: unclipped rastersetup-to-dma */
380 /* Need new ioctl for wacceptseq */
381 #endif
382 &_tnl_render_stage,
383 0,
384 };
385
386
387 static const struct dri_extension g400_extensions[] =
388 {
389 { "GL_ARB_multitexture", NULL },
390 { "GL_ARB_texture_env_add", NULL },
391 { "GL_ARB_texture_env_combine", NULL },
392 { "GL_ARB_texture_env_crossbar", NULL },
393 { "GL_EXT_texture_env_combine", NULL },
394 { "GL_EXT_texture_edge_clamp", NULL },
395 { "GL_ATI_texture_env_combine3", NULL },
396 { NULL, NULL }
397 };
398
399 static const struct dri_extension card_extensions[] =
400 {
401 { "GL_ARB_multisample", GL_ARB_multisample_functions },
402 { "GL_ARB_texture_compression", GL_ARB_texture_compression_functions },
403 { "GL_ARB_texture_rectangle", NULL },
404 { "GL_EXT_blend_logic_op", NULL },
405 { "GL_EXT_fog_coord", GL_EXT_fog_coord_functions },
406 { "GL_EXT_multi_draw_arrays", GL_EXT_multi_draw_arrays_functions },
407 /* paletted_textures currently doesn't work, but we could fix them later */
408 #if defined( need_GL_EXT_paletted_texture )
409 { "GL_EXT_shared_texture_palette", NULL },
410 { "GL_EXT_paletted_texture", GL_EXT_paletted_texture_functions },
411 #endif
412 { "GL_EXT_secondary_color", GL_EXT_secondary_color_functions },
413 { "GL_EXT_stencil_wrap", NULL },
414 { "GL_MESA_ycbcr_texture", NULL },
415 { "GL_SGIS_generate_mipmap", NULL },
416 { NULL, NULL }
417 };
418
419 static const struct dri_extension ARB_vp_extension[] = {
420 { "GL_ARB_vertex_program", GL_ARB_vertex_program_functions },
421 { NULL, NULL }
422 };
423
424 static const struct dri_extension NV_vp_extensions[] = {
425 { "GL_NV_vertex_program", GL_NV_vertex_program_functions },
426 { "GL_NV_vertex_program1_1", NULL },
427 { NULL, NULL }
428 };
429
430 static const struct dri_debug_control debug_control[] =
431 {
432 { "fall", DEBUG_VERBOSE_FALLBACK },
433 { "tex", DEBUG_VERBOSE_TEXTURE },
434 { "ioctl", DEBUG_VERBOSE_IOCTL },
435 { "verb", DEBUG_VERBOSE_MSG },
436 { "dri", DEBUG_VERBOSE_DRI },
437 { NULL, 0 }
438 };
439
440
441 static GLboolean
442 mgaCreateContext( const __GLcontextModes *mesaVis,
443 __DRIcontextPrivate *driContextPriv,
444 void *sharedContextPrivate )
445 {
446 int i;
447 unsigned maxlevels;
448 GLcontext *ctx, *shareCtx;
449 mgaContextPtr mmesa;
450 __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
451 mgaScreenPrivate *mgaScreen = (mgaScreenPrivate *)sPriv->private;
452 drm_mga_sarea_t *saPriv = (drm_mga_sarea_t *)(((char*)sPriv->pSAREA)+
453 mgaScreen->sarea_priv_offset);
454 struct dd_function_table functions;
455
456 if (MGA_DEBUG&DEBUG_VERBOSE_DRI)
457 fprintf(stderr, "mgaCreateContext\n");
458
459 /* allocate mga context */
460 mmesa = (mgaContextPtr) CALLOC(sizeof(mgaContext));
461 if (!mmesa) {
462 return GL_FALSE;
463 }
464
465 /* Init default driver functions then plug in our Radeon-specific functions
466 * (the texture functions are especially important)
467 */
468 _mesa_init_driver_functions( &functions );
469 mgaInitDriverFuncs( &functions );
470 mgaInitTextureFuncs( &functions );
471 mgaInitIoctlFuncs( &functions );
472
473 /* Allocate the Mesa context */
474 if (sharedContextPrivate)
475 shareCtx = ((mgaContextPtr) sharedContextPrivate)->glCtx;
476 else
477 shareCtx = NULL;
478 mmesa->glCtx = _mesa_create_context(mesaVis, shareCtx,
479 &functions, (void *) mmesa);
480 if (!mmesa->glCtx) {
481 FREE(mmesa);
482 return GL_FALSE;
483 }
484 driContextPriv->driverPrivate = mmesa;
485
486 /* Init mga state */
487 mmesa->hHWContext = driContextPriv->hHWContext;
488 mmesa->driFd = sPriv->fd;
489 mmesa->driHwLock = &sPriv->pSAREA->lock;
490
491 mmesa->mgaScreen = mgaScreen;
492 mmesa->driScreen = sPriv;
493 mmesa->sarea = (void *)saPriv;
494
495 /* Parse configuration files */
496 driParseConfigFiles (&mmesa->optionCache, &mgaScreen->optionCache,
497 sPriv->myNum, "mga");
498
499 (void) memset( mmesa->texture_heaps, 0, sizeof( mmesa->texture_heaps ) );
500 make_empty_list( & mmesa->swapped );
501
502 mmesa->nr_heaps = mgaScreen->texVirtual[MGA_AGP_HEAP] ? 2 : 1;
503 for ( i = 0 ; i < mmesa->nr_heaps ; i++ ) {
504 mmesa->texture_heaps[i] = driCreateTextureHeap( i, mmesa,
505 mgaScreen->textureSize[i],
506 6,
507 MGA_NR_TEX_REGIONS,
508 (drmTextureRegionPtr)mmesa->sarea->texList[i],
509 &mmesa->sarea->texAge[i],
510 &mmesa->swapped,
511 sizeof( mgaTextureObject_t ),
512 (destroy_texture_object_t *) mgaDestroyTexObj );
513 }
514
515 /* Set the maximum texture size small enough that we can guarentee
516 * that both texture units can bind a maximal texture and have them
517 * on the card at once.
518 */
519 ctx = mmesa->glCtx;
520 if ( mgaScreen->chipset == MGA_CARD_TYPE_G200 ) {
521 ctx->Const.MaxTextureUnits = 1;
522 ctx->Const.MaxTextureImageUnits = 1;
523 ctx->Const.MaxTextureCoordUnits = 1;
524 maxlevels = G200_TEX_MAXLEVELS;
525
526 }
527 else {
528 ctx->Const.MaxTextureUnits = 2;
529 ctx->Const.MaxTextureImageUnits = 2;
530 ctx->Const.MaxTextureCoordUnits = 2;
531 maxlevels = G400_TEX_MAXLEVELS;
532 }
533
534 driCalculateMaxTextureLevels( mmesa->texture_heaps,
535 mmesa->nr_heaps,
536 & ctx->Const,
537 4,
538 11, /* max 2D texture size is 2048x2048 */
539 0, /* 3D textures unsupported. */
540 0, /* cube textures unsupported. */
541 11, /* max texture rect size is 2048x2048 */
542 maxlevels,
543 GL_FALSE );
544
545 ctx->Const.MinLineWidth = 1.0;
546 ctx->Const.MinLineWidthAA = 1.0;
547 ctx->Const.MaxLineWidth = 10.0;
548 ctx->Const.MaxLineWidthAA = 10.0;
549 ctx->Const.LineWidthGranularity = 1.0;
550
551 mmesa->texture_depth = driQueryOptioni (&mmesa->optionCache,
552 "texture_depth");
553 if (mmesa->texture_depth == DRI_CONF_TEXTURE_DEPTH_FB)
554 mmesa->texture_depth = ( mesaVis->rgbBits >= 24 ) ?
555 DRI_CONF_TEXTURE_DEPTH_32 : DRI_CONF_TEXTURE_DEPTH_16;
556 mmesa->hw_stencil = mesaVis->stencilBits && mesaVis->depthBits == 24;
557
558 switch (mesaVis->depthBits) {
559 case 16:
560 mmesa->depth_scale = 1.0/(GLdouble)0xffff;
561 mmesa->depth_clear_mask = ~0;
562 mmesa->ClearDepth = 0xffff;
563 break;
564 case 24:
565 mmesa->depth_scale = 1.0/(GLdouble)0xffffff;
566 if (mmesa->hw_stencil) {
567 mmesa->depth_clear_mask = 0xffffff00;
568 mmesa->stencil_clear_mask = 0x000000ff;
569 } else
570 mmesa->depth_clear_mask = ~0;
571 mmesa->ClearDepth = 0xffffff00;
572 break;
573 case 32:
574 mmesa->depth_scale = 1.0/(GLdouble)0xffffffff;
575 mmesa->depth_clear_mask = ~0;
576 mmesa->ClearDepth = 0xffffffff;
577 break;
578 };
579
580 mmesa->haveHwStipple = GL_FALSE;
581 mmesa->RenderIndex = -1; /* impossible value */
582 mmesa->dirty = ~0;
583 mmesa->vertex_format = 0;
584 mmesa->CurrentTexObj[0] = 0;
585 mmesa->CurrentTexObj[1] = 0;
586 mmesa->tmu_source[0] = 0;
587 mmesa->tmu_source[1] = 1;
588
589 mmesa->texAge[0] = 0;
590 mmesa->texAge[1] = 0;
591
592 /* Initialize the software rasterizer and helper modules.
593 */
594 _swrast_CreateContext( ctx );
595 _ac_CreateContext( ctx );
596 _tnl_CreateContext( ctx );
597
598 _swsetup_CreateContext( ctx );
599
600 /* Install the customized pipeline:
601 */
602 _tnl_destroy_pipeline( ctx );
603 _tnl_install_pipeline( ctx, mga_pipeline );
604
605 /* Configure swrast and T&L to match hardware characteristics:
606 */
607 _swrast_allow_pixel_fog( ctx, GL_FALSE );
608 _swrast_allow_vertex_fog( ctx, GL_TRUE );
609 _tnl_allow_pixel_fog( ctx, GL_FALSE );
610 _tnl_allow_vertex_fog( ctx, GL_TRUE );
611
612 mmesa->primary_offset = mmesa->mgaScreen->primary.handle;
613
614 ctx->DriverCtx = (void *) mmesa;
615 mmesa->glCtx = ctx;
616
617 driInitExtensions( ctx, card_extensions, GL_FALSE );
618
619 if (MGA_IS_G400(MGA_CONTEXT(ctx))) {
620 driInitExtensions( ctx, g400_extensions, GL_FALSE );
621 }
622
623 if ( driQueryOptionb( &mmesa->optionCache, "arb_vertex_program" ) ) {
624 driInitSingleExtension( ctx, ARB_vp_extension );
625 }
626
627 if ( driQueryOptionb( &mmesa->optionCache, "nv_vertex_program" ) ) {
628 driInitExtensions( ctx, NV_vp_extensions, GL_FALSE );
629 }
630
631
632 /* XXX these should really go right after _mesa_init_driver_functions() */
633 mgaDDInitStateFuncs( ctx );
634 mgaDDInitSpanFuncs( ctx );
635 mgaDDInitPixelFuncs( ctx );
636 mgaDDInitTriFuncs( ctx );
637
638 mgaInitVB( ctx );
639 mgaInitState( mmesa );
640
641 driContextPriv->driverPrivate = (void *) mmesa;
642
643 #if DO_DEBUG
644 MGA_DEBUG = driParseDebugString( getenv( "MGA_DEBUG" ),
645 debug_control );
646 #endif
647
648 mmesa->vblank_flags = (mmesa->mgaScreen->irq == 0)
649 ? VBLANK_FLAG_NO_IRQ : driGetDefaultVBlankFlags(&mmesa->optionCache);
650
651 (*dri_interface->getUST)( & mmesa->swap_ust );
652
653 if (driQueryOptionb(&mmesa->optionCache, "no_rast")) {
654 fprintf(stderr, "disabling 3D acceleration\n");
655 FALLBACK(mmesa->glCtx, MGA_FALLBACK_DISABLE, 1);
656 }
657
658 return GL_TRUE;
659 }
660
661 static void
662 mgaDestroyContext(__DRIcontextPrivate *driContextPriv)
663 {
664 mgaContextPtr mmesa = (mgaContextPtr) driContextPriv->driverPrivate;
665
666 if (MGA_DEBUG&DEBUG_VERBOSE_DRI)
667 fprintf( stderr, "[%s:%d] mgaDestroyContext start\n",
668 __FILE__, __LINE__ );
669
670 assert(mmesa); /* should never be null */
671 if (mmesa) {
672 GLboolean release_texture_heaps;
673
674
675 release_texture_heaps = (mmesa->glCtx->Shared->RefCount == 1);
676 _swsetup_DestroyContext( mmesa->glCtx );
677 _tnl_DestroyContext( mmesa->glCtx );
678 _ac_DestroyContext( mmesa->glCtx );
679 _swrast_DestroyContext( mmesa->glCtx );
680
681 mgaFreeVB( mmesa->glCtx );
682
683 /* free the Mesa context */
684 mmesa->glCtx->DriverCtx = NULL;
685 _mesa_destroy_context(mmesa->glCtx);
686
687 if ( release_texture_heaps ) {
688 /* This share group is about to go away, free our private
689 * texture object data.
690 */
691 int i;
692
693 for ( i = 0 ; i < mmesa->nr_heaps ; i++ ) {
694 driDestroyTextureHeap( mmesa->texture_heaps[ i ] );
695 mmesa->texture_heaps[ i ] = NULL;
696 }
697
698 assert( is_empty_list( & mmesa->swapped ) );
699 }
700
701 /* free the option cache */
702 driDestroyOptionCache (&mmesa->optionCache);
703
704 FREE(mmesa);
705 }
706
707 if (MGA_DEBUG&DEBUG_VERBOSE_DRI)
708 fprintf( stderr, "[%s:%d] mgaDestroyContext done\n",
709 __FILE__, __LINE__ );
710 }
711
712
713 static GLboolean
714 mgaCreateBuffer( __DRIscreenPrivate *driScrnPriv,
715 __DRIdrawablePrivate *driDrawPriv,
716 const __GLcontextModes *mesaVis,
717 GLboolean isPixmap )
718 {
719 mgaScreenPrivate *screen = (mgaScreenPrivate *) driScrnPriv->private;
720
721 if (isPixmap) {
722 return GL_FALSE; /* not implemented */
723 }
724 else {
725 GLboolean swStencil = (mesaVis->stencilBits > 0 &&
726 mesaVis->depthBits != 24);
727
728 #if 0
729 driDrawPriv->driverPrivate = (void *)
730 _mesa_create_framebuffer(mesaVis,
731 GL_FALSE, /* software depth buffer? */
732 swStencil,
733 mesaVis->accumRedBits > 0,
734 mesaVis->alphaBits > 0 );
735 #else
736 struct gl_framebuffer *fb = _mesa_create_framebuffer(mesaVis);
737
738 {
739 driRenderbuffer *frontRb
740 = driNewRenderbuffer(GL_RGBA,
741 NULL,
742 screen->cpp,
743 screen->frontOffset, screen->frontPitch,
744 driDrawPriv);
745 mgaSetSpanFunctions(frontRb, mesaVis);
746 _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &frontRb->Base);
747 }
748
749 if (mesaVis->doubleBufferMode) {
750 driRenderbuffer *backRb
751 = driNewRenderbuffer(GL_RGBA,
752 NULL,
753 screen->cpp,
754 screen->backOffset, screen->backPitch,
755 driDrawPriv);
756 mgaSetSpanFunctions(backRb, mesaVis);
757 _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &backRb->Base);
758 }
759
760 if (mesaVis->depthBits == 16) {
761 driRenderbuffer *depthRb
762 = driNewRenderbuffer(GL_DEPTH_COMPONENT16,
763 NULL,
764 screen->cpp,
765 screen->depthOffset, screen->depthPitch,
766 driDrawPriv);
767 mgaSetSpanFunctions(depthRb, mesaVis);
768 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
769 }
770 else if (mesaVis->depthBits == 24) {
771 /* XXX is this right? */
772 if (mesaVis->stencilBits) {
773 driRenderbuffer *depthRb
774 = driNewRenderbuffer(GL_DEPTH_COMPONENT24,
775 NULL,
776 screen->cpp,
777 screen->depthOffset, screen->depthPitch,
778 driDrawPriv);
779 mgaSetSpanFunctions(depthRb, mesaVis);
780 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
781 }
782 else {
783 driRenderbuffer *depthRb
784 = driNewRenderbuffer(GL_DEPTH_COMPONENT32,
785 NULL,
786 screen->cpp,
787 screen->depthOffset, screen->depthPitch,
788 driDrawPriv);
789 mgaSetSpanFunctions(depthRb, mesaVis);
790 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
791 }
792 }
793 else if (mesaVis->depthBits == 32) {
794 driRenderbuffer *depthRb
795 = driNewRenderbuffer(GL_DEPTH_COMPONENT32,
796 NULL,
797 screen->cpp,
798 screen->depthOffset, screen->depthPitch,
799 driDrawPriv);
800 mgaSetSpanFunctions(depthRb, mesaVis);
801 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
802 }
803
804 if (mesaVis->stencilBits > 0 && !swStencil) {
805 driRenderbuffer *stencilRb
806 = driNewRenderbuffer(GL_STENCIL_INDEX8_EXT,
807 NULL,
808 screen->cpp,
809 screen->depthOffset, screen->depthPitch,
810 driDrawPriv);
811 mgaSetSpanFunctions(stencilRb, mesaVis);
812 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &stencilRb->Base);
813 }
814
815 _mesa_add_soft_renderbuffers(fb,
816 GL_FALSE, /* color */
817 GL_FALSE, /* depth */
818 swStencil,
819 mesaVis->accumRedBits > 0,
820 GL_FALSE, /* alpha */
821 GL_FALSE /* aux */);
822 driDrawPriv->driverPrivate = (void *) fb;
823 #endif
824
825 return (driDrawPriv->driverPrivate != NULL);
826 }
827 }
828
829
830 static void
831 mgaDestroyBuffer(__DRIdrawablePrivate *driDrawPriv)
832 {
833 _mesa_destroy_framebuffer((GLframebuffer *) (driDrawPriv->driverPrivate));
834 }
835
836 static void
837 mgaSwapBuffers(__DRIdrawablePrivate *dPriv)
838 {
839 if (dPriv->driContextPriv && dPriv->driContextPriv->driverPrivate) {
840 mgaContextPtr mmesa;
841 GLcontext *ctx;
842 mmesa = (mgaContextPtr) dPriv->driContextPriv->driverPrivate;
843 ctx = mmesa->glCtx;
844
845 if (ctx->Visual.doubleBufferMode) {
846 _mesa_notifySwapBuffers( ctx );
847 mgaCopyBuffer( dPriv );
848 }
849 } else {
850 /* XXX this shouldn't be an error but we can't handle it for now */
851 _mesa_problem(NULL, "%s: drawable has no context!\n", __FUNCTION__);
852 }
853 }
854
855 static GLboolean
856 mgaUnbindContext(__DRIcontextPrivate *driContextPriv)
857 {
858 mgaContextPtr mmesa = (mgaContextPtr) driContextPriv->driverPrivate;
859 if (mmesa)
860 mmesa->dirty = ~0;
861
862 return GL_TRUE;
863 }
864
865 /* This looks buggy to me - the 'b' variable isn't used anywhere...
866 * Hmm - It seems that the drawable is already hooked in to
867 * driDrawablePriv.
868 *
869 * But why are we doing context initialization here???
870 */
871 static GLboolean
872 mgaMakeCurrent(__DRIcontextPrivate *driContextPriv,
873 __DRIdrawablePrivate *driDrawPriv,
874 __DRIdrawablePrivate *driReadPriv)
875 {
876 if (driContextPriv) {
877 mgaContextPtr mmesa = (mgaContextPtr) driContextPriv->driverPrivate;
878
879 if (mmesa->driDrawable != driDrawPriv) {
880 driDrawableInitVBlank( driDrawPriv, mmesa->vblank_flags );
881 mmesa->driDrawable = driDrawPriv;
882 mmesa->dirty = ~0;
883 mmesa->dirty_cliprects = (MGA_FRONT|MGA_BACK);
884 mmesa->mesa_drawable = driDrawPriv;
885 }
886
887 mmesa->driReadable = driReadPriv;
888
889 _mesa_make_current(mmesa->glCtx,
890 (GLframebuffer *) driDrawPriv->driverPrivate,
891 (GLframebuffer *) driReadPriv->driverPrivate);
892 }
893 else {
894 _mesa_make_current(NULL, NULL, NULL);
895 }
896
897 return GL_TRUE;
898 }
899
900
901 void mgaGetLock( mgaContextPtr mmesa, GLuint flags )
902 {
903 __DRIdrawablePrivate *dPriv = mmesa->driDrawable;
904 drm_mga_sarea_t *sarea = mmesa->sarea;
905 int me = mmesa->hHWContext;
906 int i;
907
908 drmGetLock(mmesa->driFd, mmesa->hHWContext, flags);
909
910 if (*(dPriv->pStamp) != mmesa->lastStamp) {
911 mmesa->lastStamp = *(dPriv->pStamp);
912 mmesa->SetupNewInputs |= VERT_BIT_POS;
913 mmesa->dirty_cliprects = (MGA_FRONT|MGA_BACK);
914 mgaUpdateRects( mmesa, (MGA_FRONT|MGA_BACK) );
915 driUpdateFramebufferSize(mmesa->glCtx, dPriv);
916 }
917
918 mmesa->dirty |= MGA_UPLOAD_CONTEXT | MGA_UPLOAD_CLIPRECTS;
919
920 mmesa->sarea->dirty |= MGA_UPLOAD_CONTEXT;
921
922 if (sarea->ctxOwner != me) {
923 mmesa->dirty |= (MGA_UPLOAD_CONTEXT | MGA_UPLOAD_TEX0 |
924 MGA_UPLOAD_TEX1 | MGA_UPLOAD_PIPE);
925 sarea->ctxOwner=me;
926 }
927
928 for ( i = 0 ; i < mmesa->nr_heaps ; i++ ) {
929 DRI_AGE_TEXTURES( mmesa->texture_heaps[ i ] );
930 }
931 }
932
933
934 static const struct __DriverAPIRec mgaAPI = {
935 .InitDriver = mgaInitDriver,
936 .DestroyScreen = mgaDestroyScreen,
937 .CreateContext = mgaCreateContext,
938 .DestroyContext = mgaDestroyContext,
939 .CreateBuffer = mgaCreateBuffer,
940 .DestroyBuffer = mgaDestroyBuffer,
941 .SwapBuffers = mgaSwapBuffers,
942 .MakeCurrent = mgaMakeCurrent,
943 .UnbindContext = mgaUnbindContext,
944 .GetSwapInfo = getSwapInfo,
945 .GetMSC = driGetMSC32,
946 .WaitForMSC = driWaitForMSC32,
947 .WaitForSBC = NULL,
948 .SwapBuffersMSC = NULL
949 };
950
951
952 /**
953 * This is the bootstrap function for the driver. libGL supplies all of the
954 * requisite information about the system, and the driver initializes itself.
955 * This routine also fills in the linked list pointed to by \c driver_modes
956 * with the \c __GLcontextModes that the driver can support for windows or
957 * pbuffers.
958 *
959 * \return A pointer to a \c __DRIscreenPrivate on success, or \c NULL on
960 * failure.
961 */
962 PUBLIC
963 void * __driCreateNewScreen_20050727( __DRInativeDisplay *dpy, int scrn, __DRIscreen *psc,
964 const __GLcontextModes * modes,
965 const __DRIversion * ddx_version,
966 const __DRIversion * dri_version,
967 const __DRIversion * drm_version,
968 const __DRIframebuffer * frame_buffer,
969 drmAddress pSAREA, int fd,
970 int internal_api_version,
971 const __DRIinterfaceMethods * interface,
972 __GLcontextModes ** driver_modes )
973
974 {
975 __DRIscreenPrivate *psp;
976 static const __DRIversion ddx_expected = { 1, 2, 0 };
977 static const __DRIversion dri_expected = { 4, 0, 0 };
978 static const __DRIversion drm_expected = { 3, 0, 0 };
979
980 dri_interface = interface;
981
982 if ( ! driCheckDriDdxDrmVersions2( "MGA",
983 dri_version, & dri_expected,
984 ddx_version, & ddx_expected,
985 drm_version, & drm_expected ) ) {
986 return NULL;
987 }
988
989 psp = __driUtilCreateNewScreen(dpy, scrn, psc, NULL,
990 ddx_version, dri_version, drm_version,
991 frame_buffer, pSAREA, fd,
992 internal_api_version, &mgaAPI);
993 if ( psp != NULL ) {
994 MGADRIPtr dri_priv = (MGADRIPtr) psp->pDevPriv;
995 *driver_modes = mgaFillInModes( dri_priv->cpp * 8,
996 (dri_priv->cpp == 2) ? 16 : 24,
997 (dri_priv->cpp == 2) ? 0 : 8,
998 (dri_priv->backOffset != dri_priv->depthOffset) );
999
1000 /* Calling driInitExtensions here, with a NULL context pointer, does not actually
1001 * enable the extensions. It just makes sure that all the dispatch offsets for all
1002 * the extensions that *might* be enables are known. This is needed because the
1003 * dispatch offsets need to be known when _mesa_context_create is called, but we can't
1004 * enable the extensions until we have a context pointer.
1005 *
1006 * Hello chicken. Hello egg. How are you two today?
1007 */
1008 driInitExtensions( NULL, card_extensions, GL_FALSE );
1009 driInitExtensions( NULL, g400_extensions, GL_FALSE );
1010 driInitSingleExtension( NULL, ARB_vp_extension );
1011 driInitExtensions( NULL, NV_vp_extensions, GL_FALSE );
1012
1013 }
1014
1015 return (void *) psp;
1016 }
1017
1018
1019 /**
1020 * Get information about previous buffer swaps.
1021 */
1022 static int
1023 getSwapInfo( __DRIdrawablePrivate *dPriv, __DRIswapInfo * sInfo )
1024 {
1025 mgaContextPtr mmesa;
1026
1027 if ( (dPriv == NULL) || (dPriv->driContextPriv == NULL)
1028 || (dPriv->driContextPriv->driverPrivate == NULL)
1029 || (sInfo == NULL) ) {
1030 return -1;
1031 }
1032
1033 mmesa = (mgaContextPtr) dPriv->driContextPriv->driverPrivate;
1034 sInfo->swap_count = mmesa->swap_count;
1035 sInfo->swap_ust = mmesa->swap_ust;
1036 sInfo->swap_missed_count = mmesa->swap_missed_count;
1037
1038 sInfo->swap_missed_usage = (sInfo->swap_missed_count != 0)
1039 ? driCalculateSwapUsage( dPriv, 0, mmesa->swap_missed_ust )
1040 : 0.0;
1041
1042 return 0;
1043 }