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