Merge branch 'nouveau-import'
[mesa.git] / src / mesa / drivers / dri / r200 / r200_context.c
1 /* $XFree86: xc/lib/GL/mesa/src/drv/r200/r200_context.c,v 1.3 2003/05/06 23:52:08 daenzer Exp $ */
2 /*
3 Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved.
4
5 The Weather Channel (TM) funded Tungsten Graphics to develop the
6 initial release of the Radeon 8500 driver under the XFree86 license.
7 This notice must be preserved.
8
9 Permission is hereby granted, free of charge, to any person obtaining
10 a copy of this software and associated documentation files (the
11 "Software"), to deal in the Software without restriction, including
12 without limitation the rights to use, copy, modify, merge, publish,
13 distribute, sublicense, and/or sell copies of the Software, and to
14 permit persons to whom the Software is furnished to do so, subject to
15 the following conditions:
16
17 The above copyright notice and this permission notice (including the
18 next paragraph) shall be included in all copies or substantial
19 portions of the Software.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
25 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28
29 **************************************************************************/
30
31 /*
32 * Authors:
33 * Keith Whitwell <keith@tungstengraphics.com>
34 */
35
36 #include "glheader.h"
37 #include "api_arrayelt.h"
38 #include "context.h"
39 #include "simple_list.h"
40 #include "imports.h"
41 #include "matrix.h"
42 #include "extensions.h"
43 #include "framebuffer.h"
44 #include "state.h"
45
46 #include "swrast/swrast.h"
47 #include "swrast_setup/swrast_setup.h"
48 #include "array_cache/acache.h"
49
50 #include "tnl/tnl.h"
51 #include "tnl/t_pipeline.h"
52
53 #include "drivers/common/driverfuncs.h"
54
55 #include "r200_context.h"
56 #include "r200_ioctl.h"
57 #include "r200_state.h"
58 #include "r200_span.h"
59 #include "r200_pixel.h"
60 #include "r200_tex.h"
61 #include "r200_swtcl.h"
62 #include "r200_tcl.h"
63 #include "r200_vtxfmt.h"
64 #include "r200_maos.h"
65 #include "r200_vertprog.h"
66
67 #define need_GL_ARB_multisample
68 #define need_GL_ARB_texture_compression
69 #define need_GL_ARB_vertex_buffer_object
70 #define need_GL_ARB_vertex_program
71 #define need_GL_ATI_fragment_shader
72 #define need_GL_EXT_blend_minmax
73 #define need_GL_EXT_fog_coord
74 #define need_GL_EXT_secondary_color
75 #define need_GL_EXT_blend_equation_separate
76 #define need_GL_EXT_blend_func_separate
77 #define need_GL_NV_vertex_program
78 #define need_GL_ARB_point_parameters
79 #include "extension_helper.h"
80
81 #define DRIVER_DATE "20060602"
82
83 #include "vblank.h"
84 #include "utils.h"
85 #include "xmlpool.h" /* for symbolic values of enum-type options */
86 #ifndef R200_DEBUG
87 int R200_DEBUG = (0);
88 #endif
89
90 /* Return various strings for glGetString().
91 */
92 static const GLubyte *r200GetString( GLcontext *ctx, GLenum name )
93 {
94 r200ContextPtr rmesa = R200_CONTEXT(ctx);
95 static char buffer[128];
96 unsigned offset;
97 GLuint agp_mode = (rmesa->r200Screen->card_type == RADEON_CARD_PCI)? 0 :
98 rmesa->r200Screen->AGPMode;
99
100 switch ( name ) {
101 case GL_VENDOR:
102 return (GLubyte *)"Tungsten Graphics, Inc.";
103
104 case GL_RENDERER:
105 offset = driGetRendererString( buffer, "R200", DRIVER_DATE,
106 agp_mode );
107
108 sprintf( & buffer[ offset ], " %sTCL",
109 !(rmesa->TclFallback & R200_TCL_FALLBACK_TCL_DISABLE)
110 ? "" : "NO-" );
111
112 return (GLubyte *)buffer;
113
114 default:
115 return NULL;
116 }
117 }
118
119
120 /* Extension strings exported by the R200 driver.
121 */
122 const struct dri_extension card_extensions[] =
123 {
124 { "GL_ARB_multisample", GL_ARB_multisample_functions },
125 { "GL_ARB_multitexture", NULL },
126 { "GL_ARB_texture_border_clamp", NULL },
127 { "GL_ARB_texture_compression", GL_ARB_texture_compression_functions },
128 { "GL_ARB_texture_env_add", NULL },
129 { "GL_ARB_texture_env_combine", NULL },
130 { "GL_ARB_texture_env_dot3", NULL },
131 { "GL_ARB_texture_env_crossbar", NULL },
132 { "GL_ARB_texture_mirrored_repeat", NULL },
133 { "GL_ARB_vertex_buffer_object", GL_ARB_vertex_buffer_object_functions },
134 { "GL_EXT_blend_minmax", GL_EXT_blend_minmax_functions },
135 { "GL_EXT_blend_subtract", NULL },
136 { "GL_EXT_fog_coord", GL_EXT_fog_coord_functions },
137 { "GL_EXT_secondary_color", GL_EXT_secondary_color_functions },
138 { "GL_EXT_stencil_wrap", NULL },
139 { "GL_EXT_texture_edge_clamp", NULL },
140 { "GL_EXT_texture_env_combine", NULL },
141 { "GL_EXT_texture_env_dot3", NULL },
142 { "GL_EXT_texture_filter_anisotropic", NULL },
143 { "GL_EXT_texture_lod_bias", NULL },
144 { "GL_EXT_texture_mirror_clamp", NULL },
145 { "GL_EXT_texture_rectangle", NULL },
146 { "GL_ATI_texture_env_combine3", NULL },
147 { "GL_ATI_texture_mirror_once", NULL },
148 { "GL_MESA_pack_invert", NULL },
149 { "GL_NV_blend_square", NULL },
150 { "GL_SGIS_generate_mipmap", NULL },
151 { NULL, NULL }
152 };
153
154 const struct dri_extension blend_extensions[] = {
155 { "GL_EXT_blend_equation_separate", GL_EXT_blend_equation_separate_functions },
156 { "GL_EXT_blend_func_separate", GL_EXT_blend_func_separate_functions },
157 { NULL, NULL }
158 };
159
160 const struct dri_extension ARB_vp_extension[] = {
161 { "GL_ARB_vertex_program", GL_ARB_vertex_program_functions }
162 };
163
164 const struct dri_extension NV_vp_extension[] = {
165 { "GL_NV_vertex_program", GL_NV_vertex_program_functions }
166 };
167
168 const struct dri_extension ATI_fs_extension[] = {
169 { "GL_ATI_fragment_shader", GL_ATI_fragment_shader_functions }
170 };
171
172 const struct dri_extension point_extensions[] = {
173 { "GL_ARB_point_sprite", NULL },
174 { "GL_ARB_point_parameters", GL_ARB_point_parameters_functions },
175 { NULL, NULL }
176 };
177
178 extern const struct tnl_pipeline_stage _r200_render_stage;
179 extern const struct tnl_pipeline_stage _r200_tcl_stage;
180
181 static const struct tnl_pipeline_stage *r200_pipeline[] = {
182
183 /* Try and go straight to t&l
184 */
185 &_r200_tcl_stage,
186
187 /* Catch any t&l fallbacks
188 */
189 &_tnl_vertex_transform_stage,
190 &_tnl_normal_transform_stage,
191 &_tnl_lighting_stage,
192 &_tnl_fog_coordinate_stage,
193 &_tnl_texgen_stage,
194 &_tnl_texture_transform_stage,
195 &_tnl_point_attenuation_stage,
196 &_tnl_arb_vertex_program_stage,
197 &_tnl_vertex_program_stage,
198 /* Try again to go to tcl?
199 * - no good for asymmetric-twoside (do with multipass)
200 * - no good for asymmetric-unfilled (do with multipass)
201 * - good for material
202 * - good for texgen
203 * - need to manipulate a bit of state
204 *
205 * - worth it/not worth it?
206 */
207
208 /* Else do them here.
209 */
210 /* &_r200_render_stage, */ /* FIXME: bugs with ut2003 */
211 &_tnl_render_stage, /* FALLBACK: */
212 NULL,
213 };
214
215
216
217 /* Initialize the driver's misc functions.
218 */
219 static void r200InitDriverFuncs( struct dd_function_table *functions )
220 {
221 functions->GetBufferSize = NULL; /* OBSOLETE */
222 functions->GetString = r200GetString;
223 }
224
225 static const struct dri_debug_control debug_control[] =
226 {
227 { "fall", DEBUG_FALLBACKS },
228 { "tex", DEBUG_TEXTURE },
229 { "ioctl", DEBUG_IOCTL },
230 { "prim", DEBUG_PRIMS },
231 { "vert", DEBUG_VERTS },
232 { "state", DEBUG_STATE },
233 { "code", DEBUG_CODEGEN },
234 { "vfmt", DEBUG_VFMT },
235 { "vtxf", DEBUG_VFMT },
236 { "verb", DEBUG_VERBOSE },
237 { "dri", DEBUG_DRI },
238 { "dma", DEBUG_DMA },
239 { "san", DEBUG_SANITY },
240 { "sync", DEBUG_SYNC },
241 { "pix", DEBUG_PIXEL },
242 { "mem", DEBUG_MEMORY },
243 { NULL, 0 }
244 };
245
246
247 /* Create the device specific rendering context.
248 */
249 GLboolean r200CreateContext( const __GLcontextModes *glVisual,
250 __DRIcontextPrivate *driContextPriv,
251 void *sharedContextPrivate)
252 {
253 __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
254 radeonScreenPtr screen = (radeonScreenPtr)(sPriv->private);
255 struct dd_function_table functions;
256 r200ContextPtr rmesa;
257 GLcontext *ctx, *shareCtx;
258 int i;
259 int tcl_mode, fthrottle_mode;
260
261 assert(glVisual);
262 assert(driContextPriv);
263 assert(screen);
264
265 /* Allocate the R200 context */
266 rmesa = (r200ContextPtr) CALLOC( sizeof(*rmesa) );
267 if ( !rmesa )
268 return GL_FALSE;
269
270 /* init exp fog table data */
271 r200InitStaticFogData();
272
273 /* Parse configuration files.
274 * Do this here so that initialMaxAnisotropy is set before we create
275 * the default textures.
276 */
277 driParseConfigFiles (&rmesa->optionCache, &screen->optionCache,
278 screen->driScreen->myNum, "r200");
279 rmesa->initialMaxAnisotropy = driQueryOptionf(&rmesa->optionCache,
280 "def_max_anisotropy");
281
282 if ( driQueryOptionb( &rmesa->optionCache, "hyperz" ) ) {
283 if ( sPriv->drmMinor < 13 )
284 fprintf( stderr, "DRM version 1.%d too old to support HyperZ, "
285 "disabling.\n",sPriv->drmMinor );
286 else
287 rmesa->using_hyperz = GL_TRUE;
288 }
289
290 if ( sPriv->drmMinor >= 15 )
291 rmesa->texmicrotile = GL_TRUE;
292
293 /* Init default driver functions then plug in our R200-specific functions
294 * (the texture functions are especially important)
295 */
296 _mesa_init_driver_functions(&functions);
297 r200InitDriverFuncs(&functions);
298 r200InitIoctlFuncs(&functions);
299 r200InitStateFuncs(&functions);
300 r200InitTextureFuncs(&functions);
301 r200InitShaderFuncs(&functions);
302
303 /* Allocate and initialize the Mesa context */
304 if (sharedContextPrivate)
305 shareCtx = ((r200ContextPtr) sharedContextPrivate)->glCtx;
306 else
307 shareCtx = NULL;
308 rmesa->glCtx = _mesa_create_context(glVisual, shareCtx,
309 &functions, (void *) rmesa);
310 if (!rmesa->glCtx) {
311 FREE(rmesa);
312 return GL_FALSE;
313 }
314 driContextPriv->driverPrivate = rmesa;
315
316 /* Init r200 context data */
317 rmesa->dri.context = driContextPriv;
318 rmesa->dri.screen = sPriv;
319 rmesa->dri.drawable = NULL; /* Set by XMesaMakeCurrent */
320 rmesa->dri.hwContext = driContextPriv->hHWContext;
321 rmesa->dri.hwLock = &sPriv->pSAREA->lock;
322 rmesa->dri.fd = sPriv->fd;
323 rmesa->dri.drmMinor = sPriv->drmMinor;
324
325 rmesa->r200Screen = screen;
326 rmesa->sarea = (drm_radeon_sarea_t *)((GLubyte *)sPriv->pSAREA +
327 screen->sarea_priv_offset);
328
329
330 rmesa->dma.buf0_address = rmesa->r200Screen->buffers->list[0].address;
331
332 (void) memset( rmesa->texture_heaps, 0, sizeof( rmesa->texture_heaps ) );
333 make_empty_list( & rmesa->swapped );
334
335 rmesa->nr_heaps = 1 /* screen->numTexHeaps */ ;
336 assert(rmesa->nr_heaps < RADEON_NR_TEX_HEAPS);
337 for ( i = 0 ; i < rmesa->nr_heaps ; i++ ) {
338 rmesa->texture_heaps[i] = driCreateTextureHeap( i, rmesa,
339 screen->texSize[i],
340 12,
341 RADEON_NR_TEX_REGIONS,
342 (drmTextureRegionPtr)rmesa->sarea->tex_list[i],
343 & rmesa->sarea->tex_age[i],
344 & rmesa->swapped,
345 sizeof( r200TexObj ),
346 (destroy_texture_object_t *) r200DestroyTexObj );
347 }
348 rmesa->texture_depth = driQueryOptioni (&rmesa->optionCache,
349 "texture_depth");
350 if (rmesa->texture_depth == DRI_CONF_TEXTURE_DEPTH_FB)
351 rmesa->texture_depth = ( screen->cpp == 4 ) ?
352 DRI_CONF_TEXTURE_DEPTH_32 : DRI_CONF_TEXTURE_DEPTH_16;
353
354 rmesa->swtcl.RenderIndex = ~0;
355 rmesa->hw.all_dirty = 1;
356
357 /* Set the maximum texture size small enough that we can guarentee that
358 * all texture units can bind a maximal texture and have all of them in
359 * texturable memory at once. Depending on the allow_large_textures driconf
360 * setting allow larger textures.
361 */
362
363 ctx = rmesa->glCtx;
364 ctx->Const.MaxTextureUnits = driQueryOptioni (&rmesa->optionCache,
365 "texture_units");
366 ctx->Const.MaxTextureImageUnits = ctx->Const.MaxTextureUnits;
367 ctx->Const.MaxTextureCoordUnits = ctx->Const.MaxTextureUnits;
368
369 i = driQueryOptioni( &rmesa->optionCache, "allow_large_textures");
370
371 driCalculateMaxTextureLevels( rmesa->texture_heaps,
372 rmesa->nr_heaps,
373 & ctx->Const,
374 4,
375 11, /* max 2D texture size is 2048x2048 */
376 #if ENABLE_HW_3D_TEXTURE
377 8, /* max 3D texture size is 256^3 */
378 #else
379 0, /* 3D textures unsupported */
380 #endif
381 11, /* max cube texture size is 2048x2048 */
382 11, /* max texture rectangle size is 2048x2048 */
383 12,
384 GL_FALSE,
385 i );
386
387 ctx->Const.MaxTextureMaxAnisotropy = 16.0;
388
389 /* No wide AA points.
390 */
391 ctx->Const.MinPointSize = 1.0;
392 ctx->Const.MinPointSizeAA = 1.0;
393 ctx->Const.MaxPointSizeAA = 1.0;
394 ctx->Const.PointSizeGranularity = 0.0625;
395 if (rmesa->r200Screen->drmSupportsPointSprites)
396 ctx->Const.MaxPointSize = 2047.0;
397 else
398 ctx->Const.MaxPointSize = 1.0;
399
400 /* mesa initialization problem - _mesa_init_point was already called */
401 ctx->Point.MaxSize = ctx->Const.MaxPointSize;
402
403 ctx->Const.MinLineWidth = 1.0;
404 ctx->Const.MinLineWidthAA = 1.0;
405 ctx->Const.MaxLineWidth = 10.0;
406 ctx->Const.MaxLineWidthAA = 10.0;
407 ctx->Const.LineWidthGranularity = 0.0625;
408
409 ctx->Const.VertexProgram.MaxNativeInstructions = R200_VSF_MAX_INST;
410 ctx->Const.VertexProgram.MaxNativeAttribs = 12;
411 ctx->Const.VertexProgram.MaxNativeTemps = R200_VSF_MAX_TEMPS;
412 ctx->Const.VertexProgram.MaxNativeParameters = R200_VSF_MAX_PARAM;
413 ctx->Const.VertexProgram.MaxNativeAddressRegs = 1;
414
415 /* Initialize the software rasterizer and helper modules.
416 */
417 _swrast_CreateContext( ctx );
418 _ac_CreateContext( ctx );
419 _tnl_CreateContext( ctx );
420 _swsetup_CreateContext( ctx );
421 _ae_create_context( ctx );
422
423 /* Install the customized pipeline:
424 */
425 _tnl_destroy_pipeline( ctx );
426 _tnl_install_pipeline( ctx, r200_pipeline );
427 ctx->Driver.FlushVertices = r200FlushVertices;
428
429 /* Try and keep materials and vertices separate:
430 */
431 _tnl_isolate_materials( ctx, GL_TRUE );
432
433
434 /* Configure swrast and TNL to match hardware characteristics:
435 */
436 _swrast_allow_pixel_fog( ctx, GL_FALSE );
437 _swrast_allow_vertex_fog( ctx, GL_TRUE );
438 _tnl_allow_pixel_fog( ctx, GL_FALSE );
439 _tnl_allow_vertex_fog( ctx, GL_TRUE );
440
441
442 for ( i = 0 ; i < R200_MAX_TEXTURE_UNITS ; i++ ) {
443 _math_matrix_ctr( &rmesa->TexGenMatrix[i] );
444 _math_matrix_set_identity( &rmesa->TexGenMatrix[i] );
445 }
446 _math_matrix_ctr( &rmesa->tmpmat );
447 _math_matrix_set_identity( &rmesa->tmpmat );
448
449 driInitExtensions( ctx, card_extensions, GL_TRUE );
450 if (!(rmesa->r200Screen->chip_flags & R200_CHIPSET_YCBCR_BROKEN)) {
451 /* yuv textures don't work with some chips - R200 / rv280 okay so far
452 others get the bit ordering right but don't actually do YUV-RGB conversion */
453 _mesa_enable_extension( ctx, "GL_MESA_ycbcr_texture" );
454 }
455 if (rmesa->glCtx->Mesa_DXTn) {
456 _mesa_enable_extension( ctx, "GL_EXT_texture_compression_s3tc" );
457 _mesa_enable_extension( ctx, "GL_S3_s3tc" );
458 }
459 else if (driQueryOptionb (&rmesa->optionCache, "force_s3tc_enable")) {
460 _mesa_enable_extension( ctx, "GL_EXT_texture_compression_s3tc" );
461 }
462
463 if (rmesa->r200Screen->drmSupportsCubeMapsR200)
464 _mesa_enable_extension( ctx, "GL_ARB_texture_cube_map" );
465 if (rmesa->r200Screen->drmSupportsBlendColor) {
466 driInitExtensions( ctx, blend_extensions, GL_FALSE );
467 }
468 if(rmesa->r200Screen->drmSupportsVertexProgram)
469 driInitSingleExtension( ctx, ARB_vp_extension );
470 if(driQueryOptionb(&rmesa->optionCache, "nv_vertex_program"))
471 driInitSingleExtension( ctx, NV_vp_extension );
472
473 if ((ctx->Const.MaxTextureUnits == 6) && rmesa->r200Screen->drmSupportsFragShader)
474 driInitSingleExtension( ctx, ATI_fs_extension );
475 if (rmesa->r200Screen->drmSupportsPointSprites)
476 driInitExtensions( ctx, point_extensions, GL_FALSE );
477 #if 0
478 r200InitDriverFuncs( ctx );
479 r200InitIoctlFuncs( ctx );
480 r200InitStateFuncs( ctx );
481 r200InitTextureFuncs( ctx );
482 #endif
483 /* plug in a few more device driver functions */
484 /* XXX these should really go right after _mesa_init_driver_functions() */
485 r200InitPixelFuncs( ctx );
486 r200InitSpanFuncs( ctx );
487 r200InitTnlFuncs( ctx );
488 r200InitState( rmesa );
489 r200InitSwtcl( ctx );
490
491 fthrottle_mode = driQueryOptioni(&rmesa->optionCache, "fthrottle_mode");
492 rmesa->iw.irq_seq = -1;
493 rmesa->irqsEmitted = 0;
494 rmesa->do_irqs = (fthrottle_mode == DRI_CONF_FTHROTTLE_IRQS &&
495 rmesa->r200Screen->irq);
496
497 rmesa->do_usleeps = (fthrottle_mode == DRI_CONF_FTHROTTLE_USLEEPS);
498
499 if (!rmesa->do_irqs)
500 fprintf(stderr,
501 "IRQ's not enabled, falling back to %s: %d %d\n",
502 rmesa->do_usleeps ? "usleeps" : "busy waits",
503 fthrottle_mode,
504 rmesa->r200Screen->irq);
505
506 rmesa->vblank_flags = (rmesa->r200Screen->irq != 0)
507 ? driGetDefaultVBlankFlags(&rmesa->optionCache) : VBLANK_FLAG_NO_IRQ;
508
509 rmesa->prefer_gart_client_texturing =
510 (getenv("R200_GART_CLIENT_TEXTURES") != 0);
511
512 (*dri_interface->getUST)( & rmesa->swap_ust );
513
514
515 #if DO_DEBUG
516 R200_DEBUG = driParseDebugString( getenv( "R200_DEBUG" ),
517 debug_control );
518 R200_DEBUG |= driParseDebugString( getenv( "RADEON_DEBUG" ),
519 debug_control );
520 #endif
521
522 tcl_mode = driQueryOptioni(&rmesa->optionCache, "tcl_mode");
523 if (driQueryOptionb(&rmesa->optionCache, "no_rast")) {
524 fprintf(stderr, "disabling 3D acceleration\n");
525 FALLBACK(rmesa, R200_FALLBACK_DISABLE, 1);
526 }
527 else if (tcl_mode == DRI_CONF_TCL_SW || getenv("R200_NO_TCL") ||
528 !(rmesa->r200Screen->chip_flags & RADEON_CHIPSET_TCL)) {
529 if (rmesa->r200Screen->chip_flags & RADEON_CHIPSET_TCL) {
530 rmesa->r200Screen->chip_flags &= ~RADEON_CHIPSET_TCL;
531 fprintf(stderr, "Disabling HW TCL support\n");
532 }
533 TCL_FALLBACK(rmesa->glCtx, R200_TCL_FALLBACK_TCL_DISABLE, 1);
534 }
535
536 if (rmesa->r200Screen->chip_flags & RADEON_CHIPSET_TCL) {
537 if (tcl_mode >= DRI_CONF_TCL_VTXFMT)
538 r200VtxfmtInit( ctx, tcl_mode >= DRI_CONF_TCL_CODEGEN );
539
540 _tnl_need_dlist_norm_lengths( ctx, GL_FALSE );
541 }
542 return GL_TRUE;
543 }
544
545
546 /* Destroy the device specific context.
547 */
548 /* Destroy the Mesa and driver specific context data.
549 */
550 void r200DestroyContext( __DRIcontextPrivate *driContextPriv )
551 {
552 GET_CURRENT_CONTEXT(ctx);
553 r200ContextPtr rmesa = (r200ContextPtr) driContextPriv->driverPrivate;
554 r200ContextPtr current = ctx ? R200_CONTEXT(ctx) : NULL;
555
556 /* check if we're deleting the currently bound context */
557 if (rmesa == current) {
558 R200_FIREVERTICES( rmesa );
559 _mesa_make_current(NULL, NULL, NULL);
560 }
561
562 /* Free r200 context resources */
563 assert(rmesa); /* should never be null */
564 if ( rmesa ) {
565 GLboolean release_texture_heaps;
566
567
568 release_texture_heaps = (rmesa->glCtx->Shared->RefCount == 1);
569 _swsetup_DestroyContext( rmesa->glCtx );
570 _tnl_DestroyContext( rmesa->glCtx );
571 _ac_DestroyContext( rmesa->glCtx );
572 _swrast_DestroyContext( rmesa->glCtx );
573
574 r200DestroySwtcl( rmesa->glCtx );
575 r200ReleaseArrays( rmesa->glCtx, ~0 );
576
577 if (rmesa->dma.current.buf) {
578 r200ReleaseDmaRegion( rmesa, &rmesa->dma.current, __FUNCTION__ );
579 r200FlushCmdBuf( rmesa, __FUNCTION__ );
580 }
581
582 if (!(rmesa->TclFallback & R200_TCL_FALLBACK_TCL_DISABLE)) {
583 int tcl_mode = driQueryOptioni(&rmesa->optionCache, "tcl_mode");
584 if (tcl_mode >= DRI_CONF_TCL_VTXFMT)
585 r200VtxfmtDestroy( rmesa->glCtx );
586 }
587
588 if (rmesa->state.scissor.pClipRects) {
589 FREE(rmesa->state.scissor.pClipRects);
590 rmesa->state.scissor.pClipRects = NULL;
591 }
592
593 if ( release_texture_heaps ) {
594 /* This share group is about to go away, free our private
595 * texture object data.
596 */
597 int i;
598
599 for ( i = 0 ; i < rmesa->nr_heaps ; i++ ) {
600 driDestroyTextureHeap( rmesa->texture_heaps[ i ] );
601 rmesa->texture_heaps[ i ] = NULL;
602 }
603
604 assert( is_empty_list( & rmesa->swapped ) );
605 }
606
607 /* free the Mesa context */
608 rmesa->glCtx->DriverCtx = NULL;
609 _mesa_destroy_context( rmesa->glCtx );
610
611 /* free the option cache */
612 driDestroyOptionCache (&rmesa->optionCache);
613
614 FREE( rmesa );
615 }
616 }
617
618
619
620
621 void
622 r200SwapBuffers( __DRIdrawablePrivate *dPriv )
623 {
624 if (dPriv->driContextPriv && dPriv->driContextPriv->driverPrivate) {
625 r200ContextPtr rmesa;
626 GLcontext *ctx;
627 rmesa = (r200ContextPtr) dPriv->driContextPriv->driverPrivate;
628 ctx = rmesa->glCtx;
629 if (ctx->Visual.doubleBufferMode) {
630 _mesa_notifySwapBuffers( ctx ); /* flush pending rendering comands */
631 if ( rmesa->doPageFlip ) {
632 r200PageFlip( dPriv );
633 }
634 else {
635 r200CopyBuffer( dPriv, NULL );
636 }
637 }
638 }
639 else {
640 /* XXX this shouldn't be an error but we can't handle it for now */
641 _mesa_problem(NULL, "%s: drawable has no context!", __FUNCTION__);
642 }
643 }
644
645 void
646 r200CopySubBuffer( __DRIdrawablePrivate *dPriv,
647 int x, int y, int w, int h )
648 {
649 if (dPriv->driContextPriv && dPriv->driContextPriv->driverPrivate) {
650 r200ContextPtr rmesa;
651 GLcontext *ctx;
652 rmesa = (r200ContextPtr) dPriv->driContextPriv->driverPrivate;
653 ctx = rmesa->glCtx;
654 if (ctx->Visual.doubleBufferMode) {
655 drm_clip_rect_t rect;
656 rect.x1 = x + dPriv->x;
657 rect.y1 = (dPriv->h - y - h) + dPriv->y;
658 rect.x2 = rect.x1 + w;
659 rect.y2 = rect.y1 + h;
660 _mesa_notifySwapBuffers( ctx ); /* flush pending rendering comands */
661 r200CopyBuffer( dPriv, &rect );
662 }
663 }
664 else {
665 /* XXX this shouldn't be an error but we can't handle it for now */
666 _mesa_problem(NULL, "%s: drawable has no context!", __FUNCTION__);
667 }
668 }
669
670 /* Force the context `c' to be the current context and associate with it
671 * buffer `b'.
672 */
673 GLboolean
674 r200MakeCurrent( __DRIcontextPrivate *driContextPriv,
675 __DRIdrawablePrivate *driDrawPriv,
676 __DRIdrawablePrivate *driReadPriv )
677 {
678 if ( driContextPriv ) {
679 r200ContextPtr newCtx =
680 (r200ContextPtr) driContextPriv->driverPrivate;
681
682 if (R200_DEBUG & DEBUG_DRI)
683 fprintf(stderr, "%s ctx %p\n", __FUNCTION__, (void *)newCtx->glCtx);
684
685 if ( newCtx->dri.drawable != driDrawPriv ) {
686 driDrawableInitVBlank( driDrawPriv, newCtx->vblank_flags,
687 &newCtx->vbl_seq );
688 }
689
690 if ( newCtx->dri.drawable != driDrawPriv ||
691 newCtx->dri.readable != driReadPriv ) {
692 newCtx->dri.drawable = driDrawPriv;
693 newCtx->dri.readable = driReadPriv;
694
695 r200UpdateWindow( newCtx->glCtx );
696 r200UpdateViewportOffset( newCtx->glCtx );
697 }
698
699 _mesa_make_current( newCtx->glCtx,
700 (GLframebuffer *) driDrawPriv->driverPrivate,
701 (GLframebuffer *) driReadPriv->driverPrivate );
702
703 if (newCtx->vb.enabled)
704 r200VtxfmtMakeCurrent( newCtx->glCtx );
705
706 _mesa_update_state( newCtx->glCtx );
707 r200ValidateState( newCtx->glCtx );
708
709 } else {
710 if (R200_DEBUG & DEBUG_DRI)
711 fprintf(stderr, "%s ctx is null\n", __FUNCTION__);
712 _mesa_make_current( NULL, NULL, NULL );
713 }
714
715 if (R200_DEBUG & DEBUG_DRI)
716 fprintf(stderr, "End %s\n", __FUNCTION__);
717 return GL_TRUE;
718 }
719
720 /* Force the context `c' to be unbound from its buffer.
721 */
722 GLboolean
723 r200UnbindContext( __DRIcontextPrivate *driContextPriv )
724 {
725 r200ContextPtr rmesa = (r200ContextPtr) driContextPriv->driverPrivate;
726
727 if (R200_DEBUG & DEBUG_DRI)
728 fprintf(stderr, "%s ctx %p\n", __FUNCTION__, (void *)rmesa->glCtx);
729
730 r200VtxfmtUnbindContext( rmesa->glCtx );
731 return GL_TRUE;
732 }