Drop GLcontext typedef and use struct gl_context instead
[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/simple_list.h"
39 #include "main/imports.h"
40 #include "main/framebuffer.h"
41 #include "main/renderbuffer.h"
42
43 #include "swrast/swrast.h"
44 #include "swrast_setup/swrast_setup.h"
45 #include "tnl/tnl.h"
46 #include "vbo/vbo.h"
47
48 #include "tnl/t_pipeline.h"
49
50 #include "drivers/common/driverfuncs.h"
51
52 #include "mgadd.h"
53 #include "mgastate.h"
54 #include "mgatex.h"
55 #include "mgaspan.h"
56 #include "mgaioctl.h"
57 #include "mgatris.h"
58 #include "mgavb.h"
59 #include "mgapixel.h"
60 #include "mga_dri.h"
61
62 #include "utils.h"
63 #include "vblank.h"
64
65 #include "drirenderbuffer.h"
66
67 #include "GL/internal/dri_interface.h"
68
69 #define need_GL_ARB_vertex_program
70 #define need_GL_EXT_fog_coord
71 #define need_GL_EXT_gpu_program_parameters
72 #define need_GL_EXT_secondary_color
73 #if 0
74 #define need_GL_EXT_paletted_texture
75 #endif
76 #define need_GL_APPLE_vertex_array_object
77 #define need_GL_NV_vertex_program
78 #include "main/remap_helper.h"
79
80 /* MGA configuration
81 */
82 #include "xmlpool.h"
83
84 PUBLIC const char __driConfigOptions[] =
85 DRI_CONF_BEGIN
86 DRI_CONF_SECTION_PERFORMANCE
87 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_0)
88 DRI_CONF_SECTION_END
89 DRI_CONF_SECTION_QUALITY
90 DRI_CONF_TEXTURE_DEPTH(DRI_CONF_TEXTURE_DEPTH_FB)
91 DRI_CONF_COLOR_REDUCTION(DRI_CONF_COLOR_REDUCTION_DITHER)
92 DRI_CONF_SECTION_END
93 DRI_CONF_SECTION_SOFTWARE
94 DRI_CONF_ARB_VERTEX_PROGRAM(true)
95 DRI_CONF_NV_VERTEX_PROGRAM(true)
96 DRI_CONF_SECTION_END
97 DRI_CONF_SECTION_DEBUG
98 DRI_CONF_NO_RAST(false)
99 DRI_CONF_SECTION_END
100 DRI_CONF_END;
101 static const GLuint __driNConfigOptions = 6;
102
103 #ifndef MGA_DEBUG
104 int MGA_DEBUG = 0;
105 #endif
106
107 static const __DRIconfig **
108 mgaFillInModes( __DRIscreen *psp,
109 unsigned pixel_bits, unsigned depth_bits,
110 unsigned stencil_bits, GLboolean have_back_buffer )
111 {
112 __DRIconfig **configs;
113 struct gl_config * m;
114 unsigned depth_buffer_factor;
115 unsigned back_buffer_factor;
116 GLenum fb_format;
117 GLenum fb_type;
118 int i;
119
120 /* GLX_SWAP_COPY_OML is only supported because the MGA driver doesn't
121 * support pageflipping at all.
122 */
123 static const GLenum back_buffer_modes[] = {
124 GLX_NONE, GLX_SWAP_UNDEFINED_OML, GLX_SWAP_COPY_OML
125 };
126
127 uint8_t depth_bits_array[3];
128 uint8_t stencil_bits_array[3];
129 uint8_t msaa_samples_array[1];
130
131
132 depth_bits_array[0] = 0;
133 depth_bits_array[1] = depth_bits;
134 depth_bits_array[2] = depth_bits;
135
136 /* Just like with the accumulation buffer, always provide some modes
137 * with a stencil buffer. It will be a sw fallback, but some apps won't
138 * care about that.
139 */
140 stencil_bits_array[0] = 0;
141 stencil_bits_array[1] = 0;
142 stencil_bits_array[2] = (stencil_bits == 0) ? 8 : stencil_bits;
143
144 msaa_samples_array[0] = 0;
145
146 depth_buffer_factor = ((depth_bits != 0) || (stencil_bits != 0)) ? 3 : 1;
147 back_buffer_factor = (have_back_buffer) ? 2 : 1;
148
149 if ( pixel_bits == 16 ) {
150 fb_format = GL_RGB;
151 fb_type = GL_UNSIGNED_SHORT_5_6_5;
152 }
153 else {
154 fb_format = GL_BGR;
155 fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
156 }
157
158 configs = driCreateConfigs(fb_format, fb_type,
159 depth_bits_array, stencil_bits_array,
160 depth_buffer_factor,
161 back_buffer_modes, back_buffer_factor,
162 msaa_samples_array, 1, GL_TRUE);
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 &driMediaStreamCounterExtension.base,
185 NULL
186 };
187
188 static GLboolean
189 mgaInitDriver(__DRIscreen *sPriv)
190 {
191 mgaScreenPrivate *mgaScreen;
192 MGADRIPtr serverInfo = (MGADRIPtr)sPriv->pDevPriv;
193
194 if (sPriv->devPrivSize != sizeof(MGADRIRec)) {
195 fprintf(stderr,"\nERROR! sizeof(MGADRIRec) does not match passed size from device driver\n");
196 return GL_FALSE;
197 }
198
199 /* Allocate the private area */
200 mgaScreen = (mgaScreenPrivate *)MALLOC(sizeof(mgaScreenPrivate));
201 if (!mgaScreen) {
202 __driUtilMessage("Couldn't malloc screen struct");
203 return GL_FALSE;
204 }
205
206 mgaScreen->sPriv = sPriv;
207 sPriv->private = (void *)mgaScreen;
208
209 if (sPriv->drm_version.minor >= 1) {
210 int ret;
211 drm_mga_getparam_t gp;
212
213 gp.param = MGA_PARAM_IRQ_NR;
214 gp.value = &mgaScreen->irq;
215 mgaScreen->irq = 0;
216
217 ret = drmCommandWriteRead( sPriv->fd, DRM_MGA_GETPARAM,
218 &gp, sizeof(gp));
219 if (ret) {
220 fprintf(stderr, "drmMgaGetParam (MGA_PARAM_IRQ_NR): %d\n", ret);
221 FREE(mgaScreen);
222 sPriv->private = NULL;
223 return GL_FALSE;
224 }
225 }
226
227 sPriv->extensions = mgaScreenExtensions;
228
229 if (serverInfo->chipset != MGA_CARD_TYPE_G200 &&
230 serverInfo->chipset != MGA_CARD_TYPE_G400) {
231 FREE(mgaScreen);
232 sPriv->private = NULL;
233 __driUtilMessage("Unrecognized chipset");
234 return GL_FALSE;
235 }
236
237
238 mgaScreen->chipset = serverInfo->chipset;
239 mgaScreen->cpp = serverInfo->cpp;
240
241 mgaScreen->agpMode = serverInfo->agpMode;
242
243 mgaScreen->frontPitch = serverInfo->frontPitch;
244 mgaScreen->frontOffset = serverInfo->frontOffset;
245 mgaScreen->backOffset = serverInfo->backOffset;
246 mgaScreen->backPitch = serverInfo->backPitch;
247 mgaScreen->depthOffset = serverInfo->depthOffset;
248 mgaScreen->depthPitch = serverInfo->depthPitch;
249
250
251 /* The only reason that the MMIO region needs to be accessable and the
252 * primary DMA region base address needs to be known is so that the driver
253 * can busy wait for certain DMA operations to complete (see
254 * mgaWaitForFrameCompletion in mgaioctl.c).
255 *
256 * Starting with MGA DRM version 3.2, these are completely unneeded as
257 * there is a new, in-kernel mechanism for handling the wait.
258 */
259
260 if (mgaScreen->sPriv->drm_version.minor < 2) {
261 mgaScreen->mmio.handle = serverInfo->registers.handle;
262 mgaScreen->mmio.size = serverInfo->registers.size;
263 if ( drmMap( sPriv->fd,
264 mgaScreen->mmio.handle, mgaScreen->mmio.size,
265 &mgaScreen->mmio.map ) < 0 ) {
266 FREE( mgaScreen );
267 sPriv->private = NULL;
268 __driUtilMessage( "Couldn't map MMIO registers" );
269 return GL_FALSE;
270 }
271
272 mgaScreen->primary.handle = serverInfo->primary.handle;
273 mgaScreen->primary.size = serverInfo->primary.size;
274 }
275 else {
276 (void) memset( & mgaScreen->primary, 0, sizeof( mgaScreen->primary ) );
277 (void) memset( & mgaScreen->mmio, 0, sizeof( mgaScreen->mmio ) );
278 }
279
280 mgaScreen->textureOffset[MGA_CARD_HEAP] = serverInfo->textureOffset;
281 mgaScreen->textureOffset[MGA_AGP_HEAP] = (serverInfo->agpTextureOffset |
282 PDEA_pagpxfer_enable | 1);
283
284 mgaScreen->textureSize[MGA_CARD_HEAP] = serverInfo->textureSize;
285 mgaScreen->textureSize[MGA_AGP_HEAP] = serverInfo->agpTextureSize;
286
287
288 /* The texVirtual array stores the base addresses in the CPU's address
289 * space of the texture memory pools. The base address of the on-card
290 * memory pool is calculated as an offset of the base of video memory. The
291 * AGP texture pool has to be mapped into the processes address space by
292 * the DRM.
293 */
294
295 mgaScreen->texVirtual[MGA_CARD_HEAP] = (char *)(mgaScreen->sPriv->pFB +
296 serverInfo->textureOffset);
297
298 if ( serverInfo->agpTextureSize > 0 ) {
299 if (drmMap(sPriv->fd, serverInfo->agpTextureOffset,
300 serverInfo->agpTextureSize,
301 (drmAddress *)&mgaScreen->texVirtual[MGA_AGP_HEAP]) != 0) {
302 FREE(mgaScreen);
303 sPriv->private = NULL;
304 __driUtilMessage("Couldn't map agptexture region");
305 return GL_FALSE;
306 }
307 }
308
309
310 /* For calculating setupdma addresses.
311 */
312
313 mgaScreen->bufs = drmMapBufs(sPriv->fd);
314 if (!mgaScreen->bufs) {
315 FREE(mgaScreen);
316 sPriv->private = NULL;
317 __driUtilMessage("Couldn't map dma buffers");
318 return GL_FALSE;
319 }
320 mgaScreen->sarea_priv_offset = serverInfo->sarea_priv_offset;
321
322 /* parse information in __driConfigOptions */
323 driParseOptionInfo (&mgaScreen->optionCache,
324 __driConfigOptions, __driNConfigOptions);
325
326 return GL_TRUE;
327 }
328
329
330 static void
331 mgaDestroyScreen(__DRIscreen *sPriv)
332 {
333 mgaScreenPrivate *mgaScreen = (mgaScreenPrivate *) sPriv->private;
334
335 if (MGA_DEBUG&DEBUG_VERBOSE_DRI)
336 fprintf(stderr, "mgaDestroyScreen\n");
337
338 drmUnmapBufs(mgaScreen->bufs);
339
340
341 /* free all option information */
342 driDestroyOptionInfo (&mgaScreen->optionCache);
343
344 FREE(mgaScreen);
345 sPriv->private = NULL;
346 }
347
348
349 extern const struct tnl_pipeline_stage _mga_render_stage;
350
351 static const struct tnl_pipeline_stage *mga_pipeline[] = {
352 &_tnl_vertex_transform_stage,
353 &_tnl_normal_transform_stage,
354 &_tnl_lighting_stage,
355 &_tnl_fog_coordinate_stage,
356 &_tnl_texgen_stage,
357 &_tnl_texture_transform_stage,
358 &_tnl_vertex_program_stage,
359
360 /* REMOVE: point attenuation stage */
361 #if 0
362 &_mga_render_stage, /* ADD: unclipped rastersetup-to-dma */
363 /* Need new ioctl for wacceptseq */
364 #endif
365 &_tnl_render_stage,
366 0,
367 };
368
369
370 static const struct dri_extension g400_extensions[] =
371 {
372 { "GL_ARB_multitexture", NULL },
373 { "GL_ARB_texture_env_add", NULL },
374 { "GL_ARB_texture_env_combine", NULL },
375 { "GL_ARB_texture_env_crossbar", NULL },
376 { "GL_EXT_texture_env_combine", NULL },
377 { "GL_EXT_texture_edge_clamp", NULL },
378 { "GL_ATI_texture_env_combine3", NULL },
379 { NULL, NULL }
380 };
381
382 static const struct dri_extension card_extensions[] =
383 {
384 { "GL_ARB_texture_rectangle", NULL },
385 { "GL_EXT_blend_logic_op", NULL },
386 { "GL_EXT_fog_coord", GL_EXT_fog_coord_functions },
387 /* paletted_textures currently doesn't work, but we could fix them later */
388 #if defined( need_GL_EXT_paletted_texture )
389 { "GL_EXT_shared_texture_palette", NULL },
390 { "GL_EXT_paletted_texture", GL_EXT_paletted_texture_functions },
391 #endif
392 { "GL_EXT_secondary_color", GL_EXT_secondary_color_functions },
393 { "GL_EXT_stencil_wrap", NULL },
394 { "GL_APPLE_vertex_array_object", GL_APPLE_vertex_array_object_functions },
395 { "GL_MESA_ycbcr_texture", NULL },
396 { NULL, NULL }
397 };
398
399 static const struct dri_extension ARB_vp_extensions[] = {
400 { "GL_ARB_vertex_program", GL_ARB_vertex_program_functions },
401 { "GL_EXT_gpu_program_parameters", GL_EXT_gpu_program_parameters_functions },
402 { NULL, NULL }
403 };
404
405 static const struct dri_extension NV_vp_extensions[] = {
406 { "GL_NV_vertex_program", GL_NV_vertex_program_functions },
407 { "GL_NV_vertex_program1_1", NULL },
408 { NULL, NULL }
409 };
410
411 static const struct dri_debug_control debug_control[] =
412 {
413 { "fall", DEBUG_VERBOSE_FALLBACK },
414 { "tex", DEBUG_VERBOSE_TEXTURE },
415 { "ioctl", DEBUG_VERBOSE_IOCTL },
416 { "verb", DEBUG_VERBOSE_MSG },
417 { "dri", DEBUG_VERBOSE_DRI },
418 { NULL, 0 }
419 };
420
421
422 static GLboolean
423 mgaCreateContext( gl_api api,
424 const struct gl_config *mesaVis,
425 __DRIcontext *driContextPriv,
426 void *sharedContextPrivate )
427 {
428 int i;
429 unsigned maxlevels;
430 struct gl_context *ctx, *shareCtx;
431 mgaContextPtr mmesa;
432 __DRIscreen *sPriv = driContextPriv->driScreenPriv;
433 mgaScreenPrivate *mgaScreen = (mgaScreenPrivate *)sPriv->private;
434 drm_mga_sarea_t *saPriv = (drm_mga_sarea_t *)(((char*)sPriv->pSAREA)+
435 mgaScreen->sarea_priv_offset);
436 struct dd_function_table functions;
437
438 if (MGA_DEBUG&DEBUG_VERBOSE_DRI)
439 fprintf(stderr, "mgaCreateContext\n");
440
441 /* allocate mga context */
442 mmesa = (mgaContextPtr) CALLOC(sizeof(mgaContext));
443 if (!mmesa) {
444 return GL_FALSE;
445 }
446
447 /* Init default driver functions then plug in our Radeon-specific functions
448 * (the texture functions are especially important)
449 */
450 _mesa_init_driver_functions( &functions );
451 mgaInitDriverFuncs( &functions );
452 mgaInitTextureFuncs( &functions );
453 mgaInitIoctlFuncs( &functions );
454
455 /* Allocate the Mesa context */
456 if (sharedContextPrivate)
457 shareCtx = ((mgaContextPtr) sharedContextPrivate)->glCtx;
458 else
459 shareCtx = NULL;
460 mmesa->glCtx = _mesa_create_context(mesaVis, shareCtx,
461 &functions, (void *) mmesa);
462 if (!mmesa->glCtx) {
463 FREE(mmesa);
464 return GL_FALSE;
465 }
466 driContextPriv->driverPrivate = mmesa;
467
468 /* Init mga state */
469 mmesa->hHWContext = driContextPriv->hHWContext;
470 mmesa->driFd = sPriv->fd;
471 mmesa->driHwLock = &sPriv->pSAREA->lock;
472
473 mmesa->mgaScreen = mgaScreen;
474 mmesa->driScreen = sPriv;
475 mmesa->sarea = (void *)saPriv;
476
477 /* Parse configuration files */
478 driParseConfigFiles (&mmesa->optionCache, &mgaScreen->optionCache,
479 sPriv->myNum, "mga");
480
481 (void) memset( mmesa->texture_heaps, 0, sizeof( mmesa->texture_heaps ) );
482 make_empty_list( & mmesa->swapped );
483
484 mmesa->nr_heaps = mgaScreen->texVirtual[MGA_AGP_HEAP] ? 2 : 1;
485 for ( i = 0 ; i < mmesa->nr_heaps ; i++ ) {
486 mmesa->texture_heaps[i] = driCreateTextureHeap( i, mmesa,
487 mgaScreen->textureSize[i],
488 6,
489 MGA_NR_TEX_REGIONS,
490 (drmTextureRegionPtr)mmesa->sarea->texList[i],
491 &mmesa->sarea->texAge[i],
492 &mmesa->swapped,
493 sizeof( mgaTextureObject_t ),
494 (destroy_texture_object_t *) mgaDestroyTexObj );
495 }
496
497 /* Set the maximum texture size small enough that we can guarentee
498 * that both texture units can bind a maximal texture and have them
499 * on the card at once.
500 */
501 ctx = mmesa->glCtx;
502 if ( mgaScreen->chipset == MGA_CARD_TYPE_G200 ) {
503 ctx->Const.MaxTextureUnits = 1;
504 ctx->Const.MaxTextureImageUnits = 1;
505 ctx->Const.MaxTextureCoordUnits = 1;
506 maxlevels = G200_TEX_MAXLEVELS;
507
508 }
509 else {
510 ctx->Const.MaxTextureUnits = 2;
511 ctx->Const.MaxTextureImageUnits = 2;
512 ctx->Const.MaxTextureCoordUnits = 2;
513 maxlevels = G400_TEX_MAXLEVELS;
514 }
515
516 driCalculateMaxTextureLevels( mmesa->texture_heaps,
517 mmesa->nr_heaps,
518 & ctx->Const,
519 4,
520 11, /* max 2D texture size is 2048x2048 */
521 0, /* 3D textures unsupported. */
522 0, /* cube textures unsupported. */
523 11, /* max texture rect size is 2048x2048 */
524 maxlevels,
525 GL_FALSE,
526 0 );
527
528 ctx->Const.MinLineWidth = 1.0;
529 ctx->Const.MinLineWidthAA = 1.0;
530 ctx->Const.MaxLineWidth = 10.0;
531 ctx->Const.MaxLineWidthAA = 10.0;
532 ctx->Const.LineWidthGranularity = 1.0;
533
534 ctx->Const.MaxDrawBuffers = 1;
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(__DRIcontext *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( __DRIscreen *driScrnPriv,
697 __DRIdrawable *driDrawPriv,
698 const struct gl_config *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(MESA_FORMAT_ARGB8888,
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(MESA_FORMAT_ARGB8888,
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(MESA_FORMAT_Z16,
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(MESA_FORMAT_Z24_S8,
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(MESA_FORMAT_Z32,
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(MESA_FORMAT_Z32,
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(MESA_FORMAT_S8,
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(__DRIdrawable *driDrawPriv)
814 {
815 _mesa_reference_framebuffer((struct gl_framebuffer **)(&(driDrawPriv->driverPrivate)), NULL);
816 }
817
818 static void
819 mgaSwapBuffers(__DRIdrawable *dPriv)
820 {
821 if (dPriv->driContextPriv && dPriv->driContextPriv->driverPrivate) {
822 mgaContextPtr mmesa;
823 struct gl_context *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(__DRIcontext *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(__DRIcontext *driContextPriv,
855 __DRIdrawable *driDrawPriv,
856 __DRIdrawable *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 (struct gl_framebuffer *) driDrawPriv->driverPrivate,
879 (struct gl_framebuffer *) 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 __DRIdrawable *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 struct gl_config 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 if (!mgaInitDriver(psp))
945 return NULL;
946
947 return mgaFillInModes( psp,
948 dri_priv->cpp * 8,
949 (dri_priv->cpp == 2) ? 16 : 24,
950 (dri_priv->cpp == 2) ? 0 : 8,
951 (dri_priv->backOffset != dri_priv->depthOffset) );
952 }
953
954
955 /**
956 * Get information about previous buffer swaps.
957 */
958 static int
959 getSwapInfo( __DRIdrawable *dPriv, __DRIswapInfo * sInfo )
960 {
961 mgaContextPtr mmesa;
962
963 if ( (dPriv == NULL) || (dPriv->driContextPriv == NULL)
964 || (dPriv->driContextPriv->driverPrivate == NULL)
965 || (sInfo == NULL) ) {
966 return -1;
967 }
968
969 mmesa = (mgaContextPtr) dPriv->driContextPriv->driverPrivate;
970 sInfo->swap_count = mmesa->swap_count;
971 sInfo->swap_ust = mmesa->swap_ust;
972 sInfo->swap_missed_count = mmesa->swap_missed_count;
973
974 sInfo->swap_missed_usage = (sInfo->swap_missed_count != 0)
975 ? driCalculateSwapUsage( dPriv, 0, mmesa->swap_missed_ust )
976 : 0.0;
977
978 return 0;
979 }
980
981 const struct __DriverAPIRec driDriverAPI = {
982 .InitScreen = mgaInitScreen,
983 .DestroyScreen = mgaDestroyScreen,
984 .CreateContext = mgaCreateContext,
985 .DestroyContext = mgaDestroyContext,
986 .CreateBuffer = mgaCreateBuffer,
987 .DestroyBuffer = mgaDestroyBuffer,
988 .SwapBuffers = mgaSwapBuffers,
989 .MakeCurrent = mgaMakeCurrent,
990 .UnbindContext = mgaUnbindContext,
991 .GetSwapInfo = getSwapInfo,
992 .GetDrawableMSC = driDrawableGetMSC32,
993 .WaitForMSC = driWaitForMSC32,
994 .WaitForSBC = NULL,
995 .SwapBuffersMSC = NULL
996 };
997
998 /* This is the table of extensions that the loader will dlsym() for. */
999 PUBLIC const __DRIextension *__driDriverExtensions[] = {
1000 &driCoreExtension.base,
1001 &driLegacyExtension.base,
1002 NULL
1003 };