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