Merge branch 'master-merge' into vbo-0.2
[mesa.git] / src / mesa / drivers / dri / i965 / intel_context.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, 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 TUNGSTEN GRAPHICS 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
28
29 #include "glheader.h"
30 #include "context.h"
31 #include "matrix.h"
32 #include "simple_list.h"
33 #include "extensions.h"
34 #include "framebuffer.h"
35 #include "imports.h"
36
37 #include "swrast/swrast.h"
38 #include "swrast_setup/swrast_setup.h"
39 #include "tnl/tnl.h"
40 #include "vbo/vbo.h"
41
42 #include "tnl/t_pipeline.h"
43 #include "tnl/t_vertex.h"
44
45 #include "drivers/common/driverfuncs.h"
46
47 #include "intel_screen.h"
48
49 #include "i830_dri.h"
50 #include "i830_common.h"
51
52 #include "intel_tex.h"
53 #include "intel_span.h"
54 #include "intel_ioctl.h"
55 #include "intel_batchbuffer.h"
56 #include "intel_blit.h"
57 #include "intel_regions.h"
58 #include "intel_buffer_objects.h"
59
60 #include "bufmgr.h"
61
62 #include "utils.h"
63 #ifndef INTEL_DEBUG
64 int INTEL_DEBUG = (0);
65 #endif
66
67 #define need_GL_ARB_multisample
68 #define need_GL_ARB_point_parameters
69 #define need_GL_ARB_texture_compression
70 #define need_GL_ARB_vertex_buffer_object
71 #define need_GL_ARB_vertex_program
72 #define need_GL_ARB_window_pos
73 #define need_GL_EXT_blend_color
74 #define need_GL_EXT_blend_equation_separate
75 #define need_GL_EXT_blend_func_separate
76 #define need_GL_EXT_blend_minmax
77 #define need_GL_EXT_cull_vertex
78 #define need_GL_EXT_fog_coord
79 #define need_GL_EXT_multi_draw_arrays
80 #define need_GL_EXT_secondary_color
81 #include "extension_helper.h"
82
83 #ifndef VERBOSE
84 int VERBOSE = 0;
85 #endif
86
87 #if DEBUG_LOCKING
88 char *prevLockFile;
89 int prevLockLine;
90 #endif
91
92 /***************************************
93 * Mesa's Driver Functions
94 ***************************************/
95
96 #define DRIVER_VERSION "4.1.3002"
97
98 static const GLubyte *intelGetString( GLcontext *ctx, GLenum name )
99 {
100 const char * chipset;
101 static char buffer[128];
102
103 switch (name) {
104 case GL_VENDOR:
105 return (GLubyte *)"Tungsten Graphics, Inc";
106 break;
107
108 case GL_RENDERER:
109 switch (intel_context(ctx)->intelScreen->deviceID) {
110 case PCI_CHIP_I965_Q:
111 chipset = "Intel(R) 965Q"; break;
112 break;
113 case PCI_CHIP_I965_G:
114 case PCI_CHIP_I965_G_1:
115 chipset = "Intel(R) 965G"; break;
116 break;
117 case PCI_CHIP_I946_GZ:
118 chipset = "Intel(R) 946GZ"; break;
119 break;
120 default:
121 chipset = "Unknown Intel Chipset"; break;
122 }
123
124 (void) driGetRendererString( buffer, chipset, DRIVER_VERSION, 0 );
125 return (GLubyte *) buffer;
126
127 default:
128 return NULL;
129 }
130 }
131
132
133 /**
134 * Extension strings exported by the intel driver.
135 *
136 * \note
137 * It appears that ARB_texture_env_crossbar has "disappeared" compared to the
138 * old i830-specific driver.
139 */
140 const struct dri_extension card_extensions[] =
141 {
142 { "GL_ARB_multisample", GL_ARB_multisample_functions },
143 { "GL_ARB_multitexture", NULL },
144 { "GL_ARB_point_parameters", GL_ARB_point_parameters_functions },
145 { "GL_ARB_texture_border_clamp", NULL },
146 { "GL_ARB_texture_compression", GL_ARB_texture_compression_functions },
147 { "GL_ARB_texture_cube_map", NULL },
148 { "GL_ARB_texture_env_add", NULL },
149 { "GL_ARB_texture_env_combine", NULL },
150 { "GL_ARB_texture_env_dot3", NULL },
151 { "GL_ARB_texture_mirrored_repeat", NULL },
152 { "GL_ARB_texture_non_power_of_two", NULL },
153 { "GL_ARB_texture_rectangle", NULL },
154 { "GL_NV_texture_rectangle", NULL },
155 { "GL_EXT_texture_rectangle", NULL },
156 { "GL_ARB_texture_rectangle", NULL },
157 { "GL_ARB_vertex_buffer_object", GL_ARB_vertex_buffer_object_functions },
158 { "GL_ARB_vertex_program", GL_ARB_vertex_program_functions },
159 { "GL_ARB_window_pos", GL_ARB_window_pos_functions },
160 { "GL_EXT_blend_color", GL_EXT_blend_color_functions },
161 { "GL_EXT_blend_equation_separate", GL_EXT_blend_equation_separate_functions },
162 { "GL_EXT_blend_func_separate", GL_EXT_blend_func_separate_functions },
163 { "GL_EXT_blend_minmax", GL_EXT_blend_minmax_functions },
164 { "GL_EXT_blend_logic_op", NULL },
165 { "GL_EXT_blend_subtract", NULL },
166 { "GL_EXT_cull_vertex", GL_EXT_cull_vertex_functions },
167 { "GL_EXT_fog_coord", GL_EXT_fog_coord_functions },
168 { "GL_EXT_multi_draw_arrays", GL_EXT_multi_draw_arrays_functions },
169 { "GL_EXT_secondary_color", GL_EXT_secondary_color_functions },
170 { "GL_EXT_stencil_wrap", NULL },
171 { "GL_EXT_texture_edge_clamp", NULL },
172 { "GL_EXT_texture_env_combine", NULL },
173 { "GL_EXT_texture_env_dot3", NULL },
174 { "GL_EXT_texture_filter_anisotropic", NULL },
175 { "GL_EXT_texture_lod_bias", NULL },
176 { "GL_3DFX_texture_compression_FXT1", NULL },
177 { "GL_APPLE_client_storage", NULL },
178 { "GL_MESA_pack_invert", NULL },
179 { "GL_MESA_ycbcr_texture", NULL },
180 { "GL_NV_blend_square", NULL },
181 { "GL_SGIS_generate_mipmap", NULL },
182 { NULL, NULL }
183 };
184
185
186
187 static const struct dri_debug_control debug_control[] =
188 {
189 { "fall", DEBUG_FALLBACKS },
190 { "tex", DEBUG_TEXTURE },
191 { "ioctl", DEBUG_IOCTL },
192 { "prim", DEBUG_PRIMS },
193 { "vert", DEBUG_VERTS },
194 { "state", DEBUG_STATE },
195 { "verb", DEBUG_VERBOSE },
196 { "dri", DEBUG_DRI },
197 { "dma", DEBUG_DMA },
198 { "san", DEBUG_SANITY },
199 { "sync", DEBUG_SYNC },
200 { "sleep", DEBUG_SLEEP },
201 { "pix", DEBUG_PIXEL },
202 { "buf", DEBUG_BUFMGR },
203 { "stats", DEBUG_STATS },
204 { "tile", DEBUG_TILE },
205 { "sing", DEBUG_SINGLE_THREAD },
206 { "thre", DEBUG_SINGLE_THREAD },
207 { "wm", DEBUG_WM },
208 { "vs", DEBUG_VS },
209 { NULL, 0 }
210 };
211
212
213 static void intelInvalidateState( GLcontext *ctx, GLuint new_state )
214 {
215 struct intel_context *intel = intel_context(ctx);
216
217 _swrast_InvalidateState( ctx, new_state );
218 _swsetup_InvalidateState( ctx, new_state );
219 _vbo_InvalidateState( ctx, new_state );
220 _tnl_InvalidateState( ctx, new_state );
221 _tnl_invalidate_vertex_state( ctx, new_state );
222
223 intel->NewGLState |= new_state;
224
225 if (intel->vtbl.invalidate_state)
226 intel->vtbl.invalidate_state( intel, new_state );
227 }
228
229
230 void intelFlush( GLcontext *ctx )
231 {
232 struct intel_context *intel = intel_context( ctx );
233
234 bmLockAndFence(intel);
235 }
236
237 void intelFinish( GLcontext *ctx )
238 {
239 struct intel_context *intel = intel_context( ctx );
240
241 bmFinishFence(intel, bmLockAndFence(intel));
242 }
243
244
245 void intelInitDriverFunctions( struct dd_function_table *functions )
246 {
247 _mesa_init_driver_functions( functions );
248
249 functions->Flush = intelFlush;
250 functions->Finish = intelFinish;
251 functions->GetString = intelGetString;
252 functions->UpdateState = intelInvalidateState;
253
254 /* CopyPixels can be accelerated even with the current memory
255 * manager:
256 */
257 if (!getenv("INTEL_NO_BLIT")) {
258 functions->CopyPixels = intelCopyPixels;
259 functions->Bitmap = intelBitmap;
260 }
261
262 intelInitTextureFuncs( functions );
263 intelInitStateFuncs( functions );
264 intelInitBufferFuncs( functions );
265 }
266
267
268
269 GLboolean intelInitContext( struct intel_context *intel,
270 const __GLcontextModes *mesaVis,
271 __DRIcontextPrivate *driContextPriv,
272 void *sharedContextPrivate,
273 struct dd_function_table *functions )
274 {
275 GLcontext *ctx = &intel->ctx;
276 GLcontext *shareCtx = (GLcontext *) sharedContextPrivate;
277 __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
278 intelScreenPrivate *intelScreen = (intelScreenPrivate *)sPriv->private;
279 volatile drmI830Sarea *saPriv = (volatile drmI830Sarea *)
280 (((GLubyte *)sPriv->pSAREA)+intelScreen->sarea_priv_offset);
281
282 if (!_mesa_initialize_context(&intel->ctx,
283 mesaVis, shareCtx,
284 functions,
285 (void*) intel)) {
286 _mesa_printf("%s: failed to init mesa context\n", __FUNCTION__);
287 return GL_FALSE;
288 }
289
290 driContextPriv->driverPrivate = intel;
291 intel->intelScreen = intelScreen;
292 intel->driScreen = sPriv;
293 intel->sarea = saPriv;
294
295
296 ctx->Const.MaxTextureMaxAnisotropy = 2.0;
297
298 if (getenv("INTEL_STRICT_CONFORMANCE")) {
299 intel->strict_conformance = 1;
300 }
301
302 if (intel->strict_conformance) {
303 ctx->Const.MinLineWidth = 1.0;
304 ctx->Const.MinLineWidthAA = 1.0;
305 ctx->Const.MaxLineWidth = 1.0;
306 ctx->Const.MaxLineWidthAA = 1.0;
307 ctx->Const.LineWidthGranularity = 1.0;
308 }
309 else {
310 ctx->Const.MinLineWidth = 1.0;
311 ctx->Const.MinLineWidthAA = 1.0;
312 ctx->Const.MaxLineWidth = 5.0;
313 ctx->Const.MaxLineWidthAA = 5.0;
314 ctx->Const.LineWidthGranularity = 0.5;
315 }
316
317 ctx->Const.MinPointSize = 1.0;
318 ctx->Const.MinPointSizeAA = 1.0;
319 ctx->Const.MaxPointSize = 255.0;
320 ctx->Const.MaxPointSizeAA = 3.0;
321 ctx->Const.PointSizeGranularity = 1.0;
322
323 /* Initialize the software rasterizer and helper modules. */
324 _swrast_CreateContext( ctx );
325 _vbo_CreateContext( ctx );
326 _tnl_CreateContext( ctx );
327 _swsetup_CreateContext( ctx );
328
329 TNL_CONTEXT(ctx)->Driver.RunPipeline = _tnl_run_pipeline;
330
331 /* Configure swrast to match hardware characteristics: */
332 _swrast_allow_pixel_fog( ctx, GL_FALSE );
333 _swrast_allow_vertex_fog( ctx, GL_TRUE );
334
335 /* Dri stuff */
336 intel->hHWContext = driContextPriv->hHWContext;
337 intel->driFd = sPriv->fd;
338 intel->driHwLock = (drmLock *) &sPriv->pSAREA->lock;
339
340 intel->hw_stencil = mesaVis->stencilBits && mesaVis->depthBits == 24;
341 intel->hw_stipple = 1;
342
343 switch(mesaVis->depthBits) {
344 case 0: /* what to do in this case? */
345 case 16:
346 intel->depth_scale = 1.0/0xffff;
347 intel->polygon_offset_scale = 1.0/0xffff;
348 intel->depth_clear_mask = ~0;
349 intel->ClearDepth = 0xffff;
350 break;
351 case 24:
352 intel->depth_scale = 1.0/0xffffff;
353 intel->polygon_offset_scale = 2.0/0xffffff; /* req'd to pass glean */
354 intel->depth_clear_mask = 0x00ffffff;
355 intel->stencil_clear_mask = 0xff000000;
356 intel->ClearDepth = 0x00ffffff;
357 break;
358 default:
359 assert(0);
360 break;
361 }
362
363 /* Initialize swrast, tnl driver tables: */
364 intelInitSpanFuncs( ctx );
365
366 intel->no_hw = getenv("INTEL_NO_HW") != NULL;
367
368 if (!intel->intelScreen->irq_active) {
369 _mesa_printf("IRQs not active. Exiting\n");
370 exit(1);
371 }
372
373 driInitExtensions( ctx, card_extensions,
374 GL_TRUE );
375
376 INTEL_DEBUG = driParseDebugString( getenv( "INTEL_DEBUG" ),
377 debug_control );
378
379
380 /* Buffer manager:
381 */
382 intel->bm = bm_fake_intel_Attach( intel );
383
384
385 bmInitPool(intel,
386 intel->intelScreen->tex.offset, /* low offset */
387 intel->intelScreen->tex.map, /* low virtual */
388 intel->intelScreen->tex.size,
389 BM_MEM_AGP);
390
391 /* These are still static, but create regions for them.
392 */
393 intel->front_region =
394 intel_region_create_static(intel,
395 BM_MEM_AGP,
396 intelScreen->front.offset,
397 intelScreen->front.map,
398 intelScreen->cpp,
399 intelScreen->front.pitch / intelScreen->cpp,
400 intelScreen->height,
401 GL_FALSE);
402
403
404 intel->back_region =
405 intel_region_create_static(intel,
406 BM_MEM_AGP,
407 intelScreen->back.offset,
408 intelScreen->back.map,
409 intelScreen->cpp,
410 intelScreen->back.pitch / intelScreen->cpp,
411 intelScreen->height,
412 (INTEL_DEBUG & DEBUG_TILE) ? 0 : 1);
413
414 /* Still assuming front.cpp == depth.cpp
415 *
416 * XXX: Setting tiling to false because Depth tiling only supports
417 * YMAJOR but the blitter only supports XMAJOR tiling. Have to
418 * resolve later.
419 */
420 intel->depth_region =
421 intel_region_create_static(intel,
422 BM_MEM_AGP,
423 intelScreen->depth.offset,
424 intelScreen->depth.map,
425 intelScreen->cpp,
426 intelScreen->depth.pitch / intelScreen->cpp,
427 intelScreen->height,
428 (INTEL_DEBUG & DEBUG_TILE) ? 0 : 1);
429
430 intel_bufferobj_init( intel );
431 intel->batch = intel_batchbuffer_alloc( intel );
432
433 if (intel->ctx.Mesa_DXTn) {
434 _mesa_enable_extension( ctx, "GL_EXT_texture_compression_s3tc" );
435 _mesa_enable_extension( ctx, "GL_S3_s3tc" );
436 }
437 else if (driQueryOptionb (&intelScreen->optionCache, "force_s3tc_enable")) {
438 _mesa_enable_extension( ctx, "GL_EXT_texture_compression_s3tc" );
439 }
440
441 /* driInitTextureObjects( ctx, & intel->swapped, */
442 /* DRI_TEXMGR_DO_TEXTURE_1D | */
443 /* DRI_TEXMGR_DO_TEXTURE_2D | */
444 /* DRI_TEXMGR_DO_TEXTURE_RECT ); */
445
446
447 if (getenv("INTEL_NO_RAST")) {
448 fprintf(stderr, "disabling 3D rasterization\n");
449 intel->no_rast = 1;
450 }
451
452
453 return GL_TRUE;
454 }
455
456 void intelDestroyContext(__DRIcontextPrivate *driContextPriv)
457 {
458 struct intel_context *intel = (struct intel_context *) driContextPriv->driverPrivate;
459
460 assert(intel); /* should never be null */
461 if (intel) {
462 GLboolean release_texture_heaps;
463
464
465 intel->vtbl.destroy( intel );
466
467 release_texture_heaps = (intel->ctx.Shared->RefCount == 1);
468 _swsetup_DestroyContext (&intel->ctx);
469 _tnl_DestroyContext (&intel->ctx);
470 _vbo_DestroyContext (&intel->ctx);
471
472 _swrast_DestroyContext (&intel->ctx);
473 intel->Fallback = 0; /* don't call _swrast_Flush later */
474 intel_batchbuffer_free(intel->batch);
475 intel->batch = NULL;
476
477
478 if ( release_texture_heaps ) {
479 /* This share group is about to go away, free our private
480 * texture object data.
481 */
482
483 /* XXX: destroy the shared bufmgr struct here?
484 */
485 }
486
487 /* Free the regions created to describe front/back/depth
488 * buffers:
489 */
490 #if 0
491 intel_region_release(intel, &intel->front_region);
492 intel_region_release(intel, &intel->back_region);
493 intel_region_release(intel, &intel->depth_region);
494 intel_region_release(intel, &intel->draw_region);
495 #endif
496
497 /* free the Mesa context */
498 _mesa_destroy_context(&intel->ctx);
499 }
500
501 driContextPriv->driverPrivate = NULL;
502 }
503
504 GLboolean intelUnbindContext(__DRIcontextPrivate *driContextPriv)
505 {
506 return GL_TRUE;
507 }
508
509 GLboolean intelMakeCurrent(__DRIcontextPrivate *driContextPriv,
510 __DRIdrawablePrivate *driDrawPriv,
511 __DRIdrawablePrivate *driReadPriv)
512 {
513
514 if (driContextPriv) {
515 struct intel_context *intel = (struct intel_context *) driContextPriv->driverPrivate;
516
517 if ( intel->driDrawable != driDrawPriv ) {
518 /* Shouldn't the readbuffer be stored also? */
519 intel->driDrawable = driDrawPriv;
520 intelWindowMoved( intel );
521 }
522
523 _mesa_make_current(&intel->ctx,
524 (GLframebuffer *) driDrawPriv->driverPrivate,
525 (GLframebuffer *) driReadPriv->driverPrivate);
526
527 intel->ctx.Driver.DrawBuffer( &intel->ctx, intel->ctx.Color.DrawBuffer[0] );
528 } else {
529 _mesa_make_current(NULL, NULL, NULL);
530 }
531
532 return GL_TRUE;
533 }
534
535
536 static void intelContendedLock( struct intel_context *intel, GLuint flags )
537 {
538 __DRIdrawablePrivate *dPriv = intel->driDrawable;
539 __DRIscreenPrivate *sPriv = intel->driScreen;
540 volatile drmI830Sarea * sarea = intel->sarea;
541 int me = intel->hHWContext;
542 int my_bufmgr = bmCtxId(intel);
543
544 drmGetLock(intel->driFd, intel->hHWContext, flags);
545
546 /* If the window moved, may need to set a new cliprect now.
547 *
548 * NOTE: This releases and regains the hw lock, so all state
549 * checking must be done *after* this call:
550 */
551 if (dPriv)
552 DRI_VALIDATE_DRAWABLE_INFO(sPriv, dPriv);
553
554
555 intel->locked = 1;
556 intel->need_flush = 1;
557
558 /* Lost context?
559 */
560 if (sarea->ctxOwner != me) {
561 DBG("Lost Context: sarea->ctxOwner %x me %x\n", sarea->ctxOwner, me);
562 sarea->ctxOwner = me;
563 intel->vtbl.lost_hardware( intel );
564 }
565
566 /* As above, but don't evict the texture data on transitions
567 * between contexts which all share a local buffer manager.
568 */
569 if (sarea->texAge != my_bufmgr) {
570 DBG("Lost Textures: sarea->texAge %x my_bufmgr %x\n", sarea->ctxOwner, my_bufmgr);
571 sarea->texAge = my_bufmgr;
572 bm_fake_NotifyContendedLockTake( intel );
573 }
574
575 /* Drawable changed?
576 */
577 if (dPriv && intel->lastStamp != dPriv->lastStamp) {
578 intelWindowMoved( intel );
579 intel->lastStamp = dPriv->lastStamp;
580 }
581 }
582
583 _glthread_DECLARE_STATIC_MUTEX(lockMutex);
584
585 /* Lock the hardware and validate our state.
586 */
587 void LOCK_HARDWARE( struct intel_context *intel )
588 {
589 char __ret=0;
590
591 _glthread_LOCK_MUTEX(lockMutex);
592 assert(!intel->locked);
593
594
595 DRM_CAS(intel->driHwLock, intel->hHWContext,
596 (DRM_LOCK_HELD|intel->hHWContext), __ret);
597 if (__ret)
598 intelContendedLock( intel, 0 );
599
600 intel->locked = 1;
601
602 if (intel->aub_wrap) {
603 bm_fake_NotifyContendedLockTake( intel );
604 intel->vtbl.lost_hardware( intel );
605 intel->vtbl.aub_wrap(intel);
606 intel->aub_wrap = 0;
607 }
608
609 if (bmError(intel)) {
610 bmEvictAll(intel);
611 intel->vtbl.lost_hardware( intel );
612 }
613
614 /* Make sure nothing has been emitted prior to getting the lock:
615 */
616 assert(intel->batch->map == 0);
617
618 /* XXX: postpone, may not be needed:
619 */
620 if (!intel_batchbuffer_map(intel->batch)) {
621 bmEvictAll(intel);
622 intel->vtbl.lost_hardware( intel );
623
624 /* This could only fail if the batchbuffer was greater in size
625 * than the available texture memory:
626 */
627 if (!intel_batchbuffer_map(intel->batch)) {
628 _mesa_printf("double failure to map batchbuffer\n");
629 assert(0);
630 }
631 }
632 }
633
634
635 /* Unlock the hardware using the global current context
636 */
637 void UNLOCK_HARDWARE( struct intel_context *intel )
638 {
639 /* Make sure everything has been released:
640 */
641 assert(intel->batch->ptr == intel->batch->map + intel->batch->offset);
642
643 intel_batchbuffer_unmap(intel->batch);
644 intel->vtbl.note_unlock( intel );
645 intel->locked = 0;
646
647
648
649 DRM_UNLOCK(intel->driFd, intel->driHwLock, intel->hHWContext);
650 _glthread_UNLOCK_MUTEX(lockMutex);
651 }
652