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