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