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