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