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