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