ec95f05e6ef1ec25feff8afc4a6606dc222b51fd
[mesa.git] / src / mesa / drivers / dri / r200 / r200_context.c
1 /* $XFree86$ */
2 /**************************************************************************
3
4 Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved.
5
6 The Weather Channel (TM) funded Tungsten Graphics to develop the
7 initial release of the Radeon 8500 driver under the XFree86 license.
8 This notice must be preserved.
9
10 Permission is hereby granted, free of charge, to any person obtaining
11 a copy of this software and associated documentation files (the
12 "Software"), to deal in the Software without restriction, including
13 without limitation the rights to use, copy, modify, merge, publish,
14 distribute, sublicense, and/or sell copies of the Software, and to
15 permit persons to whom the Software is furnished to do so, subject to
16 the following conditions:
17
18 The above copyright notice and this permission notice (including the
19 next paragraph) shall be included in all copies or substantial
20 portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
26 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 **************************************************************************/
31
32 /*
33 * Authors:
34 * Keith Whitwell <keith@tungstengraphics.com>
35 */
36
37 #include "glheader.h"
38 #include "api_arrayelt.h"
39 #include "context.h"
40 #include "simple_list.h"
41 #include "imports.h"
42 #include "matrix.h"
43 #include "extensions.h"
44 #include "state.h"
45
46 #include "swrast/swrast.h"
47 #include "swrast_setup/swrast_setup.h"
48 #include "array_cache/acache.h"
49
50 #include "tnl/tnl.h"
51 #include "tnl/t_pipeline.h"
52
53 #include "r200_context.h"
54 #include "r200_ioctl.h"
55 #include "r200_state.h"
56 #include "r200_span.h"
57 #include "r200_pixel.h"
58 #include "r200_tex.h"
59 #include "r200_swtcl.h"
60 #include "r200_tcl.h"
61 #include "r200_vtxfmt.h"
62 #include "r200_maos.h"
63
64 #define DRIVER_DATE "20030328"
65
66 #include "vblank.h"
67 #include "utils.h"
68 #ifndef R200_DEBUG
69 int R200_DEBUG = (0);
70 #endif
71
72
73
74 /* Return the width and height of the given buffer.
75 */
76 static void r200GetBufferSize( GLframebuffer *buffer,
77 GLuint *width, GLuint *height )
78 {
79 GET_CURRENT_CONTEXT(ctx);
80 r200ContextPtr rmesa = R200_CONTEXT(ctx);
81
82 LOCK_HARDWARE( rmesa );
83 *width = rmesa->dri.drawable->w;
84 *height = rmesa->dri.drawable->h;
85 UNLOCK_HARDWARE( rmesa );
86 }
87
88 /* Return various strings for glGetString().
89 */
90 static const GLubyte *r200GetString( GLcontext *ctx, GLenum name )
91 {
92 r200ContextPtr rmesa = R200_CONTEXT(ctx);
93 static char buffer[128];
94 unsigned offset;
95 GLuint agp_mode = rmesa->r200Screen->IsPCI ? 0 :
96 rmesa->r200Screen->AGPMode;
97
98 switch ( name ) {
99 case GL_VENDOR:
100 return (GLubyte *)"Tungsten Graphics, Inc.";
101
102 case GL_RENDERER:
103 offset = driGetRendererString( buffer, "R200", DRIVER_DATE,
104 agp_mode );
105
106 sprintf( & buffer[ offset ], " %sTCL",
107 !(rmesa->TclFallback & R200_TCL_FALLBACK_TCL_DISABLE)
108 ? "" : "NO-" );
109
110 return (GLubyte *)buffer;
111
112 default:
113 return NULL;
114 }
115 }
116
117
118 /* Extension strings exported by the R200 driver.
119 */
120 static const char * const card_extensions[] =
121 {
122 "GL_ARB_multisample",
123 "GL_ARB_multitexture",
124 "GL_ARB_texture_border_clamp",
125 "GL_ARB_texture_compression",
126 "GL_ARB_texture_env_add",
127 "GL_ARB_texture_env_combine",
128 "GL_ARB_texture_env_dot3",
129 "GL_ARB_texture_mirrored_repeat",
130 "GL_EXT_blend_logic_op",
131 "GL_EXT_blend_minmax",
132 "GL_EXT_blend_subtract",
133 "GL_EXT_secondary_color",
134 "GL_EXT_stencil_wrap",
135 "GL_EXT_texture_edge_clamp",
136 "GL_EXT_texture_env_add",
137 "GL_EXT_texture_env_combine",
138 "GL_EXT_texture_env_dot3",
139 "GL_EXT_texture_filter_anisotropic",
140 "GL_EXT_texture_lod_bias",
141 "GL_ATI_texture_env_combine3",
142 "GL_ATI_texture_mirror_once",
143 "GL_IBM_texture_mirrored_repeat",
144 "GL_MESA_pack_invert",
145 "GL_MESA_ycbcr_texture",
146 "GL_NV_blend_square",
147 "GL_NV_texture_rectangle",
148 "GL_SGIS_generate_mipmap",
149 "GL_SGIS_texture_border_clamp",
150 "GL_SGIS_texture_edge_clamp",
151 NULL
152 };
153
154 extern const struct gl_pipeline_stage _r200_render_stage;
155 extern const struct gl_pipeline_stage _r200_tcl_stage;
156
157 static const struct gl_pipeline_stage *r200_pipeline[] = {
158
159 /* Try and go straight to t&l
160 */
161 &_r200_tcl_stage,
162
163 /* Catch any t&l fallbacks
164 */
165 &_tnl_vertex_transform_stage,
166 &_tnl_normal_transform_stage,
167 &_tnl_lighting_stage,
168 &_tnl_fog_coordinate_stage,
169 &_tnl_texgen_stage,
170 &_tnl_texture_transform_stage,
171
172 /* Try again to go to tcl?
173 * - no good for asymmetric-twoside (do with multipass)
174 * - no good for asymmetric-unfilled (do with multipass)
175 * - good for material
176 * - good for texgen
177 * - need to manipulate a bit of state
178 *
179 * - worth it/not worth it?
180 */
181
182 /* Else do them here.
183 */
184 /* &_r200_render_stage, */ /* FIXME: bugs with ut2003 */
185 &_tnl_render_stage, /* FALLBACK: */
186 0,
187 };
188
189
190
191 /* Initialize the driver's misc functions.
192 */
193 static void r200InitDriverFuncs( GLcontext *ctx )
194 {
195 ctx->Driver.GetBufferSize = r200GetBufferSize;
196 ctx->Driver.ResizeBuffers = _swrast_alloc_buffers;
197 ctx->Driver.GetString = r200GetString;
198
199 ctx->Driver.Error = NULL;
200 ctx->Driver.DrawPixels = NULL;
201 ctx->Driver.Bitmap = NULL;
202 }
203
204 static const struct dri_debug_control debug_control[] =
205 {
206 { "fall", DEBUG_FALLBACKS },
207 { "tex", DEBUG_TEXTURE },
208 { "ioctl", DEBUG_IOCTL },
209 { "prim", DEBUG_PRIMS },
210 { "vert", DEBUG_VERTS },
211 { "state", DEBUG_STATE },
212 { "code", DEBUG_CODEGEN },
213 { "vfmt", DEBUG_VFMT },
214 { "vtxf", DEBUG_VFMT },
215 { "verb", DEBUG_VERBOSE },
216 { "dri", DEBUG_DRI },
217 { "dma", DEBUG_DMA },
218 { "san", DEBUG_SANITY },
219 { "sync", DEBUG_SYNC },
220 { "pix", DEBUG_PIXEL },
221 { "mem", DEBUG_MEMORY },
222 { NULL, 0 }
223 };
224
225
226 static int
227 get_ust_nop( int64_t * ust )
228 {
229 *ust = 1;
230 return 0;
231 }
232
233
234 /* Create the device specific context.
235 */
236 GLboolean r200CreateContext( const __GLcontextModes *glVisual,
237 __DRIcontextPrivate *driContextPriv,
238 void *sharedContextPrivate)
239 {
240 __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
241 r200ScreenPtr screen = (r200ScreenPtr)(sPriv->private);
242 r200ContextPtr rmesa;
243 GLcontext *ctx, *shareCtx;
244 int i;
245
246 assert(glVisual);
247 assert(driContextPriv);
248 assert(screen);
249
250 /* Allocate the R200 context */
251 rmesa = (r200ContextPtr) CALLOC( sizeof(*rmesa) );
252 if ( !rmesa )
253 return GL_FALSE;
254
255 /* Allocate the Mesa context */
256 if (sharedContextPrivate)
257 shareCtx = ((r200ContextPtr) sharedContextPrivate)->glCtx;
258 else
259 shareCtx = NULL;
260 rmesa->glCtx = _mesa_create_context(glVisual, shareCtx, (void *) rmesa, GL_TRUE);
261 if (!rmesa->glCtx) {
262 FREE(rmesa);
263 return GL_FALSE;
264 }
265 driContextPriv->driverPrivate = rmesa;
266
267 /* Init r200 context data */
268 rmesa->dri.context = driContextPriv;
269 rmesa->dri.screen = sPriv;
270 rmesa->dri.drawable = NULL; /* Set by XMesaMakeCurrent */
271 rmesa->dri.hwContext = driContextPriv->hHWContext;
272 rmesa->dri.hwLock = &sPriv->pSAREA->lock;
273 rmesa->dri.fd = sPriv->fd;
274 rmesa->dri.drmMinor = sPriv->drmMinor;
275
276 rmesa->r200Screen = screen;
277 rmesa->sarea = (RADEONSAREAPrivPtr)((GLubyte *)sPriv->pSAREA +
278 screen->sarea_priv_offset);
279
280
281 rmesa->dma.buf0_address = rmesa->r200Screen->buffers->list[0].address;
282
283 (void) memset( rmesa->texture_heaps, 0, sizeof( rmesa->texture_heaps ) );
284 make_empty_list( & rmesa->swapped );
285
286 rmesa->nr_heaps = 1 /* screen->numTexHeaps */ ;
287 for ( i = 0 ; i < rmesa->nr_heaps ; i++ ) {
288 rmesa->texture_heaps[i] = driCreateTextureHeap( i, rmesa,
289 screen->texSize[i],
290 12,
291 RADEON_NR_TEX_REGIONS,
292 rmesa->sarea->texList[i],
293 & rmesa->sarea->texAge[i],
294 & rmesa->swapped,
295 sizeof( r200TexObj ),
296 (destroy_texture_object_t *) r200DestroyTexObj );
297 }
298
299 rmesa->swtcl.RenderIndex = ~0;
300 rmesa->lost_context = 1;
301
302 /* Set the maximum texture size small enough that we can guarentee that
303 * all texture units can bind a maximal texture and have them both in
304 * texturable memory at once.
305 */
306
307 ctx = rmesa->glCtx;
308 ctx->Const.MaxTextureUnits = 2;
309
310 driCalculateMaxTextureLevels( rmesa->texture_heaps,
311 rmesa->nr_heaps,
312 & ctx->Const,
313 4,
314 11, /* max 2D texture size is 2048x2048 */
315 #if ENABLE_HW_3D_TEXTURE
316 8, /* max 3D texture size is 256^3 */
317 #else
318 0, /* 3D textures unsupported */
319 #endif
320 11, /* max cube texture size is 2048x2048 */
321 11, /* max texture rectangle size is 2048x2048 */
322 12,
323 GL_FALSE );
324
325 ctx->Const.MaxTextureMaxAnisotropy = 16.0;
326
327 /* No wide points.
328 */
329 ctx->Const.MinPointSize = 1.0;
330 ctx->Const.MinPointSizeAA = 1.0;
331 ctx->Const.MaxPointSize = 1.0;
332 ctx->Const.MaxPointSizeAA = 1.0;
333
334 ctx->Const.MinLineWidth = 1.0;
335 ctx->Const.MinLineWidthAA = 1.0;
336 ctx->Const.MaxLineWidth = 10.0;
337 ctx->Const.MaxLineWidthAA = 10.0;
338 ctx->Const.LineWidthGranularity = 0.0625;
339
340 /* Initialize the software rasterizer and helper modules.
341 */
342 _swrast_CreateContext( ctx );
343 _ac_CreateContext( ctx );
344 _tnl_CreateContext( ctx );
345 _swsetup_CreateContext( ctx );
346 _ae_create_context( ctx );
347
348 /* Install the customized pipeline:
349 */
350 _tnl_destroy_pipeline( ctx );
351 _tnl_install_pipeline( ctx, r200_pipeline );
352 ctx->Driver.FlushVertices = r200FlushVertices;
353
354 /* Try and keep materials and vertices separate:
355 */
356 _tnl_isolate_materials( ctx, GL_TRUE );
357
358
359 /* Configure swrast to match hardware characteristics:
360 */
361 _swrast_allow_pixel_fog( ctx, GL_FALSE );
362 _swrast_allow_vertex_fog( ctx, GL_TRUE );
363
364
365 _math_matrix_ctr( &rmesa->TexGenMatrix[0] );
366 _math_matrix_ctr( &rmesa->TexGenMatrix[1] );
367 _math_matrix_ctr( &rmesa->tmpmat );
368 _math_matrix_set_identity( &rmesa->TexGenMatrix[0] );
369 _math_matrix_set_identity( &rmesa->TexGenMatrix[1] );
370 _math_matrix_set_identity( &rmesa->tmpmat );
371
372 driInitExtensions( ctx, card_extensions, GL_TRUE );
373 if (rmesa->r200Screen->drmSupportsCubeMaps)
374 _mesa_enable_extension( ctx, "GL_ARB_texture_cube_map" );
375
376 r200InitDriverFuncs( ctx );
377 r200InitIoctlFuncs( ctx );
378 r200InitStateFuncs( ctx );
379 r200InitSpanFuncs( ctx );
380 r200InitPixelFuncs( ctx );
381 r200InitTextureFuncs( ctx );
382 r200InitState( rmesa );
383 r200InitSwtcl( ctx );
384
385 rmesa->iw.irq_seq = -1;
386 rmesa->irqsEmitted = 0;
387 rmesa->do_irqs = (rmesa->dri.drmMinor >= 6 &&
388 !getenv("R200_NO_IRQS") &&
389 rmesa->r200Screen->irq);
390
391 if (!rmesa->do_irqs)
392 fprintf(stderr,
393 "IRQ's not enabled, falling back to busy waits: %d %d %d\n",
394 rmesa->dri.drmMinor,
395 !!getenv("R200_NO_IRQS"),
396 rmesa->r200Screen->irq);
397
398
399 rmesa->do_usleeps = !getenv("R200_NO_USLEEPS");
400
401 rmesa->vblank_flags = (rmesa->do_irqs)
402 ? driGetDefaultVBlankFlags() : VBLANK_FLAG_NO_IRQ;
403
404 rmesa->prefer_agp_client_texturing =
405 (getenv("R200_AGP_CLIENT_TEXTURES") != 0);
406
407 #ifndef _SOLO
408 rmesa->get_ust = (PFNGLXGETUSTPROC) glXGetProcAddress( "__glXGetUST" );
409 if ( rmesa->get_ust == NULL ) {
410 rmesa->get_ust = get_ust_nop;
411 }
412
413 (*rmesa->get_ust)( & rmesa->swap_ust );
414 #endif
415
416 #if DO_DEBUG
417 R200_DEBUG = driParseDebugString( getenv( "R200_DEBUG" ),
418 debug_control );
419 R200_DEBUG |= driParseDebugString( getenv( "RADEON_DEBUG" ),
420 debug_control );
421 #endif
422
423 if (getenv("R200_NO_RAST")) {
424 fprintf(stderr, "disabling 3D acceleration\n");
425 FALLBACK(rmesa, R200_FALLBACK_DISABLE, 1);
426 }
427 else if (getenv("R200_NO_TCL")) {
428 fprintf(stderr, "disabling TCL support\n");
429 TCL_FALLBACK(rmesa->glCtx, R200_TCL_FALLBACK_TCL_DISABLE, 1);
430 }
431 else {
432 if (!getenv("R200_NO_VTXFMT")) {
433 r200VtxfmtInit( ctx );
434 }
435 _tnl_need_dlist_norm_lengths( ctx, GL_FALSE );
436 }
437 return GL_TRUE;
438 }
439
440
441 /* Destroy the device specific context.
442 */
443 /* Destroy the Mesa and driver specific context data.
444 */
445 void r200DestroyContext( __DRIcontextPrivate *driContextPriv )
446 {
447 GET_CURRENT_CONTEXT(ctx);
448 r200ContextPtr rmesa = (r200ContextPtr) driContextPriv->driverPrivate;
449 r200ContextPtr current = ctx ? R200_CONTEXT(ctx) : NULL;
450
451 /* check if we're deleting the currently bound context */
452 if (rmesa == current) {
453 R200_FIREVERTICES( rmesa );
454 _mesa_make_current2(NULL, NULL, NULL);
455 }
456
457 /* Free r200 context resources */
458 assert(rmesa); /* should never be null */
459 if ( rmesa ) {
460 GLboolean release_texture_heaps;
461
462
463 release_texture_heaps = (rmesa->glCtx->Shared->RefCount == 1);
464 _swsetup_DestroyContext( rmesa->glCtx );
465 _tnl_DestroyContext( rmesa->glCtx );
466 _ac_DestroyContext( rmesa->glCtx );
467 _swrast_DestroyContext( rmesa->glCtx );
468
469 r200DestroySwtcl( rmesa->glCtx );
470 r200ReleaseArrays( rmesa->glCtx, ~0 );
471
472 if (rmesa->dma.current.buf) {
473 r200ReleaseDmaRegion( rmesa, &rmesa->dma.current, __FUNCTION__ );
474 r200FlushCmdBuf( rmesa, __FUNCTION__ );
475 }
476
477 if (!rmesa->TclFallback & R200_TCL_FALLBACK_TCL_DISABLE)
478 if (!getenv("R200_NO_VTXFMT"))
479 r200VtxfmtDestroy( rmesa->glCtx );
480
481 /* free the Mesa context */
482 rmesa->glCtx->DriverCtx = NULL;
483 _mesa_destroy_context( rmesa->glCtx );
484
485 if (rmesa->state.scissor.pClipRects) {
486 FREE(rmesa->state.scissor.pClipRects);
487 rmesa->state.scissor.pClipRects = 0;
488 }
489
490 if ( release_texture_heaps ) {
491 /* This share group is about to go away, free our private
492 * texture object data.
493 */
494 int i;
495
496 /* this assert is wrong. The default textures are always on swap list
497 assert( is_empty_list( & rmesa->swapped ) ); */
498
499 for ( i = 0 ; i < rmesa->nr_heaps ; i++ ) {
500 driDestroyTextureHeap( rmesa->texture_heaps[ i ] );
501 rmesa->texture_heaps[ i ] = NULL;
502 }
503 }
504
505 FREE( rmesa );
506 }
507 }
508
509
510
511
512 void
513 r200SwapBuffers( __DRIdrawablePrivate *dPriv )
514 {
515 if (dPriv->driContextPriv && dPriv->driContextPriv->driverPrivate) {
516 r200ContextPtr rmesa;
517 GLcontext *ctx;
518 rmesa = (r200ContextPtr) dPriv->driContextPriv->driverPrivate;
519 ctx = rmesa->glCtx;
520 if (ctx->Visual.doubleBufferMode) {
521 _mesa_notifySwapBuffers( ctx ); /* flush pending rendering comands */
522 if ( rmesa->doPageFlip ) {
523 r200PageFlip( dPriv );
524 }
525 else {
526 r200CopyBuffer( dPriv );
527 }
528 }
529 }
530 else {
531 /* XXX this shouldn't be an error but we can't handle it for now */
532 _mesa_problem(NULL, "%s: drawable has no context!", __FUNCTION__);
533 }
534 }
535
536
537 /* Force the context `c' to be the current context and associate with it
538 * buffer `b'.
539 */
540 GLboolean
541 r200MakeCurrent( __DRIcontextPrivate *driContextPriv,
542 __DRIdrawablePrivate *driDrawPriv,
543 __DRIdrawablePrivate *driReadPriv )
544 {
545 if ( driContextPriv ) {
546 r200ContextPtr newCtx =
547 (r200ContextPtr) driContextPriv->driverPrivate;
548
549 if (R200_DEBUG & DEBUG_DRI)
550 fprintf(stderr, "%s ctx %p\n", __FUNCTION__, newCtx->glCtx);
551
552 if ( newCtx->dri.drawable != driDrawPriv ) {
553 newCtx->dri.drawable = driDrawPriv;
554 r200UpdateWindow( newCtx->glCtx );
555 r200UpdateViewportOffset( newCtx->glCtx );
556 }
557
558 _mesa_make_current2( newCtx->glCtx,
559 (GLframebuffer *) driDrawPriv->driverPrivate,
560 (GLframebuffer *) driReadPriv->driverPrivate );
561
562 if ( !newCtx->glCtx->Viewport.Width ) {
563 _mesa_set_viewport( newCtx->glCtx, 0, 0,
564 driDrawPriv->w, driDrawPriv->h );
565 }
566
567 if (newCtx->vb.enabled)
568 r200VtxfmtMakeCurrent( newCtx->glCtx );
569
570 _mesa_update_state( newCtx->glCtx );
571 r200ValidateState( newCtx->glCtx );
572
573 } else {
574 if (R200_DEBUG & DEBUG_DRI)
575 fprintf(stderr, "%s ctx is null\n", __FUNCTION__);
576 _mesa_make_current( 0, 0 );
577 }
578
579 if (R200_DEBUG & DEBUG_DRI)
580 fprintf(stderr, "End %s\n", __FUNCTION__);
581 return GL_TRUE;
582 }
583
584 /* Force the context `c' to be unbound from its buffer.
585 */
586 GLboolean
587 r200UnbindContext( __DRIcontextPrivate *driContextPriv )
588 {
589 r200ContextPtr rmesa = (r200ContextPtr) driContextPriv->driverPrivate;
590
591 if (R200_DEBUG & DEBUG_DRI)
592 fprintf(stderr, "%s ctx %p\n", __FUNCTION__, rmesa->glCtx);
593
594 r200VtxfmtUnbindContext( rmesa->glCtx );
595 return GL_TRUE;
596 }