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