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