intel_context.c revision d282128ff68cc58bc3f5b808031c5fe7325bd69b
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 "main/glheader.h"
30#include "main/context.h"
31#include "main/arrayobj.h"
32#include "main/extensions.h"
33#include "main/framebuffer.h"
34#include "main/imports.h"
35#include "main/points.h"
36
37#include "swrast/swrast.h"
38#include "swrast_setup/swrast_setup.h"
39#include "tnl/tnl.h"
40#include "drivers/common/driverfuncs.h"
41#include "drivers/common/meta.h"
42
43#include "i830_dri.h"
44
45#include "intel_chipset.h"
46#include "intel_buffers.h"
47#include "intel_tex.h"
48#include "intel_batchbuffer.h"
49#include "intel_clear.h"
50#include "intel_extensions.h"
51#include "intel_pixel.h"
52#include "intel_regions.h"
53#include "intel_buffer_objects.h"
54#include "intel_fbo.h"
55#include "intel_decode.h"
56#include "intel_bufmgr.h"
57#include "intel_screen.h"
58
59#include "drirenderbuffer.h"
60#include "utils.h"
61#include "xmlpool.h"            /* for symbolic values of enum-type options */
62
63
64#ifndef INTEL_DEBUG
65int INTEL_DEBUG = (0);
66#endif
67
68
69#define DRIVER_DATE                     "20091221 DEVELOPMENT"
70#define DRIVER_DATE_GEM                 "GEM " DRIVER_DATE
71
72
73static void intel_flush(GLcontext *ctx, GLboolean needs_mi_flush);
74
75static const GLubyte *
76intelGetString(GLcontext * ctx, GLenum name)
77{
78   const struct intel_context *const intel = intel_context(ctx);
79   const char *chipset;
80   static char buffer[128];
81
82   switch (name) {
83   case GL_VENDOR:
84      return (GLubyte *) "Tungsten Graphics, Inc";
85      break;
86
87   case GL_RENDERER:
88      switch (intel->intelScreen->deviceID) {
89      case PCI_CHIP_845_G:
90         chipset = "Intel(R) 845G";
91         break;
92      case PCI_CHIP_I830_M:
93         chipset = "Intel(R) 830M";
94         break;
95      case PCI_CHIP_I855_GM:
96         chipset = "Intel(R) 852GM/855GM";
97         break;
98      case PCI_CHIP_I865_G:
99         chipset = "Intel(R) 865G";
100         break;
101      case PCI_CHIP_I915_G:
102         chipset = "Intel(R) 915G";
103         break;
104      case PCI_CHIP_E7221_G:
105	 chipset = "Intel (R) E7221G (i915)";
106	 break;
107      case PCI_CHIP_I915_GM:
108         chipset = "Intel(R) 915GM";
109         break;
110      case PCI_CHIP_I945_G:
111         chipset = "Intel(R) 945G";
112         break;
113      case PCI_CHIP_I945_GM:
114         chipset = "Intel(R) 945GM";
115         break;
116      case PCI_CHIP_I945_GME:
117         chipset = "Intel(R) 945GME";
118         break;
119      case PCI_CHIP_G33_G:
120	 chipset = "Intel(R) G33";
121	 break;
122      case PCI_CHIP_Q35_G:
123	 chipset = "Intel(R) Q35";
124	 break;
125      case PCI_CHIP_Q33_G:
126	 chipset = "Intel(R) Q33";
127	 break;
128      case PCI_CHIP_IGD_GM:
129      case PCI_CHIP_IGD_G:
130	 chipset = "Intel(R) IGD";
131	 break;
132      case PCI_CHIP_I965_Q:
133	 chipset = "Intel(R) 965Q";
134	 break;
135      case PCI_CHIP_I965_G:
136      case PCI_CHIP_I965_G_1:
137	 chipset = "Intel(R) 965G";
138	 break;
139      case PCI_CHIP_I946_GZ:
140	 chipset = "Intel(R) 946GZ";
141	 break;
142      case PCI_CHIP_I965_GM:
143	 chipset = "Intel(R) 965GM";
144	 break;
145      case PCI_CHIP_I965_GME:
146	 chipset = "Intel(R) 965GME/GLE";
147	 break;
148      case PCI_CHIP_GM45_GM:
149	 chipset = "Mobile Intel® GM45 Express Chipset";
150	 break;
151      case PCI_CHIP_IGD_E_G:
152	 chipset = "Intel(R) Integrated Graphics Device";
153	 break;
154      case PCI_CHIP_G45_G:
155         chipset = "Intel(R) G45/G43";
156         break;
157      case PCI_CHIP_Q45_G:
158         chipset = "Intel(R) Q45/Q43";
159         break;
160      case PCI_CHIP_G41_G:
161         chipset = "Intel(R) G41";
162         break;
163      case PCI_CHIP_B43_G:
164         chipset = "Intel(R) B43";
165         break;
166      case PCI_CHIP_ILD_G:
167         chipset = "Intel(R) IGDNG_D";
168         break;
169      case PCI_CHIP_ILM_G:
170         chipset = "Intel(R) IGDNG_M";
171         break;
172      default:
173         chipset = "Unknown Intel Chipset";
174         break;
175      }
176
177      (void) driGetRendererString(buffer, chipset, DRIVER_DATE_GEM, 0);
178      return (GLubyte *) buffer;
179
180   default:
181      return NULL;
182   }
183}
184
185static unsigned
186intel_bits_per_pixel(const struct intel_renderbuffer *rb)
187{
188   return _mesa_get_format_bytes(rb->Base.Format) * 8;
189}
190
191void
192intel_update_renderbuffers(__DRIcontext *context, __DRIdrawable *drawable)
193{
194   struct gl_framebuffer *fb = drawable->driverPrivate;
195   struct intel_renderbuffer *rb;
196   struct intel_region *region, *depth_region;
197   struct intel_context *intel = context->driverPrivate;
198   struct intel_renderbuffer *front_rb, *back_rb, *depth_rb, *stencil_rb;
199   __DRIbuffer *buffers = NULL;
200   __DRIscreen *screen;
201   int i, count;
202   unsigned int attachments[10];
203   uint32_t name;
204   const char *region_name;
205
206   if (INTEL_DEBUG & DEBUG_DRI)
207      fprintf(stderr, "enter %s, drawable %p\n", __func__, drawable);
208
209   screen = intel->intelScreen->driScrnPriv;
210
211   if (screen->dri2.loader
212       && (screen->dri2.loader->base.version > 2)
213       && (screen->dri2.loader->getBuffersWithFormat != NULL)) {
214
215      front_rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
216      back_rb = intel_get_renderbuffer(fb, BUFFER_BACK_LEFT);
217      depth_rb = intel_get_renderbuffer(fb, BUFFER_DEPTH);
218      stencil_rb = intel_get_renderbuffer(fb, BUFFER_STENCIL);
219
220      i = 0;
221      if ((intel->is_front_buffer_rendering ||
222	   intel->is_front_buffer_reading ||
223	   !back_rb) && front_rb) {
224	 attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
225	 attachments[i++] = intel_bits_per_pixel(front_rb);
226      }
227
228      if (back_rb) {
229	 attachments[i++] = __DRI_BUFFER_BACK_LEFT;
230	 attachments[i++] = intel_bits_per_pixel(back_rb);
231      }
232
233      if ((depth_rb != NULL) && (stencil_rb != NULL)) {
234	 attachments[i++] = __DRI_BUFFER_DEPTH_STENCIL;
235	 attachments[i++] = intel_bits_per_pixel(depth_rb);
236      } else if (depth_rb != NULL) {
237	 attachments[i++] = __DRI_BUFFER_DEPTH;
238	 attachments[i++] = intel_bits_per_pixel(depth_rb);
239      } else if (stencil_rb != NULL) {
240	 attachments[i++] = __DRI_BUFFER_STENCIL;
241	 attachments[i++] = intel_bits_per_pixel(stencil_rb);
242      }
243
244      buffers =
245	 (*screen->dri2.loader->getBuffersWithFormat)(drawable,
246						      &drawable->w,
247						      &drawable->h,
248						      attachments, i / 2,
249						      &count,
250						      drawable->loaderPrivate);
251   } else if (screen->dri2.loader) {
252      i = 0;
253      if (intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT))
254	 attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
255      if (intel_get_renderbuffer(fb, BUFFER_BACK_LEFT))
256	 attachments[i++] = __DRI_BUFFER_BACK_LEFT;
257      if (intel_get_renderbuffer(fb, BUFFER_DEPTH))
258	 attachments[i++] = __DRI_BUFFER_DEPTH;
259      if (intel_get_renderbuffer(fb, BUFFER_STENCIL))
260	 attachments[i++] = __DRI_BUFFER_STENCIL;
261
262      buffers = (*screen->dri2.loader->getBuffers)(drawable,
263						   &drawable->w,
264						   &drawable->h,
265						   attachments, i,
266						   &count,
267						   drawable->loaderPrivate);
268   }
269
270   if (buffers == NULL)
271      return;
272
273   drawable->x = 0;
274   drawable->y = 0;
275   drawable->backX = 0;
276   drawable->backY = 0;
277   drawable->numClipRects = 1;
278   drawable->pClipRects[0].x1 = 0;
279   drawable->pClipRects[0].y1 = 0;
280   drawable->pClipRects[0].x2 = drawable->w;
281   drawable->pClipRects[0].y2 = drawable->h;
282   drawable->numBackClipRects = 1;
283   drawable->pBackClipRects[0].x1 = 0;
284   drawable->pBackClipRects[0].y1 = 0;
285   drawable->pBackClipRects[0].x2 = drawable->w;
286   drawable->pBackClipRects[0].y2 = drawable->h;
287
288   depth_region = NULL;
289   for (i = 0; i < count; i++) {
290       switch (buffers[i].attachment) {
291       case __DRI_BUFFER_FRONT_LEFT:
292	   rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
293	   region_name = "dri2 front buffer";
294	   break;
295
296       case __DRI_BUFFER_FAKE_FRONT_LEFT:
297	   rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
298	   region_name = "dri2 fake front buffer";
299	   break;
300
301       case __DRI_BUFFER_BACK_LEFT:
302	   rb = intel_get_renderbuffer(fb, BUFFER_BACK_LEFT);
303	   region_name = "dri2 back buffer";
304	   break;
305
306       case __DRI_BUFFER_DEPTH:
307	   rb = intel_get_renderbuffer(fb, BUFFER_DEPTH);
308	   region_name = "dri2 depth buffer";
309	   break;
310
311       case __DRI_BUFFER_DEPTH_STENCIL:
312	   rb = intel_get_renderbuffer(fb, BUFFER_DEPTH);
313	   region_name = "dri2 depth / stencil buffer";
314	   break;
315
316       case __DRI_BUFFER_STENCIL:
317	   rb = intel_get_renderbuffer(fb, BUFFER_STENCIL);
318	   region_name = "dri2 stencil buffer";
319	   break;
320
321       case __DRI_BUFFER_ACCUM:
322       default:
323	   fprintf(stderr,
324		   "unhandled buffer attach event, attacment type %d\n",
325		   buffers[i].attachment);
326	   return;
327       }
328
329       if (rb == NULL)
330	  continue;
331
332       if (rb->region) {
333	  dri_bo_flink(rb->region->buffer, &name);
334	  if (name == buffers[i].name)
335	     continue;
336       }
337
338       if (INTEL_DEBUG & DEBUG_DRI)
339	  fprintf(stderr,
340		  "attaching buffer %d, at %d, cpp %d, pitch %d\n",
341		  buffers[i].name, buffers[i].attachment,
342		  buffers[i].cpp, buffers[i].pitch);
343
344       if (buffers[i].attachment == __DRI_BUFFER_STENCIL && depth_region) {
345	  if (INTEL_DEBUG & DEBUG_DRI)
346	     fprintf(stderr, "(reusing depth buffer as stencil)\n");
347	  intel_region_reference(&region, depth_region);
348       }
349       else
350          region = intel_region_alloc_for_handle(intel, buffers[i].cpp,
351						 drawable->w,
352						 drawable->h,
353						 buffers[i].pitch / buffers[i].cpp,
354						 buffers[i].name,
355						 region_name);
356
357       if (buffers[i].attachment == __DRI_BUFFER_DEPTH)
358	  depth_region = region;
359
360       intel_renderbuffer_set_region(rb, region);
361       intel_region_release(&region);
362
363       if (buffers[i].attachment == __DRI_BUFFER_DEPTH_STENCIL) {
364	  rb = intel_get_renderbuffer(fb, BUFFER_STENCIL);
365	  if (rb != NULL) {
366	     struct intel_region *stencil_region = NULL;
367
368	     if (rb->region) {
369		dri_bo_flink(rb->region->buffer, &name);
370		if (name == buffers[i].name)
371		   continue;
372	     }
373
374	     intel_region_reference(&stencil_region, region);
375	     intel_renderbuffer_set_region(rb, stencil_region);
376	     intel_region_release(&stencil_region);
377	  }
378       }
379   }
380
381   driUpdateFramebufferSize(&intel->ctx, drawable);
382}
383
384void
385intel_viewport(GLcontext *ctx, GLint x, GLint y, GLsizei w, GLsizei h)
386{
387    struct intel_context *intel = intel_context(ctx);
388    __DRIcontext *driContext = intel->driContext;
389    void (*old_viewport)(GLcontext *ctx, GLint x, GLint y,
390			 GLsizei w, GLsizei h);
391
392    if (!driContext->driScreenPriv->dri2.enabled)
393	return;
394
395    if (!intel->meta.internal_viewport_call && ctx->DrawBuffer->Name == 0) {
396       /* If we're rendering to the fake front buffer, make sure all the pending
397	* drawing has landed on the real front buffer.  Otherwise when we
398	* eventually get to DRI2GetBuffersWithFormat the stale real front
399	* buffer contents will get copied to the new fake front buffer.
400	*/
401       if (intel->is_front_buffer_rendering) {
402	  intel_flush(ctx, GL_FALSE);
403       }
404
405       intel_update_renderbuffers(driContext, driContext->driDrawablePriv);
406       if (driContext->driDrawablePriv != driContext->driReadablePriv)
407	  intel_update_renderbuffers(driContext, driContext->driReadablePriv);
408    }
409
410    old_viewport = ctx->Driver.Viewport;
411    ctx->Driver.Viewport = NULL;
412    intel->driDrawable = driContext->driDrawablePriv;
413    intel_draw_buffer(ctx, intel->ctx.DrawBuffer);
414    ctx->Driver.Viewport = old_viewport;
415}
416
417
418static const struct dri_debug_control debug_control[] = {
419   { "tex",   DEBUG_TEXTURE},
420   { "state", DEBUG_STATE},
421   { "ioctl", DEBUG_IOCTL},
422   { "blit",  DEBUG_BLIT},
423   { "mip",   DEBUG_MIPTREE},
424   { "fall",  DEBUG_FALLBACKS},
425   { "verb",  DEBUG_VERBOSE},
426   { "bat",   DEBUG_BATCH},
427   { "pix",   DEBUG_PIXEL},
428   { "buf",   DEBUG_BUFMGR},
429   { "reg",   DEBUG_REGION},
430   { "fbo",   DEBUG_FBO},
431   { "lock",  DEBUG_LOCK},
432   { "sync",  DEBUG_SYNC},
433   { "prim",  DEBUG_PRIMS },
434   { "vert",  DEBUG_VERTS },
435   { "dri",   DEBUG_DRI },
436   { "dma",   DEBUG_DMA },
437   { "san",   DEBUG_SANITY },
438   { "sleep", DEBUG_SLEEP },
439   { "stats", DEBUG_STATS },
440   { "tile",  DEBUG_TILE },
441   { "sing",  DEBUG_SINGLE_THREAD },
442   { "thre",  DEBUG_SINGLE_THREAD },
443   { "wm",    DEBUG_WM },
444   { "urb",   DEBUG_URB },
445   { "vs",    DEBUG_VS },
446   { NULL,    0 }
447};
448
449
450static void
451intelInvalidateState(GLcontext * ctx, GLuint new_state)
452{
453    struct intel_context *intel = intel_context(ctx);
454
455   _swrast_InvalidateState(ctx, new_state);
456   _swsetup_InvalidateState(ctx, new_state);
457   _vbo_InvalidateState(ctx, new_state);
458   _tnl_InvalidateState(ctx, new_state);
459   _tnl_invalidate_vertex_state(ctx, new_state);
460
461   intel->NewGLState |= new_state;
462
463   if (intel->vtbl.invalidate_state)
464      intel->vtbl.invalidate_state( intel, new_state );
465}
466
467static void
468intel_flush(GLcontext *ctx, GLboolean needs_mi_flush)
469{
470   struct intel_context *intel = intel_context(ctx);
471
472   if (intel->Fallback)
473      _swrast_flush(ctx);
474
475   if (intel->gen < 4)
476      INTEL_FIREVERTICES(intel);
477
478   /* Emit a flush so that any frontbuffer rendering that might have occurred
479    * lands onscreen in a timely manner, even if the X Server doesn't trigger
480    * a flush for us.
481    */
482   if (!intel->driScreen->dri2.enabled && needs_mi_flush)
483      intel_batchbuffer_emit_mi_flush(intel->batch);
484
485   if (intel->batch->map != intel->batch->ptr)
486      intel_batchbuffer_flush(intel->batch);
487
488   if ((ctx->DrawBuffer->Name == 0) && intel->front_buffer_dirty) {
489      __DRIscreen *const screen = intel->intelScreen->driScrnPriv;
490
491      if (screen->dri2.loader &&
492          (screen->dri2.loader->base.version >= 2)
493	  && (screen->dri2.loader->flushFrontBuffer != NULL) &&
494          intel->driDrawable && intel->driDrawable->loaderPrivate) {
495	 (*screen->dri2.loader->flushFrontBuffer)(intel->driDrawable,
496						  intel->driDrawable->loaderPrivate);
497
498	 /* Only clear the dirty bit if front-buffer rendering is no longer
499	  * enabled.  This is done so that the dirty bit can only be set in
500	  * glDrawBuffer.  Otherwise the dirty bit would have to be set at
501	  * each of N places that do rendering.  This has worse performances,
502	  * but it is much easier to get correct.
503	  */
504	 if (!intel->is_front_buffer_rendering) {
505	    intel->front_buffer_dirty = GL_FALSE;
506	 }
507      }
508   }
509}
510
511void
512intelFlush(GLcontext * ctx)
513{
514   intel_flush(ctx, GL_FALSE);
515}
516
517static void
518intel_glFlush(GLcontext *ctx)
519{
520   struct intel_context *intel = intel_context(ctx);
521
522   intel_flush(ctx, GL_TRUE);
523
524   /* We're using glFlush as an indicator that a frame is done, which is
525    * what DRI2 does before calling SwapBuffers (and means we should catch
526    * people doing front-buffer rendering, as well)..
527    *
528    * Wait for the swapbuffers before the one we just emitted, so we don't
529    * get too many swaps outstanding for apps that are GPU-heavy but not
530    * CPU-heavy.
531    *
532    * Unfortunately, we don't have a handle to the batch containing the swap,
533    * and getting our hands on that doesn't seem worth it, so we just us the
534    * first batch we emitted after the last swap.
535    */
536   if (intel->first_post_swapbuffers_batch != NULL) {
537      drm_intel_bo_wait_rendering(intel->first_post_swapbuffers_batch);
538      drm_intel_bo_unreference(intel->first_post_swapbuffers_batch);
539      intel->first_post_swapbuffers_batch = NULL;
540   }
541}
542
543void
544intelFinish(GLcontext * ctx)
545{
546   struct gl_framebuffer *fb = ctx->DrawBuffer;
547   int i;
548
549   intelFlush(ctx);
550
551   for (i = 0; i < fb->_NumColorDrawBuffers; i++) {
552       struct intel_renderbuffer *irb;
553
554       irb = intel_renderbuffer(fb->_ColorDrawBuffers[i]);
555
556       if (irb && irb->region)
557	  dri_bo_wait_rendering(irb->region->buffer);
558   }
559   if (fb->_DepthBuffer) {
560      /* XXX: Wait on buffer idle */
561   }
562}
563
564void
565intelInitDriverFunctions(struct dd_function_table *functions)
566{
567   _mesa_init_driver_functions(functions);
568
569   functions->Flush = intel_glFlush;
570   functions->Finish = intelFinish;
571   functions->GetString = intelGetString;
572   functions->UpdateState = intelInvalidateState;
573
574   intelInitTextureFuncs(functions);
575   intelInitTextureImageFuncs(functions);
576   intelInitTextureSubImageFuncs(functions);
577   intelInitTextureCopyImageFuncs(functions);
578   intelInitStateFuncs(functions);
579   intelInitClearFuncs(functions);
580   intelInitBufferFuncs(functions);
581   intelInitPixelFuncs(functions);
582   intelInitBufferObjectFuncs(functions);
583   intel_init_syncobj_functions(functions);
584}
585
586
587GLboolean
588intelInitContext(struct intel_context *intel,
589                 const __GLcontextModes * mesaVis,
590                 __DRIcontext * driContextPriv,
591                 void *sharedContextPrivate,
592                 struct dd_function_table *functions)
593{
594   GLcontext *ctx = &intel->ctx;
595   GLcontext *shareCtx = (GLcontext *) sharedContextPrivate;
596   __DRIscreen *sPriv = driContextPriv->driScreenPriv;
597   intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
598   int bo_reuse_mode;
599
600   if (!_mesa_initialize_context(&intel->ctx, mesaVis, shareCtx,
601                                 functions, (void *) intel)) {
602      _mesa_printf("%s: failed to init mesa context\n", __FUNCTION__);
603      return GL_FALSE;
604   }
605
606   driContextPriv->driverPrivate = intel;
607   intel->intelScreen = intelScreen;
608   intel->driScreen = sPriv;
609   intel->driContext = driContextPriv;
610   intel->driFd = sPriv->fd;
611
612   if (IS_965(intel->intelScreen->deviceID)) {
613      intel->gen = 4;
614   } else if (IS_9XX(intel->intelScreen->deviceID)) {
615      intel->gen = 3;
616      if (IS_945(intel->intelScreen->deviceID)) {
617	 intel->is_945 = GL_TRUE;
618      }
619   } else {
620      intel->gen = 2;
621   }
622
623   if (IS_IGDNG(intel->intelScreen->deviceID)) {
624      intel->is_ironlake = GL_TRUE;
625      intel->needs_ff_sync = GL_TRUE;
626      intel->has_luminance_srgb = GL_TRUE;
627   } else if (IS_G4X(intel->intelScreen->deviceID)) {
628      intel->has_luminance_srgb = GL_TRUE;
629      intel->is_g4x = GL_TRUE;
630   }
631
632   driParseConfigFiles(&intel->optionCache, &intelScreen->optionCache,
633                       intel->driScreen->myNum,
634		       (intel->gen >= 4) ? "i965" : "i915");
635   if (intelScreen->deviceID == PCI_CHIP_I865_G)
636      intel->maxBatchSize = 4096;
637   else
638      intel->maxBatchSize = BATCH_SZ;
639
640   intel->bufmgr = intelScreen->bufmgr;
641
642   bo_reuse_mode = driQueryOptioni(&intel->optionCache, "bo_reuse");
643   switch (bo_reuse_mode) {
644   case DRI_CONF_BO_REUSE_DISABLED:
645      break;
646   case DRI_CONF_BO_REUSE_ALL:
647      intel_bufmgr_gem_enable_reuse(intel->bufmgr);
648      break;
649   }
650
651   /* This doesn't yet catch all non-conformant rendering, but it's a
652    * start.
653    */
654   if (getenv("INTEL_STRICT_CONFORMANCE")) {
655      unsigned int value = atoi(getenv("INTEL_STRICT_CONFORMANCE"));
656      if (value > 0) {
657         intel->conformance_mode = value;
658      }
659      else {
660         intel->conformance_mode = 1;
661      }
662   }
663
664   if (intel->conformance_mode > 0) {
665      ctx->Const.MinLineWidth = 1.0;
666      ctx->Const.MinLineWidthAA = 1.0;
667      ctx->Const.MaxLineWidth = 1.0;
668      ctx->Const.MaxLineWidthAA = 1.0;
669      ctx->Const.LineWidthGranularity = 1.0;
670   }
671   else {
672      ctx->Const.MinLineWidth = 1.0;
673      ctx->Const.MinLineWidthAA = 1.0;
674      ctx->Const.MaxLineWidth = 5.0;
675      ctx->Const.MaxLineWidthAA = 5.0;
676      ctx->Const.LineWidthGranularity = 0.5;
677   }
678
679   ctx->Const.MinPointSize = 1.0;
680   ctx->Const.MinPointSizeAA = 1.0;
681   ctx->Const.MaxPointSize = 255.0;
682   ctx->Const.MaxPointSizeAA = 3.0;
683   ctx->Const.PointSizeGranularity = 1.0;
684
685   /* reinitialize the context point state.
686    * It depend on constants in __GLcontextRec::Const
687    */
688   _mesa_init_point(ctx);
689
690   meta_init_metaops(ctx, &intel->meta);
691   ctx->Const.MaxColorAttachments = 4;  /* XXX FBO: review this */
692   if (intel->gen >= 4) {
693      if (MAX_WIDTH > 8192)
694	 ctx->Const.MaxRenderbufferSize = 8192;
695   } else {
696      if (MAX_WIDTH > 2048)
697	 ctx->Const.MaxRenderbufferSize = 2048;
698   }
699
700   /* Initialize the software rasterizer and helper modules. */
701   _swrast_CreateContext(ctx);
702   _vbo_CreateContext(ctx);
703   _tnl_CreateContext(ctx);
704   _swsetup_CreateContext(ctx);
705
706   /* Configure swrast to match hardware characteristics: */
707   _swrast_allow_pixel_fog(ctx, GL_FALSE);
708   _swrast_allow_vertex_fog(ctx, GL_TRUE);
709
710   _mesa_meta_init(ctx);
711
712   intel->hw_stencil = mesaVis->stencilBits && mesaVis->depthBits == 24;
713   intel->hw_stipple = 1;
714
715   /* XXX FBO: this doesn't seem to be used anywhere */
716   switch (mesaVis->depthBits) {
717   case 0:                     /* what to do in this case? */
718   case 16:
719      intel->polygon_offset_scale = 1.0;
720      break;
721   case 24:
722      intel->polygon_offset_scale = 2.0;     /* req'd to pass glean */
723      break;
724   default:
725      assert(0);
726      break;
727   }
728
729   if (intel->gen >= 4)
730      intel->polygon_offset_scale /= 0xffff;
731
732   intel->RenderIndex = ~0;
733
734   if (intel->gen >= 4 && !intel->intelScreen->irq_active) {
735      _mesa_printf("IRQs not active.  Exiting\n");
736      exit(1);
737   }
738
739   intelInitExtensions(ctx);
740
741   INTEL_DEBUG = driParseDebugString(getenv("INTEL_DEBUG"), debug_control);
742   if (INTEL_DEBUG & DEBUG_BUFMGR)
743      dri_bufmgr_set_debug(intel->bufmgr, GL_TRUE);
744
745   intel->batch = intel_batchbuffer_alloc(intel);
746
747   intel_fbo_init(intel);
748
749   if (intel->ctx.Mesa_DXTn) {
750      _mesa_enable_extension(ctx, "GL_EXT_texture_compression_s3tc");
751      _mesa_enable_extension(ctx, "GL_S3_s3tc");
752   }
753   else if (driQueryOptionb(&intel->optionCache, "force_s3tc_enable")) {
754      _mesa_enable_extension(ctx, "GL_EXT_texture_compression_s3tc");
755   }
756   intel->use_texture_tiling = driQueryOptionb(&intel->optionCache,
757					       "texture_tiling");
758   if (intel->use_texture_tiling &&
759       !intel->intelScreen->kernel_exec_fencing) {
760      fprintf(stderr, "No kernel support for execution fencing, "
761	      "disabling texture tiling\n");
762      intel->use_texture_tiling = GL_FALSE;
763   }
764   intel->use_early_z = driQueryOptionb(&intel->optionCache, "early_z");
765
766   intel->prim.primitive = ~0;
767
768   /* Force all software fallbacks */
769   if (driQueryOptionb(&intel->optionCache, "no_rast")) {
770      fprintf(stderr, "disabling 3D rasterization\n");
771      intel->no_rast = 1;
772   }
773
774   if (driQueryOptionb(&intel->optionCache, "always_flush_batch")) {
775      fprintf(stderr, "flushing batchbuffer before/after each draw call\n");
776      intel->always_flush_batch = 1;
777   }
778
779   if (driQueryOptionb(&intel->optionCache, "always_flush_cache")) {
780      fprintf(stderr, "flushing GPU caches before/after each draw call\n");
781      intel->always_flush_cache = 1;
782   }
783
784   /* Disable all hardware rendering (skip emitting batches and fences/waits
785    * to the kernel)
786    */
787   intel->no_hw = getenv("INTEL_NO_HW") != NULL;
788
789   return GL_TRUE;
790}
791
792void
793intelDestroyContext(__DRIcontext * driContextPriv)
794{
795   struct intel_context *intel =
796      (struct intel_context *) driContextPriv->driverPrivate;
797
798   assert(intel);               /* should never be null */
799   if (intel) {
800      GLboolean release_texture_heaps;
801
802      INTEL_FIREVERTICES(intel);
803
804      _mesa_meta_free(&intel->ctx);
805
806      meta_destroy_metaops(&intel->meta);
807
808      intel->vtbl.destroy(intel);
809
810      release_texture_heaps = (intel->ctx.Shared->RefCount == 1);
811      _swsetup_DestroyContext(&intel->ctx);
812      _tnl_DestroyContext(&intel->ctx);
813      _vbo_DestroyContext(&intel->ctx);
814
815      _swrast_DestroyContext(&intel->ctx);
816      intel->Fallback = 0x0;      /* don't call _swrast_Flush later */
817
818      intel_batchbuffer_free(intel->batch);
819      intel->batch = NULL;
820
821      free(intel->prim.vb);
822      intel->prim.vb = NULL;
823      dri_bo_unreference(intel->prim.vb_bo);
824      intel->prim.vb_bo = NULL;
825      dri_bo_unreference(intel->first_post_swapbuffers_batch);
826      intel->first_post_swapbuffers_batch = NULL;
827
828      if (release_texture_heaps) {
829         /* Nothing is currently done here to free texture heaps;
830          * but we're not using the texture heap utilities, so I
831          * rather think we shouldn't.  I've taken a look, and can't
832          * find any private texture data hanging around anywhere, but
833          * I'm not yet certain there isn't any at all...
834          */
835         /* if (INTEL_DEBUG & DEBUG_TEXTURE)
836            fprintf(stderr, "do something to free texture heaps\n");
837          */
838      }
839
840      driDestroyOptionCache(&intel->optionCache);
841
842      /* free the Mesa context */
843      _mesa_free_context_data(&intel->ctx);
844
845      FREE(intel);
846      driContextPriv->driverPrivate = NULL;
847   }
848}
849
850GLboolean
851intelUnbindContext(__DRIcontext * driContextPriv)
852{
853   struct intel_context *intel =
854      (struct intel_context *) driContextPriv->driverPrivate;
855
856   /* Deassociate the context with the drawables.
857    */
858   intel->driDrawable = NULL;
859   intel->driReadDrawable = NULL;
860
861   return GL_TRUE;
862}
863
864GLboolean
865intelMakeCurrent(__DRIcontext * driContextPriv,
866                 __DRIdrawable * driDrawPriv,
867                 __DRIdrawable * driReadPriv)
868{
869   struct intel_context *intel;
870   GET_CURRENT_CONTEXT(curCtx);
871
872   if (driContextPriv)
873      intel = (struct intel_context *) driContextPriv->driverPrivate;
874   else
875      intel = NULL;
876
877   /* According to the glXMakeCurrent() man page: "Pending commands to
878    * the previous context, if any, are flushed before it is released."
879    * But only flush if we're actually changing contexts.
880    */
881   if (intel_context(curCtx) && intel_context(curCtx) != intel) {
882      _mesa_flush(curCtx);
883   }
884
885   if (driContextPriv) {
886      struct gl_framebuffer *fb = driDrawPriv->driverPrivate;
887      struct gl_framebuffer *readFb = driReadPriv->driverPrivate;
888
889      intel_update_renderbuffers(driContextPriv, driDrawPriv);
890      if (driDrawPriv != driReadPriv)
891	 intel_update_renderbuffers(driContextPriv, driReadPriv);
892
893      /* set GLframebuffer size to match window, if needed */
894      driUpdateFramebufferSize(&intel->ctx, driDrawPriv);
895
896      if (driReadPriv != driDrawPriv) {
897	 driUpdateFramebufferSize(&intel->ctx, driReadPriv);
898      }
899
900      _mesa_make_current(&intel->ctx, fb, readFb);
901      intel->driReadDrawable = driReadPriv;
902      intel->driDrawable = driDrawPriv;
903      intel_draw_buffer(&intel->ctx, fb);
904   }
905   else {
906      _mesa_make_current(NULL, NULL, NULL);
907   }
908
909   return GL_TRUE;
910}
911