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