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