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