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