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