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_PREFERRED_BPT(0,"0,16,32")
86 DRI_CONF_SECTION_DEBUG
87 DRI_CONF_NO_RAST(false)
90 const GLuint __driNConfigOptions
= 5;
92 /* Return the width and height of the given buffer.
94 static void r200GetBufferSize( GLframebuffer
*buffer
,
95 GLuint
*width
, GLuint
*height
)
97 GET_CURRENT_CONTEXT(ctx
);
98 r200ContextPtr rmesa
= R200_CONTEXT(ctx
);
100 LOCK_HARDWARE( rmesa
);
101 *width
= rmesa
->dri
.drawable
->w
;
102 *height
= rmesa
->dri
.drawable
->h
;
103 UNLOCK_HARDWARE( rmesa
);
106 /* Return various strings for glGetString().
108 static const GLubyte
*r200GetString( GLcontext
*ctx
, GLenum name
)
110 r200ContextPtr rmesa
= R200_CONTEXT(ctx
);
111 static char buffer
[128];
113 GLuint agp_mode
= rmesa
->r200Screen
->IsPCI
? 0 :
114 rmesa
->r200Screen
->AGPMode
;
118 return (GLubyte
*)"Tungsten Graphics, Inc.";
121 offset
= driGetRendererString( buffer
, "R200", DRIVER_DATE
,
124 sprintf( & buffer
[ offset
], " %sTCL",
125 !(rmesa
->TclFallback
& R200_TCL_FALLBACK_TCL_DISABLE
)
128 return (GLubyte
*)buffer
;
136 /* Extension strings exported by the R200 driver.
138 static const char * const card_extensions
[] =
140 "GL_ARB_multisample",
141 "GL_ARB_multitexture",
142 "GL_ARB_texture_border_clamp",
143 "GL_ARB_texture_compression",
144 "GL_ARB_texture_env_add",
145 "GL_ARB_texture_env_combine",
146 "GL_ARB_texture_env_dot3",
147 "GL_ARB_texture_mirrored_repeat",
148 "GL_EXT_blend_logic_op",
149 "GL_EXT_blend_minmax",
150 "GL_EXT_blend_subtract",
151 "GL_EXT_secondary_color",
152 "GL_EXT_stencil_wrap",
153 "GL_EXT_texture_edge_clamp",
154 "GL_EXT_texture_env_add",
155 "GL_EXT_texture_env_combine",
156 "GL_EXT_texture_env_dot3",
157 "GL_EXT_texture_filter_anisotropic",
158 "GL_EXT_texture_lod_bias",
159 "GL_ATI_texture_env_combine3",
160 "GL_ATI_texture_mirror_once",
161 "GL_IBM_texture_mirrored_repeat",
162 "GL_MESA_pack_invert",
163 "GL_MESA_ycbcr_texture",
164 "GL_NV_blend_square",
165 "GL_NV_texture_rectangle",
166 "GL_SGIS_generate_mipmap",
167 "GL_SGIS_texture_border_clamp",
168 "GL_SGIS_texture_edge_clamp",
172 extern const struct gl_pipeline_stage _r200_render_stage
;
173 extern const struct gl_pipeline_stage _r200_tcl_stage
;
175 static const struct gl_pipeline_stage
*r200_pipeline
[] = {
177 /* Try and go straight to t&l
181 /* Catch any t&l fallbacks
183 &_tnl_vertex_transform_stage
,
184 &_tnl_normal_transform_stage
,
185 &_tnl_lighting_stage
,
186 &_tnl_fog_coordinate_stage
,
188 &_tnl_texture_transform_stage
,
190 /* Try again to go to tcl?
191 * - no good for asymmetric-twoside (do with multipass)
192 * - no good for asymmetric-unfilled (do with multipass)
193 * - good for material
195 * - need to manipulate a bit of state
197 * - worth it/not worth it?
200 /* Else do them here.
202 /* &_r200_render_stage, */ /* FIXME: bugs with ut2003 */
203 &_tnl_render_stage
, /* FALLBACK: */
209 /* Initialize the driver's misc functions.
211 static void r200InitDriverFuncs( GLcontext
*ctx
)
213 ctx
->Driver
.GetBufferSize
= r200GetBufferSize
;
214 ctx
->Driver
.ResizeBuffers
= _swrast_alloc_buffers
;
215 ctx
->Driver
.GetString
= r200GetString
;
217 ctx
->Driver
.Error
= NULL
;
218 ctx
->Driver
.DrawPixels
= NULL
;
219 ctx
->Driver
.Bitmap
= NULL
;
222 static const struct dri_debug_control debug_control
[] =
224 { "fall", DEBUG_FALLBACKS
},
225 { "tex", DEBUG_TEXTURE
},
226 { "ioctl", DEBUG_IOCTL
},
227 { "prim", DEBUG_PRIMS
},
228 { "vert", DEBUG_VERTS
},
229 { "state", DEBUG_STATE
},
230 { "code", DEBUG_CODEGEN
},
231 { "vfmt", DEBUG_VFMT
},
232 { "vtxf", DEBUG_VFMT
},
233 { "verb", DEBUG_VERBOSE
},
234 { "dri", DEBUG_DRI
},
235 { "dma", DEBUG_DMA
},
236 { "san", DEBUG_SANITY
},
237 { "sync", DEBUG_SYNC
},
238 { "pix", DEBUG_PIXEL
},
239 { "mem", DEBUG_MEMORY
},
245 get_ust_nop( uint64_t * ust
)
252 /* Create the device specific context.
254 GLboolean
r200CreateContext( const __GLcontextModes
*glVisual
,
255 __DRIcontextPrivate
*driContextPriv
,
256 void *sharedContextPrivate
)
258 __DRIscreenPrivate
*sPriv
= driContextPriv
->driScreenPriv
;
259 r200ScreenPtr screen
= (r200ScreenPtr
)(sPriv
->private);
260 r200ContextPtr rmesa
;
261 GLcontext
*ctx
, *shareCtx
;
263 int tcl_mode
, fthrottle_mode
, preferred_bpt
;
266 assert(driContextPriv
);
269 /* Allocate the R200 context */
270 rmesa
= (r200ContextPtr
) CALLOC( sizeof(*rmesa
) );
274 /* Allocate the Mesa context */
275 if (sharedContextPrivate
)
276 shareCtx
= ((r200ContextPtr
) sharedContextPrivate
)->glCtx
;
279 rmesa
->glCtx
= _mesa_create_context(glVisual
, shareCtx
, (void *) rmesa
, GL_TRUE
);
284 driContextPriv
->driverPrivate
= rmesa
;
286 /* Init r200 context data */
287 rmesa
->dri
.context
= driContextPriv
;
288 rmesa
->dri
.screen
= sPriv
;
289 rmesa
->dri
.drawable
= NULL
; /* Set by XMesaMakeCurrent */
290 rmesa
->dri
.hwContext
= driContextPriv
->hHWContext
;
291 rmesa
->dri
.hwLock
= &sPriv
->pSAREA
->lock
;
292 rmesa
->dri
.fd
= sPriv
->fd
;
293 rmesa
->dri
.drmMinor
= sPriv
->drmMinor
;
295 /* Parse configuration files */
296 driParseConfigFiles (&rmesa
->optionCache
, &screen
->optionCache
,
297 screen
->driScreen
->myNum
, "r200");
298 rmesa
->r200Screen
= screen
;
299 rmesa
->sarea
= (RADEONSAREAPrivPtr
)((GLubyte
*)sPriv
->pSAREA
+
300 screen
->sarea_priv_offset
);
303 rmesa
->dma
.buf0_address
= rmesa
->r200Screen
->buffers
->list
[0].address
;
305 (void) memset( rmesa
->texture_heaps
, 0, sizeof( rmesa
->texture_heaps
) );
306 make_empty_list( & rmesa
->swapped
);
308 rmesa
->nr_heaps
= 1 /* screen->numTexHeaps */ ;
309 for ( i
= 0 ; i
< rmesa
->nr_heaps
; i
++ ) {
310 rmesa
->texture_heaps
[i
] = driCreateTextureHeap( i
, rmesa
,
313 RADEON_NR_TEX_REGIONS
,
314 rmesa
->sarea
->texList
[i
],
315 & rmesa
->sarea
->texAge
[i
],
317 sizeof( r200TexObj
),
318 (destroy_texture_object_t
*) r200DestroyTexObj
);
320 preferred_bpt
= driQueryOptioni (&rmesa
->optionCache
, "preferred_bpt");
321 rmesa
->default32BitTextures
=
322 ( ( preferred_bpt
== 0 && screen
->cpp
== 4 ) || preferred_bpt
== 32 );
324 rmesa
->swtcl
.RenderIndex
= ~0;
325 rmesa
->lost_context
= 1;
327 /* Set the maximum texture size small enough that we can guarentee that
328 * all texture units can bind a maximal texture and have them both in
329 * texturable memory at once.
333 ctx
->Const
.MaxTextureUnits
= 2;
335 driCalculateMaxTextureLevels( rmesa
->texture_heaps
,
339 11, /* max 2D texture size is 2048x2048 */
340 #if ENABLE_HW_3D_TEXTURE
341 8, /* max 3D texture size is 256^3 */
343 0, /* 3D textures unsupported */
345 11, /* max cube texture size is 2048x2048 */
346 11, /* max texture rectangle size is 2048x2048 */
350 ctx
->Const
.MaxTextureMaxAnisotropy
= 16.0;
354 ctx
->Const
.MinPointSize
= 1.0;
355 ctx
->Const
.MinPointSizeAA
= 1.0;
356 ctx
->Const
.MaxPointSize
= 1.0;
357 ctx
->Const
.MaxPointSizeAA
= 1.0;
359 ctx
->Const
.MinLineWidth
= 1.0;
360 ctx
->Const
.MinLineWidthAA
= 1.0;
361 ctx
->Const
.MaxLineWidth
= 10.0;
362 ctx
->Const
.MaxLineWidthAA
= 10.0;
363 ctx
->Const
.LineWidthGranularity
= 0.0625;
365 /* Initialize the software rasterizer and helper modules.
367 _swrast_CreateContext( ctx
);
368 _ac_CreateContext( ctx
);
369 _tnl_CreateContext( ctx
);
370 _swsetup_CreateContext( ctx
);
371 _ae_create_context( ctx
);
373 /* Install the customized pipeline:
375 _tnl_destroy_pipeline( ctx
);
376 _tnl_install_pipeline( ctx
, r200_pipeline
);
377 ctx
->Driver
.FlushVertices
= r200FlushVertices
;
379 /* Try and keep materials and vertices separate:
381 _tnl_isolate_materials( ctx
, GL_TRUE
);
384 /* Configure swrast to match hardware characteristics:
386 _swrast_allow_pixel_fog( ctx
, GL_FALSE
);
387 _swrast_allow_vertex_fog( ctx
, GL_TRUE
);
390 _math_matrix_ctr( &rmesa
->TexGenMatrix
[0] );
391 _math_matrix_ctr( &rmesa
->TexGenMatrix
[1] );
392 _math_matrix_ctr( &rmesa
->tmpmat
);
393 _math_matrix_set_identity( &rmesa
->TexGenMatrix
[0] );
394 _math_matrix_set_identity( &rmesa
->TexGenMatrix
[1] );
395 _math_matrix_set_identity( &rmesa
->tmpmat
);
397 driInitExtensions( ctx
, card_extensions
, GL_TRUE
);
398 if (rmesa
->r200Screen
->drmSupportsCubeMaps
)
399 _mesa_enable_extension( ctx
, "GL_ARB_texture_cube_map" );
401 r200InitDriverFuncs( ctx
);
402 r200InitIoctlFuncs( ctx
);
403 r200InitStateFuncs( ctx
);
404 r200InitSpanFuncs( ctx
);
405 r200InitPixelFuncs( ctx
);
406 r200InitTextureFuncs( ctx
);
407 r200InitState( rmesa
);
408 r200InitSwtcl( ctx
);
410 fthrottle_mode
= driQueryOptioni(&rmesa
->optionCache
, "fthrottle_mode");
411 rmesa
->iw
.irq_seq
= -1;
412 rmesa
->irqsEmitted
= 0;
413 rmesa
->do_irqs
= (rmesa
->dri
.drmMinor
>= 6 &&
414 fthrottle_mode
== DRI_CONF_FTHROTTLE_IRQS
&&
415 rmesa
->r200Screen
->irq
);
417 rmesa
->do_usleeps
= (fthrottle_mode
== DRI_CONF_FTHROTTLE_USLEEPS
);
421 "IRQ's not enabled, falling back to %s: %d %d %d\n",
422 rmesa
->do_usleeps
? "usleeps" : "busy waits",
425 rmesa
->r200Screen
->irq
);
427 rmesa
->vblank_flags
= (rmesa
->r200Screen
->irq
!= 0)
428 ? driGetDefaultVBlankFlags(&rmesa
->optionCache
) : VBLANK_FLAG_NO_IRQ
;
430 rmesa
->prefer_gart_client_texturing
=
431 (getenv("R200_GART_CLIENT_TEXTURES") != 0);
433 rmesa
->get_ust
= (PFNGLXGETUSTPROC
) glXGetProcAddress( (const GLubyte
*) "__glXGetUST" );
434 if ( rmesa
->get_ust
== NULL
) {
435 rmesa
->get_ust
= get_ust_nop
;
438 rmesa
->get_ust
= get_ust_nop
;
440 (*rmesa
->get_ust
)( & rmesa
->swap_ust
);
444 R200_DEBUG
= driParseDebugString( getenv( "R200_DEBUG" ),
446 R200_DEBUG
|= driParseDebugString( getenv( "RADEON_DEBUG" ),
450 tcl_mode
= driQueryOptioni(&rmesa
->optionCache
, "tcl_mode");
451 if (driQueryOptionb(&rmesa
->optionCache
, "no_rast")) {
452 fprintf(stderr
, "disabling 3D acceleration\n");
453 FALLBACK(rmesa
, R200_FALLBACK_DISABLE
, 1);
455 else if (tcl_mode
== DRI_CONF_TCL_SW
) {
456 fprintf(stderr
, "disabling TCL support\n");
457 TCL_FALLBACK(rmesa
->glCtx
, R200_TCL_FALLBACK_TCL_DISABLE
, 1);
460 if (tcl_mode
>= DRI_CONF_TCL_VTXFMT
) {
461 r200VtxfmtInit( ctx
, tcl_mode
>= DRI_CONF_TCL_CODEGEN
);
463 _tnl_need_dlist_norm_lengths( ctx
, GL_FALSE
);
469 /* Destroy the device specific context.
471 /* Destroy the Mesa and driver specific context data.
473 void r200DestroyContext( __DRIcontextPrivate
*driContextPriv
)
475 GET_CURRENT_CONTEXT(ctx
);
476 r200ContextPtr rmesa
= (r200ContextPtr
) driContextPriv
->driverPrivate
;
477 r200ContextPtr current
= ctx
? R200_CONTEXT(ctx
) : NULL
;
479 /* check if we're deleting the currently bound context */
480 if (rmesa
== current
) {
481 R200_FIREVERTICES( rmesa
);
482 _mesa_make_current2(NULL
, NULL
, NULL
);
485 /* Free r200 context resources */
486 assert(rmesa
); /* should never be null */
488 GLboolean release_texture_heaps
;
491 release_texture_heaps
= (rmesa
->glCtx
->Shared
->RefCount
== 1);
492 _swsetup_DestroyContext( rmesa
->glCtx
);
493 _tnl_DestroyContext( rmesa
->glCtx
);
494 _ac_DestroyContext( rmesa
->glCtx
);
495 _swrast_DestroyContext( rmesa
->glCtx
);
497 r200DestroySwtcl( rmesa
->glCtx
);
498 r200ReleaseArrays( rmesa
->glCtx
, ~0 );
500 if (rmesa
->dma
.current
.buf
) {
501 r200ReleaseDmaRegion( rmesa
, &rmesa
->dma
.current
, __FUNCTION__
);
502 r200FlushCmdBuf( rmesa
, __FUNCTION__
);
505 if (!(rmesa
->TclFallback
& R200_TCL_FALLBACK_TCL_DISABLE
)) {
506 int tcl_mode
= driQueryOptioni(&rmesa
->optionCache
, "tcl_mode");
507 if (tcl_mode
>= DRI_CONF_TCL_VTXFMT
)
508 r200VtxfmtDestroy( rmesa
->glCtx
);
511 /* free the Mesa context */
512 rmesa
->glCtx
->DriverCtx
= NULL
;
513 _mesa_destroy_context( rmesa
->glCtx
);
515 if (rmesa
->state
.scissor
.pClipRects
) {
516 FREE(rmesa
->state
.scissor
.pClipRects
);
517 rmesa
->state
.scissor
.pClipRects
= 0;
520 if ( release_texture_heaps
) {
521 /* This share group is about to go away, free our private
522 * texture object data.
526 /* this assert is wrong. The default textures are always on swap list
527 assert( is_empty_list( & rmesa->swapped ) ); */
529 for ( i
= 0 ; i
< rmesa
->nr_heaps
; i
++ ) {
530 driDestroyTextureHeap( rmesa
->texture_heaps
[ i
] );
531 rmesa
->texture_heaps
[ i
] = NULL
;
535 /* free the option cache */
536 driDestroyOptionCache (&rmesa
->optionCache
);
546 r200SwapBuffers( __DRIdrawablePrivate
*dPriv
)
548 if (dPriv
->driContextPriv
&& dPriv
->driContextPriv
->driverPrivate
) {
549 r200ContextPtr rmesa
;
551 rmesa
= (r200ContextPtr
) dPriv
->driContextPriv
->driverPrivate
;
553 if (ctx
->Visual
.doubleBufferMode
) {
554 _mesa_notifySwapBuffers( ctx
); /* flush pending rendering comands */
555 if ( rmesa
->doPageFlip
) {
556 r200PageFlip( dPriv
);
559 r200CopyBuffer( dPriv
);
564 /* XXX this shouldn't be an error but we can't handle it for now */
565 _mesa_problem(NULL
, "%s: drawable has no context!", __FUNCTION__
);
570 /* Force the context `c' to be the current context and associate with it
574 r200MakeCurrent( __DRIcontextPrivate
*driContextPriv
,
575 __DRIdrawablePrivate
*driDrawPriv
,
576 __DRIdrawablePrivate
*driReadPriv
)
578 if ( driContextPriv
) {
579 r200ContextPtr newCtx
=
580 (r200ContextPtr
) driContextPriv
->driverPrivate
;
582 if (R200_DEBUG
& DEBUG_DRI
)
583 fprintf(stderr
, "%s ctx %p\n", __FUNCTION__
, newCtx
->glCtx
);
585 if ( newCtx
->dri
.drawable
!= driDrawPriv
) {
586 driDrawableInitVBlank( driDrawPriv
, newCtx
->vblank_flags
);
587 newCtx
->dri
.drawable
= driDrawPriv
;
588 r200UpdateWindow( newCtx
->glCtx
);
589 r200UpdateViewportOffset( newCtx
->glCtx
);
592 _mesa_make_current2( newCtx
->glCtx
,
593 (GLframebuffer
*) driDrawPriv
->driverPrivate
,
594 (GLframebuffer
*) driReadPriv
->driverPrivate
);
596 if ( !newCtx
->glCtx
->Viewport
.Width
) {
597 _mesa_set_viewport( newCtx
->glCtx
, 0, 0,
598 driDrawPriv
->w
, driDrawPriv
->h
);
601 if (newCtx
->vb
.enabled
)
602 r200VtxfmtMakeCurrent( newCtx
->glCtx
);
604 _mesa_update_state( newCtx
->glCtx
);
605 r200ValidateState( newCtx
->glCtx
);
608 if (R200_DEBUG
& DEBUG_DRI
)
609 fprintf(stderr
, "%s ctx is null\n", __FUNCTION__
);
610 _mesa_make_current( 0, 0 );
613 if (R200_DEBUG
& DEBUG_DRI
)
614 fprintf(stderr
, "End %s\n", __FUNCTION__
);
618 /* Force the context `c' to be unbound from its buffer.
621 r200UnbindContext( __DRIcontextPrivate
*driContextPriv
)
623 r200ContextPtr rmesa
= (r200ContextPtr
) driContextPriv
->driverPrivate
;
625 if (R200_DEBUG
& DEBUG_DRI
)
626 fprintf(stderr
, "%s ctx %p\n", __FUNCTION__
, rmesa
->glCtx
);
628 r200VtxfmtUnbindContext( rmesa
->glCtx
);