f8d68d7874eae519767766a010fab3072607e97b
[mesa.git] / src / mesa / drivers / dri / savage / savage_xmesa.c
1 /*
2 * Copyright 1998-2003 VIA Technologies, Inc. All Rights Reserved.
3 * Copyright 2001-2003 S3 Graphics, Inc. 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 * the rights to use, copy, modify, merge, publish, distribute, sub license,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
14 * of the 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 * VIA, S3 GRAPHICS, 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 OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25
26 #include <X11/Xlibint.h>
27 #include <stdio.h>
28
29 #include "savagecontext.h"
30 #include "context.h"
31 #include "matrix.h"
32 #include "framebuffer.h"
33 #include "renderbuffer.h"
34 #include "simple_list.h"
35
36 #include "utils.h"
37
38 #include "extensions.h"
39
40 #include "swrast/swrast.h"
41 #include "swrast_setup/swrast_setup.h"
42 #include "tnl/tnl.h"
43 #include "array_cache/acache.h"
44
45 #include "tnl/t_pipeline.h"
46
47 #include "drivers/common/driverfuncs.h"
48
49 #include "savagedd.h"
50 #include "savagestate.h"
51 #include "savagetex.h"
52 #include "savagespan.h"
53 #include "savagetris.h"
54 #include "savageioctl.h"
55 #include "savage_bci.h"
56
57 #include "savage_dri.h"
58
59 #include "drirenderbuffer.h"
60 #include "texmem.h"
61
62 #define need_GL_ARB_multisample
63 #define need_GL_ARB_texture_compression
64 #define need_GL_EXT_secondary_color
65 #include "extension_helper.h"
66
67 #include "xmlpool.h"
68
69 /* Driver-specific options
70 */
71 #define SAVAGE_ENABLE_VDMA(def) \
72 DRI_CONF_OPT_BEGIN(enable_vdma,bool,def) \
73 DRI_CONF_DESC(en,"Use DMA for vertex transfers") \
74 DRI_CONF_DESC(de,"Benutze DMA für Vertextransfers") \
75 DRI_CONF_OPT_END
76 #define SAVAGE_ENABLE_FASTPATH(def) \
77 DRI_CONF_OPT_BEGIN(enable_fastpath,bool,def) \
78 DRI_CONF_DESC(en,"Use fast path for unclipped primitives") \
79 DRI_CONF_DESC(de,"Schneller Codepfad für ungeschnittene Polygone") \
80 DRI_CONF_OPT_END
81 #define SAVAGE_SYNC_FRAMES(def) \
82 DRI_CONF_OPT_BEGIN(sync_frames,bool,def) \
83 DRI_CONF_DESC(en,"Synchronize with graphics hardware after each frame") \
84 DRI_CONF_DESC(de,"Synchronisiere nach jedem Frame mit Grafikhardware") \
85 DRI_CONF_OPT_END
86
87 /* Configuration
88 */
89 PUBLIC const char __driConfigOptions[] =
90 DRI_CONF_BEGIN
91 DRI_CONF_SECTION_QUALITY
92 DRI_CONF_TEXTURE_DEPTH(DRI_CONF_TEXTURE_DEPTH_FB)
93 DRI_CONF_COLOR_REDUCTION(DRI_CONF_COLOR_REDUCTION_DITHER)
94 DRI_CONF_FLOAT_DEPTH(false)
95 DRI_CONF_SECTION_END
96 DRI_CONF_SECTION_PERFORMANCE
97 SAVAGE_ENABLE_VDMA(true)
98 SAVAGE_ENABLE_FASTPATH(true)
99 SAVAGE_SYNC_FRAMES(false)
100 DRI_CONF_MAX_TEXTURE_UNITS(2,1,2)
101 DRI_CONF_TEXTURE_HEAPS(DRI_CONF_TEXTURE_HEAPS_ALL)
102 DRI_CONF_FORCE_S3TC_ENABLE(false)
103 DRI_CONF_SECTION_END
104 DRI_CONF_SECTION_DEBUG
105 DRI_CONF_NO_RAST(false)
106 DRI_CONF_SECTION_END
107 DRI_CONF_END;
108 static const GLuint __driNConfigOptions = 10;
109
110
111 static const struct dri_debug_control debug_control[] =
112 {
113 { "fall", DEBUG_FALLBACKS },
114 { "api", DEBUG_VERBOSE_API },
115 { "tex", DEBUG_VERBOSE_TEX },
116 { "verb", DEBUG_VERBOSE_MSG },
117 { "dma", DEBUG_DMA },
118 { "state", DEBUG_STATE },
119 { NULL, 0 }
120 };
121 #ifndef SAVAGE_DEBUG
122 int SAVAGE_DEBUG = 0;
123 #endif
124
125
126 /*For time caculating test*/
127 #if defined(DEBUG_TIME) && DEBUG_TIME
128 struct timeval tv_s,tv_f;
129 unsigned long time_sum=0;
130 struct timeval tv_s1,tv_f1;
131 #endif
132
133 static const struct dri_extension card_extensions[] =
134 {
135 { "GL_ARB_multisample", GL_ARB_multisample_functions },
136 { "GL_ARB_multitexture", NULL },
137 { "GL_ARB_texture_compression", GL_ARB_texture_compression_functions },
138 { "GL_EXT_stencil_wrap", NULL },
139 { "GL_EXT_texture_lod_bias", NULL },
140 { "GL_EXT_secondary_color", GL_EXT_secondary_color_functions },
141 { NULL, NULL }
142 };
143
144 static const struct dri_extension s4_extensions[] =
145 {
146 { "GL_ARB_texture_env_add", NULL },
147 { "GL_ARB_texture_mirrored_repeat", NULL },
148 { NULL, NULL }
149 };
150
151 extern struct tnl_pipeline_stage _savage_texnorm_stage;
152 extern struct tnl_pipeline_stage _savage_render_stage;
153
154 static const struct tnl_pipeline_stage *savage_pipeline[] = {
155
156 &_tnl_vertex_transform_stage,
157 &_tnl_normal_transform_stage,
158 &_tnl_lighting_stage,
159 &_tnl_fog_coordinate_stage,
160 &_tnl_texgen_stage,
161 &_tnl_texture_transform_stage,
162 &_savage_texnorm_stage,
163 &_savage_render_stage,
164 &_tnl_render_stage,
165 0,
166 };
167
168
169 /* this is first function called in dirver*/
170
171 static GLboolean
172 savageInitDriver(__DRIscreenPrivate *sPriv)
173 {
174 savageScreenPrivate *savageScreen;
175 SAVAGEDRIPtr gDRIPriv = (SAVAGEDRIPtr)sPriv->pDevPriv;
176 PFNGLXSCRENABLEEXTENSIONPROC glx_enable_extension =
177 (PFNGLXSCRENABLEEXTENSIONPROC) (*dri_interface->getProcAddress("glxEnableExtension"));
178
179
180 if (sPriv->devPrivSize != sizeof(SAVAGEDRIRec)) {
181 fprintf(stderr,"\nERROR! sizeof(SAVAGEDRIRec) does not match passed size from device driver\n");
182 return GL_FALSE;
183 }
184
185 /* Allocate the private area */
186 savageScreen = (savageScreenPrivate *)Xmalloc(sizeof(savageScreenPrivate));
187 if (!savageScreen)
188 return GL_FALSE;
189
190 savageScreen->driScrnPriv = sPriv;
191 sPriv->private = (void *)savageScreen;
192
193 savageScreen->chipset=gDRIPriv->chipset;
194 savageScreen->width=gDRIPriv->width;
195 savageScreen->height=gDRIPriv->height;
196 savageScreen->mem=gDRIPriv->mem;
197 savageScreen->cpp=gDRIPriv->cpp;
198 savageScreen->zpp=gDRIPriv->zpp;
199
200 savageScreen->agpMode=gDRIPriv->agpMode;
201
202 savageScreen->bufferSize=gDRIPriv->bufferSize;
203
204 if (gDRIPriv->cpp == 4)
205 savageScreen->frontFormat = DV_PF_8888;
206 else
207 savageScreen->frontFormat = DV_PF_565;
208 savageScreen->frontOffset=gDRIPriv->frontOffset;
209 savageScreen->backOffset = gDRIPriv->backOffset;
210 savageScreen->depthOffset=gDRIPriv->depthOffset;
211
212 savageScreen->textureOffset[SAVAGE_CARD_HEAP] =
213 gDRIPriv->textureOffset;
214 savageScreen->textureSize[SAVAGE_CARD_HEAP] =
215 gDRIPriv->textureSize;
216 savageScreen->logTextureGranularity[SAVAGE_CARD_HEAP] =
217 gDRIPriv->logTextureGranularity;
218
219 savageScreen->textureOffset[SAVAGE_AGP_HEAP] =
220 gDRIPriv->agpTextureHandle;
221 savageScreen->textureSize[SAVAGE_AGP_HEAP] =
222 gDRIPriv->agpTextureSize;
223 savageScreen->logTextureGranularity[SAVAGE_AGP_HEAP] =
224 gDRIPriv->logAgpTextureGranularity;
225
226 savageScreen->agpTextures.handle = gDRIPriv->agpTextureHandle;
227 savageScreen->agpTextures.size = gDRIPriv->agpTextureSize;
228 if (gDRIPriv->agpTextureSize) {
229 if (drmMap(sPriv->fd,
230 savageScreen->agpTextures.handle,
231 savageScreen->agpTextures.size,
232 (drmAddress *)&(savageScreen->agpTextures.map)) != 0) {
233 Xfree(savageScreen);
234 sPriv->private = NULL;
235 return GL_FALSE;
236 }
237 } else
238 savageScreen->agpTextures.map = NULL;
239
240 savageScreen->texVirtual[SAVAGE_CARD_HEAP] =
241 (drmAddress)(((GLubyte *)sPriv->pFB)+gDRIPriv->textureOffset);
242 savageScreen->texVirtual[SAVAGE_AGP_HEAP] =
243 (drmAddress)(savageScreen->agpTextures.map);
244
245 savageScreen->aperture.handle = gDRIPriv->apertureHandle;
246 savageScreen->aperture.size = gDRIPriv->apertureSize;
247 savageScreen->aperturePitch = gDRIPriv->aperturePitch;
248 if (drmMap(sPriv->fd,
249 savageScreen->aperture.handle,
250 savageScreen->aperture.size,
251 (drmAddress *)&savageScreen->aperture.map) != 0)
252 {
253 Xfree(savageScreen);
254 sPriv->private = NULL;
255 return GL_FALSE;
256 }
257
258 savageScreen->bufs = drmMapBufs(sPriv->fd);
259
260 savageScreen->sarea_priv_offset = gDRIPriv->sarea_priv_offset;
261
262 /* parse information in __driConfigOptions */
263 driParseOptionInfo (&savageScreen->optionCache,
264 __driConfigOptions, __driNConfigOptions);
265
266 if (glx_enable_extension != NULL) {
267 (*glx_enable_extension)(sPriv->psc->screenConfigs,
268 "GLX_SGI_make_current_read");
269 }
270
271 #if 0
272 savageDDFastPathInit();
273 savageDDTrifuncInit();
274 savageDDSetupInit();
275 #endif
276 return GL_TRUE;
277 }
278
279 /* Accessed by dlsym from dri_mesa_init.c
280 */
281 static void
282 savageDestroyScreen(__DRIscreenPrivate *sPriv)
283 {
284 savageScreenPrivate *savageScreen = (savageScreenPrivate *)sPriv->private;
285
286 if (savageScreen->bufs)
287 drmUnmapBufs(savageScreen->bufs);
288
289 /* free all option information */
290 driDestroyOptionInfo (&savageScreen->optionCache);
291
292 Xfree(savageScreen);
293 sPriv->private = NULL;
294 }
295
296 #if 0
297 GLvisual *XMesaCreateVisual(Display *dpy,
298 __DRIscreenPrivate *driScrnPriv,
299 const XVisualInfo *visinfo,
300 const __GLXvisualConfig *config)
301 {
302 /* Drivers may change the args to _mesa_create_visual() in order to
303 * setup special visuals.
304 */
305 return _mesa_create_visual( config->rgba,
306 config->doubleBuffer,
307 config->stereo,
308 _mesa_bitcount(visinfo->red_mask),
309 _mesa_bitcount(visinfo->green_mask),
310 _mesa_bitcount(visinfo->blue_mask),
311 config->alphaSize,
312 0, /* index bits */
313 config->depthSize,
314 config->stencilSize,
315 config->accumRedSize,
316 config->accumGreenSize,
317 config->accumBlueSize,
318 config->accumAlphaSize,
319 0 /* num samples */ );
320 }
321 #endif
322
323
324 static GLboolean
325 savageCreateContext( const __GLcontextModes *mesaVis,
326 __DRIcontextPrivate *driContextPriv,
327 void *sharedContextPrivate )
328 {
329 GLcontext *ctx, *shareCtx;
330 savageContextPtr imesa;
331 __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
332 struct dd_function_table functions;
333 savageScreenPrivate *savageScreen = (savageScreenPrivate *)sPriv->private;
334 drm_savage_sarea_t *saPriv=(drm_savage_sarea_t *)(((char*)sPriv->pSAREA)+
335 savageScreen->sarea_priv_offset);
336 int textureSize[SAVAGE_NR_TEX_HEAPS];
337 int i;
338 imesa = (savageContextPtr)Xcalloc(sizeof(savageContext), 1);
339 if (!imesa) {
340 return GL_FALSE;
341 }
342
343 /* Init default driver functions then plug in savage-specific texture
344 * functions that are needed as early as during context creation. */
345 _mesa_init_driver_functions( &functions );
346 savageDDInitTextureFuncs( &functions );
347
348 /* Allocate the Mesa context */
349 if (sharedContextPrivate)
350 shareCtx = ((savageContextPtr) sharedContextPrivate)->glCtx;
351 else
352 shareCtx = NULL;
353 ctx = _mesa_create_context(mesaVis, shareCtx, &functions, imesa);
354 if (!ctx) {
355 Xfree(imesa);
356 return GL_FALSE;
357 }
358 driContextPriv->driverPrivate = imesa;
359
360 imesa->cmdBuf.size = SAVAGE_CMDBUF_SIZE;
361 imesa->cmdBuf.base = imesa->cmdBuf.write =
362 malloc(SAVAGE_CMDBUF_SIZE * sizeof(drm_savage_cmd_header_t));
363 if (!imesa->cmdBuf.base)
364 return GL_FALSE;
365
366 /* Parse configuration files */
367 driParseConfigFiles (&imesa->optionCache, &savageScreen->optionCache,
368 sPriv->myNum, "savage");
369
370 imesa->float_depth = driQueryOptionb(&imesa->optionCache, "float_depth") &&
371 savageScreen->chipset >= S3_SAVAGE4;
372 imesa->no_rast = driQueryOptionb(&imesa->optionCache, "no_rast");
373
374 #if 0
375 ctx->Const.MinLineWidth = 1.0;
376 ctx->Const.MinLineWidthAA = 1.0;
377 ctx->Const.MaxLineWidth = 3.0;
378 ctx->Const.MaxLineWidthAA = 3.0;
379 ctx->Const.LineWidthGranularity = 1.0;
380 #endif
381
382 /* Dri stuff
383 */
384 imesa->hHWContext = driContextPriv->hHWContext;
385 imesa->driFd = sPriv->fd;
386 imesa->driHwLock = &sPriv->pSAREA->lock;
387
388 imesa->savageScreen = savageScreen;
389 imesa->driScreen = sPriv;
390 imesa->sarea = saPriv;
391 imesa->glBuffer = NULL;
392
393 /* DMA buffer */
394
395 for(i=0;i<5;i++)
396 {
397 imesa->apertureBase[i] = (GLubyte *)savageScreen->aperture.map +
398 0x01000000 * i;
399 }
400
401 imesa->aperturePitch = savageScreen->aperturePitch;
402
403 /* change texHeap initialize to support two kind of texture heap*/
404 /* here is some parts of initialization, others in InitDriver() */
405
406 (void) memset( imesa->textureHeaps, 0, sizeof( imesa->textureHeaps ) );
407 make_empty_list( & imesa->swapped );
408
409 textureSize[SAVAGE_CARD_HEAP] = savageScreen->textureSize[SAVAGE_CARD_HEAP];
410 textureSize[SAVAGE_AGP_HEAP] = savageScreen->textureSize[SAVAGE_AGP_HEAP];
411 imesa->lastTexHeap = savageScreen->texVirtual[SAVAGE_AGP_HEAP] ? 2 : 1;
412 switch(driQueryOptioni (&imesa->optionCache, "texture_heaps")) {
413 case DRI_CONF_TEXTURE_HEAPS_CARD: /* only use card memory, if available */
414 if (textureSize[SAVAGE_CARD_HEAP])
415 imesa->lastTexHeap = 1;
416 break;
417 case DRI_CONF_TEXTURE_HEAPS_GART: /* only use gart memory, if available */
418 if (imesa->lastTexHeap == 2 && textureSize[SAVAGE_AGP_HEAP])
419 textureSize[SAVAGE_CARD_HEAP] = 0;
420 break;
421 /*default: Nothing to do, use all available memory. */
422 }
423
424 for (i = 0; i < imesa->lastTexHeap; i++) {
425 imesa->textureHeaps[i] = driCreateTextureHeap(
426 i, imesa,
427 textureSize[i],
428 11, /* 2^11 = 2k alignment */
429 SAVAGE_NR_TEX_REGIONS,
430 (drmTextureRegionPtr)imesa->sarea->texList[i],
431 &imesa->sarea->texAge[i],
432 &imesa->swapped,
433 sizeof( savageTexObj ),
434 (destroy_texture_object_t *) savageDestroyTexObj );
435 /* If textureSize[i] == 0 textureHeaps[i] is NULL. This can happen
436 * if there is not enough card memory for a card texture heap. */
437 if (imesa->textureHeaps[i])
438 driSetTextureSwapCounterLocation( imesa->textureHeaps[i],
439 & imesa->c_textureSwaps );
440 }
441 imesa->texture_depth = driQueryOptioni (&imesa->optionCache,
442 "texture_depth");
443 if (imesa->texture_depth == DRI_CONF_TEXTURE_DEPTH_FB)
444 imesa->texture_depth = ( savageScreen->cpp == 4 ) ?
445 DRI_CONF_TEXTURE_DEPTH_32 : DRI_CONF_TEXTURE_DEPTH_16;
446
447 if (savageScreen->chipset >= S3_SAVAGE4)
448 ctx->Const.MaxTextureUnits = 2;
449 else
450 ctx->Const.MaxTextureUnits = 1;
451 if (driQueryOptioni(&imesa->optionCache, "texture_units") <
452 ctx->Const.MaxTextureUnits)
453 ctx->Const.MaxTextureUnits =
454 driQueryOptioni(&imesa->optionCache, "texture_units");
455 ctx->Const.MaxTextureImageUnits = ctx->Const.MaxTextureUnits;
456 ctx->Const.MaxTextureCoordUnits = ctx->Const.MaxTextureUnits;
457
458 driCalculateMaxTextureLevels( imesa->textureHeaps,
459 imesa->lastTexHeap,
460 & ctx->Const,
461 4,
462 11, /* max 2D texture size is 2048x2048 */
463 0, /* 3D textures unsupported. */
464 0, /* cube textures unsupported. */
465 0, /* texture rectangles unsupported. */
466 12,
467 GL_FALSE,
468 0 );
469 if (ctx->Const.MaxTextureLevels <= 6) { /*spec requires at least 64x64*/
470 __driUtilMessage("Not enough texture memory. "
471 "Falling back to indirect rendering.");
472 Xfree(imesa);
473 return GL_FALSE;
474 }
475
476 imesa->hw_stencil = mesaVis->stencilBits && mesaVis->depthBits == 24;
477 imesa->depth_scale = (imesa->savageScreen->zpp == 2) ?
478 (1.0F/0xffff):(1.0F/0xffffff);
479
480 imesa->bufferSize = savageScreen->bufferSize;
481 imesa->dmaVtxBuf.total = 0;
482 imesa->dmaVtxBuf.used = 0;
483 imesa->dmaVtxBuf.flushed = 0;
484
485 imesa->clientVtxBuf.total = imesa->bufferSize / 4;
486 imesa->clientVtxBuf.used = 0;
487 imesa->clientVtxBuf.flushed = 0;
488 imesa->clientVtxBuf.buf = (u_int32_t *)malloc(imesa->bufferSize);
489
490 imesa->vtxBuf = &imesa->clientVtxBuf;
491
492 imesa->firstElt = -1;
493
494 /* Uninitialized vertex format. Force setting the vertex state in
495 * savageRenderStart.
496 */
497 imesa->vertex_size = 0;
498
499 /* Utah stuff
500 */
501 imesa->new_state = ~0;
502 imesa->new_gl_state = ~0;
503 imesa->RenderIndex = ~0;
504 imesa->dirty = ~0;
505 imesa->lostContext = GL_TRUE;
506 imesa->CurrentTexObj[0] = 0;
507 imesa->CurrentTexObj[1] = 0;
508
509 /* Initialize the software rasterizer and helper modules.
510 */
511 _swrast_CreateContext( ctx );
512 _ac_CreateContext( ctx );
513 _tnl_CreateContext( ctx );
514
515 _swsetup_CreateContext( ctx );
516
517 /* Install the customized pipeline:
518 */
519 _tnl_destroy_pipeline( ctx );
520 _tnl_install_pipeline( ctx, savage_pipeline );
521
522 imesa->enable_fastpath = driQueryOptionb(&imesa->optionCache,
523 "enable_fastpath");
524 /* DRM versions before 2.1.3 would only render triangle lists. ELTS
525 * support was added in 2.2.0. */
526 if (imesa->enable_fastpath && sPriv->drmMinor < 2) {
527 fprintf (stderr,
528 "*** Disabling fast path because your DRM version is buggy "
529 "or doesn't\n*** support ELTS. You need at least Savage DRM "
530 "version 2.2.\n");
531 imesa->enable_fastpath = GL_FALSE;
532 }
533
534 if (!savageScreen->bufs || savageScreen->chipset == S3_SUPERSAVAGE)
535 imesa->enable_vdma = GL_FALSE;
536 else
537 imesa->enable_vdma = driQueryOptionb(&imesa->optionCache, "enable_vdma");
538
539 imesa->sync_frames = driQueryOptionb(&imesa->optionCache, "sync_frames");
540
541 /* Configure swrast to match hardware characteristics:
542 */
543 _tnl_allow_pixel_fog( ctx, GL_FALSE );
544 _tnl_allow_vertex_fog( ctx, GL_TRUE );
545 _swrast_allow_pixel_fog( ctx, GL_FALSE );
546 _swrast_allow_vertex_fog( ctx, GL_TRUE );
547
548 ctx->DriverCtx = (void *) imesa;
549 imesa->glCtx = ctx;
550
551 #ifndef SAVAGE_DEBUG
552 SAVAGE_DEBUG = driParseDebugString( getenv( "SAVAGE_DEBUG" ),
553 debug_control );
554 #endif
555
556 driInitExtensions( ctx, card_extensions, GL_TRUE );
557 if (savageScreen->chipset >= S3_SAVAGE4)
558 driInitExtensions( ctx, s4_extensions, GL_FALSE );
559 if (ctx->Mesa_DXTn ||
560 driQueryOptionb (&imesa->optionCache, "force_s3tc_enable")) {
561 _mesa_enable_extension( ctx, "GL_S3_s3tc" );
562 if (savageScreen->chipset >= S3_SAVAGE4)
563 /* This extension needs DXT3 and DTX5 support in hardware.
564 * Not available on Savage3D/MX/IX. */
565 _mesa_enable_extension( ctx, "GL_EXT_texture_compression_s3tc" );
566 }
567
568 savageDDInitStateFuncs( ctx );
569 savageDDInitSpanFuncs( ctx );
570 savageDDInitDriverFuncs( ctx );
571 savageDDInitIoctlFuncs( ctx );
572 savageInitTriFuncs( ctx );
573
574 savageDDInitState( imesa );
575
576 driContextPriv->driverPrivate = (void *) imesa;
577
578 return GL_TRUE;
579 }
580
581 static void
582 savageDestroyContext(__DRIcontextPrivate *driContextPriv)
583 {
584 savageContextPtr imesa = (savageContextPtr) driContextPriv->driverPrivate;
585 GLuint i;
586
587 assert (imesa); /* should never be NULL */
588 if (imesa) {
589 savageFlushVertices(imesa);
590 savageReleaseIndexedVerts(imesa);
591 savageFlushCmdBuf(imesa, GL_TRUE); /* release DMA buffer */
592 WAIT_IDLE_EMPTY(imesa);
593
594 for (i = 0; i < imesa->lastTexHeap; i++)
595 driDestroyTextureHeap(imesa->textureHeaps[i]);
596
597 free(imesa->cmdBuf.base);
598 free(imesa->clientVtxBuf.buf);
599
600 _swsetup_DestroyContext(imesa->glCtx );
601 _tnl_DestroyContext( imesa->glCtx );
602 _ac_DestroyContext( imesa->glCtx );
603 _swrast_DestroyContext( imesa->glCtx );
604
605 /* free the Mesa context */
606 imesa->glCtx->DriverCtx = NULL;
607 _mesa_destroy_context(imesa->glCtx);
608
609 /* no longer use vertex_dma_buf*/
610 Xfree(imesa);
611 }
612 }
613
614
615 static GLboolean
616 savageCreateBuffer( __DRIscreenPrivate *driScrnPriv,
617 __DRIdrawablePrivate *driDrawPriv,
618 const __GLcontextModes *mesaVis,
619 GLboolean isPixmap)
620 {
621 savageScreenPrivate *screen = (savageScreenPrivate *) driScrnPriv->private;
622
623 if (isPixmap) {
624 return GL_FALSE; /* not implemented */
625 }
626 else {
627 GLboolean swStencil = mesaVis->stencilBits > 0 && mesaVis->depthBits != 24;
628 struct gl_framebuffer *fb = _mesa_create_framebuffer(mesaVis);
629 /*
630 * XXX: this value needs to be set according to the config file
631 * setting. But we don't get that until we create a rendering
632 * context!!!!
633 */
634 GLboolean float_depth = GL_FALSE;
635
636 {
637 driRenderbuffer *frontRb
638 = driNewRenderbuffer(GL_RGBA,
639 (GLubyte *) screen->aperture.map
640 + 0x01000000 * TARGET_FRONT,
641 screen->cpp,
642 screen->frontOffset, screen->aperturePitch,
643 driDrawPriv);
644 savageSetSpanFunctions(frontRb, mesaVis, float_depth);
645 assert(frontRb->Base.Data);
646 _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &frontRb->Base);
647 }
648
649 if (mesaVis->doubleBufferMode) {
650 driRenderbuffer *backRb
651 = driNewRenderbuffer(GL_RGBA,
652 (GLubyte *) screen->aperture.map
653 + 0x01000000 * TARGET_BACK,
654 screen->cpp,
655 screen->backOffset, screen->aperturePitch,
656 driDrawPriv);
657 savageSetSpanFunctions(backRb, mesaVis, float_depth);
658 assert(backRb->Base.Data);
659 _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &backRb->Base);
660 }
661
662 if (mesaVis->depthBits == 16) {
663 driRenderbuffer *depthRb
664 = driNewRenderbuffer(GL_DEPTH_COMPONENT16,
665 (GLubyte *) screen->aperture.map
666 + 0x01000000 * TARGET_DEPTH,
667 screen->zpp,
668 screen->depthOffset, screen->aperturePitch,
669 driDrawPriv);
670 savageSetSpanFunctions(depthRb, mesaVis, float_depth);
671 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
672 }
673 else if (mesaVis->depthBits == 24) {
674 driRenderbuffer *depthRb
675 = driNewRenderbuffer(GL_DEPTH_COMPONENT24,
676 (GLubyte *) screen->aperture.map
677 + 0x01000000 * TARGET_DEPTH,
678 screen->zpp,
679 screen->depthOffset, screen->aperturePitch,
680 driDrawPriv);
681 savageSetSpanFunctions(depthRb, mesaVis, float_depth);
682 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
683 }
684
685 if (mesaVis->stencilBits > 0 && !swStencil) {
686 driRenderbuffer *stencilRb
687 = driNewRenderbuffer(GL_STENCIL_INDEX8_EXT,
688 (GLubyte *) screen->aperture.map
689 + 0x01000000 * TARGET_DEPTH,
690 screen->zpp,
691 screen->depthOffset, screen->aperturePitch,
692 driDrawPriv);
693 savageSetSpanFunctions(stencilRb, mesaVis, float_depth);
694 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &stencilRb->Base);
695 }
696
697 _mesa_add_soft_renderbuffers(fb,
698 GL_FALSE, /* color */
699 GL_FALSE, /* depth */
700 swStencil,
701 mesaVis->accumRedBits > 0,
702 GL_FALSE, /* alpha */
703 GL_FALSE /* aux */);
704 driDrawPriv->driverPrivate = (void *) fb;
705
706 return (driDrawPriv->driverPrivate != NULL);
707 }
708 }
709
710 static void
711 savageDestroyBuffer(__DRIdrawablePrivate *driDrawPriv)
712 {
713 _mesa_destroy_framebuffer((GLframebuffer *) (driDrawPriv->driverPrivate));
714 }
715
716 #if 0
717 void XMesaSwapBuffers(__DRIdrawablePrivate *driDrawPriv)
718 {
719 /* XXX should do swap according to the buffer, not the context! */
720 savageContextPtr imesa = savageCtx;
721
722 FLUSH_VB( imesa->glCtx, "swap buffers" );
723 savageSwapBuffers(imesa);
724 }
725 #endif
726
727 void savageXMesaSetFrontClipRects( savageContextPtr imesa )
728 {
729 __DRIdrawablePrivate *dPriv = imesa->driDrawable;
730
731 imesa->numClipRects = dPriv->numClipRects;
732 imesa->pClipRects = dPriv->pClipRects;
733 imesa->drawX = dPriv->x;
734 imesa->drawY = dPriv->y;
735
736 savageCalcViewport( imesa->glCtx );
737 }
738
739
740 void savageXMesaSetBackClipRects( savageContextPtr imesa )
741 {
742 __DRIdrawablePrivate *dPriv = imesa->driDrawable;
743
744 if (dPriv->numBackClipRects == 0)
745 {
746
747
748 imesa->numClipRects = dPriv->numClipRects;
749 imesa->pClipRects = dPriv->pClipRects;
750 imesa->drawX = dPriv->x;
751 imesa->drawY = dPriv->y;
752 } else {
753
754
755 imesa->numClipRects = dPriv->numBackClipRects;
756 imesa->pClipRects = dPriv->pBackClipRects;
757 imesa->drawX = dPriv->backX;
758 imesa->drawY = dPriv->backY;
759 }
760
761 savageCalcViewport( imesa->glCtx );
762 }
763
764
765 static void savageXMesaWindowMoved( savageContextPtr imesa )
766 {
767 __DRIdrawablePrivate *const drawable = imesa->driDrawable;
768 __DRIdrawablePrivate *const readable = imesa->driReadable;
769
770 if (0)
771 fprintf(stderr, "savageXMesaWindowMoved\n\n");
772
773 switch (imesa->glCtx->DrawBuffer->_ColorDrawBufferMask[0]) {
774 case BUFFER_BIT_FRONT_LEFT:
775 savageXMesaSetFrontClipRects( imesa );
776 break;
777 case BUFFER_BIT_BACK_LEFT:
778 savageXMesaSetBackClipRects( imesa );
779 break;
780 default:
781 break;
782 }
783
784 driUpdateFramebufferSize(imesa->glCtx, drawable);
785 if (drawable != readable) {
786 driUpdateFramebufferSize(imesa->glCtx, readable);
787 }
788 }
789
790
791 static GLboolean
792 savageUnbindContext(__DRIcontextPrivate *driContextPriv)
793 {
794 savageContextPtr savage = (savageContextPtr) driContextPriv->driverPrivate;
795 if (savage)
796 savage->dirty = ~0;
797
798 return GL_TRUE;
799 }
800
801 #if 0
802 static GLboolean
803 savageOpenFullScreen(__DRIcontextPrivate *driContextPriv)
804 {
805
806
807
808 if (driContextPriv) {
809 savageContextPtr imesa = (savageContextPtr) driContextPriv->driverPrivate;
810 imesa->IsFullScreen = GL_TRUE;
811 imesa->backup_frontOffset = imesa->savageScreen->frontOffset;
812 imesa->backup_backOffset = imesa->savageScreen->backOffset;
813 imesa->backup_frontBitmapDesc = imesa->savageScreen->frontBitmapDesc;
814 imesa->savageScreen->frontBitmapDesc = imesa->savageScreen->backBitmapDesc;
815 imesa->toggle = TARGET_BACK;
816 }
817
818 return GL_TRUE;
819 }
820
821 static GLboolean
822 savageCloseFullScreen(__DRIcontextPrivate *driContextPriv)
823 {
824
825 if (driContextPriv) {
826 savageContextPtr imesa = (savageContextPtr) driContextPriv->driverPrivate;
827 WAIT_IDLE_EMPTY(imesa);
828 imesa->IsFullScreen = GL_FALSE;
829 imesa->savageScreen->frontOffset = imesa->backup_frontOffset;
830 imesa->savageScreen->backOffset = imesa->backup_backOffset;
831 imesa->savageScreen->frontBitmapDesc = imesa->backup_frontBitmapDesc;
832 }
833 return GL_TRUE;
834 }
835 #endif
836
837 static GLboolean
838 savageMakeCurrent(__DRIcontextPrivate *driContextPriv,
839 __DRIdrawablePrivate *driDrawPriv,
840 __DRIdrawablePrivate *driReadPriv)
841 {
842 if (driContextPriv) {
843 savageContextPtr imesa
844 = (savageContextPtr) driContextPriv->driverPrivate;
845 struct gl_framebuffer *drawBuffer
846 = (GLframebuffer *) driDrawPriv->driverPrivate;
847 struct gl_framebuffer *readBuffer
848 = (GLframebuffer *) driReadPriv->driverPrivate;
849 driRenderbuffer *frontRb = (driRenderbuffer *)
850 drawBuffer->Attachment[BUFFER_FRONT_LEFT].Renderbuffer;
851 driRenderbuffer *backRb = (driRenderbuffer *)
852 drawBuffer->Attachment[BUFFER_BACK_LEFT].Renderbuffer;
853
854 assert(frontRb->Base.Data);
855 if (imesa->glCtx->Visual.doubleBufferMode) {
856 assert(backRb->Base.Data);
857 }
858
859 imesa->driReadable = driReadPriv;
860 imesa->driDrawable = driDrawPriv;
861 imesa->dirty = ~0;
862
863 _mesa_make_current(imesa->glCtx, drawBuffer, readBuffer);
864
865 savageXMesaWindowMoved( imesa );
866 }
867 else
868 {
869 _mesa_make_current(NULL, NULL, NULL);
870 }
871 return GL_TRUE;
872 }
873
874
875 void savageGetLock( savageContextPtr imesa, GLuint flags )
876 {
877 __DRIdrawablePrivate *const drawable = imesa->driDrawable;
878 __DRIdrawablePrivate *const readable = imesa->driReadable;
879 __DRIscreenPrivate *sPriv = imesa->driScreen;
880 drm_savage_sarea_t *sarea = imesa->sarea;
881 int me = imesa->hHWContext;
882 int stamp = drawable->lastStamp;
883 int heap;
884 unsigned int timestamp = 0;
885
886
887
888 /* We know there has been contention.
889 */
890 drmGetLock(imesa->driFd, imesa->hHWContext, flags);
891
892
893 /* Note contention for throttling hint
894 */
895 imesa->any_contend = 1;
896
897 /* If the window moved, may need to set a new cliprect now.
898 *
899 * NOTE: This releases and regains the hw lock, so all state
900 * checking must be done *after* this call:
901 */
902 DRI_VALIDATE_DRAWABLE_INFO(sPriv, drawable);
903 if (drawable != readable) {
904 DRI_VALIDATE_DRAWABLE_INFO(sPriv, readable);
905 }
906
907
908 /* If we lost context, need to dump all registers to hardware.
909 * Note that we don't care about 2d contexts, even if they perform
910 * accelerated commands, so the DRI locking in the X server is even
911 * more broken than usual.
912 */
913 if (sarea->ctxOwner != me) {
914 imesa->dirty |= (SAVAGE_UPLOAD_LOCAL |
915 SAVAGE_UPLOAD_GLOBAL |
916 SAVAGE_UPLOAD_FOGTBL |
917 SAVAGE_UPLOAD_TEX0 |
918 SAVAGE_UPLOAD_TEX1 |
919 SAVAGE_UPLOAD_TEXGLOBAL);
920 imesa->lostContext = GL_TRUE;
921 sarea->ctxOwner = me;
922 }
923
924 for (heap = 0; heap < imesa->lastTexHeap; ++heap) {
925 /* If a heap was changed, update its timestamp. Do this before
926 * DRI_AGE_TEXTURES updates the local_age. */
927 if (imesa->textureHeaps[heap] &&
928 imesa->textureHeaps[heap]->global_age[0] >
929 imesa->textureHeaps[heap]->local_age) {
930 if (timestamp == 0)
931 timestamp = savageEmitEventLocked(imesa, 0);
932 imesa->textureHeaps[heap]->timestamp = timestamp;
933 }
934 DRI_AGE_TEXTURES( imesa->textureHeaps[heap] );
935 }
936
937 if (drawable->lastStamp != stamp) {
938 driUpdateFramebufferSize(imesa->glCtx, drawable);
939 savageXMesaWindowMoved( imesa );
940 }
941 }
942
943
944
945 static const struct __DriverAPIRec savageAPI = {
946 savageInitDriver,
947 savageDestroyScreen,
948 savageCreateContext,
949 savageDestroyContext,
950 savageCreateBuffer,
951 savageDestroyBuffer,
952 savageSwapBuffers,
953 savageMakeCurrent,
954 savageUnbindContext
955 };
956
957
958 static __GLcontextModes *
959 savageFillInModes( unsigned pixel_bits, unsigned depth_bits,
960 unsigned stencil_bits, GLboolean have_back_buffer )
961 {
962 __GLcontextModes * modes;
963 __GLcontextModes * m;
964 unsigned num_modes;
965 unsigned depth_buffer_factor;
966 unsigned back_buffer_factor;
967 GLenum fb_format;
968 GLenum fb_type;
969
970 /* Right now GLX_SWAP_COPY_OML isn't supported, but it would be easy
971 * enough to add support. Basically, if a context is created with an
972 * fbconfig where the swap method is GLX_SWAP_COPY_OML, pageflipping
973 * will never be used.
974 *
975 * FK: What about drivers that don't use page flipping? Could they
976 * just expose GLX_SWAP_COPY_OML?
977 */
978 static const GLenum back_buffer_modes[] = {
979 GLX_NONE, GLX_SWAP_UNDEFINED_OML /*, GLX_SWAP_COPY_OML */
980 };
981
982 u_int8_t depth_bits_array[2];
983 u_int8_t stencil_bits_array[2];
984
985
986 depth_bits_array[0] = depth_bits;
987 depth_bits_array[1] = depth_bits;
988
989 /* Just like with the accumulation buffer, always provide some modes
990 * with a stencil buffer. It will be a sw fallback, but some apps won't
991 * care about that.
992 */
993 stencil_bits_array[0] = 0;
994 stencil_bits_array[1] = (stencil_bits == 0) ? 8 : stencil_bits;
995
996 depth_buffer_factor = ((depth_bits != 0) || (stencil_bits != 0)) ? 2 : 1;
997 back_buffer_factor = (have_back_buffer) ? 2 : 1;
998
999 num_modes = depth_buffer_factor * back_buffer_factor * 4;
1000
1001 if ( pixel_bits == 16 ) {
1002 fb_format = GL_RGB;
1003 fb_type = GL_UNSIGNED_SHORT_5_6_5;
1004 }
1005 else {
1006 fb_format = GL_BGR;
1007 fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
1008 }
1009
1010 modes = (*dri_interface->createContextModes)( num_modes, sizeof( __GLcontextModes ) );
1011 m = modes;
1012 if ( ! driFillInModes( & m, fb_format, fb_type,
1013 depth_bits_array, stencil_bits_array, depth_buffer_factor,
1014 back_buffer_modes, back_buffer_factor,
1015 GLX_TRUE_COLOR ) ) {
1016 fprintf( stderr, "[%s:%u] Error creating FBConfig!\n",
1017 __func__, __LINE__ );
1018 return NULL;
1019 }
1020
1021 if ( ! driFillInModes( & m, fb_format, fb_type,
1022 depth_bits_array, stencil_bits_array, depth_buffer_factor,
1023 back_buffer_modes, back_buffer_factor,
1024 GLX_DIRECT_COLOR ) ) {
1025 fprintf( stderr, "[%s:%u] Error creating FBConfig!\n",
1026 __func__, __LINE__ );
1027 return NULL;
1028 }
1029
1030 /* Mark the visual as slow if there are "fake" stencil bits.
1031 */
1032 for ( m = modes ; m != NULL ; m = m->next ) {
1033 if ( (m->stencilBits != 0) && (m->stencilBits != stencil_bits) ) {
1034 m->visualRating = GLX_SLOW_CONFIG;
1035 }
1036 }
1037
1038 return modes;
1039 }
1040
1041
1042 /**
1043 * This is the bootstrap function for the driver. libGL supplies all of the
1044 * requisite information about the system, and the driver initializes itself.
1045 * This routine also fills in the linked list pointed to by \c driver_modes
1046 * with the \c __GLcontextModes that the driver can support for windows or
1047 * pbuffers.
1048 *
1049 * \return A pointer to a \c __DRIscreenPrivate on success, or \c NULL on
1050 * failure.
1051 */
1052 PUBLIC
1053 void * __driCreateNewScreen_20050727( __DRInativeDisplay *dpy, int scrn, __DRIscreen *psc,
1054 const __GLcontextModes * modes,
1055 const __DRIversion * ddx_version,
1056 const __DRIversion * dri_version,
1057 const __DRIversion * drm_version,
1058 const __DRIframebuffer * frame_buffer,
1059 drmAddress pSAREA, int fd,
1060 int internal_api_version,
1061 const __DRIinterfaceMethods * interface,
1062 __GLcontextModes ** driver_modes )
1063
1064 {
1065 __DRIscreenPrivate *psp;
1066 static const __DRIversion ddx_expected = { 2, 0, 0 };
1067 static const __DRIversion dri_expected = { 4, 0, 0 };
1068 static const __DRIversion drm_expected = { 2, 1, 0 };
1069
1070 dri_interface = interface;
1071
1072 if ( ! driCheckDriDdxDrmVersions2( "Savage",
1073 dri_version, & dri_expected,
1074 ddx_version, & ddx_expected,
1075 drm_version, & drm_expected ) ) {
1076 return NULL;
1077 }
1078
1079 psp = __driUtilCreateNewScreen(dpy, scrn, psc, NULL,
1080 ddx_version, dri_version, drm_version,
1081 frame_buffer, pSAREA, fd,
1082 internal_api_version, &savageAPI);
1083 if ( psp != NULL ) {
1084 SAVAGEDRIPtr dri_priv = (SAVAGEDRIPtr)psp->pDevPriv;
1085 *driver_modes = savageFillInModes( dri_priv->cpp*8,
1086 (dri_priv->cpp == 2) ? 16 : 24,
1087 (dri_priv->cpp == 2) ? 0 : 8,
1088 (dri_priv->backOffset != dri_priv->depthOffset) );
1089
1090 /* Calling driInitExtensions here, with a NULL context pointer, does not actually
1091 * enable the extensions. It just makes sure that all the dispatch offsets for all
1092 * the extensions that *might* be enables are known. This is needed because the
1093 * dispatch offsets need to be known when _mesa_context_create is called, but we can't
1094 * enable the extensions until we have a context pointer.
1095 *
1096 * Hello chicken. Hello egg. How are you two today?
1097 */
1098 driInitExtensions( NULL, card_extensions, GL_FALSE );
1099 }
1100
1101 return (void *) psp;
1102 }