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