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