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