i965: Check eu/subslices are > 0
[mesa.git] / src / mesa / drivers / dri / i965 / intel_screen.c
1 /*
2 * Copyright 2003 VMware, Inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 #include <errno.h>
27 #include <time.h>
28 #include <unistd.h>
29 #include "main/context.h"
30 #include "main/framebuffer.h"
31 #include "main/renderbuffer.h"
32 #include "main/texobj.h"
33 #include "main/hash.h"
34 #include "main/fbobject.h"
35 #include "main/version.h"
36 #include "swrast/s_renderbuffer.h"
37 #include "util/ralloc.h"
38 #include "brw_shader.h"
39 #include "compiler/nir/nir.h"
40
41 #include "utils.h"
42 #include "xmlpool.h"
43
44 static const __DRIconfigOptionsExtension brw_config_options = {
45 .base = { __DRI_CONFIG_OPTIONS, 1 },
46 .xml =
47 DRI_CONF_BEGIN
48 DRI_CONF_SECTION_PERFORMANCE
49 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_ALWAYS_SYNC)
50 /* Options correspond to DRI_CONF_BO_REUSE_DISABLED,
51 * DRI_CONF_BO_REUSE_ALL
52 */
53 DRI_CONF_OPT_BEGIN_V(bo_reuse, enum, 1, "0:1")
54 DRI_CONF_DESC_BEGIN(en, "Buffer object reuse")
55 DRI_CONF_ENUM(0, "Disable buffer object reuse")
56 DRI_CONF_ENUM(1, "Enable reuse of all sizes of buffer objects")
57 DRI_CONF_DESC_END
58 DRI_CONF_OPT_END
59
60 DRI_CONF_OPT_BEGIN_B(hiz, "true")
61 DRI_CONF_DESC(en, "Enable Hierarchical Z on gen6+")
62 DRI_CONF_OPT_END
63 DRI_CONF_SECTION_END
64
65 DRI_CONF_SECTION_QUALITY
66 DRI_CONF_FORCE_S3TC_ENABLE("false")
67
68 DRI_CONF_OPT_BEGIN(clamp_max_samples, int, -1)
69 DRI_CONF_DESC(en, "Clamp the value of GL_MAX_SAMPLES to the "
70 "given integer. If negative, then do not clamp.")
71 DRI_CONF_OPT_END
72 DRI_CONF_SECTION_END
73
74 DRI_CONF_SECTION_DEBUG
75 DRI_CONF_NO_RAST("false")
76 DRI_CONF_ALWAYS_FLUSH_BATCH("false")
77 DRI_CONF_ALWAYS_FLUSH_CACHE("false")
78 DRI_CONF_DISABLE_THROTTLING("false")
79 DRI_CONF_FORCE_GLSL_EXTENSIONS_WARN("false")
80 DRI_CONF_DISABLE_GLSL_LINE_CONTINUATIONS("false")
81 DRI_CONF_DISABLE_BLEND_FUNC_EXTENDED("false")
82 DRI_CONF_DUAL_COLOR_BLEND_BY_LOCATION("false")
83 DRI_CONF_ALLOW_GLSL_EXTENSION_DIRECTIVE_MIDSHADER("false")
84
85 DRI_CONF_OPT_BEGIN_B(shader_precompile, "true")
86 DRI_CONF_DESC(en, "Perform code generation at shader link time.")
87 DRI_CONF_OPT_END
88 DRI_CONF_SECTION_END
89 DRI_CONF_END
90 };
91
92 #include "intel_batchbuffer.h"
93 #include "intel_buffers.h"
94 #include "intel_bufmgr.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_image.h"
100
101 #include "brw_context.h"
102
103 #include "i915_drm.h"
104
105 /**
106 * For debugging purposes, this returns a time in seconds.
107 */
108 double
109 get_time(void)
110 {
111 struct timespec tp;
112
113 clock_gettime(CLOCK_MONOTONIC, &tp);
114
115 return tp.tv_sec + tp.tv_nsec / 1000000000.0;
116 }
117
118 void
119 aub_dump_bmp(struct gl_context *ctx)
120 {
121 struct gl_framebuffer *fb = ctx->DrawBuffer;
122
123 for (unsigned i = 0; i < fb->_NumColorDrawBuffers; i++) {
124 struct intel_renderbuffer *irb =
125 intel_renderbuffer(fb->_ColorDrawBuffers[i]);
126
127 if (irb && irb->mt) {
128 enum aub_dump_bmp_format format;
129
130 switch (irb->Base.Base.Format) {
131 case MESA_FORMAT_B8G8R8A8_UNORM:
132 case MESA_FORMAT_B8G8R8X8_UNORM:
133 format = AUB_DUMP_BMP_FORMAT_ARGB_8888;
134 break;
135 default:
136 continue;
137 }
138
139 drm_intel_gem_bo_aub_dump_bmp(irb->mt->bo,
140 irb->draw_x,
141 irb->draw_y,
142 irb->Base.Base.Width,
143 irb->Base.Base.Height,
144 format,
145 irb->mt->pitch,
146 0);
147 }
148 }
149 }
150
151 static const __DRItexBufferExtension intelTexBufferExtension = {
152 .base = { __DRI_TEX_BUFFER, 3 },
153
154 .setTexBuffer = intelSetTexBuffer,
155 .setTexBuffer2 = intelSetTexBuffer2,
156 .releaseTexBuffer = NULL,
157 };
158
159 static void
160 intel_dri2_flush_with_flags(__DRIcontext *cPriv,
161 __DRIdrawable *dPriv,
162 unsigned flags,
163 enum __DRI2throttleReason reason)
164 {
165 struct brw_context *brw = cPriv->driverPrivate;
166
167 if (!brw)
168 return;
169
170 struct gl_context *ctx = &brw->ctx;
171
172 FLUSH_VERTICES(ctx, 0);
173
174 if (flags & __DRI2_FLUSH_DRAWABLE)
175 intel_resolve_for_dri2_flush(brw, dPriv);
176
177 if (reason == __DRI2_THROTTLE_SWAPBUFFER)
178 brw->need_swap_throttle = true;
179 if (reason == __DRI2_THROTTLE_FLUSHFRONT)
180 brw->need_flush_throttle = true;
181
182 intel_batchbuffer_flush(brw);
183
184 if (INTEL_DEBUG & DEBUG_AUB) {
185 aub_dump_bmp(ctx);
186 }
187 }
188
189 /**
190 * Provides compatibility with loaders that only support the older (version
191 * 1-3) flush interface.
192 *
193 * That includes libGL up to Mesa 9.0, and the X Server at least up to 1.13.
194 */
195 static void
196 intel_dri2_flush(__DRIdrawable *drawable)
197 {
198 intel_dri2_flush_with_flags(drawable->driContextPriv, drawable,
199 __DRI2_FLUSH_DRAWABLE,
200 __DRI2_THROTTLE_SWAPBUFFER);
201 }
202
203 static const struct __DRI2flushExtensionRec intelFlushExtension = {
204 .base = { __DRI2_FLUSH, 4 },
205
206 .flush = intel_dri2_flush,
207 .invalidate = dri2InvalidateDrawable,
208 .flush_with_flags = intel_dri2_flush_with_flags,
209 };
210
211 static struct intel_image_format intel_image_formats[] = {
212 { __DRI_IMAGE_FOURCC_ARGB8888, __DRI_IMAGE_COMPONENTS_RGBA, 1,
213 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ARGB8888, 4 } } },
214
215 { __DRI_IMAGE_FOURCC_ABGR8888, __DRI_IMAGE_COMPONENTS_RGBA, 1,
216 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ABGR8888, 4 } } },
217
218 { __DRI_IMAGE_FOURCC_SARGB8888, __DRI_IMAGE_COMPONENTS_RGBA, 1,
219 { { 0, 0, 0, __DRI_IMAGE_FORMAT_SARGB8, 4 } } },
220
221 { __DRI_IMAGE_FOURCC_XRGB8888, __DRI_IMAGE_COMPONENTS_RGB, 1,
222 { { 0, 0, 0, __DRI_IMAGE_FORMAT_XRGB8888, 4 }, } },
223
224 { __DRI_IMAGE_FOURCC_XBGR8888, __DRI_IMAGE_COMPONENTS_RGB, 1,
225 { { 0, 0, 0, __DRI_IMAGE_FORMAT_XBGR8888, 4 }, } },
226
227 { __DRI_IMAGE_FOURCC_RGB565, __DRI_IMAGE_COMPONENTS_RGB, 1,
228 { { 0, 0, 0, __DRI_IMAGE_FORMAT_RGB565, 2 } } },
229
230 { __DRI_IMAGE_FOURCC_R8, __DRI_IMAGE_COMPONENTS_R, 1,
231 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 }, } },
232
233 { __DRI_IMAGE_FOURCC_GR88, __DRI_IMAGE_COMPONENTS_RG, 1,
234 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88, 2 }, } },
235
236 { __DRI_IMAGE_FOURCC_YUV410, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
237 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
238 { 1, 2, 2, __DRI_IMAGE_FORMAT_R8, 1 },
239 { 2, 2, 2, __DRI_IMAGE_FORMAT_R8, 1 } } },
240
241 { __DRI_IMAGE_FOURCC_YUV411, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
242 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
243 { 1, 2, 0, __DRI_IMAGE_FORMAT_R8, 1 },
244 { 2, 2, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
245
246 { __DRI_IMAGE_FOURCC_YUV420, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
247 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
248 { 1, 1, 1, __DRI_IMAGE_FORMAT_R8, 1 },
249 { 2, 1, 1, __DRI_IMAGE_FORMAT_R8, 1 } } },
250
251 { __DRI_IMAGE_FOURCC_YUV422, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
252 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
253 { 1, 1, 0, __DRI_IMAGE_FORMAT_R8, 1 },
254 { 2, 1, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
255
256 { __DRI_IMAGE_FOURCC_YUV444, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
257 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
258 { 1, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
259 { 2, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
260
261 { __DRI_IMAGE_FOURCC_NV12, __DRI_IMAGE_COMPONENTS_Y_UV, 2,
262 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
263 { 1, 1, 1, __DRI_IMAGE_FORMAT_GR88, 2 } } },
264
265 { __DRI_IMAGE_FOURCC_NV16, __DRI_IMAGE_COMPONENTS_Y_UV, 2,
266 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
267 { 1, 1, 0, __DRI_IMAGE_FORMAT_GR88, 2 } } },
268
269 /* For YUYV buffers, we set up two overlapping DRI images and treat
270 * them as planar buffers in the compositors. Plane 0 is GR88 and
271 * samples YU or YV pairs and places Y into the R component, while
272 * plane 1 is ARGB and samples YUYV clusters and places pairs and
273 * places U into the G component and V into A. This lets the
274 * texture sampler interpolate the Y components correctly when
275 * sampling from plane 0, and interpolate U and V correctly when
276 * sampling from plane 1. */
277 { __DRI_IMAGE_FOURCC_YUYV, __DRI_IMAGE_COMPONENTS_Y_XUXV, 2,
278 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88, 2 },
279 { 0, 1, 0, __DRI_IMAGE_FORMAT_ARGB8888, 4 } } }
280 };
281
282 static void
283 intel_image_warn_if_unaligned(__DRIimage *image, const char *func)
284 {
285 uint32_t tiling, swizzle;
286 drm_intel_bo_get_tiling(image->bo, &tiling, &swizzle);
287
288 if (tiling != I915_TILING_NONE && (image->offset & 0xfff)) {
289 _mesa_warning(NULL, "%s: offset 0x%08x not on tile boundary",
290 func, image->offset);
291 }
292 }
293
294 static struct intel_image_format *
295 intel_image_format_lookup(int fourcc)
296 {
297 struct intel_image_format *f = NULL;
298
299 for (unsigned i = 0; i < ARRAY_SIZE(intel_image_formats); i++) {
300 if (intel_image_formats[i].fourcc == fourcc) {
301 f = &intel_image_formats[i];
302 break;
303 }
304 }
305
306 return f;
307 }
308
309 static boolean intel_lookup_fourcc(int dri_format, int *fourcc)
310 {
311 for (unsigned i = 0; i < ARRAY_SIZE(intel_image_formats); i++) {
312 if (intel_image_formats[i].planes[0].dri_format == dri_format) {
313 *fourcc = intel_image_formats[i].fourcc;
314 return true;
315 }
316 }
317 return false;
318 }
319
320 static __DRIimage *
321 intel_allocate_image(int dri_format, void *loaderPrivate)
322 {
323 __DRIimage *image;
324
325 image = calloc(1, sizeof *image);
326 if (image == NULL)
327 return NULL;
328
329 image->dri_format = dri_format;
330 image->offset = 0;
331
332 image->format = driImageFormatToGLFormat(dri_format);
333 if (dri_format != __DRI_IMAGE_FORMAT_NONE &&
334 image->format == MESA_FORMAT_NONE) {
335 free(image);
336 return NULL;
337 }
338
339 image->internal_format = _mesa_get_format_base_format(image->format);
340 image->data = loaderPrivate;
341
342 return image;
343 }
344
345 /**
346 * Sets up a DRIImage structure to point to a slice out of a miptree.
347 */
348 static void
349 intel_setup_image_from_mipmap_tree(struct brw_context *brw, __DRIimage *image,
350 struct intel_mipmap_tree *mt, GLuint level,
351 GLuint zoffset)
352 {
353 intel_miptree_make_shareable(brw, mt);
354
355 intel_miptree_check_level_layer(mt, level, zoffset);
356
357 image->width = minify(mt->physical_width0, level - mt->first_level);
358 image->height = minify(mt->physical_height0, level - mt->first_level);
359 image->pitch = mt->pitch;
360
361 image->offset = intel_miptree_get_tile_offsets(mt, level, zoffset,
362 &image->tile_x,
363 &image->tile_y);
364
365 drm_intel_bo_unreference(image->bo);
366 image->bo = mt->bo;
367 drm_intel_bo_reference(mt->bo);
368 }
369
370 static __DRIimage *
371 intel_create_image_from_name(__DRIscreen *screen,
372 int width, int height, int format,
373 int name, int pitch, void *loaderPrivate)
374 {
375 struct intel_screen *intelScreen = screen->driverPrivate;
376 __DRIimage *image;
377 int cpp;
378
379 image = intel_allocate_image(format, loaderPrivate);
380 if (image == NULL)
381 return NULL;
382
383 if (image->format == MESA_FORMAT_NONE)
384 cpp = 1;
385 else
386 cpp = _mesa_get_format_bytes(image->format);
387
388 image->width = width;
389 image->height = height;
390 image->pitch = pitch * cpp;
391 image->bo = drm_intel_bo_gem_create_from_name(intelScreen->bufmgr, "image",
392 name);
393 if (!image->bo) {
394 free(image);
395 return NULL;
396 }
397
398 return image;
399 }
400
401 static __DRIimage *
402 intel_create_image_from_renderbuffer(__DRIcontext *context,
403 int renderbuffer, void *loaderPrivate)
404 {
405 __DRIimage *image;
406 struct brw_context *brw = context->driverPrivate;
407 struct gl_context *ctx = &brw->ctx;
408 struct gl_renderbuffer *rb;
409 struct intel_renderbuffer *irb;
410
411 rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
412 if (!rb) {
413 _mesa_error(ctx, GL_INVALID_OPERATION, "glRenderbufferExternalMESA");
414 return NULL;
415 }
416
417 irb = intel_renderbuffer(rb);
418 intel_miptree_make_shareable(brw, irb->mt);
419 image = calloc(1, sizeof *image);
420 if (image == NULL)
421 return NULL;
422
423 image->internal_format = rb->InternalFormat;
424 image->format = rb->Format;
425 image->offset = 0;
426 image->data = loaderPrivate;
427 drm_intel_bo_unreference(image->bo);
428 image->bo = irb->mt->bo;
429 drm_intel_bo_reference(irb->mt->bo);
430 image->width = rb->Width;
431 image->height = rb->Height;
432 image->pitch = irb->mt->pitch;
433 image->dri_format = driGLFormatToImageFormat(image->format);
434 image->has_depthstencil = irb->mt->stencil_mt? true : false;
435
436 rb->NeedsFinishRenderTexture = true;
437 return image;
438 }
439
440 static __DRIimage *
441 intel_create_image_from_texture(__DRIcontext *context, int target,
442 unsigned texture, int zoffset,
443 int level,
444 unsigned *error,
445 void *loaderPrivate)
446 {
447 __DRIimage *image;
448 struct brw_context *brw = context->driverPrivate;
449 struct gl_texture_object *obj;
450 struct intel_texture_object *iobj;
451 GLuint face = 0;
452
453 obj = _mesa_lookup_texture(&brw->ctx, texture);
454 if (!obj || obj->Target != target) {
455 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
456 return NULL;
457 }
458
459 if (target == GL_TEXTURE_CUBE_MAP)
460 face = zoffset;
461
462 _mesa_test_texobj_completeness(&brw->ctx, obj);
463 iobj = intel_texture_object(obj);
464 if (!obj->_BaseComplete || (level > 0 && !obj->_MipmapComplete)) {
465 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
466 return NULL;
467 }
468
469 if (level < obj->BaseLevel || level > obj->_MaxLevel) {
470 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
471 return NULL;
472 }
473
474 if (target == GL_TEXTURE_3D && obj->Image[face][level]->Depth < zoffset) {
475 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
476 return NULL;
477 }
478 image = calloc(1, sizeof *image);
479 if (image == NULL) {
480 *error = __DRI_IMAGE_ERROR_BAD_ALLOC;
481 return NULL;
482 }
483
484 image->internal_format = obj->Image[face][level]->InternalFormat;
485 image->format = obj->Image[face][level]->TexFormat;
486 image->data = loaderPrivate;
487 intel_setup_image_from_mipmap_tree(brw, image, iobj->mt, level, zoffset);
488 image->dri_format = driGLFormatToImageFormat(image->format);
489 image->has_depthstencil = iobj->mt->stencil_mt? true : false;
490 if (image->dri_format == MESA_FORMAT_NONE) {
491 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
492 free(image);
493 return NULL;
494 }
495
496 *error = __DRI_IMAGE_ERROR_SUCCESS;
497 return image;
498 }
499
500 static void
501 intel_destroy_image(__DRIimage *image)
502 {
503 drm_intel_bo_unreference(image->bo);
504 free(image);
505 }
506
507 static __DRIimage *
508 intel_create_image(__DRIscreen *screen,
509 int width, int height, int format,
510 unsigned int use,
511 void *loaderPrivate)
512 {
513 __DRIimage *image;
514 struct intel_screen *intelScreen = screen->driverPrivate;
515 uint32_t tiling;
516 int cpp;
517 unsigned long pitch;
518
519 tiling = I915_TILING_X;
520 if (use & __DRI_IMAGE_USE_CURSOR) {
521 if (width != 64 || height != 64)
522 return NULL;
523 tiling = I915_TILING_NONE;
524 }
525
526 if (use & __DRI_IMAGE_USE_LINEAR)
527 tiling = I915_TILING_NONE;
528
529 image = intel_allocate_image(format, loaderPrivate);
530 if (image == NULL)
531 return NULL;
532
533 cpp = _mesa_get_format_bytes(image->format);
534 image->bo = drm_intel_bo_alloc_tiled(intelScreen->bufmgr, "image",
535 width, height, cpp, &tiling,
536 &pitch, 0);
537 if (image->bo == NULL) {
538 free(image);
539 return NULL;
540 }
541 image->width = width;
542 image->height = height;
543 image->pitch = pitch;
544
545 return image;
546 }
547
548 static GLboolean
549 intel_query_image(__DRIimage *image, int attrib, int *value)
550 {
551 switch (attrib) {
552 case __DRI_IMAGE_ATTRIB_STRIDE:
553 *value = image->pitch;
554 return true;
555 case __DRI_IMAGE_ATTRIB_HANDLE:
556 *value = image->bo->handle;
557 return true;
558 case __DRI_IMAGE_ATTRIB_NAME:
559 return !drm_intel_bo_flink(image->bo, (uint32_t *) value);
560 case __DRI_IMAGE_ATTRIB_FORMAT:
561 *value = image->dri_format;
562 return true;
563 case __DRI_IMAGE_ATTRIB_WIDTH:
564 *value = image->width;
565 return true;
566 case __DRI_IMAGE_ATTRIB_HEIGHT:
567 *value = image->height;
568 return true;
569 case __DRI_IMAGE_ATTRIB_COMPONENTS:
570 if (image->planar_format == NULL)
571 return false;
572 *value = image->planar_format->components;
573 return true;
574 case __DRI_IMAGE_ATTRIB_FD:
575 if (drm_intel_bo_gem_export_to_prime(image->bo, value) == 0)
576 return true;
577 return false;
578 case __DRI_IMAGE_ATTRIB_FOURCC:
579 if (intel_lookup_fourcc(image->dri_format, value))
580 return true;
581 return false;
582 case __DRI_IMAGE_ATTRIB_NUM_PLANES:
583 *value = 1;
584 return true;
585
586 default:
587 return false;
588 }
589 }
590
591 static __DRIimage *
592 intel_dup_image(__DRIimage *orig_image, void *loaderPrivate)
593 {
594 __DRIimage *image;
595
596 image = calloc(1, sizeof *image);
597 if (image == NULL)
598 return NULL;
599
600 drm_intel_bo_reference(orig_image->bo);
601 image->bo = orig_image->bo;
602 image->internal_format = orig_image->internal_format;
603 image->planar_format = orig_image->planar_format;
604 image->dri_format = orig_image->dri_format;
605 image->format = orig_image->format;
606 image->offset = orig_image->offset;
607 image->width = orig_image->width;
608 image->height = orig_image->height;
609 image->pitch = orig_image->pitch;
610 image->tile_x = orig_image->tile_x;
611 image->tile_y = orig_image->tile_y;
612 image->has_depthstencil = orig_image->has_depthstencil;
613 image->data = loaderPrivate;
614
615 memcpy(image->strides, orig_image->strides, sizeof(image->strides));
616 memcpy(image->offsets, orig_image->offsets, sizeof(image->offsets));
617
618 return image;
619 }
620
621 static GLboolean
622 intel_validate_usage(__DRIimage *image, unsigned int use)
623 {
624 if (use & __DRI_IMAGE_USE_CURSOR) {
625 if (image->width != 64 || image->height != 64)
626 return GL_FALSE;
627 }
628
629 return GL_TRUE;
630 }
631
632 static __DRIimage *
633 intel_create_image_from_names(__DRIscreen *screen,
634 int width, int height, int fourcc,
635 int *names, int num_names,
636 int *strides, int *offsets,
637 void *loaderPrivate)
638 {
639 struct intel_image_format *f = NULL;
640 __DRIimage *image;
641 int i, index;
642
643 if (screen == NULL || names == NULL || num_names != 1)
644 return NULL;
645
646 f = intel_image_format_lookup(fourcc);
647 if (f == NULL)
648 return NULL;
649
650 image = intel_create_image_from_name(screen, width, height,
651 __DRI_IMAGE_FORMAT_NONE,
652 names[0], strides[0],
653 loaderPrivate);
654
655 if (image == NULL)
656 return NULL;
657
658 image->planar_format = f;
659 for (i = 0; i < f->nplanes; i++) {
660 index = f->planes[i].buffer_index;
661 image->offsets[index] = offsets[index];
662 image->strides[index] = strides[index];
663 }
664
665 return image;
666 }
667
668 static __DRIimage *
669 intel_create_image_from_fds(__DRIscreen *screen,
670 int width, int height, int fourcc,
671 int *fds, int num_fds, int *strides, int *offsets,
672 void *loaderPrivate)
673 {
674 struct intel_screen *intelScreen = screen->driverPrivate;
675 struct intel_image_format *f;
676 __DRIimage *image;
677 int i, index;
678
679 if (fds == NULL || num_fds != 1)
680 return NULL;
681
682 f = intel_image_format_lookup(fourcc);
683 if (f == NULL)
684 return NULL;
685
686 if (f->nplanes == 1)
687 image = intel_allocate_image(f->planes[0].dri_format, loaderPrivate);
688 else
689 image = intel_allocate_image(__DRI_IMAGE_FORMAT_NONE, loaderPrivate);
690
691 if (image == NULL)
692 return NULL;
693
694 image->bo = drm_intel_bo_gem_create_from_prime(intelScreen->bufmgr,
695 fds[0],
696 height * strides[0]);
697 if (image->bo == NULL) {
698 free(image);
699 return NULL;
700 }
701 image->width = width;
702 image->height = height;
703 image->pitch = strides[0];
704
705 image->planar_format = f;
706 for (i = 0; i < f->nplanes; i++) {
707 index = f->planes[i].buffer_index;
708 image->offsets[index] = offsets[index];
709 image->strides[index] = strides[index];
710 }
711
712 if (f->nplanes == 1) {
713 image->offset = image->offsets[0];
714 intel_image_warn_if_unaligned(image, __func__);
715 }
716
717 return image;
718 }
719
720 static __DRIimage *
721 intel_create_image_from_dma_bufs(__DRIscreen *screen,
722 int width, int height, int fourcc,
723 int *fds, int num_fds,
724 int *strides, int *offsets,
725 enum __DRIYUVColorSpace yuv_color_space,
726 enum __DRISampleRange sample_range,
727 enum __DRIChromaSiting horizontal_siting,
728 enum __DRIChromaSiting vertical_siting,
729 unsigned *error,
730 void *loaderPrivate)
731 {
732 __DRIimage *image;
733 struct intel_image_format *f = intel_image_format_lookup(fourcc);
734
735 /* For now only packed formats that have native sampling are supported. */
736 if (!f || f->nplanes != 1) {
737 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
738 return NULL;
739 }
740
741 image = intel_create_image_from_fds(screen, width, height, fourcc, fds,
742 num_fds, strides, offsets,
743 loaderPrivate);
744
745 /*
746 * Invalid parameters and any inconsistencies between are assumed to be
747 * checked by the caller. Therefore besides unsupported formats one can fail
748 * only in allocation.
749 */
750 if (!image) {
751 *error = __DRI_IMAGE_ERROR_BAD_ALLOC;
752 return NULL;
753 }
754
755 image->dma_buf_imported = true;
756 image->yuv_color_space = yuv_color_space;
757 image->sample_range = sample_range;
758 image->horizontal_siting = horizontal_siting;
759 image->vertical_siting = vertical_siting;
760
761 *error = __DRI_IMAGE_ERROR_SUCCESS;
762 return image;
763 }
764
765 static __DRIimage *
766 intel_from_planar(__DRIimage *parent, int plane, void *loaderPrivate)
767 {
768 int width, height, offset, stride, dri_format, index;
769 struct intel_image_format *f;
770 __DRIimage *image;
771
772 if (parent == NULL || parent->planar_format == NULL)
773 return NULL;
774
775 f = parent->planar_format;
776
777 if (plane >= f->nplanes)
778 return NULL;
779
780 width = parent->width >> f->planes[plane].width_shift;
781 height = parent->height >> f->planes[plane].height_shift;
782 dri_format = f->planes[plane].dri_format;
783 index = f->planes[plane].buffer_index;
784 offset = parent->offsets[index];
785 stride = parent->strides[index];
786
787 image = intel_allocate_image(dri_format, loaderPrivate);
788 if (image == NULL)
789 return NULL;
790
791 if (offset + height * stride > parent->bo->size) {
792 _mesa_warning(NULL, "intel_create_sub_image: subimage out of bounds");
793 free(image);
794 return NULL;
795 }
796
797 image->bo = parent->bo;
798 drm_intel_bo_reference(parent->bo);
799
800 image->width = width;
801 image->height = height;
802 image->pitch = stride;
803 image->offset = offset;
804
805 intel_image_warn_if_unaligned(image, __func__);
806
807 return image;
808 }
809
810 static const __DRIimageExtension intelImageExtension = {
811 .base = { __DRI_IMAGE, 11 },
812
813 .createImageFromName = intel_create_image_from_name,
814 .createImageFromRenderbuffer = intel_create_image_from_renderbuffer,
815 .destroyImage = intel_destroy_image,
816 .createImage = intel_create_image,
817 .queryImage = intel_query_image,
818 .dupImage = intel_dup_image,
819 .validateUsage = intel_validate_usage,
820 .createImageFromNames = intel_create_image_from_names,
821 .fromPlanar = intel_from_planar,
822 .createImageFromTexture = intel_create_image_from_texture,
823 .createImageFromFds = intel_create_image_from_fds,
824 .createImageFromDmaBufs = intel_create_image_from_dma_bufs,
825 .blitImage = NULL,
826 .getCapabilities = NULL
827 };
828
829 static int
830 brw_query_renderer_integer(__DRIscreen *psp, int param, unsigned int *value)
831 {
832 const struct intel_screen *const intelScreen =
833 (struct intel_screen *) psp->driverPrivate;
834
835 switch (param) {
836 case __DRI2_RENDERER_VENDOR_ID:
837 value[0] = 0x8086;
838 return 0;
839 case __DRI2_RENDERER_DEVICE_ID:
840 value[0] = intelScreen->deviceID;
841 return 0;
842 case __DRI2_RENDERER_ACCELERATED:
843 value[0] = 1;
844 return 0;
845 case __DRI2_RENDERER_VIDEO_MEMORY: {
846 /* Once a batch uses more than 75% of the maximum mappable size, we
847 * assume that there's some fragmentation, and we start doing extra
848 * flushing, etc. That's the big cliff apps will care about.
849 */
850 size_t aper_size;
851 size_t mappable_size;
852
853 drm_intel_get_aperture_sizes(psp->fd, &mappable_size, &aper_size);
854
855 const unsigned gpu_mappable_megabytes =
856 (aper_size / (1024 * 1024)) * 3 / 4;
857
858 const long system_memory_pages = sysconf(_SC_PHYS_PAGES);
859 const long system_page_size = sysconf(_SC_PAGE_SIZE);
860
861 if (system_memory_pages <= 0 || system_page_size <= 0)
862 return -1;
863
864 const uint64_t system_memory_bytes = (uint64_t) system_memory_pages
865 * (uint64_t) system_page_size;
866
867 const unsigned system_memory_megabytes =
868 (unsigned) (system_memory_bytes / (1024 * 1024));
869
870 value[0] = MIN2(system_memory_megabytes, gpu_mappable_megabytes);
871 return 0;
872 }
873 case __DRI2_RENDERER_UNIFIED_MEMORY_ARCHITECTURE:
874 value[0] = 1;
875 return 0;
876 default:
877 return driQueryRendererIntegerCommon(psp, param, value);
878 }
879
880 return -1;
881 }
882
883 static int
884 brw_query_renderer_string(__DRIscreen *psp, int param, const char **value)
885 {
886 const struct intel_screen *intelScreen =
887 (struct intel_screen *) psp->driverPrivate;
888
889 switch (param) {
890 case __DRI2_RENDERER_VENDOR_ID:
891 value[0] = brw_vendor_string;
892 return 0;
893 case __DRI2_RENDERER_DEVICE_ID:
894 value[0] = brw_get_renderer_string(intelScreen);
895 return 0;
896 default:
897 break;
898 }
899
900 return -1;
901 }
902
903 static const __DRI2rendererQueryExtension intelRendererQueryExtension = {
904 .base = { __DRI2_RENDERER_QUERY, 1 },
905
906 .queryInteger = brw_query_renderer_integer,
907 .queryString = brw_query_renderer_string
908 };
909
910 static const __DRIrobustnessExtension dri2Robustness = {
911 .base = { __DRI2_ROBUSTNESS, 1 }
912 };
913
914 static const __DRIextension *intelScreenExtensions[] = {
915 &intelTexBufferExtension.base,
916 &intelFenceExtension.base,
917 &intelFlushExtension.base,
918 &intelImageExtension.base,
919 &intelRendererQueryExtension.base,
920 &dri2ConfigQueryExtension.base,
921 NULL
922 };
923
924 static const __DRIextension *intelRobustScreenExtensions[] = {
925 &intelTexBufferExtension.base,
926 &intelFenceExtension.base,
927 &intelFlushExtension.base,
928 &intelImageExtension.base,
929 &intelRendererQueryExtension.base,
930 &dri2ConfigQueryExtension.base,
931 &dri2Robustness.base,
932 NULL
933 };
934
935 static bool
936 intel_get_param(__DRIscreen *psp, int param, int *value)
937 {
938 int ret;
939 struct drm_i915_getparam gp;
940
941 memset(&gp, 0, sizeof(gp));
942 gp.param = param;
943 gp.value = value;
944
945 ret = drmCommandWriteRead(psp->fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
946 if (ret) {
947 if (ret != -EINVAL)
948 _mesa_warning(NULL, "drm_i915_getparam: %d", ret);
949 return false;
950 }
951
952 return true;
953 }
954
955 static bool
956 intel_get_boolean(__DRIscreen *psp, int param)
957 {
958 int value = 0;
959 return intel_get_param(psp, param, &value) && value;
960 }
961
962 static void
963 intelDestroyScreen(__DRIscreen * sPriv)
964 {
965 struct intel_screen *intelScreen = sPriv->driverPrivate;
966
967 dri_bufmgr_destroy(intelScreen->bufmgr);
968 driDestroyOptionInfo(&intelScreen->optionCache);
969
970 ralloc_free(intelScreen);
971 sPriv->driverPrivate = NULL;
972 }
973
974
975 /**
976 * This is called when we need to set up GL rendering to a new X window.
977 */
978 static GLboolean
979 intelCreateBuffer(__DRIscreen * driScrnPriv,
980 __DRIdrawable * driDrawPriv,
981 const struct gl_config * mesaVis, GLboolean isPixmap)
982 {
983 struct intel_renderbuffer *rb;
984 struct intel_screen *screen = (struct intel_screen*) driScrnPriv->driverPrivate;
985 mesa_format rgbFormat;
986 unsigned num_samples = intel_quantize_num_samples(screen, mesaVis->samples);
987 struct gl_framebuffer *fb;
988
989 if (isPixmap)
990 return false;
991
992 fb = CALLOC_STRUCT(gl_framebuffer);
993 if (!fb)
994 return false;
995
996 _mesa_initialize_window_framebuffer(fb, mesaVis);
997
998 if (screen->winsys_msaa_samples_override != -1) {
999 num_samples = screen->winsys_msaa_samples_override;
1000 fb->Visual.samples = num_samples;
1001 }
1002
1003 if (mesaVis->redBits == 5)
1004 rgbFormat = MESA_FORMAT_B5G6R5_UNORM;
1005 else if (mesaVis->sRGBCapable)
1006 rgbFormat = MESA_FORMAT_B8G8R8A8_SRGB;
1007 else if (mesaVis->alphaBits == 0)
1008 rgbFormat = MESA_FORMAT_B8G8R8X8_UNORM;
1009 else {
1010 rgbFormat = MESA_FORMAT_B8G8R8A8_SRGB;
1011 fb->Visual.sRGBCapable = true;
1012 }
1013
1014 /* setup the hardware-based renderbuffers */
1015 rb = intel_create_renderbuffer(rgbFormat, num_samples);
1016 _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &rb->Base.Base);
1017
1018 if (mesaVis->doubleBufferMode) {
1019 rb = intel_create_renderbuffer(rgbFormat, num_samples);
1020 _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &rb->Base.Base);
1021 }
1022
1023 /*
1024 * Assert here that the gl_config has an expected depth/stencil bit
1025 * combination: one of d24/s8, d16/s0, d0/s0. (See intelInitScreen2(),
1026 * which constructs the advertised configs.)
1027 */
1028 if (mesaVis->depthBits == 24) {
1029 assert(mesaVis->stencilBits == 8);
1030
1031 if (screen->devinfo->has_hiz_and_separate_stencil) {
1032 rb = intel_create_private_renderbuffer(MESA_FORMAT_Z24_UNORM_X8_UINT,
1033 num_samples);
1034 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &rb->Base.Base);
1035 rb = intel_create_private_renderbuffer(MESA_FORMAT_S_UINT8,
1036 num_samples);
1037 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &rb->Base.Base);
1038 } else {
1039 /*
1040 * Use combined depth/stencil. Note that the renderbuffer is
1041 * attached to two attachment points.
1042 */
1043 rb = intel_create_private_renderbuffer(MESA_FORMAT_Z24_UNORM_S8_UINT,
1044 num_samples);
1045 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &rb->Base.Base);
1046 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &rb->Base.Base);
1047 }
1048 }
1049 else if (mesaVis->depthBits == 16) {
1050 assert(mesaVis->stencilBits == 0);
1051 rb = intel_create_private_renderbuffer(MESA_FORMAT_Z_UNORM16,
1052 num_samples);
1053 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &rb->Base.Base);
1054 }
1055 else {
1056 assert(mesaVis->depthBits == 0);
1057 assert(mesaVis->stencilBits == 0);
1058 }
1059
1060 /* now add any/all software-based renderbuffers we may need */
1061 _swrast_add_soft_renderbuffers(fb,
1062 false, /* never sw color */
1063 false, /* never sw depth */
1064 false, /* never sw stencil */
1065 mesaVis->accumRedBits > 0,
1066 false, /* never sw alpha */
1067 false /* never sw aux */ );
1068 driDrawPriv->driverPrivate = fb;
1069
1070 return true;
1071 }
1072
1073 static void
1074 intelDestroyBuffer(__DRIdrawable * driDrawPriv)
1075 {
1076 struct gl_framebuffer *fb = driDrawPriv->driverPrivate;
1077
1078 _mesa_reference_framebuffer(&fb, NULL);
1079 }
1080
1081 static void
1082 intel_detect_sseu(struct intel_screen *intelScreen)
1083 {
1084 assert(intelScreen->devinfo->gen >= 8);
1085 int ret;
1086
1087 intelScreen->subslice_total = -1;
1088 intelScreen->eu_total = -1;
1089
1090 ret = intel_get_param(intelScreen->driScrnPriv, I915_PARAM_SUBSLICE_TOTAL,
1091 &intelScreen->subslice_total);
1092 if (ret != -EINVAL)
1093 goto err_out;
1094
1095 ret = intel_get_param(intelScreen->driScrnPriv,
1096 I915_PARAM_EU_TOTAL, &intelScreen->eu_total);
1097 if (ret != -EINVAL)
1098 goto err_out;
1099
1100 /* Without this information, we cannot get the right Braswell brandstrings,
1101 * and we have to use conservative numbers for GPGPU on many platforms, but
1102 * otherwise, things will just work.
1103 */
1104 if (intelScreen->subslice_total < 1 || intelScreen->eu_total < 1)
1105 _mesa_warning(NULL,
1106 "Kernel 4.1 required to properly query GPU properties.\n");
1107
1108 return;
1109
1110 err_out:
1111 intelScreen->subslice_total = -1;
1112 intelScreen->eu_total = -1;
1113 _mesa_warning(NULL, "Failed to query GPU properties.\n");
1114 }
1115
1116 static bool
1117 intel_init_bufmgr(struct intel_screen *intelScreen)
1118 {
1119 __DRIscreen *spriv = intelScreen->driScrnPriv;
1120
1121 intelScreen->no_hw = getenv("INTEL_NO_HW") != NULL;
1122
1123 intelScreen->bufmgr = intel_bufmgr_gem_init(spriv->fd, BATCH_SZ);
1124 if (intelScreen->bufmgr == NULL) {
1125 fprintf(stderr, "[%s:%u] Error initializing buffer manager.\n",
1126 __func__, __LINE__);
1127 return false;
1128 }
1129
1130 drm_intel_bufmgr_gem_enable_fenced_relocs(intelScreen->bufmgr);
1131
1132 if (!intel_get_boolean(spriv, I915_PARAM_HAS_RELAXED_DELTA)) {
1133 fprintf(stderr, "[%s: %u] Kernel 2.6.39 required.\n", __func__, __LINE__);
1134 return false;
1135 }
1136
1137 return true;
1138 }
1139
1140 static bool
1141 intel_detect_swizzling(struct intel_screen *screen)
1142 {
1143 drm_intel_bo *buffer;
1144 unsigned long flags = 0;
1145 unsigned long aligned_pitch;
1146 uint32_t tiling = I915_TILING_X;
1147 uint32_t swizzle_mode = 0;
1148
1149 buffer = drm_intel_bo_alloc_tiled(screen->bufmgr, "swizzle test",
1150 64, 64, 4,
1151 &tiling, &aligned_pitch, flags);
1152 if (buffer == NULL)
1153 return false;
1154
1155 drm_intel_bo_get_tiling(buffer, &tiling, &swizzle_mode);
1156 drm_intel_bo_unreference(buffer);
1157
1158 if (swizzle_mode == I915_BIT_6_SWIZZLE_NONE)
1159 return false;
1160 else
1161 return true;
1162 }
1163
1164 static int
1165 intel_detect_timestamp(struct intel_screen *screen)
1166 {
1167 uint64_t dummy = 0, last = 0;
1168 int upper, lower, loops;
1169
1170 /* On 64bit systems, some old kernels trigger a hw bug resulting in the
1171 * TIMESTAMP register being shifted and the low 32bits always zero.
1172 *
1173 * More recent kernels offer an interface to read the full 36bits
1174 * everywhere.
1175 */
1176 if (drm_intel_reg_read(screen->bufmgr, TIMESTAMP | 1, &dummy) == 0)
1177 return 3;
1178
1179 /* Determine if we have a 32bit or 64bit kernel by inspecting the
1180 * upper 32bits for a rapidly changing timestamp.
1181 */
1182 if (drm_intel_reg_read(screen->bufmgr, TIMESTAMP, &last))
1183 return 0;
1184
1185 upper = lower = 0;
1186 for (loops = 0; loops < 10; loops++) {
1187 /* The TIMESTAMP should change every 80ns, so several round trips
1188 * through the kernel should be enough to advance it.
1189 */
1190 if (drm_intel_reg_read(screen->bufmgr, TIMESTAMP, &dummy))
1191 return 0;
1192
1193 upper += (dummy >> 32) != (last >> 32);
1194 if (upper > 1) /* beware 32bit counter overflow */
1195 return 2; /* upper dword holds the low 32bits of the timestamp */
1196
1197 lower += (dummy & 0xffffffff) != (last & 0xffffffff);
1198 if (lower > 1)
1199 return 1; /* timestamp is unshifted */
1200
1201 last = dummy;
1202 }
1203
1204 /* No advancement? No timestamp! */
1205 return 0;
1206 }
1207
1208 /**
1209 * Return array of MSAA modes supported by the hardware. The array is
1210 * zero-terminated and sorted in decreasing order.
1211 */
1212 const int*
1213 intel_supported_msaa_modes(const struct intel_screen *screen)
1214 {
1215 static const int gen9_modes[] = {16, 8, 4, 2, 0, -1};
1216 static const int gen8_modes[] = {8, 4, 2, 0, -1};
1217 static const int gen7_modes[] = {8, 4, 0, -1};
1218 static const int gen6_modes[] = {4, 0, -1};
1219 static const int gen4_modes[] = {0, -1};
1220
1221 if (screen->devinfo->gen >= 9) {
1222 return gen9_modes;
1223 } else if (screen->devinfo->gen >= 8) {
1224 return gen8_modes;
1225 } else if (screen->devinfo->gen >= 7) {
1226 return gen7_modes;
1227 } else if (screen->devinfo->gen == 6) {
1228 return gen6_modes;
1229 } else {
1230 return gen4_modes;
1231 }
1232 }
1233
1234 static __DRIconfig**
1235 intel_screen_make_configs(__DRIscreen *dri_screen)
1236 {
1237 static const mesa_format formats[] = {
1238 MESA_FORMAT_B5G6R5_UNORM,
1239 MESA_FORMAT_B8G8R8A8_UNORM,
1240 MESA_FORMAT_B8G8R8X8_UNORM
1241 };
1242
1243 /* GLX_SWAP_COPY_OML is not supported due to page flipping. */
1244 static const GLenum back_buffer_modes[] = {
1245 GLX_SWAP_UNDEFINED_OML, GLX_NONE,
1246 };
1247
1248 static const uint8_t singlesample_samples[1] = {0};
1249 static const uint8_t multisample_samples[2] = {4, 8};
1250
1251 struct intel_screen *screen = dri_screen->driverPrivate;
1252 const struct brw_device_info *devinfo = screen->devinfo;
1253 uint8_t depth_bits[4], stencil_bits[4];
1254 __DRIconfig **configs = NULL;
1255
1256 /* Generate singlesample configs without accumulation buffer. */
1257 for (unsigned i = 0; i < ARRAY_SIZE(formats); i++) {
1258 __DRIconfig **new_configs;
1259 int num_depth_stencil_bits = 2;
1260
1261 /* Starting with DRI2 protocol version 1.1 we can request a depth/stencil
1262 * buffer that has a different number of bits per pixel than the color
1263 * buffer, gen >= 6 supports this.
1264 */
1265 depth_bits[0] = 0;
1266 stencil_bits[0] = 0;
1267
1268 if (formats[i] == MESA_FORMAT_B5G6R5_UNORM) {
1269 depth_bits[1] = 16;
1270 stencil_bits[1] = 0;
1271 if (devinfo->gen >= 6) {
1272 depth_bits[2] = 24;
1273 stencil_bits[2] = 8;
1274 num_depth_stencil_bits = 3;
1275 }
1276 } else {
1277 depth_bits[1] = 24;
1278 stencil_bits[1] = 8;
1279 }
1280
1281 new_configs = driCreateConfigs(formats[i],
1282 depth_bits,
1283 stencil_bits,
1284 num_depth_stencil_bits,
1285 back_buffer_modes, 2,
1286 singlesample_samples, 1,
1287 false);
1288 configs = driConcatConfigs(configs, new_configs);
1289 }
1290
1291 /* Generate the minimum possible set of configs that include an
1292 * accumulation buffer.
1293 */
1294 for (unsigned i = 0; i < ARRAY_SIZE(formats); i++) {
1295 __DRIconfig **new_configs;
1296
1297 if (formats[i] == MESA_FORMAT_B5G6R5_UNORM) {
1298 depth_bits[0] = 16;
1299 stencil_bits[0] = 0;
1300 } else {
1301 depth_bits[0] = 24;
1302 stencil_bits[0] = 8;
1303 }
1304
1305 new_configs = driCreateConfigs(formats[i],
1306 depth_bits, stencil_bits, 1,
1307 back_buffer_modes, 1,
1308 singlesample_samples, 1,
1309 true);
1310 configs = driConcatConfigs(configs, new_configs);
1311 }
1312
1313 /* Generate multisample configs.
1314 *
1315 * This loop breaks early, and hence is a no-op, on gen < 6.
1316 *
1317 * Multisample configs must follow the singlesample configs in order to
1318 * work around an X server bug present in 1.12. The X server chooses to
1319 * associate the first listed RGBA888-Z24S8 config, regardless of its
1320 * sample count, with the 32-bit depth visual used for compositing.
1321 *
1322 * Only doublebuffer configs with GLX_SWAP_UNDEFINED_OML behavior are
1323 * supported. Singlebuffer configs are not supported because no one wants
1324 * them.
1325 */
1326 for (unsigned i = 0; i < ARRAY_SIZE(formats); i++) {
1327 if (devinfo->gen < 6)
1328 break;
1329
1330 __DRIconfig **new_configs;
1331 const int num_depth_stencil_bits = 2;
1332 int num_msaa_modes = 0;
1333
1334 depth_bits[0] = 0;
1335 stencil_bits[0] = 0;
1336
1337 if (formats[i] == MESA_FORMAT_B5G6R5_UNORM) {
1338 depth_bits[1] = 16;
1339 stencil_bits[1] = 0;
1340 } else {
1341 depth_bits[1] = 24;
1342 stencil_bits[1] = 8;
1343 }
1344
1345 if (devinfo->gen >= 7)
1346 num_msaa_modes = 2;
1347 else if (devinfo->gen == 6)
1348 num_msaa_modes = 1;
1349
1350 new_configs = driCreateConfigs(formats[i],
1351 depth_bits,
1352 stencil_bits,
1353 num_depth_stencil_bits,
1354 back_buffer_modes, 1,
1355 multisample_samples,
1356 num_msaa_modes,
1357 false);
1358 configs = driConcatConfigs(configs, new_configs);
1359 }
1360
1361 if (configs == NULL) {
1362 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
1363 __LINE__);
1364 return NULL;
1365 }
1366
1367 return configs;
1368 }
1369
1370 static void
1371 set_max_gl_versions(struct intel_screen *screen)
1372 {
1373 __DRIscreen *psp = screen->driScrnPriv;
1374
1375 switch (screen->devinfo->gen) {
1376 case 9:
1377 case 8:
1378 psp->max_gl_core_version = 33;
1379 psp->max_gl_compat_version = 30;
1380 psp->max_gl_es1_version = 11;
1381 psp->max_gl_es2_version = 31;
1382 break;
1383 case 7:
1384 case 6:
1385 psp->max_gl_core_version = 33;
1386 psp->max_gl_compat_version = 30;
1387 psp->max_gl_es1_version = 11;
1388 psp->max_gl_es2_version = 30;
1389 break;
1390 case 5:
1391 case 4:
1392 psp->max_gl_core_version = 0;
1393 psp->max_gl_compat_version = 21;
1394 psp->max_gl_es1_version = 11;
1395 psp->max_gl_es2_version = 20;
1396 break;
1397 default:
1398 unreachable("unrecognized intel_screen::gen");
1399 }
1400 }
1401
1402 /**
1403 * Return the revision (generally the revid field of the PCI header) of the
1404 * graphics device.
1405 *
1406 * XXX: This function is useful to keep around even if it is not currently in
1407 * use. It is necessary for new platforms and revision specific workarounds or
1408 * features. Please don't remove it so that we know it at least continues to
1409 * build.
1410 */
1411 static __attribute__((__unused__)) int
1412 brw_get_revision(int fd)
1413 {
1414 struct drm_i915_getparam gp;
1415 int revision;
1416 int ret;
1417
1418 memset(&gp, 0, sizeof(gp));
1419 gp.param = I915_PARAM_REVISION;
1420 gp.value = &revision;
1421
1422 ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
1423 if (ret)
1424 revision = -1;
1425
1426 return revision;
1427 }
1428
1429 /* Drop when RS headers get pulled to libdrm */
1430 #ifndef I915_PARAM_HAS_RESOURCE_STREAMER
1431 #define I915_PARAM_HAS_RESOURCE_STREAMER 36
1432 #endif
1433
1434 /**
1435 * This is the driver specific part of the createNewScreen entry point.
1436 * Called when using DRI2.
1437 *
1438 * \return the struct gl_config supported by this driver
1439 */
1440 static const
1441 __DRIconfig **intelInitScreen2(__DRIscreen *psp)
1442 {
1443 struct intel_screen *intelScreen;
1444
1445 if (psp->image.loader) {
1446 } else if (psp->dri2.loader->base.version <= 2 ||
1447 psp->dri2.loader->getBuffersWithFormat == NULL) {
1448 fprintf(stderr,
1449 "\nERROR! DRI2 loader with getBuffersWithFormat() "
1450 "support required\n");
1451 return false;
1452 }
1453
1454 /* Allocate the private area */
1455 intelScreen = rzalloc(NULL, struct intel_screen);
1456 if (!intelScreen) {
1457 fprintf(stderr, "\nERROR! Allocating private area failed\n");
1458 return false;
1459 }
1460 /* parse information in __driConfigOptions */
1461 driParseOptionInfo(&intelScreen->optionCache, brw_config_options.xml);
1462
1463 intelScreen->driScrnPriv = psp;
1464 psp->driverPrivate = (void *) intelScreen;
1465
1466 if (!intel_init_bufmgr(intelScreen))
1467 return false;
1468
1469 intelScreen->deviceID = drm_intel_bufmgr_gem_get_devid(intelScreen->bufmgr);
1470 intelScreen->devinfo = brw_get_device_info(intelScreen->deviceID);
1471 if (!intelScreen->devinfo)
1472 return false;
1473
1474 brw_process_intel_debug_variable();
1475
1476 if (INTEL_DEBUG & DEBUG_BUFMGR)
1477 dri_bufmgr_set_debug(intelScreen->bufmgr, true);
1478
1479 if ((INTEL_DEBUG & DEBUG_SHADER_TIME) && intelScreen->devinfo->gen < 7) {
1480 fprintf(stderr,
1481 "shader_time debugging requires gen7 (Ivybridge) or better.\n");
1482 INTEL_DEBUG &= ~DEBUG_SHADER_TIME;
1483 }
1484
1485 if (INTEL_DEBUG & DEBUG_AUB)
1486 drm_intel_bufmgr_gem_set_aub_dump(intelScreen->bufmgr, true);
1487
1488 intelScreen->hw_has_swizzling = intel_detect_swizzling(intelScreen);
1489 intelScreen->hw_has_timestamp = intel_detect_timestamp(intelScreen);
1490
1491 /* GENs prior to 8 do not support EU/Subslice info */
1492 if (intelScreen->devinfo->gen >= 8)
1493 intel_detect_sseu(intelScreen);
1494
1495 const char *force_msaa = getenv("INTEL_FORCE_MSAA");
1496 if (force_msaa) {
1497 intelScreen->winsys_msaa_samples_override =
1498 intel_quantize_num_samples(intelScreen, atoi(force_msaa));
1499 printf("Forcing winsys sample count to %d\n",
1500 intelScreen->winsys_msaa_samples_override);
1501 } else {
1502 intelScreen->winsys_msaa_samples_override = -1;
1503 }
1504
1505 set_max_gl_versions(intelScreen);
1506
1507 /* Notification of GPU resets requires hardware contexts and a kernel new
1508 * enough to support DRM_IOCTL_I915_GET_RESET_STATS. If the ioctl is
1509 * supported, calling it with a context of 0 will either generate EPERM or
1510 * no error. If the ioctl is not supported, it always generate EINVAL.
1511 * Use this to determine whether to advertise the __DRI2_ROBUSTNESS
1512 * extension to the loader.
1513 *
1514 * Don't even try on pre-Gen6, since we don't attempt to use contexts there.
1515 */
1516 if (intelScreen->devinfo->gen >= 6) {
1517 struct drm_i915_reset_stats stats;
1518 memset(&stats, 0, sizeof(stats));
1519
1520 const int ret = drmIoctl(psp->fd, DRM_IOCTL_I915_GET_RESET_STATS, &stats);
1521
1522 intelScreen->has_context_reset_notification =
1523 (ret != -1 || errno != EINVAL);
1524 }
1525
1526 struct drm_i915_getparam getparam;
1527 getparam.param = I915_PARAM_CMD_PARSER_VERSION;
1528 getparam.value = &intelScreen->cmd_parser_version;
1529 const int ret = drmIoctl(psp->fd, DRM_IOCTL_I915_GETPARAM, &getparam);
1530 if (ret == -1)
1531 intelScreen->cmd_parser_version = 0;
1532
1533 psp->extensions = !intelScreen->has_context_reset_notification
1534 ? intelScreenExtensions : intelRobustScreenExtensions;
1535
1536 intelScreen->compiler = brw_compiler_create(intelScreen,
1537 intelScreen->devinfo);
1538 intelScreen->program_id = 1;
1539
1540 if (intelScreen->devinfo->has_resource_streamer) {
1541 int val = -1;
1542 getparam.param = I915_PARAM_HAS_RESOURCE_STREAMER;
1543 getparam.value = &val;
1544
1545 drmIoctl(psp->fd, DRM_IOCTL_I915_GETPARAM, &getparam);
1546 intelScreen->has_resource_streamer = val > 0;
1547 }
1548
1549 return (const __DRIconfig**) intel_screen_make_configs(psp);
1550 }
1551
1552 struct intel_buffer {
1553 __DRIbuffer base;
1554 drm_intel_bo *bo;
1555 };
1556
1557 static __DRIbuffer *
1558 intelAllocateBuffer(__DRIscreen *screen,
1559 unsigned attachment, unsigned format,
1560 int width, int height)
1561 {
1562 struct intel_buffer *intelBuffer;
1563 struct intel_screen *intelScreen = screen->driverPrivate;
1564
1565 assert(attachment == __DRI_BUFFER_FRONT_LEFT ||
1566 attachment == __DRI_BUFFER_BACK_LEFT);
1567
1568 intelBuffer = calloc(1, sizeof *intelBuffer);
1569 if (intelBuffer == NULL)
1570 return NULL;
1571
1572 /* The front and back buffers are color buffers, which are X tiled. */
1573 uint32_t tiling = I915_TILING_X;
1574 unsigned long pitch;
1575 int cpp = format / 8;
1576 intelBuffer->bo = drm_intel_bo_alloc_tiled(intelScreen->bufmgr,
1577 "intelAllocateBuffer",
1578 width,
1579 height,
1580 cpp,
1581 &tiling, &pitch,
1582 BO_ALLOC_FOR_RENDER);
1583
1584 if (intelBuffer->bo == NULL) {
1585 free(intelBuffer);
1586 return NULL;
1587 }
1588
1589 drm_intel_bo_flink(intelBuffer->bo, &intelBuffer->base.name);
1590
1591 intelBuffer->base.attachment = attachment;
1592 intelBuffer->base.cpp = cpp;
1593 intelBuffer->base.pitch = pitch;
1594
1595 return &intelBuffer->base;
1596 }
1597
1598 static void
1599 intelReleaseBuffer(__DRIscreen *screen, __DRIbuffer *buffer)
1600 {
1601 struct intel_buffer *intelBuffer = (struct intel_buffer *) buffer;
1602
1603 drm_intel_bo_unreference(intelBuffer->bo);
1604 free(intelBuffer);
1605 }
1606
1607 static const struct __DriverAPIRec brw_driver_api = {
1608 .InitScreen = intelInitScreen2,
1609 .DestroyScreen = intelDestroyScreen,
1610 .CreateContext = brwCreateContext,
1611 .DestroyContext = intelDestroyContext,
1612 .CreateBuffer = intelCreateBuffer,
1613 .DestroyBuffer = intelDestroyBuffer,
1614 .MakeCurrent = intelMakeCurrent,
1615 .UnbindContext = intelUnbindContext,
1616 .AllocateBuffer = intelAllocateBuffer,
1617 .ReleaseBuffer = intelReleaseBuffer
1618 };
1619
1620 static const struct __DRIDriverVtableExtensionRec brw_vtable = {
1621 .base = { __DRI_DRIVER_VTABLE, 1 },
1622 .vtable = &brw_driver_api,
1623 };
1624
1625 static const __DRIextension *brw_driver_extensions[] = {
1626 &driCoreExtension.base,
1627 &driImageDriverExtension.base,
1628 &driDRI2Extension.base,
1629 &brw_vtable.base,
1630 &brw_config_options.base,
1631 NULL
1632 };
1633
1634 PUBLIC const __DRIextension **__driDriverGetExtensions_i965(void)
1635 {
1636 globalDriverAPI = &brw_driver_api;
1637
1638 return brw_driver_extensions;
1639 }