1 /* $XFree86: xc/lib/GL/mesa/src/drv/r200/r200_context.c,v 1.3 2003/05/06 23:52:08 daenzer Exp $ */
3 Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved.
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.
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:
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.
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.
29 **************************************************************************/
33 * Keith Whitwell <keith@tungstengraphics.com>
37 #include "api_arrayelt.h"
39 #include "simple_list.h"
42 #include "extensions.h"
45 #include "swrast/swrast.h"
46 #include "swrast_setup/swrast_setup.h"
47 #include "array_cache/acache.h"
50 #include "tnl/t_pipeline.h"
52 #include "r200_context.h"
53 #include "r200_ioctl.h"
54 #include "r200_state.h"
55 #include "r200_span.h"
56 #include "r200_pixel.h"
58 #include "r200_swtcl.h"
60 #include "r200_vtxfmt.h"
61 #include "r200_maos.h"
63 #define DRIVER_DATE "20030328"
67 #include "xmlpool.h" /* for symbolic values of enum-type options */
73 /* Return the width and height of the given buffer.
75 static void r200GetBufferSize( GLframebuffer
*buffer
,
76 GLuint
*width
, GLuint
*height
)
78 GET_CURRENT_CONTEXT(ctx
);
79 r200ContextPtr rmesa
= R200_CONTEXT(ctx
);
81 LOCK_HARDWARE( rmesa
);
82 *width
= rmesa
->dri
.drawable
->w
;
83 *height
= rmesa
->dri
.drawable
->h
;
84 UNLOCK_HARDWARE( rmesa
);
87 /* Return various strings for glGetString().
89 static const GLubyte
*r200GetString( GLcontext
*ctx
, GLenum name
)
91 r200ContextPtr rmesa
= R200_CONTEXT(ctx
);
92 static char buffer
[128];
94 GLuint agp_mode
= rmesa
->r200Screen
->IsPCI
? 0 :
95 rmesa
->r200Screen
->AGPMode
;
99 return (GLubyte
*)"Tungsten Graphics, Inc.";
102 offset
= driGetRendererString( buffer
, "R200", DRIVER_DATE
,
105 sprintf( & buffer
[ offset
], " %sTCL",
106 !(rmesa
->TclFallback
& R200_TCL_FALLBACK_TCL_DISABLE
)
109 return (GLubyte
*)buffer
;
117 /* Extension strings exported by the R200 driver.
119 static const char * const card_extensions
[] =
121 "GL_ARB_multisample",
122 "GL_ARB_multitexture",
123 "GL_ARB_texture_border_clamp",
124 "GL_ARB_texture_compression",
125 "GL_ARB_texture_env_add",
126 "GL_ARB_texture_env_combine",
127 "GL_ARB_texture_env_dot3",
128 "GL_ARB_texture_mirrored_repeat",
129 "GL_EXT_blend_logic_op",
130 "GL_EXT_blend_minmax",
131 "GL_EXT_blend_subtract",
132 "GL_EXT_secondary_color",
133 "GL_EXT_stencil_wrap",
134 "GL_EXT_texture_edge_clamp",
135 "GL_EXT_texture_env_combine",
136 "GL_EXT_texture_env_dot3",
137 "GL_EXT_texture_filter_anisotropic",
138 "GL_EXT_texture_lod_bias",
139 "GL_EXT_texture_rectangle",
140 "GL_ATI_texture_env_combine3",
141 "GL_ATI_texture_mirror_once",
142 "GL_MESA_pack_invert",
143 "GL_MESA_ycbcr_texture",
144 "GL_NV_blend_square",
145 "GL_SGIS_generate_mipmap",
149 extern const struct tnl_pipeline_stage _r200_render_stage
;
150 extern const struct tnl_pipeline_stage _r200_tcl_stage
;
152 static const struct tnl_pipeline_stage
*r200_pipeline
[] = {
154 /* Try and go straight to t&l
158 /* Catch any t&l fallbacks
160 &_tnl_vertex_transform_stage
,
161 &_tnl_normal_transform_stage
,
162 &_tnl_lighting_stage
,
163 &_tnl_fog_coordinate_stage
,
165 &_tnl_texture_transform_stage
,
167 /* Try again to go to tcl?
168 * - no good for asymmetric-twoside (do with multipass)
169 * - no good for asymmetric-unfilled (do with multipass)
170 * - good for material
172 * - need to manipulate a bit of state
174 * - worth it/not worth it?
177 /* Else do them here.
179 /* &_r200_render_stage, */ /* FIXME: bugs with ut2003 */
180 &_tnl_render_stage
, /* FALLBACK: */
186 /* Initialize the driver's misc functions.
188 static void r200InitDriverFuncs( GLcontext
*ctx
)
190 ctx
->Driver
.GetBufferSize
= r200GetBufferSize
;
191 ctx
->Driver
.ResizeBuffers
= _swrast_alloc_buffers
;
192 ctx
->Driver
.GetString
= r200GetString
;
194 ctx
->Driver
.Error
= NULL
;
195 ctx
->Driver
.DrawPixels
= NULL
;
196 ctx
->Driver
.Bitmap
= NULL
;
199 static const struct dri_debug_control debug_control
[] =
201 { "fall", DEBUG_FALLBACKS
},
202 { "tex", DEBUG_TEXTURE
},
203 { "ioctl", DEBUG_IOCTL
},
204 { "prim", DEBUG_PRIMS
},
205 { "vert", DEBUG_VERTS
},
206 { "state", DEBUG_STATE
},
207 { "code", DEBUG_CODEGEN
},
208 { "vfmt", DEBUG_VFMT
},
209 { "vtxf", DEBUG_VFMT
},
210 { "verb", DEBUG_VERBOSE
},
211 { "dri", DEBUG_DRI
},
212 { "dma", DEBUG_DMA
},
213 { "san", DEBUG_SANITY
},
214 { "sync", DEBUG_SYNC
},
215 { "pix", DEBUG_PIXEL
},
216 { "mem", DEBUG_MEMORY
},
222 get_ust_nop( int64_t * ust
)
229 /* Create the device specific context.
231 GLboolean
r200CreateContext( const __GLcontextModes
*glVisual
,
232 __DRIcontextPrivate
*driContextPriv
,
233 void *sharedContextPrivate
)
235 __DRIscreenPrivate
*sPriv
= driContextPriv
->driScreenPriv
;
236 r200ScreenPtr screen
= (r200ScreenPtr
)(sPriv
->private);
237 r200ContextPtr rmesa
;
238 GLcontext
*ctx
, *shareCtx
;
240 int tcl_mode
, fthrottle_mode
;
243 assert(driContextPriv
);
246 /* Allocate the R200 context */
247 rmesa
= (r200ContextPtr
) CALLOC( sizeof(*rmesa
) );
251 /* Allocate the Mesa context */
252 if (sharedContextPrivate
)
253 shareCtx
= ((r200ContextPtr
) sharedContextPrivate
)->glCtx
;
256 rmesa
->glCtx
= _mesa_create_context(glVisual
, shareCtx
, (void *) rmesa
, GL_TRUE
);
261 driContextPriv
->driverPrivate
= rmesa
;
263 /* Init r200 context data */
264 rmesa
->dri
.context
= driContextPriv
;
265 rmesa
->dri
.screen
= sPriv
;
266 rmesa
->dri
.drawable
= NULL
; /* Set by XMesaMakeCurrent */
267 rmesa
->dri
.hwContext
= driContextPriv
->hHWContext
;
268 rmesa
->dri
.hwLock
= &sPriv
->pSAREA
->lock
;
269 rmesa
->dri
.fd
= sPriv
->fd
;
270 rmesa
->dri
.drmMinor
= sPriv
->drmMinor
;
272 /* Parse configuration files */
273 driParseConfigFiles (&rmesa
->optionCache
, &screen
->optionCache
,
274 screen
->driScreen
->myNum
, "r200");
276 rmesa
->r200Screen
= screen
;
277 rmesa
->sarea
= (RADEONSAREAPrivPtr
)((GLubyte
*)sPriv
->pSAREA
+
278 screen
->sarea_priv_offset
);
281 rmesa
->dma
.buf0_address
= rmesa
->r200Screen
->buffers
->list
[0].address
;
283 (void) memset( rmesa
->texture_heaps
, 0, sizeof( rmesa
->texture_heaps
) );
284 make_empty_list( & rmesa
->swapped
);
286 rmesa
->nr_heaps
= 1 /* screen->numTexHeaps */ ;
287 for ( i
= 0 ; i
< rmesa
->nr_heaps
; i
++ ) {
288 rmesa
->texture_heaps
[i
] = driCreateTextureHeap( i
, rmesa
,
291 RADEON_NR_TEX_REGIONS
,
292 rmesa
->sarea
->texList
[i
],
293 & rmesa
->sarea
->texAge
[i
],
295 sizeof( r200TexObj
),
296 (destroy_texture_object_t
*) r200DestroyTexObj
);
298 rmesa
->texture_depth
= driQueryOptioni (&rmesa
->optionCache
,
300 if (rmesa
->texture_depth
== DRI_CONF_TEXTURE_DEPTH_FB
)
301 rmesa
->texture_depth
= ( screen
->cpp
== 4 ) ?
302 DRI_CONF_TEXTURE_DEPTH_32
: DRI_CONF_TEXTURE_DEPTH_16
;
304 rmesa
->swtcl
.RenderIndex
= ~0;
305 rmesa
->lost_context
= 1;
307 /* Set the maximum texture size small enough that we can guarentee that
308 * all texture units can bind a maximal texture and have them both in
309 * texturable memory at once.
313 ctx
->Const
.MaxTextureUnits
= 2;
314 ctx
->Const
.MaxTextureImageUnits
= 2;
315 ctx
->Const
.MaxTextureCoordUnits
= 2;
317 driCalculateMaxTextureLevels( rmesa
->texture_heaps
,
321 11, /* max 2D texture size is 2048x2048 */
322 #if ENABLE_HW_3D_TEXTURE
323 8, /* max 3D texture size is 256^3 */
325 0, /* 3D textures unsupported */
327 11, /* max cube texture size is 2048x2048 */
328 11, /* max texture rectangle size is 2048x2048 */
332 ctx
->Const
.MaxTextureMaxAnisotropy
= 16.0;
336 ctx
->Const
.MinPointSize
= 1.0;
337 ctx
->Const
.MinPointSizeAA
= 1.0;
338 ctx
->Const
.MaxPointSize
= 1.0;
339 ctx
->Const
.MaxPointSizeAA
= 1.0;
341 ctx
->Const
.MinLineWidth
= 1.0;
342 ctx
->Const
.MinLineWidthAA
= 1.0;
343 ctx
->Const
.MaxLineWidth
= 10.0;
344 ctx
->Const
.MaxLineWidthAA
= 10.0;
345 ctx
->Const
.LineWidthGranularity
= 0.0625;
347 /* Initialize the software rasterizer and helper modules.
349 _swrast_CreateContext( ctx
);
350 _ac_CreateContext( ctx
);
351 _tnl_CreateContext( ctx
);
352 _swsetup_CreateContext( ctx
);
353 _ae_create_context( ctx
);
355 /* Install the customized pipeline:
357 _tnl_destroy_pipeline( ctx
);
358 _tnl_install_pipeline( ctx
, r200_pipeline
);
359 ctx
->Driver
.FlushVertices
= r200FlushVertices
;
361 /* Try and keep materials and vertices separate:
363 _tnl_isolate_materials( ctx
, GL_TRUE
);
366 /* Configure swrast to match hardware characteristics:
368 _swrast_allow_pixel_fog( ctx
, GL_FALSE
);
369 _swrast_allow_vertex_fog( ctx
, GL_TRUE
);
372 _math_matrix_ctr( &rmesa
->TexGenMatrix
[0] );
373 _math_matrix_ctr( &rmesa
->TexGenMatrix
[1] );
374 _math_matrix_ctr( &rmesa
->tmpmat
);
375 _math_matrix_set_identity( &rmesa
->TexGenMatrix
[0] );
376 _math_matrix_set_identity( &rmesa
->TexGenMatrix
[1] );
377 _math_matrix_set_identity( &rmesa
->tmpmat
);
379 driInitExtensions( ctx
, card_extensions
, GL_TRUE
);
380 if (rmesa
->r200Screen
->drmSupportsCubeMaps
)
381 _mesa_enable_extension( ctx
, "GL_ARB_texture_cube_map" );
383 r200InitDriverFuncs( ctx
);
384 r200InitIoctlFuncs( ctx
);
385 r200InitStateFuncs( ctx
);
386 r200InitSpanFuncs( ctx
);
387 r200InitPixelFuncs( ctx
);
388 r200InitTextureFuncs( ctx
);
389 r200InitState( rmesa
);
390 r200InitSwtcl( ctx
);
392 fthrottle_mode
= driQueryOptioni(&rmesa
->optionCache
, "fthrottle_mode");
393 rmesa
->iw
.irq_seq
= -1;
394 rmesa
->irqsEmitted
= 0;
395 rmesa
->do_irqs
= (rmesa
->dri
.drmMinor
>= 6 &&
396 fthrottle_mode
== DRI_CONF_FTHROTTLE_IRQS
&&
397 rmesa
->r200Screen
->irq
);
399 rmesa
->do_usleeps
= (fthrottle_mode
== DRI_CONF_FTHROTTLE_USLEEPS
);
403 "IRQ's not enabled, falling back to %s: %d %d %d\n",
404 rmesa
->do_usleeps
? "usleeps" : "busy waits",
407 rmesa
->r200Screen
->irq
);
409 rmesa
->vblank_flags
= (rmesa
->r200Screen
->irq
!= 0)
410 ? driGetDefaultVBlankFlags(&rmesa
->optionCache
) : VBLANK_FLAG_NO_IRQ
;
412 rmesa
->prefer_gart_client_texturing
=
413 (getenv("R200_GART_CLIENT_TEXTURES") != 0);
415 rmesa
->get_ust
= (PFNGLXGETUSTPROC
) glXGetProcAddress( (const GLubyte
*) "__glXGetUST" );
416 if ( rmesa
->get_ust
== NULL
) {
417 rmesa
->get_ust
= get_ust_nop
;
420 rmesa
->get_ust
= get_ust_nop
;
422 (*rmesa
->get_ust
)( & rmesa
->swap_ust
);
426 R200_DEBUG
= driParseDebugString( getenv( "R200_DEBUG" ),
428 R200_DEBUG
|= driParseDebugString( getenv( "RADEON_DEBUG" ),
432 tcl_mode
= driQueryOptioni(&rmesa
->optionCache
, "tcl_mode");
433 if (driQueryOptionb(&rmesa
->optionCache
, "no_rast")) {
434 fprintf(stderr
, "disabling 3D acceleration\n");
435 FALLBACK(rmesa
, R200_FALLBACK_DISABLE
, 1);
437 else if (tcl_mode
== DRI_CONF_TCL_SW
|| getenv("R200_NO_TCL") ||
438 !(rmesa
->r200Screen
->chipset
& R200_CHIPSET_TCL
)) {
439 rmesa
->r200Screen
->chipset
&= ~R200_CHIPSET_TCL
;
440 fprintf(stderr
, "disabling TCL support\n");
441 TCL_FALLBACK(rmesa
->glCtx
, R200_TCL_FALLBACK_TCL_DISABLE
, 1);
443 if (rmesa
->r200Screen
->chipset
& R200_CHIPSET_TCL
) {
444 if (tcl_mode
>= DRI_CONF_TCL_VTXFMT
&& !getenv("R200_NO_VTXFMT")) {
445 r200VtxfmtInit( ctx
, tcl_mode
>= DRI_CONF_TCL_CODEGEN
);
447 _tnl_need_dlist_norm_lengths( ctx
, GL_FALSE
);
453 /* Destroy the device specific context.
455 /* Destroy the Mesa and driver specific context data.
457 void r200DestroyContext( __DRIcontextPrivate
*driContextPriv
)
459 GET_CURRENT_CONTEXT(ctx
);
460 r200ContextPtr rmesa
= (r200ContextPtr
) driContextPriv
->driverPrivate
;
461 r200ContextPtr current
= ctx
? R200_CONTEXT(ctx
) : NULL
;
463 /* check if we're deleting the currently bound context */
464 if (rmesa
== current
) {
465 R200_FIREVERTICES( rmesa
);
466 _mesa_make_current2(NULL
, NULL
, NULL
);
469 /* Free r200 context resources */
470 assert(rmesa
); /* should never be null */
472 GLboolean release_texture_heaps
;
475 release_texture_heaps
= (rmesa
->glCtx
->Shared
->RefCount
== 1);
476 _swsetup_DestroyContext( rmesa
->glCtx
);
477 _tnl_DestroyContext( rmesa
->glCtx
);
478 _ac_DestroyContext( rmesa
->glCtx
);
479 _swrast_DestroyContext( rmesa
->glCtx
);
481 r200DestroySwtcl( rmesa
->glCtx
);
482 r200ReleaseArrays( rmesa
->glCtx
, ~0 );
484 if (rmesa
->dma
.current
.buf
) {
485 r200ReleaseDmaRegion( rmesa
, &rmesa
->dma
.current
, __FUNCTION__
);
486 r200FlushCmdBuf( rmesa
, __FUNCTION__
);
489 if (!(rmesa
->TclFallback
& R200_TCL_FALLBACK_TCL_DISABLE
)) {
490 int tcl_mode
= driQueryOptioni(&rmesa
->optionCache
, "tcl_mode");
491 if (tcl_mode
>= DRI_CONF_TCL_VTXFMT
)
492 r200VtxfmtDestroy( rmesa
->glCtx
);
495 /* free the Mesa context */
496 rmesa
->glCtx
->DriverCtx
= NULL
;
497 _mesa_destroy_context( rmesa
->glCtx
);
499 if (rmesa
->state
.scissor
.pClipRects
) {
500 FREE(rmesa
->state
.scissor
.pClipRects
);
501 rmesa
->state
.scissor
.pClipRects
= 0;
504 if ( release_texture_heaps
) {
505 /* This share group is about to go away, free our private
506 * texture object data.
510 for ( i
= 0 ; i
< rmesa
->nr_heaps
; i
++ ) {
511 driDestroyTextureHeap( rmesa
->texture_heaps
[ i
] );
512 rmesa
->texture_heaps
[ i
] = NULL
;
515 assert( is_empty_list( & rmesa
->swapped
) );
518 /* free the option cache */
519 driDestroyOptionCache (&rmesa
->optionCache
);
529 r200SwapBuffers( __DRIdrawablePrivate
*dPriv
)
531 if (dPriv
->driContextPriv
&& dPriv
->driContextPriv
->driverPrivate
) {
532 r200ContextPtr rmesa
;
534 rmesa
= (r200ContextPtr
) dPriv
->driContextPriv
->driverPrivate
;
536 if (ctx
->Visual
.doubleBufferMode
) {
537 _mesa_notifySwapBuffers( ctx
); /* flush pending rendering comands */
538 if ( rmesa
->doPageFlip
) {
539 r200PageFlip( dPriv
);
542 r200CopyBuffer( dPriv
);
547 /* XXX this shouldn't be an error but we can't handle it for now */
548 _mesa_problem(NULL
, "%s: drawable has no context!", __FUNCTION__
);
553 /* Force the context `c' to be the current context and associate with it
557 r200MakeCurrent( __DRIcontextPrivate
*driContextPriv
,
558 __DRIdrawablePrivate
*driDrawPriv
,
559 __DRIdrawablePrivate
*driReadPriv
)
561 if ( driContextPriv
) {
562 r200ContextPtr newCtx
=
563 (r200ContextPtr
) driContextPriv
->driverPrivate
;
565 if (R200_DEBUG
& DEBUG_DRI
)
566 fprintf(stderr
, "%s ctx %p\n", __FUNCTION__
, (void *)newCtx
->glCtx
);
568 if ( newCtx
->dri
.drawable
!= driDrawPriv
) {
569 driDrawableInitVBlank( driDrawPriv
, newCtx
->vblank_flags
);
570 newCtx
->dri
.drawable
= driDrawPriv
;
571 r200UpdateWindow( newCtx
->glCtx
);
572 r200UpdateViewportOffset( newCtx
->glCtx
);
575 _mesa_make_current2( newCtx
->glCtx
,
576 (GLframebuffer
*) driDrawPriv
->driverPrivate
,
577 (GLframebuffer
*) driReadPriv
->driverPrivate
);
579 if ( !newCtx
->glCtx
->Viewport
.Width
) {
580 _mesa_set_viewport( newCtx
->glCtx
, 0, 0,
581 driDrawPriv
->w
, driDrawPriv
->h
);
584 if (newCtx
->vb
.enabled
)
585 r200VtxfmtMakeCurrent( newCtx
->glCtx
);
587 _mesa_update_state( newCtx
->glCtx
);
588 r200ValidateState( newCtx
->glCtx
);
591 if (R200_DEBUG
& DEBUG_DRI
)
592 fprintf(stderr
, "%s ctx is null\n", __FUNCTION__
);
593 _mesa_make_current( 0, 0 );
596 if (R200_DEBUG
& DEBUG_DRI
)
597 fprintf(stderr
, "End %s\n", __FUNCTION__
);
601 /* Force the context `c' to be unbound from its buffer.
604 r200UnbindContext( __DRIcontextPrivate
*driContextPriv
)
606 r200ContextPtr rmesa
= (r200ContextPtr
) driContextPriv
->driverPrivate
;
608 if (R200_DEBUG
& DEBUG_DRI
)
609 fprintf(stderr
, "%s ctx %p\n", __FUNCTION__
, (void *)rmesa
->glCtx
);
611 r200VtxfmtUnbindContext( rmesa
->glCtx
);