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