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