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