OSDN Git Service

intel: Use consistent pattern in intelCreateBuffer
[android-x86/external-mesa.git] / src / mesa / drivers / dri / intel / intel_screen.c
index 356d5f7..90f1778 100644 (file)
@@ -25,6 +25,7 @@
  * 
  **************************************************************************/
 
+#include <errno.h>
 #include "main/glheader.h"
 #include "main/context.h"
 #include "main/framebuffer.h"
@@ -32,6 +33,8 @@
 #include "main/hash.h"
 #include "main/fbobject.h"
 #include "main/mfeatures.h"
+#include "main/version.h"
+#include "swrast/s_renderbuffer.h"
 
 #include "utils.h"
 #include "xmlpool.h"
@@ -54,6 +57,10 @@ PUBLIC const char __driConfigOptions[] =
         DRI_CONF_DESC(en, "Enable texture tiling")
       DRI_CONF_OPT_END
 
+      DRI_CONF_OPT_BEGIN(hiz, bool, true)
+        DRI_CONF_DESC(en, "Enable Hierarchical Z on gen6+")
+      DRI_CONF_OPT_END
+
       DRI_CONF_OPT_BEGIN(early_z, bool, false)
         DRI_CONF_DESC(en, "Enable early Z in classic mode (unstable, 945-only).")
       DRI_CONF_OPT_END
@@ -71,20 +78,27 @@ PUBLIC const char __driConfigOptions[] =
      DRI_CONF_NO_RAST(false)
      DRI_CONF_ALWAYS_FLUSH_BATCH(false)
      DRI_CONF_ALWAYS_FLUSH_CACHE(false)
+     DRI_CONF_FORCE_GLSL_EXTENSIONS_WARN(false)
+     DRI_CONF_DISABLE_BLEND_FUNC_EXTENDED(false)
 
       DRI_CONF_OPT_BEGIN(stub_occlusion_query, bool, false)
         DRI_CONF_DESC(en, "Enable stub ARB_occlusion_query support on 915/945.")
       DRI_CONF_OPT_END
+
+      DRI_CONF_OPT_BEGIN(shader_precompile, bool, false)
+        DRI_CONF_DESC(en, "Perform code generation at shader link time.")
+      DRI_CONF_OPT_END
    DRI_CONF_SECTION_END
 DRI_CONF_END;
 
-const GLuint __driNConfigOptions = 11;
+const GLuint __driNConfigOptions = 15;
 
 #include "intel_batchbuffer.h"
 #include "intel_buffers.h"
 #include "intel_bufmgr.h"
 #include "intel_chipset.h"
 #include "intel_fbo.h"
+#include "intel_mipmap_tree.h"
 #include "intel_screen.h"
 #include "intel_tex.h"
 #include "intel_regions.h"
@@ -95,6 +109,40 @@ const GLuint __driNConfigOptions = 11;
 static PFNGLXCREATECONTEXTMODES create_context_modes = NULL;
 #endif /*USE_NEW_INTERFACE */
 
+void
+aub_dump_bmp(struct gl_context *ctx)
+{
+   struct gl_framebuffer *fb = ctx->DrawBuffer;
+
+   for (int i = 0; i < fb->_NumColorDrawBuffers; i++) {
+      struct intel_renderbuffer *irb =
+        intel_renderbuffer(fb->_ColorDrawBuffers[i]);
+
+      if (irb && irb->mt) {
+        enum aub_dump_bmp_format format;
+
+        switch (irb->Base.Base.Format) {
+        case MESA_FORMAT_ARGB8888:
+        case MESA_FORMAT_XRGB8888:
+           format = AUB_DUMP_BMP_FORMAT_ARGB_8888;
+           break;
+        default:
+           continue;
+        }
+
+        drm_intel_gem_bo_aub_dump_bmp(irb->mt->region->bo,
+                                      irb->draw_x,
+                                      irb->draw_y,
+                                      irb->Base.Base.Width,
+                                      irb->Base.Base.Height,
+                                      format,
+                                      irb->mt->region->pitch *
+                                      irb->mt->region->cpp,
+                                      0);
+      }
+   }
+}
+
 static const __DRItexBufferExtension intelTexBufferExtension = {
     { __DRI_TEX_BUFFER, __DRI_TEX_BUFFER_VERSION },
    intelSetTexBuffer,
@@ -104,15 +152,22 @@ static const __DRItexBufferExtension intelTexBufferExtension = {
 static void
 intelDRI2Flush(__DRIdrawable *drawable)
 {
-   struct intel_context *intel = drawable->driContextPriv->driverPrivate;
+   GET_CURRENT_CONTEXT(ctx);
+   struct intel_context *intel = intel_context(ctx);
+   if (intel == NULL)
+      return;
 
    if (intel->gen < 4)
       INTEL_FIREVERTICES(intel);
 
-   intel->need_throttle = GL_TRUE;
+   intel->need_throttle = true;
 
    if (intel->batch.used)
       intel_batchbuffer_flush(intel);
+
+   if (INTEL_DEBUG & DEBUG_AUB) {
+      aub_dump_bmp(ctx);
+   }
 }
 
 static const struct __DRI2flushExtensionRec intelFlushExtension = {
@@ -122,42 +177,67 @@ static const struct __DRI2flushExtensionRec intelFlushExtension = {
 };
 
 static __DRIimage *
-intel_create_image_from_name(__DRIscreen *screen,
-                            int width, int height, int format,
-                            int name, int pitch, void *loaderPrivate)
+intel_allocate_image(int dri_format, void *loaderPrivate)
 {
-    struct intel_screen *intelScreen = screen->private;
     __DRIimage *image;
-    int cpp;
 
     image = CALLOC(sizeof *image);
     if (image == NULL)
        return NULL;
 
-    switch (format) {
+    image->dri_format = dri_format;
+    image->offset = 0;
+
+    switch (dri_format) {
     case __DRI_IMAGE_FORMAT_RGB565:
        image->format = MESA_FORMAT_RGB565;
-       image->internal_format = GL_RGB;
-       image->data_type = GL_UNSIGNED_BYTE;
        break;
     case __DRI_IMAGE_FORMAT_XRGB8888:
        image->format = MESA_FORMAT_XRGB8888;
-       image->internal_format = GL_RGB;
-       image->data_type = GL_UNSIGNED_BYTE;
        break;
     case __DRI_IMAGE_FORMAT_ARGB8888:
        image->format = MESA_FORMAT_ARGB8888;
-       image->internal_format = GL_RGBA;
-       image->data_type = GL_UNSIGNED_BYTE;
+       break;
+    case __DRI_IMAGE_FORMAT_ABGR8888:
+       image->format = MESA_FORMAT_RGBA8888_REV;
+       break;
+    case __DRI_IMAGE_FORMAT_XBGR8888:
+       image->format = MESA_FORMAT_RGBX8888_REV;
+       break;
+    case __DRI_IMAGE_FORMAT_R8:
+       image->format = MESA_FORMAT_R8;
+       break;
+    case __DRI_IMAGE_FORMAT_GR88:
+       image->format = MESA_FORMAT_GR88;
+       break;
+    case __DRI_IMAGE_FORMAT_NONE:
+       image->format = MESA_FORMAT_NONE;
        break;
     default:
        free(image);
        return NULL;
     }
 
+    image->internal_format = _mesa_get_format_base_format(image->format);
     image->data = loaderPrivate;
-    cpp = _mesa_get_format_bytes(image->format);
 
+    return image;
+}
+
+static __DRIimage *
+intel_create_image_from_name(__DRIscreen *screen,
+                            int width, int height, int format,
+                            int name, int pitch, void *loaderPrivate)
+{
+    struct intel_screen *intelScreen = screen->driverPrivate;
+    __DRIimage *image;
+    int cpp;
+
+    image = intel_allocate_image(format, loaderPrivate);
+    if (image->format == MESA_FORMAT_NONE)
+       cpp = 0;
+    else
+       cpp = _mesa_get_format_bytes(image->format);
     image->region = intel_region_alloc_for_handle(intelScreen,
                                                  cpp, width, height,
                                                  pitch, name, "image");
@@ -192,9 +272,30 @@ intel_create_image_from_renderbuffer(__DRIcontext *context,
 
    image->internal_format = rb->InternalFormat;
    image->format = rb->Format;
-   image->data_type = rb->DataType;
+   image->offset = 0;
    image->data = loaderPrivate;
-   intel_region_reference(&image->region, irb->region);
+   intel_region_reference(&image->region, irb->mt->region);
+
+   switch (image->format) {
+   case MESA_FORMAT_RGB565:
+      image->dri_format = __DRI_IMAGE_FORMAT_RGB565;
+      break;
+   case MESA_FORMAT_XRGB8888:
+      image->dri_format = __DRI_IMAGE_FORMAT_XRGB8888;
+      break;
+   case MESA_FORMAT_ARGB8888:
+      image->dri_format = __DRI_IMAGE_FORMAT_ARGB8888;
+      break;
+   case MESA_FORMAT_RGBA8888_REV:
+      image->dri_format = __DRI_IMAGE_FORMAT_ABGR8888;
+      break;
+   case MESA_FORMAT_R8:
+      image->dri_format = __DRI_IMAGE_FORMAT_R8;
+      break;
+   case MESA_FORMAT_RG88:
+      image->dri_format = __DRI_IMAGE_FORMAT_GR88;
+      break;
+   }
 
    return image;
 }
@@ -213,40 +314,27 @@ intel_create_image(__DRIscreen *screen,
                   void *loaderPrivate)
 {
    __DRIimage *image;
-   struct intel_screen *intelScreen = screen->private;
+   struct intel_screen *intelScreen = screen->driverPrivate;
+   uint32_t tiling;
    int cpp;
 
-   image = CALLOC(sizeof *image);
-   if (image == NULL)
-      return NULL;
+   tiling = I915_TILING_X;
+   if (use & __DRI_IMAGE_USE_CURSOR) {
+      if (width != 64 || height != 64)
+        return NULL;
+      tiling = I915_TILING_NONE;
+   }
 
-   switch (format) {
-   case __DRI_IMAGE_FORMAT_RGB565:
-      image->format = MESA_FORMAT_RGB565;
-      image->internal_format = GL_RGB;
-      image->data_type = GL_UNSIGNED_BYTE;
-      break;
-   case __DRI_IMAGE_FORMAT_XRGB8888:
-      image->format = MESA_FORMAT_XRGB8888;
-      image->internal_format = GL_RGB;
-      image->data_type = GL_UNSIGNED_BYTE;
-      break;
-   case __DRI_IMAGE_FORMAT_ARGB8888:
-      image->format = MESA_FORMAT_ARGB8888;
-      image->internal_format = GL_RGBA;
-      image->data_type = GL_UNSIGNED_BYTE;
-      break;
-   default:
-      free(image);
+   /* We only support write for cursor drm images */
+   if ((use & __DRI_IMAGE_USE_WRITE) &&
+       use != (__DRI_IMAGE_USE_WRITE | __DRI_IMAGE_USE_CURSOR))
       return NULL;
-   }
 
-   image->data = loaderPrivate;
+   image = intel_allocate_image(format, loaderPrivate);
+   image->usage = use;
    cpp = _mesa_get_format_bytes(image->format);
-
    image->region =
-      intel_region_alloc(intelScreen, I915_TILING_NONE,
-                        cpp, width, height, GL_TRUE);
+      intel_region_alloc(intelScreen, tiling, cpp, width, height, true);
    if (image->region == NULL) {
       FREE(image);
       return NULL;
@@ -261,28 +349,138 @@ intel_query_image(__DRIimage *image, int attrib, int *value)
    switch (attrib) {
    case __DRI_IMAGE_ATTRIB_STRIDE:
       *value = image->region->pitch * image->region->cpp;
-      return GL_TRUE;
+      return true;
    case __DRI_IMAGE_ATTRIB_HANDLE:
-      *value = image->region->buffer->handle;
-      return GL_TRUE;
+      *value = image->region->bo->handle;
+      return true;
    case __DRI_IMAGE_ATTRIB_NAME:
       return intel_region_flink(image->region, (uint32_t *) value);
-   default:
-      return GL_FALSE;
+   case __DRI_IMAGE_ATTRIB_FORMAT:
+      *value = image->dri_format;
+      return true;
+   case __DRI_IMAGE_ATTRIB_WIDTH:
+      *value = image->region->width;
+      return true;
+   case __DRI_IMAGE_ATTRIB_HEIGHT:
+      *value = image->region->height;
+      return true;
+  default:
+      return false;
+   }
+}
+
+static __DRIimage *
+intel_dup_image(__DRIimage *orig_image, void *loaderPrivate)
+{
+   __DRIimage *image;
+
+   image = CALLOC(sizeof *image);
+   if (image == NULL)
+      return NULL;
+
+   intel_region_reference(&image->region, orig_image->region);
+   if (image->region == NULL) {
+      FREE(image);
+      return NULL;
+   }
+
+   image->internal_format = orig_image->internal_format;
+   image->usage           = orig_image->usage;
+   image->dri_format      = orig_image->dri_format;
+   image->format          = orig_image->format;
+   image->offset          = orig_image->offset;
+   image->data            = loaderPrivate;
+   
+   return image;
+}
+
+static GLboolean
+intel_validate_usage(__DRIimage *image, unsigned int use)
+{
+   if (use & __DRI_IMAGE_USE_CURSOR) {
+      if (image->region->width != 64 || image->region->height != 64)
+        return GL_FALSE;
    }
+
+   /* We only support write for cursor drm images */
+   if ((use & __DRI_IMAGE_USE_WRITE) &&
+       use != (__DRI_IMAGE_USE_WRITE | __DRI_IMAGE_USE_CURSOR))
+      return GL_FALSE;
+
+   return GL_TRUE;
+}
+
+static int
+intel_image_write(__DRIimage *image, const void *buf, size_t count)
+{
+   if (image->region->map_refcount)
+      return -1;
+   if (!(image->usage & __DRI_IMAGE_USE_WRITE))
+      return -1;
+
+   drm_intel_bo_map(image->region->bo, true);
+   memcpy(image->region->bo->virtual, buf, count);
+   drm_intel_bo_unmap(image->region->bo);
+
+   return 0;
+}
+
+static __DRIimage *
+intel_create_sub_image(__DRIimage *parent,
+                       int width, int height, int dri_format,
+                       int offset, int pitch, void *loaderPrivate)
+{
+    __DRIimage *image;
+    int cpp;
+    uint32_t mask_x, mask_y;
+
+    image = intel_allocate_image(dri_format, loaderPrivate);
+    cpp = _mesa_get_format_bytes(image->format);
+    if (offset + height * cpp * pitch > parent->region->bo->size) {
+       _mesa_warning(NULL, "intel_create_sub_image: subimage out of bounds");
+       FREE(image);
+       return NULL;
+    }
+
+    image->region = calloc(sizeof(*image->region), 1);
+    if (image->region == NULL) {
+       FREE(image);
+       return NULL;
+    }
+
+    image->region->cpp = _mesa_get_format_bytes(image->format);
+    image->region->width = width;
+    image->region->height = height;
+    image->region->pitch = pitch;
+    image->region->refcount = 1;
+    image->region->bo = parent->region->bo;
+    drm_intel_bo_reference(image->region->bo);
+    image->region->tiling = parent->region->tiling;
+    image->region->screen = parent->region->screen;
+    image->offset = offset;
+
+    intel_region_get_tile_masks(image->region, &mask_x, &mask_y);
+    if (offset & mask_x)
+       _mesa_warning(NULL,
+                     "intel_create_sub_image: offset not on tile boundary");
+
+    return image;
 }
 
 static struct __DRIimageExtensionRec intelImageExtension = {
-    { __DRI_IMAGE, __DRI_IMAGE_VERSION },
+    { __DRI_IMAGE, 5 },
     intel_create_image_from_name,
     intel_create_image_from_renderbuffer,
     intel_destroy_image,
     intel_create_image,
-    intel_query_image
+    intel_query_image,
+    intel_dup_image,
+    intel_validate_usage,
+    intel_image_write,
+    intel_create_sub_image
 };
 
 static const __DRIextension *intelScreenExtensions[] = {
-    &driReadDrawableExtension,
     &intelTexBufferExtension.base,
     &intelFlushExtension.base,
     &intelImageExtension.base,
@@ -290,22 +488,31 @@ static const __DRIextension *intelScreenExtensions[] = {
     NULL
 };
 
-static GLboolean
+static bool
 intel_get_param(__DRIscreen *psp, int param, int *value)
 {
    int ret;
    struct drm_i915_getparam gp;
 
+   memset(&gp, 0, sizeof(gp));
    gp.param = param;
    gp.value = value;
 
    ret = drmCommandWriteRead(psp->fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
    if (ret) {
-      _mesa_warning(NULL, "drm_i915_getparam: %d", ret);
-      return GL_FALSE;
+      if (ret != -EINVAL)
+        _mesa_warning(NULL, "drm_i915_getparam: %d", ret);
+      return false;
    }
 
-   return GL_TRUE;
+   return true;
+}
+
+static bool
+intel_get_boolean(__DRIscreen *psp, int param)
+{
+   int value = 0;
+   return intel_get_param(psp, param, &value) && value;
 }
 
 static void
@@ -316,7 +523,7 @@ nop_callback(GLuint key, void *data, void *userData)
 static void
 intelDestroyScreen(__DRIscreen * sPriv)
 {
-   struct intel_screen *intelScreen = sPriv->private;
+   struct intel_screen *intelScreen = sPriv->driverPrivate;
 
    dri_bufmgr_destroy(intelScreen->bufmgr);
    driDestroyOptionInfo(&intelScreen->optionCache);
@@ -328,7 +535,7 @@ intelDestroyScreen(__DRIscreen * sPriv)
    _mesa_DeleteHashTable(intelScreen->named_regions);
 
    FREE(intelScreen);
-   sPriv->private = NULL;
+   sPriv->driverPrivate = NULL;
 }
 
 
@@ -341,65 +548,79 @@ intelCreateBuffer(__DRIscreen * driScrnPriv,
                   const struct gl_config * mesaVis, GLboolean isPixmap)
 {
    struct intel_renderbuffer *rb;
+   struct intel_screen *screen = (struct intel_screen*) driScrnPriv->driverPrivate;
+   gl_format rgbFormat;
+   struct gl_framebuffer *fb;
 
-   if (isPixmap) {
-      return GL_FALSE;          /* not implemented */
-   }
-   else {
-      GLboolean swStencil = (mesaVis->stencilBits > 0 &&
-                             mesaVis->depthBits != 24);
-      gl_format rgbFormat;
+   if (isPixmap)
+      return false;
 
-      struct gl_framebuffer *fb = CALLOC_STRUCT(gl_framebuffer);
+   fb = CALLOC_STRUCT(gl_framebuffer);
+   if (!fb)
+      return false;
 
-      if (!fb)
-        return GL_FALSE;
+   _mesa_initialize_window_framebuffer(fb, mesaVis);
 
-      _mesa_initialize_window_framebuffer(fb, mesaVis);
+   if (mesaVis->redBits == 5)
+      rgbFormat = MESA_FORMAT_RGB565;
+   else if (mesaVis->alphaBits == 0)
+      rgbFormat = MESA_FORMAT_XRGB8888;
+   else
+      rgbFormat = MESA_FORMAT_ARGB8888;
 
-      if (mesaVis->redBits == 5)
-        rgbFormat = MESA_FORMAT_RGB565;
-      else if (mesaVis->alphaBits == 0)
-        rgbFormat = MESA_FORMAT_XRGB8888;
-      else
-        rgbFormat = MESA_FORMAT_ARGB8888;
+   /* setup the hardware-based renderbuffers */
+   rb = intel_create_renderbuffer(rgbFormat);
+   _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &rb->Base.Base);
 
-      /* setup the hardware-based renderbuffers */
+   if (mesaVis->doubleBufferMode) {
       rb = intel_create_renderbuffer(rgbFormat);
-      _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &rb->Base);
-
-      if (mesaVis->doubleBufferMode) {
-        rb = intel_create_renderbuffer(rgbFormat);
-         _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &rb->Base);
-      }
+      _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &rb->Base.Base);
+   }
 
-      if (mesaVis->depthBits == 24) {
-        assert(mesaVis->stencilBits == 8);
-        /* combined depth/stencil buffer */
-        struct intel_renderbuffer *depthStencilRb
-           = intel_create_renderbuffer(MESA_FORMAT_S8_Z24);
-        /* note: bind RB to two attachment points */
-        _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthStencilRb->Base);
-        _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &depthStencilRb->Base);
-      }
-      else if (mesaVis->depthBits == 16) {
-         /* just 16-bit depth buffer, no hw stencil */
-         struct intel_renderbuffer *depthRb
-           = intel_create_renderbuffer(MESA_FORMAT_Z16);
-         _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
+   /*
+    * Assert here that the gl_config has an expected depth/stencil bit
+    * combination: one of d24/s8, d16/s0, d0/s0. (See intelInitScreen2(),
+    * which constructs the advertised configs.)
+    */
+   if (mesaVis->depthBits == 24) {
+      assert(mesaVis->stencilBits == 8);
+
+      if (screen->hw_has_separate_stencil) {
+         rb = intel_create_private_renderbuffer(MESA_FORMAT_X8_Z24);
+         _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &rb->Base.Base);
+         rb = intel_create_private_renderbuffer(MESA_FORMAT_S8);
+         _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &rb->Base.Base);
+      } else {
+         /*
+          * Use combined depth/stencil. Note that the renderbuffer is
+          * attached to two attachment points.
+          */
+         rb = intel_create_private_renderbuffer(MESA_FORMAT_S8_Z24);
+         _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &rb->Base.Base);
+         _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &rb->Base.Base);
       }
-
-      /* now add any/all software-based renderbuffers we may need */
-      _mesa_add_soft_renderbuffers(fb,
-                                   GL_FALSE, /* never sw color */
-                                   GL_FALSE, /* never sw depth */
-                                   swStencil, mesaVis->accumRedBits > 0,
-                                   GL_FALSE, /* never sw alpha */
-                                   GL_FALSE  /* never sw aux */ );
-      driDrawPriv->driverPrivate = fb;
-
-      return GL_TRUE;
    }
+   else if (mesaVis->depthBits == 16) {
+      assert(mesaVis->stencilBits == 0);
+      rb = intel_create_private_renderbuffer(MESA_FORMAT_Z16);
+      _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &rb->Base.Base);
+   }
+   else {
+      assert(mesaVis->depthBits == 0);
+      assert(mesaVis->stencilBits == 0);
+   }
+
+   /* now add any/all software-based renderbuffers we may need */
+   _swrast_add_soft_renderbuffers(fb,
+                                  false, /* never sw color */
+                                  false, /* never sw depth */
+                                  false, /* never sw stencil */
+                                  mesaVis->accumRedBits > 0,
+                                  false, /* never sw alpha */
+                                  false  /* never sw aux */ );
+   driDrawPriv->driverPrivate = fb;
+
+   return true;
 }
 
 static void
@@ -414,74 +635,154 @@ intelDestroyBuffer(__DRIdrawable * driDrawPriv)
  * init-designated function to register chipids and createcontext
  * functions.
  */
-extern GLboolean i830CreateContext(const struct gl_config * mesaVis,
-                                   __DRIcontext * driContextPriv,
-                                   void *sharedContextPrivate);
-
-extern GLboolean i915CreateContext(int api,
-                                  const struct gl_config * mesaVis,
-                                   __DRIcontext * driContextPriv,
-                                   void *sharedContextPrivate);
-extern GLboolean brwCreateContext(int api,
-                                 const struct gl_config * mesaVis,
-                                 __DRIcontext * driContextPriv,
-                                 void *sharedContextPrivate);
+extern bool
+i830CreateContext(const struct gl_config *mesaVis,
+                 __DRIcontext *driContextPriv,
+                 void *sharedContextPrivate);
+
+extern bool
+i915CreateContext(int api,
+                 const struct gl_config *mesaVis,
+                 __DRIcontext *driContextPriv,
+                 void *sharedContextPrivate);
+extern bool
+brwCreateContext(int api,
+                const struct gl_config *mesaVis,
+                __DRIcontext *driContextPriv,
+                void *sharedContextPrivate);
 
 static GLboolean
 intelCreateContext(gl_api api,
                   const struct gl_config * mesaVis,
                    __DRIcontext * driContextPriv,
+                  unsigned major_version,
+                  unsigned minor_version,
+                  uint32_t flags,
+                  unsigned *error,
                    void *sharedContextPrivate)
 {
    __DRIscreen *sPriv = driContextPriv->driScreenPriv;
-   struct intel_screen *intelScreen = sPriv->private;
+   struct intel_screen *intelScreen = sPriv->driverPrivate;
+   bool success = false;
 
 #ifdef I915
    if (IS_9XX(intelScreen->deviceID)) {
       if (!IS_965(intelScreen->deviceID)) {
-        return i915CreateContext(api, mesaVis, driContextPriv,
-                                 sharedContextPrivate);
+        success = i915CreateContext(api, mesaVis, driContextPriv,
+                                    sharedContextPrivate);
       }
    } else {
-      intelScreen->no_vbo = GL_TRUE;
-      return i830CreateContext(mesaVis, driContextPriv, sharedContextPrivate);
+      intelScreen->no_vbo = true;
+      success = i830CreateContext(mesaVis, driContextPriv,
+                                 sharedContextPrivate);
    }
 #else
    if (IS_965(intelScreen->deviceID))
-      return brwCreateContext(api, mesaVis,
-                             driContextPriv, sharedContextPrivate);
+      success = brwCreateContext(api, mesaVis,
+                             driContextPriv,
+                             sharedContextPrivate);
 #endif
-   fprintf(stderr, "Unrecognized deviceID 0x%x\n", intelScreen->deviceID);
-   return GL_FALSE;
+
+   if (success) {
+      struct gl_context *ctx =
+        (struct gl_context *) driContextPriv->driverPrivate;
+
+      _mesa_compute_version(ctx);
+      if (ctx->VersionMajor > major_version
+         || (ctx->VersionMajor == major_version
+             && ctx->VersionMinor >= minor_version)) {
+        return true;
+      }
+
+      *error = __DRI_CTX_ERROR_BAD_VERSION;
+      intelDestroyContext(driContextPriv);
+   } else {
+      *error = __DRI_CTX_ERROR_NO_MEMORY;
+      fprintf(stderr, "Unrecognized deviceID 0x%x\n", intelScreen->deviceID);
+   }
+
+   return false;
 }
 
-static GLboolean
+static bool
 intel_init_bufmgr(struct intel_screen *intelScreen)
 {
    __DRIscreen *spriv = intelScreen->driScrnPriv;
    int num_fences = 0;
 
-   intelScreen->no_hw = (getenv("INTEL_NO_HW") != NULL ||
-                        getenv("INTEL_DEVID_OVERRIDE") != NULL);
+   intelScreen->no_hw = getenv("INTEL_NO_HW") != NULL;
 
    intelScreen->bufmgr = intel_bufmgr_gem_init(spriv->fd, BATCH_SZ);
    if (intelScreen->bufmgr == NULL) {
       fprintf(stderr, "[%s:%u] Error initializing buffer manager.\n",
              __func__, __LINE__);
-      return GL_FALSE;
+      return false;
    }
 
    if (!intel_get_param(spriv, I915_PARAM_NUM_FENCES_AVAIL, &num_fences) ||
        num_fences == 0) {
       fprintf(stderr, "[%s: %u] Kernel 2.6.29 required.\n", __func__, __LINE__);
-      return GL_FALSE;
+      return false;
    }
 
    drm_intel_bufmgr_gem_enable_fenced_relocs(intelScreen->bufmgr);
 
    intelScreen->named_regions = _mesa_NewHashTable();
 
-   return GL_TRUE;
+   intelScreen->relaxed_relocations = 0;
+   intelScreen->relaxed_relocations |=
+      intel_get_boolean(spriv, I915_PARAM_HAS_RELAXED_DELTA) << 0;
+
+   return true;
+}
+
+/**
+ * Override intel_screen.hw_has_separate_stencil with environment variable
+ * INTEL_SEPARATE_STENCIL.
+ *
+ * Valid values for INTEL_SEPARATE_STENCIL are "0" and "1". If an invalid
+ * valid value is encountered, a warning is emitted and INTEL_SEPARATE_STENCIL
+ * is ignored.
+ */
+static void
+intel_override_separate_stencil(struct intel_screen *screen)
+{
+   const char *s = getenv("INTEL_SEPARATE_STENCIL");
+   if (!s) {
+      return;
+   } else if (!strncmp("0", s, 2)) {
+      screen->hw_has_separate_stencil = false;
+   } else if (!strncmp("1", s, 2)) {
+      screen->hw_has_separate_stencil = true;
+   } else {
+      fprintf(stderr,
+             "warning: env variable INTEL_SEPARATE_STENCIL=\"%s\" has "
+             "invalid value and is ignored", s);
+   }
+}
+
+static bool
+intel_detect_swizzling(struct intel_screen *screen)
+{
+   drm_intel_bo *buffer;
+   unsigned long flags = 0;
+   unsigned long aligned_pitch;
+   uint32_t tiling = I915_TILING_X;
+   uint32_t swizzle_mode = 0;
+
+   buffer = drm_intel_bo_alloc_tiled(screen->bufmgr, "swizzle test",
+                                    64, 64, 4,
+                                    &tiling, &aligned_pitch, flags);
+   if (buffer == NULL)
+      return false;
+
+   drm_intel_bo_get_tiling(buffer, &tiling, &swizzle_mode);
+   drm_intel_bo_unreference(buffer);
+
+   if (swizzle_mode == I915_BIT_6_SWIZZLE_NONE)
+      return false;
+   else
+      return true;
 }
 
 /**
@@ -497,7 +798,6 @@ __DRIconfig **intelInitScreen2(__DRIscreen *psp)
    GLenum fb_format[3];
    GLenum fb_type[3];
    unsigned int api_mask;
-   char *devid_override;
 
    static const GLenum back_buffer_modes[] = {
        GLX_NONE, GLX_SWAP_UNDEFINED_OML, GLX_SWAP_COPY_OML
@@ -506,34 +806,63 @@ __DRIconfig **intelInitScreen2(__DRIscreen *psp)
    int color;
    __DRIconfig **configs = NULL;
 
+   if (psp->dri2.loader->base.version <= 2 ||
+       psp->dri2.loader->getBuffersWithFormat == NULL) {
+      fprintf(stderr,
+             "\nERROR!  DRI2 loader with getBuffersWithFormat() "
+             "support required\n");
+      return false;
+   }
+
    /* Allocate the private area */
    intelScreen = CALLOC(sizeof *intelScreen);
    if (!intelScreen) {
       fprintf(stderr, "\nERROR!  Allocating private area failed\n");
-      return GL_FALSE;
+      return false;
    }
    /* parse information in __driConfigOptions */
    driParseOptionInfo(&intelScreen->optionCache,
                       __driConfigOptions, __driNConfigOptions);
 
    intelScreen->driScrnPriv = psp;
-   psp->private = (void *) intelScreen;
+   psp->driverPrivate = (void *) intelScreen;
 
-   /* Determine chipset ID */
-   if (!intel_get_param(psp, I915_PARAM_CHIPSET_ID,
-                       &intelScreen->deviceID))
-      return GL_FALSE;
-
-   /* Allow an override of the device ID for the purpose of making the
-    * driver produce dumps for debugging of new chipset enablement.
-    * This implies INTEL_NO_HW, to avoid programming your actual GPU
-    * incorrectly.
-    */
-   devid_override = getenv("INTEL_DEVID_OVERRIDE");
-   if (devid_override) {
-      intelScreen->deviceID = strtod(devid_override, NULL);
+   if (!intel_init_bufmgr(intelScreen))
+       return false;
+
+   intelScreen->deviceID = drm_intel_bufmgr_gem_get_devid(intelScreen->bufmgr);
+
+   intelScreen->kernel_has_gen7_sol_reset =
+      intel_get_boolean(intelScreen->driScrnPriv,
+                       I915_PARAM_HAS_GEN7_SOL_RESET);
+
+   if (IS_GEN7(intelScreen->deviceID)) {
+      intelScreen->gen = 7;
+   } else if (IS_GEN6(intelScreen->deviceID)) {
+      intelScreen->gen = 6;
+   } else if (IS_GEN5(intelScreen->deviceID)) {
+      intelScreen->gen = 5;
+   } else if (IS_965(intelScreen->deviceID)) {
+      intelScreen->gen = 4;
+   } else if (IS_9XX(intelScreen->deviceID)) {
+      intelScreen->gen = 3;
+   } else {
+      intelScreen->gen = 2;
    }
 
+   intelScreen->hw_has_separate_stencil = intelScreen->gen >= 6;
+   intelScreen->hw_must_use_separate_stencil = intelScreen->gen >= 7;
+
+   int has_llc = 0;
+   bool success = intel_get_param(intelScreen->driScrnPriv, I915_PARAM_HAS_LLC,
+                                 &has_llc);
+   if (success && has_llc)
+      intelScreen->hw_has_llc = true;
+   else if (!success && intelScreen->gen >= 6)
+      intelScreen->hw_has_llc = true;
+
+   intel_override_separate_stencil(intelScreen);
+
    api_mask = (1 << __DRI_API_OPENGL);
 #if FEATURE_ES1
    api_mask |= (1 << __DRI_API_GLES);
@@ -545,8 +874,7 @@ __DRIconfig **intelInitScreen2(__DRIscreen *psp)
    if (IS_9XX(intelScreen->deviceID) || IS_965(intelScreen->deviceID))
       psp->api_mask = api_mask;
 
-   if (!intel_init_bufmgr(intelScreen))
-       return GL_FALSE;
+   intelScreen->hw_has_swizzling = intel_detect_swizzling(intelScreen);
 
    psp->extensions = intelScreenExtensions;
 
@@ -593,7 +921,7 @@ __DRIconfig **intelInitScreen2(__DRIscreen *psp)
                                     ARRAY_SIZE(back_buffer_modes),
                                     msaa_samples_array,
                                     ARRAY_SIZE(msaa_samples_array),
-                                    GL_FALSE);
+                                    false);
       if (configs == NULL)
         configs = new_configs;
       else
@@ -618,7 +946,7 @@ __DRIconfig **intelInitScreen2(__DRIscreen *psp)
                                     depth_bits, stencil_bits, 1,
                                     back_buffer_modes + 1, 1,
                                     msaa_samples_array, 1,
-                                    GL_TRUE);
+                                    true);
       if (configs == NULL)
         configs = new_configs;
       else
@@ -645,14 +973,22 @@ intelAllocateBuffer(__DRIscreen *screen,
                    int width, int height)
 {
    struct intel_buffer *intelBuffer;
-   struct intel_screen *intelScreen = screen->private;
+   struct intel_screen *intelScreen = screen->driverPrivate;
+
+   assert(attachment == __DRI_BUFFER_FRONT_LEFT ||
+          attachment == __DRI_BUFFER_BACK_LEFT);
 
    intelBuffer = CALLOC(sizeof *intelBuffer);
    if (intelBuffer == NULL)
       return NULL;
 
-   intelBuffer->region = intel_region_alloc(intelScreen, I915_TILING_NONE,
-                                           format / 8, width, height, GL_TRUE);
+   /* The front and back buffers are color buffers, which are X tiled. */
+   intelBuffer->region = intel_region_alloc(intelScreen,
+                                            I915_TILING_X,
+                                            format / 8,
+                                            width,
+                                            height,
+                                            true);
    
    if (intelBuffer->region == NULL) {
           FREE(intelBuffer);
@@ -680,6 +1016,7 @@ intelReleaseBuffer(__DRIscreen *screen, __DRIbuffer *buffer)
 
 
 const struct __DriverAPIRec driDriverAPI = {
+   .InitScreen          = intelInitScreen2,
    .DestroyScreen       = intelDestroyScreen,
    .CreateContext       = intelCreateContext,
    .DestroyContext      = intelDestroyContext,
@@ -687,7 +1024,6 @@ const struct __DriverAPIRec driDriverAPI = {
    .DestroyBuffer       = intelDestroyBuffer,
    .MakeCurrent                 = intelMakeCurrent,
    .UnbindContext       = intelUnbindContext,
-   .InitScreen2                 = intelInitScreen2,
    .AllocateBuffer       = intelAllocateBuffer,
    .ReleaseBuffer        = intelReleaseBuffer
 };