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