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