i965: Fix duplication of DRI images
[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 <drm_fourcc.h>
27 #include <errno.h>
28 #include <time.h>
29 #include <unistd.h>
30 #include "main/context.h"
31 #include "main/framebuffer.h"
32 #include "main/renderbuffer.h"
33 #include "main/texobj.h"
34 #include "main/hash.h"
35 #include "main/fbobject.h"
36 #include "main/version.h"
37 #include "swrast/s_renderbuffer.h"
38 #include "util/ralloc.h"
39 #include "brw_defines.h"
40 #include "brw_state.h"
41 #include "compiler/nir/nir.h"
42
43 #include "utils.h"
44 #include "util/xmlpool.h"
45
46 static const __DRIconfigOptionsExtension brw_config_options = {
47 .base = { __DRI_CONFIG_OPTIONS, 1 },
48 .xml =
49 DRI_CONF_BEGIN
50 DRI_CONF_SECTION_PERFORMANCE
51 /* Options correspond to DRI_CONF_BO_REUSE_DISABLED,
52 * DRI_CONF_BO_REUSE_ALL
53 */
54 DRI_CONF_OPT_BEGIN_V(bo_reuse, enum, 1, "0:1")
55 DRI_CONF_DESC_BEGIN(en, "Buffer object reuse")
56 DRI_CONF_ENUM(0, "Disable buffer object reuse")
57 DRI_CONF_ENUM(1, "Enable reuse of all sizes of buffer objects")
58 DRI_CONF_DESC_END
59 DRI_CONF_OPT_END
60 DRI_CONF_MESA_NO_ERROR("false")
61 DRI_CONF_SECTION_END
62
63 DRI_CONF_SECTION_QUALITY
64 DRI_CONF_FORCE_S3TC_ENABLE("false")
65
66 DRI_CONF_PRECISE_TRIG("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_FORCE_GLSL_VERSION(0)
81 DRI_CONF_DISABLE_GLSL_LINE_CONTINUATIONS("false")
82 DRI_CONF_DISABLE_BLEND_FUNC_EXTENDED("false")
83 DRI_CONF_DUAL_COLOR_BLEND_BY_LOCATION("false")
84 DRI_CONF_ALLOW_GLSL_EXTENSION_DIRECTIVE_MIDSHADER("false")
85 DRI_CONF_ALLOW_GLSL_BUILTIN_VARIABLE_REDECLARATION("false")
86 DRI_CONF_ALLOW_HIGHER_COMPAT_VERSION("false")
87 DRI_CONF_FORCE_GLSL_ABS_SQRT("false")
88
89 DRI_CONF_OPT_BEGIN_B(shader_precompile, "true")
90 DRI_CONF_DESC(en, "Perform code generation at shader link time.")
91 DRI_CONF_OPT_END
92 DRI_CONF_SECTION_END
93
94 DRI_CONF_SECTION_MISCELLANEOUS
95 DRI_CONF_GLSL_ZERO_INIT("false")
96 DRI_CONF_SECTION_END
97 DRI_CONF_END
98 };
99
100 #include "intel_batchbuffer.h"
101 #include "intel_buffers.h"
102 #include "brw_bufmgr.h"
103 #include "intel_fbo.h"
104 #include "intel_mipmap_tree.h"
105 #include "intel_screen.h"
106 #include "intel_tex.h"
107 #include "intel_image.h"
108
109 #include "brw_context.h"
110
111 #include "i915_drm.h"
112
113 /**
114 * For debugging purposes, this returns a time in seconds.
115 */
116 double
117 get_time(void)
118 {
119 struct timespec tp;
120
121 clock_gettime(CLOCK_MONOTONIC, &tp);
122
123 return tp.tv_sec + tp.tv_nsec / 1000000000.0;
124 }
125
126 static const __DRItexBufferExtension intelTexBufferExtension = {
127 .base = { __DRI_TEX_BUFFER, 3 },
128
129 .setTexBuffer = intelSetTexBuffer,
130 .setTexBuffer2 = intelSetTexBuffer2,
131 .releaseTexBuffer = NULL,
132 };
133
134 static void
135 intel_dri2_flush_with_flags(__DRIcontext *cPriv,
136 __DRIdrawable *dPriv,
137 unsigned flags,
138 enum __DRI2throttleReason reason)
139 {
140 struct brw_context *brw = cPriv->driverPrivate;
141
142 if (!brw)
143 return;
144
145 struct gl_context *ctx = &brw->ctx;
146
147 FLUSH_VERTICES(ctx, 0);
148
149 if (flags & __DRI2_FLUSH_DRAWABLE)
150 intel_resolve_for_dri2_flush(brw, dPriv);
151
152 if (reason == __DRI2_THROTTLE_SWAPBUFFER)
153 brw->need_swap_throttle = true;
154 if (reason == __DRI2_THROTTLE_FLUSHFRONT)
155 brw->need_flush_throttle = true;
156
157 intel_batchbuffer_flush(brw);
158 }
159
160 /**
161 * Provides compatibility with loaders that only support the older (version
162 * 1-3) flush interface.
163 *
164 * That includes libGL up to Mesa 9.0, and the X Server at least up to 1.13.
165 */
166 static void
167 intel_dri2_flush(__DRIdrawable *drawable)
168 {
169 intel_dri2_flush_with_flags(drawable->driContextPriv, drawable,
170 __DRI2_FLUSH_DRAWABLE,
171 __DRI2_THROTTLE_SWAPBUFFER);
172 }
173
174 static const struct __DRI2flushExtensionRec intelFlushExtension = {
175 .base = { __DRI2_FLUSH, 4 },
176
177 .flush = intel_dri2_flush,
178 .invalidate = dri2InvalidateDrawable,
179 .flush_with_flags = intel_dri2_flush_with_flags,
180 };
181
182 static const struct intel_image_format intel_image_formats[] = {
183 { __DRI_IMAGE_FOURCC_ARGB8888, __DRI_IMAGE_COMPONENTS_RGBA, 1,
184 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ARGB8888, 4 } } },
185
186 { __DRI_IMAGE_FOURCC_ABGR8888, __DRI_IMAGE_COMPONENTS_RGBA, 1,
187 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ABGR8888, 4 } } },
188
189 { __DRI_IMAGE_FOURCC_SARGB8888, __DRI_IMAGE_COMPONENTS_RGBA, 1,
190 { { 0, 0, 0, __DRI_IMAGE_FORMAT_SARGB8, 4 } } },
191
192 { __DRI_IMAGE_FOURCC_XRGB8888, __DRI_IMAGE_COMPONENTS_RGB, 1,
193 { { 0, 0, 0, __DRI_IMAGE_FORMAT_XRGB8888, 4 }, } },
194
195 { __DRI_IMAGE_FOURCC_XBGR8888, __DRI_IMAGE_COMPONENTS_RGB, 1,
196 { { 0, 0, 0, __DRI_IMAGE_FORMAT_XBGR8888, 4 }, } },
197
198 { __DRI_IMAGE_FOURCC_ARGB1555, __DRI_IMAGE_COMPONENTS_RGBA, 1,
199 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ARGB1555, 2 } } },
200
201 { __DRI_IMAGE_FOURCC_RGB565, __DRI_IMAGE_COMPONENTS_RGB, 1,
202 { { 0, 0, 0, __DRI_IMAGE_FORMAT_RGB565, 2 } } },
203
204 { __DRI_IMAGE_FOURCC_R8, __DRI_IMAGE_COMPONENTS_R, 1,
205 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 }, } },
206
207 { __DRI_IMAGE_FOURCC_R16, __DRI_IMAGE_COMPONENTS_R, 1,
208 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R16, 1 }, } },
209
210 { __DRI_IMAGE_FOURCC_GR88, __DRI_IMAGE_COMPONENTS_RG, 1,
211 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88, 2 }, } },
212
213 { __DRI_IMAGE_FOURCC_GR1616, __DRI_IMAGE_COMPONENTS_RG, 1,
214 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR1616, 2 }, } },
215
216 { __DRI_IMAGE_FOURCC_YUV410, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
217 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
218 { 1, 2, 2, __DRI_IMAGE_FORMAT_R8, 1 },
219 { 2, 2, 2, __DRI_IMAGE_FORMAT_R8, 1 } } },
220
221 { __DRI_IMAGE_FOURCC_YUV411, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
222 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
223 { 1, 2, 0, __DRI_IMAGE_FORMAT_R8, 1 },
224 { 2, 2, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
225
226 { __DRI_IMAGE_FOURCC_YUV420, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
227 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
228 { 1, 1, 1, __DRI_IMAGE_FORMAT_R8, 1 },
229 { 2, 1, 1, __DRI_IMAGE_FORMAT_R8, 1 } } },
230
231 { __DRI_IMAGE_FOURCC_YUV422, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
232 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
233 { 1, 1, 0, __DRI_IMAGE_FORMAT_R8, 1 },
234 { 2, 1, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
235
236 { __DRI_IMAGE_FOURCC_YUV444, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
237 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
238 { 1, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
239 { 2, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
240
241 { __DRI_IMAGE_FOURCC_YVU410, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
242 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
243 { 2, 2, 2, __DRI_IMAGE_FORMAT_R8, 1 },
244 { 1, 2, 2, __DRI_IMAGE_FORMAT_R8, 1 } } },
245
246 { __DRI_IMAGE_FOURCC_YVU411, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
247 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
248 { 2, 2, 0, __DRI_IMAGE_FORMAT_R8, 1 },
249 { 1, 2, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
250
251 { __DRI_IMAGE_FOURCC_YVU420, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
252 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
253 { 2, 1, 1, __DRI_IMAGE_FORMAT_R8, 1 },
254 { 1, 1, 1, __DRI_IMAGE_FORMAT_R8, 1 } } },
255
256 { __DRI_IMAGE_FOURCC_YVU422, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
257 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
258 { 2, 1, 0, __DRI_IMAGE_FORMAT_R8, 1 },
259 { 1, 1, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
260
261 { __DRI_IMAGE_FOURCC_YVU444, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
262 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
263 { 2, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
264 { 1, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
265
266 { __DRI_IMAGE_FOURCC_NV12, __DRI_IMAGE_COMPONENTS_Y_UV, 2,
267 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
268 { 1, 1, 1, __DRI_IMAGE_FORMAT_GR88, 2 } } },
269
270 { __DRI_IMAGE_FOURCC_NV16, __DRI_IMAGE_COMPONENTS_Y_UV, 2,
271 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
272 { 1, 1, 0, __DRI_IMAGE_FORMAT_GR88, 2 } } },
273
274 /* For YUYV and UYVY buffers, we set up two overlapping DRI images
275 * and treat them as planar buffers in the compositors.
276 * Plane 0 is GR88 and samples YU or YV pairs and places Y into
277 * the R component, while plane 1 is ARGB/ABGR and samples YUYV/UYVY
278 * clusters and places pairs and places U into the G component and
279 * V into A. This lets the texture sampler interpolate the Y
280 * components correctly when sampling from plane 0, and interpolate
281 * U and V correctly when sampling from plane 1. */
282 { __DRI_IMAGE_FOURCC_YUYV, __DRI_IMAGE_COMPONENTS_Y_XUXV, 2,
283 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88, 2 },
284 { 0, 1, 0, __DRI_IMAGE_FORMAT_ARGB8888, 4 } } },
285 { __DRI_IMAGE_FOURCC_UYVY, __DRI_IMAGE_COMPONENTS_Y_UXVX, 2,
286 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88, 2 },
287 { 0, 1, 0, __DRI_IMAGE_FORMAT_ABGR8888, 4 } } }
288 };
289
290 static const struct {
291 uint64_t modifier;
292 unsigned since_gen;
293 } supported_modifiers[] = {
294 { .modifier = DRM_FORMAT_MOD_LINEAR , .since_gen = 1 },
295 { .modifier = I915_FORMAT_MOD_X_TILED , .since_gen = 1 },
296 { .modifier = I915_FORMAT_MOD_Y_TILED , .since_gen = 6 },
297 { .modifier = I915_FORMAT_MOD_Y_TILED_CCS , .since_gen = 9 },
298 };
299
300 static bool
301 modifier_is_supported(const struct gen_device_info *devinfo,
302 const struct intel_image_format *fmt, int dri_format,
303 uint64_t modifier)
304 {
305 const struct isl_drm_modifier_info *modinfo =
306 isl_drm_modifier_get_info(modifier);
307 int i;
308
309 /* ISL had better know about the modifier */
310 if (!modinfo)
311 return false;
312
313 if (modinfo->aux_usage == ISL_AUX_USAGE_CCS_E) {
314 /* If INTEL_DEBUG=norbc is set, don't support any CCS_E modifiers */
315 if (unlikely(INTEL_DEBUG & DEBUG_NO_RBC))
316 return false;
317
318 /* CCS_E is not supported for planar images */
319 if (fmt && fmt->nplanes > 1)
320 return false;
321
322 if (fmt) {
323 assert(dri_format == 0);
324 dri_format = fmt->planes[0].dri_format;
325 }
326
327 mesa_format format = driImageFormatToGLFormat(dri_format);
328 format = _mesa_get_srgb_format_linear(format);
329 if (!isl_format_supports_ccs_e(devinfo,
330 brw_isl_format_for_mesa_format(format)))
331 return false;
332 }
333
334 for (i = 0; i < ARRAY_SIZE(supported_modifiers); i++) {
335 if (supported_modifiers[i].modifier != modifier)
336 continue;
337
338 return supported_modifiers[i].since_gen <= devinfo->gen;
339 }
340
341 return false;
342 }
343
344 static uint64_t
345 tiling_to_modifier(uint32_t tiling)
346 {
347 static const uint64_t map[] = {
348 [I915_TILING_NONE] = DRM_FORMAT_MOD_LINEAR,
349 [I915_TILING_X] = I915_FORMAT_MOD_X_TILED,
350 [I915_TILING_Y] = I915_FORMAT_MOD_Y_TILED,
351 };
352
353 assert(tiling < ARRAY_SIZE(map));
354
355 return map[tiling];
356 }
357
358 static void
359 intel_image_warn_if_unaligned(__DRIimage *image, const char *func)
360 {
361 uint32_t tiling, swizzle;
362 brw_bo_get_tiling(image->bo, &tiling, &swizzle);
363
364 if (tiling != I915_TILING_NONE && (image->offset & 0xfff)) {
365 _mesa_warning(NULL, "%s: offset 0x%08x not on tile boundary",
366 func, image->offset);
367 }
368 }
369
370 static const struct intel_image_format *
371 intel_image_format_lookup(int fourcc)
372 {
373 for (unsigned i = 0; i < ARRAY_SIZE(intel_image_formats); i++) {
374 if (intel_image_formats[i].fourcc == fourcc)
375 return &intel_image_formats[i];
376 }
377
378 return NULL;
379 }
380
381 static boolean intel_lookup_fourcc(int dri_format, int *fourcc)
382 {
383 for (unsigned i = 0; i < ARRAY_SIZE(intel_image_formats); i++) {
384 if (intel_image_formats[i].planes[0].dri_format == dri_format) {
385 *fourcc = intel_image_formats[i].fourcc;
386 return true;
387 }
388 }
389 return false;
390 }
391
392 static __DRIimage *
393 intel_allocate_image(struct intel_screen *screen, int dri_format,
394 void *loaderPrivate)
395 {
396 __DRIimage *image;
397
398 image = calloc(1, sizeof *image);
399 if (image == NULL)
400 return NULL;
401
402 image->screen = screen;
403 image->dri_format = dri_format;
404 image->offset = 0;
405
406 image->format = driImageFormatToGLFormat(dri_format);
407 if (dri_format != __DRI_IMAGE_FORMAT_NONE &&
408 image->format == MESA_FORMAT_NONE) {
409 free(image);
410 return NULL;
411 }
412
413 image->internal_format = _mesa_get_format_base_format(image->format);
414 image->data = loaderPrivate;
415
416 return image;
417 }
418
419 /**
420 * Sets up a DRIImage structure to point to a slice out of a miptree.
421 */
422 static void
423 intel_setup_image_from_mipmap_tree(struct brw_context *brw, __DRIimage *image,
424 struct intel_mipmap_tree *mt, GLuint level,
425 GLuint zoffset)
426 {
427 intel_miptree_make_shareable(brw, mt);
428
429 intel_miptree_check_level_layer(mt, level, zoffset);
430
431 image->width = minify(mt->surf.phys_level0_sa.width,
432 level - mt->first_level);
433 image->height = minify(mt->surf.phys_level0_sa.height,
434 level - mt->first_level);
435 image->pitch = mt->surf.row_pitch;
436
437 image->offset = intel_miptree_get_tile_offsets(mt, level, zoffset,
438 &image->tile_x,
439 &image->tile_y);
440
441 brw_bo_unreference(image->bo);
442 image->bo = mt->bo;
443 brw_bo_reference(mt->bo);
444 }
445
446 static __DRIimage *
447 intel_create_image_from_name(__DRIscreen *dri_screen,
448 int width, int height, int format,
449 int name, int pitch, void *loaderPrivate)
450 {
451 struct intel_screen *screen = dri_screen->driverPrivate;
452 __DRIimage *image;
453 int cpp;
454
455 image = intel_allocate_image(screen, format, loaderPrivate);
456 if (image == NULL)
457 return NULL;
458
459 if (image->format == MESA_FORMAT_NONE)
460 cpp = 1;
461 else
462 cpp = _mesa_get_format_bytes(image->format);
463
464 image->width = width;
465 image->height = height;
466 image->pitch = pitch * cpp;
467 image->bo = brw_bo_gem_create_from_name(screen->bufmgr, "image",
468 name);
469 if (!image->bo) {
470 free(image);
471 return NULL;
472 }
473 image->modifier = tiling_to_modifier(image->bo->tiling_mode);
474
475 return image;
476 }
477
478 static __DRIimage *
479 intel_create_image_from_renderbuffer(__DRIcontext *context,
480 int renderbuffer, void *loaderPrivate)
481 {
482 __DRIimage *image;
483 struct brw_context *brw = context->driverPrivate;
484 struct gl_context *ctx = &brw->ctx;
485 struct gl_renderbuffer *rb;
486 struct intel_renderbuffer *irb;
487
488 rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
489 if (!rb) {
490 _mesa_error(ctx, GL_INVALID_OPERATION, "glRenderbufferExternalMESA");
491 return NULL;
492 }
493
494 irb = intel_renderbuffer(rb);
495 intel_miptree_make_shareable(brw, irb->mt);
496 image = calloc(1, sizeof *image);
497 if (image == NULL)
498 return NULL;
499
500 image->internal_format = rb->InternalFormat;
501 image->format = rb->Format;
502 image->modifier = tiling_to_modifier(
503 isl_tiling_to_i915_tiling(irb->mt->surf.tiling));
504 image->offset = 0;
505 image->data = loaderPrivate;
506 brw_bo_unreference(image->bo);
507 image->bo = irb->mt->bo;
508 brw_bo_reference(irb->mt->bo);
509 image->width = rb->Width;
510 image->height = rb->Height;
511 image->pitch = irb->mt->surf.row_pitch;
512 image->dri_format = driGLFormatToImageFormat(image->format);
513 image->has_depthstencil = irb->mt->stencil_mt? true : false;
514
515 rb->NeedsFinishRenderTexture = true;
516 return image;
517 }
518
519 static __DRIimage *
520 intel_create_image_from_texture(__DRIcontext *context, int target,
521 unsigned texture, int zoffset,
522 int level,
523 unsigned *error,
524 void *loaderPrivate)
525 {
526 __DRIimage *image;
527 struct brw_context *brw = context->driverPrivate;
528 struct gl_texture_object *obj;
529 struct intel_texture_object *iobj;
530 GLuint face = 0;
531
532 obj = _mesa_lookup_texture(&brw->ctx, texture);
533 if (!obj || obj->Target != target) {
534 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
535 return NULL;
536 }
537
538 if (target == GL_TEXTURE_CUBE_MAP)
539 face = zoffset;
540
541 _mesa_test_texobj_completeness(&brw->ctx, obj);
542 iobj = intel_texture_object(obj);
543 if (!obj->_BaseComplete || (level > 0 && !obj->_MipmapComplete)) {
544 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
545 return NULL;
546 }
547
548 if (level < obj->BaseLevel || level > obj->_MaxLevel) {
549 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
550 return NULL;
551 }
552
553 if (target == GL_TEXTURE_3D && obj->Image[face][level]->Depth < zoffset) {
554 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
555 return NULL;
556 }
557 image = calloc(1, sizeof *image);
558 if (image == NULL) {
559 *error = __DRI_IMAGE_ERROR_BAD_ALLOC;
560 return NULL;
561 }
562
563 image->internal_format = obj->Image[face][level]->InternalFormat;
564 image->format = obj->Image[face][level]->TexFormat;
565 image->modifier = tiling_to_modifier(
566 isl_tiling_to_i915_tiling(iobj->mt->surf.tiling));
567 image->data = loaderPrivate;
568 intel_setup_image_from_mipmap_tree(brw, image, iobj->mt, level, zoffset);
569 image->dri_format = driGLFormatToImageFormat(image->format);
570 image->has_depthstencil = iobj->mt->stencil_mt? true : false;
571 if (image->dri_format == MESA_FORMAT_NONE) {
572 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
573 free(image);
574 return NULL;
575 }
576
577 *error = __DRI_IMAGE_ERROR_SUCCESS;
578 return image;
579 }
580
581 static void
582 intel_destroy_image(__DRIimage *image)
583 {
584 brw_bo_unreference(image->bo);
585 free(image);
586 }
587
588 enum modifier_priority {
589 MODIFIER_PRIORITY_INVALID = 0,
590 MODIFIER_PRIORITY_LINEAR,
591 MODIFIER_PRIORITY_X,
592 MODIFIER_PRIORITY_Y,
593 MODIFIER_PRIORITY_Y_CCS,
594 };
595
596 const uint64_t priority_to_modifier[] = {
597 [MODIFIER_PRIORITY_INVALID] = DRM_FORMAT_MOD_INVALID,
598 [MODIFIER_PRIORITY_LINEAR] = DRM_FORMAT_MOD_LINEAR,
599 [MODIFIER_PRIORITY_X] = I915_FORMAT_MOD_X_TILED,
600 [MODIFIER_PRIORITY_Y] = I915_FORMAT_MOD_Y_TILED,
601 [MODIFIER_PRIORITY_Y_CCS] = I915_FORMAT_MOD_Y_TILED_CCS,
602 };
603
604 static uint64_t
605 select_best_modifier(struct gen_device_info *devinfo,
606 int dri_format,
607 const uint64_t *modifiers,
608 const unsigned count)
609 {
610 enum modifier_priority prio = MODIFIER_PRIORITY_INVALID;
611
612 for (int i = 0; i < count; i++) {
613 if (!modifier_is_supported(devinfo, NULL, dri_format, modifiers[i]))
614 continue;
615
616 switch (modifiers[i]) {
617 case I915_FORMAT_MOD_Y_TILED_CCS:
618 prio = MAX2(prio, MODIFIER_PRIORITY_Y_CCS);
619 break;
620 case I915_FORMAT_MOD_Y_TILED:
621 prio = MAX2(prio, MODIFIER_PRIORITY_Y);
622 break;
623 case I915_FORMAT_MOD_X_TILED:
624 prio = MAX2(prio, MODIFIER_PRIORITY_X);
625 break;
626 case DRM_FORMAT_MOD_LINEAR:
627 prio = MAX2(prio, MODIFIER_PRIORITY_LINEAR);
628 break;
629 case DRM_FORMAT_MOD_INVALID:
630 default:
631 break;
632 }
633 }
634
635 return priority_to_modifier[prio];
636 }
637
638 static __DRIimage *
639 intel_create_image_common(__DRIscreen *dri_screen,
640 int width, int height, int format,
641 unsigned int use,
642 const uint64_t *modifiers,
643 unsigned count,
644 void *loaderPrivate)
645 {
646 __DRIimage *image;
647 struct intel_screen *screen = dri_screen->driverPrivate;
648 uint64_t modifier = DRM_FORMAT_MOD_INVALID;
649 bool ok;
650
651 /* Callers of this may specify a modifier, or a dri usage, but not both. The
652 * newer modifier interface deprecates the older usage flags newer modifier
653 * interface deprecates the older usage flags.
654 */
655 assert(!(use && count));
656
657 if (use & __DRI_IMAGE_USE_CURSOR) {
658 if (width != 64 || height != 64)
659 return NULL;
660 modifier = DRM_FORMAT_MOD_LINEAR;
661 }
662
663 if (use & __DRI_IMAGE_USE_LINEAR)
664 modifier = DRM_FORMAT_MOD_LINEAR;
665
666 if (modifier == DRM_FORMAT_MOD_INVALID) {
667 if (modifiers) {
668 /* User requested specific modifiers */
669 modifier = select_best_modifier(&screen->devinfo, format,
670 modifiers, count);
671 if (modifier == DRM_FORMAT_MOD_INVALID)
672 return NULL;
673 } else {
674 /* Historically, X-tiled was the default, and so lack of modifier means
675 * X-tiled.
676 */
677 modifier = I915_FORMAT_MOD_X_TILED;
678 }
679 }
680
681 image = intel_allocate_image(screen, format, loaderPrivate);
682 if (image == NULL)
683 return NULL;
684
685 const struct isl_drm_modifier_info *mod_info =
686 isl_drm_modifier_get_info(modifier);
687
688 struct isl_surf surf;
689 ok = isl_surf_init(&screen->isl_dev, &surf,
690 .dim = ISL_SURF_DIM_2D,
691 .format = brw_isl_format_for_mesa_format(image->format),
692 .width = width,
693 .height = height,
694 .depth = 1,
695 .levels = 1,
696 .array_len = 1,
697 .samples = 1,
698 .usage = ISL_SURF_USAGE_RENDER_TARGET_BIT |
699 ISL_SURF_USAGE_TEXTURE_BIT |
700 ISL_SURF_USAGE_STORAGE_BIT,
701 .tiling_flags = (1 << mod_info->tiling));
702 assert(ok);
703 if (!ok) {
704 free(image);
705 return NULL;
706 }
707
708 struct isl_surf aux_surf;
709 if (mod_info->aux_usage == ISL_AUX_USAGE_CCS_E) {
710 ok = isl_surf_get_ccs_surf(&screen->isl_dev, &surf, &aux_surf, 0);
711 if (!ok) {
712 free(image);
713 return NULL;
714 }
715 } else {
716 assert(mod_info->aux_usage == ISL_AUX_USAGE_NONE);
717 aux_surf.size = 0;
718 }
719
720 /* We request that the bufmgr zero the buffer for us for two reasons:
721 *
722 * 1) If a buffer gets re-used from the pool, we don't want to leak random
723 * garbage from our process to some other.
724 *
725 * 2) For images with CCS_E, we want to ensure that the CCS starts off in
726 * a valid state. A CCS value of 0 indicates that the given block is
727 * in the pass-through state which is what we want.
728 */
729 image->bo = brw_bo_alloc_tiled(screen->bufmgr, "image",
730 surf.size + aux_surf.size,
731 isl_tiling_to_i915_tiling(mod_info->tiling),
732 surf.row_pitch, BO_ALLOC_ZEROED);
733 if (image->bo == NULL) {
734 free(image);
735 return NULL;
736 }
737 image->width = width;
738 image->height = height;
739 image->pitch = surf.row_pitch;
740 image->modifier = modifier;
741
742 if (aux_surf.size) {
743 image->aux_offset = surf.size;
744 image->aux_pitch = aux_surf.row_pitch;
745 }
746
747 return image;
748 }
749
750 static __DRIimage *
751 intel_create_image(__DRIscreen *dri_screen,
752 int width, int height, int format,
753 unsigned int use,
754 void *loaderPrivate)
755 {
756 return intel_create_image_common(dri_screen, width, height, format, use, NULL, 0,
757 loaderPrivate);
758 }
759
760 static __DRIimage *
761 intel_create_image_with_modifiers(__DRIscreen *dri_screen,
762 int width, int height, int format,
763 const uint64_t *modifiers,
764 const unsigned count,
765 void *loaderPrivate)
766 {
767 return intel_create_image_common(dri_screen, width, height, format, 0,
768 modifiers, count, loaderPrivate);
769 }
770
771 static GLboolean
772 intel_query_image(__DRIimage *image, int attrib, int *value)
773 {
774 switch (attrib) {
775 case __DRI_IMAGE_ATTRIB_STRIDE:
776 *value = image->pitch;
777 return true;
778 case __DRI_IMAGE_ATTRIB_HANDLE:
779 *value = image->bo->gem_handle;
780 return true;
781 case __DRI_IMAGE_ATTRIB_NAME:
782 return !brw_bo_flink(image->bo, (uint32_t *) value);
783 case __DRI_IMAGE_ATTRIB_FORMAT:
784 *value = image->dri_format;
785 return true;
786 case __DRI_IMAGE_ATTRIB_WIDTH:
787 *value = image->width;
788 return true;
789 case __DRI_IMAGE_ATTRIB_HEIGHT:
790 *value = image->height;
791 return true;
792 case __DRI_IMAGE_ATTRIB_COMPONENTS:
793 if (image->planar_format == NULL)
794 return false;
795 *value = image->planar_format->components;
796 return true;
797 case __DRI_IMAGE_ATTRIB_FD:
798 return !brw_bo_gem_export_to_prime(image->bo, value);
799 case __DRI_IMAGE_ATTRIB_FOURCC:
800 return intel_lookup_fourcc(image->dri_format, value);
801 case __DRI_IMAGE_ATTRIB_NUM_PLANES:
802 if (isl_drm_modifier_has_aux(image->modifier)) {
803 assert(!image->planar_format || image->planar_format->nplanes == 1);
804 *value = 2;
805 } else if (image->planar_format) {
806 *value = image->planar_format->nplanes;
807 } else {
808 *value = 1;
809 }
810 return true;
811 case __DRI_IMAGE_ATTRIB_OFFSET:
812 *value = image->offset;
813 return true;
814 case __DRI_IMAGE_ATTRIB_MODIFIER_LOWER:
815 *value = (image->modifier & 0xffffffff);
816 return true;
817 case __DRI_IMAGE_ATTRIB_MODIFIER_UPPER:
818 *value = ((image->modifier >> 32) & 0xffffffff);
819 return true;
820
821 default:
822 return false;
823 }
824 }
825
826 static GLboolean
827 intel_query_format_modifier_attribs(__DRIscreen *dri_screen,
828 uint32_t fourcc, uint64_t modifier,
829 int attrib, uint64_t *value)
830 {
831 struct intel_screen *screen = dri_screen->driverPrivate;
832 const struct intel_image_format *f = intel_image_format_lookup(fourcc);
833
834 if (!modifier_is_supported(&screen->devinfo, f, 0, modifier))
835 return false;
836
837 switch (attrib) {
838 case __DRI_IMAGE_FORMAT_MODIFIER_ATTRIB_PLANE_COUNT:
839 *value = isl_drm_modifier_has_aux(modifier) ? 2 : f->nplanes;
840 return true;
841
842 default:
843 return false;
844 }
845 }
846
847 static __DRIimage *
848 intel_dup_image(__DRIimage *orig_image, void *loaderPrivate)
849 {
850 __DRIimage *image;
851
852 image = calloc(1, sizeof *image);
853 if (image == NULL)
854 return NULL;
855
856 brw_bo_reference(orig_image->bo);
857 image->bo = orig_image->bo;
858 image->internal_format = orig_image->internal_format;
859 image->planar_format = orig_image->planar_format;
860 image->dri_format = orig_image->dri_format;
861 image->format = orig_image->format;
862 image->modifier = orig_image->modifier;
863 image->offset = orig_image->offset;
864 image->width = orig_image->width;
865 image->height = orig_image->height;
866 image->pitch = orig_image->pitch;
867 image->tile_x = orig_image->tile_x;
868 image->tile_y = orig_image->tile_y;
869 image->has_depthstencil = orig_image->has_depthstencil;
870 image->data = loaderPrivate;
871 image->dma_buf_imported = orig_image->dma_buf_imported;
872 image->aux_offset = orig_image->aux_offset;
873 image->aux_pitch = orig_image->aux_pitch;
874
875 memcpy(image->strides, orig_image->strides, sizeof(image->strides));
876 memcpy(image->offsets, orig_image->offsets, sizeof(image->offsets));
877
878 return image;
879 }
880
881 static GLboolean
882 intel_validate_usage(__DRIimage *image, unsigned int use)
883 {
884 if (use & __DRI_IMAGE_USE_CURSOR) {
885 if (image->width != 64 || image->height != 64)
886 return GL_FALSE;
887 }
888
889 return GL_TRUE;
890 }
891
892 static __DRIimage *
893 intel_create_image_from_names(__DRIscreen *dri_screen,
894 int width, int height, int fourcc,
895 int *names, int num_names,
896 int *strides, int *offsets,
897 void *loaderPrivate)
898 {
899 const struct intel_image_format *f = NULL;
900 __DRIimage *image;
901 int i, index;
902
903 if (dri_screen == NULL || names == NULL || num_names != 1)
904 return NULL;
905
906 f = intel_image_format_lookup(fourcc);
907 if (f == NULL)
908 return NULL;
909
910 image = intel_create_image_from_name(dri_screen, width, height,
911 __DRI_IMAGE_FORMAT_NONE,
912 names[0], strides[0],
913 loaderPrivate);
914
915 if (image == NULL)
916 return NULL;
917
918 image->planar_format = f;
919 for (i = 0; i < f->nplanes; i++) {
920 index = f->planes[i].buffer_index;
921 image->offsets[index] = offsets[index];
922 image->strides[index] = strides[index];
923 }
924
925 return image;
926 }
927
928 static __DRIimage *
929 intel_create_image_from_fds_common(__DRIscreen *dri_screen,
930 int width, int height, int fourcc,
931 uint64_t modifier, int *fds, int num_fds,
932 int *strides, int *offsets,
933 void *loaderPrivate)
934 {
935 struct intel_screen *screen = dri_screen->driverPrivate;
936 const struct intel_image_format *f;
937 __DRIimage *image;
938 int i, index;
939 bool ok;
940
941 if (fds == NULL || num_fds < 1)
942 return NULL;
943
944 f = intel_image_format_lookup(fourcc);
945 if (f == NULL)
946 return NULL;
947
948 if (modifier != DRM_FORMAT_MOD_INVALID &&
949 !modifier_is_supported(&screen->devinfo, f, 0, modifier))
950 return NULL;
951
952 if (f->nplanes == 1)
953 image = intel_allocate_image(screen, f->planes[0].dri_format,
954 loaderPrivate);
955 else
956 image = intel_allocate_image(screen, __DRI_IMAGE_FORMAT_NONE,
957 loaderPrivate);
958
959 if (image == NULL)
960 return NULL;
961
962 image->width = width;
963 image->height = height;
964 image->pitch = strides[0];
965
966 image->planar_format = f;
967
968 image->bo = brw_bo_gem_create_from_prime(screen->bufmgr, fds[0]);
969 if (image->bo == NULL) {
970 free(image);
971 return NULL;
972 }
973
974 /* We only support all planes from the same bo.
975 * brw_bo_gem_create_from_prime() should return the same pointer for all
976 * fds received here */
977 for (i = 1; i < num_fds; i++) {
978 struct brw_bo *aux = brw_bo_gem_create_from_prime(screen->bufmgr, fds[i]);
979 brw_bo_unreference(aux);
980 if (aux != image->bo) {
981 brw_bo_unreference(image->bo);
982 free(image);
983 return NULL;
984 }
985 }
986
987 if (modifier != DRM_FORMAT_MOD_INVALID)
988 image->modifier = modifier;
989 else
990 image->modifier = tiling_to_modifier(image->bo->tiling_mode);
991
992 const struct isl_drm_modifier_info *mod_info =
993 isl_drm_modifier_get_info(image->modifier);
994
995 int size = 0;
996 struct isl_surf surf;
997 for (i = 0; i < f->nplanes; i++) {
998 index = f->planes[i].buffer_index;
999 image->offsets[index] = offsets[index];
1000 image->strides[index] = strides[index];
1001
1002 mesa_format format = driImageFormatToGLFormat(f->planes[i].dri_format);
1003
1004 ok = isl_surf_init(&screen->isl_dev, &surf,
1005 .dim = ISL_SURF_DIM_2D,
1006 .format = brw_isl_format_for_mesa_format(format),
1007 .width = image->width >> f->planes[i].width_shift,
1008 .height = image->height >> f->planes[i].height_shift,
1009 .depth = 1,
1010 .levels = 1,
1011 .array_len = 1,
1012 .samples = 1,
1013 .row_pitch = strides[index],
1014 .usage = ISL_SURF_USAGE_RENDER_TARGET_BIT |
1015 ISL_SURF_USAGE_TEXTURE_BIT |
1016 ISL_SURF_USAGE_STORAGE_BIT,
1017 .tiling_flags = (1 << mod_info->tiling));
1018 if (!ok) {
1019 brw_bo_unreference(image->bo);
1020 free(image);
1021 return NULL;
1022 }
1023
1024 const int end = offsets[index] + surf.size;
1025 if (size < end)
1026 size = end;
1027 }
1028
1029 if (mod_info->aux_usage == ISL_AUX_USAGE_CCS_E) {
1030 /* Even though we initialize surf in the loop above, we know that
1031 * anything with CCS_E will have exactly one plane so surf is properly
1032 * initialized when we get here.
1033 */
1034 assert(f->nplanes == 1);
1035
1036 image->aux_offset = offsets[1];
1037 image->aux_pitch = strides[1];
1038
1039 /* Scanout hardware requires that the CCS be placed after the main
1040 * surface in memory. We consider any CCS that is placed any earlier in
1041 * memory to be invalid and reject it.
1042 *
1043 * At some point in the future, this restriction may be relaxed if the
1044 * hardware becomes less strict but we may need a new modifier for that.
1045 */
1046 assert(size > 0);
1047 if (image->aux_offset < size) {
1048 brw_bo_unreference(image->bo);
1049 free(image);
1050 return NULL;
1051 }
1052
1053 struct isl_surf aux_surf;
1054 ok = isl_surf_get_ccs_surf(&screen->isl_dev, &surf, &aux_surf,
1055 image->aux_pitch);
1056 if (!ok) {
1057 brw_bo_unreference(image->bo);
1058 free(image);
1059 return NULL;
1060 }
1061
1062 const int end = image->aux_offset + aux_surf.size;
1063 if (size < end)
1064 size = end;
1065 } else {
1066 assert(mod_info->aux_usage == ISL_AUX_USAGE_NONE);
1067 }
1068
1069 /* Check that the requested image actually fits within the BO. 'size'
1070 * is already relative to the offsets, so we don't need to add that. */
1071 if (image->bo->size == 0) {
1072 image->bo->size = size;
1073 } else if (size > image->bo->size) {
1074 brw_bo_unreference(image->bo);
1075 free(image);
1076 return NULL;
1077 }
1078
1079 if (f->nplanes == 1) {
1080 image->offset = image->offsets[0];
1081 intel_image_warn_if_unaligned(image, __func__);
1082 }
1083
1084 return image;
1085 }
1086
1087 static __DRIimage *
1088 intel_create_image_from_fds(__DRIscreen *dri_screen,
1089 int width, int height, int fourcc,
1090 int *fds, int num_fds, int *strides, int *offsets,
1091 void *loaderPrivate)
1092 {
1093 return intel_create_image_from_fds_common(dri_screen, width, height, fourcc,
1094 DRM_FORMAT_MOD_INVALID,
1095 fds, num_fds, strides, offsets,
1096 loaderPrivate);
1097 }
1098
1099 static __DRIimage *
1100 intel_create_image_from_dma_bufs2(__DRIscreen *dri_screen,
1101 int width, int height,
1102 int fourcc, uint64_t modifier,
1103 int *fds, int num_fds,
1104 int *strides, int *offsets,
1105 enum __DRIYUVColorSpace yuv_color_space,
1106 enum __DRISampleRange sample_range,
1107 enum __DRIChromaSiting horizontal_siting,
1108 enum __DRIChromaSiting vertical_siting,
1109 unsigned *error,
1110 void *loaderPrivate)
1111 {
1112 __DRIimage *image;
1113 const struct intel_image_format *f = intel_image_format_lookup(fourcc);
1114
1115 if (!f) {
1116 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
1117 return NULL;
1118 }
1119
1120 image = intel_create_image_from_fds_common(dri_screen, width, height,
1121 fourcc, modifier,
1122 fds, num_fds, strides, offsets,
1123 loaderPrivate);
1124
1125 /*
1126 * Invalid parameters and any inconsistencies between are assumed to be
1127 * checked by the caller. Therefore besides unsupported formats one can fail
1128 * only in allocation.
1129 */
1130 if (!image) {
1131 *error = __DRI_IMAGE_ERROR_BAD_ALLOC;
1132 return NULL;
1133 }
1134
1135 image->dma_buf_imported = true;
1136 image->yuv_color_space = yuv_color_space;
1137 image->sample_range = sample_range;
1138 image->horizontal_siting = horizontal_siting;
1139 image->vertical_siting = vertical_siting;
1140
1141 *error = __DRI_IMAGE_ERROR_SUCCESS;
1142 return image;
1143 }
1144
1145 static __DRIimage *
1146 intel_create_image_from_dma_bufs(__DRIscreen *dri_screen,
1147 int width, int height, int fourcc,
1148 int *fds, int num_fds,
1149 int *strides, int *offsets,
1150 enum __DRIYUVColorSpace yuv_color_space,
1151 enum __DRISampleRange sample_range,
1152 enum __DRIChromaSiting horizontal_siting,
1153 enum __DRIChromaSiting vertical_siting,
1154 unsigned *error,
1155 void *loaderPrivate)
1156 {
1157 return intel_create_image_from_dma_bufs2(dri_screen, width, height,
1158 fourcc, DRM_FORMAT_MOD_INVALID,
1159 fds, num_fds, strides, offsets,
1160 yuv_color_space,
1161 sample_range,
1162 horizontal_siting,
1163 vertical_siting,
1164 error,
1165 loaderPrivate);
1166 }
1167
1168 static GLboolean
1169 intel_query_dma_buf_formats(__DRIscreen *screen, int max,
1170 int *formats, int *count)
1171 {
1172 int i, j = 0;
1173
1174 if (max == 0) {
1175 *count = ARRAY_SIZE(intel_image_formats) - 1; /* not SARGB */
1176 return true;
1177 }
1178
1179 for (i = 0; i < (ARRAY_SIZE(intel_image_formats)) && j < max; i++) {
1180 if (intel_image_formats[i].fourcc == __DRI_IMAGE_FOURCC_SARGB8888)
1181 continue;
1182 formats[j++] = intel_image_formats[i].fourcc;
1183 }
1184
1185 *count = j;
1186 return true;
1187 }
1188
1189 static GLboolean
1190 intel_query_dma_buf_modifiers(__DRIscreen *_screen, int fourcc, int max,
1191 uint64_t *modifiers,
1192 unsigned int *external_only,
1193 int *count)
1194 {
1195 struct intel_screen *screen = _screen->driverPrivate;
1196 const struct intel_image_format *f;
1197 int num_mods = 0, i;
1198
1199 f = intel_image_format_lookup(fourcc);
1200 if (f == NULL)
1201 return false;
1202
1203 for (i = 0; i < ARRAY_SIZE(supported_modifiers); i++) {
1204 uint64_t modifier = supported_modifiers[i].modifier;
1205 if (!modifier_is_supported(&screen->devinfo, f, 0, modifier))
1206 continue;
1207
1208 num_mods++;
1209 if (max == 0)
1210 continue;
1211
1212 modifiers[num_mods - 1] = modifier;
1213 if (num_mods >= max)
1214 break;
1215 }
1216
1217 if (external_only != NULL) {
1218 for (i = 0; i < num_mods && i < max; i++) {
1219 if (f->components == __DRI_IMAGE_COMPONENTS_Y_U_V ||
1220 f->components == __DRI_IMAGE_COMPONENTS_Y_UV ||
1221 f->components == __DRI_IMAGE_COMPONENTS_Y_XUXV) {
1222 external_only[i] = GL_TRUE;
1223 }
1224 else {
1225 external_only[i] = GL_FALSE;
1226 }
1227 }
1228 }
1229
1230 *count = num_mods;
1231 return true;
1232 }
1233
1234 static __DRIimage *
1235 intel_from_planar(__DRIimage *parent, int plane, void *loaderPrivate)
1236 {
1237 int width, height, offset, stride, dri_format, index;
1238 const struct intel_image_format *f;
1239 __DRIimage *image;
1240
1241 if (parent == NULL) {
1242 return NULL;
1243 } else if (parent->planar_format == NULL) {
1244 const bool is_aux =
1245 isl_drm_modifier_has_aux(parent->modifier) && plane == 1;
1246 if (!is_aux)
1247 return NULL;
1248
1249 width = parent->width;
1250 height = parent->height;
1251 dri_format = parent->dri_format;
1252 offset = parent->aux_offset;
1253 stride = parent->aux_pitch;
1254 } else {
1255 /* Planar formats don't support aux buffers/images */
1256 assert(!isl_drm_modifier_has_aux(parent->modifier));
1257 f = parent->planar_format;
1258
1259 if (plane >= f->nplanes)
1260 return NULL;
1261
1262 width = parent->width >> f->planes[plane].width_shift;
1263 height = parent->height >> f->planes[plane].height_shift;
1264 dri_format = f->planes[plane].dri_format;
1265 index = f->planes[plane].buffer_index;
1266 offset = parent->offsets[index];
1267 stride = parent->strides[index];
1268
1269 if (offset + height * stride > parent->bo->size) {
1270 _mesa_warning(NULL, "intel_create_sub_image: subimage out of bounds");
1271 return NULL;
1272 }
1273 }
1274
1275 image = intel_allocate_image(parent->screen, dri_format, loaderPrivate);
1276 if (image == NULL)
1277 return NULL;
1278
1279 image->bo = parent->bo;
1280 brw_bo_reference(parent->bo);
1281 image->modifier = parent->modifier;
1282
1283 image->width = width;
1284 image->height = height;
1285 image->pitch = stride;
1286 image->offset = offset;
1287
1288 intel_image_warn_if_unaligned(image, __func__);
1289
1290 return image;
1291 }
1292
1293 static const __DRIimageExtension intelImageExtension = {
1294 .base = { __DRI_IMAGE, 16 },
1295
1296 .createImageFromName = intel_create_image_from_name,
1297 .createImageFromRenderbuffer = intel_create_image_from_renderbuffer,
1298 .destroyImage = intel_destroy_image,
1299 .createImage = intel_create_image,
1300 .queryImage = intel_query_image,
1301 .dupImage = intel_dup_image,
1302 .validateUsage = intel_validate_usage,
1303 .createImageFromNames = intel_create_image_from_names,
1304 .fromPlanar = intel_from_planar,
1305 .createImageFromTexture = intel_create_image_from_texture,
1306 .createImageFromFds = intel_create_image_from_fds,
1307 .createImageFromDmaBufs = intel_create_image_from_dma_bufs,
1308 .blitImage = NULL,
1309 .getCapabilities = NULL,
1310 .mapImage = NULL,
1311 .unmapImage = NULL,
1312 .createImageWithModifiers = intel_create_image_with_modifiers,
1313 .createImageFromDmaBufs2 = intel_create_image_from_dma_bufs2,
1314 .queryDmaBufFormats = intel_query_dma_buf_formats,
1315 .queryDmaBufModifiers = intel_query_dma_buf_modifiers,
1316 .queryDmaBufFormatModifierAttribs = intel_query_format_modifier_attribs,
1317 };
1318
1319 static uint64_t
1320 get_aperture_size(int fd)
1321 {
1322 struct drm_i915_gem_get_aperture aperture;
1323
1324 if (drmIoctl(fd, DRM_IOCTL_I915_GEM_GET_APERTURE, &aperture) != 0)
1325 return 0;
1326
1327 return aperture.aper_size;
1328 }
1329
1330 static int
1331 brw_query_renderer_integer(__DRIscreen *dri_screen,
1332 int param, unsigned int *value)
1333 {
1334 const struct intel_screen *const screen =
1335 (struct intel_screen *) dri_screen->driverPrivate;
1336
1337 switch (param) {
1338 case __DRI2_RENDERER_VENDOR_ID:
1339 value[0] = 0x8086;
1340 return 0;
1341 case __DRI2_RENDERER_DEVICE_ID:
1342 value[0] = screen->deviceID;
1343 return 0;
1344 case __DRI2_RENDERER_ACCELERATED:
1345 value[0] = 1;
1346 return 0;
1347 case __DRI2_RENDERER_VIDEO_MEMORY: {
1348 /* Once a batch uses more than 75% of the maximum mappable size, we
1349 * assume that there's some fragmentation, and we start doing extra
1350 * flushing, etc. That's the big cliff apps will care about.
1351 */
1352 const unsigned gpu_mappable_megabytes =
1353 screen->aperture_threshold / (1024 * 1024);
1354
1355 const long system_memory_pages = sysconf(_SC_PHYS_PAGES);
1356 const long system_page_size = sysconf(_SC_PAGE_SIZE);
1357
1358 if (system_memory_pages <= 0 || system_page_size <= 0)
1359 return -1;
1360
1361 const uint64_t system_memory_bytes = (uint64_t) system_memory_pages
1362 * (uint64_t) system_page_size;
1363
1364 const unsigned system_memory_megabytes =
1365 (unsigned) (system_memory_bytes / (1024 * 1024));
1366
1367 value[0] = MIN2(system_memory_megabytes, gpu_mappable_megabytes);
1368 return 0;
1369 }
1370 case __DRI2_RENDERER_UNIFIED_MEMORY_ARCHITECTURE:
1371 value[0] = 1;
1372 return 0;
1373 case __DRI2_RENDERER_HAS_TEXTURE_3D:
1374 value[0] = 1;
1375 return 0;
1376 default:
1377 return driQueryRendererIntegerCommon(dri_screen, param, value);
1378 }
1379
1380 return -1;
1381 }
1382
1383 static int
1384 brw_query_renderer_string(__DRIscreen *dri_screen,
1385 int param, const char **value)
1386 {
1387 const struct intel_screen *screen =
1388 (struct intel_screen *) dri_screen->driverPrivate;
1389
1390 switch (param) {
1391 case __DRI2_RENDERER_VENDOR_ID:
1392 value[0] = brw_vendor_string;
1393 return 0;
1394 case __DRI2_RENDERER_DEVICE_ID:
1395 value[0] = brw_get_renderer_string(screen);
1396 return 0;
1397 default:
1398 break;
1399 }
1400
1401 return -1;
1402 }
1403
1404 static const __DRI2rendererQueryExtension intelRendererQueryExtension = {
1405 .base = { __DRI2_RENDERER_QUERY, 1 },
1406
1407 .queryInteger = brw_query_renderer_integer,
1408 .queryString = brw_query_renderer_string
1409 };
1410
1411 static const __DRIrobustnessExtension dri2Robustness = {
1412 .base = { __DRI2_ROBUSTNESS, 1 }
1413 };
1414
1415 static const __DRIextension *screenExtensions[] = {
1416 &intelTexBufferExtension.base,
1417 &intelFenceExtension.base,
1418 &intelFlushExtension.base,
1419 &intelImageExtension.base,
1420 &intelRendererQueryExtension.base,
1421 &dri2ConfigQueryExtension.base,
1422 &dri2NoErrorExtension.base,
1423 NULL
1424 };
1425
1426 static const __DRIextension *intelRobustScreenExtensions[] = {
1427 &intelTexBufferExtension.base,
1428 &intelFenceExtension.base,
1429 &intelFlushExtension.base,
1430 &intelImageExtension.base,
1431 &intelRendererQueryExtension.base,
1432 &dri2ConfigQueryExtension.base,
1433 &dri2Robustness.base,
1434 &dri2NoErrorExtension.base,
1435 NULL
1436 };
1437
1438 static int
1439 intel_get_param(struct intel_screen *screen, int param, int *value)
1440 {
1441 int ret = 0;
1442 struct drm_i915_getparam gp;
1443
1444 memset(&gp, 0, sizeof(gp));
1445 gp.param = param;
1446 gp.value = value;
1447
1448 if (drmIoctl(screen->driScrnPriv->fd, DRM_IOCTL_I915_GETPARAM, &gp) == -1) {
1449 ret = -errno;
1450 if (ret != -EINVAL)
1451 _mesa_warning(NULL, "drm_i915_getparam: %d", ret);
1452 }
1453
1454 return ret;
1455 }
1456
1457 static bool
1458 intel_get_boolean(struct intel_screen *screen, int param)
1459 {
1460 int value = 0;
1461 return (intel_get_param(screen, param, &value) == 0) && value;
1462 }
1463
1464 static int
1465 intel_get_integer(struct intel_screen *screen, int param)
1466 {
1467 int value = -1;
1468
1469 if (intel_get_param(screen, param, &value) == 0)
1470 return value;
1471
1472 return -1;
1473 }
1474
1475 static void
1476 intelDestroyScreen(__DRIscreen * sPriv)
1477 {
1478 struct intel_screen *screen = sPriv->driverPrivate;
1479
1480 brw_bufmgr_destroy(screen->bufmgr);
1481 driDestroyOptionInfo(&screen->optionCache);
1482
1483 ralloc_free(screen);
1484 sPriv->driverPrivate = NULL;
1485 }
1486
1487
1488 /**
1489 * Create a gl_framebuffer and attach it to __DRIdrawable::driverPrivate.
1490 *
1491 *_This implements driDriverAPI::createNewDrawable, which the DRI layer calls
1492 * when creating a EGLSurface, GLXDrawable, or GLXPixmap. Despite the name,
1493 * this does not allocate GPU memory.
1494 */
1495 static GLboolean
1496 intelCreateBuffer(__DRIscreen *dri_screen,
1497 __DRIdrawable * driDrawPriv,
1498 const struct gl_config * mesaVis, GLboolean isPixmap)
1499 {
1500 struct intel_renderbuffer *rb;
1501 struct intel_screen *screen = (struct intel_screen *)
1502 dri_screen->driverPrivate;
1503 mesa_format rgbFormat;
1504 unsigned num_samples =
1505 intel_quantize_num_samples(screen, mesaVis->samples);
1506
1507 if (isPixmap)
1508 return false;
1509
1510 struct gl_framebuffer *fb = CALLOC_STRUCT(gl_framebuffer);
1511 if (!fb)
1512 return false;
1513
1514 _mesa_initialize_window_framebuffer(fb, mesaVis);
1515
1516 if (screen->winsys_msaa_samples_override != -1) {
1517 num_samples = screen->winsys_msaa_samples_override;
1518 fb->Visual.samples = num_samples;
1519 }
1520
1521 if (mesaVis->redBits == 5) {
1522 rgbFormat = mesaVis->redMask == 0x1f ? MESA_FORMAT_R5G6B5_UNORM
1523 : MESA_FORMAT_B5G6R5_UNORM;
1524 } else if (mesaVis->sRGBCapable) {
1525 rgbFormat = mesaVis->redMask == 0xff ? MESA_FORMAT_R8G8B8A8_SRGB
1526 : MESA_FORMAT_B8G8R8A8_SRGB;
1527 } else if (mesaVis->alphaBits == 0) {
1528 rgbFormat = mesaVis->redMask == 0xff ? MESA_FORMAT_R8G8B8X8_UNORM
1529 : MESA_FORMAT_B8G8R8X8_UNORM;
1530 } else {
1531 rgbFormat = mesaVis->redMask == 0xff ? MESA_FORMAT_R8G8B8A8_SRGB
1532 : MESA_FORMAT_B8G8R8A8_SRGB;
1533 fb->Visual.sRGBCapable = true;
1534 }
1535
1536 /* setup the hardware-based renderbuffers */
1537 rb = intel_create_winsys_renderbuffer(screen, rgbFormat, num_samples);
1538 _mesa_attach_and_own_rb(fb, BUFFER_FRONT_LEFT, &rb->Base.Base);
1539
1540 if (mesaVis->doubleBufferMode) {
1541 rb = intel_create_winsys_renderbuffer(screen, rgbFormat, num_samples);
1542 _mesa_attach_and_own_rb(fb, BUFFER_BACK_LEFT, &rb->Base.Base);
1543 }
1544
1545 /*
1546 * Assert here that the gl_config has an expected depth/stencil bit
1547 * combination: one of d24/s8, d16/s0, d0/s0. (See intelInitScreen2(),
1548 * which constructs the advertised configs.)
1549 */
1550 if (mesaVis->depthBits == 24) {
1551 assert(mesaVis->stencilBits == 8);
1552
1553 if (screen->devinfo.has_hiz_and_separate_stencil) {
1554 rb = intel_create_private_renderbuffer(screen,
1555 MESA_FORMAT_Z24_UNORM_X8_UINT,
1556 num_samples);
1557 _mesa_attach_and_own_rb(fb, BUFFER_DEPTH, &rb->Base.Base);
1558 rb = intel_create_private_renderbuffer(screen, MESA_FORMAT_S_UINT8,
1559 num_samples);
1560 _mesa_attach_and_own_rb(fb, BUFFER_STENCIL, &rb->Base.Base);
1561 } else {
1562 /*
1563 * Use combined depth/stencil. Note that the renderbuffer is
1564 * attached to two attachment points.
1565 */
1566 rb = intel_create_private_renderbuffer(screen,
1567 MESA_FORMAT_Z24_UNORM_S8_UINT,
1568 num_samples);
1569 _mesa_attach_and_own_rb(fb, BUFFER_DEPTH, &rb->Base.Base);
1570 _mesa_attach_and_reference_rb(fb, BUFFER_STENCIL, &rb->Base.Base);
1571 }
1572 }
1573 else if (mesaVis->depthBits == 16) {
1574 assert(mesaVis->stencilBits == 0);
1575 rb = intel_create_private_renderbuffer(screen, MESA_FORMAT_Z_UNORM16,
1576 num_samples);
1577 _mesa_attach_and_own_rb(fb, BUFFER_DEPTH, &rb->Base.Base);
1578 }
1579 else {
1580 assert(mesaVis->depthBits == 0);
1581 assert(mesaVis->stencilBits == 0);
1582 }
1583
1584 /* now add any/all software-based renderbuffers we may need */
1585 _swrast_add_soft_renderbuffers(fb,
1586 false, /* never sw color */
1587 false, /* never sw depth */
1588 false, /* never sw stencil */
1589 mesaVis->accumRedBits > 0,
1590 false, /* never sw alpha */
1591 false /* never sw aux */ );
1592 driDrawPriv->driverPrivate = fb;
1593
1594 return true;
1595 }
1596
1597 static void
1598 intelDestroyBuffer(__DRIdrawable * driDrawPriv)
1599 {
1600 struct gl_framebuffer *fb = driDrawPriv->driverPrivate;
1601
1602 _mesa_reference_framebuffer(&fb, NULL);
1603 }
1604
1605 static void
1606 intel_detect_sseu(struct intel_screen *screen)
1607 {
1608 assert(screen->devinfo.gen >= 8);
1609 int ret;
1610
1611 screen->subslice_total = -1;
1612 screen->eu_total = -1;
1613
1614 ret = intel_get_param(screen, I915_PARAM_SUBSLICE_TOTAL,
1615 &screen->subslice_total);
1616 if (ret < 0 && ret != -EINVAL)
1617 goto err_out;
1618
1619 ret = intel_get_param(screen,
1620 I915_PARAM_EU_TOTAL, &screen->eu_total);
1621 if (ret < 0 && ret != -EINVAL)
1622 goto err_out;
1623
1624 /* Without this information, we cannot get the right Braswell brandstrings,
1625 * and we have to use conservative numbers for GPGPU on many platforms, but
1626 * otherwise, things will just work.
1627 */
1628 if (screen->subslice_total < 1 || screen->eu_total < 1)
1629 _mesa_warning(NULL,
1630 "Kernel 4.1 required to properly query GPU properties.\n");
1631
1632 return;
1633
1634 err_out:
1635 screen->subslice_total = -1;
1636 screen->eu_total = -1;
1637 _mesa_warning(NULL, "Failed to query GPU properties (%s).\n", strerror(-ret));
1638 }
1639
1640 static bool
1641 intel_init_bufmgr(struct intel_screen *screen)
1642 {
1643 __DRIscreen *dri_screen = screen->driScrnPriv;
1644
1645 if (getenv("INTEL_NO_HW") != NULL)
1646 screen->no_hw = true;
1647
1648 screen->bufmgr = brw_bufmgr_init(&screen->devinfo, dri_screen->fd);
1649 if (screen->bufmgr == NULL) {
1650 fprintf(stderr, "[%s:%u] Error initializing buffer manager.\n",
1651 __func__, __LINE__);
1652 return false;
1653 }
1654
1655 if (!intel_get_boolean(screen, I915_PARAM_HAS_WAIT_TIMEOUT)) {
1656 fprintf(stderr, "[%s: %u] Kernel 3.6 required.\n", __func__, __LINE__);
1657 return false;
1658 }
1659
1660 return true;
1661 }
1662
1663 static bool
1664 intel_detect_swizzling(struct intel_screen *screen)
1665 {
1666 struct brw_bo *buffer;
1667 unsigned flags = 0;
1668 uint32_t aligned_pitch;
1669 uint32_t tiling = I915_TILING_X;
1670 uint32_t swizzle_mode = 0;
1671
1672 buffer = brw_bo_alloc_tiled_2d(screen->bufmgr, "swizzle test",
1673 64, 64, 4, tiling, &aligned_pitch, flags);
1674 if (buffer == NULL)
1675 return false;
1676
1677 brw_bo_get_tiling(buffer, &tiling, &swizzle_mode);
1678 brw_bo_unreference(buffer);
1679
1680 if (swizzle_mode == I915_BIT_6_SWIZZLE_NONE)
1681 return false;
1682 else
1683 return true;
1684 }
1685
1686 static int
1687 intel_detect_timestamp(struct intel_screen *screen)
1688 {
1689 uint64_t dummy = 0, last = 0;
1690 int upper, lower, loops;
1691
1692 /* On 64bit systems, some old kernels trigger a hw bug resulting in the
1693 * TIMESTAMP register being shifted and the low 32bits always zero.
1694 *
1695 * More recent kernels offer an interface to read the full 36bits
1696 * everywhere.
1697 */
1698 if (brw_reg_read(screen->bufmgr, TIMESTAMP | 1, &dummy) == 0)
1699 return 3;
1700
1701 /* Determine if we have a 32bit or 64bit kernel by inspecting the
1702 * upper 32bits for a rapidly changing timestamp.
1703 */
1704 if (brw_reg_read(screen->bufmgr, TIMESTAMP, &last))
1705 return 0;
1706
1707 upper = lower = 0;
1708 for (loops = 0; loops < 10; loops++) {
1709 /* The TIMESTAMP should change every 80ns, so several round trips
1710 * through the kernel should be enough to advance it.
1711 */
1712 if (brw_reg_read(screen->bufmgr, TIMESTAMP, &dummy))
1713 return 0;
1714
1715 upper += (dummy >> 32) != (last >> 32);
1716 if (upper > 1) /* beware 32bit counter overflow */
1717 return 2; /* upper dword holds the low 32bits of the timestamp */
1718
1719 lower += (dummy & 0xffffffff) != (last & 0xffffffff);
1720 if (lower > 1)
1721 return 1; /* timestamp is unshifted */
1722
1723 last = dummy;
1724 }
1725
1726 /* No advancement? No timestamp! */
1727 return 0;
1728 }
1729
1730 /**
1731 * Test if we can use MI_LOAD_REGISTER_MEM from an untrusted batchbuffer.
1732 *
1733 * Some combinations of hardware and kernel versions allow this feature,
1734 * while others don't. Instead of trying to enumerate every case, just
1735 * try and write a register and see if works.
1736 */
1737 static bool
1738 intel_detect_pipelined_register(struct intel_screen *screen,
1739 int reg, uint32_t expected_value, bool reset)
1740 {
1741 if (screen->no_hw)
1742 return false;
1743
1744 struct brw_bo *results, *bo;
1745 uint32_t *batch;
1746 uint32_t offset = 0;
1747 void *map;
1748 bool success = false;
1749
1750 /* Create a zero'ed temporary buffer for reading our results */
1751 results = brw_bo_alloc(screen->bufmgr, "registers", 4096, 0);
1752 if (results == NULL)
1753 goto err;
1754
1755 bo = brw_bo_alloc(screen->bufmgr, "batchbuffer", 4096, 0);
1756 if (bo == NULL)
1757 goto err_results;
1758
1759 map = brw_bo_map(NULL, bo, MAP_WRITE);
1760 if (!map)
1761 goto err_batch;
1762
1763 batch = map;
1764
1765 /* Write the register. */
1766 *batch++ = MI_LOAD_REGISTER_IMM | (3 - 2);
1767 *batch++ = reg;
1768 *batch++ = expected_value;
1769
1770 /* Save the register's value back to the buffer. */
1771 *batch++ = MI_STORE_REGISTER_MEM | (3 - 2);
1772 *batch++ = reg;
1773 struct drm_i915_gem_relocation_entry reloc = {
1774 .offset = (char *) batch - (char *) map,
1775 .delta = offset * sizeof(uint32_t),
1776 .target_handle = results->gem_handle,
1777 .read_domains = I915_GEM_DOMAIN_INSTRUCTION,
1778 .write_domain = I915_GEM_DOMAIN_INSTRUCTION,
1779 };
1780 *batch++ = reloc.presumed_offset + reloc.delta;
1781
1782 /* And afterwards clear the register */
1783 if (reset) {
1784 *batch++ = MI_LOAD_REGISTER_IMM | (3 - 2);
1785 *batch++ = reg;
1786 *batch++ = 0;
1787 }
1788
1789 *batch++ = MI_BATCH_BUFFER_END;
1790
1791 struct drm_i915_gem_exec_object2 exec_objects[2] = {
1792 {
1793 .handle = results->gem_handle,
1794 },
1795 {
1796 .handle = bo->gem_handle,
1797 .relocation_count = 1,
1798 .relocs_ptr = (uintptr_t) &reloc,
1799 }
1800 };
1801
1802 struct drm_i915_gem_execbuffer2 execbuf = {
1803 .buffers_ptr = (uintptr_t) exec_objects,
1804 .buffer_count = 2,
1805 .batch_len = ALIGN((char *) batch - (char *) map, 8),
1806 .flags = I915_EXEC_RENDER,
1807 };
1808
1809 /* Don't bother with error checking - if the execbuf fails, the
1810 * value won't be written and we'll just report that there's no access.
1811 */
1812 __DRIscreen *dri_screen = screen->driScrnPriv;
1813 drmIoctl(dri_screen->fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf);
1814
1815 /* Check whether the value got written. */
1816 void *results_map = brw_bo_map(NULL, results, MAP_READ);
1817 if (results_map) {
1818 success = *((uint32_t *)results_map + offset) == expected_value;
1819 brw_bo_unmap(results);
1820 }
1821
1822 err_batch:
1823 brw_bo_unreference(bo);
1824 err_results:
1825 brw_bo_unreference(results);
1826 err:
1827 return success;
1828 }
1829
1830 static bool
1831 intel_detect_pipelined_so(struct intel_screen *screen)
1832 {
1833 const struct gen_device_info *devinfo = &screen->devinfo;
1834
1835 /* Supposedly, Broadwell just works. */
1836 if (devinfo->gen >= 8)
1837 return true;
1838
1839 if (devinfo->gen <= 6)
1840 return false;
1841
1842 /* See the big explanation about command parser versions below */
1843 if (screen->cmd_parser_version >= (devinfo->is_haswell ? 7 : 2))
1844 return true;
1845
1846 /* We use SO_WRITE_OFFSET0 since you're supposed to write it (unlike the
1847 * statistics registers), and we already reset it to zero before using it.
1848 */
1849 return intel_detect_pipelined_register(screen,
1850 GEN7_SO_WRITE_OFFSET(0),
1851 0x1337d0d0,
1852 false);
1853 }
1854
1855 /**
1856 * Return array of MSAA modes supported by the hardware. The array is
1857 * zero-terminated and sorted in decreasing order.
1858 */
1859 const int*
1860 intel_supported_msaa_modes(const struct intel_screen *screen)
1861 {
1862 static const int gen9_modes[] = {16, 8, 4, 2, 0, -1};
1863 static const int gen8_modes[] = {8, 4, 2, 0, -1};
1864 static const int gen7_modes[] = {8, 4, 0, -1};
1865 static const int gen6_modes[] = {4, 0, -1};
1866 static const int gen4_modes[] = {0, -1};
1867
1868 if (screen->devinfo.gen >= 9) {
1869 return gen9_modes;
1870 } else if (screen->devinfo.gen >= 8) {
1871 return gen8_modes;
1872 } else if (screen->devinfo.gen >= 7) {
1873 return gen7_modes;
1874 } else if (screen->devinfo.gen == 6) {
1875 return gen6_modes;
1876 } else {
1877 return gen4_modes;
1878 }
1879 }
1880
1881 static unsigned
1882 intel_loader_get_cap(const __DRIscreen *dri_screen, enum dri_loader_cap cap)
1883 {
1884 if (dri_screen->dri2.loader && dri_screen->dri2.loader->base.version >= 4 &&
1885 dri_screen->dri2.loader->getCapability)
1886 return dri_screen->dri2.loader->getCapability(dri_screen->loaderPrivate, cap);
1887
1888 if (dri_screen->image.loader && dri_screen->image.loader->base.version >= 2 &&
1889 dri_screen->image.loader->getCapability)
1890 return dri_screen->image.loader->getCapability(dri_screen->loaderPrivate, cap);
1891
1892 return 0;
1893 }
1894
1895 static __DRIconfig**
1896 intel_screen_make_configs(__DRIscreen *dri_screen)
1897 {
1898 static const mesa_format formats[] = {
1899 MESA_FORMAT_B5G6R5_UNORM,
1900 MESA_FORMAT_B8G8R8A8_UNORM,
1901 MESA_FORMAT_B8G8R8X8_UNORM,
1902
1903 /* The 32-bit RGBA format must not precede the 32-bit BGRA format.
1904 * Likewise for RGBX and BGRX. Otherwise, the GLX client and the GLX
1905 * server may disagree on which format the GLXFBConfig represents,
1906 * resulting in swapped color channels.
1907 *
1908 * The problem, as of 2017-05-30:
1909 * When matching a GLXFBConfig to a __DRIconfig, GLX ignores the channel
1910 * order and chooses the first __DRIconfig with the expected channel
1911 * sizes. Specifically, GLX compares the GLXFBConfig's and __DRIconfig's
1912 * __DRI_ATTRIB_{CHANNEL}_SIZE but ignores __DRI_ATTRIB_{CHANNEL}_MASK.
1913 *
1914 * EGL does not suffer from this problem. It correctly compares the
1915 * channel masks when matching EGLConfig to __DRIconfig.
1916 */
1917
1918 /* Required by Android, for HAL_PIXEL_FORMAT_RGBA_8888. */
1919 MESA_FORMAT_R8G8B8A8_UNORM,
1920
1921 /* Required by Android, for HAL_PIXEL_FORMAT_RGBX_8888. */
1922 MESA_FORMAT_R8G8B8X8_UNORM,
1923 };
1924
1925 /* GLX_SWAP_COPY_OML is not supported due to page flipping. */
1926 static const GLenum back_buffer_modes[] = {
1927 __DRI_ATTRIB_SWAP_UNDEFINED, __DRI_ATTRIB_SWAP_NONE
1928 };
1929
1930 static const uint8_t singlesample_samples[1] = {0};
1931
1932 struct intel_screen *screen = dri_screen->driverPrivate;
1933 const struct gen_device_info *devinfo = &screen->devinfo;
1934 uint8_t depth_bits[4], stencil_bits[4];
1935 __DRIconfig **configs = NULL;
1936
1937 /* Expose only BGRA ordering if the loader doesn't support RGBA ordering. */
1938 unsigned num_formats;
1939 if (intel_loader_get_cap(dri_screen, DRI_LOADER_CAP_RGBA_ORDERING))
1940 num_formats = ARRAY_SIZE(formats);
1941 else
1942 num_formats = 3;
1943
1944 /* Generate singlesample configs without accumulation buffer. */
1945 for (unsigned i = 0; i < num_formats; i++) {
1946 __DRIconfig **new_configs;
1947 int num_depth_stencil_bits = 2;
1948
1949 /* Starting with DRI2 protocol version 1.1 we can request a depth/stencil
1950 * buffer that has a different number of bits per pixel than the color
1951 * buffer, gen >= 6 supports this.
1952 */
1953 depth_bits[0] = 0;
1954 stencil_bits[0] = 0;
1955
1956 if (formats[i] == MESA_FORMAT_B5G6R5_UNORM) {
1957 depth_bits[1] = 16;
1958 stencil_bits[1] = 0;
1959 if (devinfo->gen >= 6) {
1960 depth_bits[2] = 24;
1961 stencil_bits[2] = 8;
1962 num_depth_stencil_bits = 3;
1963 }
1964 } else {
1965 depth_bits[1] = 24;
1966 stencil_bits[1] = 8;
1967 }
1968
1969 new_configs = driCreateConfigs(formats[i],
1970 depth_bits,
1971 stencil_bits,
1972 num_depth_stencil_bits,
1973 back_buffer_modes, 2,
1974 singlesample_samples, 1,
1975 false, false);
1976 configs = driConcatConfigs(configs, new_configs);
1977 }
1978
1979 /* Generate the minimum possible set of configs that include an
1980 * accumulation buffer.
1981 */
1982 for (unsigned i = 0; i < ARRAY_SIZE(formats); i++) {
1983 __DRIconfig **new_configs;
1984
1985 if (formats[i] == MESA_FORMAT_B5G6R5_UNORM) {
1986 depth_bits[0] = 16;
1987 stencil_bits[0] = 0;
1988 } else {
1989 depth_bits[0] = 24;
1990 stencil_bits[0] = 8;
1991 }
1992
1993 new_configs = driCreateConfigs(formats[i],
1994 depth_bits, stencil_bits, 1,
1995 back_buffer_modes, 1,
1996 singlesample_samples, 1,
1997 true, false);
1998 configs = driConcatConfigs(configs, new_configs);
1999 }
2000
2001 /* Generate multisample configs.
2002 *
2003 * This loop breaks early, and hence is a no-op, on gen < 6.
2004 *
2005 * Multisample configs must follow the singlesample configs in order to
2006 * work around an X server bug present in 1.12. The X server chooses to
2007 * associate the first listed RGBA888-Z24S8 config, regardless of its
2008 * sample count, with the 32-bit depth visual used for compositing.
2009 *
2010 * Only doublebuffer configs with GLX_SWAP_UNDEFINED_OML behavior are
2011 * supported. Singlebuffer configs are not supported because no one wants
2012 * them.
2013 */
2014 for (unsigned i = 0; i < ARRAY_SIZE(formats); i++) {
2015 if (devinfo->gen < 6)
2016 break;
2017
2018 __DRIconfig **new_configs;
2019 const int num_depth_stencil_bits = 2;
2020 int num_msaa_modes = 0;
2021 const uint8_t *multisample_samples = NULL;
2022
2023 depth_bits[0] = 0;
2024 stencil_bits[0] = 0;
2025
2026 if (formats[i] == MESA_FORMAT_B5G6R5_UNORM) {
2027 depth_bits[1] = 16;
2028 stencil_bits[1] = 0;
2029 } else {
2030 depth_bits[1] = 24;
2031 stencil_bits[1] = 8;
2032 }
2033
2034 if (devinfo->gen >= 9) {
2035 static const uint8_t multisample_samples_gen9[] = {2, 4, 8, 16};
2036 multisample_samples = multisample_samples_gen9;
2037 num_msaa_modes = ARRAY_SIZE(multisample_samples_gen9);
2038 } else if (devinfo->gen == 8) {
2039 static const uint8_t multisample_samples_gen8[] = {2, 4, 8};
2040 multisample_samples = multisample_samples_gen8;
2041 num_msaa_modes = ARRAY_SIZE(multisample_samples_gen8);
2042 } else if (devinfo->gen == 7) {
2043 static const uint8_t multisample_samples_gen7[] = {4, 8};
2044 multisample_samples = multisample_samples_gen7;
2045 num_msaa_modes = ARRAY_SIZE(multisample_samples_gen7);
2046 } else if (devinfo->gen == 6) {
2047 static const uint8_t multisample_samples_gen6[] = {4};
2048 multisample_samples = multisample_samples_gen6;
2049 num_msaa_modes = ARRAY_SIZE(multisample_samples_gen6);
2050 }
2051
2052 new_configs = driCreateConfigs(formats[i],
2053 depth_bits,
2054 stencil_bits,
2055 num_depth_stencil_bits,
2056 back_buffer_modes, 1,
2057 multisample_samples,
2058 num_msaa_modes,
2059 false, false);
2060 configs = driConcatConfigs(configs, new_configs);
2061 }
2062
2063 if (configs == NULL) {
2064 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
2065 __LINE__);
2066 return NULL;
2067 }
2068
2069 return configs;
2070 }
2071
2072 static void
2073 set_max_gl_versions(struct intel_screen *screen)
2074 {
2075 __DRIscreen *dri_screen = screen->driScrnPriv;
2076 const bool has_astc = screen->devinfo.gen >= 9;
2077
2078 switch (screen->devinfo.gen) {
2079 case 10:
2080 case 9:
2081 case 8:
2082 dri_screen->max_gl_core_version = 45;
2083 dri_screen->max_gl_compat_version = 30;
2084 dri_screen->max_gl_es1_version = 11;
2085 dri_screen->max_gl_es2_version = has_astc ? 32 : 31;
2086 break;
2087 case 7:
2088 dri_screen->max_gl_core_version = 33;
2089 if (can_do_pipelined_register_writes(screen)) {
2090 dri_screen->max_gl_core_version = 42;
2091 if (screen->devinfo.is_haswell && can_do_compute_dispatch(screen))
2092 dri_screen->max_gl_core_version = 43;
2093 if (screen->devinfo.is_haswell && can_do_mi_math_and_lrr(screen))
2094 dri_screen->max_gl_core_version = 45;
2095 }
2096 dri_screen->max_gl_compat_version = 30;
2097 dri_screen->max_gl_es1_version = 11;
2098 dri_screen->max_gl_es2_version = screen->devinfo.is_haswell ? 31 : 30;
2099 break;
2100 case 6:
2101 dri_screen->max_gl_core_version = 33;
2102 dri_screen->max_gl_compat_version = 30;
2103 dri_screen->max_gl_es1_version = 11;
2104 dri_screen->max_gl_es2_version = 30;
2105 break;
2106 case 5:
2107 case 4:
2108 dri_screen->max_gl_core_version = 0;
2109 dri_screen->max_gl_compat_version = 21;
2110 dri_screen->max_gl_es1_version = 11;
2111 dri_screen->max_gl_es2_version = 20;
2112 break;
2113 default:
2114 unreachable("unrecognized intel_screen::gen");
2115 }
2116 }
2117
2118 /**
2119 * Return the revision (generally the revid field of the PCI header) of the
2120 * graphics device.
2121 *
2122 * XXX: This function is useful to keep around even if it is not currently in
2123 * use. It is necessary for new platforms and revision specific workarounds or
2124 * features. Please don't remove it so that we know it at least continues to
2125 * build.
2126 */
2127 static __attribute__((__unused__)) int
2128 brw_get_revision(int fd)
2129 {
2130 struct drm_i915_getparam gp;
2131 int revision;
2132 int ret;
2133
2134 memset(&gp, 0, sizeof(gp));
2135 gp.param = I915_PARAM_REVISION;
2136 gp.value = &revision;
2137
2138 ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
2139 if (ret)
2140 revision = -1;
2141
2142 return revision;
2143 }
2144
2145 static void
2146 shader_debug_log_mesa(void *data, const char *fmt, ...)
2147 {
2148 struct brw_context *brw = (struct brw_context *)data;
2149 va_list args;
2150
2151 va_start(args, fmt);
2152 GLuint msg_id = 0;
2153 _mesa_gl_vdebug(&brw->ctx, &msg_id,
2154 MESA_DEBUG_SOURCE_SHADER_COMPILER,
2155 MESA_DEBUG_TYPE_OTHER,
2156 MESA_DEBUG_SEVERITY_NOTIFICATION, fmt, args);
2157 va_end(args);
2158 }
2159
2160 static void
2161 shader_perf_log_mesa(void *data, const char *fmt, ...)
2162 {
2163 struct brw_context *brw = (struct brw_context *)data;
2164
2165 va_list args;
2166 va_start(args, fmt);
2167
2168 if (unlikely(INTEL_DEBUG & DEBUG_PERF)) {
2169 va_list args_copy;
2170 va_copy(args_copy, args);
2171 vfprintf(stderr, fmt, args_copy);
2172 va_end(args_copy);
2173 }
2174
2175 if (brw->perf_debug) {
2176 GLuint msg_id = 0;
2177 _mesa_gl_vdebug(&brw->ctx, &msg_id,
2178 MESA_DEBUG_SOURCE_SHADER_COMPILER,
2179 MESA_DEBUG_TYPE_PERFORMANCE,
2180 MESA_DEBUG_SEVERITY_MEDIUM, fmt, args);
2181 }
2182 va_end(args);
2183 }
2184
2185 static int
2186 parse_devid_override(const char *devid_override)
2187 {
2188 static const struct {
2189 const char *name;
2190 int pci_id;
2191 } name_map[] = {
2192 { "brw", 0x2a02 },
2193 { "g4x", 0x2a42 },
2194 { "ilk", 0x0042 },
2195 { "snb", 0x0126 },
2196 { "ivb", 0x016a },
2197 { "hsw", 0x0d2e },
2198 { "byt", 0x0f33 },
2199 { "bdw", 0x162e },
2200 { "skl", 0x1912 },
2201 { "kbl", 0x5912 },
2202 { "cnl", 0x5a52 },
2203 };
2204
2205 for (unsigned i = 0; i < ARRAY_SIZE(name_map); i++) {
2206 if (!strcmp(name_map[i].name, devid_override))
2207 return name_map[i].pci_id;
2208 }
2209
2210 return strtol(devid_override, NULL, 0);
2211 }
2212
2213 /**
2214 * Get the PCI ID for the device. This can be overridden by setting the
2215 * INTEL_DEVID_OVERRIDE environment variable to the desired ID.
2216 *
2217 * Returns -1 on ioctl failure.
2218 */
2219 static int
2220 get_pci_device_id(struct intel_screen *screen)
2221 {
2222 if (geteuid() == getuid()) {
2223 char *devid_override = getenv("INTEL_DEVID_OVERRIDE");
2224 if (devid_override) {
2225 screen->no_hw = true;
2226 return parse_devid_override(devid_override);
2227 }
2228 }
2229
2230 return intel_get_integer(screen, I915_PARAM_CHIPSET_ID);
2231 }
2232
2233 /**
2234 * This is the driver specific part of the createNewScreen entry point.
2235 * Called when using DRI2.
2236 *
2237 * \return the struct gl_config supported by this driver
2238 */
2239 static const
2240 __DRIconfig **intelInitScreen2(__DRIscreen *dri_screen)
2241 {
2242 struct intel_screen *screen;
2243
2244 if (dri_screen->image.loader) {
2245 } else if (dri_screen->dri2.loader->base.version <= 2 ||
2246 dri_screen->dri2.loader->getBuffersWithFormat == NULL) {
2247 fprintf(stderr,
2248 "\nERROR! DRI2 loader with getBuffersWithFormat() "
2249 "support required\n");
2250 return NULL;
2251 }
2252
2253 /* Allocate the private area */
2254 screen = rzalloc(NULL, struct intel_screen);
2255 if (!screen) {
2256 fprintf(stderr, "\nERROR! Allocating private area failed\n");
2257 return NULL;
2258 }
2259 /* parse information in __driConfigOptions */
2260 driParseOptionInfo(&screen->optionCache, brw_config_options.xml);
2261
2262 screen->driScrnPriv = dri_screen;
2263 dri_screen->driverPrivate = (void *) screen;
2264
2265 screen->deviceID = get_pci_device_id(screen);
2266
2267 if (!gen_get_device_info(screen->deviceID, &screen->devinfo))
2268 return NULL;
2269
2270 if (!intel_init_bufmgr(screen))
2271 return NULL;
2272
2273 const struct gen_device_info *devinfo = &screen->devinfo;
2274
2275 brw_process_intel_debug_variable();
2276
2277 if ((INTEL_DEBUG & DEBUG_SHADER_TIME) && devinfo->gen < 7) {
2278 fprintf(stderr,
2279 "shader_time debugging requires gen7 (Ivybridge) or better.\n");
2280 INTEL_DEBUG &= ~DEBUG_SHADER_TIME;
2281 }
2282
2283 if (intel_get_integer(screen, I915_PARAM_MMAP_GTT_VERSION) >= 1) {
2284 /* Theorectically unlimited! At least for individual objects...
2285 *
2286 * Currently the entire (global) address space for all GTT maps is
2287 * limited to 64bits. That is all objects on the system that are
2288 * setup for GTT mmapping must fit within 64bits. An attempt to use
2289 * one that exceeds the limit with fail in brw_bo_map_gtt().
2290 *
2291 * Long before we hit that limit, we will be practically limited by
2292 * that any single object must fit in physical memory (RAM). The upper
2293 * limit on the CPU's address space is currently 48bits (Skylake), of
2294 * which only 39bits can be physical memory. (The GPU itself also has
2295 * a 48bit addressable virtual space.) We can fit over 32 million
2296 * objects of the current maximum allocable size before running out
2297 * of mmap space.
2298 */
2299 screen->max_gtt_map_object_size = UINT64_MAX;
2300 } else {
2301 /* Estimate the size of the mappable aperture into the GTT. There's an
2302 * ioctl to get the whole GTT size, but not one to get the mappable subset.
2303 * It turns out it's basically always 256MB, though some ancient hardware
2304 * was smaller.
2305 */
2306 uint32_t gtt_size = 256 * 1024 * 1024;
2307
2308 /* We don't want to map two objects such that a memcpy between them would
2309 * just fault one mapping in and then the other over and over forever. So
2310 * we would need to divide the GTT size by 2. Additionally, some GTT is
2311 * taken up by things like the framebuffer and the ringbuffer and such, so
2312 * be more conservative.
2313 */
2314 screen->max_gtt_map_object_size = gtt_size / 4;
2315 }
2316
2317 screen->aperture_threshold = get_aperture_size(dri_screen->fd) * 3 / 4;
2318
2319 screen->hw_has_swizzling = intel_detect_swizzling(screen);
2320 screen->hw_has_timestamp = intel_detect_timestamp(screen);
2321
2322 isl_device_init(&screen->isl_dev, &screen->devinfo,
2323 screen->hw_has_swizzling);
2324
2325 /* GENs prior to 8 do not support EU/Subslice info */
2326 if (devinfo->gen >= 8) {
2327 intel_detect_sseu(screen);
2328 } else if (devinfo->gen == 7) {
2329 screen->subslice_total = 1 << (devinfo->gt - 1);
2330 }
2331
2332 /* Gen7-7.5 kernel requirements / command parser saga:
2333 *
2334 * - pre-v3.16:
2335 * Haswell and Baytrail cannot use any privileged batchbuffer features.
2336 *
2337 * Ivybridge has aliasing PPGTT on by default, which accidentally marks
2338 * all batches secure, allowing them to use any feature with no checking.
2339 * This is effectively equivalent to a command parser version of
2340 * \infinity - everything is possible.
2341 *
2342 * The command parser does not exist, and querying the version will
2343 * return -EINVAL.
2344 *
2345 * - v3.16:
2346 * The kernel enables the command parser by default, for systems with
2347 * aliasing PPGTT enabled (Ivybridge and Haswell). However, the
2348 * hardware checker is still enabled, so Haswell and Baytrail cannot
2349 * do anything.
2350 *
2351 * Ivybridge goes from "everything is possible" to "only what the
2352 * command parser allows" (if the user boots with i915.cmd_parser=0,
2353 * then everything is possible again). We can only safely use features
2354 * allowed by the supported command parser version.
2355 *
2356 * Annoyingly, I915_PARAM_CMD_PARSER_VERSION reports the static version
2357 * implemented by the kernel, even if it's turned off. So, checking
2358 * for version > 0 does not mean that you can write registers. We have
2359 * to try it and see. The version does, however, indicate the age of
2360 * the kernel.
2361 *
2362 * Instead of matching the hardware checker's behavior of converting
2363 * privileged commands to MI_NOOP, it makes execbuf2 start returning
2364 * -EINVAL, making it dangerous to try and use privileged features.
2365 *
2366 * Effective command parser versions:
2367 * - Haswell: 0 (reporting 1, writes don't work)
2368 * - Baytrail: 0 (reporting 1, writes don't work)
2369 * - Ivybridge: 1 (enabled) or infinite (disabled)
2370 *
2371 * - v3.17:
2372 * Baytrail aliasing PPGTT is enabled, making it like Ivybridge:
2373 * effectively version 1 (enabled) or infinite (disabled).
2374 *
2375 * - v3.19: f1f55cc0556031c8ee3fe99dae7251e78b9b653b
2376 * Command parser v2 supports predicate writes.
2377 *
2378 * - Haswell: 0 (reporting 1, writes don't work)
2379 * - Baytrail: 2 (enabled) or infinite (disabled)
2380 * - Ivybridge: 2 (enabled) or infinite (disabled)
2381 *
2382 * So version >= 2 is enough to know that Ivybridge and Baytrail
2383 * will work. Haswell still can't do anything.
2384 *
2385 * - v4.0: Version 3 happened. Largely not relevant.
2386 *
2387 * - v4.1: 6702cf16e0ba8b0129f5aa1b6609d4e9c70bc13b
2388 * L3 config registers are properly saved and restored as part
2389 * of the hardware context. We can approximately detect this point
2390 * in time by checking if I915_PARAM_REVISION is recognized - it
2391 * landed in a later commit, but in the same release cycle.
2392 *
2393 * - v4.2: 245054a1fe33c06ad233e0d58a27ec7b64db9284
2394 * Command parser finally gains secure batch promotion. On Haswell,
2395 * the hardware checker gets disabled, which finally allows it to do
2396 * privileged commands.
2397 *
2398 * I915_PARAM_CMD_PARSER_VERSION reports 3. Effective versions:
2399 * - Haswell: 3 (enabled) or 0 (disabled)
2400 * - Baytrail: 3 (enabled) or infinite (disabled)
2401 * - Ivybridge: 3 (enabled) or infinite (disabled)
2402 *
2403 * Unfortunately, detecting this point in time is tricky, because
2404 * no version bump happened when this important change occurred.
2405 * On Haswell, if we can write any register, then the kernel is at
2406 * least this new, and we can start trusting the version number.
2407 *
2408 * - v4.4: 2bbe6bbb0dc94fd4ce287bdac9e1bd184e23057b and
2409 * Command parser reaches version 4, allowing access to Haswell
2410 * atomic scratch and chicken3 registers. If version >= 4, we know
2411 * the kernel is new enough to support privileged features on all
2412 * hardware. However, the user might have disabled it...and the
2413 * kernel will still report version 4. So we still have to guess
2414 * and check.
2415 *
2416 * - v4.4: 7b9748cb513a6bef4af87b79f0da3ff7e8b56cd8
2417 * Command parser v5 whitelists indirect compute shader dispatch
2418 * registers, needed for OpenGL 4.3 and later.
2419 *
2420 * - v4.8:
2421 * Command parser v7 lets us use MI_MATH on Haswell.
2422 *
2423 * Additionally, the kernel begins reporting version 0 when
2424 * the command parser is disabled, allowing us to skip the
2425 * guess-and-check step on Haswell. Unfortunately, this also
2426 * means that we can no longer use it as an indicator of the
2427 * age of the kernel.
2428 */
2429 if (intel_get_param(screen, I915_PARAM_CMD_PARSER_VERSION,
2430 &screen->cmd_parser_version) < 0) {
2431 /* Command parser does not exist - getparam is unrecognized */
2432 screen->cmd_parser_version = 0;
2433 }
2434
2435 /* Kernel 4.13 retuired for exec object capture */
2436 if (intel_get_boolean(screen, I915_PARAM_HAS_EXEC_CAPTURE)) {
2437 screen->kernel_features |= KERNEL_ALLOWS_EXEC_CAPTURE;
2438 }
2439
2440 if (intel_get_boolean(screen, I915_PARAM_HAS_EXEC_BATCH_FIRST)) {
2441 screen->kernel_features |= KERNEL_ALLOWS_EXEC_BATCH_FIRST;
2442 }
2443
2444 if (!intel_detect_pipelined_so(screen)) {
2445 /* We can't do anything, so the effective version is 0. */
2446 screen->cmd_parser_version = 0;
2447 } else {
2448 screen->kernel_features |= KERNEL_ALLOWS_SOL_OFFSET_WRITES;
2449 }
2450
2451 if (devinfo->gen >= 8 || screen->cmd_parser_version >= 2)
2452 screen->kernel_features |= KERNEL_ALLOWS_PREDICATE_WRITES;
2453
2454 /* Haswell requires command parser version 4 in order to have L3
2455 * atomic scratch1 and chicken3 bits
2456 */
2457 if (devinfo->is_haswell && screen->cmd_parser_version >= 4) {
2458 screen->kernel_features |=
2459 KERNEL_ALLOWS_HSW_SCRATCH1_AND_ROW_CHICKEN3;
2460 }
2461
2462 /* Haswell requires command parser version 6 in order to write to the
2463 * MI_MATH GPR registers, and version 7 in order to use
2464 * MI_LOAD_REGISTER_REG (which all users of MI_MATH use).
2465 */
2466 if (devinfo->gen >= 8 ||
2467 (devinfo->is_haswell && screen->cmd_parser_version >= 7)) {
2468 screen->kernel_features |= KERNEL_ALLOWS_MI_MATH_AND_LRR;
2469 }
2470
2471 /* Gen7 needs at least command parser version 5 to support compute */
2472 if (devinfo->gen >= 8 || screen->cmd_parser_version >= 5)
2473 screen->kernel_features |= KERNEL_ALLOWS_COMPUTE_DISPATCH;
2474
2475 const char *force_msaa = getenv("INTEL_FORCE_MSAA");
2476 if (force_msaa) {
2477 screen->winsys_msaa_samples_override =
2478 intel_quantize_num_samples(screen, atoi(force_msaa));
2479 printf("Forcing winsys sample count to %d\n",
2480 screen->winsys_msaa_samples_override);
2481 } else {
2482 screen->winsys_msaa_samples_override = -1;
2483 }
2484
2485 set_max_gl_versions(screen);
2486
2487 /* Notification of GPU resets requires hardware contexts and a kernel new
2488 * enough to support DRM_IOCTL_I915_GET_RESET_STATS. If the ioctl is
2489 * supported, calling it with a context of 0 will either generate EPERM or
2490 * no error. If the ioctl is not supported, it always generate EINVAL.
2491 * Use this to determine whether to advertise the __DRI2_ROBUSTNESS
2492 * extension to the loader.
2493 *
2494 * Don't even try on pre-Gen6, since we don't attempt to use contexts there.
2495 */
2496 if (devinfo->gen >= 6) {
2497 struct drm_i915_reset_stats stats;
2498 memset(&stats, 0, sizeof(stats));
2499
2500 const int ret = drmIoctl(dri_screen->fd, DRM_IOCTL_I915_GET_RESET_STATS, &stats);
2501
2502 screen->has_context_reset_notification =
2503 (ret != -1 || errno != EINVAL);
2504 }
2505
2506 dri_screen->extensions = !screen->has_context_reset_notification
2507 ? screenExtensions : intelRobustScreenExtensions;
2508
2509 screen->compiler = brw_compiler_create(screen, devinfo);
2510 screen->compiler->shader_debug_log = shader_debug_log_mesa;
2511 screen->compiler->shader_perf_log = shader_perf_log_mesa;
2512 screen->compiler->constant_buffer_0_is_relative = devinfo->gen < 8;
2513 screen->program_id = 1;
2514
2515 screen->has_exec_fence =
2516 intel_get_boolean(screen, I915_PARAM_HAS_EXEC_FENCE);
2517
2518 intel_screen_init_surface_formats(screen);
2519
2520 return (const __DRIconfig**) intel_screen_make_configs(dri_screen);
2521 }
2522
2523 struct intel_buffer {
2524 __DRIbuffer base;
2525 struct brw_bo *bo;
2526 };
2527
2528 static __DRIbuffer *
2529 intelAllocateBuffer(__DRIscreen *dri_screen,
2530 unsigned attachment, unsigned format,
2531 int width, int height)
2532 {
2533 struct intel_buffer *intelBuffer;
2534 struct intel_screen *screen = dri_screen->driverPrivate;
2535
2536 assert(attachment == __DRI_BUFFER_FRONT_LEFT ||
2537 attachment == __DRI_BUFFER_BACK_LEFT);
2538
2539 intelBuffer = calloc(1, sizeof *intelBuffer);
2540 if (intelBuffer == NULL)
2541 return NULL;
2542
2543 /* The front and back buffers are color buffers, which are X tiled. GEN9+
2544 * supports Y tiled and compressed buffers, but there is no way to plumb that
2545 * through to here. */
2546 uint32_t pitch;
2547 int cpp = format / 8;
2548 intelBuffer->bo = brw_bo_alloc_tiled_2d(screen->bufmgr,
2549 "intelAllocateBuffer",
2550 width,
2551 height,
2552 cpp,
2553 I915_TILING_X, &pitch,
2554 BO_ALLOC_BUSY);
2555
2556 if (intelBuffer->bo == NULL) {
2557 free(intelBuffer);
2558 return NULL;
2559 }
2560
2561 brw_bo_flink(intelBuffer->bo, &intelBuffer->base.name);
2562
2563 intelBuffer->base.attachment = attachment;
2564 intelBuffer->base.cpp = cpp;
2565 intelBuffer->base.pitch = pitch;
2566
2567 return &intelBuffer->base;
2568 }
2569
2570 static void
2571 intelReleaseBuffer(__DRIscreen *dri_screen, __DRIbuffer *buffer)
2572 {
2573 struct intel_buffer *intelBuffer = (struct intel_buffer *) buffer;
2574
2575 brw_bo_unreference(intelBuffer->bo);
2576 free(intelBuffer);
2577 }
2578
2579 static const struct __DriverAPIRec brw_driver_api = {
2580 .InitScreen = intelInitScreen2,
2581 .DestroyScreen = intelDestroyScreen,
2582 .CreateContext = brwCreateContext,
2583 .DestroyContext = intelDestroyContext,
2584 .CreateBuffer = intelCreateBuffer,
2585 .DestroyBuffer = intelDestroyBuffer,
2586 .MakeCurrent = intelMakeCurrent,
2587 .UnbindContext = intelUnbindContext,
2588 .AllocateBuffer = intelAllocateBuffer,
2589 .ReleaseBuffer = intelReleaseBuffer
2590 };
2591
2592 static const struct __DRIDriverVtableExtensionRec brw_vtable = {
2593 .base = { __DRI_DRIVER_VTABLE, 1 },
2594 .vtable = &brw_driver_api,
2595 };
2596
2597 static const __DRIextension *brw_driver_extensions[] = {
2598 &driCoreExtension.base,
2599 &driImageDriverExtension.base,
2600 &driDRI2Extension.base,
2601 &brw_vtable.base,
2602 &brw_config_options.base,
2603 NULL
2604 };
2605
2606 PUBLIC const __DRIextension **__driDriverGetExtensions_i965(void)
2607 {
2608 globalDriverAPI = &brw_driver_api;
2609
2610 return brw_driver_extensions;
2611 }