intel_screen.c revision c6abde211fa875f90e59e3709720cfe394669069
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#include <errno.h>
29#include "main/glheader.h"
30#include "main/context.h"
31#include "main/framebuffer.h"
32#include "main/renderbuffer.h"
33#include "main/hash.h"
34#include "main/fbobject.h"
35#include "main/mfeatures.h"
36
37#include "utils.h"
38#include "xmlpool.h"
39
40PUBLIC const char __driConfigOptions[] =
41   DRI_CONF_BEGIN
42   DRI_CONF_SECTION_PERFORMANCE
43      DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_ALWAYS_SYNC)
44      /* Options correspond to DRI_CONF_BO_REUSE_DISABLED,
45       * DRI_CONF_BO_REUSE_ALL
46       */
47      DRI_CONF_OPT_BEGIN_V(bo_reuse, enum, 1, "0:1")
48	 DRI_CONF_DESC_BEGIN(en, "Buffer object reuse")
49	    DRI_CONF_ENUM(0, "Disable buffer object reuse")
50	    DRI_CONF_ENUM(1, "Enable reuse of all sizes of buffer objects")
51	 DRI_CONF_DESC_END
52      DRI_CONF_OPT_END
53
54      DRI_CONF_OPT_BEGIN(texture_tiling, bool, true)
55	 DRI_CONF_DESC(en, "Enable texture tiling")
56      DRI_CONF_OPT_END
57
58      DRI_CONF_OPT_BEGIN(early_z, bool, false)
59	 DRI_CONF_DESC(en, "Enable early Z in classic mode (unstable, 945-only).")
60      DRI_CONF_OPT_END
61
62      DRI_CONF_OPT_BEGIN(fragment_shader, bool, true)
63	 DRI_CONF_DESC(en, "Enable limited ARB_fragment_shader support on 915/945.")
64      DRI_CONF_OPT_END
65
66   DRI_CONF_SECTION_END
67   DRI_CONF_SECTION_QUALITY
68      DRI_CONF_FORCE_S3TC_ENABLE(false)
69      DRI_CONF_ALLOW_LARGE_TEXTURES(2)
70   DRI_CONF_SECTION_END
71   DRI_CONF_SECTION_DEBUG
72     DRI_CONF_NO_RAST(false)
73     DRI_CONF_ALWAYS_FLUSH_BATCH(false)
74     DRI_CONF_ALWAYS_FLUSH_CACHE(false)
75
76      DRI_CONF_OPT_BEGIN(stub_occlusion_query, bool, false)
77	 DRI_CONF_DESC(en, "Enable stub ARB_occlusion_query support on 915/945.")
78      DRI_CONF_OPT_END
79
80      DRI_CONF_OPT_BEGIN(shader_precompile, bool, false)
81	 DRI_CONF_DESC(en, "Perform code generation at shader link time.")
82      DRI_CONF_OPT_END
83   DRI_CONF_SECTION_END
84DRI_CONF_END;
85
86const GLuint __driNConfigOptions = 12;
87
88#include "intel_batchbuffer.h"
89#include "intel_buffers.h"
90#include "intel_bufmgr.h"
91#include "intel_chipset.h"
92#include "intel_fbo.h"
93#include "intel_mipmap_tree.h"
94#include "intel_screen.h"
95#include "intel_tex.h"
96#include "intel_regions.h"
97
98#include "i915_drm.h"
99
100#ifdef USE_NEW_INTERFACE
101static PFNGLXCREATECONTEXTMODES create_context_modes = NULL;
102#endif /*USE_NEW_INTERFACE */
103
104static const __DRItexBufferExtension intelTexBufferExtension = {
105    { __DRI_TEX_BUFFER, __DRI_TEX_BUFFER_VERSION },
106   intelSetTexBuffer,
107   intelSetTexBuffer2,
108};
109
110static void
111intelDRI2Flush(__DRIdrawable *drawable)
112{
113   GET_CURRENT_CONTEXT(ctx);
114   struct intel_context *intel = intel_context(ctx);
115
116   if (intel->gen < 4)
117      INTEL_FIREVERTICES(intel);
118
119   intel->need_throttle = true;
120
121   if (intel->batch.used)
122      intel_batchbuffer_flush(intel);
123}
124
125static const struct __DRI2flushExtensionRec intelFlushExtension = {
126    { __DRI2_FLUSH, __DRI2_FLUSH_VERSION },
127    intelDRI2Flush,
128    dri2InvalidateDrawable,
129};
130
131static __DRIimage *
132intel_create_image_from_name(__DRIscreen *screen,
133			     int width, int height, int format,
134			     int name, int pitch, void *loaderPrivate)
135{
136    struct intel_screen *intelScreen = screen->driverPrivate;
137    __DRIimage *image;
138    int cpp;
139
140    image = CALLOC(sizeof *image);
141    if (image == NULL)
142	return NULL;
143
144    switch (format) {
145    case __DRI_IMAGE_FORMAT_RGB565:
146       image->format = MESA_FORMAT_RGB565;
147       image->internal_format = GL_RGB;
148       image->data_type = GL_UNSIGNED_BYTE;
149       break;
150    case __DRI_IMAGE_FORMAT_XRGB8888:
151       image->format = MESA_FORMAT_XRGB8888;
152       image->internal_format = GL_RGB;
153       image->data_type = GL_UNSIGNED_BYTE;
154       break;
155    case __DRI_IMAGE_FORMAT_ARGB8888:
156       image->format = MESA_FORMAT_ARGB8888;
157       image->internal_format = GL_RGBA;
158       image->data_type = GL_UNSIGNED_BYTE;
159       break;
160    case __DRI_IMAGE_FORMAT_ABGR8888:
161       image->format = MESA_FORMAT_RGBA8888_REV;
162       image->internal_format = GL_RGBA;
163       image->data_type = GL_UNSIGNED_BYTE;
164       break;
165    default:
166       free(image);
167       return NULL;
168    }
169
170    image->data = loaderPrivate;
171    cpp = _mesa_get_format_bytes(image->format);
172
173    image->region = intel_region_alloc_for_handle(intelScreen,
174						  cpp, width, height,
175						  pitch, name, "image");
176    if (image->region == NULL) {
177       FREE(image);
178       return NULL;
179    }
180
181    return image;
182}
183
184static __DRIimage *
185intel_create_image_from_renderbuffer(__DRIcontext *context,
186				     int renderbuffer, void *loaderPrivate)
187{
188   __DRIimage *image;
189   struct intel_context *intel = context->driverPrivate;
190   struct gl_renderbuffer *rb;
191   struct intel_renderbuffer *irb;
192
193   rb = _mesa_lookup_renderbuffer(&intel->ctx, renderbuffer);
194   if (!rb) {
195      _mesa_error(&intel->ctx,
196		  GL_INVALID_OPERATION, "glRenderbufferExternalMESA");
197      return NULL;
198   }
199
200   irb = intel_renderbuffer(rb);
201   image = CALLOC(sizeof *image);
202   if (image == NULL)
203      return NULL;
204
205   image->internal_format = rb->InternalFormat;
206   image->format = rb->Format;
207   image->data_type = rb->DataType;
208   image->data = loaderPrivate;
209   intel_region_reference(&image->region, irb->mt->region);
210
211   return image;
212}
213
214static void
215intel_destroy_image(__DRIimage *image)
216{
217    intel_region_release(&image->region);
218    FREE(image);
219}
220
221static __DRIimage *
222intel_create_image(__DRIscreen *screen,
223		   int width, int height, int format,
224		   unsigned int use,
225		   void *loaderPrivate)
226{
227   __DRIimage *image;
228   struct intel_screen *intelScreen = screen->driverPrivate;
229   uint32_t tiling;
230   int cpp;
231
232   tiling = I915_TILING_X;
233   if (use & __DRI_IMAGE_USE_CURSOR) {
234      if (width != 64 || height != 64)
235	 return NULL;
236      tiling = I915_TILING_NONE;
237   }
238
239   image = CALLOC(sizeof *image);
240   if (image == NULL)
241      return NULL;
242
243   switch (format) {
244   case __DRI_IMAGE_FORMAT_RGB565:
245      image->format = MESA_FORMAT_RGB565;
246      image->internal_format = GL_RGB;
247      image->data_type = GL_UNSIGNED_BYTE;
248      break;
249   case __DRI_IMAGE_FORMAT_XRGB8888:
250      image->format = MESA_FORMAT_XRGB8888;
251      image->internal_format = GL_RGB;
252      image->data_type = GL_UNSIGNED_BYTE;
253      break;
254   case __DRI_IMAGE_FORMAT_ARGB8888:
255      image->format = MESA_FORMAT_ARGB8888;
256      image->internal_format = GL_RGBA;
257      image->data_type = GL_UNSIGNED_BYTE;
258      break;
259    case __DRI_IMAGE_FORMAT_ABGR8888:
260       image->format = MESA_FORMAT_RGBA8888_REV;
261       image->internal_format = GL_RGBA;
262       image->data_type = GL_UNSIGNED_BYTE;
263       break;
264   default:
265      free(image);
266      return NULL;
267   }
268
269   image->data = loaderPrivate;
270   cpp = _mesa_get_format_bytes(image->format);
271
272   image->region =
273      intel_region_alloc(intelScreen, tiling,
274			 cpp, width, height, true);
275   if (image->region == NULL) {
276      FREE(image);
277      return NULL;
278   }
279
280   return image;
281}
282
283static GLboolean
284intel_query_image(__DRIimage *image, int attrib, int *value)
285{
286   switch (attrib) {
287   case __DRI_IMAGE_ATTRIB_STRIDE:
288      *value = image->region->pitch * image->region->cpp;
289      return true;
290   case __DRI_IMAGE_ATTRIB_HANDLE:
291      *value = image->region->bo->handle;
292      return true;
293   case __DRI_IMAGE_ATTRIB_NAME:
294      return intel_region_flink(image->region, (uint32_t *) value);
295   default:
296      return false;
297   }
298}
299
300static __DRIimage *
301intel_dup_image(__DRIimage *orig_image, void *loaderPrivate)
302{
303   __DRIimage *image;
304
305   image = CALLOC(sizeof *image);
306   if (image == NULL)
307      return NULL;
308
309   intel_region_reference(&image->region, orig_image->region);
310   if (image->region == NULL) {
311      FREE(image);
312      return NULL;
313   }
314
315   image->internal_format = orig_image->internal_format;
316   image->format          = orig_image->format;
317   image->data_type       = orig_image->data_type;
318   image->data            = loaderPrivate;
319
320   return image;
321}
322
323static struct __DRIimageExtensionRec intelImageExtension = {
324    { __DRI_IMAGE, __DRI_IMAGE_VERSION },
325    intel_create_image_from_name,
326    intel_create_image_from_renderbuffer,
327    intel_destroy_image,
328    intel_create_image,
329    intel_query_image,
330    intel_dup_image
331};
332
333static const __DRIextension *intelScreenExtensions[] = {
334    &intelTexBufferExtension.base,
335    &intelFlushExtension.base,
336    &intelImageExtension.base,
337    &dri2ConfigQueryExtension.base,
338    NULL
339};
340
341static bool
342intel_get_param(__DRIscreen *psp, int param, int *value)
343{
344   int ret;
345   struct drm_i915_getparam gp;
346
347   gp.param = param;
348   gp.value = value;
349
350   ret = drmCommandWriteRead(psp->fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
351   if (ret) {
352      if (ret != -EINVAL)
353	 _mesa_warning(NULL, "drm_i915_getparam: %d", ret);
354      return false;
355   }
356
357   return true;
358}
359
360static bool
361intel_get_boolean(__DRIscreen *psp, int param)
362{
363   int value = 0;
364   return intel_get_param(psp, param, &value) && value;
365}
366
367static void
368nop_callback(GLuint key, void *data, void *userData)
369{
370}
371
372static void
373intelDestroyScreen(__DRIscreen * sPriv)
374{
375   struct intel_screen *intelScreen = sPriv->driverPrivate;
376
377   dri_bufmgr_destroy(intelScreen->bufmgr);
378   driDestroyOptionInfo(&intelScreen->optionCache);
379
380   /* Some regions may still have references to them at this point, so
381    * flush the hash table to prevent _mesa_DeleteHashTable() from
382    * complaining about the hash not being empty; */
383   _mesa_HashDeleteAll(intelScreen->named_regions, nop_callback, NULL);
384   _mesa_DeleteHashTable(intelScreen->named_regions);
385
386   FREE(intelScreen);
387   sPriv->driverPrivate = NULL;
388}
389
390
391/**
392 * This is called when we need to set up GL rendering to a new X window.
393 */
394static GLboolean
395intelCreateBuffer(__DRIscreen * driScrnPriv,
396                  __DRIdrawable * driDrawPriv,
397                  const struct gl_config * mesaVis, GLboolean isPixmap)
398{
399   struct intel_renderbuffer *rb;
400   struct intel_screen *screen = (struct intel_screen*) driScrnPriv->driverPrivate;
401
402   if (isPixmap) {
403      return false;          /* not implemented */
404   }
405   else {
406      gl_format rgbFormat;
407
408      struct gl_framebuffer *fb = CALLOC_STRUCT(gl_framebuffer);
409
410      if (!fb)
411	 return false;
412
413      _mesa_initialize_window_framebuffer(fb, mesaVis);
414
415      if (mesaVis->redBits == 5)
416	 rgbFormat = MESA_FORMAT_RGB565;
417      else if (mesaVis->alphaBits == 0)
418	 rgbFormat = MESA_FORMAT_XRGB8888;
419      else
420	 rgbFormat = MESA_FORMAT_ARGB8888;
421
422      /* setup the hardware-based renderbuffers */
423      rb = intel_create_renderbuffer(rgbFormat);
424      _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &rb->Base);
425
426      if (mesaVis->doubleBufferMode) {
427	 rb = intel_create_renderbuffer(rgbFormat);
428         _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &rb->Base);
429      }
430
431      /*
432       * Assert here that the gl_config has an expected depth/stencil bit
433       * combination: one of d24/s8, d16/s0, d0/s0. (See intelInitScreen2(),
434       * which constructs the advertised configs.)
435       */
436      if (mesaVis->depthBits == 24) {
437	 assert(mesaVis->stencilBits == 8);
438
439	 if (screen->hw_has_separate_stencil
440	     && screen->dri2_has_hiz != INTEL_DRI2_HAS_HIZ_FALSE) {
441	    /*
442	     * Request a separate stencil buffer even if we do not yet know if
443	     * the screen supports it. (See comments for
444	     * enum intel_dri2_has_hiz).
445	     */
446	    rb = intel_create_renderbuffer(MESA_FORMAT_X8_Z24);
447	    _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &rb->Base);
448	    rb = intel_create_renderbuffer(MESA_FORMAT_S8);
449	    _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &rb->Base);
450	 } else {
451	    /*
452	     * Use combined depth/stencil. Note that the renderbuffer is
453	     * attached to two attachment points.
454	     */
455	    rb = intel_create_renderbuffer(MESA_FORMAT_S8_Z24);
456	    _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &rb->Base);
457	    _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &rb->Base);
458	 }
459      }
460      else if (mesaVis->depthBits == 16) {
461	 assert(mesaVis->stencilBits == 0);
462         /* just 16-bit depth buffer, no hw stencil */
463         struct intel_renderbuffer *depthRb
464	    = intel_create_renderbuffer(MESA_FORMAT_Z16);
465         _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
466      }
467      else {
468	 assert(mesaVis->depthBits == 0);
469	 assert(mesaVis->stencilBits == 0);
470      }
471
472      /* now add any/all software-based renderbuffers we may need */
473      _mesa_add_soft_renderbuffers(fb,
474                                   false, /* never sw color */
475                                   false, /* never sw depth */
476                                   false, /* never sw stencil */
477                                   mesaVis->accumRedBits > 0,
478                                   false, /* never sw alpha */
479                                   false  /* never sw aux */ );
480      driDrawPriv->driverPrivate = fb;
481
482      return true;
483   }
484}
485
486static void
487intelDestroyBuffer(__DRIdrawable * driDrawPriv)
488{
489    struct gl_framebuffer *fb = driDrawPriv->driverPrivate;
490
491    _mesa_reference_framebuffer(&fb, NULL);
492}
493
494/* There are probably better ways to do this, such as an
495 * init-designated function to register chipids and createcontext
496 * functions.
497 */
498extern bool
499i830CreateContext(const struct gl_config *mesaVis,
500		  __DRIcontext *driContextPriv,
501		  void *sharedContextPrivate);
502
503extern bool
504i915CreateContext(int api,
505		  const struct gl_config *mesaVis,
506		  __DRIcontext *driContextPriv,
507		  void *sharedContextPrivate);
508extern bool
509brwCreateContext(int api,
510	         const struct gl_config *mesaVis,
511	         __DRIcontext *driContextPriv,
512		 void *sharedContextPrivate);
513
514static GLboolean
515intelCreateContext(gl_api api,
516		   const struct gl_config * mesaVis,
517                   __DRIcontext * driContextPriv,
518                   void *sharedContextPrivate)
519{
520   __DRIscreen *sPriv = driContextPriv->driScreenPriv;
521   struct intel_screen *intelScreen = sPriv->driverPrivate;
522
523#ifdef I915
524   if (IS_9XX(intelScreen->deviceID)) {
525      if (!IS_965(intelScreen->deviceID)) {
526	 return i915CreateContext(api, mesaVis, driContextPriv,
527				  sharedContextPrivate);
528      }
529   } else {
530      intelScreen->no_vbo = true;
531      return i830CreateContext(mesaVis, driContextPriv, sharedContextPrivate);
532   }
533#else
534   if (IS_965(intelScreen->deviceID))
535      return brwCreateContext(api, mesaVis,
536			      driContextPriv, sharedContextPrivate);
537#endif
538   fprintf(stderr, "Unrecognized deviceID 0x%x\n", intelScreen->deviceID);
539   return false;
540}
541
542static bool
543intel_init_bufmgr(struct intel_screen *intelScreen)
544{
545   __DRIscreen *spriv = intelScreen->driScrnPriv;
546   int num_fences = 0;
547
548   intelScreen->no_hw = (getenv("INTEL_NO_HW") != NULL ||
549			 getenv("INTEL_DEVID_OVERRIDE") != NULL);
550
551   intelScreen->bufmgr = intel_bufmgr_gem_init(spriv->fd, BATCH_SZ);
552   if (intelScreen->bufmgr == NULL) {
553      fprintf(stderr, "[%s:%u] Error initializing buffer manager.\n",
554	      __func__, __LINE__);
555      return false;
556   }
557
558   if (!intel_get_param(spriv, I915_PARAM_NUM_FENCES_AVAIL, &num_fences) ||
559       num_fences == 0) {
560      fprintf(stderr, "[%s: %u] Kernel 2.6.29 required.\n", __func__, __LINE__);
561      return false;
562   }
563
564   drm_intel_bufmgr_gem_enable_fenced_relocs(intelScreen->bufmgr);
565
566   intelScreen->named_regions = _mesa_NewHashTable();
567
568   intelScreen->relaxed_relocations = 0;
569   intelScreen->relaxed_relocations |=
570      intel_get_boolean(spriv, I915_PARAM_HAS_RELAXED_DELTA) << 0;
571
572   return true;
573}
574
575/**
576 * Override intel_screen.hw_has_hiz with environment variable INTEL_HIZ.
577 *
578 * Valid values for INTEL_HIZ are "0" and "1". If an invalid valid value is
579 * encountered, a warning is emitted and INTEL_HIZ is ignored.
580 */
581static void
582intel_override_hiz(struct intel_screen *intel)
583{
584   const char *s = getenv("INTEL_HIZ");
585   if (!s) {
586      return;
587   } else if (!strncmp("0", s, 2)) {
588      intel->hw_has_hiz = false;
589   } else if (!strncmp("1", s, 2)) {
590      intel->hw_has_hiz = true;
591   } else {
592      fprintf(stderr,
593	      "warning: env variable INTEL_HIZ=\"%s\" has invalid value "
594	      "and is ignored", s);
595   }
596}
597
598/**
599 * Override intel_screen.hw_has_separate_stencil with environment variable
600 * INTEL_SEPARATE_STENCIL.
601 *
602 * Valid values for INTEL_SEPARATE_STENCIL are "0" and "1". If an invalid
603 * valid value is encountered, a warning is emitted and INTEL_SEPARATE_STENCIL
604 * is ignored.
605 */
606static void
607intel_override_separate_stencil(struct intel_screen *screen)
608{
609   const char *s = getenv("INTEL_SEPARATE_STENCIL");
610   if (!s) {
611      return;
612   } else if (!strncmp("0", s, 2)) {
613      screen->hw_has_separate_stencil = false;
614   } else if (!strncmp("1", s, 2)) {
615      screen->hw_has_separate_stencil = true;
616   } else {
617      fprintf(stderr,
618	      "warning: env variable INTEL_SEPARATE_STENCIL=\"%s\" has "
619	      "invalid value and is ignored", s);
620   }
621}
622
623/**
624 * This is the driver specific part of the createNewScreen entry point.
625 * Called when using DRI2.
626 *
627 * \return the struct gl_config supported by this driver
628 */
629static const
630__DRIconfig **intelInitScreen2(__DRIscreen *psp)
631{
632   struct intel_screen *intelScreen;
633   GLenum fb_format[3];
634   GLenum fb_type[3];
635   unsigned int api_mask;
636   char *devid_override;
637
638   static const GLenum back_buffer_modes[] = {
639       GLX_NONE, GLX_SWAP_UNDEFINED_OML, GLX_SWAP_COPY_OML
640   };
641   uint8_t depth_bits[4], stencil_bits[4], msaa_samples_array[1];
642   int color;
643   __DRIconfig **configs = NULL;
644
645   /* Allocate the private area */
646   intelScreen = CALLOC(sizeof *intelScreen);
647   if (!intelScreen) {
648      fprintf(stderr, "\nERROR!  Allocating private area failed\n");
649      return false;
650   }
651   /* parse information in __driConfigOptions */
652   driParseOptionInfo(&intelScreen->optionCache,
653                      __driConfigOptions, __driNConfigOptions);
654
655   intelScreen->driScrnPriv = psp;
656   psp->driverPrivate = (void *) intelScreen;
657
658   /* Determine chipset ID */
659   if (!intel_get_param(psp, I915_PARAM_CHIPSET_ID,
660			&intelScreen->deviceID))
661      return false;
662
663   /* Allow an override of the device ID for the purpose of making the
664    * driver produce dumps for debugging of new chipset enablement.
665    * This implies INTEL_NO_HW, to avoid programming your actual GPU
666    * incorrectly.
667    */
668   devid_override = getenv("INTEL_DEVID_OVERRIDE");
669   if (devid_override) {
670      intelScreen->deviceID = strtod(devid_override, NULL);
671   }
672
673   if (IS_GEN7(intelScreen->deviceID)) {
674      intelScreen->gen = 7;
675   } else if (IS_GEN6(intelScreen->deviceID)) {
676      intelScreen->gen = 6;
677   } else if (IS_GEN5(intelScreen->deviceID)) {
678      intelScreen->gen = 5;
679   } else if (IS_965(intelScreen->deviceID)) {
680      intelScreen->gen = 4;
681   } else if (IS_9XX(intelScreen->deviceID)) {
682      intelScreen->gen = 3;
683   } else {
684      intelScreen->gen = 2;
685   }
686
687   intelScreen->hw_has_separate_stencil = intelScreen->gen >= 6;
688   intelScreen->hw_must_use_separate_stencil = intelScreen->gen >= 7;
689   intelScreen->hw_has_hiz = intelScreen->gen == 6; /* Not yet for gen7. */
690   intelScreen->dri2_has_hiz = INTEL_DRI2_HAS_HIZ_UNKNOWN;
691
692   intel_override_hiz(intelScreen);
693   intel_override_separate_stencil(intelScreen);
694
695   api_mask = (1 << __DRI_API_OPENGL);
696#if FEATURE_ES1
697   api_mask |= (1 << __DRI_API_GLES);
698#endif
699#if FEATURE_ES2
700   api_mask |= (1 << __DRI_API_GLES2);
701#endif
702
703   if (IS_9XX(intelScreen->deviceID) || IS_965(intelScreen->deviceID))
704      psp->api_mask = api_mask;
705
706   if (!intel_init_bufmgr(intelScreen))
707       return false;
708
709   psp->extensions = intelScreenExtensions;
710
711   msaa_samples_array[0] = 0;
712
713   fb_format[0] = GL_RGB;
714   fb_type[0] = GL_UNSIGNED_SHORT_5_6_5;
715
716   fb_format[1] = GL_BGR;
717   fb_type[1] = GL_UNSIGNED_INT_8_8_8_8_REV;
718
719   fb_format[2] = GL_BGRA;
720   fb_type[2] = GL_UNSIGNED_INT_8_8_8_8_REV;
721
722   depth_bits[0] = 0;
723   stencil_bits[0] = 0;
724
725   /* Generate a rich set of useful configs that do not include an
726    * accumulation buffer.
727    */
728   for (color = 0; color < ARRAY_SIZE(fb_format); color++) {
729      __DRIconfig **new_configs;
730      int depth_factor;
731
732      /* Starting with DRI2 protocol version 1.1 we can request a depth/stencil
733       * buffer that has a diffferent number of bits per pixel than the color
734       * buffer.  This isn't yet supported here.
735       */
736      if (fb_type[color] == GL_UNSIGNED_SHORT_5_6_5) {
737	 depth_bits[1] = 16;
738	 stencil_bits[1] = 0;
739      } else {
740	 depth_bits[1] = 24;
741	 stencil_bits[1] = 8;
742      }
743
744      depth_factor = 2;
745
746      new_configs = driCreateConfigs(fb_format[color], fb_type[color],
747				     depth_bits,
748				     stencil_bits,
749				     depth_factor,
750				     back_buffer_modes,
751				     ARRAY_SIZE(back_buffer_modes),
752				     msaa_samples_array,
753				     ARRAY_SIZE(msaa_samples_array),
754				     false);
755      if (configs == NULL)
756	 configs = new_configs;
757      else
758	 configs = driConcatConfigs(configs, new_configs);
759   }
760
761   /* Generate the minimum possible set of configs that include an
762    * accumulation buffer.
763    */
764   for (color = 0; color < ARRAY_SIZE(fb_format); color++) {
765      __DRIconfig **new_configs;
766
767      if (fb_type[color] == GL_UNSIGNED_SHORT_5_6_5) {
768	 depth_bits[0] = 16;
769	 stencil_bits[0] = 0;
770      } else {
771	 depth_bits[0] = 24;
772	 stencil_bits[0] = 8;
773      }
774
775      new_configs = driCreateConfigs(fb_format[color], fb_type[color],
776				     depth_bits, stencil_bits, 1,
777				     back_buffer_modes + 1, 1,
778				     msaa_samples_array, 1,
779				     true);
780      if (configs == NULL)
781	 configs = new_configs;
782      else
783	 configs = driConcatConfigs(configs, new_configs);
784   }
785
786   if (configs == NULL) {
787      fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
788              __LINE__);
789      return NULL;
790   }
791
792   return (const __DRIconfig **)configs;
793}
794
795struct intel_buffer {
796   __DRIbuffer base;
797   struct intel_region *region;
798};
799
800/**
801 * \brief Get tiling format for a DRI buffer.
802 *
803 * \param attachment is the buffer's attachmet point, such as
804 *        __DRI_BUFFER_DEPTH.
805 * \param out_tiling is the returned tiling format for buffer.
806 * \return false if attachment is unrecognized or is incompatible with screen.
807 */
808static bool
809intel_get_dri_buffer_tiling(struct intel_screen *screen,
810                            uint32_t attachment,
811                            uint32_t *out_tiling)
812{
813   if (screen->gen < 4) {
814      *out_tiling = I915_TILING_X;
815      return true;
816   }
817
818   switch (attachment) {
819   case __DRI_BUFFER_DEPTH:
820   case __DRI_BUFFER_DEPTH_STENCIL:
821   case __DRI_BUFFER_HIZ:
822      *out_tiling = I915_TILING_Y;
823      return true;
824   case __DRI_BUFFER_ACCUM:
825   case __DRI_BUFFER_FRONT_LEFT:
826   case __DRI_BUFFER_FRONT_RIGHT:
827   case __DRI_BUFFER_BACK_LEFT:
828   case __DRI_BUFFER_BACK_RIGHT:
829   case __DRI_BUFFER_FAKE_FRONT_LEFT:
830   case __DRI_BUFFER_FAKE_FRONT_RIGHT:
831      *out_tiling = I915_TILING_X;
832      return true;
833   case __DRI_BUFFER_STENCIL:
834      /* The stencil buffer is W tiled. However, we request from the kernel
835       * a non-tiled buffer because the GTT is incapable of W fencing.
836       */
837      *out_tiling = I915_TILING_NONE;
838      return true;
839   default:
840      if(unlikely(INTEL_DEBUG & DEBUG_DRI)) {
841	 fprintf(stderr, "error: %s: unrecognized DRI buffer attachment 0x%x\n",
842	         __FUNCTION__, attachment);
843      }
844       return false;
845   }
846}
847
848static __DRIbuffer *
849intelAllocateBuffer(__DRIscreen *screen,
850		    unsigned attachment, unsigned format,
851		    int width, int height)
852{
853   struct intel_buffer *intelBuffer;
854   struct intel_screen *intelScreen = screen->driverPrivate;
855
856   uint32_t tiling;
857   uint32_t region_width;
858   uint32_t region_height;
859   uint32_t region_cpp;
860
861   bool ok = true;
862
863   ok = intel_get_dri_buffer_tiling(intelScreen, attachment, &tiling);
864   if (!ok)
865      return NULL;
866
867   intelBuffer = CALLOC(sizeof *intelBuffer);
868   if (intelBuffer == NULL)
869      return NULL;
870
871   if (attachment == __DRI_BUFFER_STENCIL) {
872      /* The stencil buffer has quirky pitch requirements.  From Vol 2a,
873       * 11.5.6.2.1 3DSTATE_STENCIL_BUFFER, field "Surface Pitch":
874       *    The pitch must be set to 2x the value computed based on width, as
875       *    the stencil buffer is stored with two rows interleaved.
876       * To accomplish this, we resort to the nasty hack of doubling the
877       * region's cpp and halving its height.
878       */
879      region_width = ALIGN(width, 64);
880      region_height = ALIGN(ALIGN(height, 2) / 2, 64);
881      region_cpp = format / 4;
882   } else {
883      region_width = width;
884      region_height = height;
885      region_cpp = format / 8;
886   }
887
888   intelBuffer->region = intel_region_alloc(intelScreen,
889                                            tiling,
890                                            region_cpp,
891                                            region_width,
892                                            region_height,
893                                            true);
894
895   if (intelBuffer->region == NULL) {
896	   FREE(intelBuffer);
897	   return NULL;
898   }
899
900   intel_region_flink(intelBuffer->region, &intelBuffer->base.name);
901
902   intelBuffer->base.attachment = attachment;
903   intelBuffer->base.cpp = intelBuffer->region->cpp;
904   intelBuffer->base.pitch =
905         intelBuffer->region->pitch * intelBuffer->region->cpp;
906
907   return &intelBuffer->base;
908}
909
910static void
911intelReleaseBuffer(__DRIscreen *screen, __DRIbuffer *buffer)
912{
913   struct intel_buffer *intelBuffer = (struct intel_buffer *) buffer;
914
915   intel_region_release(&intelBuffer->region);
916   free(intelBuffer);
917}
918
919
920const struct __DriverAPIRec driDriverAPI = {
921   .InitScreen		 = intelInitScreen2,
922   .DestroyScreen	 = intelDestroyScreen,
923   .CreateContext	 = intelCreateContext,
924   .DestroyContext	 = intelDestroyContext,
925   .CreateBuffer	 = intelCreateBuffer,
926   .DestroyBuffer	 = intelDestroyBuffer,
927   .MakeCurrent		 = intelMakeCurrent,
928   .UnbindContext	 = intelUnbindContext,
929   .AllocateBuffer       = intelAllocateBuffer,
930   .ReleaseBuffer        = intelReleaseBuffer
931};
932
933/* This is the table of extensions that the loader will dlsym() for. */
934PUBLIC const __DRIextension *__driDriverExtensions[] = {
935    &driCoreExtension.base,
936    &driDRI2Extension.base,
937    NULL
938};
939