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