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