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