iris: Add and use iris_resource_configure_main
[mesa.git] / src / gallium / drivers / iris / iris_resource.c
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/os_memory.h"
38 #include "util/u_cpu_detect.h"
39 #include "util/u_inlines.h"
40 #include "util/format/u_format.h"
41 #include "util/u_threaded_context.h"
42 #include "util/u_transfer.h"
43 #include "util/u_transfer_helper.h"
44 #include "util/u_upload_mgr.h"
45 #include "util/ralloc.h"
46 #include "iris_batch.h"
47 #include "iris_context.h"
48 #include "iris_resource.h"
49 #include "iris_screen.h"
50 #include "intel/common/gen_aux_map.h"
51 #include "intel/dev/gen_debug.h"
52 #include "isl/isl.h"
53 #include "drm-uapi/drm_fourcc.h"
54 #include "drm-uapi/i915_drm.h"
55
56 enum modifier_priority {
57 MODIFIER_PRIORITY_INVALID = 0,
58 MODIFIER_PRIORITY_LINEAR,
59 MODIFIER_PRIORITY_X,
60 MODIFIER_PRIORITY_Y,
61 MODIFIER_PRIORITY_Y_CCS,
62 MODIFIER_PRIORITY_Y_GEN12_RC_CCS,
63 };
64
65 static const uint64_t priority_to_modifier[] = {
66 [MODIFIER_PRIORITY_INVALID] = DRM_FORMAT_MOD_INVALID,
67 [MODIFIER_PRIORITY_LINEAR] = DRM_FORMAT_MOD_LINEAR,
68 [MODIFIER_PRIORITY_X] = I915_FORMAT_MOD_X_TILED,
69 [MODIFIER_PRIORITY_Y] = I915_FORMAT_MOD_Y_TILED,
70 [MODIFIER_PRIORITY_Y_CCS] = I915_FORMAT_MOD_Y_TILED_CCS,
71 [MODIFIER_PRIORITY_Y_GEN12_RC_CCS] = I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS,
72 };
73
74 static bool
75 modifier_is_supported(const struct gen_device_info *devinfo,
76 enum pipe_format pfmt, uint64_t modifier)
77 {
78 /* Check for basic device support. */
79 switch (modifier) {
80 case DRM_FORMAT_MOD_LINEAR:
81 case I915_FORMAT_MOD_X_TILED:
82 case I915_FORMAT_MOD_Y_TILED:
83 break;
84 case I915_FORMAT_MOD_Y_TILED_CCS:
85 if (devinfo->gen <= 8 || devinfo->gen >= 12)
86 return false;
87 break;
88 case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
89 if (devinfo->gen != 12)
90 return false;
91 break;
92 case DRM_FORMAT_MOD_INVALID:
93 default:
94 return false;
95 }
96
97 /* Check remaining requirements. */
98 switch (modifier) {
99 case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
100 case I915_FORMAT_MOD_Y_TILED_CCS: {
101 if (unlikely(INTEL_DEBUG & DEBUG_NO_RBC))
102 return false;
103
104 enum isl_format rt_format =
105 iris_format_for_usage(devinfo, pfmt,
106 ISL_SURF_USAGE_RENDER_TARGET_BIT).fmt;
107
108 if (rt_format == ISL_FORMAT_UNSUPPORTED ||
109 !isl_format_supports_ccs_e(devinfo, rt_format))
110 return false;
111 break;
112 }
113 default:
114 break;
115 }
116
117 return true;
118 }
119
120 static uint64_t
121 select_best_modifier(struct gen_device_info *devinfo, enum pipe_format pfmt,
122 const uint64_t *modifiers,
123 int count)
124 {
125 enum modifier_priority prio = MODIFIER_PRIORITY_INVALID;
126
127 for (int i = 0; i < count; i++) {
128 if (!modifier_is_supported(devinfo, pfmt, modifiers[i]))
129 continue;
130
131 switch (modifiers[i]) {
132 case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
133 prio = MAX2(prio, MODIFIER_PRIORITY_Y_GEN12_RC_CCS);
134 break;
135 case I915_FORMAT_MOD_Y_TILED_CCS:
136 prio = MAX2(prio, MODIFIER_PRIORITY_Y_CCS);
137 break;
138 case I915_FORMAT_MOD_Y_TILED:
139 prio = MAX2(prio, MODIFIER_PRIORITY_Y);
140 break;
141 case I915_FORMAT_MOD_X_TILED:
142 prio = MAX2(prio, MODIFIER_PRIORITY_X);
143 break;
144 case DRM_FORMAT_MOD_LINEAR:
145 prio = MAX2(prio, MODIFIER_PRIORITY_LINEAR);
146 break;
147 case DRM_FORMAT_MOD_INVALID:
148 default:
149 break;
150 }
151 }
152
153 return priority_to_modifier[prio];
154 }
155
156 enum isl_surf_dim
157 target_to_isl_surf_dim(enum pipe_texture_target target)
158 {
159 switch (target) {
160 case PIPE_BUFFER:
161 case PIPE_TEXTURE_1D:
162 case PIPE_TEXTURE_1D_ARRAY:
163 return ISL_SURF_DIM_1D;
164 case PIPE_TEXTURE_2D:
165 case PIPE_TEXTURE_CUBE:
166 case PIPE_TEXTURE_RECT:
167 case PIPE_TEXTURE_2D_ARRAY:
168 case PIPE_TEXTURE_CUBE_ARRAY:
169 return ISL_SURF_DIM_2D;
170 case PIPE_TEXTURE_3D:
171 return ISL_SURF_DIM_3D;
172 case PIPE_MAX_TEXTURE_TYPES:
173 break;
174 }
175 unreachable("invalid texture type");
176 }
177
178 static void
179 iris_query_dmabuf_modifiers(struct pipe_screen *pscreen,
180 enum pipe_format pfmt,
181 int max,
182 uint64_t *modifiers,
183 unsigned int *external_only,
184 int *count)
185 {
186 struct iris_screen *screen = (void *) pscreen;
187 const struct gen_device_info *devinfo = &screen->devinfo;
188
189 uint64_t all_modifiers[] = {
190 DRM_FORMAT_MOD_LINEAR,
191 I915_FORMAT_MOD_X_TILED,
192 I915_FORMAT_MOD_Y_TILED,
193 I915_FORMAT_MOD_Y_TILED_CCS,
194 I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS,
195 };
196
197 int supported_mods = 0;
198
199 for (int i = 0; i < ARRAY_SIZE(all_modifiers); i++) {
200 if (!modifier_is_supported(devinfo, pfmt, all_modifiers[i]))
201 continue;
202
203 if (supported_mods < max) {
204 if (modifiers)
205 modifiers[supported_mods] = all_modifiers[i];
206
207 if (external_only)
208 external_only[supported_mods] = util_format_is_yuv(pfmt);
209 }
210
211 supported_mods++;
212 }
213
214 *count = supported_mods;
215 }
216
217 enum isl_format
218 iris_image_view_get_format(struct iris_context *ice,
219 const struct pipe_image_view *img)
220 {
221 struct iris_screen *screen = (struct iris_screen *)ice->ctx.screen;
222 const struct gen_device_info *devinfo = &screen->devinfo;
223
224 isl_surf_usage_flags_t usage = ISL_SURF_USAGE_STORAGE_BIT;
225 enum isl_format isl_fmt =
226 iris_format_for_usage(devinfo, img->format, usage).fmt;
227
228 if (img->shader_access & PIPE_IMAGE_ACCESS_READ) {
229 /* On Gen8, try to use typed surfaces reads (which support a
230 * limited number of formats), and if not possible, fall back
231 * to untyped reads.
232 */
233 if (devinfo->gen == 8 &&
234 !isl_has_matching_typed_storage_image_format(devinfo, isl_fmt))
235 return ISL_FORMAT_RAW;
236 else
237 return isl_lower_storage_image_format(devinfo, isl_fmt);
238 }
239
240 return isl_fmt;
241 }
242
243 struct pipe_resource *
244 iris_resource_get_separate_stencil(struct pipe_resource *p_res)
245 {
246 /* For packed depth-stencil, we treat depth as the primary resource
247 * and store S8 as the "second plane" resource.
248 */
249 if (p_res->next && p_res->next->format == PIPE_FORMAT_S8_UINT)
250 return p_res->next;
251
252 return NULL;
253
254 }
255
256 static void
257 iris_resource_set_separate_stencil(struct pipe_resource *p_res,
258 struct pipe_resource *stencil)
259 {
260 assert(util_format_has_depth(util_format_description(p_res->format)));
261 pipe_resource_reference(&p_res->next, stencil);
262 }
263
264 void
265 iris_get_depth_stencil_resources(struct pipe_resource *res,
266 struct iris_resource **out_z,
267 struct iris_resource **out_s)
268 {
269 if (!res) {
270 *out_z = NULL;
271 *out_s = NULL;
272 return;
273 }
274
275 if (res->format != PIPE_FORMAT_S8_UINT) {
276 *out_z = (void *) res;
277 *out_s = (void *) iris_resource_get_separate_stencil(res);
278 } else {
279 *out_z = NULL;
280 *out_s = (void *) res;
281 }
282 }
283
284 enum isl_dim_layout
285 iris_get_isl_dim_layout(const struct gen_device_info *devinfo,
286 enum isl_tiling tiling,
287 enum pipe_texture_target target)
288 {
289 switch (target) {
290 case PIPE_TEXTURE_1D:
291 case PIPE_TEXTURE_1D_ARRAY:
292 return (devinfo->gen >= 9 && tiling == ISL_TILING_LINEAR ?
293 ISL_DIM_LAYOUT_GEN9_1D : ISL_DIM_LAYOUT_GEN4_2D);
294
295 case PIPE_TEXTURE_2D:
296 case PIPE_TEXTURE_2D_ARRAY:
297 case PIPE_TEXTURE_RECT:
298 case PIPE_TEXTURE_CUBE:
299 case PIPE_TEXTURE_CUBE_ARRAY:
300 return ISL_DIM_LAYOUT_GEN4_2D;
301
302 case PIPE_TEXTURE_3D:
303 return (devinfo->gen >= 9 ?
304 ISL_DIM_LAYOUT_GEN4_2D : ISL_DIM_LAYOUT_GEN4_3D);
305
306 case PIPE_MAX_TEXTURE_TYPES:
307 case PIPE_BUFFER:
308 break;
309 }
310 unreachable("invalid texture type");
311 }
312
313 void
314 iris_resource_disable_aux(struct iris_resource *res)
315 {
316 iris_bo_unreference(res->aux.bo);
317 iris_bo_unreference(res->aux.clear_color_bo);
318 free(res->aux.state);
319
320 res->aux.usage = ISL_AUX_USAGE_NONE;
321 res->aux.possible_usages = 1 << ISL_AUX_USAGE_NONE;
322 res->aux.sampler_usages = 1 << ISL_AUX_USAGE_NONE;
323 res->aux.has_hiz = 0;
324 res->aux.surf.size_B = 0;
325 res->aux.bo = NULL;
326 res->aux.extra_aux.surf.size_B = 0;
327 res->aux.clear_color_bo = NULL;
328 res->aux.state = NULL;
329 }
330
331 static void
332 iris_resource_destroy(struct pipe_screen *screen,
333 struct pipe_resource *resource)
334 {
335 struct iris_resource *res = (struct iris_resource *)resource;
336
337 if (resource->target == PIPE_BUFFER)
338 util_range_destroy(&res->valid_buffer_range);
339
340 iris_resource_disable_aux(res);
341
342 iris_bo_unreference(res->bo);
343 iris_pscreen_unref(res->base.screen);
344
345 free(res);
346 }
347
348 static struct iris_resource *
349 iris_alloc_resource(struct pipe_screen *pscreen,
350 const struct pipe_resource *templ)
351 {
352 struct iris_resource *res = calloc(1, sizeof(struct iris_resource));
353 if (!res)
354 return NULL;
355
356 res->base = *templ;
357 res->base.screen = iris_pscreen_ref(pscreen);
358 pipe_reference_init(&res->base.reference, 1);
359
360 res->aux.possible_usages = 1 << ISL_AUX_USAGE_NONE;
361 res->aux.sampler_usages = 1 << ISL_AUX_USAGE_NONE;
362
363 if (templ->target == PIPE_BUFFER)
364 util_range_init(&res->valid_buffer_range);
365
366 return res;
367 }
368
369 unsigned
370 iris_get_num_logical_layers(const struct iris_resource *res, unsigned level)
371 {
372 if (res->surf.dim == ISL_SURF_DIM_3D)
373 return minify(res->surf.logical_level0_px.depth, level);
374 else
375 return res->surf.logical_level0_px.array_len;
376 }
377
378 static enum isl_aux_state **
379 create_aux_state_map(struct iris_resource *res, enum isl_aux_state initial)
380 {
381 assert(res->aux.state == NULL);
382
383 uint32_t total_slices = 0;
384 for (uint32_t level = 0; level < res->surf.levels; level++)
385 total_slices += iris_get_num_logical_layers(res, level);
386
387 const size_t per_level_array_size =
388 res->surf.levels * sizeof(enum isl_aux_state *);
389
390 /* We're going to allocate a single chunk of data for both the per-level
391 * reference array and the arrays of aux_state. This makes cleanup
392 * significantly easier.
393 */
394 const size_t total_size =
395 per_level_array_size + total_slices * sizeof(enum isl_aux_state);
396
397 void *data = malloc(total_size);
398 if (!data)
399 return NULL;
400
401 enum isl_aux_state **per_level_arr = data;
402 enum isl_aux_state *s = data + per_level_array_size;
403 for (uint32_t level = 0; level < res->surf.levels; level++) {
404 per_level_arr[level] = s;
405 const unsigned level_layers = iris_get_num_logical_layers(res, level);
406 for (uint32_t a = 0; a < level_layers; a++)
407 *(s++) = initial;
408 }
409 assert((void *)s == data + total_size);
410
411 return per_level_arr;
412 }
413
414 static unsigned
415 iris_get_aux_clear_color_state_size(struct iris_screen *screen)
416 {
417 const struct gen_device_info *devinfo = &screen->devinfo;
418 return devinfo->gen >= 10 ? screen->isl_dev.ss.clear_color_state_size : 0;
419 }
420
421 static void
422 map_aux_addresses(struct iris_screen *screen, struct iris_resource *res)
423 {
424 const struct gen_device_info *devinfo = &screen->devinfo;
425 if (devinfo->gen >= 12 && isl_aux_usage_has_ccs(res->aux.usage)) {
426 void *aux_map_ctx = iris_bufmgr_get_aux_map_context(screen->bufmgr);
427 assert(aux_map_ctx);
428 const unsigned aux_offset = res->aux.extra_aux.surf.size_B > 0 ?
429 res->aux.extra_aux.offset : res->aux.offset;
430 gen_aux_map_add_image(aux_map_ctx, &res->surf, res->bo->gtt_offset,
431 res->aux.bo->gtt_offset + aux_offset);
432 res->bo->aux_map_address = res->aux.bo->gtt_offset;
433 }
434 }
435
436 static bool
437 want_ccs_e_for_format(const struct gen_device_info *devinfo,
438 enum isl_format format)
439 {
440 if (!isl_format_supports_ccs_e(devinfo, format))
441 return false;
442
443 const struct isl_format_layout *fmtl = isl_format_get_layout(format);
444
445 /* CCS_E seems to significantly hurt performance with 32-bit floating
446 * point formats. For example, Paraview's "Wavelet Volume" case uses
447 * both R32_FLOAT and R32G32B32A32_FLOAT, and enabling CCS_E for those
448 * formats causes a 62% FPS drop.
449 *
450 * However, many benchmarks seem to use 16-bit float with no issues.
451 */
452 if (fmtl->channels.r.bits == 32 && fmtl->channels.r.type == ISL_SFLOAT)
453 return false;
454
455 return true;
456 }
457
458 static bool
459 iris_resource_configure_main(const struct iris_screen *screen,
460 struct iris_resource *res,
461 const struct pipe_resource *templ,
462 uint64_t modifier, uint32_t row_pitch_B)
463 {
464 res->mod_info = isl_drm_modifier_get_info(modifier);
465
466 if (modifier != DRM_FORMAT_MOD_INVALID && res->mod_info == NULL)
467 return false;
468
469 isl_tiling_flags_t tiling_flags = 0;
470
471 if (res->mod_info != NULL) {
472 tiling_flags = 1 << res->mod_info->tiling;
473 } else if (templ->usage == PIPE_USAGE_STAGING ||
474 templ->bind & (PIPE_BIND_LINEAR | PIPE_BIND_CURSOR)) {
475 tiling_flags = ISL_TILING_LINEAR_BIT;
476 } else if (templ->bind & PIPE_BIND_SCANOUT) {
477 tiling_flags = screen->devinfo.has_tiling_uapi ?
478 ISL_TILING_X_BIT : ISL_TILING_LINEAR_BIT;
479 } else {
480 tiling_flags = ISL_TILING_ANY_MASK;
481 }
482
483 isl_surf_usage_flags_t usage = 0;
484
485 if (templ->bind & PIPE_BIND_RENDER_TARGET)
486 usage |= ISL_SURF_USAGE_RENDER_TARGET_BIT;
487
488 if (templ->bind & PIPE_BIND_SAMPLER_VIEW)
489 usage |= ISL_SURF_USAGE_TEXTURE_BIT;
490
491 if (templ->bind & PIPE_BIND_SHADER_IMAGE)
492 usage |= ISL_SURF_USAGE_STORAGE_BIT;
493
494 if (templ->bind & PIPE_BIND_SCANOUT)
495 usage |= ISL_SURF_USAGE_DISPLAY_BIT;
496
497 if (templ->target == PIPE_TEXTURE_CUBE ||
498 templ->target == PIPE_TEXTURE_CUBE_ARRAY) {
499 usage |= ISL_SURF_USAGE_CUBE_BIT;
500 }
501
502 if (templ->usage != PIPE_USAGE_STAGING &&
503 util_format_is_depth_or_stencil(templ->format)) {
504
505 /* Should be handled by u_transfer_helper */
506 assert(!util_format_is_depth_and_stencil(templ->format));
507
508 usage |= templ->format == PIPE_FORMAT_S8_UINT ?
509 ISL_SURF_USAGE_STENCIL_BIT : ISL_SURF_USAGE_DEPTH_BIT;
510 }
511
512 const enum isl_format format =
513 iris_format_for_usage(&screen->devinfo, templ->format, usage).fmt;
514
515 const struct isl_surf_init_info init_info = {
516 .dim = target_to_isl_surf_dim(templ->target),
517 .format = format,
518 .width = templ->width0,
519 .height = templ->height0,
520 .depth = templ->depth0,
521 .levels = templ->last_level + 1,
522 .array_len = templ->array_size,
523 .samples = MAX2(templ->nr_samples, 1),
524 .min_alignment_B = 0,
525 .row_pitch_B = row_pitch_B,
526 .usage = usage,
527 .tiling_flags = tiling_flags
528 };
529
530 if (!isl_surf_init_s(&screen->isl_dev, &res->surf, &init_info))
531 return false;
532
533 res->internal_format = templ->format;
534
535 return true;
536 }
537
538 /**
539 * Configure aux for the resource, but don't allocate it. For images which
540 * might be shared with modifiers, we must allocate the image and aux data in
541 * a single bo.
542 *
543 * Returns false on unexpected error (e.g. allocation failed, or invalid
544 * configuration result).
545 */
546 static bool
547 iris_resource_configure_aux(struct iris_screen *screen,
548 struct iris_resource *res, bool imported)
549 {
550 const struct gen_device_info *devinfo = &screen->devinfo;
551
552 /* Try to create the auxiliary surfaces allowed by the modifier or by
553 * the user if no modifier is specified.
554 */
555 assert(!res->mod_info ||
556 res->mod_info->aux_usage == ISL_AUX_USAGE_NONE ||
557 res->mod_info->aux_usage == ISL_AUX_USAGE_CCS_E ||
558 res->mod_info->aux_usage == ISL_AUX_USAGE_GEN12_CCS_E);
559
560 const bool has_mcs = !res->mod_info &&
561 isl_surf_get_mcs_surf(&screen->isl_dev, &res->surf, &res->aux.surf);
562
563 const bool has_hiz = !res->mod_info && !(INTEL_DEBUG & DEBUG_NO_HIZ) &&
564 isl_surf_get_hiz_surf(&screen->isl_dev, &res->surf, &res->aux.surf);
565
566 const bool has_ccs =
567 ((!res->mod_info && !(INTEL_DEBUG & DEBUG_NO_RBC)) ||
568 (res->mod_info && res->mod_info->aux_usage != ISL_AUX_USAGE_NONE)) &&
569 isl_surf_get_ccs_surf(&screen->isl_dev, &res->surf, &res->aux.surf,
570 &res->aux.extra_aux.surf, 0);
571
572 /* Having both HIZ and MCS is impossible. */
573 assert(!has_mcs || !has_hiz);
574
575 /* Ensure aux surface creation for MCS_CCS and HIZ_CCS is correct. */
576 if (has_ccs && (has_mcs || has_hiz)) {
577 assert(res->aux.extra_aux.surf.size_B > 0 &&
578 res->aux.extra_aux.surf.usage & ISL_SURF_USAGE_CCS_BIT);
579 assert(res->aux.surf.size_B > 0 &&
580 res->aux.surf.usage &
581 (ISL_SURF_USAGE_HIZ_BIT | ISL_SURF_USAGE_MCS_BIT));
582 }
583
584 if (res->mod_info && has_ccs) {
585 /* Only allow a CCS modifier if the aux was created successfully. */
586 res->aux.possible_usages |= 1 << res->mod_info->aux_usage;
587 } else if (has_mcs) {
588 res->aux.possible_usages |=
589 1 << (has_ccs ? ISL_AUX_USAGE_MCS_CCS : ISL_AUX_USAGE_MCS);
590 } else if (has_hiz) {
591 if (!has_ccs) {
592 res->aux.possible_usages |= 1 << ISL_AUX_USAGE_HIZ;
593 } else if (res->surf.samples == 1 &&
594 (res->surf.usage & ISL_SURF_USAGE_TEXTURE_BIT)) {
595 /* If this resource is single-sampled and will be used as a texture,
596 * put the HiZ surface in write-through mode so that we can sample
597 * from it.
598 */
599 res->aux.possible_usages |= 1 << ISL_AUX_USAGE_HIZ_CCS_WT;
600 } else {
601 res->aux.possible_usages |= 1 << ISL_AUX_USAGE_HIZ_CCS;
602 }
603 } else if (has_ccs && isl_surf_usage_is_stencil(res->surf.usage)) {
604 res->aux.possible_usages |= 1 << ISL_AUX_USAGE_STC_CCS;
605 } else if (has_ccs) {
606 if (want_ccs_e_for_format(devinfo, res->surf.format)) {
607 res->aux.possible_usages |= devinfo->gen < 12 ?
608 1 << ISL_AUX_USAGE_CCS_E : 1 << ISL_AUX_USAGE_GEN12_CCS_E;
609 } else if (isl_format_supports_ccs_d(devinfo, res->surf.format)) {
610 res->aux.possible_usages |= 1 << ISL_AUX_USAGE_CCS_D;
611 }
612 }
613
614 res->aux.usage = util_last_bit(res->aux.possible_usages) - 1;
615
616 res->aux.sampler_usages = res->aux.possible_usages;
617
618 /* We don't always support sampling with hiz. But when we do, it must be
619 * single sampled.
620 */
621 if (!devinfo->has_sample_with_hiz || res->surf.samples > 1)
622 res->aux.sampler_usages &= ~(1 << ISL_AUX_USAGE_HIZ);
623
624 /* ISL_AUX_USAGE_HIZ_CCS doesn't support sampling at all */
625 res->aux.sampler_usages &= ~(1 << ISL_AUX_USAGE_HIZ_CCS);
626
627 enum isl_aux_state initial_state;
628 assert(!res->aux.bo);
629
630 switch (res->aux.usage) {
631 case ISL_AUX_USAGE_NONE:
632 /* Update relevant fields to indicate that aux is disabled. */
633 iris_resource_disable_aux(res);
634
635 /* Having no aux buffer is only okay if there's no modifier with aux. */
636 return !res->mod_info || res->mod_info->aux_usage == ISL_AUX_USAGE_NONE;
637 case ISL_AUX_USAGE_HIZ:
638 case ISL_AUX_USAGE_HIZ_CCS:
639 case ISL_AUX_USAGE_HIZ_CCS_WT:
640 initial_state = ISL_AUX_STATE_AUX_INVALID;
641 break;
642 case ISL_AUX_USAGE_MCS:
643 case ISL_AUX_USAGE_MCS_CCS:
644 /* The Ivybridge PRM, Vol 2 Part 1 p326 says:
645 *
646 * "When MCS buffer is enabled and bound to MSRT, it is required
647 * that it is cleared prior to any rendering."
648 *
649 * Since we only use the MCS buffer for rendering, we just clear it
650 * immediately on allocation. The clear value for MCS buffers is all
651 * 1's, so we simply memset it to 0xff.
652 */
653 initial_state = ISL_AUX_STATE_CLEAR;
654 break;
655 case ISL_AUX_USAGE_CCS_D:
656 case ISL_AUX_USAGE_CCS_E:
657 case ISL_AUX_USAGE_GEN12_CCS_E:
658 case ISL_AUX_USAGE_STC_CCS:
659 /* When CCS_E is used, we need to ensure that the CCS starts off in
660 * a valid state. From the Sky Lake PRM, "MCS Buffer for Render
661 * Target(s)":
662 *
663 * "If Software wants to enable Color Compression without Fast
664 * clear, Software needs to initialize MCS with zeros."
665 *
666 * A CCS value of 0 indicates that the corresponding block is in the
667 * pass-through state which is what we want.
668 *
669 * For CCS_D, do the same thing. On Gen9+, this avoids having any
670 * undefined bits in the aux buffer.
671 */
672 if (imported) {
673 assert(res->aux.usage != ISL_AUX_USAGE_STC_CCS);
674 initial_state =
675 isl_drm_modifier_get_default_aux_state(res->mod_info->modifier);
676 } else {
677 initial_state = ISL_AUX_STATE_PASS_THROUGH;
678 }
679 break;
680 case ISL_AUX_USAGE_MC:
681 default:
682 unreachable("Unsupported aux mode");
683 }
684
685 /* Create the aux_state for the auxiliary buffer. */
686 res->aux.state = create_aux_state_map(res, initial_state);
687 if (!res->aux.state)
688 return false;
689
690 if (isl_aux_usage_has_hiz(res->aux.usage)) {
691 for (unsigned level = 0; level < res->surf.levels; ++level) {
692 uint32_t width = u_minify(res->surf.phys_level0_sa.width, level);
693 uint32_t height = u_minify(res->surf.phys_level0_sa.height, level);
694
695 /* Disable HiZ for LOD > 0 unless the width/height are 8x4 aligned.
696 * For LOD == 0, we can grow the dimensions to make it work.
697 */
698 if (level == 0 || ((width & 7) == 0 && (height & 3) == 0))
699 res->aux.has_hiz |= 1 << level;
700 }
701 }
702
703 return true;
704 }
705
706 /**
707 * Initialize the aux buffer contents.
708 *
709 * Returns false on unexpected error (e.g. mapping a BO failed).
710 */
711 static bool
712 iris_resource_init_aux_buf(struct iris_resource *res,
713 unsigned clear_color_state_size)
714 {
715 void *map = iris_bo_map(NULL, res->aux.bo, MAP_WRITE | MAP_RAW);
716
717 if (!map)
718 return false;
719
720 if (iris_resource_get_aux_state(res, 0, 0) != ISL_AUX_STATE_AUX_INVALID) {
721 /* See iris_resource_configure_aux for the memset_value rationale. */
722 uint8_t memset_value = isl_aux_usage_has_mcs(res->aux.usage) ? 0xFF : 0;
723 memset((char*)map + res->aux.offset, memset_value,
724 res->aux.surf.size_B);
725 }
726
727 memset((char*)map + res->aux.extra_aux.offset,
728 0, res->aux.extra_aux.surf.size_B);
729
730 /* Zero the indirect clear color to match ::fast_clear_color. */
731 memset((char *)map + res->aux.clear_color_offset, 0,
732 clear_color_state_size);
733
734 iris_bo_unmap(res->aux.bo);
735
736 if (clear_color_state_size > 0) {
737 res->aux.clear_color_bo = res->aux.bo;
738 iris_bo_reference(res->aux.clear_color_bo);
739 }
740
741 return true;
742 }
743
744 void
745 iris_resource_finish_aux_import(struct pipe_screen *pscreen,
746 struct iris_resource *res)
747 {
748 struct iris_screen *screen = (struct iris_screen *)pscreen;
749 assert(iris_resource_unfinished_aux_import(res));
750 assert(!res->mod_info->supports_clear_color);
751
752 struct iris_resource *aux_res = (void *) res->base.next;
753 assert(aux_res->aux.surf.row_pitch_B && aux_res->aux.offset &&
754 aux_res->aux.bo);
755
756 assert(res->bo == aux_res->aux.bo);
757 iris_bo_reference(aux_res->aux.bo);
758 res->aux.bo = aux_res->aux.bo;
759
760 res->aux.offset = aux_res->aux.offset;
761
762 assert(res->bo->size >= (res->aux.offset + res->aux.surf.size_B));
763 assert(res->aux.clear_color_bo == NULL);
764
765 assert(aux_res->aux.surf.row_pitch_B == res->aux.surf.row_pitch_B);
766
767 unsigned clear_color_state_size =
768 iris_get_aux_clear_color_state_size(screen);
769
770 if (clear_color_state_size > 0) {
771 res->aux.clear_color_bo =
772 iris_bo_alloc_tiled(screen->bufmgr, "clear color_buffer",
773 clear_color_state_size, 1, IRIS_MEMZONE_OTHER,
774 I915_TILING_NONE, 0, BO_ALLOC_ZEROED);
775 }
776
777 iris_resource_destroy(&screen->base, res->base.next);
778 res->base.next = NULL;
779
780 map_aux_addresses(screen, res);
781 }
782
783 static struct pipe_resource *
784 iris_resource_create_for_buffer(struct pipe_screen *pscreen,
785 const struct pipe_resource *templ)
786 {
787 struct iris_screen *screen = (struct iris_screen *)pscreen;
788 struct iris_resource *res = iris_alloc_resource(pscreen, templ);
789
790 assert(templ->target == PIPE_BUFFER);
791 assert(templ->height0 <= 1);
792 assert(templ->depth0 <= 1);
793 assert(templ->format == PIPE_FORMAT_NONE ||
794 util_format_get_blocksize(templ->format) == 1);
795
796 res->internal_format = templ->format;
797 res->surf.tiling = ISL_TILING_LINEAR;
798
799 enum iris_memory_zone memzone = IRIS_MEMZONE_OTHER;
800 const char *name = templ->target == PIPE_BUFFER ? "buffer" : "miptree";
801 if (templ->flags & IRIS_RESOURCE_FLAG_SHADER_MEMZONE) {
802 memzone = IRIS_MEMZONE_SHADER;
803 name = "shader kernels";
804 } else if (templ->flags & IRIS_RESOURCE_FLAG_SURFACE_MEMZONE) {
805 memzone = IRIS_MEMZONE_SURFACE;
806 name = "surface state";
807 } else if (templ->flags & IRIS_RESOURCE_FLAG_DYNAMIC_MEMZONE) {
808 memzone = IRIS_MEMZONE_DYNAMIC;
809 name = "dynamic state";
810 }
811
812 res->bo = iris_bo_alloc(screen->bufmgr, name, templ->width0, memzone);
813 if (!res->bo) {
814 iris_resource_destroy(pscreen, &res->base);
815 return NULL;
816 }
817
818 if (templ->bind & PIPE_BIND_SHARED)
819 iris_bo_make_external(res->bo);
820
821 return &res->base;
822 }
823
824 static struct pipe_resource *
825 iris_resource_create_with_modifiers(struct pipe_screen *pscreen,
826 const struct pipe_resource *templ,
827 const uint64_t *modifiers,
828 int modifiers_count)
829 {
830 struct iris_screen *screen = (struct iris_screen *)pscreen;
831 struct gen_device_info *devinfo = &screen->devinfo;
832 struct iris_resource *res = iris_alloc_resource(pscreen, templ);
833
834 if (!res)
835 return NULL;
836
837 uint64_t modifier =
838 select_best_modifier(devinfo, templ->format, modifiers, modifiers_count);
839
840 if (modifier == DRM_FORMAT_MOD_INVALID && modifiers_count > 0) {
841 fprintf(stderr, "Unsupported modifier, resource creation failed.\n");
842 goto fail;
843 }
844
845 UNUSED const bool isl_surf_created_successfully =
846 iris_resource_configure_main(screen, res, templ, modifier, 0);
847 assert(isl_surf_created_successfully);
848
849 const char *name = "miptree";
850 enum iris_memory_zone memzone = IRIS_MEMZONE_OTHER;
851
852 unsigned int flags = 0;
853 if (templ->usage == PIPE_USAGE_STAGING)
854 flags |= BO_ALLOC_COHERENT;
855
856 /* These are for u_upload_mgr buffers only */
857 assert(!(templ->flags & (IRIS_RESOURCE_FLAG_SHADER_MEMZONE |
858 IRIS_RESOURCE_FLAG_SURFACE_MEMZONE |
859 IRIS_RESOURCE_FLAG_DYNAMIC_MEMZONE)));
860
861 if (!iris_resource_configure_aux(screen, res, false))
862 goto fail;
863
864 /* Modifiers require the aux data to be in the same buffer as the main
865 * surface, but we combine them even when a modifier is not being used.
866 */
867 uint64_t bo_size = res->surf.size_B;
868
869 /* Allocate space for the aux buffer. */
870 if (res->aux.surf.size_B > 0) {
871 res->aux.offset = ALIGN(bo_size, res->aux.surf.alignment_B);
872 bo_size = res->aux.offset + res->aux.surf.size_B;
873 }
874
875 /* Allocate space for the extra aux buffer. */
876 if (res->aux.extra_aux.surf.size_B > 0) {
877 res->aux.extra_aux.offset =
878 ALIGN(bo_size, res->aux.extra_aux.surf.alignment_B);
879 bo_size = res->aux.extra_aux.offset + res->aux.extra_aux.surf.size_B;
880 }
881
882 /* Allocate space for the indirect clear color.
883 *
884 * Also add some padding to make sure the fast clear color state buffer
885 * starts at a 4K alignment. We believe that 256B might be enough, but due
886 * to lack of testing we will leave this as 4K for now.
887 */
888 if (res->aux.surf.size_B > 0) {
889 res->aux.clear_color_offset = ALIGN(bo_size, 4096);
890 bo_size = res->aux.clear_color_offset +
891 iris_get_aux_clear_color_state_size(screen);
892 }
893
894 uint32_t alignment = MAX2(4096, res->surf.alignment_B);
895 res->bo = iris_bo_alloc_tiled(screen->bufmgr, name, bo_size, alignment,
896 memzone,
897 isl_tiling_to_i915_tiling(res->surf.tiling),
898 res->surf.row_pitch_B, flags);
899
900 if (!res->bo)
901 goto fail;
902
903 if (res->aux.surf.size_B > 0) {
904 res->aux.bo = res->bo;
905 iris_bo_reference(res->aux.bo);
906 unsigned clear_color_state_size =
907 iris_get_aux_clear_color_state_size(screen);
908 if (!iris_resource_init_aux_buf(res, clear_color_state_size))
909 goto fail;
910 map_aux_addresses(screen, res);
911 }
912
913 if (templ->bind & PIPE_BIND_SHARED)
914 iris_bo_make_external(res->bo);
915
916 return &res->base;
917
918 fail:
919 fprintf(stderr, "XXX: resource creation failed\n");
920 iris_resource_destroy(pscreen, &res->base);
921 return NULL;
922
923 }
924
925 static struct pipe_resource *
926 iris_resource_create(struct pipe_screen *pscreen,
927 const struct pipe_resource *templ)
928 {
929 if (templ->target == PIPE_BUFFER)
930 return iris_resource_create_for_buffer(pscreen, templ);
931 else
932 return iris_resource_create_with_modifiers(pscreen, templ, NULL, 0);
933 }
934
935 static uint64_t
936 tiling_to_modifier(uint32_t tiling)
937 {
938 static const uint64_t map[] = {
939 [I915_TILING_NONE] = DRM_FORMAT_MOD_LINEAR,
940 [I915_TILING_X] = I915_FORMAT_MOD_X_TILED,
941 [I915_TILING_Y] = I915_FORMAT_MOD_Y_TILED,
942 };
943
944 assert(tiling < ARRAY_SIZE(map));
945
946 return map[tiling];
947 }
948
949 static struct pipe_resource *
950 iris_resource_from_user_memory(struct pipe_screen *pscreen,
951 const struct pipe_resource *templ,
952 void *user_memory)
953 {
954 struct iris_screen *screen = (struct iris_screen *)pscreen;
955 struct iris_bufmgr *bufmgr = screen->bufmgr;
956 struct iris_resource *res = iris_alloc_resource(pscreen, templ);
957 if (!res)
958 return NULL;
959
960 assert(templ->target == PIPE_BUFFER);
961
962 res->internal_format = templ->format;
963 res->bo = iris_bo_create_userptr(bufmgr, "user",
964 user_memory, templ->width0,
965 IRIS_MEMZONE_OTHER);
966 if (!res->bo) {
967 iris_resource_destroy(pscreen, &res->base);
968 return NULL;
969 }
970
971 util_range_add(&res->base, &res->valid_buffer_range, 0, templ->width0);
972
973 return &res->base;
974 }
975
976 static struct pipe_resource *
977 iris_resource_from_handle(struct pipe_screen *pscreen,
978 const struct pipe_resource *templ,
979 struct winsys_handle *whandle,
980 unsigned usage)
981 {
982 struct iris_screen *screen = (struct iris_screen *)pscreen;
983 struct iris_bufmgr *bufmgr = screen->bufmgr;
984 struct iris_resource *res = iris_alloc_resource(pscreen, templ);
985 if (!res)
986 return NULL;
987
988 switch (whandle->type) {
989 case WINSYS_HANDLE_TYPE_FD:
990 res->bo = iris_bo_import_dmabuf(bufmgr, whandle->handle,
991 whandle->modifier);
992 break;
993 case WINSYS_HANDLE_TYPE_SHARED:
994 res->bo = iris_bo_gem_create_from_name(bufmgr, "winsys image",
995 whandle->handle);
996 break;
997 default:
998 unreachable("invalid winsys handle type");
999 }
1000 if (!res->bo)
1001 goto fail;
1002
1003 res->offset = whandle->offset;
1004 res->external_format = whandle->format;
1005
1006 if (templ->target == PIPE_BUFFER) {
1007 res->surf.tiling = ISL_TILING_LINEAR;
1008 } else {
1009 /* Create a surface for each plane specified by the external format. */
1010 if (whandle->plane < util_format_get_num_planes(whandle->format)) {
1011
1012 const uint64_t modifier =
1013 whandle->modifier != DRM_FORMAT_MOD_INVALID ?
1014 whandle->modifier : tiling_to_modifier(res->bo->tiling_mode);
1015
1016 UNUSED const bool isl_surf_created_successfully =
1017 iris_resource_configure_main(screen, res, templ, modifier,
1018 whandle->stride);
1019 assert(isl_surf_created_successfully);
1020 assert(res->bo->tiling_mode ==
1021 isl_tiling_to_i915_tiling(res->surf.tiling));
1022
1023 UNUSED const bool ok = iris_resource_configure_aux(screen, res, true);
1024 assert(ok);
1025 /* The gallium dri layer will create a separate plane resource
1026 * for the aux image. iris_resource_finish_aux_import will
1027 * merge the separate aux parameters back into a single
1028 * iris_resource.
1029 */
1030 } else {
1031 /* Save modifier import information to reconstruct later. After
1032 * import, this will be available under a second image accessible
1033 * from the main image with res->base.next. See
1034 * iris_resource_finish_aux_import.
1035 */
1036 res->aux.surf.row_pitch_B = whandle->stride;
1037 res->aux.offset = whandle->offset;
1038 res->aux.bo = res->bo;
1039 res->bo = NULL;
1040 }
1041 }
1042
1043 return &res->base;
1044
1045 fail:
1046 iris_resource_destroy(pscreen, &res->base);
1047 return NULL;
1048 }
1049
1050 static void
1051 iris_flush_resource(struct pipe_context *ctx, struct pipe_resource *resource)
1052 {
1053 struct iris_context *ice = (struct iris_context *)ctx;
1054 struct iris_resource *res = (void *) resource;
1055 const struct isl_drm_modifier_info *mod = res->mod_info;
1056
1057 iris_resource_prepare_access(ice, res,
1058 0, INTEL_REMAINING_LEVELS,
1059 0, INTEL_REMAINING_LAYERS,
1060 mod ? mod->aux_usage : ISL_AUX_USAGE_NONE,
1061 mod ? mod->supports_clear_color : false);
1062 }
1063
1064 static void
1065 iris_resource_disable_aux_on_first_query(struct pipe_resource *resource,
1066 unsigned usage)
1067 {
1068 struct iris_resource *res = (struct iris_resource *)resource;
1069 bool mod_with_aux =
1070 res->mod_info && res->mod_info->aux_usage != ISL_AUX_USAGE_NONE;
1071
1072 /* Disable aux usage if explicit flush not set and this is the first time
1073 * we are dealing with this resource and the resource was not created with
1074 * a modifier with aux.
1075 */
1076 if (!mod_with_aux &&
1077 (!(usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH) && res->aux.usage != 0) &&
1078 p_atomic_read(&resource->reference.count) == 1) {
1079 iris_resource_disable_aux(res);
1080 }
1081 }
1082
1083 static bool
1084 iris_resource_get_param(struct pipe_screen *pscreen,
1085 struct pipe_context *context,
1086 struct pipe_resource *resource,
1087 unsigned plane,
1088 unsigned layer,
1089 enum pipe_resource_param param,
1090 unsigned handle_usage,
1091 uint64_t *value)
1092 {
1093 struct iris_screen *screen = (struct iris_screen *)pscreen;
1094 struct iris_resource *res = (struct iris_resource *)resource;
1095 bool mod_with_aux =
1096 res->mod_info && res->mod_info->aux_usage != ISL_AUX_USAGE_NONE;
1097 bool wants_aux = mod_with_aux && plane > 0;
1098 bool result;
1099 unsigned handle;
1100
1101 if (iris_resource_unfinished_aux_import(res))
1102 iris_resource_finish_aux_import(pscreen, res);
1103
1104 struct iris_bo *bo = wants_aux ? res->aux.bo : res->bo;
1105
1106 iris_resource_disable_aux_on_first_query(resource, handle_usage);
1107
1108 switch (param) {
1109 case PIPE_RESOURCE_PARAM_NPLANES:
1110 if (mod_with_aux) {
1111 *value = 2;
1112 } else {
1113 unsigned count = 0;
1114 for (struct pipe_resource *cur = resource; cur; cur = cur->next)
1115 count++;
1116 *value = count;
1117 }
1118 return true;
1119 case PIPE_RESOURCE_PARAM_STRIDE:
1120 *value = wants_aux ? res->aux.surf.row_pitch_B : res->surf.row_pitch_B;
1121 return true;
1122 case PIPE_RESOURCE_PARAM_OFFSET:
1123 *value = wants_aux ? res->aux.offset : 0;
1124 return true;
1125 case PIPE_RESOURCE_PARAM_MODIFIER:
1126 *value = res->mod_info ? res->mod_info->modifier :
1127 tiling_to_modifier(res->bo->tiling_mode);
1128 return true;
1129 case PIPE_RESOURCE_PARAM_HANDLE_TYPE_SHARED:
1130 result = iris_bo_flink(bo, &handle) == 0;
1131 if (result)
1132 *value = handle;
1133 return result;
1134 case PIPE_RESOURCE_PARAM_HANDLE_TYPE_KMS: {
1135 /* Because we share the same drm file across multiple iris_screen, when
1136 * we export a GEM handle we must make sure it is valid in the DRM file
1137 * descriptor the caller is using (this is the FD given at screen
1138 * creation).
1139 */
1140 uint32_t handle;
1141 if (iris_bo_export_gem_handle_for_device(bo, screen->winsys_fd, &handle))
1142 return false;
1143 *value = handle;
1144 return true;
1145 }
1146
1147 case PIPE_RESOURCE_PARAM_HANDLE_TYPE_FD:
1148 result = iris_bo_export_dmabuf(bo, (int *) &handle) == 0;
1149 if (result)
1150 *value = handle;
1151 return result;
1152 default:
1153 return false;
1154 }
1155 }
1156
1157 static bool
1158 iris_resource_get_handle(struct pipe_screen *pscreen,
1159 struct pipe_context *ctx,
1160 struct pipe_resource *resource,
1161 struct winsys_handle *whandle,
1162 unsigned usage)
1163 {
1164 struct iris_screen *screen = (struct iris_screen *) pscreen;
1165 struct iris_resource *res = (struct iris_resource *)resource;
1166 bool mod_with_aux =
1167 res->mod_info && res->mod_info->aux_usage != ISL_AUX_USAGE_NONE;
1168
1169 iris_resource_disable_aux_on_first_query(resource, usage);
1170
1171 struct iris_bo *bo;
1172 if (mod_with_aux && whandle->plane > 0) {
1173 assert(res->aux.bo);
1174 bo = res->aux.bo;
1175 whandle->stride = res->aux.surf.row_pitch_B;
1176 whandle->offset = res->aux.offset;
1177 } else {
1178 /* If this is a buffer, stride should be 0 - no need to special case */
1179 whandle->stride = res->surf.row_pitch_B;
1180 bo = res->bo;
1181 }
1182
1183 whandle->format = res->external_format;
1184 whandle->modifier =
1185 res->mod_info ? res->mod_info->modifier
1186 : tiling_to_modifier(res->bo->tiling_mode);
1187
1188 #ifndef NDEBUG
1189 enum isl_aux_usage allowed_usage =
1190 res->mod_info ? res->mod_info->aux_usage : ISL_AUX_USAGE_NONE;
1191
1192 if (res->aux.usage != allowed_usage) {
1193 enum isl_aux_state aux_state = iris_resource_get_aux_state(res, 0, 0);
1194 assert(aux_state == ISL_AUX_STATE_RESOLVED ||
1195 aux_state == ISL_AUX_STATE_PASS_THROUGH);
1196 }
1197 #endif
1198
1199 switch (whandle->type) {
1200 case WINSYS_HANDLE_TYPE_SHARED:
1201 return iris_bo_flink(bo, &whandle->handle) == 0;
1202 case WINSYS_HANDLE_TYPE_KMS: {
1203 /* Because we share the same drm file across multiple iris_screen, when
1204 * we export a GEM handle we must make sure it is valid in the DRM file
1205 * descriptor the caller is using (this is the FD given at screen
1206 * creation).
1207 */
1208 uint32_t handle;
1209 if (iris_bo_export_gem_handle_for_device(bo, screen->winsys_fd, &handle))
1210 return false;
1211 whandle->handle = handle;
1212 return true;
1213 }
1214 case WINSYS_HANDLE_TYPE_FD:
1215 return iris_bo_export_dmabuf(bo, (int *) &whandle->handle) == 0;
1216 }
1217
1218 return false;
1219 }
1220
1221 static bool
1222 resource_is_busy(struct iris_context *ice,
1223 struct iris_resource *res)
1224 {
1225 bool busy = iris_bo_busy(res->bo);
1226
1227 for (int i = 0; i < IRIS_BATCH_COUNT; i++)
1228 busy |= iris_batch_references(&ice->batches[i], res->bo);
1229
1230 return busy;
1231 }
1232
1233 static void
1234 iris_invalidate_resource(struct pipe_context *ctx,
1235 struct pipe_resource *resource)
1236 {
1237 struct iris_screen *screen = (void *) ctx->screen;
1238 struct iris_context *ice = (void *) ctx;
1239 struct iris_resource *res = (void *) resource;
1240
1241 if (resource->target != PIPE_BUFFER)
1242 return;
1243
1244 /* If it's already invalidated, don't bother doing anything. */
1245 if (res->valid_buffer_range.start > res->valid_buffer_range.end)
1246 return;
1247
1248 if (!resource_is_busy(ice, res)) {
1249 /* The resource is idle, so just mark that it contains no data and
1250 * keep using the same underlying buffer object.
1251 */
1252 util_range_set_empty(&res->valid_buffer_range);
1253 return;
1254 }
1255
1256 /* Otherwise, try and replace the backing storage with a new BO. */
1257
1258 /* We can't reallocate memory we didn't allocate in the first place. */
1259 if (res->bo->userptr)
1260 return;
1261
1262 // XXX: We should support this.
1263 if (res->bind_history & PIPE_BIND_STREAM_OUTPUT)
1264 return;
1265
1266 struct iris_bo *old_bo = res->bo;
1267 struct iris_bo *new_bo =
1268 iris_bo_alloc(screen->bufmgr, res->bo->name, resource->width0,
1269 iris_memzone_for_address(old_bo->gtt_offset));
1270 if (!new_bo)
1271 return;
1272
1273 /* Swap out the backing storage */
1274 res->bo = new_bo;
1275
1276 /* Rebind the buffer, replacing any state referring to the old BO's
1277 * address, and marking state dirty so it's reemitted.
1278 */
1279 screen->vtbl.rebind_buffer(ice, res);
1280
1281 util_range_set_empty(&res->valid_buffer_range);
1282
1283 iris_bo_unreference(old_bo);
1284 }
1285
1286 static void
1287 iris_flush_staging_region(struct pipe_transfer *xfer,
1288 const struct pipe_box *flush_box)
1289 {
1290 if (!(xfer->usage & PIPE_TRANSFER_WRITE))
1291 return;
1292
1293 struct iris_transfer *map = (void *) xfer;
1294
1295 struct pipe_box src_box = *flush_box;
1296
1297 /* Account for extra alignment padding in staging buffer */
1298 if (xfer->resource->target == PIPE_BUFFER)
1299 src_box.x += xfer->box.x % IRIS_MAP_BUFFER_ALIGNMENT;
1300
1301 struct pipe_box dst_box = (struct pipe_box) {
1302 .x = xfer->box.x + flush_box->x,
1303 .y = xfer->box.y + flush_box->y,
1304 .z = xfer->box.z + flush_box->z,
1305 .width = flush_box->width,
1306 .height = flush_box->height,
1307 .depth = flush_box->depth,
1308 };
1309
1310 iris_copy_region(map->blorp, map->batch, xfer->resource, xfer->level,
1311 dst_box.x, dst_box.y, dst_box.z, map->staging, 0,
1312 &src_box);
1313 }
1314
1315 static void
1316 iris_unmap_copy_region(struct iris_transfer *map)
1317 {
1318 iris_resource_destroy(map->staging->screen, map->staging);
1319
1320 map->ptr = NULL;
1321 }
1322
1323 static void
1324 iris_map_copy_region(struct iris_transfer *map)
1325 {
1326 struct pipe_screen *pscreen = &map->batch->screen->base;
1327 struct pipe_transfer *xfer = &map->base;
1328 struct pipe_box *box = &xfer->box;
1329 struct iris_resource *res = (void *) xfer->resource;
1330
1331 unsigned extra = xfer->resource->target == PIPE_BUFFER ?
1332 box->x % IRIS_MAP_BUFFER_ALIGNMENT : 0;
1333
1334 struct pipe_resource templ = (struct pipe_resource) {
1335 .usage = PIPE_USAGE_STAGING,
1336 .width0 = box->width + extra,
1337 .height0 = box->height,
1338 .depth0 = 1,
1339 .nr_samples = xfer->resource->nr_samples,
1340 .nr_storage_samples = xfer->resource->nr_storage_samples,
1341 .array_size = box->depth,
1342 .format = res->internal_format,
1343 };
1344
1345 if (xfer->resource->target == PIPE_BUFFER)
1346 templ.target = PIPE_BUFFER;
1347 else if (templ.array_size > 1)
1348 templ.target = PIPE_TEXTURE_2D_ARRAY;
1349 else
1350 templ.target = PIPE_TEXTURE_2D;
1351
1352 map->staging = iris_resource_create(pscreen, &templ);
1353 assert(map->staging);
1354
1355 if (templ.target != PIPE_BUFFER) {
1356 struct isl_surf *surf = &((struct iris_resource *) map->staging)->surf;
1357 xfer->stride = isl_surf_get_row_pitch_B(surf);
1358 xfer->layer_stride = isl_surf_get_array_pitch(surf);
1359 }
1360
1361 if (!(xfer->usage & PIPE_TRANSFER_DISCARD_RANGE)) {
1362 iris_copy_region(map->blorp, map->batch, map->staging, 0, extra, 0, 0,
1363 xfer->resource, xfer->level, box);
1364 /* Ensure writes to the staging BO land before we map it below. */
1365 iris_emit_pipe_control_flush(map->batch,
1366 "transfer read: flush before mapping",
1367 PIPE_CONTROL_RENDER_TARGET_FLUSH |
1368 PIPE_CONTROL_CS_STALL);
1369 }
1370
1371 struct iris_bo *staging_bo = iris_resource_bo(map->staging);
1372
1373 if (iris_batch_references(map->batch, staging_bo))
1374 iris_batch_flush(map->batch);
1375
1376 map->ptr =
1377 iris_bo_map(map->dbg, staging_bo, xfer->usage & MAP_FLAGS) + extra;
1378
1379 map->unmap = iris_unmap_copy_region;
1380 }
1381
1382 static void
1383 get_image_offset_el(const struct isl_surf *surf, unsigned level, unsigned z,
1384 unsigned *out_x0_el, unsigned *out_y0_el)
1385 {
1386 if (surf->dim == ISL_SURF_DIM_3D) {
1387 isl_surf_get_image_offset_el(surf, level, 0, z, out_x0_el, out_y0_el);
1388 } else {
1389 isl_surf_get_image_offset_el(surf, level, z, 0, out_x0_el, out_y0_el);
1390 }
1391 }
1392
1393 /**
1394 * This function computes the tile_w (in bytes) and tile_h (in rows) of
1395 * different tiling patterns.
1396 */
1397 static void
1398 iris_resource_get_tile_dims(enum isl_tiling tiling, uint32_t cpp,
1399 uint32_t *tile_w, uint32_t *tile_h)
1400 {
1401 switch (tiling) {
1402 case ISL_TILING_X:
1403 *tile_w = 512;
1404 *tile_h = 8;
1405 break;
1406 case ISL_TILING_Y0:
1407 *tile_w = 128;
1408 *tile_h = 32;
1409 break;
1410 case ISL_TILING_LINEAR:
1411 *tile_w = cpp;
1412 *tile_h = 1;
1413 break;
1414 default:
1415 unreachable("not reached");
1416 }
1417
1418 }
1419
1420 /**
1421 * This function computes masks that may be used to select the bits of the X
1422 * and Y coordinates that indicate the offset within a tile. If the BO is
1423 * untiled, the masks are set to 0.
1424 */
1425 static void
1426 iris_resource_get_tile_masks(enum isl_tiling tiling, uint32_t cpp,
1427 uint32_t *mask_x, uint32_t *mask_y)
1428 {
1429 uint32_t tile_w_bytes, tile_h;
1430
1431 iris_resource_get_tile_dims(tiling, cpp, &tile_w_bytes, &tile_h);
1432
1433 *mask_x = tile_w_bytes / cpp - 1;
1434 *mask_y = tile_h - 1;
1435 }
1436
1437 /**
1438 * Compute the offset (in bytes) from the start of the BO to the given x
1439 * and y coordinate. For tiled BOs, caller must ensure that x and y are
1440 * multiples of the tile size.
1441 */
1442 static uint32_t
1443 iris_resource_get_aligned_offset(const struct iris_resource *res,
1444 uint32_t x, uint32_t y)
1445 {
1446 const struct isl_format_layout *fmtl = isl_format_get_layout(res->surf.format);
1447 unsigned cpp = fmtl->bpb / 8;
1448 uint32_t pitch = res->surf.row_pitch_B;
1449
1450 switch (res->surf.tiling) {
1451 default:
1452 unreachable("not reached");
1453 case ISL_TILING_LINEAR:
1454 return y * pitch + x * cpp;
1455 case ISL_TILING_X:
1456 assert((x % (512 / cpp)) == 0);
1457 assert((y % 8) == 0);
1458 return y * pitch + x / (512 / cpp) * 4096;
1459 case ISL_TILING_Y0:
1460 assert((x % (128 / cpp)) == 0);
1461 assert((y % 32) == 0);
1462 return y * pitch + x / (128 / cpp) * 4096;
1463 }
1464 }
1465
1466 /**
1467 * Rendering with tiled buffers requires that the base address of the buffer
1468 * be aligned to a page boundary. For renderbuffers, and sometimes with
1469 * textures, we may want the surface to point at a texture image level that
1470 * isn't at a page boundary.
1471 *
1472 * This function returns an appropriately-aligned base offset
1473 * according to the tiling restrictions, plus any required x/y offset
1474 * from there.
1475 */
1476 uint32_t
1477 iris_resource_get_tile_offsets(const struct iris_resource *res,
1478 uint32_t level, uint32_t z,
1479 uint32_t *tile_x, uint32_t *tile_y)
1480 {
1481 uint32_t x, y;
1482 uint32_t mask_x, mask_y;
1483
1484 const struct isl_format_layout *fmtl = isl_format_get_layout(res->surf.format);
1485 const unsigned cpp = fmtl->bpb / 8;
1486
1487 iris_resource_get_tile_masks(res->surf.tiling, cpp, &mask_x, &mask_y);
1488 get_image_offset_el(&res->surf, level, z, &x, &y);
1489
1490 *tile_x = x & mask_x;
1491 *tile_y = y & mask_y;
1492
1493 return iris_resource_get_aligned_offset(res, x & ~mask_x, y & ~mask_y);
1494 }
1495
1496 /**
1497 * Get pointer offset into stencil buffer.
1498 *
1499 * The stencil buffer is W tiled. Since the GTT is incapable of W fencing, we
1500 * must decode the tile's layout in software.
1501 *
1502 * See
1503 * - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.2.1 W-Major Tile
1504 * Format.
1505 * - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.3 Tiling Algorithm
1506 *
1507 * Even though the returned offset is always positive, the return type is
1508 * signed due to
1509 * commit e8b1c6d6f55f5be3bef25084fdd8b6127517e137
1510 * mesa: Fix return type of _mesa_get_format_bytes() (#37351)
1511 */
1512 static intptr_t
1513 s8_offset(uint32_t stride, uint32_t x, uint32_t y)
1514 {
1515 uint32_t tile_size = 4096;
1516 uint32_t tile_width = 64;
1517 uint32_t tile_height = 64;
1518 uint32_t row_size = 64 * stride / 2; /* Two rows are interleaved. */
1519
1520 uint32_t tile_x = x / tile_width;
1521 uint32_t tile_y = y / tile_height;
1522
1523 /* The byte's address relative to the tile's base addres. */
1524 uint32_t byte_x = x % tile_width;
1525 uint32_t byte_y = y % tile_height;
1526
1527 uintptr_t u = tile_y * row_size
1528 + tile_x * tile_size
1529 + 512 * (byte_x / 8)
1530 + 64 * (byte_y / 8)
1531 + 32 * ((byte_y / 4) % 2)
1532 + 16 * ((byte_x / 4) % 2)
1533 + 8 * ((byte_y / 2) % 2)
1534 + 4 * ((byte_x / 2) % 2)
1535 + 2 * (byte_y % 2)
1536 + 1 * (byte_x % 2);
1537
1538 return u;
1539 }
1540
1541 static void
1542 iris_unmap_s8(struct iris_transfer *map)
1543 {
1544 struct pipe_transfer *xfer = &map->base;
1545 const struct pipe_box *box = &xfer->box;
1546 struct iris_resource *res = (struct iris_resource *) xfer->resource;
1547 struct isl_surf *surf = &res->surf;
1548
1549 if (xfer->usage & PIPE_TRANSFER_WRITE) {
1550 uint8_t *untiled_s8_map = map->ptr;
1551 uint8_t *tiled_s8_map =
1552 iris_bo_map(map->dbg, res->bo, (xfer->usage | MAP_RAW) & MAP_FLAGS);
1553
1554 for (int s = 0; s < box->depth; s++) {
1555 unsigned x0_el, y0_el;
1556 get_image_offset_el(surf, xfer->level, box->z + s, &x0_el, &y0_el);
1557
1558 for (uint32_t y = 0; y < box->height; y++) {
1559 for (uint32_t x = 0; x < box->width; x++) {
1560 ptrdiff_t offset = s8_offset(surf->row_pitch_B,
1561 x0_el + box->x + x,
1562 y0_el + box->y + y);
1563 tiled_s8_map[offset] =
1564 untiled_s8_map[s * xfer->layer_stride + y * xfer->stride + x];
1565 }
1566 }
1567 }
1568 }
1569
1570 free(map->buffer);
1571 }
1572
1573 static void
1574 iris_map_s8(struct iris_transfer *map)
1575 {
1576 struct pipe_transfer *xfer = &map->base;
1577 const struct pipe_box *box = &xfer->box;
1578 struct iris_resource *res = (struct iris_resource *) xfer->resource;
1579 struct isl_surf *surf = &res->surf;
1580
1581 xfer->stride = surf->row_pitch_B;
1582 xfer->layer_stride = xfer->stride * box->height;
1583
1584 /* The tiling and detiling functions require that the linear buffer has
1585 * a 16-byte alignment (that is, its `x0` is 16-byte aligned). Here we
1586 * over-allocate the linear buffer to get the proper alignment.
1587 */
1588 map->buffer = map->ptr = malloc(xfer->layer_stride * box->depth);
1589 assert(map->buffer);
1590
1591 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
1592 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
1593 * invalidate is set, since we'll be writing the whole rectangle from our
1594 * temporary buffer back out.
1595 */
1596 if (!(xfer->usage & PIPE_TRANSFER_DISCARD_RANGE)) {
1597 uint8_t *untiled_s8_map = map->ptr;
1598 uint8_t *tiled_s8_map =
1599 iris_bo_map(map->dbg, res->bo, (xfer->usage | MAP_RAW) & MAP_FLAGS);
1600
1601 for (int s = 0; s < box->depth; s++) {
1602 unsigned x0_el, y0_el;
1603 get_image_offset_el(surf, xfer->level, box->z + s, &x0_el, &y0_el);
1604
1605 for (uint32_t y = 0; y < box->height; y++) {
1606 for (uint32_t x = 0; x < box->width; x++) {
1607 ptrdiff_t offset = s8_offset(surf->row_pitch_B,
1608 x0_el + box->x + x,
1609 y0_el + box->y + y);
1610 untiled_s8_map[s * xfer->layer_stride + y * xfer->stride + x] =
1611 tiled_s8_map[offset];
1612 }
1613 }
1614 }
1615 }
1616
1617 map->unmap = iris_unmap_s8;
1618 }
1619
1620 /* Compute extent parameters for use with tiled_memcpy functions.
1621 * xs are in units of bytes and ys are in units of strides.
1622 */
1623 static inline void
1624 tile_extents(const struct isl_surf *surf,
1625 const struct pipe_box *box,
1626 unsigned level, int z,
1627 unsigned *x1_B, unsigned *x2_B,
1628 unsigned *y1_el, unsigned *y2_el)
1629 {
1630 const struct isl_format_layout *fmtl = isl_format_get_layout(surf->format);
1631 const unsigned cpp = fmtl->bpb / 8;
1632
1633 assert(box->x % fmtl->bw == 0);
1634 assert(box->y % fmtl->bh == 0);
1635
1636 unsigned x0_el, y0_el;
1637 get_image_offset_el(surf, level, box->z + z, &x0_el, &y0_el);
1638
1639 *x1_B = (box->x / fmtl->bw + x0_el) * cpp;
1640 *y1_el = box->y / fmtl->bh + y0_el;
1641 *x2_B = (DIV_ROUND_UP(box->x + box->width, fmtl->bw) + x0_el) * cpp;
1642 *y2_el = DIV_ROUND_UP(box->y + box->height, fmtl->bh) + y0_el;
1643 }
1644
1645 static void
1646 iris_unmap_tiled_memcpy(struct iris_transfer *map)
1647 {
1648 struct pipe_transfer *xfer = &map->base;
1649 const struct pipe_box *box = &xfer->box;
1650 struct iris_resource *res = (struct iris_resource *) xfer->resource;
1651 struct isl_surf *surf = &res->surf;
1652
1653 const bool has_swizzling = false;
1654
1655 if (xfer->usage & PIPE_TRANSFER_WRITE) {
1656 char *dst =
1657 iris_bo_map(map->dbg, res->bo, (xfer->usage | MAP_RAW) & MAP_FLAGS);
1658
1659 for (int s = 0; s < box->depth; s++) {
1660 unsigned x1, x2, y1, y2;
1661 tile_extents(surf, box, xfer->level, s, &x1, &x2, &y1, &y2);
1662
1663 void *ptr = map->ptr + s * xfer->layer_stride;
1664
1665 isl_memcpy_linear_to_tiled(x1, x2, y1, y2, dst, ptr,
1666 surf->row_pitch_B, xfer->stride,
1667 has_swizzling, surf->tiling, ISL_MEMCPY);
1668 }
1669 }
1670 os_free_aligned(map->buffer);
1671 map->buffer = map->ptr = NULL;
1672 }
1673
1674 static void
1675 iris_map_tiled_memcpy(struct iris_transfer *map)
1676 {
1677 struct pipe_transfer *xfer = &map->base;
1678 const struct pipe_box *box = &xfer->box;
1679 struct iris_resource *res = (struct iris_resource *) xfer->resource;
1680 struct isl_surf *surf = &res->surf;
1681
1682 xfer->stride = ALIGN(surf->row_pitch_B, 16);
1683 xfer->layer_stride = xfer->stride * box->height;
1684
1685 unsigned x1, x2, y1, y2;
1686 tile_extents(surf, box, xfer->level, 0, &x1, &x2, &y1, &y2);
1687
1688 /* The tiling and detiling functions require that the linear buffer has
1689 * a 16-byte alignment (that is, its `x0` is 16-byte aligned). Here we
1690 * over-allocate the linear buffer to get the proper alignment.
1691 */
1692 map->buffer =
1693 os_malloc_aligned(xfer->layer_stride * box->depth, 16);
1694 assert(map->buffer);
1695 map->ptr = (char *)map->buffer + (x1 & 0xf);
1696
1697 const bool has_swizzling = false;
1698
1699 if (!(xfer->usage & PIPE_TRANSFER_DISCARD_RANGE)) {
1700 char *src =
1701 iris_bo_map(map->dbg, res->bo, (xfer->usage | MAP_RAW) & MAP_FLAGS);
1702
1703 for (int s = 0; s < box->depth; s++) {
1704 unsigned x1, x2, y1, y2;
1705 tile_extents(surf, box, xfer->level, s, &x1, &x2, &y1, &y2);
1706
1707 /* Use 's' rather than 'box->z' to rebase the first slice to 0. */
1708 void *ptr = map->ptr + s * xfer->layer_stride;
1709
1710 isl_memcpy_tiled_to_linear(x1, x2, y1, y2, ptr, src, xfer->stride,
1711 surf->row_pitch_B, has_swizzling,
1712 surf->tiling, ISL_MEMCPY_STREAMING_LOAD);
1713 }
1714 }
1715
1716 map->unmap = iris_unmap_tiled_memcpy;
1717 }
1718
1719 static void
1720 iris_map_direct(struct iris_transfer *map)
1721 {
1722 struct pipe_transfer *xfer = &map->base;
1723 struct pipe_box *box = &xfer->box;
1724 struct iris_resource *res = (struct iris_resource *) xfer->resource;
1725
1726 void *ptr = iris_bo_map(map->dbg, res->bo, xfer->usage & MAP_FLAGS);
1727
1728 if (res->base.target == PIPE_BUFFER) {
1729 xfer->stride = 0;
1730 xfer->layer_stride = 0;
1731
1732 map->ptr = ptr + box->x;
1733 } else {
1734 struct isl_surf *surf = &res->surf;
1735 const struct isl_format_layout *fmtl =
1736 isl_format_get_layout(surf->format);
1737 const unsigned cpp = fmtl->bpb / 8;
1738 unsigned x0_el, y0_el;
1739
1740 get_image_offset_el(surf, xfer->level, box->z, &x0_el, &y0_el);
1741
1742 xfer->stride = isl_surf_get_row_pitch_B(surf);
1743 xfer->layer_stride = isl_surf_get_array_pitch(surf);
1744
1745 map->ptr = ptr + (y0_el + box->y) * xfer->stride + (x0_el + box->x) * cpp;
1746 }
1747 }
1748
1749 static bool
1750 can_promote_to_async(const struct iris_resource *res,
1751 const struct pipe_box *box,
1752 enum pipe_transfer_usage usage)
1753 {
1754 /* If we're writing to a section of the buffer that hasn't even been
1755 * initialized with useful data, then we can safely promote this write
1756 * to be unsynchronized. This helps the common pattern of appending data.
1757 */
1758 return res->base.target == PIPE_BUFFER && (usage & PIPE_TRANSFER_WRITE) &&
1759 !(usage & TC_TRANSFER_MAP_NO_INFER_UNSYNCHRONIZED) &&
1760 !util_ranges_intersect(&res->valid_buffer_range, box->x,
1761 box->x + box->width);
1762 }
1763
1764 static void *
1765 iris_transfer_map(struct pipe_context *ctx,
1766 struct pipe_resource *resource,
1767 unsigned level,
1768 enum pipe_transfer_usage usage,
1769 const struct pipe_box *box,
1770 struct pipe_transfer **ptransfer)
1771 {
1772 struct iris_context *ice = (struct iris_context *)ctx;
1773 struct iris_resource *res = (struct iris_resource *)resource;
1774 struct isl_surf *surf = &res->surf;
1775
1776 if (iris_resource_unfinished_aux_import(res))
1777 iris_resource_finish_aux_import(ctx->screen, res);
1778
1779 if (usage & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE) {
1780 /* Replace the backing storage with a fresh buffer for non-async maps */
1781 if (!(usage & (PIPE_TRANSFER_UNSYNCHRONIZED |
1782 TC_TRANSFER_MAP_NO_INVALIDATE)))
1783 iris_invalidate_resource(ctx, resource);
1784
1785 /* If we can discard the whole resource, we can discard the range. */
1786 usage |= PIPE_TRANSFER_DISCARD_RANGE;
1787 }
1788
1789 if (!(usage & PIPE_TRANSFER_UNSYNCHRONIZED) &&
1790 can_promote_to_async(res, box, usage)) {
1791 usage |= PIPE_TRANSFER_UNSYNCHRONIZED;
1792 }
1793
1794 bool need_resolve = false;
1795 bool need_color_resolve = false;
1796
1797 if (resource->target != PIPE_BUFFER) {
1798 bool need_hiz_resolve = iris_resource_level_has_hiz(res, level);
1799 bool need_stencil_resolve = res->aux.usage == ISL_AUX_USAGE_STC_CCS;
1800
1801 need_color_resolve =
1802 (res->aux.usage == ISL_AUX_USAGE_CCS_D ||
1803 res->aux.usage == ISL_AUX_USAGE_CCS_E ||
1804 res->aux.usage == ISL_AUX_USAGE_GEN12_CCS_E) &&
1805 iris_has_color_unresolved(res, level, 1, box->z, box->depth);
1806
1807 need_resolve = need_color_resolve ||
1808 need_hiz_resolve ||
1809 need_stencil_resolve;
1810 }
1811
1812 bool map_would_stall = false;
1813
1814 if (!(usage & PIPE_TRANSFER_UNSYNCHRONIZED)) {
1815 map_would_stall = need_resolve || resource_is_busy(ice, res);
1816
1817 if (map_would_stall && (usage & PIPE_TRANSFER_DONTBLOCK) &&
1818 (usage & PIPE_TRANSFER_MAP_DIRECTLY))
1819 return NULL;
1820 }
1821
1822 if (surf->tiling != ISL_TILING_LINEAR &&
1823 (usage & PIPE_TRANSFER_MAP_DIRECTLY))
1824 return NULL;
1825
1826 struct iris_transfer *map = slab_alloc(&ice->transfer_pool);
1827 struct pipe_transfer *xfer = &map->base;
1828
1829 if (!map)
1830 return NULL;
1831
1832 memset(map, 0, sizeof(*map));
1833 map->dbg = &ice->dbg;
1834
1835 pipe_resource_reference(&xfer->resource, resource);
1836 xfer->level = level;
1837 xfer->usage = usage;
1838 xfer->box = *box;
1839 *ptransfer = xfer;
1840
1841 map->dest_had_defined_contents =
1842 util_ranges_intersect(&res->valid_buffer_range, box->x,
1843 box->x + box->width);
1844
1845 if (usage & PIPE_TRANSFER_WRITE)
1846 util_range_add(&res->base, &res->valid_buffer_range, box->x, box->x + box->width);
1847
1848 /* Avoid using GPU copies for persistent/coherent buffers, as the idea
1849 * there is to access them simultaneously on the CPU & GPU. This also
1850 * avoids trying to use GPU copies for our u_upload_mgr buffers which
1851 * contain state we're constructing for a GPU draw call, which would
1852 * kill us with infinite stack recursion.
1853 */
1854 bool no_gpu = usage & (PIPE_TRANSFER_PERSISTENT |
1855 PIPE_TRANSFER_COHERENT |
1856 PIPE_TRANSFER_MAP_DIRECTLY);
1857
1858 /* GPU copies are not useful for buffer reads. Instead of stalling to
1859 * read from the original buffer, we'd simply copy it to a temporary...
1860 * then stall (a bit longer) to read from that buffer.
1861 *
1862 * Images are less clear-cut. Color resolves are destructive, removing
1863 * the underlying compression, so we'd rather blit the data to a linear
1864 * temporary and map that, to avoid the resolve. (It might be better to
1865 * a tiled temporary and use the tiled_memcpy paths...)
1866 */
1867 if (!(usage & PIPE_TRANSFER_DISCARD_RANGE) && !need_color_resolve)
1868 no_gpu = true;
1869
1870 const struct isl_format_layout *fmtl = isl_format_get_layout(surf->format);
1871 if (fmtl->txc == ISL_TXC_ASTC)
1872 no_gpu = true;
1873
1874 if ((map_would_stall ||
1875 res->aux.usage == ISL_AUX_USAGE_CCS_E ||
1876 res->aux.usage == ISL_AUX_USAGE_GEN12_CCS_E) && !no_gpu) {
1877 /* If we need a synchronous mapping and the resource is busy, or needs
1878 * resolving, we copy to/from a linear temporary buffer using the GPU.
1879 */
1880 map->batch = &ice->batches[IRIS_BATCH_RENDER];
1881 map->blorp = &ice->blorp;
1882 iris_map_copy_region(map);
1883 } else {
1884 /* Otherwise we're free to map on the CPU. */
1885
1886 if (need_resolve) {
1887 iris_resource_access_raw(ice, res, level, box->z, box->depth,
1888 usage & PIPE_TRANSFER_WRITE);
1889 }
1890
1891 if (!(usage & PIPE_TRANSFER_UNSYNCHRONIZED)) {
1892 for (int i = 0; i < IRIS_BATCH_COUNT; i++) {
1893 if (iris_batch_references(&ice->batches[i], res->bo))
1894 iris_batch_flush(&ice->batches[i]);
1895 }
1896 }
1897
1898 if (surf->tiling == ISL_TILING_W) {
1899 /* TODO: Teach iris_map_tiled_memcpy about W-tiling... */
1900 iris_map_s8(map);
1901 } else if (surf->tiling != ISL_TILING_LINEAR) {
1902 iris_map_tiled_memcpy(map);
1903 } else {
1904 iris_map_direct(map);
1905 }
1906 }
1907
1908 return map->ptr;
1909 }
1910
1911 static void
1912 iris_transfer_flush_region(struct pipe_context *ctx,
1913 struct pipe_transfer *xfer,
1914 const struct pipe_box *box)
1915 {
1916 struct iris_context *ice = (struct iris_context *)ctx;
1917 struct iris_resource *res = (struct iris_resource *) xfer->resource;
1918 struct iris_transfer *map = (void *) xfer;
1919
1920 if (map->staging)
1921 iris_flush_staging_region(xfer, box);
1922
1923 uint32_t history_flush = 0;
1924
1925 if (res->base.target == PIPE_BUFFER) {
1926 if (map->staging)
1927 history_flush |= PIPE_CONTROL_RENDER_TARGET_FLUSH;
1928
1929 if (map->dest_had_defined_contents)
1930 history_flush |= iris_flush_bits_for_history(res);
1931
1932 util_range_add(&res->base, &res->valid_buffer_range, box->x, box->x + box->width);
1933 }
1934
1935 if (history_flush & ~PIPE_CONTROL_CS_STALL) {
1936 for (int i = 0; i < IRIS_BATCH_COUNT; i++) {
1937 struct iris_batch *batch = &ice->batches[i];
1938 if (batch->contains_draw || batch->cache.render->entries) {
1939 iris_batch_maybe_flush(batch, 24);
1940 iris_emit_pipe_control_flush(batch,
1941 "cache history: transfer flush",
1942 history_flush);
1943 }
1944 }
1945 }
1946
1947 /* Make sure we flag constants dirty even if there's no need to emit
1948 * any PIPE_CONTROLs to a batch.
1949 */
1950 iris_dirty_for_history(ice, res);
1951 }
1952
1953 static void
1954 iris_transfer_unmap(struct pipe_context *ctx, struct pipe_transfer *xfer)
1955 {
1956 struct iris_context *ice = (struct iris_context *)ctx;
1957 struct iris_transfer *map = (void *) xfer;
1958
1959 if (!(xfer->usage & (PIPE_TRANSFER_FLUSH_EXPLICIT |
1960 PIPE_TRANSFER_COHERENT))) {
1961 struct pipe_box flush_box = {
1962 .x = 0, .y = 0, .z = 0,
1963 .width = xfer->box.width,
1964 .height = xfer->box.height,
1965 .depth = xfer->box.depth,
1966 };
1967 iris_transfer_flush_region(ctx, xfer, &flush_box);
1968 }
1969
1970 if (map->unmap)
1971 map->unmap(map);
1972
1973 pipe_resource_reference(&xfer->resource, NULL);
1974 slab_free(&ice->transfer_pool, map);
1975 }
1976
1977 /**
1978 * The pipe->texture_subdata() driver hook.
1979 *
1980 * Mesa's state tracker takes this path whenever possible, even with
1981 * PIPE_CAP_PREFER_BLIT_BASED_TEXTURE_TRANSFER set.
1982 */
1983 static void
1984 iris_texture_subdata(struct pipe_context *ctx,
1985 struct pipe_resource *resource,
1986 unsigned level,
1987 unsigned usage,
1988 const struct pipe_box *box,
1989 const void *data,
1990 unsigned stride,
1991 unsigned layer_stride)
1992 {
1993 struct iris_context *ice = (struct iris_context *)ctx;
1994 struct iris_resource *res = (struct iris_resource *)resource;
1995 const struct isl_surf *surf = &res->surf;
1996
1997 assert(resource->target != PIPE_BUFFER);
1998
1999 if (iris_resource_unfinished_aux_import(res))
2000 iris_resource_finish_aux_import(ctx->screen, res);
2001
2002 /* Just use the transfer-based path for linear buffers - it will already
2003 * do a direct mapping, or a simple linear staging buffer.
2004 *
2005 * Linear staging buffers appear to be better than tiled ones, too, so
2006 * take that path if we need the GPU to perform color compression, or
2007 * stall-avoidance blits.
2008 */
2009 if (surf->tiling == ISL_TILING_LINEAR ||
2010 (isl_aux_usage_has_ccs(res->aux.usage) &&
2011 res->aux.usage != ISL_AUX_USAGE_CCS_D) ||
2012 resource_is_busy(ice, res)) {
2013 return u_default_texture_subdata(ctx, resource, level, usage, box,
2014 data, stride, layer_stride);
2015 }
2016
2017 /* No state trackers pass any flags other than PIPE_TRANSFER_WRITE */
2018
2019 iris_resource_access_raw(ice, res, level, box->z, box->depth, true);
2020
2021 for (int i = 0; i < IRIS_BATCH_COUNT; i++) {
2022 if (iris_batch_references(&ice->batches[i], res->bo))
2023 iris_batch_flush(&ice->batches[i]);
2024 }
2025
2026 uint8_t *dst = iris_bo_map(&ice->dbg, res->bo, MAP_WRITE | MAP_RAW);
2027
2028 for (int s = 0; s < box->depth; s++) {
2029 const uint8_t *src = data + s * layer_stride;
2030
2031 if (surf->tiling == ISL_TILING_W) {
2032 unsigned x0_el, y0_el;
2033 get_image_offset_el(surf, level, box->z + s, &x0_el, &y0_el);
2034
2035 for (unsigned y = 0; y < box->height; y++) {
2036 for (unsigned x = 0; x < box->width; x++) {
2037 ptrdiff_t offset = s8_offset(surf->row_pitch_B,
2038 x0_el + box->x + x,
2039 y0_el + box->y + y);
2040 dst[offset] = src[y * stride + x];
2041 }
2042 }
2043 } else {
2044 unsigned x1, x2, y1, y2;
2045
2046 tile_extents(surf, box, level, s, &x1, &x2, &y1, &y2);
2047
2048 isl_memcpy_linear_to_tiled(x1, x2, y1, y2,
2049 (void *)dst, (void *)src,
2050 surf->row_pitch_B, stride,
2051 false, surf->tiling, ISL_MEMCPY);
2052 }
2053 }
2054 }
2055
2056 /**
2057 * Mark state dirty that needs to be re-emitted when a resource is written.
2058 */
2059 void
2060 iris_dirty_for_history(struct iris_context *ice,
2061 struct iris_resource *res)
2062 {
2063 uint64_t stage_dirty = 0ull;
2064
2065 if (res->bind_history & PIPE_BIND_CONSTANT_BUFFER) {
2066 stage_dirty |= ((uint64_t)res->bind_stages)
2067 << IRIS_SHIFT_FOR_STAGE_DIRTY_CONSTANTS;
2068 }
2069
2070 ice->state.stage_dirty |= stage_dirty;
2071 }
2072
2073 /**
2074 * Produce a set of PIPE_CONTROL bits which ensure data written to a
2075 * resource becomes visible, and any stale read cache data is invalidated.
2076 */
2077 uint32_t
2078 iris_flush_bits_for_history(struct iris_resource *res)
2079 {
2080 uint32_t flush = PIPE_CONTROL_CS_STALL;
2081
2082 if (res->bind_history & PIPE_BIND_CONSTANT_BUFFER) {
2083 flush |= PIPE_CONTROL_CONST_CACHE_INVALIDATE |
2084 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
2085 }
2086
2087 if (res->bind_history & PIPE_BIND_SAMPLER_VIEW)
2088 flush |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
2089
2090 if (res->bind_history & (PIPE_BIND_VERTEX_BUFFER | PIPE_BIND_INDEX_BUFFER))
2091 flush |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
2092
2093 if (res->bind_history & (PIPE_BIND_SHADER_BUFFER | PIPE_BIND_SHADER_IMAGE))
2094 flush |= PIPE_CONTROL_DATA_CACHE_FLUSH;
2095
2096 return flush;
2097 }
2098
2099 void
2100 iris_flush_and_dirty_for_history(struct iris_context *ice,
2101 struct iris_batch *batch,
2102 struct iris_resource *res,
2103 uint32_t extra_flags,
2104 const char *reason)
2105 {
2106 if (res->base.target != PIPE_BUFFER)
2107 return;
2108
2109 uint32_t flush = iris_flush_bits_for_history(res) | extra_flags;
2110
2111 iris_emit_pipe_control_flush(batch, reason, flush);
2112
2113 iris_dirty_for_history(ice, res);
2114 }
2115
2116 bool
2117 iris_resource_set_clear_color(struct iris_context *ice,
2118 struct iris_resource *res,
2119 union isl_color_value color)
2120 {
2121 if (memcmp(&res->aux.clear_color, &color, sizeof(color)) != 0) {
2122 res->aux.clear_color = color;
2123 return true;
2124 }
2125
2126 return false;
2127 }
2128
2129 union isl_color_value
2130 iris_resource_get_clear_color(const struct iris_resource *res,
2131 struct iris_bo **clear_color_bo,
2132 uint64_t *clear_color_offset)
2133 {
2134 assert(res->aux.bo);
2135
2136 if (clear_color_bo)
2137 *clear_color_bo = res->aux.clear_color_bo;
2138 if (clear_color_offset)
2139 *clear_color_offset = res->aux.clear_color_offset;
2140 return res->aux.clear_color;
2141 }
2142
2143 static enum pipe_format
2144 iris_resource_get_internal_format(struct pipe_resource *p_res)
2145 {
2146 struct iris_resource *res = (void *) p_res;
2147 return res->internal_format;
2148 }
2149
2150 static const struct u_transfer_vtbl transfer_vtbl = {
2151 .resource_create = iris_resource_create,
2152 .resource_destroy = iris_resource_destroy,
2153 .transfer_map = iris_transfer_map,
2154 .transfer_unmap = iris_transfer_unmap,
2155 .transfer_flush_region = iris_transfer_flush_region,
2156 .get_internal_format = iris_resource_get_internal_format,
2157 .set_stencil = iris_resource_set_separate_stencil,
2158 .get_stencil = iris_resource_get_separate_stencil,
2159 };
2160
2161 void
2162 iris_init_screen_resource_functions(struct pipe_screen *pscreen)
2163 {
2164 pscreen->query_dmabuf_modifiers = iris_query_dmabuf_modifiers;
2165 pscreen->resource_create_with_modifiers =
2166 iris_resource_create_with_modifiers;
2167 pscreen->resource_create = u_transfer_helper_resource_create;
2168 pscreen->resource_from_user_memory = iris_resource_from_user_memory;
2169 pscreen->resource_from_handle = iris_resource_from_handle;
2170 pscreen->resource_get_handle = iris_resource_get_handle;
2171 pscreen->resource_get_param = iris_resource_get_param;
2172 pscreen->resource_destroy = u_transfer_helper_resource_destroy;
2173 pscreen->transfer_helper =
2174 u_transfer_helper_create(&transfer_vtbl, true, true, false, true);
2175 }
2176
2177 void
2178 iris_init_resource_functions(struct pipe_context *ctx)
2179 {
2180 ctx->flush_resource = iris_flush_resource;
2181 ctx->invalidate_resource = iris_invalidate_resource;
2182 ctx->transfer_map = u_transfer_helper_transfer_map;
2183 ctx->transfer_flush_region = u_transfer_helper_transfer_flush_region;
2184 ctx->transfer_unmap = u_transfer_helper_transfer_unmap;
2185 ctx->buffer_subdata = u_default_buffer_subdata;
2186 ctx->texture_subdata = iris_texture_subdata;
2187 }