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