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