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 "radeon_ioctl.h"
58 #include "radeon_state.h"
59 #include "radeon_span.h"
60 #include "radeon_tex.h"
61 #include "radeon_swtcl.h"
62 #include "radeon_tcl.h"
63 #include "radeon_maos.h"
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_EXT_blend_minmax
69 #define need_GL_EXT_fog_coord
70 #define need_GL_EXT_secondary_color
71 #include "extension_helper.h"
73 #define DRIVER_DATE "20061018"
77 #include "xmlpool.h" /* for symbolic values of enum-type options */
79 /* Return various strings for glGetString().
81 static const GLubyte
*radeonGetString( GLcontext
*ctx
, GLenum name
)
83 r100ContextPtr rmesa
= R100_CONTEXT(ctx
);
84 static char buffer
[128];
86 GLuint agp_mode
= (rmesa
->radeon
.radeonScreen
->card_type
==RADEON_CARD_PCI
) ? 0 :
87 rmesa
->radeon
.radeonScreen
->AGPMode
;
91 return (GLubyte
*)"Tungsten Graphics, Inc.";
94 offset
= driGetRendererString( buffer
, "Radeon", DRIVER_DATE
,
97 sprintf( & buffer
[ offset
], " %sTCL",
98 !(rmesa
->radeon
.TclFallback
& RADEON_TCL_FALLBACK_TCL_DISABLE
)
101 return (GLubyte
*)buffer
;
109 /* Extension strings exported by the R100 driver.
111 const struct dri_extension card_extensions
[] =
113 { "GL_ARB_multisample", GL_ARB_multisample_functions
},
114 { "GL_ARB_multitexture", NULL
},
115 { "GL_ARB_texture_border_clamp", NULL
},
116 { "GL_ARB_texture_compression", GL_ARB_texture_compression_functions
},
117 { "GL_ARB_texture_env_add", NULL
},
118 { "GL_ARB_texture_env_combine", NULL
},
119 { "GL_ARB_texture_env_crossbar", NULL
},
120 { "GL_ARB_texture_env_dot3", NULL
},
121 { "GL_ARB_texture_mirrored_repeat", NULL
},
122 { "GL_ARB_vertex_buffer_object", GL_ARB_vertex_buffer_object_functions
},
123 { "GL_EXT_blend_logic_op", NULL
},
124 { "GL_EXT_blend_subtract", GL_EXT_blend_minmax_functions
},
125 { "GL_EXT_fog_coord", GL_EXT_fog_coord_functions
},
126 { "GL_EXT_secondary_color", GL_EXT_secondary_color_functions
},
127 { "GL_EXT_stencil_wrap", NULL
},
128 { "GL_EXT_texture_edge_clamp", NULL
},
129 { "GL_EXT_texture_env_combine", NULL
},
130 { "GL_EXT_texture_env_dot3", NULL
},
131 { "GL_EXT_texture_filter_anisotropic", NULL
},
132 { "GL_EXT_texture_lod_bias", NULL
},
133 { "GL_EXT_texture_mirror_clamp", NULL
},
134 { "GL_ATI_texture_env_combine3", NULL
},
135 { "GL_ATI_texture_mirror_once", NULL
},
136 { "GL_MESA_ycbcr_texture", NULL
},
137 { "GL_NV_blend_square", NULL
},
138 { "GL_SGIS_generate_mipmap", NULL
},
142 extern const struct tnl_pipeline_stage _radeon_render_stage
;
143 extern const struct tnl_pipeline_stage _radeon_tcl_stage
;
145 static const struct tnl_pipeline_stage
*radeon_pipeline
[] = {
147 /* Try and go straight to t&l
151 /* Catch any t&l fallbacks
153 &_tnl_vertex_transform_stage
,
154 &_tnl_normal_transform_stage
,
155 &_tnl_lighting_stage
,
156 &_tnl_fog_coordinate_stage
,
158 &_tnl_texture_transform_stage
,
160 &_radeon_render_stage
,
161 &_tnl_render_stage
, /* FALLBACK: */
167 /* Initialize the driver's misc functions.
169 static void radeonInitDriverFuncs( struct dd_function_table
*functions
)
171 functions
->GetString
= radeonGetString
;
174 static const struct dri_debug_control debug_control
[] =
176 { "fall", DEBUG_FALLBACKS
},
177 { "tex", DEBUG_TEXTURE
},
178 { "ioctl", DEBUG_IOCTL
},
179 { "prim", DEBUG_PRIMS
},
180 { "vert", DEBUG_VERTS
},
181 { "state", DEBUG_STATE
},
182 { "code", DEBUG_CODEGEN
},
183 { "vfmt", DEBUG_VFMT
},
184 { "vtxf", DEBUG_VFMT
},
185 { "verb", DEBUG_VERBOSE
},
186 { "dri", DEBUG_DRI
},
187 { "dma", DEBUG_DMA
},
188 { "san", DEBUG_SANITY
},
189 { "sync", DEBUG_SYNC
},
193 static void r100_get_lock(radeonContextPtr radeon
)
195 r100ContextPtr rmesa
= (r100ContextPtr
)radeon
;
196 drm_radeon_sarea_t
*sarea
= radeon
->sarea
;
198 RADEON_STATECHANGE(rmesa
, ctx
);
199 if (rmesa
->radeon
.sarea
->tiling_enabled
) {
200 rmesa
->hw
.ctx
.cmd
[CTX_RB3D_COLORPITCH
] |=
201 RADEON_COLOR_TILE_ENABLE
;
203 rmesa
->hw
.ctx
.cmd
[CTX_RB3D_COLORPITCH
] &=
204 ~RADEON_COLOR_TILE_ENABLE
;
207 if (sarea
->ctx_owner
!= rmesa
->radeon
.dri
.hwContext
) {
209 sarea
->ctx_owner
= rmesa
->radeon
.dri
.hwContext
;
211 for (i
= 0; i
< rmesa
->radeon
.nr_heaps
; i
++) {
212 DRI_AGE_TEXTURES(rmesa
->radeon
.texture_heaps
[i
]);
217 static void r100_vtbl_flush(GLcontext
*ctx
)
219 RADEON_FIREVERTICES(R100_CONTEXT(ctx
));
222 static void r100_vtbl_set_all_dirty(GLcontext
*ctx
)
224 r100ContextPtr rmesa
= R100_CONTEXT(ctx
);
225 rmesa
->hw
.all_dirty
= GL_TRUE
;
228 static void r100_vtbl_emit_cs_header(struct radeon_cs
*cs
, radeonContextPtr rmesa
)
232 static void r100_vtbl_emit_state(radeonContextPtr rmesa
)
234 radeonEmitState((r100ContextPtr
)rmesa
);
237 static void r100_init_vtbl(radeonContextPtr radeon
)
239 radeon
->vtbl
.get_lock
= r100_get_lock
;
240 radeon
->vtbl
.update_viewport_offset
= radeonUpdateViewportOffset
;
241 radeon
->vtbl
.flush
= r100_vtbl_flush
;
242 radeon
->vtbl
.set_all_dirty
= r100_vtbl_set_all_dirty
;
243 radeon
->vtbl
.update_draw_buffer
= radeonUpdateDrawBuffer
;
244 radeon
->vtbl
.emit_cs_header
= r100_vtbl_emit_cs_header
;
245 radeon
->vtbl
.emit_state
= r100_vtbl_emit_state
;
248 /* Create the device specific context.
251 radeonCreateContext( const __GLcontextModes
*glVisual
,
252 __DRIcontextPrivate
*driContextPriv
,
253 void *sharedContextPrivate
)
255 __DRIscreenPrivate
*sPriv
= driContextPriv
->driScreenPriv
;
256 radeonScreenPtr screen
= (radeonScreenPtr
)(sPriv
->private);
257 struct dd_function_table functions
;
258 r100ContextPtr rmesa
;
259 GLcontext
*ctx
, *shareCtx
;
261 int tcl_mode
, fthrottle_mode
;
264 assert(driContextPriv
);
267 /* Allocate the Radeon context */
268 rmesa
= (r100ContextPtr
) CALLOC( sizeof(*rmesa
) );
272 r100_init_vtbl(&rmesa
->radeon
);
274 /* init exp fog table data */
275 radeonInitStaticFogData();
277 /* Parse configuration files.
278 * Do this here so that initialMaxAnisotropy is set before we create
279 * the default textures.
281 driParseConfigFiles (&rmesa
->radeon
.optionCache
, &screen
->optionCache
,
282 screen
->driScreen
->myNum
, "radeon");
283 rmesa
->radeon
.initialMaxAnisotropy
= driQueryOptionf(&rmesa
->radeon
.optionCache
,
284 "def_max_anisotropy");
286 if ( driQueryOptionb( &rmesa
->radeon
.optionCache
, "hyperz" ) ) {
287 if ( sPriv
->drm_version
.minor
< 13 )
288 fprintf( stderr
, "DRM version 1.%d too old to support HyperZ, "
289 "disabling.\n", sPriv
->drm_version
.minor
);
291 rmesa
->using_hyperz
= GL_TRUE
;
294 if ( sPriv
->drm_version
.minor
>= 15 )
295 rmesa
->texmicrotile
= GL_TRUE
;
297 /* Init default driver functions then plug in our Radeon-specific functions
298 * (the texture functions are especially important)
300 _mesa_init_driver_functions( &functions
);
301 radeonInitDriverFuncs( &functions
);
302 radeonInitTextureFuncs( &functions
);
304 if (!radeonInitContext(&rmesa
->radeon
, &functions
,
305 glVisual
, driContextPriv
,
306 sharedContextPrivate
)) {
311 (void) memset( rmesa
->radeon
.texture_heaps
, 0, sizeof( rmesa
->radeon
.texture_heaps
) );
312 make_empty_list( & rmesa
->radeon
.swapped
);
314 rmesa
->radeon
.nr_heaps
= screen
->numTexHeaps
;
315 for ( i
= 0 ; i
< rmesa
->radeon
.nr_heaps
; i
++ ) {
316 rmesa
->radeon
.texture_heaps
[i
] = driCreateTextureHeap( i
, rmesa
,
319 RADEON_NR_TEX_REGIONS
,
320 (drmTextureRegionPtr
)rmesa
->radeon
.sarea
->tex_list
[i
],
321 & rmesa
->radeon
.sarea
->tex_age
[i
],
322 & rmesa
->radeon
.swapped
,
323 sizeof( radeonTexObj
),
324 (destroy_texture_object_t
*) radeonDestroyTexObj
);
326 driSetTextureSwapCounterLocation( rmesa
->radeon
.texture_heaps
[i
],
327 & rmesa
->c_textureSwaps
);
329 rmesa
->radeon
.texture_depth
= driQueryOptioni (&rmesa
->radeon
.optionCache
,
331 if (rmesa
->radeon
.texture_depth
== DRI_CONF_TEXTURE_DEPTH_FB
)
332 rmesa
->radeon
.texture_depth
= ( screen
->cpp
== 4 ) ?
333 DRI_CONF_TEXTURE_DEPTH_32
: DRI_CONF_TEXTURE_DEPTH_16
;
335 rmesa
->swtcl
.RenderIndex
= ~0;
336 rmesa
->hw
.all_dirty
= GL_TRUE
;
338 /* Set the maximum texture size small enough that we can guarentee that
339 * all texture units can bind a maximal texture and have all of them in
340 * texturable memory at once. Depending on the allow_large_textures driconf
341 * setting allow larger textures.
344 ctx
= rmesa
->radeon
.glCtx
;
345 ctx
->Const
.MaxTextureUnits
= driQueryOptioni (&rmesa
->radeon
.optionCache
,
347 ctx
->Const
.MaxTextureImageUnits
= ctx
->Const
.MaxTextureUnits
;
348 ctx
->Const
.MaxTextureCoordUnits
= ctx
->Const
.MaxTextureUnits
;
350 i
= driQueryOptioni( &rmesa
->radeon
.optionCache
, "allow_large_textures");
352 driCalculateMaxTextureLevels( rmesa
->radeon
.texture_heaps
,
353 rmesa
->radeon
.nr_heaps
,
356 11, /* max 2D texture size is 2048x2048 */
358 9, /* \todo: max cube texture size seems to be 512x512(x6) */
359 11, /* max rect texture size is 2048x2048. */
365 ctx
->Const
.MaxTextureMaxAnisotropy
= 16.0;
369 ctx
->Const
.MinPointSize
= 1.0;
370 ctx
->Const
.MinPointSizeAA
= 1.0;
371 ctx
->Const
.MaxPointSize
= 1.0;
372 ctx
->Const
.MaxPointSizeAA
= 1.0;
374 ctx
->Const
.MinLineWidth
= 1.0;
375 ctx
->Const
.MinLineWidthAA
= 1.0;
376 ctx
->Const
.MaxLineWidth
= 10.0;
377 ctx
->Const
.MaxLineWidthAA
= 10.0;
378 ctx
->Const
.LineWidthGranularity
= 0.0625;
380 /* Set maxlocksize (and hence vb size) small enough to avoid
381 * fallbacks in radeon_tcl.c. ie. guarentee that all vertices can
382 * fit in a single dma buffer for indexed rendering of quad strips,
385 ctx
->Const
.MaxArrayLockSize
=
386 MIN2( ctx
->Const
.MaxArrayLockSize
,
387 RADEON_BUFFER_SIZE
/ RADEON_MAX_TCL_VERTSIZE
);
391 /* Initialize the software rasterizer and helper modules.
393 _swrast_CreateContext( ctx
);
394 _vbo_CreateContext( ctx
);
395 _tnl_CreateContext( ctx
);
396 _swsetup_CreateContext( ctx
);
397 _ae_create_context( ctx
);
399 /* Install the customized pipeline:
401 _tnl_destroy_pipeline( ctx
);
402 _tnl_install_pipeline( ctx
, radeon_pipeline
);
404 /* Try and keep materials and vertices separate:
406 /* _tnl_isolate_materials( ctx, GL_TRUE ); */
408 /* Configure swrast and T&L to match hardware characteristics:
410 _swrast_allow_pixel_fog( ctx
, GL_FALSE
);
411 _swrast_allow_vertex_fog( ctx
, GL_TRUE
);
412 _tnl_allow_pixel_fog( ctx
, GL_FALSE
);
413 _tnl_allow_vertex_fog( ctx
, GL_TRUE
);
416 for ( i
= 0 ; i
< RADEON_MAX_TEXTURE_UNITS
; i
++ ) {
417 _math_matrix_ctr( &rmesa
->TexGenMatrix
[i
] );
418 _math_matrix_ctr( &rmesa
->tmpmat
[i
] );
419 _math_matrix_set_identity( &rmesa
->TexGenMatrix
[i
] );
420 _math_matrix_set_identity( &rmesa
->tmpmat
[i
] );
423 driInitExtensions( ctx
, card_extensions
, GL_TRUE
);
424 if (rmesa
->radeon
.radeonScreen
->drmSupportsCubeMapsR100
)
425 _mesa_enable_extension( ctx
, "GL_ARB_texture_cube_map" );
426 if (rmesa
->radeon
.glCtx
->Mesa_DXTn
) {
427 _mesa_enable_extension( ctx
, "GL_EXT_texture_compression_s3tc" );
428 _mesa_enable_extension( ctx
, "GL_S3_s3tc" );
430 else if (driQueryOptionb (&rmesa
->radeon
.optionCache
, "force_s3tc_enable")) {
431 _mesa_enable_extension( ctx
, "GL_EXT_texture_compression_s3tc" );
434 if (rmesa
->radeon
.dri
.drmMinor
>= 9)
435 _mesa_enable_extension( ctx
, "GL_NV_texture_rectangle");
437 /* XXX these should really go right after _mesa_init_driver_functions() */
438 radeonInitIoctlFuncs( ctx
);
439 radeonInitStateFuncs( ctx
);
440 radeonInitSpanFuncs( ctx
);
441 radeonInitState( rmesa
);
442 radeonInitSwtcl( ctx
);
444 _mesa_vector4f_alloc( &rmesa
->tcl
.ObjClean
, 0,
445 ctx
->Const
.MaxArrayLockSize
, 32 );
447 fthrottle_mode
= driQueryOptioni(&rmesa
->radeon
.optionCache
, "fthrottle_mode");
448 rmesa
->radeon
.iw
.irq_seq
= -1;
449 rmesa
->radeon
.irqsEmitted
= 0;
450 rmesa
->radeon
.do_irqs
= (rmesa
->radeon
.radeonScreen
->irq
!= 0 &&
451 fthrottle_mode
== DRI_CONF_FTHROTTLE_IRQS
);
453 rmesa
->radeon
.do_usleeps
= (fthrottle_mode
== DRI_CONF_FTHROTTLE_USLEEPS
);
455 (*sPriv
->systemTime
->getUST
)( & rmesa
->radeon
.swap_ust
);
459 RADEON_DEBUG
= driParseDebugString( getenv( "RADEON_DEBUG" ),
463 tcl_mode
= driQueryOptioni(&rmesa
->radeon
.optionCache
, "tcl_mode");
464 if (driQueryOptionb(&rmesa
->radeon
.optionCache
, "no_rast")) {
465 fprintf(stderr
, "disabling 3D acceleration\n");
466 FALLBACK(rmesa
, RADEON_FALLBACK_DISABLE
, 1);
467 } else if (tcl_mode
== DRI_CONF_TCL_SW
||
468 !(rmesa
->radeon
.radeonScreen
->chip_flags
& RADEON_CHIPSET_TCL
)) {
469 if (rmesa
->radeon
.radeonScreen
->chip_flags
& RADEON_CHIPSET_TCL
) {
470 rmesa
->radeon
.radeonScreen
->chip_flags
&= ~RADEON_CHIPSET_TCL
;
471 fprintf(stderr
, "Disabling HW TCL support\n");
473 TCL_FALLBACK(rmesa
->radeon
.glCtx
, RADEON_TCL_FALLBACK_TCL_DISABLE
, 1);
476 if (rmesa
->radeon
.radeonScreen
->chip_flags
& RADEON_CHIPSET_TCL
) {
477 /* _tnl_need_dlist_norm_lengths( ctx, GL_FALSE ); */
483 /* Destroy the device specific context.
485 /* Destroy the Mesa and driver specific context data.
487 void radeonDestroyContext( __DRIcontextPrivate
*driContextPriv
)
489 GET_CURRENT_CONTEXT(ctx
);
490 r100ContextPtr rmesa
= (r100ContextPtr
) driContextPriv
->driverPrivate
;
491 r100ContextPtr current
= ctx
? R100_CONTEXT(ctx
) : NULL
;
493 /* check if we're deleting the currently bound context */
494 if (rmesa
== current
) {
495 RADEON_FIREVERTICES( rmesa
);
496 _mesa_make_current(NULL
, NULL
, NULL
);
499 /* Free radeon context resources */
500 assert(rmesa
); /* should never be null */
502 GLboolean release_texture_heaps
;
505 release_texture_heaps
= (rmesa
->radeon
.glCtx
->Shared
->RefCount
== 1);
506 _swsetup_DestroyContext( rmesa
->radeon
.glCtx
);
507 _tnl_DestroyContext( rmesa
->radeon
.glCtx
);
508 _vbo_DestroyContext( rmesa
->radeon
.glCtx
);
509 _swrast_DestroyContext( rmesa
->radeon
.glCtx
);
511 radeonDestroySwtcl( rmesa
->radeon
.glCtx
);
512 radeonReleaseArrays( rmesa
->radeon
.glCtx
, ~0 );
513 if (rmesa
->radeon
.dma
.current
) {
514 radeonReleaseDmaRegion( &rmesa
->radeon
);
515 radeonFlushCmdBuf( &rmesa
->radeon
, __FUNCTION__
);
518 _mesa_vector4f_free( &rmesa
->tcl
.ObjClean
);
520 if (rmesa
->radeon
.state
.scissor
.pClipRects
) {
521 FREE(rmesa
->radeon
.state
.scissor
.pClipRects
);
522 rmesa
->radeon
.state
.scissor
.pClipRects
= NULL
;
525 if ( release_texture_heaps
) {
526 /* This share group is about to go away, free our private
527 * texture object data.
531 for ( i
= 0 ; i
< rmesa
->radeon
.nr_heaps
; i
++ ) {
532 driDestroyTextureHeap( rmesa
->radeon
.texture_heaps
[ i
] );
533 rmesa
->radeon
.texture_heaps
[ i
] = NULL
;
536 assert( is_empty_list( & rmesa
->radeon
.swapped
) );
539 radeonCleanupContext(&rmesa
->radeon
);
546 /* Force the context `c' to be unbound from its buffer.
549 radeonUnbindContext( __DRIcontextPrivate
*driContextPriv
)
551 r100ContextPtr rmesa
= (r100ContextPtr
) driContextPriv
->driverPrivate
;
553 if (RADEON_DEBUG
& DEBUG_DRI
)
554 fprintf(stderr
, "%s ctx %p\n", __FUNCTION__
, (void *) rmesa
->radeon
.glCtx
);