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