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