(Stephane Marchesin, me) add hyperz support to radeon and r200 drivers. Only fast...
[mesa.git] / src / mesa / drivers / dri / i830 / i830_context.c
1 /**************************************************************************
2 *
3 * Copyright 1998-1999 Precision Insight, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 * **************************************************************************/
27 /* $XFree86: xc/lib/GL/mesa/src/drv/i830/i830_context.c,v 1.9 2003/02/06 04:18:00 dawes Exp $ */
28
29 /**
30 * \file i830_context.c
31 *
32 * Heavily Based on I810 driver written by Keith Whitwell.
33 *
34 * \author Jeff Hartmann <jhartmann@2d3d.com>
35 * \author Graeme Fisher <graeme@2d3d.co.za>
36 * \author Abraham vd Merwe <abraham@2d3d.co.za>
37 * \author Keith Whitwell <keith@tungstengraphics.com>
38 */
39
40 #include "glheader.h"
41 #include "context.h"
42 #include "matrix.h"
43 #include "simple_list.h"
44 #include "extensions.h"
45 #include "imports.h"
46
47 #include "swrast/swrast.h"
48 #include "swrast_setup/swrast_setup.h"
49 #include "tnl/tnl.h"
50 #include "array_cache/acache.h"
51
52 #include "tnl/t_pipeline.h"
53
54 #include "drivers/common/driverfuncs.h"
55
56 #include "i830_screen.h"
57 #include "i830_dri.h"
58
59 #include "i830_state.h"
60 #include "i830_tex.h"
61 #include "i830_span.h"
62 #include "i830_tris.h"
63 #include "i830_ioctl.h"
64
65
66 #include "utils.h"
67 #include "xmlpool.h" /* for symbolic values of enum-type options */
68 #ifndef I830_DEBUG
69 int I830_DEBUG = (0);
70 #endif
71
72 /***************************************
73 * Mesa's Driver Functions
74 ***************************************/
75
76 #define DRIVER_DATE "20041007"
77
78
79 static const GLubyte *i830DDGetString( GLcontext *ctx, GLenum name )
80 {
81 const char * chipset;
82 static char buffer[128];
83
84 switch (name) {
85 case GL_VENDOR:
86 switch (I830_CONTEXT(ctx)->i830Screen->deviceID) {
87 case PCI_CHIP_845_G:
88 return (GLubyte *)"2d3D, Inc";
89
90 case PCI_CHIP_I830_M:
91 return (GLubyte *)"VA Linux, Inc";
92
93 case PCI_CHIP_I855_GM:
94 case PCI_CHIP_I865_G:
95 default:
96 return (GLubyte *)"Tungsten Graphics, Inc";
97 }
98 break;
99
100 case GL_RENDERER:
101 switch (I830_CONTEXT(ctx)->i830Screen->deviceID) {
102 case PCI_CHIP_845_G:
103 chipset = "Intel(R) 845G"; break;
104 case PCI_CHIP_I830_M:
105 chipset = "Intel(R) 830M"; break;
106 case PCI_CHIP_I855_GM:
107 chipset = "Intel(R) 852GM/855GM"; break;
108 case PCI_CHIP_I865_G:
109 chipset = "Intel(R) 865G"; break;
110 default:
111 chipset = "Unknown Intel Chipset"; break;
112 }
113
114 (void) driGetRendererString( buffer, chipset, DRIVER_DATE, 0 );
115 return (GLubyte *) buffer;
116
117 default:
118 return NULL;
119 }
120 }
121
122 static void i830BufferSize(GLframebuffer *buffer,
123 GLuint *width, GLuint *height)
124 {
125 GET_CURRENT_CONTEXT(ctx);
126 i830ContextPtr imesa = I830_CONTEXT(ctx);
127 /* Need to lock to make sure the driDrawable is uptodate. This
128 * information is used to resize Mesa's software buffers, so it has
129 * to be correct.
130 */
131 LOCK_HARDWARE(imesa);
132 *width = imesa->driDrawable->w;
133 *height = imesa->driDrawable->h;
134 UNLOCK_HARDWARE(imesa);
135 }
136
137
138 /* Extension strings exported by the i830 driver.
139 */
140 static const char * const card_extensions[] =
141 {
142 "GL_ARB_multisample",
143 "GL_ARB_multitexture",
144 "GL_ARB_texture_border_clamp",
145 "GL_ARB_texture_compression",
146 "GL_ARB_texture_env_add",
147 "GL_ARB_texture_env_combine",
148 "GL_ARB_texture_env_crossbar",
149 "GL_ARB_texture_env_dot3",
150 "GL_ARB_texture_mirrored_repeat",
151 "GL_EXT_blend_color",
152 "GL_EXT_blend_equation_separate",
153 "GL_EXT_blend_func_separate",
154 "GL_EXT_blend_minmax",
155 "GL_EXT_blend_subtract",
156 "GL_EXT_fog_coord",
157 "GL_EXT_secondary_color",
158 "GL_EXT_stencil_wrap",
159 "GL_EXT_texture_edge_clamp",
160 "GL_EXT_texture_env_combine",
161 "GL_EXT_texture_env_dot3",
162 "GL_EXT_texture_filter_anisotropic",
163 "GL_EXT_texture_lod_bias",
164 "GL_EXT_texture_rectangle",
165 "GL_NV_blend_square",
166 "GL_MESA_ycbcr_texture",
167 "GL_SGIS_generate_mipmap",
168 NULL
169 };
170
171
172 extern const struct tnl_pipeline_stage _i830_render_stage;
173
174 static const struct tnl_pipeline_stage *i830_pipeline[] = {
175 &_tnl_vertex_transform_stage,
176 &_tnl_normal_transform_stage,
177 &_tnl_lighting_stage,
178 &_tnl_fog_coordinate_stage,
179 &_tnl_texgen_stage,
180 &_tnl_texture_transform_stage,
181 /* REMOVE: point attenuation stage */
182 #if 1
183 &_i830_render_stage, /* ADD: unclipped rastersetup-to-dma */
184 #endif
185 &_tnl_render_stage,
186 0,
187 };
188
189
190 static const struct dri_debug_control debug_control[] =
191 {
192 { "fall", DEBUG_FALLBACKS },
193 { "tex", DEBUG_TEXTURE },
194 { "ioctl", DEBUG_IOCTL },
195 { "prim", DEBUG_PRIMS },
196 { "vert", DEBUG_VERTS },
197 { "state", DEBUG_STATE },
198 { "verb", DEBUG_VERBOSE },
199 { "dri", DEBUG_DRI },
200 { "dma", DEBUG_DMA },
201 { "san", DEBUG_SANITY },
202 { "sync", DEBUG_SYNC },
203 { "sleep", DEBUG_SLEEP },
204 { NULL, 0 }
205 };
206
207
208 GLboolean i830CreateContext( const __GLcontextModes *mesaVis,
209 __DRIcontextPrivate *driContextPriv,
210 void *sharedContextPrivate)
211 {
212 GLcontext *ctx , *shareCtx;
213 i830ContextPtr imesa;
214 __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
215 i830ScreenPrivate *screen = (i830ScreenPrivate *)sPriv->private;
216 I830SAREAPtr saPriv=(I830SAREAPtr)
217 (((GLubyte *)sPriv->pSAREA)+screen->sarea_priv_offset);
218 struct dd_function_table functions;
219
220 /* Allocate i830 context */
221 imesa = (i830ContextPtr) CALLOC_STRUCT(i830_context_t);
222 if (!imesa)
223 return GL_FALSE;
224
225 /* Init default driver functions then plug in our I830-specific functions
226 * (the texture functions are especially important)
227 */
228 _mesa_init_driver_functions(&functions);
229 i830InitIoctlFuncs(&functions);
230 i830InitTextureFuncs(&functions);
231
232 /* Allocate the Mesa context */
233 if (sharedContextPrivate)
234 shareCtx = ((i830ContextPtr) sharedContextPrivate)->glCtx;
235 else
236 shareCtx = NULL;
237 imesa->glCtx = _mesa_create_context(mesaVis, shareCtx,
238 &functions, (void*) imesa);
239 if (!imesa->glCtx) {
240 FREE(imesa);
241 return GL_FALSE;
242 }
243 driContextPriv->driverPrivate = imesa;
244
245
246 imesa->i830Screen = screen;
247 imesa->driScreen = sPriv;
248 imesa->sarea = saPriv;
249 imesa->glBuffer = NULL;
250
251 driParseConfigFiles (&imesa->optionCache, &screen->optionCache,
252 screen->driScrnPriv->myNum, "i830");
253
254 (void) memset( imesa->texture_heaps, 0, sizeof( imesa->texture_heaps ) );
255 make_empty_list( & imesa->swapped );
256
257 imesa->nr_heaps = 1;
258 imesa->texture_heaps[0] = driCreateTextureHeap( 0, imesa,
259 screen->textureSize,
260 12,
261 I830_NR_TEX_REGIONS,
262 imesa->sarea->texList,
263 (unsigned *) & imesa->sarea->texAge, /* XXX shouldn't need cast! */
264 & imesa->swapped,
265 sizeof( struct i830_texture_object_t ),
266 (destroy_texture_object_t *) i830DestroyTexObj );
267
268 /* Set the maximum texture size small enough that we can guarantee
269 * that every texture unit can bind a maximal texture and have them
270 * in memory at once.
271 */
272
273 ctx = imesa->glCtx;
274 ctx->Const.MaxTextureUnits = driQueryOptioni(&imesa->optionCache,
275 "texture_units");
276 ctx->Const.MaxTextureImageUnits = ctx->Const.MaxTextureUnits;
277 ctx->Const.MaxTextureCoordUnits = ctx->Const.MaxTextureUnits;
278
279 /* FIXME: driCalculateMaxTextureLevels assumes that mipmaps are tightly
280 * FIXME: packed, but they're not in Intel graphics hardware.
281 */
282 driCalculateMaxTextureLevels( imesa->texture_heaps,
283 imesa->nr_heaps,
284 & ctx->Const,
285 4,
286 11, /* max 2D texture size is 2048x2048 */
287 0, /* 3D textures unsupported */
288 0, /* cube textures unsupported. */
289 0, /* texture rectangles unsupported. */
290 12,
291 GL_FALSE );
292
293 ctx->Const.MaxTextureMaxAnisotropy = 2.0;
294
295 ctx->Const.MinLineWidth = 1.0;
296 ctx->Const.MinLineWidthAA = 1.0;
297 ctx->Const.MaxLineWidth = 3.0;
298 ctx->Const.MaxLineWidthAA = 3.0;
299 ctx->Const.LineWidthGranularity = 1.0;
300
301 ctx->Const.MinPointSize = 1.0;
302 ctx->Const.MinPointSizeAA = 1.0;
303 ctx->Const.MaxPointSize = 255.0;
304 ctx->Const.MaxPointSizeAA = 3.0;
305 ctx->Const.PointSizeGranularity = 1.0;
306
307 ctx->Driver.GetBufferSize = i830BufferSize;
308 ctx->Driver.ResizeBuffers = _swrast_alloc_buffers;
309 ctx->Driver.GetString = i830DDGetString;
310
311 /* Who owns who? */
312 ctx->DriverCtx = (void *) imesa;
313 imesa->glCtx = ctx;
314
315 /* Initialize the software rasterizer and helper modules. */
316 _swrast_CreateContext( ctx );
317 _ac_CreateContext( ctx );
318 _tnl_CreateContext( ctx );
319 _swsetup_CreateContext( ctx );
320
321 /* Install the customized pipeline: */
322 _tnl_destroy_pipeline( ctx );
323 _tnl_install_pipeline( ctx, i830_pipeline );
324
325 /* Configure swrast and T&L to match hardware characteristics: */
326 _swrast_allow_pixel_fog( ctx, GL_FALSE );
327 _swrast_allow_vertex_fog( ctx, GL_TRUE );
328 _tnl_allow_pixel_fog( ctx, GL_FALSE );
329 _tnl_allow_vertex_fog( ctx, GL_TRUE );
330
331 /* Dri stuff */
332 imesa->hHWContext = driContextPriv->hHWContext;
333 imesa->driFd = sPriv->fd;
334 /* drmLock ptr = &drm_hw_lock_t */
335 imesa->driHwLock = (drmLock *) &sPriv->pSAREA->lock;
336 imesa->hw_stencil = mesaVis->stencilBits && mesaVis->depthBits == 24;
337
338 switch(mesaVis->depthBits) {
339 case 16:
340 imesa->depth_scale = 1.0/0xffff;
341 imesa->depth_clear_mask = ~0;
342 imesa->ClearDepth = 0xffff;
343 break;
344 case 24:
345 imesa->depth_scale = 1.0/0xffffff;
346 imesa->depth_clear_mask = 0x00ffffff;
347 imesa->stencil_clear_mask = 0xff000000;
348 imesa->ClearDepth = 0x00ffffff;
349 break;
350 case 32: /* Not supported */
351 default:
352 break;
353 }
354 /* Completely disable stenciling for now, there are some serious issues
355 * with stencil.
356 */
357 #if 0
358 imesa->hw_stencil = 0;
359 #endif
360
361 imesa->RenderIndex = ~0;
362 imesa->dirty = ~0;
363 imesa->upload_cliprects = GL_TRUE;
364
365 imesa->CurrentTexObj[0] = 0;
366 imesa->CurrentTexObj[1] = 0;
367
368 imesa->do_irqs = (imesa->i830Screen->irq_active &&
369 !getenv("I830_NO_IRQS"));
370
371 _math_matrix_ctr (&imesa->ViewportMatrix);
372
373 driInitExtensions( ctx, card_extensions, GL_TRUE );
374
375 if (imesa->glCtx->Mesa_DXTn) {
376 _mesa_enable_extension( ctx, "GL_EXT_texture_compression_s3tc" );
377 _mesa_enable_extension( ctx, "GL_S3_s3tc" );
378 }
379 else if (driQueryOptionb (&imesa->optionCache, "force_s3tc_enable")) {
380 _mesa_enable_extension( ctx, "GL_EXT_texture_compression_s3tc" );
381 }
382
383 _mesa_enable_extension( ctx, "GL_3DFX_texture_compression_FXT1" );
384
385 /* XXX these should really go right after _mesa_init_driver_functions() */
386 i830DDInitStateFuncs( ctx );
387 i830InitTriFuncs (ctx);
388 i830DDInitSpanFuncs( ctx );
389 i830DDInitState (ctx);
390
391 #if DO_DEBUG
392 I830_DEBUG = driParseDebugString( getenv( "I830_DEBUG" ),
393 debug_control );
394 I830_DEBUG |= driParseDebugString( getenv( "INTEL_DEBUG" ),
395 debug_control );
396 #endif
397
398 if (getenv("I830_NO_RAST") ||
399 getenv("INTEL_NO_RAST")) {
400 fprintf(stderr, "disabling 3D rasterization\n");
401 FALLBACK(imesa, I830_FALLBACK_USER, 1);
402 }
403
404 return GL_TRUE;
405 }
406
407 void i830DestroyContext(__DRIcontextPrivate *driContextPriv)
408 {
409 i830ContextPtr imesa = (i830ContextPtr) driContextPriv->driverPrivate;
410
411 assert(imesa); /* should never be null */
412 if (imesa) {
413 GLboolean release_texture_heaps;
414
415
416 release_texture_heaps = (imesa->glCtx->Shared->RefCount == 1);
417 _swsetup_DestroyContext (imesa->glCtx);
418 _tnl_DestroyContext (imesa->glCtx);
419 _ac_DestroyContext (imesa->glCtx);
420 _swrast_DestroyContext (imesa->glCtx);
421
422 /* free the Mesa context */
423 imesa->glCtx->DriverCtx = NULL;
424 _mesa_destroy_context(imesa->glCtx);
425
426 if ( release_texture_heaps ) {
427 /* This share group is about to go away, free our private
428 * texture object data.
429 */
430 int i;
431
432 for ( i = 0 ; i < imesa->nr_heaps ; i++ ) {
433 driDestroyTextureHeap( imesa->texture_heaps[ i ] );
434 imesa->texture_heaps[ i ] = NULL;
435 }
436
437 assert( is_empty_list( & imesa->swapped ) );
438 }
439
440 FREE(imesa);
441 }
442 }
443
444 void i830XMesaSetFrontClipRects( i830ContextPtr imesa )
445 {
446 __DRIdrawablePrivate *dPriv = imesa->driDrawable;
447
448 imesa->numClipRects = dPriv->numClipRects;
449 imesa->pClipRects = dPriv->pClipRects;
450 imesa->drawX = dPriv->x;
451 imesa->drawY = dPriv->y;
452
453 i830EmitDrawingRectangle( imesa );
454 imesa->upload_cliprects = GL_TRUE;
455 }
456
457 void i830XMesaSetBackClipRects( i830ContextPtr imesa )
458 {
459 __DRIdrawablePrivate *dPriv = imesa->driDrawable;
460
461 if (imesa->sarea->pf_enabled == 0 && dPriv->numBackClipRects == 0) {
462 imesa->numClipRects = dPriv->numClipRects;
463 imesa->pClipRects = dPriv->pClipRects;
464 imesa->drawX = dPriv->x;
465 imesa->drawY = dPriv->y;
466 } else {
467 imesa->numClipRects = dPriv->numBackClipRects;
468 imesa->pClipRects = dPriv->pBackClipRects;
469 imesa->drawX = dPriv->backX;
470 imesa->drawY = dPriv->backY;
471 }
472
473 i830EmitDrawingRectangle( imesa );
474 imesa->upload_cliprects = GL_TRUE;
475 }
476
477 static void i830XMesaWindowMoved( i830ContextPtr imesa )
478 {
479 switch (imesa->glCtx->Color._DrawDestMask[0]) {
480 case DD_FRONT_LEFT_BIT:
481 i830XMesaSetFrontClipRects( imesa );
482 break;
483 case DD_BACK_LEFT_BIT:
484 i830XMesaSetBackClipRects( imesa );
485 break;
486 default:
487 /* glDrawBuffer(GL_NONE or GL_FRONT_AND_BACK): software fallback */
488 i830XMesaSetFrontClipRects( imesa );
489 }
490 }
491
492 GLboolean i830UnbindContext(__DRIcontextPrivate *driContextPriv)
493 {
494 i830ContextPtr imesa = (i830ContextPtr) driContextPriv->driverPrivate;
495 unsigned i;
496
497 if (imesa) {
498 /* Might want to change this so texblend isn't always updated */
499 imesa->dirty |= (I830_UPLOAD_CTX |
500 I830_UPLOAD_BUFFERS |
501 I830_UPLOAD_STIPPLE |
502 I830_UPLOAD_TEXBLEND0 |
503 I830_UPLOAD_TEXBLEND1 |
504 I830_UPLOAD_TEXBLEND2 |
505 I830_UPLOAD_TEXBLEND3);
506
507 for ( i = 0 ; i < imesa->glCtx->Const.MaxTextureUnits ; i++ ) {
508 if (imesa->CurrentTexObj[i]) imesa->dirty |= I830_UPLOAD_TEX_N( i );
509 }
510 }
511 return GL_TRUE;
512 }
513
514 GLboolean i830MakeCurrent(__DRIcontextPrivate *driContextPriv,
515 __DRIdrawablePrivate *driDrawPriv,
516 __DRIdrawablePrivate *driReadPriv)
517 {
518
519 if (driContextPriv) {
520 i830ContextPtr imesa = (i830ContextPtr) driContextPriv->driverPrivate;
521
522 if ( imesa->driDrawable != driDrawPriv ) {
523 imesa->driDrawable = driDrawPriv;
524 i830XMesaWindowMoved( imesa );
525 imesa->mesa_drawable = driDrawPriv;
526 }
527
528 imesa->driReadable = driReadPriv;
529
530 _mesa_make_current2(imesa->glCtx,
531 (GLframebuffer *) driDrawPriv->driverPrivate,
532 (GLframebuffer *) driReadPriv->driverPrivate);
533 } else {
534 _mesa_make_current(0,0);
535 }
536
537 return GL_TRUE;
538 }
539
540 void i830GetLock( i830ContextPtr imesa, GLuint flags )
541 {
542 __DRIdrawablePrivate *dPriv = imesa->driDrawable;
543 __DRIscreenPrivate *sPriv = imesa->driScreen;
544 I830SAREAPtr sarea = imesa->sarea;
545 int me = imesa->hHWContext;
546 unsigned i;
547
548 drmGetLock(imesa->driFd, imesa->hHWContext, flags);
549
550 /* If the window moved, may need to set a new cliprect now.
551 *
552 * NOTE: This releases and regains the hw lock, so all state
553 * checking must be done *after* this call:
554 */
555 DRI_VALIDATE_DRAWABLE_INFO( sPriv, dPriv);
556
557 /* If we lost context, need to dump all registers to hardware.
558 * Note that we don't care about 2d contexts, even if they perform
559 * accelerated commands, so the DRI locking in the X server is even
560 * more broken than usual.
561 */
562
563 if (sarea->ctxOwner != me) {
564 imesa->upload_cliprects = GL_TRUE;
565 imesa->dirty |= (I830_UPLOAD_CTX |
566 I830_UPLOAD_BUFFERS |
567 I830_UPLOAD_STIPPLE);
568
569 for ( i = 0 ; i < imesa->glCtx->Const.MaxTextureUnits ; i++ ) {
570 if(imesa->CurrentTexObj[i]) imesa->dirty |= I830_UPLOAD_TEX_N( i );
571 if(imesa->TexBlendWordsUsed[i]) imesa->dirty |= I830_UPLOAD_TEXBLEND_N( i );
572 }
573
574 sarea->perf_boxes = imesa->perf_boxes | I830_BOX_LOST_CONTEXT;
575 sarea->ctxOwner = me;
576 }
577
578 /* Shared texture managment - if another client has played with
579 * texture space, figure out which if any of our textures have been
580 * ejected, and update our global LRU.
581 */
582
583 for ( i = 0 ; i < imesa->nr_heaps ; i++ ) {
584 DRI_AGE_TEXTURES( imesa->texture_heaps[ i ] );
585 }
586
587 if (imesa->lastStamp != dPriv->lastStamp) {
588 i830XMesaWindowMoved( imesa );
589 imesa->lastStamp = dPriv->lastStamp;
590 }
591
592 sarea->last_quiescent = -1; /* just kill it for now */
593 }
594
595 void i830SwapBuffers( __DRIdrawablePrivate *dPriv )
596 {
597 if (dPriv->driContextPriv && dPriv->driContextPriv->driverPrivate) {
598 i830ContextPtr imesa;
599 GLcontext *ctx;
600 imesa = (i830ContextPtr) dPriv->driContextPriv->driverPrivate;
601 ctx = imesa->glCtx;
602 if (ctx->Visual.doubleBufferMode) {
603 _mesa_notifySwapBuffers( ctx ); /* flush pending rendering comands */
604 if ( 0 /*imesa->doPageFlip*/ ) { /* doPageFlip is never set !!! */
605 i830PageFlip( dPriv );
606 } else {
607 i830CopyBuffer( dPriv );
608 }
609 }
610 } else {
611 /* XXX this shouldn't be an error but we can't handle it for now */
612 _mesa_problem(NULL, "%s: drawable has no context!\n", __FUNCTION__);
613 }
614 }