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