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