1 /*
2 * Copyright © 2017 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
22
23 /**
24 * @file iris_resource.c
25 *
26 * Resources are images, buffers, and other objects used by the GPU.
27 *
28 * XXX: explain resources
29 */
30
31 #include <stdio.h>
32 #include <errno.h>
33 #include "pipe/p_defines.h"
34 #include "pipe/p_state.h"
35 #include "pipe/p_context.h"
36 #include "pipe/p_screen.h"
37 #include "util/detect_os.h"
38 #include "util/os_memory.h"
39 #include "util/u_cpu_detect.h"
40 #include "util/u_inlines.h"
41 #include "util/format/u_format.h"
42 #include "util/u_memory.h"
43 #include "util/u_resource.h"
44 #include "util/u_threaded_context.h"
45 #include "util/u_transfer.h"
46 #include "util/u_transfer_helper.h"
47 #include "util/u_upload_mgr.h"
48 #include "util/ralloc.h"
49 #include "iris_batch.h"
50 #include "iris_context.h"
51 #include "iris_resource.h"
52 #include "iris_screen.h"
53 #include "intel/common/intel_aux_map.h"
54 #include "intel/dev/intel_debug.h"
55 #include "isl/isl.h"
56 #include "drm-uapi/drm_fourcc.h"
57 #include "drm-uapi/i915_drm.h"
58
59 enum modifier_priority {
60 MODIFIER_PRIORITY_INVALID = 0,
61 MODIFIER_PRIORITY_LINEAR,
62 MODIFIER_PRIORITY_X,
63 MODIFIER_PRIORITY_Y,
64 MODIFIER_PRIORITY_Y_CCS,
65 MODIFIER_PRIORITY_Y_GFX12_RC_CCS,
66 MODIFIER_PRIORITY_Y_GFX12_RC_CCS_CC,
67 MODIFIER_PRIORITY_4,
68 MODIFIER_PRIORITY_4_DG2_RC_CCS,
69 MODIFIER_PRIORITY_4_DG2_RC_CCS_CC,
70 MODIFIER_PRIORITY_4_MTL_RC_CCS,
71 MODIFIER_PRIORITY_4_MTL_RC_CCS_CC,
72 };
73
74 static const uint64_t priority_to_modifier[] = {
75 [MODIFIER_PRIORITY_INVALID] = DRM_FORMAT_MOD_INVALID,
76 [MODIFIER_PRIORITY_LINEAR] = DRM_FORMAT_MOD_LINEAR,
77 [MODIFIER_PRIORITY_X] = I915_FORMAT_MOD_X_TILED,
78 [MODIFIER_PRIORITY_Y] = I915_FORMAT_MOD_Y_TILED,
79 [MODIFIER_PRIORITY_Y_CCS] = I915_FORMAT_MOD_Y_TILED_CCS,
80 [MODIFIER_PRIORITY_Y_GFX12_RC_CCS] = I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS,
81 [MODIFIER_PRIORITY_Y_GFX12_RC_CCS_CC] = I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC,
82 [MODIFIER_PRIORITY_4] = I915_FORMAT_MOD_4_TILED,
83 [MODIFIER_PRIORITY_4_DG2_RC_CCS] = I915_FORMAT_MOD_4_TILED_DG2_RC_CCS,
84 [MODIFIER_PRIORITY_4_DG2_RC_CCS_CC] = I915_FORMAT_MOD_4_TILED_DG2_RC_CCS_CC,
85 [MODIFIER_PRIORITY_4_MTL_RC_CCS] = I915_FORMAT_MOD_4_TILED_MTL_RC_CCS,
86 [MODIFIER_PRIORITY_4_MTL_RC_CCS_CC] = I915_FORMAT_MOD_4_TILED_MTL_RC_CCS_CC,
87 };
88
89 static bool
modifier_is_supported(const struct intel_device_info * devinfo,enum pipe_format pfmt,unsigned bind,uint64_t modifier)90 modifier_is_supported(const struct intel_device_info *devinfo,
91 enum pipe_format pfmt, unsigned bind,
92 uint64_t modifier)
93 {
94 /* Check for basic device support. */
95 switch (modifier) {
96 case DRM_FORMAT_MOD_LINEAR:
97 case I915_FORMAT_MOD_X_TILED:
98 break;
99 case I915_FORMAT_MOD_Y_TILED:
100 if (devinfo->ver <= 8 && (bind & PIPE_BIND_SCANOUT))
101 return false;
102 if (devinfo->verx10 >= 125)
103 return false;
104 break;
105 case I915_FORMAT_MOD_Y_TILED_CCS:
106 if (devinfo->ver <= 8 || devinfo->ver >= 12)
107 return false;
108 break;
109 case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
110 case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
111 case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC:
112 if (devinfo->verx10 != 120)
113 return false;
114 break;
115 case I915_FORMAT_MOD_4_TILED:
116 if (devinfo->verx10 < 125)
117 return false;
118 break;
119 case I915_FORMAT_MOD_4_TILED_DG2_RC_CCS:
120 case I915_FORMAT_MOD_4_TILED_DG2_MC_CCS:
121 case I915_FORMAT_MOD_4_TILED_DG2_RC_CCS_CC:
122 if (!intel_device_info_is_dg2(devinfo))
123 return false;
124 break;
125 case I915_FORMAT_MOD_4_TILED_MTL_RC_CCS:
126 case I915_FORMAT_MOD_4_TILED_MTL_MC_CCS:
127 case I915_FORMAT_MOD_4_TILED_MTL_RC_CCS_CC:
128 if (!intel_device_info_is_mtl_or_arl(devinfo))
129 return false;
130 break;
131 case DRM_FORMAT_MOD_INVALID:
132 default:
133 return false;
134 }
135
136 bool no_ccs = INTEL_DEBUG(DEBUG_NO_CCS) || (bind & PIPE_BIND_CONST_BW);
137
138 /* Check remaining requirements. */
139 switch (modifier) {
140 case I915_FORMAT_MOD_4_TILED_MTL_MC_CCS:
141 case I915_FORMAT_MOD_4_TILED_DG2_MC_CCS:
142 case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
143 if (no_ccs)
144 return false;
145
146 if (pfmt != PIPE_FORMAT_BGRA8888_UNORM &&
147 pfmt != PIPE_FORMAT_RGBA8888_UNORM &&
148 pfmt != PIPE_FORMAT_BGRX8888_UNORM &&
149 pfmt != PIPE_FORMAT_RGBX8888_UNORM &&
150 pfmt != PIPE_FORMAT_NV12 &&
151 pfmt != PIPE_FORMAT_P010 &&
152 pfmt != PIPE_FORMAT_P012 &&
153 pfmt != PIPE_FORMAT_P016 &&
154 pfmt != PIPE_FORMAT_YUYV &&
155 pfmt != PIPE_FORMAT_UYVY) {
156 return false;
157 }
158 break;
159 case I915_FORMAT_MOD_4_TILED_MTL_RC_CCS:
160 case I915_FORMAT_MOD_4_TILED_MTL_RC_CCS_CC:
161 case I915_FORMAT_MOD_4_TILED_DG2_RC_CCS_CC:
162 case I915_FORMAT_MOD_4_TILED_DG2_RC_CCS:
163 case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC:
164 case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
165 case I915_FORMAT_MOD_Y_TILED_CCS: {
166 if (no_ccs)
167 return false;
168
169 enum isl_format rt_format =
170 iris_format_for_usage(devinfo, pfmt,
171 ISL_SURF_USAGE_RENDER_TARGET_BIT).fmt;
172
173 if (rt_format == ISL_FORMAT_UNSUPPORTED ||
174 !isl_format_supports_ccs_e(devinfo, rt_format))
175 return false;
176 break;
177 }
178 default:
179 break;
180 }
181
182 return true;
183 }
184
185 static uint64_t
select_best_modifier(const struct intel_device_info * devinfo,const struct pipe_resource * templ,const uint64_t * modifiers,int count)186 select_best_modifier(const struct intel_device_info *devinfo,
187 const struct pipe_resource *templ,
188 const uint64_t *modifiers,
189 int count)
190 {
191 enum modifier_priority prio = MODIFIER_PRIORITY_INVALID;
192
193 for (int i = 0; i < count; i++) {
194 if (!modifier_is_supported(devinfo, templ->format, templ->bind,
195 modifiers[i]))
196 continue;
197
198 switch (modifiers[i]) {
199 case I915_FORMAT_MOD_4_TILED_MTL_RC_CCS_CC:
200 prio = MAX2(prio, MODIFIER_PRIORITY_4_MTL_RC_CCS_CC);
201 break;
202 case I915_FORMAT_MOD_4_TILED_MTL_RC_CCS:
203 prio = MAX2(prio, MODIFIER_PRIORITY_4_MTL_RC_CCS);
204 break;
205 case I915_FORMAT_MOD_4_TILED_DG2_RC_CCS_CC:
206 prio = MAX2(prio, MODIFIER_PRIORITY_4_DG2_RC_CCS_CC);
207 break;
208 case I915_FORMAT_MOD_4_TILED_DG2_RC_CCS:
209 prio = MAX2(prio, MODIFIER_PRIORITY_4_DG2_RC_CCS);
210 break;
211 case I915_FORMAT_MOD_4_TILED:
212 prio = MAX2(prio, MODIFIER_PRIORITY_4);
213 break;
214 case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC:
215 prio = MAX2(prio, MODIFIER_PRIORITY_Y_GFX12_RC_CCS_CC);
216 break;
217 case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
218 prio = MAX2(prio, MODIFIER_PRIORITY_Y_GFX12_RC_CCS);
219 break;
220 case I915_FORMAT_MOD_Y_TILED_CCS:
221 prio = MAX2(prio, MODIFIER_PRIORITY_Y_CCS);
222 break;
223 case I915_FORMAT_MOD_Y_TILED:
224 prio = MAX2(prio, MODIFIER_PRIORITY_Y);
225 break;
226 case I915_FORMAT_MOD_X_TILED:
227 prio = MAX2(prio, MODIFIER_PRIORITY_X);
228 break;
229 case DRM_FORMAT_MOD_LINEAR:
230 prio = MAX2(prio, MODIFIER_PRIORITY_LINEAR);
231 break;
232 case DRM_FORMAT_MOD_INVALID:
233 default:
234 break;
235 }
236 }
237
238 return priority_to_modifier[prio];
239 }
240
is_modifier_external_only(enum pipe_format pfmt,uint64_t modifier)241 static inline bool is_modifier_external_only(enum pipe_format pfmt,
242 uint64_t modifier)
243 {
244 /* Only allow external usage for the following cases: YUV formats
245 * and the media-compression modifier. The render engine lacks
246 * support for rendering to a media-compressed surface if the
247 * compression ratio is large enough. By requiring external usage
248 * of media-compressed surfaces, resolves are avoided.
249 */
250 return util_format_is_yuv(pfmt) ||
251 isl_drm_modifier_get_info(modifier)->supports_media_compression;
252 }
253
254 static void
iris_query_dmabuf_modifiers(struct pipe_screen * pscreen,enum pipe_format pfmt,int max,uint64_t * modifiers,unsigned int * external_only,int * count)255 iris_query_dmabuf_modifiers(struct pipe_screen *pscreen,
256 enum pipe_format pfmt,
257 int max,
258 uint64_t *modifiers,
259 unsigned int *external_only,
260 int *count)
261 {
262 struct iris_screen *screen = (void *) pscreen;
263 const struct intel_device_info *devinfo = screen->devinfo;
264
265 uint64_t all_modifiers[] = {
266 DRM_FORMAT_MOD_LINEAR,
267 I915_FORMAT_MOD_X_TILED,
268 I915_FORMAT_MOD_4_TILED,
269 I915_FORMAT_MOD_4_TILED_DG2_RC_CCS,
270 I915_FORMAT_MOD_4_TILED_DG2_MC_CCS,
271 I915_FORMAT_MOD_4_TILED_DG2_RC_CCS_CC,
272 I915_FORMAT_MOD_4_TILED_MTL_RC_CCS,
273 I915_FORMAT_MOD_4_TILED_MTL_RC_CCS_CC,
274 I915_FORMAT_MOD_4_TILED_MTL_MC_CCS,
275 I915_FORMAT_MOD_Y_TILED,
276 I915_FORMAT_MOD_Y_TILED_CCS,
277 I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS,
278 I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS,
279 I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC,
280 };
281
282 int supported_mods = 0;
283
284 for (int i = 0; i < ARRAY_SIZE(all_modifiers); i++) {
285 if (!modifier_is_supported(devinfo, pfmt, 0, all_modifiers[i]))
286 continue;
287
288 if (supported_mods < max) {
289 if (modifiers)
290 modifiers[supported_mods] = all_modifiers[i];
291
292 if (external_only) {
293 external_only[supported_mods] =
294 is_modifier_external_only(pfmt, all_modifiers[i]);
295 }
296 }
297
298 supported_mods++;
299 }
300
301 *count = supported_mods;
302 }
303
304 static bool
iris_is_dmabuf_modifier_supported(struct pipe_screen * pscreen,uint64_t modifier,enum pipe_format pfmt,bool * external_only)305 iris_is_dmabuf_modifier_supported(struct pipe_screen *pscreen,
306 uint64_t modifier, enum pipe_format pfmt,
307 bool *external_only)
308 {
309 struct iris_screen *screen = (void *) pscreen;
310 const struct intel_device_info *devinfo = screen->devinfo;
311
312 if (modifier_is_supported(devinfo, pfmt, 0, modifier)) {
313 if (external_only)
314 *external_only = is_modifier_external_only(pfmt, modifier);
315
316 return true;
317 }
318
319 return false;
320 }
321
322 static unsigned int
iris_get_dmabuf_modifier_planes(struct pipe_screen * pscreen,uint64_t modifier,enum pipe_format format)323 iris_get_dmabuf_modifier_planes(struct pipe_screen *pscreen, uint64_t modifier,
324 enum pipe_format format)
325 {
326 unsigned int planes = util_format_get_num_planes(format);
327
328 switch (modifier) {
329 case I915_FORMAT_MOD_4_TILED_MTL_RC_CCS_CC:
330 case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC:
331 return 3;
332 case I915_FORMAT_MOD_4_TILED_MTL_RC_CCS:
333 case I915_FORMAT_MOD_4_TILED_MTL_MC_CCS:
334 case I915_FORMAT_MOD_4_TILED_DG2_RC_CCS_CC:
335 case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
336 case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
337 case I915_FORMAT_MOD_Y_TILED_CCS:
338 return 2 * planes;
339 case I915_FORMAT_MOD_4_TILED_DG2_RC_CCS:
340 case I915_FORMAT_MOD_4_TILED_DG2_MC_CCS:
341 default:
342 return planes;
343 }
344 }
345
346 enum isl_format
iris_image_view_get_format(struct iris_context * ice,const struct pipe_image_view * img)347 iris_image_view_get_format(struct iris_context *ice,
348 const struct pipe_image_view *img)
349 {
350 struct iris_screen *screen = (struct iris_screen *)ice->ctx.screen;
351 const struct intel_device_info *devinfo = screen->devinfo;
352
353 isl_surf_usage_flags_t usage = ISL_SURF_USAGE_STORAGE_BIT;
354 enum isl_format isl_fmt =
355 iris_format_for_usage(devinfo, img->format, usage).fmt;
356
357 if (img->shader_access & PIPE_IMAGE_ACCESS_READ) {
358 /* On Gfx8, try to use typed surfaces reads (which support a
359 * limited number of formats), and if not possible, fall back
360 * to untyped reads.
361 */
362 if (devinfo->ver == 8 &&
363 !isl_has_matching_typed_storage_image_format(devinfo, isl_fmt))
364 return ISL_FORMAT_RAW;
365 else
366 return isl_lower_storage_image_format(devinfo, isl_fmt);
367 }
368
369 return isl_fmt;
370 }
371
372 static struct pipe_memory_object *
iris_memobj_create_from_handle(struct pipe_screen * pscreen,struct winsys_handle * whandle,bool dedicated)373 iris_memobj_create_from_handle(struct pipe_screen *pscreen,
374 struct winsys_handle *whandle,
375 bool dedicated)
376 {
377 struct iris_screen *screen = (struct iris_screen *)pscreen;
378 struct iris_memory_object *memobj = CALLOC_STRUCT(iris_memory_object);
379 struct iris_bo *bo;
380
381 if (!memobj)
382 return NULL;
383
384 switch (whandle->type) {
385 case WINSYS_HANDLE_TYPE_SHARED:
386 bo = iris_bo_gem_create_from_name(screen->bufmgr, "winsys image",
387 whandle->handle);
388 break;
389 case WINSYS_HANDLE_TYPE_FD:
390 bo = iris_bo_import_dmabuf(screen->bufmgr, whandle->handle,
391 whandle->modifier);
392 break;
393 default:
394 unreachable("invalid winsys handle type");
395 }
396
397 if (!bo) {
398 free(memobj);
399 return NULL;
400 }
401
402 memobj->b.dedicated = dedicated;
403 memobj->bo = bo;
404 memobj->format = whandle->format;
405 memobj->stride = whandle->stride;
406
407 return &memobj->b;
408 }
409
410 static void
iris_memobj_destroy(struct pipe_screen * pscreen,struct pipe_memory_object * pmemobj)411 iris_memobj_destroy(struct pipe_screen *pscreen,
412 struct pipe_memory_object *pmemobj)
413 {
414 struct iris_memory_object *memobj = (struct iris_memory_object *)pmemobj;
415
416 iris_bo_unreference(memobj->bo);
417 free(memobj);
418 }
419
420 struct pipe_resource *
iris_resource_get_separate_stencil(struct pipe_resource * p_res)421 iris_resource_get_separate_stencil(struct pipe_resource *p_res)
422 {
423 /* For packed depth-stencil, we treat depth as the primary resource
424 * and store S8 as the "second plane" resource.
425 */
426 if (p_res->next && p_res->next->format == PIPE_FORMAT_S8_UINT)
427 return p_res->next;
428
429 return NULL;
430
431 }
432
433 static void
iris_resource_set_separate_stencil(struct pipe_resource * p_res,struct pipe_resource * stencil)434 iris_resource_set_separate_stencil(struct pipe_resource *p_res,
435 struct pipe_resource *stencil)
436 {
437 assert(util_format_has_depth(util_format_description(p_res->format)));
438 pipe_resource_reference(&p_res->next, stencil);
439 }
440
441 void
iris_get_depth_stencil_resources(struct pipe_resource * res,struct iris_resource ** out_z,struct iris_resource ** out_s)442 iris_get_depth_stencil_resources(struct pipe_resource *res,
443 struct iris_resource **out_z,
444 struct iris_resource **out_s)
445 {
446 if (!res) {
447 *out_z = NULL;
448 *out_s = NULL;
449 return;
450 }
451
452 if (res->format != PIPE_FORMAT_S8_UINT) {
453 *out_z = (void *) res;
454 *out_s = (void *) iris_resource_get_separate_stencil(res);
455 } else {
456 *out_z = NULL;
457 *out_s = (void *) res;
458 }
459 }
460
461 void
iris_resource_disable_aux(struct iris_resource * res)462 iris_resource_disable_aux(struct iris_resource *res)
463 {
464 iris_bo_unreference(res->aux.bo);
465 iris_bo_unreference(res->aux.clear_color_bo);
466 free(res->aux.state);
467
468 res->aux.usage = ISL_AUX_USAGE_NONE;
469 res->aux.surf.size_B = 0;
470 res->aux.bo = NULL;
471 res->aux.extra_aux.surf.size_B = 0;
472 res->aux.clear_color_bo = NULL;
473 res->aux.state = NULL;
474 }
475
476 static unsigned
iris_resource_alloc_flags(const struct iris_screen * screen,const struct pipe_resource * templ,enum isl_aux_usage aux_usage)477 iris_resource_alloc_flags(const struct iris_screen *screen,
478 const struct pipe_resource *templ,
479 enum isl_aux_usage aux_usage)
480 {
481 if (templ->flags & IRIS_RESOURCE_FLAG_DEVICE_MEM)
482 return BO_ALLOC_PLAIN;
483
484 unsigned flags = BO_ALLOC_PLAIN;
485
486 switch (templ->usage) {
487 case PIPE_USAGE_STAGING:
488 flags |= BO_ALLOC_SMEM | BO_ALLOC_COHERENT;
489 break;
490 case PIPE_USAGE_STREAM:
491 flags |= BO_ALLOC_SMEM;
492 break;
493 case PIPE_USAGE_DYNAMIC:
494 case PIPE_USAGE_DEFAULT:
495 case PIPE_USAGE_IMMUTABLE:
496 /* Use LMEM for these if possible */
497 break;
498 }
499
500 if (templ->bind & PIPE_BIND_SCANOUT)
501 flags |= BO_ALLOC_SCANOUT;
502
503 if (templ->flags & (PIPE_RESOURCE_FLAG_MAP_COHERENT |
504 PIPE_RESOURCE_FLAG_MAP_PERSISTENT))
505 flags |= BO_ALLOC_SMEM | BO_ALLOC_COHERENT;
506
507 if (screen->devinfo->verx10 >= 125 && screen->devinfo->has_local_mem &&
508 isl_aux_usage_has_ccs(aux_usage)) {
509 assert((flags & BO_ALLOC_SMEM) == 0);
510 flags |= BO_ALLOC_LMEM;
511 }
512
513 if ((templ->bind & PIPE_BIND_SHARED) ||
514 util_format_get_num_planes(templ->format) > 1)
515 flags |= BO_ALLOC_NO_SUBALLOC;
516
517 if (templ->bind & PIPE_BIND_PROTECTED)
518 flags |= BO_ALLOC_PROTECTED;
519
520 if (templ->bind & PIPE_BIND_SHARED) {
521 flags |= BO_ALLOC_SHARED;
522
523 /* We request that the bufmgr zero because, if a buffer gets re-used
524 * from the pool, we don't want to leak random garbage from our process
525 * to some other.
526 */
527 flags |= BO_ALLOC_ZEROED;
528 }
529
530 return flags;
531 }
532
533 static void
iris_resource_destroy(struct pipe_screen * screen,struct pipe_resource * p_res)534 iris_resource_destroy(struct pipe_screen *screen,
535 struct pipe_resource *p_res)
536 {
537 struct iris_resource *res = (struct iris_resource *) p_res;
538
539 if (p_res->target == PIPE_BUFFER)
540 util_range_destroy(&res->valid_buffer_range);
541
542 iris_resource_disable_aux(res);
543
544 threaded_resource_deinit(p_res);
545 iris_bo_unreference(res->bo);
546 iris_pscreen_unref(res->orig_screen);
547
548 free(res);
549 }
550
551 static struct iris_resource *
iris_alloc_resource(struct pipe_screen * pscreen,const struct pipe_resource * templ)552 iris_alloc_resource(struct pipe_screen *pscreen,
553 const struct pipe_resource *templ)
554 {
555 struct iris_resource *res = calloc(1, sizeof(struct iris_resource));
556 if (!res)
557 return NULL;
558
559 res->base.b = *templ;
560 res->base.b.screen = pscreen;
561 res->orig_screen = iris_pscreen_ref(pscreen);
562 pipe_reference_init(&res->base.b.reference, 1);
563 threaded_resource_init(&res->base.b, false);
564
565 if (templ->target == PIPE_BUFFER)
566 util_range_init(&res->valid_buffer_range);
567
568 return res;
569 }
570
571 unsigned
iris_get_num_logical_layers(const struct iris_resource * res,unsigned level)572 iris_get_num_logical_layers(const struct iris_resource *res, unsigned level)
573 {
574 if (res->surf.dim == ISL_SURF_DIM_3D)
575 return u_minify(res->surf.logical_level0_px.depth, level);
576 else
577 return res->surf.logical_level0_px.array_len;
578 }
579
580 static enum isl_aux_state **
create_aux_state_map(struct iris_resource * res,enum isl_aux_state initial)581 create_aux_state_map(struct iris_resource *res, enum isl_aux_state initial)
582 {
583 assert(res->aux.state == NULL);
584
585 uint32_t total_slices = 0;
586 for (uint32_t level = 0; level < res->surf.levels; level++)
587 total_slices += iris_get_num_logical_layers(res, level);
588
589 const size_t per_level_array_size =
590 res->surf.levels * sizeof(enum isl_aux_state *);
591
592 /* We're going to allocate a single chunk of data for both the per-level
593 * reference array and the arrays of aux_state. This makes cleanup
594 * significantly easier.
595 */
596 const size_t total_size =
597 per_level_array_size + total_slices * sizeof(enum isl_aux_state);
598
599 void *data = malloc(total_size);
600 if (!data)
601 return NULL;
602
603 enum isl_aux_state **per_level_arr = data;
604 enum isl_aux_state *s = data + per_level_array_size;
605 for (uint32_t level = 0; level < res->surf.levels; level++) {
606 per_level_arr[level] = s;
607 const unsigned level_layers = iris_get_num_logical_layers(res, level);
608 for (uint32_t a = 0; a < level_layers; a++)
609 *(s++) = initial;
610 }
611 assert((void *)s == data + total_size);
612
613 return per_level_arr;
614 }
615
616 static unsigned
iris_get_aux_clear_color_state_size(struct iris_screen * screen,struct iris_resource * res)617 iris_get_aux_clear_color_state_size(struct iris_screen *screen,
618 struct iris_resource *res)
619 {
620 if (!isl_aux_usage_has_fast_clears(res->aux.usage))
621 return 0;
622
623 assert(!isl_surf_usage_is_stencil(res->surf.usage));
624
625 /* Depth packets can't specify indirect clear values. The only time depth
626 * buffers can use indirect clear values is when they're accessed by the
627 * sampler via render surface state objects.
628 */
629 if (isl_surf_usage_is_depth(res->surf.usage) &&
630 !iris_sample_with_depth_aux(screen->devinfo, res))
631 return 0;
632
633 return screen->isl_dev.ss.clear_color_state_size;
634 }
635
636 static void
map_aux_addresses(struct iris_screen * screen,struct iris_resource * res,enum pipe_format pfmt,unsigned plane)637 map_aux_addresses(struct iris_screen *screen, struct iris_resource *res,
638 enum pipe_format pfmt, unsigned plane)
639 {
640 void *aux_map_ctx = iris_bufmgr_get_aux_map_context(screen->bufmgr);
641 if (!aux_map_ctx)
642 return;
643
644 if (isl_aux_usage_has_ccs(res->aux.usage)) {
645 const unsigned aux_offset = res->aux.extra_aux.surf.size_B > 0 ?
646 res->aux.extra_aux.offset : res->aux.offset;
647 const enum isl_format format =
648 iris_format_for_usage(screen->devinfo, pfmt, res->surf.usage).fmt;
649 const uint64_t format_bits =
650 intel_aux_map_format_bits(res->surf.tiling, format, plane);
651 const bool mapped =
652 intel_aux_map_add_mapping(aux_map_ctx,
653 res->bo->address + res->offset,
654 res->aux.bo->address + aux_offset,
655 res->surf.size_B, format_bits);
656 assert(mapped);
657 res->bo->aux_map_address = res->aux.bo->address;
658 }
659 }
660
661 static bool
want_ccs_e_for_format(const struct intel_device_info * devinfo,enum isl_format format)662 want_ccs_e_for_format(const struct intel_device_info *devinfo,
663 enum isl_format format)
664 {
665 if (!isl_format_supports_ccs_e(devinfo, format))
666 return false;
667
668 const struct isl_format_layout *fmtl = isl_format_get_layout(format);
669
670 /* Prior to TGL, CCS_E seems to significantly hurt performance with 32-bit
671 * floating point formats. For example, Paraview's "Wavelet Volume" case
672 * uses both R32_FLOAT and R32G32B32A32_FLOAT, and enabling CCS_E for those
673 * formats causes a 62% FPS drop.
674 *
675 * However, many benchmarks seem to use 16-bit float with no issues.
676 */
677 if (devinfo->ver <= 11 &&
678 fmtl->channels.r.bits == 32 && fmtl->channels.r.type == ISL_SFLOAT)
679 return false;
680
681 return true;
682 }
683
684 static enum isl_surf_dim
target_to_isl_surf_dim(enum pipe_texture_target target)685 target_to_isl_surf_dim(enum pipe_texture_target target)
686 {
687 switch (target) {
688 case PIPE_BUFFER:
689 case PIPE_TEXTURE_1D:
690 case PIPE_TEXTURE_1D_ARRAY:
691 return ISL_SURF_DIM_1D;
692 case PIPE_TEXTURE_2D:
693 case PIPE_TEXTURE_CUBE:
694 case PIPE_TEXTURE_RECT:
695 case PIPE_TEXTURE_2D_ARRAY:
696 case PIPE_TEXTURE_CUBE_ARRAY:
697 return ISL_SURF_DIM_2D;
698 case PIPE_TEXTURE_3D:
699 return ISL_SURF_DIM_3D;
700 case PIPE_MAX_TEXTURE_TYPES:
701 break;
702 }
703 unreachable("invalid texture type");
704 }
705
706 static bool
iris_resource_configure_main(const struct iris_screen * screen,struct iris_resource * res,const struct pipe_resource * templ,uint64_t modifier,uint32_t row_pitch_B)707 iris_resource_configure_main(const struct iris_screen *screen,
708 struct iris_resource *res,
709 const struct pipe_resource *templ,
710 uint64_t modifier, uint32_t row_pitch_B)
711 {
712 res->mod_info = isl_drm_modifier_get_info(modifier);
713
714 if (modifier != DRM_FORMAT_MOD_INVALID && res->mod_info == NULL)
715 return false;
716
717 isl_tiling_flags_t tiling_flags = 0;
718
719 if (res->mod_info != NULL) {
720 tiling_flags = 1 << res->mod_info->tiling;
721 } else if (templ->usage == PIPE_USAGE_STAGING ||
722 templ->bind & (PIPE_BIND_LINEAR | PIPE_BIND_CURSOR)) {
723 tiling_flags = ISL_TILING_LINEAR_BIT;
724 } else if (res->external_format != PIPE_FORMAT_NONE) {
725 /* This came from iris_resource_from_memobj and didn't have
726 * PIPE_BIND_LINEAR set, so "optimal" tiling is desired. Let isl
727 * select the tiling. The implicit contract is that both drivers
728 * will arrive at the same tiling by using the same code to decide.
729 */
730 assert(modifier == DRM_FORMAT_MOD_INVALID);
731 tiling_flags = ISL_TILING_ANY_MASK;
732 } else if (!screen->devinfo->has_tiling_uapi &&
733 (templ->bind & (PIPE_BIND_SCANOUT | PIPE_BIND_SHARED))) {
734 tiling_flags = ISL_TILING_LINEAR_BIT;
735 } else if (templ->bind & PIPE_BIND_SCANOUT) {
736 tiling_flags = ISL_TILING_X_BIT;
737 } else {
738 tiling_flags = ISL_TILING_ANY_MASK;
739 }
740
741 /* We don't support Yf or Ys tiling yet */
742 tiling_flags &= ~ISL_TILING_STD_Y_MASK;
743 assert(tiling_flags != 0);
744
745 isl_surf_usage_flags_t usage = 0;
746
747 if (res->mod_info && !isl_drm_modifier_has_aux(modifier))
748 usage |= ISL_SURF_USAGE_DISABLE_AUX_BIT;
749 else if (templ->bind & PIPE_BIND_CONST_BW)
750 usage |= ISL_SURF_USAGE_DISABLE_AUX_BIT;
751
752 if (templ->usage == PIPE_USAGE_STAGING)
753 usage |= ISL_SURF_USAGE_STAGING_BIT;
754
755 if (templ->bind & PIPE_BIND_RENDER_TARGET)
756 usage |= ISL_SURF_USAGE_RENDER_TARGET_BIT;
757
758 if (templ->bind & PIPE_BIND_SAMPLER_VIEW)
759 usage |= ISL_SURF_USAGE_TEXTURE_BIT;
760
761 if (templ->bind & PIPE_BIND_SHADER_IMAGE)
762 usage |= ISL_SURF_USAGE_STORAGE_BIT;
763
764 if (templ->bind & PIPE_BIND_SCANOUT)
765 usage |= ISL_SURF_USAGE_DISPLAY_BIT;
766
767 if (templ->target == PIPE_TEXTURE_CUBE ||
768 templ->target == PIPE_TEXTURE_CUBE_ARRAY) {
769 usage |= ISL_SURF_USAGE_CUBE_BIT;
770 }
771
772 if (templ->usage != PIPE_USAGE_STAGING &&
773 util_format_is_depth_or_stencil(templ->format)) {
774
775 /* Should be handled by u_transfer_helper */
776 assert(!util_format_is_depth_and_stencil(templ->format));
777
778 usage |= templ->format == PIPE_FORMAT_S8_UINT ?
779 ISL_SURF_USAGE_STENCIL_BIT : ISL_SURF_USAGE_DEPTH_BIT;
780 }
781
782 const enum isl_format format =
783 iris_format_for_usage(screen->devinfo, templ->format, usage).fmt;
784
785 const struct isl_surf_init_info init_info = {
786 .dim = target_to_isl_surf_dim(templ->target),
787 .format = format,
788 .width = templ->width0,
789 .height = templ->height0,
790 .depth = templ->depth0,
791 .levels = templ->last_level + 1,
792 .array_len = templ->array_size,
793 .samples = MAX2(templ->nr_samples, 1),
794 .min_alignment_B = 0,
795 .row_pitch_B = row_pitch_B,
796 .usage = usage,
797 .tiling_flags = tiling_flags
798 };
799
800 if (!isl_surf_init_s(&screen->isl_dev, &res->surf, &init_info))
801 return false;
802
803 res->internal_format = templ->format;
804
805 return true;
806 }
807
808 static bool
iris_get_ccs_surf_or_support(const struct isl_device * dev,const struct isl_surf * surf,struct isl_surf * aux_surf,struct isl_surf * extra_aux_surf)809 iris_get_ccs_surf_or_support(const struct isl_device *dev,
810 const struct isl_surf *surf,
811 struct isl_surf *aux_surf,
812 struct isl_surf *extra_aux_surf)
813 {
814 assert(extra_aux_surf->size_B == 0);
815
816 struct isl_surf *ccs_surf;
817 const struct isl_surf *hiz_or_mcs_surf;
818 if (aux_surf->size_B > 0) {
819 assert(aux_surf->usage & (ISL_SURF_USAGE_HIZ_BIT |
820 ISL_SURF_USAGE_MCS_BIT));
821 hiz_or_mcs_surf = aux_surf;
822 ccs_surf = extra_aux_surf;
823 } else {
824 hiz_or_mcs_surf = NULL;
825 ccs_surf = aux_surf;
826 }
827
828 if (dev->info->has_flat_ccs) {
829 /* CCS doesn't require VMA on XeHP. So, instead of creating a separate
830 * surface, we can just return whether CCS is supported for the given
831 * input surfaces.
832 */
833 return isl_surf_supports_ccs(dev, surf, hiz_or_mcs_surf);
834 } else {
835 return isl_surf_get_ccs_surf(dev, surf, hiz_or_mcs_surf, ccs_surf, 0);
836 }
837 }
838
839 /**
840 * Configure aux for the resource, but don't allocate it. For images which
841 * might be shared with modifiers, we must allocate the image and aux data in
842 * a single bo.
843 *
844 * Returns false on unexpected error (e.g. allocation failed, or invalid
845 * configuration result).
846 */
847 static bool
iris_resource_configure_aux(struct iris_screen * screen,struct iris_resource * res,bool imported)848 iris_resource_configure_aux(struct iris_screen *screen,
849 struct iris_resource *res, bool imported)
850 {
851 const struct intel_device_info *devinfo = screen->devinfo;
852
853 const bool has_mcs =
854 isl_surf_get_mcs_surf(&screen->isl_dev, &res->surf, &res->aux.surf);
855
856 const bool has_hiz =
857 isl_surf_get_hiz_surf(&screen->isl_dev, &res->surf, &res->aux.surf);
858
859 const bool has_ccs =
860 iris_get_ccs_surf_or_support(&screen->isl_dev, &res->surf,
861 &res->aux.surf, &res->aux.extra_aux.surf);
862
863 if (has_mcs) {
864 assert(!res->mod_info);
865 assert(!has_hiz);
866 /* We are seeing failures with CCS compression on top of MSAA
867 * compression, so just enable MSAA compression for now on DG2.
868 */
869 if (!intel_device_info_is_dg2(devinfo) && has_ccs) {
870 res->aux.usage = ISL_AUX_USAGE_MCS_CCS;
871 } else {
872 res->aux.usage = ISL_AUX_USAGE_MCS;
873 }
874 } else if (has_hiz) {
875 assert(!res->mod_info);
876 assert(!has_mcs);
877 if (!has_ccs) {
878 res->aux.usage = ISL_AUX_USAGE_HIZ;
879 } else if (res->surf.samples == 1 &&
880 (res->surf.usage & ISL_SURF_USAGE_TEXTURE_BIT)) {
881 /* If this resource is single-sampled and will be used as a texture,
882 * put the HiZ surface in write-through mode so that we can sample
883 * from it.
884 */
885 res->aux.usage = ISL_AUX_USAGE_HIZ_CCS_WT;
886 } else {
887 res->aux.usage = ISL_AUX_USAGE_HIZ_CCS;
888 }
889 } else if (has_ccs) {
890 if (isl_surf_usage_is_stencil(res->surf.usage)) {
891 assert(!res->mod_info);
892 res->aux.usage = ISL_AUX_USAGE_STC_CCS;
893 } else if (res->mod_info && res->mod_info->supports_media_compression) {
894 res->aux.usage = ISL_AUX_USAGE_MC;
895 } else if (want_ccs_e_for_format(devinfo, res->surf.format)) {
896 res->aux.usage = devinfo->ver < 12 ?
897 ISL_AUX_USAGE_CCS_E : ISL_AUX_USAGE_FCV_CCS_E;
898 } else {
899 assert(isl_format_supports_ccs_d(devinfo, res->surf.format));
900 res->aux.usage = ISL_AUX_USAGE_CCS_D;
901 }
902 }
903
904 enum isl_aux_state initial_state;
905 switch (res->aux.usage) {
906 case ISL_AUX_USAGE_NONE:
907 /* Having no aux buffer is only okay if there's no modifier with aux. */
908 return !res->mod_info ||
909 !isl_drm_modifier_has_aux(res->mod_info->modifier);
910 case ISL_AUX_USAGE_HIZ:
911 case ISL_AUX_USAGE_HIZ_CCS:
912 case ISL_AUX_USAGE_HIZ_CCS_WT:
913 case ISL_AUX_USAGE_MCS:
914 case ISL_AUX_USAGE_MCS_CCS:
915 /* Leave the auxiliary buffer uninitialized. We can ambiguate it before
916 * accessing it later on, if needed.
917 */
918 initial_state = ISL_AUX_STATE_AUX_INVALID;
919 break;
920 case ISL_AUX_USAGE_CCS_D:
921 case ISL_AUX_USAGE_CCS_E:
922 case ISL_AUX_USAGE_FCV_CCS_E:
923 case ISL_AUX_USAGE_STC_CCS:
924 case ISL_AUX_USAGE_MC:
925 if (imported) {
926 assert(res->aux.usage != ISL_AUX_USAGE_STC_CCS);
927 initial_state =
928 isl_drm_modifier_get_default_aux_state(res->mod_info->modifier);
929 } else if (devinfo->has_flat_ccs) {
930 assert(res->aux.surf.size_B == 0);
931 /* From Bspec 47709, "MCS/CCS Buffers for Render Target(s)":
932 *
933 * "CCS surface does not require initialization. Illegal CCS
934 * [values] are treated as uncompressed memory."
935 *
936 * The above quote is from the render target section, but we assume
937 * it applies to CCS in general (e.g., STC_CCS). The uninitialized
938 * CCS may be in any aux state. We choose the one which is most
939 * convenient.
940 *
941 * We avoid states with CLEAR because stencil does not support it.
942 * Those states also create a dependency on the clear color, which
943 * can have negative performance implications. Even though some
944 * blocks may actually be encoded with CLEAR, we can get away with
945 * ignoring them - there are no known issues that require fast
946 * cleared blocks to be tracked and avoided.
947 *
948 * We specifically avoid the AUX_INVALID state because it could
949 * trigger an ambiguate. BLORP does not have support for ambiguating
950 * stencil. Also, ambiguating some LODs of mipmapped 8bpp surfaces
951 * seems to stomp on neighboring miplevels.
952 *
953 * There is only one remaining aux state which can give us correct
954 * behavior, COMPRESSED_NO_CLEAR.
955 */
956 initial_state = ISL_AUX_STATE_COMPRESSED_NO_CLEAR;
957 } else {
958 assert(res->aux.surf.size_B > 0);
959 /* When CCS is used, we need to ensure that it starts off in a valid
960 * state. From the Sky Lake PRM, "MCS Buffer for Render Target(s)":
961 *
962 * "If Software wants to enable Color Compression without Fast
963 * clear, Software needs to initialize MCS with zeros."
964 *
965 * A CCS surface initialized to zero is in the pass-through state.
966 * This state can avoid the need to ambiguate in some cases. We'll
967 * map and zero the CCS later on in iris_resource_init_aux_buf.
968 */
969 initial_state = ISL_AUX_STATE_PASS_THROUGH;
970 }
971 break;
972 default:
973 unreachable("Unsupported aux mode");
974 }
975
976 /* Create the aux_state for the auxiliary buffer. */
977 res->aux.state = create_aux_state_map(res, initial_state);
978 if (!res->aux.state)
979 return false;
980
981 return true;
982 }
983
984 /**
985 * Initialize the aux buffer contents.
986 *
987 * Returns false on unexpected error (e.g. mapping a BO failed).
988 */
989 static bool
iris_resource_init_aux_buf(struct iris_screen * screen,struct iris_resource * res)990 iris_resource_init_aux_buf(struct iris_screen *screen,
991 struct iris_resource *res)
992 {
993 void *map = NULL;
994
995 if (iris_resource_get_aux_state(res, 0, 0) != ISL_AUX_STATE_AUX_INVALID &&
996 res->aux.surf.size_B > 0) {
997 if (!map)
998 map = iris_bo_map(NULL, res->bo, MAP_WRITE | MAP_RAW);
999 if (!map)
1000 return false;
1001
1002 memset((char*)map + res->aux.offset, 0, res->aux.surf.size_B);
1003 }
1004
1005 if (res->aux.extra_aux.surf.size_B > 0) {
1006 if (!map)
1007 map = iris_bo_map(NULL, res->bo, MAP_WRITE | MAP_RAW);
1008 if (!map)
1009 return false;
1010
1011 memset((char*)map + res->aux.extra_aux.offset,
1012 0, res->aux.extra_aux.surf.size_B);
1013 }
1014
1015 if (map)
1016 iris_bo_unmap(res->bo);
1017
1018 if (res->aux.surf.size_B > 0) {
1019 res->aux.bo = res->bo;
1020 iris_bo_reference(res->aux.bo);
1021 map_aux_addresses(screen, res, res->internal_format, 0);
1022 }
1023
1024 if (iris_get_aux_clear_color_state_size(screen, res) > 0) {
1025 res->aux.clear_color_bo = res->bo;
1026 iris_bo_reference(res->aux.clear_color_bo);
1027 res->aux.clear_color_unknown = !res->aux.clear_color_bo->zeroed;
1028 }
1029
1030 return true;
1031 }
1032
1033 static uint32_t
iris_buffer_alignment(uint64_t size)1034 iris_buffer_alignment(uint64_t size)
1035 {
1036 /* Some buffer operations want some amount of alignment. The largest
1037 * buffer texture pixel size is 4 * 4 = 16B. OpenCL data is also supposed
1038 * to be aligned and largest OpenCL data type is a double16 which is
1039 * 8 * 16 = 128B. Align to the largest power of 2 which fits in the size,
1040 * up to 128B.
1041 */
1042 uint32_t align = MAX2(4 * 4, 8 * 16);
1043 while (align > size)
1044 align >>= 1;
1045
1046 return align;
1047 }
1048
1049 static struct pipe_resource *
iris_resource_create_for_buffer(struct pipe_screen * pscreen,const struct pipe_resource * templ)1050 iris_resource_create_for_buffer(struct pipe_screen *pscreen,
1051 const struct pipe_resource *templ)
1052 {
1053 struct iris_screen *screen = (struct iris_screen *)pscreen;
1054 struct iris_resource *res = iris_alloc_resource(pscreen, templ);
1055
1056 assert(templ->target == PIPE_BUFFER);
1057 assert(templ->height0 <= 1);
1058 assert(templ->depth0 <= 1);
1059 assert(templ->format == PIPE_FORMAT_NONE ||
1060 util_format_get_blocksize(templ->format) == 1);
1061
1062 res->internal_format = templ->format;
1063 res->surf.tiling = ISL_TILING_LINEAR;
1064
1065 enum iris_memory_zone memzone = IRIS_MEMZONE_OTHER;
1066 const char *name = templ->target == PIPE_BUFFER ? "buffer" : "miptree";
1067 if (templ->flags & IRIS_RESOURCE_FLAG_SHADER_MEMZONE) {
1068 memzone = IRIS_MEMZONE_SHADER;
1069 name = "shader kernels";
1070 } else if (templ->flags & IRIS_RESOURCE_FLAG_SURFACE_MEMZONE) {
1071 memzone = IRIS_MEMZONE_SURFACE;
1072 name = "surface state";
1073 } else if (templ->flags & IRIS_RESOURCE_FLAG_DYNAMIC_MEMZONE) {
1074 memzone = IRIS_MEMZONE_DYNAMIC;
1075 name = "dynamic state";
1076 } else if (templ->flags & IRIS_RESOURCE_FLAG_SCRATCH_MEMZONE) {
1077 memzone = IRIS_MEMZONE_SCRATCH;
1078 name = "scratch surface state";
1079 }
1080
1081 unsigned flags = iris_resource_alloc_flags(screen, templ, res->aux.usage);
1082
1083 res->bo = iris_bo_alloc(screen->bufmgr, name, templ->width0,
1084 iris_buffer_alignment(templ->width0),
1085 memzone, flags);
1086
1087 if (!res->bo) {
1088 iris_resource_destroy(pscreen, &res->base.b);
1089 return NULL;
1090 }
1091
1092 if (templ->bind & PIPE_BIND_SHARED) {
1093 iris_bo_mark_exported(res->bo);
1094 res->base.is_shared = true;
1095 }
1096
1097 return &res->base.b;
1098 }
1099
1100 static struct pipe_resource *
iris_resource_create_for_image(struct pipe_screen * pscreen,const struct pipe_resource * templ,const uint64_t * modifiers,int modifiers_count,unsigned row_pitch_B)1101 iris_resource_create_for_image(struct pipe_screen *pscreen,
1102 const struct pipe_resource *templ,
1103 const uint64_t *modifiers,
1104 int modifiers_count,
1105 unsigned row_pitch_B)
1106 {
1107 struct iris_screen *screen = (struct iris_screen *)pscreen;
1108 const struct intel_device_info *devinfo = screen->devinfo;
1109 struct iris_resource *res = iris_alloc_resource(pscreen, templ);
1110
1111 if (!res)
1112 return NULL;
1113
1114 uint64_t modifier =
1115 select_best_modifier(devinfo, templ, modifiers, modifiers_count);
1116
1117 if (modifier == DRM_FORMAT_MOD_INVALID && modifiers_count > 0) {
1118 fprintf(stderr, "Unsupported modifier, resource creation failed.\n");
1119 goto fail;
1120 }
1121
1122 const bool isl_surf_created_successfully =
1123 iris_resource_configure_main(screen, res, templ, modifier, row_pitch_B);
1124 if (!isl_surf_created_successfully)
1125 goto fail;
1126
1127 /* Don't create staging surfaces that will use over half the sram,
1128 * since staging implies you are copying data to another resource that's
1129 * at least as large, and then both wouldn't fit in system memory.
1130 *
1131 * Skip this for discrete cards, as the destination buffer might be in
1132 * device local memory while the staging buffer would be in system memory,
1133 * so both would fit.
1134 */
1135 if (templ->usage == PIPE_USAGE_STAGING && !devinfo->has_local_mem &&
1136 res->surf.size_B > (iris_bufmgr_sram_size(screen->bufmgr) / 2))
1137 goto fail;
1138
1139 if (!iris_resource_configure_aux(screen, res, false))
1140 goto fail;
1141
1142 const char *name = "miptree";
1143 enum iris_memory_zone memzone = IRIS_MEMZONE_OTHER;
1144
1145 unsigned flags = iris_resource_alloc_flags(screen, templ, res->aux.usage);
1146
1147 /* These are for u_upload_mgr buffers only */
1148 assert(!(templ->flags & (IRIS_RESOURCE_FLAG_SHADER_MEMZONE |
1149 IRIS_RESOURCE_FLAG_SURFACE_MEMZONE |
1150 IRIS_RESOURCE_FLAG_DYNAMIC_MEMZONE |
1151 IRIS_RESOURCE_FLAG_SCRATCH_MEMZONE)));
1152
1153 /* Modifiers require the aux data to be in the same buffer as the main
1154 * surface, but we combine them even when a modifier is not being used.
1155 */
1156 uint64_t bo_size = res->surf.size_B;
1157
1158 /* Allocate space for the aux buffer. */
1159 if (res->aux.surf.size_B > 0) {
1160 res->aux.offset = (uint32_t)align64(bo_size, res->aux.surf.alignment_B);
1161 bo_size = res->aux.offset + res->aux.surf.size_B;
1162 }
1163
1164 /* Allocate space for the extra aux buffer. */
1165 if (res->aux.extra_aux.surf.size_B > 0) {
1166 res->aux.extra_aux.offset =
1167 (uint32_t)align64(bo_size, res->aux.extra_aux.surf.alignment_B);
1168 bo_size = res->aux.extra_aux.offset + res->aux.extra_aux.surf.size_B;
1169 }
1170
1171 /* Allocate space for the indirect clear color.
1172 *
1173 * Also add some padding to make sure the fast clear color state buffer
1174 * starts at a 4K alignment. We believe that 256B might be enough, but due
1175 * to lack of testing we will leave this as 4K for now.
1176 */
1177 if (iris_get_aux_clear_color_state_size(screen, res) > 0) {
1178 res->aux.clear_color_offset = align64(bo_size, 4096);
1179 bo_size = res->aux.clear_color_offset +
1180 iris_get_aux_clear_color_state_size(screen, res);
1181 }
1182
1183 /* The ISL alignment already includes AUX-TT requirements, so no additional
1184 * attention required here :)
1185 */
1186 uint32_t alignment = MAX2(4096, res->surf.alignment_B);
1187 res->bo =
1188 iris_bo_alloc(screen->bufmgr, name, bo_size, alignment, memzone, flags);
1189
1190 if (!res->bo)
1191 goto fail;
1192
1193 if (res->aux.usage != ISL_AUX_USAGE_NONE &&
1194 !iris_resource_init_aux_buf(screen, res))
1195 goto fail;
1196
1197 if (templ->bind & PIPE_BIND_SHARED) {
1198 iris_bo_mark_exported(res->bo);
1199 res->base.is_shared = true;
1200 }
1201
1202 return &res->base.b;
1203
1204 fail:
1205 iris_resource_destroy(pscreen, &res->base.b);
1206 return NULL;
1207 }
1208
1209 static struct pipe_resource *
iris_resource_create_with_modifiers(struct pipe_screen * pscreen,const struct pipe_resource * templ,const uint64_t * modifiers,int modifier_count)1210 iris_resource_create_with_modifiers(struct pipe_screen *pscreen,
1211 const struct pipe_resource *templ,
1212 const uint64_t *modifiers,
1213 int modifier_count)
1214 {
1215 return iris_resource_create_for_image(pscreen, templ, modifiers,
1216 modifier_count, 0);
1217 }
1218
1219 static struct pipe_resource *
iris_resource_create(struct pipe_screen * pscreen,const struct pipe_resource * templ)1220 iris_resource_create(struct pipe_screen *pscreen,
1221 const struct pipe_resource *templ)
1222 {
1223 if (templ->target == PIPE_BUFFER)
1224 return iris_resource_create_for_buffer(pscreen, templ);
1225 else
1226 return iris_resource_create_with_modifiers(pscreen, templ, NULL, 0);
1227 }
1228
1229 static uint64_t
tiling_to_modifier(uint32_t tiling)1230 tiling_to_modifier(uint32_t tiling)
1231 {
1232 static const uint64_t map[] = {
1233 [I915_TILING_NONE] = DRM_FORMAT_MOD_LINEAR,
1234 [I915_TILING_X] = I915_FORMAT_MOD_X_TILED,
1235 [I915_TILING_Y] = I915_FORMAT_MOD_Y_TILED,
1236 };
1237
1238 assert(tiling < ARRAY_SIZE(map));
1239
1240 return map[tiling];
1241 }
1242
1243 static struct pipe_resource *
iris_resource_from_user_memory(struct pipe_screen * pscreen,const struct pipe_resource * templ,void * user_memory)1244 iris_resource_from_user_memory(struct pipe_screen *pscreen,
1245 const struct pipe_resource *templ,
1246 void *user_memory)
1247 {
1248 struct iris_screen *screen = (struct iris_screen *)pscreen;
1249 struct iris_bufmgr *bufmgr = screen->bufmgr;
1250 struct iris_resource *res = iris_alloc_resource(pscreen, templ);
1251 if (!res)
1252 return NULL;
1253
1254 if (templ->target != PIPE_BUFFER &&
1255 templ->target != PIPE_TEXTURE_1D &&
1256 templ->target != PIPE_TEXTURE_2D)
1257 return NULL;
1258
1259 if (templ->array_size > 1)
1260 return NULL;
1261
1262 size_t res_size = templ->width0;
1263 if (templ->target != PIPE_BUFFER) {
1264 const uint32_t row_pitch_B =
1265 templ->width0 * util_format_get_blocksize(templ->format);
1266 res_size = templ->height0 * row_pitch_B;
1267
1268 if (!iris_resource_configure_main(screen, res, templ,
1269 DRM_FORMAT_MOD_LINEAR,
1270 row_pitch_B)) {
1271 iris_resource_destroy(pscreen, &res->base.b);
1272 return NULL;
1273 }
1274 assert(res->surf.size_B <= res_size);
1275 }
1276
1277 /* The userptr ioctl only works on whole pages. Because we know that
1278 * things will exist in memory at a page granularity, we can expand the
1279 * range given by the client into the whole number of pages and use an
1280 * offset on the resource to make it looks like it starts at the user's
1281 * pointer.
1282 */
1283 size_t page_size = getpagesize();
1284 assert(util_is_power_of_two_nonzero_uintptr(page_size));
1285 size_t offset = (uintptr_t)user_memory & (page_size - 1);
1286 void *mem_start = (char *)user_memory - offset;
1287 size_t mem_size = offset + res_size;
1288 mem_size = ALIGN_NPOT(mem_size, page_size);
1289
1290 res->internal_format = templ->format;
1291 res->base.is_user_ptr = true;
1292 res->bo = iris_bo_create_userptr(bufmgr, "user", mem_start, mem_size,
1293 IRIS_MEMZONE_OTHER);
1294 res->offset = offset;
1295 if (!res->bo) {
1296 iris_resource_destroy(pscreen, &res->base.b);
1297 return NULL;
1298 }
1299
1300 util_range_add(&res->base.b, &res->valid_buffer_range, 0, templ->width0);
1301
1302 return &res->base.b;
1303 }
1304
1305 static unsigned
get_num_planes(const struct pipe_resource * resource)1306 get_num_planes(const struct pipe_resource *resource)
1307 {
1308 unsigned count = 0;
1309 for (const struct pipe_resource *cur = resource; cur; cur = cur->next)
1310 count++;
1311
1312 return count;
1313 }
1314
1315 static unsigned
get_main_plane_for_plane(enum pipe_format format,unsigned plane)1316 get_main_plane_for_plane(enum pipe_format format,
1317 unsigned plane)
1318 {
1319 if (format == PIPE_FORMAT_NONE) {
1320 /* Created dmabuf resources have this format. */
1321 return 0;
1322 } else if (isl_format_for_pipe_format(format) == ISL_FORMAT_UNSUPPORTED) {
1323 /* This format has been lowered to more planes than are native to it.
1324 * So, compression modifiers are not enabled and the plane index is used
1325 * as-is.
1326 */
1327 return plane;
1328 } else {
1329 unsigned int n_planes = util_format_get_num_planes(format);
1330 return plane % n_planes;
1331 }
1332 }
1333
1334 static struct pipe_resource *
iris_resource_from_handle(struct pipe_screen * pscreen,const struct pipe_resource * templ,struct winsys_handle * whandle,unsigned usage)1335 iris_resource_from_handle(struct pipe_screen *pscreen,
1336 const struct pipe_resource *templ,
1337 struct winsys_handle *whandle,
1338 unsigned usage)
1339 {
1340 struct iris_screen *screen = (struct iris_screen *)pscreen;
1341 const struct intel_device_info *devinfo = screen->devinfo;
1342 struct iris_bufmgr *bufmgr = screen->bufmgr;
1343
1344 /* The gallium dri layer creates a pipe resource for each plane specified
1345 * by the format and modifier. Once all planes are present, we will merge
1346 * the separate parameters into the iris_resource(s) for the main plane(s).
1347 * Save the modifier import information now to reconstruct later.
1348 */
1349 struct iris_resource *res = iris_alloc_resource(pscreen, templ);
1350 if (!res)
1351 return NULL;
1352
1353 switch (whandle->type) {
1354 case WINSYS_HANDLE_TYPE_FD:
1355 res->bo = iris_bo_import_dmabuf(bufmgr, whandle->handle,
1356 whandle->modifier);
1357 break;
1358 case WINSYS_HANDLE_TYPE_SHARED:
1359 res->bo = iris_bo_gem_create_from_name(bufmgr, "winsys image",
1360 whandle->handle);
1361 break;
1362 default:
1363 unreachable("invalid winsys handle type");
1364 }
1365 if (!res->bo)
1366 goto fail;
1367
1368 res->offset = whandle->offset;
1369 res->surf.row_pitch_B = whandle->stride;
1370
1371 if (whandle->plane == 0) {
1372 /* All planes are present. Fill out the main plane resource(s). */
1373 for (unsigned plane = 0; plane < util_resource_num(templ); plane++) {
1374 const unsigned main_plane =
1375 get_main_plane_for_plane(whandle->format, plane);
1376 struct iris_resource *main_res = (struct iris_resource *)
1377 util_resource_at_index(&res->base.b, main_plane);
1378 const struct iris_resource *plane_res = (struct iris_resource *)
1379 util_resource_at_index(&res->base.b, plane);
1380
1381 if (isl_drm_modifier_plane_is_clear_color(whandle->modifier,
1382 plane)) {
1383 /* Fill out the clear color fields. */
1384 assert(plane_res->bo->size >= plane_res->offset +
1385 screen->isl_dev.ss.clear_color_state_size);
1386
1387 iris_bo_reference(plane_res->bo);
1388 main_res->aux.clear_color_bo = plane_res->bo;
1389 main_res->aux.clear_color_offset = plane_res->offset;
1390 main_res->aux.clear_color_unknown = true;
1391 } else if (plane > main_plane) {
1392 /* Fill out some aux surface fields. */
1393 assert(isl_drm_modifier_has_aux(whandle->modifier));
1394 assert(!devinfo->has_flat_ccs);
1395 assert(plane_res->bo->size >= plane_res->offset +
1396 main_res->aux.surf.size_B);
1397 assert(main_res->aux.surf.row_pitch_B ==
1398 plane_res->surf.row_pitch_B);
1399
1400 iris_bo_reference(plane_res->bo);
1401 main_res->aux.bo = plane_res->bo;
1402 main_res->aux.offset = plane_res->offset;
1403 map_aux_addresses(screen, main_res, whandle->format, main_plane);
1404 } else {
1405 /* Fill out fields that are convenient to initialize now. */
1406 assert(plane == main_plane);
1407
1408 main_res->external_format = whandle->format;
1409
1410 if (templ->target == PIPE_BUFFER) {
1411 main_res->surf.tiling = ISL_TILING_LINEAR;
1412 return &main_res->base.b;
1413 }
1414
1415 uint64_t modifier;
1416 if (whandle->modifier == DRM_FORMAT_MOD_INVALID) {
1417 /* We have no modifier; match whatever GEM_GET_TILING says */
1418 uint32_t tiling;
1419 iris_gem_get_tiling(main_res->bo, &tiling);
1420 modifier = tiling_to_modifier(tiling);
1421 } else {
1422 modifier = whandle->modifier;
1423 }
1424
1425 const bool isl_surf_created_successfully =
1426 iris_resource_configure_main(screen, main_res,
1427 &main_res->base.b, modifier,
1428 main_res->surf.row_pitch_B);
1429 if (!isl_surf_created_successfully)
1430 goto fail;
1431
1432 assert(main_res->bo->size >= main_res->offset +
1433 main_res->surf.size_B);
1434
1435 if (!iris_resource_configure_aux(screen, main_res, true))
1436 goto fail;
1437
1438 /* Add on a clear color BO if needed.
1439 *
1440 * Also add some padding to make sure the fast clear color state
1441 * buffer starts at a 4K alignment to avoid some unknown issues.
1442 * See the matching comment in iris_resource_create_for_image().
1443 */
1444 if (!main_res->mod_info->supports_clear_color &&
1445 iris_get_aux_clear_color_state_size(screen, main_res) > 0) {
1446 main_res->aux.clear_color_bo =
1447 iris_bo_alloc(screen->bufmgr, "clear color buffer",
1448 screen->isl_dev.ss.clear_color_state_size,
1449 4096, IRIS_MEMZONE_OTHER, BO_ALLOC_ZEROED);
1450 if (!main_res->aux.clear_color_bo)
1451 goto fail;
1452 }
1453 }
1454 }
1455 }
1456
1457 return &res->base.b;
1458
1459 fail:
1460 iris_resource_destroy(pscreen, &res->base.b);
1461 return NULL;
1462 }
1463
1464 static struct pipe_resource *
iris_resource_from_memobj(struct pipe_screen * pscreen,const struct pipe_resource * templ,struct pipe_memory_object * pmemobj,uint64_t offset)1465 iris_resource_from_memobj(struct pipe_screen *pscreen,
1466 const struct pipe_resource *templ,
1467 struct pipe_memory_object *pmemobj,
1468 uint64_t offset)
1469 {
1470 struct iris_screen *screen = (struct iris_screen *)pscreen;
1471 struct iris_memory_object *memobj = (struct iris_memory_object *)pmemobj;
1472 struct iris_resource *res = iris_alloc_resource(pscreen, templ);
1473
1474 if (!res)
1475 return NULL;
1476
1477 res->bo = memobj->bo;
1478 res->offset = offset;
1479 res->external_format = templ->format;
1480 res->internal_format = templ->format;
1481
1482 if (templ->flags & PIPE_RESOURCE_FLAG_TEXTURING_MORE_LIKELY) {
1483 UNUSED const bool isl_surf_created_successfully =
1484 iris_resource_configure_main(screen, res, templ, DRM_FORMAT_MOD_INVALID, 0);
1485 assert(isl_surf_created_successfully);
1486 }
1487
1488 iris_bo_reference(memobj->bo);
1489
1490 return &res->base.b;
1491 }
1492
1493 /* Handle combined depth/stencil with memory objects.
1494 *
1495 * This function is modeled after u_transfer_helper_resource_create.
1496 */
1497 static struct pipe_resource *
iris_resource_from_memobj_wrapper(struct pipe_screen * pscreen,const struct pipe_resource * templ,struct pipe_memory_object * pmemobj,uint64_t offset)1498 iris_resource_from_memobj_wrapper(struct pipe_screen *pscreen,
1499 const struct pipe_resource *templ,
1500 struct pipe_memory_object *pmemobj,
1501 uint64_t offset)
1502 {
1503 enum pipe_format format = templ->format;
1504
1505 /* Normal case, no special handling: */
1506 if (!(util_format_is_depth_and_stencil(format)))
1507 return iris_resource_from_memobj(pscreen, templ, pmemobj, offset);
1508
1509 struct pipe_resource t = *templ;
1510 t.format = util_format_get_depth_only(format);
1511
1512 struct pipe_resource *prsc =
1513 iris_resource_from_memobj(pscreen, &t, pmemobj, offset);
1514 if (!prsc)
1515 return NULL;
1516
1517 struct iris_resource *res = (struct iris_resource *) prsc;
1518
1519 /* Stencil offset in the buffer without aux. */
1520 uint64_t s_offset = offset +
1521 align64(res->surf.size_B, res->surf.alignment_B);
1522
1523 prsc->format = format; /* frob the format back to the "external" format */
1524
1525 t.format = PIPE_FORMAT_S8_UINT;
1526 struct pipe_resource *stencil =
1527 iris_resource_from_memobj(pscreen, &t, pmemobj, s_offset);
1528 if (!stencil) {
1529 iris_resource_destroy(pscreen, prsc);
1530 return NULL;
1531 }
1532
1533 iris_resource_set_separate_stencil(prsc, stencil);
1534 return prsc;
1535 }
1536
1537 static void
iris_flush_resource(struct pipe_context * ctx,struct pipe_resource * resource)1538 iris_flush_resource(struct pipe_context *ctx, struct pipe_resource *resource)
1539 {
1540 struct iris_context *ice = (struct iris_context *)ctx;
1541 struct iris_resource *res = (void *) resource;
1542 const struct isl_drm_modifier_info *mod = res->mod_info;
1543
1544 iris_resource_prepare_access(ice, res,
1545 0, INTEL_REMAINING_LEVELS,
1546 0, INTEL_REMAINING_LAYERS,
1547 mod ? res->aux.usage : ISL_AUX_USAGE_NONE,
1548 mod ? mod->supports_clear_color : false);
1549
1550 if (!res->mod_info && res->aux.usage != ISL_AUX_USAGE_NONE) {
1551 /* flush_resource may be used to prepare an image for sharing external
1552 * to the driver (e.g. via eglCreateImage). To account for this, make
1553 * sure to get rid of any compression that a consumer wouldn't know how
1554 * to handle.
1555 */
1556 iris_foreach_batch(ice, batch) {
1557 if (iris_batch_references(batch, res->bo))
1558 iris_batch_flush(batch);
1559 }
1560
1561 iris_resource_disable_aux(res);
1562 }
1563 }
1564
1565 /**
1566 * Reallocate a (non-external) resource into new storage, copying the data
1567 * and modifying the original resource to point at the new storage.
1568 *
1569 * This is useful for e.g. moving a suballocated internal resource to a
1570 * dedicated allocation that can be exported by itself.
1571 */
1572 static void
iris_reallocate_resource_inplace(struct iris_context * ice,struct iris_resource * old_res,unsigned new_bind_flag)1573 iris_reallocate_resource_inplace(struct iris_context *ice,
1574 struct iris_resource *old_res,
1575 unsigned new_bind_flag)
1576 {
1577 struct pipe_screen *pscreen = ice->ctx.screen;
1578
1579 if (iris_bo_is_external(old_res->bo))
1580 return;
1581
1582 assert(old_res->mod_info == NULL);
1583 assert(old_res->bo == old_res->aux.bo || old_res->aux.bo == NULL);
1584 assert(old_res->bo == old_res->aux.clear_color_bo ||
1585 old_res->aux.clear_color_bo == NULL);
1586 assert(old_res->external_format == PIPE_FORMAT_NONE);
1587
1588 struct pipe_resource templ = old_res->base.b;
1589 templ.bind |= new_bind_flag;
1590
1591 struct iris_resource *new_res =
1592 (void *) pscreen->resource_create(pscreen, &templ);
1593
1594 assert(iris_bo_is_real(new_res->bo));
1595
1596 struct iris_batch *batch = &ice->batches[IRIS_BATCH_RENDER];
1597
1598 if (old_res->base.b.target == PIPE_BUFFER) {
1599 struct pipe_box box = (struct pipe_box) {
1600 .width = old_res->base.b.width0,
1601 .height = 1,
1602 };
1603
1604 iris_copy_region(&ice->blorp, batch, &new_res->base.b, 0, 0, 0, 0,
1605 &old_res->base.b, 0, &box);
1606 } else {
1607 for (unsigned l = 0; l <= templ.last_level; l++) {
1608 struct pipe_box box = (struct pipe_box) {
1609 .width = u_minify(templ.width0, l),
1610 .height = u_minify(templ.height0, l),
1611 .depth = util_num_layers(&templ, l),
1612 };
1613
1614 iris_copy_region(&ice->blorp, batch, &new_res->base.b, l, 0, 0, 0,
1615 &old_res->base.b, l, &box);
1616 }
1617 }
1618
1619 iris_flush_resource(&ice->ctx, &new_res->base.b);
1620 iris_foreach_batch(ice, batch) {
1621 if (iris_batch_references(batch, new_res->bo))
1622 iris_batch_flush(batch);
1623 }
1624
1625 struct iris_bo *old_bo = old_res->bo;
1626 struct iris_bo *old_aux_bo = old_res->aux.bo;
1627 struct iris_bo *old_clear_color_bo = old_res->aux.clear_color_bo;
1628
1629 /* Replace the structure fields with the new ones */
1630 old_res->base.b.bind = templ.bind;
1631 old_res->bo = new_res->bo;
1632 old_res->aux.surf = new_res->aux.surf;
1633 old_res->aux.bo = new_res->aux.bo;
1634 old_res->aux.offset = new_res->aux.offset;
1635 old_res->aux.extra_aux.surf = new_res->aux.extra_aux.surf;
1636 old_res->aux.extra_aux.offset = new_res->aux.extra_aux.offset;
1637 old_res->aux.clear_color_bo = new_res->aux.clear_color_bo;
1638 old_res->aux.clear_color_offset = new_res->aux.clear_color_offset;
1639 old_res->aux.usage = new_res->aux.usage;
1640
1641 if (new_res->aux.state) {
1642 assert(old_res->aux.state);
1643 for (unsigned l = 0; l <= templ.last_level; l++) {
1644 unsigned layers = util_num_layers(&templ, l);
1645 for (unsigned z = 0; z < layers; z++) {
1646 enum isl_aux_state aux =
1647 iris_resource_get_aux_state(new_res, l, z);
1648 iris_resource_set_aux_state(ice, old_res, l, z, 1, aux);
1649 }
1650 }
1651 }
1652
1653 /* old_res now points at the new BOs, make new_res point at the old ones
1654 * so they'll be freed when we unreference the resource below.
1655 */
1656 new_res->bo = old_bo;
1657 new_res->aux.bo = old_aux_bo;
1658 new_res->aux.clear_color_bo = old_clear_color_bo;
1659
1660 pipe_resource_reference((struct pipe_resource **)&new_res, NULL);
1661 }
1662
1663 static void
iris_resource_disable_suballoc_on_first_query(struct pipe_screen * pscreen,struct pipe_context * ctx,struct iris_resource * res)1664 iris_resource_disable_suballoc_on_first_query(struct pipe_screen *pscreen,
1665 struct pipe_context *ctx,
1666 struct iris_resource *res)
1667 {
1668 if (iris_bo_is_real(res->bo))
1669 return;
1670
1671 assert(!(res->base.b.bind & PIPE_BIND_SHARED));
1672
1673 bool destroy_context;
1674 if (ctx) {
1675 ctx = threaded_context_unwrap_sync(ctx);
1676 destroy_context = false;
1677 } else {
1678 /* We need to execute a blit on some GPU context, but the DRI layer
1679 * often doesn't give us one. So we have to invent a temporary one.
1680 *
1681 * We can't store a permanent context in the screen, as it would cause
1682 * circular refcounting where screens reference contexts that reference
1683 * resources, while resources reference screens...causing nothing to be
1684 * freed. So we just create and destroy a temporary one here.
1685 */
1686 ctx = iris_create_context(pscreen, NULL, 0);
1687 destroy_context = true;
1688 }
1689
1690 struct iris_context *ice = (struct iris_context *)ctx;
1691
1692 iris_reallocate_resource_inplace(ice, res, PIPE_BIND_SHARED);
1693 assert(res->base.b.bind & PIPE_BIND_SHARED);
1694
1695 if (destroy_context)
1696 iris_destroy_context(ctx);
1697 }
1698
1699
1700 static void
iris_resource_disable_aux_on_first_query(struct pipe_resource * resource,unsigned usage)1701 iris_resource_disable_aux_on_first_query(struct pipe_resource *resource,
1702 unsigned usage)
1703 {
1704 struct iris_resource *res = (struct iris_resource *)resource;
1705 bool mod_with_aux =
1706 res->mod_info && isl_drm_modifier_has_aux(res->mod_info->modifier);
1707
1708 /* Disable aux usage if explicit flush not set and this is the first time
1709 * we are dealing with this resource and the resource was not created with
1710 * a modifier with aux.
1711 */
1712 if (!mod_with_aux &&
1713 (!(usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH) && res->aux.usage != 0) &&
1714 p_atomic_read(&resource->reference.count) == 1) {
1715 iris_resource_disable_aux(res);
1716 }
1717 }
1718
1719 static bool
iris_resource_get_param(struct pipe_screen * pscreen,struct pipe_context * ctx,struct pipe_resource * resource,unsigned plane,unsigned layer,unsigned level,enum pipe_resource_param param,unsigned handle_usage,uint64_t * value)1720 iris_resource_get_param(struct pipe_screen *pscreen,
1721 struct pipe_context *ctx,
1722 struct pipe_resource *resource,
1723 unsigned plane,
1724 unsigned layer,
1725 unsigned level,
1726 enum pipe_resource_param param,
1727 unsigned handle_usage,
1728 uint64_t *value)
1729 {
1730 struct iris_screen *screen = (struct iris_screen *)pscreen;
1731 struct iris_resource *base_res = (struct iris_resource *)resource;
1732 unsigned main_plane = get_main_plane_for_plane(base_res->external_format,
1733 plane);
1734 struct iris_resource *res =
1735 (struct iris_resource *)util_resource_at_index(resource, main_plane);
1736 assert(res);
1737
1738 bool mod_with_aux =
1739 res->mod_info && isl_drm_modifier_has_aux(res->mod_info->modifier);
1740 bool wants_aux = mod_with_aux && plane != main_plane;
1741 bool wants_cc = mod_with_aux &&
1742 isl_drm_modifier_plane_is_clear_color(res->mod_info->modifier, plane);
1743 bool result;
1744 unsigned handle;
1745
1746 iris_resource_disable_aux_on_first_query(resource, handle_usage);
1747 iris_resource_disable_suballoc_on_first_query(pscreen, ctx, res);
1748
1749 struct iris_bo *bo = wants_cc ? res->aux.clear_color_bo :
1750 wants_aux ? res->aux.bo : res->bo;
1751
1752 assert(iris_bo_is_real(bo));
1753
1754 switch (param) {
1755 case PIPE_RESOURCE_PARAM_NPLANES:
1756 if (mod_with_aux) {
1757 *value = iris_get_dmabuf_modifier_planes(pscreen,
1758 res->mod_info->modifier,
1759 res->external_format);
1760 } else {
1761 *value = get_num_planes(&res->base.b);
1762 }
1763 return true;
1764 case PIPE_RESOURCE_PARAM_STRIDE:
1765 *value = wants_cc ? 64 :
1766 wants_aux ? res->aux.surf.row_pitch_B : res->surf.row_pitch_B;
1767
1768 /* Mesa's implementation of eglCreateImage rejects strides of zero (see
1769 * dri2_check_dma_buf_attribs). Ensure we return a non-zero stride as
1770 * this value may be queried from GBM and passed into EGL.
1771 *
1772 * Also, although modifiers which use a clear color plane specify that
1773 * the plane's pitch should be ignored, some kernels have been found to
1774 * require 64-byte alignment.
1775 */
1776 assert(*value != 0 && (!wants_cc || *value % 64 == 0));
1777
1778 return true;
1779 case PIPE_RESOURCE_PARAM_OFFSET:
1780 *value = wants_cc ? res->aux.clear_color_offset :
1781 wants_aux ? res->aux.offset : res->offset;
1782 return true;
1783 case PIPE_RESOURCE_PARAM_MODIFIER:
1784 *value = res->mod_info ? res->mod_info->modifier :
1785 tiling_to_modifier(isl_tiling_to_i915_tiling(res->surf.tiling));
1786 return true;
1787 case PIPE_RESOURCE_PARAM_HANDLE_TYPE_SHARED:
1788 if (!wants_aux)
1789 iris_gem_set_tiling(bo, &res->surf);
1790
1791 result = iris_bo_flink(bo, &handle) == 0;
1792 if (result)
1793 *value = handle;
1794 return result;
1795 case PIPE_RESOURCE_PARAM_HANDLE_TYPE_KMS: {
1796 if (!wants_aux)
1797 iris_gem_set_tiling(bo, &res->surf);
1798
1799 /* Because we share the same drm file across multiple iris_screen, when
1800 * we export a GEM handle we must make sure it is valid in the DRM file
1801 * descriptor the caller is using (this is the FD given at screen
1802 * creation).
1803 */
1804 uint32_t handle;
1805 if (iris_bo_export_gem_handle_for_device(bo, screen->winsys_fd, &handle))
1806 return false;
1807 *value = handle;
1808 return true;
1809 }
1810
1811 case PIPE_RESOURCE_PARAM_HANDLE_TYPE_FD:
1812 if (!wants_aux)
1813 iris_gem_set_tiling(bo, &res->surf);
1814
1815 result = iris_bo_export_dmabuf(bo, (int *) &handle) == 0;
1816 if (result)
1817 *value = handle;
1818 return result;
1819 default:
1820 return false;
1821 }
1822 }
1823
1824 static bool
iris_resource_get_handle(struct pipe_screen * pscreen,struct pipe_context * ctx,struct pipe_resource * resource,struct winsys_handle * whandle,unsigned usage)1825 iris_resource_get_handle(struct pipe_screen *pscreen,
1826 struct pipe_context *ctx,
1827 struct pipe_resource *resource,
1828 struct winsys_handle *whandle,
1829 unsigned usage)
1830 {
1831 struct iris_screen *screen = (struct iris_screen *) pscreen;
1832 struct iris_resource *res = (struct iris_resource *)resource;
1833 bool mod_with_aux =
1834 res->mod_info && isl_drm_modifier_has_aux(res->mod_info->modifier);
1835
1836 iris_resource_disable_aux_on_first_query(resource, usage);
1837 iris_resource_disable_suballoc_on_first_query(pscreen, ctx, res);
1838
1839 assert(iris_bo_is_real(res->bo));
1840
1841 struct iris_bo *bo;
1842 if (res->mod_info &&
1843 isl_drm_modifier_plane_is_clear_color(res->mod_info->modifier,
1844 whandle->plane)) {
1845 bo = res->aux.clear_color_bo;
1846 whandle->offset = res->aux.clear_color_offset;
1847 } else if (mod_with_aux && whandle->plane > 0) {
1848 bo = res->aux.bo;
1849 whandle->stride = res->aux.surf.row_pitch_B;
1850 whandle->offset = res->aux.offset;
1851 } else {
1852 /* If this is a buffer, stride should be 0 - no need to special case */
1853 whandle->stride = res->surf.row_pitch_B;
1854 bo = res->bo;
1855 }
1856
1857 whandle->format = res->external_format;
1858 whandle->modifier =
1859 res->mod_info ? res->mod_info->modifier
1860 : tiling_to_modifier(isl_tiling_to_i915_tiling(res->surf.tiling));
1861
1862 #ifndef NDEBUG
1863 enum isl_aux_usage allowed_usage =
1864 (usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH) || mod_with_aux ?
1865 res->aux.usage : ISL_AUX_USAGE_NONE;
1866
1867 if (res->aux.usage != allowed_usage) {
1868 enum isl_aux_state aux_state = iris_resource_get_aux_state(res, 0, 0);
1869 assert(aux_state == ISL_AUX_STATE_RESOLVED ||
1870 aux_state == ISL_AUX_STATE_PASS_THROUGH);
1871 }
1872 #endif
1873
1874 switch (whandle->type) {
1875 case WINSYS_HANDLE_TYPE_SHARED:
1876 iris_gem_set_tiling(bo, &res->surf);
1877 return iris_bo_flink(bo, &whandle->handle) == 0;
1878 case WINSYS_HANDLE_TYPE_KMS: {
1879 iris_gem_set_tiling(bo, &res->surf);
1880
1881 /* Because we share the same drm file across multiple iris_screen, when
1882 * we export a GEM handle we must make sure it is valid in the DRM file
1883 * descriptor the caller is using (this is the FD given at screen
1884 * creation).
1885 */
1886 uint32_t handle;
1887 if (iris_bo_export_gem_handle_for_device(bo, screen->winsys_fd, &handle))
1888 return false;
1889 whandle->handle = handle;
1890 return true;
1891 }
1892 case WINSYS_HANDLE_TYPE_FD:
1893 iris_gem_set_tiling(bo, &res->surf);
1894 return iris_bo_export_dmabuf(bo, (int *) &whandle->handle) == 0;
1895 }
1896
1897 return false;
1898 }
1899
1900 static bool
resource_is_busy(struct iris_context * ice,struct iris_resource * res)1901 resource_is_busy(struct iris_context *ice,
1902 struct iris_resource *res)
1903 {
1904 bool busy = iris_bo_busy(res->bo);
1905
1906 iris_foreach_batch(ice, batch)
1907 busy |= iris_batch_references(batch, res->bo);
1908
1909 return busy;
1910 }
1911
1912 void
iris_replace_buffer_storage(struct pipe_context * ctx,struct pipe_resource * p_dst,struct pipe_resource * p_src,unsigned num_rebinds,uint32_t rebind_mask,uint32_t delete_buffer_id)1913 iris_replace_buffer_storage(struct pipe_context *ctx,
1914 struct pipe_resource *p_dst,
1915 struct pipe_resource *p_src,
1916 unsigned num_rebinds,
1917 uint32_t rebind_mask,
1918 uint32_t delete_buffer_id)
1919 {
1920 struct iris_screen *screen = (void *) ctx->screen;
1921 struct iris_context *ice = (void *) ctx;
1922 struct iris_resource *dst = (void *) p_dst;
1923 struct iris_resource *src = (void *) p_src;
1924
1925 assert(memcmp(&dst->surf, &src->surf, sizeof(dst->surf)) == 0);
1926
1927 struct iris_bo *old_bo = dst->bo;
1928
1929 /* Swap out the backing storage */
1930 iris_bo_reference(src->bo);
1931 dst->bo = src->bo;
1932
1933 /* Rebind the buffer, replacing any state referring to the old BO's
1934 * address, and marking state dirty so it's reemitted.
1935 */
1936 screen->vtbl.rebind_buffer(ice, dst);
1937
1938 iris_bo_unreference(old_bo);
1939 }
1940
1941 /**
1942 * Discard a buffer's contents and replace it's backing storage with a
1943 * fresh, idle buffer if necessary.
1944 *
1945 * Returns true if the storage can be considered idle.
1946 */
1947 static bool
iris_invalidate_buffer(struct iris_context * ice,struct iris_resource * res)1948 iris_invalidate_buffer(struct iris_context *ice, struct iris_resource *res)
1949 {
1950 struct iris_screen *screen = (void *) ice->ctx.screen;
1951
1952 if (res->base.b.target != PIPE_BUFFER)
1953 return false;
1954
1955 /* If it's already invalidated, don't bother doing anything.
1956 * We consider the storage to be idle, because either it was freshly
1957 * allocated (and not busy), or a previous call here was what cleared
1958 * the range, and that call replaced the storage with an idle buffer.
1959 */
1960 if (res->valid_buffer_range.start > res->valid_buffer_range.end)
1961 return true;
1962
1963 if (!resource_is_busy(ice, res)) {
1964 /* The resource is idle, so just mark that it contains no data and
1965 * keep using the same underlying buffer object.
1966 */
1967 util_range_set_empty(&res->valid_buffer_range);
1968 return true;
1969 }
1970
1971 /* Otherwise, try and replace the backing storage with a new BO. */
1972
1973 /* We can't reallocate memory we didn't allocate in the first place. */
1974 if (res->bo->gem_handle && res->bo->real.userptr)
1975 return false;
1976
1977 /* Nor can we allocate buffers we imported or exported. */
1978 if (iris_bo_is_external(res->bo))
1979 return false;
1980
1981 struct iris_bo *old_bo = res->bo;
1982 unsigned flags = old_bo->real.protected ? BO_ALLOC_PROTECTED : BO_ALLOC_PLAIN;
1983 struct iris_bo *new_bo =
1984 iris_bo_alloc(screen->bufmgr, res->bo->name, res->base.b.width0,
1985 iris_buffer_alignment(res->base.b.width0),
1986 iris_memzone_for_address(old_bo->address),
1987 flags);
1988 if (!new_bo)
1989 return false;
1990
1991 /* Swap out the backing storage */
1992 res->bo = new_bo;
1993
1994 /* Rebind the buffer, replacing any state referring to the old BO's
1995 * address, and marking state dirty so it's reemitted.
1996 */
1997 screen->vtbl.rebind_buffer(ice, res);
1998
1999 util_range_set_empty(&res->valid_buffer_range);
2000
2001 iris_bo_unreference(old_bo);
2002
2003 /* The new buffer is idle. */
2004 return true;
2005 }
2006
2007 static void
iris_invalidate_resource(struct pipe_context * ctx,struct pipe_resource * resource)2008 iris_invalidate_resource(struct pipe_context *ctx,
2009 struct pipe_resource *resource)
2010 {
2011 struct iris_context *ice = (void *) ctx;
2012 struct iris_resource *res = (void *) resource;
2013
2014 iris_invalidate_buffer(ice, res);
2015 }
2016
2017 static void
iris_flush_staging_region(struct pipe_transfer * xfer,const struct pipe_box * flush_box)2018 iris_flush_staging_region(struct pipe_transfer *xfer,
2019 const struct pipe_box *flush_box)
2020 {
2021 if (!(xfer->usage & PIPE_MAP_WRITE))
2022 return;
2023
2024 struct iris_transfer *map = (void *) xfer;
2025
2026 struct pipe_box src_box = *flush_box;
2027
2028 /* Account for extra alignment padding in staging buffer */
2029 if (xfer->resource->target == PIPE_BUFFER)
2030 src_box.x += xfer->box.x % IRIS_MAP_BUFFER_ALIGNMENT;
2031
2032 struct pipe_box dst_box = (struct pipe_box) {
2033 .x = xfer->box.x + flush_box->x,
2034 .y = xfer->box.y + flush_box->y,
2035 .z = xfer->box.z + flush_box->z,
2036 .width = flush_box->width,
2037 .height = flush_box->height,
2038 .depth = flush_box->depth,
2039 };
2040
2041 iris_copy_region(map->blorp, map->batch, xfer->resource, xfer->level,
2042 dst_box.x, dst_box.y, dst_box.z, map->staging, 0,
2043 &src_box);
2044 }
2045
2046 static void
iris_unmap_copy_region(struct iris_transfer * map)2047 iris_unmap_copy_region(struct iris_transfer *map)
2048 {
2049 iris_resource_destroy(map->staging->screen, map->staging);
2050
2051 map->ptr = NULL;
2052 }
2053
2054 static void
iris_map_copy_region(struct iris_transfer * map)2055 iris_map_copy_region(struct iris_transfer *map)
2056 {
2057 struct pipe_screen *pscreen = &map->batch->screen->base;
2058 struct pipe_transfer *xfer = &map->base.b;
2059 struct pipe_box *box = &xfer->box;
2060 struct iris_resource *res = (void *) xfer->resource;
2061
2062 unsigned extra = xfer->resource->target == PIPE_BUFFER ?
2063 box->x % IRIS_MAP_BUFFER_ALIGNMENT : 0;
2064
2065 struct pipe_resource templ = (struct pipe_resource) {
2066 .usage = PIPE_USAGE_STAGING,
2067 .width0 = box->width + extra,
2068 .height0 = box->height,
2069 .depth0 = 1,
2070 .nr_samples = xfer->resource->nr_samples,
2071 .nr_storage_samples = xfer->resource->nr_storage_samples,
2072 .array_size = box->depth,
2073 .format = res->internal_format,
2074 };
2075
2076 if (xfer->resource->target == PIPE_BUFFER) {
2077 templ.target = PIPE_BUFFER;
2078 map->staging = iris_resource_create_for_buffer(pscreen, &templ);
2079 } else {
2080 templ.target = templ.array_size > 1 ? PIPE_TEXTURE_2D_ARRAY
2081 : PIPE_TEXTURE_2D;
2082
2083 unsigned row_pitch_B = 0;
2084
2085 #if DETECT_OS_ANDROID
2086 /* Staging buffers for stall-avoidance blits don't always have the
2087 * same restrictions on stride as the original buffer. For example,
2088 * the original buffer may be used for scanout, while the staging
2089 * buffer will not be. So we may compute a smaller stride for the
2090 * staging buffer than the original.
2091 *
2092 * Normally, this is good, as it saves memory. Unfortunately, for
2093 * Android, gbm_gralloc incorrectly asserts that the stride returned
2094 * by gbm_bo_map() must equal the result of gbm_bo_get_stride(),
2095 * which simply isn't always the case.
2096 *
2097 * Because gralloc is unlikely to be fixed, we hack around it in iris
2098 * by forcing the staging buffer to have a matching stride.
2099 */
2100 if (iris_bo_is_external(res->bo))
2101 row_pitch_B = res->surf.row_pitch_B;
2102 #endif
2103
2104 map->staging =
2105 iris_resource_create_for_image(pscreen, &templ, NULL, 0, row_pitch_B);
2106 }
2107
2108 /* If we fail to create a staging resource, the caller will fallback
2109 * to mapping directly on the CPU.
2110 */
2111 if (!map->staging)
2112 return;
2113
2114 if (templ.target != PIPE_BUFFER) {
2115 struct isl_surf *surf = &((struct iris_resource *) map->staging)->surf;
2116 xfer->stride = isl_surf_get_row_pitch_B(surf);
2117 xfer->layer_stride = isl_surf_get_array_pitch(surf);
2118 }
2119
2120 if ((xfer->usage & PIPE_MAP_READ) ||
2121 (res->base.b.target == PIPE_BUFFER &&
2122 !(xfer->usage & PIPE_MAP_DISCARD_RANGE))) {
2123 iris_copy_region(map->blorp, map->batch, map->staging, 0, extra, 0, 0,
2124 xfer->resource, xfer->level, box);
2125 /* Ensure writes to the staging BO land before we map it below. */
2126 iris_emit_pipe_control_flush(map->batch,
2127 "transfer read: flush before mapping",
2128 PIPE_CONTROL_RENDER_TARGET_FLUSH |
2129 PIPE_CONTROL_TILE_CACHE_FLUSH |
2130 PIPE_CONTROL_CS_STALL);
2131 }
2132
2133 struct iris_bo *staging_bo = iris_resource_bo(map->staging);
2134
2135 if (iris_batch_references(map->batch, staging_bo))
2136 iris_batch_flush(map->batch);
2137
2138 assert(((struct iris_resource *)map->staging)->offset == 0);
2139 map->ptr =
2140 iris_bo_map(map->dbg, staging_bo, xfer->usage & MAP_FLAGS) + extra;
2141
2142 map->unmap = iris_unmap_copy_region;
2143 }
2144
2145 static void
get_image_offset_el(const struct isl_surf * surf,unsigned level,unsigned z,unsigned * out_x0_el,unsigned * out_y0_el)2146 get_image_offset_el(const struct isl_surf *surf, unsigned level, unsigned z,
2147 unsigned *out_x0_el, unsigned *out_y0_el)
2148 {
2149 ASSERTED uint32_t z0_el, a0_el;
2150 if (surf->dim == ISL_SURF_DIM_3D) {
2151 isl_surf_get_image_offset_el(surf, level, 0, z,
2152 out_x0_el, out_y0_el, &z0_el, &a0_el);
2153 } else {
2154 isl_surf_get_image_offset_el(surf, level, z, 0,
2155 out_x0_el, out_y0_el, &z0_el, &a0_el);
2156 }
2157 assert(z0_el == 0 && a0_el == 0);
2158 }
2159
2160 /**
2161 * Get pointer offset into stencil buffer.
2162 *
2163 * The stencil buffer is W tiled. Since the GTT is incapable of W fencing, we
2164 * must decode the tile's layout in software.
2165 *
2166 * See
2167 * - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.2.1 W-Major Tile
2168 * Format.
2169 * - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.3 Tiling Algorithm
2170 *
2171 * Even though the returned offset is always positive, the return type is
2172 * signed due to
2173 * commit e8b1c6d6f55f5be3bef25084fdd8b6127517e137
2174 * mesa: Fix return type of _mesa_get_format_bytes() (#37351)
2175 */
2176 static intptr_t
s8_offset(uint32_t stride,uint32_t x,uint32_t y)2177 s8_offset(uint32_t stride, uint32_t x, uint32_t y)
2178 {
2179 uint32_t tile_size = 4096;
2180 uint32_t tile_width = 64;
2181 uint32_t tile_height = 64;
2182 uint32_t row_size = 64 * stride / 2; /* Two rows are interleaved. */
2183
2184 uint32_t tile_x = x / tile_width;
2185 uint32_t tile_y = y / tile_height;
2186
2187 /* The byte's address relative to the tile's base addres. */
2188 uint32_t byte_x = x % tile_width;
2189 uint32_t byte_y = y % tile_height;
2190
2191 uintptr_t u = tile_y * row_size
2192 + tile_x * tile_size
2193 + 512 * (byte_x / 8)
2194 + 64 * (byte_y / 8)
2195 + 32 * ((byte_y / 4) % 2)
2196 + 16 * ((byte_x / 4) % 2)
2197 + 8 * ((byte_y / 2) % 2)
2198 + 4 * ((byte_x / 2) % 2)
2199 + 2 * (byte_y % 2)
2200 + 1 * (byte_x % 2);
2201
2202 return u;
2203 }
2204
2205 static void
iris_unmap_s8(struct iris_transfer * map)2206 iris_unmap_s8(struct iris_transfer *map)
2207 {
2208 struct pipe_transfer *xfer = &map->base.b;
2209 const struct pipe_box *box = &xfer->box;
2210 struct iris_resource *res = (struct iris_resource *) xfer->resource;
2211 struct isl_surf *surf = &res->surf;
2212
2213 if (xfer->usage & PIPE_MAP_WRITE) {
2214 uint8_t *untiled_s8_map = map->ptr;
2215 uint8_t *tiled_s8_map = res->offset +
2216 iris_bo_map(map->dbg, res->bo, (xfer->usage | MAP_RAW) & MAP_FLAGS);
2217
2218 for (int s = 0; s < box->depth; s++) {
2219 unsigned x0_el, y0_el;
2220 get_image_offset_el(surf, xfer->level, box->z + s, &x0_el, &y0_el);
2221
2222 for (uint32_t y = 0; y < box->height; y++) {
2223 for (uint32_t x = 0; x < box->width; x++) {
2224 ptrdiff_t offset = s8_offset(surf->row_pitch_B,
2225 x0_el + box->x + x,
2226 y0_el + box->y + y);
2227 tiled_s8_map[offset] =
2228 untiled_s8_map[s * xfer->layer_stride + y * xfer->stride + x];
2229 }
2230 }
2231 }
2232 }
2233
2234 free(map->buffer);
2235 }
2236
2237 static void
iris_map_s8(struct iris_transfer * map)2238 iris_map_s8(struct iris_transfer *map)
2239 {
2240 struct pipe_transfer *xfer = &map->base.b;
2241 const struct pipe_box *box = &xfer->box;
2242 struct iris_resource *res = (struct iris_resource *) xfer->resource;
2243 struct isl_surf *surf = &res->surf;
2244
2245 xfer->stride = surf->row_pitch_B;
2246 xfer->layer_stride = xfer->stride * box->height;
2247
2248 /* The tiling and detiling functions require that the linear buffer has
2249 * a 16-byte alignment (that is, its `x0` is 16-byte aligned). Here we
2250 * over-allocate the linear buffer to get the proper alignment.
2251 */
2252 map->buffer = map->ptr = malloc(xfer->layer_stride * box->depth);
2253 assert(map->buffer);
2254
2255 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
2256 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
2257 * invalidate is set, since we'll be writing the whole rectangle from our
2258 * temporary buffer back out.
2259 */
2260 if (xfer->usage & PIPE_MAP_READ) {
2261 uint8_t *untiled_s8_map = map->ptr;
2262 uint8_t *tiled_s8_map = res->offset +
2263 iris_bo_map(map->dbg, res->bo, (xfer->usage | MAP_RAW) & MAP_FLAGS);
2264
2265 for (int s = 0; s < box->depth; s++) {
2266 unsigned x0_el, y0_el;
2267 get_image_offset_el(surf, xfer->level, box->z + s, &x0_el, &y0_el);
2268
2269 for (uint32_t y = 0; y < box->height; y++) {
2270 for (uint32_t x = 0; x < box->width; x++) {
2271 ptrdiff_t offset = s8_offset(surf->row_pitch_B,
2272 x0_el + box->x + x,
2273 y0_el + box->y + y);
2274 untiled_s8_map[s * xfer->layer_stride + y * xfer->stride + x] =
2275 tiled_s8_map[offset];
2276 }
2277 }
2278 }
2279 }
2280
2281 map->unmap = iris_unmap_s8;
2282 }
2283
2284 /* Compute extent parameters for use with tiled_memcpy functions.
2285 * xs are in units of bytes and ys are in units of strides.
2286 */
2287 static inline void
tile_extents(const struct isl_surf * surf,const struct pipe_box * box,unsigned level,int z,unsigned * x1_B,unsigned * x2_B,unsigned * y1_el,unsigned * y2_el)2288 tile_extents(const struct isl_surf *surf,
2289 const struct pipe_box *box,
2290 unsigned level, int z,
2291 unsigned *x1_B, unsigned *x2_B,
2292 unsigned *y1_el, unsigned *y2_el)
2293 {
2294 const struct isl_format_layout *fmtl = isl_format_get_layout(surf->format);
2295 const unsigned cpp = fmtl->bpb / 8;
2296
2297 assert(box->x % fmtl->bw == 0);
2298 assert(box->y % fmtl->bh == 0);
2299
2300 unsigned x0_el, y0_el;
2301 get_image_offset_el(surf, level, box->z + z, &x0_el, &y0_el);
2302
2303 *x1_B = (box->x / fmtl->bw + x0_el) * cpp;
2304 *y1_el = box->y / fmtl->bh + y0_el;
2305 *x2_B = (DIV_ROUND_UP(box->x + box->width, fmtl->bw) + x0_el) * cpp;
2306 *y2_el = DIV_ROUND_UP(box->y + box->height, fmtl->bh) + y0_el;
2307 }
2308
2309 static void
iris_unmap_tiled_memcpy(struct iris_transfer * map)2310 iris_unmap_tiled_memcpy(struct iris_transfer *map)
2311 {
2312 struct pipe_transfer *xfer = &map->base.b;
2313 const struct pipe_box *box = &xfer->box;
2314 struct iris_resource *res = (struct iris_resource *) xfer->resource;
2315 struct isl_surf *surf = &res->surf;
2316
2317 const bool has_swizzling = false;
2318
2319 if (xfer->usage & PIPE_MAP_WRITE) {
2320 char *dst = res->offset +
2321 iris_bo_map(map->dbg, res->bo, (xfer->usage | MAP_RAW) & MAP_FLAGS);
2322
2323 for (int s = 0; s < box->depth; s++) {
2324 unsigned x1, x2, y1, y2;
2325 tile_extents(surf, box, xfer->level, s, &x1, &x2, &y1, &y2);
2326
2327 void *ptr = map->ptr + s * xfer->layer_stride;
2328
2329 isl_memcpy_linear_to_tiled(x1, x2, y1, y2, dst, ptr,
2330 surf->row_pitch_B, xfer->stride,
2331 has_swizzling, surf->tiling, ISL_MEMCPY);
2332 }
2333 }
2334 os_free_aligned(map->buffer);
2335 map->buffer = map->ptr = NULL;
2336 }
2337
2338 static void
iris_map_tiled_memcpy(struct iris_transfer * map)2339 iris_map_tiled_memcpy(struct iris_transfer *map)
2340 {
2341 struct pipe_transfer *xfer = &map->base.b;
2342 const struct pipe_box *box = &xfer->box;
2343 struct iris_resource *res = (struct iris_resource *) xfer->resource;
2344 struct isl_surf *surf = &res->surf;
2345
2346 xfer->stride = ALIGN(surf->row_pitch_B, 16);
2347 xfer->layer_stride = xfer->stride * box->height;
2348
2349 unsigned x1, x2, y1, y2;
2350 tile_extents(surf, box, xfer->level, 0, &x1, &x2, &y1, &y2);
2351
2352 /* The tiling and detiling functions require that the linear buffer has
2353 * a 16-byte alignment (that is, its `x0` is 16-byte aligned). Here we
2354 * over-allocate the linear buffer to get the proper alignment.
2355 */
2356 map->buffer =
2357 os_malloc_aligned(xfer->layer_stride * box->depth, 16);
2358 assert(map->buffer);
2359 map->ptr = (char *)map->buffer + (x1 & 0xf);
2360
2361 const bool has_swizzling = false;
2362
2363 if (xfer->usage & PIPE_MAP_READ) {
2364 char *src = res->offset +
2365 iris_bo_map(map->dbg, res->bo, (xfer->usage | MAP_RAW) & MAP_FLAGS);
2366
2367 for (int s = 0; s < box->depth; s++) {
2368 unsigned x1, x2, y1, y2;
2369 tile_extents(surf, box, xfer->level, s, &x1, &x2, &y1, &y2);
2370
2371 /* Use 's' rather than 'box->z' to rebase the first slice to 0. */
2372 void *ptr = map->ptr + s * xfer->layer_stride;
2373
2374 isl_memcpy_tiled_to_linear(x1, x2, y1, y2, ptr, src, xfer->stride,
2375 surf->row_pitch_B, has_swizzling,
2376 surf->tiling,
2377 #if defined(USE_SSE41)
2378 util_get_cpu_caps()->has_sse4_1 ?
2379 ISL_MEMCPY_STREAMING_LOAD :
2380 #endif
2381 ISL_MEMCPY);
2382 }
2383 }
2384
2385 map->unmap = iris_unmap_tiled_memcpy;
2386 }
2387
2388 static void
iris_map_direct(struct iris_transfer * map)2389 iris_map_direct(struct iris_transfer *map)
2390 {
2391 struct pipe_transfer *xfer = &map->base.b;
2392 struct pipe_box *box = &xfer->box;
2393 struct iris_resource *res = (struct iris_resource *) xfer->resource;
2394
2395 void *ptr = res->offset +
2396 iris_bo_map(map->dbg, res->bo, xfer->usage & MAP_FLAGS);
2397
2398 if (res->base.b.target == PIPE_BUFFER) {
2399 xfer->stride = 0;
2400 xfer->layer_stride = 0;
2401
2402 map->ptr = ptr + box->x;
2403 } else {
2404 struct isl_surf *surf = &res->surf;
2405 const struct isl_format_layout *fmtl =
2406 isl_format_get_layout(surf->format);
2407 const unsigned cpp = fmtl->bpb / 8;
2408 unsigned x0_el, y0_el;
2409
2410 assert(box->x % fmtl->bw == 0);
2411 assert(box->y % fmtl->bh == 0);
2412 get_image_offset_el(surf, xfer->level, box->z, &x0_el, &y0_el);
2413
2414 x0_el += box->x / fmtl->bw;
2415 y0_el += box->y / fmtl->bh;
2416
2417 xfer->stride = isl_surf_get_row_pitch_B(surf);
2418 xfer->layer_stride = isl_surf_get_array_pitch(surf);
2419
2420 map->ptr = ptr + y0_el * xfer->stride + x0_el * cpp;
2421 }
2422 }
2423
2424 static bool
can_promote_to_async(const struct iris_resource * res,const struct pipe_box * box,enum pipe_map_flags usage)2425 can_promote_to_async(const struct iris_resource *res,
2426 const struct pipe_box *box,
2427 enum pipe_map_flags usage)
2428 {
2429 /* If we're writing to a section of the buffer that hasn't even been
2430 * initialized with useful data, then we can safely promote this write
2431 * to be unsynchronized. This helps the common pattern of appending data.
2432 */
2433 return res->base.b.target == PIPE_BUFFER && (usage & PIPE_MAP_WRITE) &&
2434 !(usage & TC_TRANSFER_MAP_NO_INFER_UNSYNCHRONIZED) &&
2435 !util_ranges_intersect(&res->valid_buffer_range, box->x,
2436 box->x + box->width);
2437 }
2438
2439 static bool
prefer_cpu_access(const struct iris_resource * res,const struct pipe_box * box,enum pipe_map_flags usage,unsigned level,bool map_would_stall)2440 prefer_cpu_access(const struct iris_resource *res,
2441 const struct pipe_box *box,
2442 enum pipe_map_flags usage,
2443 unsigned level,
2444 bool map_would_stall)
2445 {
2446 const enum iris_mmap_mode mmap_mode = iris_bo_mmap_mode(res->bo);
2447
2448 /* We must be able to map it. */
2449 if (mmap_mode == IRIS_MMAP_NONE)
2450 return false;
2451
2452 const bool write = usage & PIPE_MAP_WRITE;
2453 const bool read = usage & PIPE_MAP_READ;
2454 const bool preserve =
2455 res->base.b.target == PIPE_BUFFER && !(usage & PIPE_MAP_DISCARD_RANGE);
2456
2457 /* We want to avoid uncached reads because they are slow. */
2458 if (read && mmap_mode != IRIS_MMAP_WB)
2459 return false;
2460
2461 /* We want to avoid stalling. We can't avoid stalling for reads, though,
2462 * because the destination of a GPU staging copy would be busy and stall
2463 * in the exact same manner. So don't consider it for those.
2464 *
2465 * For buffer maps which aren't invalidating the destination, the GPU
2466 * staging copy path would have to read the existing buffer contents in
2467 * order to preserve them, effectively making it a read. But a direct
2468 * mapping would be able to just write the necessary parts without the
2469 * overhead of the copy. It may stall, but we would anyway.
2470 */
2471 if (map_would_stall && !read && !preserve)
2472 return false;
2473
2474 /* Use the GPU for writes if it would compress the data. */
2475 if (write && isl_aux_usage_has_compression(res->aux.usage))
2476 return false;
2477
2478 /* Writes & Cached CPU reads are fine as long as the primary is valid. */
2479 return !iris_has_invalid_primary(res, level, 1, box->z, box->depth);
2480 }
2481
2482 static void *
iris_transfer_map(struct pipe_context * ctx,struct pipe_resource * resource,unsigned level,enum pipe_map_flags usage,const struct pipe_box * box,struct pipe_transfer ** ptransfer)2483 iris_transfer_map(struct pipe_context *ctx,
2484 struct pipe_resource *resource,
2485 unsigned level,
2486 enum pipe_map_flags usage,
2487 const struct pipe_box *box,
2488 struct pipe_transfer **ptransfer)
2489 {
2490 struct iris_context *ice = (struct iris_context *)ctx;
2491 struct iris_resource *res = (struct iris_resource *)resource;
2492 struct isl_surf *surf = &res->surf;
2493
2494 /* From GL_AMD_pinned_memory issues:
2495 *
2496 * 4) Is glMapBuffer on a shared buffer guaranteed to return the
2497 * same system address which was specified at creation time?
2498 *
2499 * RESOLVED: NO. The GL implementation might return a different
2500 * virtual mapping of that memory, although the same physical
2501 * page will be used.
2502 *
2503 * So don't ever use staging buffers.
2504 */
2505 if (res->base.is_user_ptr)
2506 usage |= PIPE_MAP_PERSISTENT;
2507
2508 /* Promote discarding a range to discarding the entire buffer where
2509 * possible. This may allow us to replace the backing storage entirely
2510 * and let us do an unsynchronized map when we otherwise wouldn't.
2511 */
2512 if (resource->target == PIPE_BUFFER &&
2513 (usage & PIPE_MAP_DISCARD_RANGE) &&
2514 box->x == 0 && box->width == resource->width0) {
2515 usage |= PIPE_MAP_DISCARD_WHOLE_RESOURCE;
2516 }
2517
2518 if (usage & PIPE_MAP_DISCARD_WHOLE_RESOURCE) {
2519 /* Replace the backing storage with a fresh buffer for non-async maps */
2520 if (!(usage & (PIPE_MAP_UNSYNCHRONIZED | TC_TRANSFER_MAP_NO_INVALIDATE))
2521 && iris_invalidate_buffer(ice, res))
2522 usage |= PIPE_MAP_UNSYNCHRONIZED;
2523
2524 /* If we can discard the whole resource, we can discard the range. */
2525 usage |= PIPE_MAP_DISCARD_RANGE;
2526 }
2527
2528 if (!(usage & PIPE_MAP_UNSYNCHRONIZED) &&
2529 can_promote_to_async(res, box, usage)) {
2530 usage |= PIPE_MAP_UNSYNCHRONIZED;
2531 }
2532
2533 /* Avoid using GPU copies for persistent/coherent buffers, as the idea
2534 * there is to access them simultaneously on the CPU & GPU. This also
2535 * avoids trying to use GPU copies for our u_upload_mgr buffers which
2536 * contain state we're constructing for a GPU draw call, which would
2537 * kill us with infinite stack recursion.
2538 */
2539 if (usage & (PIPE_MAP_PERSISTENT | PIPE_MAP_COHERENT))
2540 usage |= PIPE_MAP_DIRECTLY;
2541
2542 /* We cannot provide a direct mapping of tiled resources, and we
2543 * may not be able to mmap imported BOs since they may come from
2544 * other devices that I915_GEM_MMAP cannot work with.
2545 */
2546 if ((usage & PIPE_MAP_DIRECTLY) &&
2547 (surf->tiling != ISL_TILING_LINEAR || iris_bo_is_imported(res->bo)))
2548 return NULL;
2549
2550 bool map_would_stall = false;
2551
2552 if (!(usage & PIPE_MAP_UNSYNCHRONIZED)) {
2553 map_would_stall =
2554 resource_is_busy(ice, res) ||
2555 iris_has_invalid_primary(res, level, 1, box->z, box->depth);
2556
2557 if (map_would_stall && (usage & PIPE_MAP_DONTBLOCK) &&
2558 (usage & PIPE_MAP_DIRECTLY))
2559 return NULL;
2560 }
2561
2562 struct iris_transfer *map;
2563
2564 if (usage & PIPE_MAP_THREAD_SAFE)
2565 map = CALLOC_STRUCT(iris_transfer);
2566 else if (usage & TC_TRANSFER_MAP_THREADED_UNSYNC)
2567 map = slab_zalloc(&ice->transfer_pool_unsync);
2568 else
2569 map = slab_zalloc(&ice->transfer_pool);
2570
2571 if (!map)
2572 return NULL;
2573
2574 struct pipe_transfer *xfer = &map->base.b;
2575
2576 map->dbg = &ice->dbg;
2577
2578 pipe_resource_reference(&xfer->resource, resource);
2579 xfer->level = level;
2580 xfer->usage = usage;
2581 xfer->box = *box;
2582 *ptransfer = xfer;
2583
2584 if (usage & PIPE_MAP_WRITE)
2585 util_range_add(&res->base.b, &res->valid_buffer_range, box->x, box->x + box->width);
2586
2587 if (prefer_cpu_access(res, box, usage, level, map_would_stall))
2588 usage |= PIPE_MAP_DIRECTLY;
2589
2590 /* TODO: Teach iris_map_tiled_memcpy about Tile64... */
2591 if (isl_tiling_is_64(res->surf.tiling))
2592 usage &= ~PIPE_MAP_DIRECTLY;
2593
2594 if (!(usage & PIPE_MAP_DIRECTLY)) {
2595 /* If we need a synchronous mapping and the resource is busy, or needs
2596 * resolving, we copy to/from a linear temporary buffer using the GPU.
2597 */
2598 map->batch = &ice->batches[IRIS_BATCH_RENDER];
2599 map->blorp = &ice->blorp;
2600 iris_map_copy_region(map);
2601 }
2602
2603 /* If we've requested a direct mapping, or iris_map_copy_region failed
2604 * to create a staging resource, then map it directly on the CPU.
2605 */
2606 if (!map->ptr) {
2607 if (resource->target != PIPE_BUFFER) {
2608 iris_resource_access_raw(ice, res, level, box->z, box->depth,
2609 usage & PIPE_MAP_WRITE);
2610 }
2611
2612 if (!(usage & PIPE_MAP_UNSYNCHRONIZED)) {
2613 iris_foreach_batch(ice, batch) {
2614 if (iris_batch_references(batch, res->bo))
2615 iris_batch_flush(batch);
2616 }
2617 }
2618
2619 if (surf->tiling == ISL_TILING_W) {
2620 /* TODO: Teach iris_map_tiled_memcpy about W-tiling... */
2621 iris_map_s8(map);
2622 } else if (surf->tiling != ISL_TILING_LINEAR) {
2623 iris_map_tiled_memcpy(map);
2624 } else {
2625 iris_map_direct(map);
2626 }
2627 }
2628
2629 return map->ptr;
2630 }
2631
2632 static void
iris_transfer_flush_region(struct pipe_context * ctx,struct pipe_transfer * xfer,const struct pipe_box * box)2633 iris_transfer_flush_region(struct pipe_context *ctx,
2634 struct pipe_transfer *xfer,
2635 const struct pipe_box *box)
2636 {
2637 struct iris_context *ice = (struct iris_context *)ctx;
2638 struct iris_resource *res = (struct iris_resource *) xfer->resource;
2639 struct iris_transfer *map = (void *) xfer;
2640
2641 if (map->staging)
2642 iris_flush_staging_region(xfer, box);
2643
2644 if (res->base.b.target == PIPE_BUFFER) {
2645 util_range_add(&res->base.b, &res->valid_buffer_range, box->x, box->x + box->width);
2646 }
2647
2648 /* Make sure we flag constants dirty even if there's no need to emit
2649 * any PIPE_CONTROLs to a batch.
2650 */
2651 iris_dirty_for_history(ice, res);
2652 }
2653
2654 static void
iris_transfer_unmap(struct pipe_context * ctx,struct pipe_transfer * xfer)2655 iris_transfer_unmap(struct pipe_context *ctx, struct pipe_transfer *xfer)
2656 {
2657 struct iris_context *ice = (struct iris_context *)ctx;
2658 struct iris_transfer *map = (void *) xfer;
2659
2660 if (!(xfer->usage & (PIPE_MAP_FLUSH_EXPLICIT |
2661 PIPE_MAP_COHERENT))) {
2662 struct pipe_box flush_box = {
2663 .x = 0, .y = 0, .z = 0,
2664 .width = xfer->box.width,
2665 .height = xfer->box.height,
2666 .depth = xfer->box.depth,
2667 };
2668 iris_transfer_flush_region(ctx, xfer, &flush_box);
2669 }
2670
2671 if (map->unmap)
2672 map->unmap(map);
2673
2674 pipe_resource_reference(&xfer->resource, NULL);
2675
2676 if (xfer->usage & PIPE_MAP_THREAD_SAFE) {
2677 free(map);
2678 } else {
2679 /* transfer_unmap is called from the driver thread, so we have to use
2680 * transfer_pool, not transfer_pool_unsync. Freeing an object into a
2681 * different pool is allowed, however.
2682 */
2683 slab_free(&ice->transfer_pool, map);
2684 }
2685 }
2686
2687 /**
2688 * The pipe->texture_subdata() driver hook.
2689 *
2690 * Mesa's state tracker takes this path whenever possible, even with
2691 * PIPE_CAP_TEXTURE_TRANSFER_MODES set.
2692 */
2693 static void
iris_texture_subdata(struct pipe_context * ctx,struct pipe_resource * resource,unsigned level,unsigned usage,const struct pipe_box * box,const void * data,unsigned stride,uintptr_t layer_stride)2694 iris_texture_subdata(struct pipe_context *ctx,
2695 struct pipe_resource *resource,
2696 unsigned level,
2697 unsigned usage,
2698 const struct pipe_box *box,
2699 const void *data,
2700 unsigned stride,
2701 uintptr_t layer_stride)
2702 {
2703 struct iris_context *ice = (struct iris_context *)ctx;
2704 struct iris_resource *res = (struct iris_resource *)resource;
2705 const struct isl_surf *surf = &res->surf;
2706
2707 assert(resource->target != PIPE_BUFFER);
2708
2709 /* Just use the transfer-based path for linear buffers - it will already
2710 * do a direct mapping, or a simple linear staging buffer.
2711 *
2712 * Linear staging buffers appear to be better than tiled ones, too, so
2713 * take that path if we need the GPU to perform color compression, or
2714 * stall-avoidance blits.
2715 *
2716 * TODO: Teach isl_memcpy_linear_to_tiled about Tile64...
2717 */
2718 if (surf->tiling == ISL_TILING_LINEAR ||
2719 isl_tiling_is_64(res->surf.tiling) ||
2720 isl_aux_usage_has_compression(res->aux.usage) ||
2721 resource_is_busy(ice, res) ||
2722 iris_bo_mmap_mode(res->bo) == IRIS_MMAP_NONE) {
2723 return u_default_texture_subdata(ctx, resource, level, usage, box,
2724 data, stride, layer_stride);
2725 }
2726
2727 /* No state trackers pass any flags other than PIPE_MAP_WRITE */
2728
2729 iris_resource_access_raw(ice, res, level, box->z, box->depth, true);
2730
2731 iris_foreach_batch(ice, batch) {
2732 if (iris_batch_references(batch, res->bo))
2733 iris_batch_flush(batch);
2734 }
2735
2736 uint8_t *dst = iris_bo_map(&ice->dbg, res->bo, MAP_WRITE | MAP_RAW);
2737
2738 for (int s = 0; s < box->depth; s++) {
2739 const uint8_t *src = data + s * layer_stride;
2740
2741 if (surf->tiling == ISL_TILING_W) {
2742 unsigned x0_el, y0_el;
2743 get_image_offset_el(surf, level, box->z + s, &x0_el, &y0_el);
2744
2745 for (unsigned y = 0; y < box->height; y++) {
2746 for (unsigned x = 0; x < box->width; x++) {
2747 ptrdiff_t offset = s8_offset(surf->row_pitch_B,
2748 x0_el + box->x + x,
2749 y0_el + box->y + y);
2750 dst[offset] = src[y * stride + x];
2751 }
2752 }
2753 } else {
2754 unsigned x1, x2, y1, y2;
2755
2756 tile_extents(surf, box, level, s, &x1, &x2, &y1, &y2);
2757
2758 isl_memcpy_linear_to_tiled(x1, x2, y1, y2,
2759 (void *)dst, (void *)src,
2760 surf->row_pitch_B, stride,
2761 false, surf->tiling, ISL_MEMCPY);
2762 }
2763 }
2764 }
2765
2766 /**
2767 * Mark state dirty that needs to be re-emitted when a resource is written.
2768 */
2769 void
iris_dirty_for_history(struct iris_context * ice,struct iris_resource * res)2770 iris_dirty_for_history(struct iris_context *ice,
2771 struct iris_resource *res)
2772 {
2773 const uint64_t stages = res->bind_stages;
2774 uint64_t dirty = 0ull;
2775 uint64_t stage_dirty = 0ull;
2776
2777 if (res->bind_history & PIPE_BIND_CONSTANT_BUFFER) {
2778 for (unsigned stage = 0; stage < MESA_SHADER_STAGES; stage++) {
2779 if (stages & (1u << stage)) {
2780 struct iris_shader_state *shs = &ice->state.shaders[stage];
2781 shs->dirty_cbufs |= ~0u;
2782 }
2783 }
2784 dirty |= IRIS_DIRTY_RENDER_MISC_BUFFER_FLUSHES |
2785 IRIS_DIRTY_COMPUTE_MISC_BUFFER_FLUSHES;
2786 stage_dirty |= (stages << IRIS_SHIFT_FOR_STAGE_DIRTY_CONSTANTS);
2787 }
2788
2789 if (res->bind_history & (PIPE_BIND_SAMPLER_VIEW |
2790 PIPE_BIND_SHADER_IMAGE)) {
2791 dirty |= IRIS_DIRTY_RENDER_RESOLVES_AND_FLUSHES |
2792 IRIS_DIRTY_COMPUTE_RESOLVES_AND_FLUSHES;
2793 stage_dirty |= (stages << IRIS_SHIFT_FOR_STAGE_DIRTY_BINDINGS);
2794 }
2795
2796 if (res->bind_history & PIPE_BIND_SHADER_BUFFER) {
2797 dirty |= IRIS_DIRTY_RENDER_MISC_BUFFER_FLUSHES |
2798 IRIS_DIRTY_COMPUTE_MISC_BUFFER_FLUSHES;
2799 stage_dirty |= (stages << IRIS_SHIFT_FOR_STAGE_DIRTY_BINDINGS);
2800 }
2801
2802 if (res->bind_history & PIPE_BIND_VERTEX_BUFFER)
2803 dirty |= IRIS_DIRTY_VERTEX_BUFFER_FLUSHES;
2804
2805 if (ice->state.streamout_active && (res->bind_history & PIPE_BIND_STREAM_OUTPUT))
2806 dirty |= IRIS_DIRTY_SO_BUFFERS;
2807
2808 ice->state.dirty |= dirty;
2809 ice->state.stage_dirty |= stage_dirty;
2810 }
2811
2812 bool
iris_resource_set_clear_color(struct iris_context * ice,struct iris_resource * res,union isl_color_value color)2813 iris_resource_set_clear_color(struct iris_context *ice,
2814 struct iris_resource *res,
2815 union isl_color_value color)
2816 {
2817 if (res->aux.clear_color_unknown ||
2818 memcmp(&res->aux.clear_color, &color, sizeof(color)) != 0) {
2819 res->aux.clear_color = color;
2820 res->aux.clear_color_unknown = false;
2821 return true;
2822 }
2823
2824 return false;
2825 }
2826
2827 static enum pipe_format
iris_resource_get_internal_format(struct pipe_resource * p_res)2828 iris_resource_get_internal_format(struct pipe_resource *p_res)
2829 {
2830 struct iris_resource *res = (void *) p_res;
2831 return res->internal_format;
2832 }
2833
2834 static const struct u_transfer_vtbl transfer_vtbl = {
2835 .resource_create = iris_resource_create,
2836 .resource_destroy = iris_resource_destroy,
2837 .transfer_map = iris_transfer_map,
2838 .transfer_unmap = iris_transfer_unmap,
2839 .transfer_flush_region = iris_transfer_flush_region,
2840 .get_internal_format = iris_resource_get_internal_format,
2841 .set_stencil = iris_resource_set_separate_stencil,
2842 .get_stencil = iris_resource_get_separate_stencil,
2843 };
2844
2845 void
iris_init_screen_resource_functions(struct pipe_screen * pscreen)2846 iris_init_screen_resource_functions(struct pipe_screen *pscreen)
2847 {
2848 pscreen->query_dmabuf_modifiers = iris_query_dmabuf_modifiers;
2849 pscreen->is_dmabuf_modifier_supported = iris_is_dmabuf_modifier_supported;
2850 pscreen->get_dmabuf_modifier_planes = iris_get_dmabuf_modifier_planes;
2851 pscreen->resource_create_with_modifiers =
2852 iris_resource_create_with_modifiers;
2853 pscreen->resource_create = u_transfer_helper_resource_create;
2854 pscreen->resource_from_user_memory = iris_resource_from_user_memory;
2855 pscreen->resource_from_handle = iris_resource_from_handle;
2856 pscreen->resource_from_memobj = iris_resource_from_memobj_wrapper;
2857 pscreen->resource_get_handle = iris_resource_get_handle;
2858 pscreen->resource_get_param = iris_resource_get_param;
2859 pscreen->resource_destroy = u_transfer_helper_resource_destroy;
2860 pscreen->memobj_create_from_handle = iris_memobj_create_from_handle;
2861 pscreen->memobj_destroy = iris_memobj_destroy;
2862 pscreen->transfer_helper =
2863 u_transfer_helper_create(&transfer_vtbl,
2864 U_TRANSFER_HELPER_SEPARATE_Z32S8 |
2865 U_TRANSFER_HELPER_SEPARATE_STENCIL |
2866 U_TRANSFER_HELPER_MSAA_MAP);
2867 }
2868
2869 void
iris_init_resource_functions(struct pipe_context * ctx)2870 iris_init_resource_functions(struct pipe_context *ctx)
2871 {
2872 ctx->flush_resource = iris_flush_resource;
2873 ctx->invalidate_resource = iris_invalidate_resource;
2874 ctx->buffer_map = u_transfer_helper_transfer_map;
2875 ctx->texture_map = u_transfer_helper_transfer_map;
2876 ctx->transfer_flush_region = u_transfer_helper_transfer_flush_region;
2877 ctx->buffer_unmap = u_transfer_helper_transfer_unmap;
2878 ctx->texture_unmap = u_transfer_helper_transfer_unmap;
2879 ctx->buffer_subdata = u_default_buffer_subdata;
2880 ctx->clear_buffer = u_default_clear_buffer;
2881 ctx->texture_subdata = iris_texture_subdata;
2882 }
2883