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