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