1 /**************************************************************************
3 Copyright 2000, 2001 ATI Technologies Inc., Ontario, Canada, and
4 VA Linux Systems Inc., Fremont, California.
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:
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.
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.
28 **************************************************************************/
32 * Kevin E. Martin <martin@valinux.com>
33 * Gareth Hughes <gareth@valinux.com>
34 * Keith Whitwell <keith@tungstengraphics.com>
37 #include "main/glheader.h"
38 #include "main/api_arrayelt.h"
39 #include "main/context.h"
40 #include "main/simple_list.h"
41 #include "main/imports.h"
42 #include "main/matrix.h"
43 #include "main/extensions.h"
44 #include "main/framebuffer.h"
45 #include "main/state.h"
47 #include "swrast/swrast.h"
48 #include "swrast_setup/swrast_setup.h"
52 #include "tnl/t_pipeline.h"
54 #include "drivers/common/driverfuncs.h"
56 #include "radeon_context.h"
57 #include "common_cmdbuf.h"
58 #include "radeon_ioctl.h"
59 #include "radeon_state.h"
60 #include "radeon_span.h"
61 #include "radeon_tex.h"
62 #include "radeon_swtcl.h"
63 #include "radeon_tcl.h"
64 #include "radeon_maos.h"
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_EXT_blend_minmax
70 #define need_GL_EXT_fog_coord
71 #define need_GL_EXT_secondary_color
72 #include "extension_helper.h"
74 #define DRIVER_DATE "20061018"
78 #include "xmlpool.h" /* for symbolic values of enum-type options */
80 /* Return various strings for glGetString().
82 static const GLubyte
*radeonGetString( GLcontext
*ctx
, GLenum name
)
84 r100ContextPtr rmesa
= R100_CONTEXT(ctx
);
85 static char buffer
[128];
87 GLuint agp_mode
= (rmesa
->radeon
.radeonScreen
->card_type
==RADEON_CARD_PCI
) ? 0 :
88 rmesa
->radeon
.radeonScreen
->AGPMode
;
92 return (GLubyte
*)"Tungsten Graphics, Inc.";
95 offset
= driGetRendererString( buffer
, "Radeon", DRIVER_DATE
,
98 sprintf( & buffer
[ offset
], " %sTCL",
99 !(rmesa
->radeon
.TclFallback
& RADEON_TCL_FALLBACK_TCL_DISABLE
)
102 return (GLubyte
*)buffer
;
110 /* Extension strings exported by the R100 driver.
112 const struct dri_extension card_extensions
[] =
114 { "GL_ARB_multisample", GL_ARB_multisample_functions
},
115 { "GL_ARB_multitexture", NULL
},
116 { "GL_ARB_texture_border_clamp", NULL
},
117 { "GL_ARB_texture_compression", GL_ARB_texture_compression_functions
},
118 { "GL_ARB_texture_env_add", NULL
},
119 { "GL_ARB_texture_env_combine", NULL
},
120 { "GL_ARB_texture_env_crossbar", NULL
},
121 { "GL_ARB_texture_env_dot3", NULL
},
122 { "GL_ARB_texture_mirrored_repeat", NULL
},
123 { "GL_ARB_vertex_buffer_object", GL_ARB_vertex_buffer_object_functions
},
124 { "GL_EXT_blend_logic_op", NULL
},
125 { "GL_EXT_blend_subtract", GL_EXT_blend_minmax_functions
},
126 { "GL_EXT_fog_coord", GL_EXT_fog_coord_functions
},
127 { "GL_EXT_secondary_color", GL_EXT_secondary_color_functions
},
128 { "GL_EXT_stencil_wrap", NULL
},
129 { "GL_EXT_texture_edge_clamp", NULL
},
130 { "GL_EXT_texture_env_combine", NULL
},
131 { "GL_EXT_texture_env_dot3", NULL
},
132 { "GL_EXT_texture_filter_anisotropic", NULL
},
133 { "GL_EXT_texture_lod_bias", NULL
},
134 { "GL_EXT_texture_mirror_clamp", NULL
},
135 { "GL_ATI_texture_env_combine3", NULL
},
136 { "GL_ATI_texture_mirror_once", NULL
},
137 { "GL_MESA_ycbcr_texture", NULL
},
138 { "GL_NV_blend_square", NULL
},
139 { "GL_SGIS_generate_mipmap", NULL
},
143 extern const struct tnl_pipeline_stage _radeon_render_stage
;
144 extern const struct tnl_pipeline_stage _radeon_tcl_stage
;
146 static const struct tnl_pipeline_stage
*radeon_pipeline
[] = {
148 /* Try and go straight to t&l
152 /* Catch any t&l fallbacks
154 &_tnl_vertex_transform_stage
,
155 &_tnl_normal_transform_stage
,
156 &_tnl_lighting_stage
,
157 &_tnl_fog_coordinate_stage
,
159 &_tnl_texture_transform_stage
,
161 &_radeon_render_stage
,
162 &_tnl_render_stage
, /* FALLBACK: */
168 /* Initialize the driver's misc functions.
170 static void radeonInitDriverFuncs( struct dd_function_table
*functions
)
172 functions
->GetString
= radeonGetString
;
175 static const struct dri_debug_control debug_control
[] =
177 { "fall", DEBUG_FALLBACKS
},
178 { "tex", DEBUG_TEXTURE
},
179 { "ioctl", DEBUG_IOCTL
},
180 { "prim", DEBUG_PRIMS
},
181 { "vert", DEBUG_VERTS
},
182 { "state", DEBUG_STATE
},
183 { "code", DEBUG_CODEGEN
},
184 { "vfmt", DEBUG_VFMT
},
185 { "vtxf", DEBUG_VFMT
},
186 { "verb", DEBUG_VERBOSE
},
187 { "dri", DEBUG_DRI
},
188 { "dma", DEBUG_DMA
},
189 { "san", DEBUG_SANITY
},
190 { "sync", DEBUG_SYNC
},
194 static void r100_get_lock(radeonContextPtr radeon
)
196 r100ContextPtr rmesa
= (r100ContextPtr
)radeon
;
197 drm_radeon_sarea_t
*sarea
= radeon
->sarea
;
199 RADEON_STATECHANGE(rmesa
, ctx
);
200 if (rmesa
->radeon
.sarea
->tiling_enabled
) {
201 rmesa
->hw
.ctx
.cmd
[CTX_RB3D_COLORPITCH
] |=
202 RADEON_COLOR_TILE_ENABLE
;
204 rmesa
->hw
.ctx
.cmd
[CTX_RB3D_COLORPITCH
] &=
205 ~RADEON_COLOR_TILE_ENABLE
;
208 if (sarea
->ctx_owner
!= rmesa
->radeon
.dri
.hwContext
) {
210 sarea
->ctx_owner
= rmesa
->radeon
.dri
.hwContext
;
212 for (i
= 0; i
< rmesa
->radeon
.nr_heaps
; i
++) {
213 DRI_AGE_TEXTURES(rmesa
->radeon
.texture_heaps
[i
]);
218 static void r100_vtbl_flush(GLcontext
*ctx
)
220 RADEON_FIREVERTICES(R100_CONTEXT(ctx
));
223 static void r100_vtbl_set_all_dirty(GLcontext
*ctx
)
225 r100ContextPtr rmesa
= R100_CONTEXT(ctx
);
226 rmesa
->hw
.all_dirty
= GL_TRUE
;
229 static void r100_vtbl_emit_cs_header(struct radeon_cs
*cs
, radeonContextPtr rmesa
)
233 static void r100_vtbl_emit_state(radeonContextPtr rmesa
)
235 radeonEmitState((r100ContextPtr
)rmesa
);
238 static void r100_vtbl_flush_vertices(radeonContextPtr rmesa
)
240 RADEON_FIREVERTICES(((r100ContextPtr
)rmesa
));
244 static void r100_init_vtbl(radeonContextPtr radeon
)
246 radeon
->vtbl
.get_lock
= r100_get_lock
;
247 radeon
->vtbl
.update_viewport_offset
= radeonUpdateViewportOffset
;
248 radeon
->vtbl
.flush
= r100_vtbl_flush
;
249 radeon
->vtbl
.set_all_dirty
= r100_vtbl_set_all_dirty
;
250 radeon
->vtbl
.update_draw_buffer
= radeonUpdateDrawBuffer
;
251 radeon
->vtbl
.emit_cs_header
= r100_vtbl_emit_cs_header
;
252 radeon
->vtbl
.emit_state
= r100_vtbl_emit_state
;
253 radeon
->vtbl
.swtcl_flush
= r100_swtcl_flush
;
254 radeon
->vtbl
.flush_vertices
= r100_vtbl_flush_vertices
;
257 /* Create the device specific context.
260 radeonCreateContext( const __GLcontextModes
*glVisual
,
261 __DRIcontextPrivate
*driContextPriv
,
262 void *sharedContextPrivate
)
264 __DRIscreenPrivate
*sPriv
= driContextPriv
->driScreenPriv
;
265 radeonScreenPtr screen
= (radeonScreenPtr
)(sPriv
->private);
266 struct dd_function_table functions
;
267 r100ContextPtr rmesa
;
270 int tcl_mode
, fthrottle_mode
;
273 assert(driContextPriv
);
276 /* Allocate the Radeon context */
277 rmesa
= (r100ContextPtr
) CALLOC( sizeof(*rmesa
) );
281 r100_init_vtbl(&rmesa
->radeon
);
283 /* init exp fog table data */
284 radeonInitStaticFogData();
286 /* Parse configuration files.
287 * Do this here so that initialMaxAnisotropy is set before we create
288 * the default textures.
290 driParseConfigFiles (&rmesa
->radeon
.optionCache
, &screen
->optionCache
,
291 screen
->driScreen
->myNum
, "radeon");
292 rmesa
->radeon
.initialMaxAnisotropy
= driQueryOptionf(&rmesa
->radeon
.optionCache
,
293 "def_max_anisotropy");
295 if ( driQueryOptionb( &rmesa
->radeon
.optionCache
, "hyperz" ) ) {
296 if ( sPriv
->drm_version
.minor
< 13 )
297 fprintf( stderr
, "DRM version 1.%d too old to support HyperZ, "
298 "disabling.\n", sPriv
->drm_version
.minor
);
300 rmesa
->using_hyperz
= GL_TRUE
;
303 if ( sPriv
->drm_version
.minor
>= 15 )
304 rmesa
->texmicrotile
= GL_TRUE
;
306 /* Init default driver functions then plug in our Radeon-specific functions
307 * (the texture functions are especially important)
309 _mesa_init_driver_functions( &functions
);
310 radeonInitDriverFuncs( &functions
);
311 radeonInitTextureFuncs( &functions
);
313 if (!radeonInitContext(&rmesa
->radeon
, &functions
,
314 glVisual
, driContextPriv
,
315 sharedContextPrivate
)) {
320 (void) memset( rmesa
->radeon
.texture_heaps
, 0, sizeof( rmesa
->radeon
.texture_heaps
) );
321 make_empty_list( & rmesa
->radeon
.swapped
);
324 rmesa
->radeon
.nr_heaps
= screen
->numTexHeaps
;
325 for ( i
= 0 ; i
< rmesa
->radeon
.nr_heaps
; i
++ ) {
326 rmesa
->radeon
.texture_heaps
[i
] = driCreateTextureHeap( i
, rmesa
,
329 RADEON_NR_TEX_REGIONS
,
330 (drmTextureRegionPtr
)rmesa
->radeon
.sarea
->tex_list
[i
],
331 & rmesa
->radeon
.sarea
->tex_age
[i
],
332 & rmesa
->radeon
.swapped
,
333 sizeof( radeonTexObj
),
334 (destroy_texture_object_t
*) radeonDestroyTexObj
);
336 driSetTextureSwapCounterLocation( rmesa
->radeon
.texture_heaps
[i
],
337 & rmesa
->c_textureSwaps
);
340 rmesa
->radeon
.texture_depth
= driQueryOptioni (&rmesa
->radeon
.optionCache
,
342 if (rmesa
->radeon
.texture_depth
== DRI_CONF_TEXTURE_DEPTH_FB
)
343 rmesa
->radeon
.texture_depth
= ( screen
->cpp
== 4 ) ?
344 DRI_CONF_TEXTURE_DEPTH_32
: DRI_CONF_TEXTURE_DEPTH_16
;
346 rmesa
->radeon
.swtcl
.RenderIndex
= ~0;
347 rmesa
->hw
.all_dirty
= GL_TRUE
;
349 /* Set the maximum texture size small enough that we can guarentee that
350 * all texture units can bind a maximal texture and have all of them in
351 * texturable memory at once. Depending on the allow_large_textures driconf
352 * setting allow larger textures.
355 ctx
= rmesa
->radeon
.glCtx
;
356 ctx
->Const
.MaxTextureUnits
= driQueryOptioni (&rmesa
->radeon
.optionCache
,
358 ctx
->Const
.MaxTextureImageUnits
= ctx
->Const
.MaxTextureUnits
;
359 ctx
->Const
.MaxTextureCoordUnits
= ctx
->Const
.MaxTextureUnits
;
361 i
= driQueryOptioni( &rmesa
->radeon
.optionCache
, "allow_large_textures");
363 driCalculateMaxTextureLevels( rmesa
->radeon
.texture_heaps
,
364 rmesa
->radeon
.nr_heaps
,
367 11, /* max 2D texture size is 2048x2048 */
369 9, /* \todo: max cube texture size seems to be 512x512(x6) */
370 11, /* max rect texture size is 2048x2048. */
376 ctx
->Const
.MaxTextureMaxAnisotropy
= 16.0;
380 ctx
->Const
.MinPointSize
= 1.0;
381 ctx
->Const
.MinPointSizeAA
= 1.0;
382 ctx
->Const
.MaxPointSize
= 1.0;
383 ctx
->Const
.MaxPointSizeAA
= 1.0;
385 ctx
->Const
.MinLineWidth
= 1.0;
386 ctx
->Const
.MinLineWidthAA
= 1.0;
387 ctx
->Const
.MaxLineWidth
= 10.0;
388 ctx
->Const
.MaxLineWidthAA
= 10.0;
389 ctx
->Const
.LineWidthGranularity
= 0.0625;
391 /* Set maxlocksize (and hence vb size) small enough to avoid
392 * fallbacks in radeon_tcl.c. ie. guarentee that all vertices can
393 * fit in a single dma buffer for indexed rendering of quad strips,
396 ctx
->Const
.MaxArrayLockSize
=
397 MIN2( ctx
->Const
.MaxArrayLockSize
,
398 RADEON_BUFFER_SIZE
/ RADEON_MAX_TCL_VERTSIZE
);
402 /* Initialize the software rasterizer and helper modules.
404 _swrast_CreateContext( ctx
);
405 _vbo_CreateContext( ctx
);
406 _tnl_CreateContext( ctx
);
407 _swsetup_CreateContext( ctx
);
408 _ae_create_context( ctx
);
410 /* Install the customized pipeline:
412 _tnl_destroy_pipeline( ctx
);
413 _tnl_install_pipeline( ctx
, radeon_pipeline
);
415 /* Try and keep materials and vertices separate:
417 /* _tnl_isolate_materials( ctx, GL_TRUE ); */
419 /* Configure swrast and T&L to match hardware characteristics:
421 _swrast_allow_pixel_fog( ctx
, GL_FALSE
);
422 _swrast_allow_vertex_fog( ctx
, GL_TRUE
);
423 _tnl_allow_pixel_fog( ctx
, GL_FALSE
);
424 _tnl_allow_vertex_fog( ctx
, GL_TRUE
);
427 for ( i
= 0 ; i
< RADEON_MAX_TEXTURE_UNITS
; i
++ ) {
428 _math_matrix_ctr( &rmesa
->TexGenMatrix
[i
] );
429 _math_matrix_ctr( &rmesa
->tmpmat
[i
] );
430 _math_matrix_set_identity( &rmesa
->TexGenMatrix
[i
] );
431 _math_matrix_set_identity( &rmesa
->tmpmat
[i
] );
434 driInitExtensions( ctx
, card_extensions
, GL_TRUE
);
435 if (rmesa
->radeon
.radeonScreen
->drmSupportsCubeMapsR100
)
436 _mesa_enable_extension( ctx
, "GL_ARB_texture_cube_map" );
437 if (rmesa
->radeon
.glCtx
->Mesa_DXTn
) {
438 _mesa_enable_extension( ctx
, "GL_EXT_texture_compression_s3tc" );
439 _mesa_enable_extension( ctx
, "GL_S3_s3tc" );
441 else if (driQueryOptionb (&rmesa
->radeon
.optionCache
, "force_s3tc_enable")) {
442 _mesa_enable_extension( ctx
, "GL_EXT_texture_compression_s3tc" );
445 if (rmesa
->radeon
.dri
.drmMinor
>= 9)
446 _mesa_enable_extension( ctx
, "GL_NV_texture_rectangle");
448 /* XXX these should really go right after _mesa_init_driver_functions() */
449 radeonInitSpanFuncs( ctx
);
450 radeonInitIoctlFuncs( ctx
);
451 radeonInitStateFuncs( ctx
);
452 radeonInitState( rmesa
);
453 radeonInitSwtcl( ctx
);
455 _mesa_vector4f_alloc( &rmesa
->tcl
.ObjClean
, 0,
456 ctx
->Const
.MaxArrayLockSize
, 32 );
458 fthrottle_mode
= driQueryOptioni(&rmesa
->radeon
.optionCache
, "fthrottle_mode");
459 rmesa
->radeon
.iw
.irq_seq
= -1;
460 rmesa
->radeon
.irqsEmitted
= 0;
461 rmesa
->radeon
.do_irqs
= (rmesa
->radeon
.radeonScreen
->irq
!= 0 &&
462 fthrottle_mode
== DRI_CONF_FTHROTTLE_IRQS
);
464 rmesa
->radeon
.do_usleeps
= (fthrottle_mode
== DRI_CONF_FTHROTTLE_USLEEPS
);
466 (*sPriv
->systemTime
->getUST
)( & rmesa
->radeon
.swap_ust
);
470 RADEON_DEBUG
= driParseDebugString( getenv( "RADEON_DEBUG" ),
474 tcl_mode
= driQueryOptioni(&rmesa
->radeon
.optionCache
, "tcl_mode");
475 if (driQueryOptionb(&rmesa
->radeon
.optionCache
, "no_rast")) {
476 fprintf(stderr
, "disabling 3D acceleration\n");
477 FALLBACK(rmesa
, RADEON_FALLBACK_DISABLE
, 1);
478 } else if (tcl_mode
== DRI_CONF_TCL_SW
||
479 !(rmesa
->radeon
.radeonScreen
->chip_flags
& RADEON_CHIPSET_TCL
)) {
480 if (rmesa
->radeon
.radeonScreen
->chip_flags
& RADEON_CHIPSET_TCL
) {
481 rmesa
->radeon
.radeonScreen
->chip_flags
&= ~RADEON_CHIPSET_TCL
;
482 fprintf(stderr
, "Disabling HW TCL support\n");
484 TCL_FALLBACK(rmesa
->radeon
.glCtx
, RADEON_TCL_FALLBACK_TCL_DISABLE
, 1);
487 if (rmesa
->radeon
.radeonScreen
->chip_flags
& RADEON_CHIPSET_TCL
) {
488 /* _tnl_need_dlist_norm_lengths( ctx, GL_FALSE ); */
494 /* Destroy the device specific context.
496 /* Destroy the Mesa and driver specific context data.
498 void radeonDestroyContext( __DRIcontextPrivate
*driContextPriv
)
500 GET_CURRENT_CONTEXT(ctx
);
501 r100ContextPtr rmesa
= (r100ContextPtr
) driContextPriv
->driverPrivate
;
502 r100ContextPtr current
= ctx
? R100_CONTEXT(ctx
) : NULL
;
504 /* check if we're deleting the currently bound context */
505 if (rmesa
== current
) {
506 RADEON_FIREVERTICES( rmesa
);
507 _mesa_make_current(NULL
, NULL
, NULL
);
510 /* Free radeon context resources */
511 assert(rmesa
); /* should never be null */
513 GLboolean release_texture_heaps
;
516 release_texture_heaps
= (rmesa
->radeon
.glCtx
->Shared
->RefCount
== 1);
517 _swsetup_DestroyContext( rmesa
->radeon
.glCtx
);
518 _tnl_DestroyContext( rmesa
->radeon
.glCtx
);
519 _vbo_DestroyContext( rmesa
->radeon
.glCtx
);
520 _swrast_DestroyContext( rmesa
->radeon
.glCtx
);
522 radeonDestroySwtcl( rmesa
->radeon
.glCtx
);
523 radeonReleaseArrays( rmesa
->radeon
.glCtx
, ~0 );
524 if (rmesa
->radeon
.dma
.current
) {
525 radeonReleaseDmaRegion( &rmesa
->radeon
);
526 rcommonFlushCmdBuf( &rmesa
->radeon
, __FUNCTION__
);
529 _mesa_vector4f_free( &rmesa
->tcl
.ObjClean
);
531 if (rmesa
->radeon
.state
.scissor
.pClipRects
) {
532 FREE(rmesa
->radeon
.state
.scissor
.pClipRects
);
533 rmesa
->radeon
.state
.scissor
.pClipRects
= NULL
;
536 if ( release_texture_heaps
) {
537 /* This share group is about to go away, free our private
538 * texture object data.
542 for ( i
= 0 ; i
< rmesa
->radeon
.nr_heaps
; i
++ ) {
543 driDestroyTextureHeap( rmesa
->radeon
.texture_heaps
[ i
] );
544 rmesa
->radeon
.texture_heaps
[ i
] = NULL
;
547 assert( is_empty_list( & rmesa
->radeon
.swapped
) );
550 radeonCleanupContext(&rmesa
->radeon
);
557 /* Force the context `c' to be unbound from its buffer.
560 radeonUnbindContext( __DRIcontextPrivate
*driContextPriv
)
562 r100ContextPtr rmesa
= (r100ContextPtr
) driContextPriv
->driverPrivate
;
564 if (RADEON_DEBUG
& DEBUG_DRI
)
565 fprintf(stderr
, "%s ctx %p\n", __FUNCTION__
, (void *) rmesa
->radeon
.glCtx
);