i965: Bump kernel requirement to 3.3 on Ivybridge.
[mesa.git] / src / mesa / drivers / dri / intel / intel_screen.c
index b4992e1ad7a08ae30773d53074a4d7389bf864b9..433f4ed2bd3d464a3e905e956077388a75030e4a 100644 (file)
@@ -31,9 +31,9 @@
 #include "main/context.h"
 #include "main/framebuffer.h"
 #include "main/renderbuffer.h"
+#include "main/texobj.h"
 #include "main/hash.h"
 #include "main/fbobject.h"
-#include "main/mfeatures.h"
 #include "main/version.h"
 #include "swrast/s_renderbuffer.h"
 
@@ -54,45 +54,35 @@ PUBLIC const char __driConfigOptions[] =
         DRI_CONF_DESC_END
       DRI_CONF_OPT_END
 
-      DRI_CONF_OPT_BEGIN(texture_tiling, bool, true)
-        DRI_CONF_DESC(en, "Enable texture tiling")
-      DRI_CONF_OPT_END
-
-      DRI_CONF_OPT_BEGIN(hiz, bool, true)
+      DRI_CONF_OPT_BEGIN_B(hiz, "true")
         DRI_CONF_DESC(en, "Enable Hierarchical Z on gen6+")
       DRI_CONF_OPT_END
 
-      DRI_CONF_OPT_BEGIN(early_z, bool, false)
+      DRI_CONF_OPT_BEGIN_B(early_z, "false")
         DRI_CONF_DESC(en, "Enable early Z in classic mode (unstable, 945-only).")
       DRI_CONF_OPT_END
 
-      DRI_CONF_OPT_BEGIN(fragment_shader, bool, true)
-        DRI_CONF_DESC(en, "Enable limited ARB_fragment_shader support on 915/945.")
-      DRI_CONF_OPT_END
-
    DRI_CONF_SECTION_END
    DRI_CONF_SECTION_QUALITY
-      DRI_CONF_FORCE_S3TC_ENABLE(false)
+      DRI_CONF_FORCE_S3TC_ENABLE("false")
       DRI_CONF_ALLOW_LARGE_TEXTURES(2)
    DRI_CONF_SECTION_END
    DRI_CONF_SECTION_DEBUG
-     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, true)
+      DRI_CONF_NO_RAST("false")
+      DRI_CONF_ALWAYS_FLUSH_BATCH("false")
+      DRI_CONF_ALWAYS_FLUSH_CACHE("false")
+      DRI_CONF_DISABLE_THROTTLING("false")
+      DRI_CONF_FORCE_GLSL_EXTENSIONS_WARN("false")
+      DRI_CONF_DISABLE_GLSL_LINE_CONTINUATIONS("false")
+      DRI_CONF_DISABLE_BLEND_FUNC_EXTENDED("false")
+
+      DRI_CONF_OPT_BEGIN_B(shader_precompile, "true")
         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 = 15;
+const GLuint __driNConfigOptions = 14;
 
 #include "intel_batchbuffer.h"
 #include "intel_buffers.h"
@@ -104,6 +94,10 @@ const GLuint __driNConfigOptions = 15;
 #include "intel_tex.h"
 #include "intel_regions.h"
 
+#ifndef I915
+#include "brw_context.h"
+#endif
+
 #include "i915_drm.h"
 
 #ifdef USE_NEW_INTERFACE
@@ -144,23 +138,25 @@ aub_dump_bmp(struct gl_context *ctx)
            continue;
         }
 
+         assert(irb->mt->region->pitch % irb->mt->region->cpp == 0);
         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,
+                                      irb->mt->region->pitch,
                                       0);
       }
    }
 }
 
 static const __DRItexBufferExtension intelTexBufferExtension = {
-    { __DRI_TEX_BUFFER, __DRI_TEX_BUFFER_VERSION },
-   intelSetTexBuffer,
-   intelSetTexBuffer2,
+   .base = { __DRI_TEX_BUFFER, __DRI_TEX_BUFFER_VERSION },
+
+   .setTexBuffer        = intelSetTexBuffer,
+   .setTexBuffer2       = intelSetTexBuffer2,
+   .releaseTexBuffer    = NULL,
 };
 
 static void
@@ -186,9 +182,10 @@ intelDRI2Flush(__DRIdrawable *drawable)
 }
 
 static const struct __DRI2flushExtensionRec intelFlushExtension = {
-    { __DRI2_FLUSH, __DRI2_FLUSH_VERSION },
-    intelDRI2Flush,
-    dri2InvalidateDrawable,
+    .base = { __DRI2_FLUSH, 3 },
+
+    .flush              = intelDRI2Flush,
+    .invalidate         = dri2InvalidateDrawable,
 };
 
 static struct intel_image_format intel_image_formats[] = {
@@ -292,6 +289,66 @@ intel_allocate_image(int dri_format, void *loaderPrivate)
     return image;
 }
 
+/**
+ * Sets up a DRIImage structure to point to our shared image in a region
+ */
+static void
+intel_setup_image_from_mipmap_tree(struct intel_context *intel, __DRIimage *image,
+                                   struct intel_mipmap_tree *mt, GLuint level,
+                                   GLuint zoffset)
+{
+   unsigned int draw_x, draw_y;
+   uint32_t mask_x, mask_y;
+
+   intel_miptree_check_level_layer(mt, level, zoffset);
+
+   intel_region_get_tile_masks(mt->region, &mask_x, &mask_y, false);
+   intel_miptree_get_image_offset(mt, level, zoffset, &draw_x, &draw_y);
+
+   image->width = mt->level[level].width;
+   image->height = mt->level[level].height;
+   image->tile_x = draw_x & mask_x;
+   image->tile_y = draw_y & mask_y;
+
+   image->offset = intel_region_get_aligned_offset(mt->region,
+                                                   draw_x & ~mask_x,
+                                                   draw_y & ~mask_y,
+                                                   false);
+
+   intel_region_reference(&image->region, mt->region);
+}
+
+static void
+intel_setup_image_from_dimensions(__DRIimage *image)
+{
+   image->width    = image->region->width;
+   image->height   = image->region->height;
+   image->tile_x = 0;
+   image->tile_y = 0;
+   image->has_depthstencil = false;
+}
+
+static inline uint32_t
+intel_dri_format(GLuint format)
+{
+   switch (format) {
+   case MESA_FORMAT_RGB565:
+      return __DRI_IMAGE_FORMAT_RGB565;
+   case MESA_FORMAT_XRGB8888:
+      return __DRI_IMAGE_FORMAT_XRGB8888;
+   case MESA_FORMAT_ARGB8888:
+      return __DRI_IMAGE_FORMAT_ARGB8888;
+   case MESA_FORMAT_RGBA8888_REV:
+      return __DRI_IMAGE_FORMAT_ABGR8888;
+   case MESA_FORMAT_R8:
+      return __DRI_IMAGE_FORMAT_R8;
+   case MESA_FORMAT_RG88:
+      return __DRI_IMAGE_FORMAT_GR88;
+   }
+
+   return MESA_FORMAT_NONE;
+}
+
 static __DRIimage *
 intel_create_image_from_name(__DRIscreen *screen,
                             int width, int height, int format,
@@ -302,18 +359,23 @@ intel_create_image_from_name(__DRIscreen *screen,
     int cpp;
 
     image = intel_allocate_image(format, loaderPrivate);
+    if (image == NULL)
+       return NULL;
+
     if (image->format == MESA_FORMAT_NONE)
        cpp = 1;
     else
        cpp = _mesa_get_format_bytes(image->format);
     image->region = intel_region_alloc_for_handle(intelScreen,
                                                  cpp, width, height,
-                                                 pitch, name, "image");
+                                                 pitch * cpp, name, "image");
     if (image->region == NULL) {
        free(image);
        return NULL;
     }
 
+    intel_setup_image_from_dimensions(image);
+
     return image;      
 }
 
@@ -343,28 +405,71 @@ intel_create_image_from_renderbuffer(__DRIcontext *context,
    image->offset = 0;
    image->data = loaderPrivate;
    intel_region_reference(&image->region, irb->mt->region);
+   intel_setup_image_from_dimensions(image);
+   image->dri_format = intel_dri_format(image->format);
+   image->has_depthstencil = irb->mt->stencil_mt? true : false;
 
-   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;
+   rb->NeedsFinishRenderTexture = true;
+   return image;
+}
+
+static __DRIimage *
+intel_create_image_from_texture(__DRIcontext *context, int target,
+                                unsigned texture, int zoffset,
+                                int level,
+                                unsigned *error,
+                                void *loaderPrivate)
+{
+   __DRIimage *image;
+   struct intel_context *intel = context->driverPrivate;
+   struct gl_texture_object *obj;
+   struct intel_texture_object *iobj;
+   GLuint face = 0;
+
+   obj = _mesa_lookup_texture(&intel->ctx, texture);
+   if (!obj || obj->Target != target) {
+      *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
+      return NULL;
+   }
+
+   if (target == GL_TEXTURE_CUBE_MAP)
+      face = zoffset;
+
+   _mesa_test_texobj_completeness(&intel->ctx, obj);
+   iobj = intel_texture_object(obj);
+   if (!obj->_BaseComplete || (level > 0 && !obj->_MipmapComplete)) {
+      *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
+      return NULL;
+   }
+
+   if (level < obj->BaseLevel || level > obj->_MaxLevel) {
+      *error = __DRI_IMAGE_ERROR_BAD_MATCH;
+      return NULL;
+   }
+
+   if (target == GL_TEXTURE_3D && obj->Image[face][level]->Depth < zoffset) {
+      *error = __DRI_IMAGE_ERROR_BAD_MATCH;
+      return NULL;
+   }
+   image = calloc(1, sizeof *image);
+   if (image == NULL) {
+      *error = __DRI_IMAGE_ERROR_BAD_ALLOC;
+      return NULL;
+   }
+
+   image->internal_format = obj->Image[face][level]->InternalFormat;
+   image->format = obj->Image[face][level]->TexFormat;
+   image->data = loaderPrivate;
+   intel_setup_image_from_mipmap_tree(intel, image, iobj->mt, level, zoffset);
+   image->dri_format = intel_dri_format(image->format);
+   image->has_depthstencil = iobj->mt->stencil_mt? true : false;
+   if (image->dri_format == MESA_FORMAT_NONE) {
+      *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
+      free(image);
+      return NULL;
    }
 
+   *error = __DRI_IMAGE_ERROR_SUCCESS;
    return image;
 }
 
@@ -394,6 +499,9 @@ intel_create_image(__DRIscreen *screen,
    }
 
    image = intel_allocate_image(format, loaderPrivate);
+   if (image == NULL)
+      return NULL;
+
    cpp = _mesa_get_format_bytes(image->format);
    image->region =
       intel_region_alloc(intelScreen, tiling, cpp, width, height, true);
@@ -402,6 +510,8 @@ intel_create_image(__DRIscreen *screen,
       return NULL;
    }
    
+   intel_setup_image_from_dimensions(image);
+
    return image;
 }
 
@@ -410,7 +520,7 @@ intel_query_image(__DRIimage *image, int attrib, int *value)
 {
    switch (attrib) {
    case __DRI_IMAGE_ATTRIB_STRIDE:
-      *value = image->region->pitch * image->region->cpp;
+      *value = image->region->pitch;
       return true;
    case __DRI_IMAGE_ATTRIB_HANDLE:
       *value = image->region->bo->handle;
@@ -431,6 +541,10 @@ intel_query_image(__DRIimage *image, int attrib, int *value)
          return false;
       *value = image->planar_format->components;
       return true;
+   case __DRI_IMAGE_ATTRIB_FD:
+      if (drm_intel_bo_gem_export_to_prime(image->region->bo, value) == 0)
+         return true;
+      return false;
   default:
       return false;
    }
@@ -456,6 +570,11 @@ intel_dup_image(__DRIimage *orig_image, void *loaderPrivate)
    image->dri_format      = orig_image->dri_format;
    image->format          = orig_image->format;
    image->offset          = orig_image->offset;
+   image->width           = orig_image->width;
+   image->height          = orig_image->height;
+   image->tile_x          = orig_image->tile_x;
+   image->tile_y          = orig_image->tile_y;
+   image->has_depthstencil = orig_image->has_depthstencil;
    image->data            = loaderPrivate;
 
    memcpy(image->strides, orig_image->strides, sizeof(image->strides));
@@ -503,8 +622,8 @@ intel_create_image_from_names(__DRIscreen *screen,
                                          names[0], strides[0],
                                          loaderPrivate);
 
-    if (image == NULL)
-        return NULL;
+   if (image == NULL)
+      return NULL;
 
     image->planar_format = f;
     for (i = 0; i < f->nplanes; i++) {
@@ -516,10 +635,56 @@ intel_create_image_from_names(__DRIscreen *screen,
     return image;
 }
 
+static __DRIimage *
+intel_create_image_from_fds(__DRIscreen *screen,
+                            int width, int height, int fourcc,
+                            int *fds, int num_fds, int *strides, int *offsets,
+                            void *loaderPrivate)
+{
+   struct intel_screen *intelScreen = screen->driverPrivate;
+   struct intel_image_format *f = NULL;
+   __DRIimage *image;
+   int i, index;
+
+   if (fds == NULL || num_fds != 1)
+      return NULL;
+
+   for (i = 0; i < ARRAY_SIZE(intel_image_formats); i++) {
+      if (intel_image_formats[i].fourcc == fourcc) {
+         f = &intel_image_formats[i];
+      }
+   }
+
+   if (f == NULL)
+      return NULL;
+
+   image = intel_allocate_image(__DRI_IMAGE_FORMAT_NONE, loaderPrivate);
+   if (image == NULL)
+      return NULL;
+
+   image->region = intel_region_alloc_for_fd(intelScreen,
+                                             1, width, height,
+                                             strides[0], fds[0], "image");
+   if (image->region == NULL) {
+      free(image);
+      return NULL;
+   }
+
+   image->planar_format = f;
+   for (i = 0; i < f->nplanes; i++) {
+      index = f->planes[i].buffer_index;
+      image->offsets[index] = offsets[index];
+      image->strides[index] = strides[index];
+   }
+
+   return image;
+}
+
+
 static __DRIimage *
 intel_from_planar(__DRIimage *parent, int plane, void *loaderPrivate)
 {
-    int width, height, offset, stride, dri_format, cpp, index, pitch;
+    int width, height, offset, stride, dri_format, index;
     struct intel_image_format *f;
     uint32_t mask_x, mask_y;
     __DRIimage *image;
@@ -540,9 +705,10 @@ intel_from_planar(__DRIimage *parent, int plane, void *loaderPrivate)
     stride = parent->strides[index];
 
     image = intel_allocate_image(dri_format, loaderPrivate);
-    cpp = _mesa_get_format_bytes(image->format); /* safe since no none format */
-    pitch = stride / cpp;
-    if (offset + height * cpp * pitch > parent->region->bo->size) {
+    if (image == NULL)
+       return NULL;
+
+    if (offset + height * stride > parent->region->bo->size) {
        _mesa_warning(NULL, "intel_create_sub_image: subimage out of bounds");
        free(image);
        return NULL;
@@ -557,13 +723,13 @@ intel_from_planar(__DRIimage *parent, int plane, void *loaderPrivate)
     image->region->cpp = _mesa_get_format_bytes(image->format);
     image->region->width = width;
     image->region->height = height;
-    image->region->pitch = pitch;
+    image->region->pitch = stride;
     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_setup_image_from_dimensions(image);
 
     intel_region_get_tile_masks(image->region, &mask_x, &mask_y, false);
     if (offset & mask_x)
@@ -574,16 +740,19 @@ intel_from_planar(__DRIimage *parent, int plane, void *loaderPrivate)
 }
 
 static struct __DRIimageExtensionRec intelImageExtension = {
-    { __DRI_IMAGE, 5 },
-    intel_create_image_from_name,
-    intel_create_image_from_renderbuffer,
-    intel_destroy_image,
-    intel_create_image,
-    intel_query_image,
-    intel_dup_image,
-    intel_validate_usage,
-    intel_create_image_from_names,
-    intel_from_planar
+    .base = { __DRI_IMAGE, 7 },
+
+    .createImageFromName                = intel_create_image_from_name,
+    .createImageFromRenderbuffer        = intel_create_image_from_renderbuffer,
+    .destroyImage                       = intel_destroy_image,
+    .createImage                        = intel_create_image,
+    .queryImage                         = intel_query_image,
+    .dupImage                           = intel_dup_image,
+    .validateUsage                      = intel_validate_usage,
+    .createImageFromNames               = intel_create_image_from_names,
+    .fromPlanar                         = intel_from_planar,
+    .createImageFromTexture             = intel_create_image_from_texture,
+    .createImageFromFds                 = intel_create_image_from_fds
 };
 
 static const __DRIextension *intelScreenExtensions[] = {
@@ -621,11 +790,6 @@ intel_get_boolean(__DRIscreen *psp, int param)
    return intel_get_param(psp, param, &value) && value;
 }
 
-static void
-nop_callback(GLuint key, void *data, void *userData)
-{
-}
-
 static void
 intelDestroyScreen(__DRIscreen * sPriv)
 {
@@ -634,12 +798,6 @@ intelDestroyScreen(__DRIscreen * sPriv)
    dri_bufmgr_destroy(intelScreen->bufmgr);
    driDestroyOptionInfo(&intelScreen->optionCache);
 
-   /* Some regions may still have references to them at this point, so
-    * flush the hash table to prevent _mesa_DeleteHashTable() from
-    * complaining about the hash not being empty; */
-   _mesa_HashDeleteAll(intelScreen->named_regions, nop_callback, NULL);
-   _mesa_DeleteHashTable(intelScreen->named_regions);
-
    free(intelScreen);
    sPriv->driverPrivate = NULL;
 }
@@ -670,10 +828,19 @@ intelCreateBuffer(__DRIscreen * driScrnPriv,
 
    if (mesaVis->redBits == 5)
       rgbFormat = MESA_FORMAT_RGB565;
+   else if (mesaVis->sRGBCapable)
+      rgbFormat = MESA_FORMAT_SARGB8;
    else if (mesaVis->alphaBits == 0)
       rgbFormat = MESA_FORMAT_XRGB8888;
-   else
-      rgbFormat = MESA_FORMAT_ARGB8888;
+   else {
+      if (screen->gen >= 4) {
+         rgbFormat = MESA_FORMAT_SARGB8;
+         fb->Visual.sRGBCapable = true;
+      } else {
+         rgbFormat = MESA_FORMAT_ARGB8888;
+      }
+
+   }
 
    /* setup the hardware-based renderbuffers */
    rb = intel_create_renderbuffer(rgbFormat, num_samples);
@@ -747,8 +914,12 @@ intelDestroyBuffer(__DRIdrawable * driDrawPriv)
  * functions.
  */
 extern bool
-i830CreateContext(const struct gl_config *mesaVis,
+i830CreateContext(int api,
+                  const struct gl_config *mesaVis,
                  __DRIcontext *driContextPriv,
+                 unsigned major_version,
+                 unsigned minor_version,
+                 unsigned *error,
                  void *sharedContextPrivate);
 
 extern bool
@@ -790,27 +961,10 @@ intelCreateContext(gl_api api,
                                   major_version, minor_version, error,
                                   sharedContextPrivate);
    } else {
-      switch (api) {
-      case API_OPENGL:
-         if (major_version > 1 || minor_version > 3) {
-            *error = __DRI_CTX_ERROR_BAD_VERSION;
-            success = false;
-         }
-         break;
-      case API_OPENGLES:
-         break;
-      default:
-         *error = __DRI_CTX_ERROR_BAD_API;
-         success = false;
-      }
-
-      if (success) {
-         intelScreen->no_vbo = true;
-         success = i830CreateContext(mesaVis, driContextPriv,
-                                     sharedContextPrivate);
-         if (!success)
-            *error = __DRI_CTX_ERROR_NO_MEMORY;
-      }
+      intelScreen->no_vbo = true;
+      success = i830CreateContext(api, mesaVis, driContextPriv,
+                                  major_version, minor_version, error,
+                                  sharedContextPrivate);
    }
 #else
    success = brwCreateContext(api, mesaVis,
@@ -832,7 +986,6 @@ 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;
 
@@ -843,19 +996,12 @@ intel_init_bufmgr(struct intel_screen *intelScreen)
       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 false;
-   }
-
    drm_intel_bufmgr_gem_enable_fenced_relocs(intelScreen->bufmgr);
 
-   intelScreen->named_regions = _mesa_NewHashTable();
-
-   intelScreen->relaxed_relocations = 0;
-   intelScreen->relaxed_relocations |=
-      intel_get_boolean(spriv, I915_PARAM_HAS_RELAXED_DELTA) << 0;
+   if (!intel_get_boolean(spriv, I915_PARAM_HAS_RELAXED_DELTA)) {
+      fprintf(stderr, "[%s: %u] Kernel 2.6.39 required.\n", __func__, __LINE__);
+      return false;
+   }
 
    return true;
 }
@@ -912,6 +1058,11 @@ intel_detect_swizzling(struct intel_screen *screen)
 static __DRIconfig**
 intel_screen_make_configs(__DRIscreen *dri_screen)
 {
+   static const gl_format formats[] = {
+      MESA_FORMAT_RGB565,
+      MESA_FORMAT_ARGB8888
+   };
+
    /* GLX_SWAP_COPY_OML is not supported due to page flipping. */
    static const GLenum back_buffer_modes[] = {
        GLX_SWAP_UNDEFINED_OML, GLX_NONE,
@@ -921,41 +1072,35 @@ intel_screen_make_configs(__DRIscreen *dri_screen)
    static const uint8_t multisample_samples[2]  = {4, 8};
 
    struct intel_screen *screen = dri_screen->driverPrivate;
-   GLenum fb_format[3];
-   GLenum fb_type[3];
    uint8_t depth_bits[4], stencil_bits[4];
    __DRIconfig **configs = NULL;
 
-   fb_format[0] = GL_RGB;
-   fb_type[0] = GL_UNSIGNED_SHORT_5_6_5;
-
-   fb_format[1] = GL_BGR;
-   fb_type[1] = GL_UNSIGNED_INT_8_8_8_8_REV;
-
-   fb_format[2] = GL_BGRA;
-   fb_type[2] = GL_UNSIGNED_INT_8_8_8_8_REV;
-
    /* Generate singlesample configs without accumulation buffer. */
-   for (int i = 0; i < ARRAY_SIZE(fb_format); i++) {
+   for (int i = 0; i < ARRAY_SIZE(formats); i++) {
       __DRIconfig **new_configs;
-      const int num_depth_stencil_bits = 2;
+      int num_depth_stencil_bits = 2;
 
       /* Starting with DRI2 protocol version 1.1 we can request a depth/stencil
        * buffer that has a different number of bits per pixel than the color
-       * buffer.  This isn't yet supported here.
+       * buffer, gen >= 6 supports this.
        */
       depth_bits[0] = 0;
       stencil_bits[0] = 0;
 
-      if (fb_type[i] == GL_UNSIGNED_SHORT_5_6_5) {
+      if (formats[i] == MESA_FORMAT_RGB565) {
          depth_bits[1] = 16;
          stencil_bits[1] = 0;
+         if (screen->gen >= 6) {
+             depth_bits[2] = 24;
+             stencil_bits[2] = 8;
+             num_depth_stencil_bits = 3;
+         }
       } else {
          depth_bits[1] = 24;
          stencil_bits[1] = 8;
       }
 
-      new_configs = driCreateConfigs(fb_format[i], fb_type[i],
+      new_configs = driCreateConfigs(formats[i],
                                      depth_bits,
                                      stencil_bits,
                                      num_depth_stencil_bits,
@@ -968,10 +1113,10 @@ intel_screen_make_configs(__DRIscreen *dri_screen)
    /* Generate the minimum possible set of configs that include an
     * accumulation buffer.
     */
-   for (int i = 0; i < ARRAY_SIZE(fb_format); i++) {
+   for (int i = 0; i < ARRAY_SIZE(formats); i++) {
       __DRIconfig **new_configs;
 
-      if (fb_type[i] == GL_UNSIGNED_SHORT_5_6_5) {
+      if (formats[i] == MESA_FORMAT_RGB565) {
          depth_bits[0] = 16;
          stencil_bits[0] = 0;
       } else {
@@ -979,7 +1124,7 @@ intel_screen_make_configs(__DRIscreen *dri_screen)
          stencil_bits[0] = 8;
       }
 
-      new_configs = driCreateConfigs(fb_format[i], fb_type[i],
+      new_configs = driCreateConfigs(formats[i],
                                      depth_bits, stencil_bits, 1,
                                      back_buffer_modes, 1,
                                      singlesample_samples, 1,
@@ -1000,7 +1145,7 @@ intel_screen_make_configs(__DRIscreen *dri_screen)
     * supported.  Singlebuffer configs are not supported because no one wants
     * them.
     */
-   for (int i = 0; i < ARRAY_SIZE(fb_format); i++) {
+   for (int i = 0; i < ARRAY_SIZE(formats); i++) {
       if (screen->gen < 6)
          break;
 
@@ -1011,7 +1156,7 @@ intel_screen_make_configs(__DRIscreen *dri_screen)
       depth_bits[0] = 0;
       stencil_bits[0] = 0;
 
-      if (fb_type[i] == GL_UNSIGNED_SHORT_5_6_5) {
+      if (formats[i] == MESA_FORMAT_RGB565) {
          depth_bits[1] = 16;
          stencil_bits[1] = 0;
       } else {
@@ -1024,7 +1169,7 @@ intel_screen_make_configs(__DRIscreen *dri_screen)
       else if (screen->gen == 6)
          num_msaa_modes = 1;
 
-      new_configs = driCreateConfigs(fb_format[i], fb_type[i],
+      new_configs = driCreateConfigs(formats[i],
                                      depth_bits,
                                      stencil_bits,
                                      num_depth_stencil_bits,
@@ -1044,6 +1189,67 @@ intel_screen_make_configs(__DRIscreen *dri_screen)
    return configs;
 }
 
+static void
+set_max_gl_versions(struct intel_screen *screen)
+{
+   int gl_version_override = _mesa_get_gl_version_override();
+
+   switch (screen->gen) {
+   case 7:
+      screen->max_gl_core_version = 31;
+      screen->max_gl_compat_version = 30;
+      screen->max_gl_es1_version = 11;
+      screen->max_gl_es2_version = 30;
+      break;
+   case 6:
+      screen->max_gl_core_version = 31;
+      screen->max_gl_compat_version = 30;
+      screen->max_gl_es1_version = 11;
+      screen->max_gl_es2_version = 30;
+      break;
+   case 5:
+   case 4:
+      screen->max_gl_core_version = 0;
+      screen->max_gl_compat_version = 21;
+      screen->max_gl_es1_version = 11;
+      screen->max_gl_es2_version = 20;
+      break;
+   case 3: {
+      screen->max_gl_core_version = 0;
+      screen->max_gl_es1_version = 11;
+      screen->max_gl_compat_version = 21;
+      screen->max_gl_es2_version = 20;
+
+      break;
+   }
+   case 2:
+      screen->max_gl_core_version = 0;
+      screen->max_gl_compat_version = 13;
+      screen->max_gl_es1_version = 11;
+      screen->max_gl_es2_version = 0;
+      break;
+   default:
+      assert(!"unrecognized intel_screen::gen");
+      break;
+   }
+
+   if (gl_version_override >= 31) {
+      screen->max_gl_core_version = MAX2(screen->max_gl_core_version,
+                                         gl_version_override);
+   } else {
+      screen->max_gl_compat_version = MAX2(screen->max_gl_compat_version,
+                                           gl_version_override);
+   }
+
+#ifndef FEATURE_ES1
+   screen->max_gl_es1_version = 0;
+#endif
+
+#ifndef FEATURE_ES2
+   screen->max_gl_es2_version = 0;
+#endif
+}
+
 /**
  * This is the driver specific part of the createNewScreen entry point.
  * Called when using DRI2.
@@ -1054,7 +1260,6 @@ static const
 __DRIconfig **intelInitScreen2(__DRIscreen *psp)
 {
    struct intel_screen *intelScreen;
-   unsigned int api_mask;
 
    if (psp->dri2.loader->base.version <= 2 ||
        psp->dri2.loader->getBuffersWithFormat == NULL) {
@@ -1082,10 +1287,6 @@ __DRIconfig **intelInitScreen2(__DRIscreen *psp)
 
    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)) {
@@ -1100,6 +1301,13 @@ __DRIconfig **intelInitScreen2(__DRIscreen *psp)
       intelScreen->gen = 2;
    }
 
+   if (intelScreen->gen == 7 &&
+       !intel_get_boolean(intelScreen->driScrnPriv,
+                          I915_PARAM_HAS_GEN7_SOL_RESET)) {
+      fprintf(stderr, "i965 requires Kernel 3.3 or later.\n");
+      return false;
+   }
+
    intelScreen->hw_has_separate_stencil = intelScreen->gen >= 6;
    intelScreen->hw_must_use_separate_stencil = intelScreen->gen >= 7;
 
@@ -1113,18 +1321,19 @@ __DRIconfig **intelInitScreen2(__DRIscreen *psp)
 
    intel_override_separate_stencil(intelScreen);
 
-   api_mask = (1 << __DRI_API_OPENGL);
-#if FEATURE_ES1
-   api_mask |= (1 << __DRI_API_GLES);
-#endif
-#if FEATURE_ES2
-   api_mask |= (1 << __DRI_API_GLES2);
-#endif
+   intelScreen->hw_has_swizzling = intel_detect_swizzling(intelScreen);
 
-   if (IS_9XX(intelScreen->deviceID) || IS_965(intelScreen->deviceID))
-      psp->api_mask = api_mask;
+   set_max_gl_versions(intelScreen);
 
-   intelScreen->hw_has_swizzling = intel_detect_swizzling(intelScreen);
+   psp->api_mask = (1 << __DRI_API_OPENGL);
+   if (intelScreen->max_gl_core_version > 0)
+      psp->api_mask |= (1 << __DRI_API_OPENGL_CORE);
+   if (intelScreen->max_gl_es1_version > 0)
+      psp->api_mask |= (1 << __DRI_API_GLES);
+   if (intelScreen->max_gl_es2_version > 0)
+      psp->api_mask |= (1 << __DRI_API_GLES2);
+   if (intelScreen->max_gl_es2_version >= 30)
+      psp->api_mask |= (1 << __DRI_API_GLES3);
 
    psp->extensions = intelScreenExtensions;
 
@@ -1168,8 +1377,7 @@ intelAllocateBuffer(__DRIscreen *screen,
 
    intelBuffer->base.attachment = attachment;
    intelBuffer->base.cpp = intelBuffer->region->cpp;
-   intelBuffer->base.pitch =
-         intelBuffer->region->pitch * intelBuffer->region->cpp;
+   intelBuffer->base.pitch = intelBuffer->region->pitch;
 
    return &intelBuffer->base;
 }