intel_screen.c revision 9087ba128089ed0dc00e6eb38f37126fb7557d3b
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 "main/glheader.h"
29#include "main/context.h"
30#include "main/framebuffer.h"
31#include "main/renderbuffer.h"
32#include "main/hash.h"
33#include "main/fbobject.h"
34
35#include "utils.h"
36#include "xmlpool.h"
37
38#include "intel_batchbuffer.h"
39#include "intel_buffers.h"
40#include "intel_bufmgr.h"
41#include "intel_chipset.h"
42#include "intel_fbo.h"
43#include "intel_screen.h"
44#include "intel_tex.h"
45#include "intel_regions.h"
46
47#include "i915_drm.h"
48
49#define DRI_CONF_TEXTURE_TILING(def) \
50
51PUBLIC const char __driConfigOptions[] =
52   DRI_CONF_BEGIN
53   DRI_CONF_SECTION_PERFORMANCE
54      DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_ALWAYS_SYNC)
55      /* Options correspond to DRI_CONF_BO_REUSE_DISABLED,
56       * DRI_CONF_BO_REUSE_ALL
57       */
58      DRI_CONF_OPT_BEGIN_V(bo_reuse, enum, 1, "0:1")
59	 DRI_CONF_DESC_BEGIN(en, "Buffer object reuse")
60	    DRI_CONF_ENUM(0, "Disable buffer object reuse")
61	    DRI_CONF_ENUM(1, "Enable reuse of all sizes of buffer objects")
62	 DRI_CONF_DESC_END
63      DRI_CONF_OPT_END
64
65      DRI_CONF_OPT_BEGIN(texture_tiling, bool, true)
66	 DRI_CONF_DESC(en, "Enable texture tiling")
67      DRI_CONF_OPT_END
68
69      DRI_CONF_OPT_BEGIN(early_z, bool, false)
70	 DRI_CONF_DESC(en, "Enable early Z in classic mode (unstable, 945-only).")
71      DRI_CONF_OPT_END
72
73      DRI_CONF_OPT_BEGIN(fragment_shader, bool, true)
74	 DRI_CONF_DESC(en, "Enable limited ARB_fragment_shader support on 915/945.")
75      DRI_CONF_OPT_END
76
77   DRI_CONF_SECTION_END
78   DRI_CONF_SECTION_QUALITY
79      DRI_CONF_FORCE_S3TC_ENABLE(false)
80      DRI_CONF_ALLOW_LARGE_TEXTURES(2)
81   DRI_CONF_SECTION_END
82   DRI_CONF_SECTION_DEBUG
83     DRI_CONF_NO_RAST(false)
84     DRI_CONF_ALWAYS_FLUSH_BATCH(false)
85     DRI_CONF_ALWAYS_FLUSH_CACHE(false)
86
87      DRI_CONF_OPT_BEGIN(stub_occlusion_query, bool, false)
88	 DRI_CONF_DESC(en, "Enable stub ARB_occlusion_query support on 915/945.")
89      DRI_CONF_OPT_END
90   DRI_CONF_SECTION_END
91DRI_CONF_END;
92
93const GLuint __driNConfigOptions = 11;
94
95#ifdef USE_NEW_INTERFACE
96static PFNGLXCREATECONTEXTMODES create_context_modes = NULL;
97#endif /*USE_NEW_INTERFACE */
98
99static const __DRItexBufferExtension intelTexBufferExtension = {
100    { __DRI_TEX_BUFFER, __DRI_TEX_BUFFER_VERSION },
101   intelSetTexBuffer,
102   intelSetTexBuffer2,
103};
104
105static void
106intelDRI2Flush(__DRIdrawable *drawable)
107{
108   struct intel_context *intel = drawable->driContextPriv->driverPrivate;
109
110   if (intel->gen < 4)
111      INTEL_FIREVERTICES(intel);
112
113   intel->need_throttle = GL_TRUE;
114
115   if (intel->batch->map != intel->batch->ptr)
116      intel_batchbuffer_flush(intel->batch);
117}
118
119static const struct __DRI2flushExtensionRec intelFlushExtension = {
120    { __DRI2_FLUSH, __DRI2_FLUSH_VERSION },
121    intelDRI2Flush,
122    dri2InvalidateDrawable,
123};
124
125static __DRIimage *
126intel_create_image_from_name(__DRIcontext *context,
127			     int width, int height, int format,
128			     int name, int pitch, void *loaderPrivate)
129{
130    __DRIimage *image;
131    struct intel_context *intel = context->driverPrivate;
132    int cpp;
133
134    image = CALLOC(sizeof *image);
135    if (image == NULL)
136	return NULL;
137
138    switch (format) {
139    case __DRI_IMAGE_FORMAT_RGB565:
140       image->format = MESA_FORMAT_RGB565;
141       image->internal_format = GL_RGB;
142       image->data_type = GL_UNSIGNED_BYTE;
143       break;
144    case __DRI_IMAGE_FORMAT_XRGB8888:
145       image->format = MESA_FORMAT_XRGB8888;
146       image->internal_format = GL_RGB;
147       image->data_type = GL_UNSIGNED_BYTE;
148       break;
149    case __DRI_IMAGE_FORMAT_ARGB8888:
150       image->format = MESA_FORMAT_ARGB8888;
151       image->internal_format = GL_RGBA;
152       image->data_type = GL_UNSIGNED_BYTE;
153       break;
154    default:
155       free(image);
156       return NULL;
157    }
158
159    image->data = loaderPrivate;
160    cpp = _mesa_get_format_bytes(image->format);
161
162    image->region = intel_region_alloc_for_handle(intel->intelScreen,
163						  cpp, width, height,
164						  pitch, name, "image");
165    if (image->region == NULL) {
166       FREE(image);
167       return NULL;
168    }
169
170    return image;
171}
172
173static __DRIimage *
174intel_create_image_from_renderbuffer(__DRIcontext *context,
175				     int renderbuffer, void *loaderPrivate)
176{
177   __DRIimage *image;
178   struct intel_context *intel = context->driverPrivate;
179   struct gl_renderbuffer *rb;
180   struct intel_renderbuffer *irb;
181
182   rb = _mesa_lookup_renderbuffer(&intel->ctx, renderbuffer);
183   if (!rb) {
184      _mesa_error(&intel->ctx,
185		  GL_INVALID_OPERATION, "glRenderbufferExternalMESA");
186      return NULL;
187   }
188
189   irb = intel_renderbuffer(rb);
190   image = CALLOC(sizeof *image);
191   if (image == NULL)
192      return NULL;
193
194   image->internal_format = rb->InternalFormat;
195   image->format = rb->Format;
196   image->data_type = rb->DataType;
197   image->data = loaderPrivate;
198   intel_region_reference(&image->region, irb->region);
199
200   return image;
201}
202
203static void
204intel_destroy_image(__DRIimage *image)
205{
206    intel_region_release(&image->region);
207    FREE(image);
208}
209
210static struct __DRIimageExtensionRec intelImageExtension = {
211    { __DRI_IMAGE, __DRI_IMAGE_VERSION },
212    intel_create_image_from_name,
213    intel_create_image_from_renderbuffer,
214    intel_destroy_image,
215};
216
217static const __DRIextension *intelScreenExtensions[] = {
218    &driReadDrawableExtension,
219    &intelTexBufferExtension.base,
220    &intelFlushExtension.base,
221    &intelImageExtension.base,
222    &dri2ConfigQueryExtension.base,
223    NULL
224};
225
226static GLboolean
227intel_get_param(__DRIscreen *psp, int param, int *value)
228{
229   int ret;
230   struct drm_i915_getparam gp;
231
232   gp.param = param;
233   gp.value = value;
234
235   ret = drmCommandWriteRead(psp->fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
236   if (ret) {
237      _mesa_warning(NULL, "drm_i915_getparam: %d", ret);
238      return GL_FALSE;
239   }
240
241   return GL_TRUE;
242}
243
244static void
245nop_callback(GLuint key, void *data, void *userData)
246{
247}
248
249static void
250intelDestroyScreen(__DRIscreen * sPriv)
251{
252   struct intel_screen *intelScreen = sPriv->private;
253
254   dri_bufmgr_destroy(intelScreen->bufmgr);
255   driDestroyOptionInfo(&intelScreen->optionCache);
256
257   /* Some regions may still have references to them at this point, so
258    * flush the hash table to prevent _mesa_DeleteHashTable() from
259    * complaining about the hash not being empty; */
260   _mesa_HashDeleteAll(intelScreen->named_regions, nop_callback, NULL);
261   _mesa_DeleteHashTable(intelScreen->named_regions);
262
263   FREE(intelScreen);
264   sPriv->private = NULL;
265}
266
267
268/**
269 * This is called when we need to set up GL rendering to a new X window.
270 */
271static GLboolean
272intelCreateBuffer(__DRIscreen * driScrnPriv,
273                  __DRIdrawable * driDrawPriv,
274                  const __GLcontextModes * mesaVis, GLboolean isPixmap)
275{
276   struct intel_renderbuffer *rb;
277
278   if (isPixmap) {
279      return GL_FALSE;          /* not implemented */
280   }
281   else {
282      GLboolean swStencil = (mesaVis->stencilBits > 0 &&
283                             mesaVis->depthBits != 24);
284      gl_format rgbFormat;
285
286      struct gl_framebuffer *fb = CALLOC_STRUCT(gl_framebuffer);
287
288      if (!fb)
289	 return GL_FALSE;
290
291      _mesa_initialize_window_framebuffer(fb, mesaVis);
292
293      if (mesaVis->redBits == 5)
294	 rgbFormat = MESA_FORMAT_RGB565;
295      else if (mesaVis->alphaBits == 0)
296	 rgbFormat = MESA_FORMAT_XRGB8888;
297      else
298	 rgbFormat = MESA_FORMAT_ARGB8888;
299
300      /* setup the hardware-based renderbuffers */
301      rb = intel_create_renderbuffer(rgbFormat);
302      _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &rb->Base);
303
304      if (mesaVis->doubleBufferMode) {
305	 rb = intel_create_renderbuffer(rgbFormat);
306         _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &rb->Base);
307      }
308
309      if (mesaVis->depthBits == 24) {
310	 assert(mesaVis->stencilBits == 8);
311	 /* combined depth/stencil buffer */
312	 struct intel_renderbuffer *depthStencilRb
313	    = intel_create_renderbuffer(MESA_FORMAT_S8_Z24);
314	 /* note: bind RB to two attachment points */
315	 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthStencilRb->Base);
316	 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &depthStencilRb->Base);
317      }
318      else if (mesaVis->depthBits == 16) {
319         /* just 16-bit depth buffer, no hw stencil */
320         struct intel_renderbuffer *depthRb
321	    = intel_create_renderbuffer(MESA_FORMAT_Z16);
322         _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
323      }
324
325      /* now add any/all software-based renderbuffers we may need */
326      _mesa_add_soft_renderbuffers(fb,
327                                   GL_FALSE, /* never sw color */
328                                   GL_FALSE, /* never sw depth */
329                                   swStencil, mesaVis->accumRedBits > 0,
330                                   GL_FALSE, /* never sw alpha */
331                                   GL_FALSE  /* never sw aux */ );
332      driDrawPriv->driverPrivate = fb;
333
334      return GL_TRUE;
335   }
336}
337
338static void
339intelDestroyBuffer(__DRIdrawable * driDrawPriv)
340{
341    struct gl_framebuffer *fb = driDrawPriv->driverPrivate;
342
343    _mesa_reference_framebuffer(&fb, NULL);
344}
345
346/* There are probably better ways to do this, such as an
347 * init-designated function to register chipids and createcontext
348 * functions.
349 */
350extern GLboolean i830CreateContext(const __GLcontextModes * mesaVis,
351                                   __DRIcontext * driContextPriv,
352                                   void *sharedContextPrivate);
353
354extern GLboolean i915CreateContext(int api,
355				   const __GLcontextModes * mesaVis,
356                                   __DRIcontext * driContextPriv,
357                                   void *sharedContextPrivate);
358extern GLboolean brwCreateContext(int api,
359				  const __GLcontextModes * mesaVis,
360				  __DRIcontext * driContextPriv,
361				  void *sharedContextPrivate);
362
363static GLboolean
364intelCreateContext(gl_api api,
365		   const __GLcontextModes * mesaVis,
366                   __DRIcontext * driContextPriv,
367                   void *sharedContextPrivate)
368{
369   __DRIscreen *sPriv = driContextPriv->driScreenPriv;
370   struct intel_screen *intelScreen = sPriv->private;
371
372#ifdef I915
373   if (IS_9XX(intelScreen->deviceID)) {
374      if (!IS_965(intelScreen->deviceID)) {
375	 return i915CreateContext(api, mesaVis, driContextPriv,
376				  sharedContextPrivate);
377      }
378   } else {
379      intelScreen->no_vbo = GL_TRUE;
380      return i830CreateContext(mesaVis, driContextPriv, sharedContextPrivate);
381   }
382#else
383   if (IS_965(intelScreen->deviceID))
384      return brwCreateContext(api, mesaVis,
385			      driContextPriv, sharedContextPrivate);
386#endif
387   fprintf(stderr, "Unrecognized deviceID %x\n", intelScreen->deviceID);
388   return GL_FALSE;
389}
390
391static GLboolean
392intel_init_bufmgr(struct intel_screen *intelScreen)
393{
394   __DRIscreen *spriv = intelScreen->driScrnPriv;
395   int num_fences = 0;
396
397   intelScreen->no_hw = getenv("INTEL_NO_HW") != NULL;
398
399   intelScreen->bufmgr = intel_bufmgr_gem_init(spriv->fd, BATCH_SZ);
400   /* Otherwise, use the classic buffer manager. */
401   if (intelScreen->bufmgr == NULL) {
402      fprintf(stderr, "[%s:%u] Error initializing buffer manager.\n",
403	      __func__, __LINE__);
404      return GL_FALSE;
405   }
406
407   if (!intel_get_param(spriv, I915_PARAM_NUM_FENCES_AVAIL, &num_fences) ||
408       num_fences == 0) {
409      fprintf(stderr, "[%s: %u] Kernel 2.6.29 required.\n", __func__, __LINE__);
410      return GL_FALSE;
411   }
412
413   drm_intel_bufmgr_gem_enable_fenced_relocs(intelScreen->bufmgr);
414
415   intelScreen->named_regions = _mesa_NewHashTable();
416
417   return GL_TRUE;
418}
419
420/**
421 * This is the driver specific part of the createNewScreen entry point.
422 * Called when using DRI2.
423 *
424 * \return the __GLcontextModes supported by this driver
425 */
426static const
427__DRIconfig **intelInitScreen2(__DRIscreen *psp)
428{
429   struct intel_screen *intelScreen;
430   GLenum fb_format[3];
431   GLenum fb_type[3];
432   unsigned int api_mask;
433
434   static const GLenum back_buffer_modes[] = {
435       GLX_NONE, GLX_SWAP_UNDEFINED_OML, GLX_SWAP_COPY_OML
436   };
437   uint8_t depth_bits[4], stencil_bits[4], msaa_samples_array[1];
438   int color;
439   __DRIconfig **configs = NULL;
440
441   /* Allocate the private area */
442   intelScreen = CALLOC(sizeof *intelScreen);
443   if (!intelScreen) {
444      fprintf(stderr, "\nERROR!  Allocating private area failed\n");
445      return GL_FALSE;
446   }
447   /* parse information in __driConfigOptions */
448   driParseOptionInfo(&intelScreen->optionCache,
449                      __driConfigOptions, __driNConfigOptions);
450
451   intelScreen->driScrnPriv = psp;
452   psp->private = (void *) intelScreen;
453
454   /* Determine chipset ID */
455   if (!intel_get_param(psp, I915_PARAM_CHIPSET_ID,
456			&intelScreen->deviceID))
457      return GL_FALSE;
458
459   api_mask = (1 << __DRI_API_OPENGL);
460#if FEATURE_ES1
461   api_mask |= (1 << __DRI_API_GLES);
462#endif
463#if FEATURE_ES2
464   api_mask |= (1 << __DRI_API_GLES2);
465#endif
466
467   if (IS_9XX(intelScreen->deviceID) || IS_965(intelScreen->deviceID))
468      psp->api_mask = api_mask;
469
470   if (!intel_init_bufmgr(intelScreen))
471       return GL_FALSE;
472
473   psp->extensions = intelScreenExtensions;
474
475   msaa_samples_array[0] = 0;
476
477   fb_format[0] = GL_RGB;
478   fb_type[0] = GL_UNSIGNED_SHORT_5_6_5;
479
480   fb_format[1] = GL_BGR;
481   fb_type[1] = GL_UNSIGNED_INT_8_8_8_8_REV;
482
483   fb_format[2] = GL_BGRA;
484   fb_type[2] = GL_UNSIGNED_INT_8_8_8_8_REV;
485
486   depth_bits[0] = 0;
487   stencil_bits[0] = 0;
488
489   /* Generate a rich set of useful configs that do not include an
490    * accumulation buffer.
491    */
492   for (color = 0; color < ARRAY_SIZE(fb_format); color++) {
493      __DRIconfig **new_configs;
494      int depth_factor;
495
496      /* Starting with DRI2 protocol version 1.1 we can request a depth/stencil
497       * buffer that has a diffferent number of bits per pixel than the color
498       * buffer.  This isn't yet supported here.
499       */
500      if (fb_type[color] == GL_UNSIGNED_SHORT_5_6_5) {
501	 depth_bits[1] = 16;
502	 stencil_bits[1] = 0;
503      } else {
504	 depth_bits[1] = 24;
505	 stencil_bits[1] = 8;
506      }
507
508      depth_factor = 2;
509
510      new_configs = driCreateConfigs(fb_format[color], fb_type[color],
511				     depth_bits,
512				     stencil_bits,
513				     depth_factor,
514				     back_buffer_modes,
515				     ARRAY_SIZE(back_buffer_modes),
516				     msaa_samples_array,
517				     ARRAY_SIZE(msaa_samples_array),
518				     GL_FALSE);
519      if (configs == NULL)
520	 configs = new_configs;
521      else
522	 configs = driConcatConfigs(configs, new_configs);
523   }
524
525   /* Generate the minimum possible set of configs that include an
526    * accumulation buffer.
527    */
528   for (color = 0; color < ARRAY_SIZE(fb_format); color++) {
529      __DRIconfig **new_configs;
530
531      if (fb_type[color] == GL_UNSIGNED_SHORT_5_6_5) {
532	 depth_bits[0] = 16;
533	 stencil_bits[0] = 0;
534      } else {
535	 depth_bits[0] = 24;
536	 stencil_bits[0] = 8;
537      }
538
539      new_configs = driCreateConfigs(fb_format[color], fb_type[color],
540				     depth_bits, stencil_bits, 1,
541				     back_buffer_modes + 1, 1,
542				     msaa_samples_array, 1,
543				     GL_TRUE);
544      if (configs == NULL)
545	 configs = new_configs;
546      else
547	 configs = driConcatConfigs(configs, new_configs);
548   }
549
550   if (configs == NULL) {
551      fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
552              __LINE__);
553      return NULL;
554   }
555
556   return (const __DRIconfig **)configs;
557}
558
559const struct __DriverAPIRec driDriverAPI = {
560   .DestroyScreen	 = intelDestroyScreen,
561   .CreateContext	 = intelCreateContext,
562   .DestroyContext	 = intelDestroyContext,
563   .CreateBuffer	 = intelCreateBuffer,
564   .DestroyBuffer	 = intelDestroyBuffer,
565   .MakeCurrent		 = intelMakeCurrent,
566   .UnbindContext	 = intelUnbindContext,
567   .InitScreen2		 = intelInitScreen2,
568};
569
570/* This is the table of extensions that the loader will dlsym() for. */
571PUBLIC const __DRIextension *__driDriverExtensions[] = {
572    &driCoreExtension.base,
573    &driDRI2Extension.base,
574    NULL
575};
576