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