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