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