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