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"
76 const char __driConfigOptions
[] =
78 DRI_CONF_SECTION_PERFORMANCE
79 DRI_CONF_TCL_MODE(DRI_CONF_TCL_CODEGEN
)
80 DRI_CONF_FTHROTTLE_MODE(DRI_CONF_FTHROTTLE_IRQS
)
81 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_0
)
83 DRI_CONF_SECTION_QUALITY
84 DRI_CONF_TEXTURE_DEPTH(DRI_CONF_TEXTURE_DEPTH_FB
)
85 DRI_CONF_COLOR_REDUCTION(DRI_CONF_COLOR_REDUCTION_DITHER
)
86 DRI_CONF_ROUND_MODE(DRI_CONF_ROUND_TRUNC
)
87 DRI_CONF_DITHER_MODE(DRI_CONF_DITHER_XERRORDIFF
)
89 DRI_CONF_SECTION_DEBUG
90 DRI_CONF_NO_RAST(false)
93 const GLuint __driNConfigOptions
= 8;
96 /* Return the width and height of the given buffer.
98 static void r200GetBufferSize( GLframebuffer
*buffer
,
99 GLuint
*width
, GLuint
*height
)
101 GET_CURRENT_CONTEXT(ctx
);
102 r200ContextPtr rmesa
= R200_CONTEXT(ctx
);
104 LOCK_HARDWARE( rmesa
);
105 *width
= rmesa
->dri
.drawable
->w
;
106 *height
= rmesa
->dri
.drawable
->h
;
107 UNLOCK_HARDWARE( rmesa
);
110 /* Return various strings for glGetString().
112 static const GLubyte
*r200GetString( GLcontext
*ctx
, GLenum name
)
114 r200ContextPtr rmesa
= R200_CONTEXT(ctx
);
115 static char buffer
[128];
117 GLuint agp_mode
= rmesa
->r200Screen
->IsPCI
? 0 :
118 rmesa
->r200Screen
->AGPMode
;
122 return (GLubyte
*)"Tungsten Graphics, Inc.";
125 offset
= driGetRendererString( buffer
, "R200", DRIVER_DATE
,
128 sprintf( & buffer
[ offset
], " %sTCL",
129 !(rmesa
->TclFallback
& R200_TCL_FALLBACK_TCL_DISABLE
)
132 return (GLubyte
*)buffer
;
140 /* Extension strings exported by the R200 driver.
142 static const char * const card_extensions
[] =
144 "GL_ARB_multisample",
145 "GL_ARB_multitexture",
146 "GL_ARB_texture_border_clamp",
147 "GL_ARB_texture_compression",
148 "GL_ARB_texture_env_add",
149 "GL_ARB_texture_env_combine",
150 "GL_ARB_texture_env_dot3",
151 "GL_ARB_texture_mirrored_repeat",
152 "GL_EXT_blend_logic_op",
153 "GL_EXT_blend_minmax",
154 "GL_EXT_blend_subtract",
155 "GL_EXT_secondary_color",
156 "GL_EXT_stencil_wrap",
157 "GL_EXT_texture_edge_clamp",
158 "GL_EXT_texture_env_add",
159 "GL_EXT_texture_env_combine",
160 "GL_EXT_texture_env_dot3",
161 "GL_EXT_texture_filter_anisotropic",
162 "GL_EXT_texture_lod_bias",
163 "GL_ATI_texture_env_combine3",
164 "GL_ATI_texture_mirror_once",
165 "GL_IBM_texture_mirrored_repeat",
166 "GL_MESA_pack_invert",
167 "GL_MESA_ycbcr_texture",
168 "GL_NV_blend_square",
169 "GL_NV_texture_rectangle",
170 "GL_SGIS_generate_mipmap",
171 "GL_SGIS_texture_border_clamp",
172 "GL_SGIS_texture_edge_clamp",
176 extern const struct tnl_pipeline_stage _r200_render_stage
;
177 extern const struct tnl_pipeline_stage _r200_tcl_stage
;
179 static const struct tnl_pipeline_stage
*r200_pipeline
[] = {
181 /* Try and go straight to t&l
185 /* Catch any t&l fallbacks
187 &_tnl_vertex_transform_stage
,
188 &_tnl_normal_transform_stage
,
189 &_tnl_lighting_stage
,
190 &_tnl_fog_coordinate_stage
,
192 &_tnl_texture_transform_stage
,
194 /* Try again to go to tcl?
195 * - no good for asymmetric-twoside (do with multipass)
196 * - no good for asymmetric-unfilled (do with multipass)
197 * - good for material
199 * - need to manipulate a bit of state
201 * - worth it/not worth it?
204 /* Else do them here.
206 /* &_r200_render_stage, */ /* FIXME: bugs with ut2003 */
207 &_tnl_render_stage
, /* FALLBACK: */
213 /* Initialize the driver's misc functions.
215 static void r200InitDriverFuncs( GLcontext
*ctx
)
217 ctx
->Driver
.GetBufferSize
= r200GetBufferSize
;
218 ctx
->Driver
.ResizeBuffers
= _swrast_alloc_buffers
;
219 ctx
->Driver
.GetString
= r200GetString
;
221 ctx
->Driver
.Error
= NULL
;
222 ctx
->Driver
.DrawPixels
= NULL
;
223 ctx
->Driver
.Bitmap
= NULL
;
226 static const struct dri_debug_control debug_control
[] =
228 { "fall", DEBUG_FALLBACKS
},
229 { "tex", DEBUG_TEXTURE
},
230 { "ioctl", DEBUG_IOCTL
},
231 { "prim", DEBUG_PRIMS
},
232 { "vert", DEBUG_VERTS
},
233 { "state", DEBUG_STATE
},
234 { "code", DEBUG_CODEGEN
},
235 { "vfmt", DEBUG_VFMT
},
236 { "vtxf", DEBUG_VFMT
},
237 { "verb", DEBUG_VERBOSE
},
238 { "dri", DEBUG_DRI
},
239 { "dma", DEBUG_DMA
},
240 { "san", DEBUG_SANITY
},
241 { "sync", DEBUG_SYNC
},
242 { "pix", DEBUG_PIXEL
},
243 { "mem", DEBUG_MEMORY
},
249 get_ust_nop( int64_t * ust
)
256 /* Create the device specific context.
258 GLboolean
r200CreateContext( const __GLcontextModes
*glVisual
,
259 __DRIcontextPrivate
*driContextPriv
,
260 void *sharedContextPrivate
)
262 __DRIscreenPrivate
*sPriv
= driContextPriv
->driScreenPriv
;
263 r200ScreenPtr screen
= (r200ScreenPtr
)(sPriv
->private);
264 r200ContextPtr rmesa
;
265 GLcontext
*ctx
, *shareCtx
;
267 int tcl_mode
, fthrottle_mode
;
270 assert(driContextPriv
);
273 /* Allocate the R200 context */
274 rmesa
= (r200ContextPtr
) CALLOC( sizeof(*rmesa
) );
278 /* Allocate the Mesa context */
279 if (sharedContextPrivate
)
280 shareCtx
= ((r200ContextPtr
) sharedContextPrivate
)->glCtx
;
283 rmesa
->glCtx
= _mesa_create_context(glVisual
, shareCtx
, (void *) rmesa
, GL_TRUE
);
288 driContextPriv
->driverPrivate
= rmesa
;
290 /* Init r200 context data */
291 rmesa
->dri
.context
= driContextPriv
;
292 rmesa
->dri
.screen
= sPriv
;
293 rmesa
->dri
.drawable
= NULL
; /* Set by XMesaMakeCurrent */
294 rmesa
->dri
.hwContext
= driContextPriv
->hHWContext
;
295 rmesa
->dri
.hwLock
= &sPriv
->pSAREA
->lock
;
296 rmesa
->dri
.fd
= sPriv
->fd
;
297 rmesa
->dri
.drmMinor
= sPriv
->drmMinor
;
299 /* Parse configuration files */
300 driParseConfigFiles (&rmesa
->optionCache
, &screen
->optionCache
,
301 screen
->driScreen
->myNum
, "r200");
303 rmesa
->r200Screen
= screen
;
304 rmesa
->sarea
= (RADEONSAREAPrivPtr
)((GLubyte
*)sPriv
->pSAREA
+
305 screen
->sarea_priv_offset
);
308 rmesa
->dma
.buf0_address
= rmesa
->r200Screen
->buffers
->list
[0].address
;
310 (void) memset( rmesa
->texture_heaps
, 0, sizeof( rmesa
->texture_heaps
) );
311 make_empty_list( & rmesa
->swapped
);
313 rmesa
->nr_heaps
= 1 /* screen->numTexHeaps */ ;
314 for ( i
= 0 ; i
< rmesa
->nr_heaps
; i
++ ) {
315 rmesa
->texture_heaps
[i
] = driCreateTextureHeap( i
, rmesa
,
318 RADEON_NR_TEX_REGIONS
,
319 rmesa
->sarea
->texList
[i
],
320 & rmesa
->sarea
->texAge
[i
],
322 sizeof( r200TexObj
),
323 (destroy_texture_object_t
*) r200DestroyTexObj
);
325 rmesa
->texture_depth
= driQueryOptioni (&rmesa
->optionCache
,
327 if (rmesa
->texture_depth
== DRI_CONF_TEXTURE_DEPTH_FB
)
328 rmesa
->texture_depth
= ( screen
->cpp
== 4 ) ?
329 DRI_CONF_TEXTURE_DEPTH_32
: DRI_CONF_TEXTURE_DEPTH_16
;
331 rmesa
->swtcl
.RenderIndex
= ~0;
332 rmesa
->lost_context
= 1;
334 /* Set the maximum texture size small enough that we can guarentee that
335 * all texture units can bind a maximal texture and have them both in
336 * texturable memory at once.
340 ctx
->Const
.MaxTextureUnits
= 2;
342 driCalculateMaxTextureLevels( rmesa
->texture_heaps
,
346 11, /* max 2D texture size is 2048x2048 */
347 #if ENABLE_HW_3D_TEXTURE
348 8, /* max 3D texture size is 256^3 */
350 0, /* 3D textures unsupported */
352 11, /* max cube texture size is 2048x2048 */
353 11, /* max texture rectangle size is 2048x2048 */
357 ctx
->Const
.MaxTextureMaxAnisotropy
= 16.0;
361 ctx
->Const
.MinPointSize
= 1.0;
362 ctx
->Const
.MinPointSizeAA
= 1.0;
363 ctx
->Const
.MaxPointSize
= 1.0;
364 ctx
->Const
.MaxPointSizeAA
= 1.0;
366 ctx
->Const
.MinLineWidth
= 1.0;
367 ctx
->Const
.MinLineWidthAA
= 1.0;
368 ctx
->Const
.MaxLineWidth
= 10.0;
369 ctx
->Const
.MaxLineWidthAA
= 10.0;
370 ctx
->Const
.LineWidthGranularity
= 0.0625;
372 /* Initialize the software rasterizer and helper modules.
374 _swrast_CreateContext( ctx
);
375 _ac_CreateContext( ctx
);
376 _tnl_CreateContext( ctx
);
377 _swsetup_CreateContext( ctx
);
378 _ae_create_context( ctx
);
380 /* Install the customized pipeline:
382 _tnl_destroy_pipeline( ctx
);
383 _tnl_install_pipeline( ctx
, r200_pipeline
);
384 ctx
->Driver
.FlushVertices
= r200FlushVertices
;
386 /* Try and keep materials and vertices separate:
388 _tnl_isolate_materials( ctx
, GL_TRUE
);
391 /* Configure swrast to match hardware characteristics:
393 _swrast_allow_pixel_fog( ctx
, GL_FALSE
);
394 _swrast_allow_vertex_fog( ctx
, GL_TRUE
);
397 _math_matrix_ctr( &rmesa
->TexGenMatrix
[0] );
398 _math_matrix_ctr( &rmesa
->TexGenMatrix
[1] );
399 _math_matrix_ctr( &rmesa
->tmpmat
);
400 _math_matrix_set_identity( &rmesa
->TexGenMatrix
[0] );
401 _math_matrix_set_identity( &rmesa
->TexGenMatrix
[1] );
402 _math_matrix_set_identity( &rmesa
->tmpmat
);
404 driInitExtensions( ctx
, card_extensions
, GL_TRUE
);
405 if (rmesa
->r200Screen
->drmSupportsCubeMaps
)
406 _mesa_enable_extension( ctx
, "GL_ARB_texture_cube_map" );
408 r200InitDriverFuncs( ctx
);
409 r200InitIoctlFuncs( ctx
);
410 r200InitStateFuncs( ctx
);
411 r200InitSpanFuncs( ctx
);
412 r200InitPixelFuncs( ctx
);
413 r200InitTextureFuncs( ctx
);
414 r200InitState( rmesa
);
415 r200InitSwtcl( ctx
);
417 fthrottle_mode
= driQueryOptioni(&rmesa
->optionCache
, "fthrottle_mode");
418 rmesa
->iw
.irq_seq
= -1;
419 rmesa
->irqsEmitted
= 0;
420 rmesa
->do_irqs
= (rmesa
->dri
.drmMinor
>= 6 &&
421 fthrottle_mode
== DRI_CONF_FTHROTTLE_IRQS
&&
422 rmesa
->r200Screen
->irq
);
424 rmesa
->do_usleeps
= (fthrottle_mode
== DRI_CONF_FTHROTTLE_USLEEPS
);
428 "IRQ's not enabled, falling back to %s: %d %d %d\n",
429 rmesa
->do_usleeps
? "usleeps" : "busy waits",
432 rmesa
->r200Screen
->irq
);
434 rmesa
->vblank_flags
= (rmesa
->r200Screen
->irq
!= 0)
435 ? driGetDefaultVBlankFlags(&rmesa
->optionCache
) : VBLANK_FLAG_NO_IRQ
;
437 rmesa
->prefer_gart_client_texturing
=
438 (getenv("R200_GART_CLIENT_TEXTURES") != 0);
440 rmesa
->get_ust
= (PFNGLXGETUSTPROC
) glXGetProcAddress( (const GLubyte
*) "__glXGetUST" );
441 if ( rmesa
->get_ust
== NULL
) {
442 rmesa
->get_ust
= get_ust_nop
;
445 rmesa
->get_ust
= get_ust_nop
;
447 (*rmesa
->get_ust
)( & rmesa
->swap_ust
);
451 R200_DEBUG
= driParseDebugString( getenv( "R200_DEBUG" ),
453 R200_DEBUG
|= driParseDebugString( getenv( "RADEON_DEBUG" ),
457 tcl_mode
= driQueryOptioni(&rmesa
->optionCache
, "tcl_mode");
458 if (driQueryOptionb(&rmesa
->optionCache
, "no_rast")) {
459 fprintf(stderr
, "disabling 3D acceleration\n");
460 FALLBACK(rmesa
, R200_FALLBACK_DISABLE
, 1);
462 else if (tcl_mode
== DRI_CONF_TCL_SW
) {
463 fprintf(stderr
, "disabling TCL support\n");
464 TCL_FALLBACK(rmesa
->glCtx
, R200_TCL_FALLBACK_TCL_DISABLE
, 1);
467 if (tcl_mode
>= DRI_CONF_TCL_VTXFMT
) {
468 r200VtxfmtInit( ctx
, tcl_mode
>= DRI_CONF_TCL_CODEGEN
);
470 _tnl_need_dlist_norm_lengths( ctx
, GL_FALSE
);
476 /* Destroy the device specific context.
478 /* Destroy the Mesa and driver specific context data.
480 void r200DestroyContext( __DRIcontextPrivate
*driContextPriv
)
482 GET_CURRENT_CONTEXT(ctx
);
483 r200ContextPtr rmesa
= (r200ContextPtr
) driContextPriv
->driverPrivate
;
484 r200ContextPtr current
= ctx
? R200_CONTEXT(ctx
) : NULL
;
486 /* check if we're deleting the currently bound context */
487 if (rmesa
== current
) {
488 R200_FIREVERTICES( rmesa
);
489 _mesa_make_current2(NULL
, NULL
, NULL
);
492 /* Free r200 context resources */
493 assert(rmesa
); /* should never be null */
495 GLboolean release_texture_heaps
;
498 release_texture_heaps
= (rmesa
->glCtx
->Shared
->RefCount
== 1);
499 _swsetup_DestroyContext( rmesa
->glCtx
);
500 _tnl_DestroyContext( rmesa
->glCtx
);
501 _ac_DestroyContext( rmesa
->glCtx
);
502 _swrast_DestroyContext( rmesa
->glCtx
);
504 r200DestroySwtcl( rmesa
->glCtx
);
505 r200ReleaseArrays( rmesa
->glCtx
, ~0 );
507 if (rmesa
->dma
.current
.buf
) {
508 r200ReleaseDmaRegion( rmesa
, &rmesa
->dma
.current
, __FUNCTION__
);
509 r200FlushCmdBuf( rmesa
, __FUNCTION__
);
512 if (!(rmesa
->TclFallback
& R200_TCL_FALLBACK_TCL_DISABLE
)) {
513 int tcl_mode
= driQueryOptioni(&rmesa
->optionCache
, "tcl_mode");
514 if (tcl_mode
>= DRI_CONF_TCL_VTXFMT
)
515 r200VtxfmtDestroy( rmesa
->glCtx
);
518 /* free the Mesa context */
519 rmesa
->glCtx
->DriverCtx
= NULL
;
520 _mesa_destroy_context( rmesa
->glCtx
);
522 if (rmesa
->state
.scissor
.pClipRects
) {
523 FREE(rmesa
->state
.scissor
.pClipRects
);
524 rmesa
->state
.scissor
.pClipRects
= 0;
527 if ( release_texture_heaps
) {
528 /* This share group is about to go away, free our private
529 * texture object data.
533 /* this assert is wrong. The default textures are always on swap list
534 assert( is_empty_list( & rmesa->swapped ) ); */
536 for ( i
= 0 ; i
< rmesa
->nr_heaps
; i
++ ) {
537 driDestroyTextureHeap( rmesa
->texture_heaps
[ i
] );
538 rmesa
->texture_heaps
[ i
] = NULL
;
542 /* free the option cache */
543 driDestroyOptionCache (&rmesa
->optionCache
);
553 r200SwapBuffers( __DRIdrawablePrivate
*dPriv
)
555 if (dPriv
->driContextPriv
&& dPriv
->driContextPriv
->driverPrivate
) {
556 r200ContextPtr rmesa
;
558 rmesa
= (r200ContextPtr
) dPriv
->driContextPriv
->driverPrivate
;
560 if (ctx
->Visual
.doubleBufferMode
) {
561 _mesa_notifySwapBuffers( ctx
); /* flush pending rendering comands */
562 if ( rmesa
->doPageFlip
) {
563 r200PageFlip( dPriv
);
566 r200CopyBuffer( dPriv
);
571 /* XXX this shouldn't be an error but we can't handle it for now */
572 _mesa_problem(NULL
, "%s: drawable has no context!", __FUNCTION__
);
577 /* Force the context `c' to be the current context and associate with it
581 r200MakeCurrent( __DRIcontextPrivate
*driContextPriv
,
582 __DRIdrawablePrivate
*driDrawPriv
,
583 __DRIdrawablePrivate
*driReadPriv
)
585 if ( driContextPriv
) {
586 r200ContextPtr newCtx
=
587 (r200ContextPtr
) driContextPriv
->driverPrivate
;
589 if (R200_DEBUG
& DEBUG_DRI
)
590 fprintf(stderr
, "%s ctx %p\n", __FUNCTION__
, newCtx
->glCtx
);
592 if ( newCtx
->dri
.drawable
!= driDrawPriv
) {
593 driDrawableInitVBlank( driDrawPriv
, newCtx
->vblank_flags
);
594 newCtx
->dri
.drawable
= driDrawPriv
;
595 r200UpdateWindow( newCtx
->glCtx
);
596 r200UpdateViewportOffset( newCtx
->glCtx
);
599 _mesa_make_current2( newCtx
->glCtx
,
600 (GLframebuffer
*) driDrawPriv
->driverPrivate
,
601 (GLframebuffer
*) driReadPriv
->driverPrivate
);
603 if ( !newCtx
->glCtx
->Viewport
.Width
) {
604 _mesa_set_viewport( newCtx
->glCtx
, 0, 0,
605 driDrawPriv
->w
, driDrawPriv
->h
);
608 if (newCtx
->vb
.enabled
)
609 r200VtxfmtMakeCurrent( newCtx
->glCtx
);
611 _mesa_update_state( newCtx
->glCtx
);
612 r200ValidateState( newCtx
->glCtx
);
615 if (R200_DEBUG
& DEBUG_DRI
)
616 fprintf(stderr
, "%s ctx is null\n", __FUNCTION__
);
617 _mesa_make_current( 0, 0 );
620 if (R200_DEBUG
& DEBUG_DRI
)
621 fprintf(stderr
, "End %s\n", __FUNCTION__
);
625 /* Force the context `c' to be unbound from its buffer.
628 r200UnbindContext( __DRIcontextPrivate
*driContextPriv
)
630 r200ContextPtr rmesa
= (r200ContextPtr
) driContextPriv
->driverPrivate
;
632 if (R200_DEBUG
& DEBUG_DRI
)
633 fprintf(stderr
, "%s ctx %p\n", __FUNCTION__
, rmesa
->glCtx
);
635 r200VtxfmtUnbindContext( rmesa
->glCtx
);